stm32f0xx_hal_timebase_TIM.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_timebase_TIM.c
  4. * @brief HAL time base based on the hardware TIM.
  5. ******************************************************************************
  6. *
  7. * Copyright (c) 2017 STMicroelectronics International N.V.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted, provided that the following conditions are met:
  12. *
  13. * 1. Redistribution of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of other
  19. * contributors to this software may be used to endorse or promote products
  20. * derived from this software without specific written permission.
  21. * 4. This software, including modifications and/or derivative works of this
  22. * software, must execute solely and exclusively on microcontroller or
  23. * microprocessor devices manufactured by or for STMicroelectronics.
  24. * 5. Redistribution and use of this software other than as permitted under
  25. * this license is void and will automatically terminate your rights under
  26. * this license.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  29. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  30. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  31. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  32. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  33. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  34. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  36. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  37. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  38. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  39. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. ******************************************************************************
  42. */
  43. /* Includes ------------------------------------------------------------------*/
  44. #include "stm32f0xx_hal.h"
  45. #include "stm32f0xx_hal_tim.h"
  46. /** @addtogroup STM32F7xx_HAL_Examples
  47. * @{
  48. */
  49. /** @addtogroup HAL_TimeBase
  50. * @{
  51. */
  52. /* Private typedef -----------------------------------------------------------*/
  53. /* Private define ------------------------------------------------------------*/
  54. /* Private macro -------------------------------------------------------------*/
  55. /* Private variables ---------------------------------------------------------*/
  56. TIM_HandleTypeDef htim1;
  57. uint32_t uwIncrementState = 0;
  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 priorty.
  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_BRK_UP_TRG_COM_IRQn, TickPriority ,0);
  77. /* Enable the TIM1 global Interrupt */
  78. HAL_NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_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 = HAL_RCC_GetPCLK1Freq();
  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. /**
  130. * @}
  131. */
  132. /**
  133. * @}
  134. */
  135. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/