-
Notifications
You must be signed in to change notification settings - Fork 3
/
aq_timer.c
170 lines (132 loc) · 5.04 KB
/
aq_timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
This file is part of AutoQuad.
AutoQuad is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
AutoQuad is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with AutoQuad. If not, see <http://www.gnu.org/licenses/>.
Copyright © 2014 Bill Nesbitt
*/
#include "aq_timer.h"
#include "rcc.h"
timerStruct_t timerData;
// Use TIMER_TIM to create a us system clock
void timerInit(void) {
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// Enable the TIMER_TIM global Interrupt
NVIC_InitStructure.NVIC_IRQChannel = TIMER_IRQ_CH;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIMER_EN;
/* Time base configuration for 1MHz (us)*/
TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = (TIMER_CLOCK / 1000000) - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIMER_TIM, &TIM_TimeBaseStructure);
// reset
TIM_SetCounter(TIMER_TIM, 0);
timerCancelAlarm1();
timerCancelAlarm2();
timerCancelAlarm3();
timerCancelAlarm4();
// Output Compare for alarms
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIMER_TIM, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIMER_TIM, TIM_OCPreload_Disable);
TIM_OC2Init(TIMER_TIM, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIMER_TIM, TIM_OCPreload_Disable);
// go...
TIM_Cmd(TIMER_TIM, ENABLE);
}
void timerDelayMilli(uint16_t ms) {
uint32_t t = TIMER_TIM->CNT + ms * 1000;
while (TIMER_TIM->CNT < t)
;
}
void timerCancelAlarm1(void) {
TIMER_TIM->DIER &= (uint16_t)~TIM_IT_CC1;
TIM_ClearITPendingBit(TIMER_TIM, TIM_IT_CC1);
}
void timerCancelAlarm2(void) {
TIMER_TIM->DIER &= (uint16_t)~TIM_IT_CC2;
TIM_ClearITPendingBit(TIMER_TIM, TIM_IT_CC2);
}
void timerCancelAlarm3(void) {
TIMER_TIM->DIER &= (uint16_t)~TIM_IT_CC3;
TIM_ClearITPendingBit(TIMER_TIM, TIM_IT_CC3);
}
void timerCancelAlarm4(void) {
TIMER_TIM->DIER &= (uint16_t)~TIM_IT_CC4;
TIM_ClearITPendingBit(TIMER_TIM, TIM_IT_CC4);
}
void timerSetAlarm1(int32_t us, timerCallback_t *callback, int parameter) {
// schedule it
timerData.alarm1Callback = callback;
timerData.alarm1Parameter = parameter;
TIMER_TIM->SR = (uint16_t)~TIM_IT_CC1;
TIMER_TIM->CCR1 = TIMER_TIM->CNT + us;
TIMER_TIM->DIER |= TIM_IT_CC1;
}
void timerSetAlarm2(int32_t us, timerCallback_t *callback, int parameter) {
// schedule it
timerData.alarm2Callback = callback;
timerData.alarm2Parameter = parameter;
TIMER_TIM->SR = (uint16_t)~TIM_IT_CC2;
TIMER_TIM->CCR2 = TIMER_TIM->CNT + us;
TIMER_TIM->DIER |= TIM_IT_CC2;
}
void timerSetAlarm3(int32_t us, timerCallback_t *callback, int parameter) {
// schedule it
timerData.alarm3Callback = callback;
timerData.alarm3Parameter = parameter;
TIMER_TIM->SR = (uint16_t)~TIM_IT_CC3;
TIMER_TIM->CCR3 = TIMER_TIM->CNT + us;
TIMER_TIM->DIER |= TIM_IT_CC3;
}
void timerSetAlarm4(int32_t us, timerCallback_t *callback, int parameter) {
// schedule it
timerData.alarm4Callback = callback;
timerData.alarm4Parameter = parameter;
TIMER_TIM->SR = (uint16_t)~TIM_IT_CC4;
TIMER_TIM->CCR4 = TIMER_TIM->CNT + us;
TIMER_TIM->DIER |= TIM_IT_CC4;
}
void TIMER_ISR(void) {
if (TIM_GetITStatus(TIMER_TIM, TIM_IT_CC1) != RESET) {
TIMER_TIM->SR = (uint16_t)~TIM_IT_CC1;
// Disable the Interrupt
TIMER_TIM->DIER &= (uint16_t)~TIM_IT_CC1;
timerData.alarm1Callback(timerData.alarm1Parameter);
}
else if (TIM_GetITStatus(TIMER_TIM, TIM_IT_CC2) != RESET) {
TIMER_TIM->SR = (uint16_t)~TIM_IT_CC2;
// Disable the Interrupt
TIMER_TIM->DIER &= (uint16_t)~TIM_IT_CC2;
timerData.alarm2Callback(timerData.alarm2Parameter);
}
else if (TIM_GetITStatus(TIMER_TIM, TIM_IT_CC3) != RESET) {
TIMER_TIM->SR = (uint16_t)~TIM_IT_CC3;
// Disable the Interrupt
TIMER_TIM->DIER &= (uint16_t)~TIM_IT_CC3;
timerData.alarm3Callback(timerData.alarm3Parameter);
}
else if (TIM_GetITStatus(TIMER_TIM, TIM_IT_CC4) != RESET) {
TIMER_TIM->SR = (uint16_t)~TIM_IT_CC4;
// Disable the Interrupt
TIMER_TIM->DIER &= (uint16_t)~TIM_IT_CC4;
timerData.alarm4Callback(timerData.alarm4Parameter);
}
}