stm32l4xx_hal_timebase_tim_template.c 7.2 KB

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