acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

nRF24.cpp (18088B)

      1 #include "nRF24.h"
      2 #if !defined(RADIOLIB_EXCLUDE_NRF24)
      3 
      4 nRF24::nRF24(Module* mod) : PhysicalLayer(RADIOLIB_NRF24_FREQUENCY_STEP_SIZE, RADIOLIB_NRF24_MAX_PACKET_LENGTH) {
      5   _mod = mod;
      6 }
      7 
      8 Module* nRF24::getMod() {
      9   return(_mod);
     10 }
     11 
     12 int16_t nRF24::begin(int16_t freq, int16_t dataRate, int8_t power, uint8_t addrWidth) {
     13   // set module properties
     14   _mod->SPIreadCommand = RADIOLIB_NRF24_CMD_READ;
     15   _mod->SPIwriteCommand = RADIOLIB_NRF24_CMD_WRITE;
     16   _mod->init();
     17   _mod->pinMode(_mod->getIrq(), INPUT);
     18 
     19   // set pin mode on RST (connected to nRF24 CE pin)
     20   _mod->pinMode(_mod->getRst(), OUTPUT);
     21   _mod->digitalWrite(_mod->getRst(), LOW);
     22 
     23   // wait for minimum power-on reset duration
     24   _mod->delay(100);
     25 
     26   // check SPI connection
     27   int16_t val = _mod->SPIgetRegValue(RADIOLIB_NRF24_REG_SETUP_AW);
     28   if(!((val >= 0) && (val <= 3))) {
     29     RADIOLIB_DEBUG_PRINTLN(F("No nRF24 found!"));
     30     _mod->term();
     31     return(RADIOLIB_ERR_CHIP_NOT_FOUND);
     32   }
     33   RADIOLIB_DEBUG_PRINTLN(F("M\tnRF24"));
     34 
     35   // configure settings inaccessible by public API
     36   int16_t state = config();
     37   RADIOLIB_ASSERT(state);
     38 
     39   // set mode to standby
     40   state = standby();
     41   RADIOLIB_ASSERT(state);
     42 
     43   // set frequency
     44   state = setFrequency(freq);
     45   RADIOLIB_ASSERT(state);
     46 
     47   // set data rate
     48   state = setDataRate(dataRate);
     49   RADIOLIB_ASSERT(state);
     50 
     51   // set output power
     52   state = setOutputPower(power);
     53   RADIOLIB_ASSERT(state);
     54 
     55   // set address width
     56   state = setAddressWidth(addrWidth);
     57   RADIOLIB_ASSERT(state);
     58 
     59   // set CRC
     60   state = setCrcFiltering(true);
     61   RADIOLIB_ASSERT(state);
     62 
     63   // set auto-ACK on all pipes
     64   state = setAutoAck(true);
     65   RADIOLIB_ASSERT(state);
     66 
     67   return(state);
     68 }
     69 
     70 int16_t nRF24::sleep() {
     71   return(_mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_POWER_DOWN, 1, 1));
     72 }
     73 
     74 int16_t nRF24::standby() {
     75   // make sure carrier output is disabled
     76   _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_CONT_WAVE_OFF, 7, 7);
     77   _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_PLL_LOCK_OFF, 4, 4);
     78   _mod->digitalWrite(_mod->getRst(), LOW);
     79 
     80   // use standby-1 mode
     81   return(_mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_POWER_UP, 1, 1));
     82 }
     83 
     84 int16_t nRF24::transmit(uint8_t* data, size_t len, uint8_t addr) {
     85   // start transmission
     86   int16_t state = startTransmit(data, len, addr);
     87   RADIOLIB_ASSERT(state);
     88 
     89   // wait until transmission is finished
     90   uint32_t start = _mod->micros();
     91   while(_mod->digitalRead(_mod->getIrq())) {
     92     _mod->yield();
     93 
     94     // check maximum number of retransmits
     95     if(getStatus(RADIOLIB_NRF24_MAX_RT)) {
     96       standby();
     97       clearIRQ();
     98       return(RADIOLIB_ERR_ACK_NOT_RECEIVED);
     99     }
    100 
    101     // check timeout: 15 retries * 4ms (max Tx time as per datasheet)
    102     if(_mod->micros() - start >= 60000) {
    103       standby();
    104       clearIRQ();
    105       return(RADIOLIB_ERR_TX_TIMEOUT);
    106     }
    107   }
    108 
    109   // clear interrupts
    110   clearIRQ();
    111 
    112   return(state);
    113 }
    114 
    115 int16_t nRF24::receive(uint8_t* data, size_t len) {
    116   // start reception
    117   int16_t state = startReceive();
    118   RADIOLIB_ASSERT(state);
    119 
    120   // wait for Rx_DataReady or timeout
    121   uint32_t start = _mod->micros();
    122   while(_mod->digitalRead(_mod->getIrq())) {
    123     _mod->yield();
    124 
    125     // check timeout: 15 retries * 4ms (max Tx time as per datasheet)
    126     if(_mod->micros() - start >= 60000) {
    127       standby();
    128       clearIRQ();
    129       return(RADIOLIB_ERR_RX_TIMEOUT);
    130     }
    131   }
    132 
    133   // read the received data
    134   return(readData(data, len));
    135 }
    136 
    137 int16_t nRF24::transmitDirect(uint32_t frf) {
    138   // set raw frequency value
    139   if(frf != 0) {
    140     uint8_t freqRaw = frf - 2400;
    141     _mod->SPIwriteRegister(RADIOLIB_NRF24_REG_RF_CH, freqRaw & 0b01111111);
    142   }
    143 
    144   // output carrier
    145   int16_t state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_PTX, 0, 0);
    146   state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_CONT_WAVE_ON, 7, 7);
    147   state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_PLL_LOCK_ON, 4, 4);
    148   _mod->digitalWrite(_mod->getRst(), HIGH);
    149   return(state);
    150 }
    151 
    152 int16_t nRF24::receiveDirect() {
    153   // nRF24 is unable to directly output demodulated data
    154   // this method is implemented only for PhysicalLayer compatibility
    155   return(RADIOLIB_ERR_NONE);
    156 }
    157 
    158 void nRF24::setIrqAction(void (*func)(void)) {
    159   _mod->attachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()), func, FALLING);
    160 }
    161 
    162 int16_t nRF24::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
    163   // suppress unused variable warning
    164   (void)addr;
    165 
    166   // check packet length
    167   if(len > RADIOLIB_NRF24_MAX_PACKET_LENGTH) {
    168     return(RADIOLIB_ERR_PACKET_TOO_LONG);
    169   }
    170 
    171   // set mode to standby
    172   int16_t state = standby();
    173   RADIOLIB_ASSERT(state);
    174 
    175   // enable primary Tx mode
    176   state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_PTX, 0, 0);
    177 
    178   // clear interrupts
    179   clearIRQ();
    180 
    181   // enable Tx_DataSent interrupt
    182   state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_MASK_TX_DS_IRQ_ON, 5, 5);
    183   RADIOLIB_ASSERT(state);
    184 
    185   // flush Tx FIFO
    186   SPItransfer(RADIOLIB_NRF24_CMD_FLUSH_TX);
    187 
    188   // fill Tx FIFO
    189   uint8_t buff[32];
    190   memset(buff, 0x00, 32);
    191   memcpy(buff, data, len);
    192   SPIwriteTxPayload(data, len);
    193 
    194   // CE high to start transmitting
    195   _mod->digitalWrite(_mod->getRst(), HIGH);
    196   _mod->delay(1);
    197   _mod->digitalWrite(_mod->getRst(), LOW);
    198 
    199   return(state);
    200 }
    201 
    202 int16_t nRF24::startReceive() {
    203   // set mode to standby
    204   int16_t state = standby();
    205   RADIOLIB_ASSERT(state);
    206 
    207   // enable primary Rx mode
    208   state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_PRX, 0, 0);
    209   RADIOLIB_ASSERT(state);
    210 
    211   // enable Rx_DataReady interrupt
    212   clearIRQ();
    213   state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG,  RADIOLIB_NRF24_MASK_RX_DR_IRQ_ON, 6, 6);
    214   RADIOLIB_ASSERT(state);
    215 
    216   // flush Rx FIFO
    217   SPItransfer(RADIOLIB_NRF24_CMD_FLUSH_RX);
    218 
    219   // CE high to start receiving
    220   _mod->digitalWrite(_mod->getRst(), HIGH);
    221 
    222   // wait to enter Rx state
    223   _mod->delay(1);
    224 
    225   return(state);
    226 }
    227 
    228 int16_t nRF24::readData(uint8_t* data, size_t len) {
    229   // set mode to standby
    230   int16_t state = standby();
    231   RADIOLIB_ASSERT(state);
    232 
    233   // get packet length
    234   size_t length = getPacketLength();
    235   if((len != 0) && (len < length)) {
    236     // user requested less data than we got, only return what was requested
    237     length = len;
    238   }
    239 
    240   // read packet data
    241   SPIreadRxPayload(data, length);
    242 
    243   // clear interrupt
    244   clearIRQ();
    245 
    246   return(RADIOLIB_ERR_NONE);
    247 }
    248 
    249 int16_t nRF24::setFrequency(int16_t freq) {
    250   RADIOLIB_CHECK_RANGE(freq, 2400, 2525, RADIOLIB_ERR_INVALID_FREQUENCY);
    251 
    252   // set frequency
    253   uint8_t freqRaw = freq - 2400;
    254   return(_mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_CH, freqRaw, 6, 0));
    255 }
    256 
    257 int16_t nRF24::setDataRate(int16_t dataRate) {
    258   // set mode to standby
    259   int16_t state = standby();
    260   RADIOLIB_ASSERT(state);
    261 
    262   // set data rate
    263   if(dataRate == 250) {
    264     state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_DR_250_KBPS, 5, 5);
    265     state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_DR_250_KBPS, 3, 3);
    266   } else if(dataRate == 1000) {
    267     state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_DR_1_MBPS, 5, 5);
    268     state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_DR_1_MBPS, 3, 3);
    269   } else if(dataRate == 2000) {
    270     state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_DR_2_MBPS, 5, 5);
    271     state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_DR_2_MBPS, 3, 3);
    272   } else {
    273     return(RADIOLIB_ERR_INVALID_DATA_RATE);
    274   }
    275 
    276   return(state);
    277 }
    278 
    279 int16_t nRF24::setOutputPower(int8_t power) {
    280   // set mode to standby
    281   int16_t state = standby();
    282   RADIOLIB_ASSERT(state);
    283 
    284   // check allowed values
    285   uint8_t powerRaw = 0;
    286   switch(power) {
    287     case -18:
    288       powerRaw = RADIOLIB_NRF24_RF_PWR_18_DBM;
    289       break;
    290     case -12:
    291       powerRaw = RADIOLIB_NRF24_RF_PWR_12_DBM;
    292       break;
    293     case -6:
    294       powerRaw = RADIOLIB_NRF24_RF_PWR_6_DBM;
    295       break;
    296     case 0:
    297       powerRaw = RADIOLIB_NRF24_RF_PWR_0_DBM;
    298       break;
    299     default:
    300       return(RADIOLIB_ERR_INVALID_OUTPUT_POWER);
    301   }
    302 
    303   // write new register value
    304   state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, powerRaw, 2, 1);
    305   return(state);
    306 }
    307 
    308 int16_t nRF24::setAddressWidth(uint8_t addrWidth) {
    309   // set mode to standby
    310   int16_t state = standby();
    311   RADIOLIB_ASSERT(state);
    312 
    313   // set address width
    314   switch(addrWidth) {
    315     case 2:
    316       // Even if marked as 'Illegal' on the datasheet this will work:
    317       // http://travisgoodspeed.blogspot.com/2011/02/promiscuity-is-nrf24l01s-duty.html
    318       state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_SETUP_AW, RADIOLIB_NRF24_ADDRESS_2_BYTES, 1, 0);
    319       break;
    320     case 3:
    321       state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_SETUP_AW, RADIOLIB_NRF24_ADDRESS_3_BYTES, 1, 0);
    322       break;
    323     case 4:
    324       state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_SETUP_AW, RADIOLIB_NRF24_ADDRESS_4_BYTES, 1, 0);
    325       break;
    326     case 5:
    327       state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_SETUP_AW, RADIOLIB_NRF24_ADDRESS_5_BYTES, 1, 0);
    328       break;
    329     default:
    330       return(RADIOLIB_ERR_INVALID_ADDRESS_WIDTH);
    331   }
    332 
    333   // save address width
    334   _addrWidth = addrWidth;
    335 
    336   return(state);
    337 }
    338 
    339 int16_t nRF24::setTransmitPipe(uint8_t* addr) {
    340   // set mode to standby
    341   int16_t state = standby();
    342   RADIOLIB_ASSERT(state);
    343 
    344   // set transmit address
    345   _mod->SPIwriteRegisterBurst(RADIOLIB_NRF24_REG_TX_ADDR, addr, _addrWidth);
    346 
    347   // set Rx pipe 0 address (for ACK)
    348   _mod->SPIwriteRegisterBurst(RADIOLIB_NRF24_REG_RX_ADDR_P0, addr, _addrWidth);
    349   state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P0_ON, 0, 0);
    350 
    351   return(state);
    352 }
    353 
    354 int16_t nRF24::setReceivePipe(uint8_t pipeNum, uint8_t* addr) {
    355   // set mode to standby
    356   int16_t state = standby();
    357   RADIOLIB_ASSERT(state);
    358 
    359   // write full pipe 0 - 1 address and enable the pipe
    360   switch(pipeNum) {
    361     case 0:
    362       _mod->SPIwriteRegisterBurst(RADIOLIB_NRF24_REG_RX_ADDR_P0, addr, _addrWidth);
    363       state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P0_ON, 0, 0);
    364       break;
    365     case 1:
    366       _mod->SPIwriteRegisterBurst(RADIOLIB_NRF24_REG_RX_ADDR_P1, addr, _addrWidth);
    367       state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P1_ON, 1, 1);
    368       break;
    369     default:
    370       return(RADIOLIB_ERR_INVALID_PIPE_NUMBER);
    371   }
    372 
    373   return(state);
    374 }
    375 
    376 int16_t nRF24::setReceivePipe(uint8_t pipeNum, uint8_t addrByte) {
    377   // set mode to standby
    378   int16_t state = standby();
    379   RADIOLIB_ASSERT(state);
    380 
    381   // write unique pipe 2 - 5 address and enable the pipe
    382   switch(pipeNum) {
    383     case 2:
    384       state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RX_ADDR_P2, addrByte);
    385       state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P2_ON, 2, 2);
    386       break;
    387     case 3:
    388       state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RX_ADDR_P3, addrByte);
    389       state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P3_ON, 3, 3);
    390       break;
    391     case 4:
    392       state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RX_ADDR_P4, addrByte);
    393       state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P4_ON, 4, 4);
    394       break;
    395     case 5:
    396       state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RX_ADDR_P5, addrByte);
    397       state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P5_ON, 5, 5);
    398       break;
    399     default:
    400       return(RADIOLIB_ERR_INVALID_PIPE_NUMBER);
    401   }
    402 
    403   return(state);
    404 }
    405 
    406 int16_t nRF24::disablePipe(uint8_t pipeNum) {
    407   // set mode to standby
    408   int16_t state = standby();
    409   RADIOLIB_ASSERT(state);
    410 
    411   switch(pipeNum) {
    412     case 0:
    413       state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P0_OFF, 0, 0);
    414       break;
    415     case 1:
    416       state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P1_OFF, 1, 1);
    417       break;
    418     case 2:
    419       state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P2_OFF, 2, 2);
    420       break;
    421     case 3:
    422       state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P3_OFF, 3, 3);
    423       break;
    424     case 4:
    425       state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P4_OFF, 4, 4);
    426       break;
    427     case 5:
    428       state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_RXADDR, RADIOLIB_NRF24_P5_OFF, 5, 5);
    429       break;
    430     default:
    431       return(RADIOLIB_ERR_INVALID_PIPE_NUMBER);
    432   }
    433 
    434   return(state);
    435 }
    436 
    437 int16_t nRF24::getStatus(uint8_t mask) {
    438   return(_mod->SPIgetRegValue(RADIOLIB_NRF24_REG_STATUS) & mask);
    439 }
    440 
    441 bool nRF24::isCarrierDetected() {
    442   return(_mod->SPIgetRegValue(RADIOLIB_NRF24_REG_RPD, 0, 0) == 1);
    443 }
    444 
    445 int16_t nRF24::setFrequencyDeviation(float freqDev) {
    446   // nRF24 is unable to set frequency deviation
    447   // this method is implemented only for PhysicalLayer compatibility
    448   (void)freqDev;
    449   return(RADIOLIB_ERR_NONE);
    450 }
    451 
    452 size_t nRF24::getPacketLength(bool update) {
    453   (void)update;
    454   uint8_t length = 0;
    455   SPItransfer(RADIOLIB_NRF24_CMD_READ_RX_PAYLOAD_WIDTH, false, NULL, &length, 1);
    456   return((size_t)length);
    457 }
    458 
    459 int16_t nRF24::setCrcFiltering(bool crcOn) {
    460   // Auto Ack needs to be disabled in order to disable CRC.
    461   if (!crcOn) {
    462     int16_t status = setAutoAck(false);
    463     RADIOLIB_ASSERT(status)
    464   }
    465 
    466   // Disable CRC
    467   return _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, (crcOn ? RADIOLIB_NRF24_CRC_ON : RADIOLIB_NRF24_CRC_OFF), 3, 3);
    468 }
    469 
    470 int16_t nRF24::setAutoAck(bool autoAckOn){
    471   return _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_AA, (autoAckOn ? RADIOLIB_NRF24_AA_ALL_ON : RADIOLIB_NRF24_AA_ALL_OFF), 5, 0);
    472 }
    473 
    474 int16_t nRF24::setAutoAck(uint8_t pipeNum, bool autoAckOn){
    475   switch(pipeNum) {
    476     case 0:
    477       return _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_AA, (autoAckOn ? RADIOLIB_NRF24_AA_P0_ON : RADIOLIB_NRF24_AA_P0_OFF), 0, 0);
    478       break;
    479     case 1:
    480       return _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_AA, (autoAckOn ? RADIOLIB_NRF24_AA_P1_ON : RADIOLIB_NRF24_AA_P1_OFF), 1, 1);
    481       break;
    482     case 2:
    483       return _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_AA, (autoAckOn ? RADIOLIB_NRF24_AA_P2_ON : RADIOLIB_NRF24_AA_P2_OFF), 2, 2);
    484       break;
    485     case 3:
    486       return _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_AA, (autoAckOn ? RADIOLIB_NRF24_AA_P3_ON : RADIOLIB_NRF24_AA_P3_OFF), 3, 3);
    487       break;
    488     case 4:
    489       return _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_AA, (autoAckOn ? RADIOLIB_NRF24_AA_P4_ON : RADIOLIB_NRF24_AA_P4_OFF), 4, 4);
    490       break;
    491     case 5:
    492       return _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_EN_AA, (autoAckOn ? RADIOLIB_NRF24_AA_P5_ON : RADIOLIB_NRF24_AA_P5_OFF), 5, 5);
    493       break;
    494     default:
    495       return (RADIOLIB_ERR_INVALID_PIPE_NUMBER);
    496   }
    497 }
    498 
    499 int16_t nRF24::setDataShaping(uint8_t sh) {
    500   // nRF24 is unable to set data shaping
    501   // this method is implemented only for PhysicalLayer compatibility
    502   (void)sh;
    503   return(RADIOLIB_ERR_NONE);
    504 }
    505 
    506 int16_t nRF24::setEncoding(uint8_t encoding) {
    507   // nRF24 is unable to set encoding
    508   // this method is implemented only for PhysicalLayer compatibility
    509   (void)encoding;
    510   return(RADIOLIB_ERR_NONE);
    511 }
    512 
    513 uint8_t nRF24::randomByte() {
    514   // nRF24 is unable to measure RSSI, hence no TRNG
    515   // this method is implemented only for PhysicalLayer compatibility
    516   return(0);
    517 }
    518 
    519 #if !defined(RADIOLIB_EXCLUDE_DIRECT_RECEIVE)
    520 void nRF24::setDirectAction(void (*func)(void)) {
    521   // nRF24 is unable to perform direct mode actions
    522   // this method is implemented only for PhysicalLayer compatibility
    523   (void)func;
    524 }
    525 
    526 void nRF24::readBit(RADIOLIB_PIN_TYPE pin) {
    527   // nRF24 is unable to perform direct mode actions
    528   // this method is implemented only for PhysicalLayer compatibility
    529   (void)pin;
    530 }
    531 #endif
    532 
    533 void nRF24::clearIRQ() {
    534   // clear status bits
    535   _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_STATUS, RADIOLIB_NRF24_RX_DR | RADIOLIB_NRF24_TX_DS | RADIOLIB_NRF24_MAX_RT, 6, 4);
    536 
    537   // disable interrupts
    538   _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_MASK_RX_DR_IRQ_OFF | RADIOLIB_NRF24_MASK_TX_DS_IRQ_OFF | RADIOLIB_NRF24_MASK_MAX_RT_IRQ_OFF, 6, 4);
    539 }
    540 
    541 int16_t nRF24::config() {
    542   // enable 16-bit CRC
    543   int16_t state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_CRC_ON | RADIOLIB_NRF24_CRC_16, 3, 2);
    544   RADIOLIB_ASSERT(state);
    545 
    546   // set 15 retries and delay 1500 (5*250) us
    547   _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_SETUP_RETR, (5 << 4) | 5);
    548 
    549   // set features: dynamic payload on, payload with ACK packets off, dynamic ACK off
    550   state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_FEATURE, RADIOLIB_NRF24_DPL_ON | RADIOLIB_NRF24_ACK_PAY_OFF | RADIOLIB_NRF24_DYN_ACK_OFF, 2, 0);
    551   RADIOLIB_ASSERT(state);
    552 
    553   // enable dynamic payloads
    554   state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_DYNPD, RADIOLIB_NRF24_DPL_ALL_ON, 5, 0);
    555   RADIOLIB_ASSERT(state);
    556 
    557   // reset IRQ
    558   clearIRQ();
    559 
    560   // clear status
    561   _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_STATUS, RADIOLIB_NRF24_RX_DR | RADIOLIB_NRF24_TX_DS | RADIOLIB_NRF24_MAX_RT, 6, 4);
    562 
    563   // flush FIFOs
    564   SPItransfer(RADIOLIB_NRF24_CMD_FLUSH_TX);
    565   SPItransfer(RADIOLIB_NRF24_CMD_FLUSH_RX);
    566 
    567   // power up
    568   _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_POWER_UP, 1, 1);
    569   _mod->delay(5);
    570 
    571   return(state);
    572 }
    573 
    574 void nRF24::SPIreadRxPayload(uint8_t* data, uint8_t numBytes) {
    575   SPItransfer(RADIOLIB_NRF24_CMD_READ_RX_PAYLOAD, false, NULL, data, numBytes);
    576 }
    577 
    578 void nRF24::SPIwriteTxPayload(uint8_t* data, uint8_t numBytes) {
    579   SPItransfer(RADIOLIB_NRF24_CMD_WRITE_TX_PAYLOAD, true, data, NULL, numBytes);
    580 }
    581 
    582 void nRF24::SPItransfer(uint8_t cmd, bool write, uint8_t* dataOut, uint8_t* dataIn, uint8_t numBytes) {
    583   // start transfer
    584   _mod->digitalWrite(_mod->getCs(), LOW);
    585   _mod->SPIbeginTransaction();
    586 
    587   // send command
    588   _mod->SPItransfer(cmd);
    589 
    590   // send data
    591   if(write) {
    592     for(uint8_t i = 0; i < numBytes; i++) {
    593       _mod->SPItransfer(dataOut[i]);
    594     }
    595   } else {
    596     for(uint8_t i = 0; i < numBytes; i++) {
    597       dataIn[i] = _mod->SPItransfer(0x00);
    598     }
    599   }
    600 
    601   // stop transfer
    602   _mod->SPIendTransaction();
    603   _mod->digitalWrite(_mod->getCs(), HIGH);
    604 }
    605 
    606 #endif