revert _millis & sanitizations deep sleep

This commit is contained in:
Klaus K Wilting 2020-12-09 20:37:03 +01:00
parent 8db24bfa8e
commit 22f0ce5184
10 changed files with 67 additions and 71 deletions

View File

@ -56,10 +56,7 @@
; \
}
// emulate millis to avoid rollovers
#define _millis() esp_timer_get_time() / 1000
#define _micros() esp_timer_get_time()
#define _seconds() _millis() / 1000.0
#define _seconds() millis() / 1000.0
enum timesource_t { _gps, _rtc, _lora, _unsynced };
enum snifftype_t { MAC_SNIFF_WIFI, MAC_SNIFF_BLE, MAC_SNIFF_BLE_ENS };

View File

@ -10,7 +10,7 @@
#include "power.h"
void do_reset(bool warmstart);
void do_after_reset(int reason);
void enter_deepsleep(const int wakeup_sec, const gpio_num_t wakeup_gpio);
void do_after_reset(void);
void enter_deepsleep(const uint64_t wakeup_sec, const gpio_num_t wakeup_gpio);
#endif // _RESET_H

View File

@ -228,7 +228,7 @@ void updateState(void) {
} else {
/* Update every STATE_SAVE_PERIOD minutes */
if ((stateUpdateCounter * STATE_SAVE_PERIOD) < _millis()) {
if ((stateUpdateCounter * STATE_SAVE_PERIOD) < millis()) {
update = true;
stateUpdateCounter++;
}

View File

@ -44,7 +44,7 @@ bool cwa_init(void) {
}
void cwa_mac_add(uint16_t hashedmac) {
cwaSeenNotifiers[hashedmac] = _millis(); // hash last seen at ....
cwaSeenNotifiers[hashedmac] = millis(); // hash last seen at ....
}
#endif

View File

@ -136,7 +136,7 @@ void doHousekeeping() {
} // doHousekeeping()
uint64_t uptime() { return _millis(); }
uint64_t uptime() { return millis(); }
uint32_t getFreeRAM() {
#ifndef BOARD_HAS_PSRAM

View File

@ -9,7 +9,7 @@ led_states previousLEDState =
TaskHandle_t ledLoopTask;
uint16_t LEDColor = COLOR_NONE, LEDBlinkDuration = 0; // state machine variables
unsigned long LEDBlinkStarted = 0; // When (in _millis() led blink started)
unsigned long LEDBlinkStarted = 0; // When (in millis() led blink started)
#ifdef HAS_RGB_LED
@ -133,7 +133,7 @@ void blink_LED(uint16_t set_color, uint16_t set_blinkduration) {
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
LEDColor = set_color; // set color for RGB LED
LEDBlinkDuration = set_blinkduration; // duration
LEDBlinkStarted = _millis(); // Time Start here
LEDBlinkStarted = millis(); // Time Start here
LEDState = LED_ON; // Let main set LED on
#endif
}
@ -145,8 +145,8 @@ void ledLoop(void *parameter) {
// Custom blink running always have priority other LoRaWAN led
// management
if (LEDBlinkStarted && LEDBlinkDuration) {
// Custom blink is finished, let this order, avoid _millis() overflow
if ((_millis() - LEDBlinkStarted) >= LEDBlinkDuration) {
// Custom blink is finished, let this order, avoid millis() overflow
if ((millis() - LEDBlinkStarted) >= LEDBlinkDuration) {
// Led becomes off, and stop blink
LEDState = LED_OFF;
LEDBlinkStarted = 0;
@ -165,7 +165,7 @@ void ledLoop(void *parameter) {
LEDColor = COLOR_YELLOW;
// quick blink 20ms on each 1/5 second
LEDState =
((_millis() % 200) < 20) ? LED_ON : LED_OFF; // TX data pending
((millis() % 200) < 20) ? LED_ON : LED_OFF; // TX data pending
} else if (LMIC.opmode & (OP_TXDATA | OP_TXRXPEND)) {
// select color to blink by message port
switch (LMIC.pendTxPort) {
@ -180,13 +180,13 @@ void ledLoop(void *parameter) {
break;
}
// small blink 10ms on each 1/2sec (not when joining)
LEDState = ((_millis() % 500) < 10) ? LED_ON : LED_OFF;
LEDState = ((millis() % 500) < 10) ? LED_ON : LED_OFF;
// This should not happen so indicate a problem
} else if (LMIC.opmode &
((OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0)) {
LEDColor = COLOR_RED;
// heartbeat long blink 200ms on each 2 seconds
LEDState = ((_millis() % 2000) < 200) ? LED_ON : LED_OFF;
LEDState = ((millis() % 2000) < 200) ? LED_ON : LED_OFF;
} else
#endif // HAS_LORA
{

View File

@ -138,7 +138,7 @@ void setup() {
esp_log_level_set("*", ESP_LOG_NONE);
#endif
do_after_reset(rtc_get_reset_reason(0));
do_after_reset();
// print chip information on startup if in verbose mode after coldstart
#if (VERBOSE)

View File

@ -178,9 +178,9 @@ int do_ota_update() {
client.print("Cache-Control: no-cache\r\n");
client.print("Connection: close\r\n\r\n");
unsigned long timeout = _millis();
unsigned long timeout = millis();
while (client.available() == 0) {
if ((_millis() - timeout) > (RESPONSE_TIMEOUT_MS)) {
if ((millis() - timeout) > (RESPONSE_TIMEOUT_MS)) {
ESP_LOGI(TAG, "Client timeout");
ota_display(3, " E", "client timeout");
goto abort;

View File

@ -8,8 +8,9 @@ static const char TAG[] = __FILE__;
// Conversion factor for micro seconds to seconds
#define uS_TO_S_FACTOR 1000000ULL
// variable keep its values after restart or wakeup from sleep
RTC_NOINIT_ATTR runmode_t RTC_runmode;
// 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;
const char *runmode[4] = {"powercycle", "normal", "wakeup", "update"};
@ -30,45 +31,44 @@ void do_reset(bool warmstart) {
esp_restart();
}
void do_after_reset(int reason) {
void do_after_reset(void) {
switch (reason) {
struct timeval sleep_stop_time;
uint64_t sleep_time_ms;
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;
switch (esp_sleep_get_wakeup_cause()) {
case ESP_SLEEP_WAKEUP_EXT0: // Wakeup caused by external signal using RTC_IO
case ESP_SLEEP_WAKEUP_EXT1: // Wakeup caused by external signal using
// RTC_CNTL
case ESP_SLEEP_WAKEUP_TIMER: // Wakeup caused by timer
case ESP_SLEEP_WAKEUP_TOUCHPAD: // Wakeup caused by touchpad
case ESP_SLEEP_WAKEUP_ULP: // Wakeup caused by ULP program
case SW_CPU_RESET: // 0x0c Software reset CPU
// keep previous runmode (could be RUNMODE_UPDATE)
break;
// 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);
case DEEPSLEEP_RESET: // 0x05 Deep Sleep reset digital core
RTC_runmode = RUNMODE_WAKEUP;
break;
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
case ESP_SLEEP_WAKEUP_ALL:
case ESP_SLEEP_WAKEUP_GPIO:
case ESP_SLEEP_WAKEUP_UART:
case ESP_SLEEP_WAKEUP_UNDEFINED:
default:
// not a deep sleep reset
RTC_runmode = RUNMODE_POWERCYCLE;
break;
}
} // switch
ESP_LOGI(TAG, "Starting Software v%s, runmode %s", PROGVERSION,
runmode[RTC_runmode]);
}
void enter_deepsleep(const int wakeup_sec = 60,
void enter_deepsleep(const uint64_t wakeup_sec = 60,
const gpio_num_t wakeup_gpio = GPIO_NUM_MAX) {
// ensure we are in normal runmode, not udpate or wakeup
@ -83,15 +83,6 @@ void enter_deepsleep(const int wakeup_sec = 60,
ESP_LOGI(TAG, "Attempting to sleep...");
}
// switch off radio
#if (BLECOUNTER)
stop_BLEscan();
btStop();
#endif
#if (WIFICOUNTER)
switch_wifi_sniffer(0);
#endif
// wait until all send queues are empty
ESP_LOGI(TAG, "Waiting until send queues are empty...");
while (!allQueuesEmtpy())
@ -105,23 +96,16 @@ void enter_deepsleep(const int wakeup_sec = 60,
vTaskDelay(pdMS_TO_TICKS(100));
SaveLMICToRTC(wakeup_sec);
// vTaskDelete(lmicTask);
// LMIC_shutdown();
#endif // (HAS_LORA)
// set up RTC power domains
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
// 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);
esp_sleep_enable_ext1_wakeup(1ULL << wakeup_gpio, ESP_EXT1_WAKEUP_ALL_LOW);
}
// switch off radio
#if (BLECOUNTER)
stop_BLEscan();
btStop();
#endif
#if (WIFICOUNTER)
switch_wifi_sniffer(0);
#endif
// halt interrupts accessing i2c bus
mask_user_IRQ();
@ -139,7 +123,22 @@ void enter_deepsleep(const int wakeup_sec = 60,
// shutdown i2c bus
i2c_deinit();
// enter sleep mode
// 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);
}
// save sleep start time. Deep sleep.
gettimeofday(&RTC_sleep_start_time, NULL);
ESP_LOGI(TAG, "Going to sleep, good bye.");
esp_deep_sleep_start();
}

View File

@ -26,7 +26,7 @@ Ticker timesyncer;
void setTimeSyncIRQ() { xTaskNotify(irqHandlerTask, TIMESYNC_IRQ, eSetBits); }
void calibrateTime(void) {
ESP_LOGD(TAG, "[%0.3f] calibrateTime, timeSource == %d", _millis() / 1000.0,
ESP_LOGD(TAG, "[%0.3f] calibrateTime, timeSource == %d", millis() / 1000.0,
timeSource);
time_t t = 0;
uint16_t t_msec = 0;