opt: nrf24l01 demo optimization

This commit is contained in:
IOsetting 2022-01-06 00:27:45 +08:00
parent ddbaea3157
commit 9d4f235c7a
3 changed files with 58 additions and 56 deletions

View File

@ -15,7 +15,8 @@
#include "nrf24l01.h"
uint8_t __IDATA xbuf[NRF24_PLOAD_WIDTH + 1];
uint8_t nrf24_state;
uint8_t *xbuf_data = xbuf + 1;
uint16_t NRF24L01_rxsn = 0;
const uint8_t TX_ADDRESS[NRF24_ADDR_WIDTH] = {0x32,0x4E,0x6F,0x64,0x22};
const uint8_t RX_ADDRESS[NRF24_ADDR_WIDTH] = {0x32,0x4E,0x6F,0x64,0x65};
@ -23,49 +24,48 @@ const uint8_t RX_ADDRESS[NRF24_ADDR_WIDTH] = {0x32,0x4E,0x6F,0x64,0x65};
void NRF24L01_WriteReg(uint8_t reg, uint8_t value)
{
NRF_CSN = 0;
nrf24_state = SPI_TxRx(reg);
SPI_TxRx(value);
xbuf[0] = reg;
xbuf[1] = value;
SPI_TxRxBytes(xbuf, 2);
NRF_CSN = 1;
}
uint8_t NRF24L01_ReadReg(uint8_t reg)
{
uint8_t value;
NRF_CSN = 0;
nrf24_state = SPI_TxRx(reg);
value = SPI_TxRx(NRF24_CMD_NOP);
xbuf[0] = reg;
xbuf[1] = NRF24_CMD_NOP;
SPI_TxRxBytes(xbuf, 2);
NRF_CSN = 1;
return value;
return xbuf[1];
}
void NRF24L01_ReadToBuf(uint8_t reg, uint8_t *pBuf, uint8_t len)
void NRF24L01_ReadToBuf(uint8_t reg, uint8_t len)
{
uint8_t u8_ctr;
NRF_CSN = 0;
nrf24_state = SPI_TxRx(reg);
for (u8_ctr = 0; u8_ctr < len; u8_ctr++)
pBuf[u8_ctr] = SPI_TxRx(NRF24_CMD_NOP);
memset(xbuf, NRF24_CMD_NOP, NRF24_PLOAD_WIDTH + 1);
xbuf[0] = reg;
SPI_TxRxBytes(xbuf, len + 1);
NRF_CSN = 1;
}
void NRF24L01_WriteFromBuf(uint8_t reg, const uint8_t *pBuf, uint8_t len)
{
uint8_t u8_ctr;
NRF_CSN = 0;
nrf24_state = SPI_TxRx(reg);
for (u8_ctr = 0; u8_ctr < len; u8_ctr++)
SPI_TxRx(*pBuf++);
xbuf[0] = reg;
memcpy(xbuf_data, pBuf, len);
SPI_TxRxBytes(xbuf, len + 1);
NRF_CSN = 1;
}
void NRF24L01_PrintBuf(uint8_t *buf)
void NRF24L01_PrintBuf(void)
{
uint8_t i;
for (i = 0; i < NRF24_PLOAD_WIDTH; i++)
{
UART1_TxHex(buf[i]);
}
UART1_TxString("\r\n");
uint8_t i;
for (i = 0; i < NRF24_PLOAD_WIDTH + 1; i++)
{
UART1_TxHex(xbuf[i]);
}
UART1_TxString("\r\n");
}
/**
@ -89,32 +89,33 @@ void NRF24L01_CheckFlag(uint8_t *tx_ds, uint8_t *max_rt, uint8_t *rx_dr)
// Read the status & reset the status in one easy call
NRF24L01_WriteReg(NRF24_CMD_W_REGISTER + NRF24_REG_STATUS, NRF24_FLAG_RX_DREADY|NRF24_FLAG_TX_DSENT|NRF24_FLAG_MAX_RT);
// Report to the user what happened
*tx_ds = nrf24_state & NRF24_FLAG_TX_DSENT;
*max_rt = nrf24_state & NRF24_FLAG_MAX_RT;
*rx_dr = nrf24_state & NRF24_FLAG_RX_DREADY;
*tx_ds = xbuf[0] & NRF24_FLAG_TX_DSENT;
*max_rt = xbuf[0] & NRF24_FLAG_MAX_RT;
*rx_dr = xbuf[0] & NRF24_FLAG_RX_DREADY;
}
uint8_t NRF24L01_RxAvailable(uint8_t* pipe_num)
uint8_t NRF24L01_RxAvailable(uint8_t *pipe_num)
{
uint8_t pipe;
nrf24_state = NRF24L01_ReadReg(NRF24_REG_STATUS);
pipe = (nrf24_state >> 1) & 0x07;
if (pipe > 5)
return 0;
// If the caller wants the pipe number, include that
if (pipe_num)
*pipe_num = pipe;
uint8_t pipe;
NRF24L01_ReadReg(NRF24_REG_STATUS);
pipe = (xbuf[0] >> 1) & 0x07;
if (pipe > 5)
return 0;
// If the caller wants the pipe number, include that
if (pipe_num)
*pipe_num = pipe;
return 1;
return 1;
}
void NRF24L01_HandelIrqFlag(uint8_t *buf)
void NRF24L01_HandelIrqFlag(void)
{
uint8_t tx_ds, max_rt, rx_dr, pipe_num;
NRF24L01_CheckFlag(&tx_ds, &max_rt, &rx_dr);
if (NRF24L01_RxAvailable(&pipe_num) == 1)
{
NRF24L01_ReadToBuf(NRF24_CMD_R_RX_PAYLOAD, buf, NRF24_PLOAD_WIDTH);
NRF24L01_ReadToBuf(NRF24_CMD_R_RX_PAYLOAD, NRF24_PLOAD_WIDTH);
NRF24L01_rxsn++;
}
UART1_TxHex(tx_ds);
UART1_TxChar(' ');
@ -124,14 +125,14 @@ void NRF24L01_HandelIrqFlag(uint8_t *buf)
UART1_TxChar(' ');
UART1_TxHex(pipe_num);
UART1_TxString("\r\n");
NRF24L01_PrintBuf(xbuf);
NRF24L01_PrintBuf();
}
void NRF24L01_Tx(uint8_t *txbuf)
void NRF24L01_Tx(uint8_t *pBuf)
{
NRF_CE = 0;
NRF24L01_WriteReg(NRF24_CMD_W_REGISTER + NRF24_REG_CONFIG, 0x0E);
NRF24L01_WriteFromBuf(NRF24_CMD_W_TX_PAYLOAD, txbuf, NRF24_PLOAD_WIDTH);
NRF24L01_WriteFromBuf(NRF24_CMD_W_TX_PAYLOAD, pBuf, NRF24_PLOAD_WIDTH);
NRF_CE = 1;
SYS_Delay(10); // for reliable DS state when SETUP_RETR is 0x13
NRF_CE = 0;
@ -139,21 +140,21 @@ void NRF24L01_Tx(uint8_t *txbuf)
NRF_CE = 1;
}
void NRF24L01_StartFastWrite(const void* txbuf)
void NRF24L01_StartFastWrite(const void* pBuf)
{
NRF24L01_WriteFromBuf(NRF24_CMD_W_TX_PAYLOAD, txbuf, NRF24_PLOAD_WIDTH);
NRF24L01_WriteFromBuf(NRF24_CMD_W_TX_PAYLOAD, pBuf, NRF24_PLOAD_WIDTH);
NRF_CE = 1;
}
uint8_t NRF24L01_WriteFast(const void* txbuf)
uint8_t NRF24L01_WriteFast(const void* pBuf)
{
//Blocking only if FIFO is full. This will loop and block until TX is successful or fail
while ((NRF24L01_ReadReg(NRF24_REG_STATUS) & NRF24_FLAG_TX_FULL)) {
if (nrf24_state & NRF24_FLAG_MAX_RT) {
if (xbuf[0] & NRF24_FLAG_MAX_RT) {
return 0;
}
}
NRF24L01_StartFastWrite(txbuf);
NRF24L01_StartFastWrite(pBuf);
return 1;
}
@ -169,10 +170,10 @@ uint8_t NRF24L01_Check(void)
uint8_t i;
const uint8_t *ptr = (const uint8_t *)NRF24_TEST_ADDR;
NRF24L01_WriteFromBuf(NRF24_CMD_W_REGISTER | NRF24_REG_TX_ADDR, ptr, NRF24_ADDR_WIDTH);
NRF24L01_ReadToBuf(NRF24_CMD_R_REGISTER | NRF24_REG_TX_ADDR, xbuf, NRF24_ADDR_WIDTH);
NRF24L01_ReadToBuf(NRF24_CMD_R_REGISTER | NRF24_REG_TX_ADDR, NRF24_ADDR_WIDTH);
for (i = 0; i < NRF24_ADDR_WIDTH; i++) {
UART1_TxHex(xbuf[i]);
if (xbuf[i] != *ptr++) return 1;
UART1_TxHex(*(xbuf_data + i));
if (*(xbuf_data + i) != *ptr++) return 1;
}
return 0;
}

View File

@ -16,6 +16,7 @@
#define __FW_RNF24L01_H__
#include "fw_hal.h"
#include "string.h"
#define NRF_CSN P35
#define NRF_MOSI P34
@ -118,11 +119,11 @@ void NRF24L01_WriteReg(uint8_t reg, uint8_t value);
uint8_t NRF24L01_ReadReg(uint8_t reg);
void NRF24L01_ReadToBuf(uint8_t reg, uint8_t *pBuf, uint8_t len);
void NRF24L01_ReadToBuf(uint8_t reg, uint8_t len);
void NRF24L01_WriteFromBuf(uint8_t reg, const uint8_t *pBuf, uint8_t len);
void NRF24L01_PrintBuf(uint8_t *buf);
void NRF24L01_PrintBuf(void);
/**
* Flush the RX FIFO
@ -137,10 +138,10 @@ void NRF24L01_FlushTX(void);
void NRF24L01_CheckFlag(uint8_t *tx_ds, uint8_t *max_rt, uint8_t *rx_dr);
uint8_t NRF24L01_RxAvailable(uint8_t* pipe_num);
void NRF24L01_HandelIrqFlag(uint8_t *buf);
void NRF24L01_Tx(uint8_t *txbuf);
void NRF24L01_StartFastWrite(const void* txbuf);
uint8_t NRF24L01_WriteFast(const void* txbuf);
void NRF24L01_HandelIrqFlag(void);
void NRF24L01_Tx(uint8_t *pBuf);
void NRF24L01_StartFastWrite(const void* pBuf);
uint8_t NRF24L01_WriteFast(const void* pBuf);
void NRF24L01_ResetTX(void);
uint8_t NRF24L01_Check(void);
void NRF24L01_Init(NRF24_MODE mode);

View File

@ -71,7 +71,7 @@ void INT_Init()
INTERRUPT(Int2_Routine, EXTI_VectInt2)
{
NRF24L01_HandelIrqFlag(xbuf);
NRF24L01_HandelIrqFlag();
}
void main(void)