uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
lpc17xx_rtc.c
Go to the documentation of this file.
1 /***********************************************************************/
21 /* Peripheral group ----------------------------------------------------------- */
26 /* Includes ------------------------------------------------------------------- */
27 #include "lpc17xx_rtc.h"
28 #include "lpc17xx_clkpwr.h"
29 
30 
31 /* If this source file built with example, the LPC17xx FW library configuration
32  * file in each example directory ("lpc17xx_libcfg.h") must be included,
33  * otherwise the default FW library configuration file must be included instead
34  */
35 #ifdef __BUILD_WITH_EXAMPLE__
36 #include "lpc17xx_libcfg.h"
37 #else
38 #include "lpc17xx_libcfg_default.h"
39 #endif /* __BUILD_WITH_EXAMPLE__ */
40 
41 
42 #ifdef _RTC
43 
44 /* Public Functions ----------------------------------------------------------- */
49 /********************************************************************/
54 void RTC_Init (LPC_RTC_TypeDef *RTCx)
55 {
56  CHECK_PARAM(PARAM_RTCx(RTCx));
57 
58  /* Set up clock and power for RTC module */
60 
61  // Clear all register to be default
62  RTCx->ILR = 0x00;
63  RTCx->CCR = 0x00;
64  RTCx->CIIR = 0x00;
65  RTCx->AMR = 0xFF;
66  RTCx->CALIBRATION = 0x00;
67 }
68 
69 
70 /*********************************************************************/
76 void RTC_DeInit(LPC_RTC_TypeDef *RTCx)
77 {
78  CHECK_PARAM(PARAM_RTCx(RTCx));
79 
80  RTCx->CCR = 0x00;
81  // Disable power and clock for RTC module
83 }
84 
85 /*********************************************************************/
91 {
92  CHECK_PARAM(PARAM_RTCx(RTCx));
93 
94  RTCx->CCR |= RTC_CCR_CTCRST;
95  RTCx->CCR &= (~RTC_CCR_CTCRST) & RTC_CCR_BITMASK;
96 }
97 
98 /*********************************************************************/
106 void RTC_Cmd (LPC_RTC_TypeDef *RTCx, FunctionalState NewState)
107 {
108  CHECK_PARAM(PARAM_RTCx(RTCx));
110 
111  if (NewState == ENABLE)
112  {
113  RTCx->CCR |= RTC_CCR_CLKEN;
114  }
115  else
116  {
117  RTCx->CCR &= (~RTC_CCR_CLKEN) & RTC_CCR_BITMASK;
118  }
119 }
120 
121 
122 /*********************************************************************/
144 void RTC_CntIncrIntConfig (LPC_RTC_TypeDef *RTCx, uint32_t CntIncrIntType, \
145  FunctionalState NewState)
146 {
147  CHECK_PARAM(PARAM_RTCx(RTCx));
149  CHECK_PARAM(PARAM_RTC_TIMETYPE(CntIncrIntType));
150 
151  if (NewState == ENABLE)
152  {
153  switch (CntIncrIntType)
154  {
155  case RTC_TIMETYPE_SECOND:
156  RTCx->CIIR |= RTC_CIIR_IMSEC;
157  break;
158  case RTC_TIMETYPE_MINUTE:
159  RTCx->CIIR |= RTC_CIIR_IMMIN;
160  break;
161  case RTC_TIMETYPE_HOUR:
162  RTCx->CIIR |= RTC_CIIR_IMHOUR;
163  break;
165  RTCx->CIIR |= RTC_CIIR_IMDOW;
166  break;
168  RTCx->CIIR |= RTC_CIIR_IMDOM;
169  break;
171  RTCx->CIIR |= RTC_CIIR_IMDOY;
172  break;
173  case RTC_TIMETYPE_MONTH:
174  RTCx->CIIR |= RTC_CIIR_IMMON;
175  break;
176  case RTC_TIMETYPE_YEAR:
177  RTCx->CIIR |= RTC_CIIR_IMYEAR;
178  break;
179  }
180  }
181  else
182  {
183  switch (CntIncrIntType)
184  {
185  case RTC_TIMETYPE_SECOND:
186  RTCx->CIIR &= (~RTC_CIIR_IMSEC) & RTC_CIIR_BITMASK;
187  break;
188  case RTC_TIMETYPE_MINUTE:
189  RTCx->CIIR &= (~RTC_CIIR_IMMIN) & RTC_CIIR_BITMASK;
190  break;
191  case RTC_TIMETYPE_HOUR:
192  RTCx->CIIR &= (~RTC_CIIR_IMHOUR) & RTC_CIIR_BITMASK;
193  break;
195  RTCx->CIIR &= (~RTC_CIIR_IMDOW) & RTC_CIIR_BITMASK;
196  break;
198  RTCx->CIIR &= (~RTC_CIIR_IMDOM) & RTC_CIIR_BITMASK;
199  break;
201  RTCx->CIIR &= (~RTC_CIIR_IMDOY) & RTC_CIIR_BITMASK;
202  break;
203  case RTC_TIMETYPE_MONTH:
204  RTCx->CIIR &= (~RTC_CIIR_IMMON) & RTC_CIIR_BITMASK;
205  break;
206  case RTC_TIMETYPE_YEAR:
207  RTCx->CIIR &= (~RTC_CIIR_IMYEAR) & RTC_CIIR_BITMASK;
208  break;
209  }
210  }
211 }
212 
213 
214 /*********************************************************************/
236 void RTC_AlarmIntConfig (LPC_RTC_TypeDef *RTCx, uint32_t AlarmTimeType, \
237  FunctionalState NewState)
238 {
239  CHECK_PARAM(PARAM_RTCx(RTCx));
241  CHECK_PARAM(PARAM_RTC_TIMETYPE(AlarmTimeType));
242 
243  if (NewState == ENABLE)
244  {
245  switch (AlarmTimeType)
246  {
247  case RTC_TIMETYPE_SECOND:
248  RTCx->AMR &= (~RTC_AMR_AMRSEC) & RTC_AMR_BITMASK;
249  break;
250  case RTC_TIMETYPE_MINUTE:
251  RTCx->AMR &= (~RTC_AMR_AMRMIN) & RTC_AMR_BITMASK;
252  break;
253  case RTC_TIMETYPE_HOUR:
254  RTCx->AMR &= (~RTC_AMR_AMRHOUR) & RTC_AMR_BITMASK;
255  break;
257  RTCx->AMR &= (~RTC_AMR_AMRDOW) & RTC_AMR_BITMASK;
258  break;
260  RTCx->AMR &= (~RTC_AMR_AMRDOM) & RTC_AMR_BITMASK;
261  break;
263  RTCx->AMR &= (~RTC_AMR_AMRDOY) & RTC_AMR_BITMASK;
264  break;
265  case RTC_TIMETYPE_MONTH:
266  RTCx->AMR &= (~RTC_AMR_AMRMON) & RTC_AMR_BITMASK;
267  break;
268  case RTC_TIMETYPE_YEAR:
269  RTCx->AMR &= (~RTC_AMR_AMRYEAR) & RTC_AMR_BITMASK;
270  break;
271  }
272  }
273  else
274  {
275  switch (AlarmTimeType)
276  {
277  case RTC_TIMETYPE_SECOND:
278  RTCx->AMR |= (RTC_AMR_AMRSEC);
279  break;
280  case RTC_TIMETYPE_MINUTE:
281  RTCx->AMR |= (RTC_AMR_AMRMIN);
282  break;
283  case RTC_TIMETYPE_HOUR:
284  RTCx->AMR |= (RTC_AMR_AMRHOUR);
285  break;
287  RTCx->AMR |= (RTC_AMR_AMRDOW);
288  break;
290  RTCx->AMR |= (RTC_AMR_AMRDOM);
291  break;
293  RTCx->AMR |= (RTC_AMR_AMRDOY);
294  break;
295  case RTC_TIMETYPE_MONTH:
296  RTCx->AMR |= (RTC_AMR_AMRMON);
297  break;
298  case RTC_TIMETYPE_YEAR:
299  RTCx->AMR |= (RTC_AMR_AMRYEAR);
300  break;
301  }
302  }
303 }
304 
305 
306 /*********************************************************************/
321 void RTC_SetTime (LPC_RTC_TypeDef *RTCx, uint32_t Timetype, uint32_t TimeValue)
322 {
323  CHECK_PARAM(PARAM_RTCx(RTCx));
324  CHECK_PARAM(PARAM_RTC_TIMETYPE(Timetype));
325 
326  switch ( Timetype)
327  {
328  case RTC_TIMETYPE_SECOND:
329  CHECK_PARAM(TimeValue < RTC_SECOND_MAX);
330 
331  RTCx->SEC = TimeValue & RTC_SEC_MASK;
332  break;
333 
334  case RTC_TIMETYPE_MINUTE:
335  CHECK_PARAM(TimeValue < RTC_MINUTE_MAX);
336 
337  RTCx->MIN = TimeValue & RTC_MIN_MASK;
338  break;
339 
340  case RTC_TIMETYPE_HOUR:
341  CHECK_PARAM(TimeValue < RTC_HOUR_MAX);
342 
343  RTCx->HOUR = TimeValue & RTC_HOUR_MASK;
344  break;
345 
347  CHECK_PARAM(TimeValue < RTC_DAYOFWEEK_MAX);
348 
349  RTCx->DOW = TimeValue & RTC_DOW_MASK;
350  break;
351 
353  CHECK_PARAM((TimeValue < RTC_DAYOFMONTH_MAX) \
354  && (TimeValue > RTC_DAYOFMONTH_MIN));
355 
356  RTCx->DOM = TimeValue & RTC_DOM_MASK;
357  break;
358 
360  CHECK_PARAM((TimeValue > RTC_DAYOFYEAR_MIN) \
361  && (TimeValue < RTC_DAYOFYEAR_MAX));
362 
363  RTCx->DOY = TimeValue & RTC_DOY_MASK;
364  break;
365 
366  case RTC_TIMETYPE_MONTH:
367  CHECK_PARAM((TimeValue > RTC_MONTH_MIN) \
368  && (TimeValue < RTC_MONTH_MAX));
369 
370  RTCx->MONTH = TimeValue & RTC_MONTH_MASK;
371  break;
372 
373  case RTC_TIMETYPE_YEAR:
374  CHECK_PARAM(TimeValue < RTC_YEAR_MAX);
375 
376  RTCx->YEAR = TimeValue & RTC_YEAR_MASK;
377  break;
378  }
379 }
380 
381 /*********************************************************************/
395 uint32_t RTC_GetTime(LPC_RTC_TypeDef *RTCx, uint32_t Timetype)
396 {
397  CHECK_PARAM(PARAM_RTCx(RTCx));
398  CHECK_PARAM(PARAM_RTC_TIMETYPE(Timetype));
399 
400  switch (Timetype)
401  {
402  case RTC_TIMETYPE_SECOND:
403  return (RTCx->SEC & RTC_SEC_MASK);
404  case RTC_TIMETYPE_MINUTE:
405  return (RTCx->MIN & RTC_MIN_MASK);
406  case RTC_TIMETYPE_HOUR:
407  return (RTCx->HOUR & RTC_HOUR_MASK);
409  return (RTCx->DOW & RTC_DOW_MASK);
411  return (RTCx->DOM & RTC_DOM_MASK);
413  return (RTCx->DOY & RTC_DOY_MASK);
414  case RTC_TIMETYPE_MONTH:
415  return (RTCx->MONTH & RTC_MONTH_MASK);
416  case RTC_TIMETYPE_YEAR:
417  return (RTCx->YEAR & RTC_YEAR_MASK);
418  default:
419  return (0);
420  }
421 }
422 
423 
424 /*********************************************************************/
431 void RTC_SetFullTime (LPC_RTC_TypeDef *RTCx, RTC_TIME_Type *pFullTime)
432 {
433  CHECK_PARAM(PARAM_RTCx(RTCx));
434 
435  RTCx->DOM = pFullTime->DOM & RTC_DOM_MASK;
436  RTCx->DOW = pFullTime->DOW & RTC_DOW_MASK;
437  RTCx->DOY = pFullTime->DOY & RTC_DOY_MASK;
438  RTCx->HOUR = pFullTime->HOUR & RTC_HOUR_MASK;
439  RTCx->MIN = pFullTime->MIN & RTC_MIN_MASK;
440  RTCx->SEC = pFullTime->SEC & RTC_SEC_MASK;
441  RTCx->MONTH = pFullTime->MONTH & RTC_MONTH_MASK;
442  RTCx->YEAR = pFullTime->YEAR & RTC_YEAR_MASK;
443 }
444 
445 
446 /*********************************************************************/
453 void RTC_GetFullTime (LPC_RTC_TypeDef *RTCx, RTC_TIME_Type *pFullTime)
454 {
455  CHECK_PARAM(PARAM_RTCx(RTCx));
456 
457  pFullTime->DOM = RTCx->DOM & RTC_DOM_MASK;
458  pFullTime->DOW = RTCx->DOW & RTC_DOW_MASK;
459  pFullTime->DOY = RTCx->DOY & RTC_DOY_MASK;
460  pFullTime->HOUR = RTCx->HOUR & RTC_HOUR_MASK;
461  pFullTime->MIN = RTCx->MIN & RTC_MIN_MASK;
462  pFullTime->SEC = RTCx->SEC & RTC_SEC_MASK;
463  pFullTime->MONTH = RTCx->MONTH & RTC_MONTH_MASK;
464  pFullTime->YEAR = RTCx->YEAR & RTC_YEAR_MASK;
465 }
466 
467 
468 /*********************************************************************/
483 void RTC_SetAlarmTime (LPC_RTC_TypeDef *RTCx, uint32_t Timetype, uint32_t ALValue)
484 {
485  CHECK_PARAM(PARAM_RTCx(RTCx));
486 
487  switch (Timetype)
488  {
489  case RTC_TIMETYPE_SECOND:
490  CHECK_PARAM(ALValue < RTC_SECOND_MAX);
491 
492  RTCx->ALSEC = ALValue & RTC_SEC_MASK;
493  break;
494 
495  case RTC_TIMETYPE_MINUTE:
496  CHECK_PARAM(ALValue < RTC_MINUTE_MAX);
497 
498  RTCx->ALMIN = ALValue & RTC_MIN_MASK;
499  break;
500 
501  case RTC_TIMETYPE_HOUR:
502  CHECK_PARAM(ALValue < RTC_HOUR_MAX);
503 
504  RTCx->ALHOUR = ALValue & RTC_HOUR_MASK;
505  break;
506 
508  CHECK_PARAM(ALValue < RTC_DAYOFWEEK_MAX);
509 
510  RTCx->ALDOW = ALValue & RTC_DOW_MASK;
511  break;
512 
514  CHECK_PARAM((ALValue < RTC_DAYOFMONTH_MAX) \
515  && (ALValue > RTC_DAYOFMONTH_MIN));
516 
517  RTCx->ALDOM = ALValue & RTC_DOM_MASK;
518  break;
519 
521  CHECK_PARAM((ALValue > RTC_DAYOFYEAR_MIN) \
522  && (ALValue < RTC_DAYOFYEAR_MAX));
523 
524  RTCx->ALDOY = ALValue & RTC_DOY_MASK;
525  break;
526 
527  case RTC_TIMETYPE_MONTH:
528  CHECK_PARAM((ALValue > RTC_MONTH_MIN) \
529  && (ALValue < RTC_MONTH_MAX));
530 
531  RTCx->ALMON = ALValue & RTC_MONTH_MASK;
532  break;
533 
534  case RTC_TIMETYPE_YEAR:
535  CHECK_PARAM(ALValue < RTC_YEAR_MAX);
536 
537  RTCx->ALYEAR = ALValue & RTC_YEAR_MASK;
538  break;
539  }
540 }
541 
542 
543 
544 /*********************************************************************/
558 uint32_t RTC_GetAlarmTime (LPC_RTC_TypeDef *RTCx, uint32_t Timetype)
559 {
560  switch (Timetype)
561  {
562  case RTC_TIMETYPE_SECOND:
563  return (RTCx->ALSEC & RTC_SEC_MASK);
564  case RTC_TIMETYPE_MINUTE:
565  return (RTCx->ALMIN & RTC_MIN_MASK);
566  case RTC_TIMETYPE_HOUR:
567  return (RTCx->ALHOUR & RTC_HOUR_MASK);
569  return (RTCx->ALDOW & RTC_DOW_MASK);
571  return (RTCx->ALDOM & RTC_DOM_MASK);
573  return (RTCx->ALDOY & RTC_DOY_MASK);
574  case RTC_TIMETYPE_MONTH:
575  return (RTCx->ALMON & RTC_MONTH_MASK);
576  case RTC_TIMETYPE_YEAR:
577  return (RTCx->ALYEAR & RTC_YEAR_MASK);
578  default:
579  return (0);
580  }
581 }
582 
583 
584 /*********************************************************************/
591 void RTC_SetFullAlarmTime (LPC_RTC_TypeDef *RTCx, RTC_TIME_Type *pFullTime)
592 {
593  CHECK_PARAM(PARAM_RTCx(RTCx));
594 
595  RTCx->ALDOM = pFullTime->DOM & RTC_DOM_MASK;
596  RTCx->ALDOW = pFullTime->DOW & RTC_DOW_MASK;
597  RTCx->ALDOY = pFullTime->DOY & RTC_DOY_MASK;
598  RTCx->ALHOUR = pFullTime->HOUR & RTC_HOUR_MASK;
599  RTCx->ALMIN = pFullTime->MIN & RTC_MIN_MASK;
600  RTCx->ALSEC = pFullTime->SEC & RTC_SEC_MASK;
601  RTCx->ALMON = pFullTime->MONTH & RTC_MONTH_MASK;
602  RTCx->ALYEAR = pFullTime->YEAR & RTC_YEAR_MASK;
603 }
604 
605 
606 /*********************************************************************/
613 void RTC_GetFullAlarmTime (LPC_RTC_TypeDef *RTCx, RTC_TIME_Type *pFullTime)
614 {
615  CHECK_PARAM(PARAM_RTCx(RTCx));
616 
617  pFullTime->DOM = RTCx->ALDOM & RTC_DOM_MASK;
618  pFullTime->DOW = RTCx->ALDOW & RTC_DOW_MASK;
619  pFullTime->DOY = RTCx->ALDOY & RTC_DOY_MASK;
620  pFullTime->HOUR = RTCx->ALHOUR & RTC_HOUR_MASK;
621  pFullTime->MIN = RTCx->ALMIN & RTC_MIN_MASK;
622  pFullTime->SEC = RTCx->ALSEC & RTC_SEC_MASK;
623  pFullTime->MONTH = RTCx->ALMON & RTC_MONTH_MASK;
624  pFullTime->YEAR = RTCx->ALYEAR & RTC_YEAR_MASK;
625 }
626 
627 
628 /*********************************************************************/
640 IntStatus RTC_GetIntPending (LPC_RTC_TypeDef *RTCx, uint32_t IntType)
641 {
642  CHECK_PARAM(PARAM_RTCx(RTCx));
643  CHECK_PARAM(PARAM_RTC_INT(IntType));
644 
645  return ((RTCx->ILR & IntType) ? SET : RESET);
646 }
647 
648 
649 /*********************************************************************/
659 void RTC_ClearIntPending (LPC_RTC_TypeDef *RTCx, uint32_t IntType)
660 {
661  CHECK_PARAM(PARAM_RTCx(RTCx));
662  CHECK_PARAM(PARAM_RTC_INT(IntType));
663 
664  RTCx->ILR |= IntType;
665 }
666 
667 /*********************************************************************/
676 {
677  CHECK_PARAM(PARAM_RTCx(RTCx));
679 
680  if (NewState == ENABLE)
681  {
682  RTCx->CCR &= (~RTC_CCR_CCALEN) & RTC_CCR_BITMASK;
683  }
684  else
685  {
686  RTCx->CCR |= RTC_CCR_CCALEN;
687  }
688 }
689 
690 
691 /*********************************************************************/
701 void RTC_CalibConfig(LPC_RTC_TypeDef *RTCx, uint32_t CalibValue, uint8_t CalibDir)
702 {
703  CHECK_PARAM(PARAM_RTCx(RTCx));
704  CHECK_PARAM(PARAM_RTC_CALIB_DIR(CalibDir));
705  CHECK_PARAM(CalibValue < RTC_CALIBRATION_MAX);
706 
707  RTCx->CALIBRATION = ((CalibValue - 1) & RTC_CALIBRATION_CALVAL_MASK) \
708  | ((CalibDir == RTC_CALIB_DIR_BACKWARD) ? RTC_CALIBRATION_LIBDIR : 0);
709 }
710 
711 
712 /*********************************************************************/
723 void RTC_WriteGPREG (LPC_RTC_TypeDef *RTCx, uint8_t Channel, uint32_t Value)
724 {
725  uint32_t *preg;
726 
727  CHECK_PARAM(PARAM_RTCx(RTCx));
729 
730  preg = (uint32_t *)&RTCx->GPREG0;
731  preg += Channel;
732  *preg = Value;
733 }
734 
735 
736 /*********************************************************************/
746 uint32_t RTC_ReadGPREG (LPC_RTC_TypeDef *RTCx, uint8_t Channel)
747 {
748  uint32_t *preg;
749  uint32_t value;
750 
751  CHECK_PARAM(PARAM_RTCx(RTCx));
753 
754  preg = (uint32_t *)&RTCx->GPREG0;
755  preg += Channel;
756  value = *preg;
757  return (value);
758 }
759 
764 #endif /* _RTC */
765 
770 /* --------------------------------- End Of File ------------------------------ */
771