AnsweredAssumed Answered

I cannot get I2C work on stm32l476

Question asked by Peter Curtis on Jan 19, 2018
Latest reply on Jan 27, 2018 by T J

Hi All,


First post on here and a newbie with the STM32 family of devices (I come from a PIC background - sorry about that)


I have just received my first STM32 board design back from the sub contractors and am having a little trouble and am hoping someone can point me in the right direction.


First of all, I'm using keil as the IDE/Compiler.  I have configured the micro using CUBE and am using the HAL libraries. 


My issue is that I cannot get I2C_MEM_WRITE or READ to work.


On the first use of > if(HAL_I2C_IsDeviceReady(&hi2c1, I2C_ROM, 1, 100) == HAL_OK)

I get an OK.  When I try a mem_write it fails (that is HAL_OK returns 0).


I have check my soldering and even shortened the i2c bus by truncating it immediately after the cat24c512 with a craft knife (and adding 2x10k resistors to vcc), but still no luck.


The device address (I2C_ROM) is defined as 0xA0.  I have tried playing with the timeout also.


I notice that in CUBE things like rise and fall time can be set - not idea what to set these to (didn't have to worry about that in the world of PIC).


Any pointers would be most appreciated.


Here is the code:-


uint8_t WriteConfigToI2C(void)
    char buffer[5] = "TEST";
    uint8_t i;
  if(HAL_I2C_IsDeviceReady(&hi2c1, I2C_ROM, 1, 100) == HAL_OK)  //i2c address of eeprom = 0b1010000 = 0xa0
       HAL_UART_Transmit(&hlpuart1, (uint8_t*)"\r\nWrite", 7, 1000);   //(uint8_t*)"\r\nready"
      if(HAL_I2C_Mem_Write(&hi2c1, I2C_ROM, 0x00, 0xFFFF, (uint8_t*)buffer, strlen(buffer), 100) == HAL_OK)  // write       'buffer' to I2C mem
          HAL_UART_Transmit(&hlpuart1, (uint8_t*)": OK", 4, 1000); // send 'OK' to uart if success
          HAL_Delay(100); // wait for eeprom to commit / finish
          // clear buffer
          for(i = 0; i < 50; i++)
               buffer[i] = 0; 
        // read the memory and write it back out on uart
       HAL_I2C_Mem_Read(&hi2c1, I2C_ROM, 0x00, 0xFFFF, (uint8_t*)buffer, 5, 100);  // read back eeprom and put into        'i2cBuffer'
        HAL_UART_Transmit(&hlpuart1, (uint8_t*)buffer, strlen(buffer), 1000);  // write 'i2cBuffer' to uart
     HAL_UART_Transmit(&hlpuart1, (uint8_t*)": FAIL", 6, 1000);  // failed to write
    HAL_UART_Transmit(&hlpuart1, (uint8_t*)"\r\nNotReady", 10, 1000);