stm32h7xx_hal_timebase_tim.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file stm32h7xx_hal_timebase_TIM.c
  5. * @brief HAL time base based on the hardware TIM.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2022 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 "stm32h7xx_hal.h"
  21. #include "stm32h7xx_hal_tim.h"
  22. /* Private typedef -----------------------------------------------------------*/
  23. /* Private define ------------------------------------------------------------*/
  24. /* Private macro -------------------------------------------------------------*/
  25. /* Private variables ---------------------------------------------------------*/
  26. TIM_HandleTypeDef htim6;
  27. /* Private function prototypes -----------------------------------------------*/
  28. /* Private functions ---------------------------------------------------------*/
  29. /**
  30. * @brief This function configures the TIM6 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, uwAPB1Prescaler;
  42. uint32_t uwPrescalerValue;
  43. uint32_t pFLatency;
  44. /*Configure the TIM6 IRQ priority */
  45. if (TickPriority < (1UL << __NVIC_PRIO_BITS))
  46. {
  47. HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority ,0U);
  48. /* Enable the TIM6 global Interrupt */
  49. HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
  50. uwTickPrio = TickPriority;
  51. }
  52. else
  53. {
  54. return HAL_ERROR;
  55. }
  56. /* Enable TIM6 clock */
  57. __HAL_RCC_TIM6_CLK_ENABLE();
  58. /* Get clock configuration */
  59. HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
  60. /* Get APB1 prescaler */
  61. uwAPB1Prescaler = clkconfig.APB1CLKDivider;
  62. /* Compute TIM6 clock */
  63. if (uwAPB1Prescaler == RCC_HCLK_DIV1)
  64. {
  65. uwTimclock = HAL_RCC_GetPCLK1Freq();
  66. }
  67. else
  68. {
  69. uwTimclock = 2UL * HAL_RCC_GetPCLK1Freq();
  70. }
  71. /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */
  72. uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
  73. /* Initialize TIM6 */
  74. htim6.Instance = TIM6;
  75. /* Initialize TIMx peripheral as follow:
  76. + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base.
  77. + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
  78. + ClockDivision = 0
  79. + Counter direction = Up
  80. */
  81. htim6.Init.Period = (1000000U / 1000U) - 1U;
  82. htim6.Init.Prescaler = uwPrescalerValue;
  83. htim6.Init.ClockDivision = 0;
  84. htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  85. if(HAL_TIM_Base_Init(&htim6) == HAL_OK)
  86. {
  87. /* Start the TIM time Base generation in interrupt mode */
  88. return HAL_TIM_Base_Start_IT(&htim6);
  89. }
  90. /* Return function status */
  91. return HAL_ERROR;
  92. }
  93. /**
  94. * @brief Suspend Tick increment.
  95. * @note Disable the tick increment by disabling TIM6 update interrupt.
  96. * @param None
  97. * @retval None
  98. */
  99. void HAL_SuspendTick(void)
  100. {
  101. /* Disable TIM6 update Interrupt */
  102. __HAL_TIM_DISABLE_IT(&htim6, TIM_IT_UPDATE);
  103. }
  104. /**
  105. * @brief Resume Tick increment.
  106. * @note Enable the tick increment by Enabling TIM6 update interrupt.
  107. * @param None
  108. * @retval None
  109. */
  110. void HAL_ResumeTick(void)
  111. {
  112. /* Enable TIM6 Update interrupt */
  113. __HAL_TIM_ENABLE_IT(&htim6, TIM_IT_UPDATE);
  114. }