1 /**************************************************************************//**
2 * @file rtc.c
3 * @version V3.00
4 * @brief Real Time Clock(RTC) driver source file
5 *
6 * @copyright SPDX-License-Identifier: Apache-2.0
7 * @copyright Copyright (C) 2021 Nuvoton Technology Corp. All rights reserved.
8 *****************************************************************************/
9 #include "NuMicro.h"
10
11
12 /** @cond HIDDEN_SYMBOLS */
13 /*---------------------------------------------------------------------------------------------------------*/
14 /* Global file scope (static) variables */
15 /*---------------------------------------------------------------------------------------------------------*/
16 static volatile uint32_t g_u32hiYear, g_u32loYear, g_u32hiMonth, g_u32loMonth, g_u32hiDay, g_u32loDay;
17 static volatile uint32_t g_u32hiHour, g_u32loHour, g_u32hiMin, g_u32loMin, g_u32hiSec, g_u32loSec;
18
19 /** @endcond HIDDEN_SYMBOLS */
20
21
22 /** @addtogroup Standard_Driver Standard Driver
23 @{
24 */
25
26 /** @addtogroup RTC_Driver RTC Driver
27 @{
28 */
29
30 /** @addtogroup RTC_EXPORTED_FUNCTIONS RTC Exported Functions
31 @{
32 */
33
34 /**
35 * @brief Initialize RTC module and start counting
36 *
37 * @param[in] sPt Specify the time property and current date and time. It includes: \n
38 * u32Year: Year value, range between 2000 ~ 2099. \n
39 * u32Month: Month value, range between 1 ~ 12. \n
40 * u32Day: Day value, range between 1 ~ 31. \n
41 * u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
42 * RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
43 * RTC_SATURDAY] \n
44 * u32Hour: Hour value, range between 0 ~ 23. \n
45 * u32Minute: Minute value, range between 0 ~ 59. \n
46 * u32Second: Second value, range between 0 ~ 59. \n
47 * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
48 * u8AmPm: [RTC_AM / RTC_PM] \n
49 *
50 * @retval 0: SUCCESS
51 * @retval -1: Initialize RTC module fail
52 *
53 * @details This function is used to: \n
54 * 1. Write initial key to let RTC start count. \n
55 * 2. Input parameter indicates start date/time. \n
56 * 3. User has to make sure that parameters of RTC date/time are reasonable. \n
57 * 4. Enable frequency dynamic compensation function. \n
58 * @note Null pointer for using default starting date/time.
59 */
RTC_Open(S_RTC_TIME_DATA_T * sPt)60 int32_t RTC_Open(S_RTC_TIME_DATA_T *sPt)
61 {
62 uint32_t u32TimeOutCount = SystemCoreClock; /* 1 second time-out */
63
64 RTC->INIT = RTC_INIT_KEY;
65
66 if(RTC->INIT != RTC_INIT_ACTIVE_Msk)
67 {
68 RTC->INIT = RTC_INIT_KEY;
69 while(RTC->INIT != RTC_INIT_ACTIVE_Msk)
70 {
71 if(--u32TimeOutCount == 0) return -1;
72 }
73 }
74
75 if(sPt != 0)
76 {
77 /* Enable frequency dynamic compensation function */
78 RTC->CLKFMT |= RTC_CLKFMT_DCOMPEN_Msk;
79
80 /* Set RTC date and time */
81 RTC_SetDateAndTime(sPt);
82 }
83
84 return 0;
85 }
86
87 /**
88 * @brief Disable RTC Clock
89 *
90 * @param None
91 *
92 * @return None
93 *
94 * @details This API will disable RTC peripheral clock.
95 */
RTC_Close(void)96 void RTC_Close(void)
97 {
98 CLK->APBCLK0 &= ~CLK_APBCLK0_RTCCKEN_Msk;
99 }
100
101 /**
102 * @brief Set 32K Frequency Compensation Data
103 *
104 * @param[in] i32FrequencyX10000 Specify the RTC clock X10000, ex: 327736512 means 32773.6512.
105 *
106 * @retval RTC_OK RTC operation OK.
107 * @retval RTC_ERR_TIMEOUT RTC operation abort due to timeout error.
108 *
109 * @details This API is used to compensate the 32 kHz frequency by current LXT frequency for RTC application.
110 */
RTC_32KCalibration(int32_t i32FrequencyX10000)111 int32_t RTC_32KCalibration(int32_t i32FrequencyX10000)
112 {
113 int32_t i32RegInt, i32RegFra;
114 uint32_t u32TimeOutCnt;
115
116 /* Compute integer and fraction for RTC FCR register */
117 i32RegInt = (i32FrequencyX10000 / 10000) - RTC_FCR_REFERENCE;
118 i32RegFra = ((((i32FrequencyX10000 % 10000)) * 64) + 5000) / 10000;
119
120 if(i32RegFra >= 0x40)
121 {
122 i32RegFra = 0x0;
123 i32RegInt++;
124 }
125
126 /* Judge Integer part is reasonable */
127 if((i32RegInt >= 0) && (i32RegInt <= 31))
128 {
129 u32TimeOutCnt = SystemCoreClock<<1; /* 2 second time-out */
130 while((RTC->FREQADJ & RTC_FREQADJ_FCRBUSY_Msk) == RTC_FREQADJ_FCRBUSY_Msk)
131 if(--u32TimeOutCnt == 0) return RTC_ERR_TIMEOUT;
132 RTC->FREQADJ = (uint32_t)((i32RegInt << 8) | i32RegFra);
133 u32TimeOutCnt = SystemCoreClock<<1; /* 2 second time-out */
134 while((RTC->FREQADJ & RTC_FREQADJ_FCRBUSY_Msk) == RTC_FREQADJ_FCRBUSY_Msk)
135 if(--u32TimeOutCnt == 0) return RTC_ERR_TIMEOUT;
136 }
137
138 return RTC_OK;
139 }
140
141 /**
142 * @brief Get Current RTC Date and Time
143 *
144 * @param[out] sPt The returned pointer is specified the current RTC value. It includes: \n
145 * u32Year: Year value \n
146 * u32Month: Month value \n
147 * u32Day: Day value \n
148 * u32DayOfWeek: Day of week \n
149 * u32Hour: Hour value \n
150 * u32Minute: Minute value \n
151 * u32Second: Second value \n
152 * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
153 * u8AmPm: [RTC_AM / RTC_PM] \n
154 *
155 * @return None
156 *
157 * @details This API is used to get the current RTC date and time value.
158 */
RTC_GetDateAndTime(S_RTC_TIME_DATA_T * sPt)159 void RTC_GetDateAndTime(S_RTC_TIME_DATA_T *sPt)
160 {
161 uint32_t u32Tmp;
162
163 sPt->u32TimeScale = RTC->CLKFMT & RTC_CLKFMT_24HEN_Msk; /* 12/24-hour */
164 sPt->u32DayOfWeek = RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk; /* Day of the week */
165
166 /* Get [Date digit] data */
167 g_u32hiYear = (RTC->CAL & RTC_CAL_TENYEAR_Msk) >> RTC_CAL_TENYEAR_Pos;
168 g_u32loYear = (RTC->CAL & RTC_CAL_YEAR_Msk) >> RTC_CAL_YEAR_Pos;
169 g_u32hiMonth = (RTC->CAL & RTC_CAL_TENMON_Msk) >> RTC_CAL_TENMON_Pos;
170 g_u32loMonth = (RTC->CAL & RTC_CAL_MON_Msk) >> RTC_CAL_MON_Pos;
171 g_u32hiDay = (RTC->CAL & RTC_CAL_TENDAY_Msk) >> RTC_CAL_TENDAY_Pos;
172 g_u32loDay = (RTC->CAL & RTC_CAL_DAY_Msk) >> RTC_CAL_DAY_Pos;
173
174 /* Get [Time digit] data */
175 g_u32hiHour = (RTC->TIME & RTC_TIME_TENHR_Msk) >> RTC_TIME_TENHR_Pos;
176 g_u32loHour = (RTC->TIME & RTC_TIME_HR_Msk) >> RTC_TIME_HR_Pos;
177 g_u32hiMin = (RTC->TIME & RTC_TIME_TENMIN_Msk) >> RTC_TIME_TENMIN_Pos;
178 g_u32loMin = (RTC->TIME & RTC_TIME_MIN_Msk) >> RTC_TIME_MIN_Pos;
179 g_u32hiSec = (RTC->TIME & RTC_TIME_TENSEC_Msk) >> RTC_TIME_TENSEC_Pos;
180 g_u32loSec = (RTC->TIME & RTC_TIME_SEC_Msk) >> RTC_TIME_SEC_Pos;
181
182 /* Compute to 20XX year */
183 u32Tmp = (g_u32hiYear * 10UL);
184 u32Tmp += g_u32loYear;
185 sPt->u32Year = u32Tmp + (uint32_t)RTC_YEAR2000;
186
187 /* Compute 0~12 month */
188 u32Tmp = (g_u32hiMonth * 10UL);
189 sPt->u32Month = u32Tmp + g_u32loMonth;
190
191 /* Compute 0~31 day */
192 u32Tmp = (g_u32hiDay * 10UL);
193 sPt->u32Day = u32Tmp + g_u32loDay;
194
195 /* Compute 12/24 hour */
196 if(sPt->u32TimeScale == (uint32_t)RTC_CLOCK_12)
197 {
198 u32Tmp = (g_u32hiHour * 10UL);
199 u32Tmp += g_u32loHour;
200 sPt->u32Hour = u32Tmp; /* AM: 1~12. PM: 21~32. */
201
202 if(sPt->u32Hour >= 21UL)
203 {
204 sPt->u32AmPm = (uint32_t)RTC_PM;
205 sPt->u32Hour -= 20UL;
206 }
207 else
208 {
209 sPt->u32AmPm = (uint32_t)RTC_AM;
210 }
211
212 u32Tmp = (g_u32hiMin * 10UL);
213 u32Tmp += g_u32loMin;
214 sPt->u32Minute = u32Tmp;
215
216 u32Tmp = (g_u32hiSec * 10UL);
217 u32Tmp += g_u32loSec;
218 sPt->u32Second = u32Tmp;
219 }
220 else
221 {
222 u32Tmp = (g_u32hiHour * 10UL);
223 u32Tmp += g_u32loHour;
224 sPt->u32Hour = u32Tmp;
225
226 u32Tmp = (g_u32hiMin * 10UL);
227 u32Tmp += g_u32loMin;
228 sPt->u32Minute = u32Tmp;
229
230 u32Tmp = (g_u32hiSec * 10UL);
231 u32Tmp += g_u32loSec;
232 sPt->u32Second = u32Tmp;
233 }
234 }
235
236 /**
237 * @brief Get RTC Alarm Date and Time
238 *
239 * @param[out] sPt The returned pointer is specified the RTC alarm value. It includes: \n
240 * u32Year: Year value \n
241 * u32Month: Month value \n
242 * u32Day: Day value \n
243 * u32DayOfWeek: Day of week \n
244 * u32Hour: Hour value \n
245 * u32Minute: Minute value \n
246 * u32Second: Second value \n
247 * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
248 * u8AmPm: [RTC_AM / RTC_PM] \n
249 *
250 * @return None
251 *
252 * @details This API is used to get the RTC alarm date and time setting.
253 */
RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T * sPt)254 void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt)
255 {
256 uint32_t u32Tmp;
257
258 sPt->u32TimeScale = RTC->CLKFMT & RTC_CLKFMT_24HEN_Msk; /* 12/24-hour */
259 sPt->u32DayOfWeek = RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk; /* Day of the week */
260
261 /* Get alarm [Date digit] data */
262 g_u32hiYear = (RTC->CALM & RTC_CALM_TENYEAR_Msk) >> RTC_CALM_TENYEAR_Pos;
263 g_u32loYear = (RTC->CALM & RTC_CALM_YEAR_Msk) >> RTC_CALM_YEAR_Pos;
264 g_u32hiMonth = (RTC->CALM & RTC_CALM_TENMON_Msk) >> RTC_CALM_TENMON_Pos;
265 g_u32loMonth = (RTC->CALM & RTC_CALM_MON_Msk) >> RTC_CALM_MON_Pos;
266 g_u32hiDay = (RTC->CALM & RTC_CALM_TENDAY_Msk) >> RTC_CALM_TENDAY_Pos;
267 g_u32loDay = (RTC->CALM & RTC_CALM_DAY_Msk) >> RTC_CALM_DAY_Pos;
268
269 /* Get alarm [Time digit] data */
270 g_u32hiHour = (RTC->TALM & RTC_TALM_TENHR_Msk) >> RTC_TALM_TENHR_Pos;
271 g_u32loHour = (RTC->TALM & RTC_TALM_HR_Msk) >> RTC_TALM_HR_Pos;
272 g_u32hiMin = (RTC->TALM & RTC_TALM_TENMIN_Msk) >> RTC_TALM_TENMIN_Pos;
273 g_u32loMin = (RTC->TALM & RTC_TALM_MIN_Msk) >> RTC_TALM_MIN_Pos;
274 g_u32hiSec = (RTC->TALM & RTC_TALM_TENSEC_Msk) >> RTC_TALM_TENSEC_Pos;
275 g_u32loSec = (RTC->TALM & RTC_TALM_SEC_Msk) >> RTC_TALM_SEC_Pos;
276
277 /* Compute to 20XX year */
278 u32Tmp = (g_u32hiYear * 10UL);
279 u32Tmp += g_u32loYear;
280 sPt->u32Year = u32Tmp + (uint32_t)RTC_YEAR2000;
281
282 /* Compute 0~12 month */
283 u32Tmp = (g_u32hiMonth * 10UL);
284 sPt->u32Month = u32Tmp + g_u32loMonth;
285
286 /* Compute 0~31 day */
287 u32Tmp = (g_u32hiDay * 10UL);
288 sPt->u32Day = u32Tmp + g_u32loDay;
289
290 /* Compute 12/24 hour */
291 if(sPt->u32TimeScale == (uint32_t)RTC_CLOCK_12)
292 {
293 u32Tmp = (g_u32hiHour * 10UL);
294 u32Tmp += g_u32loHour;
295 sPt->u32Hour = u32Tmp; /* AM: 1~12. PM: 21~32. */
296
297 if(sPt->u32Hour >= 21UL)
298 {
299 sPt->u32AmPm = (uint32_t)RTC_PM;
300 sPt->u32Hour -= 20UL;
301 }
302 else
303 {
304 sPt->u32AmPm = (uint32_t)RTC_AM;
305 }
306
307 u32Tmp = (g_u32hiMin * 10UL);
308 u32Tmp += g_u32loMin;
309 sPt->u32Minute = u32Tmp;
310
311 u32Tmp = (g_u32hiSec * 10UL);
312 u32Tmp += g_u32loSec;
313 sPt->u32Second = u32Tmp;
314 }
315 else
316 {
317 u32Tmp = (g_u32hiHour * 10UL);
318 u32Tmp += g_u32loHour;
319 sPt->u32Hour = u32Tmp;
320
321 u32Tmp = (g_u32hiMin * 10UL);
322 u32Tmp += g_u32loMin;
323 sPt->u32Minute = u32Tmp;
324
325 u32Tmp = (g_u32hiSec * 10UL);
326 u32Tmp += g_u32loSec;
327 sPt->u32Second = u32Tmp;
328 }
329 }
330
331 /**
332 * @brief Update Current RTC Date and Time
333 *
334 * @param[in] sPt Specify the time property and current date and time. It includes: \n
335 * u32Year: Year value, range between 2000 ~ 2099. \n
336 * u32Month: Month value, range between 1 ~ 12. \n
337 * u32Day: Day value, range between 1 ~ 31. \n
338 * u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
339 * RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
340 * RTC_SATURDAY] \n
341 * u32Hour: Hour value, range between 0 ~ 23. \n
342 * u32Minute: Minute value, range between 0 ~ 59. \n
343 * u32Second: Second value, range between 0 ~ 59. \n
344 * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
345 * u8AmPm: [RTC_AM / RTC_PM] \n
346 *
347 * @return None
348 *
349 * @details This API is used to update current date and time to RTC.
350 */
RTC_SetDateAndTime(S_RTC_TIME_DATA_T * sPt)351 void RTC_SetDateAndTime(S_RTC_TIME_DATA_T *sPt)
352 {
353 uint32_t u32RegCAL, u32RegTIME;
354
355 if(sPt != 0)
356 {
357 /*-----------------------------------------------------------------------------------------------------*/
358 /* Set RTC 24/12 hour setting and Day of the Week */
359 /*-----------------------------------------------------------------------------------------------------*/
360 if(sPt->u32TimeScale == (uint32_t)RTC_CLOCK_12)
361 {
362 RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
363
364 /*-------------------------------------------------------------------------------------------------*/
365 /* Important, range of 12-hour PM mode is 21 up to 32 */
366 /*-------------------------------------------------------------------------------------------------*/
367 if(sPt->u32AmPm == (uint32_t)RTC_PM)
368 {
369 sPt->u32Hour += 20UL;
370 }
371 }
372 else
373 {
374 RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
375 }
376
377 /* Set Day of the Week */
378 RTC->WEEKDAY = sPt->u32DayOfWeek;
379
380 /*-----------------------------------------------------------------------------------------------------*/
381 /* Set RTC Current Date and Time */
382 /*-----------------------------------------------------------------------------------------------------*/
383 u32RegCAL = ((sPt->u32Year - (uint32_t)RTC_YEAR2000) / 10UL) << 20;
384 u32RegCAL |= (((sPt->u32Year - (uint32_t)RTC_YEAR2000) % 10UL) << 16);
385 u32RegCAL |= ((sPt->u32Month / 10UL) << 12);
386 u32RegCAL |= ((sPt->u32Month % 10UL) << 8);
387 u32RegCAL |= ((sPt->u32Day / 10UL) << 4);
388 u32RegCAL |= (sPt->u32Day % 10UL);
389
390 u32RegTIME = ((sPt->u32Hour / 10UL) << 20);
391 u32RegTIME |= ((sPt->u32Hour % 10UL) << 16);
392 u32RegTIME |= ((sPt->u32Minute / 10UL) << 12);
393 u32RegTIME |= ((sPt->u32Minute % 10UL) << 8);
394 u32RegTIME |= ((sPt->u32Second / 10UL) << 4);
395 u32RegTIME |= (sPt->u32Second % 10UL);
396
397 /*-----------------------------------------------------------------------------------------------------*/
398 /* Set RTC Calender and Time Loading */
399 /*-----------------------------------------------------------------------------------------------------*/
400 RTC->CAL = (uint32_t)u32RegCAL;
401 RTC->TIME = (uint32_t)u32RegTIME;
402 }
403 }
404
405 /**
406 * @brief Update RTC Alarm Date and Time
407 *
408 * @param[in] sPt Specify the time property and alarm date and time. It includes: \n
409 * u32Year: Year value, range between 2000 ~ 2099. \n
410 * u32Month: Month value, range between 1 ~ 12. \n
411 * u32Day: Day value, range between 1 ~ 31. \n
412 * u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
413 * RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
414 * RTC_SATURDAY] \n
415 * u32Hour: Hour value, range between 0 ~ 23. \n
416 * u32Minute: Minute value, range between 0 ~ 59. \n
417 * u32Second: Second value, range between 0 ~ 59. \n
418 * u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24] \n
419 * u8AmPm: [RTC_AM / RTC_PM] \n
420 *
421 * @return None
422 *
423 * @details This API is used to update alarm date and time setting to RTC.
424 */
RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T * sPt)425 void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T *sPt)
426 {
427 uint32_t u32RegCALM, u32RegTALM;
428
429 if(sPt != 0)
430 {
431 /*-----------------------------------------------------------------------------------------------------*/
432 /* Set RTC 24/12 hour setting and Day of the Week */
433 /*-----------------------------------------------------------------------------------------------------*/
434 if(sPt->u32TimeScale == (uint32_t)RTC_CLOCK_12)
435 {
436 RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
437
438 /*-------------------------------------------------------------------------------------------------*/
439 /* Important, range of 12-hour PM mode is 21 up to 32 */
440 /*-------------------------------------------------------------------------------------------------*/
441 if(sPt->u32AmPm == (uint32_t)RTC_PM)
442 {
443 sPt->u32Hour += 20UL;
444 }
445 }
446 else
447 {
448 RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
449 }
450
451 /*-----------------------------------------------------------------------------------------------------*/
452 /* Set RTC Alarm Date and Time */
453 /*-----------------------------------------------------------------------------------------------------*/
454 u32RegCALM = ((sPt->u32Year - (uint32_t)RTC_YEAR2000) / 10UL) << 20;
455 u32RegCALM |= (((sPt->u32Year - (uint32_t)RTC_YEAR2000) % 10UL) << 16);
456 u32RegCALM |= ((sPt->u32Month / 10UL) << 12);
457 u32RegCALM |= ((sPt->u32Month % 10UL) << 8);
458 u32RegCALM |= ((sPt->u32Day / 10UL) << 4);
459 u32RegCALM |= (sPt->u32Day % 10UL);
460
461 u32RegTALM = ((sPt->u32Hour / 10UL) << 20);
462 u32RegTALM |= ((sPt->u32Hour % 10UL) << 16);
463 u32RegTALM |= ((sPt->u32Minute / 10UL) << 12);
464 u32RegTALM |= ((sPt->u32Minute % 10UL) << 8);
465 u32RegTALM |= ((sPt->u32Second / 10UL) << 4);
466 u32RegTALM |= (sPt->u32Second % 10UL);
467
468 RTC->CALM = (uint32_t)u32RegCALM;
469 RTC->TALM = (uint32_t)u32RegTALM;
470 }
471 }
472
473 /**
474 * @brief Update RTC Current Date
475 *
476 * @param[in] u32Year The year calendar digit of current RTC setting.
477 * @param[in] u32Month The month calendar digit of current RTC setting.
478 * @param[in] u32Day The day calendar digit of current RTC setting.
479 * @param[in] u32DayOfWeek The Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
480 * RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
481 * RTC_SATURDAY]
482 *
483 * @return None
484 *
485 * @details This API is used to update current date to RTC.
486 */
RTC_SetDate(uint32_t u32Year,uint32_t u32Month,uint32_t u32Day,uint32_t u32DayOfWeek)487 void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek)
488 {
489 uint32_t u32RegCAL;
490
491 u32RegCAL = ((u32Year - (uint32_t)RTC_YEAR2000) / 10UL) << 20;
492 u32RegCAL |= (((u32Year - (uint32_t)RTC_YEAR2000) % 10UL) << 16);
493 u32RegCAL |= ((u32Month / 10UL) << 12);
494 u32RegCAL |= ((u32Month % 10UL) << 8);
495 u32RegCAL |= ((u32Day / 10UL) << 4);
496 u32RegCAL |= (u32Day % 10UL);
497
498
499 /* Set Day of the Week */
500 RTC->WEEKDAY = u32DayOfWeek & RTC_WEEKDAY_WEEKDAY_Msk;
501
502 /* Set RTC Calender Loading */
503 RTC->CAL = (uint32_t)u32RegCAL;
504 }
505
506 /**
507 * @brief Update RTC Current Time
508 *
509 * @param[in] u32Hour The hour time digit of current RTC setting.
510 * @param[in] u32Minute The minute time digit of current RTC setting.
511 * @param[in] u32Second The second time digit of current RTC setting.
512 * @param[in] u32TimeMode The 24-Hour / 12-Hour Time Scale Selection. [RTC_CLOCK_12 / RTC_CLOCK_24]
513 * @param[in] u32AmPm 12-hour time scale with AM and PM indication. Only Time Scale select 12-hour used. [RTC_AM / RTC_PM]
514 *
515 * @return None
516 *
517 * @details This API is used to update current time to RTC.
518 */
RTC_SetTime(uint32_t u32Hour,uint32_t u32Minute,uint32_t u32Second,uint32_t u32TimeMode,uint32_t u32AmPm)519 void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
520 {
521 uint32_t u32RegTIME;
522
523 /* Important, range of 12-hour PM mode is 21 up to 32 */
524 if((u32TimeMode == (uint32_t)RTC_CLOCK_12) && (u32AmPm == (uint32_t)RTC_PM))
525 {
526 u32Hour += 20UL;
527 }
528
529 u32RegTIME = ((u32Hour / 10UL) << 20);
530 u32RegTIME |= ((u32Hour % 10UL) << 16);
531 u32RegTIME |= ((u32Minute / 10UL) << 12);
532 u32RegTIME |= ((u32Minute % 10UL) << 8);
533 u32RegTIME |= ((u32Second / 10UL) << 4);
534 u32RegTIME |= (u32Second % 10UL);
535
536 /*-----------------------------------------------------------------------------------------------------*/
537 /* Set RTC 24/12 hour setting and Day of the Week */
538 /*-----------------------------------------------------------------------------------------------------*/
539 if(u32TimeMode == (uint32_t)RTC_CLOCK_12)
540 {
541 RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
542 }
543 else
544 {
545 RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
546 }
547
548 RTC->TIME = (uint32_t)u32RegTIME;
549 }
550
551 /**
552 * @brief Update RTC Alarm Date
553 *
554 * @param[in] u32Year The year calendar digit of RTC alarm setting.
555 * @param[in] u32Month The month calendar digit of RTC alarm setting.
556 * @param[in] u32Day The day calendar digit of RTC alarm setting.
557 *
558 * @return None
559 *
560 * @details This API is used to update alarm date setting to RTC.
561 */
RTC_SetAlarmDate(uint32_t u32Year,uint32_t u32Month,uint32_t u32Day)562 void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day)
563 {
564 uint32_t u32RegCALM;
565
566 u32RegCALM = ((u32Year - (uint32_t)RTC_YEAR2000) / 10UL) << 20;
567 u32RegCALM |= (((u32Year - (uint32_t)RTC_YEAR2000) % 10UL) << 16);
568 u32RegCALM |= ((u32Month / 10UL) << 12);
569 u32RegCALM |= ((u32Month % 10UL) << 8);
570 u32RegCALM |= ((u32Day / 10UL) << 4);
571 u32RegCALM |= (u32Day % 10UL);
572
573
574 /* Set RTC Alarm Date */
575 RTC->CALM = (uint32_t)u32RegCALM;
576 }
577
578 /**
579 * @brief Update RTC Alarm Time
580 *
581 * @param[in] u32Hour The hour time digit of RTC alarm setting.
582 * @param[in] u32Minute The minute time digit of RTC alarm setting.
583 * @param[in] u32Second The second time digit of RTC alarm setting.
584 * @param[in] u32TimeMode The 24-Hour / 12-Hour Time Scale Selection. [RTC_CLOCK_12 / RTC_CLOCK_24]
585 * @param[in] u32AmPm 12-hour time scale with AM and PM indication. Only Time Scale select 12-hour used. [RTC_AM / RTC_PM]
586 *
587 * @return None
588 *
589 * @details This API is used to update alarm time setting to RTC.
590 */
RTC_SetAlarmTime(uint32_t u32Hour,uint32_t u32Minute,uint32_t u32Second,uint32_t u32TimeMode,uint32_t u32AmPm)591 void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
592 {
593 uint32_t u32RegTALM;
594
595 /* Important, range of 12-hour PM mode is 21 up to 32 */
596 if((u32TimeMode == (uint32_t)RTC_CLOCK_12) && (u32AmPm == (uint32_t)RTC_PM))
597 {
598 u32Hour += 20UL;
599 }
600
601 u32RegTALM = ((u32Hour / 10UL) << 20);
602 u32RegTALM |= ((u32Hour % 10UL) << 16);
603 u32RegTALM |= ((u32Minute / 10UL) << 12);
604 u32RegTALM |= ((u32Minute % 10UL) << 8);
605 u32RegTALM |= ((u32Second / 10UL) << 4);
606 u32RegTALM |= (u32Second % 10UL);
607
608 /*-----------------------------------------------------------------------------------------------------*/
609 /* Set RTC 24/12 hour setting and Day of the Week */
610 /*-----------------------------------------------------------------------------------------------------*/
611 if(u32TimeMode == (uint32_t)RTC_CLOCK_12)
612 {
613 RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
614 }
615 else
616 {
617 RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
618 }
619
620 /* Set RTC Alarm Time */
621 RTC->TALM = (uint32_t)u32RegTALM;
622 }
623
624 /**
625 * @brief Set RTC Alarm Date Mask Function
626 *
627 * @param[in] u8IsTenYMsk 1: enable 10-Year digit alarm mask; 0: disabled.
628 * @param[in] u8IsYMsk 1: enable 1-Year digit alarm mask; 0: disabled.
629 * @param[in] u8IsTenMMsk 1: enable 10-Mon digit alarm mask; 0: disabled.
630 * @param[in] u8IsMMsk 1: enable 1-Mon digit alarm mask; 0: disabled.
631 * @param[in] u8IsTenDMsk 1: enable 10-Day digit alarm mask; 0: disabled.
632 * @param[in] u8IsDMsk 1: enable 1-Day digit alarm mask; 0: disabled.
633 *
634 * @return None
635 *
636 * @details This API is used to enable or disable RTC alarm date mask function.
637 */
RTC_SetAlarmDateMask(uint8_t u8IsTenYMsk,uint8_t u8IsYMsk,uint8_t u8IsTenMMsk,uint8_t u8IsMMsk,uint8_t u8IsTenDMsk,uint8_t u8IsDMsk)638 void RTC_SetAlarmDateMask(uint8_t u8IsTenYMsk, uint8_t u8IsYMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenDMsk, uint8_t u8IsDMsk)
639 {
640 RTC->CAMSK = ((uint32_t)u8IsTenYMsk << RTC_CAMSK_MTENYEAR_Pos) |
641 ((uint32_t)u8IsYMsk << RTC_CAMSK_MYEAR_Pos) |
642 ((uint32_t)u8IsTenMMsk << RTC_CAMSK_MTENMON_Pos) |
643 ((uint32_t)u8IsMMsk << RTC_CAMSK_MMON_Pos) |
644 ((uint32_t)u8IsTenDMsk << RTC_CAMSK_MTENDAY_Pos) |
645 ((uint32_t)u8IsDMsk << RTC_CAMSK_MDAY_Pos);
646 }
647
648 /**
649 * @brief Set RTC Alarm Time Mask Function
650 *
651 * @param[in] u8IsTenHMsk 1: enable 10-Hour digit alarm mask; 0: disabled.
652 * @param[in] u8IsHMsk 1: enable 1-Hour digit alarm mask; 0: disabled.
653 * @param[in] u8IsTenMMsk 1: enable 10-Min digit alarm mask; 0: disabled.
654 * @param[in] u8IsMMsk 1: enable 1-Min digit alarm mask; 0: disabled.
655 * @param[in] u8IsTenSMsk 1: enable 10-Sec digit alarm mask; 0: disabled.
656 * @param[in] u8IsSMsk 1: enable 1-Sec digit alarm mask; 0: disabled.
657 *
658 * @return None
659 *
660 * @details This API is used to enable or disable RTC alarm time mask function.
661 */
RTC_SetAlarmTimeMask(uint8_t u8IsTenHMsk,uint8_t u8IsHMsk,uint8_t u8IsTenMMsk,uint8_t u8IsMMsk,uint8_t u8IsTenSMsk,uint8_t u8IsSMsk)662 void RTC_SetAlarmTimeMask(uint8_t u8IsTenHMsk, uint8_t u8IsHMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenSMsk, uint8_t u8IsSMsk)
663 {
664 RTC->TAMSK = ((uint32_t)u8IsTenHMsk << RTC_TAMSK_MTENHR_Pos) |
665 ((uint32_t)u8IsHMsk << RTC_TAMSK_MHR_Pos) |
666 ((uint32_t)u8IsTenMMsk << RTC_TAMSK_MTENMIN_Pos) |
667 ((uint32_t)u8IsMMsk << RTC_TAMSK_MMIN_Pos) |
668 ((uint32_t)u8IsTenSMsk << RTC_TAMSK_MTENSEC_Pos) |
669 ((uint32_t)u8IsSMsk << RTC_TAMSK_MSEC_Pos);
670 }
671
672 /**
673 * @brief Get Day of the Week
674 *
675 * @param None
676 *
677 * @retval 0 Sunday
678 * @retval 1 Monday
679 * @retval 2 Tuesday
680 * @retval 3 Wednesday
681 * @retval 4 Thursday
682 * @retval 5 Friday
683 * @retval 6 Saturday
684 *
685 * @details This API is used to get day of the week of current RTC date.
686 */
RTC_GetDayOfWeek(void)687 uint32_t RTC_GetDayOfWeek(void)
688 {
689 return (RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk);
690 }
691
692 /**
693 * @brief Set RTC Tick Period Time
694 *
695 * @param[in] u32TickSelection It is used to set the RTC tick period time for Periodic Time Tick request. \n
696 * It consists of:
697 * - \ref RTC_TICK_1_SEC : Time tick is 1 second
698 * - \ref RTC_TICK_1_2_SEC : Time tick is 1/2 second
699 * - \ref RTC_TICK_1_4_SEC : Time tick is 1/4 second
700 * - \ref RTC_TICK_1_8_SEC : Time tick is 1/8 second
701 * - \ref RTC_TICK_1_16_SEC : Time tick is 1/16 second
702 * - \ref RTC_TICK_1_32_SEC : Time tick is 1/32 second
703 * - \ref RTC_TICK_1_64_SEC : Time tick is 1/64 second
704 * - \ref RTC_TICK_1_128_SEC : Time tick is 1/128 second
705 *
706 * @return None
707 *
708 * @details This API is used to set RTC tick period time for each tick interrupt.
709 */
RTC_SetTickPeriod(uint32_t u32TickSelection)710 void RTC_SetTickPeriod(uint32_t u32TickSelection)
711 {
712 RTC->TICK = (RTC->TICK & ~RTC_TICK_TICK_Msk) | u32TickSelection;
713 }
714
715 /**
716 * @brief Enable RTC Interrupt
717 *
718 * @param[in] u32IntFlagMask Specify the interrupt source. It consists of:
719 * - \ref RTC_INTEN_ALMIEN_Msk : Alarm interrupt
720 * - \ref RTC_INTEN_TICKIEN_Msk : Tick interrupt
721 * - \ref RTC_INTEN_TAMP0IEN_Msk : Tamper 0 Pin Event Detection interrupt
722 * - \ref RTC_INTEN_TAMP1IEN_Msk : Tamper 1 or Pair 0 Pin Event Detection interrupt
723 * - \ref RTC_INTEN_TAMP2IEN_Msk : Tamper 2 Pin Event Detection interrupt
724 * - \ref RTC_INTEN_TAMP3IEN_Msk : Tamper 3 or Pair 1 Pin Event Detection interrupt
725 * - \ref RTC_INTEN_TAMP4IEN_Msk : Tamper 4 Pin Event Detection interrupt
726 * - \ref RTC_INTEN_TAMP5IEN_Msk : Tamper 5 or Pair 2 Pin Event Detection interrupt
727 *
728 * @return None
729 *
730 * @details This API is used to enable the specify RTC interrupt function.
731 */
RTC_EnableInt(uint32_t u32IntFlagMask)732 void RTC_EnableInt(uint32_t u32IntFlagMask)
733 {
734 RTC->INTEN |= u32IntFlagMask;
735 }
736
737 /**
738 * @brief Disable RTC Interrupt
739 *
740 * @param[in] u32IntFlagMask Specify the interrupt source. It consists of:
741 * - \ref RTC_INTEN_ALMIEN_Msk : Alarm interrupt
742 * - \ref RTC_INTEN_TICKIEN_Msk : Tick interrupt
743 * - \ref RTC_INTEN_TAMP0IEN_Msk : Tamper 0 Pin Event Detection interrupt
744 * - \ref RTC_INTEN_TAMP1IEN_Msk : Tamper 1 or Pair 0 Pin Event Detection interrupt
745 * - \ref RTC_INTEN_TAMP2IEN_Msk : Tamper 2 Pin Event Detection interrupt
746 * - \ref RTC_INTEN_TAMP3IEN_Msk : Tamper 3 or Pair 1 Pin Event Detection interrupt
747 * - \ref RTC_INTEN_TAMP4IEN_Msk : Tamper 4 Pin Event Detection interrupt
748 * - \ref RTC_INTEN_TAMP5IEN_Msk : Tamper 5 or Pair 2 Pin Event Detection interrupt
749 *
750 * @return None
751 *
752 * @details This API is used to disable the specify RTC interrupt function.
753 */
RTC_DisableInt(uint32_t u32IntFlagMask)754 void RTC_DisableInt(uint32_t u32IntFlagMask)
755 {
756 RTC->INTEN &= ~u32IntFlagMask;
757 RTC->INTSTS = u32IntFlagMask;
758 }
759
760 /**
761 * @brief Enable Spare Registers Access
762 *
763 * @param None
764 *
765 * @return None
766 *
767 * @details This API is used to enable the spare registers 0~19 can be accessed.
768 */
RTC_EnableSpareAccess(void)769 void RTC_EnableSpareAccess(void)
770 {
771 RTC->SPRCTL |= RTC_SPRCTL_SPRRWEN_Msk;
772 }
773
774 /**
775 * @brief Disable Spare Register
776 *
777 * @param None
778 *
779 * @return None
780 *
781 * @details This API is used to disable the spare register 0~19 cannot be accessed.
782 */
RTC_DisableSpareRegister(void)783 void RTC_DisableSpareRegister(void)
784 {
785 RTC->SPRCTL &= ~RTC_SPRCTL_SPRRWEN_Msk;
786 }
787
788 /**
789 * @brief Static Tamper Detect
790 *
791 * @param[in] u32TamperSelect Tamper pin select. Possible options are
792 * - \ref RTC_TAMPER0_SELECT
793 * - \ref RTC_TAMPER1_SELECT
794 * - \ref RTC_TAMPER2_SELECT
795 * - \ref RTC_TAMPER3_SELECT
796 * - \ref RTC_TAMPER4_SELECT
797 * - \ref RTC_TAMPER5_SELECT
798 *
799 * @param[in] u32DetecLevel Tamper pin detection level select. Possible options are
800 * - \ref RTC_TAMPER_HIGH_LEVEL_DETECT
801 * - \ref RTC_TAMPER_LOW_LEVEL_DETECT
802 *
803 * @param[in] u32DebounceEn Tamper pin de-bounce enable
804 * - \ref RTC_TAMPER_DEBOUNCE_ENABLE
805 * - \ref RTC_TAMPER_DEBOUNCE_DISABLE
806 *
807 * @return None
808 *
809 * @details This API is used to enable the tamper pin detect function with specify trigger condition.
810 * User need disable dynamic tamper function before use this API.
811 */
RTC_StaticTamperEnable(uint32_t u32TamperSelect,uint32_t u32DetecLevel,uint32_t u32DebounceEn)812 void RTC_StaticTamperEnable(uint32_t u32TamperSelect, uint32_t u32DetecLevel, uint32_t u32DebounceEn)
813 {
814 uint32_t i;
815 uint32_t u32Reg;
816 uint32_t u32TmpReg;
817
818 u32Reg = RTC->TAMPCTL;
819
820 u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk | (u32DetecLevel << RTC_TAMPCTL_TAMP0LV_Pos) |
821 (u32DebounceEn << RTC_TAMPCTL_TAMP0DBEN_Pos));
822
823 for(i = 0UL; i < (uint32_t)RTC_MAX_TAMPER_PIN_NUM; i++)
824 {
825 if(u32TamperSelect & (0x1UL << i))
826 {
827 u32Reg &= ~((RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP0LV_Msk | RTC_TAMPCTL_TAMP0DBEN_Msk) << (i * 4UL));
828 u32Reg |= (u32TmpReg << (i * 4UL));
829 }
830 }
831
832 RTC->TAMPCTL = u32Reg;
833
834 }
835
836 /**
837 * @brief Static Tamper Disable
838 *
839 * @param[in] u32TamperSelect Tamper pin select. Possible options are
840 * - \ref RTC_TAMPER0_SELECT
841 * - \ref RTC_TAMPER1_SELECT
842 * - \ref RTC_TAMPER2_SELECT
843 * - \ref RTC_TAMPER3_SELECT
844 * - \ref RTC_TAMPER4_SELECT
845 * - \ref RTC_TAMPER5_SELECT
846 *
847 * @return None
848 *
849 * @details This API is used to disable the static tamper pin detect.
850 */
RTC_StaticTamperDisable(uint32_t u32TamperSelect)851 void RTC_StaticTamperDisable(uint32_t u32TamperSelect)
852 {
853 uint32_t i;
854 uint32_t u32Reg;
855 uint32_t u32TmpReg;
856
857 u32Reg = RTC->TAMPCTL;
858
859 u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk);
860
861 for(i = 0UL; i < (uint32_t)RTC_MAX_TAMPER_PIN_NUM; i++)
862 {
863 if(u32TamperSelect & (0x1UL << i))
864 {
865 u32Reg &= ~(u32TmpReg << (i * 4UL));
866 }
867 }
868
869 RTC->TAMPCTL = u32Reg;
870 }
871
872 /**
873 * @brief Dynamic Tamper Detect
874 *
875 * @param[in] u32PairSel Tamper pin detection enable. Possible options are
876 * - \ref RTC_PAIR0_SELECT
877 * - \ref RTC_PAIR1_SELECT
878 * - \ref RTC_PAIR2_SELECT
879 *
880 * @param[in] u32DebounceEn Tamper pin de-bounce enable
881 * - \ref RTC_TAMPER_DEBOUNCE_ENABLE
882 * - \ref RTC_TAMPER_DEBOUNCE_DISABLE
883 *
884 * @param[in] u32Pair1Source Dynamic Pair 1 Input Source Select
885 * 0: Pair 1 source select tamper 2
886 * 1: Pair 1 source select tamper 0
887 *
888 * @param[in] u32Pair2Source Dynamic Pair 2 Input Source Select
889 * 0: Pair 2 source select tamper 4
890 * 1: Pair 2 source select tamper 0
891 *
892 * @return None
893 *
894 * @details This API is used to enable the dynamic tamper.
895 */
RTC_DynamicTamperEnable(uint32_t u32PairSel,uint32_t u32DebounceEn,uint32_t u32Pair1Source,uint32_t u32Pair2Source)896 void RTC_DynamicTamperEnable(uint32_t u32PairSel, uint32_t u32DebounceEn, uint32_t u32Pair1Source, uint32_t u32Pair2Source)
897 {
898 uint32_t i;
899 uint32_t u32Reg;
900 uint32_t u32TmpReg;
901 uint32_t u32Tamper2Debounce, u32Tamper4Debounce;
902
903 u32Reg = RTC->TAMPCTL;
904 u32Reg &= ~(RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP1EN_Msk | RTC_TAMPCTL_TAMP2EN_Msk |
905 RTC_TAMPCTL_TAMP3EN_Msk | RTC_TAMPCTL_TAMP4EN_Msk | RTC_TAMPCTL_TAMP5EN_Msk);
906
907 u32Tamper2Debounce = u32Reg & RTC_TAMPCTL_TAMP2DBEN_Msk;
908 u32Tamper4Debounce = u32Reg & RTC_TAMPCTL_TAMP4DBEN_Msk;
909
910 u32Reg &= ~(RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP1EN_Msk | RTC_TAMPCTL_TAMP2EN_Msk |
911 RTC_TAMPCTL_TAMP3EN_Msk | RTC_TAMPCTL_TAMP4EN_Msk | RTC_TAMPCTL_TAMP5EN_Msk);
912 u32Reg &= ~(RTC_TAMPCTL_DYN1ISS_Msk | RTC_TAMPCTL_DYN2ISS_Msk);
913 u32Reg |= ((u32Pair1Source & 0x1UL) << RTC_TAMPCTL_DYN1ISS_Pos) | ((u32Pair2Source & 0x1UL) << RTC_TAMPCTL_DYN2ISS_Pos);
914
915 if(u32DebounceEn)
916 {
917 u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP1EN_Msk |
918 RTC_TAMPCTL_TAMP0DBEN_Msk | RTC_TAMPCTL_TAMP1DBEN_Msk | RTC_TAMPCTL_DYNPR0EN_Msk);
919 }
920 else
921 {
922 u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP1EN_Msk | RTC_TAMPCTL_DYNPR0EN_Msk);
923 }
924
925 for(i = 0UL; i < (uint32_t)RTC_MAX_PAIR_NUM; i++)
926 {
927 if(u32PairSel & (0x1UL << i))
928 {
929 u32Reg &= ~((RTC_TAMPCTL_TAMP0DBEN_Msk | RTC_TAMPCTL_TAMP1DBEN_Msk) << (i * 8UL));
930 u32Reg |= (u32TmpReg << (i * 8UL));
931 }
932 }
933
934 if((u32Pair1Source) && (u32PairSel & (uint32_t)RTC_PAIR1_SELECT))
935 {
936 u32Reg &= ~RTC_TAMPCTL_TAMP2EN_Msk;
937 u32Reg |= u32Tamper2Debounce;
938 }
939
940 if((u32Pair2Source) && (u32PairSel & (uint32_t)RTC_PAIR2_SELECT))
941 {
942 u32Reg &= ~RTC_TAMPCTL_TAMP4EN_Msk;
943 u32Reg |= u32Tamper4Debounce;
944 }
945
946 RTC->TAMPCTL = u32Reg;
947 }
948
949 /**
950 * @brief Dynamic Tamper Disable
951 *
952 * @param[in] u32PairSel Tamper pin detection enable. Possible options are
953 * - \ref RTC_PAIR0_SELECT
954 * - \ref RTC_PAIR1_SELECT
955 * - \ref RTC_PAIR2_SELECT
956 *
957 * @return None
958 *
959 * @details This API is used to disable the dynamic tamper.
960 */
RTC_DynamicTamperDisable(uint32_t u32PairSel)961 void RTC_DynamicTamperDisable(uint32_t u32PairSel)
962 {
963 uint32_t i;
964 uint32_t u32Reg;
965 uint32_t u32TmpReg;
966 uint32_t u32Tamper2En = 0UL, u32Tamper4En = 0UL;
967
968 u32Reg = RTC->TAMPCTL;
969
970 if((u32Reg & (uint32_t)RTC_TAMPCTL_DYN1ISS_Msk) && (u32PairSel & (uint32_t)RTC_PAIR1_SELECT))
971 {
972 u32Tamper2En = u32Reg & RTC_TAMPCTL_TAMP2EN_Msk;
973 }
974
975 if((u32Reg & (uint32_t)RTC_TAMPCTL_DYN2ISS_Msk) && (u32PairSel & (uint32_t)RTC_PAIR2_SELECT))
976 {
977 u32Tamper4En = u32Reg & RTC_TAMPCTL_TAMP4EN_Msk;
978 }
979
980 u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP1EN_Msk | RTC_TAMPCTL_DYNPR0EN_Msk);
981
982 for(i = 0UL; i < (uint32_t)RTC_MAX_PAIR_NUM; i++)
983 {
984 if(u32PairSel & (0x1UL << i))
985 {
986 u32Reg &= ~(u32TmpReg << ((i * 8UL)));
987 }
988 }
989
990 u32Reg |= (u32Tamper2En | u32Tamper4En);
991
992 RTC->TAMPCTL = u32Reg;
993 }
994
995 /**
996 * @brief Configure Dynamic Tamper
997 *
998 * @param[in] u32ChangeRate The dynamic tamper output change rate
999 * - \ref RTC_2POW10_CLK
1000 * - \ref RTC_2POW11_CLK
1001 * - \ref RTC_2POW12_CLK
1002 * - \ref RTC_2POW13_CLK
1003 * - \ref RTC_2POW14_CLK
1004 * - \ref RTC_2POW15_CLK
1005 * - \ref RTC_2POW16_CLK
1006 * - \ref RTC_2POW17_CLK
1007 *
1008 * @param[in] u32SeedReload Reload new seed or not
1009 * 0: not reload new seed
1010 * 1: reload new seed
1011 *
1012 * @param[in] u32RefPattern Reference pattern
1013 * - \ref RTC_REF_RANDOM_PATTERN
1014 * - \ref RTC_REF_SEED_VALUE
1015 *
1016 * @param[in] u32Seed Seed Value (0x0 ~ 0xFFFFFFFF)
1017 *
1018 * @return None
1019 *
1020 * @details This API is used to config dynamic tamper setting.
1021 */
RTC_DynamicTamperConfig(uint32_t u32ChangeRate,uint32_t u32SeedReload,uint32_t u32RefPattern,uint32_t u32Seed)1022 void RTC_DynamicTamperConfig(uint32_t u32ChangeRate, uint32_t u32SeedReload, uint32_t u32RefPattern, uint32_t u32Seed)
1023 {
1024 uint32_t u32Reg;
1025
1026 u32Reg = RTC->TAMPCTL;
1027
1028 u32Reg &= ~(RTC_TAMPCTL_DYNSRC_Msk | RTC_TAMPCTL_SEEDRLD_Msk | RTC_TAMPCTL_DYNRATE_Msk);
1029
1030 u32Reg |= (u32ChangeRate) | ((u32SeedReload & 0x1UL) << RTC_TAMPCTL_SEEDRLD_Pos) |
1031 (u32RefPattern << RTC_TAMPCTL_DYNSRC_Pos);
1032
1033 RTC->TAMPSEED = u32Seed; /* need set seed value before re-loade seed */
1034 RTC->TAMPCTL = u32Reg;
1035 }
1036
1037 /**
1038 * @brief Set RTC Clock Source
1039 *
1040 * @param[in] u32ClkSrc u32ClkSrc is the RTC clock source. It could be
1041 * - \ref RTC_CLOCK_SOURCE_LXT
1042 * - \ref RTC_CLOCK_SOURCE_LIRC
1043 *
1044 * @retval RTC_CLOCK_SOURCE_LXT
1045 * @retval RTC_CLOCK_SOURCE_LIRC
1046 *
1047 * @details This API is used to get the setting of RTC clock source.
1048 * User must to enable the selected clock source by themselves executing perform this API.
1049 */
RTC_SetClockSource(uint32_t u32ClkSrc)1050 uint32_t RTC_SetClockSource(uint32_t u32ClkSrc)
1051 {
1052 uint32_t u32TrimDefault = inpw(SYS_BASE + 0x14Cul);
1053
1054 if(u32ClkSrc == RTC_CLOCK_SOURCE_LXT)
1055 {
1056 /* RTC clock source is external LXT */
1057 RTC->LXTCTL &= ~RTC_LXTCTL_RTCCKSEL_Msk;
1058
1059 return RTC_CLOCK_SOURCE_LXT;
1060 }
1061 else if(u32ClkSrc == RTC_CLOCK_SOURCE_LIRC)
1062 {
1063 /* RTC clock source is LIRC */
1064 RTC->LXTCTL |= RTC_LXTCTL_RTCCKSEL_Msk;
1065
1066 return RTC_CLOCK_SOURCE_LIRC;
1067 }
1068 else
1069 {
1070 /* Load LIRC32 trim setting */
1071 RTC->LXTCTL = ((RTC->LXTCTL & ~(0x1FFul << 16)) | ((u32TrimDefault & 0x1FFul) << 16));
1072
1073 /* RTC clock source is LIRC32K */
1074 RTC->LXTCTL |= RTC_LXTCTL_LIRC32KEN_Msk;
1075 RTC->LXTCTL &= ~RTC_LXTCTL_RTCCKSEL_Msk;
1076 RTC->LXTCTL |= RTC_LXTCTL_C32KSEL_Msk;
1077
1078 return (RTC_CLOCK_SOURCE_LIRC + 1);
1079 }
1080 }
1081
1082 /**
1083 * @brief Set RTC GPIO Operation Mode
1084 *
1085 * @param[in] u32Pin The single pin of GPIO-F port.
1086 * It could be 4~11, which means PF.4~PF.11.
1087 * @param[in] u32Mode Operation mode. It could be
1088 * - \ref RTC_IO_MODE_INPUT
1089 * - \ref RTC_IO_MODE_OUTPUT
1090 * - \ref RTC_IO_MODE_OPEN_DRAIN
1091 * - \ref RTC_IO_MODE_QUASI
1092 * @param[in] u32DigitalCtl The digital input path control of specified pin. It could be
1093 * - \ref RTC_IO_DIGITAL_ENABLE
1094 * - \ref RTC_IO_DIGITAL_DISABLE
1095 * @param[in] u32PullCtl The pull-up or pull-down control of specified pin. It could be
1096 * - \ref RTC_IO_PULL_UP_DOWN_DISABLE
1097 * - \ref RTC_IO_PULL_UP_ENABLE
1098 * - \ref RTC_IO_PULL_DOWN_ENABLE
1099 * @param[in] u32OutputLevel The I/O output level. 0: output low; 1: output high.
1100 *
1101 * @return None
1102 *
1103 * @details This function is used to set specified GPIO operation mode controlled by RTC module.
1104 */
RTC_SetGPIOMode(uint32_t u32PFPin,uint32_t u32Mode,uint32_t u32DigitalCtl,uint32_t u32PullCtl,uint32_t u32OutputLevel)1105 void RTC_SetGPIOMode(uint32_t u32PFPin, uint32_t u32Mode, uint32_t u32DigitalCtl, uint32_t u32PullCtl, uint32_t u32OutputLevel)
1106 {
1107 uint32_t u32Offset;
1108
1109 if((u32PFPin == 4) || (u32PFPin == 5) || (u32PFPin == 6) || (u32PFPin == 7))
1110 {
1111 u32Offset = u32PFPin - 4;
1112
1113 RTC_SET_IOCTL_BY_RTC();
1114
1115 RTC->GPIOCTL0 = (RTC->GPIOCTL0 & ~(0x3FUL << (u32Offset * 8))) |
1116 (u32Mode << (u32Offset * 8)) |
1117 (u32OutputLevel << ((u32Offset * 8) + 2)) |
1118 (u32DigitalCtl << ((u32Offset * 8) + 3)) |
1119 (u32PullCtl << ((u32Offset * 8) + 4));
1120 }
1121
1122 if((u32PFPin == 8) || (u32PFPin == 9) || (u32PFPin == 10) || (u32PFPin == 11))
1123 {
1124 u32Offset = u32PFPin - 8;
1125
1126 RTC_SET_IOCTL_BY_RTC();
1127
1128 RTC->GPIOCTL1 = (RTC->GPIOCTL1 & ~(0x3FUL << (u32Offset * 8))) |
1129 (u32Mode << (u32Offset * 8)) |
1130 (u32OutputLevel << ((u32Offset * 8) + 2)) |
1131 (u32DigitalCtl << ((u32Offset * 8) + 3)) |
1132 (u32PullCtl << ((u32Offset * 8) + 4));
1133 }
1134 }
1135
1136 /**
1137 * @brief Set RTC GPIO Output Level
1138 *
1139 * @param[in] u32Pin The single pin of GPIO-F port.
1140 * It could be 4~11, which means PF.4~PF.11.
1141 * @param[in] u32OutputLevel The I/O output level. 0: output low; 1: output high.
1142 *
1143 * @return None
1144 *
1145 * @details This function is used to set GPIO output level by RTC module.
1146 */
RTC_SetGPIOLevel(uint32_t u32PFPin,uint32_t u32OutputLevel)1147 void RTC_SetGPIOLevel(uint32_t u32PFPin, uint32_t u32OutputLevel)
1148 {
1149 uint32_t u32Offset;
1150
1151 if((u32PFPin == 4) || (u32PFPin == 5) || (u32PFPin == 6) || (u32PFPin == 7))
1152 {
1153 u32Offset = u32PFPin - 4;
1154
1155 RTC->GPIOCTL0 = (RTC->GPIOCTL0 & ~(0x4UL << (u32Offset * 8))) |
1156 (u32OutputLevel << ((u32Offset * 8) + 2));
1157 }
1158
1159 if((u32PFPin == 8) || (u32PFPin == 9) || (u32PFPin == 10) || (u32PFPin == 11))
1160 {
1161 u32Offset = u32PFPin - 8;
1162
1163 RTC->GPIOCTL1 = (RTC->GPIOCTL1 & ~(0x4UL << (u32Offset * 8))) |
1164 (u32OutputLevel << ((u32Offset * 8) + 2));
1165 }
1166 }
1167
1168 /**@}*/ /* end of group RTC_EXPORTED_FUNCTIONS */
1169
1170 /**@}*/ /* end of group RTC_Driver */
1171
1172 /**@}*/ /* end of group Standard_Driver */
1173