STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
stm32f7xx_hal_timebase_rtc_alarm_template.c
Go to the documentation of this file.
1 
64 /* Includes ------------------------------------------------------------------*/
65 #include "stm32f7xx_hal.h"
74 /* Private typedef -----------------------------------------------------------*/
75 /* Private define ------------------------------------------------------------*/
76 
77 /* Uncomment the line below to select the appropriate RTC Clock source for your application:
78  + RTC_CLOCK_SOURCE_HSE: can be selected for applications requiring timing precision.
79  + RTC_CLOCK_SOURCE_LSE: can be selected for applications with low constraint on timing
80  precision.
81  + RTC_CLOCK_SOURCE_LSI: can be selected for applications with low constraint on timing
82  precision.
83  */
84 #define RTC_CLOCK_SOURCE_HSE
85 /* #define RTC_CLOCK_SOURCE_LSE */
86 /* #define RTC_CLOCK_SOURCE_LSI */
87 
88 #ifdef RTC_CLOCK_SOURCE_HSE
89  #define RTC_ASYNCH_PREDIV 99U
90  #define RTC_SYNCH_PREDIV 9U
91  #define RCC_RTCCLKSOURCE_1MHZ ((uint32_t)((uint32_t)RCC_BDCR_RTCSEL | (uint32_t)((HSE_VALUE/1000000U) << 16U)))
92 #else /* RTC_CLOCK_SOURCE_LSE || RTC_CLOCK_SOURCE_LSI */
93  #define RTC_ASYNCH_PREDIV 0U
94  #define RTC_SYNCH_PREDIV 31U
95 #endif /* RTC_CLOCK_SOURCE_HSE */
96 
97 /* Private macro -------------------------------------------------------------*/
98 /* Private variables ---------------------------------------------------------*/
100 /* Private function prototypes -----------------------------------------------*/
101 void RTC_Alarm_IRQHandler(void);
102 /* Private functions ---------------------------------------------------------*/
103 
113 HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority)
114 {
115  __IO uint32_t counter = 0U;
116 
117  RCC_OscInitTypeDef RCC_OscInitStruct;
119 
120 #ifdef RTC_CLOCK_SOURCE_LSE
121  /* Configue LSE as RTC clock soucre */
122  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
123  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
124  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
125  PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
126 #elif defined (RTC_CLOCK_SOURCE_LSI)
127  /* Configue LSI as RTC clock soucre */
128  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
129  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
130  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
131  PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
132 #elif defined (RTC_CLOCK_SOURCE_HSE)
133  /* Configue HSE as RTC clock soucre */
134  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
135  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
136  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
137  /* Ensure that RTC is clocked by 1MHz */
138  PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_1MHZ;
139 #else
140 #error Please select the RTC Clock source
141 #endif /* RTC_CLOCK_SOURCE_LSE */
142 
143  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK)
144  {
145  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
146  if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) == HAL_OK)
147  {
148  /* Enable RTC Clock */
150  /* The time base should be 1ms
151  Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK
152  HSE as RTC clock
153  Time base = ((99 + 1) * (9 + 1)) / 1MHz
154  = 1ms
155  LSE as RTC clock
156  Time base = ((31 + 1) * (0 + 1)) / 32.768KHz
157  = ~1ms
158  LSI as RTC clock
159  Time base = ((31 + 1) * (0 + 1)) / 32KHz
160  = 1ms
161  */
162  hRTC_Handle.Instance = RTC;
163  hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24;
164  hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
165  hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
166  hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE;
169  HAL_RTC_Init(&hRTC_Handle);
170 
171  /* Disable the write protection for RTC registers */
172  __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
173 
174  /* Disable the Alarm A interrupt */
175  __HAL_RTC_ALARMA_DISABLE(&hRTC_Handle);
176 
177  /* Clear flag alarm A */
179 
180  counter = 0U;
181  /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
182  while(__HAL_RTC_ALARM_GET_FLAG(&hRTC_Handle, RTC_FLAG_ALRAWF) == RESET)
183  {
184  if(counter++ == (SystemCoreClock /48U)) /* Timeout = ~ 1s */
185  {
186  return HAL_ERROR;
187  }
188  }
189 
190  hRTC_Handle.Instance->ALRMAR = (uint32_t)0x01U;
191 
192  /* Configure the Alarm state: Enable Alarm */
193  __HAL_RTC_ALARMA_ENABLE(&hRTC_Handle);
194  /* Configure the Alarm interrupt */
195  __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
196 
197  /* RTC Alarm Interrupt Configuration: EXTI configuration */
200 
201  /* Check if the Initialization mode is set */
202  if((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
203  {
204  /* Set the Initialization mode */
205  hRTC_Handle.Instance->ISR = (uint32_t)RTC_INIT_MASK;
206  counter = 0U;
207  while((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
208  {
209  if(counter++ == (SystemCoreClock /48U)) /* Timeout = ~ 1s */
210  {
211  return HAL_ERROR;
212  }
213  }
214  }
215  hRTC_Handle.Instance->DR = 0U;
216  hRTC_Handle.Instance->TR = 0U;
217 
218  hRTC_Handle.Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
219 
220  /* Enable the write protection for RTC registers */
221  __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
222 
223  HAL_NVIC_SetPriority(RTC_Alarm_IRQn, TickPriority, 0U);
225  return HAL_OK;
226  }
227  }
228  return HAL_ERROR;
229 }
230 
237 void HAL_SuspendTick(void)
238 {
239  /* Disable the write protection for RTC registers */
240  __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
241  /* Disable RTC ALARM update Interrupt */
243  /* Enable the write protection for RTC registers */
244  __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
245 }
246 
253 void HAL_ResumeTick(void)
254 {
255  /* Disable the write protection for RTC registers */
256  __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
257  /* Enable RTC ALARM Update interrupt */
258  __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
259  /* Enable the write protection for RTC registers */
260  __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
261 }
262 
272 {
273  __IO uint32_t counter = 0U;
274 
275  HAL_IncTick();
276 
278 
279  /* Set the Initialization mode */
280  hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
281 
282  while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
283  {
284  if(counter++ == (SystemCoreClock /48U)) /* Timeout = ~ 1s */
285  {
286  break;
287  }
288  }
289 
290  hrtc->Instance->DR = 0U;
291  hrtc->Instance->TR = 0U;
292 
293  hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
294 
295  /* Enable the write protection for RTC registers */
297 }
298 
305 {
306  HAL_RTC_AlarmIRQHandler(&hRTC_Handle);
307 }
308 
317 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
#define __HAL_RTC_ALARM_CLEAR_FLAG(__HANDLE__, __FLAG__)
Clear the RTC Alarm&#39;s pending flags.
RTC_TypeDef * Instance
#define __HAL_RTC_WRITEPROTECTION_DISABLE(__HANDLE__)
Disable the write protection for RTC registers.
#define RTC
Definition: stm32f745xx.h:1268
uint32_t SystemCoreClock
#define RCC_PERIPHCLK_RTC
HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
#define RTC_IT_ALRA
#define RTC_FLAG_ALRAF
void HAL_SuspendTick(void)
Suspend Tick increment.
#define RCC_OSCILLATORTYPE_LSI
#define __HAL_RTC_ALARMA_ENABLE(__HANDLE__)
Enable the RTC ALARMA peripheral.
RCC_PLLInitTypeDef PLL
#define RTC_ISR_INITF
Definition: stm32f745xx.h:5871
HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
#define RCC_RTCCLKSOURCE_LSI
#define RCC_RTCCLKSOURCE_LSE
__IO uint32_t ALRMAR
Definition: stm32f745xx.h:724
#define RCC_LSI_ON
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
ALARM A Event Callback in non blocking mode.
#define __IO
Definition: core_cm0.h:213
RCC extended clocks structure definition.
#define RTC_HOURFORMAT_24
void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)
This file contains all the functions prototypes for the HAL module driver.
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
This function configures the RTC_ALARMA as a time base source. The time source is configured to have ...
#define RCC_PLL_NONE
__IO uint32_t TR
Definition: stm32f745xx.h:717
#define __HAL_RTC_ALARM_DISABLE_IT(__HANDLE__, __INTERRUPT__)
Disable the RTC Alarm interrupt.
void HAL_NVIC_EnableIRQ(IRQn_Type IRQn)
#define RTC_ISR_INIT
Definition: stm32f745xx.h:5870
void HAL_IncTick(void)
This function is called to increment a global variable "uwTick" used as application time base...
#define RTC_OUTPUT_POLARITY_HIGH
#define __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE()
Enable rising edge trigger on the RTC Alarm associated Exti line.
void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
#define RCC_HSE_ON
#define RCC_OSCILLATORTYPE_HSE
HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
#define RTC_INIT_MASK
__IO uint32_t ISR
Definition: stm32f745xx.h:720
#define __HAL_RTC_ALARMA_DISABLE(__HANDLE__)
Disable the RTC ALARMA peripheral.
RTC_HandleTypeDef hRTC_Handle
#define RTC_FLAG_ALRAWF
RCC Internal/External Oscillator (HSE, HSI, LSE and LSI) configuration structure definition.
#define __HAL_RTC_ALARM_ENABLE_IT(__HANDLE__, __INTERRUPT__)
Enable the RTC Alarm interrupt.
void RTC_Alarm_IRQHandler(void)
This function handles RTC ALARM interrupt request.
__IO uint32_t DR
Definition: stm32f745xx.h:718
#define __HAL_RCC_RTC_ENABLE()
Macros to enable or disable the RTC clock.
RTC_InitTypeDef Init
RTC Handle Structure definition.
#define RTC_OUTPUT_DISABLE
void HAL_ResumeTick(void)
Resume Tick increment.
#define __HAL_RTC_WRITEPROTECTION_ENABLE(__HANDLE__)
Enable the write protection for RTC registers.
#define __HAL_RTC_ALARM_EXTI_ENABLE_IT()
Enable interrupt on the RTC Alarm associated Exti line.
HAL_StatusTypeDef
HAL Status structures definition.
#define RCC_OSCILLATORTYPE_LSE
#define RTC_OUTPUT_TYPE_OPENDRAIN
#define __HAL_RTC_ALARM_GET_FLAG(__HANDLE__, __FLAG__)
Get the selected RTC Alarm&#39;s flag status.
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct
#define RCC_LSE_ON