before merging master
[inav.git] / lib / main / AT32F43x / Drivers / AT32F43x_StdPeriph_Driver / src / at32f435_437_ertc.c
blobd09528481ddb78a59ea0cc268d38312d8f4c1e02
1 /**
2 **************************************************************************
3 * @file at32f435_437_ertc.c
4 * @version v2.1.0
5 * @date 2022-08-16
6 * @brief contains all the functions for the ertc firmware library
7 **************************************************************************
8 * Copyright notice & Disclaimer
10 * The software Board Support Package (BSP) that is made available to
11 * download from Artery official website is the copyrighted work of Artery.
12 * Artery authorizes customers to use, copy, and distribute the BSP
13 * software and its related documentation for the purpose of design and
14 * development in conjunction with Artery microcontrollers. Use of the
15 * software is governed by this copyright notice and the following disclaimer.
17 * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
18 * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
19 * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
20 * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
21 * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 **************************************************************************
27 #include "at32f435_437_conf.h"
29 /** @addtogroup AT32F435_437_periph_driver
30 * @{
33 /** @defgroup ERTC
34 * @brief ERTC driver modules
35 * @{
38 #ifdef ERTC_MODULE_ENABLED
40 /** @defgroup ERTC_private_functions
41 * @{
44 #define ERTC_TIMEOUT ((uint32_t) 0x00360000)
46 /**
47 * @brief number conversion to bcd code.
48 * @param num: number(0~99)
49 * @retval bcd code.
51 uint8_t ertc_num_to_bcd(uint8_t num)
53 uint8_t bcd_h = 0, bcd_l = 0;
55 bcd_h = num / 10;
56 bcd_l = num % 10;
58 return ((uint8_t)(bcd_h << 4) | bcd_l);
61 /**
62 * @brief bcd code conversion to number.
63 * @param bcd: bcd code(0~99).
64 * @retval number.
66 uint8_t ertc_bcd_to_num(uint8_t bcd)
68 return ((((uint8_t)(bcd & (uint8_t)0xF0) >> 4) * 10) + (bcd & (uint8_t)0x0F));
71 /**
72 * @brief enable write protection.
73 * @param none.
74 * @retval none
76 void ertc_write_protect_enable(void)
78 ERTC->wp = 0xFF;
81 /**
82 * @brief disable write protection.
83 * @param none.
84 * @retval none
86 void ertc_write_protect_disable(void)
88 ERTC->wp = 0xCA;
89 ERTC->wp = 0x53;
92 /**
93 * @brief ertc wait register update finish.
94 * @param none.
95 * @retval error_status (ERROR or SUCCESS).
97 error_status ertc_wait_update(void)
99 uint32_t timeout = ERTC_TIMEOUT * 2;
101 /* disable write protection */
102 ertc_write_protect_disable();
104 /* clear updf flag */
105 ERTC->sts = ~(ERTC_UPDF_FLAG | 0x00000080) | (ERTC->sts_bit.imen << 7);
107 /* enable write protection */
108 ertc_write_protect_enable();
110 while(ERTC->sts_bit.updf == 0)
112 if(timeout == 0)
114 return ERROR;
117 timeout--;
120 return SUCCESS;
124 * @brief ertc wait flag status.
125 * @param flag: flag to wait.
126 * this parameter can be one of the following values:
127 * - ERTC_ALAWF_FLAG: alarm a register allows write flag.
128 * - ERTC_ALBWF_FLAG: alarm b register allows write flag.
129 * - ERTC_WATWF_FLAG: wakeup timer register allows write flag.
130 * - ERTC_TADJF_FLAG: time adjustment flag.
131 * - ERTC_CALUPDF_FLAG: calibration value update completed flag.
132 * @param status: status to wait.
133 * this parameter can be one of the following values:
134 * - SET.
135 * - RESET.
136 * @retval error_status (ERROR or SUCCESS).
138 error_status ertc_wait_flag(uint32_t flag, flag_status status)
140 uint32_t timeout = ERTC_TIMEOUT;
142 while(ertc_flag_get(flag) == status)
144 if(timeout == 0)
146 /* enable write protection */
147 ertc_write_protect_enable();
149 return ERROR;
152 timeout--;
155 return SUCCESS;
159 * @brief ertc enter init mode.
160 * @param none.
161 * @retval error_status (ERROR or SUCCESS).
163 error_status ertc_init_mode_enter(void)
165 uint32_t timeout = ERTC_TIMEOUT * 2;
167 /* disable write protection */
168 ertc_write_protect_disable();
170 if(ERTC->sts_bit.imf == 0)
172 /* enter init mode */
173 ERTC->sts = 0xFFFFFFFF;
175 while(ERTC->sts_bit.imf == 0)
177 if(timeout == 0)
179 /* enable write protection */
180 ertc_write_protect_enable();
182 return ERROR;
185 timeout--;
189 return SUCCESS;
193 * @brief ertc exit init mode.
194 * @param none.
195 * @retval none.
197 void ertc_init_mode_exit(void)
199 ERTC->sts = 0xFFFFFF7F;
203 * @brief ertc reset all register.
204 * @param none.
205 * @retval error_status (ERROR or SUCCESS).
207 error_status ertc_reset(void)
209 /* disable write protection */
210 ertc_write_protect_disable();
212 ERTC->ctrl = (uint32_t)0x00000000;
214 /* enter init mode */
215 if(ertc_init_mode_enter() != SUCCESS)
217 return ERROR;
220 /* reset register */
221 ERTC->time = (uint32_t)0x00000000;
222 ERTC->date = (uint32_t)0x00002101;
223 ERTC->ctrl = (uint32_t)0x00000000;
224 ERTC->div = (uint32_t)0x007F00FF;
225 ERTC->wat = (uint32_t)0x0000FFFF;
226 ERTC->ccal = (uint32_t)0x00000000;
227 ERTC->ala = (uint32_t)0x00000000;
228 ERTC->alb = (uint32_t)0x00000000;
229 ERTC->tadj = (uint32_t)0x00000000;
230 ERTC->scal = (uint32_t)0x00000000;
231 ERTC->tamp = (uint32_t)0x00000000;
232 ERTC->alasbs = (uint32_t)0x00000000;
233 ERTC->albsbs = (uint32_t)0x00000000;
234 ERTC->sts = (uint32_t)0x00000000;
236 /* wait calendar update */
237 ertc_wait_update();
239 /* enable write protection */
240 ertc_write_protect_enable();
242 return SUCCESS;
246 * @brief ertc division set.
247 * @param div_a: division a (0~0x7F).
248 * @param div_b: division b (0~0x7FFF).
249 * @retval error_status (ERROR or SUCCESS).
251 error_status ertc_divider_set(uint16_t div_a, uint16_t div_b)
253 /* disable write protection */
254 ertc_write_protect_disable();
256 /* enter init mode */
257 if(ertc_init_mode_enter() != SUCCESS)
259 return ERROR;
262 /* config the ertc divider */
263 ERTC->div_bit.diva = div_a;
264 ERTC->div_bit.divb = div_b;
266 /* exit init mode */
267 ertc_init_mode_exit();
269 /* enable write protection */
270 ertc_write_protect_enable();
272 return SUCCESS;
276 * @brief ertc hour mode set.
277 * @param mode: hour mode.
278 * this parameter can be one of the following values:
279 * - ERTC_HOUR_MODE_24: 24-hour format.
280 * - ERTC_HOUR_MODE_12: 12-hour format.
281 * @retval error_status (ERROR or SUCCESS).
283 error_status ertc_hour_mode_set(ertc_hour_mode_set_type mode)
285 /* disable write protection */
286 ertc_write_protect_disable();
288 /* enter init mode */
289 if(ertc_init_mode_enter() != SUCCESS)
291 return ERROR;
294 /* write register */
295 ERTC->ctrl_bit.hm = mode;
297 /* exit init mode */
298 ertc_init_mode_exit();
300 /* enable write protection */
301 ertc_write_protect_enable();
303 return SUCCESS;
307 * @brief set date.
308 * @param year: year (0~99).
309 * @param month: month (1~12).
310 * @param date: date (1~31).
311 * @param week: week (1~7).
312 * @retval error_status (ERROR or SUCCESS).
314 error_status ertc_date_set(uint8_t year, uint8_t month, uint8_t date, uint8_t week)
316 ertc_reg_date_type reg;
318 reg.date = 0;
320 reg.date_bit.y = ertc_num_to_bcd(year);
321 reg.date_bit.m = ertc_num_to_bcd(month);
322 reg.date_bit.d = ertc_num_to_bcd(date);
323 reg.date_bit.wk = week;
325 /* disable write protection */
326 ertc_write_protect_disable();
328 /* enter init mode */
329 if(ertc_init_mode_enter() != SUCCESS)
331 return ERROR;
334 /* Set the ertc_DR register */
335 ERTC->date = reg.date;
337 /* exit init mode */
338 ertc_init_mode_exit();
340 if(ERTC->ctrl_bit.dren == 0)
342 ertc_wait_update();
345 /* enable write protection */
346 ertc_write_protect_enable();
348 return SUCCESS;
352 * @brief set time.
353 * @param hour: hour (0~23).
354 * @param min: minute (0~59).
355 * @param sec: second (0~59).
356 * @param ampm: hour mode.
357 * this parameter can be one of the following values:
358 * - ERTC_24H: 24-hour format.
359 * - ERTC_AM: 12-hour format, ante meridiem.
360 * - ERTC_PM: 12-hour format, post meridiem.
361 * @retval error_status (ERROR or SUCCESS).
363 error_status ertc_time_set(uint8_t hour, uint8_t min, uint8_t sec, ertc_am_pm_type ampm)
365 ertc_reg_time_type reg;
367 reg.time = 0;
369 reg.time_bit.h = ertc_num_to_bcd(hour);
370 reg.time_bit.m = ertc_num_to_bcd(min);
371 reg.time_bit.s = ertc_num_to_bcd(sec);
372 reg.time_bit.ampm = ampm;
374 /* disable write protection */
375 ertc_write_protect_disable();
377 /* enter init mode */
378 if(ertc_init_mode_enter() != SUCCESS)
380 return ERROR;
383 ERTC->time = reg.time;
385 /* exit init mode */
386 ertc_init_mode_exit();
388 if(ERTC->ctrl_bit.dren == 0)
390 ertc_wait_update();
393 /* enable write protection */
394 ertc_write_protect_enable();
396 return SUCCESS;
400 * @brief get calendar
401 * @param time: ertc time.
402 * @retval none.
404 void ertc_calendar_get(ertc_time_type* time)
406 ertc_reg_time_type reg_tm;
407 ertc_reg_date_type reg_dt;
409 UNUSED(ERTC->sts);
411 reg_tm.time = ERTC->time;
412 reg_dt.date = ERTC->date;
414 time->hour = ertc_bcd_to_num(reg_tm.time_bit.h);
415 time->min = ertc_bcd_to_num(reg_tm.time_bit.m);
416 time->sec = ertc_bcd_to_num(reg_tm.time_bit.s);
417 time->ampm = (ertc_am_pm_type)reg_tm.time_bit.ampm;
419 time->year = ertc_bcd_to_num(reg_dt.date_bit.y);
420 time->month = ertc_bcd_to_num(reg_dt.date_bit.m);
421 time->day = ertc_bcd_to_num(reg_dt.date_bit.d);
422 time->week = reg_dt.date_bit.wk;
426 * @brief get current sub second.
427 * @param none.
428 * @retval sub second.
430 uint32_t ertc_sub_second_get(void)
432 uint32_t reg = 0;
434 reg = ERTC->sbs;
436 (void) (ERTC->date);
438 return (reg);
442 * @brief set which bits are irrelevant to the alarm match.
443 * @param alarm_x: select the alarm.
444 * this parameter can be one of the following values:
445 * - ERTC_ALA: alarm a.
446 * - ERTC_ALB: alarm b.
447 * @param mask: select which bits are irrelevant to the alarm match.
448 * this parameter can be one of the following values:
449 * - ERTC_ALARM_MASK_NONE: match all.
450 * - ERTC_ALARM_MASK_SEC: don't match seconds.
451 * - ERTC_ALARM_MASK_MIN: don't match minute.
452 * - ERTC_ALARM_MASK_HOUR: don't match hour.
453 * - ERTC_ALARM_MASK_DATE_WEEK: don't match date or week.
454 * - ERTC_ALARM_MASK_ALL: don't match all.
455 * @param alarm: alarm para.
456 * @retval none.
458 void ertc_alarm_mask_set(ertc_alarm_type alarm_x, uint32_t mask)
460 uint32_t reg;
462 /* disable write protection */
463 ertc_write_protect_disable();
465 if(alarm_x == ERTC_ALA)
467 reg = ERTC->ala;
469 reg &= ~ERTC_ALARM_MASK_ALL;
470 reg |= mask;
472 ERTC->ala= reg;
474 else
476 reg = ERTC->alb;
478 reg &= ~ERTC_ALARM_MASK_ALL;
479 reg |= mask;
481 ERTC->alb= reg;
484 /* enable write protection */
485 ertc_write_protect_enable();
489 * @brief alarm week or date mode select.
490 * @param alarm_x: select the alarm.
491 * this parameter can be one of the following values:
492 * - ERTC_ALA: alarm a.
493 * - ERTC_ALB: alarm b.
494 * @param wk: week or date mode select.
495 * this parameter can be one of the following values:
496 * - ERTC_SLECT_DATE: slect date mode.
497 * - ERTC_SLECT_WEEK: slect week mode.
498 * @retval none.
500 void ertc_alarm_week_date_select(ertc_alarm_type alarm_x, ertc_week_date_select_type wk)
502 /* disable write protection */
503 ertc_write_protect_disable();
505 if(alarm_x == ERTC_ALA)
507 ERTC->ala_bit.wksel = wk;
509 else
511 ERTC->alb_bit.wksel = wk;
514 /* enable write protection */
515 ertc_write_protect_enable();
519 * @brief set alarm.
520 * @param alarm_x: select the alarm.
521 * this parameter can be one of the following values:
522 * - ERTC_ALA: alarm a.
523 * - ERTC_ALB: alarm b.
524 * @param week_day: week or date.
525 * - week: 1~7.
526 * - date: 1~31.
527 * @param hour: hour (0~23).
528 * @param min: minute (0~59).
529 * @param sec: second (0~59).
530 * @param ampm: hour mode.
531 * this parameter can be one of the following values:
532 * - ERTC_24H: 24-hour format.
533 * - ERTC_AM: 12-hour format, ante meridiem.
534 * - ERTC_PM: 12-hour format, post meridiem.
535 * @param alarm: alarm para.
536 * @retval none.
538 void ertc_alarm_set(ertc_alarm_type alarm_x, uint8_t week_date, uint8_t hour, uint8_t min, uint8_t sec, ertc_am_pm_type ampm)
540 ertc_reg_alarm_type reg;
542 if(alarm_x == ERTC_ALA)
544 reg.ala = ERTC->ala;
546 else
548 reg.ala = ERTC->alb;
551 reg.ala_bit.d = ertc_num_to_bcd(week_date);
552 reg.ala_bit.h = ertc_num_to_bcd(hour);
553 reg.ala_bit.m = ertc_num_to_bcd(min);
554 reg.ala_bit.s = ertc_num_to_bcd(sec);
555 reg.ala_bit.ampm = ampm;
557 /* disable write protection */
558 ertc_write_protect_disable();
560 if(alarm_x == ERTC_ALA)
562 ERTC->ala= reg.ala;
564 else
566 ERTC->alb = reg.ala;
569 /* enable write protection */
570 ertc_write_protect_enable();
574 * @brief set alarm sub second.
575 * @param alarm_x: select the alarm.
576 * this parameter can be one of the following values:
577 * - ERTC_ALA: alarm a.
578 * - ERTC_ALB: alarm b.
579 * @param value: sub second value.
580 * @param mask: sub second mask.
581 * this parameter can be one of the following values:
582 * - ERTC_ALARM_SBS_MASK_ALL: do not match the sub-second.
583 * - ERTC_ALARM_SBS_MASK_14_1: only compare bit [0].
584 * - ERTC_ALARM_SBS_MASK_14_2: only compare bit [1:0].
585 * - ERTC_ALARM_SBS_MASK_14_3: only compare bit [2:0].
586 * - ERTC_ALARM_SBS_MASK_14_4: only compare bit [3:0].
587 * - ERTC_ALARM_SBS_MASK_14_5: only compare bit [4:0].
588 * - ERTC_ALARM_SBS_MASK_14_6: only compare bit [5:0].
589 * - ERTC_ALARM_SBS_MASK_14_7: only compare bit [6:0].
590 * - ERTC_ALARM_SBS_MASK_14_8: only compare bit [7:0].
591 * - ERTC_ALARM_SBS_MASK_14_9: only compare bit [8:0].
592 * - ERTC_ALARM_SBS_MASK_14_10: only compare bit [9:0].
593 * - ERTC_ALARM_SBS_MASK_14_11: only compare bit [10:0].
594 * - ERTC_ALARM_SBS_MASK_14_12: only compare bit [11:0].
595 * - ERTC_ALARM_SBS_MASK_14_13: only compare bit [12:0].
596 * - ERTC_ALARM_SBS_MASK_14: only compare bit [13:0].
597 * - ERTC_ALARM_SBS_MASK_NONE: compare bit [14:0].
598 * @retval none.
600 void ertc_alarm_sub_second_set(ertc_alarm_type alarm_x, uint32_t value, ertc_alarm_sbs_mask_type mask)
602 /* disable write protection */
603 ertc_write_protect_disable();
605 if(alarm_x == ERTC_ALA)
607 ERTC->alasbs_bit.sbsmsk = mask;
608 ERTC->alasbs_bit.sbs = value;
610 else
612 ERTC->albsbs_bit.sbsmsk = mask;
613 ERTC->albsbs_bit.sbs = value;
616 /* enable write protection */
617 ertc_write_protect_enable();
621 * @brief enable or disable alarm clock.
622 * @param alarm_x: select the alarm.
623 * this parameter can be one of the following values:
624 * - ERTC_ALA: alarm a.
625 * - ERTC_ALB: alarm b.
626 * @param new_state (TRUE or FALSE).
627 * @retval error_status (ERROR or SUCCESS).
629 error_status ertc_alarm_enable(ertc_alarm_type alarm_x, confirm_state new_state)
631 /* disable write protection */
632 ertc_write_protect_disable();
634 if(alarm_x == ERTC_ALA)
636 ERTC->ctrl_bit.alaen = new_state;
638 if(new_state == FALSE)
640 if(ertc_wait_flag(ERTC_ALAWF_FLAG, RESET) != SUCCESS)
642 return ERROR;
646 else
648 ERTC->ctrl_bit.alben = new_state;
650 if(new_state == FALSE)
652 if(ertc_wait_flag(ERTC_ALBWF_FLAG, RESET) != SUCCESS)
654 return ERROR;
659 /* enable write protection */
660 ertc_write_protect_enable();
662 return SUCCESS;
666 * @brief get alarm value.
667 * @param alarm_x: select the alarm.
668 * this parameter can be one of the following values:
669 * - ERTC_ALA: alarm a.
670 * - ERTC_ALB: alarm b.
671 * @param alarm: alarm para.
672 * @retval none.
674 void ertc_alarm_get(ertc_alarm_type alarm_x, ertc_alarm_value_type* alarm)
676 ertc_reg_alarm_type reg;
678 reg.ala = 0;
680 if(alarm_x == ERTC_ALA)
682 reg.ala = ERTC->ala;
684 else
686 reg.ala = ERTC->alb;
689 alarm->day = ertc_bcd_to_num(reg.ala_bit.d);
690 alarm->week = ertc_bcd_to_num(reg.ala_bit.d);
691 alarm->hour = ertc_bcd_to_num(reg.ala_bit.h);
692 alarm->min = ertc_bcd_to_num(reg.ala_bit.m);
693 alarm->sec = ertc_bcd_to_num(reg.ala_bit.s);
694 alarm->ampm = (ertc_am_pm_type)reg.ala_bit.ampm;
695 alarm->week_date_sel = reg.ala_bit.wksel;
696 alarm->mask = reg.ala & ERTC_ALARM_MASK_ALL;
700 * @brief get alarm sub second.
701 * @param alarm_x: select the alarm.
702 * this parameter can be one of the following values:
703 * - ERTC_ALA: alarm a.
704 * - ERTC_ALB: alarm b.
705 * @retval sub second.
707 uint32_t ertc_alarm_sub_second_get(ertc_alarm_type alarm_x)
709 if(alarm_x == ERTC_ALA)
711 return (ERTC->alasbs_bit.sbs);
713 else
715 return (ERTC->albsbs_bit.sbs);
720 * @brief set wakeup timer clock.
721 * @param clock: wakeup timer clock source.
722 * this parameter can be one of the following values:
723 * - ERTC_WAT_CLK_ERTCCLK_DIV16: ERTC_CLK / 16.
724 * - ERTC_WAT_CLK_ERTCCLK_DIV8: ERTC_CLK / 8.
725 * - ERTC_WAT_CLK_ERTCCLK_DIV4: ERTC_CLK / 4.
726 * - ERTC_WAT_CLK_ERTCCLK_DIV2: ERTC_CLK / 2.
727 * - ERTC_WAT_CLK_CK_B_16BITS: CK_B, wakeup counter = ERTC_WAT
728 * - ERTC_WAT_CLK_CK_B_17BITS: CK_B, wakeup counter = ERTC_WAT + 65535.
729 * @retval none.
731 void ertc_wakeup_clock_set(ertc_wakeup_clock_type clock)
733 /* disable write protection */
734 ertc_write_protect_disable();
736 ERTC->ctrl_bit.watclk = clock;
738 /* enable write protection */
739 ertc_write_protect_enable();
743 * @brief set wakeup counter.
744 * @param counter: wakeup counter(0~65535).
745 * @retval none.
747 void ertc_wakeup_counter_set(uint32_t counter)
749 /* disable write protection */
750 ertc_write_protect_disable();
752 ERTC->wat_bit.val = counter;
754 /* enable write protection */
755 ertc_write_protect_enable();
759 * @brief get wakeup counter.
760 * @param none.
761 * @retval wakeup counter.
763 uint16_t ertc_wakeup_counter_get(void)
765 return ERTC->wat_bit.val;
769 * @brief enable or disable wakeup timer.
770 * @param new_state (TRUE or FALSE).
771 * @retval error_status (ERROR or SUCCESS).
773 error_status ertc_wakeup_enable(confirm_state new_state)
775 /* disable write protection */
776 ertc_write_protect_disable();
778 ERTC->ctrl_bit.waten = new_state;
780 if(new_state == FALSE)
782 if(ertc_wait_flag(ERTC_WATWF_FLAG, RESET) != SUCCESS)
784 return ERROR;
788 /* enable write protection */
789 ertc_write_protect_enable();
791 return SUCCESS;
795 * @brief config the smooth calibration.
796 * @param period: calibration period.
797 * this parameter can be one of the following values:
798 * - ERTC_SMOOTH_CAL_PERIOD_32: 32 second calibration period.
799 * - ERTC_SMOOTH_CAL_PERIOD_16: 16 second calibration period.
800 * - ERTC_SMOOTH_CAL_PERIOD_8: 8 second calibration period.
801 * @param clk_add: add clock.
802 * this parameter can be one of the following values:
803 * - ERTC_SMOOTH_CAL_CLK_ADD_0: do not increase clock.
804 * - ERTC_SMOOTH_CAL_CLK_ADD_512: add 512 clocks.
805 * @param clk_dec: decrease clock(0~511).
806 * @retval error_status (ERROR or SUCCESS).
808 error_status ertc_smooth_calibration_config(ertc_smooth_cal_period_type period, ertc_smooth_cal_clk_add_type clk_add, uint32_t clk_dec)
810 ertc_reg_scal_type reg;
812 /* disable write protection */
813 ertc_write_protect_disable();
815 if(ertc_wait_flag(ERTC_CALUPDF_FLAG, SET) != SUCCESS)
817 return ERROR;
820 reg.scal = 0;
822 switch (period)
824 case ERTC_SMOOTH_CAL_PERIOD_32:
825 break;
826 case ERTC_SMOOTH_CAL_PERIOD_16:
827 reg.scal_bit.cal16 = 1;
828 break;
829 case ERTC_SMOOTH_CAL_PERIOD_8:
830 reg.scal_bit.cal8 = 1;
831 break;
832 default:
833 break;
836 reg.scal_bit.add = clk_add;
837 reg.scal_bit.dec = clk_dec;
839 ERTC->scal = reg.scal;
841 /* enable write protection */
842 ertc_write_protect_enable();
844 return SUCCESS;
848 * @brief set the coarse digital calibration.
849 * @param dir: calibration direction.
850 * this parameter can be one of the following values:
851 * - ERTC_CAL_DIR_POSITIVE: positive calibration.
852 * - ERTC_CAL_DIR_NEGATIVE: negative calibration.
853 * @param value: calibration value(0~31).
854 * @retval error_status (ERROR or SUCCESS).
856 error_status ertc_coarse_calibration_set(ertc_cal_direction_type dir, uint32_t value)
858 /* disable write protection */
859 ertc_write_protect_disable();
861 /* enter init mode */
862 if(ertc_init_mode_enter() == ERROR)
864 return ERROR;
867 ERTC->ccal_bit.caldir = dir;
869 ERTC->ccal_bit.calval = value;
871 /* exit init mode */
872 ertc_init_mode_exit();
874 /* enable write protection */
875 ertc_write_protect_enable();
877 return SUCCESS;
881 * @brief enable or disable coarse calibration.
882 * @param new_state (TRUE or FALSE).
883 * @retval error_status (ERROR or SUCCESS).
885 error_status ertc_coarse_calibration_enable(confirm_state new_state)
887 /* disable write protection */
888 ertc_write_protect_disable();
890 /* enter init mode */
891 if(ertc_init_mode_enter() == ERROR)
893 return ERROR;
896 ERTC->ctrl_bit.ccalen = new_state;
898 /* exit init mode */
899 ertc_init_mode_exit();
901 /* enable write protection */
902 ertc_write_protect_enable();
904 return SUCCESS;
908 * @brief calibration output source select.
909 * @param output: output source.
910 * this parameter can be one of the following values:
911 * - ERTC_CAL_OUTPUT_512HZ: output 512 hz.
912 * - ERTC_CAL_OUTPUT_1HZ: output 1 hz.
913 * @retval none.
915 void ertc_cal_output_select(ertc_cal_output_select_type output)
917 /* disable write protection */
918 ertc_write_protect_disable();
920 ERTC->ctrl_bit.calosel = output;
922 /* enable write protection */
923 ertc_write_protect_enable();
927 * @brief enable or disable calibration output.
928 * @param new_state (TRUE or FALSE).
929 * @retval none.
931 void ertc_cal_output_enable(confirm_state new_state)
933 /* disable write protection */
934 ertc_write_protect_disable();
936 ERTC->ctrl_bit.caloen = new_state;
938 /* enable write protection */
939 ertc_write_protect_enable();
943 * @brief adjust the time.
944 * @param add1s: second operation.
945 * this parameter can be one of the following values:
946 * - ERTC_TIME_ADD_NONE: none operation.
947 * - ERTC_TIME_ADD_1S: add 1 second.
948 * @param decsbs: decrease sub second(0~0x7FFF).
949 * @retval error_status (ERROR or SUCCESS).
951 error_status ertc_time_adjust(ertc_time_adjust_type add1s, uint32_t decsbs)
953 ertc_reg_tadj_type reg;
955 reg.tadj = 0;
957 /* disable write protection */
958 ertc_write_protect_disable();
960 if(ertc_wait_flag(ERTC_TADJF_FLAG, SET) != SUCCESS)
962 return ERROR;
965 /* check if the reference clock detection is disabled */
966 if(ERTC->ctrl_bit.rcden == 0)
968 reg.tadj_bit.add1s = add1s;
969 reg.tadj_bit.decsbs = decsbs;
971 ERTC->tadj = reg.tadj;
973 if(ertc_wait_update() == ERROR)
975 return ERROR;
978 else
980 return ERROR;
983 /* enable write protection */
984 ertc_write_protect_enable();
986 return SUCCESS;
990 * @brief config the daylight saving time.
991 * @param operation: time adjust.
992 * this parameter can be one of the following values:
993 * - ERTC_DST_ADD_1H: add 1 hour.
994 * - ERTC_DST_DEC_1H: dec 1 hour.
995 * @param save: operation save.
996 * this parameter can be one of the following values:
997 * - ERTC_DST_SAVE_0: set the bpr register value to 0.
998 * - ERTC_DST_SAVE_1: set the bpr register value to 1.
999 * @retval none.
1001 void ertc_daylight_set(ertc_dst_operation_type operation, ertc_dst_save_type save)
1003 /* disable write protection */
1004 ertc_write_protect_disable();
1006 if(operation == ERTC_DST_ADD_1H)
1008 ERTC->ctrl_bit.add1h = 1;
1010 else
1012 ERTC->ctrl_bit.dec1h = 1;
1015 ERTC->ctrl_bit.bpr = save;
1017 /* enable write protection */
1018 ertc_write_protect_enable();
1022 * @brief get the bpr value.
1023 * @param none.
1024 * @retval bpr value.
1026 uint8_t ertc_daylight_bpr_get(void)
1028 return ERTC->ctrl_bit.bpr;
1032 * @brief enable or disable refer clock detect.
1033 * @param new_state (TRUE or FALSE).
1034 * @retval error_status (ERROR or SUCCESS).
1036 error_status ertc_refer_clock_detect_enable(confirm_state new_state)
1038 /* disable write protection */
1039 ertc_write_protect_disable();
1041 /* enter init mode */
1042 if(ertc_init_mode_enter() != SUCCESS)
1044 return ERROR;
1047 /* write register */
1048 ERTC->ctrl_bit.rcden = new_state;
1050 /* exit init mode */
1051 ertc_init_mode_exit();
1053 /* enable write protection */
1054 ertc_write_protect_enable();
1056 return SUCCESS;
1060 * @brief enable or disable direct read mode.
1061 * @param new_state (TRUE or FALSE).
1062 * @retval none.
1064 void ertc_direct_read_enable(confirm_state new_state)
1066 /* disable write protection */
1067 ertc_write_protect_disable();
1069 ERTC->ctrl_bit.dren = new_state;
1071 /* enable write protection */
1072 ertc_write_protect_enable();
1076 * @brief set the output mode.
1077 * @param source: output source.
1078 * this parameter can be one of the following values:
1079 * - ERTC_OUTPUT_DISABLE: diable output.
1080 * - ERTC_OUTPUT_ALARM_A: output alarm a event.
1081 * - ERTC_OUTPUT_ALARM_B: output alarm b event.
1082 * - ERTC_OUTPUT_WAKEUP: output wakeup event.
1083 * @param polarity: output polarity.
1084 * this parameter can be one of the following values:
1085 * - ERTC_OUTPUT_POLARITY_HIGH: when the event occurs, the output is high.
1086 * - ERTC_OUTPUT_POLARITY_LOW: when the event occurs, the output is low.
1087 * @param type: output type.
1088 * this parameter can be one of the following values:
1089 * - ERTC_OUTPUT_TYPE_OPEN_DRAIN: open drain output.
1090 * - ERTC_OUTPUT_TYPE_PUSH_PULL: push pull output.
1091 * @retval none.
1093 void ertc_output_set(ertc_output_source_type source, ertc_output_polarity_type polarity, ertc_output_type type)
1095 /* disable write protection */
1096 ertc_write_protect_disable();
1098 ERTC->ctrl_bit.outp = polarity;
1100 ERTC->tamp_bit.outtype = type;
1102 ERTC->ctrl_bit.outsel = source;
1104 /* enable write protection */
1105 ertc_write_protect_enable();
1109 * @brief timestamp detection pin selection.
1110 * @param pin: data register
1111 * this parameter can be one of the following values:
1112 * - ERTC_PIN_PC13: pc13 is used as timestamp detection pin.
1113 * - ERTC_PIN_PA0: pa0 is used as timestamp detection pin.
1114 * @retval data value.
1116 void ertc_timestamp_pin_select(ertc_pin_select_type pin)
1118 /* disable write protection */
1119 ertc_write_protect_disable();
1121 ERTC->tamp_bit.tspin = pin;
1123 /* enable write protection */
1124 ertc_write_protect_enable();
1128 * @brief set the timestamp valid edge.
1129 * @param edge: calibration period.
1130 * this parameter can be one of the following values:
1131 * - ERTC_TIMESTAMP_EDGE_RISING : rising edge trigger.
1132 * - ERTC_TIMESTAMP_EDGE_FALLING: falling edge trigger.
1133 * @retval none.
1135 void ertc_timestamp_valid_edge_set(ertc_timestamp_valid_edge_type edge)
1137 /* disable write protection */
1138 ertc_write_protect_disable();
1140 ERTC->ctrl_bit.tsedg = edge;
1142 /* enable write protection */
1143 ertc_write_protect_enable();
1147 * @brief enable or disable timestamp.
1148 * @param new_state (TRUE or FALSE).
1149 * @retval none.
1151 void ertc_timestamp_enable(confirm_state new_state)
1153 /* disable write protection */
1154 ertc_write_protect_disable();
1156 ERTC->ctrl_bit.tsen = new_state;
1158 /* enable write protection */
1159 ertc_write_protect_enable();
1163 * @brief get the timestamp.
1164 * @param time: time.
1165 * @param date: date.
1166 * @retval none.
1168 void ertc_timestamp_get(ertc_time_type* time)
1170 ertc_reg_tstm_type tmtime;
1171 ertc_reg_tsdt_type tmdate;
1173 tmtime.tstm = ERTC->tstm;
1174 tmdate.tsdt = ERTC->tsdt;
1176 time->year = 0;
1177 time->month = ertc_bcd_to_num(tmdate.tsdt_bit.m);
1178 time->day = ertc_bcd_to_num(tmdate.tsdt_bit.d);
1179 time->week = ertc_bcd_to_num(tmdate.tsdt_bit.wk);
1180 time->hour = ertc_bcd_to_num(tmtime.tstm_bit.h);
1181 time->min = ertc_bcd_to_num(tmtime.tstm_bit.m);
1182 time->sec = ertc_bcd_to_num(tmtime.tstm_bit.s);
1183 time->ampm = (ertc_am_pm_type)tmtime.tstm_bit.ampm;
1187 * @brief get the timestamp sub second.
1188 * @param none.
1189 * @retval timestamp sub second.
1191 uint32_t ertc_timestamp_sub_second_get(void)
1193 return ERTC->tssbs_bit.sbs;
1197 * @brief tamper 1 detection pin selection.
1198 * @param pin: data register
1199 * this parameter can be one of the following values:
1200 * - ERTC_PIN_PC13: pc13 is used as tamper 1 detection pin.
1201 * - ERTC_PIN_PA0: pa0 is used as tamper 1 detection pin.
1202 * @retval data value.
1204 void ertc_tamper_1_pin_select(ertc_pin_select_type pin)
1206 /* disable write protection */
1207 ertc_write_protect_disable();
1209 ERTC->tamp_bit.tp1pin = pin;
1211 /* enable write protection */
1212 ertc_write_protect_enable();
1216 * @brief enable or disable tamper pin pull up.
1217 * @param new_state (TRUE or FALSE).
1218 * @retval none.
1220 void ertc_tamper_pull_up_enable(confirm_state new_state)
1222 /* disable write protection */
1223 ertc_write_protect_disable();
1225 ERTC->tamp_bit.tppu = !new_state;
1227 /* enable write protection */
1228 ertc_write_protect_enable();
1232 * @brief set the tamper pin pre-charge time.
1233 * @param precharge: tamper detection pre-charge time
1234 * this parameter can be one of the following values:
1235 * - ERTC_TAMPER_PR_1_ERTCCLK: pre-charge time is 1 ERTC_CLK.
1236 * - ERTC_TAMPER_PR_2_ERTCCLK: pre-charge time is 2 ERTC_CLK.
1237 * - ERTC_TAMPER_PR_4_ERTCCLK: pre-charge time is 4 ERTC_CLK.
1238 * - ERTC_TAMPER_PR_8_ERTCCLK: pre-charge time is 8 ERTC_CLK.
1239 * @retval none.
1241 void ertc_tamper_precharge_set(ertc_tamper_precharge_type precharge)
1243 /* disable write protection */
1244 ertc_write_protect_disable();
1246 ERTC->tamp_bit.tppr = precharge;
1248 /* enable write protection */
1249 ertc_write_protect_enable();
1253 * @brief set the tamper filter time.
1254 * @param filter: tamper filter.
1255 * this parameter can be one of the following values:
1256 * - ERTC_TAMPER_FILTER_DISABLE: disable filter function.
1257 * - ERTC_TAMPER_FILTER_2: 2 consecutive samples arw valid, effective tamper event.
1258 * - ERTC_TAMPER_FILTER_4: 4 consecutive samples arw valid, effective tamper event.
1259 * - ERTC_TAMPER_FILTER_8: 8 consecutive samples arw valid, effective tamper event.
1260 * @retval none.
1262 void ertc_tamper_filter_set(ertc_tamper_filter_type filter)
1264 /* disable write protection */
1265 ertc_write_protect_disable();
1267 ERTC->tamp_bit.tpflt = filter;
1269 /* enable write protection */
1270 ertc_write_protect_enable();
1274 * @brief set the tamper detection frequency.
1275 * @param freq: tamper detection frequency.
1276 * this parameter can be one of the following values:
1277 * - ERTC_TAMPER_FREQ_DIV_32768: ERTC_CLK / 32768.
1278 * - ERTC_TAMPER_FREQ_DIV_16384: ERTC_CLK / 16384.
1279 * - ERTC_TAMPER_FREQ_DIV_8192: ERTC_CLK / 8192.
1280 * - ERTC_TAMPER_FREQ_DIV_4096: ERTC_CLK / 4096.
1281 * - ERTC_TAMPER_FREQ_DIV_2048: ERTC_CLK / 2048.
1282 * - ERTC_TAMPER_FREQ_DIV_1024: ERTC_CLK / 1024.
1283 * - ERTC_TAMPER_FREQ_DIV_512: ERTC_CLK / 512.
1284 * - ERTC_TAMPER_FREQ_DIV_256: ERTC_CLK / 256.
1285 * @retval none.
1287 void ertc_tamper_detect_freq_set(ertc_tamper_detect_freq_type freq)
1289 /* disable write protection */
1290 ertc_write_protect_disable();
1292 ERTC->tamp_bit.tpfreq = freq;
1294 /* enable write protection */
1295 ertc_write_protect_enable();
1299 * @brief set the tamper trigger.
1300 * @param tamper_x: select the tamper.
1301 * this parameter can be one of the following values:
1302 * - ERTC_TAMPER_1: tamper 1.
1303 * - ERTC_TAMPER_2: tamper 2.
1304 * @param trigger: tamper valid edge.
1305 * this parameter can be one of the following values:
1306 * - ERTC_TAMPER_EDGE_RISING: rising gedge.
1307 * - ERTC_TAMPER_EDGE_FALLING: falling gedge.
1308 * - ERTC_TAMPER_EDGE_LOW: low level.
1309 * - ERTC_TAMPER_EDGE_HIGH: high level.
1310 * @param alarm: alarm para.
1311 * @retval none.
1313 void ertc_tamper_valid_edge_set(ertc_tamper_select_type tamper_x, ertc_tamper_valid_edge_type trigger)
1315 /* disable write protection */
1316 ertc_write_protect_disable();
1318 if(tamper_x == ERTC_TAMPER_1)
1320 ERTC->tamp_bit.tp1edg = trigger;
1322 else
1324 ERTC->tamp_bit.tp2edg = trigger;
1327 /* enable write protection */
1328 ertc_write_protect_enable();
1332 * @brief enable or disable trigger timestamp when tamper event occurs.
1333 * @param new_state (TRUE or FALSE).
1334 * @retval none.
1336 void ertc_tamper_timestamp_enable(confirm_state new_state)
1338 /* disable write protection */
1339 ertc_write_protect_disable();
1341 ERTC->tamp_bit.tptsen = new_state;
1343 /* enable write protection */
1344 ertc_write_protect_enable();
1348 * @brief enable or disable tamper.
1349 * @param tamper_x: select the tamper.
1350 * this parameter can be one of the following values:
1351 * - ERTC_TAMPER_1: tamper 1.
1352 * - ERTC_TAMPER_2: tamper 2.
1353 * @param new_state (TRUE or FALSE).
1354 * @retval none.
1356 void ertc_tamper_enable(ertc_tamper_select_type tamper_x, confirm_state new_state)
1358 /* disable write protection */
1359 ertc_write_protect_disable();
1361 if(tamper_x == ERTC_TAMPER_1)
1363 ERTC->tamp_bit.tp1en = new_state;
1365 else
1367 ERTC->tamp_bit.tp2en = new_state;
1370 /* enable write protection */
1371 ertc_write_protect_enable();
1375 * @brief enable or disable interrupt.
1376 * @param source: interrupts sources
1377 * this parameter can be any combination of the following values:
1378 * - ERTC_TP_INT: tamper interrupt.
1379 * - ERTC_ALA_INT: alarm a interrupt.
1380 * - ERTC_ALB_INT: alarm b interrupt.
1381 * - ERTC_WAT_INT: wakeup timer interrupt.
1382 * - ERTC_TS_INT: timestamp interrupt.
1383 * @param new_state (TRUE or FALSE).
1384 * @retval none.
1386 void ertc_interrupt_enable(uint32_t source, confirm_state new_state)
1388 /* disable write protection */
1389 ertc_write_protect_disable();
1391 if(source & ERTC_TP_INT)
1393 if(new_state != FALSE)
1395 ERTC->tamp |= ERTC_TP_INT;
1397 else
1399 ERTC->tamp &= ~ERTC_TP_INT;
1402 source &= ~ERTC_TP_INT;
1405 if(new_state != FALSE)
1407 ERTC->ctrl |= source;
1409 else
1411 ERTC->ctrl &= ~source;
1414 /* enable write protection */
1415 ertc_write_protect_enable();
1419 * @brief get interrupt status
1420 * @param source
1421 * this parameter can be one of the following values:
1422 * - ERTC_TP_INT: tamper interrupt.
1423 * - ERTC_ALA_INT: alarm a interrupt.
1424 * - ERTC_ALB_INT: alarm b interrupt.
1425 * - ERTC_WAT_INT: wakeup timer interrupt.
1426 * - ERTC_TS_INT: timestamp interrupt.
1427 * @retval flag_status (SET or RESET)
1429 flag_status ertc_interrupt_get(uint32_t source)
1431 if(source & ERTC_TP_INT)
1433 if((ERTC->tamp & ERTC_TP_INT) != RESET)
1435 return SET;
1437 else
1439 return RESET;
1443 if((ERTC->ctrl & source) != RESET)
1445 return SET;
1447 else
1449 return RESET;
1454 * @brief get flag status.
1455 * @param flag: specifies the flag to check.
1456 * this parameter can be one of the following values:
1457 * - ERTC_ALAWF_FLAG: alarm a register allows write flag.
1458 * - ERTC_ALBWF_FLAG: alarm b register allows write flag.
1459 * - ERTC_WATWF_FLAG: wakeup timer register allows write flag.
1460 * - ERTC_TADJF_FLAG: time adjustment flag.
1461 * - ERTC_INITF_FLAG: calendar initialization flag.
1462 * - ERTC_UPDF_FLAG: calendar update flag.
1463 * - ERTC_IMF_FLAG: enter initialization mode flag.
1464 * - ERTC_ALAF_FLAG: alarm clock a flag.
1465 * - ERTC_ALBF_FLAG: alarm clock b flag.
1466 * - ERTC_WATF_FLAG: wakeup timer flag.
1467 * - ERTC_TSF_FLAG: timestamp flag.
1468 * - ERTC_TSOF_FLAG: timestamp overflow flag.
1469 * - ERTC_TP1F_FLAG: tamper detection 1 flag.
1470 * - ERTC_TP2F_FLAG: tamper detection 2 flag.
1471 * - ERTC_CALUPDF_FLAG: calibration value update completed flag.
1472 * @retval the new state of flag (SET or RESET).
1474 flag_status ertc_flag_get(uint32_t flag)
1476 if((ERTC->sts & flag) != (uint32_t)RESET)
1478 return SET;
1480 else
1482 return RESET;
1487 * @brief clear flag status
1488 * @param flag: specifies the flag to clear.
1489 * this parameter can be any combination of the following values:
1490 * - ERTC_ALAWF_FLAG: alarm a register allows write flag.
1491 * - ERTC_ALBWF_FLAG: alarm b register allows write flag.
1492 * - ERTC_WATWF_FLAG: wakeup timer register allows write flag.
1493 * - ERTC_TADJF_FLAG: time adjustment flag.
1494 * - ERTC_INITF_FLAG: calendar initialization flag.
1495 * - ERTC_UPDF_FLAG: calendar update flag.
1496 * - ERTC_IMF_FLAG: enter initialization mode flag.
1497 * - ERTC_ALAF_FLAG: alarm clock a flag.
1498 * - ERTC_ALBF_FLAG: alarm clock b flag.
1499 * - ERTC_WATF_FLAG: wakeup timer flag.
1500 * - ERTC_TSF_FLAG: timestamp flag.
1501 * - ERTC_TSOF_FLAG: timestamp overflow flag.
1502 * - ERTC_TP1F_FLAG: tamper detection 1 flag.
1503 * - ERTC_TP2F_FLAG: tamper detection 2 flag.
1504 * - ERTC_CALUPDF_FLAG: calibration value update completed flag.
1505 * @retval none
1507 void ertc_flag_clear(uint32_t flag)
1509 ERTC->sts = ~(flag | 0x00000080) | (ERTC->sts_bit.imen << 7);
1513 * @brief write data to the bpr register.
1514 * @param dt: data register
1515 * this parameter can be one of the following values:
1516 * - ERTC_DT1
1517 * - ERTC_DT2
1518 * - ...
1519 * - ERTC_DT19
1520 * - ERTC_DT20
1521 * @param data: data to be write.
1522 * @retval none.
1524 void ertc_bpr_data_write(ertc_dt_type dt, uint32_t data)
1526 __IO uint32_t reg = 0;
1528 reg = ERTC_BASE + 0x50 + (dt * 4);
1530 /* disable write protection */
1531 ertc_write_protect_disable();
1533 *(__IO uint32_t *)reg = data;
1535 /* enable write protection */
1536 ertc_write_protect_enable();
1540 * @brief read data from bpr register.
1541 * @param dt: data register
1542 * this parameter can be one of the following values:
1543 * - ERTC_DT1
1544 * - ERTC_DT2
1545 * - ...
1546 * - ERTC_DT19
1547 * - ERTC_DT20
1548 * @retval data value.
1550 uint32_t ertc_bpr_data_read(ertc_dt_type dt)
1552 __IO uint32_t reg = 0;
1554 reg = ERTC_BASE + 0x50 + (dt * 4);
1556 return (*(__IO uint32_t *)reg);
1560 * @}
1563 #endif
1566 * @}
1570 * @}