-
Notifications
You must be signed in to change notification settings - Fork 8
/
usb_ep0.c
228 lines (209 loc) · 6.55 KB
/
usb_ep0.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
#include "usb_desc.h"
#include "usb_endp.h"
#include "usb_cdc.h"
#include <string.h> // For memcpy()
#include "bootloader.h"
#include "ch554_platform.h"
// Used in SET_FEATURE and CLEAR_FEATURE
#define USB_FEATURE_ENDPOINT_HALT 0 // Endpoint only
#define USB_FEATURE_DEVICE_REMOTE_WAKEUP 1 // Device only
#define USB_ENDP0_SIZE DEFAULT_ENDP0_SIZE
// The buffer (Tx and Rx) must have an even address, size: 10 (0x0A)
xdatabuf(EP0_ADDR, Ep0Buffer, 8 > (USB_ENDP0_SIZE + 2) ? 8 : (USB_ENDP0_SIZE + 2));
#define UsbSetupBuf ((PUSB_SETUP_REQ)Ep0Buffer)
uint8_t SetupReq, SetupLen, UsbConfig;
uint8_t* pDescr;
// Process SETUP packet
void USB_EP0_SETUP(void) {
uint8_t len = USB_RX_LEN;
if (len == (sizeof(USB_SETUP_REQ))) {
SetupLen = UsbSetupBuf->wLengthL;
if (UsbSetupBuf->wLengthH || SetupLen > 0x7F)
SetupLen = 0x7F; // Limit the total length
len = 0; // Assume success -> Tx 0-length packet
SetupReq = UsbSetupBuf->bRequest;
if ((UsbSetupBuf->bRequestType & USB_REQ_TYP_MASK) == USB_REQ_TYP_STANDARD) {
// Standard USB Request
switch (SetupReq) {
case USB_GET_DESCRIPTOR:
switch (UsbSetupBuf->wValueH) {
case 1: // Device Descriptor
pDescr = (uint8_t*)&DevDesc;
len = USB_DESCSIZE_DEVICE;
break;
case 2: // Configure Descriptor
pDescr = (uint8_t*)CfgDesc;
len = (USB_DESCSIZE_CONFIG_H << 8) | USB_DESCSIZE_CONFIG_L;
break;
case 3: // String Descriptor
len = UsbSetupBuf->wValueL; // Index
if (len < USB_STRINGDESC_COUNT) {
pDescr = (uint8_t*)(StringDescs[len]);
len = pDescr[0];
} else {
len = 0xFF; // Not supported
}
break;
case 0x22: // Report Descriptor
len = UsbSetupBuf->wIndexL;
if (len < USB_INTERFACES) {
//pDescr = USB_HID_GetReportDesc(len);
//len = USB_HID_GetReportDesc_Length(len);
} else {
len = 0xff; // The host is trying to config invalid interfaces
}
break;
default:
len = 0xff; // Unsupported descriptors
break;
} // switch (UsbSetupBuf->wValueH)
if (SetupLen > len) {
SetupLen = len; // Limit length
}
len = SetupLen >= 8 ? 8 : SetupLen;
memcpy(Ep0Buffer, pDescr, len);
SetupLen -= len;
pDescr += len;
break;
case USB_SET_ADDRESS:
SetupLen = UsbSetupBuf->wValueL; // Save the assigned address
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) { // Endpoint
len = USB_EP_HALT_CLEAR(UsbSetupBuf->wIndexL);
} else {
len = 0xFF; // Unsupported
}
break;
case USB_SET_FEATURE: /* Set Feature */
if ((UsbSetupBuf->bRequestType & USB_REQ_RECIP_MASK) == USB_REQ_RECIP_ENDP) { // Endpoint
if ((((uint16_t)UsbSetupBuf->wValueH << 8) | UsbSetupBuf->wValueL) == USB_FEATURE_ENDPOINT_HALT) {
len = USB_EP_HALT_SET(UsbSetupBuf->wIndexL);
} else {
len = 0xFF; // Unsupported
} // if ((((UINT16)UsbSetupBuf->wValueH << 8) | UsbSetupBuf->wValueL) == 0x00)
} else {
len = 0xFF; // Unsupported
}
break;
case USB_GET_STATUS:
// Device Status Bits:
// D0 1=Self-Powered 0=Bus-powered
// D1 1=Support remote wakeup
// D2-D15 Reserved, must be 0
//
// Interface Status Bits:
// All reserved, must be 0
//
// Endpoint Status Bits:
// D0 1=Endpoint is halted
// D1-D15 Reserved, must be 0
Ep0Buffer[0] = 0x00;
Ep0Buffer[1] = 0x00;
if (SetupLen >= 2) {
len = 2;
} else {
len = SetupLen;
}
break;
default:
len = 0xff; // Unsupported
break;
} // switch (SetupReq)
} // if ((UsbSetupBuf->bRequestType & USB_REQ_TYP_MASK) == USB_REQ_TYP_STANDARD)
else if ((UsbSetupBuf->bRequestType & USB_REQ_TYP_MASK) == USB_REQ_TYP_CLASS) {
// USB CDC
if (UsbSetupBuf->bRequestType & USB_REQ_TYP_IN) {
// Device -> Host
switch( SetupReq ) {
//case SERIAL_STATE: //0x20
// Return CTS DTR state
// Return format: bit4:0 [RING BREAK DSR DCD]
//break;
case GET_LINE_CODING: //0x21 currently configured
pDescr = LineCoding;
len = LINECODING_SIZE;
len = SetupLen >= DEFAULT_ENDP0_SIZE ? DEFAULT_ENDP0_SIZE : SetupLen; // 本次传输长度
memcpy(Ep0Buffer, pDescr, len);
SetupLen -= len;
pDescr += len;
break;
default:
len = 0xFF;
break;
}
} else { // USB_REQ_TYP_OUT
// Host -> Device
switch( SetupReq ) {
case SET_LINE_CODING: //0x20
break;
case SET_CONTROL_LINE_STATE: //0x22 generates RS-232/V.24 style control signals
// 0x00 0x0[0 0 RTS DTR]
break;
//case SEND_BREAK:
//break;
default:
len = 0xFF;
break;
}
}
} // else if ((UsbSetupBuf->bRequestType & USB_REQ_TYP_MASK) == USB_REQ_TYP_CLASS)
// Process class requests and vendor requests here (if necessary)
} else {
len = 0xff; // Wrong packet length
} // if (len == (sizeof(USB_SETUP_REQ)))
//Responce
if (len == 0xff) { // Return STALL to host if operation is not supported
SetupReq = 0xFF;
UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_STALL | UEP_T_RES_STALL;//STALL
} else { // Tx data to host or send 0-length packet
UEP0_T_LEN = len;
UEP0_CTRL = bUEP_R_TOG | bUEP_T_TOG | UEP_R_RES_ACK | UEP_T_RES_ACK; // Expect DATA1, Answer ACK
}
}
// Process IN packet
void USB_EP0_IN(void) {
uint8_t len = 0;
switch (SetupReq)
{
case USB_GET_DESCRIPTOR:
len = SetupLen >= 8 ? 8 : SetupLen; // Fix length
memcpy(Ep0Buffer, pDescr, len); // Copy data to Ep0Buffer, ready to Tx
SetupLen -= len;
pDescr += len;
UEP0_T_LEN = len;
UEP0_CTRL ^= bUEP_T_TOG; // Switch between DATA0 and DATA1
break;
case USB_SET_ADDRESS:
USB_DEV_AD = bUDA_GP_BIT | SetupLen;
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
break;
default:
UEP0_T_LEN = 0; // End of transaction
UEP0_CTRL = UEP_R_RES_ACK | UEP_T_RES_NAK;
break;
}
}
// Process OUT packet
void USB_EP0_OUT(void) {
uint8_t len = USB_RX_LEN;
if (SetupReq == SET_LINE_CODING) {
if( U_TOG_OK ) {
memcpy(LineCoding,UsbSetupBuf,USB_RX_LEN);
CDC_SetBaud();
UEP0_T_LEN = 0;
UEP0_CTRL |= UEP_R_RES_ACK | UEP_T_RES_ACK; // 准备上传0包
}
}
UEP0_CTRL ^= bUEP_R_TOG; // Switch between DATA0 and DATA1
}