STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
stm32f7xx_hal_timebase_rtc_wakeup_template.c
Go to the documentation of this file.
1 
65 /* Includes ------------------------------------------------------------------*/
66 #include "stm32f7xx_hal.h"
75 /* Private typedef -----------------------------------------------------------*/
76 /* Private define ------------------------------------------------------------*/
77 
78 /* Uncomment the line below to select the appropriate RTC Clock source for your application:
79  + RTC_CLOCK_SOURCE_HSE: can be selected for applications requiring timing precision.
80  + RTC_CLOCK_SOURCE_LSE: can be selected for applications with low constraint on timing
81  precision.
82  + RTC_CLOCK_SOURCE_LSI: can be selected for applications with low constraint on timing
83  precision.
84  */
85 #define RTC_CLOCK_SOURCE_HSE
86 /* #define RTC_CLOCK_SOURCE_LSE */
87 /* #define RTC_CLOCK_SOURCE_LSI */
88 
89 #ifdef RTC_CLOCK_SOURCE_HSE
90  #define RTC_ASYNCH_PREDIV 99U
91  #define RTC_SYNCH_PREDIV 9U
92  #define RCC_RTCCLKSOURCE_1MHZ ((uint32_t)((uint32_t)RCC_BDCR_RTCSEL | (uint32_t)((HSE_VALUE/1000000U) << 16U)))
93 #else /* RTC_CLOCK_SOURCE_LSE || RTC_CLOCK_SOURCE_LSI */
94  #define RTC_ASYNCH_PREDIV 0U
95  #define RTC_SYNCH_PREDIV 31U
96 #endif /* RTC_CLOCK_SOURCE_HSE */
97 
98 /* Private macro -------------------------------------------------------------*/
99 /* Private variables ---------------------------------------------------------*/
101 
102 /* Private function prototypes -----------------------------------------------*/
103 void RTC_WKUP_IRQHandler(void);
104 
105 /* Private functions ---------------------------------------------------------*/
106 
120 HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority)
121 {
122  __IO uint32_t counter = 0U;
123 
124  RCC_OscInitTypeDef RCC_OscInitStruct;
126 
127 #ifdef RTC_CLOCK_SOURCE_LSE
128  /* Configue LSE as RTC clock soucre */
129  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
130  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
131  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
132  PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
133 #elif defined (RTC_CLOCK_SOURCE_LSI)
134  /* Configue LSI as RTC clock soucre */
135  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
136  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
137  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
138  PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
139 #elif defined (RTC_CLOCK_SOURCE_HSE)
140  /* Configue HSE as RTC clock soucre */
141  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
142  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
143  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
144  /* Ensure that RTC is clocked by 1MHz */
145  PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_1MHZ;
146 #else
147 #error Please select the RTC Clock source
148 #endif /* RTC_CLOCK_SOURCE_LSE */
149 
150  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK)
151  {
152  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
153  if(HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) == HAL_OK)
154  {
155  /* Enable RTC Clock */
157  /* The time base should be 1ms
158  Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK
159  HSE as RTC clock
160  Time base = ((99 + 1) * (9 + 1)) / 1Mhz
161  = 1ms
162  LSE as RTC clock
163  Time base = ((31 + 1) * (0 + 1)) / 32.768Khz
164  = ~1ms
165  LSI as RTC clock
166  Time base = ((31 + 1) * (0 + 1)) / 32Khz
167  = 1ms
168  */
169  hRTC_Handle.Instance = RTC;
170  hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24;
171  hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
172  hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
173  hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE;
176  HAL_RTC_Init(&hRTC_Handle);
177 
178  /* Disable the write protection for RTC registers */
179  __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
180 
181  /* Disable the Wake-up Timer */
182  __HAL_RTC_WAKEUPTIMER_DISABLE(&hRTC_Handle);
183 
184  /* In case of interrupt mode is used, the interrupt source must disabled */
186 
187  /* Wait till RTC WUTWF flag is set */
188  while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(&hRTC_Handle, RTC_FLAG_WUTWF) == RESET)
189  {
190  if(counter++ == (SystemCoreClock /48U))
191  {
192  return HAL_ERROR;
193  }
194  }
195 
196  /* Clear PWR wake up Flag */
198 
199  /* Clear RTC Wake Up timer Flag */
201 
202  /* Configure the Wake-up Timer counter */
203  hRTC_Handle.Instance->WUTR = (uint32_t)0U;
204 
205  /* Clear the Wake-up Timer clock source bits in CR register */
206  hRTC_Handle.Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
207 
208  /* Configure the clock source */
209  hRTC_Handle.Instance->CR |= (uint32_t)RTC_WAKEUPCLOCK_CK_SPRE_16BITS;
210 
211  /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
213 
215 
216  /* Configure the Interrupt in the RTC_CR register */
218 
219  /* Enable the Wake-up Timer */
220  __HAL_RTC_WAKEUPTIMER_ENABLE(&hRTC_Handle);
221 
222  /* Enable the write protection for RTC registers */
223  __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
224 
225  HAL_NVIC_SetPriority(RTC_WKUP_IRQn, TickPriority, 0U);
227  return HAL_OK;
228  }
229  }
230  return HAL_ERROR;
231 }
232 
239 void HAL_SuspendTick(void)
240 {
241  /* Disable the write protection for RTC registers */
242  __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
243  /* Disable WAKE UP TIMER Interrupt */
245  /* Enable the write protection for RTC registers */
246  __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
247 }
248 
255 void HAL_ResumeTick(void)
256 {
257  /* Disable the write protection for RTC registers */
258  __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
259  /* Enable WAKE UP TIMER interrupt */
261  /* Enable the write protection for RTC registers */
262  __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
263 }
264 
274 {
275  HAL_IncTick();
276 }
277 
284 {
285  HAL_RTCEx_WakeUpTimerIRQHandler(&hRTC_Handle);
286 }
287 
296 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
RTC_TypeDef * Instance
#define __HAL_RTC_WRITEPROTECTION_DISABLE(__HANDLE__)
Disable the write protection for RTC registers.
#define RTC_IT_WUT
void RTC_WKUP_IRQHandler(void)
This function handles WAKE UP TIMER interrupt request.
__IO uint32_t WUTR
Definition: stm32f745xx.h:722
#define __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE()
Enable rising edge trigger on the RTC WakeUp Timer associated Exti line.
#define RTC
Definition: stm32f745xx.h:1268
#define __HAL_PWR_CLEAR_FLAG(__FLAG__)
Clear the PWR&#39;s pending flags.
uint32_t SystemCoreClock
RTC_HandleTypeDef hRTC_Handle
#define __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(__HANDLE__, __FLAG__)
Clear the RTC Wake Up timer&#39;s pending flags.
#define RCC_PERIPHCLK_RTC
HAL_StatusTypeDef HAL_RCCEx_PeriphCLKConfig(RCC_PeriphCLKInitTypeDef *PeriphClkInit)
void HAL_ResumeTick(void)
Resume Tick increment.
#define __HAL_RTC_WAKEUPTIMER_DISABLE_IT(__HANDLE__, __INTERRUPT__)
Disable the RTC WakeUpTimer interrupt.
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
This function configures the RTC_WKUP as a time base source. The time source is configured to have 1m...
#define __HAL_RTC_WAKEUPTIMER_DISABLE(__HANDLE__)
Disable the RTC WakeUp Timer peripheral.
#define RTC_FLAG_WUTWF
#define RCC_OSCILLATORTYPE_LSI
#define __HAL_RTC_WAKEUPTIMER_GET_FLAG(__HANDLE__, __FLAG__)
Get the selected RTC WakeUpTimer&#39;s flag status.
RCC_PLLInitTypeDef PLL
HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
#define RCC_RTCCLKSOURCE_LSI
#define RCC_RTCCLKSOURCE_LSE
#define RCC_LSI_ON
#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)
void HAL_SuspendTick(void)
Suspend Tick increment.
This file contains all the functions prototypes for the HAL module driver.
#define RCC_PLL_NONE
void HAL_NVIC_EnableIRQ(IRQn_Type IRQn)
#define __HAL_RTC_WAKEUPTIMER_ENABLE_IT(__HANDLE__, __INTERRUPT__)
Enable the RTC WakeUpTimer interrupt.
#define __HAL_RTC_WAKEUPTIMER_ENABLE(__HANDLE__)
Enable the RTC WakeUp Timer peripheral.
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 RCC_HSE_ON
#define RCC_OSCILLATORTYPE_HSE
HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
#define __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT()
Enable interrupt on the RTC WakeUp Timer associated Exti line.
RCC Internal/External Oscillator (HSE, HSI, LSE and LSI) configuration structure definition.
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
Wake Up Timer Event Callback in non blocking mode.
#define RTC_WAKEUPCLOCK_CK_SPRE_16BITS
#define __HAL_RCC_RTC_ENABLE()
Macros to enable or disable the RTC clock.
#define PWR_FLAG_WU
RTC_InitTypeDef Init
#define RTC_CR_WUCKSEL
Definition: stm32f745xx.h:5854
RTC Handle Structure definition.
#define RTC_OUTPUT_DISABLE
#define __HAL_RTC_WRITEPROTECTION_ENABLE(__HANDLE__)
Enable the write protection for RTC registers.
#define RTC_FLAG_WUTF
HAL_StatusTypeDef
HAL Status structures definition.
#define RCC_OSCILLATORTYPE_LSE
#define RTC_OUTPUT_TYPE_OPENDRAIN
__IO uint32_t CR
Definition: stm32f745xx.h:719
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct
#define RCC_LSE_ON