Raspberry Pi IoT In C - The Linux GPIO Driver
Written by Harry Fairhead   
Monday, 20 September 2021
Article Index
Raspberry Pi IoT In C - The Linux GPIO Driver
The GPIO Character Device

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:

/dev/gpiochip0

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:

  • gpiodetect 

which lists all of the GPIO controllers that are installed:

gpioutils1

  • gpioinfo 

which lists all of the GPIO lines provided by the named GPIO controller:

gpioutils2

  • gpiofind

 You can give particular lines names by editing the device tree. If you do give them appropriate fixed names then:

gpiofind name

will return the GPIO line by number.

  • gpioset 

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>

<offset2>=<value2> ...
where the options are:
-l, --active-low set the line active state to low
-m, --mode= tell the program what to do after setting values

exit: exit immediately
wait: wait for user to press ENTER
time: sleep for a specified amount of time
signal: wait for SIGINT or SIGTERM

defaults to 'exit'
-s, --sec=SEC number of seconds to wait
-u, --usec=USEC number of microseconds to wait
-b, --background detach from the controlling terminal

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.

  • gpioget 

The gpioget command returns the state of the lines specified as text:

gpioget chip name/number <offset 1> <offset 2>

For example:

gpioget 0 4 17

will display the current state of GPIO 4 and GPIO 17 on gpiochip0.

  • gpiomon

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
Format specifiers:
%o: GPIO line offset %e: event type (0 - falling edge, 1 - rising edge) %s: seconds part of the event timestamp %n: nanoseconds part of the event timestamp


For example to monitor GPIO 4 and GPIO 17 for any changes:

gpioutils3

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

  • Raw GPIO Character Device In C - ioctl
  • Getting Chip Info
  • GPIO Output
  • GPIO Input

 pi41

Summary

  • The new Linux GPIO character driver replaces the well known sysfs GPIO driver.

  • It is harder to use from the command line and mostly uses ioctl calls to access the GPIO lines.

  • If you install the gpiod library you get a set of utility functions that do allow command line use.

  • The raw GPIO character is easy to use once you know the sysctl operations supported and their structs.

  • The GPIO character driver is about twice as fast as the sysfs-based interface, but still ten times slower than direct access.

  • The GPIO lines have to remain open while you use them and you need to remember to close them when you have finished using them.

 

Raspberry Pi And The IoT In C Second Edition

By Harry Fairhead

FrontCover800

Buy from Amazon.

Contents

  1. Why Pi For IoT?
  2. Getting Started
  3. Getting Started With The GPIO
  4. Simple Output
  5. Some Electronics
  6. Simple Input
  7. GPIO The Linux Way
       Extract 1:The Linux GPIO Driver 
  8. Advanced Input – Events, Threads, Interrupts
       Extract 1: Events & Interrupts 
  9. Pulse Width Modulation - Servos And More
       Extract 1:Basic Pulse Width Modulation 
  10. Using The I2C Bus
  11. The DHT22 Sensor Implementing A Custom Protocol
  12. Exploring - 1‑Wire Bus Basics ***NEW!
  13. Using iButtons
  14. DS18B20 Temperature Sensor
      Extract 1: The DS18B20 Temperature Sensor 
  15. The Multidrop 1‑Wire Bus
  16. The Serial Port
      Extract 1: 1-wire Via Serial 
  17. Getting Started With The SPI Bus
  18. A to D With The SPI Bus
  19. Connecting With The Web - Sockets
  20. Memory-Mapped GPIO
  21. Almost Real-Time Linux
  22. Appendix I GPIO Sysfs Interface

 <ASIN:1871962633>

<ASIN:B08KLNT2JC>

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.

Banner


OpenAI Announces ChatGPT Canvas
10/10/2024

OpenAI has launched an extra facility for developers using ChatGPT. Canvas is described as offering a new way of working with ChatGPT to write and code.



A Turing Machine In LEGO
06/10/2024

A Turing Machine is one of those abstract ideas that is much easier to undertand if you can see it working. Now there's a proposal on the LEGO Ideas site for a Working Turing Machine. Lend it your sup [ ... ]


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info

 

 

 



Last Updated ( Monday, 20 September 2021 )