Other Parts Discussed in Thread: CC2520, TM4C123GH6PM
Hi,
I'm trying to port the CC2520 driver from Contiki to the tm4c123gh6pm.
I defined
/* SPI input/output registers. */
#define SPI_TXBUF HWREG(SPI_BASE + SSI_O_DR)
#define SPI_RXBUF HWREG(SPI_BASE + SSI_O_DR)
/* USART0 Tx ready? */
#define SPI_WAITFOREOTx() while((HWREG(SPI_BASE + SSI_O_SR) & SSI_SR_BSY))
/* USART0 Rx ready? */
#define SPI_WAITFOREORx() while(!(HWREG(SPI_BASE + SSI_O_SR) & SSI_SR_RNE))
/* USART0 Tx buffer ready? */
#define SPI_WAITFORTxREADY() while(!(HWREG(SPI_BASE + SSI_O_SR) & SSI_SR_TNF))
Now when trying to read a byte from the CC2520 RX fifo (when a packet was received) the driver does this to read the first byte (length):
#define CC2520_READ_FIFO_BYTE(data) \
do { \
CC2520_SPI_ENABLE(); \
SPI_WRITE(CC2520_INS_RXBUF); \
(void)SPI_RXBUF; \
SPI_READ(data); \
clock_delay(1); \
CC2520_SPI_DISABLE(); \
} while(0)
Where SPI_READ is
/* Read one character from SPI */
#define SPI_READ(data) \
do { \
SPI_TXBUF = 0; \
SPI_WAITFOREORx(); \
data = SPI_RXBUF; \
} while(0)
and SPI_WRITE
/* Write one character to SPI */
#define SPI_WRITE(data) \
do { \
SPI_WAITFORTx_BEFORE(); \
SPI_TXBUF = data; \
SPI_WAITFOREOTx(); \
} while(0)
So this writes CC2520_INS_RXBUF (0x30) to SPI, reads the SPI RX fifo to discard the status byte that the CC2520 will always write, then writes another 0 to read the response to the CC2520_INS_RXBUF command.
Only that it doesn't work like that, data will always read 193/0xc1 which is the status byte.
Why is this not being discarded by (void)SPI_RXBUF; ?
If I instead do
#define CC2520_READ_FIFO_BYTE(data) \
do { \
CC2520_SPI_ENABLE(); \
SPI_WRITE(CC2520_INS_RXBUF); \
SPI_READ(data); \
SPI_READ(data); \
clock_delay(1); \
CC2520_SPI_DISABLE(); \
} while(0)
it work, but I don't really know why.