LPCOpen Platform for LPC18XX/43XX microcontrollers: C:/w/lpcopen_docs/software/lpc_core/lpc_chip/chip_18xx_43xx/timer_18xx_43xx.c Source File

| LPCOpen Platform for LPC18XX/43XX microcontrollers 18XX43XX LPCOpen Platform for the NXP LPC18XX/43XX family of Microcontrollers

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

timer_18xx_43xx.c
Go to the documentation of this file.
1 /*
2 * @brief LPC18xx/43xx 16/32-bit Timer/PWM driver
3 *
4 * @note
5 * Copyright(C) NXP Semiconductors, 2012
6 * All rights reserved.
7 *
8 * @par
9 * Software that is described herein is for illustrative purposes only
10 * which provides customers with programming information regarding the
11 * LPC products. This software is supplied "AS IS" without any warranties of
12 * any kind, and NXP Semiconductors and its licensor disclaim any and
13 * all warranties, express or implied, including all implied warranties of
14 * merchantability, fitness for a particular purpose and non-infringement of
15 * intellectual property rights. NXP Semiconductors assumes no responsibility
16 * or liability for the use of the software, conveys no license or rights under any
17 * patent, copyright, mask work right, or any other intellectual property rights in
18 * or to any products. NXP Semiconductors reserves the right to make changes
19 * in the software without notification. NXP Semiconductors also makes no
20 * representation or warranty that such application will be suitable for the
21 * specified use without further testing or modification.
22 *
23 * @par
24 * Permission to use, copy, modify, and distribute this software and its
25 * documentation is hereby granted, under NXP Semiconductors' and its
26 * licensor's relevant copyrights in the software, without fee, provided that it
27 * is used in conjunction with NXP Semiconductors microcontrollers. This
28 * copyright, permission, and disclaimer notice must appear in all copies of
29 * this code.
30 */
31
32 #include "chip.h"
33
34 /*****************************************************************************
35 * Private types/enumerations/variables
36 ****************************************************************************/
37
38 /*****************************************************************************
39 * Public types/enumerations/variables
40 ****************************************************************************/
41
42 /*****************************************************************************
43 * Private functions
44 ****************************************************************************/
45
46 /* Returns clock index for the peripheral block */
47 STATIC CHIP_CCU_CLK_T Chip_TIMER_GetClockIndex(LPC_TIMER_T *pTMR)
48 {
49 CHIP_CCU_CLK_T clkTMR;
50
51 if (pTMR == LPC_TIMER3) {
52 clkTMR = CLK_MX_TIMER3;
53 }
54 else if (pTMR == LPC_TIMER2) {
55 clkTMR = CLK_MX_TIMER2;
56 }
57 else if (pTMR == LPC_TIMER1) {
58 clkTMR = CLK_MX_TIMER1;
59 }
60 else {
61 clkTMR = CLK_MX_TIMER0;
62 }
63
64 return clkTMR;
65 }
66
67 /*****************************************************************************
68 * Public functions
69 ****************************************************************************/
70
71 /* Initialize a timer */
72 void Chip_TIMER_Init(LPC_TIMER_T *pTMR)
73 {
74 Chip_Clock_Enable(Chip_TIMER_GetClockIndex(pTMR));
75 }
76
77 /* Shutdown a timer */
78 void Chip_TIMER_DeInit(LPC_TIMER_T *pTMR)
79 {
80 Chip_Clock_Disable(Chip_TIMER_GetClockIndex(pTMR));
81 }
82
83 /* Resets the timer terminal and prescale counts to 0 */
84 void Chip_TIMER_Reset(LPC_TIMER_T *pTMR)
85 {
86 uint32_t reg;
87
88 /* Disable timer, set terminal count to non-0 */
89 reg = pTMR->TCR;
90 pTMR->TCR = 0;
91 pTMR->TC = 1;
92
93 /* Reset timer counter */
94 pTMR->TCR = TIMER_RESET;
95
96 /* Wait for terminal count to clear */
97 while (pTMR->TC != 0) {}
98
99 /* Restore timer state */
100 pTMR->TCR = reg;
101 }
102
103 /* Sets external match control (MATn.matchnum) pin control */
104 void Chip_TIMER_ExtMatchControlSet(LPC_TIMER_T *pTMR, int8_t initial_state,
105 TIMER_PIN_MATCH_STATE_T matchState, int8_t matchnum)
106 {
107 uint32_t mask, reg;
108
109 /* Clear bits corresponding to selected match register */
110 mask = (1 << matchnum) | (0x03 << (4 + (matchnum * 2)));
111 reg = pTMR->EMR &= ~mask;
112
113 /* Set new configuration for selected match register */
114 pTMR->EMR = reg | (((uint32_t) initial_state) << matchnum) |
115 (((uint32_t) matchState) << (4 + (matchnum * 2)));
116 }
117