Author Topic: STM32 SPI: How to properly send 8bits?  (Read 1529 times)

0 Members and 1 Guest are viewing this topic.

Offline WillHuangTopic starter

  • Contributor
  • Posts: 47
STM32 SPI: How to properly send 8bits?
« on: December 30, 2016, 05:57:35 am »
I have a function here that compiles and works fine (i.e. I'm getting the output that I want) for sending 8 bit data frames on the SPI bus for the STM32F3 dev. board.

Code: [Select]
void SPI1_Transfer8(uint8_t data)
{

    uint8_t dummy;
    (volatile uint8_t)SPI1->DR = data;
    //SPI1_DR = data;
    SPI1->CR1 |= SPI_CR1_SPE;
    SPI1_Disable();
}

Although I can compile the code fine, Keil is showing me an error on line
Code: [Select]
(volatile uint8_t)SPI1->DR = data;
How can I fix this?

Thanks in advance!
 

Online Jeroen3

  • Super Contributor
  • ***
  • Posts: 4172
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: STM32 SPI: How to properly send 8bits?
« Reply #1 on: December 30, 2016, 06:35:26 am »
General rule: you can't cast the left side of an assignment.

Also, it will give an hardfault when try to force this.
Quote
The peripheral registers have to be accessed by half-words (16 bits) or words (32 bits).

The SPI1->DR register is already defined as an volatile uint16_t in the header. C will perform an implicit type conversion when you use it.

And you will have to wait for SPI to finish. The SPI Master is completed when RXNE = 1 and BUSY = 0.
« Last Edit: December 30, 2016, 06:37:58 am by Jeroen3 »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf