Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2 | mjames | 1 | /** |
| 2 | ****************************************************************************** |
||
| 3 | * @file stm32f1xx_hal_timebase_tim_template.c |
||
| 4 | * @author MCD Application Team |
||
| 5 | * @brief HAL time base based on the hardware TIM Template. |
||
| 6 | * |
||
| 7 | * This file overrides the native HAL time base functions (defined as weak) |
||
| 8 | * the TIM time base: |
||
| 9 | * + Intializes the TIM peripheral generate a Period elapsed Event each 1ms |
||
| 10 | * + HAL_IncTick is called inside HAL_TIM_PeriodElapsedCallback ie each 1ms |
||
| 11 | * |
||
| 12 | ****************************************************************************** |
||
| 13 | * @attention |
||
| 14 | * |
||
| 15 | * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> |
||
| 16 | * |
||
| 17 | * Redistribution and use in source and binary forms, with or without modification, |
||
| 18 | * are permitted provided that the following conditions are met: |
||
| 19 | * 1. Redistributions 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 its contributors |
||
| 25 | * may be used to endorse or promote products derived from this software |
||
| 26 | * without specific prior written permission. |
||
| 27 | * |
||
| 28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
||
| 29 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
| 30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
| 31 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
||
| 32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||
| 33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
||
| 34 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||
| 35 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
||
| 36 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
| 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 38 | * |
||
| 39 | ****************************************************************************** |
||
| 40 | */ |
||
| 41 | |||
| 42 | /* Includes ------------------------------------------------------------------*/ |
||
| 43 | #include "stm32f1xx_hal.h" |
||
| 44 | |||
| 45 | /** @addtogroup STM32F1xx_HAL_Driver |
||
| 46 | * @{ |
||
| 47 | */ |
||
| 48 | |||
| 49 | /** @addtogroup HAL_TimeBase_TIM |
||
| 50 | * @{ |
||
| 51 | */ |
||
| 52 | |||
| 53 | /* Private typedef -----------------------------------------------------------*/ |
||
| 54 | /* Private define ------------------------------------------------------------*/ |
||
| 55 | /* Private macro -------------------------------------------------------------*/ |
||
| 56 | /* Private variables ---------------------------------------------------------*/ |
||
| 57 | TIM_HandleTypeDef TimHandle; |
||
| 58 | /* Private function prototypes -----------------------------------------------*/ |
||
| 59 | void TIM2_IRQHandler(void); |
||
| 60 | /* Private functions ---------------------------------------------------------*/ |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @brief This function configures the TIM2 as a time base source. |
||
| 64 | * The time source is configured to have 1ms time base with a dedicated |
||
| 65 | * Tick interrupt priority. |
||
| 66 | * @note This function is called automatically at the beginning of program after |
||
| 67 | * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). |
||
| 68 | * @param TickPriority: Tick interrupt priority. |
||
| 69 | * @retval HAL status |
||
| 70 | */ |
||
| 71 | HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) |
||
| 72 | { |
||
| 73 | RCC_ClkInitTypeDef clkconfig; |
||
| 74 | uint32_t uwTimclock, uwAPB1Prescaler = 0U; |
||
| 75 | uint32_t uwPrescalerValue = 0U; |
||
| 76 | uint32_t pFLatency; |
||
| 77 | |||
| 78 | /*Configure the TIM2 IRQ priority */ |
||
| 79 | HAL_NVIC_SetPriority(TIM2_IRQn, TickPriority, 0U); |
||
| 80 | |||
| 81 | /* Enable the TIM2 global Interrupt */ |
||
| 82 | HAL_NVIC_EnableIRQ(TIM2_IRQn); |
||
| 83 | |||
| 84 | /* Enable TIM2 clock */ |
||
| 85 | __HAL_RCC_TIM2_CLK_ENABLE(); |
||
| 86 | |||
| 87 | /* Get clock configuration */ |
||
| 88 | HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); |
||
| 89 | |||
| 90 | /* Get APB1 prescaler */ |
||
| 91 | uwAPB1Prescaler = clkconfig.APB1CLKDivider; |
||
| 92 | |||
| 93 | /* Compute TIM2 clock */ |
||
| 94 | if (uwAPB1Prescaler == RCC_HCLK_DIV1) |
||
| 95 | { |
||
| 96 | uwTimclock = HAL_RCC_GetPCLK1Freq(); |
||
| 97 | } |
||
| 98 | else |
||
| 99 | { |
||
| 100 | uwTimclock = 2 * HAL_RCC_GetPCLK1Freq(); |
||
| 101 | } |
||
| 102 | |||
| 103 | /* Compute the prescaler value to have TIM2 counter clock equal to 1MHz */ |
||
| 104 | uwPrescalerValue = (uint32_t)((uwTimclock / 1000000U) - 1U); |
||
| 105 | |||
| 106 | /* Initialize TIM2 */ |
||
| 107 | TimHandle.Instance = TIM2; |
||
| 108 | |||
| 109 | /* Initialize TIMx peripheral as follow: |
||
| 110 | + Period = [(TIM2CLK/1000) - 1]. to have a (1/1000) s time base. |
||
| 111 | + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. |
||
| 112 | + ClockDivision = 0 |
||
| 113 | + Counter direction = Up |
||
| 114 | */ |
||
| 115 | TimHandle.Init.Period = (1000000U / 1000U) - 1U; |
||
| 116 | TimHandle.Init.Prescaler = uwPrescalerValue; |
||
| 117 | TimHandle.Init.ClockDivision = 0U; |
||
| 118 | TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
||
| 119 | TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
||
| 120 | if (HAL_TIM_Base_Init(&TimHandle) == HAL_OK) |
||
| 121 | { |
||
| 122 | /* Start the TIM time Base generation in interrupt mode */ |
||
| 123 | return HAL_TIM_Base_Start_IT(&TimHandle); |
||
| 124 | } |
||
| 125 | |||
| 126 | /* Return function status */ |
||
| 127 | return HAL_ERROR; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @brief Suspend Tick increment. |
||
| 132 | * @note Disable the tick increment by disabling TIM2 update interrupt. |
||
| 133 | * @retval None |
||
| 134 | */ |
||
| 135 | void HAL_SuspendTick(void) |
||
| 136 | { |
||
| 137 | /* Disable TIM2 update Interrupt */ |
||
| 138 | __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @brief Resume Tick increment. |
||
| 143 | * @note Enable the tick increment by Enabling TIM2 update interrupt. |
||
| 144 | * @retval None |
||
| 145 | */ |
||
| 146 | void HAL_ResumeTick(void) |
||
| 147 | { |
||
| 148 | /* Enable TIM2 Update interrupt */ |
||
| 149 | __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @brief Period elapsed callback in non blocking mode |
||
| 154 | * @note This function is called when TIM2 interrupt took place, inside |
||
| 155 | * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment |
||
| 156 | * a global variable "uwTick" used as application time base. |
||
| 157 | * @param htim : TIM handle |
||
| 158 | * @retval None |
||
| 159 | */ |
||
| 160 | void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) |
||
| 161 | { |
||
| 162 | HAL_IncTick(); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @brief This function handles TIM interrupt request. |
||
| 167 | * @retval None |
||
| 168 | */ |
||
| 169 | void TIM2_IRQHandler(void) |
||
| 170 | { |
||
| 171 | HAL_TIM_IRQHandler(&TimHandle); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @} |
||
| 176 | */ |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @} |
||
| 180 | */ |
||
| 181 | |||
| 182 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |