cancel
Showing results for 
Search instead for 
Did you mean: 

The first letter isn't sent correctly in SPI.

TKawa.9
Associate II

Hello.

Transmission from the master in full-duplex mode is possible, but when transmitted from the slave, only the first character is not transmitted correctly.

The signal was misaligned for the first letter, what is the cause?

STM32G431 Nucleo

SPI2 settings

0693W00000bhGZqQAM.png 

SPI3 settings0693W00000bhGaOQAU.png 

__IO uint8_t spi2Status = 0;

__IO uint8_t spi3Status = 0;

uint8_t spi2Buffer[12];

uint8_t spi3Buffer[12];

// This works.

 memset( spi2Buffer, 0x00, sizeof(uint8_t) *12 );

 memset( spi3Buffer, 0x00, sizeof(uint8_t) *12 );

 sprintf( (char *)spi2Buffer, "STM32F401RE" );

 HAL_SPI_Receive_IT(&hspi3, spi3Buffer, strlen((char const *) spi2Buffer));

 HAL_SPI_Transmit_IT(&hspi2, spi2Buffer, strlen((char const *) spi2Buffer));

// This didn't work.

 memset( spi2Buffer, 0x00, sizeof(uint8_t) *12 );

 memset( spi3Buffer, 0x00, sizeof(uint8_t) *12 );

 sprintf( (char *)spi3Buffer, "STM32F401RE" );

 HAL_SPI_Receive_IT(&hspi2, spi2Buffer, strlen((char const *) spi3Buffer));

 HAL_SPI_Transmit_IT(&hspi3, spi3Buffer, strlen((char const *) spi3Buffer));

 result when sent from SPI2

0693W00000bhGbHQAU.png result when sent from SPI3

0693W00000bhGcKQAU.png0693W00000bhGcVQAU.png0693W00000bhGfJQAU.png result when sent from SPI2

orange: NSS

yellow: Clock

green: MOSI

0693W00000bhGfdQAE.png result when sent from SPI3

green: MISO0693W00000bhGfnQAE.png 

Thank you for your response.

1 ACCEPTED SOLUTION

Accepted Solutions

Try this:

 memset( spi2Buffer, 0x00, sizeof(uint8_t) *12 );

 memset( spi3Buffer, 0x00, sizeof(uint8_t) *12 );

 sprintf( (char *)spi3Buffer, "STM32F401RE" );

 HAL_SPI_Transmit_IT(&hspi3, spi3Buffer, strlen((char const *) spi3Buffer));

 HAL_SPI_Receive_IT(&hspi2, spi2Buffer, strlen((char const *) spi3Buffer));

ie. start slave Tx *before* master's Rx.

JW

View solution in original post

8 REPLIES 8
Sarra.S
ST Employee

Hello @TKawa.9​,

just a general advice, you are using strlen() to determine the length of the data to be transmitted/received...

To ensure that the correct number of bytes are transmitted/received, you should use a variable to store the length of the data, rather than relying on strlen().

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.

TKawa.9
Associate II

a

Thank you for your reply. 

>To ensure that the correct number of bytes are transmitted/received, you should use a variable to store the length of the data, rather than relying on strlen().

I will do that next time.

Is there any possible cause for the problem that only the first character sent from the slave to the master has the data bits out of alignment?

Transmission from the master to the slave is working.

AScha.3
Chief III

just guessing:

(in your diagram orange = NSS -- right?)

nss active level is same as "idle" , not active. this could create one transistion in beginning of transfer, so you get 1bit shift wrong.

set in cube: spi .. parameter :

0693W00000bhO3hQAE.png 

and maybe to try: at slave : set pullup on nss, pulldown on data+clk. test...

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

Try this:

 memset( spi2Buffer, 0x00, sizeof(uint8_t) *12 );

 memset( spi3Buffer, 0x00, sizeof(uint8_t) *12 );

 sprintf( (char *)spi3Buffer, "STM32F401RE" );

 HAL_SPI_Transmit_IT(&hspi3, spi3Buffer, strlen((char const *) spi3Buffer));

 HAL_SPI_Receive_IT(&hspi2, spi2Buffer, strlen((char const *) spi3Buffer));

ie. start slave Tx *before* master's Rx.

JW

TKawa.9
Associate II

Thank you for your help.

I made the changes as advised by waclawek.jan and it solved the problem.

It worked fine.

Thank you very much.

But HAL_SPI_TxCpltCallback() is not called.

HAL_SPI_RxCpltCallback() is called and the send should be working as well since the received contents are stored exactly in spi2Buffer.

Please let me know if you know what the cause is.

I don't know why HAL_SPI_TxCpltCallback() is not called, I don't use Cube, sorry.

JW