Skip to content

Feature/zephyr compat/k timer isr dispatch#12

Merged
swoisz merged 5 commits into
mainfrom
feature/zephyr-compat/k-timer-isr-dispatch
May 11, 2026
Merged

Feature/zephyr compat/k timer isr dispatch#12
swoisz merged 5 commits into
mainfrom
feature/zephyr-compat/k-timer-isr-dispatch

Conversation

@swoisz

@swoisz swoisz commented May 7, 2026

Copy link
Copy Markdown
Collaborator

Align ISR dispatch ability. Previously all k_timer backend cbs were in the esp_timer task ctx. This is more zephyr-like.

@swoisz
swoisz requested a review from Copilot May 7, 2026 17:22
@swoisz swoisz self-assigned this May 7, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes Boreas’ k_timer behavior more Zephyr-compatible by optionally dispatching timer expiry callbacks in true ISR context (via ESP_TIMER_ISR) instead of the esp_timer task, and updates docs/examples/tests accordingly.

Changes:

  • Add CONFIG_K_TIMER_DISPATCH_ISR (default y) and switch k_timer to use ESP_TIMER_ISR when enabled.
  • Update internal users of k_timer (e.g., watchdog supervisor, delayable work, thread delayed start) to accommodate ISR-dispatch semantics.
  • Add ISR-dispatch-focused unit tests and update the work-queue demo to defer logging out of ISR context.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
test/sdkconfig.defaults Enables ISR-dispatch config for the test app.
test/main/test_k_timer.c Adds ISR-dispatch validation tests and marks callbacks IRAM_ATTR.
examples/work_queue_demo/main/main.c Demonstrates deferring timer ISR work to k_work before logging.
components/zsys/src/watchdog.c Defers watchdog timer expiry work via k_work to avoid ISR-heavy logic.
components/zkernel/src/k_work.c Marks delayable-work timer expiry callback IRAM_ATTR under ISR dispatch.
components/zkernel/src/k_timer.c Switches esp_timer dispatch method to ISR when configured.
components/zkernel/src/k_thread.c Adapts delayed thread start to safely resume from ISR-dispatched timer expiry.
components/zkernel/Kconfig Introduces K_TIMER_DISPATCH_ISR option (default enabled).
components/zkernel/include/boreas/zephyr/kernel.h Updates timer callback-context documentation; inlines user-data helpers.
Comments suppressed due to low confidence (1)

components/zkernel/src/k_work.c:342

  • k_work_delayable_timer_expiry() is marked IRAM_ATTR under CONFIG_K_TIMER_DISPATCH_ISR, but it calls k_work_submit*/k_work_submit_to_queue, which are not IRAM_ATTR and may reside in flash. This undermines the stated “IRAM-only” requirement for ISR-dispatched timer callbacks and can crash when flash cache is disabled. Either ensure the work submission path (and its dependencies) is IRAM-safe under this config, or defer the submission to task context (e.g., via a pended function call).
#ifdef CONFIG_K_TIMER_DISPATCH_ISR
static void IRAM_ATTR k_work_delayable_timer_expiry(struct k_timer *timer)
#else
static void k_work_delayable_timer_expiry(struct k_timer *timer)
#endif
{
	struct k_work_delayable *dwork = CONTAINER_OF(timer, struct k_work_delayable, timer);
	if (dwork->queue) {
		k_work_submit_to_queue(dwork->queue, &dwork->work);
	} else {
		k_work_submit(&dwork->work);
	}
}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread components/zkernel/src/k_thread.c Outdated
Comment thread components/zsys/src/watchdog.c Outdated
Comment thread components/zkernel/include/boreas/zephyr/kernel.h
Comment thread test/main/test_k_timer.c Outdated
Comment thread examples/work_queue_demo/main/main.c
Comment thread components/zkernel/src/k_timer.c
Comment thread components/zkernel/src/k_timer.c

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (1)

components/zkernel/src/k_timer.c:88

  • With ISR dispatch enabled, esp_timer_stop() does not wait for an in-flight callback to finish, but k_timer_start() proceeds to reset/update shared fields (status, first_interval_pending, is_periodic, period_us, etc.). This can race with k_timer_esp_callback() running on another core and, in particular, period_us is a non-atomic 64-bit value that may be torn. To match Zephyr’s restart/stop semantics safely under ESP_TIMER_ISR, add proper synchronization (e.g., a per-timer portMUX/spinlock used by both the ISR callback and start/stop paths, and/or an in_callback flag that start/stop waits out in task context).
	/* Stop any in-flight timer. With ESP_TIMER_TASK dispatch,
	 * esp_timer_stop blocks until the callback finishes; with
	 * ESP_TIMER_ISR dispatch it only removes the timer from the
	 * armed list (the ISR callback may still be executing on
	 * another core). Callers must not restart a timer whose
	 * callback is still running — same constraint as upstream Zephyr. */
	if (__atomic_load_n(&timer->running, __ATOMIC_ACQUIRE)) {
		esp_timer_stop(timer->handle);
	}

	__atomic_store_n(&timer->status, 0, __ATOMIC_RELAXED);
	__atomic_store_n(&timer->first_interval_pending, false, __ATOMIC_RELAXED);
	__atomic_store_n(&timer->is_periodic, !k_timeout_is_no_wait(period), __ATOMIC_RELEASE);

Comment thread components/zkernel/src/k_timer.c Outdated
Comment thread components/zkernel/src/k_thread.c Outdated
Comment thread components/zkernel/include/boreas/zephyr/kernel.h Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.

Comment on lines 116 to 134
@@ -133,15 +134,15 @@ static int k_work_submit_internal(struct k_work_q *queue, struct k_work *work)
return 0;
Comment on lines +49 to +53
static void K_ISR_SAFE supervisor_check(struct k_timer *timer)
{
(void)timer;
k_work_submit(&supervisor_work);
}
Comment on lines +419 to +424
static inline void k_timer_user_data_set(struct k_timer *timer, void *user_data)
{
timer->user_data = user_data;
}

static inline void *k_timer_user_data_get(const struct k_timer *timer)
@swoisz
swoisz merged commit a27fc31 into main May 11, 2026
1 check passed
@swoisz
swoisz deleted the feature/zephyr-compat/k-timer-isr-dispatch branch June 11, 2026 22:23
swoisz added a commit that referenced this pull request Jul 2, 2026
…-isr-dispatch

Feature/zephyr compat/k timer isr dispatch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants