Raspberry Pi IoT In C - gpiod
Written by Harry Fairhead   
Monday, 30 December 2024
Article Index
Raspberry Pi IoT In C - gpiod
Raw GPIO Character Device In C - ioctl
GPIO Output
GPIO Input

GPIO Input

Reading data from GPIO lines follow the same steps as writing, but with obvious changes:

#include <string.h>
#include <stdio.h> 
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/gpio.h>
int main(int argc, char **argv) {
    int fd, ret;
    struct gpiohandle_request req;
    req.lineoffsets[0] = 4;
    req.lineoffsets[1] = 17;
    req.flags = GPIOHANDLE_REQUEST_INPUT;
    strcpy(req.consumer_label, "Input test");
    req.lines = 2;
    fd = open("/dev/gpiochip0", O_RDONLY);
    ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req);
    close(fd);
    struct gpiohandle_data data;
    ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL,
&data); printf("%hhu , %hhu",data.values[0],data.values[1]); close(req.fd); return 0; }

Remember to change gpiochip0 to gpiochip4 if you are running on a Pi 5.

You can see that now the request is to set an input line and the reading part of the program uses get and then displays the values in the data structure.

 

Summary

  • A Linux-based approach for GPIO access from the Raspberry Pi offers advantages. The first is portability - your program, with potentially minor adjustments, can be ported to other Linux machines equipped with GPIO hardware.

  • Furthermore, the GPIO driver grants access without requiring root privileges or direct memory manipulation. Moreover, it facilitates a straightforward mechanism for handling I/O events from user space, making it a complementary option to the BCM2835 library in certain scenarios.

  • While sysfs GPIO is still in the Linux kernel it has been deprecated since 2008 and should not be used for new projects.

  • 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 Third Edition

By Harry Fairhead

piciot3e360

Buy from Amazon.

Contents

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

<ASIN:B0CS2LRPSS>

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


How To Get More Time To Code
31/12/2024

Amazon recently disclosed that developers spend an average of just one hour per day coding. This finding was reported in an announcement that Amazon Q Developer can now document your code by [ ... ]



Linux Foundation Launches Chromium Browser Support
10/01/2025

The Linux Foundation and Google have announced a partnership and launched an initiative in support of Chromium-based browsers.


More News

espbook

 

Comments




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



Last Updated ( Monday, 30 December 2024 )