LPCOpen Platform for LPC18XX/43XX microcontrollers  18XX43XX
LPCOpen Platform for the NXP LPC18XX/43XX family of Microcontrollers
rtc_18xx_43xx.c
Go to the documentation of this file.
1 /*
2  * @brief LPC18xx/43xx RTC 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 /*****************************************************************************
47  * Public functions
48  ****************************************************************************/
49 
50 /* Initialize the RTC peripheral */
52 {
54 
55  /* 2-Second delay after enabling RTC clock */
56  LPC_ATIMER->DOWNCOUNTER = 2048;
57  while (LPC_ATIMER->DOWNCOUNTER);
58 
59  /* Disable RTC */
60  Chip_RTC_Enable(pRTC, DISABLE);
61 
62  /* Disable Calibration */
64 
65  /* Reset RTC Clock */
67 
68  /* Clear counter increment and alarm interrupt */
70  while (pRTC->ILR != 0) {}
71 
72  /* Clear all register to be default */
73  pRTC->CIIR = 0x00;
74  pRTC->AMR = 0xFF;
75  pRTC->CALIBRATION = 0x00;
76 }
77 
78 /*De-initialize the RTC peripheral */
80 {
81  pRTC->CCR = 0x00;
82 }
83 
84 /* Reset clock tick counter in the RTC peripheral */
86 {
87  /* Reset RTC clock*/
88  pRTC->CCR |= RTC_CCR_CTCRST;
89  while (!(pRTC->CCR & RTC_CCR_CTCRST)) {}
90 
91  /* Finish resetting RTC clock */
92  pRTC->CCR = (pRTC->CCR & ~RTC_CCR_CTCRST) & RTC_CCR_BITMASK;
93  while (pRTC->CCR & RTC_CCR_CTCRST) {}
94 }
95 
96 /* Start/Stop RTC peripheral */
98 {
99  if (NewState == ENABLE) {
100  pRTC->CCR |= RTC_CCR_CLKEN;
101  } else {
102  pRTC->CCR = (pRTC->CCR & ~RTC_CCR_CLKEN) & RTC_CCR_BITMASK;
103  }
104 }
105 
106 /* Enable/Disable Counter increment interrupt for a time type in the RTC peripheral */
107 void Chip_RTC_CntIncrIntConfig(LPC_RTC_T *pRTC, uint32_t cntrMask, FunctionalState NewState)
108 {
109  if (NewState == ENABLE) {
110  pRTC->CIIR |= cntrMask;
111  }
112 
113  else {
114  pRTC->CIIR &= (~cntrMask) & RTC_AMR_CIIR_BITMASK;
115  while (pRTC->CIIR & cntrMask) {}
116  }
117 }
118 
119 /* Enable/Disable Alarm interrupt for a time type in the RTC peripheral */
120 void Chip_RTC_AlarmIntConfig(LPC_RTC_T *pRTC, uint32_t alarmMask, FunctionalState NewState)
121 {
122  if (NewState == ENABLE) {
123  pRTC->AMR &= (~alarmMask) & RTC_AMR_CIIR_BITMASK;
124  }
125  else {
126  pRTC->AMR |= (alarmMask);
127  while ((pRTC->AMR & alarmMask) == 0) {}
128  }
129 }
130 
131 /* Set full time in the RTC peripheral */
133 {
134  RTC_TIMEINDEX_T i;
135  uint32_t ccr_val = pRTC->CCR;
136 
137  /* Temporarily disable */
138  if (ccr_val & RTC_CCR_CLKEN) {
139  pRTC->CCR = ccr_val & (~RTC_CCR_CLKEN) & RTC_CCR_BITMASK;
140  }
141 
142  /* Date time setting */
143  for (i = RTC_TIMETYPE_SECOND; i < RTC_TIMETYPE_LAST; i++) {
144  pRTC->TIME[i] = pFullTime->time[i];
145  }
146 
147  /* Restore to old setting */
148  pRTC->CCR = ccr_val;
149 }
150 
151 /* Get full time from the RTC peripheral */
153 {
154  RTC_TIMEINDEX_T i;
155  uint32_t secs = 0xFF;
156 
157  /* Read full time, but verify second tick didn't change during the read. If
158  it did, re-read the time again so it will be consistent across all fields. */
159  while (secs != pRTC->TIME[RTC_TIMETYPE_SECOND]) {
160  secs = pFullTime->time[RTC_TIMETYPE_SECOND] = pRTC->TIME[RTC_TIMETYPE_SECOND];
161  for (i = RTC_TIMETYPE_MINUTE; i < RTC_TIMETYPE_LAST; i++) {
162  pFullTime->time[i] = pRTC->TIME[i];
163  }
164  }
165 }
166 
167 /* Set full alarm time in the RTC peripheral */
169 {
170  RTC_TIMEINDEX_T i;
171 
172  for (i = RTC_TIMETYPE_SECOND; i < RTC_TIMETYPE_LAST; i++) {
173  pRTC->ALRM[i] = pFullTime->time[i];
174  }
175 }
176 
177 /* Get full alarm time in the RTC peripheral */
179 {
180  RTC_TIMEINDEX_T i;
181 
182  for (i = RTC_TIMETYPE_SECOND; i < RTC_TIMETYPE_LAST; i++) {
183  pFullTime->time[i] = pRTC->ALRM[i];
184  }
185 }
186 
187 /* Enable/Disable calibration counter in the RTC peripheral */
189 {
190  if (NewState == ENABLE) {
191  do {
192  pRTC->CCR &= (~RTC_CCR_CCALEN) & RTC_CCR_BITMASK;
193  } while (pRTC->CCR & RTC_CCR_CCALEN);
194  }
195  else {
196  pRTC->CCR |= RTC_CCR_CCALEN;
197  }
198 }
199 
200 #if RTC_EV_SUPPORT
201 /* Get first timestamp value */
203 {
204  pTimeStamp->sec = RTC_ER_TIMESTAMP_SEC(pRTC->ERFIRSTSTAMP[ch]);
205  pTimeStamp->min = RTC_ER_TIMESTAMP_MIN(pRTC->ERFIRSTSTAMP[ch]);
206  pTimeStamp->hour = RTC_ER_TIMESTAMP_HOUR(pRTC->ERFIRSTSTAMP[ch]);
207  pTimeStamp->dayofyear = RTC_ER_TIMESTAMP_DOY(pRTC->ERFIRSTSTAMP[ch]);
208 }
209 
210 /* Get last timestamp value */
212 {
213  pTimeStamp->sec = RTC_ER_TIMESTAMP_SEC(pRTC->ERLASTSTAMP[ch]);
214  pTimeStamp->min = RTC_ER_TIMESTAMP_MIN(pRTC->ERLASTSTAMP[ch]);
215  pTimeStamp->hour = RTC_ER_TIMESTAMP_HOUR(pRTC->ERLASTSTAMP[ch]);
216  pTimeStamp->dayofyear = RTC_ER_TIMESTAMP_DOY(pRTC->ERLASTSTAMP[ch]);
217 }
218 
219 #endif /*RTC_EV_SUPPORT*/
220