|Raspberry Pi IoT In C - The Linux GPIO Driver|
|Written by Harry Fairhead|
|Monday, 20 September 2021|
Page 2 of 2
GPIO Character Device
The new approach to working with GPIO comes pre-installed in the latest version of Pi OS – it isn’t supported in earlier versions. If you look in the /dev directory you will find files corresponding to each GPIO controller installed. You will see at least:
This represents the main GPIO controller and all of the GPIO lines it provides. If you know how sysfs works you might well be expecting instructions on how to read and write to the file from the console. In this case reading and writing to the file will do you little good as most of the work is carried out using the ioctl() system call which cannot be used from the command line.
If you do want to explore the GPIO from the command line, you need to install some tools that have been created mainly as examples of using the new device interface. To do this you need to first install them and the gpiod library that you will use later:
sudo apt-get install gpiod libgpiod-dev libgpiod-doc
The standalone applications that are installed are:
which lists all of the GPIO controllers that are installed:
which lists all of the GPIO lines provided by the named GPIO controller:
You can give particular lines names by editing the device tree. If you do give them appropriate fixed names then:
will return the GPIO line by number.
You can use this to set any number of GPIO lines in one operation. The command has the form:
gpioset options chip name/number <offset1>=<value1>
exit: exit immediately
defaults to 'exit'
Notice that the change in the line’s state only persists while the command is executing. What this means is that if you want to see the effect, you have to use wait or time. For example:
gpioset -m wait 0 4=0 17=1
sets gpiochip 0 GPIO 4 to 0 and GPIO 17 to 1 and waits until the user presses enter.
The gpioget command returns the state of the lines specified as text:
gpioget chip name/number <offset 1> <offset 2>
gpioget 0 4 17
will display the current state of GPIO 4 and GPIO 17 on gpiochip0.
The gpiomon command lets you monitor changes in input lines using a poll system call:
gpiomon options chip name/number <offset 1> <offset 2> …
The options are:
-n, --num-events=NUM exit after processing NUM events -s, --silent don't print event info -r, --rising-edge only process rising edge events -f, --falling-edge only process falling edge events -F, --format= FMT specify custom output format FMT
These utilities are useful and they can be used in scripts to control GPIO lines. For example if you save:
while true do gpioset -m time -s 1 0 4=0 17=1 gpioset -m time -s 1 0 4=1 17=0 done
in a text file called binky.sh and set its execution permission to owner then you can run it in a console and flash a pair of LEDs connected to GPIO 4 and GPIO 17.
You can get a long way with shell scripts and the GPIO utilities, but sooner or later you are going to want to work with C.
In chapter but not in this extract
Raspberry Pi And The IoT In C Second Edition
By Harry Fairhead
Buy from Amazon.
To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
or email your comment to: firstname.lastname@example.org
|Last Updated ( Monday, 20 September 2021 )|