Getting to know RepkaPi.GPIO SysFS. Installing and managing GPIO via Python 3. Theoretical foundations of GPIO ports

Content

  1. Introduction

  2. Installation and initial setup

  3. Library constants

  4. Library Methods

  5. Getting started with the library

    1. Library and Board Information

    2. Pin operation mode

      1. State setting mode (OUTPUT)

      2. Status read mode (INPUT)

    3. Reading and setting states

  6. Conclusion

Introduction

Let’s start our acquaintance with the RepkaPi.GPIO plug-in library, this library is written in Python 3 and uses methods implemented through SysFS to control GPIO. You can read more about SysFS at wikipedia

This library has been maximally backward compatible with the RPI.GPIO library for Raspberry Pi to ensure that your scripts can be ported quickly.

The permanent address of the library on gitflic.ru: https://gitflic.ru/project/repka_pi/repkapigpiofs

Installation and initial setup

First of all, install the necessary packages

Update the package index

sudo apt-get update

Install the necessary packages

sudo apt-get install python3-dev python3-setuptools git

Clone the repository

git clone https://gitflic.ru/project/repka_pi/repkapigpiofs.git

Go to the resulting folder

cd repkapigpiofs

* You can also download the archive from gitflic and extract it to a convenient place

Before installing the module, let’s consider its possible configuration. The module has two constants, these constants are in the file RepkaPi/constants.py

Library installation

sudo python3 setup.py install

Upon successful installation of the library, the output will end like this

Without root access

If you want to use the library as a non-root user, you need to first set up a UDEV rule to grant you permissions. This can be done like this:

Create a gpio group

sudo groupadd gpio

Adding your user to the gpio group

sudo usermod -aG gpio <ваш пользователь>

Create a rule for udev on system boot

sudo nano /etc/udev/rules.d/99-gpio.rules

In the file that opens, paste the following

KERNEL=="gpio\*", MODE:="0660", GROUP:="gpio" 

KERNEL=="pwm*", MODE:="0660", GROUP:="gpio" 

KERNEL=="gpiochip*", MODE:="0660", GROUP:="gpio" 

SUBSYSTEM=="gpio*", PROGRAM="/bin/sh -c 'chown -R root:gpio /sys/class/gpio && chmod -R 777 /sys/class/gpio && chown -R root:gpio /sys/class/gpio/* && chmod -R 777 /sys/class/gpio/* && chown -R root:gpio /sys/devices/platform/soc/*.pinctrl/gpio && chmod -R 777 /sys/devices/platform/soc/*.pinctrl/gpio'" SUBSYSTEM=="pwm*", PROGRAM="/bin/sh -c 'chown -R root:gpio /sys/class/pwm && chmod -R 777 /sys/class/pwm && chown -R root:gpio /sys/class/pwm/* && chmod -R 777 /sys/class/pwm/* && chown -R root:gpio /sys/class/pwm/pwmchip0/* && chmod -R 777 /sys/class/pwm/pwmchip0/* && chown -R root:gpio /sys/devices/platform/soc/*.pwm/pwm && chmod -R 777 /sys/devices/platform/soc/*.pwm/pwm '"

Click ctrl-x, Y And ENTERto save and close the file.

Reboot the system

sudo reboot

Library constants

Let’s consider what constants the library has and can be used at work. In the article we will consider them in more detail.

Informational

Repka Pi Models

Pin numbering modes (mode)

SUNXI meanings

Pin modes

Pin state

Pin lift

Attention: Pullup in this version of the module is ignored, it is necessary to implement a physical pullup through a resistor

Event

Library Methods

Informational

Installation

Working with GPIOs

In the following articles, we will consider the remaining methods of the library.

Getting started with the library

First of all, we need to include the RepkaPi.GPIO library in your script

import RepkaPi.GPIO as GPIO

If during installation you did not specify GPIO.DEFAULTBOARD And GPIO.AUTODETECT set to False then you need to set the Repka Pi model, this can be done with the following function

GPIO.setboard(GPIO.REPKAPI3)

GPIO.REPKAPI3 – means that the library is used on Repka Pi 3. Currently the only model.

Specify which mode we want to use

GPIO.setmode(GPIO.SOC)

Above in the sectionLibrary Constants” all the constants were listed, now let’s take a closer look at how they differ.

BOARD – address by the serial number of the pin on the board, if we want to address the pin under number 7, then when addressing, you must also write 7

SUNXI – Reference by string parameter in the pinout scheme, if we use this mode, then the reference to pin 7 will be as a string value “PA7

This method will be more understandable, but it also uses additional operations when working.

SOC and SYSFS – numeric addressing by SOC and SYSFS i.e. if you pay attention to the pinout, both official and presented in the article, you will see the corresponding column.

If you want to refer to pin number 18, then you will need to specify its SOC / SysFS number equal to 355, you can also calculate this number using SUNX (P stands for Pin, we skip it. The calculation formula, (serial number of the Latin letter (L) – 1) 32 + number after the Latin letter (3), then we get (12-1) 32+3=355).

This method is preferred because no additional cast operations are performed. Also, for convenience, the library has the following constants

PA – 0, PC – 64, PD – 96, PE – 128, PF – 160, PG – 192, PL – 352 and access to 18 pin PL3 will be next GPIO.PL + 3

BCM – this scheme is designed to be compatible with BCM processors that are installed mainly in the Raspberry Pi. If we look at the Raspberry Pi circuit, for example, it has GPIO4 on pin 7

The call is made by the GPIO number i.e. 4, then the script will bring this value to the corresponding value for Repka Pi, i.e. By SOC it will be 7 or PA7 by SUNXI

We will not consider this method within the framework of the article, it will not be useful when developing on Repka Pi.

If you haven’t figured it out, then don’t worry, we will still return to this part in the examples, and perhaps more than once.

And so we have considered the main methods that must be applied in all scripts, let’s call them basic methods.

Library and Board Information

The library has two constants and one method to get information about the board and the library.

Get selected board model

GPIO.getboardmodel()

This method will return us

Repka Pi 3

* with new versions and as they are added to the library, other answers are possible

Library version

GPIO.VERSION

Will return version 0.1.2this is the version at the time of writing

Device Information

GPIO.RPI_INFO

Returns a properties object

{'P1_REVISION': 3, 'TYPE': 'Repka Pi 3', 'MANUFACTURER': 'ИНТЕЛЛЕКТ', 'RAM': '1GB', 'REVISION': '', 'PROCESSOR': 'Allwinner H5'}

This information corresponds to the structure of a similar constant in RPI.GPIO for Raspberry Pi

To get a particular property, you can access it directly:

GPIO.RPI_INFO['MANUFACTURER']

Return value

ИНТЕЛЛЕКТ
GPIO.RPI_INFO['PROCESSOR']

Return value

Allwinner H5

Find out which mode is set

GPIO.getmode()

This method will return a numeric value corresponding to the set mode or None. These values ​​can be found in the description of the constants.

Pin operation mode

After considering the basic basic actions, you can proceed to setting the pin operation mode. Consider all possible conversions using the example of 18 pins

Pin can take 2 options – INPUT (state read mode) and OUTPUT (status setting mode)

To set the state, use the method GPIO.setup(channel, direction, initial=None, pull_up_down=None)

State setting mode (OUTPUT)

In GPIO.BOARD mode

GPIO.setup(18, GPIO.OUT)

In GPIO.SUNXI mode

GPIO.setup("PL3", GPIO.OUT)

In GPIO.SOC/GPIO.SYSFS mode

GPIO.setup(355, GPIO.OUT)

or

GPIO.setup(GPIO.PL+3, GPIO.OUT)

In what follows, we will consider only the mode GPIO.SOC/GPIO.SYSFS

You can also set up multiple pins in one call using lists (list)

GPIO.setup([GPIO.PL+3, GPIO.PA+10], GPIO.OUT)

or tuples (tuple)

GPIO.setup((GPIO.PL+3, GPIO.PA+10), GPIO.OUT)

For a pin in OUT mode, you can set the initial value using the property initial

We initialize the pin in OUTPUT mode with the value of the logical unit GPIO.HIGH

GPIO.setup(355, GPIO.OUT, initial=GPIO.HIGH)

We initialize the pin in OUTPUT mode with a value of logical zero GPIO.LOW

GPIO.setup(pin, GPIO.OUT, initial=GPIO.LOW)

Status read mode (INPUT)

In GPIO.SOC/GPIO.SYSFS mode

GPIO.setup(GPIO.PL+3, GPIO.IN)

You can also set up multiple pins in one call using lists (list)

GPIO.setup([GPIO.PL+3, GPIO.PA+10], GPIO.IN)

or tuples (tuple)

GPIO.setup((GPIO.PL+3, GPIO.PA+10), GPIO.IN)

When initializing a pin in mode INPUTyou can use the same command to increase / decrease the resistor pull-up of this pin, and also disable the pull-up

Attention: There is no support for pullup resistors in this module, if they are specified, a warning will be displayed instead so that it is at least compatible with existing code, but without implementing real functionality.

If the input pin is not connected to anything, it will “float”. In other words, the read value is undefined because it is not associated with anything until you press the button or switch.

To get around this problem, we use a pull-up or pull-down resistor. This way you can set the default input value. The raise/lower resistors can be used in hardware and in software. The hardware typically uses a 10 kΩ resistor between the input channel and 3.3V (boost) or 0V (pull-down).

Software pull will be implemented in another version of the module

We pull up to + 3.3V

GPIO.setup(GPIO.PL+3, GPIO.IN, pull_up_down=GPIO.PUD_UP)

Pull up to 0V

GPIO.setup(GPIO.PL+3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

Disable pull-up

GPIO.setup(GPIO.PL+3, GPIO.IN, pull_up_down=GPIO.PUD_OFF)

Reading and setting states

To get the state of a pin set in mode INPUT (read state mode) or OUTPUT (state setting mode) use the function input(channel)

The function has a parameter channel pin number in accordance with the set numbering mode and returns a value 0/GPIO.LOW/False or 1/GPIO.HIGH/True

value=GPIO.input(GPIO.PL+3)

In this example, the state of pin PL3 will be written to value, 0/GPIO.LOW/False or 1/GPIO.HIGH/True

To set the state of a pin initialized to the mode OUTPUT (state setting mode) use the function output(channel, state)

Function parameters:

GPIO.output(GPIO.PL+3, GPIO.HIGH)

In this example PL3 set to high (1/GPIO.HIGH/True), in this case, the pin will have a voltage of + 3.3V

Now consider the options using the list (list)

GPIO.output([GPIO.PL+3, GPIO.PA+10], GPIO.HIGH)

or tuple (tuple)

GPIO.output((GPIO.PL+3, GPIO.PA+10), GPIO.HIGH)

In these examples, we have set PL3 And PA10 high level (1/GPIO.HIGH/True)

You can also pass a list to the pin value (list) values

GPIO.output([GPIO.PL+3, GPIO.PA+10], [GPIO.HIGH, GPIO.LOW])

or tuple (tuple)

GPIO.output([GPIO.PL+3, GPIO.PA+10], (GPIO.HIGH, GPIO.LOW))

Options channel And state may be of type list (list) or tuple (tuple), there is no type relationship. But in state there should be 1 value that will be set to all pins or the same number with channel.

And consider another function cleanup()designed to return the pins to their original state.

The function has a parameter channel pin number in accordance with the set numbering mode

Reset all installed pins

GPIO.cleanup()

Attention: With such a call, the set pin numbering mode will also be reset.

Reset just one pin

GPIO.cleanup(GPIO.PL+3)

Also in this function you can apply a list (list) or tuple (tuple) to reset multiple pins

GPIO.cleanup([GPIO.PL+3, GPIO.PA+10])

or

GPIO.cleanup((GPIO.PL+3, GPIO.PA+10))

Conclusions

In this article, we started our acquaintance with the RepkaPi.GPIO SysFS library for Python3, completed the installation, analyzed its settings, examined constants and some functions for working with GPIO in I/O (Input/Output) mode.

In the next article, we will look at different examples of how the library works in practice!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *