| 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_ev.c
Go to the documentation of this file.
1 /*
2 * @brief Event Monitor/Recorder example
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 "board.h"
33
66 /*****************************************************************************
67 * Private types/enumerations/variables
68 ****************************************************************************/
69 STATIC volatile bool fIntervalReached;
70
71 /*****************************************************************************
72 * Public types/enumerations/variables
73 ****************************************************************************/
74
75 /*****************************************************************************
76 * Private functions
77 ****************************************************************************/
78
79 /* Shows the current time and date */
80 static void showTime(IP_RTC_TIME_T *pTime)
81 {
82 DEBUGOUT("Time: %.2d:%.2d:%.2d %.2d/%.2d/%.4d\r\n",
83 pTime->time[RTC_TIMETYPE_HOUR],
84 pTime->time[RTC_TIMETYPE_MINUTE],
85 pTime->time[RTC_TIMETYPE_SECOND],
86 pTime->time[RTC_TIMETYPE_MONTH],
87 pTime->time[RTC_TIMETYPE_DAYOFMONTH],
88 pTime->time[RTC_TIMETYPE_YEAR]);
89 }
90
91 /* Shows timestamp value */
92 static void showTimeStamp(RTC_EV_TIMESTAMP_T *pTime)
93 {
94 DEBUGOUT(" %.2d:%.2d:%.2d (DOY: %.3d)",
95 pTime->hour,
96 pTime->min,
97 pTime->sec,
98 pTime->dayofyear);
99 }
100
101 /*****************************************************************************
102 * Public functions
103 ****************************************************************************/
108 void RTC_IRQHandler(void)
109 {
110 RTC_EV_TIMESTAMP_T timestamp;
111 uint8_t i = 0;
112 uint32_t sec;
113
114 if (Chip_RTC_GetIntPending(LPC_RTC, RTC_INT_COUNTER_INCREASE)) {
115 /* Clear pending interrupt */
116 Chip_RTC_ClearIntPending(LPC_RTC, RTC_INT_COUNTER_INCREASE);
117
118 /* display timestamp every 5 seconds in the background */
119 sec = Chip_RTC_GetTime(LPC_RTC, RTC_TIMETYPE_SECOND);
120 if ((sec % 5) == 0) {
121 fIntervalReached = true; /* set flag for background */
122 }
123 }
124
125 for (i = 0; i < RTC_EV_CHANNEL_NUM; i++) {
126 if (Chip_RTC_EV_GetChannelStatus(LPC_RTC, (IP_RTC_EV_CHANNEL_T) i)) {
127 uint8_t counter = 0;
128 Chip_RTC_EV_GetFirstTimeStamp(LPC_RTC, (IP_RTC_EV_CHANNEL_T) i, &timestamp);
129 DEBUGOUT("Event occurred at channel %.1d: ", i);
130 showTimeStamp(&timestamp);
131 Chip_RTC_EV_GetLastTimeStamp(LPC_RTC, (IP_RTC_EV_CHANNEL_T) i, &timestamp);
132 DEBUGOUT(" - ");
133 showTimeStamp(&timestamp);
134 counter = Chip_RTC_EV_GetCounter(LPC_RTC, (IP_RTC_EV_CHANNEL_T) i);
135 DEBUGOUT(", Counter = %.1d\r\n", counter);
136 Chip_RTC_EV_ClearChannelStatus(LPC_RTC, (RTC_EV_CHANNEL_T) i);
137 }
138 }
139
140 if (Chip_RTC_EV_GetStatus(LPC_RTC)) {
141 Chip_RTC_EV_ClearStatus(LPC_RTC, Chip_RTC_EV_GetStatus(LPC_RTC));
142 }
143
144 }
145
150 int main(void)
151 {
152 IP_RTC_TIME_T FullTime;
153
154 Board_Init();
155
156 Board_RTC_EV_Init();
157
158 fIntervalReached = 0;
159
160 DEBUGSTR("The RTC operates on a 1 Hz clock.\r\n" \
161 "Register writes can take up to 2 cycles.\r\n" \
162 "It will take a few seconds to fully\r\n" \
163 "initialize it and start it running.\r\n\r\n");
164
165 DEBUGSTR("We'll print a timestamp every 5 seconds.\r\n");
166 DEBUGSTR("When a positive edge occurred on J5.18 or negative edge occurred on J5.17 pin, " \
167 "the first and last timestamp will be printed out.\r\n");
168
169 Chip_RTC_Init(LPC_RTC);
170
171 /* Set current time for RTC 2:00:00PM, 2012-10-05 */
172 FullTime.time[RTC_TIMETYPE_SECOND] = 0;
173 FullTime.time[RTC_TIMETYPE_MINUTE] = 0;
174 FullTime.time[RTC_TIMETYPE_HOUR] = 14;
175 FullTime.time[RTC_TIMETYPE_DAYOFMONTH] = 5;
176 FullTime.time[RTC_TIMETYPE_DAYOFWEEK] = 5;
177 FullTime.time[RTC_TIMETYPE_DAYOFYEAR] = 279;
178 FullTime.time[RTC_TIMETYPE_MONTH] = 10;
179 FullTime.time[RTC_TIMETYPE_YEAR] = 2012;
180
181 Chip_RTC_SetFullTime(LPC_RTC, &FullTime);
182
183 /* Set the RTC to generate an interrupt on each second */
184 Chip_RTC_CntIncrIntConfig(LPC_RTC, RTC_AMR_CIIR_IMSEC, ENABLE);
185
186 /* Clear interrupt pending */
187 Chip_RTC_ClearIntPending(LPC_RTC, RTC_INT_COUNTER_INCREASE);
188
189 /* Enable RTC (starts increase the tick counter and second counter register) */
190 Chip_RTC_Enable(LPC_RTC, ENABLE);
191
192 /* Initialize Event Monitor/recorder */
193 Chip_RTC_EV_Config(LPC_RTC, RTC_EV_CHANNEL_1, RTC_ERCTRL_POL_POSITIVE | RTC_ERCTRL_INPUT_EN); /* Event on Negative edge */
194 Chip_RTC_EV_Config(LPC_RTC, RTC_EV_CHANNEL_2, RTC_ERCTRL_POL_POSITIVE); /* Event on Positive edge */
195 Chip_RTC_EV_Config(LPC_RTC, RTC_EV_CHANNEL_3, RTC_ERCTRL_POL_NEGATIVE | RTC_ERCTRL_INPUT_EN);/* Event on Negative edge */
196
197 /* Enable Event Monitor/Recorder */
198 Chip_RTC_EV_SetMode(LPC_RTC, RTC_EV_MODE_ENABLE_64HZ);
199
200 /* Enable RTC interrupt in NVIC */
201 NVIC_EnableIRQ((IRQn_Type) RTC_IRQn);
202
203 /* Loop forever */
204 while (1) {
205 if (fIntervalReached) { /* Every 5s */
206 fIntervalReached = false;
207
208 /* read and display time */
209 Chip_RTC_GetFullTime(LPC_RTC, &FullTime);
210 showTime(&FullTime);
211 }
212 }
213 }
214