Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Programming the BeagleBone

You're reading from  Programming the BeagleBone

Product type Book
Published in Jan 2016
Publisher
ISBN-13 9781784390013
Pages 180 pages
Edition 1st Edition
Languages

Table of Contents (21) Chapters

Programming the BeagleBone
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface
Cloud9 IDE Blinking Onboard LEDs Blinking External LEDs Controlling LED Using a Push Button Reading from Analog Sensors PWM – Writing Analog Information Internet of Things with BeagleBone Physical Computing in Python UART, I2C, and SPI Programming Internet of Things using Python GPIO Control in Bash BeagleBone Capes
Pinmux and the Device Tree Index

Appendix A. GPIO Control in Bash

We used JavaScript and the Python language in this book. You do not always need to write complex programs to control GPIO. You can access GPIO using simple BASH shell commands. You will need to use the sysfs interface for this; sysfs is a virtual filesystem created by kernel to export information and control of subsystems and hardware devices. So, if you modify the control files in this filesystem, you change the actual hardware parameters.

Check the BeagleBone GPIO map diagram that we studied in Chapter 3, Blinking External LED s. It has the mapping of pin numbers with GPIO names. The P8_10 pin is the GPIO pin with number 68. It is given as gpio2[4] in the BeagleBone System Reference Manual. This means that P8_10 is the fourth pin in the gpio2 bank. The BeagleBone processor has four banks of 32 GPIO pins each—gpio0, gpio1, gpio2, and gpio3. Numbers 0-32 are given to gpio0 and 33-64 numbers are given to the gpio1 bank. So, the fourth pin in the gpio2 bank is actually the 68th GPIO pin. Thus, P8_10 gets converted to GPIO number 68. This conversion works for all the GPIO pins. We will need this number when dealing with GPIO on BASH.

Attach an external LED to P8_10 as we did in Chapter 3 for the blinking exercise. Write this shell program in Cloud9 or use the vi editor. Save it as blink.sh and run it from the Cloud9 IDE or in the shell using the sudo chmod 755 blink.sh; sudo ./blink.sh command:

#!/bin/sh
echo 68 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio68/direction
while(true)
do
    echo 1 > /sys/class/gpio/gpio68/value
    sleep 1
    echo 0 > /sys/class/gpio/gpio68/value
    sleep 1
done

Explanation


First, we wanted to use /bin/sh to interpret this file. By default, the sysfs files for particular pins are not created until we export them. We echoed 68 to the /sys/class/gpio/export file. This is a request to kernel to export the control of GPIO numbered 68 to the user space. After this command, the /sys/class/gpio68 folder gets created with the control files in it. Now, we can interact with these control files that will actually change the pin state. As we have attached an output LED to the P8_10 pin, we set the direction to out. As regular GPIO steps, we have to set the direction of P8_10 as output. Then, we can turn it on/off by writing 0/1 in the special sysfs file, /sys/class/gpio/gpio68/value. We created an infinite loop using while(true), and in the loop, we write 0 and 1 to the sysfs file after a second. When we write 1 on this file, the LED attached to P8_10 will turn on. After a second, we write 0 on this file, which turns the LED off. You can get more information in the kernel documentation of the gpio sysfs interface at https://www.kernel.org/doc/Documentation/gpio/sysfs.txt. This way of accessing GPIO is possible on any Linux system (including Android).

Troubleshooting


  • Change the first line from #!/bin/sh" to "#!/bin/sh -x. This will print the line from the shell script before executing it.

  • You can get information about the current configuration of all the working GPIOs from the following file:

    sudo cat /sys/kernel/debug/gpio
    
lock icon The rest of the chapter is locked
You have been reading a chapter from
Programming the BeagleBone
Published in: Jan 2016 Publisher: ISBN-13: 9781784390013
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}