1 /**
2   ******************************************************************************
3   * @file    stm32f2xx_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 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   */
24 
25 /* Includes ------------------------------------------------------------------*/
26 #include "stm32f2xx_hal.h"
27 
28 /** @addtogroup STM32F2xx_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 TIM6_DAC_IRQHandler(void);
43 /* Private functions ---------------------------------------------------------*/
44 
45 /**
46   * @brief  This function configures the TIM6 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   HAL_StatusTypeDef     status;
61 
62 
63   /* Enable TIM6 clock */
64   __HAL_RCC_TIM6_CLK_ENABLE();
65 
66   /* Get clock configuration */
67   HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
68 
69   /* Get APB1 prescaler */
70   uwAPB1Prescaler = clkconfig.APB1CLKDivider;
71 
72   /* Compute TIM6 clock */
73   if (uwAPB1Prescaler == RCC_HCLK_DIV1)
74   {
75     uwTimclock = HAL_RCC_GetPCLK1Freq();
76   }
77   else
78   {
79     uwTimclock = 2 * HAL_RCC_GetPCLK1Freq();
80   }
81 
82   /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */
83   uwPrescalerValue = (uint32_t)((uwTimclock / 1000000U) - 1U);
84 
85   /* Initialize TIM6 */
86   TimHandle.Instance = TIM6;
87 
88   /* Initialize TIMx peripheral as follow:
89    + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base.
90    + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
91    + ClockDivision = 0
92    + Counter direction = Up
93    */
94   TimHandle.Init.Period = (1000000U / 1000U) - 1U;
95   TimHandle.Init.Prescaler = uwPrescalerValue;
96   TimHandle.Init.ClockDivision = 0;
97   TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
98   TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
99   status = HAL_TIM_Base_Init(&TimHandle);
100   if (status == HAL_OK)
101   {
102     /* Start the TIM time Base generation in interrupt mode */
103     status = HAL_TIM_Base_Start_IT(&TimHandle);
104     if (status == HAL_OK)
105     {
106       /* Enable the TIM6 global Interrupt */
107       HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
108 
109       if (TickPriority < (1UL << __NVIC_PRIO_BITS))
110       {
111         /* Enable the TIM6 global Interrupt */
112         HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority, 0);
113         uwTickPrio = TickPriority;
114       }
115       else
116       {
117         status = HAL_ERROR;
118       }
119     }
120   }
121 
122   /* Return function status */
123   return status;
124 }
125 
126 /**
127   * @brief  Suspend Tick increment.
128   * @note   Disable the tick increment by disabling TIM6 update interrupt.
129   * @param  None
130   * @retval None
131   */
HAL_SuspendTick(void)132 void HAL_SuspendTick(void)
133 {
134   /* Disable TIM6 update Interrupt */
135   __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);
136 }
137 
138 /**
139   * @brief  Resume Tick increment.
140   * @note   Enable the tick increment by Enabling TIM6 update interrupt.
141   * @param  None
142   * @retval None
143   */
HAL_ResumeTick(void)144 void HAL_ResumeTick(void)
145 {
146   /* Enable TIM6 Update interrupt */
147   __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
148 }
149 
150 /**
151   * @brief  Period elapsed callback in non blocking mode
152   * @note   This function is called  when TIM6 interrupt took place, inside
153   *         HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
154   *         a global variable "uwTick" used as application time base.
155   * @param  htim TIM handle
156   * @retval None
157   */
HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)158 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
159 {
160   HAL_IncTick();
161 }
162 
163 /**
164   * @brief  This function handles TIM interrupt request.
165   * @param  None
166   * @retval None
167   */
TIM6_DAC_IRQHandler(void)168 void TIM6_DAC_IRQHandler(void)
169 {
170   HAL_TIM_IRQHandler(&TimHandle);
171 }
172 
173 /**
174   * @}
175   */
176 
177 /**
178   * @}
179   */
180 
181 
182