2019-10-20 20:05:13 +02:00
|
|
|
// Basic Config
|
|
|
|
#include "globals.h"
|
|
|
|
#include "reset.h"
|
|
|
|
|
|
|
|
// Local logging tag
|
|
|
|
static const char TAG[] = __FILE__;
|
|
|
|
|
2020-12-09 10:15:12 +01:00
|
|
|
// Conversion factor for micro seconds to seconds
|
|
|
|
#define uS_TO_S_FACTOR 1000000ULL
|
|
|
|
|
2020-12-09 20:37:03 +01:00
|
|
|
// variables keep its values after a wakeup from sleep
|
2020-12-26 18:29:10 +01:00
|
|
|
RTC_NOINIT_ATTR runmode_t RTC_runmode = RUNMODE_POWERCYCLE;
|
2020-12-10 15:10:28 +01:00
|
|
|
RTC_DATA_ATTR struct timeval RTC_sleep_start_time;
|
2020-12-21 22:11:41 +01:00
|
|
|
RTC_DATA_ATTR unsigned long long RTC_millis = 0;
|
2020-12-10 15:10:28 +01:00
|
|
|
timeval sleep_stop_time;
|
2019-10-20 20:05:13 +02:00
|
|
|
|
2020-12-11 16:34:17 +01:00
|
|
|
const char *runmode[5] = {"powercycle", "normal", "wakeup", "update", "sleep"};
|
2020-12-09 10:15:12 +01:00
|
|
|
|
2019-10-20 20:05:13 +02:00
|
|
|
void do_reset(bool warmstart) {
|
|
|
|
if (warmstart) {
|
2020-12-11 16:34:17 +01:00
|
|
|
ESP_LOGI(TAG, "restarting device (warmstart)");
|
2019-10-20 20:05:13 +02:00
|
|
|
} else {
|
|
|
|
#if (HAS_LORA)
|
2020-12-09 10:15:12 +01:00
|
|
|
if (RTC_runmode == RUNMODE_NORMAL) {
|
2019-10-20 20:05:13 +02:00
|
|
|
LMIC_shutdown();
|
2020-12-09 10:15:12 +01:00
|
|
|
}
|
2019-10-20 20:05:13 +02:00
|
|
|
#endif
|
|
|
|
RTC_runmode = RUNMODE_POWERCYCLE;
|
2020-12-11 16:34:17 +01:00
|
|
|
ESP_LOGI(TAG, "restarting device (coldstart)");
|
2019-10-20 20:05:13 +02:00
|
|
|
}
|
|
|
|
esp_restart();
|
|
|
|
}
|
|
|
|
|
2020-12-09 20:37:03 +01:00
|
|
|
void do_after_reset(void) {
|
2019-10-20 20:05:13 +02:00
|
|
|
|
2020-12-09 20:37:03 +01:00
|
|
|
struct timeval sleep_stop_time;
|
|
|
|
uint64_t sleep_time_ms;
|
2019-10-20 20:05:13 +02:00
|
|
|
|
2020-12-26 18:29:10 +01:00
|
|
|
switch (rtc_get_reset_reason(0)) {
|
2019-10-20 20:05:13 +02:00
|
|
|
|
2020-12-26 18:29:10 +01:00
|
|
|
case POWERON_RESET: // 0x01 Vbat power on reset
|
|
|
|
case RTCWDT_BROWN_OUT_RESET: // 0x0f Reset when the vdd voltage is not
|
|
|
|
// stable
|
|
|
|
RTC_runmode = RUNMODE_POWERCYCLE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SW_CPU_RESET: // 0x0c Software reset CPU
|
|
|
|
// keep previous runmode (could be RUNMODE_UPDATE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DEEPSLEEP_RESET: // 0x05 Deep Sleep reset digital core
|
2020-12-09 20:37:03 +01:00
|
|
|
// calculate time spent in deep sleep
|
|
|
|
gettimeofday(&sleep_stop_time, NULL);
|
|
|
|
sleep_time_ms =
|
|
|
|
(sleep_stop_time.tv_sec - RTC_sleep_start_time.tv_sec) * 1000 +
|
|
|
|
(sleep_stop_time.tv_usec - RTC_sleep_start_time.tv_usec) / 1000;
|
|
|
|
ESP_LOGI(TAG, "Time spent in deep sleep: %d ms", sleep_time_ms);
|
2020-12-19 12:27:07 +01:00
|
|
|
RTC_millis += sleep_time_ms; // increment system monotonic time
|
2020-12-26 20:45:10 +01:00
|
|
|
// set wakeup state, not if we have pending OTA update
|
|
|
|
if (RTC_runmode == RUNMODE_SLEEP)
|
|
|
|
RTC_runmode = RUNMODE_WAKEUP;
|
2019-10-20 20:05:13 +02:00
|
|
|
break;
|
|
|
|
|
2020-12-26 18:29:10 +01:00
|
|
|
case SW_RESET: // 0x03 Software reset digital core
|
|
|
|
case OWDT_RESET: // 0x04 Legacy watch dog reset digital core
|
|
|
|
case SDIO_RESET: // 0x06 Reset by SLC module, reset digital core
|
|
|
|
case TG0WDT_SYS_RESET: // 0x07 Timer Group0 Watch dog reset digital core
|
|
|
|
case TG1WDT_SYS_RESET: // 0x08 Timer Group1 Watch dog reset digital core
|
|
|
|
case RTCWDT_SYS_RESET: // 0x09 RTC Watch dog Reset digital core
|
|
|
|
case INTRUSION_RESET: // 0x0a Instrusion tested to reset CPU
|
|
|
|
case TGWDT_CPU_RESET: // 0x0b Time Group reset CPU
|
|
|
|
case RTCWDT_CPU_RESET: // 0x0d RTC Watch dog Reset CPU
|
|
|
|
case EXT_CPU_RESET: // 0x0e for APP CPU, reseted by PRO CPU
|
|
|
|
case RTCWDT_RTC_RESET: // 0x10 RTC Watch dog reset digital core and rtc mode
|
2019-10-20 20:05:13 +02:00
|
|
|
default:
|
2019-10-27 16:13:33 +01:00
|
|
|
RTC_runmode = RUNMODE_POWERCYCLE;
|
2019-10-20 20:05:13 +02:00
|
|
|
break;
|
2020-12-26 18:29:10 +01:00
|
|
|
}
|
2019-10-20 20:05:13 +02:00
|
|
|
|
2020-12-09 10:15:12 +01:00
|
|
|
ESP_LOGI(TAG, "Starting Software v%s, runmode %s", PROGVERSION,
|
|
|
|
runmode[RTC_runmode]);
|
2019-10-20 20:05:13 +02:00
|
|
|
}
|
|
|
|
|
2020-12-09 20:37:03 +01:00
|
|
|
void enter_deepsleep(const uint64_t wakeup_sec = 60,
|
2020-12-10 15:10:28 +01:00
|
|
|
gpio_num_t wakeup_gpio = GPIO_NUM_MAX) {
|
|
|
|
|
2020-12-28 18:31:31 +01:00
|
|
|
ESP_LOGI(TAG, "Preparing to sleep...");
|
2020-12-16 09:36:16 +01:00
|
|
|
|
2020-12-28 18:31:31 +01:00
|
|
|
RTC_runmode = RUNMODE_SLEEP;
|
2020-12-10 15:10:28 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
// validate wake up pin, if we have
|
|
|
|
if (!GPIO_IS_VALID_GPIO(wakeup_gpio))
|
|
|
|
wakeup_gpio = GPIO_NUM_MAX;
|
2019-10-20 20:05:13 +02:00
|
|
|
|
2020-12-22 19:13:46 +01:00
|
|
|
// stop further enqueuing of senddata and MAC processing
|
|
|
|
sendTimer.detach();
|
|
|
|
|
2020-12-26 20:45:10 +01:00
|
|
|
// switch off radio and other power consuming hardware
|
2020-12-21 19:35:21 +01:00
|
|
|
#if (WIFICOUNTER)
|
|
|
|
switch_wifi_sniffer(0);
|
|
|
|
#endif
|
|
|
|
#if (BLECOUNTER)
|
|
|
|
stop_BLEscan();
|
|
|
|
btStop();
|
|
|
|
#endif
|
2020-12-26 20:45:10 +01:00
|
|
|
#if (HAS_SDS011)
|
|
|
|
sds011_sleep(void);
|
|
|
|
#endif
|
2020-12-21 19:35:21 +01:00
|
|
|
|
2020-12-22 19:13:46 +01:00
|
|
|
// stop MAC processing
|
2020-12-21 19:35:21 +01:00
|
|
|
vTaskDelete(macProcessTask);
|
2020-12-10 15:10:28 +01:00
|
|
|
|
2020-12-10 22:14:41 +01:00
|
|
|
// halt interrupts accessing i2c bus
|
|
|
|
mask_user_IRQ();
|
|
|
|
|
2020-12-10 15:10:28 +01:00
|
|
|
// wait a while (max 100 sec) to clear send queues
|
2020-12-09 10:15:12 +01:00
|
|
|
ESP_LOGI(TAG, "Waiting until send queues are empty...");
|
2020-12-28 18:31:31 +01:00
|
|
|
for (i = 100; i > 0; i--) {
|
2020-12-29 00:21:40 +01:00
|
|
|
if (allQueuesEmtpy())
|
2020-12-10 15:10:28 +01:00
|
|
|
break;
|
2020-12-29 00:21:40 +01:00
|
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
2020-12-10 15:10:28 +01:00
|
|
|
}
|
2019-10-20 20:05:13 +02:00
|
|
|
|
2020-12-28 18:31:31 +01:00
|
|
|
// shutdown LMIC safely, waiting max 100 sec
|
2019-10-22 07:16:04 +02:00
|
|
|
#if (HAS_LORA)
|
2020-12-09 10:15:12 +01:00
|
|
|
ESP_LOGI(TAG, "Waiting until LMIC is idle...");
|
2020-12-28 18:31:31 +01:00
|
|
|
for (i = 100; i > 0; i--) {
|
2020-12-10 15:10:28 +01:00
|
|
|
if ((LMIC.opmode & OP_TXRXPEND) ||
|
|
|
|
os_queryTimeCriticalJobs(sec2osticks(wakeup_sec)))
|
2020-12-28 18:31:31 +01:00
|
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
2020-12-10 15:10:28 +01:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2020-12-11 16:34:17 +01:00
|
|
|
#endif // (HAS_LORA)
|
2019-10-27 16:13:33 +01:00
|
|
|
|
2020-12-11 16:34:17 +01:00
|
|
|
// shutdown MQTT safely
|
|
|
|
#ifdef HAS_MQTT
|
2020-12-23 16:31:06 +01:00
|
|
|
mqtt_deinit();
|
2020-12-11 16:34:17 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// shutdown SPI safely
|
|
|
|
#ifdef HAS_SPI
|
2020-12-23 16:31:06 +01:00
|
|
|
spi_deinit();
|
2020-12-11 16:34:17 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// wait until rcommands are all done
|
|
|
|
for (i = 10; i > 0; i--) {
|
|
|
|
if (rcmd_busy)
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(1000));
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-12-28 18:31:31 +01:00
|
|
|
// save LMIC state to RTC RAM
|
2020-12-11 16:34:17 +01:00
|
|
|
#if (HAS_LORA)
|
2020-12-09 10:15:12 +01:00
|
|
|
SaveLMICToRTC(wakeup_sec);
|
|
|
|
#endif // (HAS_LORA)
|
2019-10-27 16:13:33 +01:00
|
|
|
|
2020-12-11 16:34:17 +01:00
|
|
|
// set display to power save mode
|
2019-10-20 20:05:13 +02:00
|
|
|
#ifdef HAS_DISPLAY
|
2020-03-29 12:10:42 +02:00
|
|
|
dp_shutdown();
|
2019-10-20 20:05:13 +02:00
|
|
|
#endif
|
|
|
|
|
2020-12-26 18:29:10 +01:00
|
|
|
// reduce power if has PMU or VEXT
|
2019-10-20 20:05:13 +02:00
|
|
|
#ifdef HAS_PMU
|
|
|
|
AXP192_power(pmu_power_sleep);
|
2020-12-26 18:29:10 +01:00
|
|
|
#elif EXT_POWER_SW
|
|
|
|
digitalWrite(EXT_POWER_SW, EXT_POWER_OFF);
|
2019-10-20 20:05:13 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// shutdown i2c bus
|
|
|
|
i2c_deinit();
|
|
|
|
|
2020-12-09 20:37:03 +01:00
|
|
|
// configure wakeup sources
|
|
|
|
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/sleep_modes.html
|
|
|
|
|
|
|
|
// set up RTC wakeup timer, if we have
|
|
|
|
if (wakeup_sec > 0) {
|
|
|
|
esp_sleep_enable_timer_wakeup(wakeup_sec * uS_TO_S_FACTOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// set wakeup gpio, if we have
|
|
|
|
if (wakeup_gpio != GPIO_NUM_MAX) {
|
|
|
|
rtc_gpio_isolate(wakeup_gpio); // minimize deep sleep current
|
|
|
|
esp_sleep_enable_ext1_wakeup(1ULL << wakeup_gpio, ESP_EXT1_WAKEUP_ALL_LOW);
|
|
|
|
}
|
|
|
|
|
2020-12-19 12:27:07 +01:00
|
|
|
// time stamp sleep start time and save system monotonic time. Deep sleep.
|
2020-12-09 20:37:03 +01:00
|
|
|
gettimeofday(&RTC_sleep_start_time, NULL);
|
2020-12-19 22:30:34 +01:00
|
|
|
RTC_millis += millis();
|
2020-12-09 10:15:12 +01:00
|
|
|
ESP_LOGI(TAG, "Going to sleep, good bye.");
|
2019-10-20 20:05:13 +02:00
|
|
|
esp_deep_sleep_start();
|
2020-12-19 22:30:34 +01:00
|
|
|
}
|
|
|
|
|
2020-12-21 22:11:41 +01:00
|
|
|
unsigned long long uptime() { return (RTC_millis + millis()); }
|