[zephyr] Use native k_uptime_get() for millis_64() (#14350)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
J. Nick Koston
2026-02-27 13:01:09 -07:00
committed by GitHub
parent edd63e3d2d
commit 63e757807e
3 changed files with 12 additions and 13 deletions

View File

@@ -4,7 +4,6 @@
#include <zephyr/drivers/watchdog.h>
#include <zephyr/sys/reboot.h>
#include <zephyr/random/random.h>
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/defines.h"
@@ -17,8 +16,8 @@ static const device *const WDT = DEVICE_DT_GET(DT_ALIAS(watchdog0));
#endif
void yield() { ::k_yield(); }
uint32_t millis() { return k_ticks_to_ms_floor32(k_uptime_ticks()); }
uint64_t millis_64() { return App.scheduler.millis_64_impl_(millis()); }
uint32_t millis() { return static_cast<uint32_t>(millis_64()); }
uint64_t millis_64() { return static_cast<uint64_t>(k_uptime_get()); }
uint32_t micros() { return k_ticks_to_us_floor32(k_uptime_ticks()); }
void delayMicroseconds(uint32_t us) { ::k_usleep(us); }
void delay(uint32_t ms) { ::k_msleep(ms); }

View File

@@ -28,7 +28,7 @@ static constexpr size_t MAX_POOL_SIZE = 5;
// Set to 5 to match the pool size - when we have as many cancelled items as our
// pool can hold, it's time to clean up and recycle them.
static constexpr uint32_t MAX_LOGICALLY_DELETED_ITEMS = 5;
#if !defined(USE_ESP32) && !defined(USE_HOST)
#if !defined(USE_ESP32) && !defined(USE_HOST) && !defined(USE_ZEPHYR)
// Half the 32-bit range - used to detect rollovers vs normal time progression
static constexpr uint32_t HALF_MAX_UINT32 = std::numeric_limits<uint32_t>::max() / 2;
#endif
@@ -475,12 +475,12 @@ void HOT Scheduler::call(uint32_t now) {
if (now_64 - last_print > 2000) {
last_print = now_64;
std::vector<SchedulerItemPtr> old_items;
#if !defined(USE_ESP32) && !defined(USE_HOST) && defined(ESPHOME_THREAD_MULTI_ATOMICS)
#if !defined(USE_ESP32) && !defined(USE_HOST) && !defined(USE_ZEPHYR) && defined(ESPHOME_THREAD_MULTI_ATOMICS)
const auto last_dbg = this->last_millis_.load(std::memory_order_relaxed);
const auto major_dbg = this->millis_major_.load(std::memory_order_relaxed);
ESP_LOGD(TAG, "Items: count=%zu, pool=%zu, now=%" PRIu64 " (%" PRIu16 ", %" PRIu32 ")", this->items_.size(),
this->scheduler_item_pool_.size(), now_64, major_dbg, last_dbg);
#elif !defined(USE_ESP32) && !defined(USE_HOST)
#elif !defined(USE_ESP32) && !defined(USE_HOST) && !defined(USE_ZEPHYR)
ESP_LOGD(TAG, "Items: count=%zu, pool=%zu, now=%" PRIu64 " (%" PRIu16 ", %" PRIu32 ")", this->items_.size(),
this->scheduler_item_pool_.size(), now_64, this->millis_major_, this->last_millis_);
#else
@@ -714,7 +714,7 @@ bool HOT Scheduler::cancel_item_locked_(Component *component, NameType name_type
return total_cancelled > 0;
}
#if !defined(USE_ESP32) && !defined(USE_HOST)
#if !defined(USE_ESP32) && !defined(USE_HOST) && !defined(USE_ZEPHYR)
uint64_t Scheduler::millis_64_impl_(uint32_t now) {
// THREAD SAFETY NOTE:
// This function has three implementations, based on the precompiler flags
@@ -872,7 +872,7 @@ uint64_t Scheduler::millis_64_impl_(uint32_t now) {
"No platform threading model defined. One of ESPHOME_THREAD_SINGLE, ESPHOME_THREAD_MULTI_NO_ATOMICS, or ESPHOME_THREAD_MULTI_ATOMICS must be defined."
#endif
}
#endif // !USE_ESP32 && !USE_HOST
#endif // !USE_ESP32 && !USE_HOST && !USE_ZEPHYR
bool HOT Scheduler::SchedulerItem::cmp(const SchedulerItemPtr &a, const SchedulerItemPtr &b) {
// High bits are almost always equal (change only on 32-bit rollover ~49 days)

View File

@@ -287,7 +287,7 @@ class Scheduler {
// On ESP32, ignores now and uses esp_timer_get_time() directly (native 64-bit).
// On non-ESP32, extends now to 64-bit using rollover tracking.
uint64_t millis_64_from_(uint32_t now) {
#if defined(USE_ESP32) || defined(USE_HOST)
#if defined(USE_ESP32) || defined(USE_HOST) || defined(USE_ZEPHYR)
(void) now;
return millis_64();
#else
@@ -295,7 +295,7 @@ class Scheduler {
#endif
}
#if !defined(USE_ESP32) && !defined(USE_HOST)
#if !defined(USE_ESP32) && !defined(USE_HOST) && !defined(USE_ZEPHYR)
// On platforms without native 64-bit time, millis_64() HAL function delegates to this
// method which tracks 32-bit millis() rollover using millis_major_ and last_millis_.
friend uint64_t millis_64();
@@ -567,8 +567,8 @@ class Scheduler {
// to synchronize between tasks (see https://github.com/esphome/backlog/issues/52)
std::vector<SchedulerItemPtr> scheduler_item_pool_;
#if !defined(USE_ESP32) && !defined(USE_HOST)
// On platforms with native 64-bit time (ESP32, Host), no rollover tracking needed.
#if !defined(USE_ESP32) && !defined(USE_HOST) && !defined(USE_ZEPHYR)
// On platforms with native 64-bit time (ESP32, Host, Zephyr), no rollover tracking needed.
// On other platforms, these fields track 32-bit millis() rollover for millis_64_impl_().
#ifdef ESPHOME_THREAD_MULTI_ATOMICS
/*
@@ -598,7 +598,7 @@ class Scheduler {
#else /* not ESPHOME_THREAD_MULTI_ATOMICS */
uint16_t millis_major_{0};
#endif /* else ESPHOME_THREAD_MULTI_ATOMICS */
#endif /* !USE_ESP32 && !USE_HOST */
#endif /* !USE_ESP32 && !USE_HOST && !USE_ZEPHYR */
};
} // namespace esphome