GPIO
The phyCORE-i.MX7 SOM brings out a selection of GPIOs to the Carrier Board. Many of these signals have been made accessible on the PEB-D-RPI Expansion Board included with your development kit. This guide walks through the basic steps of toggling and reading the state of these IO interfaces.
In order to match signal names in the phyCORE-i.MX7 hardware to the device name in sysfs, use the following equation:
Hardware Signal = GPIO(Bank)_(num) sysfs GPIO Device = (Bank -1)*32 + num |
---|
For example:
Hardware Signal = GPIO2_10
sysfs GPIO Device = (2 - 1)*32 + 10 = 42
GPIO2_10 is represented as GPIO42 in sysfs
User LED (D13)
The green user-configurable LED (D13) is located on the Carrier Board. This LED is ON by default when the CPU is running, but we can export and toggle it for general use.
To use the D13 LED we will first export GPIO42:
Target (Linux)
echo 42 > /sys/class/gpio/export
CODESet GPIO42 as an output:
Target (Linux)
echo out > /sys/class/gpio/gpio42/direction
CODETurn the LED ON:
Target (Linux)
echo 1 > /sys/class/gpio/gpio42/value
CODETurn the LED OFF:
Target (Linux)
echo 0 > /sys/class/gpio/gpio42/value
CODE
PED-D-RPI Expansion Board LEDs
Use the following command to turn ON D1:
Target (Linux)
echo 1 > /sys/class/leds/pebdrpi_led_1/brightness
CODEUse the following command to turn ON D2:
Target (Linux)
echo 1 > /sys/class/leds/pebdrpi_led_2/brightness
CODESimilarly, writing a zero to the LED's brightness attribute will turn them OFF:
Target (Linux)
echo 0 > /sys/class/leds/pebdrpi_led_1/brightness echo 0 > /sys/class/leds/pebdrpi_led_2/brightness
CODE
Get fancy!
Try making use of the pre-configured heartbeat rhythm!
Target (Linux)
echo "heartbeat" > /sys/class/leds/pebdrpi_led_1/trigger
CODEI aorta tell you how to turn that off, try setting the trigger back to GPIO:
Target (Linux)
echo "gpio" > /sys/class/leds/pebdrpi_led_1/trigger
CODE
PED-D-RPI Expansion Board Buttons
- Use the following command to read the state of the buttons S1 and S2 on the PED-D-RPI Expansion Board:
Target (Linux)
cat /sys/kernel/debug/gpio | grep "pebdrpi_button"
- You should see the current state of the GPIO pins connected to the active-low buttons displayed on your console.
- Use the above command again while pressing and holding either of the buttons and notice that the state is now pulled high.
Use Both!
Open a text editor to write a script:
Target (Linux)
vi ~/buttonLED.sh
CODEEnter the following and save the file:
Vi Text Editor
#!/bin/bash var=0 state="hi" while true; do firstIF=0 button=$(grep "pebdrpi_button_1" /sys/kernel/debug/gpio) if [[ "$button" == *$state* ]] && [ $var == 0 ]; then echo 1 > /sys/class/leds/pebdrpi_led_1/brightness var=1 firstIF=1 sleep 0.5 fi if [[ "$button" == *$state* ]] && [ $var == 1 ] && [ $firstIF == 0 ]; then echo 0 > /sys/class/leds/pebdrpi_led_1/brightness var=0 sleep 0.5 fi done
CODEThe vi text editor begins in "Command Mode" and you must first hit the 'i' key in order to enter "Insert Mode". Using the arrow keys to navigate, make the necessary changes and then hit ESC to go back to "Command mode". Now enter ":wq" to write the file and quit.
Pro Tip: Use the right click on your mouse to paste. This will only work if you are in "Insert Mode" first.
Change the permissions in order to execute the script:
Target (Linux)
chmod +x ~/buttonLED.sh
CODENow run the script in the background:
Target (Linux)
~/buttonLED.sh &
CODE- Now try pressing button S1 and see what the LED does.
To end the background process, enter the following:
Target (Linux)
killall buttonLED.sh
CODE