stm32g4xx_hal_timebase_tim_template.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. ******************************************************************************
  3. * @file stm32g4xx_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 to generate a Period elapsed Event each 1ms
  10. * + HAL_IncTick is called inside HAL_TIM_PeriodElapsedCallback ie each 1ms
  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. @verbatim
  23. ==============================================================================
  24. ##### How to use this driver #####
  25. ==============================================================================
  26. [..]
  27. This file must be copied to the application folder and modified as follows:
  28. (#) Rename it to 'stm32g4xx_hal_timebase_tim.c'
  29. (#) Add this file and the TIM HAL driver files to your project and make sure
  30. HAL_TIM_MODULE_ENABLED is defined in stm32g4xx_hal_conf.h
  31. [..]
  32. (@) The application needs to ensure that the time base is always set to 1 millisecond
  33. to have correct HAL operation.
  34. @endverbatim
  35. ******************************************************************************
  36. */
  37. /* Includes ------------------------------------------------------------------*/
  38. #include "stm32g4xx_hal.h"
  39. /** @addtogroup STM32G4xx_HAL_Driver
  40. * @{
  41. */
  42. /** @addtogroup HAL_TimeBase
  43. * @{
  44. */
  45. /* Private typedef -----------------------------------------------------------*/
  46. /* Private define ------------------------------------------------------------*/
  47. /* Private macro -------------------------------------------------------------*/
  48. /* Private variables ---------------------------------------------------------*/
  49. TIM_HandleTypeDef TimHandle;
  50. /* Private function prototypes -----------------------------------------------*/
  51. void TIM6_DAC_IRQHandler(void);
  52. /* Private functions ---------------------------------------------------------*/
  53. /**
  54. * @brief This function configures the TIM6 as a time base source.
  55. * The time source is configured to have 1ms time base with a dedicated
  56. * Tick interrupt priority.
  57. * @note This function is called automatically at the beginning of program after
  58. * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
  59. * @param TickPriority: Tick interrupt priority.
  60. * @retval HAL status
  61. */
  62. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  63. {
  64. RCC_ClkInitTypeDef clkconfig;
  65. uint32_t uwTimclock;
  66. uint32_t uwAPB1Prescaler;
  67. uint32_t uwPrescalerValue;
  68. uint32_t pFLatency;
  69. HAL_StatusTypeDef status;
  70. /* Configure the TIM6 IRQ priority */
  71. HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority, 0U);
  72. /* Enable the TIM6 global Interrupt */
  73. HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
  74. /* Enable TIM6 clock */
  75. __HAL_RCC_TIM6_CLK_ENABLE();
  76. /* Get clock configuration */
  77. HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
  78. /* Get APB1 prescaler */
  79. uwAPB1Prescaler = clkconfig.APB1CLKDivider;
  80. /* Compute TIM6 clock */
  81. if (uwAPB1Prescaler == RCC_HCLK_DIV1)
  82. {
  83. uwTimclock = HAL_RCC_GetPCLK1Freq();
  84. }
  85. else
  86. {
  87. uwTimclock = 2U * HAL_RCC_GetPCLK1Freq();
  88. }
  89. /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */
  90. uwPrescalerValue = (uint32_t)((uwTimclock / 1000000U) - 1U);
  91. /* Initialize TIM6 */
  92. TimHandle.Instance = TIM6;
  93. /* Initialize TIMx peripheral as follow:
  94. + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base.
  95. + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
  96. + ClockDivision = 0
  97. + Counter direction = Up
  98. */
  99. TimHandle.Init.Period = (1000000U / 1000U) - 1U;
  100. TimHandle.Init.Prescaler = uwPrescalerValue;
  101. TimHandle.Init.ClockDivision = 0;
  102. TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
  103. status = HAL_TIM_Base_Init(&TimHandle);
  104. if (status == HAL_OK)
  105. {
  106. /* Start the TIM time Base generation in interrupt mode */
  107. status = HAL_TIM_Base_Start_IT(&TimHandle);
  108. if (status == HAL_OK)
  109. {
  110. /* Configure the SysTick IRQ priority */
  111. if (TickPriority < (1UL << __NVIC_PRIO_BITS))
  112. {
  113. /* Configure the TIM IRQ priority */
  114. HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority, 0U);
  115. uwTickPrio = TickPriority;
  116. }
  117. else
  118. {
  119. status = HAL_ERROR;
  120. }
  121. }
  122. }
  123. /* Return function status */
  124. return status;
  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. */
  132. void HAL_SuspendTick(void)
  133. {
  134. /* Disable TIM6 update interrupt */
  135. __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);
  136. }
  137. /**
  138. * @brief Resume Tick increment.
  139. * @note Enable the tick increment by enabling TIM6 update interrupt.
  140. * @param None
  141. * @retval None
  142. */
  143. void HAL_ResumeTick(void)
  144. {
  145. /* Enable TIM6 update interrupt */
  146. __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
  147. }
  148. /**
  149. * @brief Period elapsed callback in non blocking mode
  150. * @note This function is called when TIM6 interrupt took place, inside
  151. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  152. * a global variable "uwTick" used as application time base.
  153. * @param htim : TIM handle
  154. * @retval None
  155. */
  156. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  157. {
  158. HAL_IncTick();
  159. }
  160. /**
  161. * @brief This function handles TIM interrupt request.
  162. * @param None
  163. * @retval None
  164. */
  165. void TIM6_DAC_IRQHandler(void)
  166. {
  167. HAL_TIM_IRQHandler(&TimHandle);
  168. }
  169. /**
  170. * @}
  171. */
  172. /**
  173. * @}
  174. */