AnsweredAssumed Answered

help~ SPI initialization error (STM8S105S6)

Question asked by fan.felix.001 on Mar 24, 2013
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.    
  3.   /* full-duplex is (BDM = 0 and RXONLY = 0) */
  4.    
  5.   SPI_CR1 |= SETV(SPI_DIV8, BR0) | SETB(MSTR)
  6.    
  7.   SPI_CR1 |= SETB(SPE);
  8.    
  9.   return;
  10.    
  11. }

with the marcos and pseudo-functions:

       
  1. #define SETB(bit)      (1<<(bit))
  2.    
  3. #define SETV(val,bit)  ((val)<<(bit))
  4.    
  5.    
  6. /* bits of SPI control register 1 (SPI_CR1) */
  7.    
  8. #define LSBFIRST 7 /* Frame format, 0 for MSB first */
  9.    
  10. #define SPE      6 /* SPI Enable, 1 for enabled */
  11.    
  12. #define BR0      3 /* Baut rate control */
  13.    
  14. #define MSTR     2 /* Master selection, 1 for master */
  15.    
  16. #define CPOL     1 /* Clock polarity, 0 for sck=LOW when idle */
  17.    
  18. #define CPHA     0 /* Clock phase, ?? */
  19.    
  20.    
  21. #define SPI_DIV2  0
  22.    
  23. #define SPI_DIV4  1
  24.    
  25. #define SPI_DIV8  2
  26.    
  27. #define SPI_DIV16 3
  28.    
  29.    
  30. /* pseudo func of SPI */
  31.    
  32. #define SPI_ISBSY     (SPI_SR & SETB(BSY))
  33.    
  34. #define SPI_TNE      !(SPI_SR & SETB(TXE))
  35.    
  36. #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.    
  3. SPI_CR1=00
  4.    
  5. SPI_SR=82
  6.    
  7. after set SPI_CR1  (SPI_CR1 |= SETV(SPI_DIV8, BR0) | SETB(MSTR))
  8.    
  9. SPI_SR=82
  10.    
  11. SPI_CR1=14
  12.    
  13. after set SPE  ( SPI_CR1 |= SETB(SPE); )
  14.    
  15. SPI_CR1=10
  16.    
  17. SPI_SR=22

Outcomes