stm32f2xx_hal_timebase_tim.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2xx_hal_timebase_tim.c
  4. * @author MCD Application Team
  5. * @brief HAL time base based on the hardware TIM.
  6. *
  7. * This file override 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 "stm32f2xx_hal.h"
  27. /** @addtogroup STM32F2xx_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 TIM6_DAC_IRQHandler(void);
  40. /* Private functions ---------------------------------------------------------*/
  41. /**
  42. * @brief This function configures the TIM6 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 TIM6 IRQ priority */
  57. HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority ,0U);
  58. /* Enable the TIM6 global Interrupt */
  59. HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
  60. /* Enable TIM6 clock */
  61. __HAL_RCC_TIM6_CLK_ENABLE();
  62. /* Get clock configuration */
  63. HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
  64. /* Get APB1 prescaler */
  65. uwAPB1Prescaler = clkconfig.APB1CLKDivider;
  66. /* Compute TIM6 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 TIM6 counter clock equal to 1MHz */
  76. uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
  77. /* Initialize TIM6 */
  78. TimHandle.Instance = TIM6;
  79. /* Initialize TIMx peripheral as follow:
  80. + Period = [(TIM6CLK/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 = 0;
  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 TIM6 update interrupt.
  101. * @param None
  102. * @retval None
  103. */
  104. void HAL_SuspendTick(void)
  105. {
  106. /* Disable TIM6 update Interrupt */
  107. __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);
  108. }
  109. /**
  110. * @brief Resume Tick increment.
  111. * @note Enable the tick increment by Enabling TIM6 update interrupt.
  112. * @param None
  113. * @retval None
  114. */
  115. void HAL_ResumeTick(void)
  116. {
  117. /* Enable TIM6 Update interrupt */
  118. __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
  119. }
  120. /**
  121. * @brief Period elapsed callback in non blocking mode
  122. * @note This function is called when TIM6 interrupt took place, inside
  123. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  124. * a global variable "uwTick" used as application time base.
  125. * @param htim : TIM handle
  126. * @retval None
  127. */
  128. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  129. {
  130. HAL_IncTick();
  131. }
  132. /**
  133. * @brief This function handles TIM interrupt request.
  134. * @param None
  135. * @retval None
  136. */
  137. void TIM6_DAC_IRQHandler(void)
  138. {
  139. HAL_TIM_IRQHandler(&TimHandle);
  140. }
  141. /**
  142. * @}
  143. */
  144. /**
  145. * @}
  146. */
  147. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/