xref: /aosp_15_r20/system/chre/platform/tinysys/include/chre/target_platform/system_timer_base.h (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CHRE_PLATFORM_TINYSYS_SYSTEM_TIMER_BASE_H_
18 #define CHRE_PLATFORM_TINYSYS_SYSTEM_TIMER_BASE_H_
19 
20 #include <cinttypes>
21 #include <csignal>
22 #include <ctime>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #include "FreeRTOS.h"
29 #include "sensorhub/rt_timer.h"
30 #include "task.h"
31 
32 #ifdef __cplusplus
33 }  // extern "C"
34 #endif
35 
36 namespace chre {
37 
38 class SystemTimerBase {
39  protected:
40   /** Stack size (in words) of the timer callback runner task */
41   static constexpr uint32_t kStackDepthWords = 0x200;  // 2K stack size
42 
43   /** Priority of the callback runner task */
44 #ifdef PRI_CHRE_SYS_TIMER
45   static constexpr UBaseType_t kTaskPriority = PRI_CHRE_SYS_TIMER;
46 #else
47   static constexpr UBaseType_t kTaskPriority = tskIDLE_PRIORITY + 4;
48 #endif
49 
50   /** Name of the callback runner task */
51   static constexpr char kTaskName[] = "ChreTimerCbRunner";
52 
53   /**
54    * Callback function woken up by the timer, which in turn wakes up the
55    * callback runner task
56    */
57   static void rtTimerCallback(struct rt_timer *timer);
58 
59   /**
60    * The callback runner task that blocks until it gets woken up by the timer
61    * callback.
62    *
63    * This task runs the actual callback set by the user as the timer callback is
64    * running from the ISR.
65    *
66    * @param context a raw pointer that will be casted to a pointer to
67    * SystemTimer.
68    */
69 
70   static void callbackRunner(void *context);
71 
72   /** A FreeRTOS task handle holding the callback runner task */
73   TaskHandle_t mCallbackRunnerHandle = nullptr;
74 
75   /** Tracks whether the timer has been initialized correctly. */
76   bool mInitialized = false;
77 
78   /** The properties of the timer including callback, data, etc. */
79   struct rt_timer rtSystemTimer;
80 };
81 }  // namespace chre
82 #endif  // CHRE_PLATFORM_TINYSYS_SYSTEM_TIMER_BASE_H_
83