beegee-tokyo / SX126x-Arduino

Arduino library to use Semtech SX126x LoRa chips and modules to communicate
MIT License
235 stars 64 forks source link

Duration to period conversions are false in RadioSetRxDutyCycle (radio.cpp) #111

Closed JeromeBriot closed 7 months ago

JeromeBriot commented 1 year ago

Hello,

If the arguments rxTime and sleepTime are in ms, then the call to SX126xSetRxDutyCycle is false in the code below (Radio.cpp):

void RadioSetRxDutyCycle(uint32_t rxTime, uint32_t sleepTime)
{
    SX126xSetDioIrqParams(IRQ_RADIO_ALL | IRQ_RX_TX_TIMEOUT,
                          IRQ_RADIO_ALL | IRQ_RX_TX_TIMEOUT,
                          IRQ_RADIO_NONE, IRQ_RADIO_NONE);
    SX126xSetRxDutyCycle(rxTime, sleepTime);
}

It should be:

void RadioSetRxDutyCycle(uint32_t rxTime, uint32_t sleepTime)
{
    SX126xSetDioIrqParams(IRQ_RADIO_ALL | IRQ_RX_TX_TIMEOUT,
                          IRQ_RADIO_ALL | IRQ_RX_TX_TIMEOUT,
                          IRQ_RADIO_NONE, IRQ_RADIO_NONE);

    // Duration (s) = Period * 0.000015625 (see DS_SX1261-2_V2.1 datasheet chapter 13.1.7)
    // Period = Duration (s) / 0.000015625
    // 1/0.000015625 => 64000 => SX126X RTC 64 kHz clock (see DS_SX1261-2_V2.1 datasheet chapter 4.1.1)
    // Period = Duration (s) * 64000
    // Period = Duration (ms) * 64000 / 1000 = Duration (ms) * 64 = Duration (ms) << 6
    //
    // Example for duration = 2ms => period = 2 << 6 = 128 => 128 * 0.000015625 = 0.002s = 2ms

    SX126xSetRxDutyCycle(rxTime << 6, sleepTime << 6);
}

To be consistent with the datasheet, it should be useful to rename the arguments of the SX126xSetRxDutyCycle function (sx126x.cpp):

void SX126xSetRxDutyCycle(uint32_t rxPeriod, uint32_t sleepPeriod)
{
    uint8_t buf[6];

    buf[0] = (uint8_t)((rxPeriod >> 16) & 0xFF);
    buf[1] = (uint8_t)((rxPeriod >> 8) & 0xFF);
    buf[2] = (uint8_t)(rxPeriod & 0xFF);
    buf[3] = (uint8_t)((sleepPeriod >> 16) & 0xFF);
    buf[4] = (uint8_t)((sleepPeriod >> 8) & 0xFF);
    buf[5] = (uint8_t)(sleepPeriod & 0xFF);
    SX126xWriteCommand(RADIO_SET_RXDUTYCYCLE, buf, 6);
    SX126xSetOperatingMode(MODE_RX_DC);
}

Thank you