Subversion Repositories testOled

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f1xx_hal_timebase_rtc_alarm_template.c
  4.   * @author  MCD Application Team
  5.   * @brief   HAL time base based on the hardware RTC_ALARM.
  6.   *
  7.   *          This file override the native HAL time base functions (defined as weak)
  8.   *          to use the RTC ALARM for time base generation:
  9.   *           + Intializes the RTC peripheral to increment the seconds registers each 1ms
  10.   *           + The alarm is configured to assert an interrupt when the RTC reaches 1ms
  11.   *           + HAL_IncTick is called at each Alarm event and the time is reset to 00:00:00
  12.   *           + HSE (default), LSE or LSI can be selected as RTC clock source
  13.  @verbatim
  14.   ==============================================================================
  15.                         ##### How to use this driver #####
  16.   ==============================================================================
  17.     [..]
  18.     This file must be copied to the application folder and modified as follows:
  19.     (#) Rename it to 'stm32f1xx_hal_timebase_rtc_alarm.c'
  20.     (#) Add this file and the RTC HAL drivers to your project and uncomment
  21.        HAL_RTC_MODULE_ENABLED define in stm32f1xx_hal_conf.h
  22.  
  23.     [..]
  24.     (@) HAL RTC alarm and HAL RTC wakeup drivers can’t be used with low power modes:
  25.         The wake up capability of the RTC may be intrusive in case of prior low power mode
  26.         configuration requiring different wake up sources.
  27.         Application/Example behavior is no more guaranteed
  28.     (@) The stm32f1xx_hal_timebase_tim use is recommended for the Applications/Examples
  29.           requiring low power modes
  30.  
  31.   @endverbatim
  32.   ******************************************************************************
  33.   * @attention
  34.   *
  35.   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  36.   * All rights reserved.</center></h2>
  37.   *
  38.   * This software component is licensed by ST under BSD 3-Clause license,
  39.   * the "License"; You may not use this file except in compliance with the
  40.   * License. You may obtain a copy of the License at:
  41.   *                        opensource.org/licenses/BSD-3-Clause
  42.   *
  43.   ******************************************************************************
  44.   */
  45.  
  46. /* Includes ------------------------------------------------------------------*/
  47. #include "stm32f1xx_hal.h"
  48. /** @addtogroup STM32F1xx_HAL_Driver
  49.   * @{
  50.   */
  51.  
  52. /** @defgroup HAL_TimeBase_RTC_Alarm_Template  HAL TimeBase RTC Alarm Template
  53.   * @{
  54.   */
  55.  
  56. /* Private typedef -----------------------------------------------------------*/
  57. /* Private define ------------------------------------------------------------*/
  58.  
  59. /* Uncomment the line below to select the appropriate RTC Clock source for your application:
  60.   + RTC_CLOCK_SOURCE_HSE: can be selected for applications requiring timing precision.
  61.   + RTC_CLOCK_SOURCE_LSE: can be selected for applications with low constraint on timing
  62.                           precision.
  63.   + RTC_CLOCK_SOURCE_LSI: can be selected for applications with low constraint on timing
  64.                           precision.
  65.   */
  66. #define RTC_CLOCK_SOURCE_HSE
  67. /* #define RTC_CLOCK_SOURCE_LSE */
  68. /* #define RTC_CLOCK_SOURCE_LSI */
  69.  
  70. /* Private macro -------------------------------------------------------------*/
  71. /* Private variables ---------------------------------------------------------*/
  72. RTC_HandleTypeDef        hRTC_Handle;
  73. /* Private function prototypes -----------------------------------------------*/
  74. void RTC_Alarm_IRQHandler(void);
  75. /* Private functions ---------------------------------------------------------*/
  76.  
  77. /**
  78.   * @brief  This function configures the RTC_ALARMA as a time base source.
  79.   *         The time source is configured  to have 1ms time base with a dedicated
  80.   *         Tick interrupt priority.
  81.   * @note   This function is called  automatically at the beginning of program after
  82.   *         reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
  83.   * @param  TickPriority Tick interrupt priority.
  84.   * @retval HAL status
  85.   */
  86. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  87. {
  88.   __IO uint32_t counter = 0U;
  89.  
  90.   RCC_OscInitTypeDef        RCC_OscInitStruct;
  91.   RCC_PeriphCLKInitTypeDef  PeriphClkInitStruct;
  92.   HAL_StatusTypeDef         status;
  93.  
  94. #ifdef RTC_CLOCK_SOURCE_LSE
  95.   /* Configue LSE as RTC clock soucre */
  96.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
  97.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  98.   RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  99.   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  100. #elif defined (RTC_CLOCK_SOURCE_LSI)
  101.   /* Configue LSI as RTC clock soucre */
  102.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
  103.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  104.   RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  105.   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  106. #elif defined (RTC_CLOCK_SOURCE_HSE)
  107.   /* Configue HSE as RTC clock soucre */
  108.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  109.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  110.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  111.   PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_HSE_DIV128;
  112. #else
  113. #error Please select the RTC Clock source
  114. #endif /* RTC_CLOCK_SOURCE_LSE */
  115.   status = HAL_RCC_OscConfig(&RCC_OscInitStruct);
  116.   if (status == HAL_OK)
  117.   {
  118.     PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  119.     status = HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
  120.     if (status == HAL_OK)
  121.     {
  122.       /* Enable RTC Clock */
  123.       __HAL_RCC_RTC_ENABLE();
  124.  
  125.       /* Configure RTC time base to 10Khz */
  126.       hRTC_Handle.Instance = RTC;
  127.       hRTC_Handle.Init.AsynchPrediv = (HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_RTC) / 10000) - 1;
  128.       hRTC_Handle.Init.OutPut = RTC_OUTPUTSOURCE_NONE;
  129.       status = HAL_RTC_Init(&hRTC_Handle);
  130.     }
  131.   }
  132.   if (status == HAL_OK)
  133.   {
  134.     /* Disable the write protection for RTC registers */
  135.     __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
  136.  
  137.     /* Clear flag alarm A */
  138.     __HAL_RTC_ALARM_CLEAR_FLAG(&hRTC_Handle, RTC_FLAG_ALRAF);
  139.  
  140.     counter = 0U;
  141.     /* Wait till RTC ALRAF flag is set and if Time out is reached exit */
  142.     while (__HAL_RTC_ALARM_GET_FLAG(&hRTC_Handle, RTC_FLAG_ALRAF) != RESET)
  143.     {
  144.       if (counter++ == SystemCoreClock / 48U) /* Timeout = ~ 1s */
  145.       {
  146.         status = HAL_ERROR;
  147.       }
  148.     }
  149.   }
  150.   if (status == HAL_OK)
  151.   {
  152.     /* Set RTC COUNTER MSB word */
  153.     hRTC_Handle.Instance->ALRH = 0x00U;
  154.     /* Set RTC COUNTER LSB word */
  155.     hRTC_Handle.Instance->ALRL = 0x09U;
  156.  
  157.     /* RTC Alarm Interrupt Configuration: EXTI configuration */
  158.     __HAL_RTC_ALARM_EXTI_ENABLE_IT();
  159.     __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
  160.  
  161.     /* Clear Second and overflow flags */
  162.     CLEAR_BIT(hRTC_Handle.Instance->CRL, (RTC_FLAG_SEC | RTC_FLAG_OW));
  163.  
  164.     /* Set RTC COUNTER MSB word */
  165.     hRTC_Handle.Instance->CNTH = 0x00U;
  166.     /* Set RTC COUNTER LSB word */
  167.     hRTC_Handle.Instance->CNTL = 0x00U;
  168.  
  169.     /* Configure the Alarm interrupt */
  170.     __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
  171.  
  172.     /* Enable the write protection for RTC registers */
  173.     __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
  174.  
  175.     /* Wait till RTC is in INIT state and if Time out is reached exit */
  176.     counter = 0U;
  177.     while ((hRTC_Handle.Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET)
  178.     {
  179.       if (counter++ == SystemCoreClock / 48U) /* Timeout = ~ 1s */
  180.       {
  181.         status = HAL_ERROR;
  182.       }
  183.     }
  184.   }
  185.   if (status == HAL_OK)
  186.   {
  187.     /* Enable the RTC global Interrupt */
  188.     HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
  189.  
  190.     /* Configure the SysTick IRQ priority */
  191.     if (TickPriority < (1UL << __NVIC_PRIO_BITS))
  192.     {
  193.       HAL_NVIC_SetPriority(RTC_Alarm_IRQn, TickPriority ,0U);
  194.       uwTickPrio = TickPriority;
  195.     }
  196.     else
  197.     {
  198.       status = HAL_ERROR;
  199.     }
  200.   }
  201.  
  202.   return status;
  203. }
  204.  
  205. /**
  206.   * @brief  Suspend Tick increment.
  207.   * @note   Disable the tick increment by disabling RTC ALARM interrupt.
  208.   * @param  None
  209.   * @retval None
  210.   */
  211. void HAL_SuspendTick(void)
  212. {
  213.   /* Disable RTC ALARM update Interrupt */
  214.   __HAL_RTC_ALARM_DISABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
  215. }
  216.  
  217. /**
  218.   * @brief  Resume Tick increment.
  219.   * @note   Enable the tick increment by Enabling RTC ALARM interrupt.
  220.   * @param  None
  221.   * @retval None
  222.   */
  223. void HAL_ResumeTick(void)
  224. {
  225.   __IO uint32_t counter = 0U;
  226.  
  227.   /* Disable the write protection for RTC registers */
  228.   __HAL_RTC_WRITEPROTECTION_DISABLE(&hRTC_Handle);
  229.  
  230.   /* Set RTC COUNTER MSB word */
  231.   hRTC_Handle.Instance->CNTH = 0x00U;
  232.   /* Set RTC COUNTER LSB word */
  233.   hRTC_Handle.Instance->CNTL = 0x00U;
  234.  
  235.   /* Clear Second and overflow flags */
  236.   CLEAR_BIT(hRTC_Handle.Instance->CRL, (RTC_FLAG_SEC | RTC_FLAG_OW | RTC_FLAG_ALRAF));
  237.  
  238.   /* Enable RTC ALARM Update interrupt */
  239.   __HAL_RTC_ALARM_ENABLE_IT(&hRTC_Handle, RTC_IT_ALRA);
  240.  
  241.   /* Enable the write protection for RTC registers */
  242.   __HAL_RTC_WRITEPROTECTION_ENABLE(&hRTC_Handle);
  243.  
  244.   /* Wait till RTC is in INIT state and if Time out is reached exit */
  245.   while ((hRTC_Handle.Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET)
  246.   {
  247.     if (counter++ == SystemCoreClock / 48U) /* Timeout = ~ 1s */
  248.     {
  249.       break;
  250.     }
  251.   }
  252. }
  253.  
  254. /**
  255.   * @brief  ALARM A Event Callback in non blocking mode
  256.   * @note   This function is called  when RTC_ALARM interrupt took place, inside
  257.   * RTC_ALARM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  258.   * a global variable "uwTick" used as application time base.
  259.   * @param  hrtc RTC handle
  260.   * @retval None
  261.   */
  262. void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
  263. {
  264.   __IO uint32_t counter = 0U;
  265.  
  266.   HAL_IncTick();
  267.  
  268.   __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  269.  
  270.   /* Set RTC COUNTER MSB word */
  271.   WRITE_REG(hrtc->Instance->CNTH, 0x00U);
  272.   /* Set RTC COUNTER LSB word */
  273.   WRITE_REG(hrtc->Instance->CNTL, 0x00U);
  274.  
  275.   /* Clear Second and overflow flags */
  276.   CLEAR_BIT(hrtc->Instance->CRL, (RTC_FLAG_SEC | RTC_FLAG_OW));
  277.  
  278.   /* Enable the write protection for RTC registers */
  279.   __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  280.  
  281.   /* Wait till RTC is in INIT state and if Time out is reached exit */
  282.   while ((hrtc->Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET)
  283.   {
  284.     if (counter++ == SystemCoreClock / 48U) /* Timeout = ~ 1s */
  285.     {
  286.       break;
  287.     }
  288.   }
  289. }
  290.  
  291. /**
  292.   * @brief  This function handles RTC ALARM interrupt request.
  293.   * @retval None
  294.   */
  295. void RTC_Alarm_IRQHandler(void)
  296. {
  297.   HAL_RTC_AlarmIRQHandler(&hRTC_Handle);
  298. }
  299.  
  300. /**
  301.   * @}
  302.   */
  303.  
  304. /**
  305.   * @}
  306.   */
  307.  
  308. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  309.