Skip to content

I2CTarget: Add number of bytes read/written #17996

Description

@sfe-SparkFro

Description

Love the new I2CTarget class, thanks for adding it!

I2CTarget.memaddr specifies the first memory address read/written by the controller, but there is no way to know how many bytes were read/written. This would be a very useful feature for some I2CTarget implementations.

As a slightly contrived example for motivation, suppose I'm creating an I2CTarget that controls multiple stepper motors, and is implemented as a "memory device". Whenever the I2C controller writes a byte to a memory address (or multiple bytes in a single transaction), the corresponding stepper motor(s) moves that many steps. The IRQ implementation would need to wait for a IRQ_END_WRITE event, then look at i2c_target.memaddr and a currently non-existent i2c_target.numbytes (or similar) to know which stepper motors to move.

I suppose this hypothetical device could be implemented as an arbitrary I2C device by handling all the events in the IRQ, instead of as a memory device, so you could count each time the IRQ_WRITE_REQ event occurs until IRQ_END_WRITE occurs. However this would be a lot more Python code, and I have yet to evaluate performance and whether this would result in unnecessary clock stretching (particularly at clock frequencies higher than 100kHz). Clock stretching should really be avoided if possible, it can cause a lot of obscure problems (example, example).

Code Size

IMO this should be required as part of I2CTarget. Can't imagine it would be a huge increase to the code size.

Implementation

I hope the MicroPython maintainers or community will implement this feature

Code of Conduct

Yes, I agree

Activity

  1. added
    portsRelates to multiple ports, or a new/proposed port
    on Oct 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementFeature requests, new feature implementationsportsRelates to multiple ports, or a new/proposed port

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions