Repository navigation
Expand file tree
/
Copy pathSTM32RTC.cpp
More file actions
887 lines (812 loc) · 19.2 KB
/
Copy pathSTM32RTC.cpp
File metadata and controls
887 lines (812 loc) · 19.2 KB
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
/**
******************************************************************************
* @file STM32RTC.cpp
* @author WI6LABS
* @version V1.0.0
* @date 12-December-2017
* @brief Provides a RTC interface for Arduino
*
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#include <time.h>
#include "STM32RTC.h"
#define EPOCH_TIME_OFF 946684800 // This is 1st January 2000, 00:00:00 in epoch time
#define EPOCH_TIME_YEAR_OFF 100 // years since 1900
// Initialize static variable
bool STM32RTC::_configured = false;
STM32RTC::STM32RTC(void): _clockSource(RTC_LSI_CLOCK)
{
}
/**
* @brief initializes the RTC
* @param resetTime: if true reconfigures the RTC
* @param format: hour format: RTC_HOUR_12 or RTC_HOUR_24(default)
* @retval None
*/
void STM32RTC::begin(bool resetTime, RTC_Hour_Format format)
{
if(resetTime == true) {
_configured = false;
}
begin(format);
}
/**
* @brief initializes the RTC
* @param format: hour format: RTC_HOUR_12 or RTC_HOUR_24(default)
* @retval None
*/
void STM32RTC::begin(RTC_Hour_Format format)
{
if(_configured == false) {
RTC_init((format == RTC_HOUR_12)? HOUR_FORMAT_12: HOUR_FORMAT_24,
(_clockSource == RTC_LSE_CLOCK)? LSE_CLOCK:
(_clockSource == RTC_HSE_CLOCK)? HSE_CLOCK : LSI_CLOCK);
// Must be set before call of sync methods
_configured = true;
syncTime();
syncDate();
// Use current time to init alarm members
_alarmDay = _day;
_alarmHours = _hours;
_alarmMinutes = _minutes;
_alarmSeconds = _seconds;
_alarmSubSeconds = _subSeconds;
_alarmPeriod = _hoursPeriod;
} else {
syncTime();
syncDate();
syncAlarmTime();
}
}
/**
* @brief Deinitialize and stop the RTC
* @param None
* @retval None
*/
void STM32RTC::end(void)
{
if(_configured == true) {
RTC_DeInit();
_configured = false;
}
}
/**
* @brief set the RTC clock source. By default LSI clock is selected. This
* method must be called before begin().
* @param source: clock source: RTC_LSI_CLOCK, RTC_LSE_CLOCK or RTC_HSE_CLOCK
* @retval None
*/
void STM32RTC::setClockSource(RTC_Source_Clock source)
{
if(IS_CLOCK_SOURCE(source)) {
_clockSource = source;
RTC_SetClockSource((_clockSource == RTC_LSE_CLOCK)? LSE_CLOCK:
(_clockSource == RTC_HSE_CLOCK)? HSE_CLOCK : LSI_CLOCK);
}
}
/**
* @brief get user (a)synchronous prescaler values if set else computed
* ones for the current clock source.
* @param predivA: pointer to the current Asynchronous prescaler value
* @param predivS: pointer to the current Synchronous prescaler value
* @retval None
*/
void STM32RTC::getPrediv(int8_t *predivA, int16_t *predivS)
{
if((predivA != NULL) && (predivS != NULL)) {
RTC_getPrediv(predivA, predivS);
}
}
/**
* @brief set user (a)synchronous prescalers value.
* @note This method must be called before begin().
* @param predivA: Asynchronous prescaler value. Reset value: -1
* @param predivS: Synchronous prescaler value. Reset value: -1
* @retval None
*/
void STM32RTC::setPrediv(int8_t predivA, int16_t predivS)
{
RTC_setPrediv(predivA, predivS);
}
/**
* @brief enable the RTC alarm.
* @param match: Alarm_Match configuration
* @retval None
*/
void STM32RTC::enableAlarm(Alarm_Match match)
{
if(_configured) {
_alarmMatch = match;
switch (match) {
case MATCH_OFF:
RTC_StopAlarm();
break;
case MATCH_YYMMDDHHMMSS://kept for compatibility
case MATCH_MMDDHHMMSS: //kept for compatibility
case MATCH_DHHMMSS:
case MATCH_HHMMSS:
case MATCH_MMSS:
case MATCH_SS:
RTC_StartAlarm(_alarmDay, _alarmHours, _alarmMinutes, _alarmSeconds,
_alarmSubSeconds, (_alarmPeriod == RTC_AM)? AM: PM,
static_cast<uint8_t>(_alarmMatch));
break;
default:
break;
}
}
}
/**
* @brief disable the RTC alarm.
* @retval None
*/
void STM32RTC::disableAlarm(void)
{
if(_configured) {
RTC_StopAlarm();
}
}
/**
* @brief attach a callback to the RTC alarm interrupt.
* @param callback: pointer to the callback
* @retval None
*/
void STM32RTC::attachInterrupt(voidFuncPtr callback, void *data)
{
attachAlarmCallback(callback, data);
}
/**
* @brief detach the RTC alarm callback.
* @retval None
*/
void STM32RTC::detachInterrupt(void)
{
detachAlarmCallback();
}
// Kept for compatibility. Use STM32LowPower library.
void STM32RTC::standbyMode(void)
{
}
/*
* Get Functions
*/
/**
* @brief get RTC subseconds.
* @retval return the current subseconds from the RTC.
*/
uint32_t STM32RTC::getSubSeconds(void)
{
syncTime();
return _subSeconds;
}
/**
* @brief get RTC seconds.
* @retval return the current seconds from the RTC.
*/
uint8_t STM32RTC::getSeconds(void)
{
syncTime();
return _seconds;
}
/**
* @brief get RTC minutes.
* @retval return the current minutes from the RTC.
*/
uint8_t STM32RTC::getMinutes(void)
{
syncTime();
return _minutes;
}
/**
* @brief get RTC hours.
* @param format: optional (default: NULL)
* pointer to the current hour format set in the RTC: AM or PM
* @retval return the current hours from the RTC.
*/
uint8_t STM32RTC::getHours(RTC_AM_PM *period)
{
syncTime();
if(period != NULL) {
*period = _hoursPeriod;
}
return _hours;
}
/**
* @brief get RTC week day.
* @retval return the current week day from the RTC.
*/
uint8_t STM32RTC::getWeekDay(void)
{
syncDate();
return _wday;
}
/**
* @brief get RTC day.
* @retval return the current day from the RTC.
*/
uint8_t STM32RTC::getDay(void)
{
syncDate();
return _day;
}
/**
* @brief get RTC month.
* @retval return the current month from the RTC.
*/
uint8_t STM32RTC::getMonth(void)
{
syncDate();
return _month;
}
/**
* @brief get RTC year.
* @retval return the current year from the RTC.
*/
uint8_t STM32RTC::getYear(void)
{
syncDate();
return _year;
}
/**
* @brief get RTC alarm subsecond.
* @retval return the current alarm subsecond.
*/
uint32_t STM32RTC::getAlarmSubSeconds(void)
{
syncAlarmTime();
return _alarmSubSeconds;
}
/**
* @brief get RTC alarm second.
* @retval return the current alarm second.
*/
uint8_t STM32RTC::getAlarmSeconds(void)
{
syncAlarmTime();
return _alarmSeconds;
}
/**
* @brief get RTC alarm minute.
* @retval return the current alarm minute.
*/
uint8_t STM32RTC::getAlarmMinutes(void)
{
syncAlarmTime();
return _alarmMinutes;
}
/**
* @brief get RTC alarm hour.
* @param format: optional (default: NULL)
* pointer to the current hour format set in the RTC: AM or PM
* @retval return the current alarm hour.
*/
uint8_t STM32RTC::getAlarmHours(RTC_AM_PM *period)
{
syncAlarmTime();
if(period != NULL) {
*period = _alarmPeriod;
}
return _alarmHours;
}
/**
* @brief get RTC alarm day.
* @retval return the current alarm day.
*/
uint8_t STM32RTC::getAlarmDay(void)
{
syncAlarmTime();
return _alarmDay;
}
/**
* @brief get RTC alarm month.
* @NOTE This function is kept for compatibility but the STM32 RTC
* can't assign a month to an alarm. See board datasheet.
* @retval always returns 0
*/
uint8_t STM32RTC::getAlarmMonth(void)
{
return 0;
}
/**
* @brief get RTC alarm year.
* @NOTE This function is kept for compatibility but the STM32 RTC
* can't assign a year to an alarm. See board datasheet.
* @retval always returns 0
*/
uint8_t STM32RTC::getAlarmYear(void)
{
return 0;
}
/*
* Set Functions
*/
/**
* @brief set RTC subseconds.
* @param subseconds: 0-999
* @retval none
*/
void STM32RTC::setSubSeconds(uint32_t subSeconds)
{
if (_configured) {
syncTime();
if(subSeconds < 1000) {
_subSeconds = subSeconds;
}
RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM);
}
}
/**
* @brief set RTC seconds.
* @param seconds: 0-59
* @retval none
*/
void STM32RTC::setSeconds(uint8_t seconds)
{
if (_configured) {
syncTime();
if(seconds < 60) {
_seconds = seconds;
}
RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM);
}
}
/**
* @brief set RTC minutes.
* @param minutes: 0-59
* @retval none
*/
void STM32RTC::setMinutes(uint8_t minutes)
{
if (_configured) {
syncTime();
if(minutes < 60) {
_minutes = minutes;
}
RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM);
}
}
/**
* @brief set RTC hours.
* @param hours: 0-23
* @retval none
*/
void STM32RTC::setHours(uint8_t hours)
{
if (_configured) {
syncTime();
if(hours < 24) {
_hours = hours;
}
RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM);
}
}
/**
* @brief set RTC hours.
* @param hours: 0-23 or 0-12
* @param hours format: AM or PM
* @retval none
*/
void STM32RTC::setHours(uint8_t hours, RTC_AM_PM period)
{
if (_configured) {
syncTime();
if(hours < 24) {
_hours = hours;
}
_hoursPeriod = period;
RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM);
}
}
/**
* @brief set RTC time.
* @param hours: 0-23
* @param minutes: 0-59
* @param seconds: 0-59
* @retval none
*/
void STM32RTC::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds)
{
if (_configured) {
setSeconds(seconds);
setMinutes(minutes);
setHours(hours);
}
}
/**
* @brief set RTC time.
* @param hours: 0-23 or 0-12
* @param minutes: 0-59
* @param seconds: 0-59
* @param hour format: AM or PM
* @retval none
*/
void STM32RTC::setTime(uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t subSeconds, RTC_AM_PM period)
{
if (_configured) {
setSubSeconds(subSeconds);
setSeconds(seconds);
setMinutes(minutes);
setHours(hours, period);
}
}
/**
* @brief set RTC week day.
* @param week day: 1-7 (Monday first)
* @retval none
*/
void STM32RTC::setWeekDay(uint8_t weekDay)
{
if (_configured) {
RTC_GetDate(&_year, &_month, &_day, &_wday);
if((weekDay >= 1) && (weekDay <= 7)) {
_wday = weekDay;
}
RTC_SetDate(_year, _month, _day, _wday);
}
}
/**
* @brief set RTC day.
* @param day: 1-31
* @retval none
*/
void STM32RTC::setDay(uint8_t day)
{
if (_configured) {
RTC_GetDate(&_year, &_month, &_day, &_wday);
if((day >= 1) && (day <= 31)) {
_day = day;
}
RTC_SetDate(_year, _month, _day, _wday);
}
}
/**
* @brief set RTC month.
* @param month: 1-12
* @retval none
*/
void STM32RTC::setMonth(uint8_t month)
{
if (_configured) {
RTC_GetDate(&_year, &_month, &_day, &_wday);
if((month >= 1) && (month <= 12)) {
_month = month;
}
RTC_SetDate(_year, _month, _day, _wday);
}
}
/**
* @brief set RTC year.
* @param year: 0-99
* @retval none
*/
void STM32RTC::setYear(uint8_t year)
{
if (_configured) {
RTC_GetDate(&_year, &_month, &_day, &_wday);
if(year < 100) {
_year = year;
}
RTC_SetDate(_year, _month, _day, _wday);
}
}
/**
* @brief set RTC calendar.
* @param day: 1-31
* @param month: 1-12
* @param year: 0-99
* @retval none
*/
void STM32RTC::setDate(uint8_t day, uint8_t month, uint8_t year)
{
if (_configured) {
setDay(day);
setMonth(month);
setYear(year);
}
}
/**
* @brief set RTC calendar.
* @param weekDay: 1-7 (Monday first)
* @param day: 1-31
* @param month: 1-12
* @param year: 0-99
* @retval none
*/
void STM32RTC::setDate(uint8_t weekDay, uint8_t day, uint8_t month, uint8_t year)
{
if (_configured) {
setWeekDay(weekDay);
setDay(day);
setMonth(month);
setYear(year);
}
}
/**
* @brief set RTC alarm second.
* @param seconds: 0-59
* @retval none
*/
void STM32RTC::setAlarmSeconds(uint8_t seconds)
{
if (_configured) {
if(seconds < 60) {
_alarmSeconds = seconds;
}
}
}
/**
* @brief set RTC alarm minute.
* @param minutes: 0-59
* @retval none
*/
void STM32RTC::setAlarmMinutes(uint8_t minutes)
{
if (_configured) {
if(minutes < 60) {
_alarmMinutes = minutes;
}
}
}
/**
* @brief set RTC alarm hour.
* @param hour: 0-23
* @retval none
*/
void STM32RTC::setAlarmHours(uint8_t hours)
{
if (_configured) {
if(hours < 24) {
_alarmHours = hours;
}
}
}
/**
* @brief set RTC alarm hour.
* @param hour: 0-23 or 0-12
* @param hour format: AM or PM
* @retval none
*/
void STM32RTC::setAlarmHours(uint8_t hours, RTC_AM_PM period)
{
if (_configured) {
if(hours < 24) {
_alarmHours = hours;
}
_alarmPeriod = period;
}
}
/**
* @brief set RTC alarm time.
* @param hours: 0-23
* @param minutes: 0-59
* @param seconds: 0-59
* @retval none
*/
void STM32RTC::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds)
{
if (_configured) {
setAlarmHours(hours);
setAlarmMinutes(minutes);
setAlarmSeconds(seconds);
}
}
/**
* @brief set RTC alarm time.
* @param hours: 0-23
* @param minutes: 0-59
* @param seconds: 0-59
* @param hour format: AM or PM
* @retval none
*/
void STM32RTC::setAlarmTime(uint8_t hours, uint8_t minutes, uint8_t seconds, RTC_AM_PM period)
{
if (_configured) {
setAlarmHours(hours, period);
setAlarmMinutes(minutes);
setAlarmSeconds(seconds);
}
}
/**
* @brief set RTC alarm day.
* @param day: 1-31
* @retval none
*/
void STM32RTC::setAlarmDay(uint8_t day)
{
if (_configured) {
if((day >= 1) && (day <= 31)) {
_alarmDay = day;
}
}
}
/**
* @brief set RTC alarm month.
* @NOTE This function is kept for compatibility but the STM32 RTC
* can't assign a month to an alarm. See board datasheet.
* @param month is ignored.
*/
void STM32RTC::setAlarmMonth(uint8_t month)
{
UNUSED(month);
}
/**
* @brief set RTC alarm year.
* @NOTE This function is kept for compatibility but the STM32 RTC
* can't assign a year to an alarm. See board datasheet.
* @param year is ignored.
*/
void STM32RTC::setAlarmYear(uint8_t year)
{
UNUSED(year);
}
/**
* @brief set RTC alarm date.
* @NOTE Parameters month and year are ingored because the STM32 RTC can't
* assign a month or year to an alarm. See board datasheet.
* @param day: 1-31
* @param month is ignored
* @param year is ignored
*/
void STM32RTC::setAlarmDate(uint8_t day, uint8_t month, uint8_t year)
{
UNUSED(month);
UNUSED(year);
setAlarmDay(day);
}
/**
* @brief get epoch time
* @retval epoch time in seconds
*/
uint32_t STM32RTC::getEpoch(void)
{
struct tm tm;
syncDate();
syncTime();
tm.tm_isdst = -1;
tm.tm_yday = 0;
tm.tm_wday = _wday - 1;
tm.tm_year = _year + EPOCH_TIME_YEAR_OFF;
tm.tm_mon = _month - 1;
tm.tm_mday = _day;
tm.tm_hour = _hours;
tm.tm_min = _minutes;
tm.tm_sec = _seconds;
return mktime(&tm);
}
/**
* @brief get epoch time since 1st January 2000, 00:00:00
* @retval epoch time in seconds
*/
uint32_t STM32RTC::getY2kEpoch(void)
{
return (getEpoch() - EPOCH_TIME_OFF);
}
/**
* @brief set RTC alarm from epoch time
* @param epoch time in seconds
*/
void STM32RTC::setAlarmEpoch(uint32_t ts, Alarm_Match match)
{
if (_configured) {
if (ts < EPOCH_TIME_OFF) {
ts = EPOCH_TIME_OFF;
}
time_t t = ts;
struct tm* tmp = gmtime(&t);
setAlarmDay(tmp->tm_mday);
setAlarmHours(tmp->tm_hour);
setAlarmMinutes(tmp->tm_min);
setAlarmSeconds(tmp->tm_sec);
enableAlarm(match);
}
}
/**
* @brief set RTC time from epoch time
* @param epoch time in seconds
*/
void STM32RTC::setEpoch(uint32_t ts)
{
if (_configured) {
if (ts < EPOCH_TIME_OFF) {
ts = EPOCH_TIME_OFF;
}
time_t t = ts;
struct tm* tmp = gmtime(&t);
_year = tmp->tm_year - EPOCH_TIME_YEAR_OFF;
_month = tmp->tm_mon + 1;
_day = tmp->tm_mday;
_wday = tmp->tm_wday + 1;
_hours = tmp->tm_hour;
_minutes = tmp->tm_min;
_seconds = tmp->tm_sec;
RTC_SetDate(_year, _month, _day, _wday);
RTC_SetTime(_hours, _minutes, _seconds, _subSeconds, (_hoursPeriod == RTC_AM)? AM : PM);
}
}
/**
* @brief set RTC time from epoch time since 1st January 2000, 00:00:00
* @param epoch time in seconds
*/
void STM32RTC::setY2kEpoch(uint32_t ts)
{
if (_configured) {
setEpoch(ts + EPOCH_TIME_OFF);
}
}
/**
* @brief synchronise the time from the current RTC one
* @param none
*/
void STM32RTC::syncTime(void)
{
if(_configured) {
hourAM_PM_t p = AM;
RTC_GetTime(&_hours, &_minutes, &_seconds, &_subSeconds, &p);
_hoursPeriod = (p == AM)? RTC_AM : RTC_PM;
}
}
/**
* @brief synchronise the time from the current RTC one
* @param none
*/
void STM32RTC::syncDate(void)
{
if(_configured) {
RTC_GetDate(&_year, &_month, &_day, &_wday);
}
}
/**
* @brief synchronise the alarm time from the current RTC one
* @param none
*/
void STM32RTC::syncAlarmTime(void)
{
if(_configured) {
hourAM_PM_t p = AM;
uint8_t match;
RTC_GetAlarm(&_alarmDay, &_alarmHours, &_alarmMinutes, &_alarmSeconds,
&_alarmSubSeconds, &p, &match);
_alarmPeriod = (p == AM)? RTC_AM : RTC_PM;
switch (static_cast<Alarm_Match>(match)) {
case MATCH_OFF:
case MATCH_YYMMDDHHMMSS://kept for compatibility
case MATCH_MMDDHHMMSS: //kept for compatibility
case MATCH_DHHMMSS:
case MATCH_HHMMSS:
case MATCH_MMSS:
case MATCH_SS:
_alarmMatch = static_cast<Alarm_Match>(match);
break;
default:
_alarmMatch = MATCH_OFF;
break;
}
}
}