-
Notifications
You must be signed in to change notification settings - Fork 0
/
RTC_ex.c
executable file
·67 lines (54 loc) · 2.12 KB
/
RTC_ex.c
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
/*!\file RTC_ex.c
** \author SMFSW
** \copyright MIT (c) 2017-2024, SMFSW
** \brief Basic RTC handling
**/
/****************************************************************/
#include "sarmfsw.h"
#include "time_utils.h"
#include "RTC_ex.h"
#if defined(HAL_RTC_MODULE_ENABLED)
/****************************************************************/
FctERR NONNULL__ RTC_SetTime(const DateTime * const time_new)
{
HAL_StatusTypeDef st;
RTC_TimeTypeDef RTCTime;
RTC_DateTypeDef RTCDate;
#if defined(STM32F2) || defined(STM32F3) || defined(STM32F4) || defined(STM32F7) || defined(STM32H7) || defined(STM32L0) || defined(STM32L1) || defined(STM32L4)
RTCTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
RTCTime.StoreOperation = RTC_STOREOPERATION_RESET;
#endif
RTCTime.Hours = time_new->Hours;
RTCTime.Minutes = time_new->Minutes;
RTCTime.Seconds = time_new->Seconds;
RTCDate.Year = time_new->Year;
RTCDate.Month = time_new->Month;
RTCDate.Date = time_new->Day;
RTCDate.WeekDay = Get_Weekday(time_new->Year + 2000, time_new->Month, time_new->Day);
if (RTCDate.WeekDay == Sunday) { RTCDate.WeekDay = 7; } // To match with STM32 RTC weekdays definitions
st = HAL_RTC_SetTime(&hrtc, &RTCTime, RTC_FORMAT_BIN);
st |= HAL_RTC_SetDate(&hrtc, &RTCDate, RTC_FORMAT_BIN);
if (st) { return ERROR_FAULT; }
return ERROR_OK;
}
FctERR NONNULL__ RTC_GetTime(DateTime * const time_now)
{
HAL_StatusTypeDef st;
RTC_TimeTypeDef RTCTime;
RTC_DateTypeDef RTCDate;
st = HAL_RTC_GetTime(&hrtc, &RTCTime, RTC_FORMAT_BIN);
st |= HAL_RTC_GetDate(&hrtc, &RTCDate, RTC_FORMAT_BIN);
if (st) { return ERROR_FAULT; }
time_now->Hours = RTCTime.Hours;
time_now->Minutes = RTCTime.Minutes;
time_now->Seconds = RTCTime.Seconds;
time_now->Year = RTCDate.Year;
time_now->Month = RTCDate.Month;
time_now->Day = RTCDate.Date;
time_now->Weekday = RTCDate.WeekDay;
if (time_now->Weekday == 7) { time_now->Weekday = Sunday; } // To match with STM32 RTC weekdays definitions
return ERROR_OK;
}
/****************************************************************/
#endif /* defined(HAL_RTC_MODULE_ENABLED) */
/****************************************************************/