stm32f4xx_hal_timebase_tim_template.c 6.3 KB

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