Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LoRaWAN] Change and upgrade persistence handling #1017

Merged
merged 6 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ setModem KEYWORD2

# LoRaWAN
wipe KEYWORD2
getBufferNonces KEYWORD2
setBufferNonces KEYWORD2
getBufferSession KEYWORD2
setBufferSession KEYWORD2
restore KEYWORD2
beginOTAA KEYWORD2
beginABP KEYWORD2
Expand Down
47 changes: 0 additions & 47 deletions src/ArduinoHal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

#if defined(RADIOLIB_BUILD_ARDUINO)

#if !defined(RADIOLIB_EEPROM_UNSUPPORTED)
#include <EEPROM.h>
#endif

ArduinoHal::ArduinoHal(): RadioLibHal(INPUT, OUTPUT, LOW, HIGH, RISING, FALLING), spi(&RADIOLIB_DEFAULT_SPI), initInterface(true) {}

ArduinoHal::ArduinoHal(SPIClass& spi, SPISettings spiSettings): RadioLibHal(INPUT, OUTPUT, LOW, HIGH, RISING, FALLING), spi(&spi), spiSettings(spiSettings) {}
Expand Down Expand Up @@ -118,49 +114,6 @@ void inline ArduinoHal::spiEnd() {
spi->end();
}

void ArduinoHal::readPersistentStorage(uint32_t addr, uint8_t* buff, size_t len) {
#if !defined(RADIOLIB_EEPROM_UNSUPPORTED)
#if defined(RADIOLIB_ESP32) || defined(ARDUINO_ARCH_RP2040)
EEPROM.begin(RADIOLIB_HAL_PERSISTENT_STORAGE_SIZE);
#elif defined(ARDUINO_ARCH_APOLLO3)
EEPROM.init();
#endif
for(size_t i = 0; i < len; i++) {
buff[i] = EEPROM.read(addr + i);
}
#if defined(RADIOLIB_ESP32) || defined(ARDUINO_ARCH_RP2040)
EEPROM.end();
#endif
#else
(void)addr;
(void)buff;
(void)len;
#endif
}

void ArduinoHal::writePersistentStorage(uint32_t addr, uint8_t* buff, size_t len) {
#if !defined(RADIOLIB_EEPROM_UNSUPPORTED)
#if defined(RADIOLIB_ESP32) || defined(ARDUINO_ARCH_RP2040)
EEPROM.begin(RADIOLIB_HAL_PERSISTENT_STORAGE_SIZE);
#elif defined(ARDUINO_ARCH_APOLLO3)
EEPROM.init();
#endif
for(size_t i = 0; i < len; i++) {
if(EEPROM.read(addr + i) != buff[i]) { // only write if value is new
EEPROM.write(addr + i, buff[i]);
}
}
#if defined(RADIOLIB_ESP32) || defined(ARDUINO_ARCH_RP2040)
EEPROM.commit();
EEPROM.end();
#endif
#else
(void)addr;
(void)buff;
(void)len;
#endif
}

void inline ArduinoHal::tone(uint32_t pin, unsigned int frequency, unsigned long duration) {
#if !defined(RADIOLIB_TONE_UNSUPPORTED)
if(pin == RADIOLIB_NC) {
Expand Down
3 changes: 0 additions & 3 deletions src/ArduinoHal.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ class ArduinoHal : public RadioLibHal {
void spiEndTransaction() override;
void spiEnd() override;

void readPersistentStorage(uint32_t addr, uint8_t* buff, size_t len) override;
void writePersistentStorage(uint32_t addr, uint8_t* buff, size_t len) override;

// implementations of virtual RadioLibHal methods
void init() override;
void term() override;
Expand Down
103 changes: 15 additions & 88 deletions src/BuildOpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@
* Debug output enable.
* Warning: Debug output will slow down the whole system significantly.
* Also, it will result in larger compiled binary.
* Levels: basic - only main info
* protocol - mainly LoRaWAN stuff, but other protocols as well
* SPI - full transcript of all SPI communication
* Levels: debug - only main info
* verbose - full transcript of all SPI communication
*/
#if !defined(RADIOLIB_DEBUG_BASIC)
#define RADIOLIB_DEBUG_BASIC (0)
#if !defined(RADIOLIB_DEBUG)
#define RADIOLIB_DEBUG (0)
#endif
#if !defined(RADIOLIB_DEBUG_PROTOCOL)
#define RADIOLIB_DEBUG_PROTOCOL (0)
#endif
#if !defined(RADIOLIB_DEBUG_SPI)
#define RADIOLIB_DEBUG_SPI (0)
#if !defined(RADIOLIB_VERBOSE)
#define RADIOLIB_VERBOSE (0)
#endif

// set which output port should be used for debug output
Expand Down Expand Up @@ -104,21 +100,6 @@
#define RADIOLIB_STATIC_ARRAY_SIZE (256)
#endif

// the base address for persistent storage
// some protocols (e.g. LoRaWAN) require a method
// to store some data persistently
// on Arduino, this will use EEPROM, on non-Arduino platform,
// it will use anything provided by the hardware abstraction layer
// RadioLib will place these starting at this address
#if !defined(RADIOLIB_HAL_PERSISTENT_STORAGE_BASE)
#define RADIOLIB_HAL_PERSISTENT_STORAGE_BASE (0)
#endif

// the amount of space allocated to the persistent storage
#if !defined(RADIOLIB_HAL_PERSISTENT_STORAGE_SIZE)
#define RADIOLIB_HAL_PERSISTENT_STORAGE_SIZE (0x01C0)
#endif

/*
* Uncomment on boards whose clock runs too slow or too fast
* Set the value according to the following scheme:
Expand Down Expand Up @@ -238,26 +219,22 @@
#elif defined(SAMD_SERIES)
// Adafruit SAMD boards (M0 and M4)
#define RADIOLIB_PLATFORM "Adafruit SAMD"
#define RADIOLIB_EEPROM_UNSUPPORTED

#elif defined(ARDUINO_ARCH_SAMD)
// Arduino SAMD (Zero, MKR, etc.)
#define RADIOLIB_PLATFORM "Arduino SAMD"
#define RADIOLIB_ARDUINOHAL_PIN_MODE_CAST (PinMode)
#define RADIOLIB_ARDUINOHAL_PIN_STATUS_CAST (PinStatus)
#define RADIOLIB_ARDUINOHAL_INTERRUPT_MODE_CAST (PinStatus)
#define RADIOLIB_EEPROM_UNSUPPORTED

#elif defined(__SAM3X8E__)
// Arduino Due
#define RADIOLIB_PLATFORM "Arduino Due"
#define RADIOLIB_TONE_UNSUPPORTED
#define RADIOLIB_EEPROM_UNSUPPORTED

#elif (defined(NRF52832_XXAA) || defined(NRF52840_XXAA)) && !defined(ARDUINO_ARDUINO_NANO33BLE)
// Adafruit nRF52 boards
#define RADIOLIB_PLATFORM "Adafruit nRF52"
#define RADIOLIB_EEPROM_UNSUPPORTED

#elif defined(ARDUINO_ARC32_TOOLS)
// Intel Curie
Expand All @@ -280,7 +257,6 @@
#define RADIOLIB_ARDUINOHAL_PIN_MODE_CAST (PinMode)
#define RADIOLIB_ARDUINOHAL_PIN_STATUS_CAST (PinStatus)
#define RADIOLIB_ARDUINOHAL_INTERRUPT_MODE_CAST (PinStatus)
#define RADIOLIB_EEPROM_UNSUPPORTED

// Arduino mbed OS boards have a really bad tone implementation which will crash after a couple seconds
#define RADIOLIB_TONE_UNSUPPORTED
Expand All @@ -292,7 +268,6 @@
#define RADIOLIB_ARDUINOHAL_PIN_MODE_CAST (PinMode)
#define RADIOLIB_ARDUINOHAL_PIN_STATUS_CAST (PinStatus)
#define RADIOLIB_ARDUINOHAL_INTERRUPT_MODE_CAST (PinStatus)
#define RADIOLIB_EEPROM_UNSUPPORTED

// Arduino mbed OS boards have a really bad tone implementation which will crash after a couple seconds
#define RADIOLIB_TONE_UNSUPPORTED
Expand All @@ -314,7 +289,6 @@
#define RADIOLIB_ARDUINOHAL_PIN_MODE_CAST (PinMode)
#define RADIOLIB_ARDUINOHAL_PIN_STATUS_CAST (PinStatus)
#define RADIOLIB_ARDUINOHAL_INTERRUPT_MODE_CAST (PinStatus)
#define RADIOLIB_EEPROM_UNSUPPORTED

// Arduino mbed OS boards have a really bad tone implementation which will crash after a couple seconds
#define RADIOLIB_TONE_UNSUPPORTED
Expand Down Expand Up @@ -473,85 +447,38 @@
#define RADIOLIB_EXCLUDE_STM32WLX (1)
#endif

// set the global debug mode flag
#if RADIOLIB_DEBUG_BASIC || RADIOLIB_DEBUG_PROTOCOL || RADIOLIB_DEBUG_SPI
#define RADIOLIB_DEBUG (1)
#else
#define RADIOLIB_DEBUG (0)
#endif

#if RADIOLIB_DEBUG
#if defined(RADIOLIB_BUILD_ARDUINO)
#define RADIOLIB_DEBUG_PRINT(...) Module::serialPrintf(__VA_ARGS__)
#define RADIOLIB_DEBUG_PRINTLN(M, ...) Module::serialPrintf(M "\n", ##__VA_ARGS__)
#define RADIOLIB_DEBUG_PRINT_LVL(LEVEL, M, ...) Module::serialPrintf(LEVEL "" M, ##__VA_ARGS__)
#define RADIOLIB_DEBUG_PRINTLN_LVL(LEVEL, M, ...) Module::serialPrintf(LEVEL "" M "\n", ##__VA_ARGS__)

// some platforms do not support printf("%f"), so it has to be done this way
#define RADIOLIB_DEBUG_PRINT_FLOAT(LEVEL, VAL, DECIMALS) RADIOLIB_DEBUG_PRINT(LEVEL); RADIOLIB_DEBUG_PORT.print(VAL, DECIMALS)
#define RADIOLIB_DEBUG_PRINT_FLOAT(VAL, DECIMALS) RADIOLIB_DEBUG_PORT.print(VAL, DECIMALS)
#else
#if !defined(RADIOLIB_DEBUG_PRINT)
#define RADIOLIB_DEBUG_PRINT(...) fprintf(RADIOLIB_DEBUG_PORT, __VA_ARGS__)
#define RADIOLIB_DEBUG_PRINT_LVL(LEVEL, M, ...) fprintf(RADIOLIB_DEBUG_PORT, LEVEL "" M, ##__VA_ARGS__)
#endif
#if !defined(RADIOLIB_DEBUG_PRINTLN)
#define RADIOLIB_DEBUG_PRINTLN(M, ...) fprintf(RADIOLIB_DEBUG_PORT, M "\n", ##__VA_ARGS__)
#define RADIOLIB_DEBUG_PRINTLN_LVL(LEVEL, M, ...) fprintf(RADIOLIB_DEBUG_PORT, LEVEL "" M "\n", ##__VA_ARGS__)
#endif
#define RADIOLIB_DEBUG_PRINT_FLOAT(LEVEL, VAL, DECIMALS) RADIOLIB_DEBUG_PRINT(LEVEL "%.3f", VAL)
#define RADIOLIB_DEBUG_PRINT_FLOAT(VAL, DECIMALS) RADIOLIB_DEBUG_PRINT("%.3f", VAL)
#endif

#define RADIOLIB_DEBUG_HEXDUMP(LEVEL, ...) RADIOLIB_DEBUG_PRINT(LEVEL); Module::hexdump(__VA_ARGS__)
#define RADIOLIB_DEBUG_HEXDUMP(...) Module::hexdump(__VA_ARGS__)
#else
#define RADIOLIB_DEBUG_PRINT(...) {}
#define RADIOLIB_DEBUG_PRINTLN(...) {}
#define RADIOLIB_DEBUG_PRINT_FLOAT(VAL, DECIMALS) {}
#define RADIOLIB_DEBUG_HEXDUMP(...) {}
#endif

#if RADIOLIB_DEBUG_BASIC
#define RADIOLIB_DEBUG_BASIC_PRINT(...) RADIOLIB_DEBUG_PRINT_LVL("RLB_DBG: ", __VA_ARGS__)
#define RADIOLIB_DEBUG_BASIC_PRINT_NOTAG(...) RADIOLIB_DEBUG_PRINT_LVL("", __VA_ARGS__)
#define RADIOLIB_DEBUG_BASIC_PRINTLN(...) RADIOLIB_DEBUG_PRINTLN_LVL("RLB_DBG: ", __VA_ARGS__)
#define RADIOLIB_DEBUG_BASIC_PRINT_FLOAT(...) RADIOLIB_DEBUG_PRINT_FLOAT("RLB_DBG: ", __VA_ARGS__);
#define RADIOLIB_DEBUG_BASIC_HEXDUMP(...) RADIOLIB_DEBUG_HEXDUMP("RLB_DBG: ", __VA_ARGS__);
#else
#define RADIOLIB_DEBUG_BASIC_PRINT(...) {}
#define RADIOLIB_DEBUG_BASIC_PRINT_NOTAG(...) {}
#define RADIOLIB_DEBUG_BASIC_PRINTLN(...) {}
#define RADIOLIB_DEBUG_BASIC_PRINT_FLOAT(...) {}
#define RADIOLIB_DEBUG_BASIC_HEXDUMP(...) {}
#endif

#if RADIOLIB_DEBUG_PROTOCOL
#define RADIOLIB_DEBUG_PROTOCOL_PRINT(...) RADIOLIB_DEBUG_PRINT_LVL("RLB_PRO: ", __VA_ARGS__)
#define RADIOLIB_DEBUG_PROTOCOL_PRINTLN(...) RADIOLIB_DEBUG_PRINTLN_LVL("RLB_PRO: ", __VA_ARGS__)
#define RADIOLIB_DEBUG_PROTOCOL_PRINT_FLOAT(...) RADIOLIB_DEBUG_PRINT_FLOAT("RLB_PRO: ", __VA_ARGS__);
#define RADIOLIB_DEBUG_PROTOCOL_HEXDUMP(...) RADIOLIB_DEBUG_HEXDUMP("RLB_PRO: ", __VA_ARGS__);
#if RADIOLIB_VERBOSE
#define RADIOLIB_VERBOSE_PRINT(...) RADIOLIB_DEBUG_PRINT(__VA_ARGS__)
#define RADIOLIB_VERBOSE_PRINTLN(...) RADIOLIB_DEBUG_PRINTLN(__VA_ARGS__)
#else
#define RADIOLIB_DEBUG_PROTOCOL_PRINT(...) {}
#define RADIOLIB_DEBUG_PROTOCOL_PRINTLN(...) {}
#define RADIOLIB_DEBUG_PROTOCOL_PRINT_FLOAT(...) {}
#define RADIOLIB_DEBUG_PROTOCOL_HEXDUMP(...) {}
#define RADIOLIB_VERBOSE_PRINT(...) {}
#define RADIOLIB_VERBOSE_PRINTLN(...) {}
#endif

#if RADIOLIB_DEBUG_SPI
#define RADIOLIB_DEBUG_SPI_PRINT(...) RADIOLIB_DEBUG_PRINT_LVL("RLB_SPI: ", __VA_ARGS__)
#define RADIOLIB_DEBUG_SPI_PRINT_NOTAG(...) RADIOLIB_DEBUG_PRINT_LVL("", __VA_ARGS__)
#define RADIOLIB_DEBUG_SPI_PRINTLN(...) RADIOLIB_DEBUG_PRINTLN_LVL("RLB_SPI: ", __VA_ARGS__)
#define RADIOLIB_DEBUG_SPI_PRINTLN_NOTAG(...) RADIOLIB_DEBUG_PRINTLN_LVL("", __VA_ARGS__)
#define RADIOLIB_DEBUG_SPI_PRINT_FLOAT(...) RADIOLIB_DEBUG_PRINT_FLOAT("RLB_SPI: ", __VA_ARGS__);
#define RADIOLIB_DEBUG_SPI_HEXDUMP(...) RADIOLIB_DEBUG_HEXDUMP("RLB_SPI: ", __VA_ARGS__);
#else
#define RADIOLIB_DEBUG_SPI_PRINT(...) {}
#define RADIOLIB_DEBUG_SPI_PRINT_NOTAG(...) {}
#define RADIOLIB_DEBUG_SPI_PRINTLN(...) {}
#define RADIOLIB_DEBUG_SPI_PRINTLN_NOTAG(...) {}
#define RADIOLIB_DEBUG_SPI_PRINT_FLOAT(...) {}
#define RADIOLIB_DEBUG_SPI_HEXDUMP(...) {}
#endif


/*!
\brief A simple assert macro, will return on error.
*/
Expand Down Expand Up @@ -581,7 +508,7 @@
#define RADIOLIB_VERSION_MAJOR 6
#define RADIOLIB_VERSION_MINOR 4
#define RADIOLIB_VERSION_PATCH 2
#define RADIOLIB_VERSION_EXTRA 0
#define RADIOLIB_VERSION_EXTRA 5

#define RADIOLIB_VERSION (((RADIOLIB_VERSION_MAJOR) << 24) | ((RADIOLIB_VERSION_MINOR) << 16) | ((RADIOLIB_VERSION_PATCH) << 8) | (RADIOLIB_VERSION_EXTRA))

Expand Down
47 changes: 0 additions & 47 deletions src/Hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,50 +33,3 @@ void RadioLibHal::yield() {
uint32_t RadioLibHal::pinToInterrupt(uint32_t pin) {
return(pin);
}

void RadioLibHal::readPersistentStorage(uint32_t addr, uint8_t* buff, size_t len) {
// these are only needed for some protocols, so it's not needed to have them by default
(void)addr;
(void)buff;
(void)len;
}

void RadioLibHal::writePersistentStorage(uint32_t addr, uint8_t* buff, size_t len) {
// these are only needed for some protocols, so it's not needed to have them by default
(void)addr;
(void)buff;
(void)len;
}

void RadioLibHal::wipePersistentStorage() {
uint8_t dummy = 0;
for(size_t i = 0; i < RADIOLIB_HAL_PERSISTENT_STORAGE_SIZE; i++) {
this->writePersistentStorage(RADIOLIB_HAL_PERSISTENT_STORAGE_BASE + i, &dummy, sizeof(uint8_t));
}
}

uint32_t RadioLibHal::getPersistentAddr(uint32_t id) {
return(RadioLibPersistentParamTable[id]);
}

template<typename T>
void RadioLibHal::setPersistentParameter(uint32_t id, T val, uint32_t offset) {
uint8_t *ptr = (uint8_t*)&val;
this->writePersistentStorage(RADIOLIB_HAL_PERSISTENT_STORAGE_BASE + RadioLibPersistentParamTable[id] + offset, ptr, sizeof(T));
}

template void RadioLibHal::setPersistentParameter(uint32_t id, uint8_t val, uint32_t offset);
template void RadioLibHal::setPersistentParameter(uint32_t id, uint16_t val, uint32_t offset);
template void RadioLibHal::setPersistentParameter(uint32_t id, uint32_t val, uint32_t offset);

template<typename T>
T RadioLibHal::getPersistentParameter(uint32_t id) {
T val = 0;
uint8_t *ptr = (uint8_t*)&val;
this->readPersistentStorage(RADIOLIB_HAL_PERSISTENT_STORAGE_BASE + RadioLibPersistentParamTable[id], ptr, sizeof(T));
return(val);
}

template uint8_t RadioLibHal::getPersistentParameter(uint32_t id);
template uint16_t RadioLibHal::getPersistentParameter(uint32_t id);
template uint32_t RadioLibHal::getPersistentParameter(uint32_t id);
Loading
Loading