cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 I2C multi-device connection

MuratHepeyiler
Associate II

Hello to everyone, I am a newbie on STM MCUs. I need to communicate 6 slaves and 1 master device. But I could not figure out. The master device has been selected Arduino mega due to some specifications. Slaves are responsible to drive motors, Arduino sends to 4-byte data. I can communicate only one master and slave. But if the slave up to one, they cannot communicate properly. 

4 REPLIES 4
Uwe Bonnes
Principal III

Read some more explanation around I2C. As long as all slaves have different addresses, you can have multiple I2C slave on a bus and talk to each slave selectively by addressing the slave. If the Masters implement the full I2C protocoll, even multiple masters can be on the bus...

S.Ma
Principal

Give the ref part number of the slave devices and their unique I2C slave addresses.

This will be the start.

MuratHepeyiler
Associate II

Yes, I have done it. Actually, I have problem with timing. My code is below. Arduino cannot send proper data each device. Is there any function to onRecieve like that? I have checked but I could not find.

while(1)
{
HAL_I2C_Slave_Receive(&hi2c1, testArr, 4, 50);
decode();
if(old_vel != vel){
	__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_1,vel);//ccw
        __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,0);//cw
	old_vel = vel;
}
}

I meet the same problem too.

What I doing right now is, use 1 Arduino Due as master to communicate with 4 STM32 boards, which I have set to fast mode(400kHz) and each has an different I2C ID(31,32,33,34), like this

0690X000008ibFMQAY.png

1 Arduino communicates with 1 STM32 bord is ok, everything correct, but when I test with 2 STM32 bord, it become strange, the data got from STM32 sometimes is correct, but most of time is totally wrong, shown in the following picture0690X000008ibExQAI.png

the first and fifth line is exactly what I want, but the other is complete wrong. And I have found that , when I change the I2C timeout in STM32 0690X000008ibIQQAY.png0690X000008ibIVQAY.pngor the delay time at the end of the Arduino code, it can also effect the I2C communication. Is anyone kown why?

while(1)
{
 
....
 
for(int i = 0; i < channel_num; i++)
  {
    if(i == 0)
    {
      tcaselect(0);
      /*  send data to Slave1  */
      Wire.beginTransmission(motor1address);   
      Wire.write(my_buffer1,my_size);
      Wire.endTransmission();
      
      /*  Begin a transmission to the I2C Slave device with address #31  */
      Wire.beginTransmission(motor1address);
      /*  Wire.requestFrom(AA,BB);receive the data form slave.
          AA: Slave Address ; BB: Data Bytes  */
      Wire.requestFrom(motor1address,my_size);               // request data from slave device #31
      /*  Wire.available: Returns the number of bytes available for retrieval with read() */
      if(Wire.available()>0)    
      {
        for(int i = 0; i < my_size; i++)          //get data from STM32
        {
        re_buffer1[i] = Wire.read();             //store STM32 data to re_buffer1
        } 
    
....
    
        outdata1 = (motor1state + "," + String(outfinalspeed_1) + "," + String(outdurationms_1) + "," + String(outspeedref_1) + "," 
        + String(outspeedave_1) + "," + String(outdirection_1));
        
      }
      else
      {
        outdata1 = "I2C is not available for motor1";
      }
      Wire.endTransmission();
      Serial.println("Motor1:" + outdata1);
    }
 
....
 
  delay(20);
}