STM32 and SPI confusion - Page 1
Forum section: electronics / beginners

author: Veketti
author_rank: Regular Contributor
timestamp: October 27, 2024, 07:47:46 am
content: Hi,I'm making a flue gas temperature meter to my fireplace that uses a K-type probe and MAX31855K thermocouple-to-digital converter which is interfaced by STM32G070CBT6. I used a DSLogic plus logic analyser to check the traffic between MCU and MAX31855K and noticed that even though I read four bytes it sends six bytes. Well, as it only uses the first four bytes it works just fine, but I would like to know what is the reason that I get six bytes instead?Here's the code for receiving by SPI:Code:HAL_SPI_Receive(MAX31855->hspi, payload, 4, 1000); // receive four bytes from MAX31855And here's the capture from logic analyser.edit: My understanding is that the MCU is the culprit here as it sends six clock cycles and CS is pulled low so long. MAX31855 is just responding with four bytes.

author: pcprogrammer
author_rank: Super Contributor
timestamp: October 27, 2024, 09:27:42 am
content: Indeed the MCU is responsible for the six byte read sequence. Why can only be determined by diving into the HAL code. The datasheet of the MAX31855 does not show any reason why six bytes need to be read.According to the datasheet you could even just read 14 bits if all you want is the thermocouple temperature data.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 27, 2024, 09:29:55 am
content: Probably the SPI is set to RX-only mode.In this mode it'll send clocks non-stop for best efficiency, so you don't need to clear flags for the next transfer to start, when you read DR register the next byte is already being clocked in.The downside of this mode is this, it'll always send extra clocks until the code stops the peripheral.Try:Code:HAL_SPI_TransmitReceive(MAX31855->hspi, payload, payload, 4, 1000); // receive four bytes from MAX31855This code is slower, will send whatever it's in your payload buffer and overwrite it with the received data, but controls every byte/clock accurately.You don't need to setup MOSI pin, the sent data goes nowhere.If you're using MOSI for other SPI devices, no problem either, they'll be inhibited.

author: Veketti
author_rank: Regular Contributor
timestamp: October 27, 2024, 06:47:06 pm
content: Indeed it is RX-only mode. I found yourpostfrom 2021 but only after I had ordered the PCB and the MOSI pin was allocated to CS so I couldn't use the bi-directional SPI, but with the reset code you included in that post I managed to get that RX only mode to work.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 27, 2024, 08:09:13 pm
content: As I already said, you can use bidirectional mode without MOSI / SDO pin, leave it as gpio.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: October 28, 2024, 05:30:25 pm
content: My own opinion about HAL, is that it may be nice to get started quickly and have a (somewhat?) working example, but that the HAL code is not suited for real projects. So read the sourcecode, pick out what you need and put it in your own library and then discard the rest. Pay special attention to whether workarounds for bugs have been implemented in the HAL code.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 28, 2024, 07:05:00 pm
content: Or use LL which is great.