2012-06-12 14:43:52 +02:00
|
|
|
/* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
|
|
|
|
|
|
|
|
This software may be distributed and modified under the terms of the GNU
|
|
|
|
General Public License version 2 (GPL2) as published by the Free Software
|
|
|
|
Foundation and appearing in the file GPL2.TXT included in the packaging of
|
|
|
|
this file. Please note that GPL2 Section 2[b] requires that all works based
|
|
|
|
on this software must also be made publicly available under the terms of
|
|
|
|
the GPL2 ("Copyleft").
|
|
|
|
|
|
|
|
Contact information
|
|
|
|
-------------------
|
2012-04-06 17:18:55 +02:00
|
|
|
|
2012-06-12 14:43:52 +02:00
|
|
|
Circuits At Home, LTD
|
|
|
|
Web : http://www.circuitsathome.com
|
|
|
|
e-mail : support@circuitsathome.com
|
|
|
|
*/
|
2012-06-26 16:20:26 +02:00
|
|
|
|
2012-06-12 14:43:52 +02:00
|
|
|
/* USB functions */
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
#include "Arduino.h"
|
2012-04-06 17:18:55 +02:00
|
|
|
#include "Usb.h"
|
2012-06-07 16:11:26 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
static uint32_t usb_error = 0;
|
|
|
|
static uint32_t usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE;
|
2012-04-06 17:18:55 +02:00
|
|
|
|
2012-06-07 16:11:26 +02:00
|
|
|
/**
|
2012-06-26 16:20:26 +02:00
|
|
|
* \brief USBHost class constructor.
|
2012-06-07 16:11:26 +02:00
|
|
|
*/
|
2012-06-26 16:20:26 +02:00
|
|
|
USBHost::USBHost() : bmHubPre(0)
|
2012-06-12 14:43:52 +02:00
|
|
|
{
|
2012-06-07 16:11:26 +02:00
|
|
|
// Set up state machine
|
|
|
|
usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE;
|
|
|
|
|
|
|
|
// Init host stack
|
|
|
|
init();
|
2012-04-06 17:18:55 +02:00
|
|
|
}
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
/**
|
2012-06-26 16:20:26 +02:00
|
|
|
* \brief Initialize USBHost class.
|
2012-06-07 16:11:26 +02:00
|
|
|
*/
|
|
|
|
void USBHost::init()
|
2012-04-06 17:18:55 +02:00
|
|
|
{
|
2012-06-12 14:43:52 +02:00
|
|
|
devConfigIndex = 0;
|
|
|
|
bmHubPre = 0;
|
2012-04-06 17:18:55 +02:00
|
|
|
}
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
|
2012-06-07 16:11:26 +02:00
|
|
|
/**
|
2012-06-26 16:20:26 +02:00
|
|
|
* \brief Get USBHost state.
|
|
|
|
*
|
|
|
|
* \return USB enumeration status (see USBHost::task).
|
2012-06-07 16:11:26 +02:00
|
|
|
*/
|
|
|
|
uint32_t USBHost::getUsbTaskState(void)
|
2012-04-06 17:18:55 +02:00
|
|
|
{
|
2012-06-07 16:11:26 +02:00
|
|
|
return (usb_task_state);
|
2012-04-06 17:18:55 +02:00
|
|
|
}
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
/**
|
2012-06-26 16:20:26 +02:00
|
|
|
* \brief Set USB state.
|
|
|
|
*
|
|
|
|
* \param state New USBHost status to be set.
|
2012-06-07 16:11:26 +02:00
|
|
|
*/
|
|
|
|
void USBHost::setUsbTaskState(uint32_t state)
|
2012-04-06 17:18:55 +02:00
|
|
|
{
|
|
|
|
usb_task_state = state;
|
2012-06-07 16:11:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-26 16:20:26 +02:00
|
|
|
* \brief Get endpoint info from USB device address and device endpoint.
|
|
|
|
*
|
|
|
|
* \note This function should be used to know which host pipe is being used for
|
|
|
|
* the corresponding device endpoint.
|
|
|
|
*
|
|
|
|
* \param addr USB device address.
|
|
|
|
* \param ep USB device endpoint number.
|
|
|
|
*
|
|
|
|
* \return Pointer to an EpInfo structure.
|
2012-06-07 16:11:26 +02:00
|
|
|
*/
|
2012-06-12 14:43:52 +02:00
|
|
|
EpInfo* USBHost::getEpInfoEntry(uint32_t addr, uint32_t ep)
|
2012-04-06 17:18:55 +02:00
|
|
|
{
|
2012-06-12 14:43:52 +02:00
|
|
|
UsbDevice *p = addrPool.GetUsbDevicePtr(addr);
|
|
|
|
|
|
|
|
if (!p || !p->epinfo)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
EpInfo *pep = p->epinfo;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-12 14:43:52 +02:00
|
|
|
for (uint32_t i = 0; i < p->epcount; i++)
|
|
|
|
{
|
2012-06-26 16:20:26 +02:00
|
|
|
if (pep->deviceEpNum == ep)
|
2012-06-12 14:43:52 +02:00
|
|
|
return pep;
|
|
|
|
|
|
|
|
pep++;
|
|
|
|
}
|
2012-06-26 16:20:26 +02:00
|
|
|
|
2012-06-12 14:43:52 +02:00
|
|
|
return NULL;
|
2012-04-06 17:18:55 +02:00
|
|
|
}
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
/**
|
2012-06-26 16:20:26 +02:00
|
|
|
* \brief Set device endpoint entry.
|
|
|
|
*
|
|
|
|
* \note Each device is different and has a different number of endpoints.
|
|
|
|
* This function sets endpoint record structure to the device using address
|
|
|
|
* addr in the address pool.
|
|
|
|
*
|
|
|
|
* \param ul_pipe Pipe address.
|
|
|
|
* \param ul_token_type Token type.
|
|
|
|
*
|
|
|
|
* \retval 0 on success.
|
|
|
|
* \retval USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL device not found.
|
2012-06-07 16:11:26 +02:00
|
|
|
*/
|
2012-06-12 14:43:52 +02:00
|
|
|
uint32_t USBHost::setEpInfoEntry(uint32_t addr, uint32_t epcount, EpInfo* eprecord_ptr)
|
|
|
|
{
|
|
|
|
if (!eprecord_ptr)
|
|
|
|
return USB_ERROR_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
UsbDevice *p = addrPool.GetUsbDevicePtr(addr);
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
|
|
|
|
|
|
|
|
p->address = addr;
|
|
|
|
p->epinfo = eprecord_ptr;
|
|
|
|
p->epcount = epcount;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
/**
|
|
|
|
* \brief Set host pipe target address and set ppep pointer to the endpoint
|
|
|
|
* structure matching the specified USB device address and endpoint.
|
|
|
|
*
|
|
|
|
* \param addr USB device address.
|
|
|
|
* \param ep USB device endpoint number.
|
|
|
|
* \param ppep Endpoint info structure pointer set by setPipeAddress.
|
|
|
|
* \param nak_limit Maximum number of NAK permitted.
|
|
|
|
*
|
|
|
|
* \retval 0 on success.
|
|
|
|
* \retval USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL device not found.
|
|
|
|
* \retval USB_ERROR_EPINFO_IS_NULL no endpoint structure found for this device.
|
|
|
|
* \retval USB_ERROR_EP_NOT_FOUND_IN_TBL the specified device endpoint cannot be found.
|
|
|
|
*/
|
|
|
|
uint32_t USBHost::setPipeAddress(uint32_t addr, uint32_t ep, EpInfo **ppep, uint32_t &nak_limit)
|
2012-04-06 17:18:55 +02:00
|
|
|
{
|
2012-06-12 14:43:52 +02:00
|
|
|
UsbDevice *p = addrPool.GetUsbDevicePtr(addr);
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
|
|
|
|
|
|
|
|
if (!p->epinfo)
|
|
|
|
return USB_ERROR_EPINFO_IS_NULL;
|
|
|
|
|
|
|
|
*ppep = getEpInfoEntry(addr, ep);
|
|
|
|
|
|
|
|
if (!*ppep)
|
|
|
|
return USB_ERROR_EP_NOT_FOUND_IN_TBL;
|
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
nak_limit = (0x0001UL << (((*ppep)->bmNakPower > USB_NAK_MAX_POWER ) ? USB_NAK_MAX_POWER : (*ppep)->bmNakPower));
|
2012-06-12 14:43:52 +02:00
|
|
|
nak_limit--;
|
|
|
|
|
|
|
|
// Set peripheral address
|
2012-06-26 16:20:26 +02:00
|
|
|
TRACE_USBHOST(printf(" => SetAddress deviceEP=%lu configued as hostPIPE=%lu sending to address=%lu\r\n", ep, (*ppep)->hostPipeNum, addr);)
|
|
|
|
uhd_configure_address((*ppep)->hostPipeNum, addr);
|
2012-06-12 14:43:52 +02:00
|
|
|
|
|
|
|
return 0;
|
2012-04-06 17:18:55 +02:00
|
|
|
}
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
/**
|
2012-06-26 16:20:26 +02:00
|
|
|
* \brief Send a control request.
|
|
|
|
* Sets address, endpoint, fills control packet with necessary data, dispatches
|
|
|
|
* control packet, and initiates bulk IN transfer depending on request.
|
|
|
|
*
|
|
|
|
* \param addr USB device address.
|
|
|
|
* \param ep USB device endpoint number.
|
|
|
|
* \param bmReqType Request direction.
|
|
|
|
* \param bRequest Request type.
|
|
|
|
* \param wValLo Value low.
|
|
|
|
* \param wValHi Value high.
|
|
|
|
* \param wInd Index field.
|
|
|
|
* \param total Request length.
|
|
|
|
* \param nbytes Number of bytes to read.
|
|
|
|
* \param dataptr Data pointer.
|
|
|
|
* \param p USB class reader.
|
2012-06-07 16:11:26 +02:00
|
|
|
*
|
2012-06-26 16:20:26 +02:00
|
|
|
* \return 0 on success, error code otherwise.
|
2012-06-07 16:11:26 +02:00
|
|
|
*/
|
2012-06-12 14:43:52 +02:00
|
|
|
uint32_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)
|
2012-04-06 17:18:55 +02:00
|
|
|
{
|
2012-06-07 16:11:26 +02:00
|
|
|
// Request direction, IN or OUT
|
|
|
|
uint32_t direction = 0;
|
|
|
|
uint32_t rcode = 0;
|
|
|
|
SETUP_PKT setup_pkt;
|
|
|
|
|
2012-06-12 14:43:52 +02:00
|
|
|
EpInfo *pep = 0;
|
|
|
|
uint32_t nak_limit;
|
|
|
|
|
2012-06-07 16:11:26 +02:00
|
|
|
TRACE_USBHOST(printf(" => ctrlReq\r\n");)
|
|
|
|
|
|
|
|
// Set peripheral address
|
2012-06-26 16:20:26 +02:00
|
|
|
rcode = setPipeAddress(addr, ep, &pep, nak_limit);
|
2012-06-12 14:43:52 +02:00
|
|
|
if (rcode)
|
|
|
|
return rcode;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
// Allocate Pipe0 with default 64 bytes size if not already initialized
|
|
|
|
// TODO : perform a get device descriptor first to get device endpoint size (else data can be missed if device ep0 > host pipe0)
|
|
|
|
rcode = UHD_Pipe0_Alloc(0, 64);
|
2012-06-07 16:11:26 +02:00
|
|
|
if (rcode)
|
|
|
|
{
|
|
|
|
TRACE_USBHOST(printf("/!\\ USBHost::ctrlReq : EP0 allocation error: %lu\r\n", rcode);)
|
|
|
|
return (rcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine request direction
|
2012-06-26 16:20:26 +02:00
|
|
|
direction = ((bmReqType & 0x80 ) > 0);
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
// Fill in setup packet
|
2012-06-12 14:43:52 +02:00
|
|
|
setup_pkt.ReqType_u.bmRequestType = bmReqType;
|
|
|
|
setup_pkt.bRequest = bRequest;
|
|
|
|
setup_pkt.wVal_u.wValueLo = wValLo;
|
|
|
|
setup_pkt.wVal_u.wValueHi = wValHi;
|
|
|
|
setup_pkt.wIndex = wInd;
|
|
|
|
setup_pkt.wLength = total;
|
|
|
|
|
|
|
|
// Configure and write the setup packet into the FIFO
|
|
|
|
uhd_configure_pipe_token(0, tokSETUP);
|
2012-06-26 16:20:26 +02:00
|
|
|
UHD_Pipe_Write(pep->hostPipeNum, 8, (uint8_t *)&setup_pkt);
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
// Dispatch packet
|
2012-06-26 16:20:26 +02:00
|
|
|
rcode = dispatchPkt(tokSETUP, pep->hostPipeNum, nak_limit);
|
2012-06-07 16:11:26 +02:00
|
|
|
if (rcode)
|
|
|
|
{
|
|
|
|
// Return HRSLT if not zero
|
|
|
|
TRACE_USBHOST(printf("/!\\ USBHost::ctrlReq : Setup packet error: %lu\r\n", rcode);)
|
|
|
|
return (rcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data stage (if present)
|
|
|
|
if (dataptr != 0)
|
|
|
|
{
|
2012-06-12 14:43:52 +02:00
|
|
|
if (direction)
|
2012-06-07 16:11:26 +02:00
|
|
|
{
|
2012-06-12 14:43:52 +02:00
|
|
|
// IN transfer
|
|
|
|
TRACE_USBHOST(printf(" => ctrlData IN\r\n");)
|
|
|
|
uint32_t left = total;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-12 14:43:52 +02:00
|
|
|
while (left)
|
|
|
|
{
|
|
|
|
// Bytes read into buffer
|
|
|
|
uint32_t read = nbytes;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-12 14:43:52 +02:00
|
|
|
rcode = InTransfer(pep, nak_limit, &read, dataptr);
|
|
|
|
if (rcode)
|
|
|
|
return rcode;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-12 14:43:52 +02:00
|
|
|
// Invoke callback function if inTransfer completed successfuly and callback function pointer is specified
|
|
|
|
if (!rcode && p)
|
|
|
|
((USBReadParser*)p)->Parse(read, dataptr, total - left);
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-12 14:43:52 +02:00
|
|
|
left -= read;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-12 14:43:52 +02:00
|
|
|
if (read < nbytes)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// OUT transfer
|
|
|
|
TRACE_USBHOST(printf(" => ctrlData OUT\r\n");)
|
|
|
|
rcode = OutTransfer(pep, nak_limit, nbytes, dataptr);
|
|
|
|
}
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-12 14:43:52 +02:00
|
|
|
if (rcode)
|
|
|
|
{
|
|
|
|
TRACE_USBHOST(printf("/!\\ USBHost::ctrlData : Data packet error: %lu\r\n", rcode);)
|
|
|
|
return (rcode);
|
|
|
|
}
|
2012-06-07 16:11:26 +02:00
|
|
|
}
|
2012-06-12 14:43:52 +02:00
|
|
|
|
|
|
|
// Status stage
|
2012-06-26 16:20:26 +02:00
|
|
|
return dispatchPkt((direction) ? tokOUTHS : tokINHS, pep->hostPipeNum, nak_limit);
|
2012-04-06 17:18:55 +02:00
|
|
|
}
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
/**
|
2012-06-26 16:20:26 +02:00
|
|
|
* \brief Perform IN request to the specified USB device.
|
|
|
|
*
|
|
|
|
* \note This function handles multiple packets (if necessary) and can
|
|
|
|
* receive a maximum of 'nbytesptr' bytes. It keep sending INs and writes data
|
|
|
|
* to memory area pointed by 'data'. The actual amount of received bytes is
|
|
|
|
* stored in 'nbytesptr'.
|
|
|
|
*
|
|
|
|
* \param addr USB device address.
|
|
|
|
* \param ep USB device endpoint number.
|
|
|
|
* \param nbytesptr Receive buffer size. It is set to the amount of received
|
|
|
|
* bytes when the function returns.
|
|
|
|
* \param data Buffer to store received data.
|
|
|
|
*
|
|
|
|
* \return 0 on success, error code otherwise.
|
2012-06-07 16:11:26 +02:00
|
|
|
*/
|
2012-06-12 14:43:52 +02:00
|
|
|
uint32_t USBHost::inTransfer(uint32_t addr, uint32_t ep, uint32_t *nbytesptr, uint8_t* data)
|
|
|
|
{
|
|
|
|
EpInfo *pep = NULL;
|
|
|
|
uint32_t nak_limit = 0;
|
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
uint32_t rcode = setPipeAddress(addr, ep, &pep, nak_limit);
|
2012-06-12 14:43:52 +02:00
|
|
|
|
|
|
|
if (rcode)
|
2012-06-26 16:20:26 +02:00
|
|
|
{
|
2012-06-12 14:43:52 +02:00
|
|
|
return rcode;
|
2012-06-26 16:20:26 +02:00
|
|
|
}
|
2012-06-12 14:43:52 +02:00
|
|
|
|
|
|
|
return InTransfer(pep, nak_limit, nbytesptr, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t USBHost::InTransfer(EpInfo *pep, uint32_t nak_limit, uint32_t *nbytesptr, uint8_t* data)
|
2012-04-06 17:18:55 +02:00
|
|
|
{
|
2012-06-07 16:11:26 +02:00
|
|
|
uint32_t rcode = 0;
|
|
|
|
uint32_t pktsize = 0;
|
2012-06-26 16:20:26 +02:00
|
|
|
uint32_t nbytes = *nbytesptr;
|
|
|
|
uint32_t maxpktsize = pep->maxPktSize;
|
2012-06-12 14:43:52 +02:00
|
|
|
|
|
|
|
*nbytesptr = 0;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
// Use a 'return' to exit this loop
|
|
|
|
// IN packet to EP-'endpoint'. Function takes care of NAKS.
|
2012-06-26 16:20:26 +02:00
|
|
|
rcode = dispatchPkt(tokIN, pep->hostPipeNum, nak_limit);
|
2012-06-07 16:11:26 +02:00
|
|
|
if (rcode)
|
|
|
|
{
|
2012-06-26 16:20:26 +02:00
|
|
|
if (rcode == 1)
|
|
|
|
{
|
|
|
|
// Pipe freeze is mandatory to avoid sending IN endlessly (else reception becomes messy then)
|
|
|
|
uhd_freeze_pipe(pep->hostPipeNum);
|
|
|
|
}
|
|
|
|
// Should be 1, indicating NAK. Else return error code.
|
|
|
|
return rcode;
|
2012-04-06 17:18:55 +02:00
|
|
|
}
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
// Number of received bytes
|
2012-06-26 16:20:26 +02:00
|
|
|
pktsize = uhd_byte_count(pep->hostPipeNum);
|
2012-06-07 16:11:26 +02:00
|
|
|
if (nbytes < pktsize)
|
2012-06-26 16:20:26 +02:00
|
|
|
{
|
|
|
|
TRACE_USBHOST(printf("/!\\ USBHost::InTransfer : receive buffer is too small, size=%lu, expected=%lu\r\n", nbytes, pktsize);)
|
|
|
|
}
|
|
|
|
data += UHD_Pipe_Read(pep->hostPipeNum, pktsize, data);
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
// Add this packet's byte count to total transfer length
|
2012-06-12 14:43:52 +02:00
|
|
|
*nbytesptr += pktsize;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
// The transfer is complete under two conditions:
|
|
|
|
// 1. The device sent a short packet (L.T. maxPacketSize)
|
|
|
|
// 2. 'nbytes' have been transferred.
|
2012-06-12 14:43:52 +02:00
|
|
|
if ((pktsize < maxpktsize) || (*nbytesptr >= nbytes))
|
2012-06-07 16:11:26 +02:00
|
|
|
{
|
2012-06-26 16:20:26 +02:00
|
|
|
return 0;
|
2012-04-06 17:18:55 +02:00
|
|
|
}
|
2012-06-07 16:11:26 +02:00
|
|
|
}
|
2012-04-06 17:18:55 +02:00
|
|
|
}
|
|
|
|
|
2012-06-07 16:11:26 +02:00
|
|
|
/**
|
2012-06-26 16:20:26 +02:00
|
|
|
* \brief Perform OUT request to the specified USB device.
|
|
|
|
*
|
|
|
|
* \note This function handles multiple packets (if necessary) and sends
|
|
|
|
* 'nbytes' bytes.
|
2012-06-07 16:11:26 +02:00
|
|
|
*
|
2012-06-26 16:20:26 +02:00
|
|
|
* \param addr USB device address.
|
|
|
|
* \param ep USB device endpoint number.
|
|
|
|
* \param nbytes Buffer size to be sent.
|
|
|
|
* \param data Buffer to send.
|
|
|
|
*
|
|
|
|
* \return 0 on success, error code otherwise.
|
2012-06-07 16:11:26 +02:00
|
|
|
*/
|
2012-06-12 14:43:52 +02:00
|
|
|
uint32_t USBHost::outTransfer(uint32_t addr, uint32_t ep, uint32_t nbytes, uint8_t* data)
|
2012-04-06 17:18:55 +02:00
|
|
|
{
|
2012-06-26 16:20:26 +02:00
|
|
|
EpInfo *pep = NULL;
|
|
|
|
uint32_t nak_limit = 0;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
uint32_t rcode = setPipeAddress(addr, ep, &pep, nak_limit);
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-12 14:43:52 +02:00
|
|
|
if (rcode)
|
2012-06-26 16:20:26 +02:00
|
|
|
{
|
2012-06-12 14:43:52 +02:00
|
|
|
return rcode;
|
2012-06-26 16:20:26 +02:00
|
|
|
}
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-12 14:43:52 +02:00
|
|
|
return OutTransfer(pep, nak_limit, nbytes, data);
|
|
|
|
}
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-12 14:43:52 +02:00
|
|
|
uint32_t USBHost::OutTransfer(EpInfo *pep, uint32_t nak_limit, uint32_t nbytes, uint8_t *data)
|
|
|
|
{
|
2012-06-26 16:20:26 +02:00
|
|
|
uint32_t rcode = 0;
|
|
|
|
uint32_t bytes_tosend = 0;
|
|
|
|
uint32_t bytes_left = nbytes;
|
|
|
|
uint32_t maxpktsize = pep->maxPktSize;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
if (maxpktsize < 1)
|
2012-06-12 14:43:52 +02:00
|
|
|
return USB_ERROR_INVALID_MAX_PKT_SIZE;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
while (bytes_left)
|
|
|
|
{
|
|
|
|
bytes_tosend = (bytes_left >= maxpktsize) ? maxpktsize : bytes_left;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
// Write FIFO
|
|
|
|
UHD_Pipe_Write(pep->hostPipeNum, bytes_tosend, data);
|
2012-06-12 14:43:52 +02:00
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
// Use a 'return' to exit this loop
|
|
|
|
// OUT packet to EP-'endpoint'. Function takes care of NAKS.
|
|
|
|
rcode = dispatchPkt(tokOUT, pep->hostPipeNum, nak_limit);
|
|
|
|
if (rcode)
|
2012-06-07 16:11:26 +02:00
|
|
|
{
|
2012-06-26 16:20:26 +02:00
|
|
|
// Should be 0, indicating ACK. Else return error code.
|
|
|
|
return rcode;
|
|
|
|
}
|
|
|
|
|
2012-06-07 16:11:26 +02:00
|
|
|
bytes_left -= bytes_tosend;
|
2012-06-26 16:20:26 +02:00
|
|
|
data += bytes_tosend;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be 0 in all cases
|
|
|
|
return rcode;
|
2012-04-06 17:18:55 +02:00
|
|
|
}
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
/**
|
2012-06-26 16:20:26 +02:00
|
|
|
* \brief Dispatch USB packet.
|
|
|
|
*
|
|
|
|
* \note Ensure peripheral address is set and relevant buffer is loaded/empty.
|
2012-06-07 16:11:26 +02:00
|
|
|
* If NAK, tries to re-send up to nak_limit times.
|
|
|
|
* If nak_limit == 0, do not count NAKs, exit after timeout.
|
2012-06-12 14:43:52 +02:00
|
|
|
*
|
2012-06-26 16:20:26 +02:00
|
|
|
* \param token Token type (Setup, In or Out).
|
|
|
|
* \param hostPipeNum Host pipe number to use for sending USB packet.
|
|
|
|
* \param nak_limit Maximum number of NAK permitted.
|
|
|
|
*
|
|
|
|
* \return 0 on success, error code otherwise.
|
2012-06-07 16:11:26 +02:00
|
|
|
*/
|
2012-06-26 16:20:26 +02:00
|
|
|
uint32_t USBHost::dispatchPkt(uint32_t token, uint32_t hostPipeNum, uint32_t nak_limit)
|
2012-04-06 17:18:55 +02:00
|
|
|
{
|
2012-06-07 16:11:26 +02:00
|
|
|
uint32_t timeout = millis() + USB_XFER_TIMEOUT;
|
|
|
|
uint32_t nak_count = 0;
|
2012-06-12 14:43:52 +02:00
|
|
|
uint32_t rcode = USB_ERROR_TRANSFER_TIMEOUT;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
TRACE_USBHOST(printf(" => dispatchPkt token=%lu pipe=%lu nak_limit=%lu\r\n", token, hostPipeNum, nak_limit);)
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
// Launch the transfer
|
2012-06-26 16:20:26 +02:00
|
|
|
UHD_Pipe_Send(hostPipeNum, token);
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
// Check timeout but don't hold timeout if VBUS is lost
|
|
|
|
while ((timeout > millis()) && (UHD_GetVBUSState() == UHD_STATE_CONNECTED))
|
2012-06-07 16:11:26 +02:00
|
|
|
{
|
|
|
|
// Wait for transfer completion
|
2012-06-26 16:20:26 +02:00
|
|
|
if (UHD_Pipe_Is_Transfer_Complete(hostPipeNum, token))
|
2012-06-07 16:11:26 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
// Is NAK received?
|
|
|
|
if (Is_uhd_nak_received(hostPipeNum))
|
2012-06-07 16:11:26 +02:00
|
|
|
{
|
2012-06-26 16:20:26 +02:00
|
|
|
uhd_ack_nak_received(hostPipeNum);
|
2012-06-07 16:11:26 +02:00
|
|
|
nak_count++;
|
2012-06-26 16:20:26 +02:00
|
|
|
|
2012-06-07 16:11:26 +02:00
|
|
|
if (nak_limit && (nak_count == nak_limit))
|
|
|
|
{
|
2012-06-26 16:20:26 +02:00
|
|
|
// Return NAK
|
|
|
|
return 1;
|
2012-06-07 16:11:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-12 14:43:52 +02:00
|
|
|
return rcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-06-26 16:20:26 +02:00
|
|
|
* \brief Configure device using known device classes.
|
2012-06-12 14:43:52 +02:00
|
|
|
* The device get a new address even if its class remain unknown.
|
2012-06-26 16:20:26 +02:00
|
|
|
*
|
|
|
|
* \param parent USB device address of the device's parent (0 if root).
|
|
|
|
* \param port USB device base address (see AddressPoolImpl).
|
|
|
|
* \param lowspeed Device speed.
|
|
|
|
*
|
|
|
|
* \return 0 on success, error code otherwise.
|
2012-06-12 14:43:52 +02:00
|
|
|
*/
|
|
|
|
uint32_t USBHost::Configuring(uint32_t parent, uint32_t port, uint32_t lowspeed)
|
|
|
|
{
|
|
|
|
uint32_t rcode = 0;
|
|
|
|
|
|
|
|
for (; devConfigIndex < USB_NUMDEVICES; ++devConfigIndex)
|
|
|
|
{
|
|
|
|
if (!devConfig[devConfigIndex])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
rcode = devConfig[devConfigIndex]->Init(parent, port, lowspeed);
|
|
|
|
|
|
|
|
if (!rcode)
|
|
|
|
{
|
|
|
|
TRACE_USBHOST(printf("USBHost::Configuring : found device class!\r\n");)
|
|
|
|
devConfigIndex = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
|
|
|
|
if (rcode == USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED)
|
|
|
|
{
|
|
|
|
TRACE_USBHOST(printf("USBHost::Configuring : ERROR : device not supported!\r\n");)
|
|
|
|
}
|
|
|
|
else if (rcode == USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE)
|
|
|
|
{
|
|
|
|
TRACE_USBHOST(printf("USBHost::Configuring : ERROR : class instance already in use!\r\n");)
|
|
|
|
}
|
|
|
|
else
|
2012-06-12 14:43:52 +02:00
|
|
|
{
|
|
|
|
// in case of an error devConfigIndex should be reset to 0
|
2012-06-26 16:20:26 +02:00
|
|
|
// in order to start from the very beginning the next time
|
|
|
|
// the program gets here
|
2012-06-12 14:43:52 +02:00
|
|
|
if (rcode != USB_DEV_CONFIG_ERROR_DEVICE_INIT_INCOMPLETE)
|
|
|
|
devConfigIndex = 0;
|
|
|
|
|
|
|
|
return rcode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
// Device class is not supported by any of the registered classes
|
2012-06-12 14:43:52 +02:00
|
|
|
devConfigIndex = 0;
|
|
|
|
|
|
|
|
rcode = DefaultAddressing(parent, port, lowspeed);
|
|
|
|
|
|
|
|
return rcode;
|
|
|
|
}
|
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
/**
|
|
|
|
* \brief Configure device with unknown USB class.
|
|
|
|
*
|
|
|
|
* \param parent USB device address of the device's parent (0 if root).
|
|
|
|
* \param port USB device base address (see AddressPoolImpl).
|
|
|
|
* \param lowspeed Device speed.
|
|
|
|
*
|
|
|
|
* \return 0 on success, error code otherwise.
|
|
|
|
*/
|
2012-06-12 14:43:52 +02:00
|
|
|
uint32_t USBHost::DefaultAddressing(uint32_t parent, uint32_t port, uint32_t lowspeed)
|
|
|
|
{
|
|
|
|
uint32_t rcode = 0;
|
|
|
|
UsbDevice *p0 = 0, *p = 0;
|
|
|
|
|
|
|
|
// Get pointer to pseudo device with address 0 assigned
|
|
|
|
p0 = addrPool.GetUsbDevicePtr(0);
|
|
|
|
|
|
|
|
if (!p0)
|
|
|
|
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
|
|
|
|
|
|
|
|
if (!p0->epinfo)
|
|
|
|
return USB_ERROR_EPINFO_IS_NULL;
|
|
|
|
|
|
|
|
p0->lowspeed = (lowspeed) ? 1 : 0;
|
|
|
|
|
|
|
|
// Allocate new address according to device class
|
|
|
|
uint32_t bAddress = addrPool.AllocAddress(parent, 0, port);
|
|
|
|
|
|
|
|
if (!bAddress)
|
|
|
|
return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
|
|
|
|
|
|
|
|
p = addrPool.GetUsbDevicePtr(bAddress);
|
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
|
|
|
|
|
|
|
|
p->lowspeed = lowspeed;
|
|
|
|
|
|
|
|
// Assign new address to the device
|
|
|
|
rcode = setAddr(0, 0, bAddress);
|
|
|
|
|
|
|
|
if (rcode)
|
|
|
|
{
|
|
|
|
TRACE_USBHOST(printf("/!\\ USBHost::DefaultAddressing : Set address failed with code: %lu\r\n", rcode);)
|
|
|
|
addrPool.FreeAddress(bAddress);
|
|
|
|
bAddress = 0;
|
|
|
|
return rcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
/**
|
|
|
|
* \brief Release device and free associated resources.
|
|
|
|
*
|
|
|
|
* \param addr USB device address.
|
|
|
|
*
|
|
|
|
* \return 0 on success, error code otherwise.
|
|
|
|
*/
|
2012-06-12 14:43:52 +02:00
|
|
|
uint32_t USBHost::ReleaseDevice(uint32_t addr)
|
|
|
|
{
|
|
|
|
if (!addr)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < USB_NUMDEVICES; ++i)
|
2012-06-26 16:20:26 +02:00
|
|
|
{
|
2012-06-12 14:43:52 +02:00
|
|
|
if (devConfig[i]->GetAddress() == addr)
|
2012-06-26 16:20:26 +02:00
|
|
|
{
|
2012-06-12 14:43:52 +02:00
|
|
|
return devConfig[i]->Release();
|
2012-06-26 16:20:26 +02:00
|
|
|
}
|
|
|
|
}
|
2012-06-12 14:43:52 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
/**
|
|
|
|
* \brief Get device descriptor.
|
|
|
|
*
|
|
|
|
* \param addr USB device address.
|
|
|
|
* \param ep USB device endpoint number.
|
|
|
|
* \param nbytes Buffer size.
|
|
|
|
* \param dataptr Buffer to store received descriptor.
|
|
|
|
*
|
|
|
|
* \return 0 on success, error code otherwise.
|
|
|
|
*/
|
2012-06-12 14:43:52 +02:00
|
|
|
uint32_t USBHost::getDevDescr(uint32_t addr, uint32_t ep, uint32_t nbytes, uint8_t* dataptr)
|
|
|
|
{
|
|
|
|
return (ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, 0x00, USB_DESCRIPTOR_DEVICE, 0x0000, nbytes, nbytes, dataptr, 0));
|
|
|
|
}
|
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
/**
|
|
|
|
* \brief Get configuration descriptor.
|
|
|
|
*
|
|
|
|
* \param addr USB device address.
|
|
|
|
* \param ep USB device endpoint number.
|
|
|
|
* \param nbytes Buffer size.
|
|
|
|
* \param conf Configuration number.
|
|
|
|
* \param dataptr Buffer to store received descriptor.
|
|
|
|
*
|
|
|
|
* \return 0 on success, error code otherwise.
|
|
|
|
*/
|
2012-06-12 14:43:52 +02:00
|
|
|
uint32_t USBHost::getConfDescr(uint32_t addr, uint32_t ep, uint32_t nbytes, uint32_t conf, uint8_t* dataptr)
|
|
|
|
{
|
|
|
|
return (ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, nbytes, nbytes, dataptr, 0));
|
|
|
|
}
|
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
/**
|
|
|
|
* \brief Get configuration descriptor and extract endpoints using USBReadParser object.
|
|
|
|
*
|
|
|
|
* \param addr USB device address.
|
|
|
|
* \param ep USB device endpoint number.
|
|
|
|
* \param conf Configuration number.
|
|
|
|
* \param p USBReadParser object pointer used to extract endpoints.
|
|
|
|
*
|
|
|
|
* \return 0 on success, error code otherwise.
|
|
|
|
*/
|
2012-06-12 14:43:52 +02:00
|
|
|
uint32_t USBHost::getConfDescr(uint32_t addr, uint32_t ep, uint32_t conf, USBReadParser *p)
|
|
|
|
{
|
|
|
|
const uint32_t bufSize = 64;
|
|
|
|
uint8_t buf[bufSize];
|
|
|
|
|
|
|
|
uint32_t ret = getConfDescr(addr, ep, 8, conf, buf);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
uint32_t total = ((USB_CONFIGURATION_DESCRIPTOR*)buf)->wTotalLength;
|
|
|
|
delay(100);
|
|
|
|
|
|
|
|
return (ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, total, bufSize, buf, p));
|
|
|
|
}
|
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
/**
|
|
|
|
* \brief Get string descriptor.
|
|
|
|
*
|
|
|
|
* \param addr USB device address.
|
|
|
|
* \param ep USB device endpoint number.
|
|
|
|
* \param nbytes Buffer size.
|
|
|
|
* \param index String index.
|
|
|
|
* \param langid Language ID.
|
|
|
|
* \param dataptr Buffer to store received descriptor.
|
|
|
|
*
|
|
|
|
* \return 0 on success, error code otherwise.
|
|
|
|
*/
|
|
|
|
uint32_t USBHost::getStrDescr(uint32_t addr, uint32_t ep, uint32_t nbytes, uint8_t index, uint16_t langid, uint8_t* dataptr)
|
2012-06-12 14:43:52 +02:00
|
|
|
{
|
2012-06-26 16:20:26 +02:00
|
|
|
return (ctrlReq(addr, ep, bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, index, USB_DESCRIPTOR_STRING, langid, nbytes, nbytes, dataptr, 0));
|
2012-06-12 14:43:52 +02:00
|
|
|
}
|
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
/**
|
|
|
|
* \brief Set USB device address.
|
|
|
|
*
|
|
|
|
* \param oldaddr Current USB device address.
|
|
|
|
* \param ep USB device endpoint number.
|
|
|
|
* \param addr New USB device address to be set.
|
|
|
|
*
|
|
|
|
* \return 0 on success, error code otherwise.
|
|
|
|
*/
|
2012-06-12 14:43:52 +02:00
|
|
|
uint32_t USBHost::setAddr(uint32_t oldaddr, uint32_t ep, uint32_t newaddr)
|
|
|
|
{
|
2012-06-26 16:20:26 +02:00
|
|
|
TRACE_USBHOST(printf(" => USBHost::setAddr\r\n");)
|
2012-06-12 14:43:52 +02:00
|
|
|
return ctrlReq(oldaddr, ep, bmREQ_SET, USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000, 0x0000, 0, 0);
|
|
|
|
}
|
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
/**
|
|
|
|
* \brief Set configuration.
|
|
|
|
*
|
|
|
|
* \param addr USB device address.
|
|
|
|
* \param ep USB device endpoint number.
|
|
|
|
* \param conf_value New configuration value to be set.
|
|
|
|
*
|
|
|
|
* \return 0 on success, error code otherwise.
|
|
|
|
*/
|
2012-06-12 14:43:52 +02:00
|
|
|
uint32_t USBHost::setConf(uint32_t addr, uint32_t ep, uint32_t conf_value)
|
|
|
|
{
|
|
|
|
return (ctrlReq(addr, ep, bmREQ_SET, USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000, 0x0000, 0, 0));
|
2012-04-06 17:18:55 +02:00
|
|
|
}
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
/**
|
2012-06-26 16:20:26 +02:00
|
|
|
* \brief USB main task, responsible for enumeration and clean up stage.
|
|
|
|
*
|
|
|
|
* \note Must be periodically called from loop().
|
2012-06-07 16:11:26 +02:00
|
|
|
*/
|
|
|
|
void USBHost::Task(void)
|
2012-04-06 17:18:55 +02:00
|
|
|
{
|
2012-06-07 16:11:26 +02:00
|
|
|
uint32_t rcode = 0;
|
|
|
|
volatile uint32_t tmpdata = 0;
|
|
|
|
static uint32_t delay = 0;
|
2012-06-12 14:43:52 +02:00
|
|
|
uint32_t lowspeed = 0;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
// Update USB task state on Vbus change
|
|
|
|
tmpdata = UHD_GetVBUSState();
|
|
|
|
switch (tmpdata)
|
|
|
|
{
|
|
|
|
case UHD_STATE_ERROR:
|
|
|
|
// Illegal state
|
2012-04-06 17:18:55 +02:00
|
|
|
usb_task_state = USB_DETACHED_SUBSTATE_ILLEGAL;
|
2012-06-12 14:43:52 +02:00
|
|
|
lowspeed = 0;
|
2012-04-06 17:18:55 +02:00
|
|
|
break;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
case UHD_STATE_DISCONNECTED:
|
|
|
|
// Disconnected state
|
|
|
|
if ((usb_task_state & USB_STATE_MASK) != USB_STATE_DETACHED)
|
|
|
|
{
|
2012-04-06 17:18:55 +02:00
|
|
|
usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE;
|
2012-06-12 14:43:52 +02:00
|
|
|
lowspeed = 0;
|
2012-04-06 17:18:55 +02:00
|
|
|
}
|
|
|
|
break;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
case UHD_STATE_CONNECTED:
|
|
|
|
// Attached state
|
|
|
|
if ((usb_task_state & USB_STATE_MASK) == USB_STATE_DETACHED)
|
|
|
|
{
|
2012-04-06 17:18:55 +02:00
|
|
|
delay = millis() + USB_SETTLE_DELAY;
|
|
|
|
usb_task_state = USB_ATTACHED_SUBSTATE_SETTLE;
|
2012-06-26 16:20:26 +02:00
|
|
|
//FIXME TODO: lowspeed = 0 ou 1; already done by hardware?
|
2012-04-06 17:18:55 +02:00
|
|
|
}
|
|
|
|
break;
|
2012-06-07 16:11:26 +02:00
|
|
|
}
|
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
// Poll connected devices (if required)
|
2012-06-12 14:43:52 +02:00
|
|
|
for (uint32_t i = 0; i < USB_NUMDEVICES; ++i)
|
|
|
|
if (devConfig[i])
|
|
|
|
rcode = devConfig[i]->Poll();
|
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
// Perform USB enumeration stage and clean up
|
2012-06-07 16:11:26 +02:00
|
|
|
switch (usb_task_state)
|
|
|
|
{
|
2012-04-06 17:18:55 +02:00
|
|
|
case USB_DETACHED_SUBSTATE_INITIALIZE:
|
2012-06-07 16:11:26 +02:00
|
|
|
TRACE_USBHOST(printf(" + USB_DETACHED_SUBSTATE_INITIALIZE\r\n");)
|
|
|
|
|
|
|
|
// Init USB stack and driver
|
|
|
|
UHD_Init();
|
2012-04-06 17:18:55 +02:00
|
|
|
init();
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-26 16:20:26 +02:00
|
|
|
// Free all USB resources
|
2012-06-12 14:43:52 +02:00
|
|
|
for (uint32_t i = 0; i < USB_NUMDEVICES; ++i)
|
|
|
|
if (devConfig[i])
|
|
|
|
rcode = devConfig[i]->Release();
|
|
|
|
|
2012-04-06 17:18:55 +02:00
|
|
|
usb_task_state = USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE;
|
|
|
|
break;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
case USB_DETACHED_SUBSTATE_WAIT_FOR_DEVICE:
|
|
|
|
// Nothing to do
|
2012-04-06 17:18:55 +02:00
|
|
|
break;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
case USB_DETACHED_SUBSTATE_ILLEGAL:
|
|
|
|
// Nothing to do
|
2012-04-06 17:18:55 +02:00
|
|
|
break;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
case USB_ATTACHED_SUBSTATE_SETTLE:
|
|
|
|
// Settle time for just attached device
|
|
|
|
if (delay < millis())
|
|
|
|
{
|
|
|
|
TRACE_USBHOST(printf(" + USB_ATTACHED_SUBSTATE_SETTLE\r\n");)
|
2012-04-06 17:18:55 +02:00
|
|
|
usb_task_state = USB_ATTACHED_SUBSTATE_RESET_DEVICE;
|
|
|
|
}
|
|
|
|
break;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-04-06 17:18:55 +02:00
|
|
|
case USB_ATTACHED_SUBSTATE_RESET_DEVICE:
|
2012-06-07 16:11:26 +02:00
|
|
|
TRACE_USBHOST(printf(" + USB_ATTACHED_SUBSTATE_RESET_DEVICE\r\n");)
|
|
|
|
|
|
|
|
// Trigger Bus Reset
|
|
|
|
UHD_BusReset();
|
2012-04-06 17:18:55 +02:00
|
|
|
usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE;
|
|
|
|
break;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-04-06 17:18:55 +02:00
|
|
|
case USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE:
|
2012-06-07 16:11:26 +02:00
|
|
|
if (Is_uhd_reset_sent())
|
|
|
|
{
|
|
|
|
TRACE_USBHOST(printf(" + USB_ATTACHED_SUBSTATE_WAIT_RESET_COMPLETE\r\n");)
|
|
|
|
|
|
|
|
// Clear Bus Reset flag
|
|
|
|
uhd_ack_reset_sent();
|
|
|
|
|
|
|
|
// Enable Start Of Frame generation
|
|
|
|
uhd_enable_sof();
|
|
|
|
|
2012-04-06 17:18:55 +02:00
|
|
|
usb_task_state = USB_ATTACHED_SUBSTATE_WAIT_SOF;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
// Wait 20ms after Bus Reset (USB spec)
|
|
|
|
delay = millis() + 20;
|
2012-04-06 17:18:55 +02:00
|
|
|
}
|
|
|
|
break;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
|
|
|
case USB_ATTACHED_SUBSTATE_WAIT_SOF:
|
|
|
|
// Wait for SOF received first
|
|
|
|
if (Is_uhd_sof())
|
|
|
|
{
|
|
|
|
if (delay < millis())
|
|
|
|
{
|
|
|
|
TRACE_USBHOST(printf(" + USB_ATTACHED_SUBSTATE_WAIT_SOF\r\n");)
|
|
|
|
|
|
|
|
// 20ms waiting elapsed
|
2012-06-12 14:43:52 +02:00
|
|
|
usb_task_state = USB_STATE_CONFIGURING;
|
2012-06-07 16:11:26 +02:00
|
|
|
}
|
2012-04-06 17:18:55 +02:00
|
|
|
}
|
|
|
|
break;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-12 14:43:52 +02:00
|
|
|
case USB_STATE_CONFIGURING:
|
|
|
|
TRACE_USBHOST(printf(" + USB_STATE_CONFIGURING\r\n");)
|
|
|
|
rcode = Configuring(0, 0, lowspeed);
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-06-12 14:43:52 +02:00
|
|
|
if (rcode)
|
2012-06-07 16:11:26 +02:00
|
|
|
{
|
2012-06-12 14:43:52 +02:00
|
|
|
TRACE_USBHOST(printf("/!\\ USBHost::Task : USB_STATE_CONFIGURING failed with code: %lu\r\n", rcode);)
|
|
|
|
if (rcode != USB_DEV_CONFIG_ERROR_DEVICE_INIT_INCOMPLETE)
|
2012-06-07 16:11:26 +02:00
|
|
|
{
|
2012-06-12 14:43:52 +02:00
|
|
|
usb_error = rcode;
|
2012-06-07 16:11:26 +02:00
|
|
|
usb_task_state = USB_STATE_ERROR;
|
|
|
|
}
|
2012-06-12 14:43:52 +02:00
|
|
|
}
|
|
|
|
else
|
2012-06-07 16:11:26 +02:00
|
|
|
{
|
2012-06-12 14:43:52 +02:00
|
|
|
usb_task_state = USB_STATE_RUNNING;
|
|
|
|
TRACE_USBHOST(printf(" + USB_STATE_RUNNING\r\n");)
|
|
|
|
}
|
2012-04-06 17:18:55 +02:00
|
|
|
break;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-04-06 17:18:55 +02:00
|
|
|
case USB_STATE_RUNNING:
|
|
|
|
break;
|
2012-06-07 16:11:26 +02:00
|
|
|
|
2012-04-06 17:18:55 +02:00
|
|
|
case USB_STATE_ERROR:
|
|
|
|
break;
|
2012-06-07 16:11:26 +02:00
|
|
|
}
|
|
|
|
}
|