1 /*
2  * @brief LPC15xx RTC chip driver
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2014
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 #ifndef __RTC_15XX_H_
33 #define __RTC_15XX_H_
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /** @defgroup RTC_15XX CHIP: LPC15xx RTC driver
40  * @ingroup CHIP_15XX_Drivers
41  * @{
42  */
43 
44 /**
45  * @brief LPC15xx Pin Interrupt and Pattern Match register block structure
46  */
47 typedef struct {			/*!< RTC */
48 	__IO uint32_t CTRL;		/*!< RTC control register */
49 	__IO uint32_t MATCH;	/*!< PRTC match (alarm) register */
50 	__IO uint32_t COUNT;	/*!< RTC counter register */
51 	__IO uint32_t WAKE;		/*!< RTC high-resolution/wake-up timer control register */
52 } LPC_RTC_T;
53 
54 /* CTRL register defniitions */
55 #define RTC_CTRL_SWRESET        (1 << 0)	/*!< Apply reset to RTC */
56 #define RTC_CTRL_OFD            (1 << 1)	/*!< Oscillator fail detect status (failed bit) */
57 #define RTC_CTRL_ALARM1HZ       (1 << 2)	/*!< RTC 1 Hz timer alarm flag status (match) bit */
58 #define RTC_CTRL_WAKE1KHZ       (1 << 3)	/*!< RTC 1 kHz timer wake-up flag status (timeout) bit */
59 #define RTC_CTRL_ALARMDPD_EN    (1 << 4)	/*!< RTC 1 Hz timer alarm for Deep power-down enable bit */
60 #define RTC_CTRL_WAKEDPD_EN     (1 << 5)	/*!< RTC 1 kHz timer wake-up for Deep power-down enable bit */
61 #define RTC_CTRL_RTC1KHZ_EN     (1 << 6)	/*!< RTC 1 kHz clock enable bit */
62 #define RTC_CTRL_RTC_EN         (1 << 7)	/*!< RTC enable bit */
63 
64 /**
65  * @brief	Initialize the RTC peripheral
66  * @param	pRTC	: RTC peripheral selected
67  * @return	None
68  */
Chip_RTC_Init(LPC_RTC_T * pRTC)69 STATIC INLINE void Chip_RTC_Init(LPC_RTC_T *pRTC)
70 {
71 	Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_RTC);
72 }
73 
74 /**
75  * @brief	De-initialize the RTC peripheral
76  * @param	pRTC	: RTC peripheral selected
77  * @return	None
78  */
Chip_RTC_DeInit(LPC_RTC_T * pRTC)79 STATIC INLINE void Chip_RTC_DeInit(LPC_RTC_T *pRTC)
80 {
81 	Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_RTC);
82 }
83 
84 /**
85  * @brief	Enable RTC options
86  * @param	pRTC	: The base address of RTC block
87  * @param	flags	: And OR'ed value of RTC_CTRL_* definitions to enable
88  * @return	Nothing
89  * @note	You can enable multiple RTC options at once using this function
90  *			by OR'ing them together. It is recommended to only use the
91  *			RTC_CTRL_ALARMDPD_EN, RTC_CTRL_WAKEDPD_EN, RTC_CTRL_RTC1KHZ_EN, and
92  *			RTC_CTRL_RTC_EN flags with this function.
93  */
Chip_RTC_EnableOptions(LPC_RTC_T * pRTC,uint32_t flags)94 STATIC INLINE void Chip_RTC_EnableOptions(LPC_RTC_T *pRTC, uint32_t flags)
95 {
96 	pRTC->CTRL |= flags;
97 }
98 
99 /**
100  * @brief	Disable RTC options
101  * @param	pRTC	: The base address of RTC block
102  * @param	flags	: And OR'ed value of RTC_CTRL_* definitions to disable
103  * @return	Nothing
104  * @note	You can enable multiple RTC options at once using this function
105  *			by OR'ing them together. It is recommended to only use the
106  *			RTC_CTRL_ALARMDPD_EN, RTC_CTRL_WAKEDPD_EN, RTC_CTRL_RTC1KHZ_EN, and
107  *			RTC_CTRL_RTC_EN flags with this function.
108  */
Chip_RTC_DisableOptions(LPC_RTC_T * pRTC,uint32_t flags)109 STATIC INLINE void Chip_RTC_DisableOptions(LPC_RTC_T *pRTC, uint32_t flags)
110 {
111 	pRTC->CTRL &= ~flags;
112 }
113 
114 /**
115  * @brief	Reset RTC
116  * @param	pRTC	: The base address of RTC block
117  * @return	Nothing
118  * @note	The RTC state will be returned to it's default.
119  */
Chip_RTC_Reset(LPC_RTC_T * pRTC)120 STATIC INLINE void Chip_RTC_Reset(LPC_RTC_T *pRTC)
121 {
122 	Chip_RTC_EnableOptions(pRTC, RTC_CTRL_SWRESET);
123 	Chip_RTC_DisableOptions(pRTC, RTC_CTRL_SWRESET);
124 }
125 
126 /**
127  * @brief	Enables the RTC
128  * @param	pRTC	: The base address of RTC block
129  * @return	Nothing
130  * @note	You can also use Chip_RTC_EnableOptions() with the
131  *			RTC_CTRL_RTC_EN flag to enable the RTC.
132  */
Chip_RTC_Enable(LPC_RTC_T * pRTC)133 STATIC INLINE void Chip_RTC_Enable(LPC_RTC_T *pRTC)
134 {
135 	Chip_RTC_EnableOptions(pRTC, RTC_CTRL_RTC_EN);
136 }
137 
138 /**
139  * @brief	Disables the RTC
140  * @param	pRTC	: The base address of RTC block
141  * @return	Nothing
142  * @note	You can also use Chip_RTC_DisableOptions() with the
143  *			RTC_CTRL_RTC_EN flag to enable the RTC.
144  */
Chip_RTC_Disable(LPC_RTC_T * pRTC)145 STATIC INLINE void Chip_RTC_Disable(LPC_RTC_T *pRTC)
146 {
147 	Chip_RTC_DisableOptions(pRTC, RTC_CTRL_RTC_EN);
148 }
149 
150 /**
151  * @brief	Enables the RTC 1KHz high resolution timer
152  * @param	pRTC	: The base address of RTC block
153  * @return	Nothing
154  * @note	You can also use Chip_RTC_EnableOptions() with the
155  *			RTC_CTRL_RTC1KHZ_EN flag to enable the high resolution
156  *			timer.
157  */
Chip_RTC_Enable1KHZ(LPC_RTC_T * pRTC)158 STATIC INLINE void Chip_RTC_Enable1KHZ(LPC_RTC_T *pRTC)
159 {
160 	Chip_RTC_EnableOptions(pRTC, RTC_CTRL_RTC1KHZ_EN);
161 }
162 
163 /**
164  * @brief	Disables the RTC 1KHz high resolution timer
165  * @param	pRTC	: The base address of RTC block
166  * @return	Nothing
167  * @note	You can also use Chip_RTC_DisableOptions() with the
168  *			RTC_CTRL_RTC1KHZ_EN flag to disable the high resolution
169  *			timer.
170  */
Chip_RTC_Disable1KHZ(LPC_RTC_T * pRTC)171 STATIC INLINE void Chip_RTC_Disable1KHZ(LPC_RTC_T *pRTC)
172 {
173 	Chip_RTC_DisableOptions(pRTC, RTC_CTRL_RTC1KHZ_EN);
174 }
175 
176 /**
177  * @brief	Enables selected RTC wakeup events
178  * @param	pRTC	: The base address of RTC block
179  * @param	ints	: Wakeup events to enable
180  * @return	Nothing
181  * @note	Select either one or both (OR'ed) RTC_CTRL_ALARMDPD_EN
182  *			and RTC_CTRL_WAKEDPD_EN values to enabled. You can also
183  *			use Chip_RTC_EnableOptions() with the flags to enable
184  *			the events.
185  */
Chip_RTC_EnableWakeup(LPC_RTC_T * pRTC,uint32_t ints)186 STATIC INLINE void Chip_RTC_EnableWakeup(LPC_RTC_T *pRTC, uint32_t ints)
187 {
188 	Chip_RTC_EnableOptions(pRTC, ints);
189 }
190 
191 /**
192  * @brief	Disables selected RTC wakeup events
193  * @param	pRTC	: The base address of RTC block
194  * @param	ints	: Wakeup events to disable
195  * @return	Nothing
196  * @note	Select either one or both (OR'ed) RTC_CTRL_ALARMDPD_EN
197  *			and RTC_CTRL_WAKEDPD_EN values to disabled. You can also
198  *			use Chip_RTC_DisableOptions() with the flags to disable
199  *			the events.
200  */
Chip_RTC_DisableWakeup(LPC_RTC_T * pRTC,uint32_t ints)201 STATIC INLINE void Chip_RTC_DisableWakeup(LPC_RTC_T *pRTC, uint32_t ints)
202 {
203 	Chip_RTC_DisableOptions(pRTC, ints);
204 }
205 
206 /**
207  * @brief	Clears latched RTC statuses
208  * @param	pRTC	: The base address of RTC block
209  * @param	stsMask	: OR'ed status bits to clear
210  * @return	Nothing
211  * @note	Use and OR'ed stsMask value of RTC_CTRL_OFD, RTC_CTRL_ALARM1HZ,
212  *			and RTC_CTRL_WAKE1KHZ to clear specific RTC states.
213  */
Chip_RTC_ClearStatus(LPC_RTC_T * pRTC,uint32_t stsMask)214 STATIC INLINE uint32_t Chip_RTC_ClearStatus(LPC_RTC_T *pRTC, uint32_t stsMask)
215 {
216 	return pRTC->CTRL;
217 }
218 
219 /**
220  * @brief	Return RTC control/status register
221  * @param	pRTC	: The base address of RTC block
222  * @return	The current RTC control/status register
223  * @note	Mask the return value with a RTC_CTRL_* definitions to determine
224  *			which bits are set. For example, mask the return value with
225  *			RTC_CTRL_ALARM1HZ to determine if the alarm interrupt is pending.
226  */
Chip_RTC_GetStatus(LPC_RTC_T * pRTC)227 STATIC INLINE uint32_t Chip_RTC_GetStatus(LPC_RTC_T *pRTC)
228 {
229 	return pRTC->CTRL;
230 }
231 
232 /**
233  * @brief	Set RTC match value for alarm status/interrupt
234  * @param	pRTC	: The base address of RTC block
235  * @param	count	: Alarm event time
236  * @return	Nothing
237  */
Chip_RTC_SetAlarm(LPC_RTC_T * pRTC,uint32_t count)238 STATIC INLINE void Chip_RTC_SetAlarm(LPC_RTC_T *pRTC, uint32_t count)
239 {
240 	pRTC->MATCH = count;
241 }
242 
243 /**
244  * @brief	Return the RTC match value used for alarm status/interrupt
245  * @param	pRTC	: The base address of RTC block
246  * @return	Alarm event time
247  */
Chip_RTC_GetAlarm(LPC_RTC_T * pRTC)248 STATIC INLINE uint32_t Chip_RTC_GetAlarm(LPC_RTC_T *pRTC)
249 {
250 	return pRTC->MATCH;
251 }
252 
253 /**
254  * @brief	Set RTC match count for 1 second timer count
255  * @param	pRTC	: The base address of RTC block
256  * @param	count	: Initial count to set
257  * @return	Nothing
258  * @note	Only write to this register when the RTC_CTRL_RTC_EN bit in
259  *			the CTRL Register is 0. The counter increments one second
260  *			after the RTC_CTRL_RTC_EN bit is set.
261  */
Chip_RTC_SetCount(LPC_RTC_T * pRTC,uint32_t count)262 STATIC INLINE void Chip_RTC_SetCount(LPC_RTC_T *pRTC, uint32_t count)
263 {
264 	pRTC->COUNT = count;
265 }
266 
267 /**
268  * @brief	Get current RTC 1 second timer count
269  * @param	pRTC	: The base address of RTC block
270  * @return	current RTC 1 second timer count
271  */
Chip_RTC_GetCount(LPC_RTC_T * pRTC)272 STATIC INLINE uint32_t Chip_RTC_GetCount(LPC_RTC_T *pRTC)
273 {
274 	return pRTC->COUNT;
275 }
276 
277 /**
278  * @brief	Set RTC wake count countdown value (in mS ticks)
279  * @param	pRTC	: The base address of RTC block
280  * @param	count	: wakeup time in milliSeconds
281  * @return	Nothing
282  * @note	A write pre-loads a start count value into the wake-up
283  *			timer and initializes a count-down sequence.
284  */
Chip_RTC_SetWake(LPC_RTC_T * pRTC,uint16_t count)285 STATIC INLINE void Chip_RTC_SetWake(LPC_RTC_T *pRTC, uint16_t count)
286 {
287 	pRTC->WAKE = count;
288 }
289 
290 /**
291  * @brief	Get RTC wake count countdown value
292  * @param	pRTC	: The base address of RTC block
293  * @return	current RTC wake count countdown value (in mS)
294  */
Chip_RTC_GetWake(LPC_RTC_T * pRTC)295 STATIC INLINE uint16_t Chip_RTC_GetWake(LPC_RTC_T *pRTC)
296 {
297 	return pRTC->WAKE;
298 }
299 
300 /**
301  * @}
302  */
303 
304 #ifdef __cplusplus
305 }
306 #endif
307 
308 #endif /* __RTC_15XX_H_ */
309