1*61c4878aSAndroid Build Coastguard Worker // Copyright 2021 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker //
3*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker // the License at
6*61c4878aSAndroid Build Coastguard Worker //
7*61c4878aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker //
9*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker // the License.
14*61c4878aSAndroid Build Coastguard Worker
15*61c4878aSAndroid Build Coastguard Worker #include "pw_chrono/system_timer.h"
16*61c4878aSAndroid Build Coastguard Worker
17*61c4878aSAndroid Build Coastguard Worker #include <algorithm>
18*61c4878aSAndroid Build Coastguard Worker #include <mutex>
19*61c4878aSAndroid Build Coastguard Worker
20*61c4878aSAndroid Build Coastguard Worker #include "RTOS.h"
21*61c4878aSAndroid Build Coastguard Worker #include "pw_assert/check.h"
22*61c4878aSAndroid Build Coastguard Worker #include "pw_chrono_embos/system_clock_constants.h"
23*61c4878aSAndroid Build Coastguard Worker #include "pw_interrupt/context.h"
24*61c4878aSAndroid Build Coastguard Worker
25*61c4878aSAndroid Build Coastguard Worker namespace pw::chrono {
26*61c4878aSAndroid Build Coastguard Worker namespace {
27*61c4878aSAndroid Build Coastguard Worker
28*61c4878aSAndroid Build Coastguard Worker // Instead of adding targeted locks to each instance, simply use the global
29*61c4878aSAndroid Build Coastguard Worker // recursive critical section lock. Note it has to be recursive because a user
30*61c4878aSAndroid Build Coastguard Worker // callback may use the Invoke* API which in turn needs to grab the lock.
31*61c4878aSAndroid Build Coastguard Worker class RecursiveCriticalSectionLock {
32*61c4878aSAndroid Build Coastguard Worker public:
lock()33*61c4878aSAndroid Build Coastguard Worker void lock() {
34*61c4878aSAndroid Build Coastguard Worker OS_IncDI(); // Mask interrupts.
35*61c4878aSAndroid Build Coastguard Worker OS_SuspendAllTasks(); // Disable task switching.
36*61c4878aSAndroid Build Coastguard Worker }
37*61c4878aSAndroid Build Coastguard Worker
unlock()38*61c4878aSAndroid Build Coastguard Worker void unlock() {
39*61c4878aSAndroid Build Coastguard Worker OS_ResumeAllSuspendedTasks(); // Restore task switching.
40*61c4878aSAndroid Build Coastguard Worker OS_DecRI(); // Restore interrupts.
41*61c4878aSAndroid Build Coastguard Worker }
42*61c4878aSAndroid Build Coastguard Worker };
43*61c4878aSAndroid Build Coastguard Worker RecursiveCriticalSectionLock recursive_global_timer_lock;
44*61c4878aSAndroid Build Coastguard Worker
HandleTimerCallback(void * void_native_system_timer)45*61c4878aSAndroid Build Coastguard Worker void HandleTimerCallback(void* void_native_system_timer) {
46*61c4878aSAndroid Build Coastguard Worker PW_DCHECK(interrupt::InInterruptContext(),
47*61c4878aSAndroid Build Coastguard Worker "HandleTimerCallback must be invoked from an interrupt");
48*61c4878aSAndroid Build Coastguard Worker std::lock_guard lock(recursive_global_timer_lock);
49*61c4878aSAndroid Build Coastguard Worker
50*61c4878aSAndroid Build Coastguard Worker backend::NativeSystemTimer& native_type =
51*61c4878aSAndroid Build Coastguard Worker *static_cast<backend::NativeSystemTimer*>(void_native_system_timer);
52*61c4878aSAndroid Build Coastguard Worker const SystemClock::duration time_until_deadline =
53*61c4878aSAndroid Build Coastguard Worker native_type.expiry_deadline - SystemClock::now();
54*61c4878aSAndroid Build Coastguard Worker if (time_until_deadline <= SystemClock::duration::zero()) {
55*61c4878aSAndroid Build Coastguard Worker // We have met the deadline, execute the user's callback.
56*61c4878aSAndroid Build Coastguard Worker native_type.user_callback(native_type.expiry_deadline);
57*61c4878aSAndroid Build Coastguard Worker return;
58*61c4878aSAndroid Build Coastguard Worker }
59*61c4878aSAndroid Build Coastguard Worker const SystemClock::duration period =
60*61c4878aSAndroid Build Coastguard Worker std::min(pw::chrono::embos::kMaxTimeout, time_until_deadline);
61*61c4878aSAndroid Build Coastguard Worker OS_SetTimerPeriodEx(&native_type.tcb, static_cast<OS_TIME>(period.count()));
62*61c4878aSAndroid Build Coastguard Worker OS_StartTimerEx(&native_type.tcb);
63*61c4878aSAndroid Build Coastguard Worker }
64*61c4878aSAndroid Build Coastguard Worker
65*61c4878aSAndroid Build Coastguard Worker // embOS requires a timer to have a non-zero period.
66*61c4878aSAndroid Build Coastguard Worker constexpr SystemClock::duration kMinTimerPeriod = SystemClock::duration(1);
67*61c4878aSAndroid Build Coastguard Worker constexpr OS_TIME kInvalidPeriod = 0;
68*61c4878aSAndroid Build Coastguard Worker
69*61c4878aSAndroid Build Coastguard Worker } // namespace
70*61c4878aSAndroid Build Coastguard Worker
SystemTimer(ExpiryCallback && callback)71*61c4878aSAndroid Build Coastguard Worker SystemTimer::SystemTimer(ExpiryCallback&& callback)
72*61c4878aSAndroid Build Coastguard Worker : native_type_{.tcb{},
73*61c4878aSAndroid Build Coastguard Worker .expiry_deadline = SystemClock::time_point(),
74*61c4878aSAndroid Build Coastguard Worker .user_callback = std::move(callback)} {
75*61c4878aSAndroid Build Coastguard Worker OS_CreateTimerEx(
76*61c4878aSAndroid Build Coastguard Worker &native_type_.tcb, HandleTimerCallback, kInvalidPeriod, &native_type_);
77*61c4878aSAndroid Build Coastguard Worker }
78*61c4878aSAndroid Build Coastguard Worker
~SystemTimer()79*61c4878aSAndroid Build Coastguard Worker SystemTimer::~SystemTimer() {
80*61c4878aSAndroid Build Coastguard Worker // Not threadsafe by design.
81*61c4878aSAndroid Build Coastguard Worker Cancel();
82*61c4878aSAndroid Build Coastguard Worker OS_DeleteTimerEx(&native_type_.tcb);
83*61c4878aSAndroid Build Coastguard Worker }
84*61c4878aSAndroid Build Coastguard Worker
InvokeAt(SystemClock::time_point timestamp)85*61c4878aSAndroid Build Coastguard Worker void SystemTimer::InvokeAt(SystemClock::time_point timestamp) {
86*61c4878aSAndroid Build Coastguard Worker std::lock_guard lock(recursive_global_timer_lock);
87*61c4878aSAndroid Build Coastguard Worker
88*61c4878aSAndroid Build Coastguard Worker // Ensure the timer has been cancelled first.
89*61c4878aSAndroid Build Coastguard Worker Cancel();
90*61c4878aSAndroid Build Coastguard Worker
91*61c4878aSAndroid Build Coastguard Worker native_type_.expiry_deadline = timestamp;
92*61c4878aSAndroid Build Coastguard Worker const SystemClock::duration time_until_deadline =
93*61c4878aSAndroid Build Coastguard Worker timestamp - SystemClock::now();
94*61c4878aSAndroid Build Coastguard Worker
95*61c4878aSAndroid Build Coastguard Worker // Schedule the timer as far out as possible. Note that the timeout might be
96*61c4878aSAndroid Build Coastguard Worker // clamped and it may be rescheduled internally.
97*61c4878aSAndroid Build Coastguard Worker const SystemClock::duration period = std::clamp(
98*61c4878aSAndroid Build Coastguard Worker kMinTimerPeriod, time_until_deadline, pw::chrono::embos::kMaxTimeout);
99*61c4878aSAndroid Build Coastguard Worker
100*61c4878aSAndroid Build Coastguard Worker OS_SetTimerPeriodEx(&native_type_.tcb, static_cast<OS_TIME>(period.count()));
101*61c4878aSAndroid Build Coastguard Worker OS_RetriggerTimerEx(&native_type_.tcb);
102*61c4878aSAndroid Build Coastguard Worker }
103*61c4878aSAndroid Build Coastguard Worker
104*61c4878aSAndroid Build Coastguard Worker } // namespace pw::chrono
105