uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lpc17xx_timer.c
Go to the documentation of this file.
1 /***********************************************************************/
20 /* Peripheral group ----------------------------------------------------------- */
25 /* Includes ------------------------------------------------------------------- */
26 #include "lpc17xx_timer.h"
27 #include "lpc17xx_clkpwr.h"
28 #include "lpc17xx_pinsel.h"
29 
30 /* If this source file built with example, the LPC17xx FW library configuration
31  * file in each example directory ("lpc17xx_libcfg.h") must be included,
32  * otherwise the default FW library configuration file must be included instead
33  */
34 #ifdef __BUILD_WITH_EXAMPLE__
35 #include "lpc17xx_libcfg.h"
36 #else
37 #include "lpc17xx_libcfg_default.h"
38 #endif /* __BUILD_WITH_EXAMPLE__ */
39 
40 #ifdef _TIM
41 
42 /* Private Functions ---------------------------------------------------------- */
43 
44 static uint32_t getPClock (uint32_t timernum);
45 static uint32_t converUSecToVal (uint32_t timernum, uint32_t usec);
46 static uint32_t converPtrToTimeNum (LPC_TIM_TypeDef *TIMx);
47 
48 
49 /*********************************************************************/
54 static uint32_t getPClock (uint32_t timernum)
55 {
56  uint32_t clkdlycnt = 0;
57  switch (timernum)
58  {
59  case 0:
61  break;
62 
63  case 1:
65  break;
66 
67  case 2:
69  break;
70 
71  case 3:
73  break;
74  }
75  return clkdlycnt;
76 }
77 
78 
79 /*********************************************************************/
85 uint32_t converUSecToVal (uint32_t timernum, uint32_t usec)
86 {
87  uint64_t clkdlycnt;
88 
89  // Get Pclock of timer
90  clkdlycnt = (uint64_t) getPClock(timernum);
91 
92  clkdlycnt = (clkdlycnt * usec) / 1000000;
93  return (uint32_t) clkdlycnt;
94 }
95 
96 
97 /*********************************************************************/
106 uint32_t converPtrToTimeNum (LPC_TIM_TypeDef *TIMx)
107 {
108  uint32_t tnum = -1;
109 
110  if (TIMx == LPC_TIM0)
111  {
112  tnum = 0;
113  }
114  else if (TIMx == LPC_TIM1)
115  {
116  tnum = 1;
117  }
118  else if (TIMx == LPC_TIM2)
119  {
120  tnum = 2;
121  }
122  else if (TIMx == LPC_TIM3)
123  {
124  tnum = 3;
125  }
126 
127  return tnum;
128 }
129 
130 /* End of Private Functions ---------------------------------------------------- */
131 
132 
133 /* Public Functions ----------------------------------------------------------- */
138 /*********************************************************************/
157 {
158  uint8_t temp;
159  CHECK_PARAM(PARAM_TIMx(TIMx));
161  temp = (TIMx->IR)& TIM_IR_CLR(IntFlag);
162  if (temp)
163  return SET;
164 
165  return RESET;
166 
167 }
168 /*********************************************************************/
187 {
188  uint8_t temp;
189  CHECK_PARAM(PARAM_TIMx(TIMx));
191  temp = (TIMx->IR) & (1<<(4+IntFlag));
192  if(temp)
193  return SET;
194  return RESET;
195 }
196 /*********************************************************************/
213 {
214  CHECK_PARAM(PARAM_TIMx(TIMx));
216  TIMx->IR |= TIM_IR_CLR(IntFlag);
217 }
218 
219 /*********************************************************************/
236 {
237  CHECK_PARAM(PARAM_TIMx(TIMx));
239  TIMx->IR |= (1<<(4+IntFlag));
240 }
241 
242 /*********************************************************************/
253 void TIM_ConfigStructInit(TIM_MODE_OPT TimerCounterMode, void *TIM_ConfigStruct)
254 {
255  if (TimerCounterMode == TIM_TIMER_MODE )
256  {
257  TIM_TIMERCFG_Type * pTimeCfg = (TIM_TIMERCFG_Type *)TIM_ConfigStruct;
259  pTimeCfg->PrescaleValue = 1;
260  }
261  else
262  {
263  TIM_COUNTERCFG_Type * pCounterCfg = (TIM_COUNTERCFG_Type *)TIM_ConfigStruct;
264  pCounterCfg->CountInputSelect = TIM_COUNTER_INCAP0;
265  }
266 }
267 
268 /*********************************************************************/
287 void TIM_Init(LPC_TIM_TypeDef *TIMx, TIM_MODE_OPT TimerCounterMode, void *TIM_ConfigStruct)
288 {
289  TIM_TIMERCFG_Type *pTimeCfg;
290  TIM_COUNTERCFG_Type *pCounterCfg;
291 
292  CHECK_PARAM(PARAM_TIMx(TIMx));
293  CHECK_PARAM(PARAM_TIM_MODE_OPT(TimerCounterMode));
294 
295  //set power
296  if (TIMx== LPC_TIM0)
297  {
300  }
301  else if (TIMx== LPC_TIM1)
302  {
305 
306  }
307 
308  else if (TIMx== LPC_TIM2)
309  {
312  }
313  else if (TIMx== LPC_TIM3)
314  {
317 
318  }
319 
320  TIMx->CCR &= ~TIM_CTCR_MODE_MASK;
321  TIMx->CCR |= TIM_TIMER_MODE;
322 
323  TIMx->TC =0;
324  TIMx->PC =0;
325  TIMx->PR =0;
326  TIMx->TCR |= (1<<1); //Reset Counter
327  TIMx->TCR &= ~(1<<1); //release reset
328  if (TimerCounterMode == TIM_TIMER_MODE )
329  {
330  pTimeCfg = (TIM_TIMERCFG_Type *)TIM_ConfigStruct;
331  if (pTimeCfg->PrescaleOption == TIM_PRESCALE_TICKVAL)
332  {
333  TIMx->PR = pTimeCfg->PrescaleValue -1 ;
334  }
335  else
336  {
337  TIMx->PR = converUSecToVal (converPtrToTimeNum(TIMx),pTimeCfg->PrescaleValue)-1;
338  }
339  }
340  else
341  {
342 
343  pCounterCfg = (TIM_COUNTERCFG_Type *)TIM_ConfigStruct;
344  TIMx->CCR &= ~TIM_CTCR_INPUT_MASK;
345  if (pCounterCfg->CountInputSelect == TIM_COUNTER_INCAP1)
346  TIMx->CCR |= _BIT(2);
347  }
348 
349  // Clear interrupt pending
350  TIMx->IR = 0xFFFFFFFF;
351 
352 }
353 
354 /*********************************************************************/
363 void TIM_DeInit (LPC_TIM_TypeDef *TIMx)
364 {
365  CHECK_PARAM(PARAM_TIMx(TIMx));
366  // Disable timer/counter
367  TIMx->TCR = 0x00;
368 
369  // Disable power
370  if (TIMx== LPC_TIM0)
372 
373  else if (TIMx== LPC_TIM1)
375 
376  else if (TIMx== LPC_TIM2)
378 
379  else if (TIMx== LPC_TIM3)
381 
382 }
383 
384 /*********************************************************************/
396 void TIM_Cmd(LPC_TIM_TypeDef *TIMx, FunctionalState NewState)
397 {
398  CHECK_PARAM(PARAM_TIMx(TIMx));
399  if (NewState == ENABLE)
400  {
401  TIMx->TCR |= TIM_ENABLE;
402  }
403  else
404  {
405  TIMx->TCR &= ~TIM_ENABLE;
406  }
407 }
408 
409 /*********************************************************************/
421 {
422  CHECK_PARAM(PARAM_TIMx(TIMx));
423  TIMx->TCR |= TIM_RESET;
424  TIMx->TCR &= ~TIM_RESET;
425 }
426 
427 /*********************************************************************/
450 void TIM_ConfigMatch(LPC_TIM_TypeDef *TIMx, TIM_MATCHCFG_Type *TIM_MatchConfigStruct)
451 {
452 
453  CHECK_PARAM(PARAM_TIMx(TIMx));
454  CHECK_PARAM(PARAM_TIM_EXTMATCH_OPT(TIM_MatchConfigStruct->ExtMatchOutputType));
455 
456  switch(TIM_MatchConfigStruct->MatchChannel)
457  {
458  case 0:
459  TIMx->MR0 = TIM_MatchConfigStruct->MatchValue;
460  break;
461  case 1:
462  TIMx->MR1 = TIM_MatchConfigStruct->MatchValue;
463  break;
464  case 2:
465  TIMx->MR2 = TIM_MatchConfigStruct->MatchValue;
466  break;
467  case 3:
468  TIMx->MR3 = TIM_MatchConfigStruct->MatchValue;
469  break;
470  default:
471  //Error match value
472  //Error loop
473  while(1);
474  }
475  //interrupt on MRn
476  TIMx->MCR &=~TIM_MCR_CHANNEL_MASKBIT(TIM_MatchConfigStruct->MatchChannel);
477 
478  if (TIM_MatchConfigStruct->IntOnMatch)
479  TIMx->MCR |= TIM_INT_ON_MATCH(TIM_MatchConfigStruct->MatchChannel);
480 
481  //reset on MRn
482  if (TIM_MatchConfigStruct->ResetOnMatch)
483  TIMx->MCR |= TIM_RESET_ON_MATCH(TIM_MatchConfigStruct->MatchChannel);
484 
485  //stop on MRn
486  if (TIM_MatchConfigStruct->StopOnMatch)
487  TIMx->MCR |= TIM_STOP_ON_MATCH(TIM_MatchConfigStruct->MatchChannel);
488 
489  // match output type
490 
491  TIMx->EMR &= ~TIM_EM_MASK(TIM_MatchConfigStruct->MatchChannel);
492  TIMx->EMR |= TIM_EM_SET(TIM_MatchConfigStruct->MatchChannel,TIM_MatchConfigStruct->ExtMatchOutputType);
493 }
494 /*********************************************************************/
505 void TIM_UpdateMatchValue(LPC_TIM_TypeDef *TIMx,uint8_t MatchChannel, uint32_t MatchValue)
506 {
507  CHECK_PARAM(PARAM_TIMx(TIMx));
508  switch(MatchChannel)
509  {
510  case 0:
511  TIMx->MR0 = MatchValue;
512  break;
513  case 1:
514  TIMx->MR1 = MatchValue;
515  break;
516  case 2:
517  TIMx->MR2 = MatchValue;
518  break;
519  case 3:
520  TIMx->MR3 = MatchValue;
521  break;
522  default:
523  //Error Loop
524  while(1);
525  }
526 
527 }
528 /*********************************************************************/
542 void TIM_ConfigCapture(LPC_TIM_TypeDef *TIMx, TIM_CAPTURECFG_Type *TIM_CaptureConfigStruct)
543 {
544 
545  CHECK_PARAM(PARAM_TIMx(TIMx));
546  TIMx->CCR &= ~TIM_CCR_CHANNEL_MASKBIT(TIM_CaptureConfigStruct->CaptureChannel);
547 
548  if (TIM_CaptureConfigStruct->RisingEdge)
549  TIMx->CCR |= TIM_CAP_RISING(TIM_CaptureConfigStruct->CaptureChannel);
550 
551  if (TIM_CaptureConfigStruct->FallingEdge)
552  TIMx->CCR |= TIM_CAP_FALLING(TIM_CaptureConfigStruct->CaptureChannel);
553 
554  if (TIM_CaptureConfigStruct->IntOnCaption)
555  TIMx->CCR |= TIM_INT_ON_CAP(TIM_CaptureConfigStruct->CaptureChannel);
556 }
557 
558 /*********************************************************************/
570 uint32_t TIM_GetCaptureValue(LPC_TIM_TypeDef *TIMx, TIM_COUNTER_INPUT_OPT CaptureChannel)
571 {
572  CHECK_PARAM(PARAM_TIMx(TIMx));
573  CHECK_PARAM(PARAM_TIM_COUNTER_INPUT_OPT(CaptureChannel));
574 
575  if(CaptureChannel==0)
576  return TIMx->CR0;
577  else
578  return TIMx->CR1;
579 }
580 
585 #endif /* _TIMER */
586 
591 /* --------------------------------- End Of File ------------------------------ */