deep sleep further development

This commit is contained in:
Klaus K Wilting 2020-12-10 15:10:28 +01:00
parent 61615d37e3
commit 258b6fce53
4 changed files with 60 additions and 24 deletions

View File

@ -25,6 +25,7 @@
extern TaskHandle_t mqttTask; extern TaskHandle_t mqttTask;
void mqtt_enqueuedata(MessageBuffer_t *message); void mqtt_enqueuedata(MessageBuffer_t *message);
uint32_t mqtt_queuewaiting(void);
void mqtt_queuereset(void); void mqtt_queuereset(void);
void setMqttIRQ(void); void setMqttIRQ(void);
void mqtt_loop(void); void mqtt_loop(void);

View File

@ -32,6 +32,7 @@ esp_err_t spi_init();
extern TaskHandle_t spiTask; extern TaskHandle_t spiTask;
void spi_enqueuedata(MessageBuffer_t *message); void spi_enqueuedata(MessageBuffer_t *message);
void spi_queuereset(); uint32_t spi_queuewaiting(void);
void spi_queuereset(void);
#endif // _SPISLAVE_H #endif // _SPISLAVE_H

View File

@ -98,7 +98,7 @@ void irqHandler(void *pvParameters) {
// goto sleep if we have a sleep cycle // goto sleep if we have a sleep cycle
if (cfg.sleepcycle) if (cfg.sleepcycle)
#ifdef HAS_BUTTON #ifdef HAS_BUTTON
enter_deepsleep(cfg.sleepcycle * 2, HAS_BUTTON); enter_deepsleep(cfg.sleepcycle * 2, (gpio_num_t)HAS_BUTTON);
#else #else
enter_deepsleep(cfg.sleepcycle * 2); enter_deepsleep(cfg.sleepcycle * 2);
#endif #endif

View File

@ -10,7 +10,8 @@ static const char TAG[] = __FILE__;
// variables keep its values after a wakeup from sleep // variables keep its values after a wakeup from sleep
RTC_DATA_ATTR runmode_t RTC_runmode = RUNMODE_POWERCYCLE; 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"}; 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, 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 // ensure we are in normal runmode, not udpate or wakeup
if ((RTC_runmode != RUNMODE_NORMAL) if ((RTC_runmode != RUNMODE_NORMAL)
#if (HAS_LORA) #if (HAS_LORA)
|| (LMIC.opmode & (OP_JOINING | OP_REJOIN)) || (LMIC.opmode & (OP_JOINING | OP_REJOIN))
#endif #endif
) { )
ESP_LOGE(TAG, "Can't go to sleep now");
return; return;
} else {
ESP_LOGI(TAG, "Attempting to sleep...");
}
// wait until all send queues are empty ESP_LOGI(TAG, "Preparing to sleep...");
ESP_LOGI(TAG, "Waiting until send queues are empty...");
while (!allQueuesEmtpy())
vTaskDelay(pdMS_TO_TICKS(100));
#if (HAS_LORA) // stop further enqueuing of senddata
// shutdown LMIC safely sendTimer.detach();
ESP_LOGI(TAG, "Waiting until LMIC is idle...");
while ((LMIC.opmode & OP_TXRXPEND) ||
os_queryTimeCriticalJobs(sec2osticks(wakeup_sec)))
vTaskDelay(pdMS_TO_TICKS(100));
SaveLMICToRTC(wakeup_sec); // switch off radio
#endif // (HAS_LORA)
// switch off radio
#if (BLECOUNTER) #if (BLECOUNTER)
stop_BLEscan(); stop_BLEscan();
btStop(); btStop();
@ -107,6 +100,43 @@ void enter_deepsleep(const uint64_t wakeup_sec = 60,
switch_wifi_sniffer(0); switch_wifi_sniffer(0);
#endif #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 // halt interrupts accessing i2c bus
mask_user_IRQ(); 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); 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); gettimeofday(&RTC_sleep_start_time, NULL);
ESP_LOGI(TAG, "Going to sleep, good bye."); ESP_LOGI(TAG, "Going to sleep, good bye.");
esp_deep_sleep_start(); esp_deep_sleep_start();
Error:
ESP_LOGE(TAG, "Can't go to sleep. Resetting.");
do_reset(true);
} }