move timer stuff out of sntp/util

This commit is contained in:
Sergio R. Caprile committed 2026-11-30 12:41:06 -03:00
1 parent 3322a119eb
commit 48a6b0d422
10 files changed
+184 -180

No files matched your search

+61
View File
@@ -2,6 +2,67 @@
#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;