]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0+_LPC51U68_GCC_IAR_KEIL/drivers/fsl_ctimer.h
Increase test coverage for queue sets.
[freertos] / FreeRTOS / Demo / CORTEX_M0+_LPC51U68_GCC_IAR_KEIL / drivers / fsl_ctimer.h
1 /*\r
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.\r
3  * Copyright 2016-2018 NXP\r
4  * All rights reserved.\r
5  *\r
6  * SPDX-License-Identifier: BSD-3-Clause\r
7  */\r
8 #ifndef _FSL_CTIMER_H_\r
9 #define _FSL_CTIMER_H_\r
10 \r
11 #include "fsl_common.h"\r
12 \r
13 /*!\r
14  * @addtogroup ctimer\r
15  * @{\r
16  */\r
17 \r
18 /*! @file */\r
19 \r
20 /*******************************************************************************\r
21  * Definitions\r
22  ******************************************************************************/\r
23 \r
24 /*! @name Driver version */\r
25 /*@{*/\r
26 #define FSL_CTIMER_DRIVER_VERSION (MAKE_VERSION(2, 0, 2)) /*!< Version 2.0.2 */\r
27 /*@}*/\r
28 \r
29 /*! @brief List of Timer capture channels */\r
30 typedef enum _ctimer_capture_channel\r
31 {\r
32     kCTIMER_Capture_0 = 0U, /*!< Timer capture channel 0 */\r
33     kCTIMER_Capture_1,      /*!< Timer capture channel 1 */\r
34     kCTIMER_Capture_2,      /*!< Timer capture channel 2 */\r
35 #if defined(FSL_FEATURE_CTIMER_HAS_CCR_CAP3) && FSL_FEATURE_CTIMER_HAS_CCR_CAP3\r
36     kCTIMER_Capture_3 /*!< Timer capture channel 3 */\r
37 #endif                /* FSL_FEATURE_CTIMER_HAS_IR_CR3INT */\r
38 } ctimer_capture_channel_t;\r
39 \r
40 /*! @brief List of capture edge options */\r
41 typedef enum _ctimer_capture_edge\r
42 {\r
43     kCTIMER_Capture_RiseEdge = 1U, /*!< Capture on rising edge */\r
44     kCTIMER_Capture_FallEdge = 2U, /*!< Capture on falling edge */\r
45     kCTIMER_Capture_BothEdge = 3U, /*!< Capture on rising and falling edge */\r
46 } ctimer_capture_edge_t;\r
47 \r
48 /*! @brief List of Timer match registers */\r
49 typedef enum _ctimer_match\r
50 {\r
51     kCTIMER_Match_0 = 0U, /*!< Timer match register 0 */\r
52     kCTIMER_Match_1,      /*!< Timer match register 1 */\r
53     kCTIMER_Match_2,      /*!< Timer match register 2 */\r
54     kCTIMER_Match_3       /*!< Timer match register 3 */\r
55 } ctimer_match_t;\r
56 \r
57 /*! @brief List of output control options */\r
58 typedef enum _ctimer_match_output_control\r
59 {\r
60     kCTIMER_Output_NoAction = 0U, /*!< No action is taken */\r
61     kCTIMER_Output_Clear,         /*!< Clear the EM bit/output to 0 */\r
62     kCTIMER_Output_Set,           /*!< Set the EM bit/output to 1 */\r
63     kCTIMER_Output_Toggle         /*!< Toggle the EM bit/output */\r
64 } ctimer_match_output_control_t;\r
65 \r
66 /*! @brief List of Timer modes */\r
67 typedef enum _ctimer_timer_mode\r
68 {\r
69     kCTIMER_TimerMode = 0U,     /* TC is incremented every rising APB bus clock edge */\r
70     kCTIMER_IncreaseOnRiseEdge, /* TC is incremented on rising edge of input signal */\r
71     kCTIMER_IncreaseOnFallEdge, /* TC is incremented on falling edge of input signal */\r
72     kCTIMER_IncreaseOnBothEdge  /* TC is incremented on both edges of input signal */\r
73 } ctimer_timer_mode_t;\r
74 \r
75 /*! @brief List of Timer interrupts */\r
76 typedef enum _ctimer_interrupt_enable\r
77 {\r
78     kCTIMER_Match0InterruptEnable = CTIMER_MCR_MR0I_MASK, /*!< Match 0 interrupt */\r
79     kCTIMER_Match1InterruptEnable = CTIMER_MCR_MR1I_MASK, /*!< Match 1 interrupt */\r
80     kCTIMER_Match2InterruptEnable = CTIMER_MCR_MR2I_MASK, /*!< Match 2 interrupt */\r
81     kCTIMER_Match3InterruptEnable = CTIMER_MCR_MR3I_MASK, /*!< Match 3 interrupt */\r
82 #if !(defined(FSL_FEATURE_CTIMER_HAS_NO_INPUT_CAPTURE) && (FSL_FEATURE_CTIMER_HAS_NO_INPUT_CAPTURE))\r
83     kCTIMER_Capture0InterruptEnable = CTIMER_CCR_CAP0I_MASK, /*!< Capture 0 interrupt */\r
84     kCTIMER_Capture1InterruptEnable = CTIMER_CCR_CAP1I_MASK, /*!< Capture 1 interrupt */\r
85     kCTIMER_Capture2InterruptEnable = CTIMER_CCR_CAP2I_MASK, /*!< Capture 2 interrupt */\r
86 #if defined(FSL_FEATURE_CTIMER_HAS_CCR_CAP3) && FSL_FEATURE_CTIMER_HAS_CCR_CAP3\r
87     kCTIMER_Capture3InterruptEnable = CTIMER_CCR_CAP3I_MASK, /*!< Capture 3 interrupt */\r
88 #endif                                                       /* FSL_FEATURE_CTIMER_HAS_CCR_CAP3 */\r
89 #endif\r
90 } ctimer_interrupt_enable_t;\r
91 \r
92 /*! @brief List of Timer flags */\r
93 typedef enum _ctimer_status_flags\r
94 {\r
95     kCTIMER_Match0Flag = CTIMER_IR_MR0INT_MASK, /*!< Match 0 interrupt flag */\r
96     kCTIMER_Match1Flag = CTIMER_IR_MR1INT_MASK, /*!< Match 1 interrupt flag */\r
97     kCTIMER_Match2Flag = CTIMER_IR_MR2INT_MASK, /*!< Match 2 interrupt flag */\r
98     kCTIMER_Match3Flag = CTIMER_IR_MR3INT_MASK, /*!< Match 3 interrupt flag */\r
99 #if !(defined(FSL_FEATURE_CTIMER_HAS_NO_INPUT_CAPTURE) && (FSL_FEATURE_CTIMER_HAS_NO_INPUT_CAPTURE))\r
100     kCTIMER_Capture0Flag = CTIMER_IR_CR0INT_MASK, /*!< Capture 0 interrupt flag */\r
101     kCTIMER_Capture1Flag = CTIMER_IR_CR1INT_MASK, /*!< Capture 1 interrupt flag */\r
102     kCTIMER_Capture2Flag = CTIMER_IR_CR2INT_MASK, /*!< Capture 2 interrupt flag */\r
103 #if defined(FSL_FEATURE_CTIMER_HAS_IR_CR3INT) && FSL_FEATURE_CTIMER_HAS_IR_CR3INT\r
104     kCTIMER_Capture3Flag = CTIMER_IR_CR3INT_MASK, /*!< Capture 3 interrupt flag */\r
105 #endif                                            /* FSL_FEATURE_CTIMER_HAS_IR_CR3INT */\r
106 #endif\r
107 } ctimer_status_flags_t;\r
108 \r
109 typedef void (*ctimer_callback_t)(uint32_t flags);\r
110 \r
111 /*! @brief Callback type when registering for a callback. When registering a callback\r
112  *         an array of function pointers is passed the size could be 1 or 8, the callback\r
113  *         type will tell that.\r
114  */\r
115 typedef enum\r
116 {\r
117     kCTIMER_SingleCallback,  /*!< Single Callback type where there is only one callback for the timer.\r
118                                  based on the status flags different channels needs to be handled differently */\r
119     kCTIMER_MultipleCallback /*!< Multiple Callback type where there can be 8 valid callbacks, one per channel.\r
120                                  for both match/capture */\r
121 } ctimer_callback_type_t;\r
122 \r
123 /*!\r
124  * @brief Match configuration\r
125  *\r
126  * This structure holds the configuration settings for each match register.\r
127  */\r
128 typedef struct _ctimer_match_config\r
129 {\r
130     uint32_t matchValue;                      /*!< This is stored in the match register */\r
131     bool enableCounterReset;                  /*!< true: Match will reset the counter\r
132                                                    false: Match will not reser the counter */\r
133     bool enableCounterStop;                   /*!< true: Match will stop the counter\r
134                                                    false: Match will not stop the counter */\r
135     ctimer_match_output_control_t outControl; /*!< Action to be taken on a match on the EM bit/output */\r
136     bool outPinInitState;                     /*!< Initial value of the EM bit/output */\r
137     bool enableInterrupt;                     /*!< true: Generate interrupt upon match\r
138                                                    false: Do not generate interrupt on match */\r
139 \r
140 } ctimer_match_config_t;\r
141 \r
142 /*!\r
143  * @brief Timer configuration structure\r
144  *\r
145  * This structure holds the configuration settings for the Timer peripheral. To initialize this\r
146  * structure to reasonable defaults, call the CTIMER_GetDefaultConfig() function and pass a\r
147  * pointer to the configuration structure instance.\r
148  *\r
149  * The configuration structure can be made constant so as to reside in flash.\r
150  */\r
151 typedef struct _ctimer_config\r
152 {\r
153     ctimer_timer_mode_t mode;       /*!< Timer mode */\r
154     ctimer_capture_channel_t input; /*!< Input channel to increment the timer, used only in timer\r
155                                         modes that rely on this input signal to increment TC */\r
156     uint32_t prescale;              /*!< Prescale value */\r
157 } ctimer_config_t;\r
158 \r
159 /*******************************************************************************\r
160  * API\r
161  ******************************************************************************/\r
162 \r
163 #if defined(__cplusplus)\r
164 extern "C" {\r
165 #endif\r
166 \r
167 /*!\r
168  * @name Initialization and deinitialization\r
169  * @{\r
170  */\r
171 \r
172 /*!\r
173  * @brief Ungates the clock and configures the peripheral for basic operation.\r
174  *\r
175  * @note This API should be called at the beginning of the application before using the driver.\r
176  *\r
177  * @param base   Ctimer peripheral base address\r
178  * @param config Pointer to the user configuration structure.\r
179  */\r
180 void CTIMER_Init(CTIMER_Type *base, const ctimer_config_t *config);\r
181 \r
182 /*!\r
183  * @brief Gates the timer clock.\r
184  *\r
185  * @param base Ctimer peripheral base address\r
186  */\r
187 void CTIMER_Deinit(CTIMER_Type *base);\r
188 \r
189 /*!\r
190  * @brief  Fills in the timers configuration structure with the default settings.\r
191  *\r
192  * The default values are:\r
193  * @code\r
194  *   config->mode = kCTIMER_TimerMode;\r
195  *   config->input = kCTIMER_Capture_0;\r
196  *   config->prescale = 0;\r
197  * @endcode\r
198  * @param config Pointer to the user configuration structure.\r
199  */\r
200 void CTIMER_GetDefaultConfig(ctimer_config_t *config);\r
201 \r
202 /*! @}*/\r
203 \r
204 /*!\r
205  * @name PWM setup operations\r
206  * @{\r
207  */\r
208 \r
209 /*!\r
210  * @brief Configures the PWM signal parameters.\r
211  *\r
212  * Enables PWM mode on the match channel passed in and will then setup the match value\r
213  * and other match parameters to generate a PWM signal.\r
214  * This function will assign match channel 3 to set the PWM cycle.\r
215  *\r
216  * @note When setting PWM output from multiple output pins, all should use the same PWM\r
217  * period\r
218  *\r
219  * @param base             Ctimer peripheral base address\r
220  * @param matchChannel     Match pin to be used to output the PWM signal\r
221  * @param pwmPeriod        PWM period match value\r
222  * @param pulsePeriod      Pulse width match value\r
223  * @param enableInt        Enable interrupt when the timer value reaches the match value of the PWM pulse,\r
224  *                         if it is 0 then no interrupt is generated\r
225  *\r
226  * @return kStatus_Success on success\r
227  *         kStatus_Fail If matchChannel passed in is 3; this channel is reserved to set the PWM period\r
228  */\r
229 status_t CTIMER_SetupPwmPeriod(\r
230     CTIMER_Type *base, ctimer_match_t matchChannel, uint32_t pwmPeriod, uint32_t pulsePeriod, bool enableInt);\r
231 \r
232 /*!\r
233  * @brief Configures the PWM signal parameters.\r
234  *\r
235  * Enables PWM mode on the match channel passed in and will then setup the match value\r
236  * and other match parameters to generate a PWM signal.\r
237  * This function will assign match channel 3 to set the PWM cycle.\r
238  *\r
239  * @note When setting PWM output from multiple output pins, all should use the same PWM\r
240  * frequency. Please use CTIMER_SetupPwmPeriod to set up the PWM with high resolution.\r
241  *\r
242  * @param base             Ctimer peripheral base address\r
243  * @param matchChannel     Match pin to be used to output the PWM signal\r
244  * @param dutyCyclePercent PWM pulse width; the value should be between 0 to 100\r
245  * @param pwmFreq_Hz       PWM signal frequency in Hz\r
246  * @param srcClock_Hz      Timer counter clock in Hz\r
247  * @param enableInt        Enable interrupt when the timer value reaches the match value of the PWM pulse,\r
248  *                         if it is 0 then no interrupt is generated\r
249  *\r
250  * @return kStatus_Success on success\r
251  *         kStatus_Fail If matchChannel passed in is 3; this channel is reserved to set the PWM cycle\r
252  */\r
253 status_t CTIMER_SetupPwm(CTIMER_Type *base,\r
254                          ctimer_match_t matchChannel,\r
255                          uint8_t dutyCyclePercent,\r
256                          uint32_t pwmFreq_Hz,\r
257                          uint32_t srcClock_Hz,\r
258                          bool enableInt);\r
259 \r
260 /*!\r
261  * @brief Updates the pulse period of an active PWM signal.\r
262  *\r
263  * @param base         Ctimer peripheral base address\r
264  * @param matchChannel Match pin to be used to output the PWM signal\r
265  * @param pulsePeriod  New PWM pulse width match value\r
266  */\r
267 static inline void CTIMER_UpdatePwmPulsePeriod(CTIMER_Type *base, ctimer_match_t matchChannel, uint32_t pulsePeriod)\r
268 {\r
269     /* Update PWM pulse period match value */\r
270     base->MR[matchChannel] = pulsePeriod;\r
271 }\r
272 \r
273 /*!\r
274  * @brief Updates the duty cycle of an active PWM signal.\r
275  *\r
276  * @note Please use CTIMER_UpdatePwmPulsePeriod to update the PWM with high resolution.\r
277  *\r
278  * @param base             Ctimer peripheral base address\r
279  * @param matchChannel     Match pin to be used to output the PWM signal\r
280  * @param dutyCyclePercent New PWM pulse width; the value should be between 0 to 100\r
281  */\r
282 void CTIMER_UpdatePwmDutycycle(CTIMER_Type *base, ctimer_match_t matchChannel, uint8_t dutyCyclePercent);\r
283 \r
284 /*! @}*/\r
285 \r
286 /*!\r
287  * @brief Setup the match register.\r
288  *\r
289  * User configuration is used to setup the match value and action to be taken when a match occurs.\r
290  *\r
291  * @param base         Ctimer peripheral base address\r
292  * @param matchChannel Match register to configure\r
293  * @param config       Pointer to the match configuration structure\r
294  */\r
295 void CTIMER_SetupMatch(CTIMER_Type *base, ctimer_match_t matchChannel, const ctimer_match_config_t *config);\r
296 \r
297 /*!\r
298  * @brief Setup the capture.\r
299  *\r
300  * @param base      Ctimer peripheral base address\r
301  * @param capture   Capture channel to configure\r
302  * @param edge      Edge on the channel that will trigger a capture\r
303  * @param enableInt Flag to enable channel interrupts, if enabled then the registered call back\r
304  *                  is called upon capture\r
305  */\r
306 void CTIMER_SetupCapture(CTIMER_Type *base,\r
307                          ctimer_capture_channel_t capture,\r
308                          ctimer_capture_edge_t edge,\r
309                          bool enableInt);\r
310 \r
311 /*!\r
312  * @brief Get the timer count value from TC register.\r
313  *\r
314  * @param  base  Ctimer peripheral base address.\r
315  * @return       return the timer count value.\r
316  */\r
317 static inline uint32_t CTIMER_GetTimerCountValue(CTIMER_Type *base)\r
318 {\r
319     return (base->TC);\r
320 }\r
321 \r
322 /*!\r
323  * @brief Register callback.\r
324  *\r
325  * @param base      Ctimer peripheral base address\r
326  * @param cb_func   callback function\r
327  * @param cb_type   callback function type, singular or multiple\r
328  */\r
329 void CTIMER_RegisterCallBack(CTIMER_Type *base, ctimer_callback_t *cb_func, ctimer_callback_type_t cb_type);\r
330 \r
331 /*!\r
332  * @name Interrupt Interface\r
333  * @{\r
334  */\r
335 \r
336 /*!\r
337  * @brief Enables the selected Timer interrupts.\r
338  *\r
339  * @param base Ctimer peripheral base address\r
340  * @param mask The interrupts to enable. This is a logical OR of members of the\r
341  *             enumeration ::ctimer_interrupt_enable_t\r
342  */\r
343 static inline void CTIMER_EnableInterrupts(CTIMER_Type *base, uint32_t mask)\r
344 {\r
345     /* Enable match interrupts */\r
346     base->MCR |= mask & (CTIMER_MCR_MR0I_MASK | CTIMER_MCR_MR1I_MASK | CTIMER_MCR_MR2I_MASK | CTIMER_MCR_MR3I_MASK);\r
347 \r
348 /* Enable capture interrupts */\r
349 #if !(defined(FSL_FEATURE_CTIMER_HAS_NO_INPUT_CAPTURE) && (FSL_FEATURE_CTIMER_HAS_NO_INPUT_CAPTURE))\r
350     base->CCR |= mask & (CTIMER_CCR_CAP0I_MASK | CTIMER_CCR_CAP1I_MASK | CTIMER_CCR_CAP2I_MASK\r
351 #if defined(FSL_FEATURE_CTIMER_HAS_CCR_CAP3) && FSL_FEATURE_CTIMER_HAS_CCR_CAP3\r
352                          | CTIMER_CCR_CAP3I_MASK\r
353 #endif /* FSL_FEATURE_CTIMER_HAS_CCR_CAP3 */\r
354                         );\r
355 #endif\r
356 }\r
357 \r
358 /*!\r
359  * @brief Disables the selected Timer interrupts.\r
360  *\r
361  * @param base Ctimer peripheral base address\r
362  * @param mask The interrupts to enable. This is a logical OR of members of the\r
363  *             enumeration ::ctimer_interrupt_enable_t\r
364  */\r
365 static inline void CTIMER_DisableInterrupts(CTIMER_Type *base, uint32_t mask)\r
366 {\r
367     /* Disable match interrupts */\r
368     base->MCR &= ~(mask & (CTIMER_MCR_MR0I_MASK | CTIMER_MCR_MR1I_MASK | CTIMER_MCR_MR2I_MASK | CTIMER_MCR_MR3I_MASK));\r
369 \r
370 /* Disable capture interrupts */\r
371 #if !(defined(FSL_FEATURE_CTIMER_HAS_NO_INPUT_CAPTURE) && (FSL_FEATURE_CTIMER_HAS_NO_INPUT_CAPTURE))\r
372     base->CCR &= ~(mask & (CTIMER_CCR_CAP0I_MASK | CTIMER_CCR_CAP1I_MASK | CTIMER_CCR_CAP2I_MASK\r
373 #if defined(FSL_FEATURE_CTIMER_HAS_CCR_CAP3) && FSL_FEATURE_CTIMER_HAS_CCR_CAP3\r
374                            | CTIMER_CCR_CAP3I_MASK\r
375 #endif /* FSL_FEATURE_CTIMER_HAS_CCR_CAP3 */\r
376                            ));\r
377 #endif\r
378 }\r
379 \r
380 /*!\r
381  * @brief Gets the enabled Timer interrupts.\r
382  *\r
383  * @param base Ctimer peripheral base address\r
384  *\r
385  * @return The enabled interrupts. This is the logical OR of members of the\r
386  *         enumeration ::ctimer_interrupt_enable_t\r
387  */\r
388 static inline uint32_t CTIMER_GetEnabledInterrupts(CTIMER_Type *base)\r
389 {\r
390     uint32_t enabledIntrs = 0;\r
391 \r
392     /* Get all the match interrupts enabled */\r
393     enabledIntrs =\r
394         base->MCR & (CTIMER_MCR_MR0I_MASK | CTIMER_MCR_MR1I_MASK | CTIMER_MCR_MR2I_MASK | CTIMER_MCR_MR3I_MASK);\r
395 \r
396 /* Get all the capture interrupts enabled */\r
397 #if !(defined(FSL_FEATURE_CTIMER_HAS_NO_INPUT_CAPTURE) && (FSL_FEATURE_CTIMER_HAS_NO_INPUT_CAPTURE))\r
398     enabledIntrs |= base->CCR & (CTIMER_CCR_CAP0I_MASK | CTIMER_CCR_CAP1I_MASK | CTIMER_CCR_CAP2I_MASK\r
399 #if defined(FSL_FEATURE_CTIMER_HAS_CCR_CAP3) && FSL_FEATURE_CTIMER_HAS_CCR_CAP3\r
400                                  | CTIMER_CCR_CAP3I_MASK\r
401 #endif /* FSL_FEATURE_CTIMER_HAS_CCR_CAP3 */\r
402                                 );\r
403 #endif\r
404 \r
405     return enabledIntrs;\r
406 }\r
407 \r
408 /*! @}*/\r
409 \r
410 /*!\r
411  * @name Status Interface\r
412  * @{\r
413  */\r
414 \r
415 /*!\r
416  * @brief Gets the Timer status flags.\r
417  *\r
418  * @param base Ctimer peripheral base address\r
419  *\r
420  * @return The status flags. This is the logical OR of members of the\r
421  *         enumeration ::ctimer_status_flags_t\r
422  */\r
423 static inline uint32_t CTIMER_GetStatusFlags(CTIMER_Type *base)\r
424 {\r
425     return base->IR;\r
426 }\r
427 \r
428 /*!\r
429  * @brief Clears the Timer status flags.\r
430  *\r
431  * @param base Ctimer peripheral base address\r
432  * @param mask The status flags to clear. This is a logical OR of members of the\r
433  *             enumeration ::ctimer_status_flags_t\r
434  */\r
435 static inline void CTIMER_ClearStatusFlags(CTIMER_Type *base, uint32_t mask)\r
436 {\r
437     base->IR = mask;\r
438 }\r
439 \r
440 /*! @}*/\r
441 \r
442 /*!\r
443  * @name Counter Start and Stop\r
444  * @{\r
445  */\r
446 \r
447 /*!\r
448  * @brief Starts the Timer counter.\r
449  *\r
450  * @param base Ctimer peripheral base address\r
451  */\r
452 static inline void CTIMER_StartTimer(CTIMER_Type *base)\r
453 {\r
454     base->TCR |= CTIMER_TCR_CEN_MASK;\r
455 }\r
456 \r
457 /*!\r
458  * @brief Stops the Timer counter.\r
459  *\r
460  * @param base Ctimer peripheral base address\r
461  */\r
462 static inline void CTIMER_StopTimer(CTIMER_Type *base)\r
463 {\r
464     base->TCR &= ~CTIMER_TCR_CEN_MASK;\r
465 }\r
466 \r
467 /*! @}*/\r
468 \r
469 /*!\r
470  * @brief Reset the counter.\r
471  *\r
472  * The timer counter and prescale counter are reset on the next positive edge of the APB clock.\r
473  *\r
474  * @param base Ctimer peripheral base address\r
475  */\r
476 static inline void CTIMER_Reset(CTIMER_Type *base)\r
477 {\r
478     base->TCR |= CTIMER_TCR_CRST_MASK;\r
479     base->TCR &= ~CTIMER_TCR_CRST_MASK;\r
480 }\r
481 \r
482 #if defined(__cplusplus)\r
483 }\r
484 #endif\r
485 \r
486 /*! @}*/\r
487 \r
488 #endif /* _FSL_CTIMER_H_ */\r