1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_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) 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   @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 'stm32l4xx_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 stm32l4xx_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 "stm32l4xx_hal.h"
43 
44 /** @addtogroup STM32L4xx_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 TIM6_DAC_IRQHandler(void);
59 /* Private functions ---------------------------------------------------------*/
60 
61 /**
62   * @brief  This function configures the TIM6 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, 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 TIM6 clock */
82     __HAL_RCC_TIM6_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 TIM6 clock */
91     if (uwAPB1Prescaler == RCC_HCLK_DIV1)
92     {
93       uwTimclock = HAL_RCC_GetPCLK1Freq();
94     }
95     else
96     {
97       uwTimclock = 2U * HAL_RCC_GetPCLK1Freq();
98     }
99 
100     /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */
101     uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
102 
103     /* Initialize TIM6 */
104     TimHandle.Instance = TIM6;
105 
106     /* Initialize TIMx peripheral as follows:
107     + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) 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) - 1U;
113     TimHandle.Init.Prescaler = uwPrescalerValue;
114     TimHandle.Init.ClockDivision = 0;
115     TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
116     TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
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 TIM6_DAC global Interrupt */
123         HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
124 
125         /* Configure the SysTick IRQ priority */
126         if (TickPriority < (1UL << __NVIC_PRIO_BITS))
127         {
128           /*Configure the TIM6_DAC IRQ priority */
129           HAL_NVIC_SetPriority(TIM6_DAC_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 TIM6 update interrupt.
159   * @retval None
160   */
HAL_SuspendTick(void)161 void HAL_SuspendTick(void)
162 {
163   /* Disable TIM6 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 TIM6 update interrupt.
170   * @retval None
171   */
HAL_ResumeTick(void)172 void HAL_ResumeTick(void)
173 {
174   /* Enable TIM6 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 TIM6 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   /* Prevent unused argument(s) compilation warning */
189   UNUSED(htim);
190 
191   HAL_IncTick();
192 }
193 
194 /**
195   * @brief  This function handles TIM interrupt request.
196   * @retval None
197   */
TIM6_DAC_IRQHandler(void)198 void TIM6_DAC_IRQHandler(void)
199 {
200   HAL_TIM_IRQHandler(&TimHandle);
201 }
202 
203 /**
204   * @}
205   */
206 
207 /**
208   * @}
209   */
210