1 /*
2  * Copyright 2018-2020 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 #ifndef _FSL_OSTIMER_H_
8 #define _FSL_OSTIMER_H_
9 
10 #include "fsl_common.h"
11 
12 /*!
13  * @addtogroup ostimer
14  * @{
15  */
16 
17 /*! @file*/
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 
23 /*! @name Driver version */
24 /*@{*/
25 /*! @brief OSTIMER driver version. */
26 #define FSL_OSTIMER_DRIVER_VERSION (MAKE_VERSION(2, 1, 1))
27 /*@}*/
28 
29 /*!
30  * @brief OSTIMER status flags.
31  */
32 enum _ostimer_flags
33 {
34     kOSTIMER_MatchInterruptFlag = (OSTIMER_OSEVENT_CTRL_OSTIMER_INTRFLAG_MASK), /*!< Match interrupt flag bit, sets if
35                                                                                    the match value was reached. */
36 };
37 
38 /*! @brief ostimer callback function. */
39 typedef void (*ostimer_callback_t)(void);
40 
41 /*******************************************************************************
42  * API
43  ******************************************************************************/
44 
45 #if defined(__cplusplus)
46 extern "C" {
47 #endif /* _cplusplus */
48 
49 /*!
50  * @name Initialization and deinitialization
51  * @{
52  */
53 
54 /*!
55  * @brief Initializes an OSTIMER by turning its bus clock on
56  *
57  */
58 void OSTIMER_Init(OSTIMER_Type *base);
59 
60 /*!
61  * @brief Deinitializes a OSTIMER instance.
62  *
63  * This function shuts down OSTIMER bus clock
64  *
65  * @param base OSTIMER peripheral base address.
66  */
67 void OSTIMER_Deinit(OSTIMER_Type *base);
68 
69 /*!
70  * @brief OSTIMER software reset.
71  *
72  * This function will use software to trigger an OSTIMER reset.
73  * Please note that, the OS timer reset bit was in PMC->OSTIMERr register.
74  *
75  * @param base OSTIMER peripheral base address.
76  */
OSTIMER_SoftwareReset(OSTIMER_Type * base)77 static inline void OSTIMER_SoftwareReset(OSTIMER_Type *base)
78 {
79 #if !(defined(FSL_FEATURE_PMC_HAS_NO_OSTIMER_REG) && FSL_FEATURE_PMC_HAS_NO_OSTIMER_REG)
80     PMC->OSTIMERr |= PMC_OSTIMER_SOFTRESET_MASK;
81     PMC->OSTIMERr &= ~PMC_OSTIMER_SOFTRESET_MASK;
82 #endif
83 }
84 
85 /*!
86  * @brief Get OSTIMER status Flags.
87  *
88  * This returns the status flag.
89  * Currently, only match interrupt flag can be got.
90  *
91  * @param base OSTIMER peripheral base address.
92  * @return status register value
93  */
94 uint32_t OSTIMER_GetStatusFlags(OSTIMER_Type *base);
95 
96 /*!
97  * @brief Clear Status Interrupt Flags.
98  *
99  * This clears intrrupt status flag.
100  * Currently, only match interrupt flag can be cleared.
101  *
102  * @param base OSTIMER peripheral base address.
103  * @param mask Clear bit mask.
104  * @return none
105  */
106 void OSTIMER_ClearStatusFlags(OSTIMER_Type *base, uint32_t mask);
107 
108 /*!
109  * @brief Set the match raw value for OSTIMER.
110  *
111  * This function will set a match value for OSTIMER with an optional callback. And this callback
112  * will be called while the data in dedicated pair match register is equals to the value of central EVTIMER.
113  * Please note that, the data format is gray-code, if decimal data was desired, please using OSTIMER_SetMatchValue().
114  *
115  * @param base   OSTIMER peripheral base address.
116  * @param count  OSTIMER timer match value.(Value is gray-code format)
117  *
118  * @param cb     OSTIMER callback (can be left as NULL if none, otherwise should be a void func(void)).
119  * @retval kStatus_Success - Set match raw value and enable interrupt Successfully.
120  * @retval kStatus_Fail    - Set match raw value fail.
121  */
122 status_t OSTIMER_SetMatchRawValue(OSTIMER_Type *base, uint64_t count, ostimer_callback_t cb);
123 
124 /*!
125  * @brief Set the match value for OSTIMER.
126  *
127  * This function will set a match value for OSTIMER with an optional callback. And this callback
128  * will be called while the data in dedicated pair match register is equals to the value of central OS TIMER.
129  *
130  * @param base   OSTIMER peripheral base address.
131  * @param count  OSTIMER timer match value.(Value is decimal format, and this value will be translate to Gray code
132  * internally.)
133  *
134  * @param cb     OSTIMER callback (can be left as NULL if none, otherwise should be a void func(void)).
135  * @retval kStatus_Success - Set match value and enable interrupt Successfully.
136  * @retval kStatus_Fail    - Set match value fail.
137  */
138 status_t OSTIMER_SetMatchValue(OSTIMER_Type *base, uint64_t count, ostimer_callback_t cb);
139 
140 /*!
141  * @brief Get current timer raw count value from OSTIMER.
142  *
143  * This function will get a gray code type timer count value from OS timer register.
144  * The raw value of timer count is gray code format.
145  *
146  * @param base   OSTIMER peripheral base address.
147  * @return       Raw value of OSTIMER, gray code format.
148  */
OSTIMER_GetCurrentTimerRawValue(OSTIMER_Type * base)149 static inline uint64_t OSTIMER_GetCurrentTimerRawValue(OSTIMER_Type *base)
150 {
151     uint64_t tmp = 0U;
152 
153     tmp = base->EVTIMERL;
154     tmp |= (uint64_t)(base->EVTIMERH) << 32U;
155 
156     return tmp;
157 }
158 
159 /*!
160  * @brief Get current timer count value from OSTIMER.
161  *
162  * This function will get a decimal timer count value.
163  * The RAW value of timer count is gray code format, will be translated to decimal data internally.
164  *
165  * @param base   OSTIMER peripheral base address.
166  * @return       Value of OSTIMER which will be formated to decimal value.
167  */
168 uint64_t OSTIMER_GetCurrentTimerValue(OSTIMER_Type *base);
169 
170 /*!
171  * @brief Get the capture value from OSTIMER.
172  *
173  * This function will get a captured gray-code value from OSTIMER.
174  * The Raw value of timer capture is gray code format.
175  *
176  * @param base   OSTIMER peripheral base address.
177  * @return       Raw value of capture register, data format is gray code.
178  */
OSTIMER_GetCaptureRawValue(OSTIMER_Type * base)179 static inline uint64_t OSTIMER_GetCaptureRawValue(OSTIMER_Type *base)
180 {
181     uint64_t tmp = 0U;
182 
183     tmp = base->CAPTURE_L;
184     tmp |= (uint64_t)(base->CAPTURE_H) << 32U;
185 
186     return tmp;
187 }
188 
189 /*!
190  * @brief Get the capture value from OSTIMER.
191  *
192  * This function will get a capture decimal-value from OSTIMER.
193  * The RAW value of timer capture is gray code format, will be translated to decimal data internally.
194  *
195  * @param base   OSTIMER peripheral base address.
196  * @return Value of capture register, data format is decimal.
197  */
198 uint64_t OSTIMER_GetCaptureValue(OSTIMER_Type *base);
199 
200 /*!
201  * @brief OS timer interrupt Service Handler.
202  *
203  * This function handles the interrupt and refers to the callback array in the driver to callback user (as per request
204  * in OSTIMER_SetMatchValue()).
205  * if no user callback is scheduled, the interrupt will simply be cleared.
206  *
207  * @param base   OS timer peripheral base address.
208  * @param cb     callback scheduled for this instance of OS timer
209  * @return       none
210  */
211 void OSTIMER_HandleIRQ(OSTIMER_Type *base, ostimer_callback_t cb);
212 /* @} */
213 
214 #if defined(__cplusplus)
215 }
216 #endif
217 
218 /*! @}*/
219 
220 #endif /* _FSL_OSTIMER_H_ */
221