LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 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 */
52 {
53  do {
54  /* Reset RTC clock*/
56  } while (pRTC->CCR != (RTC_CCR_CTCRST | RTC_CCR_CCALEN));
57  do {
58  /* Finish resetting RTC clock */
59  pRTC->CCR = RTC_CCR_CCALEN;
60  } while (pRTC->CCR != RTC_CCR_CCALEN);
61 
62  /* Clear counter increment and alarm interrupt */
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 */
74 {
75  pRTC->CCR |= RTC_CCR_CTCRST;
76  pRTC->CCR &= (~RTC_CCR_CTCRST) & RTC_CCR_BITMASK;
77 }
78 
79 /* Start/Stop RTC peripheral */
81 {
82  if (NewState == ENABLE) {
83  do {
84  pRTC->CCR |= RTC_CCR_CLKEN;
85  } while ((pRTC->CCR & RTC_CCR_CLKEN) == 0);
86  }
87  else {
88  pRTC->CCR &= (~RTC_CCR_CLKEN) & RTC_CCR_BITMASK;
89  }
90 }
91 
92 /* Enable/Disable Counter increment interrupt for a time type in the RTC peripheral */
94  FunctionalState NewState)
95 {
96  if (NewState == ENABLE) {
97  pRTC->CIIR |= cntrMask;
98  }
99 
100  else {
101  pRTC->CIIR &= (~cntrMask) & RTC_AMR_CIIR_BITMASK;
102  while (pRTC->CIIR & cntrMask) {}
103  }
104 
105 }
106 
107 /* Enable/Disable Alarm interrupt for a time type in the RTC peripheral */
109  FunctionalState NewState)
110 {
111  if (NewState == ENABLE) {
112  pRTC->AMR &= (~alarmMask) & RTC_AMR_CIIR_BITMASK;
113  }
114  else {
115  pRTC->AMR |= (alarmMask);
116  while ((pRTC->AMR & alarmMask) == 0) {}
117  }
118 }
119 
120 /* Set current time value for a time type in the RTC peripheral */
121 void IP_RTC_SetTime(IP_RTC_001_T *pRTC, IP_RTC_TIMEINDEX_T Timetype, uint32_t TimeValue)
122 {
123  pRTC->TIME[Timetype] = TimeValue;
124 }
125 
126 /* Get current time value for a type time type */
128 {
129  return pRTC->TIME[Timetype];
130 }
131 
132 /* Set full time in the RTC peripheral */
134 {
136  uint32_t secs = 0xFF;
137 
138  /* Write time to registers and verify second tick didn't update during the
139  write cycle. If it did, the time may not be consistent across all fields,
140  so write the time again */
141  while (secs != pRTC->TIME[RTC_TIMETYPE_SECOND]) {
142  /* Write seconds first */
143  for (i = RTC_TIMETYPE_SECOND; i < RTC_TIMETYPE_LAST; i++) {
144  pRTC->TIME[i] = pFullTime->time[i];
145  }
146 
147  secs = pRTC->TIME[RTC_TIMETYPE_SECOND];
148  }
149 }
150 
151 /* Get full time from the RTC peripheral */
153 {
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 alarm time value for a time type */
169 {
170  pRTC->ALRM[Timetype] = ALValue;
171 }
172 
173 /* Get alarm time value for a time */
175 {
176  return pRTC->ALRM[Timetype];
177 }
178 
179 /* Set full alarm time in the RTC peripheral */
181 {
183 
184  for (i = RTC_TIMETYPE_SECOND; i < RTC_TIMETYPE_LAST; i++) {
185  pRTC->ALRM[i] = pFullTime->time[i];
186  }
187 }
188 
189 /* Get full alarm time in the RTC peripheral */
191 {
193 
194  for (i = RTC_TIMETYPE_SECOND; i < RTC_TIMETYPE_LAST; i++) {
195  pFullTime->time[i] = pRTC->ALRM[i];
196  }
197 }
198 
199 /* Enable/Disable calibration counter in the RTC peripheral */
201 {
202  if (NewState == ENABLE) {
203  do {
204  pRTC->CCR &= (~RTC_CCR_CCALEN) & RTC_CCR_BITMASK;
205  } while (pRTC->CCR & RTC_CCR_CCALEN);
206  }
207  else {
208  pRTC->CCR |= RTC_CCR_CCALEN;
209  }
210 }
211 
212 /* Configures Calibration in the RTC peripheral */
213 void IP_RTC_CalibConfig(IP_RTC_001_T *pRTC, uint32_t CalibValue, uint8_t CalibDir)
214 {
215  pRTC->CALIBRATION = ((CalibValue - 1) & RTC_CALIBRATION_CALVAL_MASK)
216  | ((CalibDir == RTC_CALIB_DIR_BACKWARD) ? RTC_CALIBRATION_LIBDIR : 0);
217 }