Skip to main content
Visitor II
July 17, 2025
Solved

SPI 8-Bit frame

  • July 17, 2025
  • 2 replies
  • 304 views

Hi, 

I hope this is the right place to ask. I'm currently working on interfacing an SD Card with a nucleo f767. I used the DS-Bits in CR2 to set the data frame to 8 Bit (what also should be the reset value). Despite that, MOSI always sends the whole data register, which is 16 Bits. So if I use the function below to send 0xFF, the hardware will send 0x00FF. Does anybody know this issue? 

uint8_t spi_txrx(uint8_t b) {
  while (!(SPI3->SR & SPI_SR_TXE));
  SPI3->DR = b;
  while (!(SPI3->SR & SPI_SR_RXNE));
  return SPI3->DR;
}
    This topic has been closed for replies.

    2 replies

    Super User
    July 17, 2025

    Write 8 bits to the register instead of 16:

    *(volatile uint8_t*)&SPI3->DR = b;

     

    Look up data packing in the Reference Manual.

     

    LuLeWiAuthor
    Visitor II
    July 17, 2025

    Thank you for the quick response. I already tried this cast, but it leads into a system crash. With the following version, the cmd0 command works. Is it save to use?
    void spi_send(uint8_t b) {
    while (!(SPI3->SR & SPI_SR_TXE));
    __asm__ volatile("strb %0, [%1]"
    :
    : "r"(b), "r"(&SPI3->DR)
    : "memory");
    while (!(SPI3->SR & SPI_SR_TXE));
    }

    TDKAnswer
    Super User
    July 17, 2025
    LuLeWiAuthor
    Visitor II
    July 18, 2025

    This one is working. Thank you very much!