[RPI 4B 8GB] How can the I2C call readfrom_mem_into be replicated with SMBus methods in micropython?

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
TroCeAtO
Posts: 2
Joined: Wed May 12, 2021 1:54 pm

[RPI 4B 8GB] How can the I2C call readfrom_mem_into be replicated with SMBus methods in micropython?

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

For a micropython application on a Raspberry Pi I want to read a sensor, but in the linux port of micropython the I2C library is not available. The SMBus library is available as a port on Github (https://github.com/dlech/micropython-ev ... dev/i2c.py), 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: 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?

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: [RPI 4B 8GB] How can the I2C call readfrom_mem_into be replicated with SMBus methods in micropython?

Post by scruss » Wed May 12, 2021 2:23 pm

Why are you using MicroPython? An 8 GB quad-core Raspberry Pi 4B is perfectly capable of running Python 3. It has I²C support built into the OS.

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

Re: [RPI 4B 8GB] How can the I2C call readfrom_mem_into be replicated with SMBus methods in micropython?

Post by dhylands » Wed May 12, 2021 6:06 pm

The readfrom_mem_into function provided with micropython is designed to read into a previously allocated buffer.

The regulsr smbus library doesn't hacve any equivalent function. Instead you'll need to use one of the read functions which returns a buffer containing the data which was read.

The smbus function read_i2c_block_data(i2c_addr, register, length) maps to I2C.readfrom_mem(addr, memaddr, nbytes) where register and memaddr are equivalent.

Post Reply