cancel
Showing results for 
Search instead for 
Did you mean: 

Getting correct register value when reading "WHO_AM_I" register on LPS25HB mems sensor but the wrong values read from the sensor. (STM32G474RE)

LincolnWallace
Associate

I have a board developed by one of my colleague, that uses a STM32G474RE microcontroller and in his SPI1 I had both an LPS25HB (Mems ST barometer) and too an acelerometer SCA3300. For the LPS25HB I'm using the following driver:

https://github.com/STMicroelectronics/STMems_Standard_C_drivers/tree/master/lps25hb_STdC

It's possible ignore the details of the driver code but the important thing to note is that

The driver requires you to write your own "platform_read" and "plataform_write" functions since this is platform dependent.

I am using SPI and this is how my implementation of "plataform_read" is writed:

int32_t platform_read_bar(void *handle, uint8_t reg, uint8_t *bufp,
		uint16_t len) {
 
	HAL_GPIO_WritePin(DEMUX_KEY01_GPIO_Port, DEMUX_KEY01_Pin, GPIO_PIN_RESET);
	delay_us(5); // 5 us delay
	reg |= 0x80;
	HAL_GPIO_WritePin(CS_PIN01_GPIO_Port, CS_PIN01_Pin, GPIO_PIN_RESET);//Begin of transmission
	delay_us(5); // 5 us delay
	Status_SPI = HAL_SPI_Transmit(handle, &reg, 1, 1000);
 
	Status_SPI = HAL_SPI_Receive(handle, bufp, len, 1000);
	delay_us(5); // 5 us delay
	HAL_GPIO_WritePin(CS_PIN01_GPIO_Port, CS_PIN01_Pin, GPIO_PIN_SET);	//End of transmission
	return 0;
}

And my implementation of plataform write function looks like:

int32_t platform_write_bar(void *handle, uint8_t reg, const uint8_t *bufp,
		uint16_t len) {
 
	HAL_GPIO_WritePin(DEMUX_KEY01_GPIO_Port, DEMUX_KEY01_Pin, GPIO_PIN_RESET);
	delay_us(5);
	HAL_GPIO_WritePin(CS_PIN01_GPIO_Port, CS_PIN01_Pin, GPIO_PIN_RESET); //Begin of transmission
	delay_us(5);
	Status_SPI = HAL_SPI_Transmit(handle, &reg, 1, 1000);
	Status_SPI = HAL_SPI_Transmit(handle, (uint8_t*) bufp, len, 1000);
	delay_us(5);
	HAL_GPIO_WritePin(CS_PIN01_GPIO_Port, CS_PIN01_Pin, GPIO_PIN_SET);	//End of transmission
	return 0;
}

When I start to debug the code I have this situation:

0693W00000Dm7jGQAR.jpgI'm runing my code using FreeRTOS (CMSIS_V2), in the task of barometer readings I'm checking all the time WhoAmI register and puting in two variable counters, one for erros and the other for correct readings. I realize that one time a wrong reading happen and after this only correct readings happen (10111101 value or 189 in decimal as explained in datasheet). The pressure readed in hPa was 402982.125 hPa the expected values for my city is something around 1000 - 1020 hPa and the temperature in celsius degrees the expected was 22 - 26 celsius degrees and not 62.84ºC.

My SPI init:

static void MX_SPI1_Init(void)
{
 
  /* USER CODE BEGIN SPI1_Init 0 */
 
  /* USER CODE END SPI1_Init 0 */
 
  /* USER CODE BEGIN SPI1_Init 1 */
 
  /* USER CODE END SPI1_Init 1 */
  /* SPI1 parameter configuration*/
  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
  hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 7;
  hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI1_Init 2 */
 
  /* USER CODE END SPI1_Init 2 */
 
}

Regards,

Lincoln

2 REPLIES 2
LincolnWallace
Associate

More info:

I taken this SPI configs from datasheet informations.

What I have tried (without success):

  • Changing the baud rate prescaler.
  • changing CPOL to "Low" (when I make this change I notice that I have rigth values from WhoAmI register but whong and nonsense values from sensor )
  • changing CPHA to "1 Edges"
  • set a delay after setting CS line to high/low
  • set a delay before/after reading with SPI_Transmit / SPI_Recieve

I'm without ideas about how to fix this, can anyone give some options?

Regards,

Lincoln

Hi Lincoln,

Were you able to communicate with the SCA3300 sensor? If so, can you share the driver you used and maybe an example code? I have been trying to integrate sca3300 sensor with nucleo board and still unsuccessful. 

 

Best Regards,

Shubham