cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Data Doubling Issue (Left Shifting) - Screenshots Included

Ayoub_Bnina
Associate III

Hi Community,

I'm new to working with STM32 microcontrollers and I'm encountering a challenge when transmitting data through SPI on my STM32 Nucleo board. I'd appreciate some guidance from the experienced members of this community.

My Goal:

  • Transmit the value 6 (binary: 0b10) using the SPI interface.
  • I'm utilizing the HAL_SPI_Transmit function for data transmission.

The Problem:

Regardless of the data I send, I consistently see a doubling of the value on my logic analyzer. For example, sending 6 results in seeing 12 displayed. It seems like a constant shift is happening, and I'm unsure of the cause.

My Code Snippet:

C
const uint8_t Value1 = 0b10;
while (1)
{
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, RESET);
  HAL_SPI_Transmit(&hspi1, (uint8_t *)&Value1, 1, 100);
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, SET);
  HAL_Delay(2000);
}

Attached Screenshots:

  • Logic Analyzer View
  • CubeMX SPI Configuration

 

Ayoub_Bnina_1-1719514875168.png

 

Ayoub_Bnina_2-1719514903664.png

 

Seeking Assistance:

By reviewing the screenshots and considering my code, I'd be incredibly grateful if someone could help me understand, one more thing the CS pin is handled manually.

Thank you so much in advance for any support!

1 ACCEPTED SOLUTION

Accepted Solutions
AScha.3
Chief III

Hi,

i cannot see much from your LA pic...too small.

AScha3_0-1719516045050.png

 

But ... seems, your LA is set on falling edge, while you set in Cube rising edge (1. edge);

so try setting both the same : set in Cube "2. edge" - and see.

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

View solution in original post

3 REPLIES 3
AScha.3
Chief III

Hi,

i cannot see much from your LA pic...too small.

AScha3_0-1719516045050.png

 

But ... seems, your LA is set on falling edge, while you set in Cube rising edge (1. edge);

so try setting both the same : set in Cube "2. edge" - and see.

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

Sorry for the small size of the image, but you're right I set the clock phase to 2 Edge and it's working fine.
Thank you @AScha.3 !

If it's okay for you to explain it to me, why 1 Edge is causing extra 0 ?


Hey,  its shifting in at the edge, you set. So anything might come out, 0->1 , or 1->0 , on all bits; so shifting the whole "result" up or down, depending on the error.

Wrong is wrong...the SPI basically is not very complex, but the point of decision is important: 1 or 0 now .

So if you set this wrong...it might work, sometimes, depends on timing, or never.

But to see this, you need a scope (or LA) with much more resolution, than the signal, then you know, what happens.

Or just try...not so many possible settings. (only 4 .. rising, falling, 1. or 2. edge)

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