Posted on March 24, 2013 at 11:24hi 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:
- void spi_init(void) {
- /* full-duplex is (BDM = 0 and RXONLY = 0) */
- SPI_CR1 |= SETV(SPI_DIV8, BR0) | SETB(MSTR)
- SPI_CR1 |= SETB(SPE);
- return;
- }
with the marcos and pseudo-functions:
- #define SETB(bit) (1<<(bit))
- #define SETV(val,bit) ((val)<<(bit))
- /* bits of SPI control register 1 (SPI_CR1) */
- #define LSBFIRST 7 /* Frame format, 0 for MSB first */
- #define SPE 6 /* SPI Enable, 1 for enabled */
- #define BR0 3 /* Baut rate control */
- #define MSTR 2 /* Master selection, 1 for master */
- #define CPOL 1 /* Clock polarity, 0 for sck=LOW when idle */
- #define CPHA 0 /* Clock phase, ?? */
- #define SPI_DIV2 0
- #define SPI_DIV4 1
- #define SPI_DIV8 2
- #define SPI_DIV16 3
- /* pseudo func of SPI */
- #define SPI_ISBSY (SPI_SR & SETB(BSY))
- #define SPI_TNE !(SPI_SR & SETB(TXE))
- #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:
- in spi_init
- SPI_CR1=00
- SPI_SR=82
- after set SPI_CR1 (SPI_CR1 |= SETV(SPI_DIV8, BR0) | SETB(MSTR))
- SPI_SR=82
- SPI_CR1=14
- after set SPE ( SPI_CR1 |= SETB(SPE); )
- SPI_CR1=10
- SPI_SR=22