1 /**
2   ******************************************************************************
3   * @file    stm32f2xx_hal_rtc.h
4   * @author  MCD Application Team
5   * @brief   Header file of RTC HAL module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2016 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 STM32F2xx_HAL_RTC_H
21 #define STM32F2xx_HAL_RTC_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Includes ------------------------------------------------------------------*/
28 
29 #include "stm32f2xx_hal_def.h"
30 
31 /** @addtogroup STM32F2xx_HAL_Driver
32   * @{
33   */
34 
35 /** @addtogroup RTC
36   * @{
37   */
38 
39 /* Exported types ------------------------------------------------------------*/
40 
41 /** @defgroup RTC_Exported_Types RTC Exported Types
42   * @{
43   */
44 
45 /**
46   * @brief  HAL State structures definition
47   */
48 typedef enum
49 {
50   HAL_RTC_STATE_RESET             = 0x00U,  /*!< RTC not yet initialized or disabled */
51   HAL_RTC_STATE_READY             = 0x01U,  /*!< RTC initialized and ready for use   */
52   HAL_RTC_STATE_BUSY              = 0x02U,  /*!< RTC process is ongoing              */
53   HAL_RTC_STATE_TIMEOUT           = 0x03U,  /*!< RTC timeout state                   */
54   HAL_RTC_STATE_ERROR             = 0x04U   /*!< RTC error state                     */
55 } HAL_RTCStateTypeDef;
56 
57 /**
58   * @brief  RTC Configuration Structure definition
59   */
60 typedef struct
61 {
62   uint32_t HourFormat;      /*!< Specifies the RTC Hour Format.
63                                  This parameter can be a value of @ref RTC_Hour_Formats */
64 
65   uint32_t AsynchPrediv;    /*!< Specifies the RTC Asynchronous Predivider value.
66                                  This parameter must be a number between Min_Data = 0x00 and Max_Data = 0x7F */
67 
68   uint32_t SynchPrediv;     /*!< Specifies the RTC Synchronous Predivider value.
69                                  This parameter must be a number between Min_Data = 0x0000 and Max_Data = 0x1FFF */
70 
71   uint32_t OutPut;          /*!< Specifies which signal will be routed to the RTC output.
72                                  This parameter can be a value of @ref RTC_Output_selection_Definitions */
73 
74   uint32_t OutPutPolarity;  /*!< Specifies the polarity of the output signal.
75                                  This parameter can be a value of @ref RTC_Output_Polarity_Definitions */
76 
77   uint32_t OutPutType;      /*!< Specifies the RTC Output Pin mode.
78                                  This parameter can be a value of @ref RTC_Output_Type_ALARM_OUT */
79 } RTC_InitTypeDef;
80 
81 /**
82   * @brief  RTC Time structure definition
83   */
84 typedef struct
85 {
86   uint8_t Hours;            /*!< Specifies the RTC Time Hour.
87                                  This parameter must be a number between Min_Data = 0 and Max_Data = 12 if the RTC_HourFormat_12 is selected
88                                  This parameter must be a number between Min_Data = 0 and Max_Data = 23 if the RTC_HourFormat_24 is selected */
89 
90   uint8_t Minutes;          /*!< Specifies the RTC Time Minutes.
91                                  This parameter must be a number between Min_Data = 0 and Max_Data = 59 */
92 
93   uint8_t Seconds;          /*!< Specifies the RTC Time Seconds.
94                                  This parameter must be a number between Min_Data = 0 and Max_Data = 59 */
95 
96   uint8_t TimeFormat;       /*!< Specifies the RTC AM/PM Time.
97                                  This parameter can be a value of @ref RTC_AM_PM_Definitions */
98 
99   uint32_t DayLightSaving;  /*!< This interface is deprecated. To manage Daylight
100                                  Saving Time, please use HAL_RTC_DST_xxx functions */
101 
102   uint32_t StoreOperation;  /*!< This interface is deprecated. To manage Daylight
103                                  Saving Time, please use HAL_RTC_DST_xxx functions */
104 } RTC_TimeTypeDef;
105 
106 /**
107   * @brief  RTC Date structure definition
108   */
109 typedef struct
110 {
111   uint8_t WeekDay;  /*!< Specifies the RTC Date WeekDay.
112                          This parameter can be a value of @ref RTC_WeekDay_Definitions */
113 
114   uint8_t Month;    /*!< Specifies the RTC Date Month (in BCD format).
115                          This parameter can be a value of @ref RTC_Month_Date_Definitions */
116 
117   uint8_t Date;     /*!< Specifies the RTC Date.
118                          This parameter must be a number between Min_Data = 1 and Max_Data = 31 */
119 
120   uint8_t Year;     /*!< Specifies the RTC Date Year.
121                          This parameter must be a number between Min_Data = 0 and Max_Data = 99 */
122 
123 } RTC_DateTypeDef;
124 
125 /**
126   * @brief  RTC Alarm structure definition
127   */
128 typedef struct
129 {
130   RTC_TimeTypeDef AlarmTime;     /*!< Specifies the RTC Alarm Time members */
131 
132   uint32_t AlarmMask;            /*!< Specifies the RTC Alarm Masks.
133                                       This parameter can be a value of @ref RTC_AlarmMask_Definitions */
134 
135   uint32_t AlarmDateWeekDaySel;  /*!< Specifies the RTC Alarm is on Date or WeekDay.
136                                       This parameter can be a value of @ref RTC_AlarmDateWeekDay_Definitions */
137 
138   uint8_t AlarmDateWeekDay;      /*!< Specifies the RTC Alarm Date/WeekDay.
139                                       If the Alarm Date is selected, this parameter must be set to a value in the 1-31 range.
140                                       If the Alarm WeekDay is selected, this parameter can be a value of @ref RTC_WeekDay_Definitions */
141 
142   uint32_t Alarm;                /*!< Specifies the alarm .
143                                       This parameter can be a value of @ref RTC_Alarms_Definitions */
144 } RTC_AlarmTypeDef;
145 
146 /**
147   * @brief  RTC Handle Structure definition
148   */
149 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
150 typedef struct __RTC_HandleTypeDef
151 #else
152 typedef struct
153 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
154 {
155   RTC_TypeDef                 *Instance;  /*!< Register base address    */
156 
157   RTC_InitTypeDef             Init;       /*!< RTC required parameters  */
158 
159   HAL_LockTypeDef             Lock;       /*!< RTC locking object       */
160 
161   __IO HAL_RTCStateTypeDef    State;      /*!< Time communication state */
162 
163 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
164   void (* AlarmAEventCallback)      (struct __RTC_HandleTypeDef *hrtc);  /*!< RTC Alarm A Event callback         */
165 
166   void (* AlarmBEventCallback)      (struct __RTC_HandleTypeDef *hrtc);  /*!< RTC Alarm B Event callback         */
167 
168   void (* TimeStampEventCallback)   (struct __RTC_HandleTypeDef *hrtc);  /*!< RTC Timestamp Event callback       */
169 
170   void (* WakeUpTimerEventCallback) (struct __RTC_HandleTypeDef *hrtc);  /*!< RTC WakeUpTimer Event callback     */
171 
172   void (* Tamper1EventCallback)     (struct __RTC_HandleTypeDef *hrtc);  /*!< RTC Tamper 1 Event callback        */
173 
174   void (* MspInitCallback)          (struct __RTC_HandleTypeDef *hrtc);  /*!< RTC Msp Init callback              */
175 
176   void (* MspDeInitCallback)        (struct __RTC_HandleTypeDef *hrtc);  /*!< RTC Msp DeInit callback            */
177 
178 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
179 
180 } RTC_HandleTypeDef;
181 
182 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
183 /**
184   * @brief  HAL RTC Callback ID enumeration definition
185   */
186 typedef enum
187 {
188   HAL_RTC_ALARM_A_EVENT_CB_ID           = 0x00U,    /*!< RTC Alarm A Event Callback ID       */
189   HAL_RTC_ALARM_B_EVENT_CB_ID           = 0x01U,    /*!< RTC Alarm B Event Callback ID       */
190   HAL_RTC_TIMESTAMP_EVENT_CB_ID         = 0x02U,    /*!< RTC Timestamp Event Callback ID     */
191   HAL_RTC_WAKEUPTIMER_EVENT_CB_ID       = 0x03U,    /*!< RTC Wakeup Timer Event Callback ID  */
192   HAL_RTC_TAMPER1_EVENT_CB_ID           = 0x04U,    /*!< RTC Tamper 1 Callback ID            */
193   HAL_RTC_MSPINIT_CB_ID                 = 0x0EU,    /*!< RTC Msp Init callback ID            */
194   HAL_RTC_MSPDEINIT_CB_ID               = 0x0FU     /*!< RTC Msp DeInit callback ID          */
195 } HAL_RTC_CallbackIDTypeDef;
196 
197 /**
198   * @brief  HAL RTC Callback pointer definition
199   */
200 typedef  void (*pRTC_CallbackTypeDef)(RTC_HandleTypeDef *hrtc);  /*!< pointer to an RTC callback function */
201 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
202 
203 /**
204   * @}
205   */
206 
207 /* Exported constants --------------------------------------------------------*/
208 
209 /** @defgroup RTC_Exported_Constants RTC Exported Constants
210   * @{
211   */
212 
213 /** @defgroup RTC_Hour_Formats RTC Hour Formats
214   * @{
215   */
216 #define RTC_HOURFORMAT_24              0x00000000U
217 #define RTC_HOURFORMAT_12              RTC_CR_FMT
218 /**
219   * @}
220   */
221 
222 /** @defgroup RTC_Output_selection_Definitions RTC Output Selection Definitions
223   * @{
224   */
225 #define RTC_OUTPUT_DISABLE             0x00000000U
226 #define RTC_OUTPUT_ALARMA              RTC_CR_OSEL_0
227 #define RTC_OUTPUT_ALARMB              RTC_CR_OSEL_1
228 #define RTC_OUTPUT_WAKEUP              RTC_CR_OSEL
229 /**
230   * @}
231   */
232 
233 /** @defgroup RTC_Output_Polarity_Definitions RTC Output Polarity Definitions
234   * @{
235   */
236 #define RTC_OUTPUT_POLARITY_HIGH       0x00000000U
237 #define RTC_OUTPUT_POLARITY_LOW        RTC_CR_POL
238 /**
239   * @}
240   */
241 
242 /** @defgroup RTC_Output_Type_ALARM_OUT RTC Output Type ALARM OUT
243   * @{
244   */
245 #define RTC_OUTPUT_TYPE_OPENDRAIN      0x00000000U
246 #define RTC_OUTPUT_TYPE_PUSHPULL       RTC_TAFCR_ALARMOUTTYPE
247 /**
248   * @}
249   */
250 
251 /** @defgroup RTC_AM_PM_Definitions RTC AM PM Definitions
252   * @{
253   */
254 #define RTC_HOURFORMAT12_AM            ((uint8_t)0x00)
255 #define RTC_HOURFORMAT12_PM            ((uint8_t)0x01)
256 /**
257   * @}
258   */
259 
260 /** @defgroup RTC_DayLightSaving_Definitions RTC DayLight Saving Definitions
261   * @{
262   */
263 #define RTC_DAYLIGHTSAVING_SUB1H       RTC_CR_SUB1H
264 #define RTC_DAYLIGHTSAVING_ADD1H       RTC_CR_ADD1H
265 #define RTC_DAYLIGHTSAVING_NONE        0x00000000U
266 /**
267   * @}
268   */
269 
270 /** @defgroup RTC_StoreOperation_Definitions RTC Store Operation Definitions
271   * @{
272   */
273 #define RTC_STOREOPERATION_RESET        0x00000000U
274 #define RTC_STOREOPERATION_SET          RTC_CR_BKP
275 /**
276   * @}
277   */
278 
279 /** @defgroup RTC_Input_parameter_format_definitions RTC Input Parameter Format Definitions
280   * @{
281   */
282 #define RTC_FORMAT_BIN                  0x00000000U
283 #define RTC_FORMAT_BCD                  0x00000001U
284 /**
285   * @}
286   */
287 
288 /** @defgroup RTC_Month_Date_Definitions RTC Month Date Definitions (in BCD format)
289   * @{
290   */
291 #define RTC_MONTH_JANUARY              ((uint8_t)0x01)
292 #define RTC_MONTH_FEBRUARY             ((uint8_t)0x02)
293 #define RTC_MONTH_MARCH                ((uint8_t)0x03)
294 #define RTC_MONTH_APRIL                ((uint8_t)0x04)
295 #define RTC_MONTH_MAY                  ((uint8_t)0x05)
296 #define RTC_MONTH_JUNE                 ((uint8_t)0x06)
297 #define RTC_MONTH_JULY                 ((uint8_t)0x07)
298 #define RTC_MONTH_AUGUST               ((uint8_t)0x08)
299 #define RTC_MONTH_SEPTEMBER            ((uint8_t)0x09)
300 #define RTC_MONTH_OCTOBER              ((uint8_t)0x10)
301 #define RTC_MONTH_NOVEMBER             ((uint8_t)0x11)
302 #define RTC_MONTH_DECEMBER             ((uint8_t)0x12)
303 /**
304   * @}
305   */
306 
307 /** @defgroup RTC_WeekDay_Definitions RTC WeekDay Definitions
308   * @{
309   */
310 #define RTC_WEEKDAY_MONDAY             ((uint8_t)0x01)
311 #define RTC_WEEKDAY_TUESDAY            ((uint8_t)0x02)
312 #define RTC_WEEKDAY_WEDNESDAY          ((uint8_t)0x03)
313 #define RTC_WEEKDAY_THURSDAY           ((uint8_t)0x04)
314 #define RTC_WEEKDAY_FRIDAY             ((uint8_t)0x05)
315 #define RTC_WEEKDAY_SATURDAY           ((uint8_t)0x06)
316 #define RTC_WEEKDAY_SUNDAY             ((uint8_t)0x07)
317 /**
318   * @}
319   */
320 
321 /** @defgroup RTC_AlarmDateWeekDay_Definitions RTC Alarm Date WeekDay Definitions
322   * @{
323   */
324 #define RTC_ALARMDATEWEEKDAYSEL_DATE      0x00000000U
325 #define RTC_ALARMDATEWEEKDAYSEL_WEEKDAY   RTC_ALRMAR_WDSEL
326 /**
327   * @}
328   */
329 
330 /** @defgroup RTC_AlarmMask_Definitions RTC Alarm Mask Definitions
331   * @{
332   */
333 #define RTC_ALARMMASK_NONE                0x00000000U
334 #define RTC_ALARMMASK_DATEWEEKDAY         RTC_ALRMAR_MSK4
335 #define RTC_ALARMMASK_HOURS               RTC_ALRMAR_MSK3
336 #define RTC_ALARMMASK_MINUTES             RTC_ALRMAR_MSK2
337 #define RTC_ALARMMASK_SECONDS             RTC_ALRMAR_MSK1
338 #define RTC_ALARMMASK_ALL                 (RTC_ALARMMASK_DATEWEEKDAY | \
339                                            RTC_ALARMMASK_HOURS       | \
340                                            RTC_ALARMMASK_MINUTES     | \
341                                            RTC_ALARMMASK_SECONDS)
342 /**
343   * @}
344   */
345 
346 /** @defgroup RTC_Alarms_Definitions RTC Alarms Definitions
347   * @{
348   */
349 #define RTC_ALARM_A                       RTC_CR_ALRAE
350 #define RTC_ALARM_B                       RTC_CR_ALRBE
351 /**
352   * @}
353   */
354 
355 /** @defgroup RTC_Interrupts_Definitions RTC Interrupts Definitions
356   * @{
357   */
358 #define RTC_IT_TS                         RTC_CR_TSIE         /*!< Enable Timestamp Interrupt               */
359 #define RTC_IT_WUT                        RTC_CR_WUTIE        /*!< Enable Wakeup timer Interrupt            */
360 #define RTC_IT_ALRB                       RTC_CR_ALRBIE       /*!< Enable Alarm B Interrupt                 */
361 #define RTC_IT_ALRA                       RTC_CR_ALRAIE       /*!< Enable Alarm A Interrupt                 */
362 /**
363   * @}
364   */
365 
366 /** @defgroup RTC_Flags_Definitions RTC Flags Definitions
367   * @{
368   */
369 #define RTC_FLAG_TAMP1F                   RTC_ISR_TAMP1F      /*!< Tamper 1 event flag                      */
370 #define RTC_FLAG_TSOVF                    RTC_ISR_TSOVF       /*!< Timestamp overflow flag                  */
371 #define RTC_FLAG_TSF                      RTC_ISR_TSF         /*!< Timestamp event flag                     */
372 #define RTC_FLAG_WUTF                     RTC_ISR_WUTF        /*!< Wakeup timer event flag                  */
373 #define RTC_FLAG_ALRBF                    RTC_ISR_ALRBF       /*!< Alarm B event flag                       */
374 #define RTC_FLAG_ALRAF                    RTC_ISR_ALRAF       /*!< Alarm A event flag                       */
375 #define RTC_FLAG_INITF                    RTC_ISR_INITF       /*!< RTC in initialization mode flag          */
376 #define RTC_FLAG_RSF                      RTC_ISR_RSF         /*!< Register synchronization flag            */
377 #define RTC_FLAG_INITS                    RTC_ISR_INITS       /*!< RTC initialization status flag           */
378 #define RTC_FLAG_WUTWF                    RTC_ISR_WUTWF       /*!< WUTR register write allowance flag       */
379 #define RTC_FLAG_ALRBWF                   RTC_ISR_ALRBWF      /*!< ALRMBR register write allowance flag     */
380 #define RTC_FLAG_ALRAWF                   RTC_ISR_ALRAWF      /*!< ALRMAR register write allowance flag     */
381 /**
382   * @}
383   */
384 
385 /**
386   * @}
387   */
388 
389 /* Exported macros -----------------------------------------------------------*/
390 
391 /** @defgroup RTC_Exported_Macros RTC Exported Macros
392   * @{
393   */
394 
395 /** @brief Reset RTC handle state
396   * @param  __HANDLE__ specifies the RTC handle.
397   * @retval None
398   */
399 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
400 #define __HAL_RTC_RESET_HANDLE_STATE(__HANDLE__) do {                                            \
401                                                       (__HANDLE__)->State = HAL_RTC_STATE_RESET; \
402                                                       (__HANDLE__)->MspInitCallback = NULL;      \
403                                                       (__HANDLE__)->MspDeInitCallback = NULL;    \
404                                                     } while(0U)
405 #else
406 #define __HAL_RTC_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_RTC_STATE_RESET)
407 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
408 
409 /**
410   * @brief  Disable the write protection for RTC registers.
411   * @param  __HANDLE__ specifies the RTC handle.
412   * @retval None
413   */
414 #define __HAL_RTC_WRITEPROTECTION_DISABLE(__HANDLE__) do {                                       \
415                                                            (__HANDLE__)->Instance->WPR = 0xCAU;  \
416                                                            (__HANDLE__)->Instance->WPR = 0x53U;  \
417                                                          } while(0U)
418 
419 /**
420   * @brief  Enable the write protection for RTC registers.
421   * @param  __HANDLE__ specifies the RTC handle.
422   * @retval None
423   */
424 #define __HAL_RTC_WRITEPROTECTION_ENABLE(__HANDLE__) do {                                       \
425                                                           (__HANDLE__)->Instance->WPR = 0xFFU;  \
426                                                         } while(0U)
427 
428 /**
429   * @brief  Check whether the RTC Calendar is initialized.
430   * @param  __HANDLE__ specifies the RTC handle.
431   * @retval None
432   */
433 #define __HAL_RTC_IS_CALENDAR_INITIALIZED(__HANDLE__)                 (((((__HANDLE__)->Instance->ISR) & (RTC_FLAG_INITS)) == RTC_FLAG_INITS) ? 1U : 0U)
434 
435 /**
436   * @brief  Enable the RTC ALARMA peripheral.
437   * @param  __HANDLE__ specifies the RTC handle.
438   * @retval None
439   */
440 #define __HAL_RTC_ALARMA_ENABLE(__HANDLE__)                           ((__HANDLE__)->Instance->CR |= (RTC_CR_ALRAE))
441 
442 /**
443   * @brief  Disable the RTC ALARMA peripheral.
444   * @param  __HANDLE__ specifies the RTC handle.
445   * @retval None
446   */
447 #define __HAL_RTC_ALARMA_DISABLE(__HANDLE__)                          ((__HANDLE__)->Instance->CR &= ~(RTC_CR_ALRAE))
448 
449 /**
450   * @brief  Enable the RTC ALARMB peripheral.
451   * @param  __HANDLE__ specifies the RTC handle.
452   * @retval None
453   */
454 #define __HAL_RTC_ALARMB_ENABLE(__HANDLE__)                           ((__HANDLE__)->Instance->CR |= (RTC_CR_ALRBE))
455 
456 /**
457   * @brief  Disable the RTC ALARMB peripheral.
458   * @param  __HANDLE__ specifies the RTC handle.
459   * @retval None
460   */
461 #define __HAL_RTC_ALARMB_DISABLE(__HANDLE__)                          ((__HANDLE__)->Instance->CR &= ~(RTC_CR_ALRBE))
462 
463 /**
464   * @brief  Enable the RTC Alarm interrupt.
465   * @param  __HANDLE__ specifies the RTC handle.
466   * @param  __INTERRUPT__ specifies the RTC Alarm interrupt sources to be enabled or disabled.
467   *          This parameter can be any combination of the following values:
468   *             @arg RTC_IT_ALRA: Alarm A interrupt
469   *             @arg RTC_IT_ALRB: Alarm B interrupt
470   * @retval None
471   */
472 #define __HAL_RTC_ALARM_ENABLE_IT(__HANDLE__, __INTERRUPT__)          ((__HANDLE__)->Instance->CR |= (__INTERRUPT__))
473 
474 /**
475   * @brief  Disable the RTC Alarm interrupt.
476   * @param  __HANDLE__ specifies the RTC handle.
477   * @param  __INTERRUPT__ specifies the RTC Alarm interrupt sources to be enabled or disabled.
478   *          This parameter can be any combination of the following values:
479   *             @arg RTC_IT_ALRA: Alarm A interrupt
480   *             @arg RTC_IT_ALRB: Alarm B interrupt
481   * @retval None
482   */
483 #define __HAL_RTC_ALARM_DISABLE_IT(__HANDLE__, __INTERRUPT__)         ((__HANDLE__)->Instance->CR &= ~(__INTERRUPT__))
484 
485 /**
486   * @brief  Check whether the specified RTC Alarm interrupt has occurred or not.
487   * @param  __HANDLE__ specifies the RTC handle.
488   * @param  __INTERRUPT__ specifies the RTC Alarm interrupt to check.
489   *         This parameter can be:
490   *            @arg RTC_IT_ALRA: Alarm A interrupt
491   *            @arg RTC_IT_ALRB: Alarm B interrupt
492   * @retval None
493   */
494 #define __HAL_RTC_ALARM_GET_IT(__HANDLE__, __INTERRUPT__)           (((((__HANDLE__)->Instance->ISR) & ((__INTERRUPT__) >> 4U)) != 0U) ? 1U : 0U)
495 
496 /**
497   * @brief  Get the selected RTC Alarm's flag status.
498   * @param  __HANDLE__ specifies the RTC handle.
499   * @param  __FLAG__ specifies the RTC Alarm Flag to check.
500   *         This parameter can be:
501   *            @arg RTC_FLAG_ALRAF: Alarm A interrupt flag
502   *            @arg RTC_FLAG_ALRAWF: Alarm A 'write allowed' flag
503   *            @arg RTC_FLAG_ALRBF: Alarm B interrupt flag
504   *            @arg RTC_FLAG_ALRBWF: Alarm B 'write allowed' flag
505   * @retval None
506   */
507 #define __HAL_RTC_ALARM_GET_FLAG(__HANDLE__, __FLAG__)                (((((__HANDLE__)->Instance->ISR) & (__FLAG__)) != 0U) ? 1U : 0U)
508 
509 /**
510   * @brief  Clear the RTC Alarm's pending flags.
511   * @param  __HANDLE__ specifies the RTC handle.
512   * @param  __FLAG__ specifies the RTC Alarm flag to be cleared.
513   *          This parameter can be:
514   *             @arg RTC_FLAG_ALRAF
515   *             @arg RTC_FLAG_ALRBF
516   * @retval None
517   */
518 #define __HAL_RTC_ALARM_CLEAR_FLAG(__HANDLE__, __FLAG__)                  ((__HANDLE__)->Instance->ISR) = (~((__FLAG__) | RTC_ISR_INIT)|((__HANDLE__)->Instance->ISR & RTC_ISR_INIT))
519 
520 /**
521   * @brief  Check whether the specified RTC Alarm interrupt has been enabled or not.
522   * @param  __HANDLE__ specifies the RTC handle.
523   * @param  __INTERRUPT__ specifies the RTC Alarm interrupt sources to check.
524   *         This parameter can be:
525   *            @arg RTC_IT_ALRA: Alarm A interrupt
526   *            @arg RTC_IT_ALRB: Alarm B interrupt
527   * @retval None
528   */
529 #define __HAL_RTC_ALARM_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__)     (((((__HANDLE__)->Instance->CR) & (__INTERRUPT__)) != 0U) ? 1U : 0U)
530 
531 /**
532   * @brief  Enable interrupt on the RTC Alarm associated EXTI line.
533   * @retval None
534   */
535 #define __HAL_RTC_ALARM_EXTI_ENABLE_IT()            (EXTI->IMR |= RTC_EXTI_LINE_ALARM_EVENT)
536 
537 /**
538   * @brief  Disable interrupt on the RTC Alarm associated EXTI line.
539   * @retval None
540   */
541 #define __HAL_RTC_ALARM_EXTI_DISABLE_IT()           (EXTI->IMR &= ~RTC_EXTI_LINE_ALARM_EVENT)
542 
543 /**
544   * @brief  Enable event on the RTC Alarm associated EXTI line.
545   * @retval None.
546   */
547 #define __HAL_RTC_ALARM_EXTI_ENABLE_EVENT()          (EXTI->EMR |= RTC_EXTI_LINE_ALARM_EVENT)
548 
549 /**
550   * @brief  Disable event on the RTC Alarm associated EXTI line.
551   * @retval None.
552   */
553 #define __HAL_RTC_ALARM_EXTI_DISABLE_EVENT()         (EXTI->EMR &= ~RTC_EXTI_LINE_ALARM_EVENT)
554 
555 /**
556   * @brief  Enable falling edge trigger on the RTC Alarm associated EXTI line.
557   * @retval None.
558   */
559 #define __HAL_RTC_ALARM_EXTI_ENABLE_FALLING_EDGE()   (EXTI->FTSR |= RTC_EXTI_LINE_ALARM_EVENT)
560 
561 /**
562   * @brief  Disable falling edge trigger on the RTC Alarm associated EXTI line.
563   * @retval None.
564   */
565 #define __HAL_RTC_ALARM_EXTI_DISABLE_FALLING_EDGE()  (EXTI->FTSR &= ~RTC_EXTI_LINE_ALARM_EVENT)
566 
567 /**
568   * @brief  Enable rising edge trigger on the RTC Alarm associated EXTI line.
569   * @retval None.
570   */
571 #define __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE()    (EXTI->RTSR |= RTC_EXTI_LINE_ALARM_EVENT)
572 
573 /**
574   * @brief  Disable rising edge trigger on the RTC Alarm associated EXTI line.
575   * @retval None.
576   */
577 #define __HAL_RTC_ALARM_EXTI_DISABLE_RISING_EDGE()   (EXTI->RTSR &= ~RTC_EXTI_LINE_ALARM_EVENT)
578 
579 /**
580   * @brief  Enable rising & falling edge trigger on the RTC Alarm associated EXTI line.
581   * @retval None.
582   */
583 #define __HAL_RTC_ALARM_EXTI_ENABLE_RISING_FALLING_EDGE() do {                                             \
584                                                                __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();  \
585                                                                __HAL_RTC_ALARM_EXTI_ENABLE_FALLING_EDGE(); \
586                                                              } while(0U)
587 
588 /**
589   * @brief  Disable rising & falling edge trigger on the RTC Alarm associated EXTI line.
590   * @retval None.
591   */
592 #define __HAL_RTC_ALARM_EXTI_DISABLE_RISING_FALLING_EDGE() do {                                              \
593                                                                 __HAL_RTC_ALARM_EXTI_DISABLE_RISING_EDGE();  \
594                                                                 __HAL_RTC_ALARM_EXTI_DISABLE_FALLING_EDGE(); \
595                                                               } while(0U)
596 
597 /**
598   * @brief Check whether the RTC Alarm associated EXTI line interrupt flag is set or not.
599   * @retval Line Status.
600   */
601 #define __HAL_RTC_ALARM_EXTI_GET_FLAG()              (EXTI->PR & RTC_EXTI_LINE_ALARM_EVENT)
602 
603 /**
604   * @brief Clear the RTC Alarm associated EXTI line flag.
605   * @retval None.
606   */
607 #define __HAL_RTC_ALARM_EXTI_CLEAR_FLAG()            (EXTI->PR = RTC_EXTI_LINE_ALARM_EVENT)
608 
609 /**
610   * @brief Generate a Software interrupt on RTC Alarm associated EXTI line.
611   * @retval None.
612   */
613 #define __HAL_RTC_ALARM_EXTI_GENERATE_SWIT()         (EXTI->SWIER |= RTC_EXTI_LINE_ALARM_EVENT)
614 /**
615   * @}
616   */
617 
618 /* Include RTC HAL Extended module */
619 #include "stm32f2xx_hal_rtc_ex.h"
620 
621 /* Exported functions --------------------------------------------------------*/
622 
623 /** @addtogroup RTC_Exported_Functions
624   * @{
625   */
626 
627 /** @addtogroup RTC_Exported_Functions_Group1
628   * @{
629   */
630 /* Initialization and de-initialization functions  ****************************/
631 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc);
632 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc);
633 void              HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc);
634 void              HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc);
635 
636 /* Callbacks Register/UnRegister functions  ***********************************/
637 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
638 HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID, pRTC_CallbackTypeDef pCallback);
639 HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID);
640 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
641 /**
642   * @}
643   */
644 
645 /** @addtogroup RTC_Exported_Functions_Group2
646   * @{
647   */
648 /* RTC Time and Date functions ************************************************/
649 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format);
650 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format);
651 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format);
652 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format);
653 /**
654   * @}
655   */
656 
657 /** @addtogroup RTC_Exported_Functions_Group3
658   * @{
659   */
660 /* RTC Alarm functions ********************************************************/
661 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format);
662 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format);
663 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm);
664 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format);
665 void              HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc);
666 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout);
667 void              HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc);
668 /**
669   * @}
670   */
671 
672 /** @addtogroup RTC_Exported_Functions_Group4
673   * @{
674   */
675 /* Peripheral Control functions ***********************************************/
676 HAL_StatusTypeDef   HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc);
677 
678 /* RTC Daylight Saving Time functions *****************************************/
679 void              HAL_RTC_DST_Add1Hour(RTC_HandleTypeDef *hrtc);
680 void              HAL_RTC_DST_Sub1Hour(RTC_HandleTypeDef *hrtc);
681 void              HAL_RTC_DST_SetStoreOperation(RTC_HandleTypeDef *hrtc);
682 void              HAL_RTC_DST_ClearStoreOperation(RTC_HandleTypeDef *hrtc);
683 uint32_t          HAL_RTC_DST_ReadStoreOperation(RTC_HandleTypeDef *hrtc);
684 /**
685   * @}
686   */
687 
688 /** @addtogroup RTC_Exported_Functions_Group5
689   * @{
690   */
691 /* Peripheral State functions *************************************************/
692 HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc);
693 /**
694   * @}
695   */
696 
697 /**
698   * @}
699   */
700 
701 /* Private types -------------------------------------------------------------*/
702 /* Private variables ---------------------------------------------------------*/
703 /* Private constants ---------------------------------------------------------*/
704 
705 /** @defgroup RTC_Private_Constants RTC Private Constants
706   * @{
707   */
708 /* Masks Definition */
709 #define RTC_TR_RESERVED_MASK    ((uint32_t)(RTC_TR_HT  | RTC_TR_HU  | \
710                                             RTC_TR_MNT | RTC_TR_MNU | \
711                                             RTC_TR_ST  | RTC_TR_SU  | \
712                                             RTC_TR_PM))
713 #define RTC_DR_RESERVED_MASK    ((uint32_t)(RTC_DR_YT | RTC_DR_YU | \
714                                             RTC_DR_MT | RTC_DR_MU | \
715                                             RTC_DR_DT | RTC_DR_DU | \
716                                             RTC_DR_WDU))
717 #define RTC_ISR_RESERVED_MASK   ((uint32_t)(RTC_FLAGS_MASK | RTC_ISR_INIT))
718 #define RTC_INIT_MASK           0xFFFFFFFFU
719 #define RTC_RSF_MASK            ((uint32_t)~(RTC_ISR_INIT | RTC_ISR_RSF))
720 #define RTC_FLAGS_MASK          ((uint32_t)(RTC_FLAG_INITF   | RTC_FLAG_INITS  | \
721                                             RTC_FLAG_ALRAF   | RTC_FLAG_ALRAWF | \
722                                             RTC_FLAG_ALRBF   | RTC_FLAG_ALRBWF | \
723                                             RTC_FLAG_WUTF    | RTC_FLAG_WUTWF  | \
724                                             RTC_FLAG_TSF     | RTC_FLAG_TSOVF  | \
725                                             RTC_FLAG_RSF     | RTC_TAMPER_FLAGS_MASK))
726 
727 #define RTC_TIMEOUT_VALUE       1000U
728 
729 #define RTC_EXTI_LINE_ALARM_EVENT  EXTI_IMR_MR17  /*!< External interrupt line 17 connected to the RTC Alarm event */
730 /**
731   * @}
732   */
733 
734 /* Private macros ------------------------------------------------------------*/
735 
736 /** @defgroup RTC_Private_Macros RTC Private Macros
737   * @{
738   */
739 
740 /** @defgroup RTC_IS_RTC_Definitions RTC Private macros to check input parameters
741   * @{
742   */
743 #define IS_RTC_HOUR_FORMAT(FORMAT)     (((FORMAT) == RTC_HOURFORMAT_12) || \
744                                         ((FORMAT) == RTC_HOURFORMAT_24))
745 
746 #define IS_RTC_OUTPUT(OUTPUT) (((OUTPUT) == RTC_OUTPUT_DISABLE) || \
747                                ((OUTPUT) == RTC_OUTPUT_ALARMA)  || \
748                                ((OUTPUT) == RTC_OUTPUT_ALARMB)  || \
749                                ((OUTPUT) == RTC_OUTPUT_WAKEUP))
750 
751 #define IS_RTC_OUTPUT_POL(POL) (((POL) == RTC_OUTPUT_POLARITY_HIGH) || \
752                                 ((POL) == RTC_OUTPUT_POLARITY_LOW))
753 
754 #define IS_RTC_OUTPUT_TYPE(TYPE) (((TYPE) == RTC_OUTPUT_TYPE_OPENDRAIN) || \
755                                   ((TYPE) == RTC_OUTPUT_TYPE_PUSHPULL))
756 
757 #define IS_RTC_ASYNCH_PREDIV(PREDIV)   ((PREDIV) <= 0x7FU)
758 #define IS_RTC_SYNCH_PREDIV(PREDIV)    ((PREDIV) <= 0x1FFFU)
759 
760 #define IS_RTC_HOUR12(HOUR)            (((HOUR) > 0U) && ((HOUR) <= 12U))
761 #define IS_RTC_HOUR24(HOUR)            ((HOUR) <= 23U)
762 #define IS_RTC_MINUTES(MINUTES)        ((MINUTES) <= 59U)
763 #define IS_RTC_SECONDS(SECONDS)        ((SECONDS) <= 59U)
764 
765 #define IS_RTC_HOURFORMAT12(PM)  (((PM) == RTC_HOURFORMAT12_AM) || \
766                                   ((PM) == RTC_HOURFORMAT12_PM))
767 
768 #define IS_RTC_DAYLIGHT_SAVING(SAVE) (((SAVE) == RTC_DAYLIGHTSAVING_SUB1H) || \
769                                       ((SAVE) == RTC_DAYLIGHTSAVING_ADD1H) || \
770                                       ((SAVE) == RTC_DAYLIGHTSAVING_NONE))
771 
772 #define IS_RTC_STORE_OPERATION(OPERATION) (((OPERATION) == RTC_STOREOPERATION_RESET) || \
773                                            ((OPERATION) == RTC_STOREOPERATION_SET))
774 
775 #define IS_RTC_FORMAT(FORMAT) (((FORMAT) == RTC_FORMAT_BIN) || ((FORMAT) == RTC_FORMAT_BCD))
776 
777 #define IS_RTC_YEAR(YEAR)              ((YEAR) <= 99U)
778 #define IS_RTC_MONTH(MONTH)            (((MONTH) >= 1U) && ((MONTH) <= 12U))
779 #define IS_RTC_DATE(DATE)              (((DATE) >= 1U) && ((DATE) <= 31U))
780 
781 #define IS_RTC_WEEKDAY(WEEKDAY) (((WEEKDAY) == RTC_WEEKDAY_MONDAY)    || \
782                                  ((WEEKDAY) == RTC_WEEKDAY_TUESDAY)   || \
783                                  ((WEEKDAY) == RTC_WEEKDAY_WEDNESDAY) || \
784                                  ((WEEKDAY) == RTC_WEEKDAY_THURSDAY)  || \
785                                  ((WEEKDAY) == RTC_WEEKDAY_FRIDAY)    || \
786                                  ((WEEKDAY) == RTC_WEEKDAY_SATURDAY)  || \
787                                  ((WEEKDAY) == RTC_WEEKDAY_SUNDAY))
788 
789 #define IS_RTC_ALARM_DATE_WEEKDAY_DATE(DATE) (((DATE) > 0U) && ((DATE) <= 31U))
790 
791 #define IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(WEEKDAY) (((WEEKDAY) == RTC_WEEKDAY_MONDAY)    || \
792                                                     ((WEEKDAY) == RTC_WEEKDAY_TUESDAY)   || \
793                                                     ((WEEKDAY) == RTC_WEEKDAY_WEDNESDAY) || \
794                                                     ((WEEKDAY) == RTC_WEEKDAY_THURSDAY)  || \
795                                                     ((WEEKDAY) == RTC_WEEKDAY_FRIDAY)    || \
796                                                     ((WEEKDAY) == RTC_WEEKDAY_SATURDAY)  || \
797                                                     ((WEEKDAY) == RTC_WEEKDAY_SUNDAY))
798 
799 #define IS_RTC_ALARM_DATE_WEEKDAY_SEL(SEL) (((SEL) == RTC_ALARMDATEWEEKDAYSEL_DATE) || \
800                                             ((SEL) == RTC_ALARMDATEWEEKDAYSEL_WEEKDAY))
801 
802 #define IS_RTC_ALARM_MASK(MASK)  (((MASK) & ((uint32_t)~RTC_ALARMMASK_ALL)) == 0U)
803 
804 #define IS_RTC_ALARM(ALARM)      (((ALARM) == RTC_ALARM_A) || ((ALARM) == RTC_ALARM_B))
805 
806 /**
807   * @}
808   */
809 
810 /**
811   * @}
812   */
813 
814 /* Private functions ---------------------------------------------------------*/
815 
816 /** @defgroup RTC_Private_Functions RTC Private Functions
817   * @{
818   */
819 HAL_StatusTypeDef  RTC_EnterInitMode(RTC_HandleTypeDef *hrtc);
820 HAL_StatusTypeDef  RTC_ExitInitMode(RTC_HandleTypeDef *hrtc);
821 uint8_t            RTC_ByteToBcd2(uint8_t number);
822 uint8_t            RTC_Bcd2ToByte(uint8_t number);
823 /**
824   * @}
825   */
826 
827 /**
828   * @}
829   */
830 
831 /**
832   * @}
833   */
834 
835 #ifdef __cplusplus
836 }
837 #endif
838 
839 #endif /* STM32F2xx_HAL_RTC_H */
840