Sunday, May 27, 2012

The Strange World of Beaglebone GPIO

In the last post I mentioned that I was going to investigate a workaround for the problem of a non-responsive GSM modem, based on unplugging, then replugging it in, simulated by switching the +5V USB line connected to pin 1 of the USN connector. I modified a USB cable, breaking the red +5V line, and connecting and disconnecting it to simulate plugging and unplugging the GSM mode.

The results were as expected: when disconnected, the software disabled the ttyHS* ports, and reconnecting the line caused the software to reestablish them. This demonstrated we can reset the modem under program control. All that remains is to design a circuit that switched the +5V USB line under program control, using spare GPIO bits. And, of course writing software to sense when the modem is misbehaving, and toggle the +5V control bit to simulate the unplug/plug operation.

As many people have noted, managing the GPIO bits is a little complicated. Nathan Dumont has provided a good overview of the process in his blog. I followed his general instructions and wanted to quickly record what I did here.  These are some commands entered from the bash command line to control gpio1_7, which is on pin 4 of expansion connector P8. The name format gpiox_y is how the system codes bit y of gpio bank x.

Check that the multiplexer has gpio1_7 (which is called gpmc_ad7 in the mux directory and tables) set for mode7, which is a GPIO bit:

root@beaglebone:~/work# cat /sys/kernel/debug/omap_mux/gpmc_ad7
name: gpmc_ad7.gpio1_7 (0x44e1081c/0x81c = 0x0007), b NA, t NA
mode: OMAP_PIN_OUTPUT | OMAP_MUX_MODE7
signals: gpmc_ad7 | mmc1_dat7 | NA | NA | NA | NA | NA | gpio1_7





It's set right, so no need to change it.

Now, to be able to control the pin we need to write the number representing the gpio bit, computed by taking the gpio bank (1), multiplying by 32 (bits per bank) and adding the bit number (7), giving 2*32+7 = 39, to the export 'file' in  /sys/class/gpio:

root@beaglebone:~/work# echo 39 > /sys/class/gpio/export'



We want to use it as an output, so write 'out' to the 'file /sys/class/gpio/gpio39/direction:

root@beaglebone:~/work# echo out >/sys/class/gpio/gpio39/direction


Set the bit to 1:

root@beaglebone:~/work# echo 1 >/sys/class/gpio/gpio39/value

Then 0:

 root@beaglebone:~/work# echo 0 >/sys/class/gpio/gpio39/value

And when done, unexport so other programs can use it:

root@beaglebone:~/work# echo 39 > /sys/class/gpio/unexport



No comments:

Post a Comment