Skip to main content
Associate
Apr 12, 2025 8:16 PM UTC
Solved

STM32L486: SPI-Clock doubled

Use SPI with MCU above.  With

spi_transmit(0x55);

and

void spi_transmit(uint8_t tx_data)
{
 SPI1->DR= tx_data;
 while (!(SPI1->SR & SPI_SR_TXE));
}

I have 16 SPI clocks. Init was:

hspi1.Init.DataSize = SPI_DATASIZE_8BIT;

 Data on MOSI showing on the first 8 clocks, MOSI on remaining 8 clocks is zero.

While using HAL (same init):

pdat= 0x55;
HAL_SPI_Transmit(&hspi1, (const uint8_t *)&pdat, 1, 100);

I have, as expected, 8 SPI clocks.

Any Suggestions? Thanks!

This topic has been closed for replies.
TDK
TDKBest answer
Apr 13, 2025 12:12 AM UTC

Write 8 bits to DR instead of 16.

*(volatile uint8_t*)&SPI1->DR = tx_data;
"If you feel a post has answered your question, please click ""Accept as Solution""."

3 replies

TDK
TDKBest answer
Apr 13, 2025 12:12 AM UTC

Write 8 bits to DR instead of 16.

*(volatile uint8_t*)&SPI1->DR = tx_data;
"If you feel a post has answered your question, please click ""Accept as Solution""."
ByteslaveAuthor
Associate
Apr 13, 2025 8:02 AM UTC

Wow, that worked! I'm still confused, because "tx_data" is 8bits, init was for 8bit data and the subroutine already worked at an STM32F103C8T6.  But it is like it is, Thanks again!

TDK
Apr 13, 2025 1:45 PM UTC

The access size is dictated by the left hand side of the expression and since SPI->DR is a 16-bit register, that's what you write regardless of what is on the right, which gets converted to the right size.

F103 doesn't have data packing so it works there.

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