uint8_t NRF24L01_DataReady(void) { uint8_t status = NRF24L01_GetStatus(); if (NRF24L01_CHECK_BIT(status, NRF24L01_RX_DR)) { return 1; } return !NRF24L01_RxFifoEmpty(); }
NRF24L01_Transmit_Status_t NRF24L01_GetTransmissionStatus(void) { uint8_t status = NRF24L01_GetStatus(); if (NRF24L01_CHECK_BIT(status, NRF24L01_TX_DS)) { /* Successfully sent */ return NRF24L01_Transmit_Status_Ok; } else if (NRF24L01_CHECK_BIT(status, NRF24L01_MAX_RT)) { /* Message lost */ return NRF24L01_Transmit_Status_Lost; } /* Still sending */ return NRF24L01_Transmit_Status_Sending; }
/* Interrupt Service Routines ------------------------------------------------*/ void NRF24L01_Interrupt(NRF24L01_Device* Device) { uint8_t status = NRF24L01_GetStatus(Device); /* Data Sent TX FIFO interrupt, asserted when packet transmitted on TX */ if (status & (1 << TX_DS)) { NRF24L01_PowerUpInRxMode(Device); NRF24L01_ResetTxFlags(Device); /* Give the semaphore because we are done transmitting */ xSemaphoreGiveFromISR(Device->xTxSemaphore, NULL); } /* Maximum number of TX retransmits interrupt */ else if (status & (1 << MAX_RT)) { NRF24L01_PowerUpInRxMode(Device); NRF24L01_ResetTxFlags(Device); /* Give the semaphore because we are done transmitting */ xSemaphoreGiveFromISR(Device->xTxSemaphore, NULL); } /* Data Ready interrupt */ else if (status & (1 << RX_DR)) { uint8_t pipe = PIPE_FROM_STATUS(status); if (pipe < 6) { uint8_t buffer[32] = {}; uint8_t availableData = NRF24L01_GetDataFromRxBuffer(Device, buffer); for (uint32_t i = 0; i < availableData; i++) { if (!CIRC_BUFFER_IsFull(&Device->RxPipeBuffer[pipe])) CIRC_BUFFER_Insert(&Device->RxPipeBuffer[pipe], buffer[i]); } /* Give the xDataAvailableSemaphore to indicate there is new data available */ xSemaphoreGiveFromISR(Device->xDataAvailableSemaphore, NULL); } NRF24L01_ResetDataReadyFlag(Device); } }
/** * @brief Write some debug info to the UART * @param Device: The device to use * @param None * @retval None */ void NRF24L01_PrintDebugInfo(NRF24L01_Device* Device) { printf("------- nRF24L01 Debug Info -------\n"); printf("Name: %s\n", Device->NRF24L01_DeviceName); /* ============================ */ uint8_t config; NRF24L01_ReadRegister(Device, CONFIG, &config, 1); printf("CONFIG: 0x%02x\n", config); /* ============================ */ uint8_t en_aa; NRF24L01_ReadRegister(Device, EN_AA, &en_aa, 1); printf("EN_AA: 0x%02x\n", en_aa); /* ============================ */ uint8_t en_rxaddr; NRF24L01_ReadRegister(Device, EN_RXADDR, &en_rxaddr, 1); printf("EN_RXADDR: 0x%02x\n", en_rxaddr); /* ============================ */ uint8_t setupAw; NRF24L01_ReadRegister(Device, SETUP_AW, &setupAw, 1); printf("SETUP_AW: 0x%02x\n", setupAw); /* ============================ */ uint8_t setupRetr; NRF24L01_ReadRegister(Device, SETUP_RETR, &setupRetr, 1); printf("SETUP_RETR: 0x%02x\n", setupRetr); /* ============================ */ uint8_t rfChannel; NRF24L01_ReadRegister(Device, RF_CH, &rfChannel, 1); printf("RF_CH: 0x%02x\n", rfChannel); /* ============================ */ uint8_t rfSetup; NRF24L01_ReadRegister(Device, RF_SETUP, &rfSetup, 1); printf("RF_SETUP: 0x%02x\n", rfSetup); /* ============================ */ uint8_t status = NRF24L01_GetStatus(Device); printf("STATUS: 0x%02x\n", status); /* ============================ */ // uint8_t pipe = NRF24L01_GetPipeNumber(Device); // if (0x07 == pipe) // printf("Pipe: RF FIFO Empty\n"); // else if (0x06 == pipe) // printf("Pipe: ERROR - Should not happen!\n"); // else // printf("Pipe: %d\n", pipe); /* ============================ */ uint8_t observeTx; NRF24L01_ReadRegister(Device, OBSERVE_TX, &observeTx, 1); printf("OBSERVE_TX: 0x%02x\n", observeTx); /* ============================ */ uint8_t rpd; NRF24L01_ReadRegister(Device, RPD, &rpd, 1); printf("RPD: 0x%02x\n", rpd); /* ============================ */ uint8_t addr0[5]; NRF24L01_ReadRegister(Device, RX_ADDR_P0, addr0, 5); printf("RX_ADDR_P0: 0x%02x%02x%02x%02x%02x\n", addr0[4], addr0[3], addr0[2], addr0[1], addr0[0]); uint8_t addr1[5]; NRF24L01_ReadRegister(Device, RX_ADDR_P1, addr1, 5); printf("RX_ADDR_P1: 0x%02x%02x%02x%02x%02x\n", addr1[4], addr1[3], addr1[2], addr1[1], addr1[0]); uint8_t addr2; NRF24L01_ReadRegister(Device, RX_ADDR_P2, &addr2, 1); printf("RX_ADDR_P2: 0x%02x\n", addr2); uint8_t addr3; NRF24L01_ReadRegister(Device, RX_ADDR_P3, &addr3, 1); printf("RX_ADDR_P3: 0x%02x\n", addr3); uint8_t addr4; NRF24L01_ReadRegister(Device, RX_ADDR_P4, &addr4, 1); printf("RX_ADDR_P4: 0x%02x\n", addr4); uint8_t addr5; NRF24L01_ReadRegister(Device, RX_ADDR_P5, &addr5, 1); printf("RX_ADDR_P5: 0x%02x\n", addr5); /* ============================ */ uint8_t addrTx[5]; NRF24L01_ReadRegister(Device, TX_ADDR, addrTx, 5); printf("TX_ADDR: 0x%02x%02x%02x%02x%02x\n", addrTx[4], addrTx[3], addrTx[2], addrTx[1], addrTx[0]); /* ============================ */ uint8_t payloads[6]; NRF24L01_ReadRegister(Device, RX_PW_P0, payloads, 6); printf("RX_PW_P0-5: 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x\n", payloads[0], payloads[1], payloads[2], payloads[3], payloads[4], payloads[5]); /* ============================ */ uint8_t fifoStatus; NRF24L01_ReadRegister(Device, FIFO_STATUS, &fifoStatus, 1); printf("FIFO_STATUS: 0x%02x\n", fifoStatus); /* ============================ */ uint8_t dynpd; NRF24L01_ReadRegister(Device, DYNPD, &dynpd, 1); printf("DYNPD: 0x%02x\n", dynpd); printf("-----------------------------------\n"); }
void transmitter(void) { /* My address */ uint8_t MyAddress[] = { 0xE7, 0xE7, 0xE7, 0xE7, 0xE7 }; /* Receiver address */ uint8_t TxAddress[] = { 0x7E, 0x7E, 0x7E, 0x7E, 0x7E }; uint8_t dataOut[32], dataIn[32], i; uint32_t tr_count; NRF24L01_Transmit_Status_t transmissionStatus; /* Initialize NRF24L01+ on channel 15 and 32bytes of payload */ /* By default 2Mbps data rate and 0dBm output power */ /* NRF24L01 goes to RX mode by default */ NRF24L01_Init(15, 32); /* Set RF settings, Data rate to 2Mbps, Output power to -18dBm */ NRF24L01_SetRF(NRF24L01_DataRate_2M, NRF24L01_OutputPower_M18dBm); /* Set my address, 5 bytes */ //NRF24L01_SetMyAddress(MyAddress); /* Set TX address, 5 bytes */ //NRF24L01_SetTxAddress(TxAddress); uint8_t reg; delay_(720000); reg = NRF24L01_ReadRegister(NRF24L01_REG_CONFIG); printf("NRF24L01_REG_CONFIG: 0x%X\r\n", reg); reg = NRF24L01_ReadRegister(NRF24L01_REG_EN_RXADDR); printf("NRF24L01_REG_EN_RXADDR: 0x%X\r\n", reg); reg = NRF24L01_ReadRegister(NRF24L01_REG_RF_CH); printf("NRF24L01_REG_RF_CH: 0x%X\r\n", reg); reg = NRF24L01_ReadRegister(NRF24L01_REG_CONFIG); printf("NRF24L01_REG_CONFIG: 0x%X\r\n", reg); reg = NRF24L01_ReadRegister(NRF24L01_REG_RF_CH); printf("NRF24L01_REG_RF_CH: 0x%X\r\n", reg); reg = NRF24L01_ReadRegister(NRF24L01_REG_CONFIG); printf("NRF24L01_REG_CONFIG: 0x%X\r\n", reg); delay_(720000); for (i = 0; i < 32; i++) { dataOut[i] = i + 30; dataIn[i] = 0; } while (1) { /* Transmit data, goes automatically to TX mode */ NRF24L01_Transmit(dataOut); /* Turn on led to indicate sending */ LED2_ON; /* Wait for data to be sent */ do { transmissionStatus = NRF24L01_GetTransmissionStatus(); printf("transmissionStatus: %d\r\n", transmissionStatus); delay_(72000); } while (transmissionStatus == NRF24L01_Transmit_Status_Sending); /* Turn off led */ LED2_OFF; /* Go back to RX mode */ NRF24L01_PowerUpRx(); i = 0; uint8_t status = NRF24L01_GetStatus(); printf("status: %d\r\n", status); /* Wait received data, wait max 100ms, if time is larger, then data were probably lost */ while (!NRF24L01_DataReady()) { delay_(720000); i++; printf("TM_NRF24L01_DataReady\r\n"); if( i > 100) break; } /* Get data from NRF2L01+ */ NRF24L01_GetData(dataIn); printf("dataIn: %s\r\n", dataIn); /* Check transmit status */ if (transmissionStatus == NRF24L01_Transmit_Status_Ok) { tr_count++; printf("tr_count: %s\r\n", tr_count); } delay_(7200000); } }