From 0a7f37d720ef8e2a0c6541054e9c01161ba0afb0 Mon Sep 17 00:00:00 2001 From: Alec Berg Date: Thu, 4 Jun 2015 11:45:19 -0700 Subject: core: add task_wait_event_mask() function to other cores Add task_wait_event_mask() function to core/cortex-m0, core/host and board/zinger/runtime in order to delay a task until a specific event occurs. BUG=none BRANCH=smaug TEST=make -j buildall Change-Id: Ic89487fcae5352eec53b745179c7f0d5893ad3e0 Signed-off-by: Alec Berg Reviewed-on: https://chromium-review.googlesource.com/276744 Reviewed-by: Vincent Palatin --- core/cortex-m0/task.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'core/cortex-m0/task.c') diff --git a/core/cortex-m0/task.c b/core/cortex-m0/task.c index d26a82b6d2..1ab82d7d24 100644 --- a/core/cortex-m0/task.c +++ b/core/cortex-m0/task.c @@ -400,6 +400,34 @@ uint32_t task_wait_event(int timeout_us) return __wait_evt(timeout_us, TASK_ID_IDLE); } +uint32_t task_wait_event_mask(uint32_t event_mask, int timeout_us) +{ + uint64_t deadline = get_time().val + timeout_us; + uint32_t events = 0; + int time_remaining_us = timeout_us; + + /* Add the timer event to the mask so we can indicate a timeout */ + event_mask |= TASK_EVENT_TIMER; + + while (!(events & event_mask)) { + /* Collect events to re-post later */ + events |= __wait_evt(time_remaining_us, TASK_ID_IDLE); + + time_remaining_us = deadline - get_time().val; + if (timeout_us > 0 && time_remaining_us <= 0) { + /* Ensure we return a TIMER event if we timeout */ + events |= TASK_EVENT_TIMER; + break; + } + } + + /* Re-post any other events collected */ + if (events & ~event_mask) + atomic_or(¤t_task->events, events & ~event_mask); + + return events & event_mask; +} + void task_enable_irq(int irq) { CPU_NVIC_EN(0) = 1 << irq; -- cgit v1.2.1