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
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:
echo 42 > /sys/class/gpio/exportCODE
Set GPIO42 as an output:
echo out > /sys/class/gpio/gpio42/directionCODE
Turn the LED ON:
echo 1 > /sys/class/gpio/gpio42/valueCODE
Turn the LED OFF:
echo 0 > /sys/class/gpio/gpio42/valueCODE
PED-D-RPI Expansion Board LEDs
Use the following command to turn ON D1:
echo 1 > /sys/class/leds/pebdrpi_led_1/brightnessCODE
Use the following command to turn ON D2:
echo 1 > /sys/class/leds/pebdrpi_led_2/brightnessCODE
Similarly, writing a zero to the LED's brightness attribute will turn them OFF:
echo 0 > /sys/class/leds/pebdrpi_led_1/brightness echo 0 > /sys/class/leds/pebdrpi_led_2/brightnessCODE
Try making use of the pre-configured heartbeat rhythm!
echo "heartbeat" > /sys/class/leds/pebdrpi_led_1/triggerCODE
I aorta tell you how to turn that off, try setting the trigger back to GPIO:
echo "gpio" > /sys/class/leds/pebdrpi_led_1/triggerCODE
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:
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.
Open a text editor to write a script:
Enter 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 doneCODE
The 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:
chmod +x ~/buttonLED.shCODE
Now run the script in the background:
- Now try pressing button S1 and see what the LED does.
To end the background process, enter the following: