Skip to main content
paul2
Associate III
October 18, 2014
Question

Sending ten bits to SPI slave

Posted on October 18, 2014 at 20:19

Hi,

I want to send ten bits from my STM32F4 Discovery to a SPI slave.

The SPI Init struct only allows 8- or 16 bit (SPI_DataSize_8b, SPI_DataSize_16b)

Whats the best way to send the required ten bits to the SPI slave?

Thanks.
    This topic has been closed for replies.

    3 replies

    Tesla DeLorean
    Guru
    October 18, 2014
    Posted on October 18, 2014 at 23:26

    Send as 40-bits (5 x 8-bit), or bit bang

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    paul2
    paul2Author
    Associate III
    October 19, 2014
    Posted on October 19, 2014 at 08:26

    Hi Clive,

    Is there a typical SPI bit-banging example available?

    Thanks.

    paul2
    paul2Author
    Associate III
    October 21, 2014
    Posted on October 21, 2014 at 21:43

    Hi,

    to send SPI data I use the code below:

    GPIOE->BSRRH |= GPIO_Pin_13; 
    received_val=SPI2_send(0x16); 
    GPIOE->BSRRL |= GPIO_Pin_13;
    

    The problem is the CS (GPIO_Pin_13) already becomes high at the rising edge of the last CLK pulse. I have implemented several checks (code below) to make sure the data has been send before raising the CS signal again.

    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
    SPI_I2S_SendData(SPI2, data / 256);
    // be sure that the character goes to the shift register
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
    SPI_I2S_SendData(SPI2, data);
    // be sure that the character goes to the shift register
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
    // and then be sure it has been sent over the wire
    while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET);

    Whats the best solution in order prevent the CS level already going high at the rising edge of the last CLK pulse? Thanks.