cancel
Showing results for 
Search instead for 
Did you mean: 

I2C not working on STM32L053R8T6 ?

ASING.91
Associate II

I have added the code written in HAL for I2C communication between I2C 2 of STM32L053R8T6 and LIs3dh accelerometer to receive whoami register. doesn't seem to be able to receive any data from the accelerometer.

#include "stm32l0xx.h"

#include "stm32l0xx_hal_rcc.h"

#include "stm32l0xx_hal_gpio.h"

#include "stm32l0xx_hal_i2c.h"

#include "stdio.h"

#define lis3dh_address 0x19       //0x18 if sd0 connected to ground   //0x19 if sd0 connected to power supply

#define who_am_i 0x0F

#define BUFFERSIZE 1

uint8_t aRxBuffer[BUFFERSIZE];

__IO uint16_t hTxNumData = 0;

__IO uint16_t hRxNumData = 0;

uint8_t bTransferRequest = 0;

int i2c2_gpio_init(void);

int i2c2_init(void);

GPIO_InitTypeDef gpiob;

I2C_HandleTypeDef i2c2;

int main(void)

{

 __HAL_RCC_GPIOB_CLK_ENABLE();

 __HAL_RCC_I2C2_CLK_ENABLE();

   while(1)

   {

  

    i2c2_gpio_init();

    i2c2_init();

    hRxNumData=BUFFERSIZE;

    

    HAL_I2C_Mem_Read(&i2c2,(uint16_t)lis3dh_address,(uint16_t)who_am_i,1,(uint8_t*) aRxBuffer,hRxNumData,((uint32_t)25));

    uint8_t a= aRxBuffer[BUFFERSIZE-1];

    printf("%d",(uint8_t)a);

  }

}

int i2c2_gpio_init(void)

{

 gpiob.Pin = GPIO_PIN_13 |GPIO_PIN_14;

 gpiob.Mode = GPIO_MODE_AF_OD;

 gpiob.Pull = GPIO_PULLUP;

 gpiob.Speed = GPIO_SPEED_FAST;

 gpiob.Alternate = GPIO_AF5_I2C2;

 HAL_GPIO_Init(GPIOB, &gpiob);

 return 0;

}

int i2c2_init(void)

{

 i2c2.Instance = I2C2;

 i2c2.Init.Timing = 0x10420F13; i2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

 i2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;

 i2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

 i2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;

 i2c2.Init.OwnAddress1 = 0;

 i2c2.Init.OwnAddress2 = 0;

 HAL_I2C_Init(&i2c2);

 return 0;

}

12 REPLIES 12

I have this problem? Im trayiing with a simple example but the sensor never be ready

https://community.st.com/s/question/0D50X0000ALw0rUSQR/stm32l053-i2c-problems-with-lis3dh-sensor

@NAmat​  I have this error "HAL_I2C_ERROR_TIMEOUT"

Yes, I shared a simple generic code somewhere in this forum, which can be ported to any MCU easily.