Reading a 16-bit word via SPI from a current sensor [STM32 & CubeMx]

Thread Starter

Xavier Pacheco Paulino

Joined Oct 21, 2015
728
I'm having problems to read my current sensor, which is located at the source leg of a power transistor.

This is my sensor datasheet TLI4970

This is my routine every 10ms:

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); //Bring slave select low
HAL_SPI_Receive(&hspi1, (uint8_t *)&SPIRx, 2, 10); //Receive data
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); //Bring slave select high

However, I get random values even when the current is zero. I'm getting values like:
0x8800
0x8803
0x8801
0xA7FF
0x87FC
0x97FE
0xA802
0xA801
0x97FD
0x87FD
0xA800
0xA7FE

According to the datasheet, these are sensor status messages, but none of them are Sensor Current Messages. The hardware seems okay. This sensor is not so common I think. I haven't found much help. Does anyone here have worked with SPI in STM32?
 

nsaspook

Joined Aug 27, 2009
9,991
Check the SPI mode: SPI Mode '1' (CPOL 0 - clock idles low and CPHA 1 clock phase has falling edge on data sample)

The CS (GPIO) to SCK start/stop delay times should be at least 100ns. Tcss/Tcsh
 

Thread Starter

Xavier Pacheco Paulino

Joined Oct 21, 2015
728
I've captured a new pattern of received data and all of them start at either 8 or A in the MSB position. I don't expect to receive "1" in the MSB bit after the first message which is the status. It's like noise, because if I ignore the first bits, the measurement makes sense a little bit.
 

Thread Starter

Xavier Pacheco Paulino

Joined Oct 21, 2015
728
Check the SPI mode: SPI Mode '1' (CPOL 0 - clock idles low and CPHA 1 clock phase has falling edge on data sample)

The CS (GPIO) to SCK start/stop delay times should be at least 100ns. Tcss/Tcsh
Thank you! That was the problem. I had SP1 mode 0. But the right one is the Mode 1. I had misinterpreted a line in my initialization code.
 
Top