1 /**
2  * @file os_timer.h
3  * @author ALLWINNERTECH IOT WLAN Team
4  */
5 
6 /*
7  * Copyright (C) 2017 ALLWINNERTECH TECHNOLOGY CO., LTD. All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *    1. Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *    2. Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in the
16  *       documentation and/or other materials provided with the
17  *       distribution.
18  *    3. Neither the name of ALLWINNERTECH TECHNOLOGY CO., LTD. nor the names of
19  *       its contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef _KERNEL_OS_FREERTOS_OS_TIMER_H_
36 #define _KERNEL_OS_FREERTOS_OS_TIMER_H_
37 
38 #include "_os_common.h"
39 #include "timers.h"
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 #if (defined(configUSE_TIMER_ID_AS_CALLBACK_ARG) && configUSE_TIMER_ID_AS_CALLBACK_ARG == 1)
46 #define OS_TIMER_USE_FREERTOS_ORIG_CALLBACK 0
47 #else
48 #define OS_TIMER_USE_FREERTOS_ORIG_CALLBACK 1
49 #endif
50 
51 /**
52  * @brief Timer type definition
53  *     - one shot timer: Timer will be in the dormant state after it expires.
54  *     - periodic timer: Timer will auto-reload after it expires.
55  */
56 typedef enum {
57     OS_TIMER_ONCE       = 0, /* one shot timer */
58     OS_TIMER_PERIODIC   = 1  /* periodic timer */
59 } OS_TimerType;
60 
61 /** @brief Timer expire callback function definition */
62 typedef void (*OS_TimerCallback_t)(void *arg);
63 
64 /** @brief Timer handle definition */
65 typedef TimerHandle_t OS_TimerHandle_t;
66 
67 #if OS_TIMER_USE_FREERTOS_ORIG_CALLBACK
68 /**
69  * @brief Timer expire callback data definition
70  */
71 typedef struct OS_TimerCallbackData {
72     OS_TimerCallback_t  callback;   /* Timer expire callback function */
73     void                *argument;  /* Argument of timer expire callback function */
74 } OS_TimerCallbackData_t;
75 #endif
76 
77 /**
78  * @brief Timer object definition
79  */
80 typedef struct OS_Timer {
81     TimerHandle_t           handle;
82 #if OS_TIMER_USE_FREERTOS_ORIG_CALLBACK
83     OS_TimerCallbackData_t  *priv;  /* private data for internally usage */
84 #endif
85 } OS_Timer_t;
86 
87 
88 OS_Status OS_TimerCreate(OS_Timer_t *timer, OS_TimerType type,
89                          OS_TimerCallback_t cb, void *arg, OS_Time_t periodMS);
90 OS_Status OS_TimerDelete(OS_Timer_t *timer);
91 OS_Status OS_TimerStart(OS_Timer_t *timer);
92 OS_Status OS_TimerChangePeriod(OS_Timer_t *timer, OS_Time_t periodMS);
93 OS_Status OS_TimerStop(OS_Timer_t *timer);
94 
95 /**
96  * @brief Check whether the timer object is valid or not
97  * @param[in] timer Pointer to the timer object
98  * @return 1 on valid, 0 on invalid
99  */
OS_TimerIsValid(OS_Timer_t * timer)100 static __always_inline int OS_TimerIsValid(OS_Timer_t *timer)
101 {
102     return (timer->handle != OS_INVALID_HANDLE);
103 }
104 
105 /**
106  * @brief Set the timer object to invalid state
107  * @param[in] timer Pointer to the timer object
108  * @return None
109  */
OS_TimerSetInvalid(OS_Timer_t * timer)110 static __always_inline void OS_TimerSetInvalid(OS_Timer_t *timer)
111 {
112     timer->handle = OS_INVALID_HANDLE;
113 }
114 
115 /**
116  * @brief Check whether the timer is active or not
117  *
118  * A timer is inactive when it is in one of the following cases:
119  *     - The timer has been created, but not started.
120  *     - The timer is a one shot timer that has not been restarted since it
121  *       expired.
122  *
123  * @param[in] timer Pointer to the timer object
124  * @return 1 on active, 0 on inactive
125  */
OS_TimerIsActive(OS_Timer_t * timer)126 static __always_inline int OS_TimerIsActive(OS_Timer_t *timer)
127 {
128     return (xTimerIsTimerActive(timer->handle) != pdFALSE);
129 }
130 
131 #ifdef __cplusplus
132 }
133 #endif
134 
135 #endif /* _KERNEL_OS_FREERTOS_OS_TIMER_H_ */
136