stm32f4xx_hal_timebase_tim.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under Ultimate Liberty license
  13. * SLA0044, the "License"; You may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at:
  15. * www.st.com/SLA0044
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32f4xx_hal.h"
  22. #include "stm32f4xx_hal_tim.h"
  23. /* Private typedef -----------------------------------------------------------*/
  24. /* Private define ------------------------------------------------------------*/
  25. /* Private macro -------------------------------------------------------------*/
  26. /* Private variables ---------------------------------------------------------*/
  27. TIM_HandleTypeDef htim7;
  28. /* Private function prototypes -----------------------------------------------*/
  29. /* Private functions ---------------------------------------------------------*/
  30. /**
  31. * @brief This function configures the TIM7 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;
  46. /* Enable TIM7 clock */
  47. __HAL_RCC_TIM7_CLK_ENABLE();
  48. /* Get clock configuration */
  49. HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
  50. /* Get APB1 prescaler */
  51. uwAPB1Prescaler = clkconfig.APB1CLKDivider;
  52. /* Compute TIM7 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 TIM7 counter clock equal to 1MHz */
  62. uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
  63. /* Initialize TIM7 */
  64. htim7.Instance = TIM7;
  65. /* Initialize TIMx peripheral as follow:
  66. + Period = [(TIM7CLK/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. htim7.Init.Period = (1000000U / 1000U) - 1U;
  72. htim7.Init.Prescaler = uwPrescalerValue;
  73. htim7.Init.ClockDivision = 0;
  74. htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
  75. htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  76. status = HAL_TIM_Base_Init(&htim7);
  77. if (status == HAL_OK)
  78. {
  79. /* Start the TIM time Base generation in interrupt mode */
  80. status = HAL_TIM_Base_Start_IT(&htim7);
  81. if (status == HAL_OK)
  82. {
  83. /* Enable the TIM7 global Interrupt */
  84. HAL_NVIC_EnableIRQ(TIM7_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(TIM7_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 TIM7 update interrupt.
  104. * @param None
  105. * @retval None
  106. */
  107. void HAL_SuspendTick(void)
  108. {
  109. /* Disable TIM7 update Interrupt */
  110. __HAL_TIM_DISABLE_IT(&htim7, TIM_IT_UPDATE);
  111. }
  112. /**
  113. * @brief Resume Tick increment.
  114. * @note Enable the tick increment by Enabling TIM7 update interrupt.
  115. * @param None
  116. * @retval None
  117. */
  118. void HAL_ResumeTick(void)
  119. {
  120. /* Enable TIM7 Update interrupt */
  121. __HAL_TIM_ENABLE_IT(&htim7, TIM_IT_UPDATE);
  122. }