-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSTM32H7RTC.cpp
270 lines (243 loc) · 8.14 KB
/
STM32H7RTC.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
#include "STM32H7RTC.h"
STM32RTC::STM32RTC()
{
}
//-------------------Library Functions----------------------------------
RTC_HandleTypeDef STM32RTC::begin(uint8_t _format, bool _resetRtc)
{
//RCC_PeriphCLKInitTypeDef PeriphClkInit;
//enableClock(LSE_CLOCK);
//PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
//PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
//if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
// Error_Handler();
//}
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
Error_Handler();
}
__HAL_RCC_RTC_ENABLE();
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
hrtc.Instance = RTC;
hrtc.Init.HourFormat = _format;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
if (!_resetRtc && isTimeSet()) return hrtc;
enableBackupDomain();
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_INDEX, 0);
sTime.Hours = 0;
sTime.Minutes = 0;
sTime.Seconds = 0;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT) != HAL_OK)
{
Error_Handler();
}
sDate.WeekDay = RTC_WEEKDAY_FRIDAY;
sDate.Month = RTC_MONTH_JANUARY;
sDate.Date = 1;
sDate.Year = 21;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT) != HAL_OK)
{
Error_Handler();
}
return hrtc;
}
void STM32RTC::setTime(uint8_t _h, uint8_t _m, uint8_t _s, uint32_t _ss, uint8_t _pmAm, uint32_t _dayLightSaving)
{
RTC_TimeTypeDef myTime = {0};
myTime.Hours = _h;
myTime.Minutes = _m;
myTime.Seconds = _s;
myTime.SubSeconds = _ss;
myTime.TimeFormat = _pmAm;
myTime.DayLightSaving = _dayLightSaving;
myTime.StoreOperation = RTC_STOREOPERATION_RESET;
HAL_RTC_SetTime(&hrtc, &myTime, RTC_FORMAT);
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_INDEX, RTC_BKP_VALUE);
}
RTC_TimeTypeDef STM32RTC::getTime(uint8_t *_h, uint8_t *_m, uint8_t *_s, uint32_t *_ss, uint8_t *_pmAm, uint32_t *_dayLightSaving)
{
RTC_TimeTypeDef myTime = {0};
RTC_DateTypeDef myDate = {0};
HAL_RTC_GetTime(&hrtc, &myTime, RTC_FORMAT);
HAL_RTC_GetDate(&hrtc, &myDate, RTC_FORMAT);
*_h = myTime.Hours;
*_m = myTime.Minutes;
*_s = myTime.Seconds;
if(_ss != NULL) *_ss = myTime.SubSeconds;
if(_pmAm != NULL) *_pmAm = myTime.TimeFormat;
if(_pmAm != NULL) *_dayLightSaving = myTime.DayLightSaving;
return myTime;
}
RTC_TimeTypeDef STM32RTC::getTime()
{
uint8_t dummy1;
uint32_t dummy2;
return getTime(&dummy1, &dummy1, &dummy1, &dummy2, &dummy1, &dummy2);
}
void STM32RTC::setDate(uint8_t _d, uint8_t _m, uint16_t _y, uint8_t _weekday)
{
RTC_DateTypeDef myDate = {0};
myDate.WeekDay = _weekday;
myDate.Month = _m;
myDate.Date = _d;
myDate.Year = (uint8_t)(_y % 100);
HAL_RTC_SetDate(&hrtc, &myDate, RTC_FORMAT);
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_INDEX, RTC_BKP_VALUE);
}
RTC_DateTypeDef STM32RTC::getDate(uint8_t *_d, uint8_t *_m, uint8_t *_y, uint8_t *_weekday)
{
RTC_DateTypeDef myDate = {0};
HAL_RTC_GetDate(&hrtc, &myDate, RTC_FORMAT);
*_weekday = myDate.WeekDay;
*_m = myDate.Month;
*_d = myDate.Date;
*_y = myDate.Year;
return myDate;
}
RTC_DateTypeDef STM32RTC::getDate()
{
uint8_t dummy;
return getDate(&dummy, &dummy, &dummy, &dummy);
}
void STM32RTC::enableAlarm(uint8_t _d, uint8_t _h, uint8_t _m, uint8_t _s, uint32_t _alarm, uint32_t _alarmMask, uint8_t _pmAm, uint32_t _dayLightSaving)
{
RTC_AlarmTypeDef myAlarm = {0};
myAlarm.AlarmTime.Hours = _h;
myAlarm.AlarmTime.Minutes = _m;
myAlarm.AlarmTime.Seconds = _s;
myAlarm.AlarmTime.TimeFormat = _pmAm;
myAlarm.AlarmTime.DayLightSaving = _dayLightSaving;
myAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
myAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
myAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
myAlarm.AlarmDateWeekDay = _d;
myAlarm.Alarm = _alarm;
myAlarm.AlarmMask = _alarmMask;
HAL_RTC_SetAlarm_IT(&hrtc, &myAlarm, RTC_FORMAT);
}
void STM32RTC::enableAlarmInterrupt(void (*_f)())
{
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
userFcn = _f;
}
void STM32RTC::disableAlarm(uint32_t _alarm)
{
HAL_RTC_DeactivateAlarm(&hrtc, _alarm);
}
void STM32RTC::disableAlarmInterrupt()
{
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_13);
HAL_NVIC_DisableIRQ(RTC_Alarm_IRQn);
userFcn = NULL;
}
void STM32RTC::enableSimpleAlarm(uint8_t _d, uint8_t _h, uint8_t _m, uint8_t _s, uint32_t _alarm, uint32_t _alarmMask, uint8_t _pmAm, uint32_t _dayLightSaving)
{
RTC_AlarmTypeDef myAlarm = {0};
myAlarm.AlarmTime.Hours = _h;
myAlarm.AlarmTime.Minutes = _m;
myAlarm.AlarmTime.Seconds = _s;
myAlarm.AlarmTime.TimeFormat = _pmAm;
myAlarm.AlarmTime.DayLightSaving = _dayLightSaving;
myAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
myAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
myAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
myAlarm.AlarmDateWeekDay = _d;
myAlarm.Alarm = _alarm;
myAlarm.AlarmMask = _alarmMask;
HAL_RTC_SetAlarm(&hrtc, &myAlarm, RTC_FORMAT);
}
bool STM32RTC::checkForAlarm(bool _clearFlag)
{
bool _r = (__HAL_RTC_ALARM_GET_FLAG(&hrtc, RTC_FLAG_ALRAF)) == 0 ? false : true;
if (_r && _clearFlag) __HAL_RTC_ALARM_CLEAR_FLAG(&hrtc, RTC_FLAG_ALRAF);
return _r;
}
RTC_AlarmTypeDef STM32RTC::getAlarm(uint8_t *_d, uint8_t *_h, uint8_t *_m, uint8_t *_s, uint32_t _alarm, uint32_t *_alarmMask, uint8_t *_pmAm, uint32_t *_dayLightSaving)
{
RTC_AlarmTypeDef myAlarm = {0};
HAL_RTC_GetAlarm(&hrtc, &myAlarm, _alarm, RTC_FORMAT);
*_d = myAlarm.AlarmDateWeekDay;
*_h = myAlarm.AlarmTime.Hours;
*_m = myAlarm.AlarmTime.Minutes;
*_s = myAlarm.AlarmTime.Seconds;
*_alarmMask = myAlarm.AlarmMask;
*_pmAm = myAlarm.AlarmTime.TimeFormat;
*_dayLightSaving = myAlarm.AlarmTime.DayLightSaving;
return myAlarm;
}
RTC_AlarmTypeDef STM32RTC::getAlarm()
{
uint8_t dummy1;
uint32_t dummy2;
return getAlarm(&dummy1, &dummy1, &dummy1, &dummy1, dummy2, &dummy2, &dummy1, &dummy2);
}
//RTC_OUTPUT_ALARMA, RTC_OUTPUT_ALARMB or RTC_OUTPUT_DISABLE for _alarm
void STM32RTC::setAlarmOutput(bool _outEn, uint32_t _alarm)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if (_outEn)
{
// PC13 is output from RTC
__HAL_RCC_GPIOC_CLK_ENABLE();
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
hrtc.Init.OutPut = _alarm;
// You can change impulse polarity here. By default, on alarm event output will be pulled to high.
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_PUSHPULL;
}
else
{
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_13);
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_PUSHPULL;
}
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
Error_Handler();
}
}
bool STM32RTC::isTimeSet()
{
return (HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_INDEX) == RTC_BKP_VALUE)?true:false;
}
extern "C" void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
UNUSED(hrtc);
if (userFcn != NULL) (*userFcn)();
}
extern "C" void RTC_Alarm_IRQHandler(void)
{
HAL_RTC_AlarmIRQHandler(&hrtc);
}
extern "C" void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
{
if (hrtc->Instance == RTC)
{
__HAL_RCC_RTC_ENABLE();
}
}
extern "C" void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
{
if (hrtc->Instance == RTC)
{
__HAL_RCC_RTC_DISABLE();
}
}
//-------------------End of Library Functions----------------------------------