cancel
Showing results for 
Search instead for 
Did you mean: 

[STM32F103C8T6] SPI stuck on HAL_SPI_TransmitReceive using Receive Only Master mode but works on Full-Duplex Master

nicosan
Associate

Hello, everyone this is my first post here.

I'm starting to learn Embedded Programming and I choose STM32F103C8T6 using Blue Pill development board. I'm trying to understand SPI communication, my goal is to read multiple input using 74HC165 IC. I'm able to read that successfully when I set SPI1 to use Full-Duplex Master. But since I just want to receive, I thought why not use the Receive Only Master to save 1 pin(SPI_MOSI) since it won't be used anyway, [but the problem is somehow when I use Receive Only Master the clock pulse is not generated for SPI_SCK pin] The clock turns out to be generated but it stuck on this line HAL_SPI_TransmitReceive when I debug it and I can't get over it . Can anyone help me understand what is going on and what I miss? I also attached the spi and main file.

Please let me know in case I need to provide more information and thank you in advance everyone!

 

4 REPLIES 4
ELABI.1
ST Employee

Hi @nicosan,

To configure the SPI in "Receive Only" mode, you need to set it to Half-Duplex reception mode. In the spi.c file, change hspi1.Init.Direction = SPI_DIRECTION_2LINES; on hspi1.Init.Direction = SPI_DIRECTION_1LINE; (change to 1 line for Receive Only). Additionally, ensure that the MOSI pin PB5 is not configured if it is not used.

Please check these points and provide me with an update.

Thank you.

Eya

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Thank you very much for the response ELABI.1, but if I set Half-Duplex is that means I got no input on my MCU? since the available pin for Half-Duplex is PB5(MOSI) and PB3(SCK)? I want to be able to read input from the 74HC165 IC so I think I will need MISO instead (correct me if I'm wrong). 

So I was thinking to use the Receive Only Master which automatically going to generate this for me when save .ioc file

  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 10;

which configure hspi1.Init.Direction = SPI_DIRECTION_2LINES_RXONLY; 

I tried to use Logic Analyzer I just bought to see how it's going and actually it seems it still generated a clock using Receive Only Master, but somehow it just loops on this call HAL_SPI_TransmitReceive(&hspi1, &dummy, data, 2, HAL_MAX_DELAY) != HAL_OK and keep generating clock then when I debug it I just can't get over that line

nicosan_0-1723122656641.png

But using hspi1.Init.Direction = SPI_DIRECTION_2LINES; (Full-Duplex Master) it works I can get over that HAL_SPI_TransmitReceive line. The logic is also seems to be correct

nicosan_1-1723123191798.png


Thank you.

 



TDK
Guru

The code you attached initializes the SPI in 2 lines and calls HAL_SPI_TransmitReceive. Since this code works, perhaps you meant to attach the code that isn't working?

Should be working, but I'd recommend using 2-lines mode and just leaving MOSI disconnected. Receive-only can generate extra clocks.

 

If you feel a post has answered your question, please click "Accept as Solution".

My bad, I should posted the not working code. I just changed the attachment (the spi.c since the main.c is the same) and change the tittle + description because indeed it produce a clock but seems to be stuck. 

And I will follow your recommendation, but do you know what possibly the cause or do we we have a documentation that give the usage example especially for the Receive Only Master? Since I'm curious why it seems to be stuck on clocking and can't get past HAL_SPI_TransmitReceive() while the full duplex master is not.

Thank you TDK