Reading gpio in linux

How does Linux perform GPIO reads and writes?

Abstract: This article introduces GPIO read and write, introduces the basic principle, and the performance of different read and write methods.

This article is shared by huawei Cloud community «Linux BASED on SYSFS GPIO read and write operations», by a small tree X.

preface

Recently I came into contact with THE DEVELOPMENT of GPIO in Linux system. Here I will make a summary and share with you. This article will introduce GPIO read and write, introduce the basic principles, and the performance of different read and write methods.

GPIO SYSFS interface

In Linux, the most common way to read and write GPIO is to use the GPIO sysfs interface, /sys/class/gpio export, unexport, gPIO /direction, gpio/value (with actual pin numbers instead of ).

First declare GPIO port, such as GPIO258 :(this command requires root permission)

echo 258 /sys/class/gpio/export Copy the code

Then at /sys/class/ gPIO, a new directory named gPIo258 is generated

For example: GPIO258, N corresponds to 258; Go to the directory CD /sys/class/ gPIO/gPIo258

The following commands can be seen: Active_low, Device, Direction, EDGE, power, Subsystem, uEvent, value

The most common one is the value file, which stores the value of GPIO. The range is 0 or 1. We can read and write to it directly to achieve the effect of reading and writing GPIO.

2. Define GPIO as input

For example, to define GPIO258 as the input, run the following command :(this command requires root permission)

echo in /sys/class/gpio/gpio258/direction Copy the code

Define GPIO as output

For example, to define GPIO258 as output, run the following command :(this command requires root permission)

echo out /sys/class/gpio/gpio258/direction Copy the code

Read the value of GPIO

/sys/class/ gPIO/gPIo258 / gPIo258 /sys/class/ gPIO/gPIo258 / gPIo258 /sys/class/ GPIO/gPIo258 /sys/class/ GPIO/gPIo258 /sys/class/ GPIO/gPIo258

You can use cat to check the value of GPIO, such as GPIO258:

cat /sys/class/gpio/gpio258/value Copy the code

Value is just a file that can be read in other ways; Write a Python program that reads the value of GPIO:

Define a function to read the value of GPIO258. def read_258(): with open('/sys/class/gpio/gpio258/value', 'r') as f: Io_258 = int(f.read()) print("read_258:%d"%io_258)Copy the code

Write the value of GPIO

/sys/class/ gPIO/gPIo258 /gpio258 /sys/class/ gPIO/gPIo258 / gPIo258 /sys/class/ gPIO/gPIo258 /sys/class/ gPIO/gPIo258

We can write GPIO values with EHCO, such as GPIO258 values:

echo 1 /sys/class/gpio/gpio258/value # output logic 1 level echo 0 /sys/class/gpio/gpio258/value # output logic 0 level Copy the code

Write a Python program that writes the value of GPIO:

Define a function to write the value of GPIO258. def write_258(io_str): with open('/sys/class/gpio/gpio258/value', 'w+') as f: F. write(io_str) print("write_258:%s"% s")Copy the code

After the test, the program to write a operation, time-consuming about 0.6ms; Ehco mode is relatively long, about 10ms.

Читайте также:  Командная строка содержимое папки linux

Set GPIO as input and read IO value

Method 1: Pure shell command

# set GPIO20 to input echo 20 / sys/class/gpio/export echo in / sys/class/gpio/GPIO20 / direction # read IO cat /sys/class/gpio/gpio20/valueCopy the code

Method 2: Shell command + Python program (more efficient)

# set GPIO20 to input echo 20 / sys/class/gpio/export echo in / sys/class/gpio/GPIO20 / directionCopy the code
Define a function to read the value of GPIO258. def read_20(): with open('/sys/class/gpio/gpio20/value', 'r') as f: Io_20 = int (f.r ead ()) print (" read_20: % d % io_20) # calling function read_20 ()Copy the code

Set GPIO as output and read/write IO values

Method 1: Pure shell command

# set GPIO40 to output echo 40 / sys/class/gpio/export echo out / sys/class/gpio GPIO40 / direction # write IO values, High level echo 1 / sys/class/gpio gpio40 / value # write IO value, low level echo 0 / sys/class/gpio/gpio40 / valueCopy the code

Method 2: Shell command + Python program (more efficient)

# set GPIO40 to output echo 40 / sys/class/gpio/export echo out / sys/class/gpio/GPIO40 / directionCopy the code
import time def read_40(): with open('/sys/class/gpio/gpio40/value', 'r') as f: io_40 = int(f.read()) print("read_40:%d"%io_40) def write_40(io_str): with open('/sys/class/gpio/gpio40/value', 'w') as f: f.write(io_str) print("write_40:%s"%(io_str)) start = time.time() read_40() write_40("1") read_40() end = time.time() Print (" time to read/write I/O ", end-start)Copy the code

Effect: time-consuming 0.6ms.

Источник

Reading gpio in linux

NAME

gpio - Command-line access to Raspberry Pi's GPIO

SYNOPSIS

gpio -v gpio [ -g | -1 ] mode/read/write/aread/awrite/wb/pwm/pwnTone/clock/toggle/blink . gpio [ -x extension:params ] mode/read/write/aread/awrite/pwm/toggle/blink . gpio [ -p ] read/write/toggle/blink . gpio [ -p ] pwnTone pin frequency . gpio readall gpio unexportall/exports gpio export/edge/unexport . gpio wfi . gpio drive group value gpio usbp high | low gpio pwm-bal/pwm-ms gpio pwmr range gpio load i2c/spi . gpio gbr channel gpio gbw channel value

DESCRIPTION

GPIO is a swiss army knife of a command line tool to allow the user easy access to the GPIO pins on the Raspberry Pi and the SPI A/D and D/A converters on the Gertboard. It's designed for simple testing and diagnostic purposes, but can be used in shell scripts for general if somewhat slow control of the GPIO pins. It can also control the IO's on the PiFace IO board and load the SPI and I2C kernel modules if required. Additionally, it can be used to set the exports in the /sys/class/gpio system directory to allow subsequent programs to use the /sys/class/gpio interface without needing to be run as root.

OPTIONS

-v Output the current version including the board revision of the Raspberry Pi. -g Use the BCM_GPIO pins numbers rather than wiringPi pin numbers. Note: The BCM_GPIO pin numbers are always used with the export and edge commands. -1 Use the physical pin numbers rather than wiringPi pin numbers. Note: that this applies to the P1 connector only. It is not possible to use pins on the Revision 2 P5 connector this way, and as with -g the BCM_GPIO pin numbers are always used with the export and edge commands. -x extension This causes the named extension to be initialised. Extensions comprise of a name (e.g. mcp23017) followed by a colon, then the pin-base, then more optional parameters depending on the extension type. See the web page on http://wiringpi.com/the-gpio-utility/ -p Use the PiFace interface board and its corresponding pin numbers. The PiFace will always appear at pin number 200 in the gpio command. You can assign any pin numbers you like in your own programs though. read Read the digital value of the given pin and print 0 or 1 to represent the respective logic levels. write  Write the given value (0 or 1) to the pin. You need to set the pin to output mode first. toggle Changes the state of a GPIO pin; 0 to 1, or 1 to 0. Note unlike the blink command, the pin must be in output mode first. blink Blinks the given pin on/off. Press Control-C to exit. Note: This command explicitly sets the pin to output mode. aread Read the analog value of the given pin. This needs to be used in conjunction with a -x flag to add in an extension that handles analog inputs. e.g. gpio -x mcp3002:200:0 aread 200 will read the first analog input on an mcp3002 SPI ADC chip. awrite  Write the analog value to the given pin. This needs to be used in conjunction with a -x flag to add in an extension that handles analog inputs. e.g. gpio -x mcp4802:200:0 awrite 200 128 will write the value 128 to the first DAC port on an mcp4802 chip on the Pi's SPI bus 0. wb Write the given byte to the 8 main GPIO pins. You can prefix it with 0x to specify a hexadecimal number. You need to set pins to output mode first. readall Output a table of all GPIO pins values. The values represent the actual values read if the pin is in input mode, or the last value written if the pin is in output mode. The readall command is usable with an extension module (via the -x parameter), but it's unable to determine pin modes or states, so will perform both a digital and analog read on each pin in-turn. pwm  Write a PWM value (0-1023) to the given pin. The pin needs to be put into PWM mode first. clock  Set the output frequency on the given pin. The pin needs to be put into clock mode first. mode  Set a pin into input, output or pwm mode. Can also use the literals up, down or tri to set the internal pull-up, pull-down or tristate (off) controls. The ALT modes can also be set using alt0, alt1, . alt5. unexportall Un-Export all the GPIO pins in the /sys/class/gpio directory. exports Print a list (if any) of all the exported GPIO pins and their current values. export Export a GPIO pin in the /sys/class/gpio directory. Use like the mode command above however only in, out, high and low are supported at this time. Note that the pin number is the BCM_GPIO number and not the wiringPi number. The high and low commands pre-set the output value at the same time as the export to output mode. Once a GPIO pin has been exported, the gpio program changes the ownership of the /sys/class/gpio/gpioX/value and if present in later kernels, the /sys/class/gpio/gpioX/edge pseudo files to that of the user running the gpio program. This means that you can have a small script of gpio exports to setup the gpio pins as your program requires without the need to run anything as root, or with the sudo command. edge This exports a GPIO pin in the /sys/class/gpio directory, set the direction to input and set the edge interrupt method to none, rising, falling or both. Use like the export command above and note that BCM_GPIO pin number is used not not wiringPi pin numbering. Like the export commands above, ownership is set to that of the calling user, allowing subsequent access from user programs without requiring root/sudo. unexport Un-Export a GPIO pin in the /sys/class/gpio directory. wfi  This set the given pin to the supplied interrupt mode: rising, falling or both then waits for the interrupt to happen. It's a non-busy wait, so does not consume and CPU while it's waiting. drive group value Change the pad driver value for the given pad group to the supplied drive value. Group is 0, 1 or 2 and value is 0-7. Do not use unless you are absolutely sure you know what you're doing. usbp high | low Change the USB current limiter to high (1.2 amps) or low (the default, 600mA) This is only applicable to the Model B+ and the Model B, v2. pwm-bal/pwm-ms Change the PWM mode to balanced (the default) or mark:space ratio (traditional) pwmr Change the PWM range register. The default is 1024. gbr channel This reads the analog to digital converter on the Gertboard on the given channel. The board jumpers need to be in-place to do this operation. gbw channel value This writes the supplied value to the output channel on the Gertboards SPI digital to analogue converter. The board jumpers need to be in-place to do this operation.

WiringPi vs. BCM_GPIO Pin numbering vs. Physical pin numbering

The quickest way to get a list of the pin differences is to run the command gpio readall

FILES

EXAMPLES

gpio mode 4 output # Set pin 4 to output gpio -g mode 23 output # Set GPIO pin 23 to output (same as WiringPi pin 4) gpio mode 1 pwm # Set pin 1 to PWM mode gpio pwm 1 512 # Set pin 1 to PWM value 512 - half brightness gpio export 17 out # Set GPIO Pin 17 to output gpio export 0 in # Set GPIO Pin 0 (SDA0) to input. gpio -g read 0 # Read GPIO Pin 0 (SDA0)

NOTES

When using the export, edge or unexport commands, the pin numbers are always native BCM_GPIO numbers and never wiringPi pin numbers. As of kernels 4.1.7, a user-level GPIO access mechanism is available, however wiringPi will not use this by default - because at this time there appears to be issues when trying to program the PWM or clock output hardware. If you can live without PWM or GPIO clocks and you want to use the GPIO from a non-root program, then you need to make sure that the module bcm2835_gpiomem is loaded at boot time. This should happen automatically when you enable the device tree in raspi-config. You may also need some additional information in /etc/udev/rules.d/ to change the mode and ownership of the /dev/gpiomem file. Finally, you need to set the environment variable WIRINGPI_GPIOMEM. This will go-away in future releases once the /dev/gpiomem interface is fully operational.

SEE ALSO

AUTHOR

REPORTING BUGS

Copyright (c) 2012-2018 Gordon Henderson This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

TRADEMARKS AND ACKNOWLEDGEMENTS

© 2019 Canonical Ltd. Ubuntu and Canonical are registered trademarks of Canonical Ltd.

Источник

Оцените статью
Adblock
detector