Skip to main content
ASING.91
Associate III
January 22, 2019
Question

I2C not working on STM32L053R8T6 ?

  • January 22, 2019
  • 12 replies
  • 2485 views

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;

}

This topic has been closed for replies.

12 replies

NAmat
Associate II
January 22, 2019

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");

}

ASING.91
ASING.91Author
Associate III
January 22, 2019

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.

S.Ma
Principal
January 22, 2019

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
ASING.91Author
Associate III
January 22, 2019

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 ?

S.Ma
Principal
February 15, 2019

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

ASING.91
ASING.91Author
Associate III
January 22, 2019

__IO

JJavi
Associate
February 13, 2019

@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?

ASING.91
ASING.91Author
Associate III
February 14, 2019

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

JJavi
Associate
February 14, 2019
NAmat
Associate II
February 14, 2019

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
Associate
February 14, 2019

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