Author Topic: STM32 and SPI confusion  (Read 562 times)

0 Members and 1 Guest are viewing this topic.

Offline VekettiTopic starter

  • Regular Contributor
  • *
  • Posts: 188
  • Country: fi
STM32 and SPI confusion
« on: October 27, 2024, 07:47:46 am »
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: [Select]
HAL_SPI_Receive(MAX31855->hspi, payload, 4, 1000); // receive four bytes from MAX31855
And 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.
« Last Edit: October 27, 2024, 07:56:26 am by Veketti »
 

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 4632
  • Country: nl
Re: STM32 and SPI confusion
« Reply #1 on: October 27, 2024, 09:27:42 am »
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.

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6331
  • Country: es
Re: STM32 and SPI confusion
« Reply #2 on: October 27, 2024, 09:29:55 am »
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: [Select]
HAL_SPI_TransmitReceive(MAX31855->hspi, payload, payload, 4, 1000); // receive four bytes from MAX31855

This 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.
« Last Edit: October 27, 2024, 09:35:20 am by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: Veketti

Offline VekettiTopic starter

  • Regular Contributor
  • *
  • Posts: 188
  • Country: fi
Re: STM32 and SPI confusion
« Reply #3 on: October 27, 2024, 06:47:06 pm »
Indeed it is RX-only mode. I found your post from 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.
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6331
  • Country: es
Re: STM32 and SPI confusion
« Reply #4 on: October 27, 2024, 08:09:13 pm »
As I already said, you can use bidirectional mode without MOSI / SDO pin, leave it as gpio.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 3982
  • Country: nl
Re: STM32 and SPI confusion
« Reply #5 on: October 28, 2024, 05:30:25 pm »
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.
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 6331
  • Country: es
Re: STM32 and SPI confusion
« Reply #6 on: October 28, 2024, 07:05:00 pm »
Or use LL which is great.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf