cancel
Showing results for 
Search instead for 
Did you mean: 

help~ SPI initialization error (STM8S105S6)

fnems
Associate
Posted on March 24, 2013 at 11:24

hi there

here's my problem, i tried to program a spi device, and (after a long debug i finally knew that it) got stuck at waiting for TXE flag of SPI_SR, trying to write to the SPI bus.

and i checked the SPI_CR1 register and SPI_SR register, they are:

SPI_CR1 = 0x10

SPI_SR  = 0x20

so the configuration of SPI is not correct...

but my initialization code is very simple:

  1. void spi_init(void) {
  2.   /* full-duplex is (BDM = 0 and RXONLY = 0) */
  3.   SPI_CR1 |= SETV(SPI_DIV8, BR0) | SETB(MSTR)
  4.   SPI_CR1 |= SETB(SPE);
  5.   return;
  6. }

with the marcos and pseudo-functions:

  1. #define SETB(bit)      (1<<(bit))
  2. #define SETV(val,bit)  ((val)<<(bit))
  3. /* bits of SPI control register 1 (SPI_CR1) */
  4. #define LSBFIRST 7 /* Frame format, 0 for MSB first */
  5. #define SPE      6 /* SPI Enable, 1 for enabled */
  6. #define BR0      3 /* Baut rate control */
  7. #define MSTR     2 /* Master selection, 1 for master */
  8. #define CPOL     1 /* Clock polarity, 0 for sck=LOW when idle */
  9. #define CPHA     0 /* Clock phase, ?? */
  10. #define SPI_DIV2  0
  11. #define SPI_DIV4  1
  12. #define SPI_DIV8  2
  13. #define SPI_DIV16 3
  14. /* pseudo func of SPI */
  15. #define SPI_ISBSY     (SPI_SR & SETB(BSY))
  16. #define SPI_TNE      !(SPI_SR & SETB(TXE))
  17. #define SPI_RE       !(SPI_SR & SETB(RXNE))
i added some debug code in the init function, to print the registers through UART.

the report is like this:

  1. in spi_init
  2. SPI_CR1=00
  3. SPI_SR=82
  4. after set SPI_CR1  (SPI_CR1 |= SETV(SPI_DIV8, BR0) | SETB(MSTR))
  5. SPI_SR=82
  6. SPI_CR1=14
  7. after set SPE  ( SPI_CR1 |= SETB(SPE); )
  8. SPI_CR1=10
  9. SPI_SR=22

0 REPLIES 0