]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC/ThirdParty/CMSISv2p10_LPC18xx_DriverLib/src/lpc18xx_timer.c
Update copyright date ready for tagging V10.1.0.
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_UDP_and_CLI_LPC1830_GCC / ThirdParty / CMSISv2p10_LPC18xx_DriverLib / src / lpc18xx_timer.c
1 /**********************************************************************\r
2 * $Id$          lpc18xx_timer.c         2011-06-02\r
3 *//**\r
4 * @file         lpc18xx_timer.c\r
5 * @brief        Contains all functions support for Timer firmware library on LPC18xx\r
6 * @version      1.0\r
7 * @date         02. June. 2011\r
8 * @author       NXP MCU SW Application Team\r
9 *\r
10 * Copyright(C) 2011, NXP Semiconductor\r
11 * All rights reserved.\r
12 *\r
13 ***********************************************************************\r
14 * Software that is described herein is for illustrative purposes only\r
15 * which provides customers with programming information regarding the\r
16 * products. This software is supplied "AS IS" without any warranties.\r
17 * NXP Semiconductors assumes no responsibility or liability for the\r
18 * use of the software, conveys no license or title under any patent,\r
19 * copyright, or mask work right to the product. NXP Semiconductors\r
20 * reserves the right to make changes in the software without\r
21 * notification. NXP Semiconductors also make no representation or\r
22 * warranty that such application will be suitable for the specified\r
23 * use without further testing or modification.\r
24 **********************************************************************/\r
25 \r
26 /* Peripheral group ----------------------------------------------------------- */\r
27 /** @addtogroup TIMER\r
28  * @{\r
29  */\r
30 \r
31 /* Includes ------------------------------------------------------------------- */\r
32 #include "lpc18xx_timer.h"\r
33 #include "lpc18xx_cgu.h"\r
34 \r
35 /* If this source file built with example, the LPC18xx FW library configuration\r
36  * file in each example directory ("lpc18xx_libcfg.h") must be included,\r
37  * otherwise the default FW library configuration file must be included instead\r
38  */\r
39 #ifdef __BUILD_WITH_EXAMPLE__\r
40 #include "lpc18xx_libcfg.h"\r
41 #else\r
42 #include "lpc18xx_libcfg_default.h"\r
43 #endif /* __BUILD_WITH_EXAMPLE__ */\r
44 \r
45 #ifdef _TIM\r
46 \r
47 /* Private Functions ---------------------------------------------------------- */\r
48 \r
49 static uint32_t getPClock (uint32_t timernum);\r
50 static uint32_t converUSecToVal (uint32_t timernum, uint32_t usec);\r
51 static uint32_t converPtrToTimeNum (LPC_TIMERn_Type *TIMx);\r
52 \r
53 \r
54 /*********************************************************************//**\r
55  * @brief               Get peripheral clock of each timer controller\r
56  * @param[in]   timernum Timer number, should be: 0..3\r
57  * @return              Peripheral clock of timer\r
58  **********************************************************************/\r
59 extern uint32_t M3Frequency;\r
60 static uint32_t getPClock (uint32_t timernum)\r
61 {\r
62         uint32_t clkdlycnt;\r
63         switch (timernum)\r
64         {\r
65         case 0:\r
66                 clkdlycnt = /*CGU_GetPCLK (CGU_PCLKSEL_TIMER0)*/ CGU_GetPCLKFrequency(CGU_PERIPHERAL_TIMER0);\r
67                 break;\r
68 \r
69         case 1:\r
70                 clkdlycnt = /*CGU_GetPCLK (CGU_PCLKSEL_TIMER1)*/ CGU_GetPCLKFrequency(CGU_PERIPHERAL_TIMER1);\r
71                 break;\r
72 \r
73         case 2:\r
74                 clkdlycnt = /*CGU_GetPCLK (CGU_PCLKSEL_TIMER2)*/ CGU_GetPCLKFrequency(CGU_PERIPHERAL_TIMER2);\r
75                 break;\r
76 \r
77         case 3:\r
78                 clkdlycnt = /*CGU_GetPCLK (CGU_PCLKSEL_TIMER3)*/ CGU_GetPCLKFrequency(CGU_PERIPHERAL_TIMER3);\r
79                 break;\r
80         }\r
81         return clkdlycnt;\r
82 }\r
83 \r
84 \r
85 /*********************************************************************//**\r
86  * @brief               Convert a time to a timer count value\r
87  * @param[in]   timernum Timer number, should be: 0..3\r
88  * @param[in]   usec Time in microseconds\r
89  * @return              The number of required clock ticks to give the time delay\r
90  **********************************************************************/\r
91 uint32_t converUSecToVal (uint32_t timernum, uint32_t usec)\r
92 {\r
93         uint64_t clkdlycnt;\r
94 \r
95         // Get Pclock of timer\r
96         clkdlycnt = (uint64_t) getPClock(timernum);\r
97 \r
98         clkdlycnt = (clkdlycnt * usec) / 1000000;\r
99         return (uint32_t) clkdlycnt;\r
100 }\r
101 \r
102 \r
103 /*********************************************************************//**\r
104  * @brief               Convert a timer register pointer to a timer number\r
105  * @param[in]   TIMx Pointer to LPC_TIMERn_Type, should be:\r
106  *                                      - LPC_TIM0      :TIMER0 peripheral\r
107  *                                      - LPC_TIM1      :TIMER1 peripheral\r
108  *                                      - LPC_TIM2      :TIMER2 peripheral\r
109  *                                      - LPC_TIM3      :TIMER3 peripheral\r
110  * @return              The timer number (0 to 3) or -1 if register pointer is bad\r
111  **********************************************************************/\r
112 uint32_t converPtrToTimeNum (LPC_TIMERn_Type *TIMx)\r
113 {\r
114         uint32_t tnum = 0xFFFFFFFF;\r
115 \r
116         if (TIMx == LPC_TIMER0)\r
117         {\r
118                 tnum = 0;\r
119         }\r
120         else if (TIMx == LPC_TIMER1)\r
121         {\r
122                 tnum = 1;\r
123         }\r
124         else if (TIMx == LPC_TIMER2)\r
125         {\r
126                 tnum = 2;\r
127         }\r
128         else if (TIMx == LPC_TIMER3)\r
129         {\r
130                 tnum = 3;\r
131         }\r
132 \r
133         return tnum;\r
134 }\r
135 \r
136 /* End of Private Functions ---------------------------------------------------- */\r
137 \r
138 \r
139 /* Public Functions ----------------------------------------------------------- */\r
140 /** @addtogroup TIM_Public_Functions\r
141  * @{\r
142  */\r
143 \r
144 /*********************************************************************//**\r
145  * @brief               Get Interrupt Status\r
146  * @param[in]   TIMx Timer selection, should be:\r
147  *                                      - LPC_TIM0      :TIMER0 peripheral\r
148  *                                      - LPC_TIM1      :TIMER1 peripheral\r
149  *                                      - LPC_TIM2      :TIMER2 peripheral\r
150  *                                      - LPC_TIM3      :TIMER3 peripheral\r
151  * @param[in]   IntFlag: interrupt type, should be:\r
152  *                                      - TIM_MR0_INT   :Interrupt for Match channel 0\r
153  *                                      - TIM_MR1_INT   :Interrupt for Match channel 1\r
154  *                                      - TIM_MR2_INT   :Interrupt for Match channel 2\r
155  *                                      - TIM_MR3_INT   :Interrupt for Match channel 3\r
156  *                                      - TIM_CR0_INT   :Interrupt for Capture channel 0\r
157  *                                      - TIM_CR1_INT   :Interrupt for Capture channel 1\r
158  * @return              FlagStatus\r
159  *                                      - SET   :interrupt\r
160  *                                      - RESET :no interrupt\r
161  **********************************************************************/\r
162 FlagStatus TIM_GetIntStatus(LPC_TIMERn_Type *TIMx, TIM_INT_TYPE IntFlag)\r
163 {\r
164         uint8_t temp;\r
165         CHECK_PARAM(PARAM_TIMx(TIMx));\r
166         CHECK_PARAM(PARAM_TIM_INT_TYPE(IntFlag));\r
167         temp = (TIMx->IR)& TIM_IR_CLR(IntFlag);\r
168         if (temp)\r
169                 return SET;\r
170 \r
171         return RESET;\r
172 \r
173 }\r
174 /*********************************************************************//**\r
175  * @brief               Get Capture Interrupt Status\r
176  * @param[in]   TIMx Timer selection, should be:\r
177  *                                      - LPC_TIM0      :TIMER0 peripheral\r
178  *                                      - LPC_TIM1      :TIMER1 peripheral\r
179  *                                      - LPC_TIM2      :TIMER2 peripheral\r
180  *                                      - LPC_TIM3      :TIMER3 peripheral\r
181  * @param[in]   IntFlag: interrupt type, should be:\r
182  *                                      - TIM_MR0_INT   :Interrupt for Match channel 0\r
183  *                                      - TIM_MR1_INT   :Interrupt for Match channel 1\r
184  *                                      - TIM_MR2_INT   :Interrupt for Match channel 2\r
185  *                                      - TIM_MR3_INT   :Interrupt for Match channel 3\r
186  *                                      - TIM_CR0_INT   :Interrupt for Capture channel 0\r
187  *                                      - TIM_CR1_INT   :Interrupt for Capture channel 1\r
188  * @return              FlagStatus\r
189  *                                      - SET   :interrupt\r
190  *                                      - RESET :no interrupt\r
191  **********************************************************************/\r
192 FlagStatus TIM_GetIntCaptureStatus(LPC_TIMERn_Type *TIMx, TIM_INT_TYPE IntFlag)\r
193 {\r
194         uint8_t temp;\r
195         CHECK_PARAM(PARAM_TIMx(TIMx));\r
196         CHECK_PARAM(PARAM_TIM_INT_TYPE(IntFlag));\r
197         temp = (TIMx->IR) & (1<<(4+IntFlag));\r
198         if(temp)\r
199                 return SET;\r
200         return RESET;\r
201 }\r
202 /*********************************************************************//**\r
203  * @brief               Clear Interrupt pending\r
204  * @param[in]   TIMx Timer selection, should be:\r
205  *                                      - LPC_TIM0      :TIMER0 peripheral\r
206  *                                      - LPC_TIM1      :TIMER1 peripheral\r
207  *                                      - LPC_TIM2      :TIMER2 peripheral\r
208  *                                      - LPC_TIM3      :TIMER3 peripheral\r
209  * @param[in]   IntFlag: interrupt type, should be:\r
210  *                                      - TIM_MR0_INT   :Interrupt for Match channel 0\r
211  *                                      - TIM_MR1_INT   :Interrupt for Match channel 1\r
212  *                                      - TIM_MR2_INT   :Interrupt for Match channel 2\r
213  *                                      - TIM_MR3_INT   :Interrupt for Match channel 3\r
214  *                                      - TIM_CR0_INT   :Interrupt for Capture channel 0\r
215  *                                      - TIM_CR1_INT   :Interrupt for Capture channel 1\r
216  * @return              None\r
217  **********************************************************************/\r
218 void TIM_ClearIntPending(LPC_TIMERn_Type *TIMx, TIM_INT_TYPE IntFlag)\r
219 {\r
220         CHECK_PARAM(PARAM_TIMx(TIMx));\r
221         CHECK_PARAM(PARAM_TIM_INT_TYPE(IntFlag));\r
222         TIMx->IR = TIM_IR_CLR(IntFlag);\r
223 }\r
224 \r
225 /*********************************************************************//**\r
226  * @brief               Clear Capture Interrupt pending\r
227  * @param[in]   TIMx Timer selection, should be\r
228  *                                      - LPC_TIM0      :TIMER0 peripheral\r
229  *                                      - LPC_TIM1      :TIMER1 peripheral\r
230  *                                      - LPC_TIM2      :TIMER2 peripheral\r
231  *                                      - LPC_TIM3      :TIMER3 peripheral\r
232  * @param[in]   IntFlag interrupt type, should be:\r
233  *                                      - TIM_MR0_INT   :Interrupt for Match channel 0\r
234  *                                      - TIM_MR1_INT   :Interrupt for Match channel 1\r
235  *                                      - TIM_MR2_INT   :Interrupt for Match channel 2\r
236  *                                      - TIM_MR3_INT   :Interrupt for Match channel 3\r
237  *                                      - TIM_CR0_INT   :Interrupt for Capture channel 0\r
238  *                                      - TIM_CR1_INT   :Interrupt for Capture channel 1\r
239  * @return              None\r
240  **********************************************************************/\r
241 void TIM_ClearIntCapturePending(LPC_TIMERn_Type *TIMx, TIM_INT_TYPE IntFlag)\r
242 {\r
243         CHECK_PARAM(PARAM_TIMx(TIMx));\r
244         CHECK_PARAM(PARAM_TIM_INT_TYPE(IntFlag));\r
245         TIMx->IR = (1<<(4+IntFlag));\r
246 }\r
247 \r
248 /*********************************************************************//**\r
249  * @brief               Configuration for Timer at initial time\r
250  * @param[in]   TimerCounterMode timer counter mode, should be:\r
251  *                                      - TIM_TIMER_MODE                        :Timer mode\r
252  *                                      - TIM_COUNTER_RISING_MODE       :Counter rising mode\r
253  *                                      - TIM_COUNTER_FALLING_MODE      :Counter falling mode\r
254  *                                      - TIM_COUNTER_ANY_MODE          :Counter on both edges\r
255  * @param[in]   TIM_ConfigStruct pointer to TIM_TIMERCFG_Type or\r
256  *                              TIM_COUNTERCFG_Type\r
257  * @return              None\r
258  **********************************************************************/\r
259 void TIM_ConfigStructInit(TIM_MODE_OPT TimerCounterMode, void *TIM_ConfigStruct)\r
260 {\r
261         if (TimerCounterMode == TIM_TIMER_MODE )\r
262         {\r
263                 TIM_TIMERCFG_Type * pTimeCfg = (TIM_TIMERCFG_Type *)TIM_ConfigStruct;\r
264                 pTimeCfg->PrescaleOption = TIM_PRESCALE_USVAL;\r
265                 pTimeCfg->PrescaleValue = 1;\r
266         }\r
267         else\r
268         {\r
269                 TIM_COUNTERCFG_Type * pCounterCfg = (TIM_COUNTERCFG_Type *)TIM_ConfigStruct;\r
270                 pCounterCfg->CountInputSelect = TIM_COUNTER_INCAP0;\r
271         }\r
272 }\r
273 \r
274 /*********************************************************************//**\r
275  * @brief               Initial Timer/Counter device\r
276  *                                      Set Clock frequency for Timer\r
277  *                                      Set initial configuration for Timer\r
278  * @param[in]   TIMx  Timer selection, should be:\r
279  *                                      - LPC_TIM0      :TIMER0 peripheral\r
280  *                                      - LPC_TIM1      :TIMER1 peripheral\r
281  *                                      - LPC_TIM2      :TIMER2 peripheral\r
282  *                                      - LPC_TIM3      :TIMER3 peripheral\r
283  * @param[in]   TimerCounterMode Timer counter mode, should be:\r
284  *                                      - TIM_TIMER_MODE                        :Timer mode\r
285  *                                      - TIM_COUNTER_RISING_MODE       :Counter rising mode\r
286  *                                      - TIM_COUNTER_FALLING_MODE      :Counter falling mode\r
287  *                                      - TIM_COUNTER_ANY_MODE          :Counter on both edges\r
288  * @param[in]   TIM_ConfigStruct pointer to TIM_TIMERCFG_Type\r
289  *                              that contains the configuration information for the\r
290  *                    specified Timer peripheral.\r
291  * @return              None\r
292  **********************************************************************/\r
293 void TIM_Init(LPC_TIMERn_Type *TIMx, TIM_MODE_OPT TimerCounterMode, void *TIM_ConfigStruct)\r
294 {\r
295         TIM_TIMERCFG_Type *pTimeCfg;\r
296         TIM_COUNTERCFG_Type *pCounterCfg;\r
297 \r
298         CHECK_PARAM(PARAM_TIMx(TIMx));\r
299         CHECK_PARAM(PARAM_TIM_MODE_OPT(TimerCounterMode));\r
300 \r
301         //set power\r
302         if (TIMx== LPC_TIMER0)\r
303         {\r
304 \r
305         }\r
306         else if (TIMx== LPC_TIMER1)\r
307         {\r
308 \r
309         }\r
310 \r
311         else if (TIMx== LPC_TIMER2)\r
312         {\r
313 \r
314         }\r
315         else if (TIMx== LPC_TIMER3)\r
316         {\r
317 \r
318         }\r
319 \r
320         TIMx->CCR &= ~TIM_CTCR_MODE_MASK;\r
321         TIMx->CCR |= TIM_TIMER_MODE;\r
322 \r
323         TIMx->TC =0;\r
324         TIMx->PC =0;\r
325         TIMx->PR =0;\r
326         TIMx->TCR |= (1<<1); //Reset Counter\r
327         TIMx->TCR &= ~(1<<1); //release reset\r
328         if (TimerCounterMode == TIM_TIMER_MODE )\r
329         {\r
330                 pTimeCfg = (TIM_TIMERCFG_Type *)TIM_ConfigStruct;\r
331                 if (pTimeCfg->PrescaleOption  == TIM_PRESCALE_TICKVAL)\r
332                 {\r
333                         TIMx->PR   = pTimeCfg->PrescaleValue -1  ;\r
334                 }\r
335                 else\r
336                 {\r
337                         TIMx->PR   = converUSecToVal (converPtrToTimeNum(TIMx),pTimeCfg->PrescaleValue)-1;\r
338                 }\r
339         }\r
340         else\r
341         {\r
342 \r
343                 pCounterCfg = (TIM_COUNTERCFG_Type *)TIM_ConfigStruct;\r
344                 TIMx->CCR  &= ~TIM_CTCR_INPUT_MASK;\r
345                 if (pCounterCfg->CountInputSelect == TIM_COUNTER_INCAP1)\r
346                         TIMx->CCR |= _BIT(2);\r
347         }\r
348 \r
349         // Clear interrupt pending\r
350         TIMx->IR = 0xFFFFFFFF;\r
351 \r
352 }\r
353 \r
354 /*********************************************************************//**\r
355  * @brief               Close Timer/Counter device\r
356  * @param[in]   TIMx  Pointer to timer device, should be:\r
357  *                                      - LPC_TIM0      :TIMER0 peripheral\r
358  *                                      - LPC_TIM1      :TIMER1 peripheral\r
359  *                                      - LPC_TIM2      :TIMER2 peripheral\r
360  *                                      - LPC_TIM3      :TIMER3 peripheral\r
361  * @return              None\r
362  **********************************************************************/\r
363 void TIM_DeInit (LPC_TIMERn_Type *TIMx)\r
364 {\r
365         CHECK_PARAM(PARAM_TIMx(TIMx));\r
366         // Disable timer/counter\r
367         TIMx->TCR = 0x00;\r
368 \r
369 }\r
370 \r
371 /*********************************************************************//**\r
372  * @brief               Start/Stop Timer/Counter device\r
373  * @param[in]   TIMx Pointer to timer device, should be:\r
374  *                                      - LPC_TIM0      :TIMER0 peripheral\r
375  *                                      - LPC_TIM1      :TIMER1 peripheral\r
376  *                                      - LPC_TIM2      :TIMER2 peripheral\r
377  *                                      - LPC_TIM3      :TIMER3 peripheral\r
378  * @param[in]   NewState\r
379  *                                      - ENABLE        :Set timer enable\r
380  *                                      - DISABLE       :Disable timer\r
381  * @return              None\r
382  **********************************************************************/\r
383 void TIM_Cmd(LPC_TIMERn_Type *TIMx, FunctionalState NewState)\r
384 {\r
385         CHECK_PARAM(PARAM_TIMx(TIMx));\r
386         if (NewState == ENABLE)\r
387         {\r
388                 TIMx->TCR       |=  TIM_ENABLE;\r
389         }\r
390         else\r
391         {\r
392                 TIMx->TCR &= ~TIM_ENABLE;\r
393         }\r
394 }\r
395 \r
396 /*********************************************************************//**\r
397  * @brief               Reset Timer/Counter device,\r
398  *                                      Make TC and PC are synchronously reset on the next\r
399  *                                      positive edge of PCLK\r
400  * @param[in]   TIMx Pointer to timer device, should be:\r
401  *                                      - LPC_TIM0      :TIMER0 peripheral\r
402  *                                      - LPC_TIM1      :TIMER1 peripheral\r
403  *                                      - LPC_TIM2      :TIMER2 peripheral\r
404  *                                      - LPC_TIM3      :TIMER3 peripheral\r
405  * @return              None\r
406  **********************************************************************/\r
407 void TIM_ResetCounter(LPC_TIMERn_Type *TIMx)\r
408 {\r
409         CHECK_PARAM(PARAM_TIMx(TIMx));\r
410         TIMx->TCR |= TIM_RESET;\r
411         TIMx->TCR &= ~TIM_RESET;\r
412 }\r
413 \r
414 /*********************************************************************//**\r
415  * @brief               Configuration for Match register\r
416  * @param[in]   TIMx Pointer to timer device, should be:\r
417  *                                      - LPC_TIM0      :TIMER0 peripheral\r
418  *                                      - LPC_TIM1      :TIMER1 peripheral\r
419  *                                      - LPC_TIM2      :TIMER2 peripheral\r
420  *                                      - LPC_TIM3      :TIMER3 peripheral\r
421  * @param[in]   TIM_MatchConfigStruct Pointer to TIM_MATCHCFG_Type\r
422  *                                      - MatchChannel  : choose channel 0 or 1\r
423  *                                      - IntOnMatch    : if SET, interrupt will be generated when MRxx match\r
424  *                                                                      the value in TC\r
425  *                                      - StopOnMatch   : if SET, TC and PC will be stopped whenM Rxx match\r
426  *                                                                      the value in TC\r
427  *                                      - ResetOnMatch  : if SET, Reset on MR0 when MRxx match\r
428  *                                                                      the value in TC\r
429  *                                      -ExtMatchOutputType: Select output for external match\r
430  *                                               +       0:     Do nothing for external output pin if match\r
431  *                                               +   1: Force external output pin to low if match\r
432  *                                               +       2: Force external output pin to high if match\r
433  *                                               +       3: Toggle external output pin if match\r
434  *                                      MatchValue: Set the value to be compared with TC value\r
435  * @return              None\r
436  **********************************************************************/\r
437 void TIM_ConfigMatch(LPC_TIMERn_Type *TIMx, TIM_MATCHCFG_Type *TIM_MatchConfigStruct)\r
438 {\r
439 \r
440         CHECK_PARAM(PARAM_TIMx(TIMx));\r
441         CHECK_PARAM(PARAM_TIM_EXTMATCH_OPT(TIM_MatchConfigStruct->ExtMatchOutputType));\r
442 \r
443         switch(TIM_MatchConfigStruct->MatchChannel)\r
444         {\r
445         case 0:\r
446                 TIMx->MR[0] = TIM_MatchConfigStruct->MatchValue;\r
447                 break;\r
448         case 1:\r
449                 TIMx->MR[1] = TIM_MatchConfigStruct->MatchValue;\r
450                 break;\r
451         case 2:\r
452                 TIMx->MR[2] = TIM_MatchConfigStruct->MatchValue;\r
453                 break;\r
454         case 3:\r
455                 TIMx->MR[3] = TIM_MatchConfigStruct->MatchValue;\r
456                 break;\r
457         default:\r
458                 //Error match value\r
459                 //Error loop\r
460                 while(1);\r
461         }\r
462         //interrupt on MRn\r
463         TIMx->MCR &=~TIM_MCR_CHANNEL_MASKBIT(TIM_MatchConfigStruct->MatchChannel);\r
464 \r
465         if (TIM_MatchConfigStruct->IntOnMatch)\r
466                 TIMx->MCR |= TIM_INT_ON_MATCH(TIM_MatchConfigStruct->MatchChannel);\r
467 \r
468         //reset on MRn\r
469         if (TIM_MatchConfigStruct->ResetOnMatch)\r
470                 TIMx->MCR |= TIM_RESET_ON_MATCH(TIM_MatchConfigStruct->MatchChannel);\r
471 \r
472         //stop on MRn\r
473         if (TIM_MatchConfigStruct->StopOnMatch)\r
474                 TIMx->MCR |= TIM_STOP_ON_MATCH(TIM_MatchConfigStruct->MatchChannel);\r
475 \r
476         // match output type\r
477 \r
478         TIMx->EMR       &= ~TIM_EM_MASK(TIM_MatchConfigStruct->MatchChannel);\r
479         TIMx->EMR   |= TIM_EM_SET(TIM_MatchConfigStruct->MatchChannel,TIM_MatchConfigStruct->ExtMatchOutputType);\r
480 }\r
481 /*********************************************************************//**\r
482  * @brief               Update Match value\r
483  * @param[in]   TIMx Pointer to timer device, should be:\r
484  *                                      - LPC_TIM0      :TIMER0 peripheral\r
485  *                                      - LPC_TIM1      :TIMER1 peripheral\r
486  *                                      - LPC_TIM2      :TIMER2 peripheral\r
487  *                                      - LPC_TIM3      :TIMER3 peripheral\r
488  * @param[in]   MatchChannel    Match channel, should be: 0..3\r
489  * @param[in]   MatchValue              updated match value\r
490  * @return              None\r
491  **********************************************************************/\r
492 void TIM_UpdateMatchValue(LPC_TIMERn_Type *TIMx,uint8_t MatchChannel, uint32_t MatchValue)\r
493 {\r
494         CHECK_PARAM(PARAM_TIMx(TIMx));\r
495         switch(MatchChannel)\r
496         {\r
497         case 0:\r
498                 TIMx->MR[0] = MatchValue;\r
499                 break;\r
500         case 1:\r
501                 TIMx->MR[1] = MatchValue;\r
502                 break;\r
503         case 2:\r
504                 TIMx->MR[2] = MatchValue;\r
505                 break;\r
506         case 3:\r
507                 TIMx->MR[3] = MatchValue;\r
508                 break;\r
509         default:\r
510                 //Error Loop\r
511                 while(1);\r
512         }\r
513 \r
514 }\r
515 /*********************************************************************//**\r
516  * @brief               Configuration for Capture register\r
517  * @param[in]   TIMx Pointer to timer device, should be:\r
518  *                                      - LPC_TIM0      :TIMER0 peripheral\r
519  *                                      - LPC_TIM1      :TIMER1 peripheral\r
520  *                                      - LPC_TIM2      :TIMER2 peripheral\r
521  *                                      - LPC_TIM3      :TIMER3 peripheral\r
522  * @param[in]   TIM_CaptureConfigStruct Pointer to TIM_CAPTURECFG_Type\r
523  * @return              None\r
524  **********************************************************************/\r
525 void TIM_ConfigCapture(LPC_TIMERn_Type *TIMx, TIM_CAPTURECFG_Type *TIM_CaptureConfigStruct)\r
526 {\r
527 \r
528         CHECK_PARAM(PARAM_TIMx(TIMx));\r
529         TIMx->CCR &= ~TIM_CCR_CHANNEL_MASKBIT(TIM_CaptureConfigStruct->CaptureChannel);\r
530 \r
531         if (TIM_CaptureConfigStruct->RisingEdge)\r
532                 TIMx->CCR |= TIM_CAP_RISING(TIM_CaptureConfigStruct->CaptureChannel);\r
533 \r
534         if (TIM_CaptureConfigStruct->FallingEdge)\r
535                 TIMx->CCR |= TIM_CAP_FALLING(TIM_CaptureConfigStruct->CaptureChannel);\r
536 \r
537         if (TIM_CaptureConfigStruct->IntOnCaption)\r
538                 TIMx->CCR |= TIM_INT_ON_CAP(TIM_CaptureConfigStruct->CaptureChannel);\r
539 }\r
540 \r
541 /*********************************************************************//**\r
542  * @brief               Read value of capture register in timer/counter device\r
543  * @param[in]   TIMx Pointer to timer/counter device, should be:\r
544  *                                      - LPC_TIM0      :TIMER0 peripheral\r
545  *                                      - LPC_TIM1      :TIMER1 peripheral\r
546  *                                      - LPC_TIM2      :TIMER2 peripheral\r
547  *                                      - LPC_TIM3      :TIMER3 peripheral\r
548  * @param[in]   CaptureChannel: capture channel number, should be:\r
549  *                              - TIM_COUNTER_INCAP0: CAPn.0 input pin for TIMERn\r
550  *                              - TIM_COUNTER_INCAP1: CAPn.1 input pin for TIMERn\r
551  *                              - TIM_COUNTER_INCAP1: CAPn.2 input pin for TIMERn\r
552  *                              - TIM_COUNTER_INCAP1: CAPn.3 input pin for TIMERn\r
553  * @return              Value of capture register\r
554  **********************************************************************/\r
555 uint32_t TIM_GetCaptureValue(LPC_TIMERn_Type *TIMx, TIM_COUNTER_INPUT_OPT CaptureChannel)\r
556 {\r
557         CHECK_PARAM(PARAM_TIMx(TIMx));\r
558         CHECK_PARAM(PARAM_TIM_COUNTER_INPUT_OPT(CaptureChannel));\r
559 \r
560         switch(CaptureChannel){\r
561                 case 0: return TIMx->CR[0];\r
562                 case 1: return TIMx->CR[1];\r
563                 case 2: return TIMx->CR[2];\r
564                 case 3: return TIMx->CR[3];\r
565         }\r
566         return 0;\r
567 }\r
568 /*---------------Advanced TIMER functions -----------------------------------------*/\r
569 /*********************************************************************//**\r
570  * @brief               Timer wait (microseconds)\r
571  * @param[in]   time    number of microseconds waiting\r
572  * @return              None\r
573  **********************************************************************/\r
574 void TIM_Waitus(uint32_t time)\r
575 {\r
576         TIM_MATCHCFG_Type MatchConfigStruct;\r
577         LPC_TIMER0->IR = 0xFFFFFFFF;\r
578 \r
579         MatchConfigStruct.MatchChannel = 0;\r
580         MatchConfigStruct.IntOnMatch = ENABLE;\r
581         MatchConfigStruct.ResetOnMatch = ENABLE;\r
582         MatchConfigStruct.StopOnMatch = ENABLE;\r
583         MatchConfigStruct.ExtMatchOutputType = 0;\r
584         MatchConfigStruct.MatchValue = time;\r
585 \r
586         TIM_ConfigMatch(LPC_TIMER0, &MatchConfigStruct);\r
587         TIM_Cmd(LPC_TIMER0,ENABLE);\r
588         //wait until interrupt flag occur\r
589         while(!(LPC_TIMER0->IR & 0x01));\r
590         TIM_ResetCounter(LPC_TIMER0);\r
591 }\r
592 /*********************************************************************//**\r
593  * @brief               Timer wait (milliseconds)\r
594  * @param[in]   time    number of millisecond waiting\r
595  * @return              None\r
596  **********************************************************************/\r
597 void TIM_Waitms(uint32_t time)\r
598 {\r
599         TIM_Waitus(time * 1000);\r
600 }\r
601 /**\r
602  * @}\r
603  */\r
604 \r
605 #endif /* _TIMER */\r
606 \r
607 /**\r
608  * @}\r
609  */\r
610 \r
611 /* --------------------------------- End Of File ------------------------------ */\r