1 /*!
2  * \file      timer.h
3  *
4  * \brief     Timer objects and scheduling management implementation
5  *
6  * \copyright Revised BSD License, see section \ref LICENSE.
7  *
8  * \code
9  *                ______                              _
10  *               / _____)             _              | |
11  *              ( (____  _____ ____ _| |_ _____  ____| |__
12  *               \____ \| ___ |    (_   _) ___ |/ ___)  _ \
13  *               _____) ) ____| | | || |_| ____( (___| | | |
14  *              (______/|_____)_|_|_| \__)_____)\____)_| |_|
15  *              (C)2013-2017 Semtech
16  *
17  * \endcode
18  *
19  * \author    Miguel Luis ( Semtech )
20  *
21  * \author    Gregory Cristian ( Semtech )
22  */
23 #ifndef __TIMER_H__
24 #define __TIMER_H__
25 
26 #ifdef __cplusplus
27 extern "C"
28 {
29 #endif
30 
31 #include <stddef.h>
32 #include <stdbool.h>
33 #include <stdint.h>
34 
35 /*!
36  * \brief Timer object description
37  */
38 typedef struct TimerEvent_s
39 {
40     uint32_t Timestamp;                  //! Current timer value
41     uint32_t ReloadValue;                //! Timer delay value
42     bool IsStarted;                      //! Is the timer currently running
43     bool IsNext2Expire;                  //! Is the next timer to expire
44     void ( *Callback )( void* context ); //! Timer IRQ callback function
45     void *Context;                       //! User defined data object pointer to pass back
46     struct TimerEvent_s *Next;           //! Pointer to the next Timer object.
47 }TimerEvent_t;
48 
49 /*!
50  * \brief Timer time variable definition
51  */
52 #ifndef TimerTime_t
53 typedef uint32_t TimerTime_t;
54 #define TIMERTIME_T_MAX                             ( ( uint32_t )~0 )
55 #endif
56 
57 /*!
58  * \brief Initializes the timer object
59  *
60  * \remark TimerSetValue function must be called before starting the timer.
61  *         this function initializes timestamp and reload value at 0.
62  *
63  * \param [IN] obj          Structure containing the timer object parameters
64  * \param [IN] callback     Function callback called at the end of the timeout
65  */
66 void TimerInit( TimerEvent_t *obj, void ( *callback )( void *context ) );
67 
68 /*!
69  * \brief Sets a user defined object pointer
70  *
71  * \param [IN] context User defined data object pointer to pass back
72  *                     on IRQ handler callback
73  */
74 void TimerSetContext( TimerEvent_t *obj, void* context );
75 
76 /*!
77  * Timer IRQ event handler
78  */
79 void TimerIrqHandler( void );
80 
81 /*!
82  * \brief Starts and adds the timer object to the list of timer events
83  *
84  * \param [IN] obj Structure containing the timer object parameters
85  */
86 void TimerStart( TimerEvent_t *obj );
87 
88 /*!
89  * \brief Checks if the provided timer is running
90  *
91  * \param [IN] obj Structure containing the timer object parameters
92  *
93  * \retval status  returns the timer activity status [true: Started,
94  *                                                    false: Stopped]
95  */
96 bool TimerIsStarted( TimerEvent_t *obj );
97 
98 /*!
99  * \brief Stops and removes the timer object from the list of timer events
100  *
101  * \param [IN] obj Structure containing the timer object parameters
102  */
103 void TimerStop( TimerEvent_t *obj );
104 
105 /*!
106  * \brief Resets the timer object
107  *
108  * \param [IN] obj Structure containing the timer object parameters
109  */
110 void TimerReset( TimerEvent_t *obj );
111 
112 /*!
113  * \brief Set timer new timeout value
114  *
115  * \param [IN] obj   Structure containing the timer object parameters
116  * \param [IN] value New timer timeout value
117  */
118 void TimerSetValue( TimerEvent_t *obj, uint32_t value );
119 
120 /*!
121  * \brief Read the current time
122  *
123  * \retval time returns current time
124  */
125 TimerTime_t TimerGetCurrentTime( void );
126 
127 /*!
128  * \brief Return the Time elapsed since a fix moment in Time
129  *
130  * \remark TimerGetElapsedTime will return 0 for argument 0.
131  *
132  * \param [IN] past         fix moment in Time
133  * \retval time             returns elapsed time
134  */
135 TimerTime_t TimerGetElapsedTime( TimerTime_t past );
136 
137 /*!
138  * \brief Computes the temperature compensation for a period of time on a
139  *        specific temperature.
140  *
141  * \param [IN] period Time period to compensate
142  * \param [IN] temperature Current temperature
143  *
144  * \retval Compensated time period
145  */
146 TimerTime_t TimerTempCompensation( TimerTime_t period, float temperature );
147 
148 /*!
149  * \brief Processes pending timer events
150  */
151 void TimerProcess( void );
152 
153 #ifdef __cplusplus
154 }
155 #endif
156 
157 #endif // __TIMER_H__
158