1 /**************************************************************************//**
2  * @file     ttmr.h
3  * @version  V1.00
4  * @brief    Tick Timer Controller (TTMR) driver header file
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  * @copyright (C) 2023 Nuvoton Technology Corp. All rights reserved.
8  *****************************************************************************/
9 #ifndef __TTMR_H__
10 #define __TTMR_H__
11 
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16 
17 
18 /** @addtogroup Standard_Driver Standard Driver
19   @{
20 */
21 
22 /** @addtogroup TTMR_Driver TTMR Driver
23   @{
24 */
25 
26 /** @addtogroup TTMR_EXPORTED_CONSTANTS TTMR Exported Constants
27   @{
28 */
29 /*---------------------------------------------------------------------------------------------------------*/
30 /*  TTMR Operation Mode, External Counter and Capture Mode Constant Definitions                           */
31 /*---------------------------------------------------------------------------------------------------------*/
32 #define TTMR_ONESHOT_MODE                   (0UL << TTMR_CTL_OPMODE_Pos)    /*!< TTMR working in one-shot mode \hideinitializer */
33 #define TTMR_PERIODIC_MODE                  (1UL << TTMR_CTL_OPMODE_Pos)    /*!< TTMR working in periodic mode \hideinitializer */
34 #define TTMR_CONTINUOUS_MODE                (3UL << TTMR_CTL_OPMODE_Pos)    /*!< TTMR working in continuous counting mode \hideinitializer */
35 
36 #define TTMR_TRGSRC_TIMEOUT_EVENT           (0UL << TTMR_TRGCTL_TRGSSEL_Pos)    /*!< Select internal trigger source from TTMR time-out event \hideinitializer */
37 
38 #define TTMR_TRGEN                          (TTMR_TRGCTL_TRGEN_Msk)         /*!< Each TTMR event to trigger Low Power IP \hideinitializer */
39 #define TTMR_TRG_TO_LPPDMA                  (TTMR_TRGCTL_TRGLPPDMA_Msk)     /*!< Each TTMR event to trigger LPPDMA transfer \hideinitializer */
40 
41 #define TTMR_TIMEOUT_ERR                    (-1L)                           /*!< TTMR operation abort due to timeout error \hideinitializer */
42 
43 /*@}*/ /* end of group TTMR_EXPORTED_CONSTANTS */
44 
45 
46 /** @addtogroup TTMR_EXPORTED_FUNCTIONS TTMR Exported Functions
47   @{
48 */
49 
50 /**
51   * @brief      Set Timer Compared Value
52   *
53   * @param[in]  ttmr        The pointer of the specified Timer module. It could be TTMR0, TTMR1.
54   * @param[in]  u32Value    Timer compare value. Valid values are between 2 to 0xFFFFFF.
55   *
56   * @return     None
57   *
58   * @details    This macro is used to set ttmr compared value to adjust ttmr time-out interval.
59   * @note       1. Never write 0x0 or 0x1 in this field, or the core will run into unknown state. \n
60   *             2. If update ttmr compared value in continuous counting mode, ttmr counter value will keep counting continuously. \n
61   *                But if ttmr is operating at other modes, the ttmr up counter will restart counting and start from 0.
62   * \hideinitializer
63   */
64 #define TTMR_SET_CMP_VALUE(ttmr, u32Value)          ((ttmr)->CMP = (u32Value))
65 
66 /**
67   * @brief      Set Timer Prescale Value
68   *
69   * @param[in]  ttmr        The pointer of the specified Timer module. It could be TTMR0, TTMR1.
70   * @param[in]  u32Value    Timer prescale value. Valid values are between 0 to 0xFF.
71   *
72   * @return     None
73   *
74   * @details    This macro is used to set ttmr prescale value and ttmr source clock will be divided by (prescale + 1) \n
75   *             before it is fed into ttmr.
76   * \hideinitializer
77   */
78 #define TTMR_SET_PRESCALE_VALUE(ttmr, u32Value)     ((ttmr)->CTL = ((ttmr)->CTL & ~TTMR_CTL_PSC_Msk) | (u32Value))
79 
80 /**
81   * @brief      Check specify Timer Status
82   *
83   * @param[in]  ttmr        The pointer of the specified Timer module. It could be TTMR0, TTMR1.
84   *
85   * @retval     0   Timer 24-bit up counter is inactive
86   * @retval     1   Timer 24-bit up counter is active
87   *
88   * @details    This macro is used to check if specify Timer counter is inactive or active.
89   * \hideinitializer
90   */
91 #define TTMR_IS_ACTIVE(ttmr)                        (((ttmr)->CTL & TTMR_CTL_ACTSTS_Msk)? 1 : 0)
92 
93 
94 /**
95   * @brief      Select Timer operating mode
96   *
97   * @param[in]  ttmr        The pointer of the specified Timer module. It could be TTMR0, TTMR1.
98   * @param[in]  u32OpMode   Operation mode. Possible options are
99   *                         - \ref TTMR_ONESHOT_MODE
100   *                         - \ref TTMR_PERIODIC_MODE
101   *                         - \ref TTMR_CONTINUOUS_MODE
102   *
103   * @return     None
104   * \hideinitializer
105   */
106 #define TTMR_SET_OPMODE(ttmr, u32OpMode)    ((ttmr)->CTL = ((ttmr)->CTL & ~TTMR_CTL_OPMODE_Msk) | (u32OpMode))
107 
108 /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
109 __STATIC_INLINE void     TTMR_Start(TTMR_T *ttmr);
110 __STATIC_INLINE void     TTMR_Stop(TTMR_T *ttmr);
111 __STATIC_INLINE void     TTMR_EnableWakeup(TTMR_T *ttmr);
112 __STATIC_INLINE void     TTMR_DisableWakeup(TTMR_T *ttmr);
113 __STATIC_INLINE void     TTMR_EnableInt(TTMR_T *ttmr);
114 __STATIC_INLINE void     TTMR_DisableInt(TTMR_T *ttmr);
115 __STATIC_INLINE uint32_t TTMR_GetIntFlag(TTMR_T *ttmr);
116 __STATIC_INLINE void     TTMR_ClearIntFlag(TTMR_T *ttmr);
117 __STATIC_INLINE uint32_t TTMR_GetWakeupFlag(TTMR_T *ttmr);
118 __STATIC_INLINE void     TTMR_ClearWakeupFlag(TTMR_T *ttmr);
119 __STATIC_INLINE uint32_t TTMR_GetCounter(TTMR_T *ttmr);
120 
121 /**
122   * @brief      Start Timer Counting
123   *
124   * @param[in]  ttmr        The pointer of the specified Timer module. It could be TTMR0, TTMR1.
125   *
126   * @return     None
127   *
128   * @details    This function is used to start Timer counting.
129   */
TTMR_Start(TTMR_T * ttmr)130 __STATIC_INLINE void TTMR_Start(TTMR_T *ttmr)
131 {
132     ttmr->CTL |= TTMR_CTL_CNTEN_Msk;
133 }
134 
135 /**
136   * @brief      Stop Timer Counting
137   *
138   * @param[in]  ttmr        The pointer of the specified Timer module. It could be TTMR0, TTMR1.
139   *
140   * @return     None
141   *
142   * @details    This function is used to stop/suspend Timer counting.
143   */
TTMR_Stop(TTMR_T * ttmr)144 __STATIC_INLINE void TTMR_Stop(TTMR_T *ttmr)
145 {
146     ttmr->CTL &= ~TTMR_CTL_CNTEN_Msk;
147 }
148 
149 /**
150   * @brief      Enable Timer Interrupt Wake-up Function
151   *
152   * @param[in]  ttmr        The pointer of the specified Timer module. It could be TTMR0, TTMR1.
153   *
154   * @return     None
155   *
156   * @details    This function is used to enable the ttmr interrupt wake-up function and interrupt source could be time-out interrupt, \n
157   *             counter event interrupt or capture trigger interrupt.
158   * @note       To wake the system from Power-down mode, ttmr clock source must be ether LXT or LIRC.
159   */
TTMR_EnableWakeup(TTMR_T * ttmr)160 __STATIC_INLINE void TTMR_EnableWakeup(TTMR_T *ttmr)
161 {
162     ttmr->CTL |= (TTMR_CTL_WKEN_Msk | TTMR_CTL_PDCLKEN_Msk);
163 }
164 
165 /**
166   * @brief      Disable Timer Wake-up Function
167   *
168   * @param[in]  ttmr        The pointer of the specified Timer module. It could be TTMR0, TTMR1.
169   *
170   * @return     None
171   *
172   * @details    This function is used to disable the ttmr interrupt wake-up function.
173   */
TTMR_DisableWakeup(TTMR_T * ttmr)174 __STATIC_INLINE void TTMR_DisableWakeup(TTMR_T *ttmr)
175 {
176     ttmr->CTL &= ~TTMR_CTL_WKEN_Msk;
177 }
178 
179 
180 /**
181   * @brief      Enable Timer Time-out Interrupt
182   *
183   * @param[in]  ttmr        The pointer of the specified Timer module. It could be TTMR0, TTMR1.
184   *
185   * @return     None
186   *
187   * @details    This function is used to enable the ttmr time-out interrupt function.
188   */
TTMR_EnableInt(TTMR_T * ttmr)189 __STATIC_INLINE void TTMR_EnableInt(TTMR_T *ttmr)
190 {
191     ttmr->CTL |= TTMR_CTL_INTEN_Msk;
192 }
193 
194 /**
195   * @brief      Disable Timer Time-out Interrupt
196   *
197   * @param[in]  ttmr        The pointer of the specified Timer module. It could be TTMR0, TTMR1.
198   *
199   * @return     None
200   *
201   * @details    This function is used to disable the ttmr time-out interrupt function.
202   */
TTMR_DisableInt(TTMR_T * ttmr)203 __STATIC_INLINE void TTMR_DisableInt(TTMR_T *ttmr)
204 {
205     ttmr->CTL &= ~TTMR_CTL_INTEN_Msk;
206 }
207 
208 
209 /**
210   * @brief      Get Timer Time-out Interrupt Flag
211   *
212   * @param[in]  ttmr    The pointer of the specified Timer module. It could be TTMR0, TTMR1.
213   *
214   * @retval     0   Timer time-out interrupt did not occur
215   * @retval     1   Timer time-out interrupt occurred
216   *
217   * @details    This function indicates ttmr time-out interrupt occurred or not.
218   */
TTMR_GetIntFlag(TTMR_T * ttmr)219 __STATIC_INLINE uint32_t TTMR_GetIntFlag(TTMR_T *ttmr)
220 {
221     return ((ttmr->INTSTS & TTMR_INTSTS_TIF_Msk) ? 1UL : 0UL);
222 }
223 
224 /**
225   * @brief      Clear Timer Time-out Interrupt Flag
226   *
227   * @param[in]  ttmr        The pointer of the specified Timer module. It could be TTMR0, TTMR1.
228   *
229   * @return     None
230   *
231   * @details    This function clears ttmr time-out interrupt flag to 0.
232   */
TTMR_ClearIntFlag(TTMR_T * ttmr)233 __STATIC_INLINE void TTMR_ClearIntFlag(TTMR_T *ttmr)
234 {
235     ttmr->INTSTS = TTMR_INTSTS_TIF_Msk;
236 }
237 
238 
239 /**
240   * @brief      Get Timer Wake-up Flag
241   *
242   * @param[in]  ttmr    The pointer of the specified Timer module. It could be TTMR0, TTMR1.
243   *
244   * @retval     0   Timer does not cause CPU wake-up
245   * @retval     1   Timer interrupt event cause CPU wake-up
246   *
247   * @details    This function indicates ttmr interrupt event has waked up system or not.
248   */
TTMR_GetWakeupFlag(TTMR_T * ttmr)249 __STATIC_INLINE uint32_t TTMR_GetWakeupFlag(TTMR_T *ttmr)
250 {
251     return (ttmr->INTSTS & TTMR_INTSTS_TWKF_Msk ? 1UL : 0UL);
252 }
253 
254 /**
255   * @brief      Clear Timer Wake-up Flag
256   *
257   * @param[in]  ttmr        The pointer of the specified Timer module. It could be TTMR0, TTMR1.
258   *
259   * @return     None
260   *
261   * @details    This function clears the ttmr wake-up system flag to 0.
262   */
TTMR_ClearWakeupFlag(TTMR_T * ttmr)263 __STATIC_INLINE void TTMR_ClearWakeupFlag(TTMR_T *ttmr)
264 {
265     ttmr->INTSTS = TTMR_INTSTS_TWKF_Msk;
266 }
267 
268 
269 /**
270   * @brief      Get Counter value
271   *
272   * @param[in]  ttmr        The pointer of the specified Timer module. It could be TTMR0, TTMR1.
273   *
274   * @return     24-bit Counter Value
275   *
276   * @details    This function reports the current 24-bit ttmr counter value.
277   */
TTMR_GetCounter(TTMR_T * ttmr)278 __STATIC_INLINE uint32_t TTMR_GetCounter(TTMR_T *ttmr)
279 {
280     return ttmr->CNT;
281 }
282 
283 /**
284   * @brief      Enable Timer Power-down Engine Clock
285   *
286   * @param[in]  ttmr       The pointer of the specified Timer module. It could be TTMR0, TTMR1.
287   *
288   * @return     None
289   *
290   * @details    This function is used to enable the ttmr Power-down Engine Clock.
291   */
TTMR_EnablePDCLK(TTMR_T * ttmr)292 __STATIC_INLINE void TTMR_EnablePDCLK(TTMR_T *ttmr)
293 {
294     ttmr->CTL |= TTMR_CTL_PDCLKEN_Msk;
295 }
296 
297 /**
298   * @brief      Disable Timer Power-down Engine Clock
299   *
300   * @param[in]  ttmr       The pointer of the specified Timer module. It could be TTMR0, TTMR1.
301   *
302   * @return     None
303   *
304   * @details    This function is used to disable the ttmr Power-down Engine Clock.
305   */
TTMR_DisablePDCLK(TTMR_T * ttmr)306 __STATIC_INLINE void TTMR_DisablePDCLK(TTMR_T *ttmr)
307 {
308     ttmr->CTL &= ~TTMR_CTL_PDCLKEN_Msk;
309 }
310 
311 uint32_t TTMR_Open(TTMR_T *ttmr, uint32_t u32Mode, uint32_t u32Freq);
312 void     TTMR_Close(TTMR_T *ttmr);
313 int32_t  TTMR_Delay(TTMR_T *ttmr, uint32_t u32Usec);
314 uint32_t TTMR_GetModuleClock(TTMR_T *ttmr);
315 void     TTMR_SetTriggerTarget(TTMR_T *ttmr, uint32_t u32Mask);
316 int32_t  TTMR_ResetCounter(TTMR_T *ttmr);
317 
318 /*@}*/ /* end of group TTMR_EXPORTED_FUNCTIONS */
319 
320 /*@}*/ /* end of group TTMR_Driver */
321 
322 /*@}*/ /* end of group Standard_Driver */
323 
324 #ifdef __cplusplus
325 }
326 #endif
327 
328 #endif /* __TTMR_H__ */
329