forked from zhuhuijia0001/usb-device-ch551
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Usb.c
576 lines (489 loc) · 21.9 KB
/
Usb.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#include <string.h>
#include "Type.h"
#include "Ch552.h"
#include "Mcu.h"
#include "PinDefine.h"
#include "System.h"
#include "UsbDef.h"
#include "Usb.h"
#include "UsbDescriptor.h"
#define THIS_ENDP0_SIZE DEFAULT_ENDP0_SIZE
static UINT8X Ep0Buffer[8>(THIS_ENDP0_SIZE+2)?8:(THIS_ENDP0_SIZE+2)] _at_ 0x0000; //端点0 OUT&IN缓冲区,必须是偶地址
static UINT8X Ep1Buffer[64>(MAX_PACKET_SIZE+2)?64:(MAX_PACKET_SIZE+2)] _at_ 0x000a; //端点1 IN缓冲区,必须是偶地址
static UINT8X Ep2Buffer[64>(MAX_PACKET_SIZE+2)?64:(MAX_PACKET_SIZE+2)] _at_ 0x0050; //端点2 IN缓冲区,必须是偶地址
static UINT8 SetupReq,SetupLen,UsbConfig;
static BOOL PCSleeped = FALSE;
static BOOL Ready = FALSE;
static PUINT8 pDescr;
//keyboard led status
static UINT8 volatile keyboardLed = 0x00;
#define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer)
/*******************************************************************************
* Function Name : CH554SoftReset()
* Description : CH554软复位
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CH554SoftReset()
{
SAFE_MOD = 0x55;
SAFE_MOD = 0xAA;
GLOBAL_CFG |=bSW_RESET;
}
/*******************************************************************************
* Function Name : CH554USBDevWakeup()
* Description : CH554设备模式唤醒主机,发送K信号
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void CH554USBDevWakeup()
{
UDEV_CTRL |= bUD_LOW_SPEED;
mDelaymS(2);
UDEV_CTRL &= ~bUD_LOW_SPEED;
}
static UINT8 HexToAscii(UINT8 hex)
{
if (hex <= 9)
{
hex += '0';
}
else if (hex >= 10 && hex <= 15)
{
hex += 'A' - 10;
}
return hex;
}
//for unique serial no
static void InitSerialString()
{
UINT8 i = 2;
UINT8 dat;
dat = *((PUINT8C)(0x3FFC));
StringSerial[i++] = HexToAscii(dat & 0x0f);
StringSerial[i++] = 0;
StringSerial[i++] = HexToAscii(dat >> 4);
StringSerial[i++] = 0;
dat = *((PUINT8C)(0x3FFD));
StringSerial[i++] = HexToAscii(dat & 0x0f);
StringSerial[i++] = 0;
StringSerial[i++] = HexToAscii(dat >> 4);
StringSerial[i++] = 0;
dat = *((PUINT8C)(0x3FFE));
StringSerial[i++] = HexToAscii(dat & 0x0f);
StringSerial[i++] = 0;
StringSerial[i++] = HexToAscii(dat >> 4);
StringSerial[i++] = 0;
dat = *((PUINT8C)(0x3FFF));
StringSerial[i++] = HexToAscii(dat & 0x0f);
StringSerial[i++] = 0;
StringSerial[i++] = HexToAscii(dat >> 4);
StringSerial[i++] = 0;
}
/*******************************************************************************
* Function Name : USBDeviceInit()
* Description : USB设备模式配置,设备模式启动,收发端点配置,中断开启
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USBDeviceInit()
{
InitSerialString();
IE_USB = 0;
USB_CTRL = 0x00; // 先设定USB设备模式
USB_CTRL &= ~bUC_LOW_SPEED;
UDEV_CTRL &= ~bUD_LOW_SPEED;
UEP0_DMA = (UINT16)Ep0Buffer; //端点0数据传输地址
UEP4_1_MOD &= ~(bUEP4_RX_EN | bUEP4_TX_EN); //端点0单64字节收发缓冲区
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK; //OUT事务返回ACK,IN事务返回NAK
UEP1_DMA = (UINT16)Ep1Buffer; //端点1数据传输地址
UEP4_1_MOD = UEP4_1_MOD & ~bUEP1_BUF_MOD | bUEP1_TX_EN; //端点1发送使能 64字节缓冲区
UEP1_CTRL = bUEP_AUTO_TOG | UEP_T_RES_NAK; //端点1自动翻转同步标志位,IN事务返回NAK
UEP2_DMA = (UINT16)Ep2Buffer; //端点2数据传输地址
UEP2_3_MOD = UEP2_3_MOD & ~bUEP2_BUF_MOD | bUEP2_TX_EN; //端点2发送使能 64字节缓冲区
UEP2_CTRL = bUEP_AUTO_TOG | UEP_T_RES_NAK; //端点2自动翻转同步标志位,IN事务返回NAK
USB_DEV_AD = 0x00;
UDEV_CTRL = bUD_PD_DIS; // 禁止DP/DM下拉电阻
USB_CTRL = bUC_DEV_PU_EN | bUC_INT_BUSY | bUC_DMA_EN; // 启动USB设备及DMA,在中断期间中断标志未清除前自动返回NAK
UDEV_CTRL |= bUD_PORT_EN; // 允许USB端口
USB_INT_FG = 0xFF; // 清中断标志
USB_INT_EN = bUIE_SUSPEND | bUIE_TRANSFER | bUIE_BUS_RST;
UEP1_T_LEN = 0; //预使用发送长度一定要清空
UEP2_T_LEN = 0; //预使用发送长度一定要清空
IE_USB = 1;
}
UINT8 GetKeyboardLedStatus()
{
UINT8 led;
HAL_CRITICAL_STATEMENT(led = keyboardLed);
return led;
}
BOOL CheckPCReady()
{
BOOL ready;
HAL_CRITICAL_STATEMENT(ready = Ready);
return ready;
}
void SetPCSleeped(BOOL sleeped)
{
HAL_CRITICAL_STATEMENT(PCSleeped = sleeped);
}
BOOL CheckPCSleeped()
{
BOOL sleeped;
HAL_CRITICAL_STATEMENT(sleeped = PCSleeped);
return sleeped;
}
/*******************************************************************************
* Function Name : Enp1IntIn()
* Description : USB设备模式端点1的中断上传
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void Enp1IntIn(UINT8 *dat, UINT8 size)
{
memcpy(Ep1Buffer, dat, size);
UEP1_T_LEN = size;
UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; //有数据时上传数据并应答ACK
}
/*******************************************************************************
* Function Name : Enp2IntIn()
* Description : USB设备模式端点2的中断上传
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void Enp2IntIn(UINT8 *dat, UINT8 size)
{
memcpy(Ep2Buffer, dat, size);
UEP2_T_LEN = size;
UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_ACK; //有数据时上传数据并应答ACK
}
/*******************************************************************************
* Function Name : DeviceInterrupt()
* Description : CH559USB中断处理函数
*******************************************************************************/
void UsbIsr(void) interrupt INT_NO_USB using 1 //USB中断服务程序,使用寄存器组1
{
UINT8 len = 0;
if(UIF_TRANSFER) //USB传输完成标志
{
switch (USB_INT_ST & (MASK_UIS_TOKEN | MASK_UIS_ENDP))
{
case UIS_TOKEN_IN | 2: //endpoint 2# 中断端点上传
UEP2_T_LEN = 0; //预使用发送长度一定要清空
// UEP1_CTRL ^= bUEP_T_TOG; //如果不设置自动翻转则需要手动翻转
UEP2_CTRL = UEP2_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; //默认应答NAK
break;
case UIS_TOKEN_IN | 1: //endpoint 1# 中断端点上传
UEP1_T_LEN = 0; //预使用发送长度一定要清空
// UEP2_CTRL ^= bUEP_T_TOG; //如果不设置自动翻转则需要手动翻转
UEP1_CTRL = UEP1_CTRL & ~ MASK_UEP_T_RES | UEP_T_RES_NAK; //默认应答NAK
break;
case UIS_TOKEN_SETUP | 0: //SETUP事务
len = USB_RX_LEN;
if(len == (sizeof(USB_SETUP_REQ)))
{
SetupLen = UsbSetupBuf->wLengthL;
if(UsbSetupBuf->wLengthH || SetupLen > 0x7F )
{
SetupLen = 0x7F; // 限制总长度
}
len = 0; // 默认为成功并且上传0长度
SetupReq = UsbSetupBuf->bRequest;
if (( UsbSetupBuf->bRequestType & USB_REQ_TYP_MASK ) != USB_REQ_TYP_STANDARD)/* HID类命令 */
{
switch( SetupReq )
{
case HID_GET_REPORT:
break;
case HID_GET_IDLE:
break;
case HID_GET_PROTOCOL:
break;
case HID_SET_REPORT:
break;
case HID_SET_IDLE:
break;
case HID_SET_PROTOCOL:
break;
default:
len = 0xFF; /*命令不支持*/
break;
}
}
else
{//标准请求
switch(SetupReq) //请求码
{
case USB_GET_DESCRIPTOR:
switch(UsbSetupBuf->wValueH)
{
case USB_DESCR_TYP_DEVICE: //设备描述符
pDescr = DevDesc.descr; //把设备描述符送到要发送的缓冲区
len = DevDesc.size;
//restart enumeration
Ready = FALSE;
break;
case USB_DESCR_TYP_CONFIG: //配置描述符
pDescr = CfgDesc.descr; //把设备描述符送到要发送的缓冲区
len = CfgDesc.size;
break;
case USB_DESCR_TYP_STRING:
pDescr = StringDescriptors[UsbSetupBuf->wValueL].descr;
len = StringDescriptors[UsbSetupBuf->wValueL].size;
break;
case USB_DESCR_TYP_REPORT: //报表描述符
if(UsbSetupBuf->wIndexL == 0) //接口0报表描述符
{
pDescr = KeyRepDesc.descr; //数据准备上传
len = KeyRepDesc.size;
}
else if(UsbSetupBuf->wIndexL == 1) //接口1报表描述符
{
pDescr = MouseRepDesc.descr; //数据准备上传
len = MouseRepDesc.size;
Ready = TRUE; //如果有更多接口,该标准位应该在最后一个接口配置完成后有效
}
else
{
len = 0xFF; //本程序只有2个接口,这句话正常不可能执行
}
break;
case USB_DESCR_TYP_QUALIF:
pDescr = DeviceQualifierCfg.descr;
len = DeviceQualifierCfg.size;
break;
default:
len = 0xFF; //不支持的命令或者出错
break;
}
if (len != 0xFF)
{
if (SetupLen > len)
{
SetupLen = len; //限制总长度
}
len = SetupLen >= DEFAULT_ENDP0_SIZE ? DEFAULT_ENDP0_SIZE : SetupLen; //本次传输长度
memcpy(Ep0Buffer, pDescr, len); //加载上传数据
SetupLen -= len;
pDescr += len;
}
break;
case USB_SET_ADDRESS:
SetupLen = UsbSetupBuf->wValueL; //暂存USB设备地址
break;
case USB_GET_CONFIGURATION:
Ep0Buffer[0] = UsbConfig;
if (SetupLen >= 1)
{
len = 1;
}
break;
case USB_SET_CONFIGURATION:
UsbConfig = UsbSetupBuf->wValueL;
break;
case 0x0A:
break;
case USB_CLEAR_FEATURE: //Clear Feature
if ((UsbSetupBuf->bRequestType & USB_REQ_RECIP_MASK) == USB_REQ_RECIP_ENDP)// 端点
{
switch( UsbSetupBuf->wIndexL )
{
case 0x82:
UEP2_CTRL = UEP2_CTRL & ~ ( bUEP_T_TOG | MASK_UEP_T_RES ) | UEP_T_RES_NAK;
break;
case 0x81:
UEP1_CTRL = UEP1_CTRL & ~ ( bUEP_T_TOG | MASK_UEP_T_RES ) | UEP_T_RES_NAK;
break;
case 0x01:
UEP1_CTRL = UEP1_CTRL & ~ ( bUEP_R_TOG | MASK_UEP_R_RES ) | UEP_R_RES_ACK;
break;
default:
len = 0xFF; // 不支持的端点
break;
}
}
if ((UsbSetupBuf->bRequestType & USB_REQ_RECIP_MASK) == USB_REQ_RECIP_DEVICE )// 璁惧
{
len = 0xFF;
break;
}
else
{
len = 0xFF; // 涓嶆槸绔偣涓嶆敮鎸�
}
break;
case USB_SET_FEATURE: /* Set Feature */
if( ( UsbSetupBuf->bRequestType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_DEVICE ) /* 设置设备 */
{
if( ( ( ( UINT16 )UsbSetupBuf->wValueH << 8 ) | UsbSetupBuf->wValueL ) == 0x01 )
{
if( CfgDesc.descr[ 7 ] & 0x20 )
{
/* 设置唤醒使能标志 */
PCSleeped = TRUE;
}
else
{
len = 0xFF; /* 操作失败 */
}
}
else
{
len = 0xFF; /* 操作失败 */
}
}
else if( ( UsbSetupBuf->bRequestType & USB_REQ_RECIP_MASK ) == USB_REQ_RECIP_ENDP ) /* 设置端点 */
{
if( ( ( ( UINT16 )UsbSetupBuf->wValueH << 8 ) | UsbSetupBuf->wValueL ) == 0x00 )
{
switch( ( ( UINT16 )UsbSetupBuf->wIndexH << 8 ) | UsbSetupBuf->wIndexL )
{
case 0x82:
UEP2_CTRL = UEP2_CTRL & (~bUEP_T_TOG) | UEP_T_RES_STALL;/* 设置端点2 IN STALL */
break;
case 0x02:
UEP2_CTRL = UEP2_CTRL & (~bUEP_R_TOG) | UEP_R_RES_STALL;/* 设置端点2 OUT Stall */
break;
case 0x81:
UEP1_CTRL = UEP1_CTRL & (~bUEP_T_TOG) | UEP_T_RES_STALL;/* 设置端点1 IN STALL */
break;
default:
len = 0xFF; //操作失败
break;
}
}
else
{
len = 0xFF; //操作失败
}
}
else
{
len = 0xFF; //操作失败
}
break;
case USB_GET_STATUS:
Ep0Buffer[0] = 0x00;
Ep0Buffer[1] = 0x00;
if ( SetupLen >= 2 )
{
len = 2;
}
else
{
len = SetupLen;
}
break;
default:
len = 0xFF; //操作失败
break;
}
}
}
else
{
len = 0xff; //包长度错误
}
if (len == 0xff)
{
SetupReq = 0xFF;
UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_STALL | UEP_T_RES_STALL;//STALL
}
else if (len <= DEFAULT_ENDP0_SIZE) //上传数据或者状态阶段返回0长度包
{
UEP0_T_LEN = len;
UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_ACK;//默认数据包是DATA1,返回应答ACK
}
else
{
UEP0_T_LEN = 0; //虽然尚未到状态阶段,但是提前预置上传0长度数据包以防主机提前进入状态阶段
UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_ACK;//默认数据包是DATA1,返回应答ACK
}
break;
case UIS_TOKEN_IN | 0: //endpoint0 IN
switch(SetupReq)
{
case USB_GET_DESCRIPTOR:
case HID_GET_REPORT:
len = SetupLen >= DEFAULT_ENDP0_SIZE ? DEFAULT_ENDP0_SIZE : SetupLen; //本次传输长度
memcpy(Ep0Buffer, pDescr, len); //加载上传数据
SetupLen -= len;
pDescr += len;
UEP0_T_LEN = len;
UEP0_CTRL ^= bUEP_T_TOG; //同步标志位翻转
if (len < DEFAULT_ENDP0_SIZE)
{
SetupReq = 0xFF;
}
break;
case USB_SET_ADDRESS:
USB_DEV_AD = USB_DEV_AD & bUDA_GP_BIT | SetupLen;
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
break;
default:
UEP0_T_LEN = 0; //状态阶段完成中断或者是强制上传0长度数据包结束控制传输
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
break;
}
break;
case UIS_TOKEN_OUT | 0: // endpoint0 OUT
len = USB_RX_LEN;
if (SetupReq == HID_SET_REPORT) //set report
{
keyboardLed = Ep0Buffer[0];
}
UEP0_CTRL ^= bUEP_R_TOG; //同步标志位翻转
break;
default:
break;
}
UIF_TRANSFER = 0; //写0清空中断
}
if (UIF_BUS_RST) //设备模式USB总线复位中断
{
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
UEP1_CTRL = bUEP_AUTO_TOG | UEP_R_RES_ACK;
UEP2_CTRL = bUEP_AUTO_TOG | UEP_R_RES_ACK | UEP_T_RES_NAK;
USB_DEV_AD = 0x00;
UIF_SUSPEND = 0;
UIF_TRANSFER = 0;
UIF_BUS_RST = 0; //清中断标志
}
if (UIF_SUSPEND) //USB总线挂起/唤醒完成
{
UIF_SUSPEND = 0;
if ( USB_MIS_ST & bUMS_SUSPEND ) //挂起
{
//suspend
//TRACE( "zz" ); //睡眠状态
// while ( XBUS_AUX & bUART0_TX ); //等待发送完成
// SAFE_MOD = 0x55;
// SAFE_MOD = 0xAA;
// WAKE_CTRL = bWAK_BY_USB | bWAK_RXD0_LO; //USB或者RXD0有信号时可被唤醒
// PCON |= PD; //睡眠
// SAFE_MOD = 0x55;
// SAFE_MOD = 0xAA;
// WAKE_CTRL = 0x00;
if (!GET_GPIO_BIT(PIN_USB_POWER))
{
Ready = FALSE;
}
}
else
{
//resume
PCSleeped = FALSE;
}
}
else
{ //意外的中断,不可能发生的情况
USB_INT_FG = 0xFF; //清中断标志
// TRACE("UnknownInt N");
}
}