-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDrv_pwm_in.c
397 lines (350 loc) · 13.6 KB
/
Drv_pwm_in.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/******************** (C) COPYRIGHT 2017 ANO Tech ********************************
* 作者 :匿名科创
* 官网 :www.anotc.com
* 淘宝 :anotc.taobao.com
* 技术Q群 :190169595
* 描述 :PWM输入捕获
**********************************************************************************/
#include "Drv_pwm_in.h"
#include "Ano_RC.h"
#include "Drv_sbus.h"
#include "include.h"
#define PPM_CHNUM 8
u16 Rc_Pwm_In[8];
u16 Rc_Ppm_In[PPM_CHNUM ];
static u8 pwminmode_irq;
void PWM_IN_Init ( u8 pwminmode )
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
pwminmode_irq = pwminmode;
if(pwminmode == SBUS) // SBUS = 2
{
}
else if(pwminmode == PPM) //判断接收机模式
{
GPIO_StructInit(&GPIO_InitStructure);
TIM_ICStructInit(&TIM_ICInitStructure);
RCC_APB1PeriphClockCmd ( RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4, ENABLE );
RCC_AHB1PeriphClockCmd ( RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE );
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_PWMIN_P;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = NVIC_PWMIN_S;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init ( &NVIC_InitStructure );
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_PWMIN_P;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = NVIC_PWMIN_S;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init ( &NVIC_InitStructure );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init ( GPIOC, &GPIO_InitStructure );
GPIO_PinAFConfig ( GPIOC, GPIO_PinSource6, GPIO_AF_TIM3 );
TIM3->PSC = ( 168 / 2 ) - 1;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit ( TIM3, &TIM_ICInitStructure );
TIM_Cmd ( TIM3, ENABLE );
TIM_ITConfig ( TIM3, TIM_IT_CC1, ENABLE ); // 使能TIM3通道中断
}
else if(pwminmode == PWM) //判断接收机模式 PWM = 0
{
GPIO_StructInit(&GPIO_InitStructure);
TIM_ICStructInit(&TIM_ICInitStructure);
RCC_APB1PeriphClockCmd ( RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4, ENABLE );
RCC_AHB1PeriphClockCmd ( RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE );
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_PWMIN_P;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = NVIC_PWMIN_S;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init ( &NVIC_InitStructure );
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_PWMIN_P;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = NVIC_PWMIN_S;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init ( &NVIC_InitStructure );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init ( GPIOC, &GPIO_InitStructure );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init ( GPIOB, &GPIO_InitStructure );
GPIO_PinAFConfig ( GPIOC, GPIO_PinSource6, GPIO_AF_TIM3 );
GPIO_PinAFConfig ( GPIOC, GPIO_PinSource7, GPIO_AF_TIM3 );
GPIO_PinAFConfig ( GPIOB, GPIO_PinSource0, GPIO_AF_TIM3 );
GPIO_PinAFConfig ( GPIOB, GPIO_PinSource1, GPIO_AF_TIM3 );
TIM3->PSC = ( 168 / 2 ) - 1;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit ( TIM3, &TIM_ICInitStructure );
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit ( TIM3, &TIM_ICInitStructure );
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit ( TIM3, &TIM_ICInitStructure );
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit ( TIM3, &TIM_ICInitStructure );
TIM_Cmd ( TIM3, ENABLE );
TIM_ITConfig ( TIM3, TIM_IT_CC1, ENABLE );
TIM_ITConfig ( TIM3, TIM_IT_CC2, ENABLE );
TIM_ITConfig ( TIM3, TIM_IT_CC3, ENABLE );
TIM_ITConfig ( TIM3, TIM_IT_CC4, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init ( GPIOD, &GPIO_InitStructure );
GPIO_PinAFConfig ( GPIOD, GPIO_PinSource12, GPIO_AF_TIM3 );
GPIO_PinAFConfig ( GPIOD, GPIO_PinSource13, GPIO_AF_TIM3 );
GPIO_PinAFConfig ( GPIOD, GPIO_PinSource14, GPIO_AF_TIM3 );
GPIO_PinAFConfig ( GPIOD, GPIO_PinSource15, GPIO_AF_TIM3 );
TIM4->PSC = ( 168 / 2 ) - 1;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit ( TIM4, &TIM_ICInitStructure );
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit ( TIM4, &TIM_ICInitStructure );
TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit ( TIM4, &TIM_ICInitStructure );
TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit ( TIM4, &TIM_ICInitStructure );
/* TIM enable counter */
TIM_Cmd ( TIM4, ENABLE );
/* Enable the CC2 Interrupt Request */
TIM_ITConfig ( TIM4, TIM_IT_CC1, ENABLE );
TIM_ITConfig ( TIM4, TIM_IT_CC2, ENABLE );
TIM_ITConfig ( TIM4, TIM_IT_CC3, ENABLE );
TIM_ITConfig ( TIM4, TIM_IT_CC4, ENABLE );
}
}
// 定时器 TIMx 额中断服务函数中实现主要的业务代码
void _TIM3_IRQHandler ( void ) // TIM3 中断服务程序,
{
static u16 temp_cnt1, temp_cnt1_2, temp_cnt2, temp_cnt2_2, temp_cnt3, temp_cnt3_2, temp_cnt4, temp_cnt4_2;
if ( TIM3->SR & TIM_IT_CC1 ) // 查询 TIM3 的中断状态
{
ch_watch_dog_feed(CH1);//通道检测喂狗
TIM3->SR = ~TIM_IT_CC1;//TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
TIM3->SR = ~TIM_FLAG_CC1OF;
if(pwminmode_irq == PWM) //判断接收机模式,如果为PWM模式,则通道1正常接收pwm信号
{
if ( GPIOC->IDR & GPIO_Pin_6 )
{
temp_cnt1 = TIM_GetCapture1 ( TIM3 ); // 获取TIM3_>CRR1寄存器的值,TIM_GetCapture1():return TIMx->CCR1;
}
else
{
temp_cnt1_2 = TIM_GetCapture1 ( TIM3 );
if ( temp_cnt1_2 >= temp_cnt1 )
Rc_Pwm_In[0] = temp_cnt1_2 - temp_cnt1;
else
Rc_Pwm_In[0] = 0xffff - temp_cnt1 + temp_cnt1_2 + 1;
}
}
else if(pwminmode_irq == PPM) //判断接收机模式,如果为PPM模式,则使用通道1进行PPM信号解析
{
static u8 ch_sta = 0;
if ( GPIOC->IDR & GPIO_Pin_6 )
{
temp_cnt1 = TIM_GetCapture1 ( TIM3 );
}
else
{
temp_cnt1_2 = TIM_GetCapture1 ( TIM3 );
u16 _tmp;
if ( temp_cnt1_2 >= temp_cnt1 )
_tmp = temp_cnt1_2 - temp_cnt1;
else
_tmp = 0xffff - temp_cnt1 + temp_cnt1_2 + 1;
if(_tmp > 2100 || ch_sta == PPM_CHNUM )
{
ch_sta = 0;
}
else
{
ch_watch_dog_feed(ch_sta);
Rc_Ppm_In[ch_sta] = _tmp;
ch_sta++;
}
}
}
}
//如果接收机模式不是pwm模式,则退出后续判断,因为只有pwm模式才需要后续通道采样
if(pwminmode_irq != PWM) return;
if ( TIM3->SR & TIM_IT_CC2 )
{
ch_watch_dog_feed(CH2);//通道检测喂狗
TIM3->SR = ~TIM_IT_CC2;
TIM3->SR = ~TIM_FLAG_CC2OF;
if ( GPIOC->IDR & GPIO_Pin_7 )
{
temp_cnt2 = TIM_GetCapture2 ( TIM3 );
}
else
{
temp_cnt2_2 = TIM_GetCapture2 ( TIM3 );
if ( temp_cnt2_2 >= temp_cnt2 )
Rc_Pwm_In[1] = temp_cnt2_2 - temp_cnt2;
else
Rc_Pwm_In[1] = 0xffff - temp_cnt2 + temp_cnt2_2 + 1;
}
}
if ( TIM3->SR & TIM_IT_CC3 )
{
ch_watch_dog_feed(CH3);//通道检测喂狗
TIM3->SR = ~TIM_IT_CC3;
TIM3->SR = ~TIM_FLAG_CC3OF;
if ( GPIOB->IDR & GPIO_Pin_0 )
{
temp_cnt3 = TIM_GetCapture3 ( TIM3 );
}
else
{
temp_cnt3_2 = TIM_GetCapture3 ( TIM3 );
if ( temp_cnt3_2 >= temp_cnt3 )
Rc_Pwm_In[2] = temp_cnt3_2 - temp_cnt3;
else
Rc_Pwm_In[2] = 0xffff - temp_cnt3 + temp_cnt3_2 + 1;
}
}
if ( TIM3->SR & TIM_IT_CC4 )
{
ch_watch_dog_feed(CH4);//通道检测喂狗
TIM3->SR = ~TIM_IT_CC4;
TIM3->SR = ~TIM_FLAG_CC4OF;
if ( GPIOB->IDR & GPIO_Pin_1 )
{
temp_cnt4 = TIM_GetCapture4 ( TIM3 );
}
else
{
temp_cnt4_2 = TIM_GetCapture4 ( TIM3 );
if ( temp_cnt4_2 >= temp_cnt4 )
Rc_Pwm_In[3] = temp_cnt4_2 - temp_cnt4;
else
Rc_Pwm_In[3] = 0xffff - temp_cnt4 + temp_cnt4_2 + 1;
}
}
}
void _TIM4_IRQHandler ( void )
{
static u16 temp_cnt1, temp_cnt1_2, temp_cnt2, temp_cnt2_2, temp_cnt3, temp_cnt3_2, temp_cnt4, temp_cnt4_2;
if(pwminmode_irq != PWM) return;
if ( TIM4->SR & TIM_IT_CC1 )
{
ch_watch_dog_feed(CH5);//通道检测喂狗
TIM4->SR = ~TIM_IT_CC1;//TIM_ClearITPendingBit(TIM3, TIM_IT_CC1);
TIM4->SR = ~TIM_FLAG_CC1OF;
if ( GPIOD->IDR & GPIO_Pin_12 )
{
temp_cnt1 = TIM_GetCapture1 ( TIM4 );
}
else
{
temp_cnt1_2 = TIM_GetCapture1 ( TIM4 );
if ( temp_cnt1_2 >= temp_cnt1 )
Rc_Pwm_In[4] = temp_cnt1_2 - temp_cnt1;
else
Rc_Pwm_In[4] = 0xffff - temp_cnt1 + temp_cnt1_2 + 1;
}
}
if ( TIM4->SR & TIM_IT_CC2 )
{
ch_watch_dog_feed(CH6);//通道检测喂狗
TIM4->SR = ~TIM_IT_CC2;
TIM4->SR = ~TIM_FLAG_CC2OF;
if ( GPIOD->IDR & GPIO_Pin_13 )
{
temp_cnt2 = TIM_GetCapture2 ( TIM4 );
}
else
{
temp_cnt2_2 = TIM_GetCapture2 ( TIM4 );
if ( temp_cnt2_2 >= temp_cnt2 )
Rc_Pwm_In[5] = temp_cnt2_2 - temp_cnt2;
else
Rc_Pwm_In[5] = 0xffff - temp_cnt2 + temp_cnt2_2 + 1;
}
}
if ( TIM4->SR & TIM_IT_CC3 )
{
ch_watch_dog_feed(CH7);//通道检测喂狗
TIM4->SR = ~TIM_IT_CC3;
TIM4->SR = ~TIM_FLAG_CC3OF;
if ( GPIOD->IDR & GPIO_Pin_14 )
{
temp_cnt3 = TIM_GetCapture3 ( TIM4 );
}
else
{
temp_cnt3_2 = TIM_GetCapture3 ( TIM4 );
if ( temp_cnt3_2 >= temp_cnt3 )
Rc_Pwm_In[6] = temp_cnt3_2 - temp_cnt3;
else
Rc_Pwm_In[6] = 0xffff - temp_cnt3 + temp_cnt3_2 + 1;
}
}
if ( TIM4->SR & TIM_IT_CC4 )
{
ch_watch_dog_feed(CH8);//通道检测喂狗
TIM4->SR = ~TIM_IT_CC4;
TIM4->SR = ~TIM_FLAG_CC4OF;
if ( GPIOD->IDR & GPIO_Pin_15 ) // IDR : GPIO port input data register
{
temp_cnt4 = TIM_GetCapture4 ( TIM4 );
}
else
{
temp_cnt4_2 = TIM_GetCapture4 ( TIM4 );
if ( temp_cnt4_2 >= temp_cnt4 )
Rc_Pwm_In[7] = temp_cnt4_2 - temp_cnt4;
else
Rc_Pwm_In[7] = 0xffff - temp_cnt4 + temp_cnt4_2 + 1;
}
}
}
/******************* (C) COPYRIGHT 2014 ANO TECH *****END OF FILE************/