1 /**
2   ******************************************************************************
3   * @file    stm32h7xx_hal_timebase_tim_template.c
4   * @author  MCD Application Team
5   * @brief   HAL time base based on the hardware TIM.
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   *           + 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 'stm32h7xx_hal_timebase_tim.c'
30     (#) Add this file and the TIM HAL drivers to your project and uncomment
31        HAL_TIM_MODULE_ENABLED define in stm32h7xx_hal_conf.h
32 
33   @endverbatim
34   ******************************************************************************
35   */
36 
37 /* Includes ------------------------------------------------------------------*/
38 #include "stm32h7xx_hal.h"
39 
40 /* Private typedef -----------------------------------------------------------*/
41 /* Private define ------------------------------------------------------------*/
42 /* Private macro -------------------------------------------------------------*/
43 /* Private variables ---------------------------------------------------------*/
44 static TIM_HandleTypeDef        TimHandle;
45 /* Private function prototypes -----------------------------------------------*/
46 void TIM6_DAC_IRQHandler(void);
47 /* Private functions ---------------------------------------------------------*/
48 
49 /**
50   * @brief  This function configures the TIM6 as a time base source.
51   *         The time source is configured to have 1ms time base with a dedicated
52   *         Tick interrupt priority.
53   * @note   This function is called  automatically at the beginning of program after
54   *         reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
55   * @param  TickPriority Tick interrupt priority.
56   * @retval HAL status
57   */
HAL_InitTick(uint32_t TickPriority)58 HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority)
59 {
60   RCC_ClkInitTypeDef    clkconfig;
61   uint32_t              uwTimclock, uwAPB1Prescaler;
62   uint32_t              uwPrescalerValue;
63   uint32_t              pFLatency;
64   HAL_StatusTypeDef     status;
65 
66   /* Enable TIM6 clock */
67   __HAL_RCC_TIM6_CLK_ENABLE();
68 
69   /* Get clock configuration */
70   HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
71 
72   /* Get APB1 prescaler */
73   uwAPB1Prescaler = clkconfig.APB1CLKDivider;
74 
75   /* Compute TIM6 clock */
76   if (uwAPB1Prescaler == RCC_HCLK_DIV1)
77   {
78     uwTimclock = HAL_RCC_GetPCLK1Freq();
79   }
80   else
81   {
82     uwTimclock = 2UL * HAL_RCC_GetPCLK1Freq();
83   }
84 
85   /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */
86   uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
87 
88   /* Initialize TIM6 */
89   TimHandle.Instance = TIM6;
90 
91   /* Initialize TIMx peripheral as follow:
92   + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base.
93   + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
94   + ClockDivision = 0
95   + Counter direction = Up
96   */
97   TimHandle.Init.Period = (1000000U / 1000U) - 1U;
98   TimHandle.Init.Prescaler = uwPrescalerValue;
99   TimHandle.Init.ClockDivision = 0U;
100   TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
101   status = HAL_TIM_Base_Init(&TimHandle);
102   if (status == HAL_OK)
103   {
104     /* Start the TIM time Base generation in interrupt mode */
105     status = HAL_TIM_Base_Start_IT(&TimHandle);
106     if (status == HAL_OK)
107     {
108 	  /* Enable the TIM6 global Interrupt */
109       HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
110 
111       if (TickPriority < (1UL << __NVIC_PRIO_BITS))
112       {
113         /* Enable the TIM6 global Interrupt */
114         HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority, 0);
115         uwTickPrio = TickPriority;
116       }
117       else
118       {
119         status = HAL_ERROR;
120       }
121    }
122 }
123 
124   /* Return function status */
125   return status;
126 }
127 
128 /**
129   * @brief  Suspend Tick increment.
130   * @note   Disable the tick increment by disabling TIM6 update interrupt.
131   * @param  None
132   * @retval None
133   */
HAL_SuspendTick(void)134 void HAL_SuspendTick(void)
135 {
136   /* Disable TIM6 update Interrupt */
137   __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);
138 }
139 
140 /**
141   * @brief  Resume Tick increment.
142   * @note   Enable the tick increment by Enabling TIM6 update interrupt.
143   * @param  None
144   * @retval None
145   */
HAL_ResumeTick(void)146 void HAL_ResumeTick(void)
147 {
148   /* Enable TIM6 Update interrupt */
149   __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
150 }
151 
152 /**
153   * @brief  Period elapsed callback in non blocking mode
154   * @note   This function is called  when TIM6 interrupt took place, inside
155   * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
156   * a global variable "uwTick" used as application time base.
157   * @param  htim TIM handle
158   * @retval None
159   */
HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)160 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
161 {
162   /* Prevent unused argument(s) compilation warning */
163   UNUSED(htim);
164 
165   HAL_IncTick();
166 }
167 
168 /**
169   * @brief  This function handles TIM interrupt request.
170   * @param  None
171   * @retval None
172   */
TIM6_DAC_IRQHandler(void)173 void TIM6_DAC_IRQHandler(void)
174 {
175   HAL_TIM_IRQHandler(&TimHandle);
176 }
177 
178