Page 1 of 1

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

Posted: Wed May 12, 2021 2:01 pm
by TroCeAtO
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?

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

Posted: Wed May 12, 2021 2:23 pm
by scruss
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.

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

Posted: Wed May 12, 2021 6:06 pm
by dhylands
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.