1 /**
2  ****************************************************************************************
3  *
4  * @file ke_timer.h
5  *
6  * @brief This file contains the definitions used for timer management
7  *
8  * Copyright (C) RivieraWaves 2009-2015
9  *
10  *
11  ****************************************************************************************
12  */
13 
14 #ifndef _KE_TIMER_H_
15 #define _KE_TIMER_H_
16 
17 /**
18  ****************************************************************************************
19  * @defgroup TIMER BT Time
20  * @ingroup KERNEL
21  * @brief Timer management module.
22  *
23  * This module implements the functions used for managing kernel timers.
24  *
25  ****************************************************************************************
26  */
27 
28 #include "rwip_config.h"          // stack configuration
29 #include "ke_msg.h"               // messaging definition
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 Set a timer.
76  *
77  * The function first cancel the timer if it is already existing, then
78  * it creates a new one. The timer can be one-shot or periodic, i.e. it
79  * will be automatically set again after each trigger.
80  *
81  * When the timer expires, a message is sent to the task provided as
82  * argument, with the timer id as message id.
83  *
84  * The timer is programmed in time units (TU is 10ms).
85  *
86  * @param[in] timer_id      Timer identifier (message identifier type).
87  * @param[in] task_id       Task identifier which will be notified
88  * @param[in] delay         Delay in time units.
89  ****************************************************************************************
90  */
91 void ke_timer_set(ke_msg_id_t const timer_id, ke_task_id_t const task, uint32_t delay);
92 
93 /**
94  ****************************************************************************************
95  * @brief Remove an registered timer.
96  *
97  * This function search for the timer identified by its id and its task id.
98  * If found it is stopped and freed, otherwise an error message is returned.
99  *
100  * @param[in] timer_id  Timer identifier.
101  * @param[in] task      Task identifier.
102  ****************************************************************************************
103  */
104 void ke_timer_clear(ke_msg_id_t const timerid, ke_task_id_t const task);
105 
106 /**
107  ****************************************************************************************
108  * @brief Checks if a requested timer is active.
109  *
110  * This function pops the first timer from the timer queue and notifies the appropriate
111  * task by sending a kernel message. If the timer is periodic, it is set again;
112  * if it is one-shot, the timer is freed. The function checks also the next timers
113  * and process them if they have expired or are about to expire.
114  ****************************************************************************************
115  */
116 bool ke_timer_active(ke_msg_id_t const timer_id, ke_task_id_t const task_id);
117 
118 /**
119  ****************************************************************************************
120  * @brief Adjust all kernel timers by specified adjustment delay.
121  *
122  * This function updates all timers to align to a new SCLK after a system clock adjust.
123  ****************************************************************************************
124  */
125 void ke_timer_adjust_all(uint32_t delay);
126 
127 
128 #if (DEEP_SLEEP)
129 
130 /**
131  ****************************************************************************************
132  * @brief Get the first timer target (in Slot) used for deep sleep decision
133  *
134  * @return Invalid time if nothing programmed; target time else.
135  ****************************************************************************************
136  */
137 uint32_t ke_timer_target_get(void);
138 
139 #endif //DEEP_SLEEP
140 void ke_timer_clear(ke_msg_id_t const timer_id, ke_task_id_t const task_id);
141 /// @} TIMER
142 
143 #endif // _KE_TIMER_H_
144