]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC/ThirdParty/LPCOpen/lpc_core/lpc_ip/timer_001.h
Update LPC18xx FreeRTOS+UDP demo to use LPCOpen USB and Ethernet drivers.
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC / ThirdParty / LPCOpen / lpc_core / lpc_ip / timer_001.h
1 /*\r
2  * @brief 32-bit Timer/PWM registers and control functions\r
3  *\r
4  * @note\r
5  * Copyright(C) NXP Semiconductors, 2012\r
6  * All rights reserved.\r
7  *\r
8  * @par\r
9  * Software that is described herein is for illustrative purposes only\r
10  * which provides customers with programming information regarding the\r
11  * LPC products.  This software is supplied "AS IS" without any warranties of\r
12  * any kind, and NXP Semiconductors and its licensor disclaim any and\r
13  * all warranties, express or implied, including all implied warranties of\r
14  * merchantability, fitness for a particular purpose and non-infringement of\r
15  * intellectual property rights.  NXP Semiconductors assumes no responsibility\r
16  * or liability for the use of the software, conveys no license or rights under any\r
17  * patent, copyright, mask work right, or any other intellectual property rights in\r
18  * or to any products. NXP Semiconductors reserves the right to make changes\r
19  * in the software without notification. NXP Semiconductors also makes no\r
20  * representation or warranty that such application will be suitable for the\r
21  * specified use without further testing or modification.\r
22  *\r
23  * @par\r
24  * Permission to use, copy, modify, and distribute this software and its\r
25  * documentation is hereby granted, under NXP Semiconductors' and its\r
26  * licensor's relevant copyrights in the software, without fee, provided that it\r
27  * is used in conjunction with NXP Semiconductors microcontrollers.  This\r
28  * copyright, permission, and disclaimer notice must appear in all copies of\r
29  * this code.\r
30  */\r
31 \r
32 #ifndef __TIMER_001_H_\r
33 #define __TIMER_001_H_\r
34 \r
35 #include "sys_config.h"\r
36 #include "cmsis.h"\r
37 \r
38 #ifdef __cplusplus\r
39 extern "C" {\r
40 #endif\r
41 \r
42 /** @defgroup IP_TIMER_001 IP: Timer register block and driver\r
43  * @ingroup IP_Drivers\r
44  * @{\r
45  */\r
46 \r
47 /**\r
48  * @brief 32-bit Standard timer register block structure\r
49  */\r
50 typedef struct {                                        /*!< TIMERn Structure       */\r
51         __IO uint32_t IR;                               /*!< Interrupt Register. The IR can be written to clear interrupts. The IR can be read to identify which of eight possible interrupt sources are pending. */\r
52         __IO uint32_t TCR;                              /*!< Timer Control Register. The TCR is used to control the Timer Counter functions. The Timer Counter can be disabled or reset through the TCR. */\r
53         __IO uint32_t TC;                               /*!< Timer Counter. The 32 bit TC is incremented every PR+1 cycles of PCLK. The TC is controlled through the TCR. */\r
54         __IO uint32_t PR;                               /*!< Prescale Register. The Prescale Counter (below) is equal to this value, the next clock increments the TC and clears the PC. */\r
55         __IO uint32_t PC;                               /*!< Prescale Counter. The 32 bit PC is a counter which is incremented to the value stored in PR. When the value in PR is reached, the TC is incremented and the PC is cleared. The PC is observable and controllable through the bus interface. */\r
56         __IO uint32_t MCR;                              /*!< Match Control Register. The MCR is used to control if an interrupt is generated and if the TC is reset when a Match occurs. */\r
57         __IO uint32_t MR[4];                    /*!< Match Register. MR can be enabled through the MCR to reset the TC, stop both the TC and PC, and/or generate an interrupt every time MR matches the TC. */\r
58         __IO uint32_t CCR;                              /*!< Capture Control Register. The CCR controls which edges of the capture inputs are used to load the Capture Registers and whether or not an interrupt is generated when a capture takes place. */\r
59         __IO uint32_t CR[4];                    /*!< Capture Register. CR is loaded with the value of TC when there is an event on the CAPn.0 input. */\r
60         __IO uint32_t EMR;                              /*!< External Match Register. The EMR controls the external match pins MATn.0-3 (MAT0.0-3 and MAT1.0-3 respectively). */\r
61         __I  uint32_t RESERVED0[12];\r
62         __IO uint32_t CTCR;                             /*!< Count Control Register. The CTCR selects between Timer and Counter mode, and in Counter mode selects the signal and edge(s) for counting. */\r
63 #if defined(CHIP_LPC110X) || defined(CHIP_LPC11XXLV) || defined(CHIP_LPC11AXX) || \\r
64                 defined(CHIP_LPC11CXX) || defined(CHIP_LPC11EXX) || defined(CHIP_LPC11UXX)\r
65         __IO uint32_t PWMC;\r
66 #endif\r
67 } IP_TIMER_001_T;\r
68 \r
69 /** Macro to clear interrupt pending */\r
70 #define TIMER_IR_CLR(n)         _BIT(n)\r
71 \r
72 /** Macro for getting a timer match interrupt bit */\r
73 #define TIMER_MATCH_INT(n)      (_BIT((n) & 0x0F))\r
74 /** Macro for getting a capture event interrupt bit */\r
75 #define TIMER_CAP_INT(n)        (_BIT((((n) & 0x0F) + 4)))\r
76 \r
77 /** Timer/counter enable bit */\r
78 #define TIMER_ENABLE            ((uint32_t) (1 << 0))\r
79 /** Timer/counter reset bit */\r
80 #define TIMER_RESET             ((uint32_t) (1 << 1))\r
81 \r
82 /** Bit location for interrupt on MRx match, n = 0 to 3 */\r
83 #define TIMER_INT_ON_MATCH(n)   (_BIT(((n) * 3)))\r
84 /** Bit location for reset on MRx match, n = 0 to 3 */\r
85 #define TIMER_RESET_ON_MATCH(n) (_BIT((((n) * 3) + 1)))\r
86 /** Bit location for stop on MRx match, n = 0 to 3 */\r
87 #define TIMER_STOP_ON_MATCH(n)  (_BIT((((n) * 3) + 2)))\r
88 \r
89 /** Bit location for CAP.n on CRx rising edge, n = 0 to 3 */\r
90 #define TIMER_CAP_RISING(n)     (_BIT(((n) * 3)))\r
91 /** Bit location for CAP.n on CRx falling edge, n = 0 to 3 */\r
92 #define TIMER_CAP_FALLING(n)    (_BIT((((n) * 3) + 1)))\r
93 /** Bit location for CAP.n on CRx interrupt enable, n = 0 to 3 */\r
94 #define TIMER_INT_ON_CAP(n)     (_BIT((((n) * 3) + 2)))\r
95 \r
96 /**\r
97  * @brief       Determine if a match interrupt is pending\r
98  * @param       pTimer          : Pointer to timer IP register address\r
99  * @param       matchnum        : Match interrupt number to check\r
100  * @return      false if the interrupt is not pending, otherwise true\r
101  * @note        Determine if the match interrupt for the passed timer and match\r
102  * counter is pending.\r
103  */\r
104 STATIC INLINE bool IP_TIMER_MatchPending(IP_TIMER_001_T *pTimer, int8_t matchnum)\r
105 {\r
106         return (bool) ((pTimer->IR & TIMER_MATCH_INT(matchnum)) != 0);\r
107 }\r
108 \r
109 /**\r
110  * @brief       Determine if a capture interrupt is pending\r
111  * @param       pTimer  : Pointer to timer IP register address\r
112  * @param       capnum  : Capture interrupt number to check\r
113  * @return      false if the interrupt is not pending, otherwise true\r
114  * @note        Determine if the capture interrupt for the passed capture pin is\r
115  * pending.\r
116  */\r
117 STATIC INLINE bool IP_TIMER_CapturePending(IP_TIMER_001_T *pTimer, int8_t capnum)\r
118 {\r
119         return (bool) ((pTimer->IR & TIMER_CAP_INT(capnum)) != 0);\r
120 }\r
121 \r
122 /**\r
123  * @brief       Clears a (pending) match interrupt\r
124  * @param       pTimer          : Pointer to timer IP register address\r
125  * @param       matchnum        : Match interrupt number to clear\r
126  * @return      Nothing\r
127  * @note        Clears a pending timer match interrupt.\r
128  */\r
129 STATIC INLINE void IP_TIMER_ClearMatch(IP_TIMER_001_T *pTimer, int8_t matchnum)\r
130 {\r
131         pTimer->IR = TIMER_IR_CLR(matchnum);\r
132 }\r
133 \r
134 /**\r
135  * @brief       Clears a (pending) capture interrupt\r
136  * @param       pTimer  : Pointer to timer IP register address\r
137  * @param       capnum  : Capture interrupt number to clear\r
138  * @return      Nothing\r
139  * @note        Clears a pending timer capture interrupt.\r
140  */\r
141 STATIC INLINE void IP_TIMER_ClearCapture(IP_TIMER_001_T *pTimer, int8_t capnum)\r
142 {\r
143         pTimer->IR = (0x10 << capnum);\r
144 }\r
145 \r
146 /**\r
147  * @brief       Enables the timer (starts count)\r
148  * @param       pTimer  : Pointer to timer IP register address\r
149  * @return      Nothing\r
150  * @note        Enables the timer to start counting.\r
151  */\r
152 STATIC INLINE void IP_TIMER_Enable(IP_TIMER_001_T *pTimer)\r
153 {\r
154         pTimer->TCR |= TIMER_ENABLE;\r
155 }\r
156 \r
157 /**\r
158  * @brief       Disables the timer (stops count)\r
159  * @param       pTimer  : Pointer to timer IP register address\r
160  * @return      Nothing\r
161  * @note        Disables the timer to stop counting.\r
162  */\r
163 STATIC INLINE void IP_TIMER_Disable(IP_TIMER_001_T *pTimer)\r
164 {\r
165         pTimer->TCR &= ~TIMER_ENABLE;\r
166 }\r
167 \r
168 /**\r
169  * @brief       Returns the current timer count\r
170  * @param       pTimer  : Pointer to timer IP register address\r
171  * @return      Current timer terminal count value\r
172  * @note        Returns the current timer terminal count.\r
173  */\r
174 STATIC INLINE uint32_t IP_TIMER_ReadCount(IP_TIMER_001_T *pTimer)\r
175 {\r
176         return pTimer->TC;\r
177 }\r
178 \r
179 /**\r
180  * @brief  Returns the current prescale count\r
181  * @param       pTimer  : Pointer to timer IP register address\r
182  * @return Current timer prescale count value\r
183  * @note        Returns the current prescale count.\r
184  */\r
185 STATIC INLINE uint32_t IP_TIMER_ReadPrescale(IP_TIMER_001_T *pTimer)\r
186 {\r
187         return pTimer->PC;\r
188 }\r
189 \r
190 /**\r
191  * @brief       Sets the prescaler value\r
192  * @param       pTimer          : Pointer to timer IP register address\r
193  * @param       prescale        : Prescale value to set the prescale register to\r
194  * @return      Nothing\r
195  * @note        Sets the prescale count value.\r
196  */\r
197 STATIC INLINE void IP_TIMER_PrescaleSet(IP_TIMER_001_T *pTimer, uint32_t prescale)\r
198 {\r
199         pTimer->PR = prescale;\r
200 }\r
201 \r
202 /**\r
203  * @brief       Sets a timer match value\r
204  * @param       pTimer          : Pointer to timer IP register address\r
205  * @param       matchnum        : Match timer to set match count for\r
206  * @param       matchval        : Match value for the selected match count\r
207  * @return      Nothing\r
208  * @note        Sets ones of the timer match values.\r
209  */\r
210 STATIC INLINE void IP_TIMER_SetMatch(IP_TIMER_001_T *pTimer, int8_t matchnum, uint32_t matchval)\r
211 {\r
212         pTimer->MR[matchnum] = matchval;\r
213 }\r
214 \r
215 /**\r
216  * @brief       Reads a capture register\r
217  * @param       pTimer  : Pointer to timer IP register address\r
218  * @param       capnum  : Capture register to read\r
219  * @return      The selected capture register value\r
220  * @note        Returns the selected capture register value.\r
221  */\r
222 STATIC INLINE uint32_t IP_TIMER_ReadCapture(IP_TIMER_001_T *pTimer, int8_t capnum)\r
223 {\r
224         return pTimer->CR[capnum];\r
225 }\r
226 \r
227 /**\r
228  * @brief       Resets the timer terminal and prescale counts to 0\r
229  * @param       pTimer  : Pointer to timer IP register address\r
230  * @return      Nothing\r
231  */\r
232 void IP_TIMER_Reset(IP_TIMER_001_T *pTimer);\r
233 \r
234 /**\r
235  * @brief       Enables a match interrupt that fires when the terminal count\r
236  *                      matches the match counter value.\r
237  * @param       pTimer          : Pointer to timer IP register address\r
238  * @param       matchnum        : Match timer, 0 to 3\r
239  * @return      Nothing\r
240  */\r
241 STATIC INLINE void IP_TIMER_MatchEnableInt(IP_TIMER_001_T *pTimer, int8_t matchnum)\r
242 {\r
243         pTimer->MCR |= TIMER_INT_ON_MATCH(matchnum);\r
244 }\r
245 \r
246 /**\r
247  * @brief       Disables a match interrupt for a match counter.\r
248  * @param       pTimer          : Pointer to timer IP register address\r
249  * @param       matchnum        : Match timer, 0 to 3\r
250  * @return      Nothing\r
251  */\r
252 STATIC INLINE void IP_TIMER_MatchDisableInt(IP_TIMER_001_T *pTimer, int8_t matchnum)\r
253 {\r
254         pTimer->MCR &= ~TIMER_INT_ON_MATCH(matchnum);\r
255 }\r
256 \r
257 /**\r
258  * @brief       For the specific match counter, enables reset of the terminal count register when a match occurs\r
259  * @param       pTimer          : Pointer to timer IP register address\r
260  * @param       matchnum        : Match timer, 0 to 3\r
261  * @return      Nothing\r
262  */\r
263 STATIC INLINE void IP_TIMER_ResetOnMatchEnable(IP_TIMER_001_T *pTimer, int8_t matchnum)\r
264 {\r
265         pTimer->MCR |= TIMER_RESET_ON_MATCH(matchnum);\r
266 }\r
267 \r
268 /**\r
269  * @brief       For the specific match counter, disables reset of the terminal count register when a match occurs\r
270  * @param       pTimer          : Pointer to timer IP register address\r
271  * @param       matchnum        : Match timer, 0 to 3\r
272  * @return      Nothing\r
273  */\r
274 STATIC INLINE void IP_TIMER_ResetOnMatchDisable(IP_TIMER_001_T *pTimer, int8_t matchnum)\r
275 {\r
276         pTimer->MCR &= ~TIMER_RESET_ON_MATCH(matchnum);\r
277 }\r
278 \r
279 /**\r
280  * @brief       Enable a match timer to stop the terminal count when a\r
281  *                      match count equals the terminal count.\r
282  * @param       pTimer          : Pointer to timer IP register address\r
283  * @param       matchnum        : Match timer, 0 to 3\r
284  * @return      Nothing\r
285  */\r
286 STATIC INLINE void IP_TIMER_StopOnMatchEnable(IP_TIMER_001_T *pTimer, int8_t matchnum)\r
287 {\r
288         pTimer->MCR |= TIMER_STOP_ON_MATCH(matchnum);\r
289 }\r
290 \r
291 /**\r
292  * @brief       Disable stop on match for a match timer. Disables a match timer\r
293  *                      to stop the terminal count when a match count equals the terminal count.\r
294  * @param       pTimer          : Pointer to timer IP register address\r
295  * @param       matchnum        : Match timer, 0 to 3\r
296  * @return      Nothing\r
297  */\r
298 STATIC INLINE void IP_TIMER_StopOnMatchDisable(IP_TIMER_001_T *pTimer, int8_t matchnum)\r
299 {\r
300         pTimer->MCR &= ~TIMER_STOP_ON_MATCH(matchnum);\r
301 }\r
302 \r
303 /**\r
304  * @brief       Enables capture on on rising edge of selected CAP signal for the\r
305  *                      selected capture register, enables the selected CAPn.capnum signal to load\r
306  *                      the capture register with the terminal coount on a rising edge.\r
307  * @param       pTimer  : Pointer to timer IP register address\r
308  * @param       capnum  : Capture signal/register to use\r
309  * @return      Nothing\r
310  */\r
311 STATIC INLINE void IP_TIMER_CaptureRisingEdgeEnable(IP_TIMER_001_T *pTimer, int8_t capnum)\r
312 {\r
313         pTimer->CCR |= TIMER_CAP_RISING(capnum);\r
314 }\r
315 \r
316 /**\r
317  * @brief       Disables capture on on rising edge of selected CAP signal. For the\r
318  *                      selected capture register, disables the selected CAPn.capnum signal to load\r
319  *                      the capture register with the terminal coount on a rising edge.\r
320  * @param       pTimer  : Pointer to timer IP register address\r
321  * @param       capnum  : Capture signal/register to use\r
322  * @return      Nothing\r
323  */\r
324 STATIC INLINE void IP_TIMER_CaptureRisingEdgeDisable(IP_TIMER_001_T *pTimer, int8_t capnum)\r
325 {\r
326         pTimer->CCR &= ~TIMER_CAP_RISING(capnum);\r
327 }\r
328 \r
329 /**\r
330  * @brief       Enables capture on on falling edge of selected CAP signal. For the\r
331  *                      selected capture register, enables the selected CAPn.capnum signal to load\r
332  *                      the capture register with the terminal coount on a falling edge.\r
333  * @param       pTimer  : Pointer to timer IP register address\r
334  * @param       capnum  : Capture signal/register to use\r
335  * @return      Nothing\r
336  */\r
337 STATIC INLINE void IP_TIMER_CaptureFallingEdgeEnable(IP_TIMER_001_T *pTimer, int8_t capnum)\r
338 {\r
339         pTimer->CCR |= TIMER_CAP_FALLING(capnum);\r
340 }\r
341 \r
342 /**\r
343  * @brief       Disables capture on on falling edge of selected CAP signal. For the\r
344  *                      selected capture register, disables the selected CAPn.capnum signal to load\r
345  *                      the capture register with the terminal coount on a falling edge.\r
346  * @param       pTimer  : Pointer to timer IP register address\r
347  * @param       capnum  : Capture signal/register to use\r
348  * @return      Nothing\r
349  */\r
350 STATIC INLINE void IP_TIMER_CaptureFallingEdgeDisable(IP_TIMER_001_T *pTimer, int8_t capnum)\r
351 {\r
352         pTimer->CCR &= ~TIMER_CAP_FALLING(capnum);\r
353 }\r
354 \r
355 /**\r
356  * @brief       Enables interrupt on capture of selected CAP signal. For the\r
357  *                      selected capture register, an interrupt will be generated when the enabled\r
358  *                      rising or falling edge on CAPn.capnum is detected.\r
359  * @param       pTimer  : Pointer to timer IP register address\r
360  * @param       capnum  : Capture signal/register to use\r
361  * @return      Nothing\r
362  */\r
363 STATIC INLINE void IP_TIMER_CaptureEnableInt(IP_TIMER_001_T *pTimer, int8_t capnum)\r
364 {\r
365         pTimer->CCR |= TIMER_INT_ON_CAP(capnum);\r
366 }\r
367 \r
368 /**\r
369  * @brief       Disables interrupt on capture of selected CAP signal\r
370  * @param       pTimer  : Pointer to timer IP register address\r
371  * @param       capnum  : Capture signal/register to use\r
372  * @return      Nothing\r
373  */\r
374 STATIC INLINE void IP_TIMER_CaptureDisableInt(IP_TIMER_001_T *pTimer, int8_t capnum)\r
375 {\r
376         pTimer->CCR &= ~TIMER_INT_ON_CAP(capnum);\r
377 }\r
378 \r
379 /**\r
380  * @brief Standard timer initial match pin state and change state\r
381  */\r
382 typedef enum IP_TIMER_PIN_MATCH_STATE {\r
383         TIMER_EXTMATCH_DO_NOTHING = 0,  /*!< Timer match state does nothing on match pin */\r
384         TIMER_EXTMATCH_CLEAR      = 1,  /*!< Timer match state sets match pin low */\r
385         TIMER_EXTMATCH_SET        = 2,  /*!< Timer match state sets match pin high */\r
386         TIMER_EXTMATCH_TOGGLE     = 3   /*!< Timer match state toggles match pin */\r
387 } IP_TIMER_PIN_MATCH_STATE_T;\r
388 \r
389 /**\r
390  * @brief       Sets external match control (MATn.matchnum) pin control\r
391  * @param       pTimer                  : Pointer to timer IP register address\r
392  * @param       initial_state   : Initial state of the pin, high(1) or low(0)\r
393  * @param       matchState              : Selects the match state for the pin\r
394  * @param       matchnum                : MATn.matchnum signal to use\r
395  * @return      Nothing\r
396  * @note        For the pin selected with matchnum, sets the function of the pin that occurs on\r
397  * a terminal count match for the match count.\r
398  */\r
399 void IP_TIMER_ExtMatchControlSet(IP_TIMER_001_T *pTimer, int8_t initial_state,\r
400                                                                  IP_TIMER_PIN_MATCH_STATE_T matchState, int8_t matchnum);\r
401 \r
402 /**\r
403  * @brief Standard timer clock and edge for count source\r
404  */\r
405 typedef enum IP_TIMER_CAP_SRC_STATE {\r
406         TIMER_CAPSRC_RISING_PCLK  = 0,  /*!< Timer ticks on PCLK rising edge */\r
407         TIMER_CAPSRC_RISING_CAPN  = 1,  /*!< Timer ticks on CAPn.x rising edge */\r
408         TIMER_CAPSRC_FALLING_CAPN = 2,  /*!< Timer ticks on CAPn.x falling edge */\r
409         TIMER_CAPSRC_BOTH_CAPN    = 3   /*!< Timer ticks on CAPn.x both edges */\r
410 } IP_TIMER_CAP_SRC_STATE_T;\r
411 \r
412 /**\r
413  * @brief       Sets timer count source and edge with the selected passed from CapSrc\r
414  * @param       pTimer  : Pointer to timer IP register address\r
415  * @param       capSrc  : timer clock source and edge\r
416  * @param       capnum  : CAPn.capnum pin to use (if used)\r
417  * @return      Nothing\r
418  * @note        If CapSrc selected a CAPn pin, select the specific CAPn pin with the capnum value.\r
419  */\r
420 void IP_TIMER_SetCountClockSrc(IP_TIMER_001_T *pTimer,\r
421                                                            IP_TIMER_CAP_SRC_STATE_T capSrc, int8_t capnum);\r
422 \r
423 /**\r
424  * @}\r
425  */\r
426 \r
427 #ifdef __cplusplus\r
428 }\r
429 #endif\r
430 \r
431 #endif /* __TIMER_001_H_ */\r