revert _millis & sanitizations deep sleep
This commit is contained in:
parent
8db24bfa8e
commit
22f0ce5184
@ -56,10 +56,7 @@
|
|||||||
; \
|
; \
|
||||||
}
|
}
|
||||||
|
|
||||||
// emulate millis to avoid rollovers
|
#define _seconds() millis() / 1000.0
|
||||||
#define _millis() esp_timer_get_time() / 1000
|
|
||||||
#define _micros() esp_timer_get_time()
|
|
||||||
#define _seconds() _millis() / 1000.0
|
|
||||||
|
|
||||||
enum timesource_t { _gps, _rtc, _lora, _unsynced };
|
enum timesource_t { _gps, _rtc, _lora, _unsynced };
|
||||||
enum snifftype_t { MAC_SNIFF_WIFI, MAC_SNIFF_BLE, MAC_SNIFF_BLE_ENS };
|
enum snifftype_t { MAC_SNIFF_WIFI, MAC_SNIFF_BLE, MAC_SNIFF_BLE_ENS };
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
#include "power.h"
|
#include "power.h"
|
||||||
|
|
||||||
void do_reset(bool warmstart);
|
void do_reset(bool warmstart);
|
||||||
void do_after_reset(int reason);
|
void do_after_reset(void);
|
||||||
void enter_deepsleep(const int wakeup_sec, const gpio_num_t wakeup_gpio);
|
void enter_deepsleep(const uint64_t wakeup_sec, const gpio_num_t wakeup_gpio);
|
||||||
|
|
||||||
#endif // _RESET_H
|
#endif // _RESET_H
|
@ -228,7 +228,7 @@ void updateState(void) {
|
|||||||
} else {
|
} else {
|
||||||
|
|
||||||
/* Update every STATE_SAVE_PERIOD minutes */
|
/* Update every STATE_SAVE_PERIOD minutes */
|
||||||
if ((stateUpdateCounter * STATE_SAVE_PERIOD) < _millis()) {
|
if ((stateUpdateCounter * STATE_SAVE_PERIOD) < millis()) {
|
||||||
update = true;
|
update = true;
|
||||||
stateUpdateCounter++;
|
stateUpdateCounter++;
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ bool cwa_init(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void cwa_mac_add(uint16_t hashedmac) {
|
void cwa_mac_add(uint16_t hashedmac) {
|
||||||
cwaSeenNotifiers[hashedmac] = _millis(); // hash last seen at ....
|
cwaSeenNotifiers[hashedmac] = millis(); // hash last seen at ....
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -136,7 +136,7 @@ void doHousekeeping() {
|
|||||||
|
|
||||||
} // doHousekeeping()
|
} // doHousekeeping()
|
||||||
|
|
||||||
uint64_t uptime() { return _millis(); }
|
uint64_t uptime() { return millis(); }
|
||||||
|
|
||||||
uint32_t getFreeRAM() {
|
uint32_t getFreeRAM() {
|
||||||
#ifndef BOARD_HAS_PSRAM
|
#ifndef BOARD_HAS_PSRAM
|
||||||
|
14
src/led.cpp
14
src/led.cpp
@ -9,7 +9,7 @@ led_states previousLEDState =
|
|||||||
TaskHandle_t ledLoopTask;
|
TaskHandle_t ledLoopTask;
|
||||||
|
|
||||||
uint16_t LEDColor = COLOR_NONE, LEDBlinkDuration = 0; // state machine variables
|
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
|
#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)
|
#if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED)
|
||||||
LEDColor = set_color; // set color for RGB LED
|
LEDColor = set_color; // set color for RGB LED
|
||||||
LEDBlinkDuration = set_blinkduration; // duration
|
LEDBlinkDuration = set_blinkduration; // duration
|
||||||
LEDBlinkStarted = _millis(); // Time Start here
|
LEDBlinkStarted = millis(); // Time Start here
|
||||||
LEDState = LED_ON; // Let main set LED on
|
LEDState = LED_ON; // Let main set LED on
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@ -145,8 +145,8 @@ void ledLoop(void *parameter) {
|
|||||||
// Custom blink running always have priority other LoRaWAN led
|
// Custom blink running always have priority other LoRaWAN led
|
||||||
// management
|
// management
|
||||||
if (LEDBlinkStarted && LEDBlinkDuration) {
|
if (LEDBlinkStarted && LEDBlinkDuration) {
|
||||||
// Custom blink is finished, let this order, avoid _millis() overflow
|
// Custom blink is finished, let this order, avoid millis() overflow
|
||||||
if ((_millis() - LEDBlinkStarted) >= LEDBlinkDuration) {
|
if ((millis() - LEDBlinkStarted) >= LEDBlinkDuration) {
|
||||||
// Led becomes off, and stop blink
|
// Led becomes off, and stop blink
|
||||||
LEDState = LED_OFF;
|
LEDState = LED_OFF;
|
||||||
LEDBlinkStarted = 0;
|
LEDBlinkStarted = 0;
|
||||||
@ -165,7 +165,7 @@ void ledLoop(void *parameter) {
|
|||||||
LEDColor = COLOR_YELLOW;
|
LEDColor = COLOR_YELLOW;
|
||||||
// quick blink 20ms on each 1/5 second
|
// quick blink 20ms on each 1/5 second
|
||||||
LEDState =
|
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)) {
|
} else if (LMIC.opmode & (OP_TXDATA | OP_TXRXPEND)) {
|
||||||
// select color to blink by message port
|
// select color to blink by message port
|
||||||
switch (LMIC.pendTxPort) {
|
switch (LMIC.pendTxPort) {
|
||||||
@ -180,13 +180,13 @@ void ledLoop(void *parameter) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// small blink 10ms on each 1/2sec (not when joining)
|
// 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
|
// This should not happen so indicate a problem
|
||||||
} else if (LMIC.opmode &
|
} else if (LMIC.opmode &
|
||||||
((OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0)) {
|
((OP_TXDATA | OP_TXRXPEND | OP_JOINING | OP_REJOIN) == 0)) {
|
||||||
LEDColor = COLOR_RED;
|
LEDColor = COLOR_RED;
|
||||||
// heartbeat long blink 200ms on each 2 seconds
|
// 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
|
} else
|
||||||
#endif // HAS_LORA
|
#endif // HAS_LORA
|
||||||
{
|
{
|
||||||
|
@ -138,7 +138,7 @@ void setup() {
|
|||||||
esp_log_level_set("*", ESP_LOG_NONE);
|
esp_log_level_set("*", ESP_LOG_NONE);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
do_after_reset(rtc_get_reset_reason(0));
|
do_after_reset();
|
||||||
|
|
||||||
// print chip information on startup if in verbose mode after coldstart
|
// print chip information on startup if in verbose mode after coldstart
|
||||||
#if (VERBOSE)
|
#if (VERBOSE)
|
||||||
|
@ -178,9 +178,9 @@ int do_ota_update() {
|
|||||||
client.print("Cache-Control: no-cache\r\n");
|
client.print("Cache-Control: no-cache\r\n");
|
||||||
client.print("Connection: close\r\n\r\n");
|
client.print("Connection: close\r\n\r\n");
|
||||||
|
|
||||||
unsigned long timeout = _millis();
|
unsigned long timeout = millis();
|
||||||
while (client.available() == 0) {
|
while (client.available() == 0) {
|
||||||
if ((_millis() - timeout) > (RESPONSE_TIMEOUT_MS)) {
|
if ((millis() - timeout) > (RESPONSE_TIMEOUT_MS)) {
|
||||||
ESP_LOGI(TAG, "Client timeout");
|
ESP_LOGI(TAG, "Client timeout");
|
||||||
ota_display(3, " E", "client timeout");
|
ota_display(3, " E", "client timeout");
|
||||||
goto abort;
|
goto abort;
|
||||||
|
101
src/reset.cpp
101
src/reset.cpp
@ -8,8 +8,9 @@ static const char TAG[] = __FILE__;
|
|||||||
// Conversion factor for micro seconds to seconds
|
// Conversion factor for micro seconds to seconds
|
||||||
#define uS_TO_S_FACTOR 1000000ULL
|
#define uS_TO_S_FACTOR 1000000ULL
|
||||||
|
|
||||||
// variable keep its values after restart or wakeup from sleep
|
// variables keep its values after a wakeup from sleep
|
||||||
RTC_NOINIT_ATTR runmode_t RTC_runmode;
|
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"};
|
const char *runmode[4] = {"powercycle", "normal", "wakeup", "update"};
|
||||||
|
|
||||||
@ -30,45 +31,44 @@ void do_reset(bool warmstart) {
|
|||||||
esp_restart();
|
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
|
switch (esp_sleep_get_wakeup_cause()) {
|
||||||
case RTCWDT_BROWN_OUT_RESET: // 0x0f Reset when the vdd voltage is not
|
case ESP_SLEEP_WAKEUP_EXT0: // Wakeup caused by external signal using RTC_IO
|
||||||
// stable
|
case ESP_SLEEP_WAKEUP_EXT1: // Wakeup caused by external signal using
|
||||||
RTC_runmode = RUNMODE_POWERCYCLE;
|
// RTC_CNTL
|
||||||
break;
|
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
|
// calculate time spent in deep sleep
|
||||||
// keep previous runmode (could be RUNMODE_UPDATE)
|
gettimeofday(&sleep_stop_time, NULL);
|
||||||
break;
|
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;
|
RTC_runmode = RUNMODE_WAKEUP;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SW_RESET: // 0x03 Software reset digital core
|
case ESP_SLEEP_WAKEUP_ALL:
|
||||||
case OWDT_RESET: // 0x04 Legacy watch dog reset digital core
|
case ESP_SLEEP_WAKEUP_GPIO:
|
||||||
case SDIO_RESET: // 0x06 Reset by SLC module, reset digital core
|
case ESP_SLEEP_WAKEUP_UART:
|
||||||
case TG0WDT_SYS_RESET: // 0x07 Timer Group0 Watch dog reset digital core
|
case ESP_SLEEP_WAKEUP_UNDEFINED:
|
||||||
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
|
|
||||||
default:
|
default:
|
||||||
|
// not a deep sleep reset
|
||||||
RTC_runmode = RUNMODE_POWERCYCLE;
|
RTC_runmode = RUNMODE_POWERCYCLE;
|
||||||
break;
|
break;
|
||||||
}
|
} // switch
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Starting Software v%s, runmode %s", PROGVERSION,
|
ESP_LOGI(TAG, "Starting Software v%s, runmode %s", PROGVERSION,
|
||||||
runmode[RTC_runmode]);
|
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) {
|
const gpio_num_t 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
|
||||||
@ -83,15 +83,6 @@ void enter_deepsleep(const int wakeup_sec = 60,
|
|||||||
ESP_LOGI(TAG, "Attempting to sleep...");
|
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
|
// wait until all send queues are empty
|
||||||
ESP_LOGI(TAG, "Waiting until send queues are empty...");
|
ESP_LOGI(TAG, "Waiting until send queues are empty...");
|
||||||
while (!allQueuesEmtpy())
|
while (!allQueuesEmtpy())
|
||||||
@ -105,23 +96,16 @@ void enter_deepsleep(const int wakeup_sec = 60,
|
|||||||
vTaskDelay(pdMS_TO_TICKS(100));
|
vTaskDelay(pdMS_TO_TICKS(100));
|
||||||
|
|
||||||
SaveLMICToRTC(wakeup_sec);
|
SaveLMICToRTC(wakeup_sec);
|
||||||
// vTaskDelete(lmicTask);
|
|
||||||
// LMIC_shutdown();
|
|
||||||
#endif // (HAS_LORA)
|
#endif // (HAS_LORA)
|
||||||
|
|
||||||
// set up RTC power domains
|
// switch off radio
|
||||||
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
|
#if (BLECOUNTER)
|
||||||
|
stop_BLEscan();
|
||||||
// set up RTC wakeup timer, if we have
|
btStop();
|
||||||
if (wakeup_sec > 0) {
|
#endif
|
||||||
esp_sleep_enable_timer_wakeup(wakeup_sec * uS_TO_S_FACTOR);
|
#if (WIFICOUNTER)
|
||||||
}
|
switch_wifi_sniffer(0);
|
||||||
|
#endif
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// halt interrupts accessing i2c bus
|
// halt interrupts accessing i2c bus
|
||||||
mask_user_IRQ();
|
mask_user_IRQ();
|
||||||
@ -139,7 +123,22 @@ void enter_deepsleep(const int wakeup_sec = 60,
|
|||||||
// shutdown i2c bus
|
// shutdown i2c bus
|
||||||
i2c_deinit();
|
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_LOGI(TAG, "Going to sleep, good bye.");
|
||||||
esp_deep_sleep_start();
|
esp_deep_sleep_start();
|
||||||
}
|
}
|
@ -26,7 +26,7 @@ Ticker timesyncer;
|
|||||||
void setTimeSyncIRQ() { xTaskNotify(irqHandlerTask, TIMESYNC_IRQ, eSetBits); }
|
void setTimeSyncIRQ() { xTaskNotify(irqHandlerTask, TIMESYNC_IRQ, eSetBits); }
|
||||||
|
|
||||||
void calibrateTime(void) {
|
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);
|
timeSource);
|
||||||
time_t t = 0;
|
time_t t = 0;
|
||||||
uint16_t t_msec = 0;
|
uint16_t t_msec = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user