| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rtc_001.c
Go to the documentation of this file.
1 /*
2 * @brief Real Time Clock registers and control functions
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 "rtc_001.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 the RTC peripheral */
51 void IP_RTC_Init(IP_RTC_001_T *pRTC)
52 {
53 /* Disable RTC */
54 IP_RTC_Enable(pRTC, DISABLE);
55
56 /* Disable Calibration */
57 IP_RTC_CalibCounterCmd(pRTC, DISABLE);
58
59 /* Reset RTC Clock */
60 IP_RTC_ResetClockTickCounter(pRTC);
61
62 /* Clear counter increment and alarm interrupt */
63 pRTC->ILR = RTC_IRL_RTCCIF | RTC_IRL_RTCALF;
64 while (pRTC->ILR != 0) {}
65
66 /* Clear all register to be default */
67 pRTC->CIIR = 0x00;
68 pRTC->AMR = 0xFF;
69 pRTC->CALIBRATION = 0x00;
70 }
71
72 /* Reset clock tick counter in the RTC peripheral */
73 void IP_RTC_ResetClockTickCounter(IP_RTC_001_T *pRTC)
74 {
75 do {
76 /* Reset RTC clock*/
77 pRTC->CCR |= RTC_CCR_CTCRST;
78 } while ((pRTC->CCR & RTC_CCR_CTCRST) != RTC_CCR_CTCRST);
79
80 do {
81 /* Finish resetting RTC clock */
82 pRTC->CCR &= (~RTC_CCR_CTCRST) & RTC_CCR_BITMASK;
83 } while (pRTC->CCR & RTC_CCR_CTCRST);
84 }
85
86 /* Start/Stop RTC peripheral */
87 void IP_RTC_Enable(IP_RTC_001_T *pRTC, FunctionalState NewState)
88 {
89 if (NewState == ENABLE) {
90 do {
91 pRTC->CCR |= RTC_CCR_CLKEN;
92 } while ((pRTC->CCR & RTC_CCR_CLKEN) == 0);
93 }
94 else {
95 pRTC->CCR &= (~RTC_CCR_CLKEN) & RTC_CCR_BITMASK;
96 }
97 }
98
99 /* Enable/Disable Counter increment interrupt for a time type in the RTC peripheral */
100 void IP_RTC_CntIncrIntConfig(IP_RTC_001_T *pRTC, uint32_t cntrMask,
101 FunctionalState NewState)
102 {
103 if (NewState == ENABLE) {
104 pRTC->CIIR |= cntrMask;
105 }
106
107 else {
108 pRTC->CIIR &= (~cntrMask) & RTC_AMR_CIIR_BITMASK;
109 while (pRTC->CIIR & cntrMask) {}
110 }
111 }
112
113 /* Enable/Disable Alarm interrupt for a time type in the RTC peripheral */
114 void IP_RTC_AlarmIntConfig(IP_RTC_001_T *pRTC, uint32_t alarmMask,
115 FunctionalState NewState)
116 {
117 if (NewState == ENABLE) {
118 pRTC->AMR &= (~alarmMask) & RTC_AMR_CIIR_BITMASK;
119 }
120 else {
121 pRTC->AMR |= (alarmMask);
122 while ((pRTC->AMR & alarmMask) == 0) {}
123 }
124 }
125
126 /* Set current time value for a time type in the RTC peripheral */
127 void IP_RTC_SetTime(IP_RTC_001_T *pRTC, IP_RTC_TIMEINDEX_T Timetype, uint32_t TimeValue)
128 {
129 pRTC->TIME[Timetype] = TimeValue;
130 }
131
132 /* Get current time value for a type time type */
133 uint32_t IP_RTC_GetTime(IP_RTC_001_T *pRTC, IP_RTC_TIMEINDEX_T Timetype)
134 {
135 return pRTC->TIME[Timetype];
136 }
137
138 /* Set full time in the RTC peripheral */
139 void IP_RTC_SetFullTime(IP_RTC_001_T *pRTC, IP_RTC_TIME_T *pFullTime)
140 {
141 IP_RTC_TIMEINDEX_T i;
142 uint32_t ccr_val = pRTC->CCR;
143
144 /* Temporarily disable */
145 if (ccr_val & RTC_CCR_CLKEN) {
146 pRTC->CCR = ccr_val & (~RTC_CCR_CLKEN) & RTC_CCR_BITMASK;
147 }
148
149 /* Date time setting */
150 for (i = RTC_TIMETYPE_SECOND; i < RTC_TIMETYPE_LAST; i++) {
151 pRTC->TIME[i] = pFullTime->time[i];
152 }
153
154 /* Restore to old setting */
155 pRTC->CCR = ccr_val;
156 }
157
158 /* Get full time from the RTC peripheral */
159 void IP_RTC_GetFullTime(IP_RTC_001_T *pRTC, IP_RTC_TIME_T *pFullTime)
160 {
161 IP_RTC_TIMEINDEX_T i;
162 uint32_t secs = 0xFF;
163
164 /* Read full time, but verify second tick didn't change during the read. If
165 it did, re-read the time again so it will be consistent across all fields. */
166 while (secs != pRTC->TIME[RTC_TIMETYPE_SECOND]) {
167 secs = pFullTime->time[RTC_TIMETYPE_SECOND] = pRTC->TIME[RTC_TIMETYPE_SECOND];
168 for (i = RTC_TIMETYPE_MINUTE; i < RTC_TIMETYPE_LAST; i++) {
169 pFullTime->time[i] = pRTC->TIME[i];
170 }
171 }
172 }
173
174 /* Set alarm time value for a time type */
175 void IP_RTC_SetAlarmTime(IP_RTC_001_T *pRTC, IP_RTC_TIMEINDEX_T Timetype, uint32_t ALValue)
176 {
177 pRTC->ALRM[Timetype] = ALValue;
178 }
179
180 /* Get alarm time value for a time */
181 uint32_t IP_RTC_GetAlarmTime(IP_RTC_001_T *pRTC, IP_RTC_TIMEINDEX_T Timetype)
182 {
183 return pRTC->ALRM[Timetype];
184 }
185
186 /* Set full alarm time in the RTC peripheral */
187 void IP_RTC_SetFullAlarmTime(IP_RTC_001_T *pRTC, IP_RTC_TIME_T *pFullTime)
188 {
189 IP_RTC_TIMEINDEX_T i;
190
191 for (i = RTC_TIMETYPE_SECOND; i < RTC_TIMETYPE_LAST; i++) {
192 pRTC->ALRM[i] = pFullTime->time[i];
193 }
194 }
195
196 /* Get full alarm time in the RTC peripheral */
197 void IP_RTC_GetFullAlarmTime(IP_RTC_001_T *pRTC, IP_RTC_TIME_T *pFullTime)
198 {
199 IP_RTC_TIMEINDEX_T i;
200
201 for (i = RTC_TIMETYPE_SECOND; i < RTC_TIMETYPE_LAST; i++) {
202 pFullTime->time[i] = pRTC->ALRM[i];
203 }
204 }
205
206 /* Enable/Disable calibration counter in the RTC peripheral */
207 void IP_RTC_CalibCounterCmd(IP_RTC_001_T *pRTC, FunctionalState NewState)
208 {
209 if (NewState == ENABLE) {
210 do {
211 pRTC->CCR &= (~RTC_CCR_CCALEN) & RTC_CCR_BITMASK;
212 } while (pRTC->CCR & RTC_CCR_CCALEN);
213 }
214 else {
215 pRTC->CCR |= RTC_CCR_CCALEN;
216 }
217 }
218
219 /* Configures Calibration in the RTC peripheral */
220 void IP_RTC_CalibConfig(IP_RTC_001_T *pRTC, uint32_t CalibValue, uint8_t CalibDir)
221 {
222 pRTC->CALIBRATION = ((CalibValue - 1) & RTC_CALIBRATION_CALVAL_MASK)
223 | ((CalibDir == RTC_CALIB_DIR_BACKWARD) ? RTC_CALIBRATION_LIBDIR : 0);
224 }
225
226 #if RTC_EV_SUPPORT
227 /* Get first timestamp value */
228 void IP_RTC_EV_GetFirstTimeStamp(IP_RTC_001_T *pRTC, IP_RTC_EV_CHANNEL_T ch, IP_RTC_EV_TIMESTAMP_T *pTimeStamp)
229 {
230 pTimeStamp->sec = RTC_ER_TIMESTAMP_SEC(pRTC->ERFIRSTSTAMP[ch]);
231 pTimeStamp->min = RTC_ER_TIMESTAMP_MIN(pRTC->ERFIRSTSTAMP[ch]);
232 pTimeStamp->hour = RTC_ER_TIMESTAMP_HOUR(pRTC->ERFIRSTSTAMP[ch]);
233 pTimeStamp->dayofyear = RTC_ER_TIMESTAMP_DOY(pRTC->ERFIRSTSTAMP[ch]);
234 }
235
236 /* Get last timestamp value */
237 void IP_RTC_EV_GetLastTimeStamp(IP_RTC_001_T *pRTC, IP_RTC_EV_CHANNEL_T ch, IP_RTC_EV_TIMESTAMP_T *pTimeStamp)
238 {
239 pTimeStamp->sec = RTC_ER_TIMESTAMP_SEC(pRTC->ERLASTSTAMP[ch]);
240 pTimeStamp->min = RTC_ER_TIMESTAMP_MIN(pRTC->ERLASTSTAMP[ch]);
241 pTimeStamp->hour = RTC_ER_TIMESTAMP_HOUR(pRTC->ERLASTSTAMP[ch]);
242 pTimeStamp->dayofyear = RTC_ER_TIMESTAMP_DOY(pRTC->ERLASTSTAMP[ch]);
243 }
244 #endif /*RTC_EV_SUPPORT*/