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