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
|
/*
ChibiOS - Copyright (C) 2006..2015 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 STM32/RTCv1/rtc_lld.c
* @brief STM32 RTC subsystem low level driver header.
*
* @addtogroup RTC
* @{
*/
#include "hal.h"
#if HAL_USE_RTC || defined(__DOXYGEN__)
/*===========================================================================*/
/* Driver local definitions. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver exported variables. */
/*===========================================================================*/
/**
* @brief RTC driver identifier.
*/
RTCDriver RTCD1;
/*===========================================================================*/
/* Driver local variables and types. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local functions. */
/*===========================================================================*/
/**
* @brief Wait for synchronization of RTC registers with APB1 bus.
* @details This function must be invoked before trying to read RTC registers
* in the backup domain: DIV, CNT, ALR. CR registers can always
* be read.
*
* @notapi
*/
static void rtc_apb1_sync(void) {
while ((RTC->CRL & RTC_CRL_RSF) == 0)
;
}
/**
* @brief Wait for for previous write operation complete.
* @details This function must be invoked before writing to any RTC registers
*
* @notapi
*/
static void rtc_wait_write_completed(void) {
while ((RTC->CRL & RTC_CRL_RTOFF) == 0)
;
}
/**
* @brief Acquires write access to RTC registers.
* @details Before writing to the backup domain RTC registers the previous
* write operation must be completed. Use this function before
* writing to PRL, CNT, ALR registers.
*
* @notapi
*/
static void rtc_acquire_access(void) {
rtc_wait_write_completed();
RTC->CRL |= RTC_CRL_CNF;
}
/**
* @brief Releases write access to RTC registers.
*
* @notapi
*/
static void rtc_release_access(void) {
RTC->CRL &= ~RTC_CRL_CNF;
}
/**
* @brief Converts time from timespec to seconds counter.
*
* @param[in] timespec pointer to a @p RTCDateTime structure
* @return the TR register encoding.
*
* @notapi
*/
static time_t rtc_encode(const RTCDateTime *timespec) {
struct tm tim;
rtcConvertDateTimeToStructTm(timespec, &tim, NULL);
return mktime(&tim);
}
/**
* @brief Converts time from seconds/milliseconds to timespec.
*
* @param[in] tv_sec seconds value
* @param[in] tv_msec milliseconds value
* @param[out] timespec pointer to a @p RTCDateTime structure
*
* @notapi
*/
static void rtc_decode(uint32_t tv_sec, uint32_t tv_msec,
RTCDateTime *timespec) {
struct tm tim;
struct tm *canary;
/* If the conversion is successful the function returns a pointer
to the object the result was written into.*/
canary = localtime_r((time_t *)&(tv_sec), &tim);
osalDbgCheck(&tim == canary);
rtcConvertStructTmToDateTime(&tim, tv_msec, timespec);
}
/*===========================================================================*/
/* Driver interrupt handlers. */
/*===========================================================================*/
/**
* @brief RTC interrupt handler.
*
* @isr
*/
OSAL_IRQ_HANDLER(STM32_RTC1_HANDLER) {
uint16_t flags;
OSAL_IRQ_PROLOGUE();
/* Code hits this wait only when AHB1 bus was previously powered off by any
reason (standby, reset, etc). In other cases there is no waiting.*/
rtc_apb1_sync();
/* Mask of all enabled and pending sources.*/
flags = RTCD1.rtc->CRH & RTCD1.rtc->CRL;
RTCD1.rtc->CRL &= ~(RTC_CRL_SECF | RTC_CRL_ALRF | RTC_CRL_OWF);
if (flags & RTC_CRL_SECF)
RTCD1.callback(&RTCD1, RTC_EVENT_SECOND);
if (flags & RTC_CRL_ALRF)
RTCD1.callback(&RTCD1, RTC_EVENT_ALARM);
if (flags & RTC_CRL_OWF)
RTCD1.callback(&RTCD1, RTC_EVENT_OVERFLOW);
OSAL_IRQ_EPILOGUE();
}
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
/**
* @brief Load value of RTCCLK to prescaler registers.
* @note The pre-scaler must not be set on every reset as RTC clock
* counts are lost when it is set.
* @note This function designed to be called from
* hal_lld_backup_domain_init(). Because there is only place
* where possible to detect BKP domain reset event reliably.
*
* @notapi
*/
void rtc_lld_set_prescaler(void) {
syssts_t sts;
/* Entering a reentrant critical zone.*/
sts = chSysGetStatusAndLockX();
rtc_acquire_access();
RTC->PRLH = (uint16_t)((STM32_RTCCLK - 1) >> 16) & 0x000F;
RTC->PRLL = (uint16_t)(((STM32_RTCCLK - 1)) & 0xFFFF);
rtc_release_access();
/* Leaving a reentrant critical zone.*/
chSysRestoreStatusX(sts);
}
/**
* @brief Initialize RTC.
*
* @notapi
*/
void rtc_lld_init(void) {
/* RTC object initialization.*/
rtcObjectInit(&RTCD1);
/* RTC pointer initialization.*/
RTCD1.rtc = RTC;
/* RSF bit must be cleared by software after an APB1 reset or an APB1 clock
stop. Otherwise its value will not be actual. */
RTCD1.rtc->CRL &= ~RTC_CRL_RSF;
/* Required because access to PRL.*/
rtc_apb1_sync();
/* All interrupts initially disabled.*/
rtc_wait_write_completed();
RTCD1.rtc->CRH = 0;
/* Callback initially disabled.*/
RTCD1.callback = NULL;
/* IRQ vector permanently assigned to this driver.*/
nvicEnableVector(STM32_RTC1_NUMBER, STM32_RTC_IRQ_PRIORITY);
}
/**
* @brief Set current time.
* @note Fractional part will be silently ignored. There is no possibility
* to change it on STM32F1xx 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 RTCTime structure
*
* @notapi
*/
void rtc_lld_set_time(RTCDriver *rtcp, const RTCDateTime *timespec) {
time_t tv_sec = rtc_encode(timespec);
rtcSTM32SetSec(rtcp, tv_sec);
}
/**
* @brief Get current time.
* @note The function can be called from any context.
*
* @param[in] rtcp pointer to RTC driver structure
* @param[in] timespec pointer to a @p RTCTime structure
*
* @notapi
*/
void rtc_lld_get_time(RTCDriver *rtcp, RTCDateTime *timespec) {
uint32_t tv_sec, tv_msec;
rtcSTM32GetSecMsec(rtcp, &tv_sec, &tv_msec);
rtc_decode(tv_sec, tv_msec, timespec);
}
/**
* @brief Set alarm time.
*
* @note Default value after BKP domain reset is 0xFFFFFFFF
* @note The function can be called from any context.
*
* @param[in] rtcp pointer to RTC driver structure
* @param[in] alarm alarm identifier
* @param[in] alarmspec pointer to a @p RTCAlarm structure
*
* @notapi
*/
void rtc_lld_set_alarm(RTCDriver *rtcp,
rtcalarm_t alarm_number,
const RTCAlarm *alarmspec) {
syssts_t sts;
(void)alarm_number;
/* Entering a reentrant critical zone.*/
sts = chSysGetStatusAndLockX();
rtc_acquire_access();
if (alarmspec != NULL) {
rtcp->rtc->ALRH = (uint16_t)(alarmspec->tv_sec >> 16);
rtcp->rtc->ALRL = (uint16_t)(alarmspec->tv_sec & 0xFFFF);
}
else {
rtcp->rtc->ALRH = 0;
rtcp->rtc->ALRL = 0;
}
rtc_release_access();
/* Leaving a reentrant critical zone.*/
chSysRestoreStatusX(sts);
}
/**
* @brief Get current alarm.
* @note If an alarm has not been set then the returned alarm specification
* is not meaningful.
* @note The function can be called from any context.
* @note Default value after BKP domain reset is 0xFFFFFFFF.
*
* @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_number,
RTCAlarm *alarmspec) {
syssts_t sts;
(void)alarm_number;
/* Entering a reentrant critical zone.*/
sts = chSysGetStatusAndLockX();
/* Required because access to ALR.*/
rtc_apb1_sync();
alarmspec->tv_sec = ((rtcp->rtc->ALRH << 16) + rtcp->rtc->ALRL);
/* Leaving a reentrant critical zone.*/
chSysRestoreStatusX(sts);
}
/**
* @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) {
syssts_t sts;
/* Entering a reentrant critical zone.*/
sts = chSysGetStatusAndLockX();
if (callback != NULL) {
/* IRQ sources enabled only after setting up the callback.*/
rtcp->callback = callback;
rtc_wait_write_completed();
rtcp->rtc->CRL &= ~(RTC_CRL_OWF | RTC_CRL_ALRF | RTC_CRL_SECF);
rtcp->rtc->CRH = RTC_CRH_OWIE | RTC_CRH_ALRIE | RTC_CRH_SECIE;
}
else {
rtc_wait_write_completed();
rtcp->rtc->CRH = 0;
/* Callback set to NULL only after disabling the IRQ sources.*/
rtcp->callback = NULL;
}
/* Leaving a reentrant critical zone.*/
chSysRestoreStatusX(sts);
}
/**
* @brief Get seconds and (optionally) milliseconds from RTC.
* @note The function can be called from any context.
*
* @param[in] rtcp pointer to RTC driver structure
* @param[out] tv_sec pointer to seconds value
* @param[out] tv_msec pointer to milliseconds value, set it
* to @p NULL if not needed
*
* @api
*/
void rtcSTM32GetSecMsec(RTCDriver *rtcp, uint32_t *tv_sec, uint32_t *tv_msec) {
uint32_t time_frac;
syssts_t sts;
osalDbgCheck((NULL != tv_sec) && (NULL != rtcp));
/* Entering a reentrant critical zone.*/
sts = chSysGetStatusAndLockX();
/* Required because access to CNT and DIV.*/
rtc_apb1_sync();
/* Loops until two consecutive read returning the same value.*/
do {
*tv_sec = ((uint32_t)(rtcp->rtc->CNTH) << 16) + rtcp->rtc->CNTL;
time_frac = (((uint32_t)rtcp->rtc->DIVH) << 16) + (uint32_t)rtcp->rtc->DIVL;
} while ((*tv_sec) != (((uint32_t)(rtcp->rtc->CNTH) << 16) + rtcp->rtc->CNTL));
/* Leaving a reentrant critical zone.*/
chSysRestoreStatusX(sts);
if (NULL != tv_msec)
*tv_msec = (((uint32_t)STM32_RTCCLK - 1 - time_frac) * 1000) / STM32_RTCCLK;
}
/**
* @brief Set seconds in RTC.
* @note The function can be called from any context.
*
* @param[in] rtcp pointer to RTC driver structure
* @param[in] tv_sec seconds value
*
* @api
*/
void rtcSTM32SetSec(RTCDriver *rtcp, uint32_t tv_sec) {
syssts_t sts;
osalDbgCheck(NULL != rtcp);
/* Entering a reentrant critical zone.*/
sts = chSysGetStatusAndLockX();
rtc_acquire_access();
rtcp->rtc->CNTH = (uint16_t)(tv_sec >> 16);
rtcp->rtc->CNTL = (uint16_t)(tv_sec & 0xFFFF);
rtc_release_access();
/* Leaving a reentrant critical zone.*/
chSysRestoreStatusX(sts);
}
#endif /* HAL_USE_RTC */
/** @} */
|