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
|
/*
ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
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
http://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.
*/
/*
Concepts and parts of this file have been contributed by Uladzimir Pylinsky
aka barthess.
*/
/**
* @file RTCv2/hal_rtc_lld.c
* @brief STM32 RTC low level driver.
*
* @addtogroup RTC
* @{
*/
#include "hal.h"
#if HAL_USE_RTC || defined(__DOXYGEN__)
/*===========================================================================*/
/* Driver local definitions. */
/*===========================================================================*/
#define RTC_TR_PM_OFFSET 22
#define RTC_TR_HT_OFFSET 20
#define RTC_TR_HU_OFFSET 16
#define RTC_TR_MNT_OFFSET 12
#define RTC_TR_MNU_OFFSET 8
#define RTC_TR_ST_OFFSET 4
#define RTC_TR_SU_OFFSET 0
#define RTC_DR_YT_OFFSET 20
#define RTC_DR_YU_OFFSET 16
#define RTC_DR_WDU_OFFSET 13
#define RTC_DR_MT_OFFSET 12
#define RTC_DR_MU_OFFSET 8
#define RTC_DR_DT_OFFSET 4
#define RTC_DR_DU_OFFSET 0
#define RTC_CR_BKP_OFFSET 18
/*===========================================================================*/
/* Driver exported variables. */
/*===========================================================================*/
/**
* @brief RTC driver identifier.
*/
RTCDriver RTCD1;
/*===========================================================================*/
/* Driver local variables and types. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local functions. */
/*===========================================================================*/
/**
* @brief Beginning of configuration procedure.
*
* @notapi
*/
static void rtc_enter_init(void) {
RTCD1.rtc->ISR |= RTC_ISR_INIT;
while ((RTCD1.rtc->ISR & RTC_ISR_INITF) == 0)
;
}
/**
* @brief Finalizing of configuration procedure.
*
* @notapi
*/
static inline void rtc_exit_init(void) {
RTCD1.rtc->ISR &= ~RTC_ISR_INIT;
}
/**
* @brief Converts time from TR register encoding to timespec.
*
* @param[in] tr TR register value
* @param[out] timespec pointer to a @p RTCDateTime structure
*
* @notapi
*/
static void rtc_decode_time(uint32_t tr, RTCDateTime *timespec) {
uint32_t n;
n = ((tr >> RTC_TR_HT_OFFSET) & 3) * 36000000;
n += ((tr >> RTC_TR_HU_OFFSET) & 15) * 3600000;
n += ((tr >> RTC_TR_MNT_OFFSET) & 7) * 600000;
n += ((tr >> RTC_TR_MNU_OFFSET) & 15) * 60000;
n += ((tr >> RTC_TR_ST_OFFSET) & 7) * 10000;
n += ((tr >> RTC_TR_SU_OFFSET) & 15) * 1000;
timespec->millisecond = n;
}
/**
* @brief Converts date from DR register encoding to timespec.
*
* @param[in] dr DR register value
* @param[out] timespec pointer to a @p RTCDateTime structure
*
* @notapi
*/
static void rtc_decode_date(uint32_t dr, RTCDateTime *timespec) {
timespec->year = (((dr >> RTC_DR_YT_OFFSET) & 15) * 10) +
((dr >> RTC_DR_YU_OFFSET) & 15);
timespec->month = (((dr >> RTC_TR_MNT_OFFSET) & 1) * 10) +
((dr >> RTC_TR_MNU_OFFSET) & 15);
timespec->day = (((dr >> RTC_DR_DT_OFFSET) & 3) * 10) +
((dr >> RTC_DR_DU_OFFSET) & 15);
timespec->dayofweek = (dr >> RTC_DR_WDU_OFFSET) & 7;
}
/**
* @brief Converts time from timespec to TR register encoding.
*
* @param[in] timespec pointer to a @p RTCDateTime structure
* @return the TR register encoding.
*
* @notapi
*/
static uint32_t rtc_encode_time(const RTCDateTime *timespec) {
uint32_t n, tr = 0;
/* Subseconds cannot be set.*/
n = timespec->millisecond / 1000;
/* Seconds conversion.*/
tr = tr | ((n % 10) << RTC_TR_SU_OFFSET);
n /= 10;
tr = tr | ((n % 6) << RTC_TR_ST_OFFSET);
n /= 6;
/* Minutes conversion.*/
tr = tr | ((n % 10) << RTC_TR_MNU_OFFSET);
n /= 10;
tr = tr | ((n % 6) << RTC_TR_MNT_OFFSET);
n /= 6;
/* Hours conversion.*/
tr = tr | ((n % 10) << RTC_TR_HU_OFFSET);
n /= 10;
tr = tr | (n << RTC_TR_HT_OFFSET);
return tr;
}
/**
* @brief Converts a date from timespec to DR register encoding.
*
* @param[in] timespec pointer to a @p RTCDateTime structure
* @return the DR register encoding.
*
* @notapi
*/
static uint32_t rtc_encode_date(const RTCDateTime *timespec) {
uint32_t n, dr = 0;
/* Year conversion. Note, only years last two digits are considered.*/
n = timespec->year;
dr = dr | ((n % 10) << RTC_DR_YU_OFFSET);
n /= 10;
dr = dr | ((n % 10) << RTC_DR_YT_OFFSET);
/* Months conversion.*/
n = timespec->month;
dr = dr | ((n % 10) << RTC_DR_MU_OFFSET);
n /= 10;
dr = dr | ((n % 10) << RTC_DR_MT_OFFSET);
/* Days conversion.*/
n = timespec->day;
dr = dr | ((n % 10) << RTC_DR_DU_OFFSET);
n /= 10;
dr = dr | ((n % 10) << RTC_DR_DT_OFFSET);
/* Days of week conversion.*/
dr = dr | (timespec->dayofweek << RTC_DR_WDU_OFFSET);
return dr;
}
#if RTC_HAS_STORAGE == TRUE
static size_t _getsize(void *instance) {
(void)instance;
return (size_t)STM32_RTC_STORAGE_SIZE;
}
static ps_error_t _read(void *instance, ps_offset_t offset,
size_t n, uint8_t *rp) {
volatile uint32_t *bkpr = &((RTCDriver *)instance)->rtc->BKP0R;
unsigned i;
chDbgCheck((instance != NULL) && (rp != NULL));
chDbgCheck((n > 0U) && (n <= STM32_RTC_STORAGE_SIZE));
chDbgCheck((offset < STM32_RTC_STORAGE_SIZE) &&
(offset + n <= STM32_RTC_STORAGE_SIZE));
for (i = 0; i < (unsigned)n; i++) {
unsigned index = ((unsigned)offset + i) / sizeof (uint32_t);
unsigned shift = ((unsigned)offset + i) % sizeof (uint32_t);
*rp++ = (uint8_t)(bkpr[index] >> (shift * 8U));
}
return PS_NO_ERROR;
}
static ps_error_t _write(void *instance, ps_offset_t offset,
size_t n, const uint8_t *wp) {
volatile uint32_t *bkpr = &((RTCDriver *)instance)->rtc->BKP0R;
unsigned i;
chDbgCheck((instance != NULL) && (wp != NULL));
chDbgCheck((n > 0U) && (n <= STM32_RTC_STORAGE_SIZE));
chDbgCheck((offset < STM32_RTC_STORAGE_SIZE) &&
(offset + n <= STM32_RTC_STORAGE_SIZE));
for (i = 0; i < (unsigned)n; i++) {
unsigned index = ((unsigned)offset + i) / sizeof (uint32_t);
unsigned shift = ((unsigned)offset + i) % sizeof (uint32_t);
uint32_t regval = bkpr[index];
regval &= ~(0xFFU << (shift * 8U));
regval |= (uint32_t)*wp++ << (shift * 8U);
bkpr[index] = regval;
}
return PS_NO_ERROR;
}
/**
* @brief VMT for the RTC storage file interface.
*/
struct RTCDriverVMT _rtc_lld_vmt = {
(size_t)0,
_getsize, _read, _write
};
#endif /* RTC_HAS_STORAGE == TRUE */
/*===========================================================================*/
/* Driver interrupt handlers. */
/*===========================================================================*/
#if defined(STM32_RTC_COMMON_HANDLER)
#if !defined(STM32_RTC_SUPPRESS_COMMON_ISR)
/**
* @brief RTC common interrupt handler.
*
* @isr
*/
OSAL_IRQ_HANDLER(STM32_RTC_COMMON_HANDLER) {
uint32_t isr, clear;
OSAL_IRQ_PROLOGUE();
clear = (0U
| RTC_ISR_TSF
| RTC_ISR_TSOVF
#if defined(RTC_ISR_TAMP1F)
| RTC_ISR_TAMP1F
#endif
#if defined(RTC_ISR_TAMP2F)
| RTC_ISR_TAMP2F
#endif
#if defined(RTC_ISR_TAMP3F)
| RTC_ISR_TAMP3F
#endif
#if defined(RTC_ISR_WUTF)
| RTC_ISR_WUTF
#endif
#if defined(RTC_ISR_ALRAF)
| RTC_ISR_ALRAF
#endif
#if defined(RTC_ISR_ALRBF)
| RTC_ISR_ALRBF
#endif
);
isr = RTCD1.rtc->ISR;
RTCD1.rtc->ISR = isr & ~clear;
extiClearGroup1(EXTI_MASK1(STM32_RTC_ALARM_EXTI) |
EXTI_MASK1(STM32_RTC_TAMP_STAMP_EXTI) |
EXTI_MASK1(STM32_RTC_WKUP_EXTI));
if (RTCD1.callback != NULL) {
uint32_t cr = RTCD1.rtc->CR;
uint32_t tcr;
#if defined(RTC_ISR_WUTF)
if (((cr & RTC_CR_WUTIE) != 0U) && ((isr & RTC_ISR_WUTF) != 0U)) {
RTCD1.callback(&RTCD1, RTC_EVENT_WAKEUP);
}
#endif
#if defined(RTC_ISR_ALRAF)
if (((cr & RTC_CR_ALRAIE) != 0U) && ((isr & RTC_ISR_ALRAF) != 0U)) {
RTCD1.callback(&RTCD1, RTC_EVENT_ALARM_A);
}
#endif
#if defined(RTC_ISR_ALRBF)
if (((cr & RTC_CR_ALRBIE) != 0U) && ((isr & RTC_ISR_ALRBF) != 0U)) {
RTCD1.callback(&RTCD1, RTC_EVENT_ALARM_B);
}
#endif
if ((cr & RTC_CR_TSIE) != 0U) {
if ((isr & RTC_ISR_TSF) != 0U) {
RTCD1.callback(&RTCD1, RTC_EVENT_TS);
}
if ((isr & RTC_ISR_TSOVF) != 0U) {
RTCD1.callback(&RTCD1, RTC_EVENT_TS_OVF);
}
}
/* This part is different depending on if the RTC has a TAMPCR or TAFCR
register.*/
#if defined(RTC_TAFCR_TAMP1E)
tcr = RTCD1.rtc->TAFCR;
if ((tcr & RTC_TAFCR_TAMPIE) != 0U) {
#if defined(RTC_ISR_TAMP1F)
if ((isr & RTC_ISR_TAMP1F) != 0U) {
RTCD1.callback(&RTCD1, RTC_EVENT_TAMP1);
}
#endif
#if defined(RTC_ISR_TAMP2F)
if ((isr & RTC_ISR_TAMP2F) != 0U) {
RTCD1.callback(&RTCD1, RTC_EVENT_TAMP2);
}
#endif
}
#else /* !defined(RTC_TAFCR_TAMP1E) */
tcr = RTCD1.rtc->TAMPCR;
#if defined(RTC_ISR_TAMP1F)
if (((tcr & RTC_TAMPCR_TAMP1IE) != 0U) &&
((isr & RTC_ISR_TAMP1F) != 0U)) {
RTCD1.callback(&RTCD1, RTC_EVENT_TAMP1);
}
#endif
#if defined(RTC_ISR_TAMP2F)
if (((tcr & RTC_TAMPCR_TAMP2IE) != 0U) &&
((isr & RTC_ISR_TAMP2F) != 0U)) {
RTCD1.callback(&RTCD1, RTC_EVENT_TAMP2);
}
#endif
#if defined(RTC_ISR_TAMP3F)
if (((tcr & RTC_TAMPCR_TAMP3IE) != 0U) &&
((isr & RTC_ISR_TAMP3F) != 0U)) {
RTCD1.callback(&RTCD1, RTC_EVENT_TAMP3);
}
#endif
#endif /* !defined(RTC_TAFCR_TAMP1E) */
}
OSAL_IRQ_EPILOGUE();
}
#endif /* !defined(STM32_RTC_SUPPRESS_COMMON_ISR) */
#elif defined(STM32_RTC_TAMP_STAMP_HANDLER) && \
defined(STM32_RTC_WKUP_HANDLER) && \
defined(STM32_RTC_ALARM_HANDLER)
/**
* @brief RTC TAMP/STAMP interrupt handler.
*
* @isr
*/
OSAL_IRQ_HANDLER(STM32_RTC_TAMP_STAMP_HANDLER) {
uint32_t isr, clear;
OSAL_IRQ_PROLOGUE();
clear = (0U
| RTC_ISR_TSF
| RTC_ISR_TSOVF
#if defined(RTC_ISR_TAMP1F)
| RTC_ISR_TAMP1F
#endif
#if defined(RTC_ISR_TAMP2F)
| RTC_ISR_TAMP2F
#endif
#if defined(RTC_ISR_TAMP3F)
| RTC_ISR_TAMP3F
#endif
);
isr = RTCD1.rtc->ISR;
RTCD1.rtc->ISR = isr & ~clear;
extiClearGroup1(EXTI_MASK1(STM32_RTC_TAMP_STAMP_EXTI));
if (RTCD1.callback != NULL) {
uint32_t cr, tcr;
cr = RTCD1.rtc->CR;
if ((cr & RTC_CR_TSIE) != 0U) {
if ((isr & RTC_ISR_TSF) != 0U) {
RTCD1.callback(&RTCD1, RTC_EVENT_TS);
}
if ((isr & RTC_ISR_TSOVF) != 0U) {
RTCD1.callback(&RTCD1, RTC_EVENT_TS_OVF);
}
}
/* This part is different depending on if the RTC has a TAMPCR or TAFCR
register.*/
#if defined(RTC_TAFCR_TAMP1E)
tcr = RTCD1.rtc->TAFCR;
if ((tcr & RTC_TAFCR_TAMPIE) != 0U) {
#if defined(RTC_ISR_TAMP1F)
if ((isr & RTC_ISR_TAMP1F) != 0U) {
RTCD1.callback(&RTCD1, RTC_EVENT_TAMP1);
}
#endif
#if defined(RTC_ISR_TAMP2F)
if ((isr & RTC_ISR_TAMP2F) != 0U) {
RTCD1.callback(&RTCD1, RTC_EVENT_TAMP2);
}
#endif
}
#else /* !defined(RTC_TAFCR_TAMP1E) */
tcr = RTCD1.rtc->TAMPCR;
#if defined(RTC_ISR_TAMP1F)
if (((tcr & RTC_TAMPCR_TAMP1IE) != 0U) &&
((isr & RTC_ISR_TAMP1F) != 0U)) {
RTCD1.callback(&RTCD1, RTC_EVENT_TAMP1);
}
#endif
#if defined(RTC_ISR_TAMP2F)
if (((tcr & RTC_TAMPCR_TAMP2IE) != 0U) &&
((isr & RTC_ISR_TAMP2F) != 0U)) {
RTCD1.callback(&RTCD1, RTC_EVENT_TAMP2);
}
#endif
#if defined(RTC_ISR_TAMP3F)
if (((tcr & RTC_TAMPCR_TAMP3IE) != 0U) &&
((isr & RTC_ISR_TAMP3F) != 0U)) {
RTCD1.callback(&RTCD1, RTC_EVENT_TAMP3);
}
#endif
#endif /* !defined(RTC_TAFCR_TAMP1E) */
}
OSAL_IRQ_EPILOGUE();
}
/**
* @brief RTC wakeup interrupt handler.
*
* @isr
*/
OSAL_IRQ_HANDLER(STM32_RTC_WKUP_HANDLER) {
uint32_t isr;
OSAL_IRQ_PROLOGUE();
isr = RTCD1.rtc->ISR;
RTCD1.rtc->ISR = isr & ~RTC_ISR_WUTF;
extiClearGroup1(EXTI_MASK1(STM32_RTC_WKUP_EXTI));
if (RTCD1.callback != NULL) {
uint32_t cr = RTCD1.rtc->CR;
if (((cr & RTC_CR_WUTIE) != 0U) && ((isr & RTC_ISR_WUTF) != 0U)) {
RTCD1.callback(&RTCD1, RTC_EVENT_WAKEUP);
}
}
OSAL_IRQ_EPILOGUE();
}
/**
* @brief RTC alarm interrupt handler.
*
* @isr
*/
OSAL_IRQ_HANDLER(STM32_RTC_ALARM_HANDLER) {
uint32_t isr, clear;
OSAL_IRQ_PROLOGUE();
clear = (0U
#if defined(RTC_ISR_ALRAF)
| RTC_ISR_ALRAF
#endif
#if defined(RTC_ISR_ALRBF)
| RTC_ISR_ALRBF
#endif
);
isr = RTCD1.rtc->ISR;
RTCD1.rtc->ISR = isr & ~clear;
extiClearGroup1(EXTI_MASK1(STM32_RTC_ALARM_EXTI));
if (RTCD1.callback != NULL) {
uint32_t cr = RTCD1.rtc->CR;
#if defined(RTC_ISR_ALRAF)
if (((cr & RTC_CR_ALRAIE) != 0U) && ((isr & RTC_ISR_ALRAF) != 0U)) {
RTCD1.callback(&RTCD1, RTC_EVENT_ALARM_A);
}
#endif
#if defined(RTC_ISR_ALRBF)
if (((cr & RTC_CR_ALRBIE) != 0U) && ((isr & RTC_ISR_ALRBF) != 0U)) {
RTCD1.callback(&RTCD1, RTC_EVENT_ALARM_B);
}
#endif
}
OSAL_IRQ_EPILOGUE();
}
#else
#error "missing required RTC handlers definitions in registry"
#endif
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
/**
* @brief Enable access to registers.
*
* @notapi
*/
void rtc_lld_init(void) {
/* RTC object initialization.*/
rtcObjectInit(&RTCD1);
/* RTC pointer initialization.*/
RTCD1.rtc = RTC;
/* Disable write protection. */
RTCD1.rtc->WPR = 0xCA;
RTCD1.rtc->WPR = 0x53;
/* If calendar has not been initialized yet then proceed with the
initial setup.*/
if (!(RTCD1.rtc->ISR & RTC_ISR_INITS)) {
rtc_enter_init();
RTCD1.rtc->CR = STM32_RTC_CR_INIT;
#if defined(RTC_TAFCR_TAMP1E)
RTCD1.rtc->TAFCR = STM32_RTC_TAMPCR_INIT;
#else
RTCD1.rtc->TAMPCR = STM32_RTC_TAMPCR_INIT;
#endif
RTCD1.rtc->ISR = RTC_ISR_INIT; /* Clearing all but RTC_ISR_INIT. */
RTCD1.rtc->PRER = STM32_RTC_PRER_BITS;
RTCD1.rtc->PRER = STM32_RTC_PRER_BITS;
rtc_exit_init();
}
else {
RTCD1.rtc->ISR &= ~RTC_ISR_RSF;
}
/* Callback initially disabled.*/
RTCD1.callback = NULL;
/* Enabling RTC-related EXTI lines.*/
extiEnableGroup1(EXTI_MASK1(STM32_RTC_ALARM_EXTI) |
EXTI_MASK1(STM32_RTC_TAMP_STAMP_EXTI) |
EXTI_MASK1(STM32_RTC_WKUP_EXTI),
EXTI_MODE_RISING_EDGE | EXTI_MODE_ACTION_INTERRUPT);
/* IRQ vectors permanently assigned to this driver.*/
STM32_RTC_IRQ_ENABLE();
}
/**
* @brief Set current time.
* @note Fractional part will be silently ignored. There is no possibility
* to set it on STM32 platform.
* @note The function can be called from any context.
*
* @param[in] rtcp pointer to RTC driver structure
* @param[in] timespec pointer to a @p RTCDateTime structure
*
* @notapi
*/
void rtc_lld_set_time(RTCDriver *rtcp, const RTCDateTime *timespec) {
uint32_t dr, tr;
syssts_t sts;
tr = rtc_encode_time(timespec);
dr = rtc_encode_date(timespec);
/* Entering a reentrant critical zone.*/
sts = osalSysGetStatusAndLockX();
/* Writing the registers.*/
rtc_enter_init();
rtcp->rtc->TR = tr;
rtcp->rtc->DR = dr;
rtcp->rtc->CR = (rtcp->rtc->CR & ~(1U << RTC_CR_BKP_OFFSET)) |
(timespec->dstflag << RTC_CR_BKP_OFFSET);
rtc_exit_init();
/* Leaving a reentrant critical zone.*/
osalSysRestoreStatusX(sts);
}
/**
* @brief Get current time.
* @note The function can be called from any context.
*
* @param[in] rtcp pointer to RTC driver structure
* @param[out] timespec pointer to a @p RTCDateTime structure
*
* @notapi
*/
void rtc_lld_get_time(RTCDriver *rtcp, RTCDateTime *timespec) {
uint32_t dr, tr, cr;
uint32_t subs;
#if STM32_RTC_HAS_SUBSECONDS
uint32_t ssr;
#endif /* STM32_RTC_HAS_SUBSECONDS */
syssts_t sts;
/* Entering a reentrant critical zone.*/
sts = osalSysGetStatusAndLockX();
/* Synchronization with the RTC and reading the registers, note
DR must be read last.*/
while ((rtcp->rtc->ISR & RTC_ISR_RSF) == 0)
;
#if STM32_RTC_HAS_SUBSECONDS
ssr = rtcp->rtc->SSR;
#endif /* STM32_RTC_HAS_SUBSECONDS */
tr = rtcp->rtc->TR;
dr = rtcp->rtc->DR;
cr = rtcp->rtc->CR;
rtcp->rtc->ISR &= ~RTC_ISR_RSF;
/* Leaving a reentrant critical zone.*/
osalSysRestoreStatusX(sts);
/* Decoding day time, this starts the atomic read sequence, see "Reading
the calendar" in the RTC documentation.*/
rtc_decode_time(tr, timespec);
/* If the RTC is capable of sub-second counting then the value is
normalized in milliseconds and added to the time.*/
#if STM32_RTC_HAS_SUBSECONDS
subs = (((STM32_RTC_PRESS_VALUE - 1U) - ssr) * 1000U) / STM32_RTC_PRESS_VALUE;
#else
subs = 0;
#endif /* STM32_RTC_HAS_SUBSECONDS */
timespec->millisecond += subs;
/* Decoding date, this concludes the atomic read sequence.*/
rtc_decode_date(dr, timespec);
/* Retrieving the DST bit.*/
timespec->dstflag = (cr >> RTC_CR_BKP_OFFSET) & 1;
}
#if (RTC_ALARMS > 0) || defined(__DOXYGEN__)
/**
* @brief Set alarm time.
* @note Default value after BKP domain reset for both comparators is 0.
* @note Function does not performs any checks of alarm time validity.
* @note The function can be called from any context.
*
* @param[in] rtcp pointer to RTC driver structure.
* @param[in] alarm alarm identifier. Can be 1 or 2.
* @param[in] alarmspec pointer to a @p RTCAlarm structure.
*
* @notapi
*/
void rtc_lld_set_alarm(RTCDriver *rtcp,
rtcalarm_t alarm,
const RTCAlarm *alarmspec) {
syssts_t sts;
/* Entering a reentrant critical zone.*/
sts = osalSysGetStatusAndLockX();
if (alarm == 0) {
if (alarmspec != NULL) {
rtcp->rtc->CR &= ~RTC_CR_ALRAE;
while (!(rtcp->rtc->ISR & RTC_ISR_ALRAWF))
;
rtcp->rtc->ALRMAR = alarmspec->alrmr;
rtcp->rtc->CR |= RTC_CR_ALRAE;
rtcp->rtc->CR |= RTC_CR_ALRAIE;
}
else {
rtcp->rtc->CR &= ~RTC_CR_ALRAIE;
rtcp->rtc->CR &= ~RTC_CR_ALRAE;
}
}
#if RTC_ALARMS > 1
else {
if (alarmspec != NULL) {
rtcp->rtc->CR &= ~RTC_CR_ALRBE;
while (!(rtcp->rtc->ISR & RTC_ISR_ALRBWF))
;
rtcp->rtc->ALRMBR = alarmspec->alrmr;
rtcp->rtc->CR |= RTC_CR_ALRBE;
rtcp->rtc->CR |= RTC_CR_ALRBIE;
}
else {
rtcp->rtc->CR &= ~RTC_CR_ALRBIE;
rtcp->rtc->CR &= ~RTC_CR_ALRBE;
}
}
#endif /* RTC_ALARMS > 1 */
/* Leaving a reentrant critical zone.*/
osalSysRestoreStatusX(sts);
}
/**
* @brief Get alarm time.
* @note The function can be called from any context.
*
* @param[in] rtcp pointer to RTC driver structure
* @param[in] alarm alarm identifier
* @param[out] alarmspec pointer to a @p RTCAlarm structure
*
* @notapi
*/
void rtc_lld_get_alarm(RTCDriver *rtcp,
rtcalarm_t alarm,
RTCAlarm *alarmspec) {
if (alarm == 0)
alarmspec->alrmr = rtcp->rtc->ALRMAR;
#if RTC_ALARMS > 1
else
alarmspec->alrmr = rtcp->rtc->ALRMBR;
#endif /* RTC_ALARMS > 1 */
}
#endif /* RTC_ALARMS > 0 */
/**
* @brief Enables or disables RTC callbacks.
* @details This function enables or disables callbacks, use a @p NULL pointer
* in order to disable a callback.
* @note The function can be called from any context.
*
* @param[in] rtcp pointer to RTC driver structure
* @param[in] callback callback function pointer or @p NULL
*
* @notapi
*/
void rtc_lld_set_callback(RTCDriver *rtcp, rtccb_t callback) {
rtcp->callback = callback;
}
#if STM32_RTC_HAS_PERIODIC_WAKEUPS || defined(__DOXYGEN__)
/**
* @brief Sets time of periodic wakeup.
* @note Default value after BKP domain reset is 0x0000FFFF
* @note The function can be called from any context.
*
* @param[in] rtcp pointer to RTC driver structure
* @param[in] wakeupspec pointer to a @p RTCWakeup structure
*
* @api
*/
void rtcSTM32SetPeriodicWakeup(RTCDriver *rtcp, const RTCWakeup *wakeupspec) {
syssts_t sts;
/* Entering a reentrant critical zone.*/
sts = osalSysGetStatusAndLockX();
if (wakeupspec != NULL) {
osalDbgCheck(wakeupspec->wutr != 0x30000);
rtcp->rtc->CR &= ~RTC_CR_WUTE;
rtcp->rtc->CR &= ~RTC_CR_WUTIE;
while (!(rtcp->rtc->ISR & RTC_ISR_WUTWF))
;
rtcp->rtc->WUTR = wakeupspec->wutr & 0xFFFF;
rtcp->rtc->CR &= ~RTC_CR_WUCKSEL;
rtcp->rtc->CR |= (wakeupspec->wutr >> 16) & RTC_CR_WUCKSEL;
rtcp->rtc->CR |= RTC_CR_WUTIE;
rtcp->rtc->CR |= RTC_CR_WUTE;
}
else {
rtcp->rtc->CR &= ~RTC_CR_WUTE;
rtcp->rtc->CR &= ~RTC_CR_WUTIE;
}
/* Leaving a reentrant critical zone.*/
osalSysRestoreStatusX(sts);
}
/**
* @brief Gets time of periodic wakeup.
* @note Default value after BKP domain reset is 0x0000FFFF
* @note The function can be called from any context.
*
* @param[in] rtcp pointer to RTC driver structure
* @param[out] wakeupspec pointer to a @p RTCWakeup structure
*
* @api
*/
void rtcSTM32GetPeriodicWakeup(RTCDriver *rtcp, RTCWakeup *wakeupspec) {
syssts_t sts;
/* Entering a reentrant critical zone.*/
sts = osalSysGetStatusAndLockX();
wakeupspec->wutr = 0;
wakeupspec->wutr |= rtcp->rtc->WUTR;
wakeupspec->wutr |= (((uint32_t)rtcp->rtc->CR) & 0x7) << 16;
/* Leaving a reentrant critical zone.*/
osalSysRestoreStatusX(sts);
}
#endif /* STM32_RTC_HAS_PERIODIC_WAKEUPS */
#endif /* HAL_USE_RTC */
/** @} */
|