diff --git a/include/mqttclient.h b/include/mqttclient.h index 9b7ac649..804ee0f3 100644 --- a/include/mqttclient.h +++ b/include/mqttclient.h @@ -25,6 +25,7 @@ extern TaskHandle_t mqttTask; void mqtt_enqueuedata(MessageBuffer_t *message); +uint32_t mqtt_queuewaiting(void); void mqtt_queuereset(void); void setMqttIRQ(void); void mqtt_loop(void); diff --git a/include/spislave.h b/include/spislave.h index 6db35d00..e4b71f02 100644 --- a/include/spislave.h +++ b/include/spislave.h @@ -32,6 +32,7 @@ esp_err_t spi_init(); extern TaskHandle_t spiTask; void spi_enqueuedata(MessageBuffer_t *message); -void spi_queuereset(); +uint32_t spi_queuewaiting(void); +void spi_queuereset(void); #endif // _SPISLAVE_H diff --git a/src/irqhandler.cpp b/src/irqhandler.cpp index e8316119..687a6965 100644 --- a/src/irqhandler.cpp +++ b/src/irqhandler.cpp @@ -98,7 +98,7 @@ void irqHandler(void *pvParameters) { // goto sleep if we have a sleep cycle if (cfg.sleepcycle) #ifdef HAS_BUTTON - enter_deepsleep(cfg.sleepcycle * 2, HAS_BUTTON); + enter_deepsleep(cfg.sleepcycle * 2, (gpio_num_t)HAS_BUTTON); #else enter_deepsleep(cfg.sleepcycle * 2); #endif diff --git a/src/reset.cpp b/src/reset.cpp index 109c201f..ff295baf 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -10,7 +10,8 @@ static const char TAG[] = __FILE__; // variables keep its values after a wakeup from sleep RTC_DATA_ATTR runmode_t RTC_runmode = RUNMODE_POWERCYCLE; -static RTC_DATA_ATTR struct timeval RTC_sleep_start_time; +RTC_DATA_ATTR struct timeval RTC_sleep_start_time; +timeval sleep_stop_time; const char *runmode[4] = {"powercycle", "normal", "wakeup", "update"}; @@ -69,36 +70,28 @@ void do_after_reset(void) { } void enter_deepsleep(const uint64_t wakeup_sec = 60, - const gpio_num_t wakeup_gpio = GPIO_NUM_MAX) { + gpio_num_t wakeup_gpio = GPIO_NUM_MAX) { + + int i; + + // validate wake up pin, if we have + if (!GPIO_IS_VALID_GPIO(wakeup_gpio)) + wakeup_gpio = GPIO_NUM_MAX; // ensure we are in normal runmode, not udpate or wakeup if ((RTC_runmode != RUNMODE_NORMAL) #if (HAS_LORA) || (LMIC.opmode & (OP_JOINING | OP_REJOIN)) #endif - ) { - ESP_LOGE(TAG, "Can't go to sleep now"); + ) return; - } else { - ESP_LOGI(TAG, "Attempting to sleep..."); - } - // wait until all send queues are empty - ESP_LOGI(TAG, "Waiting until send queues are empty..."); - while (!allQueuesEmtpy()) - vTaskDelay(pdMS_TO_TICKS(100)); + ESP_LOGI(TAG, "Preparing to sleep..."); -#if (HAS_LORA) - // shutdown LMIC safely - ESP_LOGI(TAG, "Waiting until LMIC is idle..."); - while ((LMIC.opmode & OP_TXRXPEND) || - os_queryTimeCriticalJobs(sec2osticks(wakeup_sec))) - vTaskDelay(pdMS_TO_TICKS(100)); + // stop further enqueuing of senddata + sendTimer.detach(); - SaveLMICToRTC(wakeup_sec); -#endif // (HAS_LORA) - -// switch off radio + // switch off radio #if (BLECOUNTER) stop_BLEscan(); btStop(); @@ -107,6 +100,43 @@ void enter_deepsleep(const uint64_t wakeup_sec = 60, switch_wifi_sniffer(0); #endif + // wait a while (max 100 sec) to clear send queues + ESP_LOGI(TAG, "Waiting until send queues are empty..."); + for (i = 10; i > 0; i--) { + if (!allQueuesEmtpy()) + vTaskDelay(pdMS_TO_TICKS(10000)); + else + break; + } + if (i == 0) + goto Error; + + // shutdown LMIC safely, waiting max 100 sec +#if (HAS_LORA) + ESP_LOGI(TAG, "Waiting until LMIC is idle..."); + for (i = 10; i > 0; i--) { + if ((LMIC.opmode & OP_TXRXPEND) || + os_queryTimeCriticalJobs(sec2osticks(wakeup_sec))) + vTaskDelay(pdMS_TO_TICKS(10000)); + else + break; + } + if (i == 0) + goto Error; + + SaveLMICToRTC(wakeup_sec); +#endif // (HAS_LORA) + +// shutdown MQTT safely +#ifdef HAS_MQTT +// to come +#endif + +// shutdown SPI safely +#ifdef HAS_SPI +// to come +#endif + // halt interrupts accessing i2c bus mask_user_IRQ(); @@ -137,8 +167,12 @@ void enter_deepsleep(const uint64_t wakeup_sec = 60, esp_sleep_enable_ext1_wakeup(1ULL << wakeup_gpio, ESP_EXT1_WAKEUP_ALL_LOW); } - // save sleep start time. Deep sleep. + // time stamp sleep start time. Deep sleep. gettimeofday(&RTC_sleep_start_time, NULL); ESP_LOGI(TAG, "Going to sleep, good bye."); esp_deep_sleep_start(); + +Error: + ESP_LOGE(TAG, "Can't go to sleep. Resetting."); + do_reset(true); } \ No newline at end of file