Getting to know RepkaPi.GPIO SysFS. Installing and managing GPIO via Python 3. Theoretical foundations of GPIO ports
Content
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
GPIO.DEFAULTBOARD – allows you to specify the Repka Pi model on which the module will be used. The default value is None. In this case, the value must be determined by the board when working with the library. At the moment, it is possible to set the value GPIO.REPKAPI3 or 1, this will mean that the library works on Repka Pi 3 and in the future, when working with the library, it is not necessary to specify the board version
GPIO.AUTODETECT – automatic detection of the Repka Pi model. Takes a Boolean True/False value, defaults to True. If set to True then GPIO.DEFAULTBOARD ignored
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 ENTER
to 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)
GPIO.BOARD – 10 – physical numbering in order from 1 to 40
GPIO.BCM – 11 – numbering according to Raspberry Pi 3B for backward compatibility of scripts, we do not recommend using it in new scripts
GPIO.SUNXI – 12 – by SUNXI string designation “PA0…, PC…, PL…”
GPIO.SOC – 13 – by numerical address in SOC, used in SysFS, recommended for use
GPIO.SYSFS – alias for GPIO.SOC
SUNXI meanings
GPIO.PA – 0
GPIO.PC – 64
GPIO.PD – 96
GPIO.PE – 128
GPIO.PF – 160
GPIO.PG – 192
GPIO.PL – 352
Pin modes
Pin state
GPIO.HIGH – 1 – high level, logical unit
GPIO.LOW – 0 – low level, logic zero
Pin lift
GPIO.PUD_OFF – does not stretch
GPIO.PUD_DOWN – pin through internal resistor will be connected to 0 (GND)
GPIO.PUD_UP – the pin through the internal resistor will be connected to +3.3V
Attention: Pullup in this version of the module is ignored, it is necessary to implement a physical pullup through a resistor
Event
GPIO.RISING – signal change from LOW to HIGH
GPIO.FALLING – signal change from HIGH to LOW
GPIO.BOTH – signal change in any direction
Library Methods
Informational
Installation
GPIO.setboard() – installation of the Repka Pi model, takes the value
GPIO.setmode() – setting the pin numbering mode, takes the value
GPIO.setwarnings() – management of warnings, accepts a boolean value True/False
Working with GPIOs
GPIO.setup() – pin initialization, takes values
channel – pin number in accordance with the set numbering mode,
direction – set GPIO pin as input or output (only use GPIO.IN or GPIO.OUT)
initial – (optional) setting the initial value of the pin (GPIO.LOW (0) or GPIO.HIGH (1),
pull_up_down – (optional) pull-up to +3.3V (GPIO.PUD_UP), to 0 (GPIO.PUD_DOWN) or not set (GPIO.PUD_OFF).
Attention: Pullup does not work in this version of the library, but the description is kept for backwards compatibility
GPIO.input() – getting the state of the pin, takes a value
GPIO.output() – sets pin state, accepts values
channel – pin number in accordance with the set numbering mode,
state – pin state GPIO.LOW (0) or GPIO.HIGH (1)
GPIO.cleanup() – return the pins to their original state, takes the value
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.2
this 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)
channel – pin number in accordance with the set numbering mode,
direction – set GPIO pin as input or output (only use GPIO.IN or GPIO.OUT)
initial – (optional) setting the initial value of the pin (GPIO.LOW (0) or GPIO.HIGH (1),
pull_up_down – (optional) pull-up to +3.3V (GPIO.PUD_UP), to 0 (GPIO.PUD_DOWN) or not set (GPIO.PUD_OFF)
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:
channel – pin number, can accept a list (list) and tuples (tuple)
state – pin state 0/GPIO.LOW/False or 1/GPIO.HIGH/True, can take a list (list) and tuples (tuple)
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!