Pi IoT In Python Using Linux Drivers - GPIO Character Driver
Written by Mike James & Harry Fairhead   
Monday, 29 January 2024
Article Index
Pi IoT In Python Using Linux Drivers - GPIO Character Driver
Installing libgpiod and Python Binding
Working With More Than One Line

Working With More Than One Line

There are also “bulk” versions of the line functions which work with a set of lines at one time. The key to understanding these is the LineBulk object

lines=gpiod.LineBulk([List of Line objects])

For example:

chip=gpiod.Chip("0")
line4=chip.get_line(4)
line5=chip.get_line(5)
lines=gpiod.LineBulk([line4,line5])

This isn’t a particularly useful way of creating a LineBulk object and the get_lines method is more commonly used:

lines = chip.get_lines(offsets)

where offsets is a List of GPIO line offsets.

Using this the previous example can be written:

chip=gpiod.Chip("0")
lines=chip.get_lines([4,5])

You can do the same thing using find_lines which returns a LineBulk object with lines defined by a List of names.

There is also a method that will get all of the lines supported by a GPIO chip:

lines=chip.get_all_lines()

Once you have a LineBulk object you can initialize all of the lines it represents using its request method:

request(consumer, type=None, flags=None, 
default_vals=None)

This sets all of the lines to the same consumer, type and flags. The default_vals is a list of values that are applied one per line.

For example, to set the two lines to output you would use:

chip=gpiod.Chip("0")
lines=chip.get_lines([4,5])
lines.request(consumer="myprog.py",
type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0,1])

Notice that consumer and type are set to the same value for each line, but the default values are different.

There is also a function that will close all of the lines that you have been working with:

lines.release()

Once you have set up the bulk lines you can get and set them:

vals=lines.get_values()
lines.set_values(vals)

For the get function the vals List is set to the state of all of the lines, including output lines, and for the set function the vals List is written to the lines.

How these functions all work is easy to understand after you see an example. In this case the program pulses two lines that are out of phase:

import gpiod
chip=gpiod.Chip("0")
lines=chip.get_lines([4,17])
lines.request(consumer="myprog.py",
type=gpiod.LINE_REQ_DIR_OUT, default_vals=[0,1])
while(True):
lines.set_values([1,0])
lines.set_values([0,1])

The new steps are the creation of a LineBulk object for lines GPIO 4 and GPIO 17. Once we have the line_bulk object we set the lines to output mode and then the set_values method sets them to 1,0 and 0,1.

Remember the need to change the chip number to "4" for a Pi 5.

You can see that despite the program implying that the lines are switched at the same time, they are not:

phase1

You can see that for a Pi 4 the pulse is about 2µs and 240kHz and there is a lag of 0.1µs. For a Pi Zero the pulse width is 16µs a frequency of 30kHz the lag is around 0.2µs.

In chapter but not in this extract

  • Using GPIO Lines – Measuring R and C

Summary

  • The GPIO character driver replaces the sysfs GPIO driver and it is the one to use for all future projects.
  • The GPIOD library provides a higher level way of using the GPIO character driver, but if you don’t want the overhead of using it then the direct ioctl interface is easy to use.
  • After the library has been installed there are a number of utilities that are sometimes useful at the command line or in scripts.
  • The C functions wrapped by the Python bindings do require you to get/open the GPIO line and configure it before you use it. Once used you have to release the line to allow another process to make use of it or to change its configuration.
  • You can work with multiple GPIO lines in a single function call. However, there are still delays between setting lines to particular values.
  • The Python methods can create pulses as fast as 2µs on a Pi 5 and 40µs on a Pi Zero.
  • Using the time-to-charge you can measure either the resistance or the capacitance of a circuit using a single GPIO line.

Raspberry Pi IoT In PythonUsing Linux Drivers
Second Edition

By Harry Fairhead & Mike James

DriverPython2e360

Buy from Amazon.

Contents

  1.  Choosing A Pi For IoT
  2.  Getting Started With Python
  3.   Drivers: A First Program
  4.  The GPIO Character Driver ***NEW!!
  5.  GPIO Using I/O Control
  6.  GPIO Events
  7.  The Device Tree
       Extract: The DHT22
  8.  Some Electronics
  9.  Pulse Width Modulation
       Extract: PWM *
  10. SPI Devices
  11. I2C Basics
       Extract: I2C *
  12. The I2C Linux Driver
  13. Advanced I2C
  14. Sensor Drivers
  15. 1-Wire Bus
       Extract 1-Wire And The DS18B20 *
  16. Going Further With Drivers
  17. Appendix I

*From the first edition waiting for update.

 <ASIN:B0CT46R6LF>

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


The WinterJS Javascript Runtime Is Asking For Your Attention
11/04/2024

WinterJS is a brand new Javascript runtime by Wasmer which comes with the claim that it's the fastest of them all. Let's find out if that holds true.



ACM Adopts Open Access Publishing Model
05/04/2024

ACM, the Association for Computing Machinery, the professional body for computer scientists, has relaunched Communications of the ACM, the organization’s flagship magazine, as a web-first  [ ... ]


More News

raspberry pi books

 

Comments




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



Last Updated ( Wednesday, 31 January 2024 )