Skip to main content
Explorer II
April 12, 2025
Solved

STM32L486: SPI-Clock doubled

  • April 12, 2025
  • 1 reply
  • 395 views

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.
    Best answer by TDK

    Write 8 bits to DR instead of 16.

    *(volatile uint8_t*)&SPI1->DR = tx_data;

    1 reply

    TDKAnswer
    Super User
    April 13, 2025

    Write 8 bits to DR instead of 16.

    *(volatile uint8_t*)&SPI1->DR = tx_data;
    ByteslaveAuthor
    Explorer II
    April 13, 2025

    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!

    Super User
    April 13, 2025

    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.