Discussion

How to control the GPIO on the SBC boards

1 627
Edited by Acousticlv at Feb 24, 2016 01:13

At the very start you can simply check the under #Appendix: 40 Pins GPIO Mapping Table for 96boards HiKey to obtain the correct sys Pin number:
http://wiki.lemaker.org/HiKey(LeMaker_version):How_to_control_the_GPIO_on_the_SBC_boards#Appendix:_40_Pins_GPIO_Mapping_Table_for_96boards_HiKey
To access a GPIO pin such as GPIO13_6(400+6 = 406) you first need to export it with.

Now we have two ways to control the GPIO on the SBC boards
i sysfs
ii 96BoardsGPIO(you have to install it first)


i sysfs

To access a GPIO pin such as GPIO13_6(400+6 = 406) you first need to export it with:
echo XX > /sys/class/gpio/export


With XX being the numbering index of the desired pin, here XX = 406. If succeed, you would find the sys file system node:
ls
#! /sys/class/gpio/gpioXX is generated


To set a GPIO pin as output you have to change the input/output direction with
echo out > /sys/class/gpio/gpioXX/direction


To set a GPIO pin as input you have to change the input/output direction with
echo in > /sys/class/gpio/gpioXX/direction


You can read a GPIO pin with /sys/class/gpio/gpioXX/value using
cat /sys/class/gpio/gpioXX/value


When the direction is set to out, you can write 1 or 0 to a GPIO pin with /sys/class/gpio/gpioXX/value using
echo 1 /sys/class/gpio/gpioXX/value



ii 96BoardsGPIO


Install libsoc 3
libsoc is a C shared library that has been modified to use pin mapping for 96Boards, it is extensive and very useful accessing GPIO, I2C and SPI functions on Linux. As of this writing it only support C and C++, though Python and Java support is being added by Linaro.

$ sudo apt-get update
$ sudo apt-get install build-essential autoconf automake libtool libsocdev

libsoc is a cross platform library and will work on any of the 96Boards, and now handles the mapping between LS connector pins and Linux GPIO pin numbers. libsoc is a bit complex and if you want a more simplistic, easier set of calls to access to the 96Boards GPIO we recommend installing and using the 96BoardsGPIO library in conjunction with the libsoc library. The 96BoardsGPIO shared library has been rewritten to depend on libsoc but presents simpler calls similar to the Arduino digitalRead() and digitalWrite() type of calls.


Install 96BoardsGPIO 3
To build and install 96BoardsGPIO from source, use the following commands:

git clone https://github.com/96Boards/96BoardsGPIO.git
cd 96BoardsGPIO
./autogen.sh
./configure
make
sudo make install


GPIO read example

cd /sys/class/gpio
echo XX | sudo tee export #XX is the Pin number mentioned at the very beginning,export it.
cd gpioXX
cat value

GPIO write example

cd /sys/class/gpio
echo XX| sudo tee export
cd gpioXX
echo out| sudo tee direction #To set a GPIO pin as output
echo 1 |sudo tee value #When the direction is set to out, you can write 1 or 0
echo 0 |sudo tee value
You have to log in before you can reply Login | Sign Up

Points Rules