void nrf24_powerUpRx() { nrf24_csn_digitalWrite(LOW); spi_transfer(NRF24L01P_CMD_FLUSH_RX, 1); nrf24_csn_digitalWrite(HIGH); nrf24_configRegister(NRF24L01P_REG_STATUS,(1<<NRF24L01P_SHIFT_RX_DR)|(1<<NRF24L01P_SHIFT_TX_DS)|(1<<NRF24L01P_SHIFT_MAX_RT)); nrf24_ce_digitalWrite(LOW); nrf24_configRegister(NRF24L01P_REG_CONFIG,NRF_INIT|((1<<NRF24L01P_SHIFT_PWR_UP)|(1<<NRF24L01P_SHIFT_PRIM_RX))); nrf24_ce_digitalWrite(HIGH); }
void nrf24_powerUpRx() { nrf24_csn_digitalWrite(LOW); spi_transfer(FLUSH_RX); nrf24_csn_digitalWrite(HIGH); nrf24_configRegister(STATUS,(1<<RX_DR)|(1<<TX_DS)|(1<<MAX_RT)); nrf24_ce_digitalWrite(LOW); nrf24_configRegister(CONFIG,nrf24_CONFIG|((1<<PWR_UP)|(1<<PRIM_RX))); nrf24_ce_digitalWrite(HIGH); }
/* init the hardware pins */ void nrf24_init() { nrf24_setupPins(); nrf24_ce_digitalWrite(LOW); nrf24_csn_digitalWrite(HIGH); nrf24_ce_digitalWrite(HIGH); nrf24_config(2,32); //nrf24_readRegister(CONFIG,&tx1,1); //tx1=0; //nrf24_configRegister(RF_CH,2); //nrf24_readRegister(EN_AA,&tx1,1); //tx1=0; nrf24_configRegister(RF_CH,2); nrf24_readRegister(RF_CH,&tx1,1); tx1=0; nrf24_readRegister(CONFIG,&tx1,1); tx1=0; nrf24_readRegister(RX_ADDR_P0,&readData,5); tx1=0; nrf24_readRegister(RF_SETUP,&tx1,1); tx1=0; nrf24_readRegister(TX_ADDR,&readData,5); tx1=0; nrf24_configRegister(SETUP_AW,2); //nrf24_readRegister(SETUP_AW,&tx1,1); //nrf24_configRegister(DYNPD,0x00); //nrf24_configRegister(FEATURE,0x00); //nrf24_tx_address(&tx_address11); //copy_paste(); // nrf_cmd(0x20, 0x12); //on, no crc, int on RX/TX done // nrf_cmd(0x21, 0x00); //no auto-acknowledge // nrf_cmd(0x22, 0x00); //no RX // nrf_cmd(0x23, 0x02); //5-byte address // nrf_cmd(0x24, 0x00); //no auto-retransmit // nrf_cmd(0x26, 0x06); //1MBps at 0dBm // nrf_cmd(0x27, 0x3E); //clear various flags // nrf_cmd(0x3C, 0x00); //no dynamic payloads // nrf_cmd(0x3D, 0x00); //no features // nrf_cmd(0x31, 32); //always RX 32 bytes // nrf_cmd(0x22, 0x01); //RX on pipe 0 }
/* Reads payload bytes into data array */ void nrf24_getData(uint8_t* data) { /* Pull down chip select */ nrf24_csn_digitalWrite(LOW); /* Send cmd to read rx payload */ spi_transfer( R_RX_PAYLOAD ); /* Read payload */ nrf24_transferSync(data,data,payload_len); /* Pull up chip select */ nrf24_csn_digitalWrite(HIGH); /* Reset status register */ nrf24_configRegister(STATUS,(1<<RX_DR)); }
/* configure the module */ void nrf24_config(uint8_t channel, uint8_t pay_length) { /* Use static payload length ... */ payload_len = pay_length; // Set RF channel nrf24_configRegister(RF_CH,channel); // Set length of incoming payload nrf24_configRegister(RX_PW_P0, 0x00); // Auto-ACK pipe ... nrf24_configRegister(RX_PW_P1, payload_len); // Data payload pipe nrf24_configRegister(RX_PW_P2, 0x00); // Pipe not used nrf24_configRegister(RX_PW_P3, 0x00); // Pipe not used nrf24_configRegister(RX_PW_P4, 0x00); // Pipe not used nrf24_configRegister(RX_PW_P5, 0x00); // Pipe not used // 250 kbps, TX gain: 0dbm nrf24_configRegister(RF_SETUP, (0<<RF_DR_HIGH)|((1<<RF_DR_LOW)|(0x03)<<RF_PWR)); // CRC enable, 1 byte CRC length nrf24_configRegister(CONFIG,nrf24_CONFIG); // Auto Acknowledgment nrf24_configRegister(EN_AA,(1<<ENAA_P0)|(1<<ENAA_P1)|(0<<ENAA_P2)|(0<<ENAA_P3)|(0<<ENAA_P4)|(0<<ENAA_P5)); // Enable RX addresses nrf24_configRegister(EN_RXADDR,(1<<ERX_P0)|(1<<ERX_P1)|(0<<ERX_P2)|(0<<ERX_P3)|(0<<ERX_P4)|(0<<ERX_P5)); // Auto retransmit delay: 1000 us and Up to 15 retransmit trials nrf24_configRegister(SETUP_RETR,(0x04<<ARD)|(0x0F<<ARC)); // Dynamic length configurations: No dynamic length nrf24_configRegister(DYNPD,(0<<DPL_P0)|(0<<DPL_P1)|(0<<DPL_P2)|(0<<DPL_P3)|(0<<DPL_P4)|(0<<DPL_P5)); // Start listening nrf24_powerUpRx(); }
void nrf24_powerDown() { nrf24_ce_digitalWrite(LOW); nrf24_configRegister(CONFIG,nrf24_CONFIG); }
void nrf24_powerUpTx() { nrf24_configRegister(STATUS,(1<<RX_DR)|(1<<TX_DS)|(1<<MAX_RT)); nrf24_configRegister(CONFIG,nrf24_CONFIG|((1<<PWR_UP)|(0<<PRIM_RX))); }
/* configure the module */ void nrf24_config(uint8_t channel, uint8_t pay_length) { /* Use static payload length ... */ payload_len = pay_length; //nrf24_activate_special(); // Set RF channel nrf24_configRegister(RF_CH,channel); // Set length of incoming payload nrf24_configRegister(RX_PW_P0, 32); // Auto-ACK pipe ... nrf24_configRegister(RX_PW_P1, 0x00); // Data payload pipe nrf24_configRegister(RX_PW_P2, 0x00); // Pipe not used nrf24_configRegister(RX_PW_P3, 0x00); // Pipe not used nrf24_configRegister(RX_PW_P4, 0x00); // Pipe not used nrf24_configRegister(RX_PW_P5, 0x00); // Pipe not used // 1 Mbps, TX gain: 0dbm nrf24_configRegister(RF_SETUP, 0x07); // CRC enable, 1 byte CRC length nrf24_configRegister(CONFIG,0x06); // Auto Acknowledgment nrf24_configRegister(EN_AA,0x00); // Enable RX addresses nrf24_configRegister(EN_RXADDR,(1<<ERX_P0)|(0<<ERX_P1)|(0<<ERX_P2)|(0<<ERX_P3)|(0<<ERX_P4)|(0<<ERX_P5)); // Auto retransmit delay: 1000 us and Up to 15 retransmit trials nrf24_configRegister(SETUP_RETR,(0<<ARD)|(0<<ARC)); nrf24_configRegister(SETUP_AW,2); // Dynamic length configurations: No dynamic length // Start listening //nrf24_configRegister(FEATURE,1<<EN_DPL); //nrf24_configRegister(DYNPD,(1<<DPL_P0)|(1<<DPL_P1)|(0<<DPL_P2)|(0<<DPL_P3)|(0<<DPL_P4)|(0<<DPL_P5)); //nrf24_powerUpRx(); }
/* configure the module */ void nrf24_config(uint8_t channel) { /* Use static payload length ... */ // payload_len = pay_length; // Set RF channel nrf24_configRegister(NRF24L01P_REG_RF_CH, (channel << NRF24L01P_SHIFT_RF_CH) & NRF24L01P_MASK_RF_CH); // Set length of incoming payload nrf24_configRegister(NRF24L01P_REG_RX_PW_P0, 0x00); // Auto-ACK pipe ... nrf24_configRegister(NRF24L01P_REG_RX_PW_P1, payload_len); // Data payload pipe nrf24_configRegister(NRF24L01P_REG_RX_PW_P2, 0x00); // Pipe not used nrf24_configRegister(NRF24L01P_REG_RX_PW_P3, 0x00); // Pipe not used nrf24_configRegister(NRF24L01P_REG_RX_PW_P4, 0x00); // Pipe not used nrf24_configRegister(NRF24L01P_REG_RX_PW_P5, 0x00); // Pipe not used // 1 Mbps, TX gain: 0dbm nrf24_configRegister(NRF24L01P_REG_RF_SETUP, (0<<NRF24L01P_SHIFT_RF_DR_HIGH)|((0x03)<<NRF24L01P_SHIFT_RF_PWR)); //0x6 // CRC enable, 1 byte CRC length nrf24_configRegister(NRF24L01P_REG_CONFIG,NRF_INIT); // Auto Acknowledgment nrf24_configRegister(NRF24L01P_REG_EN_AA,(1<<NRF24L01P_SHIFT_ENAA_P0)|(1<<NRF24L01P_SHIFT_ENAA_P1)|(0<<NRF24L01P_SHIFT_ENAA_P2)|(0<<NRF24L01P_SHIFT_ENAA_P3)|(0<<NRF24L01P_SHIFT_ENAA_P4)|(0<<NRF24L01P_SHIFT_ENAA_P5)); // Enable RX addresses nrf24_configRegister(NRF24L01P_REG_EN_RXADDR,(1<<NRF24L01P_SHIFT_ERX_P0)|(1<<NRF24L01P_SHIFT_ERX_P1)|(0<<NRF24L01P_SHIFT_ERX_P2)|(0<<NRF24L01P_SHIFT_ERX_P3)|(0<<NRF24L01P_SHIFT_ERX_P4)|(0<<NRF24L01P_SHIFT_ERX_P5)); // Auto retransmit delay: 1000 us and Up to 15 retransmit trials nrf24_configRegister(NRF24L01P_REG_SETUP_RETR,(0x04<<NRF24L01P_SHIFT_ARD)|(0x0F<<NRF24L01P_SHIFT_ARC)); // Dynamic length configurations: No dynamic length nrf24_configRegister(NRF24L01P_REG_DYNPD,(0<<NRF24L01P_SHIFT_DPL_P0)|(0<<NRF24L01P_SHIFT_DPL_P1)|(0<<NRF24L01P_SHIFT_DPL_P2)|(0<<NRF24L01P_SHIFT_DPL_P3)|(0<<NRF24L01P_SHIFT_DPL_P4)|(0<<NRF24L01P_SHIFT_DPL_P5)); // Start listening nrf24_powerUpRx(); }
void nrf24_powerDown() { nrf24_ce_digitalWrite(LOW); nrf24_configRegister(NRF24L01P_REG_CONFIG,NRF_INIT); }
void nrf24_powerUpTx() { nrf24_configRegister(NRF24L01P_REG_STATUS,(1<<NRF24L01P_SHIFT_RX_DR)|(1<<NRF24L01P_SHIFT_TX_DS)|(1<<NRF24L01P_SHIFT_MAX_RT)); //0x27, 0x70 nrf24_configRegister(NRF24L01P_REG_CONFIG,NRF_INIT|((1<<NRF24L01P_SHIFT_PWR_UP)|(0<<NRF24L01P_SHIFT_PRIM_RX))); //0x20, 0x0a }
void nrf24_set_channel(uint8_t channel) { nrf24_configRegister(NRF24L01P_REG_RF_CH, (channel << NRF24L01P_SHIFT_RF_CH) & NRF24L01P_MASK_RF_CH); }