cancel
Showing results for 
Search instead for 
Did you mean: 

LIS3DH Can read WHO AM I register But everything else freezes the program

Nick Alexis
Associate
Posted on September 26, 2017 at 09:49

 Can read WHO AM I register in LIS3DH succesfully but everything else from the functions of the universal ST driver freezes the Program. Any idea what might be wrong? I am kind of confused. 

2 REPLIES 2
Tilen MAJERLE
ST Employee
Posted on September 26, 2017 at 09:51

Hello

nalexopo

‌,

From your input, we are unable to know what is the issue. Please update your question with more info, including possible code which hangs.

Best regards,

Tilen

Nick Alexis
Associate
Posted on September 26, 2017 at 10:54

I am using Cube Mx and HAL to generate I2c interface code this is part of the main that manages the LIS3DH. It reads whoami correctly and then freezes without entering in my Error_Handler Function.

set_i2cHandle(&hi2c2);

LIS3DH_GetWHO_AM_I(i2cRxBuffer);

sprintf(aTxBuffer,'\n\r I AM HERE AFTER WHO AM I which reads as %d \n\r',i2cRxBuffer[0] );

HAL_UART_Transmit(&huart2,(uint8_t *) aTxBuffer, strlen(aTxBuffer), 500);

HAL_Delay(1000);

LIS3DH_SetODR(LIS3DH_ODR_400Hz);

sprintf(aTxBuffer,'\n\r I AM HERE AFTER SET UP \n\r');

HAL_UART_Transmit(&huart2,(uint8_t *)aTxBuffer, strlen(aTxBuffer), 500);

LIS3DH_SetMode(LIS3DH_NORMAL);

LIS3DH_SetFullScale(LIS3DH_FULLSCALE_2);

LIS3DH_SetAxis(LIS3DH_X_ENABLE | LIS3DH_Y_ENABLE | LIS3DH_Z_ENABLE);

sprintf(aTxBuffer,'\n\r HELLO \n\r');

HAL_UART_Transmit(&huart2,(uint8_t *) aTxBuffer, strlen(aTxBuffer), 500);

LIS3DH_GetWHO_AM_I(i2cRxBuffer);

sprintf(aTxBuffer,'\n\r I AM HERE AFTER WHO AM I which reads as %d \n\r',i2cRxBuffer[0] );

HAL_UART_Transmit(&huart2,(uint8_t *) aTxBuffer, strlen(aTxBuffer), 500);

while (1)

{

LIS3DH_Get6DPosition(i2cRxBuffer);

HAL_Delay(1000);

sprintf(aTxBuffer,'\n\r I AM IN WHILE AND 6D reads AS %d \n\r',i2cRxBuffer[0]);

HAL_UART_Transmit(&huart2,(uint8_t *) aTxBuffer, strlen(aTxBuffer), 500);

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

}

/* USER CODE END 3 */

}

This is my code on the driver

I2C_HandleTypeDef *hi2clisdh;

void set_i2cHandle(I2C_HandleTypeDef *i2chandle) {

hi2clisdh = i2chandle;

}

u8_t LIS3DH_ReadReg(u8_t Reg, u8_t* Data) {

LIS3DH_PointReg(Reg);

while(HAL_I2C_Master_Receive(hi2clisdh, (uint16_t)I2C_ADDRESS, (uint8_t *)Data, 10, 500) != HAL_OK)

{

/* Error_Handler() function is called when Timeout error occurs.

When Acknowledge failure occurs (Slave don't acknowledge it's address)

Master restarts communication */

if (HAL_I2C_GetError(hi2clisdh) != HAL_I2C_ERROR_AF)

{

Error_Handler();

}

}

//To be completed with either I2c or SPI reading function

//i.e. *Data = SPI_Mems_Read_Reg( Reg );

return 1;

}

u8_t LIS3DH_PointReg(u8_t Reg){

uint8_t buff[2];

buff[0]=Reg;

while(HAL_I2C_Master_Transmit(hi2clisdh, (uint16_t)I2C_ADDRESS, (uint8_t*)buff, 1,500)!= HAL_OK)

{

/* Error_Handler() function is called when Timeout error occurs.

When Acknowledge failure occurs (Slave don't acknowledge its address)

Master restarts communication */

if (HAL_I2C_GetError(hi2clisdh) != HAL_I2C_ERROR_AF)

{

Error_Handler();

}

}

//To be completed with either I2c or SPI writing function

//i.e. SPI_Mems_Write_Reg(WriteAddr, Data);

return 1;

}

/*******************************************************************************

* Function Name : LIS3DH_WriteReg

* Description : Generic Writing function. It must be fullfilled with either

* : I2C or SPI writing function

* Input : Register Address, Data to be written

* Output : None

* Return : None

*******************************************************************************/

u8_t LIS3DH_WriteReg(u8_t Reg, u8_t Data) {

u8_t buff[2];

buff[0]= Reg;

buff[1]= Data;

while(HAL_I2C_Master_Transmit(hi2clisdh, (uint16_t)I2C_ADDRESS, (uint8_t*)buff, 2,500)!= HAL_OK)

{

/* Error_Handler() function is called when Timeout error occurs.

When Acknowledge failure occurs (Slave don't acknowledge its address)

Master restarts communication */

if (HAL_I2C_GetError(hi2clisdh) != HAL_I2C_ERROR_AF)

{

Error_Handler();

}

}

//To be completed with either I2c or SPI writing function

//i.e. SPI_Mems_Write_Reg(WriteAddr, Data);

return 1;

}