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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
|
/**
******************************************************************************
* @file ald_rtc.c
* @brief RTC module driver.
* This file provides firmware functions to manage the following
* functionalities of the RTC peripheral:
* + Initialization functions
* + Time and date functions
* + Alarm functions
* + Time stamp functions
* + Tamper functions
* + Wake-up functions
* + Clock output functions
* + Peripheral Control functions
* @version V1.0
* @date 25 Apr 2017
* @author AE Team
*
* Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
********************************************************************************
* @verbatim
==============================================================================
##### How to use this driver #####
==============================================================================
[..]
(+) Enable the RTC controller interface clock.
(+) Select the RTC source clock(default LOSC).
(+) Configure the RTC asynchronous prescaler, synchronous prescaler and hour
format using the ald_rtc_init() function.
*** Time and date operation ***
=================================
[..]
(+) To configure the time use the ald_rtc_set_time() function.
(+) To configure the date use the ald_rtc_set_date() function.
(+) To read the time use the ald_rtc_get_time() function.
(+) To read the date use the ald_rtc_get_date() function.
*** Alarm operation ***
===================================
[..]
(+) To configure the alarm use ald_rtc_set_alarm() function
(+) To read the alarm use ald_rtc_get_alarm() function
(+) To cancel the alarm use ald_rtc_alarm_cmd() function
*** Time stamp operation ***
===================================
[..]
(+) To configure the time stamp use ald_rtc_set_time_stamp() function
(+) To read the time stamp use ald_rtc_get_time_stamp() function
(+) To cancel the time stamp use ald_rtc_cancel_time_stamp() function
*** Tamper operation ***
===================================
[..]
(+) To configure the tamper use ald_rtc_set_tamper() function
(+) To cancel the tamper use ald_rtc_alarm_cmd() function
*** Wake-up operation ***
===================================
[..]
(+) To configure the wake-up parameters use ald_rtc_set_wakeup() function
(+) To read the re-load register value use ald_rtc_get_wakeup_timer_value() function
(+) To cancel the wake-up use ald_rtc_cancel_wakeup() function
*** Output clock operation ***
===================================
[..]
(+) To configure the clock output type use ald_rtc_set_clock_output() function
(+) To cancel the clock output use ald_rtc_cancel_clock_output() function
*** Control functions ***
===================================
[..]
(+) Configure interrupt enable/disable.
(+) Enable/disable alarm.
(+) Configure rtc shift.
(+) Calibrate time.
(+) Get interrupt source status.
(+) Get interrupt flag status.
(+) Clear interrupt flag.
==================================================================
##### RTC and low power modes #####
==================================================================
[..] The MCU can be woken up from a low power mode by an RTC alternate function.
[..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
RTC wake-up, RTC tamper event detection and RTC time stamp event detection.
These RTC alternate functions can wake up the system from the Stop and
Standby low power modes.
[..] The system can also wake up from low power modes without depending
on an external interrupt (Auto-wake-up mode), by using the RTC alarm
or the RTC wake-up events.
[..] The RTC provides a programmable time base for waking up from the Stop or
Standby mode at regular intervals. Wake-up from STOP and STANDBY modes
is possible only when the RTC clock source is LSE or LSI.
*** RTC driver macros list ***
=============================================
[..]
Below the list of most used macros in RTC driver.
(+) RTC_UNLOCK() Disable the protect.
(+) RTC_LOCK() Enable the protect.
(+) RTC_BY_PASS_ENABLE() Enable the by-pass shadow register.
(+) RTC_BY_PASS_DISABLE() Disable the by-pass shadow register.
(+) RTC_SUMMER_TIME_ENABLE() Enable summer time.
(+) RTC_SUMMER_TIME_DISABLE() Disable summer time.
(+) RTC_WINTER_TIME_ENABLE() Enable winter time.
(+) RTC_WINTER_TIME_DISABLE() Disable winter time.
[..]
(@) You can refer to the RTC driver header file for used the macros
@endverbatim
******************************************************************************
*/
#include "ald_rtc.h"
#include "ald_bkpc.h"
#include "ald_tsense.h"
#include "ald_syscfg.h"
/** @addtogroup ES32FXXX_ALD
* @{
*/
/** @defgroup RTC RTC
* @brief RTC module driver
* @{
*/
#ifdef ALD_RTC
/** @addtogroup RTC_Private_Functions RTC Private Functions
* @{
*/
/**
* @brief Converts form 2 digit BCD to Binary.
* @param bcd: BCD value to be converted.
* @retval Converted word.
*/
static uint32_t bcd_to_dec(uint32_t bcd)
{
return ((bcd & 0xF) + ((bcd >> 4) & 0xF) * 10);
}
/**
* @brief Converts a 2 digit decimal to BCD format.
* @param dec: Byte to be converted.
* @retval Converted byte.
*/
static uint32_t dec_to_bcd(uint32_t dec)
{
return (((dec / 10) << 4) | (dec % 10));
}
/**
* @brief Time and Date consistency check.
* @param t_last: Last time.
* @param d_last: Last date.
* @param time: Current time.
* @param date: Current time.
* @retval status:
* 0 - Not consistency
* 1 - Consistency
*/
static int32_t rtc_consistency_check(rtc_time_t *t_last,
rtc_date_t *d_last, rtc_time_t *time, rtc_date_t *date)
{
if (t_last->second != time->second)
return 0;
if (t_last->minute != time->minute)
return 0;
if (t_last->hour != time->hour)
return 0;
if (d_last->day != date->day)
return 0;
if (d_last->month != date->month)
return 0;
if (d_last->year != date->year)
return 0;
return 1;
}
/**
* @}
*/
/** @defgroup RTC_Public_Functions RTC Public Functions
* @{
*/
/** @defgroup RTC_Public_Functions_Group1 Initialization functions
* @brief Initialization functions
*
* @verbatim
===============================================================================
##### Initialization function #####
===============================================================================
[..] This section provides functions allowing to initialize and configure the
RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
RTC registers Write protection.
(#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
It is split into 2 programmable prescalers to minimize power consumption.
(++) A 7-bit asynchronous prescaler and a 13-bit synchronous prescaler.
(++) When both prescalers are used, it is recommended to configure the
asynchronous prescaler to a high value to minimize power consumption.
(#) All RTC registers are Write protected. Writing to the RTC registers
is enabled by writing a key into the Write Protection register.
@endverbatim
* @{
*/
/**
* @brief Reset RTC register.
* @retval None
*/
void ald_rtc_reset(void)
{
RTC_UNLOCK();
WRITE_REG(RTC->CON, 0x0);
WRITE_REG(RTC->TAMPCON, 0x0);
WRITE_REG(RTC->WUMAT, 0x0);
WRITE_REG(RTC->IER, 0x0);
WRITE_REG(RTC->IFCR, ~0x0);
RTC_LOCK();
return;
}
/**
* @brief Initialize the RTC module.
* @param init: Pointer to rtc_init_t structure which contains
* the configuration parameters.
* @retval None
*/
void ald_rtc_init(rtc_init_t *init)
{
assert_param(IS_RTC_HOUR_FORMAT(init->hour_format));
assert_param(IS_RTC_OUTPUT_SEL(init->output));
assert_param(IS_RTC_OUTPUT_POLARITY(init->output_polarity));
ald_rtc_reset();
RTC_UNLOCK();
MODIFY_REG(RTC->CON, RTC_CON_HFM_MSK, init->hour_format << RTC_CON_HFM_POS);
MODIFY_REG(RTC->CON, RTC_CON_EOS_MSK, init->output << RTC_CON_EOS_POSS);
MODIFY_REG(RTC->CON, RTC_CON_POL_MSK, init->output_polarity << RTC_CON_POL_POS);
MODIFY_REG(RTC->PSR, RTC_PSR_SPRS_MSK, init->synch_pre_div << RTC_PSR_SPRS_POSS);
MODIFY_REG(RTC->PSR, RTC_PSR_APRS_MSK, init->asynch_pre_div << RTC_PSR_APRS_POSS);
SET_BIT(RTC->CON, RTC_CON_GO_MSK);
RTC_LOCK();
return;
}
/**
* @brief Configure the RTC source.
* @param sel: RTC source type.
* @retval None
*/
void ald_rtc_source_select(rtc_source_sel_t sel)
{
assert_param(IS_RTC_SOURCE_SEL(sel));
BKPC_UNLOCK();
MODIFY_REG(BKPC->PCCR, BKPC_PCCR_RTCCS_MSK, sel << BKPC_PCCR_RTCCS_POSS);
if (sel == RTC_SOURCE_LOSC) {
SET_BIT(BKPC->CR, BKPC_CR_LOSCEN_MSK);
}
else if (sel == RTC_SOURCE_LRC) {
SET_BIT(BKPC->CR, BKPC_CR_LRCEN_MSK);
}
else {
; /* do nothing */
}
BKPC_LOCK();
return;
}
/**
* @}
*/
/** @defgroup RTC_Public_Functions_Group2 Time and Date functions
* @brief RTC Time and Date functions
*
* @verbatim
===============================================================================
##### Time and Date functions #####
===============================================================================
[..] This section provides functions allowing:
[#]
(+) To configure the time use the ald_rtc_set_time() function.
(+) To configure the date use the ald_rtc_set_date() function.
(+) To read the time use the ald_rtc_get_time() function.
(+) To read the date use the ald_rtc_get_date() function.
@endverbatim
* @{
*/
/**
* @brief Set specified time.
* @param time: pointer to a rtc_time_t structure.
* @param format: Data format.
* @retval ALD status.
*/
ald_status_t ald_rtc_set_time(rtc_time_t *time, rtc_format_t format)
{
uint32_t tmp;
assert_param(IS_RTC_FORMAT(format));
if (format == RTC_FORMAT_DEC) {
assert_param(IS_RTC_SECOND(time->second));
assert_param(IS_RTC_MINUTE(time->minute));
assert_param(IS_RTC_HOUR(time->hour));
tmp = (dec_to_bcd(time->second)) |
(dec_to_bcd(time->minute) << 8) |
(dec_to_bcd(time->hour) << 16);
}
else {
assert_param(IS_RTC_SECOND(bcd_to_dec(time->second)));
assert_param(IS_RTC_MINUTE(bcd_to_dec(time->minute)));
assert_param(IS_RTC_HOUR(bcd_to_dec(time->hour)));
tmp = time->second | (time->minute << 8) | (time->hour << 16);
}
RTC_UNLOCK();
WRITE_REG(RTC->TIME, tmp);
RTC_LOCK();
tmp = ald_get_tick();
while (READ_BIT(RTC->CON, RTC_CON_BUSY_MSK)) {
if ((ald_get_tick() - tmp) > RTC_TIMEOUT_VALUE)
return TIMEOUT;
}
return OK;
}
/**
* @brief Set specified date.
* @param date: pointer to a rtc_date_t structure.
* @param format: Data format.
* @retval ALD status.
*/
ald_status_t ald_rtc_set_date(rtc_date_t *date, rtc_format_t format)
{
uint32_t tmp;
assert_param(IS_RTC_FORMAT(format));
if (format == RTC_FORMAT_DEC) {
assert_param(IS_RTC_DAY(date->day));
assert_param(IS_RTC_MONTH(date->month));
assert_param(IS_RTC_YEAR(date->year));
tmp = (dec_to_bcd(date->day)) |
(dec_to_bcd(date->month) << 8) |
(dec_to_bcd(date->year) << 16) |
(dec_to_bcd(date->week) << 24);
}
else {
assert_param(IS_RTC_DAY(bcd_to_dec(date->day)));
assert_param(IS_RTC_MONTH(bcd_to_dec(date->month)));
assert_param(IS_RTC_YEAR(bcd_to_dec(date->year)));
tmp = date->day | (date->month << 8) |
(date->year << 16) | (date->week << 24);
}
RTC_UNLOCK();
WRITE_REG(RTC->DATE, tmp);
RTC_LOCK();
tmp = ald_get_tick();
while (READ_BIT(RTC->CON, RTC_CON_BUSY_MSK)) {
if ((ald_get_tick() - tmp) > RTC_TIMEOUT_VALUE)
return TIMEOUT;
}
return OK;
}
/**
* @brief Get current time.
* @param time: pointer to a rtc_time_t structure.
* @param format: Data format.
* @retval None
*/
void ald_rtc_get_time(rtc_time_t *time, rtc_format_t format)
{
uint32_t tmp;
assert_param(time != NULL);
assert_param(IS_RTC_FORMAT(format));
time->sub_sec = RTC->SSEC & 0xFFFF;
tmp = RTC->TIME;
if (format == RTC_FORMAT_DEC) {
time->second = bcd_to_dec(tmp & 0x7F);
time->minute = bcd_to_dec((tmp >> 8) & 0x7F);
time->hour = bcd_to_dec((tmp >> 16) & 0x7F);
}
else {
time->second = tmp & 0x7F;
time->minute = (tmp >> 8) & 0x7F;
time->hour = (tmp >> 16) & 0x7F;
}
return;
}
/**
* @brief Get current date.
* @param date: pointer to a rtc_date_t structure.
* @param format: Data format.
* @retval None
*/
void ald_rtc_get_date(rtc_date_t *date, rtc_format_t format)
{
uint32_t tmp = RTC->DATE;
assert_param(date != NULL);
assert_param(IS_RTC_FORMAT(format));
if (format == RTC_FORMAT_DEC) {
date->day = bcd_to_dec(tmp & 0x3F);
date->month = bcd_to_dec((tmp >> 8) & 0x1F);
date->year = bcd_to_dec((tmp >> 16) & 0xFF);
date->week = bcd_to_dec((tmp >> 24) & 0x7);
}
else {
date->day = tmp & 0x3F;
date->month = (tmp >> 8) & 0x1F;
date->year = (tmp >> 16) & 0xFF;
date->week = (tmp >> 24) & 0x7;
}
return;
}
/**
* @brief Get time and date consistency.
* @param date: pointer to a rtc_date_t structure.
* @param time: pointer to a rtc_time_t structure.
* @param format: Data format.
* @retval Status:
* 0 - Consistency
* -1 - Not consistency
*/
int32_t ald_rtc_get_date_time(rtc_date_t *date, rtc_time_t *time, rtc_format_t format)
{
int32_t nr = 3;
rtc_date_t d_last;
rtc_time_t t_last;
while (nr--) {
ald_rtc_get_time(&t_last, format);
ald_rtc_get_date(&d_last, format);
ald_rtc_get_time(time, format);
ald_rtc_get_date(date, format);
if (rtc_consistency_check(&t_last, &d_last, time, date))
return 0;
}
return -1;
}
/**
* @}
*/
/** @defgroup RTC_Public_Functions_Group3 Alarm functions
* @brief RTC alarm functions
*
* @verbatim
===============================================================================
##### Alarm functions #####
===============================================================================
[..] This section provides functions allowing:
[#]
(+) To configure the alarm use ald_rtc_set_alarm() function
(+) To read the alarm use ald_rtc_get_alarm() function
@endverbatim
* @{
*/
/**
* @brief Set alarm.
* @param alarm: pointer to rtc_alarm_t struct.
* @param format: Data format.
* @retval None
*/
void ald_rtc_set_alarm(rtc_alarm_t *alarm, rtc_format_t format)
{
unsigned int tmp, ss_tmp;
assert_param(IS_RTC_ALARM(alarm->idx));
assert_param(IS_RTC_ALARM_SEL(alarm->sel));
assert_param(IS_RTC_ALARM_SS_MASK(alarm->ss_mask));
assert_param(IS_RTC_FORMAT(format));
if (format == RTC_FORMAT_DEC) {
assert_param(IS_RTC_SECOND(alarm->time.second));
assert_param(IS_RTC_MINUTE(alarm->time.minute));
assert_param(IS_RTC_HOUR(alarm->time.hour));
tmp = (dec_to_bcd(alarm->time.second)) |
(dec_to_bcd(alarm->time.minute) << 8) |
(dec_to_bcd(alarm->time.hour) << 16) |
alarm->mask;
if (alarm->sel == RTC_SELECT_DAY) {
assert_param(IS_RTC_DAY(alarm->day));
tmp |= (dec_to_bcd(alarm->day) << 24);
tmp &= 0x7FFFFFFF; /* Reset bit31 */
}
else {
tmp |= (1 << (alarm->week + 24));
tmp |= 0x80000000; /* Set bit31 */
}
}
else {
assert_param(IS_RTC_SECOND(bcd_to_dec(alarm->time.second)));
assert_param(IS_RTC_MINUTE(bcd_to_dec(alarm->time.minute)));
assert_param(IS_RTC_HOUR(bcd_to_dec(alarm->time.hour)));
tmp = alarm->time.second |
(alarm->time.minute << 8) |
(alarm->time.hour << 16) |
alarm->mask;
if (alarm->sel == RTC_SELECT_DAY) {
assert_param(IS_RTC_DAY(bcd_to_dec(alarm->day)));
tmp |= (alarm->day << 24);
tmp &= 0x7FFFFFFF; /* Reset bit31 */
}
else {
tmp |= (1 << (alarm->week + 24));
tmp |= 0x80000000; /* Set bit31 */
}
}
ss_tmp = (alarm->time.sub_sec & 0x7F) |
(alarm->ss_mask << 24);
RTC_UNLOCK();
if (alarm->idx == RTC_ALARM_A) {
WRITE_REG(RTC->ALMA, tmp);
WRITE_REG(RTC->ALMASSEC, ss_tmp);
SET_BIT(RTC->CON, RTC_CON_ALMAEN_MSK);
}
else {
WRITE_REG(RTC->ALMB, tmp);
WRITE_REG(RTC->ALMBSSEC, ss_tmp);
SET_BIT(RTC->CON, RTC_CON_ALMBEN_MSK);
}
RTC_LOCK();
return;
}
/**
* @brief Get alarm parameters.
* @param alarm: pointer to rtc_alarm_t struct.
* @param format: Data format.
* @retval None
*/
void ald_rtc_get_alarm(rtc_alarm_t *alarm, rtc_format_t format)
{
uint8_t week;
uint32_t tmp, ss_tmp;
assert_param(alarm != NULL);
assert_param(IS_RTC_FORMAT(format));
if (alarm->idx == RTC_ALARM_A) {
tmp = RTC->ALMA;
ss_tmp = RTC->ALMASSEC;
}
else {
tmp = RTC->ALMB;
ss_tmp = RTC->ALMBSSEC;
}
if ((tmp >> 31) & 0x1) {
alarm->sel = RTC_SELECT_WEEK;
week = ((tmp >> 24) & 0x7F);
switch (week) {
case 1:
alarm->week = 0;
break;
case 2:
alarm->week = 1;
break;
case 4:
alarm->week = 2;
break;
case 8:
alarm->week = 3;
break;
case 16:
alarm->week = 4;
break;
case 32:
alarm->week = 5;
break;
case 64:
alarm->week = 6;
break;
default:
break;
}
}
else {
alarm->sel = RTC_SELECT_DAY;
if (format == RTC_FORMAT_DEC)
alarm->day = bcd_to_dec((tmp >> 24) & 0x3F);
else
alarm->day = (tmp >> 24) & 0x3F;
}
if (format == RTC_FORMAT_DEC) {
alarm->time.second = bcd_to_dec(tmp & 0x7F);
alarm->time.minute = bcd_to_dec((tmp >> 8) & 0x7F);
alarm->time.hour = bcd_to_dec((tmp >> 16) & 0x3F);
}
else {
alarm->time.second = tmp & 0x7F;
alarm->time.minute = (tmp >> 8) & 0x7F;
alarm->time.hour = (tmp >> 16) & 0x3F;
}
alarm->time.sub_sec = ss_tmp & 0x7FFF;
alarm->ss_mask = (rtc_sub_second_mask_t)((ss_tmp >> 24) & 0xF);
alarm->mask = tmp & ALARM_MASK_ALL;
return;
}
/**
* @}
*/
/** @defgroup RTC_Public_Functions_Group4 Time stamp functions
* @brief RTC time stamp functions
*
* @verbatim
===============================================================================
##### Time stamp functions #####
===============================================================================
[..] This section provides functions allowing:
[#]
(+) To configure the time stamp use ald_rtc_set_time_stamp() function
(+) To read the time stamp use ald_rtc_get_time_stamp() function
(+) To cancel the time stamp use ald_rtc_cancel_time_stamp() function
@endverbatim
* @{
*/
/**
* @brief Set time stamp.
* @param sel: time stamp signal select:
* @arg RTC_TS_SIGNAL_SEL_TAMPER0
* @arg RTC_TS_SIGNAL_SEL_TAMPER1
* @param style: time stamp trigger style:
* @arg RTC_TS_RISING_EDGE
* @arg RTC_TS_FALLING_EDGE
* @retval None
*/
void ald_rtc_set_time_stamp(rtc_ts_signal_sel_t sel, rtc_ts_trigger_style_t style)
{
assert_param(IS_RTC_TS_SIGNAL(sel));
assert_param(IS_RTC_TS_STYLE(style));
RTC_UNLOCK();
CLEAR_BIT(RTC->CON, RTC_CON_TSEN_MSK);
MODIFY_REG(RTC->CON, RTC_CON_TSSEL_MSK, style << RTC_CON_TSSEL_POS);
MODIFY_REG(RTC->CON, RTC_CON_TSPIN_MSK, sel << RTC_CON_TSPIN_POS);
SET_BIT(RTC->CON, RTC_CON_TSEN_MSK);
RTC_LOCK();
return;
}
/**
* @brief Cancel time stamp.
* @retval None
*/
void ald_rtc_cancel_time_stamp(void)
{
RTC_UNLOCK();
CLEAR_BIT(RTC->CON, RTC_CON_TSEN_MSK);
RTC_LOCK();
return;
}
/**
* @brief Get time stamp value.
* @param ts_time: pointer to rtc_time_t structure.
* @param ts_date: pointer to rtc_date_t structure.
* @param format: Data format.
* @retval None
*/
void ald_rtc_get_time_stamp(rtc_time_t *ts_time, rtc_date_t *ts_date, rtc_format_t format)
{
uint32_t tmp0, tmp1;
assert_param(ts_time != NULL);
assert_param(ts_date != NULL);
assert_param(IS_RTC_FORMAT(format));
ts_time->sub_sec = RTC->TSSSEC & 0xFFFF;
tmp0 = RTC->TSTIME;
tmp1 = RTC->TSDATE;
if (format == RTC_FORMAT_DEC) {
ts_time->second = bcd_to_dec(tmp0 & 0x7F);
ts_time->minute = bcd_to_dec((tmp0 >> 8) & 0x7F);
ts_time->hour = bcd_to_dec((tmp0 >> 16) & 0x3F);
ts_date->day = bcd_to_dec(tmp1 & 0x3F);
ts_date->month = bcd_to_dec((tmp1 >> 8) & 0x1F);
ts_date->year = bcd_to_dec((tmp1 >> 16) & 0xFF);
ts_date->week = bcd_to_dec((tmp1 >> 24) & 0x7);
}
else {
ts_time->second = tmp0 & 0x7F;
ts_time->minute = (tmp0 >> 8) & 0x7F;
ts_time->hour = (tmp0 >> 16) & 0x3F;
ts_date->day = tmp1 & 0x3F;
ts_date->month = (tmp1 >> 8) & 0x1F;
ts_date->year = (tmp1 >> 16) & 0xFF;
ts_date->week = (tmp1 >> 24) & 0x7;
}
return;
}
/**
* @}
*/
/** @defgroup RTC_Public_Functions_Group5 Tamper functions
* @brief RTC tamper functions
*
* @verbatim
===============================================================================
##### Tamper functions #####
===============================================================================
[..] This section provides functions allowing:
[#]
(+) To configure the tamper use ald_rtc_set_tamper() function
(+) To cancel the tamper use ald_rtc_alarm_cmd() function
@endverbatim
* @{
*/
/**
* @brief Set tamper parameters.
* @param tamper: pointer to rtc_tamper_t structure.
* @retval None
*/
void ald_rtc_set_tamper(rtc_tamper_t *tamper)
{
assert_param(IS_RTC_TAMPER(tamper->idx));
assert_param(IS_RTC_TAMPER_TRIGGER(tamper->trig));
assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(tamper->freq));
assert_param(IS_RTC_TAMPER_DURATION(tamper->dur));
assert_param(IS_FUNC_STATE(tamper->ts));
RTC_UNLOCK();
MODIFY_REG(RTC->TAMPCON, RTC_TAMPCON_TAMPTS_MSK, tamper->ts << RTC_TAMPCON_TAMPTS_POS);
MODIFY_REG(RTC->TAMPCON, RTC_TAMPCON_TAMPCKS_MSK, tamper->freq << RTC_TAMPCON_TAMPCKS_POSS);
MODIFY_REG(RTC->TAMPCON, RTC_TAMPCON_TAMPFLT_MSK, tamper->dur << RTC_TAMPCON_TAMPFLT_POSS);
if (tamper->idx == RTC_TAMPER_0) {
MODIFY_REG(RTC->TAMPCON, RTC_TAMPCON_TAMP0LV_MSK, tamper->trig << RTC_TAMPCON_TAMP0LV_POS);
SET_BIT(RTC->TAMPCON, RTC_TAMPCON_TAMP0EN_MSK);
}
else {
MODIFY_REG(RTC->TAMPCON, RTC_TAMPCON_TAMP1LV_MSK, tamper->trig << RTC_TAMPCON_TAMP1LV_POS);
SET_BIT(RTC->TAMPCON, RTC_TAMPCON_TAMP1EN_MSK);
}
RTC_LOCK();
return;
}
/**
* @brief Cancel tamper.
* @param idx: index of tamper:
* @arg RTC_TAMPER_0
* @arg RTC_TAMPER_1
* @retval None
*/
void ald_rtc_cancel_tamper(rtc_tamper_idx_t idx)
{
assert_param(IS_RTC_TAMPER(idx));
RTC_UNLOCK();
if (idx == RTC_TAMPER_0)
CLEAR_BIT(RTC->TAMPCON, RTC_TAMPCON_TAMP0EN_MSK);
else
CLEAR_BIT(RTC->TAMPCON, RTC_TAMPCON_TAMP1EN_MSK);
RTC_LOCK();
return;
}
/**
* @}
*/
/** @defgroup RTC_Public_Functions_Group6 Wake-up functions
* @brief RTC wake-up functions
*
* @verbatim
===============================================================================
##### Wake-up functions #####
===============================================================================
[..] This section provides functions allowing:
[#]
(+) To configure the wake-up parameters use ald_rtc_set_wakeup() function
(+) To read the re-load register value use ald_rtc_get_wakeup_timer_value() function
(+) To cancel the wake-up use ald_rtc_cancel_wakeup() function
@endverbatim
* @{
*/
/**
* @brief Set wake-up parameters.
* @param clock: pointer to rtc_wakeup_clock_t structure.
* @param value: re-load value.
* @retval None
*/
void ald_rtc_set_wakeup(rtc_wakeup_clock_t clock, uint16_t value)
{
assert_param(IS_RTC_WAKEUP_CLOCK(clock));
RTC_UNLOCK();
MODIFY_REG(RTC->CON, RTC_CON_WUCKS_MSK, clock << RTC_CON_WUCKS_POSS);
WRITE_REG(RTC->WUMAT, value & 0xFFFF);
SET_BIT(RTC->CON, RTC_CON_WUTE_MSK);
RTC_LOCK();
return;
}
/**
* @brief Cancel wake-up.
* @retval None
*/
void ald_rtc_cancel_wakeup(void)
{
RTC_UNLOCK();
CLEAR_BIT(RTC->CON, RTC_CON_WUTE_MSK);
RTC_LOCK();
return;
}
/**
* @brief Get wake-up re-load register value.
* @retval Value of re-load register.
*/
uint16_t ald_rtc_get_wakeup_timer_value(void)
{
return RTC->WUMAT & 0xFFFF;
}
/**
* @}
*/
/** @defgroup RTC_Public_Functions_Group7 Clock output functions
* @brief RTC clock output functions
*
* @verbatim
===============================================================================
##### Clock output functions #####
===============================================================================
[..] This section provides functions allowing:
[#]
(+) To configure the clock output type use ald_rtc_set_clock_output() function
(+) To cancel the clock output use ald_rtc_cancel_clock_output() function
@endverbatim
* @{
*/
/**
* @brief Set clock output parameters.
* @param clock: pointer to rtc_clock_output_t structure.
* @retval ALD status.
*/
ald_status_t ald_rtc_set_clock_output(rtc_clock_output_t clock)
{
uint32_t cnt = 4000;
assert_param(IS_RTC_CLOCK_OUTPUT(clock));
SYSCFG_UNLOCK();
if (clock == RTC_CLOCK_OUTPUT_EXA_1) {
SET_BIT(CMU->CLKENR, CMU_CLKENR_PLL2EN_MSK);
while ((READ_BIT(CMU->PLLCFG, CMU_PLLCFG_PLL2LCKN_MSK)) && (--cnt));
cnt = 4000;
while ((!(READ_BIT(CMU->CLKSR, CMU_CLKSR_PLL2RDY_MSK))) && (--cnt));
}
else {
CLEAR_BIT(CMU->CLKENR, CMU_CLKENR_PLL2EN_MSK);
}
SYSCFG_LOCK();
RTC_UNLOCK();
MODIFY_REG(RTC->CON, RTC_CON_CKOS_MSK, clock << RTC_CON_CKOS_POSS);
SET_BIT(RTC->CON, RTC_CON_CKOE_MSK);
RTC_LOCK();
return OK;
}
/**
* @brief Cancel clock output.
* @retval None
*/
void ald_rtc_cancel_clock_output(void)
{
RTC_UNLOCK();
CLEAR_BIT(RTC->CON, RTC_CON_CKOE_MSK);
RTC_LOCK();
return;
}
/**
* @}
*/
/** @defgroup RTC_Public_Functions_Group8 Control functions
* @brief RTC control functions
*
* @verbatim
===============================================================================
##### Control functions #####
===============================================================================
[..] This section provides functions allowing:
[#]
(+) Configure interrupt enable/disable.
(+) Enable/disable alarm.
(+) Configure rtc shift.
(+) Calibrate time.
(+) Get interrupt source status.
(+) Get interrupt flag status.
(+) Clear interrupt flag.
@endverbatim
* @{
*/
/**
* @brief Enable/disable the specified RTC interrupts.
* @param it: Specifies the RTC interrupt sources to be enabled or disabled.
* This parameter can be one of the @ref rtc_it_t.
* @param state: New state of the specified RTC interrupts.
* This parameter can be:
* @arg ENABLE
* @arg DISABLE
* @retval None
*/
void ald_rtc_interrupt_config(rtc_it_t it, type_func_t state)
{
assert_param(IS_RTC_IT(it));
assert_param(IS_FUNC_STATE(state));
RTC_UNLOCK();
if (state == ENABLE)
SET_BIT(RTC->IER, it);
else
CLEAR_BIT(RTC->IER, it);
RTC_LOCK();
return;
}
/**
* @brief Enable/Disable alarm.
* @param idx: index of alarm:
* @arg RTC_ALARM_A
* @arg RTC_ALARM_B
* @param state: New status of the specified alarm:
* @arg ENABLE
* @arg DISABLE
* @retval None
*/
void ald_rtc_alarm_cmd(rtc_alarm_idx_t idx, type_func_t state)
{
assert_param(IS_RTC_ALARM(idx));
assert_param(IS_FUNC_STATE(state));
RTC_UNLOCK();
if (idx == RTC_ALARM_A)
MODIFY_REG(RTC->CON, RTC_CON_ALMAEN_MSK, state << RTC_CON_ALMAEN_POS);
else
MODIFY_REG(RTC->CON, RTC_CON_ALMBEN_MSK, state << RTC_CON_ALMBEN_POS);
RTC_LOCK();
return;
}
/**
* @brief Set shift parameters.
* @param add_1s: Enable/Disable added 1 second.
* @param sub_ss: value of sub-sconde.
* @retval ALD status.
*/
ald_status_t ald_rtc_set_shift(type_func_t add_1s, uint16_t sub_ss)
{
uint32_t tick;
assert_param(IS_FUNC_STATE(add_1s));
assert_param(IS_SHIFT_SUB_SS(sub_ss));
RTC_UNLOCK();
MODIFY_REG(RTC->SSECTR, RTC_SSECTR_TRIM_MSK, sub_ss << RTC_SSECTR_TRIM_POSS);
MODIFY_REG(RTC->SSECTR, RTC_SSECTR_INC_MSK, add_1s << RTC_SSECTR_INC_POS);
RTC_LOCK();
tick = ald_get_tick();
while (READ_BIT(RTC->CON, RTC_CON_SSEC_MSK)) {
if ((ald_get_tick() - tick) > RTC_TIMEOUT_VALUE)
return TIMEOUT;
}
return OK;
}
/**
* @brief Set calibation
* @param config: pointer to rtc_cali_t structure.
* @retval None
*/
void ald_rtc_set_cali(rtc_cali_t *config)
{
assert_param(IS_RTC_CALI_FREQ(config->cali_freq));
assert_param(IS_RTC_CALI_TC(config->tc));
assert_param(IS_RTC_CALC_FREQ(config->calc_freq));
assert_param(IS_RTC_CALI_CALC(config->calc));
assert_param(IS_FUNC_STATE(config->acc));
RTC_UNLOCK();
RTC_CALI_UNLOCK();
MODIFY_REG(RTC->CALCON, RTC_CALCON_CALP_MSK, config->cali_freq << RTC_CALCON_CALP_POSS);
MODIFY_REG(RTC->CALCON, RTC_CALCON_TCM_MSK, config->tc << RTC_CALCON_TCM_POSS);
MODIFY_REG(RTC->CALCON, RTC_CALCON_TCP_MSK, config->calc_freq << RTC_CALCON_TCP_POSS);
MODIFY_REG(RTC->CALCON, RTC_CALCON_ALG_MSK, config->calc << RTC_CALCON_ALG_POS);
MODIFY_REG(RTC->CALCON, RTC_CALCON_DCMACC_MSK, config->acc << RTC_CALCON_DCMACC_POS);
SET_BIT(RTC->CALCON, RTC_CALCON_CALEN_MSK);
RTC_CALI_LOCK();
RTC_LOCK();
return;
}
/**
* @brief Cancel calibration
* @retval None
*/
void ald_rtc_cancel_cali(void)
{
RTC_CALI_UNLOCK();
CLEAR_BIT(RTC->CALCON, RTC_CALCON_CALEN_MSK);
RTC_CALI_LOCK();
return;
}
/**
* @brief Get calibration status.
* @retval ALD status.
*/
ald_status_t ald_rtc_get_cali_status(void)
{
if (READ_BIT(RTC->CALCON, RTC_CALCON_ERR_MSK))
return ERROR;
else
return OK;
}
/**
* @brief Write temperature value.
* @param temp: the value of temperature.
* @retval None
*/
void ald_rtc_write_temp(uint16_t temp)
{
RTC_CALI_UNLOCK();
MODIFY_REG(RTC->TEMPR, RTC_TEMPR_VAL_MSK, temp << RTC_TEMPR_VAL_POSS);
RTC_CALI_LOCK();
return;
}
/**
* @brief Get the status of RTC interrupt source.
* @param it: Specifies the RTC interrupt source.
* This parameter can be one of the @ref rtc_it_t.
* @retval Status:
* - 0: RESET
* - 1: SET
*/
it_status_t ald_rtc_get_it_status(rtc_it_t it)
{
assert_param(IS_RTC_IT(it));
if (READ_BIT(RTC->IER, it))
return SET;
return RESET;
}
/**
* @brief Get the status of RTC interrupt flag.
* @param flag: Specifies the RTC interrupt flag.
* This parameter can be one of the @ref rtc_flag_t.
* @retval Status:
* - 0: RESET
* - 1: SET
*/
flag_status_t ald_rtc_get_flag_status(rtc_flag_t flag)
{
assert_param(IS_RTC_IF(flag));
if (READ_BIT(RTC->IFR, flag))
return SET;
return RESET;
}
/** @brief Clear the specified RTC pending flag.
* @param flag: specifies the flag to check.
* @retval None.
*/
void ald_rtc_clear_flag_status(rtc_flag_t flag)
{
assert_param(IS_RTC_IF(flag));
RTC_UNLOCK();
WRITE_REG(RTC->IFCR, flag);
RTC_LOCK();
return;
}
/**
* @}
*/
/**
* @}
*/
#endif /* ALD_RTC */
/**
* @}
*/
/**
* @}
*/
|