1 /**
2   ******************************************************************************
3   * @file    stm32g0xx_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 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) 2018 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 'stm32g0xx_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 stm32g0xx_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 "stm32g0xx_hal.h"
43 
44 /** @addtogroup STM32G0xx_HAL_Driver
45   * @{
46   */
47 
48 /** @addtogroup HAL_TimeBase_TIM
49   * @{
50   */
51 
52 /* Private typedef -----------------------------------------------------------*/
53 /* Private define ------------------------------------------------------------*/
54 /* Private macro -------------------------------------------------------------*/
55 /* Private variables ---------------------------------------------------------*/
56 TIM_HandleTypeDef TimHandle = {.Init = {0}};
57 
58 /* Private function prototypes -----------------------------------------------*/
59 void TIM14_IRQHandler(void);
60 /* Private functions ---------------------------------------------------------*/
61 
62 /**
63   * @brief  This function configures the TIM14 as a time base source.
64   *         The time source is configured to have 1ms time base with a dedicated
65   *         Tick interrupt priority.
66   * @note   This function is called  automatically at the beginning of program
67   *         after reset by HAL_Init() or at any time when clock is configured,
68   *         by HAL_RCC_ClockConfig().
69   * @param  TickPriority Tick interrupt priority.
70   * @retval HAL status
71   */
HAL_InitTick(uint32_t TickPriority)72 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
73 {
74   RCC_ClkInitTypeDef    clkconfig;
75   uint32_t              uwTimclock;
76   uint32_t              uwAPB1Prescaler;
77   uint32_t              uwPrescalerValue;
78   uint32_t              pFLatency;
79   HAL_StatusTypeDef     status = HAL_OK;
80 
81   /* Check uwTickFreq for MisraC 2012 (even if uwTickFreq is a enum type that don't take the value zero)*/
82   if ((uint32_t)uwTickFreq != 0U)
83   {
84     /* Enable TIM14 clock */
85     __HAL_RCC_TIM14_CLK_ENABLE();
86 
87     /* Get clock configuration */
88     HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
89 
90     /* Get APB1 prescaler */
91     uwAPB1Prescaler = clkconfig.APB1CLKDivider;
92 
93     /* Compute TIM14 clock */
94     if (uwAPB1Prescaler == RCC_HCLK_DIV1)
95     {
96       uwTimclock = HAL_RCC_GetPCLK1Freq();
97     }
98     else
99     {
100       uwTimclock = 2U*HAL_RCC_GetPCLK1Freq();
101     }
102 
103     /* Compute the prescaler value to have TIM14 counter clock equal to 1MHz */
104     uwPrescalerValue = (uint32_t)((uwTimclock / 1000000U) - 1U);
105 
106     /* Initialize TIM14 */
107     TimHandle.Instance = TIM14;
108 
109     /* Initialize TIMx peripheral as follow:
110     + Period = [(TIM14CLK/uwTickFreq) - 1]. to have a (1/uwTickFreq) s time base.
111     + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
112     + ClockDivision = 0
113     + Counter direction = Up
114     */
115     TimHandle.Init.Period = (1000000U / (1000U / (uint32_t)uwTickFreq)) - 1U;
116     TimHandle.Init.Prescaler = uwPrescalerValue;
117     TimHandle.Init.ClockDivision = 0U;
118     TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
119     TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
120     if (HAL_TIM_Base_Init(&TimHandle) == HAL_OK)
121     {
122       /* Start the TIM time Base generation in interrupt mode */
123       if (HAL_TIM_Base_Start_IT(&TimHandle) == HAL_OK)
124       {
125         /* Enable the TIM14 global Interrupt */
126         HAL_NVIC_EnableIRQ(TIM14_IRQn);
127 
128         /* Configure the SysTick IRQ priority */
129         if (TickPriority < (1UL << __NVIC_PRIO_BITS))
130         {
131           /*Configure the TIM14 IRQ priority */
132           HAL_NVIC_SetPriority(TIM14_IRQn, TickPriority,0U);
133           uwTickPrio = TickPriority;
134         }
135         else
136         {
137           status = HAL_ERROR;
138         }
139       }
140       else
141       {
142         status = HAL_ERROR;
143       }
144     }
145     else
146     {
147       status = HAL_ERROR;
148     }
149   }
150   else
151   {
152       status = HAL_ERROR;
153   }
154 
155   /* Return function status */
156   return status;
157 }
158 
159 /**
160   * @brief  Suspend Tick increment.
161   * @note   Disable the tick increment by disabling TIM14 update interrupt.
162   * @retval None
163   */
HAL_SuspendTick(void)164 void HAL_SuspendTick(void)
165 {
166   /* Disable TIM14 update interrupt */
167   __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);
168 }
169 
170 /**
171   * @brief  Resume Tick increment.
172   * @note   Enable the tick increment by enabling TIM14 update interrupt.
173   * @retval None
174   */
HAL_ResumeTick(void)175 void HAL_ResumeTick(void)
176 {
177   /* Enable TIM14 update interrupt */
178   __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
179 }
180 
181 /**
182   * @brief  Period elapsed callback in non blocking mode
183   * @note   This function is called  when TIM14 interrupt took place, inside
184   * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
185   * a global variable "uwTick" used as application time base.
186   * @param  htim TIM handle
187   * @retval None
188   */
HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)189 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
190 {
191   HAL_IncTick();
192 }
193 
194 /**
195   * @brief  This function handles TIM interrupt request.
196   * @retval None
197   */
TIM14_IRQHandler(void)198 void TIM14_IRQHandler(void)
199 {
200   HAL_TIM_IRQHandler(&TimHandle);
201 }
202 
203 /**
204   * @}
205   */
206 
207 /**
208   * @}
209   */
210 
211