You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_hal_timebase_tim_template.c 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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 'stm32l1xx_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 stm32l1xx_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.
  29. * All rights reserved.</center></h2>
  30. *
  31. * This software component is licensed by ST under BSD 3-Clause license,
  32. * the "License"; You may not use this file except in compliance with the
  33. * License. You may obtain a copy of the License at:
  34. * opensource.org/licenses/BSD-3-Clause
  35. *
  36. ******************************************************************************
  37. */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "stm32l1xx_hal.h"
  40. /** @addtogroup STM32L1xx_HAL_Driver
  41. * @{
  42. */
  43. /** @addtogroup HAL_TimeBase_TIM
  44. * @{
  45. */
  46. /* Private typedef -----------------------------------------------------------*/
  47. /* Private define ------------------------------------------------------------*/
  48. /* Private macro -------------------------------------------------------------*/
  49. /* Private variables ---------------------------------------------------------*/
  50. TIM_HandleTypeDef TimHandle;
  51. /* Private function prototypes -----------------------------------------------*/
  52. void TIM6_IRQHandler(void);
  53. /* Private functions ---------------------------------------------------------*/
  54. /**
  55. * @brief This function configures the TIM6 as a time base source.
  56. * The time source is configured to have 1ms time base with a dedicated
  57. * Tick interrupt priority.
  58. * @note This function is called automatically at the beginning of program after
  59. * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
  60. * @param TickPriority Tick interrupt priority.
  61. * @retval HAL status
  62. */
  63. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  64. {
  65. RCC_ClkInitTypeDef clkconfig;
  66. uint32_t uwTimclock, uwAPB1Prescaler = 0U;
  67. uint32_t uwPrescalerValue = 0U;
  68. uint32_t pFLatency;
  69. /* Configure the TIM6 IRQ priority */
  70. HAL_NVIC_SetPriority(TIM6_IRQn, TickPriority, 0U);
  71. /* Enable the TIM6 global Interrupt */
  72. HAL_NVIC_EnableIRQ(TIM6_IRQn);
  73. /* Enable TIM6 clock */
  74. __HAL_RCC_TIM6_CLK_ENABLE();
  75. /* Get clock configuration */
  76. HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
  77. /* Get APB1 prescaler */
  78. uwAPB1Prescaler = clkconfig.APB1CLKDivider;
  79. /* Compute TIM6 clock */
  80. if (uwAPB1Prescaler == RCC_HCLK_DIV1)
  81. {
  82. uwTimclock = HAL_RCC_GetPCLK1Freq();
  83. }
  84. else
  85. {
  86. uwTimclock = 2*HAL_RCC_GetPCLK1Freq();
  87. }
  88. /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */
  89. uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
  90. /* Initialize TIM6 */
  91. TimHandle.Instance = TIM6;
  92. /* Initialize TIMx peripheral as follow:
  93. + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base.
  94. + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
  95. + ClockDivision = 0
  96. + Counter direction = Up
  97. */
  98. TimHandle.Init.Period = (1000000U / 1000U) - 1U;
  99. TimHandle.Init.Prescaler = uwPrescalerValue;
  100. TimHandle.Init.ClockDivision = 0U;
  101. TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
  102. if(HAL_TIM_Base_Init(&TimHandle) == HAL_OK)
  103. {
  104. /* Start the TIM time Base generation in interrupt mode */
  105. return HAL_TIM_Base_Start_IT(&TimHandle);
  106. }
  107. /* Return function status */
  108. return HAL_ERROR;
  109. }
  110. /**
  111. * @brief Suspend Tick increment.
  112. * @note Disable the tick increment by disabling TIM6 update interrupt.
  113. * @param None
  114. * @retval None
  115. */
  116. void HAL_SuspendTick(void)
  117. {
  118. /* Disable TIM6 update interrupt */
  119. __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);
  120. }
  121. /**
  122. * @brief Resume Tick increment.
  123. * @note Enable the tick increment by enabling TIM6 update interrupt.
  124. * @param None
  125. * @retval None
  126. */
  127. void HAL_ResumeTick(void)
  128. {
  129. /* Enable TIM6 update interrupt */
  130. __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
  131. }
  132. /**
  133. * @brief Period elapsed callback in non blocking mode
  134. * @note This function is called when TIM6 interrupt took place, inside
  135. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  136. * a global variable "uwTick" used as application time base.
  137. * @param htim TIM handle
  138. * @retval None
  139. */
  140. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  141. {
  142. HAL_IncTick();
  143. }
  144. /**
  145. * @brief This function handles TIM interrupt request.
  146. * @param None
  147. * @retval None
  148. */
  149. void TIM6_IRQHandler(void)
  150. {
  151. HAL_TIM_IRQHandler(&TimHandle);
  152. }
  153. /**
  154. * @}
  155. */
  156. /**
  157. * @}
  158. */
  159. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/