1 /** 2 ****************************************************************************** 3 * @file stm32wbxx_hal_timebase_tim.c 4 * @author MCD Application Team 5 * @brief HAL time base based on the hardware TIM. 6 * 7 * This file overrides the native HAL time base functions (defined as weak) 8 * the TIM time base: 9 * + Intializes the TIM peripheral to generate a Period elapsed Event each 1ms 10 * + HAL_IncTick is called inside HAL_TIM_PeriodElapsedCallback ie each 1ms 11 * 12 @verbatim 13 ============================================================================== 14 ##### How to use this driver ##### 15 ============================================================================== 16 [..] 17 This file must be copied to the application folder and modified as follows: 18 (#) Rename it to 'stm32g0xx_hal_timebase_tim.c' 19 (#) Add this file and the TIM HAL driver files to your project and make sure 20 HAL_TIM_MODULE_ENABLED is defined in stm32wbxx_hal_conf.h 21 22 [..] 23 (@) The application needs to ensure that the time base is always set to 1 millisecond 24 to have correct HAL operation. 25 26 @endverbatim 27 ****************************************************************************** 28 * @attention 29 * 30 * <h2><center>© COPYRIGHT(c) 2019 STMicroelectronics</center></h2> 31 * 32 * Redistribution and use in source and binary forms, with or without modification, 33 * are permitted provided that the following conditions are met: 34 * 1. Redistributions of source code must retain the above copyright notice, 35 * this list of conditions and the following disclaimer. 36 * 2. Redistributions in binary form must reproduce the above copyright notice, 37 * this list of conditions and the following disclaimer in the documentation 38 * and/or other materials provided with the distribution. 39 * 3. Neither the name of STMicroelectronics nor the names of its contributors 40 * may be used to endorse or promote products derived from this software 41 * without specific prior written permission. 42 * 43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 44 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 46 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 49 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 50 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 51 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 52 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 53 * 54 ****************************************************************************** 55 */ 56 57 /* Includes ------------------------------------------------------------------*/ 58 #include "stm32wbxx_hal.h" 59 #include "stm32wbxx_hal_tim.h" 60 61 /* Private typedef -----------------------------------------------------------*/ 62 /* Private define ------------------------------------------------------------*/ 63 /* Private macro -------------------------------------------------------------*/ 64 /* Private variables ---------------------------------------------------------*/ 65 TIM_HandleTypeDef htim17; 66 /* Private function prototypes -----------------------------------------------*/ 67 /* Private functions ---------------------------------------------------------*/ 68 69 /** 70 * @brief This function configures the TIM17 as a time base source. 71 * The time source is configured to have 1ms time base with a dedicated 72 * Tick interrupt priority. 73 * @note This function is called automatically at the beginning of program after 74 * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). 75 * @param TickPriority: Tick interrupt priority. 76 * @retval HAL status 77 */ 78 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) 79 { 80 RCC_ClkInitTypeDef clkconfig; 81 uint32_t uwTimclock = 0; 82 uint32_t uwPrescalerValue = 0; 83 uint32_t pFLatency; 84 85 /*Configure the TIM17 IRQ priority */ 86 HAL_NVIC_SetPriority(TIM1_TRG_COM_TIM17_IRQn, TickPriority ,0); 87 88 /* Enable the TIM17 global Interrupt */ 89 HAL_NVIC_EnableIRQ(TIM1_TRG_COM_TIM17_IRQn); 90 91 /* Enable TIM17 clock */ 92 __HAL_RCC_TIM17_CLK_ENABLE(); 93 94 /* Get clock configuration */ 95 HAL_RCC_GetClockConfig(&clkconfig, &pFLatency); 96 97 /* Compute TIM17 clock */ 98 uwTimclock = HAL_RCC_GetPCLK2Freq(); 99 100 /* Compute the prescaler value to have TIM17 counter clock equal to 1MHz */ 101 uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000) - 1); 102 103 /* Initialize TIM17 */ 104 htim17.Instance = TIM17; 105 106 /* Initialize TIMx peripheral as follow: 107 + Period = [(TIM17CLK/1000) - 1]. to have a (1/1000) s time base. 108 + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. 109 + ClockDivision = 0 110 + Counter direction = Up 111 */ 112 htim17.Init.Period = (1000000 / 1000) - 1; 113 htim17.Init.Prescaler = uwPrescalerValue; 114 htim17.Init.ClockDivision = 0; 115 htim17.Init.CounterMode = TIM_COUNTERMODE_UP; 116 if(HAL_TIM_Base_Init(&htim17) == HAL_OK) 117 { 118 /* Start the TIM time Base generation in interrupt mode */ 119 return HAL_TIM_Base_Start_IT(&htim17); 120 } 121 122 /* Return function status */ 123 return HAL_ERROR; 124 } 125 126 /** 127 * @brief Suspend Tick increment. 128 * @note Disable the tick increment by disabling TIM17 update interrupt. 129 * @param None 130 * @retval None 131 */ 132 void HAL_SuspendTick(void) 133 { 134 /* Disable TIM17 update Interrupt */ 135 __HAL_TIM_DISABLE_IT(&htim17, TIM_IT_UPDATE); 136 } 137 138 /** 139 * @brief Resume Tick increment. 140 * @note Enable the tick increment by Enabling TIM17 update interrupt. 141 * @param None 142 * @retval None 143 */ 144 void HAL_ResumeTick(void) 145 { 146 /* Enable TIM17 Update interrupt */ 147 __HAL_TIM_ENABLE_IT(&htim17, TIM_IT_UPDATE); 148 } 149 150 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 151