• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef _BLE_KE_TIMER_H
16 #define _BLE_KE_TIMER_H
17 
18 /**
19  ****************************************************************************************
20  * @defgroup TIMER BT Time
21  * @ingroup KERNEL
22  * @brief Timer management module.
23  *
24  * This module implements the functions used for managing kernel timers.
25  *
26  ****************************************************************************************
27  */
28 
29 #include "ble_ip_config.h"          // stack configuration
30 
31 
32 /*
33  * DEFINITIONS
34  ****************************************************************************************
35  */
36 
37 ///  Convert timer from second to timer accuracy (10ms)
38 #define KE_TIME_IN_SEC(_time) (_time * 100)
39 
40 
41 /*
42  * TYPE DEFINITIONS
43  ****************************************************************************************
44  */
45 
46 /// Timer Object
47 struct ke_timer
48 {
49     /// next ke timer
50     struct ke_timer *next;
51     /// message identifier
52     ke_msg_id_t     id;
53     /// task identifier
54     ke_task_id_t    task;
55     /// time value
56     uint32_t        time;
57 };
58 
59 
60 /*
61  * FUNCTION PROTOTYPES
62  ****************************************************************************************
63  */
64 
65 
66 /**
67  ****************************************************************************************
68  * @brief Initialize Kernel timer module.
69  ****************************************************************************************
70  */
71 void ke_timer_init(void);
72 
73 /**
74  ****************************************************************************************
75  * @brief Retrieve kernel time.
76  *
77  * @return time value (in Kernel time (10 ms))
78  ****************************************************************************************
79  */
80 uint32_t ke_time(void);
81 
82 /**
83  ****************************************************************************************
84  * @brief Set a timer.
85  *
86  * The function first cancel the timer if it is already existing, then
87  * it creates a new one. The timer can be one-shot or periodic, i.e. it
88  * will be automatically set again after each trigger.
89  *
90  * When the timer expires, a message is sent to the task provided as
91  * argument, with the timer id as message id.
92  *
93  * The timer is programmed in time units (TU is 10ms).
94  *
95  * @param[in] timer_id      Timer identifier (message identifier type).
96  * @param[in] task_id       Task identifier which will be notified
97  * @param[in] delay         Delay in time units.
98  ****************************************************************************************
99  */
100 void ke_timer_set(ke_msg_id_t const timer_id, ke_task_id_t const task, uint32_t delay);
101 
102 /**
103  ****************************************************************************************
104  * @brief Remove an registered timer.
105  *
106  * This function search for the timer identified by its id and its task id.
107  * If found it is stopped and freed, otherwise an error message is returned.
108  *
109  * @param[in] timer_id  Timer identifier.
110  * @param[in] task      Task identifier.
111  ****************************************************************************************
112  */
113 void ke_timer_clear(ke_msg_id_t const timerid, ke_task_id_t const task);
114 
115 /**
116  ****************************************************************************************
117  * @brief Checks if a requested timer is active.
118  *
119  * This function pops the first timer from the timer queue and notifies the appropriate
120  * task by sending a kernel message. If the timer is periodic, it is set again;
121  * if it is one-shot, the timer is freed. The function checks also the next timers
122  * and process them if they have expired or are about to expire.
123  ****************************************************************************************
124  */
125 bool ke_timer_active(ke_msg_id_t const timer_id, ke_task_id_t const task_id);
126 
127 /**
128  ****************************************************************************************
129  * @brief Adjust all kernel timers by specified adjustment delay.
130  *
131  * This function updates all timers to align to a new SCLK after a system clock adjust.
132  ****************************************************************************************
133  */
134 void ke_timer_adjust_all(uint32_t delay);
135 
136 /**
137  ****************************************************************************************
138  * @brief Compute the authorized sleep time until next kernel timer event
139  *
140  * The function takes as argument the current time to compute the sleep time.
141  *
142  * if some timer are programmed it return the sleep time between current time and
143  * first timer expiration.
144  *
145  * If there is no timer programmed, function return false meaning that platform should not
146  * consider the kernel timer to decide the sleep time duration.
147  *
148  * @param[in]     current_time   Current system time
149  * @param[out]    sleep_time     Computed sleep duration
150  *
151  * @return true sleep time has been computed ; false if there is no programed timer,
152            meaning that sleep_time is not relevant and can be ignored.
153  ****************************************************************************************
154  */
155 bool ke_sleep_time_get(uint32_t current_time, int32_t* sleep_time);
156 
157 void ke_timer_reset(void);
158 
159 void ke_timer_10ms_set(uint32_t target);
160 
161 /// @} TIMER
162 
163 #endif // _BLE_KE_TIMER_H
164