eliminate usage of millis() in whole code

This commit is contained in:
Klaus K Wilting 2020-11-02 12:39:46 +01:00
parent 8341a2115e
commit e5d13be42e
9 changed files with 31 additions and 29 deletions

View File

@ -62,6 +62,8 @@
// emulate millis to avoid rollovers // emulate millis to avoid rollovers
#define _millis() esp_timer_get_time() / 1000 #define _millis() esp_timer_get_time() / 1000
#define _micros() esp_timer_get_time()
#define _seconds() _millis() / 1000.0
enum sendprio_t { prio_low, prio_normal, prio_high }; enum sendprio_t { prio_low, prio_normal, prio_high };
enum timesource_t { _gps, _rtc, _lora, _unsynced }; enum timesource_t { _gps, _rtc, _lora, _unsynced };

View File

@ -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++;
} }

View File

@ -49,7 +49,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

View File

@ -94,7 +94,7 @@ void dp_init(bool verbose) {
#if (HAS_DISPLAY) == 1 // i2c #if (HAS_DISPLAY) == 1 // i2c
// block i2c bus access // block i2c bus access
if (!I2C_MUTEX_LOCK()) if (!I2C_MUTEX_LOCK())
ESP_LOGV(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0); ESP_LOGV(TAG, "[%0.3f] i2c mutex lock failed", _seconds());
else { else {
#endif #endif
@ -190,7 +190,7 @@ void dp_refresh(bool nextPage) {
// block i2c bus access // block i2c bus access
if (!I2C_MUTEX_LOCK()) if (!I2C_MUTEX_LOCK())
ESP_LOGV(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0); ESP_LOGV(TAG, "[%0.3f] i2c mutex lock failed", _seconds());
else { else {
// set display on/off according to current device configuration // set display on/off according to current device configuration
if (DisplayIsOn != cfg.screenon) { if (DisplayIsOn != cfg.screenon) {
@ -600,7 +600,7 @@ void dp_shutdown(void) {
#if (HAS_DISPLAY) == 1 #if (HAS_DISPLAY) == 1
// block i2c bus access // block i2c bus access
if (!I2C_MUTEX_LOCK()) if (!I2C_MUTEX_LOCK())
ESP_LOGV(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0); ESP_LOGV(TAG, "[%0.3f] i2c mutex lock failed", _seconds());
else { else {
cfg.screenon = 0; cfg.screenon = 0;
obdPower(&ssoled, false); obdPower(&ssoled, false);

View File

@ -109,7 +109,7 @@ uint8_t i2c_readBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len) {
I2C_MUTEX_UNLOCK(); // release i2c bus access I2C_MUTEX_UNLOCK(); // release i2c bus access
return ret; return ret;
} else { } else {
ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0); ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", _seconds());
return 0xFF; return 0xFF;
} }
} }
@ -128,7 +128,7 @@ uint8_t i2c_writeBytes(uint8_t addr, uint8_t reg, uint8_t *data, uint8_t len) {
I2C_MUTEX_UNLOCK(); // release i2c bus access I2C_MUTEX_UNLOCK(); // release i2c bus access
return ret ? ret : 0xFF; return ret ? ret : 0xFF;
} else { } else {
ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0); ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", _seconds());
return 0xFF; return 0xFF;
} }
} }

View File

@ -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
{ {

View File

@ -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;

View File

@ -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;
@ -85,7 +85,7 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec,
vTaskDelay(pdMS_TO_TICKS(1000 - t_msec % 1000)); vTaskDelay(pdMS_TO_TICKS(1000 - t_msec % 1000));
} }
ESP_LOGI(TAG, "[%0.3f] UTC time: %d.%03d sec", millis() / 1000.0, ESP_LOGI(TAG, "[%0.3f] UTC time: %d.%03d sec", _seconds(),
time_to_set, t_msec % 1000); time_to_set, t_msec % 1000);
// if we have got an external timesource, set RTC time and shift RTC_INT pulse // if we have got an external timesource, set RTC time and shift RTC_INT pulse
@ -106,11 +106,11 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec,
timeSource = mytimesource; // set global variable timeSource = mytimesource; // set global variable
timesyncer.attach(TIME_SYNC_INTERVAL * 60, setTimeSyncIRQ); timesyncer.attach(TIME_SYNC_INTERVAL * 60, setTimeSyncIRQ);
ESP_LOGD(TAG, "[%0.3f] Timesync finished, time was set | source: %c", ESP_LOGD(TAG, "[%0.3f] Timesync finished, time was set | source: %c",
millis() / 1000.0, timeSetSymbols[mytimesource]); _seconds(), timeSetSymbols[mytimesource]);
} else { } else {
timesyncer.attach(TIME_SYNC_INTERVAL_RETRY * 60, setTimeSyncIRQ); timesyncer.attach(TIME_SYNC_INTERVAL_RETRY * 60, setTimeSyncIRQ);
ESP_LOGD(TAG, "[%0.3f] Timesync failed, invalid time fetched | source: %c", ESP_LOGD(TAG, "[%0.3f] Timesync failed, invalid time fetched | source: %c",
millis() / 1000.0, timeSetSymbols[mytimesource]); _seconds(), timeSetSymbols[mytimesource]);
} }
} }

View File

@ -47,7 +47,7 @@ void timesync_request(void) {
// start timesync handshake // start timesync handshake
else { else {
ESP_LOGI(TAG, "[%0.3f] Timeserver sync request started, seqNo#%d", ESP_LOGI(TAG, "[%0.3f] Timeserver sync request started, seqNo#%d",
millis() / 1000.0, time_sync_seqNo); _seconds(), time_sync_seqNo);
xTaskNotifyGive(timeSyncProcTask); // unblock timesync task xTaskNotifyGive(timeSyncProcTask); // unblock timesync task
} }
} }
@ -98,14 +98,14 @@ void IRAM_ATTR timesync_processReq(void *taskparameter) {
if (xTaskNotifyWait(0x00, ULONG_MAX, &rcv_seqNo, if (xTaskNotifyWait(0x00, ULONG_MAX, &rcv_seqNo,
pdMS_TO_TICKS(TIME_SYNC_TIMEOUT * 1000)) == pdFALSE) { pdMS_TO_TICKS(TIME_SYNC_TIMEOUT * 1000)) == pdFALSE) {
ESP_LOGW(TAG, "[d%0.3f] Timesync aborted: timed out", ESP_LOGW(TAG, "[d%0.3f] Timesync aborted: timed out",
millis() / 1000.0); _seconds());
goto Fail; // no timestamp received before timeout goto Fail; // no timestamp received before timeout
} }
// check if we are in handshake with server // check if we are in handshake with server
if (rcv_seqNo != time_sync_seqNo) { if (rcv_seqNo != time_sync_seqNo) {
ESP_LOGW(TAG, "[%0.3f] Timesync aborted: handshake out of sync", ESP_LOGW(TAG, "[%0.3f] Timesync aborted: handshake out of sync",
millis() / 1000.0); _seconds());
goto Fail; goto Fail;
} }
@ -166,7 +166,7 @@ void IRAM_ATTR timesync_processReq(void *taskparameter) {
// store incoming timestamps // store incoming timestamps
void timesync_store(uint32_t timestamp, timesync_t timestamp_type) { void timesync_store(uint32_t timestamp, timesync_t timestamp_type) {
ESP_LOGD(TAG, "[%0.3f] seq#%d[%d]: t%d=%d", millis() / 1000.0, ESP_LOGD(TAG, "[%0.3f] seq#%d[%d]: t%d=%d", _seconds(),
time_sync_seqNo, sample_idx, timestamp_type, timestamp); time_sync_seqNo, sample_idx, timestamp_type, timestamp);
timesync_timestamp[sample_idx][timestamp_type] = timestamp; timesync_timestamp[sample_idx][timestamp_type] = timestamp;
} }
@ -214,10 +214,10 @@ void IRAM_ATTR timesync_serverAnswer(void *pUserData, int flag) {
if (flag != TIME_SYNC_FRAME_LENGTH) { if (flag != TIME_SYNC_FRAME_LENGTH) {
if (rcv_seqNo == TIME_SYNC_END_FLAG) if (rcv_seqNo == TIME_SYNC_END_FLAG)
ESP_LOGI(TAG, "[%0.3f] Timeserver error: no confident time available", ESP_LOGI(TAG, "[%0.3f] Timeserver error: no confident time available",
millis() / 1000.0); _seconds());
else else
ESP_LOGW(TAG, "[%0.3f] Timeserver error: spurious data received", ESP_LOGW(TAG, "[%0.3f] Timeserver error: spurious data received",
millis() / 1000.0); _seconds());
goto Exit; // failure goto Exit; // failure
} }
@ -233,13 +233,13 @@ void IRAM_ATTR timesync_serverAnswer(void *pUserData, int flag) {
if (flag != 1) { if (flag != 1) {
ESP_LOGW(TAG, "[%0.3f] Network did not answer time request", ESP_LOGW(TAG, "[%0.3f] Network did not answer time request",
millis() / 1000.0); _seconds());
goto Exit; goto Exit;
} }
// Populate lmic_time_reference // Populate lmic_time_reference
if ((LMIC_getNetworkTimeReference(&lmicTime)) != 1) { if ((LMIC_getNetworkTimeReference(&lmicTime)) != 1) {
ESP_LOGW(TAG, "[%0.3f] Network time request failed", millis() / 1000.0); ESP_LOGW(TAG, "[%0.3f] Network time request failed", _seconds());
goto Exit; goto Exit;
} }
@ -267,7 +267,7 @@ Finish:
rc = 1; rc = 1;
} else { } else {
ESP_LOGW(TAG, "[%0.3f] Timeserver error: outdated time received", ESP_LOGW(TAG, "[%0.3f] Timeserver error: outdated time received",
millis() / 1000.0); _seconds());
} }
Exit: Exit: