Archon
Archon

Reputation: 636

STM32 I2C scan returns different addresses than Arduino

Good day

The goal is to use the LSM9DS0 with a ST chip.

The situation is that the I2C address as returned by the scanner (ST environment) is not the same to that of Arduino I2C scanner. I am using a STM32 Nucleo-F429 and ESP32 devkit.

When I scan for I2C addresses using the below code, it returns the following four addresses:

0x3A
0x3B
0xD6
0xD7

However I have used this very IMU breakout on a ESP32 before and I noticed that the addresses are not the same. When I run the I2C scanning code I get the following addresses.

0x1D
0x6B

STM32 code: Src files were generated by CubeMX. Let me know if the i2c.h/c are required. But they should be pretty standard.

for (uint16_t i = 0; i < 255; i++)
{
    if (HAL_I2C_IsDeviceReady(&hi2c1, i, 5, 50) == HAL_OK)
    {
        HAL_GPIO_TogglePin(LED_RED_PORT, LED_RED_PIN);

        char I2cMessage[10];
        sprintf(I2cMessage, "%d , 0x%X\r\n", i, i);
        HAL_UART_Transmit(&huart2, (uint8_t *)I2cMessage, strlen(I2cMessage), 10);
    }
}

Arduino code:

#include <Wire.h>
 
 
void setup()
{
  Wire.begin();
 
  Serial.begin(115200);
  while (!Serial);             // Leonardo: wait for serial monitor
  Serial.println("\nI2C Scanner");
}
 
 
void loop()
{
  byte error, address;
  int nDevices;
 
  Serial.println("Scanning...");
 
  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
 
    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
 
      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
 
  delay(5000);           // wait 5 seconds for next scan
}

Does anyone know why this is and is it a problem?

Upvotes: 0

Views: 1338

Answers (2)

Miguel Tom&#225;s
Miguel Tom&#225;s

Reputation: 1911

one can find I2C devices on an ESP32 using the code below:

  void I2Cscanner() {
  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;

  Wire.begin();
  for (byte i = 8; i < 120; i++)
  {
    Wire.beginTransmission (i);          // Begin I2C transmission Address (i)
    if (Wire.endTransmission () == 0)  // Receive 0 = success (ACK response) 
    {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);     // PCF8574 7 bit address
      Serial.println (")");
      count++;
    }
  }
  Serial.print ("Found ");      
  Serial.print (count, DEC);        // numbers of devices
  Serial.println (" device(s).");
}

Upvotes: 0

Archon
Archon

Reputation: 636

I found the answer in the description of the HAL API. The API requires the 7bt address to be bit shifted to the left by 1bit.

Within the file: stm32F4xx_hal_i2c.c the description of the HAL_I2C_IsDeviceReady() API says the following:

* @param  DevAddress Target device address: The device 7 bits address value
*         in datasheet must be shifted to the left before calling the interface

Thus, change the IsDeviceReady arguments as follows and it will work.

for (uint16_t i = 0; i < 255; i++)
    {
        if (HAL_I2C_IsDeviceReady(&hi2c1, i<<1, 5, 10) == HAL_OK)
        {
            // Do crazy stuff
        }
    }

This worked for me. Hope it helps anyone with the same issue

Upvotes: 0

Related Questions