8000 esp8266: Fix ticks_ms to correctly handle wraparound of system counter. · guidebee/micropython@5357dad · GitHub
[go: up one dir, main page]

Skip to content

Commit 5357dad

Browse files
committed
esp8266: Fix ticks_ms to correctly handle wraparound of system counter.
Fixes issue micropython#4795.
1 parent c4a6d9c commit 5357dad

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

ports/esp8266/esp_mphal.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ void mp_hal_debug_tx_strn_cooked(void *env, const char *str, uint32_t len) {
120120
}
121121

122122
uint32_t mp_hal_ticks_ms(void) {
123-
return ((uint64_t)system_time_high_word << 32 | (uint64_t)system_get_time()) / 1000;
123+
// Compute milliseconds from 64-bit microsecond counter
124+
system_time_update();
125+
return ((uint64_t)system_time_high_word << 32 | (uint64_t)system_time_low_word) / 1000;
124126
}
125127

126128
uint32_t mp_hal_ticks_us(void) {

ports/esp8266/ets_alt_task.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,27 @@ int ets_loop_iter_disable = 0;
112112
int ets_loop_dont_feed_sw_wdt = 0;
113113

114114
// to implement a 64-bit wide microsecond counter
115-
static uint32_t system_time_prev = 0;
115+
uint32_t system_time_low_word = 0;
116116
uint32_t system_time_high_word = 0;
117117

118-
bool ets_loop_iter(void) {
119-
if (ets_loop_iter_disable) {
120-
return false;
121-
}
122-
123-
// handle overflow of system microsecond counter
118+
void system_time_update(void) {
119+
// Handle overflow of system microsecond counter
124120
ets_intr_lock();
125121
uint32_t system_time_cur = system_get_time();
126-
if (system_time_cur < system_time_prev) {
122+
if (system_time_cur < system_time_low_word) {
127123
system_time_high_word += 1; // record overflow of low 32-bits
128124
}
129-
system_time_prev = system_time_cur;
125+
system_time_low_word = system_time_cur;
130126
ets_intr_unlock();
127+
}
128+
129+
bool ets_loop_iter(void) {
130+
if (ets_loop_iter_disable) {
131+
return false;
132+
}
133+
134+
// Update 64-bit microsecond counter
135+
system_time_update();
131136

132137
// 6 words before pend_flag_noise_check is a variable that is used by
133138
// the software WDT. A 1.6 second period timer will increment this

ports/esp8266/ets_alt_task.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
extern int ets_loop_iter_disable;
55
extern int ets_loop_dont_feed_sw_wdt;
6+
extern uint32_t system_time_low_word;
67
extern uint32_t system_time_high_word;
78

9+
void system_time_update(void);
810
bool ets_loop_iter(void);
911

1012
#endif // MICROPY_INCLUDED_ESP8266_ETS_ALT_TASK_H

0 commit comments

Comments
 (0)
0