#include "arch.h" #include "timer.h" #include "util.h" // The clock lives in mg_micros(), mg_millis() is derived from it. Either one // can be replaced, see MG_ENABLE_CUSTOM_MICROS and MG_ENABLE_CUSTOM_MILLIS #if MG_ENABLE_CUSTOM_MICROS == 0 uint64_t mg_micros(void) { #if MG_ARCH == MG_ARCH_ESP32 return (uint64_t) esp_timer_get_time(); #elif MG_ENABLE_FREERTOS return (uint64_t) xTaskGetTickCount() * portTICK_PERIOD_MS * 1000; #elif MG_ARCH == MG_ARCH_THREADX return (uint64_t) tx_time_get() * (1000 /* MS per SEC */ / TX_TIMER_TICKS_PER_SECOND) * 1000; #elif MG_ARCH == MG_ARCH_TIRTOS return (uint64_t) Clock_getTicks() * 1000; #elif MG_ARCH == MG_ARCH_ZEPHYR return k_ticks_to_us_floor64((uint64_t) k_uptime_ticks()); #elif MG_ARCH == MG_ARCH_CMSIS_RTOS1 return (uint64_t) rt_time_get() * 1000; #elif MG_ARCH == MG_ARCH_CMSIS_RTOS2 return (uint64_t) ((osKernelGetTickCount() * 1000) / osKernelGetTickFreq()) * 1000; #elif MG_ARCH == MG_ARCH_RTTHREAD return (uint64_t) ((rt_tick_get() * 1000) / RT_TICK_PER_SECOND) * 1000; #elif MG_ARCH == MG_ARCH_WIN32 return (uint64_t) GetTickCount() * 1000; #elif MG_ARCH == MG_ARCH_PICOSDK return time_us_64(); #elif MG_ARCH == MG_ARCH_CUBE return (uint64_t) HAL_GetTick() * 1000; #elif MG_ARCH == MG_ARCH_UNIX && defined(__APPLE__) // Apple CLOCK_MONOTONIC_RAW is equivalent to CLOCK_BOOTTIME on linux // Apple CLOCK_UPTIME_RAW is equivalent to CLOCK_MONOTONIC_RAW on linux return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000; #elif MG_ARCH == MG_ARCH_UNIX struct timespec ts = {0, 0}; // See #1615 - prefer monotonic clock #if defined(CLOCK_MONOTONIC_RAW) // Raw hardware-based time that is not subject to NTP adjustment clock_gettime(CLOCK_MONOTONIC_RAW, &ts); #elif defined(CLOCK_MONOTONIC) // Affected by the incremental adjustments performed by adjtime and NTP clock_gettime(CLOCK_MONOTONIC, &ts); #else // Affected by discontinuous jumps in the system time and by the incremental // adjustments performed by adjtime and NTP clock_gettime(CLOCK_REALTIME, &ts); #endif return ((uint64_t) ts.tv_sec * 1000000 + (uint64_t) ts.tv_nsec / 1000); #elif defined(ARDUINO) return (uint64_t) micros(); #else return (uint64_t) time(NULL) * 1000000; #endif } #endif #if MG_ENABLE_CUSTOM_MILLIS == 0 uint64_t mg_millis(void) { return mg_micros() / 1000; } #endif void mg_timer_init(struct mg_timer **head, struct mg_timer *t, uint64_t ms, unsigned flags, void (*fn)(void *), void *arg) { t->period_ms = ms, t->expire = 0; t->flags = flags, t->fn = fn, t->arg = arg, t->next = *head; *head = t; } void mg_timer_free(struct mg_timer **head, struct mg_timer *t) { while (*head && *head != t) head = &(*head)->next; if (*head) *head = t->next; } // t: expiration time, prd: period, now: current time. Return true if expired bool mg_timer_expired(uint64_t *t, uint64_t prd, uint64_t now) { if (now + prd < *t) *t = 0; // Time wrapped? Reset timer if (*t == 0) *t = now + prd; // Firt poll? Set expiration if (*t > now) return false; // Not expired yet, return *t = (now - *t) > prd ? now + prd : *t + prd; // Next expiration time return true; // Expired, return true } void mg_timer_poll(struct mg_timer **head, uint64_t now_ms) { struct mg_timer *t, *tmp; for (t = *head; t != NULL; t = tmp) { bool once = t->expire == 0 && (t->flags & MG_TIMER_RUN_NOW) && !(t->flags & MG_TIMER_CALLED); // Handle MG_TIMER_NOW only once bool expired = mg_timer_expired(&t->expire, t->period_ms, now_ms); tmp = t->next; if (!once && !expired) continue; if ((t->flags & MG_TIMER_REPEAT) || !(t->flags & MG_TIMER_CALLED)) { t->fn(t->arg); } t->flags |= MG_TIMER_CALLED; // If this timer is not repeating and marked AUTODELETE, remove it if (!(t->flags & MG_TIMER_REPEAT) && (t->flags & MG_TIMER_AUTODELETE)) { mg_timer_free(head, t); mg_free(t); } } }