stm32f7xx_hal_timebase_tim.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file stm32f7xx_hal_timebase_TIM.c
  5. * @brief HAL time base based on the hardware TIM.
  6. ******************************************************************************
  7. * This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * Copyright (c) 2019 STMicroelectronics International N.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted, provided that the following conditions are met:
  18. *
  19. * 1. Redistribution of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of other
  25. * contributors to this software may be used to endorse or promote products
  26. * derived from this software without specific written permission.
  27. * 4. This software, including modifications and/or derivative works of this
  28. * software, must execute solely and exclusively on microcontroller or
  29. * microprocessor devices manufactured by or for STMicroelectronics.
  30. * 5. Redistribution and use of this software other than as permitted under
  31. * this license is void and will automatically terminate your rights under
  32. * this license.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /* USER CODE END Header */
  50. /* Includes ------------------------------------------------------------------*/
  51. #include "stm32f7xx_hal.h"
  52. #include "stm32f7xx_hal_tim.h"
  53. /* Private typedef -----------------------------------------------------------*/
  54. /* Private define ------------------------------------------------------------*/
  55. /* Private macro -------------------------------------------------------------*/
  56. /* Private variables ---------------------------------------------------------*/
  57. TIM_HandleTypeDef htim1;
  58. /* Private function prototypes -----------------------------------------------*/
  59. /* Private functions ---------------------------------------------------------*/
  60. /**
  61. * @brief This function configures the TIM1 as a time base source.
  62. * The time source is configured to have 1ms time base with a dedicated
  63. * Tick interrupt priority.
  64. * @note This function is called automatically at the beginning of program after
  65. * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
  66. * @param TickPriority: Tick interrupt priority.
  67. * @retval HAL status
  68. */
  69. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
  70. {
  71. RCC_ClkInitTypeDef clkconfig;
  72. uint32_t uwTimclock = 0;
  73. uint32_t uwPrescalerValue = 0;
  74. uint32_t pFLatency;
  75. /*Configure the TIM1 IRQ priority */
  76. HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, TickPriority ,0);
  77. /* Enable the TIM1 global Interrupt */
  78. HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);
  79. /* Enable TIM1 clock */
  80. __HAL_RCC_TIM1_CLK_ENABLE();
  81. /* Get clock configuration */
  82. HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
  83. /* Compute TIM1 clock */
  84. uwTimclock = 2*HAL_RCC_GetPCLK2Freq();
  85. /* Compute the prescaler value to have TIM1 counter clock equal to 1MHz */
  86. uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000) - 1);
  87. /* Initialize TIM1 */
  88. htim1.Instance = TIM1;
  89. /* Initialize TIMx peripheral as follow:
  90. + Period = [(TIM1CLK/1000) - 1]. to have a (1/1000) s time base.
  91. + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
  92. + ClockDivision = 0
  93. + Counter direction = Up
  94. */
  95. htim1.Init.Period = (1000000 / 1000) - 1;
  96. htim1.Init.Prescaler = uwPrescalerValue;
  97. htim1.Init.ClockDivision = 0;
  98. htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  99. if(HAL_TIM_Base_Init(&htim1) == HAL_OK)
  100. {
  101. /* Start the TIM time Base generation in interrupt mode */
  102. return HAL_TIM_Base_Start_IT(&htim1);
  103. }
  104. /* Return function status */
  105. return HAL_ERROR;
  106. }
  107. /**
  108. * @brief Suspend Tick increment.
  109. * @note Disable the tick increment by disabling TIM1 update interrupt.
  110. * @param None
  111. * @retval None
  112. */
  113. void HAL_SuspendTick(void)
  114. {
  115. /* Disable TIM1 update Interrupt */
  116. __HAL_TIM_DISABLE_IT(&htim1, TIM_IT_UPDATE);
  117. }
  118. /**
  119. * @brief Resume Tick increment.
  120. * @note Enable the tick increment by Enabling TIM1 update interrupt.
  121. * @param None
  122. * @retval None
  123. */
  124. void HAL_ResumeTick(void)
  125. {
  126. /* Enable TIM1 Update interrupt */
  127. __HAL_TIM_ENABLE_IT(&htim1, TIM_IT_UPDATE);
  128. }
  129. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/