1 | // Copyright (c) 2010, Peter Barrett
|
---|
2 | /*
|
---|
3 | ** Permission to use, copy, modify, and/or distribute this software for
|
---|
4 | ** any purpose with or without fee is hereby granted, provided that the
|
---|
5 | ** above copyright notice and this permission notice appear in all copies.
|
---|
6 | **
|
---|
7 | ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
---|
8 | ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
---|
9 | ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
|
---|
10 | ** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
|
---|
11 | ** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
---|
12 | ** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
|
---|
13 | ** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
---|
14 | ** SOFTWARE.
|
---|
15 | */
|
---|
16 |
|
---|
17 | #include <stdlib.h>
|
---|
18 | #include <stdio.h>
|
---|
19 | #include <stdint.h>
|
---|
20 | //#include "../Arduino.h"
|
---|
21 |
|
---|
22 | #include "sam.h"
|
---|
23 | #include "wiring_constants.h"
|
---|
24 | #include "USBCore.h"
|
---|
25 | #include "USB/USB_device.h" // needed for USB PID define
|
---|
26 | #include "USBDesc.h"
|
---|
27 | #include "USBAPI.h"
|
---|
28 |
|
---|
29 | #define TRACE_CORE(x)
|
---|
30 |
|
---|
31 | //==================================================================
|
---|
32 |
|
---|
33 |
|
---|
34 |
|
---|
35 | /*
|
---|
36 | #if (defined CDC_ENABLED) && defined(HID_ENABLED)
|
---|
37 | //#define USB_PID_ZERO 0x004B // CDC and HID
|
---|
38 | #define USB_PID_ZERO 0x804d // CDC only
|
---|
39 | #else
|
---|
40 | #if (defined CDC_ENABLED)
|
---|
41 | #define USB_PID_ZERO 0x804d // CDC only usbserial.name
|
---|
42 | #else
|
---|
43 | #define USB_PID_ZERO 0x804d // HID only
|
---|
44 | #endif
|
---|
45 | #endif
|
---|
46 | */
|
---|
47 |
|
---|
48 | #define USB_PID_ZERO_PRO 0x804d
|
---|
49 |
|
---|
50 | // USB Device
|
---|
51 | //#define USB_VID 0x2a03 // arduino srl vid
|
---|
52 | //#undef USB_PID
|
---|
53 | //#define USB_PID USB_PID_ZERO
|
---|
54 |
|
---|
55 | //==================================================================
|
---|
56 |
|
---|
57 | static char isRemoteWakeUpEnabled = 0;
|
---|
58 | static char isEndpointHalt = 0;
|
---|
59 |
|
---|
60 |
|
---|
61 | const uint16_t STRING_LANGUAGE[2] = {
|
---|
62 | (3<<8) | (2+2),
|
---|
63 | 0x0409 // English
|
---|
64 | };
|
---|
65 |
|
---|
66 | #ifndef USB_PRODUCT
|
---|
67 | // Use a hardcoded product name if none is provided
|
---|
68 | #if USB_PID == USB_PID_ZERO_PRO
|
---|
69 | #define USB_PRODUCT "Arduino Zero Pro"
|
---|
70 | #else
|
---|
71 | #define USB_PRODUCT "USB IO Board"
|
---|
72 | #endif
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | const uint8_t STRING_PRODUCT[] = USB_PRODUCT;
|
---|
76 |
|
---|
77 | #if USB_VID == 0x2a03
|
---|
78 | # if defined(USB_MANUFACTURER)
|
---|
79 | # undef USB_MANUFACTURER
|
---|
80 | # endif
|
---|
81 | # define USB_MANUFACTURER "Arduino Srl"
|
---|
82 | #elif !defined(USB_MANUFACTURER)
|
---|
83 | // Fall through to unknown if no manufacturer name was provided in a macro
|
---|
84 | # define USB_MANUFACTURER "Unknown"
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | const uint8_t STRING_MANUFACTURER[12] = USB_MANUFACTURER;
|
---|
88 |
|
---|
89 |
|
---|
90 |
|
---|
91 | // DEVICE DESCRIPTOR
|
---|
92 | #if (defined CDC_ENABLED) && defined(HID_ENABLED)
|
---|
93 | const DeviceDescriptor USB_DeviceDescriptor =
|
---|
94 | D_DEVICE(0xEF,0x02,0x01,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
|
---|
95 | #elif defined(CDC_ENABLED) // CDC only
|
---|
96 | const DeviceDescriptor USB_DeviceDescriptor =
|
---|
97 | D_DEVICE(0x02,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
|
---|
98 | #else // HID only
|
---|
99 | const DeviceDescriptor USB_DeviceDescriptor =
|
---|
100 | D_DEVICE(0,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
|
---|
101 | #endif
|
---|
102 |
|
---|
103 | //==================================================================
|
---|
104 |
|
---|
105 | volatile uint32_t _usbConfiguration = 0;
|
---|
106 | volatile uint32_t _usbInitialized = 0;
|
---|
107 | volatile uint32_t _usbSetInterface = 0;
|
---|
108 |
|
---|
109 | //==================================================================
|
---|
110 |
|
---|
111 |
|
---|
112 | // Number of bytes, assumes a rx endpoint
|
---|
113 | uint32_t USBD_Available(uint32_t ep)
|
---|
114 | {
|
---|
115 | return UDD_FifoByteCount(ep);
|
---|
116 | }
|
---|
117 |
|
---|
118 | // Non Blocking receive
|
---|
119 | // Return number of bytes read
|
---|
120 | uint32_t USBD_Recv(uint32_t ep, void* d, uint32_t len)
|
---|
121 | {
|
---|
122 | if (!_usbConfiguration || len < 0)
|
---|
123 | return -1;
|
---|
124 |
|
---|
125 | uint32_t n = UDD_FifoByteCount(ep);
|
---|
126 | len = min(n,len);
|
---|
127 | n = len;
|
---|
128 | uint8_t* dst = (uint8_t*)d;
|
---|
129 | while (n--)
|
---|
130 | *dst++ = UDD_Recv8(ep);
|
---|
131 |
|
---|
132 |
|
---|
133 |
|
---|
134 |
|
---|
135 | if (len && !UDD_FifoByteCount(ep)) // release empty buffer
|
---|
136 | UDD_ReleaseRX(ep);
|
---|
137 |
|
---|
138 | //----- Tx & Rx led blinking during transmission ----- begin ----
|
---|
139 | PORT->Group[1].OUTTGL.reg =0x00000008 ; //RxLED
|
---|
140 | for(int i=0; i < 100000; i++)
|
---|
141 | {
|
---|
142 | asm("NOP");
|
---|
143 | }
|
---|
144 | PORT->Group[1].OUTTGL.reg =0x00000008 ;
|
---|
145 | //----- Tx & Rx led blinking during transmission ----- end ----
|
---|
146 | return len;
|
---|
147 | }
|
---|
148 |
|
---|
149 | // Recv 1 byte if ready
|
---|
150 | uint32_t USBD_Recv(uint32_t ep)
|
---|
151 | {
|
---|
152 | uint8_t c;
|
---|
153 | if (USBD_Recv(ep, &c, 1) != 1)
|
---|
154 | return -1;
|
---|
155 | else
|
---|
156 | return c;
|
---|
157 | }
|
---|
158 |
|
---|
159 | // Blocking Send of data to an endpoint
|
---|
160 | uint32_t USBD_Send(uint32_t ep, const void* d, uint32_t len)
|
---|
161 | {
|
---|
162 | int r = len;
|
---|
163 | const uint8_t* data = (const uint8_t*)d;
|
---|
164 |
|
---|
165 | if (!_usbConfiguration)
|
---|
166 | {
|
---|
167 | TRACE_CORE(printf("pb conf\n\r");)
|
---|
168 | return -1;
|
---|
169 | }
|
---|
170 | UDD_Send(ep, data, len);
|
---|
171 |
|
---|
172 | /* Clear the transfer complete flag */
|
---|
173 | udd_clear_transf_cplt(ep);
|
---|
174 | /* Set the bank as ready */
|
---|
175 | udd_bk_rdy(ep);
|
---|
176 |
|
---|
177 | /* Wait for transfer to complete */
|
---|
178 | while (! udd_is_transf_cplt(ep)); // need fire exit.
|
---|
179 |
|
---|
180 | //----- Tx & Rx led blinking during transmission ----- begin ----
|
---|
181 | PORT->Group[0].OUTTGL.reg =0x08000000 ; //TxLED
|
---|
182 | for(int i=0; i < 100000; i++)
|
---|
183 | {
|
---|
184 | asm("NOP");
|
---|
185 | }
|
---|
186 | PORT->Group[0].OUTTGL.reg =0x08000000 ;
|
---|
187 | /*for(int i=0; i < 100000; i++)
|
---|
188 | {
|
---|
189 | asm("NOP");
|
---|
190 | }*/
|
---|
191 | //----- Tx & Rx led blinking during transmission ----- end ----
|
---|
192 | return r;
|
---|
193 | }
|
---|
194 |
|
---|
195 | uint32_t USBD_SendControl(uint8_t flags, const void* d, uint32_t len)
|
---|
196 | {
|
---|
197 | const uint8_t* data = (const uint8_t*)d;
|
---|
198 | uint32_t length = len;
|
---|
199 | uint32_t sent = 0;
|
---|
200 | uint32_t pos = 0;
|
---|
201 |
|
---|
202 | TRACE_CORE(printf("=> USBD_SendControl TOTAL len=%lu\r\n", len);)
|
---|
203 |
|
---|
204 | while (len > 0)
|
---|
205 | {
|
---|
206 | sent = UDD_Send(EP0, data + pos, len);
|
---|
207 | TRACE_CORE(printf("=> USBD_SendControl sent=%lu\r\n", sent);)
|
---|
208 | pos += sent;
|
---|
209 | len -= sent;
|
---|
210 | }
|
---|
211 |
|
---|
212 | return length;
|
---|
213 | }
|
---|
214 |
|
---|
215 | // Send a USB descriptor string. The string is stored as a
|
---|
216 | // plain ASCII string but is sent out as UTF-16 with the
|
---|
217 | // correct 2-byte prefix
|
---|
218 | static bool USB_SendStringDescriptor(const uint8_t *string, int wLength)
|
---|
219 | {
|
---|
220 | uint16_t buff[64];
|
---|
221 | int l = 1;
|
---|
222 |
|
---|
223 | wLength -= 2;
|
---|
224 | while (*string && wLength>0)
|
---|
225 | {
|
---|
226 | buff[l++] = (uint8_t)(*string++);
|
---|
227 | wLength -= 2;
|
---|
228 | }
|
---|
229 | buff[0] = (3<<8) | (l*2);
|
---|
230 |
|
---|
231 | return USBD_SendControl(0, (uint8_t*)buff, l*2);
|
---|
232 | }
|
---|
233 |
|
---|
234 | uint32_t USBD_RecvControl(void* d, uint32_t len)
|
---|
235 | {
|
---|
236 | udd_ack_out_received(0);
|
---|
237 |
|
---|
238 | return len;
|
---|
239 | }
|
---|
240 |
|
---|
241 | // Handle CLASS_INTERFACE requests
|
---|
242 | bool USBD_ClassInterfaceRequest(Setup& setup)
|
---|
243 | {
|
---|
244 | uint8_t i = setup.wIndex;
|
---|
245 |
|
---|
246 | TRACE_CORE(printf("=> USBD_ClassInterfaceRequest\r\n");)
|
---|
247 |
|
---|
248 | #ifdef CDC_ENABLED
|
---|
249 | if (CDC_ACM_INTERFACE == i)
|
---|
250 | {
|
---|
251 | if( CDC_Setup(setup) == false )
|
---|
252 | {
|
---|
253 | send_zlp();
|
---|
254 | }
|
---|
255 | return true;
|
---|
256 | }
|
---|
257 | #endif
|
---|
258 |
|
---|
259 | #ifdef HID_ENABLED
|
---|
260 | if (HID_INTERFACE == i)
|
---|
261 | {
|
---|
262 | if( HID_Setup(setup) == true )
|
---|
263 | {
|
---|
264 | send_zlp();
|
---|
265 | }
|
---|
266 | return true;
|
---|
267 | }
|
---|
268 | #endif
|
---|
269 |
|
---|
270 | return false;
|
---|
271 | }
|
---|
272 |
|
---|
273 | // Construct a dynamic configuration descriptor
|
---|
274 | // This really needs dynamic endpoint allocation etc
|
---|
275 | // TODO
|
---|
276 | static bool USBD_SendConfiguration(uint32_t maxlen)
|
---|
277 | {
|
---|
278 | uint8_t cache_buffer[128];
|
---|
279 | uint8_t i;
|
---|
280 |
|
---|
281 | const uint8_t* interfaces;
|
---|
282 | uint32_t interfaces_length = 0;
|
---|
283 | uint8_t num_interfaces[1];
|
---|
284 |
|
---|
285 | num_interfaces[0] = 0;
|
---|
286 |
|
---|
287 | #if (defined CDC_ENABLED) && defined(HID_ENABLED)
|
---|
288 | num_interfaces[0] += 3;
|
---|
289 | interfaces = (const uint8_t*) CDC_GetInterface();
|
---|
290 | interfaces_length = CDC_GetInterfaceLength() + HID_GetInterfaceLength();
|
---|
291 | if( maxlen > CDC_GetInterfaceLength() + HID_GetInterfaceLength() + sizeof(ConfigDescriptor) )
|
---|
292 | {
|
---|
293 | maxlen = CDC_GetInterfaceLength() + HID_GetInterfaceLength() + sizeof(ConfigDescriptor);
|
---|
294 | }
|
---|
295 |
|
---|
296 | #else
|
---|
297 | #ifdef CDC_ENABLED
|
---|
298 | num_interfaces[0] += 2;
|
---|
299 | interfaces = (const uint8_t*) CDC_GetInterface();
|
---|
300 | interfaces_length += CDC_GetInterfaceLength();
|
---|
301 | if( maxlen > CDC_GetInterfaceLength()+ sizeof(ConfigDescriptor) )
|
---|
302 | {
|
---|
303 | maxlen = CDC_GetInterfaceLength()+ sizeof(ConfigDescriptor);
|
---|
304 | }
|
---|
305 | #endif
|
---|
306 |
|
---|
307 | #ifdef HID_ENABLED
|
---|
308 | num_interfaces[0] += 1;
|
---|
309 | interfaces = (const uint8_t*) HID_GetInterface();
|
---|
310 | interfaces_length += HID_GetInterfaceLength();
|
---|
311 | if( maxlen > HID_GetInterfaceLength()+ sizeof(ConfigDescriptor) )
|
---|
312 | {
|
---|
313 | maxlen = HID_GetInterfaceLength()+ sizeof(ConfigDescriptor);
|
---|
314 | }
|
---|
315 | #endif
|
---|
316 | #endif
|
---|
317 |
|
---|
318 | _Pragma("pack(1)")
|
---|
319 | ConfigDescriptor config = D_CONFIG((uint16_t)(interfaces_length + sizeof(ConfigDescriptor)),num_interfaces[0]);
|
---|
320 | _Pragma("pack()")
|
---|
321 |
|
---|
322 | memcpy( cache_buffer, &config, sizeof(ConfigDescriptor) );
|
---|
323 |
|
---|
324 | #if (defined CDC_ENABLED) && defined(HID_ENABLED)
|
---|
325 | for ( i=0; i<CDC_GetInterfaceLength(); i++)
|
---|
326 | {
|
---|
327 | cache_buffer[i+sizeof(ConfigDescriptor)] = interfaces[i];
|
---|
328 | }
|
---|
329 | interfaces = (const uint8_t*) HID_GetInterface();
|
---|
330 | for ( i=0; i<HID_GetInterfaceLength(); i++)
|
---|
331 | {
|
---|
332 | cache_buffer[i+sizeof(ConfigDescriptor)+CDC_GetInterfaceLength()] = interfaces[i];
|
---|
333 | }
|
---|
334 | #else
|
---|
335 | #ifdef HID_ENABLED
|
---|
336 | for ( i=0; i<interfaces_length; i++)
|
---|
337 | {
|
---|
338 | cache_buffer[i+sizeof(ConfigDescriptor)] = interfaces[i];
|
---|
339 | }
|
---|
340 | #endif
|
---|
341 |
|
---|
342 | #ifdef CDC_ENABLED
|
---|
343 | for ( i=0; i<interfaces_length; i++)
|
---|
344 | {
|
---|
345 | cache_buffer[i+sizeof(ConfigDescriptor)] = interfaces[i];
|
---|
346 | }
|
---|
347 | #endif
|
---|
348 | #endif
|
---|
349 |
|
---|
350 | if (maxlen > sizeof(cache_buffer))
|
---|
351 | {
|
---|
352 | maxlen = sizeof(cache_buffer);
|
---|
353 | }
|
---|
354 | USBD_SendControl(0,cache_buffer, maxlen );
|
---|
355 | return true;
|
---|
356 | }
|
---|
357 |
|
---|
358 | static bool USBD_SendDescriptor(Setup* pSetup)
|
---|
359 | {
|
---|
360 | uint8_t t = pSetup->wValueH;
|
---|
361 | uint8_t desc_length = 0;
|
---|
362 | const uint8_t* desc_addr = 0;
|
---|
363 |
|
---|
364 | if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t)
|
---|
365 | {
|
---|
366 | TRACE_CORE(printf("=> USBD_SendDescriptor : USB_CONFIGURATION_DESCRIPTOR_TYPE length=%d\r\n", setup.wLength);)
|
---|
367 | return USBD_SendConfiguration(pSetup->wLength);
|
---|
368 | }
|
---|
369 |
|
---|
370 | #ifdef HID_ENABLED
|
---|
371 | if (HID_REPORT_DESCRIPTOR_TYPE == t)
|
---|
372 | {
|
---|
373 | TRACE_CORE(puts("=> USBD_SendDescriptor : HID_REPORT_DESCRIPTOR_TYPE\r\n");)
|
---|
374 | return HID_GetDescriptor();
|
---|
375 | }
|
---|
376 | if (HID_HID_DESCRIPTOR_TYPE == t)
|
---|
377 | {
|
---|
378 | uint8_t tab[9] = D_HIDREPORT((uint8_t)HID_SizeReportDescriptor());
|
---|
379 |
|
---|
380 | TRACE_CORE(puts("=> USBD_SendDescriptor : HID_HID_DESCRIPTOR_TYPE\r\n");)
|
---|
381 |
|
---|
382 | return USBD_SendControl(0, tab, sizeof(tab));
|
---|
383 | }
|
---|
384 | #endif
|
---|
385 |
|
---|
386 | if (USB_DEVICE_DESCRIPTOR_TYPE == t)
|
---|
387 | {
|
---|
388 | TRACE_CORE(puts("=> USBD_SendDescriptor : USB_DEVICE_DESCRIPTOR_TYPE\r\n");)
|
---|
389 | desc_addr = (const uint8_t*)&USB_DeviceDescriptor;
|
---|
390 | if( *desc_addr > pSetup->wLength ) {
|
---|
391 | desc_length = pSetup->wLength;
|
---|
392 | }
|
---|
393 | }
|
---|
394 | else if (USB_STRING_DESCRIPTOR_TYPE == t)
|
---|
395 | {
|
---|
396 | TRACE_CORE(puts("=> USBD_SendDescriptor : USB_STRING_DESCRIPTOR_TYPE\r\n");)
|
---|
397 | if (pSetup->wValueL == 0) {
|
---|
398 | desc_addr = (const uint8_t*)&STRING_LANGUAGE;
|
---|
399 | }
|
---|
400 | else if (pSetup->wValueL == IPRODUCT) {
|
---|
401 | return USB_SendStringDescriptor(STRING_PRODUCT, pSetup->wLength);
|
---|
402 | }
|
---|
403 | else if (pSetup->wValueL == IMANUFACTURER) {
|
---|
404 | return USB_SendStringDescriptor(STRING_MANUFACTURER, pSetup->wLength);
|
---|
405 | }
|
---|
406 | else {
|
---|
407 | return false;
|
---|
408 | }
|
---|
409 | if( *desc_addr > pSetup->wLength ) {
|
---|
410 | desc_length = pSetup->wLength;
|
---|
411 | }
|
---|
412 | }
|
---|
413 | else
|
---|
414 | {
|
---|
415 | TRACE_CORE(printf("Device ERROR");)
|
---|
416 | }
|
---|
417 |
|
---|
418 | if (desc_addr == 0)
|
---|
419 | {
|
---|
420 | return false;
|
---|
421 | }
|
---|
422 |
|
---|
423 | if (desc_length == 0)
|
---|
424 | {
|
---|
425 | desc_length = *desc_addr;
|
---|
426 | }
|
---|
427 |
|
---|
428 | TRACE_CORE(printf("=> USBD_SendDescriptor : desc_addr=%p desc_length=%d\r\n", desc_addr, desc_length);)
|
---|
429 | USBD_SendControl(0, desc_addr, desc_length);
|
---|
430 |
|
---|
431 | return true;
|
---|
432 | }
|
---|
433 |
|
---|
434 |
|
---|
435 | void EndpointHandler(uint8_t bEndpoint)
|
---|
436 | {
|
---|
437 | #ifdef CDC_ENABLED
|
---|
438 | if( bEndpoint == CDC_ENDPOINT_OUT )
|
---|
439 | {
|
---|
440 | udd_ack_out_received(CDC_ENDPOINT_OUT);
|
---|
441 |
|
---|
442 | // Handle received bytes
|
---|
443 | if (USBD_Available(CDC_ENDPOINT_OUT))
|
---|
444 | {
|
---|
445 | SerialUSB.accept();
|
---|
446 | }
|
---|
447 | }
|
---|
448 | if( bEndpoint == CDC_ENDPOINT_IN )
|
---|
449 | {
|
---|
450 | udd_ack_in_received(CDC_ENDPOINT_IN);
|
---|
451 | /* Clear the transfer complete flag */
|
---|
452 | udd_clear_transf_cplt(CDC_ENDPOINT_IN);
|
---|
453 |
|
---|
454 | }
|
---|
455 | if( bEndpoint == CDC_ENDPOINT_ACM )
|
---|
456 | {
|
---|
457 | udd_ack_in_received(CDC_ENDPOINT_ACM);
|
---|
458 | /* Clear the transfer complete flag */
|
---|
459 | udd_clear_transf_cplt(CDC_ENDPOINT_ACM);
|
---|
460 | }
|
---|
461 | #endif
|
---|
462 |
|
---|
463 | #ifdef HID_ENABLED
|
---|
464 | /* Nothing to do in our example */
|
---|
465 | #endif
|
---|
466 | }
|
---|
467 |
|
---|
468 |
|
---|
469 | void USB_Handler(void)
|
---|
470 | {
|
---|
471 | uint16_t flags;
|
---|
472 | uint8_t i;
|
---|
473 | uint8_t ept_int;
|
---|
474 |
|
---|
475 | ept_int = udd_endpoint_interrupt();
|
---|
476 |
|
---|
477 | /* Not endpoint interrupt */
|
---|
478 | if (0 == ept_int)
|
---|
479 | {
|
---|
480 | udd_clear_wakeup_interrupt();
|
---|
481 | udd_clear_eorsm_interrupt();
|
---|
482 | udd_clear_suspend_interrupt();
|
---|
483 |
|
---|
484 | // End of bus reset
|
---|
485 | if (Is_udd_reset())
|
---|
486 | {
|
---|
487 | TRACE_CORE(printf(">>> End of Reset\r\n");)
|
---|
488 | // Reset USB address to 0
|
---|
489 | udd_configure_address(0);
|
---|
490 |
|
---|
491 | // Configure EP 0
|
---|
492 | UDD_InitEP(0, USB_ENDPOINT_TYPE_CONTROL);
|
---|
493 | udd_enable_setup_received_interrupt(0);
|
---|
494 | _usbConfiguration = 0;
|
---|
495 | udd_ack_reset();
|
---|
496 | }
|
---|
497 |
|
---|
498 | if (Is_udd_sof())
|
---|
499 | {
|
---|
500 | udd_ack_sof();
|
---|
501 | }
|
---|
502 |
|
---|
503 | }
|
---|
504 | else
|
---|
505 | {
|
---|
506 | // Endpoint interrupt
|
---|
507 | flags = udd_read_endpoint_flag(0);
|
---|
508 |
|
---|
509 | // endpoint received setup interrupt
|
---|
510 | if (flags & USB_DEVICE_EPINTFLAG_RXSTP)
|
---|
511 | {
|
---|
512 | Setup *pSetupData;
|
---|
513 |
|
---|
514 | /* Clear the Received Setup flag */
|
---|
515 | udd_read_endpoint_flag(0) = USB_DEVICE_EPINTFLAG_RXSTP;
|
---|
516 |
|
---|
517 | UDD_Recv(EP0, (uint8_t**)&pSetupData);
|
---|
518 |
|
---|
519 | /* Clear the Bank 0 ready flag on Control OUT */
|
---|
520 | udd_ack_out_received(0);
|
---|
521 |
|
---|
522 | bool ok = true;
|
---|
523 | if (REQUEST_STANDARD == (pSetupData->bmRequestType & REQUEST_TYPE))
|
---|
524 | {
|
---|
525 | unsigned char data_to_be_send[2];
|
---|
526 |
|
---|
527 | // Standard Requests
|
---|
528 | uint8_t r = pSetupData->bRequest;
|
---|
529 | if (GET_STATUS == r)
|
---|
530 | {
|
---|
531 | if( pSetupData->bmRequestType == 0 ) // device
|
---|
532 | {
|
---|
533 | // Send the device status
|
---|
534 | TRACE_CORE(puts(">>> EP0 Int: GET_STATUS\r\n");)
|
---|
535 | // Check current configuration for power mode (if device is configured)
|
---|
536 |
|
---|
537 | // Check if remote wake-up is enabled
|
---|
538 |
|
---|
539 | data_to_be_send[0]=0;
|
---|
540 | data_to_be_send[1]=0;
|
---|
541 | UDD_Send(0, data_to_be_send, 2);
|
---|
542 | }
|
---|
543 | // if( pSetupData->bmRequestType == 2 ) // Endpoint:
|
---|
544 | else
|
---|
545 | {
|
---|
546 | // Send the endpoint status
|
---|
547 | // Check if the endpoint if currently halted
|
---|
548 | if( isEndpointHalt == 1 )
|
---|
549 | data_to_be_send[0]=1;
|
---|
550 | else
|
---|
551 | data_to_be_send[0]=0;
|
---|
552 | data_to_be_send[1]=0;
|
---|
553 | UDD_Send(0, data_to_be_send, 2);
|
---|
554 | }
|
---|
555 | }
|
---|
556 | else if (CLEAR_FEATURE == r)
|
---|
557 | {
|
---|
558 | // Check which is the selected feature
|
---|
559 | if( pSetupData->wValueL == 1) // DEVICEREMOTEWAKEUP
|
---|
560 | {
|
---|
561 | // Enable remote wake-up and send a ZLP
|
---|
562 | if( isRemoteWakeUpEnabled == 1 )
|
---|
563 | data_to_be_send[0]=1;
|
---|
564 | else
|
---|
565 | data_to_be_send[0]=0;
|
---|
566 | data_to_be_send[1]=0;
|
---|
567 | UDD_Send(0, data_to_be_send, 2);
|
---|
568 | }
|
---|
569 | else // if( pSetupData->wValueL == 0) // ENDPOINTHALT
|
---|
570 | {
|
---|
571 | isEndpointHalt = 0;
|
---|
572 | send_zlp();
|
---|
573 | }
|
---|
574 | }
|
---|
575 | else if (SET_FEATURE == r)
|
---|
576 | {
|
---|
577 | // Check which is the selected feature
|
---|
578 | if( pSetupData->wValueL == 1) // DEVICEREMOTEWAKEUP
|
---|
579 | {
|
---|
580 | // Enable remote wake-up and send a ZLP
|
---|
581 | isRemoteWakeUpEnabled = 1;
|
---|
582 | data_to_be_send[0] = 0;
|
---|
583 | UDD_Send(0, data_to_be_send, 1);
|
---|
584 | }
|
---|
585 | if( pSetupData->wValueL == 0) // ENDPOINTHALT
|
---|
586 | {
|
---|
587 | // Halt endpoint
|
---|
588 | isEndpointHalt = 1;
|
---|
589 | send_zlp();
|
---|
590 | }
|
---|
591 | }
|
---|
592 | else if (SET_ADDRESS == r)
|
---|
593 | {
|
---|
594 | TRACE_CORE(puts(">>> EP0 Int: SET_ADDRESS\r\n");)
|
---|
595 | UDD_SetAddress(pSetupData->wValueL);
|
---|
596 | }
|
---|
597 | else if (GET_DESCRIPTOR == r)
|
---|
598 | {
|
---|
599 | TRACE_CORE(puts(">>> EP0 Int: GET_DESCRIPTOR\r\n");)
|
---|
600 | ok = USBD_SendDescriptor(pSetupData);
|
---|
601 | }
|
---|
602 | else if (SET_DESCRIPTOR == r)
|
---|
603 | {
|
---|
604 | TRACE_CORE(puts(">>> EP0 Int: SET_DESCRIPTOR\r\n");)
|
---|
605 | ok = false;
|
---|
606 | }
|
---|
607 | else if (GET_CONFIGURATION == r)
|
---|
608 | {
|
---|
609 | TRACE_CORE(puts(">>> EP0 Int: GET_CONFIGURATION\r\n");)
|
---|
610 | UDD_Send(0, (void*)&_usbConfiguration, 1);
|
---|
611 | }
|
---|
612 | else if (SET_CONFIGURATION == r)
|
---|
613 | {
|
---|
614 | if (REQUEST_DEVICE == (pSetupData->bmRequestType & REQUEST_RECIPIENT))
|
---|
615 | {
|
---|
616 | TRACE_CORE(printf(">>> EP0 Int: SET_CONFIGURATION REQUEST_DEVICE %d\r\n", pSetupData->wValueL);)
|
---|
617 | #ifdef HID_ENABLED
|
---|
618 | UDD_InitEP( HID_ENDPOINT_INT, USB_ENDPOINT_TYPE_INTERRUPT | USB_ENDPOINT_IN(0));
|
---|
619 | #endif
|
---|
620 |
|
---|
621 | #ifdef CDC_ENABLED
|
---|
622 | UDD_InitEP( CDC_ENDPOINT_ACM, USB_ENDPOINT_TYPE_BULK | USB_ENDPOINT_IN(0));
|
---|
623 | UDD_InitEP( CDC_ENDPOINT_OUT, USB_ENDPOINT_TYPE_BULK | USB_ENDPOINT_OUT(0));
|
---|
624 | UDD_InitEP( CDC_ENDPOINT_IN, USB_ENDPOINT_TYPE_INTERRUPT | USB_ENDPOINT_IN(0));
|
---|
625 | #endif
|
---|
626 | _usbConfiguration = pSetupData->wValueL;
|
---|
627 |
|
---|
628 | #ifdef CDC_ENABLED
|
---|
629 | // Enable interrupt for CDC reception from host (OUT packet)
|
---|
630 | udd_ept_enable_it_transf_cplt_in(CDC_ENDPOINT_ACM);
|
---|
631 | udd_ept_enable_it_transf_cplt_out(CDC_ENDPOINT_OUT);
|
---|
632 | #endif
|
---|
633 | send_zlp();
|
---|
634 | }
|
---|
635 | else
|
---|
636 | {
|
---|
637 | TRACE_CORE(puts(">>> EP0 Int: SET_CONFIGURATION failed!\r\n");)
|
---|
638 | ok = false;
|
---|
639 | }
|
---|
640 | }
|
---|
641 | else if (GET_INTERFACE == r)
|
---|
642 | {
|
---|
643 | TRACE_CORE(puts(">>> EP0 Int: GET_INTERFACE\r\n");)
|
---|
644 | UDD_Send(0, (void*)&_usbSetInterface, 1);
|
---|
645 | }
|
---|
646 | else if (SET_INTERFACE == r)
|
---|
647 | {
|
---|
648 | _usbSetInterface = pSetupData->wValueL;
|
---|
649 | TRACE_CORE(puts(">>> EP0 Int: SET_INTERFACE\r\n");)
|
---|
650 | send_zlp();
|
---|
651 | }
|
---|
652 | }
|
---|
653 | else
|
---|
654 | {
|
---|
655 | TRACE_CORE(puts(">>> EP0 Int: ClassInterfaceRequest\r\n");)
|
---|
656 | ok = USBD_ClassInterfaceRequest(*pSetupData);
|
---|
657 | }
|
---|
658 |
|
---|
659 | if (ok)
|
---|
660 | {
|
---|
661 | TRACE_CORE(puts(">>> EP0 Int: Send packet\r\n");)
|
---|
662 | UDD_ClearIN();
|
---|
663 | }
|
---|
664 | else
|
---|
665 | {
|
---|
666 | TRACE_CORE(puts(">>> EP0 Int: Stall\r\n");)
|
---|
667 | UDD_Stall(0);
|
---|
668 | }
|
---|
669 |
|
---|
670 | if( flags & USB_DEVICE_EPINTFLAG_STALL1 )
|
---|
671 | {
|
---|
672 | /* Clear the stall flag */
|
---|
673 | udd_clear_stall_request(0);
|
---|
674 |
|
---|
675 | // Remove stall request
|
---|
676 | udd_remove_stall_request(0);
|
---|
677 | }
|
---|
678 | } // end if USB_DEVICE_EPINTFLAG_RXSTP
|
---|
679 |
|
---|
680 | i=0;
|
---|
681 | ept_int &= 0xFE; // Remove endpoint number 0 (setup)
|
---|
682 | while (ept_int != 0)
|
---|
683 | {
|
---|
684 | // Check if endpoint has a pending interrupt
|
---|
685 | if ((ept_int & (1 << i)) != 0)
|
---|
686 | {
|
---|
687 | if( (udd_read_endpoint_flag(i) & USB_DEVICE_EPINTFLAG_TRCPT_Msk ) != 0 )
|
---|
688 |
|
---|
689 | {
|
---|
690 | EndpointHandler(i);
|
---|
691 | }
|
---|
692 | ept_int &= ~(1 << i);
|
---|
693 |
|
---|
694 | if (ept_int != 0)
|
---|
695 | {
|
---|
696 |
|
---|
697 | TRACE_CORE("\n\r - ");
|
---|
698 | }
|
---|
699 | }
|
---|
700 | i++;
|
---|
701 | if( i> USB_EPT_NUM) break; // exit
|
---|
702 | }
|
---|
703 | }
|
---|
704 | }
|
---|
705 |
|
---|
706 |
|
---|
707 |
|
---|
708 | void USBD_Flush(uint32_t ep)
|
---|
709 | {
|
---|
710 | if (UDD_FifoByteCount(ep))
|
---|
711 | {
|
---|
712 | UDD_ReleaseTX(ep);
|
---|
713 | }
|
---|
714 | }
|
---|
715 |
|
---|
716 | // Counting frames
|
---|
717 | uint32_t USBD_Connected(void)
|
---|
718 | {
|
---|
719 | uint8_t f = UDD_GetFrameNumber();
|
---|
720 | return f != UDD_GetFrameNumber();
|
---|
721 | }
|
---|
722 |
|
---|
723 |
|
---|
724 | //=======================================================================
|
---|
725 | //=======================================================================
|
---|
726 |
|
---|
727 | USBDevice_ USBDevice;
|
---|
728 |
|
---|
729 | USBDevice_::USBDevice_()
|
---|
730 | {
|
---|
731 | }
|
---|
732 |
|
---|
733 | bool USBDevice_::attach()
|
---|
734 | {
|
---|
735 | if (_usbInitialized != 0UL)
|
---|
736 | {
|
---|
737 | UDD_Attach();
|
---|
738 | _usbConfiguration = 0;
|
---|
739 | return true;
|
---|
740 | }
|
---|
741 | else
|
---|
742 | {
|
---|
743 | return false;
|
---|
744 | }
|
---|
745 | }
|
---|
746 |
|
---|
747 | bool USBDevice_::detach()
|
---|
748 | {
|
---|
749 | if (_usbInitialized != 0UL)
|
---|
750 | {
|
---|
751 | UDD_Detach();
|
---|
752 | return true;
|
---|
753 | }
|
---|
754 | else
|
---|
755 | {
|
---|
756 | return false;
|
---|
757 | }
|
---|
758 | }
|
---|
759 |
|
---|
760 | bool USBDevice_::configured()
|
---|
761 | {
|
---|
762 | return _usbConfiguration;
|
---|
763 | }
|
---|
764 |
|
---|
765 | void USBDevice_::poll()
|
---|
766 | {
|
---|
767 | }
|
---|
768 |
|
---|
769 | void USBDevice_::init()
|
---|
770 | {
|
---|
771 | UDD_Init();
|
---|
772 | _usbInitialized=1UL;
|
---|
773 | }
|
---|