// Put nRF24L01 in TX mode void nRF24_TXMode(void) { CE_L(); nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_EN_AA,0x01); // Enable ShockBurst for data pipe 0 nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_SETUP_RETR,0x1A); // Auto retransmit: wait 500us, 10 retries nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_RF_CH,0x6E); // Set frequency channel 110 (2.510MHz) nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_RF_SETUP,0x06); // Setup: 1Mbps, 0dBm, LNA off nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_CONFIG,0x0E); // Config: CRC on (2 bytes), Power UP, RX/TX ctl = PTX }
// Send data packet // input: // pBuf - buffer with data to send // return: // nRF24_MASK_MAX_RT - if transmit failed with maximum auto retransmit count // nRF24_MAX_TX_DS - if transmit succeed // contents of STATUS register otherwise uint8_t nRF24_TXPacket(uint8_t * pBuf, uint8_t TX_PAYLOAD) { uint8_t status; CE_L(); nRF24_WriteBuf(nRF24_CMD_WREG | nRF24_REG_TX_ADDR,nRF24_TX_addr,nRF24_TX_ADDR_WIDTH); // Set static TX address nRF24_WriteBuf(nRF24_CMD_WREG | nRF24_REG_RX_ADDR_P0,nRF24_RX_addr,nRF24_RX_ADDR_WIDTH); // Set static RX address for auto ack nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_EN_AA,0x01); // Enable auto acknowledgement for data pipe 0 nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_SETUP_RETR,0x1A); // Automatic retransmission: wait 500us, 10 retries nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_RF_CH,0x6E); // Set frequency channel 110 (2.510MHz) nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_RF_SETUP,0x07); // Setup: 1Mbps, 0dBm, LNA on nRF24_WriteBuf(nRF24_CMD_W_TX_PAYLOAD,pBuf,TX_PAYLOAD); // Write specified buffer to FIFO nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_CONFIG,0x0E); // Config: CRC on (2 bytes), Power UP, RX/TX ctl = PTX CE_H(); // CE pin high => Start transmit // Delay_us(10); // Must hold CE at least 10us //while(PB_IDR_bit.IDR2 != 0); // Wait for IRQ from nRF24L01 CE_L(); status = nRF24_ReadReg(nRF24_REG_STATUS); // Read status register nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_STATUS,status | 0x70); // Clear RX_DR, TX_DS, MAX_RT flags if (status & nRF24_MASK_MAX_RT) { // Auto retransmit counter exceeds the programmed maximum limit. FIFO is not removed. nRF24_RWReg(nRF24_CMD_FLUSH_TX,0xFF); // Flush TX FIFO buffer return nRF24_MASK_MAX_RT; }; if (status & nRF24_MASK_TX_DS) { // Transmit ok nRF24_RWReg(nRF24_CMD_FLUSH_TX,0xFF); // Flush TX FIFO buffer return nRF24_MASK_TX_DS; } // Some banana happens return status; }
// Wake nRF24 from Power Down mode (usually wakes to Standby-I mode within 1.5ms) void nRF24_Wake(void) { uint8_t conf; conf = nRF24_ReadReg(nRF24_REG_CONFIG); conf |= (1<<1); // Set PWR_UP bit nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_CONFIG,conf); // Wakeup // Delay_ms(2); // Wakeup from Power Down to Standby-I mode takes 1.5ms }
// Put nRF24 in Power Down mode void nRF24_PowerDown(void) { uint8_t conf; CE_L(); // CE pin to low conf = nRF24_ReadReg(nRF24_REG_CONFIG); conf &= ~(1<<1); // Clear PWR_UP bit nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_CONFIG,conf); // Go Power down mode }
uint8_t nRF24_RXPacket(uint8_t* pBuf, uint8_t RX_PAYLOAD) { uint8_t status; status = nRF24_ReadReg(nRF24_REG_STATUS); // Read status register if (status & nRF24_MASK_RX_DR) { if ((status & 0x0E) == 0) { // pipe 0 nRF24_ReadBuf(nRF24_CMD_R_RX_PAYLOAD,pBuf,RX_PAYLOAD); // read received payload from RX FIFO buffer } nRF24_ReadWrite(nRF24_CMD_FLUSH_RX); // Flush RX FIFO buffer nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_STATUS,status | 0x70); // Clear RX_DR, TX_DS, MAX_RT flags //return nRF24_MASK_RX_DR; return status; } // Some banana happens nRF24_ReadWrite(nRF24_CMD_FLUSH_RX); // Flush RX FIFO buffer nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_STATUS,status | 0x70); // Clear RX_DR, TX_DS, MAX_RT flags return status; }
// Put nRF24L01 in RX mode void nRF24_RXMode(uint8_t RX_PAYLOAD) { CE_L(); nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_EN_AA,0x01); // Enable ShockBurst for data pipe 0 nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_EN_RXADDR,0x01); // Enable data pipe 0 nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_RF_CH,0x6E); // Set frequency channel 110 (2.510MHz) nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_RX_PW_P0,RX_PAYLOAD); // Set RX payload length nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_RF_SETUP,0x06); // Setup: 1Mbps, 0dBm, LNA off nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_CONFIG,0x0F); // Config: CRC on (2 bytes), Power UP, RX/TX ctl = PRX nRF24_WriteBuf(nRF24_CMD_WREG | nRF24_REG_RX_ADDR_P0,nRF24_RX_addr,nRF24_RX_ADDR_WIDTH); // Set static RX address CE_H(); // Delay_us(10); // Hold CE high at least 10us }
// Clear all IRQ flags void nRF24_ClearIRQFlags(void) { uint8_t status; status = nRF24_ReadReg(nRF24_REG_STATUS); nRF24_RWReg(nRF24_CMD_WREG | nRF24_REG_STATUS,status | 0x70); // Clear RX_DR, TX_DS, MAX_RT flags }