stm32f1xx_hal_timebase_tim.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file stm32f1xx_hal_timebase_tim.c
  5. * @brief HAL time base based on the hardware TIM.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2025 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32f1xx_hal.h"
  21. #include "stm32f1xx_hal_tim.h"
  22. /* Private typedef -----------------------------------------------------------*/
  23. /* Private define ------------------------------------------------------------*/
  24. /* Private macro -------------------------------------------------------------*/
  25. /* Private variables ---------------------------------------------------------*/
  26. TIM_HandleTypeDef htim4;
  27. /* Private function prototypes -----------------------------------------------*/
  28. void TIM4_IRQHandler(void);
  29. /* Private functions ---------------------------------------------------------*/
  30. /**
  31. * @brief This function configures the TIM4 as a time base source.
  32. * The time source is configured to have 1ms time base with a dedicated
  33. * Tick interrupt priority.
  34. * @note This function is called automatically at the beginning of program after
  35. * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
  36. * @param TickPriority: Tick interrupt priority.
  37. * @retval HAL status
  38. */
  39. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  40. {
  41. RCC_ClkInitTypeDef clkconfig;
  42. uint32_t uwTimclock, uwAPB1Prescaler = 0U;
  43. uint32_t uwPrescalerValue = 0U;
  44. uint32_t pFLatency;
  45. HAL_StatusTypeDef status = HAL_OK;
  46. /* Enable TIM4 clock */
  47. __HAL_RCC_TIM4_CLK_ENABLE();
  48. /* Get clock configuration */
  49. HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
  50. /* Get APB1 prescaler */
  51. uwAPB1Prescaler = clkconfig.APB1CLKDivider;
  52. /* Compute TIM4 clock */
  53. if (uwAPB1Prescaler == RCC_HCLK_DIV1)
  54. {
  55. uwTimclock = HAL_RCC_GetPCLK1Freq();
  56. }
  57. else
  58. {
  59. uwTimclock = 2UL * HAL_RCC_GetPCLK1Freq();
  60. }
  61. /* Compute the prescaler value to have TIM4 counter clock equal to 1MHz */
  62. uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
  63. /* Initialize TIM4 */
  64. htim4.Instance = TIM4;
  65. /* Initialize TIMx peripheral as follow:
  66. + Period = [(TIM4CLK/1000) - 1]. to have a (1/1000) s time base.
  67. + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
  68. + ClockDivision = 0
  69. + Counter direction = Up
  70. */
  71. htim4.Init.Period = (1000000U / 1000U) - 1U;
  72. htim4.Init.Prescaler = uwPrescalerValue;
  73. htim4.Init.ClockDivision = 0;
  74. htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  75. htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  76. status = HAL_TIM_Base_Init(&htim4);
  77. if (status == HAL_OK)
  78. {
  79. /* Start the TIM time Base generation in interrupt mode */
  80. status = HAL_TIM_Base_Start_IT(&htim4);
  81. if (status == HAL_OK)
  82. {
  83. /* Enable the TIM4 global Interrupt */
  84. HAL_NVIC_EnableIRQ(TIM4_IRQn);
  85. /* Configure the SysTick IRQ priority */
  86. if (TickPriority < (1UL << __NVIC_PRIO_BITS))
  87. {
  88. /* Configure the TIM IRQ priority */
  89. HAL_NVIC_SetPriority(TIM4_IRQn, TickPriority, 0U);
  90. uwTickPrio = TickPriority;
  91. }
  92. else
  93. {
  94. status = HAL_ERROR;
  95. }
  96. }
  97. }
  98. /* Return function status */
  99. return status;
  100. }
  101. /**
  102. * @brief Suspend Tick increment.
  103. * @note Disable the tick increment by disabling TIM4 update interrupt.
  104. * @param None
  105. * @retval None
  106. */
  107. void HAL_SuspendTick(void)
  108. {
  109. /* Disable TIM4 update Interrupt */
  110. __HAL_TIM_DISABLE_IT(&htim4, TIM_IT_UPDATE);
  111. }
  112. /**
  113. * @brief Resume Tick increment.
  114. * @note Enable the tick increment by Enabling TIM4 update interrupt.
  115. * @param None
  116. * @retval None
  117. */
  118. void HAL_ResumeTick(void)
  119. {
  120. /* Enable TIM4 Update interrupt */
  121. __HAL_TIM_ENABLE_IT(&htim4, TIM_IT_UPDATE);
  122. }