stm32f1xx_hal_timebase_tim_template.c 5.3 KB

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