Subversion Repositories canSerial

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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