Skip to content

Commit 34e0198

Browse files
committed
esp8266: Extend system microsecond counter to 64-bits; use in ticks_ms.
So now ticks_ms can count up to the full 30 bits. Fixes issue adafruit#2412.
1 parent cc7c311 commit 34e0198

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

esp8266/esp_mphal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void mp_hal_debug_tx_strn_cooked(void *env, const char *str, uint32_t len) {
117117
}
118118

119119
uint32_t mp_hal_ticks_ms(void) {
120-
return system_get_time() / 1000;
120+
return ((uint64_t)system_time_high_word << 32 | (uint64_t)system_get_time()) / 1000;
121121
}
122122

123123
uint32_t mp_hal_ticks_us(void) {

esp8266/ets_alt_task.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,22 @@ bool ets_post(uint8 prio, os_signal_t sig, os_param_t param) {
110110

111111
int ets_loop_iter_disable = 0;
112112

113+
// to implement a 64-bit wide microsecond counter
114+
static uint32_t system_time_prev = 0;
115+
uint32_t system_time_high_word = 0;
116+
113117
bool ets_loop_iter(void) {
114118
if (ets_loop_iter_disable) {
115119
return false;
116120
}
121+
122+
// handle overflow of system microsecond counter
123+
uint32_t system_time_cur = system_get_time();
124+
if (system_time_cur < system_time_prev) {
125+
system_time_high_word += 1; // record overflow of low 32-bits
126+
}
127+
system_time_prev = system_time_cur;
128+
117129
//static unsigned cnt;
118130
bool progress = false;
119131
for (volatile struct task_entry *t = emu_tasks; t < &emu_tasks[MP_ARRAY_SIZE(emu_tasks)]; t++) {

esp8266/ets_alt_task.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
extern int ets_loop_iter_disable;
2+
extern uint32_t system_time_high_word;
3+
24
bool ets_loop_iter(void);

0 commit comments

Comments
 (0)