source: rtos_arduino/trunk/arduino_lib/libraries/USBHost/src/Usb.cpp@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 23.1 KB
Line 
1/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2
3This software may be distributed and modified under the terms of the GNU
4General Public License version 2 (GPL2) as published by the Free Software
5Foundation and appearing in the file GPL2.TXT included in the packaging of
6this file. Please note that GPL2 Section 2[b] requires that all works based
7on this software must also be made publicly available under the terms of
8the GPL2 ("Copyleft").
9
10Contact information
11-------------------
12
13Circuits At Home, LTD
14Web : http://www.circuitsathome.com
15e-mail : support@circuitsathome.com
16*/
17
18/* USB functions */
19
20#include "Arduino.h"
21#include "Usb.h"
22#include <stdio.h>
23
24static uint32_t usb_error = 0;
25static uint32_t usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE;
26
27/**
28 * \brief USBHost class constructor.
29 */
30USBHost::USBHost() : bmHubPre(0)
31{
32 // Set up state machine
33 usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE;
34
35 // Init host stack
36 init();
37}
38
39/**
40 * \brief Initialize USBHost class.
41 */
42void USBHost::init()
43{
44 devConfigIndex = 0;
45 bmHubPre = 0;
46}
47
48
49/**
50 * \brief Get USBHost state.
51 *
52 * \return USB enumeration status (see USBHost::task).
53 */
54uint32_t USBHost::getUsbTaskState(void)
55{
56 return (usb_task_state);
57}
58
59/**
60 * \brief Set USB state.
61 *
62 * \param state New USBHost status to be set.
63 */
64void USBHost::setUsbTaskState(uint32_t state)
65{
66 usb_task_state = state;
67}
68
69/**
70 * \brief Get endpoint info from USB device address and device endpoint.
71 *
72 * \note This function should be used to know which host pipe is being used for
73 * the corresponding device endpoint.
74 *
75 * \param addr USB device address.
76 * \param ep USB device endpoint number.
77 *
78 * \return Pointer to an EpInfo structure.
79 */
80EpInfo* USBHost::getEpInfoEntry(uint32_t addr, uint32_t ep)
81{
82 UsbDevice *p = addrPool.GetUsbDevicePtr(addr);
83
84 if (!p || !p->epinfo)
85 return NULL;
86
87 EpInfo *pep = p->epinfo;
88
89 for (uint32_t i = 0; i < p->epcount; i++)
90 {
91 if (pep->deviceEpNum == ep)
92 return pep;
93
94 pep++;
95 }
96
97 return NULL;
98}
99
100/**
101 * \brief Set device endpoint entry.
102 *
103 * \note Each device is different and has a different number of endpoints.
104 * This function sets endpoint record structure to the device using address
105 * addr in the address pool.
106 *
107 * \param ul_pipe Pipe address.
108 * \param ul_token_type Token type.
109 *
110 * \retval 0 on success.
111 * \retval USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL device not found.
112 */
113uint32_t USBHost::setEpInfoEntry(uint32_t addr, uint32_t epcount, EpInfo* eprecord_ptr)
114{
115 if (!eprecord_ptr)
116 return USB_ERROR_INVALID_ARGUMENT;
117
118 UsbDevice *p = addrPool.GetUsbDevicePtr(addr);
119
120 if (!p)
121 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
122
123 p->address = addr;
124 p->epinfo = eprecord_ptr;
125 p->epcount = epcount;
126
127 return 0;
128}
129
130/**
131 * \brief Set host pipe target address and set ppep pointer to the endpoint
132 * structure matching the specified USB device address and endpoint.
133 *
134 * \param addr USB device address.
135 * \param ep USB device endpoint number.
136 * \param ppep Endpoint info structure pointer set by setPipeAddress.
137 * \param nak_limit Maximum number of NAK permitted.
138 *
139 * \retval 0 on success.
140 * \retval USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL device not found.
141 * \retval USB_ERROR_EPINFO_IS_NULL no endpoint structure found for this device.
142 * \retval USB_ERROR_EP_NOT_FOUND_IN_TBL the specified device endpoint cannot be found.
143 */
144uint32_t USBHost::setPipeAddress(uint32_t addr, uint32_t ep, EpInfo **ppep, uint32_t &nak_limit)
145{
146 UsbDevice *p = addrPool.GetUsbDevicePtr(addr);
147
148 if (!p)
149 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
150
151 if (!p->epinfo)
152 return USB_ERROR_EPINFO_IS_NULL;
153
154 *ppep = getEpInfoEntry(addr, ep);
155
156 if (!*ppep)
157 return USB_ERROR_EP_NOT_FOUND_IN_TBL;
158
159 nak_limit = (0x0001UL << (((*ppep)->bmNakPower > USB_NAK_MAX_POWER ) ? USB_NAK_MAX_POWER : (*ppep)->bmNakPower));
160 nak_limit--;
161
162 // Set peripheral address
163 TRACE_USBHOST(printf(" => SetAddress deviceEP=%lu configued as hostPIPE=%lu sending to address=%lu\r\n", ep, (*ppep)->hostPipeNum, addr);)
164 uhd_configure_address((*ppep)->hostPipeNum, addr);
165
166 return 0;
167}
168
169/**
170 * \brief Send a control request.
171 * Sets address, endpoint, fills control packet with necessary data, dispatches
172 * control packet, and initiates bulk IN transfer depending on request.
173 *
174 * \param addr USB device address.
175 * \param ep USB device endpoint number.
176 * \param bmReqType Request direction.
177 * \param bRequest Request type.
178 * \param wValLo Value low.
179 * \param wValHi Value high.
180 * \param wInd Index field.
181 * \param total Request length.
182 * \param nbytes Number of bytes to read.
183 * \param dataptr Data pointer.
184 * \param p USB class reader.
185 *
186 * \return 0 on success, error code otherwise.
187 */
188uint32_t USBHost::ctrlReq(uint32_t addr, uint32_t ep, uint8_t bmReqType, uint8_t bRequest, uint8_t wValLo, uint8_t wValHi, uint16_t wInd, uint16_t total, uint32_t nbytes, uint8_t* dataptr, USBReadParser *p)
189{
190 // Request direction, IN or OUT
191 uint32_t direction = 0;
192 uint32_t rcode = 0;
193 SETUP_PKT setup_pkt;
194
195 EpInfo *pep = 0;
196 uint32_t nak_limit;
197
198 TRACE_USBHOST(printf(" => ctrlReq\r\n");)
199
200 // Set peripheral address
201 rcode = setPipeAddress(addr, ep, &pep, nak_limit);
202 if (rcode)
203 return rcode;
204
205 // Allocate Pipe0 with default 64 bytes size if not already initialized
206 // TODO : perform a get device descriptor first to get device endpoint size (else data can be missed if device ep0 > host pipe0)
207 rcode = UHD_Pipe0_Alloc(0, 64);
208 if (rcode)
209 {
210 TRACE_USBHOST(printf("/!\\ USBHost::ctrlReq : EP0 allocation error: %lu\r\n", rcode);)
211 return (rcode);
212 }
213
214 // Determine request direction
215 direction = ((bmReqType & 0x80 ) > 0);
216
217 // Fill in setup packet
218 setup_pkt.ReqType_u.bmRequestType = bmReqType;
219 setup_pkt.bRequest = bRequest;
220 setup_pkt.wVal_u.wValueLo = wValLo;
221 setup_pkt.wVal_u.wValueHi = wValHi;
222 setup_pkt.wIndex = wInd;
223 setup_pkt.wLength = total;
224
225 // Configure and write the setup packet into the FIFO
226 uhd_configure_pipe_token(0, tokSETUP);
227 UHD_Pipe_Write(pep->hostPipeNum, 8, (uint8_t *)&setup_pkt);
228
229 // Dispatch packet
230 rcode = dispatchPkt(tokSETUP, pep->hostPipeNum, nak_limit);
231 if (rcode)
232 {
233 // Return HRSLT if not zero
234 TRACE_USBHOST(printf("/!\\ USBHost::ctrlReq : Setup packet error: %lu\r\n", rcode);)
235 return (rcode);
236 }
237
238 // Data stage (if present)
239 if (dataptr != 0)
240 {
241 if (direction)
242 {
243 // IN transfer
244 TRACE_USBHOST(printf(" => ctrlData IN\r\n");)
245 uint32_t left = total;
246
247 while (left)
248 {
249 // Bytes read into buffer
250 uint32_t read = nbytes;
251
252 rcode = InTransfer(pep, nak_limit, &read, dataptr);
253 if (rcode)
254 return rcode;
255
256 // Invoke callback function if inTransfer completed successfuly and callback function pointer is specified
257 if (!rcode && p)
258 ((USBReadParser*)p)->Parse(read, dataptr, total - left);
259
260 left -= read;
261
262 if (read < nbytes)
263 break;
264 }
265 }
266 else
267 {
268 // OUT transfer
269 TRACE_USBHOST(printf(" => ctrlData OUT\r\n");)
270 rcode = OutTransfer(pep, nak_limit, nbytes, dataptr);
271 }
272
273 if (rcode)
274 {
275 TRACE_USBHOST(printf("/!\\ USBHost::ctrlData : Data packet error: %lu\r\n", rcode);)
276 return (rcode);
277 }
278 }
279
280 // Status stage
281 return dispatchPkt((direction) ? tokOUTHS : tokINHS, pep->hostPipeNum, nak_limit);
282}
283
284/**
285 * \brief Perform IN request to the specified USB device.
286 *
287 * \note This function handles multiple packets (if necessary) and can
288 * receive a maximum of 'nbytesptr' bytes. It keep sending INs and writes data
289 * to memory area pointed by 'data'. The actual amount of received bytes is
290 * stored in 'nbytesptr'.
291 *
292 * \param addr USB device address.
293 * \param ep USB device endpoint number.
294 * \param nbytesptr Receive buffer size. It is set to the amount of received
295 * bytes when the function returns.
296 * \param data Buffer to store received data.
297 *
298 * \return 0 on success, error code otherwise.
299 */
300 uint32_t USBHost::inTransfer(uint32_t addr, uint32_t ep, uint32_t *nbytesptr, uint8_t* data)
301{
302 EpInfo *pep = NULL;
303 uint32_t nak_limit = 0;
304
305 uint32_t rcode = setPipeAddress(addr, ep, &pep, nak_limit);
306
307 if (rcode)
308 {
309 return rcode;
310 }
311
312 return InTransfer(pep, nak_limit, nbytesptr, data);
313}
314
315uint32_t USBHost::InTransfer(EpInfo *pep, uint32_t nak_limit, uint32_t *nbytesptr, uint8_t* data)
316{
317 uint32_t rcode = 0;
318 uint32_t pktsize = 0;
319 uint32_t nbytes = *nbytesptr;
320 uint32_t maxpktsize = pep->maxPktSize;
321
322 *nbytesptr = 0;
323
324 while (1)
325 {
326 // Use a 'return' to exit this loop
327 // IN packet to EP-'endpoint'. Function takes care of NAKS.
328 rcode = dispatchPkt(tokIN, pep->hostPipeNum, nak_limit);
329 if (rcode)
330 {
331 if (rcode == 1)
332 {
333 // Pipe freeze is mandatory to avoid sending IN endlessly (else reception becomes messy then)
334 uhd_freeze_pipe(pep->hostPipeNum);
335 }
336 // Should be 1, indicating NAK. Else return error code.
337 return rcode;
338 }
339
340 // Number of received bytes
341 pktsize = uhd_byte_count(pep->hostPipeNum);
342 if (nbytes < pktsize)
343 {
344 TRACE_USBHOST(printf("/!\\ USBHost::InTransfer : receive buffer is too small, size=%lu, expected=%lu\r\n", nbytes, pktsize);)
345 }
346 data += UHD_Pipe_Read(pep->hostPipeNum, pktsize, data);
347
348 // Add this packet's byte count to total transfer length
349 *nbytesptr += pktsize;
350
351 // The transfer is complete under two conditions:
352 // 1. The device sent a short packet (L.T. maxPacketSize)
353 // 2. 'nbytes' have been transferred.
354 if ((pktsize < maxpktsize) || (*nbytesptr >= nbytes))
355 {
356 return 0;
357 }
358 }
359}
360
361/**
362 * \brief Perform OUT request to the specified USB device.
363 *
364 * \note This function handles multiple packets (if necessary) and sends
365 * 'nbytes' bytes.
366 *
367 * \param addr USB device address.
368 * \param ep USB device endpoint number.
369 * \param nbytes Buffer size to be sent.
370 * \param data Buffer to send.
371 *
372 * \return 0 on success, error code otherwise.
373 */
374uint32_t USBHost::outTransfer(uint32_t addr, uint32_t ep, uint32_t nbytes, uint8_t* data)
375{
376 EpInfo *pep = NULL;
377 uint32_t nak_limit = 0;
378
379 uint32_t rcode = setPipeAddress(addr, ep, &pep, nak_limit);
380
381 if (rcode)
382 {
383 return rcode;
384 }
385
386 return OutTransfer(pep, nak_limit, nbytes, data);
387}
388
389uint32_t USBHost::OutTransfer(EpInfo *pep, uint32_t nak_limit, uint32_t nbytes, uint8_t *data)
390{
391 uint32_t rcode = 0;
392 uint32_t bytes_tosend = 0;
393 uint32_t bytes_left = nbytes;
394 uint32_t maxpktsize = pep->maxPktSize;
395
396 if (maxpktsize < 1)
397 return USB_ERROR_INVALID_MAX_PKT_SIZE;
398
399 while (bytes_left)
400 {
401 bytes_tosend = (bytes_left >= maxpktsize) ? maxpktsize : bytes_left;
402
403 // Write FIFO
404 UHD_Pipe_Write(pep->hostPipeNum, bytes_tosend, data);
405
406 // Use a 'return' to exit this loop
407 // OUT packet to EP-'endpoint'. Function takes care of NAKS.
408 rcode = dispatchPkt(tokOUT, pep->hostPipeNum, nak_limit);
409 if (rcode)
410 {
411 // Should be 0, indicating ACK. Else return error code.
412 return rcode;
413 }
414
415 bytes_left -= bytes_tosend;
416 data += bytes_tosend;
417 }
418
419 // Should be 0 in all cases
420 return rcode;
421}
422
423/**
424 * \brief Dispatch USB packet.
425 *
426 * \note Ensure peripheral address is set and relevant buffer is loaded/empty.
427 * If NAK, tries to re-send up to nak_limit times.
428 * If nak_limit == 0, do not count NAKs, exit after timeout.
429 *
430 * \param token Token type (Setup, In or Out).
431 * \param hostPipeNum Host pipe number to use for sending USB packet.
432 * \param nak_limit Maximum number of NAK permitted.
433 *
434 * \return 0 on success, error code otherwise.
435 */
436uint32_t USBHost::dispatchPkt(uint32_t token, uint32_t hostPipeNum, uint32_t nak_limit)
437{
438 uint32_t timeout = millis() + USB_XFER_TIMEOUT;
439 uint32_t nak_count = 0;
440 uint32_t rcode = USB_ERROR_TRANSFER_TIMEOUT;
441
442 TRACE_USBHOST(printf(" => dispatchPkt token=%lu pipe=%lu nak_limit=%lu\r\n", token, hostPipeNum, nak_limit);)
443
444 // Launch the transfer
445 UHD_Pipe_Send(hostPipeNum, token);
446
447 // Check timeout but don't hold timeout if VBUS is lost
448 while ((timeout > millis()) && (UHD_GetVBUSState() == UHD_STATE_CONNECTED))
449 {
450 // Wait for transfer completion
451 if (UHD_Pipe_Is_Transfer_Complete(hostPipeNum, token))
452 {
453 return 0;
454 }
455
456 // Is NAK received?
457 if (Is_uhd_nak_received(hostPipeNum))
458 {
459 uhd_ack_nak_received(hostPipeNum);
460 nak_count++;
461
462 if (nak_limit && (nak_count == nak_limit))
463 {
464 // Return NAK
465 return 1;
466 }
467 }
468 }
469
470 return rcode;
471}
472
473/**
474 * \brief Configure device using known device classes.
475 * The device get a new address even if its class remain unknown.
476 *
477 * \param parent USB device address of the device's parent (0 if root).
478 * \param port USB device base address (see AddressPoolImpl).
479 * \param lowspeed Device speed.
480 *
481 * \return 0 on success, error code otherwise.
482 */
483uint32_t USBHost::Configuring(uint32_t parent, uint32_t port, uint32_t lowspeed)
484{
485 uint32_t rcode = 0;
486
487 for (; devConfigIndex < USB_NUMDEVICES; ++devConfigIndex)
488 {
489 if (!devConfig[devConfigIndex])
490 continue;
491
492 rcode = devConfig[devConfigIndex]->Init(parent, port, lowspeed);
493
494 if (!rcode)
495 {
496 TRACE_USBHOST(printf("USBHost::Configuring : found device class!\r\n");)
497 devConfigIndex = 0;
498 return 0;
499 }
500
501
502 if (rcode == USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED)
503 {
504 TRACE_USBHOST(printf("USBHost::Configuring : ERROR : device not supported!\r\n");)
505 }
506 else if (rcode == USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE)
507 {
508 TRACE_USBHOST(printf("USBHost::Configuring : ERROR : class instance already in use!\r\n");)
509 }
510 else
511 {
512 // in case of an error devConfigIndex should be reset to 0
513 // in order to start from the very beginning the next time
514 // the program gets here
515 if (rcode != USB_DEV_CONFIG_ERROR_DEVICE_INIT_INCOMPLETE)
516 devConfigIndex = 0;
517
518 return rcode;
519 }
520 }
521
522 // Device class is not supported by any of the registered classes
523 devConfigIndex = 0;
524
525 rcode = DefaultAddressing(parent, port, lowspeed);
526
527 return rcode;
528}
529
530/**
531 * \brief Configure device with unknown USB class.
532 *
533 * \param parent USB device address of the device's parent (0 if root).
534 * \param port USB device base address (see AddressPoolImpl).
535 * \param lowspeed Device speed.
536 *
537 * \return 0 on success, error code otherwise.
538 */
539uint32_t USBHost::DefaultAddressing(uint32_t parent, uint32_t port, uint32_t lowspeed)
540{
541 uint32_t rcode = 0;
542 UsbDevice *p0 = 0, *p = 0;
543
544 // Get pointer to pseudo device with address 0 assigned
545 p0 = addrPool.GetUsbDevicePtr(0);
546
547 if (!p0)
548 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
549
550 if (!p0->epinfo)
551 return USB_ERROR_EPINFO_IS_NULL;
552
553 p0->lowspeed = (lowspeed) ? 1 : 0;
554
555 // Allocate new address according to device class
556 uint32_t bAddress = addrPool.AllocAddress(parent, 0, port);
557
558 if (!bAddress)
559 return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
560
561 p = addrPool.GetUsbDevicePtr(bAddress);
562
563 if (!p)
564 return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
565
566 p->lowspeed = lowspeed;
567
568 // Assign new address to the device
569 rcode = setAddr(0, 0, bAddress);
570
571 if (rcode)
572 {
573 TRACE_USBHOST(printf("/!\\ USBHost::DefaultAddressing : Set address failed with code: %lu\r\n", rcode);)
574 addrPool.FreeAddress(bAddress);
575 bAddress = 0;
576 return rcode;
577 }
578
579 return 0;
580}
581
582/**
583 * \brief Release device and free associated resources.
584 *
585 * \param addr USB device address.
586 *
587 * \return 0 on success, error code otherwise.
588 */
589uint32_t USBHost::ReleaseDevice(uint32_t addr)
590{
591 if (!addr)
592 return 0;
593
594 for (uint32_t i = 0; i < USB_NUMDEVICES; ++i)
595 {
596 if (devConfig[i]->GetAddress() == addr)
597 {
598 return devConfig[i]->Release();
599 }
600 }
601
602 return 0;
603}
604
605/**
606 * \brief Get device descriptor.
607 *
608 * \param addr USB device address.
609 * \param ep USB device endpoint number.
610 * \param nbytes Buffer size.
611 * \param dataptr Buffer to store received descriptor.
612 *
613 * \return 0 on success, error code otherwise.
614 */
615uint32_t USBHost::getDevDescr(uint32_t addr, uint32_t ep, uint32_t nbytes, uint8_t* dataptr)
616{
617 return (ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, 0x00, USB_DESCRIPTOR_DEVICE, 0x0000, nbytes, nbytes, dataptr, 0));
618}
619
620/**
621 * \brief Get configuration descriptor.
622 *
623 * \param addr USB device address.
624 * \param ep USB device endpoint number.
625 * \param nbytes Buffer size.
626 * \param conf Configuration number.
627 * \param dataptr Buffer to store received descriptor.
628 *
629 * \return 0 on success, error code otherwise.
630 */
631uint32_t USBHost::getConfDescr(uint32_t addr, uint32_t ep, uint32_t nbytes, uint32_t conf, uint8_t* dataptr)
632{
633 return (ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, nbytes, nbytes, dataptr, 0));
634}
635
636/**
637 * \brief Get configuration descriptor and extract endpoints using USBReadParser object.
638 *
639 * \param addr USB device address.
640 * \param ep USB device endpoint number.
641 * \param conf Configuration number.
642 * \param p USBReadParser object pointer used to extract endpoints.
643 *
644 * \return 0 on success, error code otherwise.
645 */
646uint32_t USBHost::getConfDescr(uint32_t addr, uint32_t ep, uint32_t conf, USBReadParser *p)
647{
648 const uint32_t bufSize = 64;
649 uint8_t buf[bufSize];
650
651 uint32_t ret = getConfDescr(addr, ep, 8, conf, buf);
652
653 if (ret)
654 return ret;
655
656 uint32_t total = ((USB_CONFIGURATION_DESCRIPTOR*)buf)->wTotalLength;
657 delay(100);
658
659 return (ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, total, bufSize, buf, p));
660}
661
662/**
663 * \brief Get string descriptor.
664 *
665 * \param addr USB device address.
666 * \param ep USB device endpoint number.
667 * \param nbytes Buffer size.
668 * \param index String index.
669 * \param langid Language ID.
670 * \param dataptr Buffer to store received descriptor.
671 *
672 * \return 0 on success, error code otherwise.
673 */
674uint32_t USBHost::getStrDescr(uint32_t addr, uint32_t ep, uint32_t nbytes, uint8_t index, uint16_t langid, uint8_t* dataptr)
675{
676 return (ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, index, USB_DESCRIPTOR_STRING, langid, nbytes, nbytes, dataptr, 0));
677}
678
679/**
680 * \brief Set USB device address.
681 *
682 * \param oldaddr Current USB device address.
683 * \param ep USB device endpoint number.
684 * \param addr New USB device address to be set.
685 *
686 * \return 0 on success, error code otherwise.
687 */
688uint32_t USBHost::setAddr(uint32_t oldaddr, uint32_t ep, uint32_t newaddr)
689{
690 TRACE_USBHOST(printf(" => USBHost::setAddr\r\n");)
691 return ctrlReq(oldaddr, ep, bmREQ_SET, USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, 0x0000, 0, 0);
692}
693
694/**
695 * \brief Set configuration.
696 *
697 * \param addr USB device address.
698 * \param ep USB device endpoint number.
699 * \param conf_value New configuration value to be set.
700 *
701 * \return 0 on success, error code otherwise.
702 */
703uint32_t USBHost::setConf(uint32_t addr, uint32_t ep, uint32_t conf_value)
704{
705 return (ctrlReq(addr, ep, bmREQ_SET, USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000, 0x0000, 0, 0));
706}
707
708/**
709 * \brief USB main task, responsible for enumeration and clean up stage.
710 *
711 * \note Must be periodically called from loop().
712 */
713void USBHost::Task(void)
714{
715 uint32_t rcode = 0;
716 volatile uint32_t tmpdata = 0;
717 static uint32_t delay = 0;
718 uint32_t lowspeed = 0;
719
720 // Update USB task state on Vbus change
721 tmpdata = UHD_GetVBUSState();
722 switch (tmpdata)
723 {
724 case UHD_STATE_ERROR:
725 // Illegal state
726 usb_task_state = USB_DETACHED_SUBSTATE_ILLEGAL;
727 lowspeed = 0;
728 break;
729
730 case UHD_STATE_DISCONNECTED:
731 // Disconnected state
732 if ((usb_task_state & USB_STATE_MASK) != USB_STATE_DETACHED)
733 {
734 usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE;
735 lowspeed = 0;
736 }
737 break;
738
739 case UHD_STATE_CONNECTED:
740 // Attached state
741 if ((usb_task_state & USB_STATE_MASK) == USB_STATE_DETACHED)
742 {
743 delay = millis() + USB_SETTLE_DELAY;
744 usb_task_state = USB_ATTACHED_SUBSTATE_SETTLE;
745 //FIXME TODO: lowspeed = 0 ou 1; already done by hardware?
746 }
747 break;
748 }
749
750 // Poll connected devices (if required)
751 for (uint32_t i = 0; i < USB_NUMDEVICES; ++i)
752 if (devConfig[i])
753 rcode = devConfig[i]->Poll();
754
755 // Perform USB enumeration stage and clean up
756 switch (usb_task_state)
757 {
758 case USB_DETACHED_SUBSTATE_INITIALIZE:
759 TRACE_USBHOST(printf(" + USB_DETACHED_SUBSTATE_INITIALIZE\r\n");)
760
761 // Init USB stack and driver
762 UHD_Init();
763 init();
764
765 // Free all USB resources
766 for (uint32_t i = 0; i < USB_NUMDEVICES; ++i)
767 if (devConfig[i])
768 rcode = devConfig[i]->Release();
769
770 usb_task_state = USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE;
771 break;
772
773 case USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE:
774 // Nothing to do
775 break;
776
777 case USB_DETACHED_SUBSTATE_ILLEGAL:
778 // Nothing to do
779 break;
780
781 case USB_ATTACHED_SUBSTATE_SETTLE:
782 // Settle time for just attached device
783 if (delay < millis())
784 {
785 TRACE_USBHOST(printf(" + USB_ATTACHED_SUBSTATE_SETTLE\r\n");)
786 usb_task_state = USB_ATTACHED_SUBSTATE_RESET_DEVICE;
787 }
788 break;
789
790 case USB_ATTACHED_SUBSTATE_RESET_DEVICE:
791 TRACE_USBHOST(printf(" + USB_ATTACHED_SUBSTATE_RESET_DEVICE\r\n");)
792
793 // Trigger Bus Reset
794 UHD_BusReset();
795 usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE;
796 break;
797
798 case USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE:
799 if (Is_uhd_reset_sent())
800 {
801 TRACE_USBHOST(printf(" + USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE\r\n");)
802
803 // Clear Bus Reset flag
804 uhd_ack_reset_sent();
805
806 // Enable Start Of Frame generation
807 uhd_enable_sof();
808
809 usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_SOF;
810
811 // Wait 20ms after Bus Reset (USB spec)
812 delay = millis() + 20;
813 }
814 break;
815
816 case USB_ATTACHED_SUBSTATE_WAIT_SOF:
817 // Wait for SOF received first
818 if (Is_uhd_sof())
819 {
820 if (delay < millis())
821 {
822 TRACE_USBHOST(printf(" + USB_ATTACHED_SUBSTATE_WAIT_SOF\r\n");)
823
824 // 20ms waiting elapsed
825 usb_task_state = USB_STATE_CONFIGURING;
826 }
827 }
828 break;
829
830 case USB_STATE_CONFIGURING:
831 TRACE_USBHOST(printf(" + USB_STATE_CONFIGURING\r\n");)
832 rcode = Configuring(0, 0, lowspeed);
833
834 if (rcode)
835 {
836 TRACE_USBHOST(printf("/!\\ USBHost::Task : USB_STATE_CONFIGURING failed with code: %lu\r\n", rcode);)
837 if (rcode != USB_DEV_CONFIG_ERROR_DEVICE_INIT_INCOMPLETE)
838 {
839 usb_error = rcode;
840 usb_task_state = USB_STATE_ERROR;
841 }
842 }
843 else
844 {
845 usb_task_state = USB_STATE_RUNNING;
846 TRACE_USBHOST(printf(" + USB_STATE_RUNNING\r\n");)
847 }
848 break;
849
850 case USB_STATE_RUNNING:
851 break;
852
853 case USB_STATE_ERROR:
854 break;
855 }
856}
Note: See TracBrowser for help on using the repository browser.