Friday, September 24, 2010

testing the GPIO pins

We'll have to interface to some external devices using the GPIO pins. The expansion header has a couple dozen pins, most of which can be configured as GPIO's. There's also 3 pins that can be configured as PWMs (which can be controlled by hardware timers), and an I2C.

I verified that pin 3 can function as an output (driving either +1.8V high, or 0V low) using the following on the console:

#first, export the pin so the file shows up in /sys/class/gpio
sudo sh -c "echo 139 > /sys/class/gpio/export"

#now make an output
sudo sh -c "echo out > /sys/class/gpio/gpio139/direction"

#now we can toggle the pin high
sudo sh -c "echo high > /sys/class/gpio/gpio139/direction"

#or low
sudo sh -c "echo low > /sys/class/gpio/gpio139/direction"


We can also verify that the pins functions correctly as an input:

#change direction to an input
sudo sh -c "echo in > /sys/class/gpio/gpio139/direction"

#short pin3 to pin1 (1.8V) to verify it's high
cat /sys/class/gpio/gpio139/value
1

#short pin3 to pin 27 (gnd) to verify it's low
cat /sys/class/gpio/gpio139/value
0

Finally, note that the 2 user LED's (USR0 and USR1) are already configured by the default Ubuntu kernel, and can be turned on and off like so:

sudo sh -c "echo 1 > /sys/devices/platform/leds-gpio/leds/beagleboard\:\:usr0/brightness"
sudo sh -c "echo 0 > /sys/devices/platform/leds-gpio/leds/beagleboard\:\:usr0/brightness"

In summary ,this verifies that the kernel gpio module is working and we can properly manipulate the GPIO's. Note this can also be done programatically within a C program using fopen, fread, fwrite, etc.

No comments:

Post a Comment