stm32f4xx_hal_timebase_tim.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file stm32f4xx_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 "stm32f4xx_hal.h"
  21. #include "stm32f4xx_hal_tim.h"
  22. /* Private typedef -----------------------------------------------------------*/
  23. /* Private define ------------------------------------------------------------*/
  24. /* Private macro -------------------------------------------------------------*/
  25. /* Private variables ---------------------------------------------------------*/
  26. TIM_HandleTypeDef htim9;
  27. /* Private function prototypes -----------------------------------------------*/
  28. /* Private functions ---------------------------------------------------------*/
  29. /**
  30. * @brief This function configures the TIM9 as a time base source.
  31. * The time source is configured to have 1ms time base with a dedicated
  32. * Tick interrupt priority.
  33. * @note This function is called automatically at the beginning of program after
  34. * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
  35. * @param TickPriority: Tick interrupt priority.
  36. * @retval HAL status
  37. */
  38. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  39. {
  40. RCC_ClkInitTypeDef clkconfig;
  41. uint32_t uwTimclock = 0U;
  42. uint32_t uwPrescalerValue = 0U;
  43. uint32_t pFLatency;
  44. HAL_StatusTypeDef status;
  45. /* Enable TIM9 clock */
  46. __HAL_RCC_TIM9_CLK_ENABLE();
  47. /* Get clock configuration */
  48. HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
  49. /* Compute TIM9 clock */
  50. uwTimclock = 2*HAL_RCC_GetPCLK2Freq();
  51. /* Compute the prescaler value to have TIM9 counter clock equal to 1MHz */
  52. uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
  53. /* Initialize TIM9 */
  54. htim9.Instance = TIM9;
  55. /* Initialize TIMx peripheral as follow:
  56. + Period = [(TIM9CLK/1000) - 1]. to have a (1/1000) s time base.
  57. + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
  58. + ClockDivision = 0
  59. + Counter direction = Up
  60. */
  61. htim9.Init.Period = (1000000U / 1000U) - 1U;
  62. htim9.Init.Prescaler = uwPrescalerValue;
  63. htim9.Init.ClockDivision = 0;
  64. htim9.Init.CounterMode = TIM_COUNTERMODE_UP;
  65. htim9.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  66. status = HAL_TIM_Base_Init(&htim9);
  67. if (status == HAL_OK)
  68. {
  69. /* Start the TIM time Base generation in interrupt mode */
  70. status = HAL_TIM_Base_Start_IT(&htim9);
  71. if (status == HAL_OK)
  72. {
  73. /* Enable the TIM9 global Interrupt */
  74. HAL_NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn);
  75. /* Configure the SysTick IRQ priority */
  76. if (TickPriority < (1UL << __NVIC_PRIO_BITS))
  77. {
  78. /* Configure the TIM IRQ priority */
  79. HAL_NVIC_SetPriority(TIM1_BRK_TIM9_IRQn, TickPriority, 0U);
  80. uwTickPrio = TickPriority;
  81. }
  82. else
  83. {
  84. status = HAL_ERROR;
  85. }
  86. }
  87. }
  88. /* Return function status */
  89. return status;
  90. }
  91. /**
  92. * @brief Suspend Tick increment.
  93. * @note Disable the tick increment by disabling TIM9 update interrupt.
  94. * @param None
  95. * @retval None
  96. */
  97. void HAL_SuspendTick(void)
  98. {
  99. /* Disable TIM9 update Interrupt */
  100. __HAL_TIM_DISABLE_IT(&htim9, TIM_IT_UPDATE);
  101. }
  102. /**
  103. * @brief Resume Tick increment.
  104. * @note Enable the tick increment by Enabling TIM9 update interrupt.
  105. * @param None
  106. * @retval None
  107. */
  108. void HAL_ResumeTick(void)
  109. {
  110. /* Enable TIM9 Update interrupt */
  111. __HAL_TIM_ENABLE_IT(&htim9, TIM_IT_UPDATE);
  112. }