Micropython Unix port on Raspberry Pi - I2C/SPI?

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
gersch07
Posts: 3
Joined: Sun Jan 26, 2020 10:00 pm

Micropython Unix port on Raspberry Pi - I2C/SPI?

Post by gersch07 » Mon Aug 03, 2020 8:36 pm

Dear all,

I've been recently experimenting a bit with Micropython's Unix port on the Raspberry Pi 3B+ (with Raspberry Pi OS) and found it really nice & lightweight. However, there is unfortunately no support for I2C or SPI required to communicate with most sensors and displays... Is it possible (with reasonable effort) to generate bindings to one the following C libraries (for which Python bindings already exist)? I'm currently exploring the automated generator script "gen_mpy.py" used to create bindings for the LVGL graphics library (https://github.com/lvgl/lv_binding_micr ... d080abe9b2). For the SMBus library, it indeed does produce C code, but I haven't been able to include that into the Micropython source code and was hoping for a simpler method...

I saw that there is a git repo containing a bare-metal port for the Raspberry Pi 1/Zero (with Broadcom BCM2835 CPU), however that library won't work for newer models, I guess...
Thanks for your help in advance!

User avatar
dlech
Posts: 1
Joined: Wed Aug 05, 2020 4:39 pm

Re: Micropython Unix port on Raspberry Pi - I2C/SPI?

Post by dlech » Wed Aug 05, 2020 4:49 pm


TroCeAtO
Posts: 2
Joined: Wed May 12, 2021 1:54 pm

How can the I2C call readfrom_mem_into be replicated with SMBus methods in micropython?

Post by TroCeAtO » Wed May 12, 2021 2:09 pm

For a micropython application on a Raspberry Pi I want to read a sensor via I2C. As the OP already mentioned, I2C does not work with Micropython running on a Raspberry Pi (4B), but the python port mentioned above seems to help. So I'm trying to rebuild the I2C method I2C.readfrom_mem_into (https://docs.micropython.org/en/latest ... m_mem_into) to read data from memory using SMBus methods.

SMBus provides a method to read I2C data: smbus.read_i2c_block_data(addr,cmd) where this has only two instead of the three parameters from the I2C.readfrom_mem_into method. There are other read methods like:
  • read_block_data
  • read_byte
  • read_byte_data
which also do not offer three parameters.

The question now is, how can I reconstruct the I2C.readfrom_mem_into call by one or more SMBus methods?

Asensio12
Posts: 6
Joined: Thu Jan 13, 2022 3:33 pm

Re: Micropython Unix port on Raspberry Pi - I2C/SPI?

Post by Asensio12 » Thu Mar 07, 2024 11:06 am

I did this a few months ago for a personal project: https://github.com/AsensioL/smbus2/blob ... /smbus2.py
I only recently got a chance to upload it.

Check the last 2 commits. In the most recent one I add some support for the existing machine.i2c functions.

Maybe it can help.

Good luck

Edit: All credits to https://github.com/kplindegaard/smbus2 for their original implementation in pure Python that I was able to adapt.
Also, note that I haven't thoroughly tested my changes, but they worked just fine (as far as I could tell) for my pet project.

Post Reply