1 /*
2 * Copyright (c) 2016, Freescale Semiconductor, Inc.
3 * Copyright 2016-2020 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8 #ifndef _FSL_RIT_H_
9 #define _FSL_RIT_H_
10
11 #include "fsl_common.h"
12
13 /*!
14 * @addtogroup rit
15 * @{
16 */
17
18 /*******************************************************************************
19 * Definitions
20 ******************************************************************************/
21
22 /*! @name Driver version */
23 /*@{*/
24 #define FSL_RIT_DRIVER_VERSION (MAKE_VERSION(2, 1, 1)) /*!< Version 2.1.1 */
25 /*@}*/
26
27 /*! @brief List of RIT status flags */
28 typedef enum _rit_status_flags
29 {
30 kRIT_TimerFlag = RIT_CTRL_RITINT_MASK, /*!< Timer flag */
31 } rit_status_flags_t;
32
33 /*!
34 * @brief RIT config structure
35 *
36 * This structure holds the configuration settings for the RIT peripheral. To initialize this
37 * structure to reasonable defaults, call the RIT_GetDefaultConfig() function and pass a
38 * pointer to your config structure instance.
39 *
40 * The config struct can be made const so it resides in flash
41 */
42 typedef struct _rit_config
43 {
44 bool enableRunInDebug; /*!< true: The timer is halted when the processor is halted for debugging.; false: Debug has
45 no effect on the timer operation. */
46 } rit_config_t;
47
48 /*******************************************************************************
49 * API
50 ******************************************************************************/
51
52 #if defined(__cplusplus)
53 extern "C" {
54 #endif
55
56 /*!
57 * @name Initialization and deinitialization
58 * @{
59 */
60
61 /*!
62 * @brief Ungates the RIT clock, enables the RIT module, and configures the peripheral for basic operations.
63 *
64 * @note This API should be called at the beginning of the application using the RIT driver.
65 *
66 * @param base RIT peripheral base address
67 * @param config Pointer to the user's RIT config structure
68 */
69 void RIT_Init(RIT_Type *base, const rit_config_t *config);
70
71 /*!
72 * @brief Gates the RIT clock and disables the RIT module.
73 *
74 * @param base RIT peripheral base address
75 */
76 void RIT_Deinit(RIT_Type *base);
77
78 /*!
79 * @brief Fills in the RIT configuration structure with the default settings.
80 *
81 * The default values are as follows.
82 * @code
83 * config->enableRunInDebug = false;
84 * @endcode
85 * @param config Pointer to the onfiguration structure.
86 */
87 void RIT_GetDefaultConfig(rit_config_t *config);
88
89 /*! @}*/
90
91 /*!
92 * @name Status Interface
93 * @{
94 */
95
96 /*!
97 * @brief Gets the RIT status flags.
98 *
99 * @param base RIT peripheral base address
100 *
101 * @return The status flags. This is the logical OR of members of the
102 * enumeration ::rit_status_flags_t
103 */
RIT_GetStatusFlags(RIT_Type * base)104 static inline uint32_t RIT_GetStatusFlags(RIT_Type *base)
105 {
106 return (base->CTRL);
107 }
108
109 /*!
110 * @brief Clears the RIT status flags.
111 *
112 * @param base RIT peripheral base address
113 * @param mask The status flags to clear. This is a logical OR of members of the
114 * enumeration ::rit_status_flags_t
115 */
RIT_ClearStatusFlags(RIT_Type * base,uint32_t mask)116 static inline void RIT_ClearStatusFlags(RIT_Type *base, uint32_t mask)
117 {
118 base->CTRL |= mask;
119 }
120
121 /*! @}*/
122
123 /*!
124 * @name Read and Write the timer period
125 * @{
126 */
127
128 /*!
129 * @brief Sets the timer period in units of count.
130 *
131 * This function sets the RI compare value. If the counter value equals to the compare value,
132 * it will generate an interrupt.
133 *
134 * @note Users can call the utility macros provided in fsl_common.h to convert to ticks
135 *
136 * @param base RIT peripheral base address
137 * @param count Timer period in units of ticks
138 */
139 void RIT_SetTimerCompare(RIT_Type *base, uint64_t count);
140
141 /*!
142 * @brief Sets the mask bit of count compare.
143 *
144 * This function sets the RI mask value. A 1 written to any bit will force the compare to
145 * be true for the corresponding bit of the counter and compare register (causes the comparison of
146 * the register bits to be always true).
147 *
148 * @note Users can call the utility macros provided in fsl_common.h to convert to ticks
149 *
150 * @param base RIT peripheral base address
151 * @param count Timer period in units of ticks
152 */
153 void RIT_SetMaskBit(RIT_Type *base, uint64_t count);
154
155 /*!
156 * @brief Reads the current value of compare register.
157 *
158 * @note Users can call the utility macros provided in fsl_common.h to convert ticks to usec or msec
159 *
160 * @param base RIT peripheral base address
161 *
162 * @return Current RI compare value
163 */
164 uint64_t RIT_GetCompareTimerCount(RIT_Type *base);
165
166 /*!
167 * @brief Reads the current timer counting value of counter register.
168 *
169 * This function returns the real-time timer counting value, in a range from 0 to a
170 * timer period.
171 *
172 * @note Users can call the utility macros provided in fsl_common.h to convert ticks to usec or msec
173 *
174 * @param base RIT peripheral base address
175 *
176 * @return Current timer counting value in ticks
177 */
178 uint64_t RIT_GetCounterTimerCount(RIT_Type *base);
179
180 /*!
181 * @brief Reads the current value of mask register.
182 *
183 * @note Users can call the utility macros provided in fsl_common.h to convert ticks to usec or msec
184 *
185 * @param base RIT peripheral base address
186 *
187 * @return Current RI mask value
188 */
189 uint64_t RIT_GetMaskTimerCount(RIT_Type *base);
190
191 /*! @}*/
192
193 /*!
194 * @name Timer Start and Stop
195 * @{
196 */
197
198 /*!
199 * @brief Starts the timer counting.
200 *
201 * After calling this function, timers load initial value(0U), count up to desired value or over-flow
202 * then the counter will count up again.
203 *
204 * @param base RIT peripheral base address
205 */
RIT_StartTimer(RIT_Type * base)206 static inline void RIT_StartTimer(RIT_Type *base)
207 {
208 base->CTRL |= RIT_CTRL_RITEN_MASK;
209 }
210
211 /*!
212 * @brief Stops the timer counting.
213 *
214 * This function stop timer counting. Timer reload their new value
215 * after the next time they call the RIT_StartTimer.
216 *
217 * @param base RIT peripheral base address
218 */
RIT_StopTimer(RIT_Type * base)219 static inline void RIT_StopTimer(RIT_Type *base)
220 {
221 /* Disable RIT timers */
222 base->CTRL &= ~RIT_CTRL_RITEN_MASK;
223 }
224
225 /*! @}*/
226 /*!
227 * @brief Sets the Timer Counter auto clear or not
228 *
229 * This function set the counter auto clear or not whenever the counter value equals the masked
230 * compare value specified by the contents of COMPVAL/COMPVAL_H and MASK/MASK_H registers..
231
232 * @deprecated Do not use this function. It has been superceded by @ref RIT_SetCountAutoClear.
233 */
RIT_ClearCounter(RIT_Type * base,bool enable)234 static inline void RIT_ClearCounter(RIT_Type *base, bool enable)
235 {
236 if (enable)
237 {
238 base->CTRL |= RIT_CTRL_RITENCLR_MASK;
239 }
240 else
241 {
242 base->CTRL &= ~RIT_CTRL_RITENCLR_MASK;
243 }
244 }
245
246 /*!
247 * @brief Sets the Timer Counter auto clear or not
248 *
249 * This function set the counter auto clear or not whenever the counter value equals the masked
250 * compare value specified by the contents of COMPVAL/COMPVAL_H and MASK/MASK_H registers..
251 *
252 * @param base RIT peripheral base address
253 * @param enable Enable/disable Counter auto clear when value equals the compare value.
254 * - true: Enable Counter auto clear.
255 * - false: Disable Counter auto clear.
256 */
RIT_SetCountAutoClear(RIT_Type * base,bool enable)257 static inline void RIT_SetCountAutoClear(RIT_Type *base, bool enable)
258 {
259 if (enable)
260 {
261 base->CTRL |= RIT_CTRL_RITENCLR_MASK;
262 }
263 else
264 {
265 base->CTRL &= ~RIT_CTRL_RITENCLR_MASK;
266 }
267 }
268
269 #if defined(__cplusplus)
270 }
271 #endif
272
273 /*! @}*/
274
275 #endif /* _FSL_RIT_H_ */
276