reset.cpp/.h code sanitizations

This commit is contained in:
cyberman54 2023-03-12 00:31:55 +01:00
parent d626f86754
commit 3b6ecf7b36
2 changed files with 16 additions and 14 deletions

View File

@ -15,7 +15,7 @@ void reset_rtc_vars(void);
void do_reset(bool warmstart);
void do_after_reset(void);
void enter_deepsleep(uint32_t wakeup_sec, const gpio_num_t wakeup_gpio);
unsigned long long uptime(void);
uint64_t uptime(void);
enum runmode_t {
RUNMODE_POWERCYCLE,

View File

@ -5,14 +5,14 @@
// Conversion factor for micro seconds to seconds
#define uS_TO_S_FACTOR 1000000ULL
// RTC_NOINIT_ATTR -> keep value after a software restart or system crash
// RTC_NOINIT_ATTR -> keeps value after a software restart or system crash
RTC_NOINIT_ATTR runmode_t RTC_runmode;
RTC_NOINIT_ATTR uint32_t RTC_restarts;
// RTC_DATA_ATTR -> keep values after a wakeup from sleep
RTC_DATA_ATTR struct timeval RTC_sleep_start_time;
RTC_DATA_ATTR unsigned long long RTC_millis = 0;
// RTC_DATA_ATTR -> keeps value after a wakeup from sleep
RTC_DATA_ATTR struct timeval sleep_start_time;
RTC_DATA_ATTR int64_t RTC_millis = 0;
timeval sleep_stop_time;
struct timeval sleep_stop_time;
void reset_rtc_vars(void) {
RTC_runmode = RUNMODE_POWERCYCLE;
@ -36,7 +36,7 @@ void do_reset(bool warmstart) {
void do_after_reset(void) {
struct timeval sleep_stop_time;
uint64_t sleep_time_ms;
int64_t sleep_time_ms;
// read (and initialize on first run) runtime settings from NVRAM
loadConfig();
@ -62,9 +62,11 @@ void do_after_reset(void) {
case RESET_REASON_CORE_DEEP_SLEEP:
// 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;
sleep_time_ms = ((int64_t)sleep_stop_time.tv_sec * 1000000L +
(int64_t)sleep_stop_time.tv_usec -
(int64_t)sleep_start_time.tv_sec * 1000000L -
(int64_t)sleep_start_time.tv_usec) /
1000LL;
RTC_millis += sleep_time_ms; // increment system monotonic time
ESP_LOGI(TAG, "Time spent in deep sleep: %llu ms", sleep_time_ms);
// do we have a valid time? -> set global variable
@ -180,8 +182,8 @@ void enter_deepsleep(uint32_t wakeup_sec, gpio_num_t wakeup_gpio) {
}
// time stamp sleep start time and save system monotonic time. Deep sleep.
gettimeofday(&RTC_sleep_start_time, NULL);
RTC_millis += esp_timer_get_time() / 1000;
gettimeofday(&sleep_start_time, NULL);
RTC_millis += esp_timer_get_time() / 1000LL;
ESP_LOGI(TAG, "Going to sleep, good bye.");
// flush & close sd card, if we have
@ -192,6 +194,6 @@ void enter_deepsleep(uint32_t wakeup_sec, gpio_num_t wakeup_gpio) {
esp_deep_sleep_start();
}
unsigned long long uptime() {
return (RTC_millis + esp_timer_get_time() / 1000);
uint64_t uptime() {
return (uint64_t)(RTC_millis + esp_timer_get_time() / 1000LL);
}