Managing GPIO
In Wiren Board a part of GPIOs is output to terminal blocks,a part - to connectors on the expansion modules, the other part is used for utility purposes. GPIO are also used to control the switching transistors for low-voltage load.
The correspondence between GPIO numbers in Linux and between their location and function can be found in the table WB SH 3.5: List of GPIO or WB rev. 2.8: List of GPIO.
In Wiren Board rev. 2.8 8 GPIO are placed on a special connector and connected via MCP23S08 and temporarily do not work with Linux API standard. How to work with them: see page MCP23S08.
All GPIOs (as well as other ports of Wiren Board controllers) work with 3.3V voltage.Do not connect signals with voltage over 3.3V directly to the GPIO! In case you want to connect devices operating at a higher voltage, you must use matching circuits or connect(for 5V) via a resistor of 20 kOhm or more
On some GPIOs (particularly at the 3 GPIOs, launched into terminals in Wiren Board rev. 2.8) can be programmatically set to lift 47K + 3.3V. See. Pin_pull-up.
sysfs interface in Linux
GPIO under Linux are supported through sysfs-interface.
To work through a certain GPIO sysfs it must be exported:
Here and further N - number of gpio
echo N > /sys/class/gpio/export
Exported gpio appear in the catalog /sys/class/gpio :
root@wirenboard:~# ls -1 /sys/class/gpio/ export gpio32 gpiochip0 gpiochip120 gpiochip32 gpiochip64 unexport
The directory /sys/class/gpioN are now files to work with GPIO (where N - No. of GPIO, as was said earlier):
root@wirenboard:~# ls -1 /sys/class/gpio/gpioN/ active_low device direction edge power subsystem uevent value
Setting the direction of GPIO (input / output) is made by writing to the file direction </ b>
echo in > /sys/class/gpio/gpioN/direction #set the GPIO to input echo out > /sys/class/gpio/gpioN/direction # set the GPIO to output
Reading and Setting the GPIO is made using the file value.
Reading:
echo in > /sys/class/gpio/gpioN/direction # Set GPIO a number N to input cat /sys/class/gpio/gpioN/value # returns 1 or 0
Record:
echo out > /sys/class/gpio/gpioN/direction # set GPIO No. N to output echo 0 > /sys/class/gpio/gpioN/value # set logic 0 (low voltage) on the GPIO No. N echo 1 > /sys/class/gpio/gpioN/value # set logical 1 (high voltage) to GPIO No. N
Sysfs interface and interrupt
Via sysfs interface, you can request a change of state interrupt processor.
Setting the interrupt is done by writing the values in the file "edge". Values can be:
- "none" - disable interrupt
- "rising" - enable interrupt on falling edge
- "falling" - enable interrupt on the rising edge
- "both" - enable interrupt on both edges.
Example:
root@wirenboard:~# echo 3 > /sys/class/gpio/export # export GPIO No. 3 (TB10 in WB3.3) root@wirenboard:~# cat /sys/class/gpio/gpio3/edge # check the status of the interrupt none root@wirenboard:~# echo falling > /sys/class/gpio/gpio3/edge # set interrupt falling edge root@wirenboard:~# cat /proc/interrupts | grep gpiolib # interrupt appears in the list. 26 - internal interrupt, 0 - the number of events 26: 0 gpio-mxs 3 gpiolib root@wirenboard:~# cat /proc/interrupts | grep gpiolib # After several events, 76 - the number of events 26: 76 gpio-mxs 3 gpiolib
Interrupts can be caught from userspace using the system call poll () and select () on file value. Example of look here [1]
see also elinux.org
Direct access by processor memory
This method is strongly NOT recommended for use without sufficient reason. To work in C / C ++ is to use the files to work through sysfs, as described in the previous section.
Driving GPIO can use the direct access to the processor registers, bypassing the Linux interface/dev/mem. At the same time, compared to the work via sysfs minimization of costs. This method can be used, if you need a very fast access to GPIO, such bitbang protocols or PWM. It should be borne in mind that the process scheduler can still contribute to the work program of significant delays. It is recommended to make time-critical tasks in the kernel.
GPIO and Device Tree
Pointing to the GPIO Device Tree you need to configure the GPIO to work in a mode of software SPI, I2C, GPIO to be used as the source of the interrupt, etc. For example on a pin 10 @ UEXT1 (CS) and pins 5 @ UEXT2 (SCL), 6 @ UEXT2 (SDA), 10 @ UEXT2 (CS) derived processor GPIO lines. They can be configured to use such as a chip-select for as SPI or I2C.
GPIO processor and peripherals are divided into banks (gpio chip). GPIO processor split into 3 banks for 32 GPIO: gpio0, gpio1, gpio2. Addressing GPIO in Device Tree comes to the number of the bank and the number of GPIO * in * the bank.
Example 1
We define signal 6 @ UEXT2 (SDA) as the source of the interrupt driver mrf24j40. According to the table List of GPIO, signal corresponds to 53 GPIO of processor. 53 belongs to the second bank gpio (32 to 63). GPIO number inside the bank 53-32 = 21:
6lowpan@0 { compatible = "microchip,mrf24j40"; spi-max-frequency = <100000>; reg = <6>; interrupt-parent = <&gpio1>; interrupts = <21 0>; };