1 /**
2   ******************************************************************************
3   * @file    stm32wbxx_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   *             when uwTickFreq is set to default value, else 10 ms or
11   *             100 ms, depending of above global variable value.
12   *           + HAL_IncTick is called inside HAL_TIM_PeriodElapsedCallback
13   *
14   ******************************************************************************
15   * @attention
16   *
17   * Copyright (c) 2019 STMicroelectronics.
18   * All rights reserved.
19   *
20   * This software is licensed under terms that can be found in the LICENSE file
21   * in the root directory of this software component.
22   * If no LICENSE file comes with this software, it is provided AS-IS.
23   *
24   ******************************************************************************
25   @verbatim
26   ==============================================================================
27                         ##### How to use this driver #####
28   ==============================================================================
29     [..]
30     This file must be copied to the application folder and modified as follows:
31     (#) Rename it to 'stm32wlxx_hal_timebase_tim.c'
32     (#) Add this file and the TIM HAL drivers to your project and uncomment
33        HAL_TIM_MODULE_ENABLED define in stm32wlxx_hal_conf.h
34 
35   @endverbatim
36   */
37 
38 /* Includes ------------------------------------------------------------------*/
39 #include "stm32wbxx_hal.h"
40 
41 /** @addtogroup STM32WBxx_HAL_Driver
42   * @{
43   */
44 
45 /** @addtogroup HAL_TimeBase_TIM
46   * @{
47   */
48 
49 /* Private typedef -----------------------------------------------------------*/
50 /* Private define ------------------------------------------------------------*/
51 /* Private macro -------------------------------------------------------------*/
52 /* Private variables ---------------------------------------------------------*/
53 extern TIM_HandleTypeDef TimHandle;
54 TIM_HandleTypeDef        TimHandle;
55 
56 /* Private function prototypes -----------------------------------------------*/
57 void TIM2_IRQHandler(void);
58 /* Private functions ---------------------------------------------------------*/
59 
60 /**
61   * @brief  This function configures the TIM2 as a time base source.
62   *         The time source is configured to have 1ms time base with a dedicated
63   *         Tick interrupt priority.
64   * @note   This function is called automatically at the beginning of program after
65   *         reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
66   * @param  TickPriority Tick interrupt priority.
67   * @retval HAL status
68   */
HAL_InitTick(uint32_t TickPriority)69 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
70 {
71   RCC_ClkInitTypeDef    clkconfig;
72   uint32_t              uwTimclock;
73   uint32_t              uwAPB1Prescaler;
74   uint32_t              uwPrescalerValue;
75   uint32_t              pFLatency;
76 HAL_StatusTypeDef     status = HAL_OK;
77 
78   /* Check uwTickFreq for MisraC 2012 (even if uwTickFreq is a enum type that don't take the value zero)*/
79   if ((uint32_t)uwTickFreq != 0U)
80   {
81     /* Enable TIM2 clock */
82     __HAL_RCC_TIM2_CLK_ENABLE();
83 
84     /* Get clock configuration */
85     HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
86 
87     /* Get APB1 prescaler */
88     uwAPB1Prescaler = clkconfig.APB1CLKDivider;
89 
90     /* Compute TIM2 clock */
91     if (uwAPB1Prescaler == RCC_HCLK_DIV1)
92     {
93       uwTimclock = HAL_RCC_GetPCLK1Freq();
94     }
95     else
96     {
97       uwTimclock = 2*HAL_RCC_GetPCLK1Freq();
98     }
99 
100     /* Compute the prescaler value to have TIM2 counter clock equal to 1MHz */
101     uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
102 
103     /* Initialize TIM2 */
104     TimHandle.Instance = TIM2;
105 
106     /* Initialize TIMx peripheral as follow:
107     + Period = [(TIM2CLK/uwTickFreq) - 1]. to have a (1/uwTickFreq) s time base.
108     + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
109     + ClockDivision = 0
110     + Counter direction = Up
111     */
112     TimHandle.Init.Period = (1000000U / (1000U / (uint32_t)uwTickFreq)) - 1U;
113     TimHandle.Init.Prescaler = uwPrescalerValue;
114     TimHandle.Init.ClockDivision = 0U;
115     TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
116     TimHandle.Init.RepetitionCounter = 0U;
117     if (HAL_TIM_Base_Init(&TimHandle) == HAL_OK)
118     {
119       /* Start the TIM time Base generation in interrupt mode */
120       if (HAL_TIM_Base_Start_IT(&TimHandle) == HAL_OK)
121       {
122         /* Enable the TIM2 global Interrupt */
123         HAL_NVIC_EnableIRQ(TIM2_IRQn);
124 
125         /* Configure the SysTick IRQ priority */
126         if (TickPriority < (1UL << __NVIC_PRIO_BITS))
127         {
128           /*Configure the TIM2 IRQ priority */
129           HAL_NVIC_SetPriority(TIM2_IRQn, TickPriority ,0U);
130           uwTickPrio = TickPriority;
131         }
132         else
133         {
134           status = HAL_ERROR;
135         }
136       }
137       else
138       {
139         status = HAL_ERROR;
140       }
141     }
142     else
143     {
144       status = HAL_ERROR;
145     }
146   }
147   else
148   {
149       status = HAL_ERROR;
150   }
151 
152   /* Return function status */
153   return status;
154 }
155 
156 /**
157   * @brief  Suspend Tick increment.
158   * @note   Disable the tick increment by disabling TIM2 update interrupt.
159   * @retval None
160   */
HAL_SuspendTick(void)161 void HAL_SuspendTick(void)
162 {
163   /* Disable TIM2 update Interrupt */
164   __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);
165 }
166 
167 /**
168   * @brief  Resume Tick increment.
169   * @note   Enable the tick increment by Enabling TIM2 update interrupt.
170   * @retval None
171   */
HAL_ResumeTick(void)172 void HAL_ResumeTick(void)
173 {
174   /* Enable TIM2 Update interrupt */
175   __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
176 }
177 
178 /**
179   * @brief  Period elapsed callback in non blocking mode
180   * @note   This function is called  when TIM2 interrupt took place, inside
181   * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
182   * a global variable "uwTick" used as application time base.
183   * @param  htim : TIM handle
184   * @retval None
185   */
HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)186 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
187 {
188   HAL_IncTick();
189 }
190 
191 /**
192   * @brief  This function handles TIM interrupt request.
193   * @retval None
194   */
TIM2_IRQHandler(void)195 void TIM2_IRQHandler(void)
196 {
197   HAL_TIM_IRQHandler(&TimHandle);
198 }
199 
200 /**
201   * @}
202   */
203 
204 /**
205   * @}
206   */
207