stm32f1xx_hal_timebase_tim_template.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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</center></h2>
  16. *
  17. * Redistribution and use in source and binary forms, with or without modification,
  18. * are permitted provided that the following conditions are met:
  19. * 1. Redistributions of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  25. * may be used to endorse or promote products derived from this software
  26. * without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  34. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. ******************************************************************************
  40. */
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "stm32f1xx_hal.h"
  43. /** @addtogroup STM32F1xx_HAL_Driver
  44. * @{
  45. */
  46. /** @addtogroup HAL_TimeBase_TIM
  47. * @{
  48. */
  49. /* Private typedef -----------------------------------------------------------*/
  50. /* Private define ------------------------------------------------------------*/
  51. /* Private macro -------------------------------------------------------------*/
  52. /* Private variables ---------------------------------------------------------*/
  53. TIM_HandleTypeDef TimHandle;
  54. /* Private function prototypes -----------------------------------------------*/
  55. void TIM2_IRQHandler(void);
  56. /* Private functions ---------------------------------------------------------*/
  57. /**
  58. * @brief This function configures the TIM2 as a time base source.
  59. * The time source is configured to have 1ms time base with a dedicated
  60. * Tick interrupt priority.
  61. * @note This function is called automatically at the beginning of program after
  62. * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
  63. * @param TickPriority: Tick interrupt priority.
  64. * @retval HAL status
  65. */
  66. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  67. {
  68. RCC_ClkInitTypeDef clkconfig;
  69. uint32_t uwTimclock, uwAPB1Prescaler = 0U;
  70. uint32_t uwPrescalerValue = 0U;
  71. uint32_t pFLatency;
  72. /*Configure the TIM2 IRQ priority */
  73. HAL_NVIC_SetPriority(TIM2_IRQn, TickPriority, 0U);
  74. /* Enable the TIM2 global Interrupt */
  75. HAL_NVIC_EnableIRQ(TIM2_IRQn);
  76. /* Enable TIM2 clock */
  77. __HAL_RCC_TIM2_CLK_ENABLE();
  78. /* Get clock configuration */
  79. HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
  80. /* Get APB1 prescaler */
  81. uwAPB1Prescaler = clkconfig.APB1CLKDivider;
  82. /* Compute TIM2 clock */
  83. if (uwAPB1Prescaler == RCC_HCLK_DIV1)
  84. {
  85. uwTimclock = HAL_RCC_GetPCLK1Freq();
  86. }
  87. else
  88. {
  89. uwTimclock = 2 * HAL_RCC_GetPCLK1Freq();
  90. }
  91. /* Compute the prescaler value to have TIM2 counter clock equal to 1MHz */
  92. uwPrescalerValue = (uint32_t)((uwTimclock / 1000000U) - 1U);
  93. /* Initialize TIM2 */
  94. TimHandle.Instance = TIM2;
  95. /* Initialize TIMx peripheral as follow:
  96. + Period = [(TIM2CLK/1000) - 1]. to have a (1/1000) s time base.
  97. + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
  98. + ClockDivision = 0
  99. + Counter direction = Up
  100. */
  101. TimHandle.Init.Period = (1000000U / 1000U) - 1U;
  102. TimHandle.Init.Prescaler = uwPrescalerValue;
  103. TimHandle.Init.ClockDivision = 0U;
  104. TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
  105. TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  106. if (HAL_TIM_Base_Init(&TimHandle) == HAL_OK)
  107. {
  108. /* Start the TIM time Base generation in interrupt mode */
  109. return HAL_TIM_Base_Start_IT(&TimHandle);
  110. }
  111. /* Return function status */
  112. return HAL_ERROR;
  113. }
  114. /**
  115. * @brief Suspend Tick increment.
  116. * @note Disable the tick increment by disabling TIM2 update interrupt.
  117. * @retval None
  118. */
  119. void HAL_SuspendTick(void)
  120. {
  121. /* Disable TIM2 update Interrupt */
  122. __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);
  123. }
  124. /**
  125. * @brief Resume Tick increment.
  126. * @note Enable the tick increment by Enabling TIM2 update interrupt.
  127. * @retval None
  128. */
  129. void HAL_ResumeTick(void)
  130. {
  131. /* Enable TIM2 Update interrupt */
  132. __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
  133. }
  134. /**
  135. * @brief Period elapsed callback in non blocking mode
  136. * @note This function is called when TIM2 interrupt took place, inside
  137. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  138. * a global variable "uwTick" used as application time base.
  139. * @param htim : TIM handle
  140. * @retval None
  141. */
  142. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  143. {
  144. HAL_IncTick();
  145. }
  146. /**
  147. * @brief This function handles TIM interrupt request.
  148. * @retval None
  149. */
  150. void TIM2_IRQHandler(void)
  151. {
  152. HAL_TIM_IRQHandler(&TimHandle);
  153. }
  154. /**
  155. * @}
  156. */
  157. /**
  158. * @}
  159. */
  160. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/