stm32f1xx_hal_timebase_tim_template.c 5.2 KB

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