Subversion Repositories FuelGauge

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f0xx_hal_timebase_tim_template.c
  4.   * @brief   HAL time base based on the hardware TIM Template.
  5.   *    
  6.   *          This file override the native HAL time base functions (defined as weak)
  7.   *          the TIM time base:
  8.   *           + Intializes the TIM peripheral generate a Period elapsed Event each 1ms
  9.   *           + HAL_IncTick is called inside HAL_TIM_PeriodElapsedCallback ie each 1ms
  10.   *
  11.   ******************************************************************************
  12.   * @attention
  13.   *
  14.   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  15.   * All rights reserved.</center></h2>
  16.   *
  17.   * This software component is licensed by ST under BSD 3-Clause license,
  18.   * the "License"; You may not use this file except in compliance with the
  19.   * License. You may obtain a copy of the License at:
  20.   *                        opensource.org/licenses/BSD-3-Clause
  21.   *
  22.   ******************************************************************************
  23.   */
  24.  
  25. /* Includes ------------------------------------------------------------------*/
  26. #include "stm32f0xx_hal.h"
  27.  
  28. /** @addtogroup STM32F0xx_HAL_Driver
  29.   * @{
  30.   */
  31.  
  32. /** @addtogroup HAL_TimeBase_TIM
  33.   * @{
  34.   */
  35.  
  36. /* Private typedef -----------------------------------------------------------*/
  37. /* Private define ------------------------------------------------------------*/
  38. /* Private macro -------------------------------------------------------------*/
  39. /* Private variables ---------------------------------------------------------*/
  40. TIM_HandleTypeDef        TimHandle;
  41. /* Private function prototypes -----------------------------------------------*/
  42. void TIM6_DAC_IRQHandler(void);
  43. /* Private functions ---------------------------------------------------------*/
  44.  
  45. /**
  46.   * @brief  This function configures the TIM6 as a time base source.
  47.   *         The time source is configured  to have 1ms time base with a dedicated
  48.   *         Tick interrupt priority.
  49.   * @note   This function is called  automatically at the beginning of program after
  50.   *         reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
  51.   * @param  TickPriority Tick interrupt priority.
  52.   * @retval HAL status
  53.   */
  54. HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority)
  55. {
  56.   RCC_ClkInitTypeDef    clkconfig;
  57.   uint32_t              uwTimclock, uwAPB1Prescaler = 0U;
  58.   uint32_t              uwPrescalerValue = 0U;
  59.   uint32_t              pFLatency;
  60.  
  61.     /*Configure the TIM6 IRQ priority */
  62.   HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority ,0U);
  63.  
  64.   /* Enable the TIM6 global Interrupt */
  65.   HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
  66.  
  67.   /* Enable TIM6 clock */
  68.   __HAL_RCC_TIM6_CLK_ENABLE();
  69.  
  70.   /* Get clock configuration */
  71.   HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
  72.  
  73.   /* Get APB1 prescaler */
  74.   uwAPB1Prescaler = clkconfig.APB1CLKDivider;
  75.  
  76.   /* Compute TIM6 clock */
  77.   if (uwAPB1Prescaler == RCC_HCLK_DIV1)
  78.   {
  79.     uwTimclock = HAL_RCC_GetPCLK1Freq();
  80.   }
  81.   else
  82.   {
  83.     uwTimclock = 2*HAL_RCC_GetPCLK1Freq();
  84.   }
  85.  
  86.   /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */
  87.   uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
  88.  
  89.   /* Initialize TIM6 */
  90.   TimHandle.Instance = TIM6;
  91.  
  92.   /* Initialize TIMx peripheral as follow:
  93.   + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base.
  94.   + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
  95.   + ClockDivision = 0
  96.   + Counter direction = Up
  97.   */
  98.   TimHandle.Init.Period = (1000000U / 1000U) - 1U;
  99.   TimHandle.Init.Prescaler = uwPrescalerValue;
  100.   TimHandle.Init.ClockDivision = 0U;
  101.   TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
  102.   TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  103.   if(HAL_TIM_Base_Init(&TimHandle) == HAL_OK)
  104.   {
  105.     /* Start the TIM time Base generation in interrupt mode */
  106.     return HAL_TIM_Base_Start_IT(&TimHandle);
  107.   }
  108.  
  109.   /* Return function status */
  110.   return HAL_ERROR;
  111. }
  112.  
  113. /**
  114.   * @brief  Suspend Tick increment.
  115.   * @note   Disable the tick increment by disabling TIM6 update interrupt.
  116.   * @param  None
  117.   * @retval None
  118.   */
  119. void HAL_SuspendTick(void)
  120. {
  121.   /* Disable TIM6 update Interrupt */
  122.   __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);
  123. }
  124.  
  125. /**
  126.   * @brief  Resume Tick increment.
  127.   * @note   Enable the tick increment by Enabling TIM6 update interrupt.
  128.   * @param  None
  129.   * @retval None
  130.   */
  131. void HAL_ResumeTick(void)
  132. {
  133.   /* Enable TIM6 Update interrupt */
  134.   __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
  135. }
  136.  
  137. /**
  138.   * @brief  Period elapsed callback in non blocking mode
  139.   * @note   This function is called  when TIM6 interrupt took place, inside
  140.   * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  141.   * a global variable "uwTick" used as application time base.
  142.   * @param  htim TIM handle
  143.   * @retval None
  144.   */
  145. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  146. {
  147.   HAL_IncTick();
  148. }
  149.  
  150. /**
  151.   * @brief  This function handles TIM interrupt request.
  152.   * @param  None
  153.   * @retval None
  154.   */
  155. void TIM6_DAC_IRQHandler(void)
  156. {
  157.   HAL_TIM_IRQHandler(&TimHandle);
  158. }
  159.  
  160. /**
  161.   * @}
  162.   */
  163.  
  164. /**
  165.   * @}
  166.   */
  167.  
  168. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  169.