1 | /* USER CODE BEGIN Header */ |
---|
2 | /** |
---|
3 | ****************************************************************************** |
---|
4 | * @file rtc.c |
---|
5 | * @brief This file provides code for the configuration |
---|
6 | * of the RTC instances. |
---|
7 | ****************************************************************************** |
---|
8 | * @attention |
---|
9 | * |
---|
10 | * Copyright (c) 2024 STMicroelectronics. |
---|
11 | * All rights reserved. |
---|
12 | * |
---|
13 | * This software is licensed under terms that can be found in the LICENSE file |
---|
14 | * in the root directory of this software component. |
---|
15 | * If no LICENSE file comes with this software, it is provided AS-IS. |
---|
16 | * |
---|
17 | ****************************************************************************** |
---|
18 | */ |
---|
19 | /* USER CODE END Header */ |
---|
20 | /* Includes ------------------------------------------------------------------*/ |
---|
21 | #include "rtc.h" |
---|
22 | |
---|
23 | /* USER CODE BEGIN 0 */ |
---|
24 | |
---|
25 | /* USER CODE END 0 */ |
---|
26 | |
---|
27 | RTC_HandleTypeDef hrtc; |
---|
28 | |
---|
29 | /* RTC init function */ |
---|
30 | void MX_RTC_Init(void) |
---|
31 | { |
---|
32 | |
---|
33 | /* USER CODE BEGIN RTC_Init 0 */ |
---|
34 | |
---|
35 | /* USER CODE END RTC_Init 0 */ |
---|
36 | |
---|
37 | RTC_TimeTypeDef sTime = {0}; |
---|
38 | RTC_DateTypeDef sDate = {0}; |
---|
39 | |
---|
40 | /* USER CODE BEGIN RTC_Init 1 */ |
---|
41 | |
---|
42 | /* USER CODE END RTC_Init 1 */ |
---|
43 | |
---|
44 | /** Initialize RTC Only |
---|
45 | */ |
---|
46 | hrtc.Instance = RTC; |
---|
47 | hrtc.Init.HourFormat = RTC_HOURFORMAT_24; |
---|
48 | hrtc.Init.AsynchPrediv = 127; |
---|
49 | hrtc.Init.SynchPrediv = 255; |
---|
50 | hrtc.Init.OutPut = RTC_OUTPUT_DISABLE; |
---|
51 | hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; |
---|
52 | hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; |
---|
53 | hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE; |
---|
54 | if (HAL_RTC_Init(&hrtc) != HAL_OK) |
---|
55 | { |
---|
56 | Error_Handler(); |
---|
57 | } |
---|
58 | |
---|
59 | /* USER CODE BEGIN Check_RTC_BKUP */ |
---|
60 | |
---|
61 | uint32_t firstRunID = HAL_RTCEx_BKUPRead(&hrtc, RTC_DATE_TIME_REG); |
---|
62 | if (firstRunID == RTC_DATE_TIME_FIRST_START_ID) return; |
---|
63 | |
---|
64 | /* USER CODE END Check_RTC_BKUP */ |
---|
65 | |
---|
66 | /** Initialize RTC and set the Time and Date |
---|
67 | */ |
---|
68 | sTime.Hours = 12; |
---|
69 | sTime.Minutes = 0; |
---|
70 | sTime.Seconds = 0; |
---|
71 | sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; |
---|
72 | sTime.StoreOperation = RTC_STOREOPERATION_RESET; |
---|
73 | if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK) |
---|
74 | { |
---|
75 | Error_Handler(); |
---|
76 | } |
---|
77 | sDate.WeekDay = RTC_WEEKDAY_MONDAY; |
---|
78 | sDate.Month = RTC_MONTH_JANUARY; |
---|
79 | sDate.Date = 1; |
---|
80 | sDate.Year = 25; |
---|
81 | |
---|
82 | if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK) |
---|
83 | { |
---|
84 | Error_Handler(); |
---|
85 | } |
---|
86 | /* USER CODE BEGIN RTC_Init 2 */ |
---|
87 | |
---|
88 | HAL_RTCEx_BKUPWrite(&hrtc, RTC_DATE_TIME_REG, RTC_DATE_TIME_FIRST_START_ID); |
---|
89 | |
---|
90 | /* USER CODE END RTC_Init 2 */ |
---|
91 | |
---|
92 | } |
---|
93 | |
---|
94 | void HAL_RTC_MspInit(RTC_HandleTypeDef* rtcHandle) |
---|
95 | { |
---|
96 | |
---|
97 | RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0}; |
---|
98 | if(rtcHandle->Instance==RTC) |
---|
99 | { |
---|
100 | /* USER CODE BEGIN RTC_MspInit 0 */ |
---|
101 | |
---|
102 | /* USER CODE END RTC_MspInit 0 */ |
---|
103 | |
---|
104 | /** Initializes the peripherals clock |
---|
105 | */ |
---|
106 | PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; |
---|
107 | PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE; |
---|
108 | if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) |
---|
109 | { |
---|
110 | Error_Handler(); |
---|
111 | } |
---|
112 | |
---|
113 | /* RTC clock enable */ |
---|
114 | __HAL_RCC_RTC_ENABLE(); |
---|
115 | /* USER CODE BEGIN RTC_MspInit 1 */ |
---|
116 | |
---|
117 | /* USER CODE END RTC_MspInit 1 */ |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | void HAL_RTC_MspDeInit(RTC_HandleTypeDef* rtcHandle) |
---|
122 | { |
---|
123 | |
---|
124 | if(rtcHandle->Instance==RTC) |
---|
125 | { |
---|
126 | /* USER CODE BEGIN RTC_MspDeInit 0 */ |
---|
127 | |
---|
128 | /* USER CODE END RTC_MspDeInit 0 */ |
---|
129 | /* Peripheral clock disable */ |
---|
130 | __HAL_RCC_RTC_DISABLE(); |
---|
131 | /* USER CODE BEGIN RTC_MspDeInit 1 */ |
---|
132 | |
---|
133 | /* USER CODE END RTC_MspDeInit 1 */ |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | /* USER CODE BEGIN 1 */ |
---|
138 | |
---|
139 | /* USER CODE END 1 */ |
---|