diff --git a/include/lorawan.h b/include/lorawan.h index e98da2bf..f5790623 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -24,7 +24,6 @@ extern TaskHandle_t lmicTask, lorasendTask; extern RTC_NOINIT_ATTR u4_t RTCnetid, RTCdevaddr; extern RTC_NOINIT_ATTR u1_t RTCnwkKey[16], RTCartKey[16]; -extern RTC_NOINIT_ATTR int RTCseqnoUp, RTCseqnoDn; // table of LORAWAN MAC commands typedef struct { diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 281a66ad..6bbe0b3e 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -91,7 +91,7 @@ void lora_setupForNetwork(bool preJoin) { #elif CFG_LMIC_EU_like // setting for TheThingsNetwork // TTN uses SF9, not SF12, for RX2 window - // LMIC.dn2Dr = EU868_DR_SF9; + LMIC.dn2Dr = EU868_DR_SF9; // Enable link check validation LMIC_setLinkCheckMode(true); #endif @@ -478,6 +478,12 @@ static void myEventCallback(void *pUserData, ev_t ev) { lora_setupForNetwork(false); break; + case EV_TXCOMPLETE: + // save current Fcnt to RTC RAM + RTCseqnoUp = LMIC.seqnoUp; + RTCseqnoDn = LMIC.seqnoDn; + break; + case EV_JOIN_TXCOMPLETE: // replace descriptor from library with more descriptive term snprintf(lmic_event_msg, LMIC_EVENTMSG_LEN, "%-16s", "JOIN_WAIT"); diff --git a/src/reset.cpp b/src/reset.cpp index 0d30f05b..464a514b 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -13,8 +13,6 @@ void do_reset(bool warmstart) { // store LMIC keys and counters in RTC memory #if (HAS_LORA) LMIC_getSessionKeys(&RTCnetid, &RTCdevaddr, RTCnwkKey, RTCartKey); - RTCseqnoUp = LMIC.seqnoUp; - RTCseqnoDn = LMIC.seqnoDn; #endif ESP_LOGI(TAG, "restarting device (warmstart), keeping runmode %d", RTC_runmode); @@ -67,6 +65,12 @@ void enter_deepsleep(const int wakeup_sec, const gpio_num_t wakeup_gpio) { if ((!wakeup_sec) && (!wakeup_gpio) && (RTC_runmode == RUNMODE_NORMAL)) return; +// assure LMIC is in safe state +#if (HAS_LORA) + if (os_queryTimeCriticalJobs(ms2osticks(10000))) + return; +#endif + // set up power domains esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON); @@ -83,8 +87,6 @@ void enter_deepsleep(const int wakeup_sec, const gpio_num_t wakeup_gpio) { // store LMIC keys and counters in RTC memory #if (HAS_LORA) LMIC_getSessionKeys(&RTCnetid, &RTCdevaddr, RTCnwkKey, RTCartKey); - RTCseqnoUp = LMIC.seqnoUp; - RTCseqnoDn = LMIC.seqnoDn; #endif // halt interrupts accessing i2c bus