SPI_Receive reading different values on each read

23 Feb 2019

Hi guys, so I am trying to read the content of one of my Control Registers but every time I am reading it, without changing value, the SPI_Receive reads different value. I am looking at it with my project leader for more than 10 hours and we just can't figure it out. The sensor I am using is LSM9DS1 and the microcontroller is STM32L432KC. Here is my read function. I am trying to use Transmit and TransmitReceive, but none of them is working. The driver direction is 0x80 / 0x00 base on reading or writing. The CS_Activate and Deactivate are used to enable SPI communication. Driver.data is an uint8_t array of 12 items. The "n" parameter is count of bytes. Any ideas how to fix this?

spi_read

void spi_read(uint8_t n)
{
	driver.data[0] = driver.direction | (driver.address & 0x3F);
	driver.data[1] = 0;
	CS_Activate();
	HAL_SPI_TransmitReceive(driver.hspi, driver.data, driver.data, n + 1, 1000);
	CS_Deactivate();

	driver.data[0] = driver.direction | (driver.address & 0x3F);
	uint8_t data_temp[2];
	data_temp[0] = 0;
	data_temp[1] = 0;
	CS_Activate();
	HAL_SPI_Transmit(driver.hspi, driver.data, n /*+ 1*/, 1000);
	HAL_SPI_Receive(driver.hspi, data_temp, n /*+ 1*/, 1000);
	CS_Deactivate();
}
23 Feb 2019

This is screenshot from Login analyzer. /media/uploads/Qido/wrong_spi.png

24 Feb 2019

The logic analyser shows that the reply bitstream on MISO is pretty much the same for both reads. Decoding is wrong because you have selected the wrong SPI mode in the settings of your analyser (which looks like it is a Saleae). The arrow in SCK shows that MOSI is currently sampled on the first (ie falling) edge. That should be the second (ie rising) edge for your sensor device. MISO will be sampled on the next edge. Check the SPI mode of your HAL driver in case the software also gives you the same incorrect values.

24 Feb 2019

Thanks, I am gonna try it. Hope this will fix it.

25 Feb 2019

Wim Huiskamp wrote:

The logic analyser shows that the reply bitstream on MISO is pretty much the same for both reads. Decoding is wrong because you have selected the wrong SPI mode in the settings of your analyser (which looks like it is a Saleae). The arrow in SCK shows that MOSI is currently sampled on the first (ie falling) edge. That should be the second (ie rising) edge for your sensor device. MISO will be sampled on the next edge. Check the SPI mode of your HAL driver in case the software also gives you the same incorrect values.

Thanks a lot. This finally fixed the issue.