stm32f1xx_hal_timebase_tim_template.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /*Configure the TIM2 IRQ priority */
  57. HAL_NVIC_SetPriority(TIM2_IRQn, TickPriority, 0U);
  58. /* Enable the TIM2 global Interrupt */
  59. HAL_NVIC_EnableIRQ(TIM2_IRQn);
  60. /* Enable TIM2 clock */
  61. __HAL_RCC_TIM2_CLK_ENABLE();
  62. /* Get clock configuration */
  63. HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
  64. /* Get APB1 prescaler */
  65. uwAPB1Prescaler = clkconfig.APB1CLKDivider;
  66. /* Compute TIM2 clock */
  67. if (uwAPB1Prescaler == RCC_HCLK_DIV1)
  68. {
  69. uwTimclock = HAL_RCC_GetPCLK1Freq();
  70. }
  71. else
  72. {
  73. uwTimclock = 2 * HAL_RCC_GetPCLK1Freq();
  74. }
  75. /* Compute the prescaler value to have TIM2 counter clock equal to 1MHz */
  76. uwPrescalerValue = (uint32_t)((uwTimclock / 1000000U) - 1U);
  77. /* Initialize TIM2 */
  78. TimHandle.Instance = TIM2;
  79. /* Initialize TIMx peripheral as follow:
  80. + Period = [(TIM2CLK/1000) - 1]. to have a (1/1000) s time base.
  81. + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
  82. + ClockDivision = 0
  83. + Counter direction = Up
  84. */
  85. TimHandle.Init.Period = (1000000U / 1000U) - 1U;
  86. TimHandle.Init.Prescaler = uwPrescalerValue;
  87. TimHandle.Init.ClockDivision = 0U;
  88. TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
  89. TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  90. if (HAL_TIM_Base_Init(&TimHandle) == HAL_OK)
  91. {
  92. /* Start the TIM time Base generation in interrupt mode */
  93. return HAL_TIM_Base_Start_IT(&TimHandle);
  94. }
  95. /* Return function status */
  96. return HAL_ERROR;
  97. }
  98. /**
  99. * @brief Suspend Tick increment.
  100. * @note Disable the tick increment by disabling TIM2 update interrupt.
  101. * @retval None
  102. */
  103. void HAL_SuspendTick(void)
  104. {
  105. /* Disable TIM2 update Interrupt */
  106. __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);
  107. }
  108. /**
  109. * @brief Resume Tick increment.
  110. * @note Enable the tick increment by Enabling TIM2 update interrupt.
  111. * @retval None
  112. */
  113. void HAL_ResumeTick(void)
  114. {
  115. /* Enable TIM2 Update interrupt */
  116. __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
  117. }
  118. /**
  119. * @brief Period elapsed callback in non blocking mode
  120. * @note This function is called when TIM2 interrupt took place, inside
  121. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  122. * a global variable "uwTick" used as application time base.
  123. * @param htim TIM handle
  124. * @retval None
  125. */
  126. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  127. {
  128. HAL_IncTick();
  129. }
  130. /**
  131. * @brief This function handles TIM interrupt request.
  132. * @retval None
  133. */
  134. void TIM2_IRQHandler(void)
  135. {
  136. HAL_TIM_IRQHandler(&TimHandle);
  137. }
  138. /**
  139. * @}
  140. */
  141. /**
  142. * @}
  143. */
  144. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/