1 /**
2   ******************************************************************************
3   * @file    stm32mp1xx_hal_timebase_tim.c
4   * @brief   HAL time base based on the hardware TIM.
5   *
6   *          This file override the native HAL time base functions (defined as weak)
7   *          the TIM time base:
8   *           + Intializes the TIM peripheral generate a Period elapsed Event each 1ms
9   *           + HAL_IncTick is called inside HAL_TIM_PeriodElapsedCallback ie each 1ms
10   *
11   ******************************************************************************
12   * @attention
13   *
14   * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
15   * All rights reserved.</center></h2>
16   *
17   * This software component is licensed by ST under BSD 3-Clause license,
18   * the "License"; You may not use this file except in compliance with the
19   * License. You may obtain a copy of the License at:
20   *                        opensource.org/licenses/BSD-3-Clause
21   *
22   ******************************************************************************
23   */
24 
25 /* Includes ------------------------------------------------------------------*/
26 #include "stm32mp1xx_hal.h"
27 
28 /** @addtogroup STM32MP1xx_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_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 
61     /*Configure the TIM6 IRQ priority */
62   HAL_NVIC_SetPriority(TIM6_IRQn, TickPriority ,0U);
63 
64   /* Enable the TIM6 global Interrupt */
65   HAL_NVIC_EnableIRQ(TIM6_IRQn);
66 
67   /* Enable TIM6 clock */
68   __HAL_RCC_TIM6_CLK_ENABLE();
69 
70   /* Get clock configuration */
71   HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
72 
73   /* Get APB1 prescaler */
74   uwAPB1Prescaler = clkconfig.APB1_Div;
75 
76   /* Compute TIM6 clock */
77   if (uwAPB1Prescaler == RCC_APB1_DIV1)
78   {
79     uwTimclock = HAL_RCCEx_GetPCLK1Freq();
80   }
81   else
82   {
83     uwTimclock = 2*HAL_RCCEx_GetPCLK1Freq();
84   }
85 
86   /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */
87   uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
88 
89   /* Initialize TIM6 */
90   TimHandle.Instance = TIM6;
91 
92   /* Initialize TIMx peripheral as follow:
93   + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base.
94   + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
95   + ClockDivision = 0
96   + Counter direction = Up
97   */
98   TimHandle.Init.Period = (1000000U / 1000U) - 1U;
99   TimHandle.Init.Prescaler = uwPrescalerValue;
100   TimHandle.Init.ClockDivision = 0U;
101   TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
102   if(HAL_TIM_Base_Init(&TimHandle) == HAL_OK)
103   {
104     /* Start the TIM time Base generation in interrupt mode */
105     return HAL_TIM_Base_Start_IT(&TimHandle);
106   }
107 
108   /* Return function status */
109   return HAL_ERROR;
110 }
111 
112 /**
113   * @brief  Suspend Tick increment.
114   * @note   Disable the tick increment by disabling TIM6 update interrupt.
115   * @param  None
116   * @retval None
117   */
HAL_SuspendTick(void)118 void HAL_SuspendTick(void)
119 {
120   /* Disable TIM6 update Interrupt */
121   __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);
122 }
123 
124 /**
125   * @brief  Resume Tick increment.
126   * @note   Enable the tick increment by Enabling TIM6 update interrupt.
127   * @param  None
128   * @retval None
129   */
HAL_ResumeTick(void)130 void HAL_ResumeTick(void)
131 {
132   /* Enable TIM6 Update interrupt */
133   __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
134 }
135 
136 /**
137   * @brief  Period elapsed callback in non blocking mode
138   * @note   This function is called  when TIM6 interrupt took place, inside
139   * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
140   * a global variable "uwTick" used as application time base.
141   * @param  htim : TIM handle
142   * @retval None
143   */
HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)144 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
145 {
146   HAL_IncTick();
147 }
148 
149 /**
150   * @brief  This function handles TIM interrupt request.
151   * @param  None
152   * @retval None
153   */
TIM6_IRQHandler(void)154 void TIM6_IRQHandler(void)
155 {
156   HAL_TIM_IRQHandler(&TimHandle);
157 }
158 
159 /**
160   * @}
161   */
162 
163 /**
164   * @}
165   */
166 
167 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
168