6 years, 6 months ago.

Async I2CSlave

Hi all, Setup in my personal project : Stm32 blupill board Configured as I2C Master on I2C1 connected to a Sensor (later multiple sensors). Configured as I2C slave on I2C2 connected to a Lego NXT. MCU is basically preparing the sensor(s) and fetches its measurements and then the NXT should fetch the values. Both parts work well independently. But for me the I2CSlave implementation using Busy Waiting seems to be very timing sensitive and leaves no time to fetch new values from the sensor. For the I2C master implementation there is async support. Is there any for I2CSlave?

1 Answer

6 years, 6 months ago.

I2CSlave indeed does not have an asynchronous API. If you run the I2CSlave part in a separate thread, do you still have problems?

void start_slave() {
     slave.address(0xA0);
     while (1) {
         int i = slave.receive();
        // do operations
    }
}

int main() {
    Thread slave_thread;
    slave_thread.start(&start_slave);

    // rest of your code
}

If you have prioritisation problems, you can run the slave thread with lower priority than your sensor reading thread via:

Thread slave_thread(osPriorityLow);