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 * Copyright (c) 2019 STMicroelectronics. 15 * All rights reserved. 16 * 17 * This software is licensed under terms that can be found in the LICENSE file 18 * in the root directory of this software component. 19 * If no LICENSE file comes with this software, it is provided AS-IS. 20 * 21 ****************************************************************************** 22 */ 23 24 /* Includes ------------------------------------------------------------------*/ 25 #include "stm32mp1xx_hal.h" 26 27 /** @addtogroup STM32MP1xx_HAL_Driver 28 * @{ 29 */ 30 31 /** @addtogroup HAL_TimeBase_TIM 32 * @{ 33 */ 34 35 /* Private typedef -----------------------------------------------------------*/ 36 /* Private define ------------------------------------------------------------*/ 37 /* Private macro -------------------------------------------------------------*/ 38 /* Private variables ---------------------------------------------------------*/ 39 TIM_HandleTypeDef TimHandle; 40 /* Private function prototypes -----------------------------------------------*/ 41 void TIM6_IRQHandler(void); 42 /* Private functions ---------------------------------------------------------*/ 43 44 /** 45 * @brief This function configures the TIM6 as a time base source. 46 * The time source is configured to have 1ms time base with a dedicated 47 * Tick interrupt priority. 48 * @note This function is called automatically at the beginning of program after 49 * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). 50 * @param TickPriority: Tick interrupt priority. 51 * @retval HAL status 52 */ HAL_InitTick(uint32_t TickPriority)53HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority) 54 { 55 RCC_ClkInitTypeDef clkconfig; 56 uint32_t uwTimclock; 57 uint32_t uwPrescalerValue = 0U; 58 uint32_t pFLatency; 59 60 /*Configure the TIM6 IRQ priority */ 61 HAL_NVIC_SetPriority(TIM6_IRQn, TickPriority ,0U); 62 63 /* Enable the TIM6 global Interrupt */ 64 HAL_NVIC_EnableIRQ(TIM6_IRQn); 65 66 /* Enable TIM6 clock */ 67 __HAL_RCC_TIM6_CLK_ENABLE(); 68 69 /* Get clock configuration */ 70 HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); 71 72 /* Compute TIM6 clock */ 73 uwTimclock = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_TIM6); 74 75 /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */ 76 uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U); 77 78 /* Initialize TIM6 */ 79 TimHandle.Instance = TIM6; 80 81 /* Initialize TIMx peripheral as follow: 82 + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base. 83 + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. 84 + ClockDivision = 0 85 + Counter direction = Up 86 */ 87 TimHandle.Init.Period = (1000000U / 1000U) - 1U; 88 TimHandle.Init.Prescaler = uwPrescalerValue; 89 TimHandle.Init.ClockDivision = 0U; 90 TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; 91 if(HAL_TIM_Base_Init(&TimHandle) == HAL_OK) 92 { 93 /* Start the TIM time Base generation in interrupt mode */ 94 return HAL_TIM_Base_Start_IT(&TimHandle); 95 } 96 97 /* Return function status */ 98 return HAL_ERROR; 99 } 100 101 /** 102 * @brief Suspend Tick increment. 103 * @note Disable the tick increment by disabling TIM6 update interrupt. 104 * @param None 105 * @retval None 106 */ HAL_SuspendTick(void)107void HAL_SuspendTick(void) 108 { 109 /* Disable TIM6 update Interrupt */ 110 __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE); 111 } 112 113 /** 114 * @brief Resume Tick increment. 115 * @note Enable the tick increment by Enabling TIM6 update interrupt. 116 * @param None 117 * @retval None 118 */ HAL_ResumeTick(void)119void HAL_ResumeTick(void) 120 { 121 /* Enable TIM6 Update interrupt */ 122 __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE); 123 } 124 125 /** 126 * @brief Period elapsed callback in non blocking mode 127 * @note This function is called when TIM6 interrupt took place, inside 128 * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment 129 * a global variable "uwTick" used as application time base. 130 * @param htim : TIM handle 131 * @retval None 132 */ HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)133void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) 134 { 135 HAL_IncTick(); 136 } 137 138 /** 139 * @brief This function handles TIM interrupt request. 140 * @param None 141 * @retval None 142 */ TIM6_IRQHandler(void)143void TIM6_IRQHandler(void) 144 { 145 HAL_TIM_IRQHandler(&TimHandle); 146 } 147 148 /** 149 * @} 150 */ 151 152 /** 153 * @} 154 */ 155