stm32f4xx_hal_timebase_TIM.c 6.1 KB

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