Skip to main content
RS009
Associate II
January 10, 2023
Solved

HAL_SPI_Transmit() API

  • January 10, 2023
  • 3 replies
  • 13889 views

I observed the data on both MOSI and MISO on logic analyzer, even when I use only one of the HAL_SPI_Transmit() or HAL_SPI_Receive(), why?

This topic has been closed for replies.
Best answer by S.Ma

This time only, for fun.

Part 1: chatGPT:

When using the HAL_SPI_Transmit() or HAL_SPI_Receive() functions on the STM32, the MOSI and MISO lines are both active. The MOSI line is used to send data from the STM32 to the slave device, while the MISO line is used to receive data from the slave device. Even if you are only transmitting or receiving data in one direction, the MOSI and MISO lines may still be active as the slave device might be configured to send dummy data on the MISO line. Additionally, some slave devices may require that the MOSI line be driven with a specific value (such as a "0") during idle periods to properly function.

3 replies

gbm
Lead III
January 10, 2023

Cause this is how SPI works. The transfer always goes in both directions, even if one of the wires is not present. ;)

To receive, master must send data - it may be all zeros, all ones or some random stuff. The master may ignore received data but if there is some data signal from slave to master, the slave is sending something.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
RS009
RS009Author
Associate II
January 10, 2023

oh ok, got it, I think MISO line/wire comes to picture only when we use SPI_Receive API,

I want to write and read back one slave register how to do that?

e.g., Reg = 0x2C 10 1E, first byte is address and second two bytes are data,

 uint8_t Rx_Data[2];

  uint8_t R44[3] = {0x2C,0x10,0x1E};

 uint8_t Buf[]="Application is Running\n";

 HAL_UART_Transmit(&huart3, Buf, strlen((char *)Buf), 1000);

 HAL_GPIO_WritePin(GPIOF, GPIO_PIN_12, GPIO_PIN_SET);

 while (1)

 {

 while(! HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13));//on board user btn

 HAL_Delay(200);

 HAL_GPIO_WritePin(GPIOF, GPIO_PIN_12, GPIO_PIN_RESET);

 HAL_SPI_Transmit(&hspi1, R44, 1, 1000);

 HAL_SPI_Transmit(&hspi1, &R44[1], 2, 1000);

 HAL_GPIO_WritePin(GPIOF, GPIO_PIN_12, GPIO_PIN_SET);

 strcpy((char *)Buf,"Programming is Done\n");

 HAL_UART_Transmit(&huart3, Buf, strlen((char *)Buf), 1000);

 HAL_GPIO_WritePin(GPIOF, GPIO_PIN_12, GPIO_PIN_RESET);

 HAL_SPI_Transmit(&hspi1, R44, 1, 1000);

 HAL_SPI_Receive(&hspi1, Rx_Data, 2, 1000);

 HAL_GPIO_WritePin(GPIOF, GPIO_PIN_12, GPIO_PIN_SET);

 sprintf((char*)Buf,"Data is : 0x%X 0x%X \n",Rx_Data[0],Rx_Data[1]);

 HAL_UART_Transmit(&huart3,Buf, strlen((char *)Buf), 1000);

here is my code, please correct me if I am wrong, @Bold line, in logic analyzer I observed that it transmits 3 bytes, why?

Wijeden RHIMI
ST Employee
January 11, 2023

Hello @RS009 

Thank you for posting your issue !

  • Could you please provide us a screenshot on which we can see the data In logic analyzer?

Best regards,

Wijeden,

S.Ma
Principal
January 12, 2023

I think chatGPT can explain SPI and how to generate code example snippets on STM32

Pavel A.
Super User
January 12, 2023

> I think chatGPT can explain SPI 

LOL. Could you share some example? We've already seen some bot generated posts here even so often.

S.Ma
Principal
January 14, 2023

Once chatGPT answers with formatted code source can be html linked, why not?