Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "k_rtc.h"
#define RTC_ASYNCH_PREDIV
RtcHandle
RCC_ClockFreq
TimInputCaptureHandle
uwLsiFreq
uwCaptureNumber
uwPeriodValue
uwMeasurementDone
k_CalendarBkupInit()
HAL_RTC_MspInit(RTC_HandleTypeDef *)
HAL_TIM_IC_MspInit(TIM_HandleTypeDef *)
GetLSIFrequency()
HAL_RTC_MspDeInit(RTC_HandleTypeDef *)
k_BkupSaveParameter(uint32_t, uint32_t)
k_BkupRestoreParameter(uint32_t)
k_GetTime(RTC_TimeTypeDef *)
k_SetTime(RTC_TimeTypeDef *)
k_GetDate(RTC_DateTypeDef *)
k_SetDate(RTC_DateTypeDef *)
Files
loading...
SourceVuSTM32 Libraries and SamplesSTemWinCore/Src/k_rtc.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
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file k_rtc.c * @author MCD Application Team * @brief This file provides the kernel rtc functions ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /* Includes ------------------------------------------------------------------*/ #include "k_rtc.h" /** @addtogroup CORE * @{ *//* ... */ /** @defgroup KERNEL_RTC * @brief Kernel rtc routines * @{ *//* ... */ Includes /* External variables --------------------------------------------------------*/ /* Private typedef -----------------------------------------------------------*/ /* Private defines -----------------------------------------------------------*/ #define RTC_ASYNCH_PREDIV 0x7F /* LSI as RTC clock */ /* Private macros ------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ RTC_HandleTypeDef RtcHandle; RCC_ClkInitTypeDef RCC_ClockFreq; TIM_HandleTypeDef TimInputCaptureHandle; static __IO uint32_t uwLsiFreq = 0; __IO uint32_t uwCaptureNumber = 0; __IO uint32_t uwPeriodValue = 0; __IO uint32_t uwMeasurementDone = 0; static uint32_t GetLSIFrequency(void); /** * @brief Configure the current time and date. * @param None * @retval None *//* ... */ void k_CalendarBkupInit(void) { RCC_OscInitTypeDef RCC_OscInitStruct; RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; /*##-1- Configure LSE as RTC clock source ##################################*/ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; RCC_OscInitStruct.LSEState = RCC_LSE_OFF; RCC_OscInitStruct.LSIState = RCC_LSI_ON; if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { return; }if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { ... } PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC; PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI; if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { return; }if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK) { ... } /*##-3- Get the LSI frequency: TIM5 is used to measure the LSI frequency ###*/ uwLsiFreq = GetLSIFrequency(); /*##-1- Configure the RTC peripheral #######################################*/ /* Configure RTC prescaler and RTC data registers */ /* RTC configured as follow: - Hour Format = Format 24 - Asynch Prediv = Value according to source clock - Synch Prediv = Value according to source clock - OutPut = Output Disable - OutPutPolarity = High Polarity - OutPutType = Open Drain *//* ... */ RtcHandle.Instance = RTC; RtcHandle.Init.HourFormat = RTC_HOURFORMAT_24; RtcHandle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV; RtcHandle.Init.SynchPrediv = (uwLsiFreq/128) - 1; RtcHandle.Init.OutPut = RTC_OUTPUT_DISABLE; RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH; RtcHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; if(HAL_RTC_Init(&RtcHandle) != HAL_OK) { }if (HAL_RTC_Init(&RtcHandle) != HAL_OK) { ... } }{ ... } /** * @brief RTC MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * @param hrtc: RTC handle pointer * @retval None *//* ... */ void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc) { /*##-2- Enable RTC peripheral Clocks #######################################*/ /* Enable RTC Clock */ __HAL_RCC_RTC_ENABLE(); }{ ... } /** * @brief TIM MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * @param htim: TIM handle pointer * @retval None *//* ... */ void HAL_TIM_IC_MspInit(TIM_HandleTypeDef *htim) { /* TIM5 Peripheral clock enable */ __HAL_RCC_TIM5_CLK_ENABLE(); /* Configure the NVIC for TIM5 */ HAL_NVIC_SetPriority(TIM5_IRQn, 0, 0); /* Enable the TIM5 global Interrupt */ HAL_NVIC_EnableIRQ(TIM5_IRQn); }{ ... } /** * @brief Configures TIM5 to measure the LSI oscillator frequency. * @param None * @retval LSI Frequency *//* ... */ static uint32_t GetLSIFrequency(void) { uint32_t pclk1 = 0; TIM_IC_InitTypeDef timinputconfig; /* Configure the TIM peripheral */ /* Set TIM5 instance */ TimInputCaptureHandle.Instance = TIM5; /* TIM5 configuration: Input Capture mode --------------------- The LSI oscillator is connected to TIM5 CH4. The Rising edge is used as active edge. The TIM5 CCR4 is used to compute the frequency value. ------------------------------------------------------------ *//* ... */ TimInputCaptureHandle.Init.Prescaler = 0; TimInputCaptureHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimInputCaptureHandle.Init.Period = 0xFFFF; TimInputCaptureHandle.Init.ClockDivision = 0; TimInputCaptureHandle.Init.RepetitionCounter = 0; if(HAL_TIM_IC_Init(&TimInputCaptureHandle) != HAL_OK) {}if (HAL_TIM_IC_Init(&TimInputCaptureHandle) != HAL_OK) { ... } /* Connect internally the TIM5_CH4 Input Capture to the LSI clock output */ HAL_TIMEx_RemapConfig(&TimInputCaptureHandle, TIM_TIM5_LSI); /* Configure the Input Capture of channel 4 */ timinputconfig.ICPolarity = TIM_ICPOLARITY_RISING; timinputconfig.ICSelection = TIM_ICSELECTION_DIRECTTI; timinputconfig.ICPrescaler = TIM_ICPSC_DIV8; timinputconfig.ICFilter = 0; if(HAL_TIM_IC_ConfigChannel(&TimInputCaptureHandle, &timinputconfig, TIM_CHANNEL_4) != HAL_OK) {}if (HAL_TIM_IC_ConfigChannel(&TimInputCaptureHandle, &timinputconfig, TIM_CHANNEL_4) != HAL_OK) { ... } /* Reset the flags */ TimInputCaptureHandle.Instance->SR = 0; /* Start the TIM Input Capture measurement in interrupt mode */ if(HAL_TIM_IC_Start_IT(&TimInputCaptureHandle, TIM_CHANNEL_4) != HAL_OK) { }if (HAL_TIM_IC_Start_IT(&TimInputCaptureHandle, TIM_CHANNEL_4) != HAL_OK) { ... } /* Wait until the TIM5 get 2 LSI edges (refer to TIM5_IRQHandler() in stm32f4xx_it.c file) *//* ... */ while(uwMeasurementDone == 0) { }while (uwMeasurementDone == 0) { ... } uwCaptureNumber = 0; /* Deinitialize the TIM5 peripheral registers to their default reset values */ HAL_TIM_IC_DeInit(&TimInputCaptureHandle); /* Compute the LSI frequency, depending on TIM5 input clock frequency (PCLK1)*/ /* Get PCLK1 frequency */ pclk1 = HAL_RCC_GetPCLK1Freq(); /* Get PCLK1 prescaler */ if((RCC->CFGR & RCC_CFGR_PPRE1) == 0) { /* PCLK1 prescaler equal to 1 => TIMCLK = PCLK1 */ return ((pclk1 * 8) / uwPeriodValue); }if ((RCC->CFGR & RCC_CFGR_PPRE1) == 0) { ... } else { /* PCLK1 prescaler different from 1 => TIMCLK = 2 * PCLK1 */ return (((2 * pclk1) * 8) / uwPeriodValue); }else { ... } }{ ... } /** * @brief RTC MSP De-Initialization * This function frees the hardware resources used in this example: * - Disable the Peripheral's clock * @param hrtc: RTC handle pointer * @retval None *//* ... */ void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc) { /*##-1- Reset peripherals ##################################################*/ __HAL_RCC_RTC_DISABLE(); }{ ... } /** * @brief Backup save parameter * @param address: RTC Backup data Register number. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to * specify the register. * @param Data: Data to be written in the specified RTC Backup data register. * @retval None *//* ... */ void k_BkupSaveParameter(uint32_t address, uint32_t data) { HAL_RTCEx_BKUPWrite(&RtcHandle,address,data); }{ ... } /** * @brief Backup restore parameter. * @param address: RTC Backup data Register number. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to * specify the register. * @retval None *//* ... */ uint32_t k_BkupRestoreParameter(uint32_t address) { return HAL_RTCEx_BKUPRead(&RtcHandle,address); }{ ... } /** * @brief RTC Get time. * @param Time: Pointer to Time structure * @retval None *//* ... */ void k_GetTime( RTC_TimeTypeDef *Time) { HAL_RTC_GetTime(&RtcHandle, Time, RTC_FORMAT_BIN); }{ ... } /** * @brief RTC Set time. * @param Time: Pointer to Time structure * @retval None *//* ... */ void k_SetTime( RTC_TimeTypeDef *Time) { Time->StoreOperation = 0; Time->SubSeconds = 0; Time->DayLightSaving = 0; HAL_RTC_SetTime(&RtcHandle, Time, RTC_FORMAT_BIN); }{ ... } /** * @brief RTC Get date * @param Date: Pointer to Date structure * @retval None *//* ... */ void k_GetDate( RTC_DateTypeDef *Date) { HAL_RTC_GetDate(&RtcHandle, Date, RTC_FORMAT_BIN); if((Date->Date == 0) || (Date->Month == 0)) { Date->Date = Date->Month = 1; }if ((Date->Date == 0) || (Date->Month == 0)) { ... } }{ ... } /** * @brief RTC Set date * @param Date: Pointer to Date structure * @retval None *//* ... */ void k_SetDate( RTC_DateTypeDef *Date) { HAL_RTC_SetDate(&RtcHandle, Date, RTC_FORMAT_BIN); }{ ... } /** * @} *//* ... */ /** * @} *//* ... */
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.