uint8_t NRF24L01_ReadBit(uint8_t reg, uint8_t bit) { uint8_t tmp; tmp = NRF24L01_ReadRegister(reg); if (!NRF24L01_CHECK_BIT(tmp, bit)) { return 0; } return 1; }
/** * @brief Enable a pipe * @param Device: The device to use * @param Pipe: The pipe to enable, can be 0 to 5 * @retval None */ void NRF24L01_EnablePipe(NRF24L01_Device* Device, uint8_t Pipe) { if (Pipe < 6) { uint8_t value = 0; NRF24L01_ReadRegister(Device, EN_RXADDR, &value, 1); /* Get the old value */ value |= (1 << Pipe); /* Enable the new pipe */ NRF24L01_WriteRegister(Device, EN_RXADDR, &value, 1); /* Set the new value */ } }
void NRF24L01_WriteBit(uint8_t reg, uint8_t bit, uint8_t value) { uint8_t tmp; /* Read register */ tmp = NRF24L01_ReadRegister(reg); /* Make operation */ if (value) { tmp |= 1 << bit; } else { tmp &= ~(1 << bit); } /* Write back */ NRF24L01_WriteRegister(reg, tmp); }
uint8_t NRF24L01_GetRetransmissionsCount(void) { /* Low 4 bits */ return NRF24L01_ReadRegister(NRF24L01_REG_OBSERVE_TX) & 0x0F; }
uint8_t NRF24L01_RxFifoEmpty(void) { uint8_t reg = NRF24L01_ReadRegister(NRF24L01_REG_FIFO_STATUS); return NRF24L01_CHECK_BIT(reg, NRF24L01_RX_EMPTY); }
/** * @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"); }
/** * @brief Get the RF channel to transmit on * @param Device: The device to use * @retval None */ uint8_t NRF24L01_GetRFChannel(NRF24L01_Device* Device) { uint8_t data = 0; NRF24L01_ReadRegister(Device, R_REGISTER | RF_CH, &data, 1); return data; }
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); } }