stm32f1xx_hal_timebase_tim.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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) 2024 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 htim1;
  27. /* Private function prototypes -----------------------------------------------*/
  28. void TIM1_IRQHandler(void);
  29. /* Private functions ---------------------------------------------------------*/
  30. /**
  31. * @brief This function configures the TIM1 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 = 0U;
  43. uint32_t uwPrescalerValue = 0U;
  44. uint32_t pFLatency;
  45. HAL_StatusTypeDef status = HAL_OK;
  46. /* Enable TIM1 clock */
  47. __HAL_RCC_TIM1_CLK_ENABLE();
  48. /* Get clock configuration */
  49. HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
  50. /* Compute TIM1 clock */
  51. uwTimclock = HAL_RCC_GetPCLK2Freq();
  52. /* Compute the prescaler value to have TIM1 counter clock equal to 1MHz */
  53. uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
  54. /* Initialize TIM1 */
  55. htim1.Instance = TIM1;
  56. /* Initialize TIMx peripheral as follow:
  57. + Period = [(TIM1CLK/1000) - 1]. to have a (1/1000) s time base.
  58. + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
  59. + ClockDivision = 0
  60. + Counter direction = Up
  61. */
  62. htim1.Init.Period = (1000000U / 1000U) - 1U;
  63. htim1.Init.Prescaler = uwPrescalerValue;
  64. htim1.Init.ClockDivision = 0;
  65. htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  66. htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  67. status = HAL_TIM_Base_Init(&htim1);
  68. if (status == HAL_OK)
  69. {
  70. /* Start the TIM time Base generation in interrupt mode */
  71. status = HAL_TIM_Base_Start_IT(&htim1);
  72. if (status == HAL_OK)
  73. {
  74. /* Enable the TIM1 global Interrupt */
  75. HAL_NVIC_EnableIRQ(TIM1_UP_IRQn);
  76. /* Configure the SysTick IRQ priority */
  77. if (TickPriority < (1UL << __NVIC_PRIO_BITS))
  78. {
  79. /* Configure the TIM IRQ priority */
  80. HAL_NVIC_SetPriority(TIM1_UP_IRQn, TickPriority, 0U);
  81. uwTickPrio = TickPriority;
  82. }
  83. else
  84. {
  85. status = HAL_ERROR;
  86. }
  87. }
  88. }
  89. /* Return function status */
  90. return status;
  91. }
  92. /**
  93. * @brief Suspend Tick increment.
  94. * @note Disable the tick increment by disabling TIM1 update interrupt.
  95. * @param None
  96. * @retval None
  97. */
  98. void HAL_SuspendTick(void)
  99. {
  100. /* Disable TIM1 update Interrupt */
  101. __HAL_TIM_DISABLE_IT(&htim1, TIM_IT_UPDATE);
  102. }
  103. /**
  104. * @brief Resume Tick increment.
  105. * @note Enable the tick increment by Enabling TIM1 update interrupt.
  106. * @param None
  107. * @retval None
  108. */
  109. void HAL_ResumeTick(void)
  110. {
  111. /* Enable TIM1 Update interrupt */
  112. __HAL_TIM_ENABLE_IT(&htim1, TIM_IT_UPDATE);
  113. }