User | Revision | Line number | New contents of line |
ganlikun |
0:06036f8bee2d
|
1
|
/* mbed Microcontroller Library
|
ganlikun |
0:06036f8bee2d
|
2
|
* Copyright (c) 2006-2016 ARM Limited
|
ganlikun |
0:06036f8bee2d
|
3
|
*
|
ganlikun |
0:06036f8bee2d
|
4
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
ganlikun |
0:06036f8bee2d
|
5
|
* you may not use this file except in compliance with the License.
|
ganlikun |
0:06036f8bee2d
|
6
|
* You may obtain a copy of the License at
|
ganlikun |
0:06036f8bee2d
|
7
|
*
|
ganlikun |
0:06036f8bee2d
|
8
|
* http://www.apache.org/licenses/LICENSE-2.0
|
ganlikun |
0:06036f8bee2d
|
9
|
*
|
ganlikun |
0:06036f8bee2d
|
10
|
* Unless required by applicable law or agreed to in writing, software
|
ganlikun |
0:06036f8bee2d
|
11
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
ganlikun |
0:06036f8bee2d
|
12
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
ganlikun |
0:06036f8bee2d
|
13
|
* See the License for the specific language governing permissions and
|
ganlikun |
0:06036f8bee2d
|
14
|
* limitations under the License.
|
ganlikun |
0:06036f8bee2d
|
15
|
*/
|
ganlikun |
0:06036f8bee2d
|
16
|
#include "hal_tick.h"
|
ganlikun |
0:06036f8bee2d
|
17
|
|
ganlikun |
0:06036f8bee2d
|
18
|
// A 32-bit timer is used
|
ganlikun |
0:06036f8bee2d
|
19
|
#if !TIM_MST_16BIT
|
ganlikun |
0:06036f8bee2d
|
20
|
|
ganlikun |
0:06036f8bee2d
|
21
|
#define DEBUG_TICK 0 // Set to 1 to toggle a pin (see below which pin) at each tick
|
ganlikun |
0:06036f8bee2d
|
22
|
|
ganlikun |
0:06036f8bee2d
|
23
|
extern TIM_HandleTypeDef TimMasterHandle;
|
ganlikun |
0:06036f8bee2d
|
24
|
|
ganlikun |
0:06036f8bee2d
|
25
|
extern void HAL_IncTick(void);
|
ganlikun |
0:06036f8bee2d
|
26
|
|
ganlikun |
0:06036f8bee2d
|
27
|
volatile uint32_t PreviousVal = 0;
|
ganlikun |
0:06036f8bee2d
|
28
|
|
ganlikun |
0:06036f8bee2d
|
29
|
void us_ticker_irq_handler(void);
|
ganlikun |
0:06036f8bee2d
|
30
|
|
ganlikun |
0:06036f8bee2d
|
31
|
void timer_irq_handler(void)
|
ganlikun |
0:06036f8bee2d
|
32
|
{
|
ganlikun |
0:06036f8bee2d
|
33
|
// Channel 1 for mbed timeout
|
ganlikun |
0:06036f8bee2d
|
34
|
if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
|
ganlikun |
0:06036f8bee2d
|
35
|
if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) {
|
ganlikun |
0:06036f8bee2d
|
36
|
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
|
ganlikun |
0:06036f8bee2d
|
37
|
us_ticker_irq_handler();
|
ganlikun |
0:06036f8bee2d
|
38
|
}
|
ganlikun |
0:06036f8bee2d
|
39
|
}
|
ganlikun |
0:06036f8bee2d
|
40
|
|
ganlikun |
0:06036f8bee2d
|
41
|
// Channel 2 for HAL tick
|
ganlikun |
0:06036f8bee2d
|
42
|
if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) {
|
ganlikun |
0:06036f8bee2d
|
43
|
if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC2) == SET) {
|
ganlikun |
0:06036f8bee2d
|
44
|
__HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2);
|
ganlikun |
0:06036f8bee2d
|
45
|
uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle);
|
ganlikun |
0:06036f8bee2d
|
46
|
if ((val - PreviousVal) >= HAL_TICK_DELAY) {
|
ganlikun |
0:06036f8bee2d
|
47
|
// Increment HAL variable
|
ganlikun |
0:06036f8bee2d
|
48
|
HAL_IncTick();
|
ganlikun |
0:06036f8bee2d
|
49
|
// Prepare next interrupt
|
ganlikun |
0:06036f8bee2d
|
50
|
__HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY);
|
ganlikun |
0:06036f8bee2d
|
51
|
PreviousVal = val;
|
ganlikun |
0:06036f8bee2d
|
52
|
#if DEBUG_TICK > 0
|
ganlikun |
0:06036f8bee2d
|
53
|
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6);
|
ganlikun |
0:06036f8bee2d
|
54
|
#endif
|
ganlikun |
0:06036f8bee2d
|
55
|
}
|
ganlikun |
0:06036f8bee2d
|
56
|
}
|
ganlikun |
0:06036f8bee2d
|
57
|
}
|
ganlikun |
0:06036f8bee2d
|
58
|
}
|
ganlikun |
0:06036f8bee2d
|
59
|
|
ganlikun |
0:06036f8bee2d
|
60
|
// Reconfigure the HAL tick using a standard timer instead of systick.
|
ganlikun |
0:06036f8bee2d
|
61
|
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
|
ganlikun |
0:06036f8bee2d
|
62
|
{
|
ganlikun |
0:06036f8bee2d
|
63
|
RCC_ClkInitTypeDef RCC_ClkInitStruct;
|
ganlikun |
0:06036f8bee2d
|
64
|
uint32_t PclkFreq;
|
ganlikun |
0:06036f8bee2d
|
65
|
|
ganlikun |
0:06036f8bee2d
|
66
|
// Get clock configuration
|
ganlikun |
0:06036f8bee2d
|
67
|
// Note: PclkFreq contains here the Latency (not used after)
|
ganlikun |
0:06036f8bee2d
|
68
|
HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq);
|
ganlikun |
0:06036f8bee2d
|
69
|
|
ganlikun |
0:06036f8bee2d
|
70
|
// Get timer clock value
|
ganlikun |
0:06036f8bee2d
|
71
|
#if TIM_MST_PCLK == 1
|
ganlikun |
0:06036f8bee2d
|
72
|
PclkFreq = HAL_RCC_GetPCLK1Freq();
|
ganlikun |
0:06036f8bee2d
|
73
|
#else
|
ganlikun |
0:06036f8bee2d
|
74
|
PclkFreq = HAL_RCC_GetPCLK2Freq();
|
ganlikun |
0:06036f8bee2d
|
75
|
#endif
|
ganlikun |
0:06036f8bee2d
|
76
|
|
ganlikun |
0:06036f8bee2d
|
77
|
// Enable timer clock
|
ganlikun |
0:06036f8bee2d
|
78
|
TIM_MST_RCC;
|
ganlikun |
0:06036f8bee2d
|
79
|
|
ganlikun |
0:06036f8bee2d
|
80
|
// Reset timer
|
ganlikun |
0:06036f8bee2d
|
81
|
TIM_MST_RESET_ON;
|
ganlikun |
0:06036f8bee2d
|
82
|
TIM_MST_RESET_OFF;
|
ganlikun |
0:06036f8bee2d
|
83
|
|
ganlikun |
0:06036f8bee2d
|
84
|
// Configure time base
|
ganlikun |
0:06036f8bee2d
|
85
|
TimMasterHandle.Instance = TIM_MST;
|
ganlikun |
0:06036f8bee2d
|
86
|
TimMasterHandle.Init.Period = 0xFFFFFFFF;
|
ganlikun |
0:06036f8bee2d
|
87
|
|
ganlikun |
0:06036f8bee2d
|
88
|
// TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx
|
ganlikun |
0:06036f8bee2d
|
89
|
#if TIM_MST_PCLK == 1
|
ganlikun |
0:06036f8bee2d
|
90
|
if (RCC_ClkInitStruct.APB1CLKDivider == RCC_HCLK_DIV1) {
|
ganlikun |
0:06036f8bee2d
|
91
|
#else
|
ganlikun |
0:06036f8bee2d
|
92
|
if (RCC_ClkInitStruct.APB2CLKDivider == RCC_HCLK_DIV1) {
|
ganlikun |
0:06036f8bee2d
|
93
|
#endif
|
ganlikun |
0:06036f8bee2d
|
94
|
TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick
|
ganlikun |
0:06036f8bee2d
|
95
|
} else {
|
ganlikun |
0:06036f8bee2d
|
96
|
TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick
|
ganlikun |
0:06036f8bee2d
|
97
|
}
|
ganlikun |
0:06036f8bee2d
|
98
|
|
ganlikun |
0:06036f8bee2d
|
99
|
TimMasterHandle.Init.ClockDivision = 0;
|
ganlikun |
0:06036f8bee2d
|
100
|
TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
|
ganlikun |
0:06036f8bee2d
|
101
|
#if !TARGET_STM32L1
|
ganlikun |
0:06036f8bee2d
|
102
|
TimMasterHandle.Init.RepetitionCounter = 0;
|
ganlikun |
0:06036f8bee2d
|
103
|
#endif
|
ganlikun |
0:06036f8bee2d
|
104
|
#ifdef TIM_AUTORELOAD_PRELOAD_DISABLE
|
ganlikun |
0:06036f8bee2d
|
105
|
TimMasterHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
ganlikun |
0:06036f8bee2d
|
106
|
#endif
|
ganlikun |
0:06036f8bee2d
|
107
|
HAL_TIM_OC_Init(&TimMasterHandle);
|
ganlikun |
0:06036f8bee2d
|
108
|
|
ganlikun |
0:06036f8bee2d
|
109
|
NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler);
|
ganlikun |
0:06036f8bee2d
|
110
|
NVIC_EnableIRQ(TIM_MST_IRQ);
|
ganlikun |
0:06036f8bee2d
|
111
|
|
ganlikun |
0:06036f8bee2d
|
112
|
// Channel 1 for mbed timeout
|
ganlikun |
0:06036f8bee2d
|
113
|
HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);
|
ganlikun |
0:06036f8bee2d
|
114
|
|
ganlikun |
0:06036f8bee2d
|
115
|
// Channel 2 for HAL tick
|
ganlikun |
0:06036f8bee2d
|
116
|
HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2);
|
ganlikun |
0:06036f8bee2d
|
117
|
PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle);
|
ganlikun |
0:06036f8bee2d
|
118
|
__HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY);
|
ganlikun |
0:06036f8bee2d
|
119
|
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
|
ganlikun |
0:06036f8bee2d
|
120
|
|
ganlikun |
0:06036f8bee2d
|
121
|
// Freeze timer on stop/breakpoint
|
ganlikun |
0:06036f8bee2d
|
122
|
// Define the FREEZE_TIMER_ON_DEBUG macro in mbed_app.json for example
|
ganlikun |
0:06036f8bee2d
|
123
|
#if !defined(NDEBUG) && defined(FREEZE_TIMER_ON_DEBUG) && defined(TIM_MST_DBGMCU_FREEZE)
|
ganlikun |
0:06036f8bee2d
|
124
|
TIM_MST_DBGMCU_FREEZE;
|
ganlikun |
0:06036f8bee2d
|
125
|
#endif
|
ganlikun |
0:06036f8bee2d
|
126
|
|
ganlikun |
0:06036f8bee2d
|
127
|
#if DEBUG_TICK > 0
|
ganlikun |
0:06036f8bee2d
|
128
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
ganlikun |
0:06036f8bee2d
|
129
|
GPIO_InitTypeDef GPIO_InitStruct;
|
ganlikun |
0:06036f8bee2d
|
130
|
GPIO_InitStruct.Pin = GPIO_PIN_6;
|
ganlikun |
0:06036f8bee2d
|
131
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
ganlikun |
0:06036f8bee2d
|
132
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
ganlikun |
0:06036f8bee2d
|
133
|
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
|
ganlikun |
0:06036f8bee2d
|
134
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
ganlikun |
0:06036f8bee2d
|
135
|
#endif
|
ganlikun |
0:06036f8bee2d
|
136
|
|
ganlikun |
0:06036f8bee2d
|
137
|
return HAL_OK;
|
ganlikun |
0:06036f8bee2d
|
138
|
}
|
ganlikun |
0:06036f8bee2d
|
139
|
|
ganlikun |
0:06036f8bee2d
|
140
|
/* NOTE: must be called with interrupts disabled! */
|
ganlikun |
0:06036f8bee2d
|
141
|
void HAL_SuspendTick(void)
|
ganlikun |
0:06036f8bee2d
|
142
|
{
|
ganlikun |
0:06036f8bee2d
|
143
|
__HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2);
|
ganlikun |
0:06036f8bee2d
|
144
|
}
|
ganlikun |
0:06036f8bee2d
|
145
|
|
ganlikun |
0:06036f8bee2d
|
146
|
/* NOTE: must be called with interrupts disabled! */
|
ganlikun |
0:06036f8bee2d
|
147
|
void HAL_ResumeTick(void)
|
ganlikun |
0:06036f8bee2d
|
148
|
{
|
ganlikun |
0:06036f8bee2d
|
149
|
__HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
|
ganlikun |
0:06036f8bee2d
|
150
|
}
|
ganlikun |
0:06036f8bee2d
|
151
|
|
ganlikun |
0:06036f8bee2d
|
152
|
#endif // !TIM_MST_16BIT
|
ganlikun |
0:06036f8bee2d
|
153
|
|