diff --git a/src/Arduino_LoRaWAN.h b/src/Arduino_LoRaWAN.h index e4f3f76..b00c6df 100644 --- a/src/Arduino_LoRaWAN.h +++ b/src/Arduino_LoRaWAN.h @@ -143,6 +143,8 @@ class Arduino_LoRaWAN ; } + static bool setFrequencyRaw(uint8_t *pFreq, unsigned iCh, uint32_t frequency); + /* || provisioning things: */ @@ -358,16 +360,12 @@ class Arduino_LoRaWAN /// bool setFrequency(uint8_t (&freq)[nCh * 3], unsigned iCh, uint32_t frequency) { + // check the template parameter while it's in scope. if (iCh > nCh) return false; - const uint32_t reducedFreq = frequency / 100; - if (reducedFreq > 0xFFFFFFu) - return false; - auto const chPtr = freq + iCh * 3; - chPtr[0] = uint8_t(reducedFreq >> 16); - chPtr[1] = uint8_t(reducedFreq >> 8); - chPtr[2] = uint8_t(reducedFreq); + // call an external function to make it harder for optimizer to do the wrong thing (for test) + return Arduino_LoRaWAN::setFrequencyRaw(freq, iCh, frequency); } /// \brief clear all entries in the channel table. diff --git a/src/lib/arduino_lorawan.cpp b/src/lib/arduino_lorawan.cpp index b35db47..cdb1453 100644 --- a/src/lib/arduino_lorawan.cpp +++ b/src/lib/arduino_lorawan.cpp @@ -65,3 +65,18 @@ Revision history: Arduino_LoRaWAN::Arduino_LoRaWAN() { } + +// this is the former body of setFrequency(), with &freq changed to pFreq +/* static */ +bool Arduino_LoRaWAN::setFrequencyRaw(uint8_t *pFreq, unsigned iCh, uint32_t frequency) + { + const uint32_t reducedFreq = frequency / 100; + if (reducedFreq > 0xFFFFFFu) + return false; + + auto const chPtr = pFreq + iCh * 3; + chPtr[0] = uint8_t(reducedFreq >> 16); + chPtr[1] = uint8_t(reducedFreq >> 8); + chPtr[2] = uint8_t(reducedFreq); + } +