AnsweredAssumed Answered

I2C bus scanner

Question asked by ben biles on Apr 10, 2017
Latest reply on Apr 10, 2017 by ben biles

Hi , I'm trying to make a inple I2C bus scanner ( re-writing simple arduino I2C scanner code )

I am not sure how to return the error if there a device address exits?

1st, am i trying to re-invent the wheel and there is already a working I2C bus code out there for ST32Fx ?

Sorry if my questions are a bit basic , i'm new to ST32

 

Here is my 1st attemps , I'm not even sure if the i2c bus definition I2C1 is correct.

The I2C bus should be initialised since I'm using the audio recorder playback application code from CubeMX that works with the codec IC. I'm just adding this code as a function in that example executing the function I2CSCANNER(); from the while loop.

 

void I2CSCANNER(void) 

{
    unsigned char error, address;
    int nDevices;

    nDevices = 0;

 

  BSP_LCD_SetTextColor(LCD_COLOR_GREEN);
  BSP_LCD_SetFont(&LCD_LOG_HEADER_FONT);

  BSP_LCD_DisplayStringAtLine(15, (uint8_t *)"Scanning...");

 

            

    // do i need  HAL_I2C_Master_Abort_IT(I2C1, devaddress); to make the write happen?

    // how do I know if a  device did acknowledge the address when I write to it ?

           
             for(address = 01; address < 127; address++ )
             {
          
               // print current device being scanned on screen
               sprintf(buffera, "0x%02x", address);
               BSP_LCD_ClearStringLine(20);
               BSP_LCD_DisplayStringAtLine(20, (uint8_t *)"     .....device being scanned..");
               BSP_LCD_DisplayStringAtLine(20, (uint8_t *) buffera);
               HAL_Delay(10);

 

BSP_LCD_Clear(LCD_COLOR_BLACK); // clear screen

 

               uint16_t devaddress = address;  // cast to uint16_t
               uint32_t timeout = 10;
               uint16_t i2cpacketsize = 1;
               uint8_t rdata = 0;
               nulldata[0] = 0;

 

   error =  HAL_I2C_Master_Transmit (I2C1, devaddress, nulldata, i2cpacketsize, timeout);

 

// can I used any of these ??????????? 

//  HAL_I2C_Master_Receive(I2C1, devaddress, rdata, 1, timeout); // could i use this?       
  // error = Wire.endTransmission(); // original arduino scanner code
  // error = HAL_I2C_Master_Abort_IT(I2C1, devaddress);  // does this return anything ?

 

        HAL_Delay(10);

 

               if (error == 0)
               {

 

               BSP_LCD_DisplayStringAtLine(16, (uint8_t *)"I2C device found at address 0x");

 

                 if (address<16)

 

                BSP_LCD_DisplayStringAtLine(17, (uint8_t *)"0");

 

           sprintf(buffera, "0x%02x", address) ;
           BSP_LCD_DisplayStringAtLine(17, (uint8_t *) buffera) ;  // make this hex code !!!!!!!!!!!!!
           BSP_LCD_DisplayStringAtLine(17, (uint8_t *)"   !");

 

                 nDevices++;
               }
               else if (error==4)
               {
                   BSP_LCD_DisplayStringAtLine(17, (uint8_t *)"Unknown error at address 0x");
                

             if (address<16)
                     BSP_LCD_DisplayStringAtLine(17, (uint8_t *)"0");

                     sprintf(buffera, "0x%02x", address) ;  // put the hex number in buffera string as a hex number
                      BSP_LCD_DisplayStringAtLine(17, (uint8_t *) buffera);  // print the hex number being scanned
               }
             }
             if (nDevices == 0)

 

              BSP_LCD_DisplayStringAtLine(17, (uint8_t *)"No I2C devices found\n");
             else
               BSP_LCD_DisplayStringAtLine(17, (uint8_t *)"done\n");

 

            HAL_Delay(1000);           // wait for next scan

           

           }

Outcomes