The i2c device data cannot be obtained in real time

Issue #500 resolved
gavin created an issue

Hello, I seem to be having communication problems while adding an i2c device to the microbit master.

This requirement is like this: first send instructions and data to the device through i2c, and then wait for the i2c device to complete the action. And, of course, the i2c device feeds back its own state in real time.

Here's what I do: I send instructions to the device via 'sensors:i2cWrite', and I can guarantee that the device has received the instructions and is executing them. Then I write a loop to wait for the device to complete the task and use the '[sensors:i2cRead]' method in the loop to read the state of the device. But the program always gets stuck in this loop because it never gets the finished state data. I can guarantee that the device will change its state in real time. And, when I use the wait 500 microseconds in the loop, in most cases I can get the device state normally, but this way is not desirable, it affects the real-time performance of the program.

I want to know what caused this to happen and how I can fix it.

Thank you very much!

Comments (3)

  1. John Maloney repo owner

    What I2C device are you using?

    In general, an I2C transaction starts by writing the address of the I2C device and, usually, a register address before reading. For operations that take time, one would usually send a command or write a register to start the operation as one I2C transaction and, later, read a register to see if the operation has finished.

    Some I2C devices use a technique called clock stretching that forces the CPU to wait until they are finished. Clock streteching is usually not desirable since it prevents other MicroBlocks tasks from running while the I2C transaction is in progress.

    Check the datasheet of your i2c device to better understand the i2c transactions it expects.

  2. John Maloney repo owner

    I'm marking this resolved because it does not appear to be an issue with MicroBlocks itself. But feel free to add additional comments if you would like further suggestions.

  3. Log in to comment