Subversion Repositories testOled

Rev

Details | Last modification | View Log | RSS feed

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