File indexing completed on 2025-05-11 08:23:09
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 #include "stm32h7xx_hal.h"
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068 #define RTC_CLOCK_SOURCE_HSE
0069
0070
0071
0072 #ifdef RTC_CLOCK_SOURCE_HSE
0073 #define RTC_ASYNCH_PREDIV 99U
0074 #define RTC_SYNCH_PREDIV 9U
0075 #define RCC_RTCCLKSOURCE_1MHZ ((uint32_t)((uint32_t)RCC_BDCR_RTCSEL | (uint32_t)((HSE_VALUE/1000000U) << 12U)))
0076 #else
0077 #define RTC_ASYNCH_PREDIV 0U
0078 #define RTC_SYNCH_PREDIV 31U
0079 #endif
0080
0081
0082
0083 static RTC_HandleTypeDef hRTC_Handle;
0084
0085 void RTC_Alarm_IRQHandler(void);
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
0098 {
0099 __IO uint32_t counter = 0U;
0100
0101 RCC_OscInitTypeDef RCC_OscInitStruct;
0102 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
0103 HAL_StatusTypeDef status;
0104
0105 #ifdef RTC_CLOCK_SOURCE_LSE
0106
0107 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
0108 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
0109 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
0110 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
0111 #elif defined (RTC_CLOCK_SOURCE_LSI)
0112
0113 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
0114 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
0115 RCC_OscInitStruct.LSIState = RCC_LSI_ON;
0116 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
0117 #elif defined (RTC_CLOCK_SOURCE_HSE)
0118
0119 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
0120 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
0121 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
0122
0123 PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_1MHZ;
0124 #else
0125 #error Please select the RTC Clock source
0126 #endif
0127
0128 status = HAL_RCC_OscConfig(&RCC_OscInitStruct);
0129 if (status == HAL_OK)
0130 {
0131 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
0132 status = HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
0133 }
0134 if (status == HAL_OK)
0135 {
0136
0137 __HAL_RCC_RTC_ENABLE();
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150 hRTC_Handle.Instance = RTC;
0151 hRTC_Handle.Init.HourFormat = RTC_HOURFORMAT_24;
0152 hRTC_Handle.Init.AsynchPrediv = RTC_ASYNCH_PREDIV;
0153 hRTC_Handle.Init.SynchPrediv = RTC_SYNCH_PREDIV;
0154 hRTC_Handle.Init.OutPut = RTC_OUTPUT_DISABLE;
0155 hRTC_Handle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
0156 hRTC_Handle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
0157 status = HAL_RTC_Init(&hRTC_Handle);
0158 }
0159 if (status == HAL_OK)
0160 {
0161
0162 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
0163
0164
0165 __HAL_RTC_ALARMA_DISABLE(&hRTC_Handle);
0166
0167
0168 __HAL_RTC_ALARM_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_ALRAF);
0169
0170 counter = 0U;
0171
0172 #if defined(RTC_ICSR_ALRAWF)
0173 while ( READ_BIT(hRTC_Handle.Instance->ICSR, RTC_FLAG_ALRAWF) == (uint32_t)RESET)
0174 #else
0175 while (__HAL_RTC_ALARM_GET_FLAG(&hRTC_Handle, RTC_FLAG_ALRAWF) == (uint32_t)RESET)
0176 #endif
0177 {
0178 if (counter++ == (SystemCoreClock / 48U))
0179 {
0180 status = HAL_ERROR;
0181 }
0182 }
0183 }
0184 if (status == HAL_OK)
0185 {
0186 hRTC_Handle.Instance->ALRMAR = (uint32_t)0x01U;
0187
0188
0189 __HAL_RTC_ALARMA_ENABLE(&hRTC_Handle);
0190
0191 __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
0192
0193
0194 __HAL_RTC_ALARM_EXTI_ENABLE_IT();
0195 __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
0196
0197
0198 #if defined(RTC_ISR_INITF)
0199 if ((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
0200 #else
0201 if ((hRTC_Handle.Instance->ICSR & RTC_ICSR_INITF) == (uint32_t)RESET)
0202 #endif
0203 {
0204
0205 #if defined(RTC_ISR_INITF)
0206 hRTC_Handle.Instance->ISR = (uint32_t)RTC_INIT_MASK;
0207 #else
0208 hRTC_Handle.Instance->ICSR = (uint32_t)RTC_INIT_MASK;
0209 #endif
0210 counter = 0U;
0211 #if defined(RTC_ISR_INITF)
0212 while ((hRTC_Handle.Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
0213 #else
0214 while ((hRTC_Handle.Instance->ICSR & RTC_ICSR_INITF) == (uint32_t)RESET)
0215 #endif
0216 {
0217 if (counter++ == (SystemCoreClock / 48U))
0218 {
0219 status = HAL_ERROR;
0220 }
0221 }
0222 }
0223 }
0224 if (status == HAL_OK)
0225 {
0226 hRTC_Handle.Instance->DR = 0U;
0227 hRTC_Handle.Instance->TR = 0U;
0228
0229 #if defined(RTC_ISR_INIT)
0230 hRTC_Handle.Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
0231 #else
0232 hRTC_Handle.Instance->ICSR &= (uint32_t)~RTC_ICSR_INIT;
0233 #endif
0234
0235
0236 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
0237
0238
0239 HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
0240
0241
0242 if (TickPriority < (1UL << __NVIC_PRIO_BITS))
0243 {
0244 HAL_NVIC_SetPriority(RTC_Alarm_IRQn, TickPriority, 0U);
0245 uwTickPrio = TickPriority;
0246 }
0247 else
0248 {
0249 status = HAL_ERROR;
0250 }
0251
0252 }
0253 return status;
0254 }
0255
0256
0257
0258
0259
0260
0261 void HAL_SuspendTick(void)
0262 {
0263
0264 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
0265
0266 __HAL_RTC_ALARM_DISABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
0267
0268 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
0269 }
0270
0271
0272
0273
0274
0275
0276 void HAL_ResumeTick(void)
0277 {
0278
0279 __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
0280
0281 __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
0282
0283 __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
0284 }
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294 void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
0295 {
0296 __IO uint32_t counter = 0U;
0297
0298 HAL_IncTick();
0299
0300 __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
0301
0302
0303 #if defined(RTC_ISR_INIT)
0304 hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
0305
0306 while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
0307 #else
0308 hrtc->Instance->ICSR = (uint32_t)RTC_INIT_MASK;
0309
0310 while((hrtc->Instance->ICSR & RTC_ICSR_INITF) == (uint32_t)RESET)
0311 #endif
0312 {
0313 if(counter++ == (SystemCoreClock /48U))
0314 {
0315 break;
0316 }
0317 }
0318
0319 hrtc->Instance->DR = 0U;
0320 hrtc->Instance->TR = 0U;
0321 #if defined(RTC_ISR_INIT)
0322 hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
0323 #else
0324 hrtc->Instance->ICSR &= (uint32_t)~RTC_ICSR_INIT;
0325 #endif
0326
0327 __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
0328 }
0329
0330
0331
0332
0333
0334 void RTC_Alarm_IRQHandler(void)
0335 {
0336 HAL_RTC_AlarmIRQHandler(&hRTC_Handle);
0337 }
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347