semf
stm32rtc.cpp
Go to the documentation of this file.
1
12
13#if defined(STM32) && defined(HAL_RTC_MODULE_ENABLED)
14namespace semf
15{
16Stm32Rtc::Stm32Rtc(RTC_HandleTypeDef& hrtc)
17: m_hrtc(&hrtc)
18{
19}
20
22{
23#if defined(STM32F1)
24 return 0;
25#else
26 auto time = halTime();
27 return static_cast<uint16_t>(1000 * (time.SecondFraction - time.SubSeconds) / (time.SecondFraction + 1));
28#endif
29}
30
31void Stm32Rtc::setMillisecond(uint16_t millisecond)
32{
33 SEMF_INFO("set milisecond %u", millisecond);
34 RTC_TimeTypeDef hal = halTime();
35#if !defined(STM32F0) && !defined(STM32F1)
36 hal.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
37 hal.StoreOperation = RTC_STOREOPERATION_RESET;
38#endif
39 millisecond %= 1000;
40#if !defined(STM32F1)
41 hal.SubSeconds = hal.SecondFraction - (hal.SecondFraction + 1) * millisecond / 1000;
42#endif
43 HAL_StatusTypeDef state = HAL_RTC_SetTime(m_hrtc, &hal, RTC_FORMAT_BIN);
44 if (state != HAL_OK)
45 {
46 if (state == HAL_ERROR)
47 {
48 SEMF_ERROR("hal error");
49 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetMillisecond_HalError)));
50 }
51 else if (state == HAL_BUSY)
52 {
53 SEMF_ERROR("hal busy");
54 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetMillisecond_HalBusy)));
55 }
56 else if (state == HAL_TIMEOUT)
57 {
58 SEMF_ERROR("hal timeout");
59 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetMillisecond_HalTimeout)));
60 }
61 return;
62 }
63}
64
65uint8_t Stm32Rtc::second() const
66{
67 return halTime().Seconds;
68}
69
70void Stm32Rtc::setSecond(uint8_t second)
71{
72 SEMF_INFO("set second %u", second);
73 RTC_TimeTypeDef hal = halTime();
74#if !defined(STM32F0) && !defined(STM32F1)
75 hal.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
76 hal.StoreOperation = RTC_STOREOPERATION_RESET;
77#endif
78 hal.Seconds = second % 60;
79 HAL_StatusTypeDef state = HAL_RTC_SetTime(m_hrtc, &hal, RTC_FORMAT_BIN);
80 if (state != HAL_OK)
81 {
82 if (state == HAL_ERROR)
83 {
84 SEMF_ERROR("hal error");
85 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetSecond_HalError)));
86 }
87 else if (state == HAL_BUSY)
88 {
89 SEMF_ERROR("hal busy");
90 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetSecond_HalBusy)));
91 }
92 else if (state == HAL_TIMEOUT)
93 {
94 SEMF_ERROR("hal timeout");
95 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetSecond_HalTimeout)));
96 }
97 return;
98 }
99}
100
101uint8_t Stm32Rtc::minute() const
102{
103 return halTime().Minutes;
104}
105
106void Stm32Rtc::setMinute(uint8_t minute)
107{
108 SEMF_INFO("set minute %u", minute);
109 RTC_TimeTypeDef hal = halTime();
110#if !defined(STM32F0) && !defined(STM32F1)
111 hal.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
112 hal.StoreOperation = RTC_STOREOPERATION_RESET;
113#endif
114 hal.Minutes = minute % 60;
115 HAL_StatusTypeDef state = HAL_RTC_SetTime(m_hrtc, &hal, RTC_FORMAT_BIN);
116 if (state != HAL_OK)
117 {
118 if (state == HAL_ERROR)
119 {
120 SEMF_ERROR("hal error");
121 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetMinute_HalError)));
122 }
123 else if (state == HAL_BUSY)
124 {
125 SEMF_ERROR("hal busy");
126 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetMinute_HalBusy)));
127 }
128 else if (state == HAL_TIMEOUT)
129 {
130 SEMF_ERROR("hal timeout");
131 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetMinute_HalTimeout)));
132 }
133 return;
134 }
135}
136
137uint8_t Stm32Rtc::hour() const
138{
139 return halTime().Hours;
140}
141
142void Stm32Rtc::setHour(uint8_t hour)
143{
144 SEMF_INFO("set hour %u", hour);
145 RTC_TimeTypeDef hal = halTime();
146#if !defined(STM32F0) && !defined(STM32F1)
147 hal.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
148 hal.StoreOperation = RTC_STOREOPERATION_RESET;
149#endif
150 hal.Hours = hour;
151 HAL_StatusTypeDef state = HAL_RTC_SetTime(m_hrtc, &hal, RTC_FORMAT_BIN);
152 if (state != HAL_OK)
153 {
154 if (state == HAL_ERROR)
155 {
156 SEMF_ERROR("hal error");
157 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetHour_HalError)));
158 }
159 else if (state == HAL_BUSY)
160 {
161 SEMF_ERROR("hal busy");
162 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetHour_HalBusy)));
163 }
164 else if (state == HAL_TIMEOUT)
165 {
166 SEMF_ERROR("hal timeout");
167 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetHour_HalTimeout)));
168 }
169 return;
170 }
171}
172
173uint8_t Stm32Rtc::day() const
174{
175 return halDate().Date;
176}
177
178void Stm32Rtc::setDay(uint8_t day)
179{
180 SEMF_INFO("set day %u", day);
181 RTC_DateTypeDef hal = halDate();
182 hal.Date = day;
183 HAL_StatusTypeDef state = HAL_RTC_SetDate(m_hrtc, &hal, RTC_FORMAT_BIN);
184 if (state != HAL_OK)
185 {
186 if (state == HAL_ERROR)
187 {
188 SEMF_ERROR("hal error");
189 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetDay_HalError)));
190 }
191 else if (state == HAL_BUSY)
192 {
193 SEMF_ERROR("hal busy");
194 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetDay_HalBusy)));
195 }
196 else if (state == HAL_TIMEOUT)
197 {
198 SEMF_ERROR("hal timeout");
199 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetDay_HalTimeout)));
200 }
201 return;
202 }
203}
204
205uint8_t Stm32Rtc::month() const
206{
207 return halDate().Month;
208}
209
210void Stm32Rtc::setMonth(uint8_t month)
211{
212 SEMF_INFO("set month %u", month);
213 RTC_DateTypeDef hal = halDate();
214 hal.Month = month;
215 HAL_StatusTypeDef state = HAL_RTC_SetDate(m_hrtc, &hal, RTC_FORMAT_BIN);
216 if (state != HAL_OK)
217 {
218 if (state == HAL_ERROR)
219 {
220 SEMF_ERROR("hal error");
221 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetMonth_HalError)));
222 }
223 else if (state == HAL_BUSY)
224 {
225 SEMF_ERROR("hal busy");
226 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetMonth_HalBusy)));
227 }
228 else if (state == HAL_TIMEOUT)
229 {
230 SEMF_ERROR("hal timeout");
231 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetMonth_HalTimeout)));
232 }
233 return;
234 }
235}
236
237uint16_t Stm32Rtc::year() const
238{
239 return halDate().Year + 2000;
240}
241
242void Stm32Rtc::setYear(uint16_t year)
243{
244 SEMF_INFO("set year %u", year);
245 RTC_DateTypeDef hal = halDate();
246 hal.Year = static_cast<uint8_t>(year % 100);
247 HAL_StatusTypeDef state = HAL_RTC_SetDate(m_hrtc, &hal, RTC_FORMAT_BIN);
248 if (state != HAL_OK)
249 {
250 if (state == HAL_ERROR)
251 {
252 SEMF_ERROR("hal error");
253 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetYear_HalError)));
254 }
255 else if (state == HAL_BUSY)
256 {
257 SEMF_ERROR("hal busy");
258 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetYear_HalBusy)));
259 }
260 else if (state == HAL_TIMEOUT)
261 {
262 SEMF_ERROR("hal timeout");
263 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::SetYear_HalTimeout)));
264 }
265 return;
266 }
267}
268
269RTC_TimeTypeDef Stm32Rtc::halTime() const
270{
271 RTC_TimeTypeDef halTime;
272 HAL_StatusTypeDef state = HAL_RTC_GetTime(m_hrtc, &halTime, RTC_FORMAT_BIN);
273 if (state != HAL_OK)
274 {
275 if (state == HAL_ERROR)
276 {
277 SEMF_ERROR("hal error");
278 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::HalTime_HalError)));
279 }
280 else if (state == HAL_BUSY)
281 {
282 SEMF_ERROR("hal busy");
283 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::HalTime_HalBusy)));
284 }
285 else if (state == HAL_TIMEOUT)
286 {
287 SEMF_ERROR("hal timeout");
288 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::HalTime_HalTimeout)));
289 }
290 }
291 return halTime;
292}
293
294RTC_DateTypeDef Stm32Rtc::halDate() const
295{
296 RTC_DateTypeDef halDate;
297 HAL_StatusTypeDef state = HAL_RTC_GetDate(m_hrtc, &halDate, RTC_FORMAT_BIN);
298 if (state != HAL_OK)
299 {
300 if (state == HAL_ERROR)
301 {
302 SEMF_ERROR("hal error");
303 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::HalDate_HalError)));
304 }
305 else if (state == HAL_BUSY)
306 {
307 SEMF_ERROR("hal busy");
308 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::HalDate_HalBusy)));
309 }
310 else if (state == HAL_TIMEOUT)
311 {
312 SEMF_ERROR("hal timeout");
313 error(Error(kSemfClassId, static_cast<uint8_t>(ErrorCode::HalDate_HalTimeout)));
314 }
315 }
316 return halDate;
317}
318} /* namespace semf */
319#endif
Class for representing errors. Every error should have a unique source code. As a user feel encourage...
Definition: error.h:22
Signal< Error > error
Definition: rtc.h:99
void setYear(uint16_t year) override
For set a new date.
Definition: stm32rtc.cpp:242
uint16_t year() const override
Get the current year.
Definition: stm32rtc.cpp:237
uint16_t millisecond() override
Get the milliseconds from the current time.
Definition: stm32rtc.cpp:21
void setDay(uint8_t day) override
For set a new date.
Definition: stm32rtc.cpp:178
uint8_t second() const override
Get the seconds from the current time.
Definition: stm32rtc.cpp:65
void setHour(uint8_t hour) override
For set a new time.
Definition: stm32rtc.cpp:142
uint8_t month() const override
Get the current month.
Definition: stm32rtc.cpp:205
uint8_t day() const override
Definition: stm32rtc.cpp:173
void setMonth(uint8_t month) override
For set a new date.
Definition: stm32rtc.cpp:210
Stm32Rtc(RTC_HandleTypeDef &hrtc)
Constructor.
Definition: stm32rtc.cpp:16
uint8_t hour() const override
Get the hours from the current time.
Definition: stm32rtc.cpp:137
void setMinute(uint8_t minute) override
For set a new time.
Definition: stm32rtc.cpp:106
void setMillisecond(uint16_t millisecond) override
For set a new time.
Definition: stm32rtc.cpp:31
void setSecond(uint8_t second) override
For set a new time.
Definition: stm32rtc.cpp:70
uint8_t minute() const override
Get the minutes from the current time.
Definition: stm32rtc.cpp:101
#define SEMF_ERROR(...)
Definition: debug.h:39
#define SEMF_INFO(...)
Definition: debug.h:41