1 /**
2   ******************************************************************************
3   * @file    stm32f1xx_hal_timebase_tim_template.c
4   * @author  MCD Application Team
5   * @brief   HAL time base based on the hardware TIM Template.
6   *
7   *          This file overrides the native HAL time base functions (defined as weak)
8   *          the TIM time base:
9   *           + Initializes the TIM peripheral generate a Period elapsed Event each 1ms
10   *           + HAL_IncTick is called inside HAL_TIM_PeriodElapsedCallback ie each 1ms
11   *
12   ******************************************************************************
13   * @attention
14   *
15   * Copyright (c) 2017 STMicroelectronics.
16   * All rights reserved.
17   *
18   * This software is licensed under terms that can be found in the LICENSE file
19   * in the root directory of this software component.
20   * If no LICENSE file comes with this software, it is provided AS-IS.
21   *
22   ******************************************************************************
23   */
24 
25 /* Includes ------------------------------------------------------------------*/
26 #include "stm32f1xx_hal.h"
27 
28 /** @addtogroup STM32F1xx_HAL_Driver
29   * @{
30   */
31 
32 /** @addtogroup HAL_TimeBase_TIM
33   * @{
34   */
35 
36 /* Private typedef -----------------------------------------------------------*/
37 /* Private define ------------------------------------------------------------*/
38 /* Private macro -------------------------------------------------------------*/
39 /* Private variables ---------------------------------------------------------*/
40 TIM_HandleTypeDef        TimHandle;
41 /* Private function prototypes -----------------------------------------------*/
42 void TIM2_IRQHandler(void);
43 /* Private functions ---------------------------------------------------------*/
44 
45 /**
46   * @brief  This function configures the TIM2 as a time base source.
47   *         The time source is configured to have 1ms time base with a dedicated
48   *         Tick interrupt priority.
49   * @note   This function is called  automatically at the beginning of program after
50   *         reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
51   * @param  TickPriority Tick interrupt priority.
52   * @retval HAL status
53   */
HAL_InitTick(uint32_t TickPriority)54 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
55 {
56   RCC_ClkInitTypeDef    clkconfig;
57   uint32_t              uwTimclock, uwAPB1Prescaler = 0U;
58   uint32_t              uwPrescalerValue = 0U;
59   uint32_t              pFLatency;
60 
61   /*Configure the TIM2 IRQ priority */
62   HAL_NVIC_SetPriority(TIM2_IRQn, TickPriority, 0U);
63 
64   /* Enable the TIM2 global Interrupt */
65   HAL_NVIC_EnableIRQ(TIM2_IRQn);
66 
67   /* Enable TIM2 clock */
68   __HAL_RCC_TIM2_CLK_ENABLE();
69 
70   /* Get clock configuration */
71   HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
72 
73   /* Get APB1 prescaler */
74   uwAPB1Prescaler = clkconfig.APB1CLKDivider;
75 
76   /* Compute TIM2 clock */
77   if (uwAPB1Prescaler == RCC_HCLK_DIV1)
78   {
79     uwTimclock = HAL_RCC_GetPCLK1Freq();
80   }
81   else
82   {
83     uwTimclock = 2 * HAL_RCC_GetPCLK1Freq();
84   }
85 
86   /* Compute the prescaler value to have TIM2 counter clock equal to 1MHz */
87   uwPrescalerValue = (uint32_t)((uwTimclock / 1000000U) - 1U);
88 
89   /* Initialize TIM2 */
90   TimHandle.Instance = TIM2;
91 
92   /* Initialize TIMx peripheral as follow:
93   + Period = [(TIM2CLK/1000) - 1]. to have a (1/1000) s time base.
94   + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
95   + ClockDivision = 0
96   + Counter direction = Up
97   */
98   TimHandle.Init.Period = (1000000U / 1000U) - 1U;
99   TimHandle.Init.Prescaler = uwPrescalerValue;
100   TimHandle.Init.ClockDivision = 0U;
101   TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
102   TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
103   status = HAL_TIM_Base_Init(&TimHandle);
104   if (status == HAL_OK)
105   {
106     /* Start the TIM time Base generation in interrupt mode */
107     status = HAL_TIM_Base_Start_IT(&TimHandle);
108     if (status == HAL_OK)
109     {
110       /* Enable the TIM2 global Interrupt */
111       HAL_NVIC_EnableIRQ(TIM2_IRQn);
112 
113       if (TickPriority < (1UL << __NVIC_PRIO_BITS))
114       {
115         /*Configure the TIM2 IRQ priority */
116         HAL_NVIC_SetPriority(TIM2_IRQn, TickPriority ,0);
117         uwTickPrio = TickPriority;
118       }
119       else
120       {
121         status = HAL_ERROR;
122       }
123     }
124   }
125 
126   /* Return function status */
127   return status;
128 }
129 
130 /**
131   * @brief  Suspend Tick increment.
132   * @note   Disable the tick increment by disabling TIM2 update interrupt.
133   * @retval None
134   */
HAL_SuspendTick(void)135 void HAL_SuspendTick(void)
136 {
137   /* Disable TIM2 update Interrupt */
138   __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);
139 }
140 
141 /**
142   * @brief  Resume Tick increment.
143   * @note   Enable the tick increment by Enabling TIM2 update interrupt.
144   * @retval None
145   */
HAL_ResumeTick(void)146 void HAL_ResumeTick(void)
147 {
148   /* Enable TIM2 Update interrupt */
149   __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
150 }
151 
152 /**
153   * @brief  Period elapsed callback in non blocking mode
154   * @note   This function is called  when TIM2 interrupt took place, inside
155   * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
156   * a global variable "uwTick" used as application time base.
157   * @param  htim TIM handle
158   * @retval None
159   */
HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)160 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
161 {
162   HAL_IncTick();
163 }
164 
165 /**
166   * @brief  This function handles TIM interrupt request.
167   * @retval None
168   */
TIM2_IRQHandler(void)169 void TIM2_IRQHandler(void)
170 {
171   HAL_TIM_IRQHandler(&TimHandle);
172 }
173 
174 /**
175   * @}
176   */
177 
178 /**
179   * @}
180   */
181