Raspberry PI GPIO

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Raspberry PI GPIO

Post by kevinkk525 » Tue Feb 26, 2019 6:24 pm

I don't intend to run a bare metal port on the raspberry pi but the standard unix port. This way I don't need to write all the code again in CPython. However I would like to use the RPI GPIOs.

This is in a small scale quite possible using

Code: Select all

os.popen("gpio read 0")
but this is is very basic and very slow (this instruction takes 11ms!).
Not good enough to read any sensor.

Is there a better way to access the GPIOs from micropython?

I was thinking about running a local socketserver that provides access to the GPIOs which would involve a lot of coding. I'd have to check the performance of this first.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Raspberry PI GPIO

Post by dhylands » Tue Feb 26, 2019 8:19 pm

The simplest way is to use the gpio sysfs files.

The documentation for the sysfs filesystem can be found here: https://git.kernel.org/pub/scm/linux/ke ... h=v4.20.12

A quick google search came up with this python library: https://github.com/vitiral/gpio which will probably require some modifications for MicroPython, but it shouldn't be much. You're just reading/writing files.

If you want more performance, then the next step to take is to memory ma the GPIO registers and access them directly. However, I don't generally recommend this approach unless you know what you're doing. There are all kinds of things that can go wrong, and you can cause conflicts/collisions with other software which is using GPIO pins for other purposes, not to mention differences between various raspberry pi models.

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Raspberry PI GPIO

Post by kevinkk525 » Tue Feb 26, 2019 9:14 pm

Thanks a lot! I completely forgot about the sysfs option.

I'll look into it tomorrow, this looks very promising.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Turbinenreiter
Posts: 288
Joined: Sun May 04, 2014 8:54 am

Re: Raspberry PI GPIO

Post by Turbinenreiter » Tue Feb 26, 2019 9:16 pm

Afaik, CircuitPythons API is implemented for the Linux on the RPi and is portable, too.

https://learn.adafruit.com/circuitpytho ... x/overview

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Raspberry PI GPIO

Post by kevinkk525 » Tue Feb 26, 2019 10:07 pm

Turbinenreiter wrote:
Tue Feb 26, 2019 9:16 pm
Afaik, CircuitPythons API is implemented for the Linux on the RPi and is portable, too.

https://learn.adafruit.com/circuitpytho ... x/overview
I looked at the code and it looks more like they simulate a circuitpython environment in Cpython3 and use the Cpython library "RPi.GPIO" for gpio access.
That was the other possibility I thought about, using Cpython3 and simulating a micropython environment by providing the correct libraries instead of using a real micropython environment.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

kevinkk525
Posts: 969
Joined: Sat Feb 03, 2018 7:02 pm

Re: Raspberry PI GPIO

Post by kevinkk525 » Wed Feb 27, 2019 10:19 am

dhylands wrote:
Tue Feb 26, 2019 8:19 pm
The simplest way is to use the gpio sysfs files.

The documentation for the sysfs filesystem can be found here: https://git.kernel.org/pub/scm/linux/ke ... h=v4.20.12

A quick google search came up with this python library: https://github.com/vitiral/gpio which will probably require some modifications for MicroPython, but it shouldn't be much. You're just reading/writing files.

If you want more performance, then the next step to take is to memory ma the GPIO registers and access them directly. However, I don't generally recommend this approach unless you know what you're doing. There are all kinds of things that can go wrong, and you can cause conflicts/collisions with other software which is using GPIO pins for other purposes, not to mention differences between various raspberry pi models.
I changed the library and so far it seems to work. Reading a pin takes ~300us now which is great but still slower than reading a pin on the esp8266 which takes 160us.
I'm curious if I can get the I2C and SPI interface working too.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

Post Reply