Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:09

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_timebase_rtc_alarm_template.c
0004   * @author  MCD Application Team
0005   * @brief   HAL time base based on the hardware RTC_ALARM Template.
0006   *
0007   *          This file override the native HAL time base functions (defined as weak)
0008   *          to use the RTC ALARM for time base generation:
0009   *           + Initializes the RTC peripheral to increment the seconds registers each 1ms
0010   *           + The alarm is configured to assert an interrupt when the RTC reaches 1ms
0011   *           + HAL_IncTick is called at each Alarm event and the time is reset to 00:00:00
0012   *           + HSE (default), LSE or LSI can be selected as RTC clock source
0013   *
0014   ******************************************************************************
0015   * @attention
0016   *
0017   * Copyright (c) 2017 STMicroelectronics.
0018   * All rights reserved.
0019   *
0020   * This software is licensed under terms that can be found in the LICENSE file
0021   * in the root directory of this software component.
0022   * If no LICENSE file comes with this software, it is provided AS-IS.
0023   *
0024   ******************************************************************************
0025  @verbatim
0026   ==============================================================================
0027                         ##### How to use this driver #####
0028   ==============================================================================
0029     [..]
0030     This file must be copied to the application folder and modified as follows:
0031     (#) Rename it to 'stm32h7xx_hal_timebase_rtc_alarm.c'
0032     (#) Add this file and the RTC HAL drivers to your project and uncomment
0033        HAL_RTC_MODULE_ENABLED define in stm32h7xx_hal_conf.h
0034 
0035     [..]
0036     (@) HAL RTC alarm and HAL RTC wakeup drivers can not be used with low power modes:
0037         The wake up capability of the RTC may be intrusive in case of prior low power mode
0038         configuration requiring different wake up sources.
0039         Application/Example behavior is no more guaranteed
0040     (@) The stm32h7xx_hal_timebase_tim use is recommended for the Applications/Examples
0041           requiring low power modes
0042 
0043   @endverbatim
0044   ******************************************************************************
0045   */
0046 
0047 /* Includes ------------------------------------------------------------------*/
0048 #include "stm32h7xx_hal.h"
0049 /** @addtogroup STM32H7xx_HAL_Driver
0050   * @{
0051   */
0052 
0053 /** @defgroup HAL_TimeBase_RTC_Alarm_Template  HAL TimeBase RTC Alarm Template
0054   * @ingroup RTEMSBSPsARMSTM32H7
0055   * @{
0056   */
0057 
0058 /* Private typedef -----------------------------------------------------------*/
0059 /* Private define ------------------------------------------------------------*/
0060 
0061 /* Uncomment the line below to select the appropriate RTC Clock source for your application:
0062   + RTC_CLOCK_SOURCE_HSE: can be selected for applications requiring timing precision.
0063   + RTC_CLOCK_SOURCE_LSE: can be selected for applications with low constraint on timing
0064                           precision.
0065   + RTC_CLOCK_SOURCE_LSI: can be selected for applications with low constraint on timing
0066                           precision.
0067   */
0068 #define RTC_CLOCK_SOURCE_HSE
0069 /* #define RTC_CLOCK_SOURCE_LSE */
0070 /* #define RTC_CLOCK_SOURCE_LSI */
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 /* RTC_CLOCK_SOURCE_LSE || RTC_CLOCK_SOURCE_LSI */
0077   #define RTC_ASYNCH_PREDIV       0U
0078   #define RTC_SYNCH_PREDIV        31U
0079 #endif /* RTC_CLOCK_SOURCE_HSE */
0080 
0081 /* Private macro -------------------------------------------------------------*/
0082 /* Private variables ---------------------------------------------------------*/
0083 static RTC_HandleTypeDef        hRTC_Handle;
0084 /* Private function prototypes -----------------------------------------------*/
0085 void RTC_Alarm_IRQHandler(void);
0086 /* Private functions ---------------------------------------------------------*/
0087 
0088 /**
0089   * @brief  This function configures the RTC_ALARMA as a time base source.
0090   *         The time source is configured to have 1ms time base with a dedicated
0091   *         Tick interrupt priority.
0092   * @note   This function is called  automatically at the beginning of program after
0093   *         reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
0094   * @param  TickPriority Tick interrupt priority.
0095   * @retval HAL status
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   /* Configure LSE as RTC clock source */
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   /* Configure LSI as RTC clock source */
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   /* Configure HSE as RTC clock source */
0119   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
0120   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
0121   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
0122   /* Ensure that RTC is clocked by 1MHz */
0123   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_1MHZ;
0124 #else
0125 #error Please select the RTC Clock source
0126 #endif /* RTC_CLOCK_SOURCE_LSE */
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       /* Enable RTC Clock */
0137       __HAL_RCC_RTC_ENABLE();
0138       /* The time base should be 1ms
0139          Time base = ((RTC_ASYNCH_PREDIV + 1) * (RTC_SYNCH_PREDIV + 1)) / RTC_CLOCK
0140          HSE as RTC clock
0141            Time base = ((99 + 1) * (9 + 1)) / 1MHz
0142                      = 1ms
0143          LSE as RTC clock
0144            Time base = ((31 + 1) * (0 + 1)) / 32.768KHz
0145                      = ~1ms
0146          LSI as RTC clock
0147            Time base = ((31 + 1) * (0 + 1)) / 32KHz
0148                      = 1ms
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      /* Disable the write protection for RTC registers */
0162      __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
0163 
0164      /* Disable the Alarm A interrupt */
0165      __HAL_RTC_ALARMA_DISABLE(&hRTC_Handle);
0166 
0167      /* Clear flag alarm A */
0168      __HAL_RTC_ALARM_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_ALRAF);
0169 
0170      counter = 0U;
0171      /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
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 /* RTC_ICSR_ALRAWF */
0177      {
0178        if (counter++ == (SystemCoreClock / 48U)) /* Timeout = ~ 1s */
0179        {
0180           status = HAL_ERROR;
0181        }
0182      }
0183   }
0184   if (status == HAL_OK)
0185   {
0186     hRTC_Handle.Instance->ALRMAR = (uint32_t)0x01U;
0187 
0188     /* Configure the Alarm state: Enable Alarm */
0189     __HAL_RTC_ALARMA_ENABLE(&hRTC_Handle);
0190     /* Configure the Alarm interrupt */
0191     __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
0192 
0193     /* RTC Alarm Interrupt Configuration: EXTI configuration */
0194     __HAL_RTC_ALARM_EXTI_ENABLE_IT();
0195     __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
0196 
0197     /* Check if the Initialization mode is set */
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 /* RTC_ISR_INITF */
0203     {
0204        /* Set the Initialization mode */
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 /* RTC_ISR_INITF */
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 /* RTC_ISR_INITF */
0216        {
0217          if (counter++ == (SystemCoreClock / 48U)) /* Timeout = ~ 1s */
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 /* RTC_ISR_INIT */
0234 
0235     /* Enable the write protection for RTC registers */
0236     __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
0237 
0238     /* Enable the RTC Alarm Interrupt */
0239     HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
0240 
0241     /* Configure the SysTick IRQ priority */
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   * @brief  Suspend Tick increment.
0258   * @note   Disable the tick increment by disabling RTC ALARM interrupt.
0259   * @retval None
0260   */
0261 void HAL_SuspendTick(void)
0262 {
0263   /* Disable the write protection for RTC registers */
0264   __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
0265   /* Disable RTC ALARM update Interrupt */
0266   __HAL_RTC_ALARM_DISABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
0267   /* Enable the write protection for RTC registers */
0268   __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
0269 }
0270 
0271 /**
0272   * @brief  Resume Tick increment.
0273   * @note   Enable the tick increment by Enabling RTC ALARM interrupt.
0274   * @retval None
0275   */
0276 void HAL_ResumeTick(void)
0277 {
0278   /* Disable the write protection for RTC registers */
0279   __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
0280   /* Enable RTC ALARM Update interrupt */
0281   __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
0282   /* Enable the write protection for RTC registers */
0283   __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
0284 }
0285 
0286 /**
0287   * @brief  ALARM A Event Callback in non blocking mode
0288   * @note   This function is called  when RTC_ALARM interrupt took place, inside
0289   * RTC_ALARM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
0290   * a global variable "uwTick" used as application time base.
0291   * @param  hrtc RTC handle
0292   * @retval None
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   /* Set the Initialization mode */
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 /* RTC_ISR_INIT */
0312   {
0313     if(counter++ == (SystemCoreClock /48U)) /* Timeout = ~ 1s */
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 /* RTC_ISR_INIT  */
0326   /* Enable the write protection for RTC registers */
0327   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
0328 }
0329 
0330 /**
0331   * @brief  This function handles RTC ALARM interrupt request.
0332   * @retval None
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