1 /**
2   ******************************************************************************
3   * @file    stm32wb0x_hal_rtc_ex.h
4   * @author  MCD Application Team
5   * @brief   Header file of RTC HAL Extended module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2024 STMicroelectronics.
10   * All rights reserved.
11   *
12   * This software is licensed under terms that can be found in the LICENSE file
13   * in the root directory of this software component.
14   * If no LICENSE file comes with this software, it is provided AS-IS.
15   *
16   ******************************************************************************
17   */
18 
19 /* Define to prevent recursive inclusion -------------------------------------*/
20 #ifndef STM32WB0x_HAL_RTC_EX_H
21 #define STM32WB0x_HAL_RTC_EX_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 
29 #include "stm32wb0x_hal_def.h"
30 
31 /** @addtogroup STM32WB0x_HAL_Driver
32   * @{
33   */
34 
35 /** @addtogroup RTCEx
36   * @{
37   */
38 
39 /* Exported types ------------------------------------------------------------*/
40 
41 /* Exported constants --------------------------------------------------------*/
42 
43 /** @defgroup RTCEx_Exported_Constants RTCEx Exported Constants
44   * @{
45   */
46 
47 /** @defgroup RTCEx_Backup_Registers_Definitions RTCEx Backup Registers Definitions
48   * @{
49   */
50 #define RTC_BKP_DR0                       0x00000000U
51 #define RTC_BKP_DR1                       0x00000001U
52 /**
53   * @}
54   */
55 
56 /** @defgroup RTCEx_Wakeup_Timer_Definitions RTCEx Wakeup Timer Definitions
57   * @{
58   */
59 #define RTC_WAKEUPCLOCK_RTCCLK_DIV16        0x00000000U
60 #define RTC_WAKEUPCLOCK_RTCCLK_DIV8         RTC_CR_WUCKSEL_0
61 #define RTC_WAKEUPCLOCK_RTCCLK_DIV4         RTC_CR_WUCKSEL_1
62 #define RTC_WAKEUPCLOCK_RTCCLK_DIV2         (RTC_CR_WUCKSEL_0 | RTC_CR_WUCKSEL_1)
63 #define RTC_WAKEUPCLOCK_CK_SPRE_16BITS      RTC_CR_WUCKSEL_2
64 #define RTC_WAKEUPCLOCK_CK_SPRE_17BITS      (RTC_CR_WUCKSEL_1 | RTC_CR_WUCKSEL_2)
65 /**
66   * @}
67   */
68 
69 /** @defgroup RTCEx_Smooth_calib_period_Definitions RTCEx Smooth Calib Period Definitions
70   * @{
71   */
72 #define RTC_SMOOTHCALIB_PERIOD_32SEC   0x00000000U      /*!< If RTCCLK = 32768 Hz, smooth calibration
73                                                               period is 32s, otherwise 2^20 RTCCLK pulses */
74 #define RTC_SMOOTHCALIB_PERIOD_16SEC   RTC_CALR_CALW16  /*!< If RTCCLK = 32768 Hz, smooth calibration
75                                                               period is 16s, otherwise 2^19 RTCCLK pulses */
76 #define RTC_SMOOTHCALIB_PERIOD_8SEC    RTC_CALR_CALW8   /*!< If RTCCLK = 32768 Hz, smooth calibration
77                                                               period is 8s, otherwise 2^18 RTCCLK pulses  */
78 /**
79   * @}
80   */
81 
82 /** @defgroup RTCEx_Smooth_calib_Plus_pulses_Definitions RTCEx Smooth Calib Plus Pulses Definitions
83   * @{
84   */
85 #define RTC_SMOOTHCALIB_PLUSPULSES_SET    RTC_CALR_CALP           /*!< The number of RTCCLK pulses added
86                                                                         during a X -second window = Y - CALM[8:0]
87                                                                         with Y = 512, 256, 128 when X = 32, 16, 8 */
88 #define RTC_SMOOTHCALIB_PLUSPULSES_RESET  0x00000000U             /*!< The number of RTCCLK pulses subbstited
89                                                                         during a 32-second window = CALM[8:0] */
90 /**
91   * @}
92   */
93 
94 /** @defgroup RTCEx_Add_1_Second_Parameter_Definitions RTCEx Add 1 Second Parameter Definitions
95   * @{
96   */
97 #define RTC_SHIFTADD1S_RESET      0x00000000U
98 #define RTC_SHIFTADD1S_SET        RTC_SHIFTR_ADD1S
99 /**
100   * @}
101   */
102 
103 /** @defgroup RTCEx_Calib_Output_selection_Definitions RTCEx Calib Output Selection Definitions
104   * @{
105   */
106 #define RTC_CALIBOUTPUT_512HZ            0x00000000U
107 #define RTC_CALIBOUTPUT_1HZ              RTC_CR_COSEL
108 /**
109   * @}
110   */
111 
112 /**
113   * @}
114   */
115 
116 /* Exported macros -----------------------------------------------------------*/
117 
118 /** @defgroup RTCEx_Exported_Macros RTCEx Exported Macros
119   * @{
120   */
121 
122 /* ---------------------------------WAKEUPTIMER-------------------------------*/
123 
124 /** @defgroup RTCEx_WakeUp_Timer RTCEx WakeUp Timer
125   * @{
126   */
127 
128 /**
129   * @brief  Enable the RTC WakeUp Timer peripheral.
130   * @param  __HANDLE__ specifies the RTC handle.
131   * @retval None
132   */
133 #define __HAL_RTC_WAKEUPTIMER_ENABLE(__HANDLE__)                      ((__HANDLE__)->Instance->CR |= (RTC_CR_WUTE))
134 
135 /**
136   * @brief  Disable the RTC Wakeup Timer peripheral.
137   * @param  __HANDLE__ specifies the RTC handle.
138   * @retval None
139   */
140 #define __HAL_RTC_WAKEUPTIMER_DISABLE(__HANDLE__)                     ((__HANDLE__)->Instance->CR &= ~(RTC_CR_WUTE))
141 
142 /**
143   * @brief  Enable the RTC Wakeup Timer interrupt.
144   * @param  __HANDLE__ specifies the RTC handle.
145   * @param  __INTERRUPT__ specifies the RTC Wakeup Timer interrupt sources to be enabled or disabled.
146   *         This parameter can be:
147   *            @arg RTC_IT_WUT: Wakeup Timer interrupt
148   * @retval None
149   */
150 #define __HAL_RTC_WAKEUPTIMER_ENABLE_IT(__HANDLE__, __INTERRUPT__)    ((__HANDLE__)->Instance->CR |= (__INTERRUPT__))
151 
152 /**
153   * @brief  Disable the RTC Wakeup Timer interrupt.
154   * @param  __HANDLE__ specifies the RTC handle.
155   * @param  __INTERRUPT__ specifies the RTC Wakeup Timer interrupt sources to be enabled or disabled.
156   *         This parameter can be:
157   *            @arg RTC_IT_WUT: Wakeup Timer interrupt
158   * @retval None
159   */
160 #define __HAL_RTC_WAKEUPTIMER_DISABLE_IT(__HANDLE__, __INTERRUPT__)   ((__HANDLE__)->Instance->CR &= ~(__INTERRUPT__))
161 
162 /**
163   * @brief  Check whether the specified RTC Wakeup Timer interrupt has occurred or not.
164   * @param  __HANDLE__ specifies the RTC handle.
165   * @param  __INTERRUPT__ specifies the RTC Wakeup Timer interrupt to check.
166   *         This parameter can be:
167   *            @arg RTC_IT_WUT: Wakeup Timer interrupt
168   * @retval None
169   */
170 #define __HAL_RTC_WAKEUPTIMER_GET_IT(__HANDLE__, __INTERRUPT__)          (((((__HANDLE__)->Instance->ISR) & ((__INTERRUPT__) >> 4U)) != 0U) ? 1U : 0U)
171 
172 /**
173   * @brief  Check whether the specified RTC Wakeup timer interrupt has been enabled or not.
174   * @param  __HANDLE__ specifies the RTC handle.
175   * @param  __INTERRUPT__ specifies the RTC Wakeup timer interrupt sources to check.
176   *         This parameter can be:
177   *            @arg RTC_IT_WUT: WakeUpTimer interrupt
178   * @retval None
179   */
180 #define __HAL_RTC_WAKEUPTIMER_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__)   (((((__HANDLE__)->Instance->CR) & (__INTERRUPT__)) != 0U) ? 1U : 0U)
181 
182 /**
183   * @brief  Get the selected RTC Wakeup Timer's flag status.
184   * @param  __HANDLE__ specifies the RTC handle.
185   * @param  __FLAG__ specifies the RTC Wakeup Timer flag to check.
186   *          This parameter can be:
187   *             @arg RTC_FLAG_WUTF: Wakeup Timer interrupt flag
188   *             @arg RTC_FLAG_WUTWF: Wakeup Timer 'write allowed' flag
189   * @retval None
190   */
191 #define __HAL_RTC_WAKEUPTIMER_GET_FLAG(__HANDLE__, __FLAG__)          (((((__HANDLE__)->Instance->ISR) & (__FLAG__)) != 0U)? 1U : 0U)
192 
193 /**
194   * @brief  Clear the RTC Wakeup timer's pending flags.
195   * @param  __HANDLE__ specifies the RTC handle.
196   * @param  __FLAG__ specifies the RTC Wakeup Timer Flag to clear.
197   *         This parameter can be:
198   *             @arg RTC_FLAG_WUTF: Wakeup Timer interrupt Flag
199   * @retval None
200   */
201 #define __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(__HANDLE__, __FLAG__)            ((__HANDLE__)->Instance->ISR) = (~((__FLAG__) | RTC_ISR_INIT)|((__HANDLE__)->Instance->ISR & RTC_ISR_INIT))
202 
203 /**
204   * @}
205   */
206 
207 
208 /* ------------------------------CALIBRATION----------------------------------*/
209 
210 /** @defgroup RTCEx_Calibration RTCEx Calibration
211   * @{
212   */
213 
214 /**
215   * @brief  Enable the RTC calibration output.
216   * @param  __HANDLE__ specifies the RTC handle.
217   * @retval None
218   */
219 #define __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(__HANDLE__)                 ((__HANDLE__)->Instance->CR |= (RTC_CR_COE))
220 
221 /**
222   * @brief  Disable the calibration output.
223   * @param  __HANDLE__ specifies the RTC handle.
224   * @retval None
225   */
226 #define __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(__HANDLE__)                ((__HANDLE__)->Instance->CR &= ~(RTC_CR_COE))
227 
228 /**
229   * @brief  Get the selected RTC shift operation's flag status.
230   * @param  __HANDLE__ specifies the RTC handle.
231   * @param  __FLAG__ specifies the RTC shift operation Flag is pending or not.
232   *          This parameter can be:
233   *             @arg RTC_FLAG_SHPF: Shift pending flag
234   * @retval None
235   */
236 #define __HAL_RTC_SHIFT_GET_FLAG(__HANDLE__, __FLAG__)                (((((__HANDLE__)->Instance->ISR) & (__FLAG__)) != 0U)? 1U : 0U)
237 /**
238   * @}
239   */
240 
241 /**
242   * @}
243   */
244 
245 /* Exported functions --------------------------------------------------------*/
246 
247 /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
248   * @{
249   */
250 
251 /** @addtogroup RTCEx_Exported_Functions_Group2
252   * @{
253   */
254 /* RTC Wakeup functions ******************************************************/
255 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock);
256 HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock);
257 HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc);
258 uint32_t          HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc);
259 void              HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc);
260 void              HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc);
261 HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout);
262 /**
263   * @}
264   */
265 
266 /** @addtogroup RTCEx_Exported_Functions_Group3
267   * @{
268   */
269 /* Extended Control functions ************************************************/
270 void              HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data);
271 uint32_t          HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister);
272 
273 HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue);
274 HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS);
275 HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput);
276 HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc);
277 HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc);
278 HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc);
279 HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef *hrtc);
280 HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef *hrtc);
281 /**
282   * @}
283   */
284 
285 /**
286   * @}
287   */
288 
289 /* Private types -------------------------------------------------------------*/
290 /* Private variables ---------------------------------------------------------*/
291 /* Private constants ---------------------------------------------------------*/
292 /* Private macros ------------------------------------------------------------*/
293 
294 /** @defgroup RTCEx_Private_Macros RTCEx Private Macros
295   * @{
296   */
297 
298 /** @defgroup RTCEx_IS_RTC_Definitions Private macros to check input parameters
299   * @{
300   */
301 #define IS_RTC_BKP(BKP)     ((BKP) < (uint32_t) RTC_BKP_NUMBER)
302 
303 #define IS_RTC_WAKEUP_CLOCK(CLOCK) (((CLOCK) == RTC_WAKEUPCLOCK_RTCCLK_DIV16)   || \
304                                     ((CLOCK) == RTC_WAKEUPCLOCK_RTCCLK_DIV8)    || \
305                                     ((CLOCK) == RTC_WAKEUPCLOCK_RTCCLK_DIV4)    || \
306                                     ((CLOCK) == RTC_WAKEUPCLOCK_RTCCLK_DIV2)    || \
307                                     ((CLOCK) == RTC_WAKEUPCLOCK_CK_SPRE_16BITS) || \
308                                     ((CLOCK) == RTC_WAKEUPCLOCK_CK_SPRE_17BITS))
309 
310 #define IS_RTC_WAKEUP_COUNTER(COUNTER)  ((COUNTER) <= RTC_WUTR_WUT)
311 
312 #define IS_RTC_SMOOTH_CALIB_PERIOD(PERIOD) (((PERIOD) == RTC_SMOOTHCALIB_PERIOD_32SEC) || \
313                                             ((PERIOD) == RTC_SMOOTHCALIB_PERIOD_16SEC) || \
314                                             ((PERIOD) == RTC_SMOOTHCALIB_PERIOD_8SEC))
315 
316 #define IS_RTC_SMOOTH_CALIB_PLUS(PLUS) (((PLUS) == RTC_SMOOTHCALIB_PLUSPULSES_SET) || \
317                                         ((PLUS) == RTC_SMOOTHCALIB_PLUSPULSES_RESET))
318 
319 #define  IS_RTC_SMOOTH_CALIB_MINUS(VALUE) ((VALUE) <= RTC_CALR_CALM)
320 
321 #define IS_RTC_SHIFT_ADD1S(SEL) (((SEL) == RTC_SHIFTADD1S_RESET) || \
322                                  ((SEL) == RTC_SHIFTADD1S_SET))
323 
324 #define IS_RTC_SHIFT_SUBFS(FS) ((FS) <= RTC_SHIFTR_SUBFS)
325 
326 #define IS_RTC_CALIB_OUTPUT(OUTPUT)  (((OUTPUT) == RTC_CALIBOUTPUT_512HZ) || \
327                                       ((OUTPUT) == RTC_CALIBOUTPUT_1HZ))
328 /**
329   * @}
330   */
331 
332 /**
333   * @}
334   */
335 
336 /**
337   * @}
338   */
339 
340 /**
341   * @}
342   */
343 
344 #ifdef __cplusplus
345 }
346 #endif
347 
348 #endif /* STM32WB0x_HAL_RTC_EX_H */
349