Subversion Repositories FuelGauge

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 mjames 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;
6 mjames 60
  HAL_StatusTypeDef     status;
61
 
2 mjames 62
  /* Enable TIM6 clock */
63
  __HAL_RCC_TIM6_CLK_ENABLE();
6 mjames 64
 
2 mjames 65
  /* Get clock configuration */
66
  HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
6 mjames 67
 
2 mjames 68
  /* Get APB1 prescaler */
69
  uwAPB1Prescaler = clkconfig.APB1CLKDivider;
6 mjames 70
 
2 mjames 71
  /* Compute TIM6 clock */
6 mjames 72
  if (uwAPB1Prescaler == RCC_HCLK_DIV1)
2 mjames 73
  {
74
    uwTimclock = HAL_RCC_GetPCLK1Freq();
75
  }
76
  else
77
  {
6 mjames 78
    uwTimclock = 2 * HAL_RCC_GetPCLK1Freq();
2 mjames 79
  }
6 mjames 80
 
2 mjames 81
  /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */
82
  uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000U) - 1U);
6 mjames 83
 
2 mjames 84
  /* Initialize TIM6 */
85
  TimHandle.Instance = TIM6;
6 mjames 86
 
2 mjames 87
  /* Initialize TIMx peripheral as follow:
88
  + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base.
89
  + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
90
  + ClockDivision = 0
91
  + Counter direction = Up
92
  */
93
  TimHandle.Init.Period = (1000000U / 1000U) - 1U;
94
  TimHandle.Init.Prescaler = uwPrescalerValue;
95
  TimHandle.Init.ClockDivision = 0U;
96
  TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
97
  TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
6 mjames 98
  status = HAL_TIM_Base_Init(&TimHandle);
99
  if (status == HAL_OK)
2 mjames 100
  {
101
    /* Start the TIM time Base generation in interrupt mode */
6 mjames 102
    status = HAL_TIM_Base_Start_IT(&TimHandle);
103
    if (status == HAL_OK)
104
    {
105
      /* Enable the TIM6 global Interrupt */
106
      HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
107
 
108
      if (TickPriority < (1UL << __NVIC_PRIO_BITS))
109
      {
110
        /* Enable the TIM6 global Interrupt */
111
        HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority, 0);
112
        uwTickPrio = TickPriority;
113
      }
114
      else
115
      {
116
        status = HAL_ERROR;
117
      }
118
    }
2 mjames 119
  }
120
  /* Return function status */
6 mjames 121
  return status;
2 mjames 122
}
123
 
124
/**
125
  * @brief  Suspend Tick increment.
126
  * @note   Disable the tick increment by disabling TIM6 update interrupt.
127
  * @param  None
128
  * @retval None
129
  */
130
void HAL_SuspendTick(void)
131
{
132
  /* Disable TIM6 update Interrupt */
133
  __HAL_TIM_DISABLE_IT(&TimHandle, TIM_IT_UPDATE);
134
}
135
 
136
/**
137
  * @brief  Resume Tick increment.
138
  * @note   Enable the tick increment by Enabling TIM6 update interrupt.
139
  * @param  None
140
  * @retval None
141
  */
142
void HAL_ResumeTick(void)
143
{
144
  /* Enable TIM6 Update interrupt */
145
  __HAL_TIM_ENABLE_IT(&TimHandle, TIM_IT_UPDATE);
146
}
147
 
148
/**
149
  * @brief  Period elapsed callback in non blocking mode
150
  * @note   This function is called  when TIM6 interrupt took place, inside
151
  * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
152
  * a global variable "uwTick" used as application time base.
153
  * @param  htim TIM handle
154
  * @retval None
155
  */
156
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
157
{
158
  HAL_IncTick();
159
}
160
 
161
/**
162
  * @brief  This function handles TIM interrupt request.
163
  * @param  None
164
  * @retval None
165
  */
166
void TIM6_DAC_IRQHandler(void)
167
{
168
  HAL_TIM_IRQHandler(&TimHandle);
169
}
170
 
171
/**
172
  * @}
173
  */
174
 
175
/**
176
  * @}
177
  */
178
 
179
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/