1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  *   of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  *   list of conditions and the following disclaimer in the documentation and/or
13  *   other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  *   contributors may be used to endorse or promote products derived from this
17  *   software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef __EPIT_H__
32 #define __EPIT_H__
33 
34 #include <stdint.h>
35 #include <stdbool.h>
36 #include <assert.h>
37 #include "device_imx.h"
38 
39 /*!
40  * @addtogroup epit_driver
41  * @{
42  */
43 
44 /*******************************************************************************
45  * Definitions
46  ******************************************************************************/
47 
48 /*! @brief Clock source. */
49 enum _epit_clock_source
50 {
51     epitClockSourceOff      = 0U, /*!< EPIT Clock Source Off.*/
52     epitClockSourcePeriph   = 1U, /*!< EPIT Clock Source from Peripheral Clock.*/
53     epitClockSourceHighFreq = 2U, /*!< EPIT Clock Source from High Frequency Reference Clock.*/
54     epitClockSourceLowFreq  = 3U, /*!< EPIT Clock Source from Low Frequency Reference Clock.*/
55 };
56 
57 /*! @brief Output compare operation mode. */
58 enum _epit_output_operation_mode
59 {
60     epitOutputOperationDisconnected = 0U, /*!< EPIT Output Operation: Disconnected from pad.*/
61     epitOutputOperationToggle       = 1U, /*!< EPIT Output Operation: Toggle output pin.*/
62     epitOutputOperationClear        = 2U, /*!< EPIT Output Operation: Clear output pin.*/
63     epitOutputOperationSet          = 3U, /*!< EPIT Output Operation: Set putput pin.*/
64 };
65 
66 /*! @brief Structure to configure the running mode. */
67 typedef struct _epit_init_config
68 {
69     bool freeRun;    /*!< true: set-and-forget mode, false: free-running mode. */
70     bool waitEnable; /*!< EPIT enabled in wait mode. */
71     bool stopEnable; /*!< EPIT enabled in stop mode. */
72     bool dbgEnable;  /*!< EPIT enabled in debug mode. */
73     bool enableMode; /*!< true: counter starts counting from load value (RLD=1) or 0xFFFF_FFFF (If RLD=0) when enabled,
74 	                      false: counter restores the value that it was disabled when enabled. */
75 } epit_init_config_t;
76 
77 /*******************************************************************************
78  * API
79  ******************************************************************************/
80 
81 #if defined(__cplusplus)
82 extern "C" {
83 #endif
84 
85 /*!
86  * @name EPIT State Control
87  * @{
88  */
89 
90 /*!
91  * @brief Initialize EPIT to reset state and initialize running mode.
92  *
93  * @param base EPIT base pointer.
94  * @param initConfig EPIT initialize structure.
95  */
96 void EPIT_Init(EPIT_Type* base, const epit_init_config_t* initConfig);
97 
98 /*!
99  * @brief Software reset of EPIT module.
100  *
101  * @param base EPIT base pointer.
102  */
EPIT_SoftReset(EPIT_Type * base)103 static inline void EPIT_SoftReset(EPIT_Type* base)
104 {
105     EPIT_CR_REG(base) |= EPIT_CR_SWR_MASK;
106     /* Wait reset finished. */
107     while (EPIT_CR_REG(base) & EPIT_CR_SWR_MASK) { }
108 }
109 
110 /*!
111  * @brief Set clock source of EPIT.
112  *
113  * @param base EPIT base pointer.
114  * @param source clock source (see @ref _epit_clock_source enumeration).
115  */
EPIT_SetClockSource(EPIT_Type * base,uint32_t source)116 static inline void EPIT_SetClockSource(EPIT_Type* base, uint32_t source)
117 {
118     EPIT_CR_REG(base) = (EPIT_CR_REG(base) & ~EPIT_CR_CLKSRC_MASK) | EPIT_CR_CLKSRC(source);
119 }
120 
121 /*!
122  * @brief Get clock source of EPIT.
123  *
124  * @param base EPIT base pointer.
125  * @return clock source (see @ref _epit_clock_source enumeration).
126  */
EPIT_GetClockSource(EPIT_Type * base)127 static inline uint32_t EPIT_GetClockSource(EPIT_Type* base)
128 {
129     return (EPIT_CR_REG(base) & EPIT_CR_CLKSRC_MASK) >> EPIT_CR_CLKSRC_SHIFT;
130 }
131 
132 /*!
133  * @brief Set pre scaler of EPIT.
134  *
135  * @param base EPIT base pointer.
136  * @param prescaler Pre-scaler of EPIT (0-4095, divider = prescaler + 1).
137  */
EPIT_SetPrescaler(EPIT_Type * base,uint32_t prescaler)138 static inline void EPIT_SetPrescaler(EPIT_Type* base, uint32_t prescaler)
139 {
140     assert(prescaler <= (EPIT_CR_PRESCALAR_MASK >> EPIT_CR_PRESCALAR_SHIFT));
141     EPIT_CR_REG(base) = (EPIT_CR_REG(base) & ~EPIT_CR_PRESCALAR_MASK) | EPIT_CR_PRESCALAR(prescaler);
142 }
143 
144 /*!
145  * @brief Get pre scaler of EPIT.
146  *
147  * @param base EPIT base pointer.
148  * @return Pre-scaler of EPIT (0-4095).
149  */
EPIT_GetPrescaler(EPIT_Type * base)150 static inline uint32_t EPIT_GetPrescaler(EPIT_Type* base)
151 {
152     return (EPIT_CR_REG(base) & EPIT_CR_PRESCALAR_MASK) >> EPIT_CR_PRESCALAR_SHIFT;
153 }
154 
155 /*!
156  * @brief Enable EPIT module.
157  *
158  * @param base EPIT base pointer.
159  */
EPIT_Enable(EPIT_Type * base)160 static inline void EPIT_Enable(EPIT_Type* base)
161 {
162     EPIT_CR_REG(base) |= EPIT_CR_EN_MASK;
163 }
164 
165 /*!
166  * @brief Disable EPIT module.
167  *
168  * @param base EPIT base pointer.
169  */
EPIT_Disable(EPIT_Type * base)170 static inline void EPIT_Disable(EPIT_Type* base)
171 {
172     EPIT_CR_REG(base) &= ~EPIT_CR_EN_MASK;
173 }
174 
175 /*!
176  * @brief Get EPIT counter value.
177  *
178  * @param base EPIT base pointer.
179  * @return EPIT counter value.
180  */
EPIT_ReadCounter(EPIT_Type * base)181 static inline uint32_t EPIT_ReadCounter(EPIT_Type* base)
182 {
183     return EPIT_CNR_REG(base);
184 }
185 
186 /*@}*/
187 
188 /*!
189  * @name EPIT Output Signal Control
190  * @{
191  */
192 
193 /*!
194  * @brief Set EPIT output compare operation mode.
195  *
196  * @param base EPIT base pointer.
197  * @param mode EPIT output compare operation mode (see @ref _epit_output_operation_mode enumeration).
198  */
EPIT_SetOutputOperationMode(EPIT_Type * base,uint32_t mode)199 static inline void EPIT_SetOutputOperationMode(EPIT_Type* base, uint32_t mode)
200 {
201     EPIT_CR_REG(base) = (EPIT_CR_REG(base) & ~EPIT_CR_OM_MASK) | EPIT_CR_OM(mode);
202 }
203 
204 /*!
205  * @brief Get EPIT output compare operation mode.
206  *
207  * @param base EPIT base pointer.
208  * @return EPIT output operation mode (see @ref _epit_output_operation_mode enumeration).
209  */
EPIT_GetOutputOperationMode(EPIT_Type * base)210 static inline uint32_t EPIT_GetOutputOperationMode(EPIT_Type* base)
211 {
212     return (EPIT_CR_REG(base) & EPIT_CR_OM_MASK) >> EPIT_CR_OM_SHIFT;
213 }
214 
215 /*!
216  * @brief Set EPIT output compare value.
217  *
218  * @param base EPIT base pointer.
219  * @param value EPIT output compare value.
220  */
EPIT_SetOutputCompareValue(EPIT_Type * base,uint32_t value)221 static inline void EPIT_SetOutputCompareValue(EPIT_Type* base, uint32_t value)
222 {
223     EPIT_CMPR_REG(base) = value;
224 }
225 
226 /*!
227  * @brief Get EPIT output compare value.
228  *
229  * @param base EPIT base pointer.
230  * @return EPIT output compare value.
231  */
EPIT_GetOutputCompareValue(EPIT_Type * base)232 static inline uint32_t EPIT_GetOutputCompareValue(EPIT_Type* base)
233 {
234     return EPIT_CMPR_REG(base);
235 }
236 
237 /*@}*/
238 
239 /*!
240  * @name EPIT Data Load Control
241  * @{
242  */
243 
244 /*!
245  * @brief Set the value that is to be loaded into counter register.
246  *
247  * @param base EPIT base pointer.
248  * @param value Counter load value.
249  */
EPIT_SetCounterLoadValue(EPIT_Type * base,uint32_t value)250 static inline void EPIT_SetCounterLoadValue(EPIT_Type* base, uint32_t value)
251 {
252     EPIT_LR_REG(base) = value;
253 }
254 
255 /*!
256  * @brief Get the value that loaded into counter register.
257  *
258  * @param base EPIT base pointer.
259  * @return The counter load value.
260  */
EPIT_GetCounterLoadValue(EPIT_Type * base)261 static inline uint32_t EPIT_GetCounterLoadValue(EPIT_Type* base)
262 {
263     return EPIT_LR_REG(base);
264 }
265 
266 /*!
267  * @brief Enable or disable EPIT overwrite counter immediately.
268  *
269  * @param base EPIT base pointer.
270  * @param enable Enable/Disable EPIT overwrite counter immediately.
271  *               - true: Enable overwrite counter immediately.
272  *               - false: Disable overwrite counter immediately.
273  */
274 void EPIT_SetOverwriteCounter(EPIT_Type* base, bool enable);
275 
276 /*@}*/
277 
278 /*!
279  * @name EPIT Interrupt and Status Control
280  * @{
281  */
282 
283 /*!
284  * @brief Get EPIT status of output compare interrupt flag.
285  *
286  * @param base EPIT base pointer.
287  * @return EPIT status of output compare interrupt flag.
288  */
EPIT_GetStatusFlag(EPIT_Type * base)289 static inline uint32_t EPIT_GetStatusFlag(EPIT_Type* base)
290 {
291     return EPIT_SR_REG(base) & EPIT_SR_OCIF_MASK;
292 }
293 
294 /*!
295  * @brief Clear EPIT Output compare interrupt flag.
296  *
297  * @param base EPIT base pointer.
298  */
EPIT_ClearStatusFlag(EPIT_Type * base)299 static inline void EPIT_ClearStatusFlag(EPIT_Type* base)
300 {
301     EPIT_SR_REG(base) = EPIT_SR_OCIF_MASK;
302 }
303 
304 /*!
305  * @brief Enable or disable EPIT interrupt.
306  *
307  * @param base EPIT base pointer.
308  * @param enable Enable/Disable EPIT interrupt.
309  *        - true: Enable interrupt.
310  *        - false: Disable interrupt.
311  */
312 void EPIT_SetIntCmd(EPIT_Type* base, bool enable);
313 
314 /*@}*/
315 
316 #if defined(__cplusplus)
317 }
318 #endif
319 
320 /*! @} */
321 
322 #endif /*__EPIT_H__*/
323 
324 /*******************************************************************************
325  * EOF
326  ******************************************************************************/
327