cancel
Showing results for 
Search instead for 
Did you mean: 

pure 8-Bit SPI communication ?

mmensch
Associate II
Posted on November 14, 2013 at 10:34

Hi,

I am in trouble with 8-bit SPI communication. I did this:

  SPI1->CR1 = (uint16_t)( SPI_CR1_MSTR | SPI_CR1_BR_0 | SPI_CR1_BR_1

                          | SPI_CR1_SSI | SPI_CR1_SSM );

  SPI1->CR2 = (uint16_t)(SPI_CR2_DS_0 | SPI_CR2_DS_1 | SPI_CR2_DS_2

                                          SPI_CR2_FRXTH);

and to send data this

      while (BitMaskIsClear (SPI1->SR, SPI_SR_TXE));

      SPI1->DR = (uint8_t)(0x0F);

      while (BitMaskIsClear (SPI1->SR, SPI_SR_TXE));

      SPI1->DR = (uint8_t)(0x55);

      while (BitMaskIsClear (SPI1->SR, SPI_SR_TXE));

      SPI1->DR = (uint8_t)(0xAA);

      while (BitMaskIsClear (SPI1->SR, SPI_SR_TXE));

      SPI1->DR = (uint8_t)(0x5A);

but I get 4 times 16 Bit out with the high byte always being 0x00.

How can I send only 8 bit with each access to ->DR without doing any prepacking before loading ->DR ? Also I would like to repeat this in a loop to send any number (even or odd) of bytes.

I didn't try it yet but I guess there will be a similiar problem when trying to read single bytes out of ->DR while there are two bytes in it. Are there any hints on this?

I read the data sheet but I feel it is a bit confusing.

I am using STM32F051, GCC

Thank you very much for any help

Martin

1 REPLY 1
mmensch
Associate II
Posted on November 14, 2013 at 16:52

Hi,

I found out myself.

To write or read only 8 bit out of the Tx / RxFifo you need to access ->DR as an 8 bit type but it is declared as a uint16_t. So you need to cast it like it is done in the lib functions SPI_SendData8 and SPI_ReceiveData8.

Hope anyone can use this hint.

Martin