-
Notifications
You must be signed in to change notification settings - Fork 11
/
CatenaStm32L0Rtc.cpp
494 lines (407 loc) · 10 KB
/
CatenaStm32L0Rtc.cpp
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
/*
Module: CatenaStm32L0Rtc.cpp
Function:
Class CatenaStm32L0Rtc
Copyright notice:
See accompanying LICENSE file.
Author:
ChaeHee Won, MCCI Corporation October 2017
*/
#ifdef ARDUINO_ARCH_STM32
#include "CatenaStm32L0Rtc.h"
using namespace McciCatena;
/****************************************************************************\
|
| Manifest constants & typedefs.
|
\****************************************************************************/
/****************************************************************************\
|
| Read-only data.
|
\****************************************************************************/
/****************************************************************************\
|
| Variables.
|
\****************************************************************************/
/****************************************************************************\
|
| Overrides for "weak" definitions in core HAL.
|
\****************************************************************************/
extern "C" {
static volatile uint32_t *gs_pAlarm;
static RTC_HandleTypeDef *gs_phRtc;
void RTC_IRQHandler(void)
{
HAL_RTC_AlarmIRQHandler(gs_phRtc);
}
void HAL_RTC_AlarmAEventCallback(
RTC_HandleTypeDef * hRtc
)
{
if (gs_pAlarm)
*gs_pAlarm = 1;
}
void HAL_RTC_MspInit(
RTC_HandleTypeDef * hRtc
)
{
if (hRtc->Instance == RTC)
{
/* USER CODE BEGIN RTC_MspInit 0 */
/* USER CODE END RTC_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_RTC_ENABLE();
/* USER CODE BEGIN RTC_MspInit 1 */
HAL_NVIC_SetPriority(RTC_IRQn, TICK_INT_PRIORITY, 0U);
HAL_NVIC_EnableIRQ(RTC_IRQn);
/* USER CODE END RTC_MspInit 1 */
}
}
void HAL_RTC_MspDeInit(
RTC_HandleTypeDef * hRtc
)
{
if (hRtc->Instance == RTC)
{
/* USER CODE BEGIN RTC_MspDeInit 0 */
HAL_NVIC_DisableIRQ(RTC_IRQn);
/* USER CODE END RTC_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_RTC_DISABLE();
/* USER CODE BEGIN RTC_MspDeInit 1 */
/* USER CODE END RTC_MspDeInit 1 */
}
}
uint32_t HAL_AddTick(
uint32_t delta
)
{
extern __IO uint32_t uwTick;
// copy old interrupt-enable state to flags.
uint32_t const flags = __get_PRIMASK();
// disable interrupts
__set_PRIMASK(1);
// observe uwTick, and advance it.
uint32_t const tickCount = uwTick + delta;
// save uwTick
uwTick = tickCount;
// restore interrupts (does nothing if ints were disabled on entry)
__set_PRIMASK(flags);
// return the new value of uwTick.
return tickCount;
}
} /* extern "C" */
bool CatenaStm32L0Rtc::begin(bool fResetTime)
{
RTC_TimeTypeDef Time;
RTC_DateTypeDef Date;
// memset(&this->m_hRtc, 0, sizeof(this->m_hRtc));
this->m_hRtc.Instance = RTC;
this->m_hRtc.Init.HourFormat = RTC_HOURFORMAT_24;
// this->m_hRtc.Init.AsynchPrediv = 37 - 1; /* 37kHz / 37 = 1000Hz */
// this->m_hRtc.Init.SynchPrediv = 1000 - 1; /* 1000Hz / 1000 = 1Hz */
// this->m_hRtc.Init.AsynchPrediv = 64 - 1; /* 40kHz / 64 = 625Hz */
// this->m_hRtc.Init.SynchPrediv = 625 - 1; /* 625Hz / 625 = 1Hz */
this->m_hRtc.Init.AsynchPrediv = 128 - 1; /* 32768Hz / 128 = 256Hz */
// this->m_hRtc.Init.SynchPrediv = 256 - 1; /* 256Hz / 256 = 1Hz */
this->m_hRtc.Init.SynchPrediv = 256; /* 256Hz / 256 = 1Hz */
this->m_hRtc.Init.OutPut = RTC_OUTPUT_DISABLE;
this->m_hRtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
this->m_hRtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
this->m_hRtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
gs_phRtc = &this->m_hRtc;
if (HAL_RTC_Init(&this->m_hRtc) != HAL_OK)
{
Serial.println("HAL_RTC_Init() failed");
return false;
}
/* Initialize RTC and set the Time and Date */
if (fResetTime ||
HAL_RTCEx_BKUPRead(&this->m_hRtc, RTC_BKP_DR0) != 0x32F2)
{
Time.Hours = 0x0;
Time.Minutes = 0x0;
Time.Seconds = 0x0;
Time.SubSeconds = 0x0;
Time.TimeFormat = RTC_HOURFORMAT12_AM;
Time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
Time.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(
&this->m_hRtc,
&Time,
RTC_FORMAT_BIN
) != HAL_OK)
{
Serial.println("HAL_RTC_SetTime() failed");
return false;
}
/* Sunday 1st January 2017 */
Date.WeekDay = RTC_WEEKDAY_SUNDAY;
Date.Month = RTC_MONTH_JANUARY;
Date.Date = 0x1;
Date.Year = 0x0;
if (HAL_RTC_SetDate(
&this->m_hRtc,
&Date,
RTC_FORMAT_BIN
) != HAL_OK)
{
Serial.println("HAL_RTC_SetDate() failed");
return false;
}
HAL_RTCEx_BKUPWrite(&this->m_hRtc, RTC_BKP_DR0, 0x32F2);
}
/* Enable Direct Read of the calendar registers (not through Shadow) */
HAL_RTCEx_EnableBypassShadow(&this->m_hRtc);
HAL_RTC_DeactivateAlarm(&this->m_hRtc, RTC_ALARM_A);
return true;
}
const uint16_t CatenaStm32L0Rtc::md[13] =
{
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
};
CatenaStm32L0Rtc::CalendarTime
CatenaStm32L0Rtc::GetTime(void)
{
CalendarTime Result;
RTC_TimeTypeDef Time;
RTC_DateTypeDef Date;
uint32_t SubSeconds;
do {
HAL_RTC_GetTime(&this->m_hRtc, &Time, RTC_FORMAT_BIN);
SubSeconds = Time.SubSeconds;
HAL_RTC_GetDate(&this->m_hRtc, &Date, RTC_FORMAT_BIN);
HAL_RTC_GetTime(&this->m_hRtc, &Time, RTC_FORMAT_BIN);
} while (SubSeconds != Time.SubSeconds);
Result.Second = Time.Seconds;
Result.Minute = Time.Minutes;
Result.Hour = Time.Hours;
Result.Day = Date.Date;
Result.Month = Date.Month;
Result.Year = Date.Year;
return Result;
}
bool CatenaStm32L0Rtc::SetTime(const CalendarTime *pNow)
{
RTC_TimeTypeDef Time;
RTC_DateTypeDef Date;
Time.Hours = pNow->Hour;
Time.Minutes = pNow->Minute;
Time.Seconds = pNow->Second;
Time.SubSeconds = 0x0;
Time.TimeFormat = RTC_HOURFORMAT12_AM;
Time.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
Time.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&this->m_hRtc, &Time, RTC_FORMAT_BIN) != HAL_OK)
{
Serial.println("HAL_RTC_SetTime() failed");
return false;
}
Date.WeekDay = RTC_WEEKDAY_MONDAY; /* don't care */
Date.Month = pNow->Month;
Date.Date = pNow->Day;
Date.Year = pNow->Year;
if (HAL_RTC_SetDate(&this->m_hRtc, &Date, RTC_FORMAT_BIN) != HAL_OK)
{
Serial.println("HAL_RTC_SetDate() failed");
return false;
}
return true;
}
void CatenaStm32L0Rtc::SetAlarm(uint32_t delta)
{
CalendarTime now = GetTime();
now.Advance(delta);
SetAlarm(&now);
}
void CatenaStm32L0Rtc::SetAlarm(const CalendarTime *pNow)
{
RTC_AlarmTypeDef Alarm;
this->m_fAlarmEnabled = true;
this->m_Alarm = 0;
gs_pAlarm = &this->m_Alarm;
// Serial.print("SetAlarm: ");
// Serial.print(pNow->Year + CATENA_STM32L0_RTC_BASE_YEAR);
// Serial.print("-");
// Serial.print(pNow->Month);
// Serial.print("-");
// Serial.print(pNow->Day);
// Serial.print(" ");
// Serial.print(pNow->Hour);
// Serial.print(":");
// Serial.print(pNow->Minute);
// Serial.print(":");
// Serial.println(pNow->Second);
/* Set Alarm with calculated values*/
Alarm.AlarmTime.Hours = pNow->Hour;
Alarm.AlarmTime.Minutes = pNow->Minute;
Alarm.AlarmTime.Seconds = pNow->Second;
Alarm.AlarmTime.SubSeconds = 0;
Alarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
Alarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
Alarm.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM;
Alarm.AlarmDateWeekDay = pNow->Day;
Alarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
Alarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
Alarm.AlarmMask = RTC_ALARMMASK_NONE;
Alarm.Alarm = RTC_ALARM_A;
/* Set RTC_Alarm */
HAL_RTC_SetAlarm_IT(&this->m_hRtc, &Alarm, RTC_FORMAT_BIN);
}
bool CatenaStm32L0Rtc::PollAlarmState(void)
{
if (this->m_Alarm)
{
this->m_fAlarmEnabled = false;
return true;
}
return false;
}
void CatenaStm32L0Rtc::SleepForAlarm(
CatenaStm32L0Rtc::SleepMode howSleep
)
{
uint32_t nWakes;
if (! this->m_fAlarmEnabled || this->m_Alarm)
return;
HAL_SuspendTick();
/* we may want to try deep sleep, maybe not */
nWakes = 0;
switch (howSleep)
{
default:
case SleepMode::SleepWithRegulator:
while (! m_Alarm)
{
++nWakes;
HAL_PWR_EnterSLEEPMode(
PWR_MAINREGULATOR_ON,
PWR_SLEEPENTRY_WFI
);
}
break;
case SleepMode::SleepWithLowPowerRegulator:
while (! m_Alarm)
{
++nWakes;
HAL_PWR_EnterSLEEPMode(
PWR_LOWPOWERREGULATOR_ON,
PWR_SLEEPENTRY_WFI
);
}
break;
case SleepMode::StopWithRegulator:
while (! m_Alarm)
{
++nWakes;
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
HAL_PWR_EnterSTOPMode(
PWR_MAINREGULATOR_ON,
PWR_SLEEPENTRY_WFI
);
}
break;
case SleepMode::StopWithLowPowerRegulator:
while (! m_Alarm)
{
++nWakes;
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
HAL_PWR_EnterSTOPMode(
PWR_LOWPOWERREGULATOR_ON,
PWR_SLEEPENTRY_WFI
);
}
break;
case SleepMode::Standby:
while (! m_Alarm)
{
++nWakes;
HAL_PWR_EnterSTANDBYMode();
}
break;
}
HAL_IncTick();
HAL_ResumeTick();
// Serial.print("SleepForAlarm: ");
// Serial.println(nWakes);
}
uint32_t CatenaStm32L0Rtc::AdjustMillisForward(uint32_t delta)
{
return HAL_AddTick(delta);
}
/*
|| assumes that we're only advancing by a day or so -- anything more
|| than a day is treated as a day.
*/
bool
CatenaStm32L0Rtc::CalendarTime::Advance(
uint32_t delta
)
{
uint32_t d;
bool result;
result = true;
if (delta > 86400) // everyone knows that one day is 86400 secs,
{ // right?
delta = 86400;
result = false;
}
d = delta % 60;
delta /= 60;
Second += d;
if (Second > 59)
{
delta += Second / 60;
Second %= 60;
}
if (delta == 0)
return result;
d = delta % 60;
delta /= 60;
Minute += d;
if (Minute > 59)
{
delta += Minute / 60;
Minute %= 60;
}
if (delta == 0)
return result;
d = delta % 24;
delta /= 24;
Hour += d;
if (Hour > 23)
{
delta += Hour / 24;
Hour %= 24;
}
if (delta == 0)
return result;
uint8_t leapYear = (Year & 0x3) ? 0 : 1;
uint8_t firstDay = CatenaStm32L0Rtc::md[Month - 1];
uint8_t lastDay = CatenaStm32L0Rtc::md[Month];
if (leapYear)
{
if (Month >= 2)
{
++lastDay;
if (Month >= 3)
++firstDay;
}
}
// now we just propagate carry
++Day;
if (Day <= lastDay)
return result;
Day = 1;
++Month;
if (Month <= 12)
return result;
Month = 1;
++Year;
return result;
}
#endif // ARDUINO_ARCH_STM32
/**** end of CatenaStm32L0Rtc.cpp ****/