cancel
Showing results for 
Search instead for 
Did you mean: 

Is anyone known how to implement I2C in asynchronous mode or can it implement asynchronously?

WLIAN.3
Associate II

In my project I'm trying to use 1 Arduino Due as I2C Master communicate with 4 NucleoF7 as slave, and the I2C mode is setted to Fast-Mode(400kHz), the I2C part of the code on STM MCU show as the following

i2c_timeout = 1;                   // 1 ms
 
while(1)
{          
         HAL_I2C_Init(&hi2c1);
 
	 HAL_I2C_Slave_Receive(&hi2c1, re_buffer, 3, i2c_timeout);
 
         /***** some data processing*****/
 
         HAL_I2C_Slave_Transmit(&hi2c1, re_buffer, 10, i2c_timeout);
 
         HAL_Delay(2);
}

and the code on Arduino side show as below:

void setup()
{
      Wire.begin();
      Wire.setClock(400000);
      Serial.begin(115200);
}
 
void loop()
{
      Wire.beginTransmission(NUCLEO_Address);
      Wire.write(my_buffer, command_size);
      ack = Wire.endTransmission();
      while((ack!=0))       /* Wire.endTransmission() returns 
                                              0:success
                                              1:data too long to fit in transmit buffer
                                              2:received NACK on transmit of address
                                              3:received NACK on transmit of data
                                              4:other error */
      {
       Wire.write(my_buffer, command_size);
       ack = Wire.endTransmission();
 
      }
 
      if((Wire.requestFrom(NUCLEO_Address,receive_size,true) == receive_size) && (Wire.available()>=0))               
      {
           for(int i = 0; i < receive_size; i++)
          {
               re_buffer[i] = Wire.read();
           }
 
           /******** some data processing*********/
 
           Serial.println(output);
       }
 
   delay(10);
}
 

There are 2 issues I don't known why it occurs and also how to solve it.

First, in the STM MCU code, I have to initialize the I2C in the begining of every loop, otherweise the I2C BUS was very easy to lock and the Arduino received NACK on transmit of address.

Second, the I2C communication between Arduino and STM32 is sensitive to the delay() command at end of while loop, it seems like there are some relationship between the delay time on Arduino and STM MCU, just like PID parameter, which must to be tuned.

I don't know if it is because of the I2C synchronous communication protocol or something else. Is anyone know about that ?

0 REPLIES 0