Skip to main content
DK.7
Associate III
August 31, 2021
Solved

Why do I get wrong data by SPI?

 /* USER CODE BEGIN 2 */
 // CS pin should befault high
 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
 uint8_t TX_Data[13] = "Hello World!";
 uint8_t RX_Data[13];
 /* USER CODE END 2 */
 
 while (1)
 {
 /* USER CODE END WHILE */
 
 /* USER CODE BEGIN 3 */
 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); // CS set LOW
 HAL_SPI_TransmitReceive(&hspi2, TX_Data, RX_Data, 13, 100); 
 }
 while (hspi2.State == HAL_SPI_STATE_BUSY); 
 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); // CS set HIGH
 HAL_Delay(1000);
 }
 /* USER CODE END 3 */
}

Very simple code, but I do something wrong! I'm  sending "Hello world" and in a logic analyzer I get a trash! Could you help me? link to my full main.c https://pastebin.com/jygZ5Rh10693W00000DlnPzQAJ.png

This topic has been closed for replies.
TDK
TDKBest answer
August 31, 2021

Looks like your data is being sent MSB first, but being interpreted LSB first. Perhaps an error in the analyzer software somewhere. Try switching it to LSB first to see what happens.

0b01100101 = 0x65 = "e"

0693W00000DlnpOQAR.pngReversed: 0b10100110 = 0xA6, which is what it's reporting.

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

5 replies

TDK
August 31, 2021

You need to match the CPOL/CPHA values in your analyzer to what your code is doing. Signal looks correct.

"If you feel a post has answered your question, please click ""Accept as Solution""."
DK.7
DK.7Author
Associate III
August 31, 2021

Thanks for the answer! There are not many possibility! I tried all possible options with CPOL/CPHA and not only with CPOL/CPHA. The best option with that settings. But the problem still the same.0693W00000DlnmPQAR.png

TDK
TDKBest answer
August 31, 2021

Looks like your data is being sent MSB first, but being interpreted LSB first. Perhaps an error in the analyzer software somewhere. Try switching it to LSB first to see what happens.

0b01100101 = 0x65 = "e"

0693W00000DlnpOQAR.pngReversed: 0b10100110 = 0xA6, which is what it's reporting.

"If you feel a post has answered your question, please click ""Accept as Solution""."
DK.7
DK.7Author
Associate III
September 1, 2021

Thanks for the help! I do not know why, but now it works! I can only assume, after switching from "Least Significant Bit First" to "Least Significant Bit First" and ba�?k it start works!0693W00000Dlp5hQAB.png

TDK
September 1, 2021
Weird.
If you can replicate it, the Saleae devs are pretty responsive on their forum as far as fixing software bugs.
"If you feel a post has answered your question, please click ""Accept as Solution""."