stm32g0xx_hal_timebase_tim_template.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_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) 2018 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 'stm32g0xx_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 stm32g0xx_hal_conf.h
  32. [..]
  33. (@) The application needs to ensure that the time base is always set to 1 millisecond
  34. to have correct HAL operation.
  35. @endverbatim
  36. ******************************************************************************
  37. */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "stm32g0xx_hal.h"
  40. /** @addtogroup STM32G0xx_HAL_Driver
  41. * @{
  42. */
  43. /** @addtogroup HAL_TimeBase_TIM
  44. * @{
  45. */
  46. /* Private typedef -----------------------------------------------------------*/
  47. /* Private define ------------------------------------------------------------*/
  48. /* Private macro -------------------------------------------------------------*/
  49. /* Private variables ---------------------------------------------------------*/
  50. TIM_HandleTypeDef TimHandle = {.Init = {0}};
  51. /* Private function prototypes -----------------------------------------------*/
  52. void TIM14_IRQHandler(void);
  53. /* Private functions ---------------------------------------------------------*/
  54. /**
  55. * @brief This function configures the TIM14 as a time base source.
  56. * The time source is configured to have 1ms time base with a dedicated
  57. * Tick interrupt priority.
  58. * @note This function is called automatically at the beginning of program
  59. * after reset by HAL_Init() or at any time when clock is configured,
  60. * by HAL_RCC_ClockConfig().
  61. * @param TickPriority Tick interrupt priority.
  62. * @retval HAL status
  63. */
  64. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  65. {
  66. RCC_ClkInitTypeDef clkconfig;
  67. uint32_t uwTimclock;
  68. uint32_t uwAPB1Prescaler;
  69. uint32_t uwPrescalerValue;
  70. uint32_t pFLatency;
  71. HAL_StatusTypeDef status = HAL_OK;
  72. /* Check uwTickFreq for MisraC 2012 (even if uwTickFreq is a enum type that don't take the value zero)*/
  73. if ((uint32_t)uwTickFreq != 0U)
  74. {
  75. /* Enable TIM14 clock */
  76. __HAL_RCC_TIM14_CLK_ENABLE();
  77. /* Get clock configuration */
  78. HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
  79. /* Get APB1 prescaler */
  80. uwAPB1Prescaler = clkconfig.APB1CLKDivider;
  81. /* Compute TIM14 clock */
  82. if (uwAPB1Prescaler == RCC_HCLK_DIV1)
  83. {
  84. uwTimclock = HAL_RCC_GetPCLK1Freq();
  85. }
  86. else
  87. {
  88. uwTimclock = 2U*HAL_RCC_GetPCLK1Freq();
  89. }
  90. /* Compute the prescaler value to have TIM14 counter clock equal to 1MHz */
  91. uwPrescalerValue = (uint32_t)((uwTimclock / 1000000U) - 1U);
  92. /* Initialize TIM14 */
  93. TimHandle.Instance = TIM14;
  94. /* Initialize TIMx peripheral as follow:
  95. + Period = [(TIM14CLK/uwTickFreq) - 1]. to have a (1/uwTickFreq) s time base.
  96. + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
  97. + ClockDivision = 0
  98. + Counter direction = Up
  99. */
  100. TimHandle.Init.Period = (1000000U / (1000U / (uint32_t)uwTickFreq)) - 1U;
  101. TimHandle.Init.Prescaler = uwPrescalerValue;
  102. TimHandle.Init.ClockDivision = 0U;
  103. TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
  104. TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  105. if (HAL_TIM_Base_Init(&TimHandle) == HAL_OK)
  106. {
  107. /* Start the TIM time Base generation in interrupt mode */
  108. if (HAL_TIM_Base_Start_IT(&TimHandle) == HAL_OK)
  109. {
  110. /* Enable the TIM14 global Interrupt */
  111. HAL_NVIC_EnableIRQ(TIM14_IRQn);
  112. /* Configure the SysTick IRQ priority */
  113. if (TickPriority < (1UL << __NVIC_PRIO_BITS))
  114. {
  115. /*Configure the TIM14 IRQ priority */
  116. HAL_NVIC_SetPriority(TIM14_IRQn, TickPriority,0U);
  117. uwTickPrio = TickPriority;
  118. }
  119. else
  120. {
  121. status = HAL_ERROR;
  122. }
  123. }
  124. else
  125. {
  126. status = HAL_ERROR;
  127. }
  128. }
  129. else
  130. {
  131. status = HAL_ERROR;
  132. }
  133. }
  134. else
  135. {
  136. status = HAL_ERROR;
  137. }
  138. /* Return function status */
  139. return status;
  140. }
  141. /**
  142. * @brief Suspend Tick increment.
  143. * @note Disable the tick increment by disabling TIM14 update interrupt.
  144. * @retval None
  145. */
  146. void HAL_SuspendTick(void)
  147. {
  148. /* Disable TIM14 update interrupt */
  149. __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);
  150. }
  151. /**
  152. * @brief Resume Tick increment.
  153. * @note Enable the tick increment by enabling TIM14 update interrupt.
  154. * @retval None
  155. */
  156. void HAL_ResumeTick(void)
  157. {
  158. /* Enable TIM14 update interrupt */
  159. __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
  160. }
  161. /**
  162. * @brief Period elapsed callback in non blocking mode
  163. * @note This function is called when TIM14 interrupt took place, inside
  164. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  165. * a global variable "uwTick" used as application time base.
  166. * @param htim TIM handle
  167. * @retval None
  168. */
  169. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  170. {
  171. HAL_IncTick();
  172. }
  173. /**
  174. * @brief This function handles TIM interrupt request.
  175. * @retval None
  176. */
  177. void TIM14_IRQHandler(void)
  178. {
  179. HAL_TIM_IRQHandler(&TimHandle);
  180. }
  181. /**
  182. * @}
  183. */
  184. /**
  185. * @}
  186. */