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
NAmat
Associate II

Please check with this function if the i2c slave device is ready

if(HAL_I2C_IsDeviceReady(&hi2c2,slave_address,10,100)==HAL_OK){

sprintf((char*)TxBuff,"\r\ni2c device ready");

}

else{

sprintf((char*)TxBuff,"\r\ni2c device fail");

}

S.Ma
Principal

Remember that the MEMs I2C bus mode is selected only when NSS (chip select) or its SPI is high (when not SPI slave selected, mems uses I2C)

As backup, for investigation, use I2C by GPIO emulation to validate the HW before SW.

ASING.91
Associate II

__IO

Well when I use the above function before the mem_read. After the program runs completely. I am getting the required value in the aRxBuffer, but I am also getting a hard fault error and the hardfault handler starts running as soon as the program ends. I am not able to understand why hardfault is occurring.

Well the MEMs is working fine with other microcontrollers. The I2C mode has to be selected by keeping CS pin high on accelerometer. Thank you for trying to help.

What do you mean when you say use I2C by GPIO emulation ?__ do you mean bit banging ?

JJavi
Associate II

@ASING.9​  I'm trying to use the LIS3DH sensor, I'm occupying Cubemx, I'm just starting two pins corresponding to the i2c and I'm activating the RCC with a ceramic crystal. However when I try to verify if the device is ready I have many problems. Do you have an advice for me?

NAmat
Associate II

Do you have a signal analyser? With signal analyser, you can cross verify the signal as per the datasheet so that you can figure out where you are lacking.

@JJavi​  could you tell more as in what sort of errors you get ?