aboutsummaryrefslogtreecommitdiff
path: root/KSDK_1.2.0/platform/drivers/inc/fsl_rtc_driver.h
blob: 99584253e2a20e9c08220ecec985402eac442ab4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#if !defined(__FSL_RTC_DRIVER_H__)
#define __FSL_RTC_DRIVER_H__

#include <stdint.h>
#include "fsl_rtc_hal.h"
#include "fsl_interrupt_manager.h"

#if FSL_FEATURE_SOC_RTC_COUNT

/*!
 * @addtogroup rtc_driver
 * @{
 */

/*******************************************************************************
 * Definitions
 ******************************************************************************/

/*! @brief Table of base addresses for RTC instances. */
extern RTC_Type * const g_rtcBase[RTC_INSTANCE_COUNT];

/*! @brief Table to save RTC Alarm IRQ numbers for RTC instances. */
extern const IRQn_Type g_rtcIrqId[RTC_INSTANCE_COUNT];
/*! @brief Table to save RTC Seconds IRQ numbers for RTC instances. */
extern const IRQn_Type g_rtcSecondsIrqId[RTC_INSTANCE_COUNT];

/*!
 *  @brief RTC repeated alarm information used by the RTC driver
 */
typedef struct RtcRepeatAlarmState
{
    rtc_datetime_t alarmTime; /*!< Set the RTC alarm time. */
    rtc_datetime_t alarmRepTime;       /*!< Period for alarm to repeat, needs alarm interrupt be enabled.*/
} rtc_repeat_alarm_state_t;

/*******************************************************************************
 * API
 ******************************************************************************/

#if defined(__cplusplus)
extern "C" {
#endif

/*!
 * @name Initialization and De-initialization
 * @{
 */

/*!
 * @brief  Initializes the RTC module.
 *
 * Enables the RTC clock and interrupts if requested by the user.
 *
 * @param  instance The RTC peripheral instance number.
 * @return kStatusRtcSuccess means succees, otherwise means failed.
 */
rtc_status_t RTC_DRV_Init(uint32_t instance);

/*!
 * @brief  Disables the RTC module clock gate control.
 *
 * @param  instance The RTC peripheral instance number.
 */
void RTC_DRV_Deinit(uint32_t instance);

/* @} */

/*!
 * @brief  Checks whether the RTC is enabled.
 *
 * The function checks the TCE bit in the RTC control register.
 *
 * @param  instance The RTC peripheral instance number.
 *
 * @return true: The RTC counter is enabled\n
 *         false: The RTC counter is disabled
 */
bool RTC_DRV_IsCounterEnabled(uint32_t instance);

/*!
 * @name RTC datetime set and get
 * @{
 */

/*!
 * @brief  Sets the RTC date and time according to the given time structure.
 *
 * The RTC counter is started after the time is set.
 *
 * @param  instance The RTC peripheral instance number.
 * @param  datetime [in] pointer to structure where the date and time
 *         details to set are stored.
 *
 * @return true: success in setting the time and starting the RTC\n
 *         false: failure. An error occurs because the datetime format is incorrect.
 */
bool RTC_DRV_SetDatetime(uint32_t instance, rtc_datetime_t *datetime);

/*!
 * @brief  Gets the RTC time and stores it in the given time structure.
 *
 * @param  instance The RTC peripheral instance number.
 * @param  datetime [out] pointer to structure where the date and time details are
 *         stored.
 */
void RTC_DRV_GetDatetime(uint32_t instance, rtc_datetime_t *datetime);
/* @} */

/*!
 * @brief  Enables or disables the RTC seconds interrupt.
 *
 * @param  instance The RTC peripheral instance number.
 * @param  secondsEnable Takes true or false\n
 *          true: indicates seconds interrupt should be enabled\n
 *          false: indicates seconds interrupt should be disabled
 */
void RTC_DRV_SetSecsIntCmd(uint32_t instance, bool secondsEnable);

/*!
 * @name RTC alarm
 * @{
 */

/*!
 * @brief  Sets the RTC alarm time and enables the alarm interrupt.
 *
 * The function checks if the specified alarm time is greater than the present
 * time. If not, the function does not set the alarm and returns an error.
 *
 * @param  instance The RTC peripheral instance number.
 * @param  alarmTime [in] pointer to structure where the alarm time is store.
 * @param  enableAlarmInterrupt Takes true of false\n
 *          true: indicates alarm interrupt should be enabled\n
 *          false: indicates alarm interrupt should be disabled
 *
 * @return  true: success in setting the RTC alarm\n
 *          false: error in setting the RTC alarm. Error is because the alarm datetime format
 *                 is incorrect.
 */
bool RTC_DRV_SetAlarm(uint32_t instance, rtc_datetime_t *alarmTime, bool enableAlarmInterrupt);

/*!
 * @brief  Returns the RTC alarm time.
 *
 * @param  instance The RTC peripheral instance number.
 * @param  date [out] pointer to structure where the alarm date and time
 *         details are stored.
 */
void RTC_DRV_GetAlarm(uint32_t instance, rtc_datetime_t *date);

/*!
 * @brief  Initializes the RTC repeat alarm state structure.
 *
 * The RTC driver uses this user-provided structure to store the alarm state
 * information.
 *
 * @param  instance The RTC peripheral instance number.
 * @param  repeatAlarmState Pointer to structure where the alarm state is stored
 */
void RTC_DRV_InitRepeatAlarm(uint32_t instance, rtc_repeat_alarm_state_t *repeatAlarmState);

/*!
 * @brief  Sets an alarm that is periodically repeated.
 *
 * @param  instance The RTC peripheral instance number.
 * @param  alarmTime Pointer to structure where the alarm time is provided.
 * @param  alarmRepInterval pointer to structure with the alarm repeat interval.
 *
 * @return true: success in setting the RTC alarm\n
 *         false: error in setting the RTC alarm. Error is because the alarm datetime format
 *                is incorrect.
 */
bool RTC_DRV_SetAlarmRepeat(uint32_t instance, rtc_datetime_t *alarmTime, rtc_datetime_t *alarmRepInterval);

/*!
 * @brief  De-initializes the RTC repeat alarm state structure.
 *
 * @param  instance The RTC peripheral instance number.
 */
void RTC_DRV_DeinitRepeatAlarm(uint32_t instance);

/* @} */

/*!
 * @brief  Enables or disables the alarm interrupt.
 *
 * @param  instance The RTC peripheral instance number.
 * @param  alarmEnable Takes true or false\n
 *          true: indicates alarm interrupt should be enabled\n
 *          false: indicates alarm interrupt should be disabled
 */
void RTC_DRV_SetAlarmIntCmd(uint32_t instance, bool alarmEnable);

/*!
 * @brief  Reads the alarm interrupt.
 *
 * @param  instance The RTC peripheral instance number.
 *
 * @return  true: indicates alarm interrupt is enabled\n
 *          false: indicates alarm interrupt is disabled
 */
bool RTC_DRV_GetAlarmIntCmd(uint32_t instance);

/*!
 * @brief  Reads the alarm status to see if the alarm has triggered.
 *
 * @param  instance The RTC peripheral instance number.
 *
 * @return  returns alarm status, for example, returns whether the alarm triggered\n
 *          true: indicates alarm has occurred\n
 *          false: indicates alarm has not occurred
 */
bool RTC_DRV_IsAlarmPending(uint32_t instance);

/*!
 * @brief  Writes the compensation value to the RTC compensation register.
 *
 * @param  instance The RTC peripheral instance number.
 * @param  compensationInterval User specified compensation interval that is written
 *                           to the CIR field in RTC Time Compensation Register (TCR)
 * @param  compensationTime User specified compensation time that is written
 *                           to the TCR field in RTC Time Compensation Register (TCR)
 */
void RTC_DRV_SetTimeCompensation(uint32_t instance, uint32_t compensationInterval,
                                            uint32_t compensationTime);


/*!
 * @brief  Reads the compensation value from the RTC compensation register.
 *
 * @param  instance The RTC peripheral instance number.
 * @param  compensationInterval User specified pointer to store the compensation interval counter. This value
 *                           is read from the CIC field in RTC Time Compensation Register (TCR)
 * @param  compensationTime User specified pointer to store the compensation time value. This value
 *                           is read from the TCV field in RTC Time Compensation Register (TCR)
 */
void RTC_DRV_GetTimeCompensation(uint32_t instance, uint32_t *compensationInterval,
                                            uint32_t *compensationTime);


#if FSL_FEATURE_RTC_HAS_MONOTONIC
/*!
 * @name Increments monotonic counter
 * @{
 */

/*!
 * @brief      Increments the monotonic counter by one.
 *
 * @param  instance The RTC peripheral instance number.
 *
 * @return     True: increment successful\n
 *             False: error invalid time found because of a tamper source enabled is detected
 *             and any write to the tamper time seconds counter is done.
 */
bool RTC_DRV_IncrementMonotonic(uint32_t instance);
/* @} */
#endif

/*!
 * @name ISR Functions
 * @{
 */

/*!
 * @brief Implements the RTC alarm handler named in the startup code.
 *
 * Handles the RTC alarm interrupt and invokes any callback that is interested
 * in the RTC alarm.
 */
void RTC_IRQHandler(void);

/*!
 * @brief Implements the RTC seconds handler named in the startup code.
 *
 * Handles the RTC seconds interrupt and invokes any callback that is interested
 * in the RTC second tick.
 */
void RTC_Seconds_IRQHandler(void);

/*! @}*/

/*!
 * @brief  Action to take when an RTC alarm interrupt is triggered. To receive
 *         alarms periodically, the RTC_TAR register is updated using the repeat interval.
 *
 * @param  instance The RTC peripheral instance number.
 */
void RTC_DRV_AlarmIntAction(uint32_t instance);

/*!
 * @brief  Action to take when an RTC seconds interrupt is triggered.
 *
 * Disables the time seconds interrupt (TSIE) bit.
 *
 * @param  instance The RTC peripheral instance number.
 */
void RTC_DRV_SecsIntAction(uint32_t instance);

#if defined(__cplusplus)
}
#endif

/*! @}*/

#endif /* FSL_FEATURE_SOC_RTC_COUNT */

#endif /* __FSL_RTC_DRIVER_H__*/

/*******************************************************************************
 * EOF
 ******************************************************************************/