2019-01-22 01:58 AM
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;
}
2019-01-22 03:29 AM
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");
}
2019-01-22 07:25 AM
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.
2019-01-22 07:45 AM
__IO
2019-01-22 07:47 AM
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.
2019-01-22 07:51 AM
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 ?
2019-02-13 01:29 PM
@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?
2019-02-13 10:23 PM
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.
2019-02-14 01:36 AM
@JJavi could you tell more as in what sort of errors you get ?
2019-02-14 01:51 AM