I am trying to write simple code for transmitting with the NRF24L01 wireless chip, but the SPI communication is not going very well.
Using the code below I can read any register on the NRF24L01, verified using the registers' default values from the datasheet (a good reference point is that register CONFIG 0x00 reads 8 while register 0x10 reads first byte of E7).
The problem is that when I try to write any value whatsoever to any register it always ends up having a value of zero, verified using GDB server to the L432KC board.
The board is running on max. 80 mHz and the baud rate of the SPI is the lowest for now for debugging purposes (312.5 kbits/s)
The questions are:
- Why would a write end up as zeros in the registers?
- Why do I end up getting 3 bytes back for every byte I send? I know that the SPI only gets one byte for every one transmitted, but I get 3 from the module which eventually creates an OVR error. I solved the problem by reading the first two dummy bytes and saving the last one which carries the meaningful data in the while loop of the SPITransfer function below.
SPI function:
uint8_t SPITransfer(uint8_t data) {
uint8_t temp;
while (!(SPI1->SR & (1 << 1))); // check the TXE flag
SPI1->DR = data; // write the data
while ((SPI1->SR & (1 << 7)) || !(SPI1->SR & (1 << 0))); // check bsy and RXNE flag
while (SPI1->SR & (1 << 0)) // Read ACK bytes and returned data
temp = SPI1->DR;
return temp;
}
NRF24Read and Write:
uint8_t NRF24ReadReg(uint8_t reg) {
uint8_t rx, dummy;
CS_Select();
SPITransfer(reg);
rx = SPITransfer(dummy);
CS_UnSelect();
return rx;
}
void NRF24WriteReg(uint8_t reg, uint8_t data) {
uint8_t buf[2];
buf[0] = reg | (1 << 5);
buf[1] = data;
CS_Select();
SPITransfer(buf[0]);
SPITransfer(buf[1]);
CS_UnSelect();
}
GDB Debug:
(gdb) call NRF24ReadReg(0x10)
$26 = 231 '\347'
(gdb) call NRF24WriteReg(0x10, 4)
(gdb) call NRF24ReadReg(0x10)
$27 = 0 '\000'
(gdb)