stm32f4xx_hal_timebase_tim.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 = 0;
  43. uint32_t uwPrescalerValue = 0;
  44. uint32_t pFLatency;
  45. /*Configure the TIM7 IRQ priority */
  46. HAL_NVIC_SetPriority(TIM7_IRQn, TickPriority ,0);
  47. /* Enable the TIM7 global Interrupt */
  48. HAL_NVIC_EnableIRQ(TIM7_IRQn);
  49. /* Enable TIM7 clock */
  50. __HAL_RCC_TIM7_CLK_ENABLE();
  51. /* Get clock configuration */
  52. HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
  53. /* Compute TIM7 clock */
  54. uwTimclock = 2*HAL_RCC_GetPCLK1Freq();
  55. /* Compute the prescaler value to have TIM7 counter clock equal to 1MHz */
  56. uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000) - 1);
  57. /* Initialize TIM7 */
  58. htim7.Instance = TIM7;
  59. /* Initialize TIMx peripheral as follow:
  60. + Period = [(TIM7CLK/1000) - 1]. to have a (1/1000) s time base.
  61. + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
  62. + ClockDivision = 0
  63. + Counter direction = Up
  64. */
  65. htim7.Init.Period = (1000000 / 1000) - 1;
  66. htim7.Init.Prescaler = uwPrescalerValue;
  67. htim7.Init.ClockDivision = 0;
  68. htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
  69. if(HAL_TIM_Base_Init(&htim7) == HAL_OK)
  70. {
  71. /* Start the TIM time Base generation in interrupt mode */
  72. return HAL_TIM_Base_Start_IT(&htim7);
  73. }
  74. /* Return function status */
  75. return HAL_ERROR;
  76. }
  77. /**
  78. * @brief Suspend Tick increment.
  79. * @note Disable the tick increment by disabling TIM7 update interrupt.
  80. * @param None
  81. * @retval None
  82. */
  83. void HAL_SuspendTick(void)
  84. {
  85. /* Disable TIM7 update Interrupt */
  86. __HAL_TIM_DISABLE_IT(&htim7, TIM_IT_UPDATE);
  87. }
  88. /**
  89. * @brief Resume Tick increment.
  90. * @note Enable the tick increment by Enabling TIM7 update interrupt.
  91. * @param None
  92. * @retval None
  93. */
  94. void HAL_ResumeTick(void)
  95. {
  96. /* Enable TIM7 Update interrupt */
  97. __HAL_TIM_ENABLE_IT(&htim7, TIM_IT_UPDATE);
  98. }
  99. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/