2024-02-27 07:29 AM - edited 2024-02-27 07:43 AM
Hello,
I have a custom board with a STM32L010C6 MCU, which periodically reads 3 bytes (24 bit array) from an external component through SPI.
I only need SCLK and MISO since the frame resets using a timeout.
I configured the SPI as Only-Receiving Master, which didn't work OK. Now I am using the SPI peripheral as a Full-Duplex Master with software NSS and I perform data reads by polling function HAL_SPI_Receive().
It seems to work OK, with some delay between bytes.
The problem is that I periodically get bad data in the MCU. I identified that this happens when the SPI clock is not divided by 8-bit packets.
When this happens, the data transmitted by the external sensor is OK but the MCU doesn't read the correct value. In addition, I don't get any error state from the function, so I don't have a way to skip wrong values.
I tried reinitializing the SPI peripheral every time I want to perform a reading, but the problem persists.
Is there any reason which leads to this behaviour?
2024-02-27 09:38 AM - edited 2024-02-27 10:05 AM
You don't say in what way the data is wrong. Is is garbled bits, missing bytes, repeated bytes?
Obviously, since you a operating as spi master, as a work-around you could just force a pause between bytes.
2024-02-27 11:22 AM
>>I only need SCLK and MISO since the frame resets using a timeout.
Ok, so how does that maintain synchronization?
Typically the start of words / bytes is aligned to a CHIP SELECT (CS)
>>from an external component through SPI
What one? Does it have a data sheet? Does that set or express expectations?
2024-02-27 10:32 PM
After digesting your issue, here my thoughts (steps to verify):
2024-02-28 07:05 AM
>>You don't say in what way the data is wrong. Is is garbled bits, missing bytes, repeated bytes?
In wrong reads, the bytes are shuffled.
>>Ok, so how does that maintain synchronization?
The synchronization is kept by using a minimum delay between samples. The clock signal is also used to detect when the frame starts so, after the frame timeout is reached, both sides of communication know the transaction is finished.
The proof that synchronization is kept OK without CS signal is that I have already solved the problem and it is working perfectly.
The problem was in the HAL function timeout. I was using a 1ms timeout since the frame timeout is about 100us, but it randomly triggered the function timeout too early. I set the timeout to HAL_MAX_DELAY since the MCU is the one who starts and ends the transaction and now it is working the right way. However, I have had to use a higher delay between samples (about 150us) to ensure the data is latched.