2 * @brief 32-bit Timer/PWM registers and control functions
\r
5 * Copyright(C) NXP Semiconductors, 2012
\r
6 * All rights reserved.
\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
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
32 #ifndef __TIMER_001_H_
\r
33 #define __TIMER_001_H_
\r
35 #include "sys_config.h"
\r
42 /** @defgroup IP_TIMER_001 IP: Timer register block and driver
\r
43 * @ingroup IP_Drivers
\r
48 * @brief 32-bit Standard timer register block structure
\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
69 /** Macro to clear interrupt pending */
\r
70 #define TIMER_IR_CLR(n) _BIT(n)
\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
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
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
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
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
104 STATIC INLINE bool IP_TIMER_MatchPending(IP_TIMER_001_T *pTimer, int8_t matchnum)
\r
106 return (bool) ((pTimer->IR & TIMER_MATCH_INT(matchnum)) != 0);
\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
117 STATIC INLINE bool IP_TIMER_CapturePending(IP_TIMER_001_T *pTimer, int8_t capnum)
\r
119 return (bool) ((pTimer->IR & TIMER_CAP_INT(capnum)) != 0);
\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
127 * @note Clears a pending timer match interrupt.
\r
129 STATIC INLINE void IP_TIMER_ClearMatch(IP_TIMER_001_T *pTimer, int8_t matchnum)
\r
131 pTimer->IR = TIMER_IR_CLR(matchnum);
\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
139 * @note Clears a pending timer capture interrupt.
\r
141 STATIC INLINE void IP_TIMER_ClearCapture(IP_TIMER_001_T *pTimer, int8_t capnum)
\r
143 pTimer->IR = (0x10 << capnum);
\r
147 * @brief Enables the timer (starts count)
\r
148 * @param pTimer : Pointer to timer IP register address
\r
150 * @note Enables the timer to start counting.
\r
152 STATIC INLINE void IP_TIMER_Enable(IP_TIMER_001_T *pTimer)
\r
154 pTimer->TCR |= TIMER_ENABLE;
\r
158 * @brief Disables the timer (stops count)
\r
159 * @param pTimer : Pointer to timer IP register address
\r
161 * @note Disables the timer to stop counting.
\r
163 STATIC INLINE void IP_TIMER_Disable(IP_TIMER_001_T *pTimer)
\r
165 pTimer->TCR &= ~TIMER_ENABLE;
\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
174 STATIC INLINE uint32_t IP_TIMER_ReadCount(IP_TIMER_001_T *pTimer)
\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
185 STATIC INLINE uint32_t IP_TIMER_ReadPrescale(IP_TIMER_001_T *pTimer)
\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
195 * @note Sets the prescale count value.
\r
197 STATIC INLINE void IP_TIMER_PrescaleSet(IP_TIMER_001_T *pTimer, uint32_t prescale)
\r
199 pTimer->PR = prescale;
\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
208 * @note Sets ones of the timer match values.
\r
210 STATIC INLINE void IP_TIMER_SetMatch(IP_TIMER_001_T *pTimer, int8_t matchnum, uint32_t matchval)
\r
212 pTimer->MR[matchnum] = matchval;
\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
222 STATIC INLINE uint32_t IP_TIMER_ReadCapture(IP_TIMER_001_T *pTimer, int8_t capnum)
\r
224 return pTimer->CR[capnum];
\r
228 * @brief Resets the timer terminal and prescale counts to 0
\r
229 * @param pTimer : Pointer to timer IP register address
\r
232 void IP_TIMER_Reset(IP_TIMER_001_T *pTimer);
\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
241 STATIC INLINE void IP_TIMER_MatchEnableInt(IP_TIMER_001_T *pTimer, int8_t matchnum)
\r
243 pTimer->MCR |= TIMER_INT_ON_MATCH(matchnum);
\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
252 STATIC INLINE void IP_TIMER_MatchDisableInt(IP_TIMER_001_T *pTimer, int8_t matchnum)
\r
254 pTimer->MCR &= ~TIMER_INT_ON_MATCH(matchnum);
\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
263 STATIC INLINE void IP_TIMER_ResetOnMatchEnable(IP_TIMER_001_T *pTimer, int8_t matchnum)
\r
265 pTimer->MCR |= TIMER_RESET_ON_MATCH(matchnum);
\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
274 STATIC INLINE void IP_TIMER_ResetOnMatchDisable(IP_TIMER_001_T *pTimer, int8_t matchnum)
\r
276 pTimer->MCR &= ~TIMER_RESET_ON_MATCH(matchnum);
\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
286 STATIC INLINE void IP_TIMER_StopOnMatchEnable(IP_TIMER_001_T *pTimer, int8_t matchnum)
\r
288 pTimer->MCR |= TIMER_STOP_ON_MATCH(matchnum);
\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
298 STATIC INLINE void IP_TIMER_StopOnMatchDisable(IP_TIMER_001_T *pTimer, int8_t matchnum)
\r
300 pTimer->MCR &= ~TIMER_STOP_ON_MATCH(matchnum);
\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
311 STATIC INLINE void IP_TIMER_CaptureRisingEdgeEnable(IP_TIMER_001_T *pTimer, int8_t capnum)
\r
313 pTimer->CCR |= TIMER_CAP_RISING(capnum);
\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
324 STATIC INLINE void IP_TIMER_CaptureRisingEdgeDisable(IP_TIMER_001_T *pTimer, int8_t capnum)
\r
326 pTimer->CCR &= ~TIMER_CAP_RISING(capnum);
\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
337 STATIC INLINE void IP_TIMER_CaptureFallingEdgeEnable(IP_TIMER_001_T *pTimer, int8_t capnum)
\r
339 pTimer->CCR |= TIMER_CAP_FALLING(capnum);
\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
350 STATIC INLINE void IP_TIMER_CaptureFallingEdgeDisable(IP_TIMER_001_T *pTimer, int8_t capnum)
\r
352 pTimer->CCR &= ~TIMER_CAP_FALLING(capnum);
\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
363 STATIC INLINE void IP_TIMER_CaptureEnableInt(IP_TIMER_001_T *pTimer, int8_t capnum)
\r
365 pTimer->CCR |= TIMER_INT_ON_CAP(capnum);
\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
374 STATIC INLINE void IP_TIMER_CaptureDisableInt(IP_TIMER_001_T *pTimer, int8_t capnum)
\r
376 pTimer->CCR &= ~TIMER_INT_ON_CAP(capnum);
\r
380 * @brief Standard timer initial match pin state and change state
\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
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
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
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
403 * @brief Standard timer clock and edge for count source
\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
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
418 * @note If CapSrc selected a CAPn pin, select the specific CAPn pin with the capnum value.
\r
420 void IP_TIMER_SetCountClockSrc(IP_TIMER_001_T *pTimer,
\r
421 IP_TIMER_CAP_SRC_STATE_T capSrc, int8_t capnum);
\r
431 #endif /* __TIMER_001_H_ */
\r