1 /*
2 * SPDX-FileCopyrightText: 2016 STMicroelectronics
3 * SPDX-FileCopyrightText: 2019-2025 SiFli Technologies(Nanjing) Co., Ltd
4 *
5 * SPDX-License-Identifier: BSD-3-Clause AND Apache-2.0
6 */
7
8 #include "bf0_hal.h"
9
10 /** @addtogroup BF0_HAL_Driver
11 * @{
12 */
13
14 /** @defgroup RTC Real timer clock
15 * @brief RTC HAL module driver
16 * @{
17 */
18
19 #if defined(HAL_RTC_MODULE_ENABLED)||defined(_SIFLI_DOXYGEN_)
20
21 /* Private typedef -----------------------------------------------------------*/
22 /* Private define ------------------------------------------------------------*/
23 /* Private macro -------------------------------------------------------------*/
24 /* Private variables ---------------------------------------------------------*/
25
26 /* Private function prototypes -----------------------------------------------*/
27 /* Private functions ---------------------------------------------------------*/
28
29 /** @defgroup RTC_Exported_Functions RTC Exported Functions
30 * @{
31 */
32
33 /** @defgroup RTC_Exported_Functions_Group1 Initialization and de-initialization functions
34 * @brief Initialization and Configuration functions
35 *
36 @verbatim
37 ===============================================================================
38 ##### Initialization and de-initialization functions #####
39 ===============================================================================
40 [..] This section provides functions allowing to initialize and configure the
41 RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
42 RTC registers Write protection, enter and exit the RTC initialization mode,
43 RTC registers synchronization check and reference clock detection enable.
44 (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
45 It is split into 2 programmable prescalers to minimize power consumption.
46 (++) A 7-bit asynchronous prescaler and a 13-bit synchronous prescaler.
47 (++) When both prescalers are used, it is recommended to configure the
48 asynchronous prescaler to a high value to minimize power consumption.
49 (#) All RTC registers are Write protected. Writing to the RTC registers
50 is enabled by writing a key into the Write Protection register, RTC_WPR.
51 (#) To configure the RTC Calendar, user application should enter
52 initialization mode. In this mode, the calendar counter is stopped
53 and its value can be updated. When the initialization sequence is
54 complete, the calendar restarts counting after 4 RTCCLK cycles.
55 (#) To read the calendar through the shadow registers after Calendar
56 initialization, calendar update or after wake-up from low power modes
57 the software must first clear the RSF flag. The software must then
58 wait until it is set again before reading the calendar, which means
59 that the calendar registers have been correctly copied into the
60 RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
61 implements the above software sequence (RSF clear and RSF check).
62
63 @endverbatim
64 * @{
65 */
66
67 /**
68 * @brief Initializes the RTC peripheral
69 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
70 * the configuration information for RTC.
71 * @param wakesrc wakeup source, see RTC_INIT_XXX
72 * @retval HAL status
73 */
HAL_RTC_Init(RTC_HandleTypeDef * hrtc,uint32_t wakesrc)74 __HAL_ROM_USED HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc, uint32_t wakesrc)
75 {
76 HAL_StatusTypeDef r = HAL_ERROR;
77
78 if (hrtc)
79 {
80 if (wakesrc == RTC_INIT_SKIP)
81 {
82 hrtc->State = HAL_RTC_STATE_READY;
83 if (SystemPowerOnModeGet() == PM_COLD_BOOT) /*Cold boot, remove wakeup timer and Alarm*/
84 {
85 hrtc->Instance->CR &=
86 ((uint32_t)~(RTC_CR_ALRME | RTC_CR_ALRMIE | RTC_CR_WUTE | RTC_CR_WUTIE));
87 hrtc->Instance->ISR &= ~(RTC_ISR_ALRMF | RTC_ISR_WUTF);
88 }
89 r = HAL_OK;
90 }
91 else
92 {
93 if (hrtc->State == HAL_RTC_STATE_RESET)
94 {
95 hrtc->Lock = HAL_UNLOCKED; /* Allocate lock resource and initialize it */
96 HAL_RTC_MspInit(hrtc); /* Initialize RTC MSP */
97 }
98 hrtc->State = HAL_RTC_STATE_BUSY; /* Set RTC state */
99 hrtc->callback = NULL;
100 if (RTC_EnterInitMode(hrtc) != HAL_OK) /* Set Initialization mode */
101 hrtc->State = HAL_RTC_STATE_ERROR; /* Set RTC state */
102 else
103 {
104 hrtc->Instance->CR &= /* Clear RTC_CR FMT, OSEL and POL Bits */
105 ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL));
106 if (wakesrc == RTC_INIT_NORMAL) /* Clear wakeup timer and alarm */
107 {
108 hrtc->Instance->CR &=
109 ((uint32_t)~(RTC_CR_ALRME | RTC_CR_ALRMIE | RTC_CR_WUTE | RTC_CR_WUTIE));
110 hrtc->Instance->ISR &= ~(RTC_ISR_ALRMF | RTC_ISR_WUTF);
111 }
112 hrtc->Instance->CR |= /* Set RTC_CR register */
113 (uint32_t)(hrtc->Init.HourFormat);
114 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT; /* Exit Initialization mode */
115
116 {
117 uint32_t psclr = (hrtc->Init.DivAInt << RTC_PSCLR_DIVA_INT_Pos) & RTC_PSCLR_DIVA_INT;
118 psclr |= (hrtc->Init.DivAFrac << RTC_PSCLR_DIVA_FRAC_Pos) & RTC_PSCLR_DIVA_FRAC;
119 psclr |= (hrtc->Init.DivB << RTC_PSCLR_DIVB_Pos) & RTC_PSCLR_DIVB;
120 hrtc->Instance->PSCLR = psclr;
121 }
122 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
123 if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET &&
124 (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK))
125 hrtc->State = HAL_RTC_STATE_ERROR;
126 else
127 {
128 /* Set RTC state */
129 hrtc->State = HAL_RTC_STATE_READY;
130 r = HAL_OK;
131 }
132 }
133 }
134 }
135 return r;
136 }
137
138 /**
139 * @brief DeInitializes the RTC peripheral
140 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
141 * the configuration information for RTC.
142 * @note This function doesn't reset the RTC Backup Data registers.
143 * @retval HAL status
144 */
HAL_RTC_DeInit(RTC_HandleTypeDef * hrtc)145 __HAL_ROM_USED HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
146 {
147 uint32_t tickstart = 0U;
148 HAL_StatusTypeDef r = HAL_ERROR;
149
150 hrtc->State = HAL_RTC_STATE_BUSY; /* Set RTC state */
151 if (RTC_EnterInitMode(hrtc) != HAL_OK) /* Set Initialization mode */
152 hrtc->State = HAL_RTC_STATE_ERROR; /* Set RTC state */
153 else
154 {
155 hrtc->Instance->TR = 0x00000000U; /* Reset TR, DR and CR registers */
156 hrtc->Instance->DR = 0x00002101U;
157 hrtc->Instance->CR &= RTC_CR_WUCKSEL; /* Reset All CR bits except CR[0] */
158 tickstart = HAL_GetTick(); /* Get tick */
159 /* Wait till WUTWF flag is set and if Time out is reached exit */
160 while (((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == (uint32_t)RESET)
161 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
162 {
163 /* Set RTC state */
164 hrtc->State = HAL_RTC_STATE_TIMEOUT;
165 r = HAL_TIMEOUT;
166 }
167 if (r != HAL_TIMEOUT)
168 {
169 hrtc->Instance->CR &= 0x00000000U; /* Reset all RTC CR register bits */
170 hrtc->Instance->WUTR = 0x0000FFFFU;
171 hrtc->Instance->SHIFTR = 0x00000000U;
172 hrtc->Instance->ISR = 0x00000000U; /* Reset ISR register and exit initialization mode */
173
174 /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
175 if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET && HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
176 hrtc->State = HAL_RTC_STATE_ERROR;
177 else
178 {
179 HAL_RTC_MspDeInit(hrtc); /* De-Initialize RTC MSP */
180 hrtc->State = HAL_RTC_STATE_RESET;
181 r = HAL_OK;
182 }
183 }
184 }
185
186 __HAL_UNLOCK(hrtc); /* Release Lock */
187 return HAL_OK;
188 }
189
190 /**
191 * @brief Initializes the RTC MSP.
192 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
193 * the configuration information for RTC.
194 * @retval None
195 */
HAL_RTC_MspInit(RTC_HandleTypeDef * hrtc)196 __weak void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
197 {
198 /* Prevent unused argument(s) compilation warning */
199 UNUSED(hrtc);
200 /* NOTE : This function Should not be modified, when the callback is needed,
201 the HAL_RTC_MspInit could be implemented in the user file
202 */
203 }
204
205 /**
206 * @brief DeInitializes the RTC MSP.
207 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
208 * the configuration information for RTC.
209 * @retval None
210 */
HAL_RTC_MspDeInit(RTC_HandleTypeDef * hrtc)211 __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
212 {
213 /* Prevent unused argument(s) compilation warning */
214 UNUSED(hrtc);
215 /* NOTE : This function Should not be modified, when the callback is needed,
216 the HAL_RTC_MspDeInit could be implemented in the user file
217 */
218 }
219
time_2_reg(RTC_TimeTypeDef * sTime,uint32_t Format)220 static uint32_t time_2_reg(RTC_TimeTypeDef *sTime, uint32_t Format)
221 {
222 uint32_t tmpreg = 0U;
223
224 if (Format == RTC_FORMAT_BIN)
225 {
226 tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << RTC_TR_HU_Pos) | \
227 ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \
228 ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos) | \
229 (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos) | \
230 sTime->SubSeconds \
231 );
232 }
233 else
234 {
235 tmpreg = (((uint32_t)(sTime->Hours) << RTC_TR_HU_Pos) | \
236 ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \
237 ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos) | \
238 ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos) | \
239 sTime->SubSeconds \
240 );
241 }
242 return tmpreg;
243 }
244
245 /**
246 * @} RTC_Exported_Functions_Group1
247 */
248
249 /** @defgroup RTC_Exported_Functions_Group2 RTC Time and Date functions
250 * @brief RTC Time and Date functions
251 *
252 @verbatim
253 ===============================================================================
254 ##### RTC Time and Date functions #####
255 ===============================================================================
256
257 [..] This section provides functions allowing to configure Time and Date features
258
259 @endverbatim
260 * @{
261 */
262
263 /**
264 * @brief Sets RTC current time.
265 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
266 * the configuration information for RTC.
267 * @param sTime Pointer to Time structure
268 * @param Format Specifies the format of the entered parameters.
269 * This parameter can be one of the following values:
270 * @arg RTC_FORMAT_BIN: Binary data format
271 * @arg RTC_FORMAT_BCD: BCD data format
272 * @retval HAL status
273 */
HAL_RTC_SetTime(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTime,uint32_t Format)274 __HAL_ROM_USED HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
275 {
276 uint32_t tmpreg = 0U;
277 HAL_StatusTypeDef r = HAL_ERROR;
278
279 /* Process Locked */
280 __HAL_LOCK(hrtc);
281 hrtc->State = HAL_RTC_STATE_BUSY;
282
283 if ((hrtc->Instance->CR & RTC_CR_FMT) == (uint32_t)RESET)
284 sTime->TimeFormat = 0x00U;
285 tmpreg = time_2_reg(sTime, Format);
286 if (RTC_EnterInitMode(hrtc) != HAL_OK) /* Set Initialization mode */
287 hrtc->State = HAL_RTC_STATE_ERROR; /* Set RTC state */
288 else
289 {
290 hrtc->Instance->TR = (uint32_t)(tmpreg); /* Set the RTC_TR register */
291 #if 0
292 hrtc->Instance->CR &= (uint32_t)~RTC_CR_BCK; /* Clear the bits to be configured */
293 #endif
294 hrtc->Instance->CR |= /* Configure the RTC_CR register */
295 (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
296 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT; /* Exit Initialization mode */
297
298 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
299 if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET
300 && HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
301 hrtc->State = HAL_RTC_STATE_ERROR;
302
303 else
304 {
305 hrtc->State = HAL_RTC_STATE_READY;
306 r = HAL_OK;
307 }
308 }
309 __HAL_UNLOCK(hrtc);
310 return r;
311 }
312
reg_2_time(uint32_t reg,RTC_TimeTypeDef * sTime,uint32_t Format)313 __HAL_ROM_USED void reg_2_time(uint32_t reg, RTC_TimeTypeDef *sTime, uint32_t Format)
314 {
315 /* Fill the structure fields with the read parameters */
316 sTime->Hours = (uint8_t)((reg & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos);
317 sTime->Minutes = (uint8_t)((reg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos);
318 sTime->Seconds = (uint8_t)((reg & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos);
319 sTime->TimeFormat = (uint8_t)((reg & (RTC_TR_PM)) >> RTC_TR_PM_Pos);
320
321 /* Check the input parameters format */
322 if (Format == RTC_FORMAT_BIN)
323 {
324 /* Convert the time structure parameters to Binary format */
325 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
326 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
327 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
328 }
329 }
330 /**
331 * @brief Gets RTC current time.
332 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
333 * the configuration information for RTC.
334 * @param sTime Pointer to Time structure
335 * @param Format Specifies the format of the entered parameters.
336 * This parameter can be one of the following values:
337 * @arg RTC_FORMAT_BIN: Binary data format
338 * @arg RTC_FORMAT_BCD: BCD data format
339 * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
340 * value in second fraction ratio with time unit following generic formula:
341 * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
342 * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
343 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
344 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
345 * Reading RTC current time locks the values in calendar shadow registers until current date is read.
346 * @retval HAL status
347 */
HAL_RTC_GetTime(RTC_HandleTypeDef * hrtc,RTC_TimeTypeDef * sTime,uint32_t Format)348 __HAL_ROM_USED HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
349 {
350 uint32_t tmpreg = 0U;
351
352 /* Get the TR register */
353
354 #ifdef SOC_BF0_HCPU // Use LPSYS_CFG register to get time immediately.
355 tmpreg = hwp_hpsys_cfg->RTC_TR;
356 #else
357 tmpreg = hwp_lpsys_cfg->RTC_TR;
358 #endif
359 reg_2_time(tmpreg, sTime, Format);
360 sTime->SubSeconds = tmpreg & RTC_TR_SS;
361 sTime->SecondFractionInt = (hrtc->Instance->PSCLR & RTC_PSCLR_DIVA_INT) >> RTC_PSCLR_DIVA_INT_Pos;
362 sTime->SecondFractionFrac = (hrtc->Instance->PSCLR & RTC_PSCLR_DIVA_FRAC) >> RTC_PSCLR_DIVA_FRAC_Pos;
363
364 return HAL_OK;
365 }
366
date_2_reg(RTC_DateTypeDef * sDate,uint32_t Format)367 static uint32_t date_2_reg(RTC_DateTypeDef *sDate, uint32_t Format)
368 {
369 uint32_t datetmpreg = 0U;
370
371 if ((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
372 sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
373
374 if (sDate->Year < 100)
375 datetmpreg |= RTC_DR_CB;
376 else
377 sDate->Year -= 100;
378
379 if (Format == RTC_FORMAT_BIN)
380 datetmpreg |= (((uint32_t)RTC_ByteToBcd2(sDate->Year) << RTC_DR_YU_Pos) | \
381 ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \
382 ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \
383 ((uint32_t)sDate->WeekDay << RTC_DR_WD_Pos));
384 else
385 datetmpreg |= ((((uint32_t)sDate->Year) << RTC_DR_YU_Pos) | \
386 (((uint32_t)sDate->Month) << RTC_DR_MU_Pos) | \
387 ((uint32_t)sDate->Date) | \
388 (((uint32_t)sDate->WeekDay) << RTC_DR_WD_Pos));
389 return datetmpreg;
390 }
391
392 /**
393 * @brief Sets RTC current date.
394 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
395 * the configuration information for RTC.
396 * @param sDate Pointer to date structure
397 * @param Format specifies the format of the entered parameters.
398 * This parameter can be one of the following values:
399 * @arg RTC_FORMAT_BIN: Binary data format
400 * @arg RTC_FORMAT_BCD: BCD data format
401 * @retval HAL status
402 */
HAL_RTC_SetDate(RTC_HandleTypeDef * hrtc,RTC_DateTypeDef * sDate,uint32_t Format)403 __HAL_ROM_USED HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
404 {
405 uint32_t datetmpreg = 0U;
406 HAL_StatusTypeDef r = HAL_ERROR;
407
408 /* Process Locked */
409 __HAL_LOCK(hrtc);
410 hrtc->State = HAL_RTC_STATE_BUSY;
411
412 datetmpreg = date_2_reg(sDate, Format);
413
414 if (RTC_EnterInitMode(hrtc) != HAL_OK) /* Set Initialization mode */
415 hrtc->State = HAL_RTC_STATE_ERROR; /* Set RTC state*/
416 else
417 {
418 hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK); /* Set the RTC_DR register */
419 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT; /* Exit Initialization mode */
420
421 /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
422 if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET && HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
423 hrtc->State = HAL_RTC_STATE_ERROR;
424 else
425 {
426 hrtc->State = HAL_RTC_STATE_READY ;
427 r = HAL_OK;
428 }
429 }
430 /* Process Unlocked */
431 __HAL_UNLOCK(hrtc);
432 return r;
433 }
434
reg_2_date(uint32_t reg,RTC_DateTypeDef * sDate,uint32_t Format)435 __HAL_ROM_USED void reg_2_date(uint32_t reg, RTC_DateTypeDef *sDate, uint32_t Format)
436 {
437 /* Fill the structure fields with the read parameters */
438 sDate->Year = (uint8_t)((reg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos);
439 sDate->Month = (uint8_t)((reg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos);
440 sDate->Date = (uint8_t)(reg & (RTC_DR_DT | RTC_DR_DU));
441 sDate->WeekDay = (uint8_t)((reg & (RTC_DR_WD)) >> RTC_DR_WD_Pos);
442
443 /* Check the input parameters format */
444 if (Format == RTC_FORMAT_BIN)
445 {
446 /* Convert the date structure parameters to Binary format */
447 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
448 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
449 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
450 }
451 }
452
453 /**
454 * @brief Gets RTC current date.
455 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
456 * the configuration information for RTC.
457 * @param sDate Pointer to Date structure
458 * @param Format Specifies the format of the entered parameters.
459 * This parameter can be one of the following values:
460 * @arg RTC_FORMAT_BIN: Binary data format
461 * @arg RTC_FORMAT_BCD: BCD data format
462 * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
463 * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
464 * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
465 * @retval HAL status
466 */
HAL_RTC_GetDate(RTC_HandleTypeDef * hrtc,RTC_DateTypeDef * sDate,uint32_t Format)467 __HAL_ROM_USED HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
468 {
469 uint32_t datetmpreg = 0U;
470
471 /* Get the DR register */
472 #ifdef SOC_BF0_HCPU
473 datetmpreg = (uint32_t)(hwp_hpsys_cfg->RTC_DR & RTC_DR_RESERVED_MASK);
474 if (hwp_hpsys_cfg->RTC_DR & RTC_DR_ERR)
475 return HAL_ERROR;
476 #else
477 datetmpreg = (uint32_t)(hwp_lpsys_cfg->RTC_DR & RTC_DR_RESERVED_MASK);
478 if (hwp_lpsys_cfg->RTC_DR & RTC_DR_ERR)
479 return HAL_ERROR;
480 #endif
481
482 /* Fill the structure fields with the read parameters */
483 reg_2_date(datetmpreg, sDate, Format);
484 if (datetmpreg & RTC_DR_CB)
485 {
486 if (sDate->Year < 70)
487 hrtc->Instance->DR &= ~(RTC_DR_CB);
488 else
489 sDate->Year |= RTC_CENTURY_BIT;
490 }
491 return HAL_OK;
492 }
493
494 /**
495 * @brief Increase/Decrease RTC by 1 second..
496 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
497 * the configuration information for RTC.
498 * @param increase >0: increase, <0:decrease, 0 no change
499 * @retval HAL status
500 */
HAL_RTC_IncOneSecond(RTC_HandleTypeDef * hrtc,int32_t increase)501 HAL_StatusTypeDef HAL_RTC_IncOneSecond(RTC_HandleTypeDef *hrtc, int32_t increase)
502 {
503 HAL_StatusTypeDef r = HAL_OK;
504
505 #ifndef SF32LB55X
506 if (increase)
507 {
508 if (hrtc->Instance->ISR & RTC_ISR_SHPF)
509 r = HAL_BUSY;
510 else
511 {
512 if (increase > 0)
513 hrtc->Instance->SHIFTR = RTC_SHIFTR_ADD1S;
514 else
515 hrtc->Instance->SHIFTR = (hrtc->Instance->PSCLR & RTC_PSCLR_DIVB_Msk);
516 }
517 }
518 #else
519 r = HAL_ERROR;
520 #endif
521 return r;
522 }
523
524
525 /**
526 * @} RTC_Exported_Functions_Group2
527 */
528
529
530 /** @addtogroup RTC_Exported_Functions_Group3
531 * @brief RTC Alarm functions
532 *
533 @verbatim
534 ===============================================================================
535 ##### RTC Alarm functions #####
536 ===============================================================================
537
538 [..] This section provides functions allowing to configure Alarm feature
539
540 @endverbatim
541 * @{
542 */
543
544 /**
545 * @brief Sets the specified RTC Alarm.
546 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
547 * the configuration information for RTC.
548 * @param sAlarm Pointer to Alarm structure
549 * @param Format Specifies the format of the entered parameters.
550 * This parameter can be one of the following values:
551 * @arg RTC_FORMAT_BIN: Binary data format
552 * @arg RTC_FORMAT_BCD: BCD data format
553 * @retval HAL status
554 */
HAL_RTC_SetAlarm(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Format)555 __HAL_ROM_USED HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
556 {
557
558
559 /* Process Locked */
560 __HAL_LOCK(hrtc);
561
562 hrtc->State = HAL_RTC_STATE_BUSY;
563
564 while (!(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_ISR_ALRMWF)))
565 {
566 __HAL_RTC_ALARM_DISABLE(hrtc);
567 }
568
569 if ((hrtc->Instance->CR & RTC_CR_FMT) == (uint32_t)RESET)
570 sAlarm->AlarmTime.TimeFormat = 0x00U;
571 hrtc->Instance->ALRMTR = time_2_reg(&(sAlarm->AlarmTime), Format) ;
572 /*Ignore Year in Alarm*/
573 hrtc->Instance->ALRMDR = date_2_reg(&(sAlarm->AlarmDate), Format) & 0xFFFF;
574 hrtc->Instance->ALRMDR |= (sAlarm->AlarmMask | sAlarm->AlarmSubSecondMask);
575
576 NVIC_EnableIRQ(RTC_IRQn);
577 __HAL_RTC_ALARM_ENABLE_IT(hrtc);
578 __HAL_RTC_ALARM_ENABLE(hrtc);
579
580 /* Change RTC state */
581 hrtc->State = HAL_RTC_STATE_READY;
582
583 /* Process Unlocked */
584 __HAL_UNLOCK(hrtc);
585
586 return HAL_OK;
587 }
588
589 /**
590 * @brief Deactivate the specified RTC Alarm
591 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
592 * the configuration information for RTC.
593 * @retval HAL status
594 */
HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef * hrtc)595 __HAL_ROM_USED HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc)
596 {
597 uint32_t tickstart = 0U;
598
599 /* Process Locked */
600 __HAL_LOCK(hrtc);
601 hrtc->State = HAL_RTC_STATE_BUSY;
602
603 __HAL_RTC_ALARM_DISABLE(hrtc);
604 /* In case of interrupt mode is used, the interrupt source must disabled */
605 __HAL_RTC_ALARM_DISABLE_IT(hrtc);
606
607 /* Get tick */
608 tickstart = HAL_GetTick();
609
610 /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
611 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_ISR_ALRMWF) == RESET)
612 {
613 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
614 {
615 hrtc->State = HAL_RTC_STATE_TIMEOUT;
616 /* Process Unlocked */
617 __HAL_UNLOCK(hrtc);
618
619 return HAL_TIMEOUT;
620 }
621 }
622
623 hrtc->State = HAL_RTC_STATE_READY;
624 /* Process Unlocked */
625 __HAL_UNLOCK(hrtc);
626
627 return HAL_OK;
628 }
629
630 /**
631 * @brief Gets the RTC Alarm value and masks.
632 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
633 * the configuration information for RTC.
634 * @param sAlarm Pointer to Date structure
635 * @param Format Specifies the format of the entered parameters.
636 * This parameter can be one of the following values:
637 * @arg RTC_FORMAT_BIN: Binary data format
638 * @arg RTC_FORMAT_BCD: BCD data format
639 * @retval HAL status
640 */
HAL_RTC_GetAlarm(RTC_HandleTypeDef * hrtc,RTC_AlarmTypeDef * sAlarm,uint32_t Format)641 __HAL_ROM_USED HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
642 {
643 uint32_t tmpreg = 0U;
644
645 tmpreg = (uint32_t)(hrtc->Instance->ALRMTR);
646 reg_2_time(tmpreg, &(sAlarm->AlarmTime), Format);
647 sAlarm->AlarmTime.SubSeconds = tmpreg & RTC_ALRMTR_SS;
648
649 return HAL_OK;
650 }
651
652 /**
653 * @brief This function handles Alarm interrupt request.
654 * @retval None
655 */
HAL_RTC_IRQHandler(RTC_HandleTypeDef * hrtc)656 __HAL_ROM_USED void HAL_RTC_IRQHandler(RTC_HandleTypeDef *hrtc)
657 {
658 if (hrtc == NULL)
659 return;
660 if (hrtc->callback)
661 {
662 int reason;
663
664 if (__HAL_RTC_ALARM_GET_IT(hrtc))
665 {
666 reason = RTC_CBK_ALARM;
667 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_ISR_ALRMF);
668 }
669 else if (__HAL_RTC_WAKEUPTIMER_GET_IT(hrtc))
670 {
671 reason = RTC_CBK_WAKEUP;
672 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_ISR_WUTF);
673 }
674 else
675 {
676 reason = -1;
677 }
678 (*(hrtc->callback))(reason);
679 }
680 else if (__HAL_RTC_ALARM_GET_IT(hrtc))
681 {
682 HAL_RTC_AlarmAEventCallback(hrtc);
683 /* Clear the Alarm interrupt pending bit */
684 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_ISR_ALRMF);
685 }
686 else if (__HAL_RTC_WAKEUPTIMER_GET_IT(hrtc))
687 {
688 HAL_RTC_WakeupTimerEventCallback(hrtc);
689 /* Clear the Wakeuptimer pending bit */
690 __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_ISR_WUTF);
691 }
692 /* Change RTC state */
693 hrtc->State = HAL_RTC_STATE_READY;
694 }
695
696 /**
697 * @brief Alarm A callback.
698 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
699 * the configuration information for RTC.
700 * @retval None
701 */
HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef * hrtc)702 __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
703 {
704 /* Prevent unused argument(s) compilation warning */
705
706 UNUSED(hrtc);
707 /* NOTE : This function should not be modified, when the callback is needed,
708 the HAL_RTC_AlarmAEventCallback could be implemented in the user file
709 */
710 }
711
712 /**
713 * @brief This function handles AlarmA Polling request.
714 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
715 * the configuration information for RTC.
716 * @param Timeout Timeout duration
717 * @retval HAL status
718 */
HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef * hrtc,uint32_t Timeout)719 __HAL_ROM_USED HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
720 {
721 uint32_t tickstart = 0U;
722
723 /* Get tick */
724 tickstart = HAL_GetTick();
725
726 while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_ISR_ALRMF) == RESET)
727 {
728 if (Timeout != HAL_MAX_DELAY)
729 {
730 if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
731 {
732 hrtc->State = HAL_RTC_STATE_TIMEOUT;
733 return HAL_TIMEOUT;
734 }
735 }
736 }
737
738 /* Clear the Alarm interrupt pending bit */
739 __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_ISR_ALRMF);
740
741 /* Change RTC state */
742 hrtc->State = HAL_RTC_STATE_READY;
743
744 return HAL_OK;
745 }
746
747 /**
748 * @} RTC_Exported_Functions_Group3
749 */
750
751 /** @defgroup RTC_Exported_Functions_Group4 Peripheral Control functions
752 * @brief Peripheral Control functions
753 *
754 @verbatim
755 ===============================================================================
756 ##### Peripheral Control functions #####
757 ===============================================================================
758 [..]
759 This subsection provides functions allowing to
760 (+) Wait for RTC Time and Date Synchronization
761
762 @endverbatim
763 * @{
764 */
765
766 /**
767 * @brief Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are
768 * synchronized with RTC APB clock.
769 * @note To read the calendar through the shadow registers after Calendar
770 * initialization, calendar update or after wake-up from low power modes
771 * the software must first clear the RSF flag.
772 * The software must then wait until it is set again before reading
773 * the calendar, which means that the calendar registers have been
774 * correctly copied into the RTC_TR and RTC_DR shadow registers.
775 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
776 * the configuration information for RTC.
777 * @retval HAL status
778 */
HAL_RTC_WaitForSynchro(RTC_HandleTypeDef * hrtc)779 __HAL_ROM_USED HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
780 {
781 uint32_t tickstart = 0U;
782 HAL_StatusTypeDef r = HAL_OK;
783
784 hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK; /* Clear RSF flag */
785 tickstart = HAL_GetTick(); /* Get tick */
786 while ((hrtc->Instance->ISR & RTC_ISR_RSF) == (uint32_t)RESET) /* Wait the registers to be synchronised */
787 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
788 {
789 r = HAL_TIMEOUT;
790 break;
791 }
792 return r;
793 }
794
795 /**
796 * @} RTC_Exported_Functions_Group4
797 */
798
799 /** @defgroup RTC_Exported_Functions_Group5 Peripheral State functions
800 * @brief Peripheral State functions
801 *
802 @verbatim
803 ===============================================================================
804 ##### Peripheral State functions #####
805 ===============================================================================
806 [..]
807 This subsection provides functions allowing to
808 (+) Get RTC state
809
810 @endverbatim
811 * @{
812 */
813 /**
814 * @brief Returns the RTC state.
815 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
816 * the configuration information for RTC.
817 * @retval HAL state
818 */
HAL_RTC_GetState(RTC_HandleTypeDef * hrtc)819 __HAL_ROM_USED HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc)
820 {
821 return hrtc->State;
822 }
823
824
825 /**
826 * @brief Set the RTC backup register.
827 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
828 * the configuration information for RTC.
829 * @param idx index of backup register
830 * @param value value to set into backup register
831 * @retval None
832 */
HAL_RTC_set_backup(RTC_HandleTypeDef * hrtc,uint8_t idx,uint32_t value)833 __HAL_ROM_USED void HAL_RTC_set_backup(RTC_HandleTypeDef *hrtc, uint8_t idx, uint32_t value)
834 {
835 volatile uint32_t *p = &hrtc->Instance->BKP0R;
836 *(p + idx) = value;
837 }
838
839 /**
840 * @brief get the RTC backup register value.
841 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
842 * the configuration information for RTC.
843 * @param idx index of backup register
844 * @retval value saved in the backup register
845 */
HAL_RTC_get_backup(RTC_HandleTypeDef * hrtc,uint8_t idx)846 __HAL_ROM_USED uint32_t HAL_RTC_get_backup(RTC_HandleTypeDef *hrtc, uint8_t idx)
847 {
848 volatile uint32_t *p = &hrtc->Instance->BKP0R;
849 return *(p + idx);
850 }
851
852
853 #ifndef SF32LB55X
HAL_RAM_RET_CODE_SECT(HAL_PBR_ConfigMode,__HAL_ROM_USED HAL_StatusTypeDef HAL_PBR_ConfigMode (uint8_t pin,bool output_en))854 HAL_RAM_RET_CODE_SECT(HAL_PBR_ConfigMode, __HAL_ROM_USED HAL_StatusTypeDef HAL_PBR_ConfigMode(uint8_t pin, bool output_en))
855 {
856 HAL_StatusTypeDef ret = HAL_ERROR;
857 __IO uint32_t *pbr;
858
859 if (pin > HAL_PBR_MAX)
860 {
861 goto __EXIT;
862 }
863
864 pbr = &hwp_rtc->PBR0R;
865 if (output_en)
866 {
867 MODIFY_REG(pbr[pin], RTC_PBR0R_OE_Msk | RTC_PBR0R_IE_Msk, RTC_PBR0R_OE_Msk | RTC_PBR0R_IE_Msk);
868 }
869 else
870 {
871 MODIFY_REG(pbr[pin], RTC_PBR0R_OE_Msk | RTC_PBR0R_IE_Msk, RTC_PBR0R_IE_Msk);
872 }
873
874 ret = HAL_OK;
875
876 __EXIT:
877 return ret;
878 }
879
HAL_RAM_RET_CODE_SECT(HAL_PBR_ReadPin,__HAL_ROM_USED int8_t HAL_PBR_ReadPin (uint8_t pin))880 HAL_RAM_RET_CODE_SECT(HAL_PBR_ReadPin, __HAL_ROM_USED int8_t HAL_PBR_ReadPin(uint8_t pin))
881 {
882 int8_t val = -1;
883 __IO uint32_t *pbr;
884
885 if (pin > HAL_PBR_MAX)
886 {
887 goto __EXIT;
888 }
889
890 pbr = &hwp_rtc->PBR0R;
891
892 val = GET_REG_VAL(pbr[pin], RTC_PBR0R_IN_Msk, RTC_PBR0R_IN_Pos);
893
894 __EXIT:
895 return val;
896 }
897
HAL_RAM_RET_CODE_SECT(HAL_PBR_WritePin,__HAL_ROM_USED HAL_StatusTypeDef HAL_PBR_WritePin (uint8_t pin,uint8_t state))898 HAL_RAM_RET_CODE_SECT(HAL_PBR_WritePin, __HAL_ROM_USED HAL_StatusTypeDef HAL_PBR_WritePin(uint8_t pin, uint8_t state))
899 {
900 HAL_StatusTypeDef ret = HAL_ERROR;
901 __IO uint32_t *pbr;
902
903 if (pin > HAL_PBR_MAX)
904 {
905 goto __EXIT;
906 }
907
908 pbr = &hwp_rtc->PBR0R;
909 if (RTC_PBR0R_OE != (pbr[pin] & RTC_PBR0R_OE_Msk))
910 {
911 goto __EXIT;
912 }
913 MODIFY_REG(pbr[pin], RTC_PBR0R_OUT_Msk, MAKE_REG_VAL(state, RTC_PBR0R_OUT_Msk, RTC_PBR0R_OUT_Pos));
914
915 ret = HAL_OK;
916
917 __EXIT:
918 return ret;
919 }
920
HAL_RAM_RET_CODE_SECT(HAL_PBR_GetMode,__HAL_ROM_USED HAL_StatusTypeDef HAL_PBR_GetMode (uint8_t pin,bool * output_en))921 HAL_RAM_RET_CODE_SECT(HAL_PBR_GetMode, __HAL_ROM_USED HAL_StatusTypeDef HAL_PBR_GetMode(uint8_t pin, bool *output_en))
922 {
923 HAL_StatusTypeDef ret = HAL_ERROR;
924 __IO uint32_t *pbr;
925
926 if (!output_en)
927 {
928 goto __EXIT;
929 }
930
931 if (pin > HAL_PBR_MAX)
932 {
933 goto __EXIT;
934 }
935
936 pbr = &hwp_rtc->PBR0R;
937 if (pbr[pin] & RTC_PBR0R_OE)
938 {
939 *output_en = true;
940 }
941 else
942 {
943 *output_en = false;
944 }
945
946 ret = HAL_OK;
947
948 __EXIT:
949 return ret;
950 }
951
952 #endif /* SF32LB55X */
953
954
955 /**
956 * @} RTC_Exported_Functions_Group5
957 */
958
959 /**
960 * @} RTC_Exported_Functions_Group
961 */
962
963
964 /** @addtogroup RTC_Private_Functions
965 * @{
966 */
967
968 /**
969 * @brief Enters the RTC Initialization mode.
970 * @note The RTC Initialization mode is write protected, use the
971 * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
972 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
973 * the configuration information for RTC.
974 * @retval HAL status
975 */
RTC_EnterInitMode(RTC_HandleTypeDef * hrtc)976 __HAL_ROM_USED HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
977 {
978 uint32_t tickstart = 0U;
979 HAL_StatusTypeDef r = HAL_OK;
980
981 hrtc->Instance->ISR |= RTC_ISR_INIT; // set to enter init mode
982 tickstart = HAL_GetTick();
983
984 /* Wait till RTC is in INIT state and if Time out is reached exit */
985 while ((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
986 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
987 {
988 r = HAL_TIMEOUT;
989 break;
990 }
991
992 return r;
993 }
994
995
996 /**
997 * @brief Converts a 2 digit decimal to BCD format.
998 * @param Value Byte to be converted
999 * @retval Converted byte
1000 */
RTC_ByteToBcd2(uint8_t Value)1001 __HAL_ROM_USED uint8_t RTC_ByteToBcd2(uint8_t Value)
1002 {
1003 uint32_t bcdhigh = 0U;
1004
1005 while (Value >= 10U)
1006 {
1007 bcdhigh++;
1008 Value -= 10U;
1009 }
1010
1011 return ((uint8_t)(bcdhigh << 4U) | Value);
1012 }
1013
1014 /**
1015 * @brief Converts from 2 digit BCD to Binary.
1016 * @param Value BCD value to be converted
1017 * @retval Converted word
1018 */
RTC_Bcd2ToByte(uint8_t Value)1019 __HAL_ROM_USED uint8_t RTC_Bcd2ToByte(uint8_t Value)
1020 {
1021 uint32_t tmp = 0U;
1022 tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
1023 return (tmp + (Value & (uint8_t)0x0F));
1024 }
1025
1026
1027 /**
1028 * @brief Sets wake up timer with interrupt
1029 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1030 * the configuration information for RTC.
1031 * @param WakeUpCounter Wake up counter
1032 * @param WakeUpClock Wake up clock
1033 * @retval HAL status
1034 */
HAL_RTC_SetWakeUpTimer(RTC_HandleTypeDef * hrtc,uint32_t WakeUpCounter,uint32_t WakeUpClock)1035 __HAL_ROM_USED HAL_StatusTypeDef HAL_RTC_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
1036 {
1037 uint32_t tickstart = 0U;
1038
1039 /* Process Locked */
1040 __HAL_LOCK(hrtc);
1041
1042 hrtc->State = HAL_RTC_STATE_BUSY;
1043
1044 /*Check RTC WUTWF flag is reset only when wake up timer enabled*/
1045 if ((hrtc->Instance->CR & RTC_CR_WUTE) != RESET)
1046 {
1047 tickstart = HAL_GetTick();
1048
1049 /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
1050 while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == SET)
1051 {
1052 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1053 {
1054 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1055
1056 /* Process Unlocked */
1057 __HAL_UNLOCK(hrtc);
1058
1059 return HAL_TIMEOUT;
1060 }
1061 }
1062 }
1063
1064 __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1065
1066 tickstart = HAL_GetTick();
1067
1068 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
1069 while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
1070 {
1071 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1072 {
1073 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1074
1075 /* Process Unlocked */
1076 __HAL_UNLOCK(hrtc);
1077
1078 return HAL_TIMEOUT;
1079 }
1080 }
1081
1082 HAL_ASSERT(WakeUpClock == RTC_WAKEUP_SEC || WakeUpClock == RTC_WAKEUP_SUBSEC);
1083 MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL_Msk, MAKE_REG_VAL(WakeUpClock, RTC_CR_WUCKSEL_Msk, RTC_CR_WUCKSEL_Pos));
1084
1085 /* Configure the Wake-up Timer counter */
1086 hrtc->Instance->WUTR = (uint32_t)(WakeUpCounter & RTC_WUTR_WUT);
1087
1088 /* Enable the Wake-up Timer */
1089 NVIC_EnableIRQ(RTC_IRQn);
1090 __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc);
1091 __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
1092
1093
1094 hrtc->State = HAL_RTC_STATE_READY;
1095
1096 /* Process Unlocked */
1097 __HAL_UNLOCK(hrtc);
1098
1099 return HAL_OK;
1100 }
1101
1102 /**
1103 * @brief Deactivates wake up timer counter.
1104 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1105 * the configuration information for RTC.
1106 * @retval HAL status
1107 */
HAL_RTC_DeactivateWakeUpTimer(RTC_HandleTypeDef * hrtc)1108 __HAL_ROM_USED uint32_t HAL_RTC_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
1109 {
1110
1111 uint32_t tickstart = 0U;
1112
1113 /* Process Locked */
1114 __HAL_LOCK(hrtc);
1115
1116 hrtc->State = HAL_RTC_STATE_BUSY;
1117
1118 /* Disable the Wake-up Timer */
1119 __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
1120
1121 /* In case of interrupt mode is used, the interrupt source must disabled */
1122 __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc);
1123
1124 /* Get tick */
1125 tickstart = HAL_GetTick();
1126
1127 /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
1128 while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
1129 {
1130 if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1131 {
1132 hrtc->State = HAL_RTC_STATE_TIMEOUT;
1133
1134 /* Process Unlocked */
1135 __HAL_UNLOCK(hrtc);
1136
1137 return HAL_TIMEOUT;
1138 }
1139 }
1140
1141 hrtc->State = HAL_RTC_STATE_READY;
1142
1143 /* Process Unlocked */
1144 __HAL_UNLOCK(hrtc);
1145
1146 return HAL_OK;
1147
1148
1149 }
1150
1151
1152 /**
1153 * @brief Gets wake up timer counter.
1154 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1155 * the configuration information for RTC.
1156 * @retval Counter value
1157 */
HAL_RTC_GetWakeUpTimer(RTC_HandleTypeDef * hrtc)1158 __HAL_ROM_USED uint32_t HAL_RTC_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
1159 {
1160 /* Get the counter value */
1161 return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
1162 }
1163
1164
1165
1166 /**
1167 * @brief Wakeup Timer callback.
1168 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1169 * the configuration information for RTC.
1170 * @retval None
1171 */
HAL_RTC_WakeupTimerEventCallback(RTC_HandleTypeDef * hrtc)1172 __weak void HAL_RTC_WakeupTimerEventCallback(RTC_HandleTypeDef *hrtc)
1173 {
1174 /* Prevent unused argument(s) compilation warning */
1175
1176 UNUSED(hrtc);
1177 /* NOTE : This function Should not be modified, when the callback is needed,
1178 the HAL_RTC_WakeUpTimerEventCallback could be implemented in the user file
1179 */
1180
1181 }
1182
1183
1184 /**
1185 * @brief Register call back functions for RTC module.
1186 * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
1187 * the configuration information for RTC.
1188 * @param cbk callback function to be registered.
1189 * @retval None
1190 */
HAL_RTC_RegCallback(RTC_HandleTypeDef * hrtc,RTC_cb cbk)1191 __HAL_ROM_USED void HAL_RTC_RegCallback(RTC_HandleTypeDef *hrtc, RTC_cb cbk)
1192 {
1193 if (hrtc)
1194 hrtc->callback = cbk;
1195 }
1196
1197
1198 /**
1199 * @} RTC_Private_Functions
1200 */
1201
1202 #endif /* HAL_RTC_MODULE_ENABLED */
1203 /**
1204 * @} RTC
1205 */
1206
1207 /**
1208 * @} BF0_HAL_Driver
1209 */