1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_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 override the native HAL time base functions (defined as weak)
8   *          the TIM time base:
9   *           + Initializes the TIM peripheral to 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) 2016 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  @verbatim
24   ==============================================================================
25                         ##### How to use this driver #####
26   ==============================================================================
27     [..]
28     This file must be copied to the application folder and modified as follows:
29     (#) Rename it to 'stm32l0xx_hal_timebase_tim.c'
30     (#) Add this file and the TIM HAL driver files to your project and make sure
31        HAL_TIM_MODULE_ENABLED is defined in stm32l0xx_hal_conf.h
32 
33     [..]
34     (@) The application needs to ensure that the time base is always set to 1 millisecond
35        to have correct HAL operation.
36 
37   @endverbatim
38   ******************************************************************************
39   */
40 
41 /* Includes ------------------------------------------------------------------*/
42 #include "stm32l0xx_hal.h"
43 
44 /** @addtogroup STM32L0xx_HAL_Driver
45   * @{
46   */
47 
48 /** @addtogroup HAL_TimeBase
49   * @{
50   */
51 
52 /* Private typedef -----------------------------------------------------------*/
53 /* Private define ------------------------------------------------------------*/
54 /* Private macro -------------------------------------------------------------*/
55 /* Private variables ---------------------------------------------------------*/
56 TIM_HandleTypeDef        TimHandle;
57 /* Private function prototypes -----------------------------------------------*/
58 void TIM21_IRQHandler(void);
59 /* Private functions ---------------------------------------------------------*/
60 
61 /**
62   * @brief  This function configures the TIM21 as a time base source.
63   *         The time source is configured  to have 1ms time base with a dedicated
64   *         Tick interrupt priority.
65   * @note   This function is called  automatically at the beginning of program after
66   *         reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
67   * @param  TickPriority: Tick interrupt priority.
68   * @retval HAL status
69   */
HAL_InitTick(uint32_t TickPriority)70 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
71 {
72   RCC_ClkInitTypeDef    clkconfig;
73   uint32_t              uwTimclock;
74   uint32_t              uwAPB2Prescaler;
75   uint32_t              uwPrescalerValue;
76   uint32_t              pFLatency;
77   HAL_StatusTypeDef     status;
78 
79   /* Configure the TIM21 IRQ priority */
80   HAL_NVIC_SetPriority(TIM21_IRQn, TickPriority, 0U);
81 
82   /* Enable the TIM21 global Interrupt */
83   HAL_NVIC_EnableIRQ(TIM21_IRQn);
84 
85   /* Enable TIM21 clock */
86   __HAL_RCC_TIM21_CLK_ENABLE();
87 
88   /* Get clock configuration */
89   HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
90 
91   /* Get APB2 prescaler */
92   uwAPB2Prescaler = clkconfig.APB2CLKDivider;
93 
94   /* Compute TIM21 clock */
95   if (uwAPB2Prescaler == RCC_HCLK_DIV1)
96   {
97     uwTimclock = HAL_RCC_GetPCLK2Freq();
98   }
99   else
100   {
101     uwTimclock = 2U * HAL_RCC_GetPCLK2Freq();
102   }
103 
104   /* Compute the prescaler value to have TIM21 counter clock equal to 1MHz */
105   uwPrescalerValue = (uint32_t)((uwTimclock / 1000000U) - 1U);
106 
107   /* Initialize TIM21 */
108   TimHandle.Instance = TIM21;
109 
110   /* Initialize TIMx peripheral as follow:
111   + Period = [(TIM21CLK/1000) - 1]. to have a (1/1000) s time base.
112   + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
113   + ClockDivision = 0
114   + Counter direction = Up
115   */
116   TimHandle.Init.Period = (1000000U / 1000U) - 1U;
117   TimHandle.Init.Prescaler = uwPrescalerValue;
118   TimHandle.Init.ClockDivision = 0;
119   TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
120   status = HAL_TIM_Base_Init(&TimHandle);
121   if (status == HAL_OK)
122   {
123     /* Start the TIM time Base generation in interrupt mode */
124     status = HAL_TIM_Base_Start_IT(&TimHandle);
125     if (status == HAL_OK)
126     {
127       /* Configure the SysTick IRQ priority */
128       if (TickPriority < (1UL << __NVIC_PRIO_BITS))
129       {
130         /* Configure the TIM IRQ priority */
131         HAL_NVIC_SetPriority(TIM21_IRQn, TickPriority, 0U);
132         uwTickPrio = TickPriority;
133       }
134       else
135       {
136         status = HAL_ERROR;
137       }
138     }
139   }
140 
141   /* Return function status */
142   return status;
143 }
144 
145 /**
146   * @brief  Suspend Tick increment.
147   * @note   Disable the tick increment by disabling TIM21 update interrupt.
148   * @param  None
149   * @retval None
150   */
HAL_SuspendTick(void)151 void HAL_SuspendTick(void)
152 {
153   /* Disable TIM21 update interrupt */
154   __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);
155 }
156 
157 /**
158   * @brief  Resume Tick increment.
159   * @note   Enable the tick increment by enabling TIM21 update interrupt.
160   * @param  None
161   * @retval None
162   */
HAL_ResumeTick(void)163 void HAL_ResumeTick(void)
164 {
165   /* Enable TIM21 update interrupt */
166   __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
167 }
168 
169 /**
170   * @brief  Period elapsed callback in non blocking mode
171   * @note   This function is called  when TIM21 interrupt took place, inside
172   * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
173   * a global variable "uwTick" used as application time base.
174   * @param  htim : TIM handle
175   * @retval None
176   */
HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)177 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
178 {
179   HAL_IncTick();
180 }
181 
182 /**
183   * @brief  This function handles TIM interrupt request.
184   * @param  None
185   * @retval None
186   */
TIM21_IRQHandler(void)187 void TIM21_IRQHandler(void)
188 {
189   HAL_TIM_IRQHandler(&TimHandle);
190 }
191 
192 /**
193   * @}
194   */
195 
196 /**
197   * @}
198   */
199 
200