xref: /aosp_15_r20/system/chre/platform/embos/system_timer.cc (revision 84e339476a462649f82315436d70fd732297a399)
1*84e33947SAndroid Build Coastguard Worker /*
2*84e33947SAndroid Build Coastguard Worker  * Copyright (C) 2022 The Android Open Source Project
3*84e33947SAndroid Build Coastguard Worker  *
4*84e33947SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*84e33947SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*84e33947SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*84e33947SAndroid Build Coastguard Worker  *
8*84e33947SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*84e33947SAndroid Build Coastguard Worker  *
10*84e33947SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*84e33947SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*84e33947SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*84e33947SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*84e33947SAndroid Build Coastguard Worker  * limitations under the License.
15*84e33947SAndroid Build Coastguard Worker  */
16*84e33947SAndroid Build Coastguard Worker #include <algorithm>
17*84e33947SAndroid Build Coastguard Worker #include <cinttypes>
18*84e33947SAndroid Build Coastguard Worker 
19*84e33947SAndroid Build Coastguard Worker #include "chre/platform/fatal_error.h"
20*84e33947SAndroid Build Coastguard Worker #include "chre/platform/log.h"
21*84e33947SAndroid Build Coastguard Worker #include "chre/platform/system_timer.h"
22*84e33947SAndroid Build Coastguard Worker #include "chre/util/time.h"
23*84e33947SAndroid Build Coastguard Worker 
24*84e33947SAndroid Build Coastguard Worker namespace chre {
25*84e33947SAndroid Build Coastguard Worker 
invokeCallback(void * instance)26*84e33947SAndroid Build Coastguard Worker void SystemTimerBase::invokeCallback(void *instance) {
27*84e33947SAndroid Build Coastguard Worker   auto *timer = static_cast<SystemTimer *>(instance);
28*84e33947SAndroid Build Coastguard Worker   timer->mCallback(timer->mData);
29*84e33947SAndroid Build Coastguard Worker }
30*84e33947SAndroid Build Coastguard Worker 
SystemTimer()31*84e33947SAndroid Build Coastguard Worker SystemTimer::SystemTimer() {}
32*84e33947SAndroid Build Coastguard Worker 
~SystemTimer()33*84e33947SAndroid Build Coastguard Worker SystemTimer::~SystemTimer() {
34*84e33947SAndroid Build Coastguard Worker   // cancel an existing timer if any, and delete the timer instance.
35*84e33947SAndroid Build Coastguard Worker   cancel();
36*84e33947SAndroid Build Coastguard Worker   OS_DeleteTimerEx(&mTimer);
37*84e33947SAndroid Build Coastguard Worker }
38*84e33947SAndroid Build Coastguard Worker 
init()39*84e33947SAndroid Build Coastguard Worker bool SystemTimer::init() {
40*84e33947SAndroid Build Coastguard Worker   constexpr uint32_t kSomeInitialPeriod = 100;
41*84e33947SAndroid Build Coastguard Worker   OS_CreateTimerEx(&mTimer, SystemTimerBase::invokeCallback, kSomeInitialPeriod,
42*84e33947SAndroid Build Coastguard Worker                    this /*context*/);
43*84e33947SAndroid Build Coastguard Worker   return true;
44*84e33947SAndroid Build Coastguard Worker }
45*84e33947SAndroid Build Coastguard Worker 
set(SystemTimerCallback * callback,void * data,Nanoseconds delay)46*84e33947SAndroid Build Coastguard Worker bool SystemTimer::set(SystemTimerCallback *callback, void *data,
47*84e33947SAndroid Build Coastguard Worker                       Nanoseconds delay) {
48*84e33947SAndroid Build Coastguard Worker   // The public EmbOS documentation does not specify how it handles calls to
49*84e33947SAndroid Build Coastguard Worker   // its timer create API if the values lie beyond the specified interval of
50*84e33947SAndroid Build Coastguard Worker   // 1 ≤ Period ≤ 0x7FFFFFFF. Since there's not return value to assess API
51*84e33947SAndroid Build Coastguard Worker   // call success, we clamp the delay to the supported interval.
52*84e33947SAndroid Build Coastguard Worker   // Note that since the EmbOS timer is a millisecond tick timer, an additional
53*84e33947SAndroid Build Coastguard Worker   // delay of 1ms is added to the requested delay to avoid clipping/zeroing
54*84e33947SAndroid Build Coastguard Worker   // during the time factor conversion.
55*84e33947SAndroid Build Coastguard Worker   // TODO(b/237819962): Investigate the possibility of a spare hardware timer
56*84e33947SAndroid Build Coastguard Worker   // available on SLSI that we can eventually switch to.
57*84e33947SAndroid Build Coastguard Worker   constexpr uint64_t kMinDelayMs = 1;
58*84e33947SAndroid Build Coastguard Worker   constexpr uint64_t kMaxDelayMs = INT32_MAX;
59*84e33947SAndroid Build Coastguard Worker   uint64_t delayMs = Milliseconds(delay).getMilliseconds();
60*84e33947SAndroid Build Coastguard Worker   delayMs = std::min(std::max(delayMs + 1, kMinDelayMs), kMaxDelayMs);
61*84e33947SAndroid Build Coastguard Worker 
62*84e33947SAndroid Build Coastguard Worker   OS_StopTimerEx(&mTimer);
63*84e33947SAndroid Build Coastguard Worker   OS_SetTimerPeriodEx(&mTimer, static_cast<OS_TIME>(delayMs));
64*84e33947SAndroid Build Coastguard Worker 
65*84e33947SAndroid Build Coastguard Worker   mCallback = callback;
66*84e33947SAndroid Build Coastguard Worker   mData = data;
67*84e33947SAndroid Build Coastguard Worker 
68*84e33947SAndroid Build Coastguard Worker   OS_RetriggerTimerEx(&mTimer);
69*84e33947SAndroid Build Coastguard Worker   return true;
70*84e33947SAndroid Build Coastguard Worker }
71*84e33947SAndroid Build Coastguard Worker 
72*84e33947SAndroid Build Coastguard Worker // The return value for this function is not guaranteed to be correct - please
73*84e33947SAndroid Build Coastguard Worker // see the note in @ref SystemTimerBase.
cancel()74*84e33947SAndroid Build Coastguard Worker bool SystemTimer::cancel() {
75*84e33947SAndroid Build Coastguard Worker   bool success = false;
76*84e33947SAndroid Build Coastguard Worker   if (isActive()) {
77*84e33947SAndroid Build Coastguard Worker     OS_StopTimerEx(&mTimer);
78*84e33947SAndroid Build Coastguard Worker     success = true;
79*84e33947SAndroid Build Coastguard Worker   }
80*84e33947SAndroid Build Coastguard Worker   return success;
81*84e33947SAndroid Build Coastguard Worker }
82*84e33947SAndroid Build Coastguard Worker 
isActive()83*84e33947SAndroid Build Coastguard Worker bool SystemTimer::isActive() {
84*84e33947SAndroid Build Coastguard Worker   return (OS_GetTimerStatusEx(&mTimer) != 0);
85*84e33947SAndroid Build Coastguard Worker }
86*84e33947SAndroid Build Coastguard Worker 
87*84e33947SAndroid Build Coastguard Worker }  // namespace chre
88