Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:09

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_timebase_tim_template.c
0004   * @author  MCD Application Team
0005   * @brief   HAL time base based on the hardware TIM.
0006   *
0007   *          This file overrides the native HAL time base functions (defined as weak)
0008   *          the TIM time base:
0009   *           + Initializes the TIM peripheral generate a Period elapsed Event each 1ms
0010   *           + HAL_IncTick is called inside HAL_TIM_PeriodElapsedCallback ie each 1ms
0011   *
0012   ******************************************************************************
0013   * @attention
0014   *
0015   * Copyright (c) 2017 STMicroelectronics.
0016   * All rights reserved.
0017   *
0018   * This software is licensed under terms that can be found in the LICENSE file
0019   * in the root directory of this software component.
0020   * If no LICENSE file comes with this software, it is provided AS-IS.
0021   *
0022   ******************************************************************************
0023  @verbatim
0024   ==============================================================================
0025                         ##### How to use this driver #####
0026   ==============================================================================
0027     [..]
0028     This file must be copied to the application folder and modified as follows:
0029     (#) Rename it to 'stm32h7xx_hal_timebase_tim.c'
0030     (#) Add this file and the TIM HAL drivers to your project and uncomment
0031        HAL_TIM_MODULE_ENABLED define in stm32h7xx_hal_conf.h
0032 
0033   @endverbatim
0034   ******************************************************************************
0035   */
0036 
0037 /* Includes ------------------------------------------------------------------*/
0038 #include "stm32h7xx_hal.h"
0039 
0040 /* Private typedef -----------------------------------------------------------*/
0041 /* Private define ------------------------------------------------------------*/
0042 /* Private macro -------------------------------------------------------------*/
0043 /* Private variables ---------------------------------------------------------*/
0044 static TIM_HandleTypeDef        TimHandle;
0045 /* Private function prototypes -----------------------------------------------*/
0046 void TIM6_DAC_IRQHandler(void);
0047 /* Private functions ---------------------------------------------------------*/
0048 
0049 /**
0050   * @brief  This function configures the TIM6 as a time base source.
0051   *         The time source is configured to have 1ms time base with a dedicated
0052   *         Tick interrupt priority.
0053   * @note   This function is called  automatically at the beginning of program after
0054   *         reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
0055   * @param  TickPriority Tick interrupt priority.
0056   * @retval HAL status
0057   */
0058 HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority)
0059 {
0060   RCC_ClkInitTypeDef    clkconfig;
0061   uint32_t              uwTimclock, uwAPB1Prescaler;
0062   uint32_t              uwPrescalerValue;
0063   uint32_t              pFLatency;
0064   HAL_StatusTypeDef     status;
0065 
0066   /* Enable TIM6 clock */
0067   __HAL_RCC_TIM6_CLK_ENABLE();
0068 
0069   /* Get clock configuration */
0070   HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
0071 
0072   /* Get APB1 prescaler */
0073   uwAPB1Prescaler = clkconfig.APB1CLKDivider;
0074 
0075   /* Compute TIM6 clock */
0076   if (uwAPB1Prescaler == RCC_HCLK_DIV1)
0077   {
0078     uwTimclock = HAL_RCC_GetPCLK1Freq();
0079   }
0080   else
0081   {
0082     uwTimclock = 2UL * HAL_RCC_GetPCLK1Freq();
0083   }
0084 
0085   /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */
0086   uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
0087 
0088   /* Initialize TIM6 */
0089   TimHandle.Instance = TIM6;
0090 
0091   /* Initialize TIMx peripheral as follow:
0092   + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base.
0093   + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
0094   + ClockDivision = 0
0095   + Counter direction = Up
0096   */
0097   TimHandle.Init.Period = (1000000U / 1000U) - 1U;
0098   TimHandle.Init.Prescaler = uwPrescalerValue;
0099   TimHandle.Init.ClockDivision = 0U;
0100   TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
0101   status = HAL_TIM_Base_Init(&TimHandle);
0102   if (status == HAL_OK)
0103   {
0104     /* Start the TIM time Base generation in interrupt mode */
0105     status = HAL_TIM_Base_Start_IT(&TimHandle);
0106     if (status == HAL_OK)
0107     {
0108       /* Enable the TIM6 global Interrupt */
0109       HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
0110    
0111       if (TickPriority < (1UL << __NVIC_PRIO_BITS))
0112       {
0113         /* Enable the TIM6 global Interrupt */
0114         HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority, 0);
0115         uwTickPrio = TickPriority;
0116       }
0117       else
0118       {
0119         status = HAL_ERROR;
0120       }
0121    }
0122 }
0123 
0124   /* Return function status */
0125   return status;
0126 }
0127 
0128 /**
0129   * @brief  Suspend Tick increment.
0130   * @note   Disable the tick increment by disabling TIM6 update interrupt.
0131   * @param  None
0132   * @retval None
0133   */
0134 void HAL_SuspendTick(void)
0135 {
0136   /* Disable TIM6 update Interrupt */
0137   __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);
0138 }
0139 
0140 /**
0141   * @brief  Resume Tick increment.
0142   * @note   Enable the tick increment by Enabling TIM6 update interrupt.
0143   * @param  None
0144   * @retval None
0145   */
0146 void HAL_ResumeTick(void)
0147 {
0148   /* Enable TIM6 Update interrupt */
0149   __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
0150 }
0151 
0152 /**
0153   * @brief  Period elapsed callback in non blocking mode
0154   * @note   This function is called  when TIM6 interrupt took place, inside
0155   * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
0156   * a global variable "uwTick" used as application time base.
0157   * @param  htim TIM handle
0158   * @retval None
0159   */
0160 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
0161 {
0162   /* Prevent unused argument(s) compilation warning */
0163   UNUSED(htim);
0164 
0165   HAL_IncTick();
0166 }
0167 
0168 /**
0169   * @brief  This function handles TIM interrupt request.
0170   * @param  None
0171   * @retval None
0172   */
0173 void TIM6_DAC_IRQHandler(void)
0174 {
0175   HAL_TIM_IRQHandler(&TimHandle);
0176 }
0177 
0178