| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | /****************************************************************************** |
| 2 | |||
| 3 | USB Host Driver |
||
| 4 | |||
| 5 | This file provides the hardware interface for a USB Embedded Host application. |
||
| 6 | Most applications will not make direct use of the functions in this file. |
||
| 7 | Instead, one or more client driver files should also be included in the project |
||
| 8 | to support the devices that will be attached to the host. Application |
||
| 9 | interface will be through the client drivers. |
||
| 10 | |||
| 11 | Note: USB interrupts are cleared by writing a "1" to the interrupt flag. This |
||
| 12 | means that read-modify-write instructions cannot be used to clear the flag. A |
||
| 13 | bit manipulation instruction, such as "U1OTGIRbits.T1MSECIF = 1;" will read the |
||
| 14 | value of the U1OTGIR register, set the T1MSECIF bit in that value to "1", and |
||
| 15 | then write that value back to U1OTGIR. If U1OTGIR had any other flags set, |
||
| 16 | those flags are written back as "1", which will clear those flags. To avoid |
||
| 17 | this issue, a constant value must be written to U1OTGIR where only the interrupt |
||
| 18 | flag in question is set, such as "U1OTGIR = USB_INTERRUPT_T1MSECIF;", where |
||
| 19 | USB_INTERRUPT_T1MSECIF equals 0x40. |
||
| 20 | |||
| 21 | *******************************************************************************/ |
||
| 22 | //DOM-IGNORE-BEGIN |
||
| 23 | /****************************************************************************** |
||
| 24 | |||
| 25 | File Name: usb_host.c |
||
| 26 | Dependencies: None |
||
| 27 | Processor: PIC24F/PIC32MX |
||
| 28 | Compiler: C30/C32 |
||
| 29 | Company: Microchip Technology, Inc. |
||
| 30 | |||
| 31 | Software License Agreement |
||
| 32 | |||
| 33 | The software supplied herewith by Microchip Technology Incorporated |
||
| 34 | (the Company) for its PICmicro® Microcontroller is intended and |
||
| 35 | supplied to you, the Companys customer, for use solely and |
||
| 36 | exclusively on Microchip PICmicro Microcontroller products. The |
||
| 37 | software is owned by the Company and/or its supplier, and is |
||
| 38 | protected under applicable copyright laws. All rights are reserved. |
||
| 39 | Any use in violation of the foregoing restrictions may subject the |
||
| 40 | user to criminal sanctions under applicable laws, as well as to |
||
| 41 | civil liability for the breach of the terms and conditions of this |
||
| 42 | license. |
||
| 43 | |||
| 44 | THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES, |
||
| 45 | WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED |
||
| 46 | TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
||
| 47 | PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, |
||
| 48 | IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR |
||
| 49 | CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
||
| 50 | |||
| 51 | Change History: |
||
| 52 | Rev Description |
||
| 53 | ---------- ---------------------------------------------------------- |
||
| 54 | 2.6 - 2.6a No change |
||
| 55 | |||
| 56 | 2.7 Fixed an error where the USBHostClearEndpointErrors() function |
||
| 57 | didn't properly return USB_SUCCESS if the errors were successfully |
||
| 58 | cleared. |
||
| 59 | http://www.microchip.com/forums/fb.aspx?m=490651 |
||
| 60 | |||
| 61 | Fixed an error where the DTS bits for the attached device could |
||
| 62 | be accidentally reset on a class specific request with the same |
||
| 63 | bRequest and wValue as a HALT_ENDPOINT request. |
||
| 64 | |||
| 65 | Fixed an error where device may never be able to enumerate if it |
||
| 66 | is already attached when the host stack initializes. |
||
| 67 | |||
| 68 | *******************************************************************************/ |
||
| 69 | |||
| 70 | #include <stdlib.h> |
||
| 71 | #include <string.h> |
||
| 72 | #include "GenericTypeDefs.h" |
||
| 73 | #include "USB\usb.h" |
||
| 74 | #include "usb_host_local.h" |
||
| 75 | #include "usb_hal_local.h" |
||
| 76 | #include "HardwareProfile.h" |
||
| 77 | //#include "USB\usb_hal.h" |
||
| 78 | |||
| 79 | #ifndef USB_MALLOC |
||
| 80 | #define USB_MALLOC(size) malloc(size) |
||
| 81 | #endif |
||
| 82 | |||
| 83 | #ifndef USB_FREE |
||
| 84 | #define USB_FREE(ptr) free(ptr) |
||
| 85 | #endif |
||
| 86 | |||
| 87 | #define USB_FREE_AND_CLEAR(ptr) {USB_FREE(ptr); ptr = NULL;} |
||
| 88 | |||
| 89 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 90 | #include "struct_queue.h" |
||
| 91 | #endif |
||
| 92 | |||
| 93 | // ***************************************************************************** |
||
| 94 | // Low Level Functionality Configurations. |
||
| 95 | |||
| 96 | //#define DEBUG_MODE |
||
| 97 | #ifdef DEBUG_MODE |
||
| 98 | #include "uart2.h" |
||
| 99 | #endif |
||
| 100 | |||
| 101 | // If the TPL includes an entry specifying a VID of 0xFFFF and a PID of 0xFFFF, |
||
| 102 | // the specified client driver will be used for any device that attaches. This |
||
| 103 | // can be useful for debugging or for providing generic charging functionality. |
||
| 104 | #define ALLOW_GLOBAL_VID_AND_PID |
||
| 105 | |||
| 106 | // If we allow multiple control transactions during a frame and a NAK is |
||
| 107 | // generated, we don't get TRNIF. So we will allow only one control transaction |
||
| 108 | // per frame. |
||
| 109 | #define ONE_CONTROL_TRANSACTION_PER_FRAME |
||
| 110 | |||
| 111 | // This definition allow Bulk transfers to take all of the remaining bandwidth |
||
| 112 | // of a frame. |
||
| 113 | #define ALLOW_MULTIPLE_BULK_TRANSACTIONS_PER_FRAME |
||
| 114 | |||
| 115 | // If this is defined, then we will repeat a NAK'd request in the same frame. |
||
| 116 | // Otherwise, we will wait until the next frame to repeat the request. Some |
||
| 117 | // mass storage devices require the host to wait until the next frame to |
||
| 118 | // repeat the request. |
||
| 119 | //#define ALLOW_MULTIPLE_NAKS_PER_FRAME |
||
| 120 | |||
| 121 | //#define USE_MANUAL_DETACH_DETECT |
||
| 122 | |||
| 123 | // The USB specification states that transactions should be tried three times |
||
| 124 | // if there is a bus error. We will allow that number to be configurable. The |
||
| 125 | // maximum value is 31. |
||
| 126 | #define USB_TRANSACTION_RETRY_ATTEMPTS 20 |
||
| 127 | |||
| 128 | //****************************************************************************** |
||
| 129 | //****************************************************************************** |
||
| 130 | // Section: Host Global Variables |
||
| 131 | //****************************************************************************** |
||
| 132 | //****************************************************************************** |
||
| 133 | |||
| 134 | // When using the PIC32, ping pong mode must be set to FULL. |
||
| 135 | #if defined (__PIC32MX__) |
||
| 136 | #if (USB_PING_PONG_MODE != USB_PING_PONG__FULL_PING_PONG) |
||
| 137 | #undef USB_PING_PONG_MODE |
||
| 138 | #define USB_PING_PONG_MODE USB_PING_PONG__FULL_PING_PONG |
||
| 139 | #endif |
||
| 140 | #endif |
||
| 141 | |||
| 142 | #if (USB_PING_PONG_MODE == USB_PING_PONG__NO_PING_PONG) || (USB_PING_PONG_MODE == USB_PING_PONG__ALL_BUT_EP0) |
||
| 143 | #if !defined(USB_SUPPORT_OTG) && !defined(USB_SUPPORT_DEVICE) |
||
| 144 | static BDT_ENTRY __attribute__ ((aligned(512))) BDT[2]; |
||
| 145 | #endif |
||
| 146 | #define BDT_IN (&BDT[0]) // EP0 IN Buffer Descriptor |
||
| 147 | #define BDT_OUT (&BDT[1]) // EP0 OUT Buffer Descriptor |
||
| 148 | #elif (USB_PING_PONG_MODE == USB_PING_PONG__EP0_OUT_ONLY) |
||
| 149 | #if !defined(USB_SUPPORT_OTG) && !defined(USB_SUPPORT_DEVICE) |
||
| 150 | static BDT_ENTRY __attribute__ ((aligned(512))) BDT[3]; |
||
| 151 | #endif |
||
| 152 | #define BDT_IN (&BDT[0]) // EP0 IN Buffer Descriptor |
||
| 153 | #define BDT_OUT (&BDT[1]) // EP0 OUT Even Buffer Descriptor |
||
| 154 | #define BDT_OUT_ODD (&BDT[2]) // EP0 OUT Odd Buffer Descriptor |
||
| 155 | #elif (USB_PING_PONG_MODE == USB_PING_PONG__FULL_PING_PONG) |
||
| 156 | #if !defined(USB_SUPPORT_OTG) && !defined(USB_SUPPORT_DEVICE) |
||
| 157 | static BDT_ENTRY __attribute__ ((aligned(512))) BDT[4]; |
||
| 158 | #endif |
||
| 159 | #define BDT_IN (&BDT[0]) // EP0 IN Even Buffer Descriptor |
||
| 160 | #define BDT_IN_ODD (&BDT[1]) // EP0 IN Odd Buffer Descriptor |
||
| 161 | #define BDT_OUT (&BDT[2]) // EP0 OUT Even Buffer Descriptor |
||
| 162 | #define BDT_OUT_ODD (&BDT[3]) // EP0 OUT Odd Buffer Descriptor |
||
| 163 | #endif |
||
| 164 | |||
| 165 | #if defined(USB_SUPPORT_OTG) || defined(USB_SUPPORT_DEVICE) |
||
| 166 | extern BDT_ENTRY BDT[] __attribute__ ((aligned (512))); |
||
| 167 | #endif |
||
| 168 | |||
| 169 | // These should all be moved into the USB_DEVICE_INFO structure. |
||
| 170 | static BYTE countConfigurations; // Count the Configuration Descriptors read during enumeration. |
||
| 171 | static BYTE numCommandTries; // The number of times the current command has been tried. |
||
| 172 | static BYTE numEnumerationTries; // The number of times enumeration has been attempted on the attached device. |
||
| 173 | static volatile WORD numTimerInterrupts; // The number of milliseconds elapsed during the current waiting period. |
||
| 174 | static volatile USB_ENDPOINT_INFO *pCurrentEndpoint; // Pointer to the endpoint currently performing a transfer. |
||
| 175 | BYTE *pCurrentConfigurationDescriptor = NULL; // Pointer to the current configuration descriptor of the attached device. |
||
| 176 | BYTE *pDeviceDescriptor = NULL; // Pointer to the Device Descriptor of the attached device. |
||
| 177 | static BYTE *pEP0Data = NULL; // A data buffer for use by EP0. |
||
| 178 | static volatile WORD usbHostState; // State machine state of the attached device. |
||
| 179 | volatile WORD usbOverrideHostState; // Next state machine state, when set by interrupt processing. |
||
| 180 | #ifdef ENABLE_STATE_TRACE // Debug trace support |
||
| 181 | static WORD prevHostState; |
||
| 182 | #endif |
||
| 183 | |||
| 184 | |||
| 185 | static USB_BUS_INFO usbBusInfo; // Information about the USB bus. |
||
| 186 | static USB_DEVICE_INFO usbDeviceInfo; // A collection of information about the attached device. |
||
| 187 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 188 | static USB_EVENT_QUEUE usbEventQueue; // Queue of USB events used to synchronize ISR to main tasks loop. |
||
| 189 | #endif |
||
| 190 | static USB_ROOT_HUB_INFO usbRootHubInfo; // Information about a specific port. |
||
| 191 | |||
| 192 | |||
| 193 | |||
| 194 | // ***************************************************************************** |
||
| 195 | // ***************************************************************************** |
||
| 196 | // Section: Application Callable Functions |
||
| 197 | // ***************************************************************************** |
||
| 198 | // ***************************************************************************** |
||
| 199 | |||
| 200 | /**************************************************************************** |
||
| 201 | Function: |
||
| 202 | BYTE USBHostClearEndpointErrors( BYTE deviceAddress, BYTE endpoint ) |
||
| 203 | |||
| 204 | Summary: |
||
| 205 | This function clears an endpoint's internal error condition. |
||
| 206 | |||
| 207 | Description: |
||
| 208 | This function is called to clear the internal error condition of a device's |
||
| 209 | endpoint. It should be called after the application has dealt with the |
||
| 210 | error condition on the device. This routine clears internal status only; |
||
| 211 | it does not interact with the device. |
||
| 212 | |||
| 213 | Precondition: |
||
| 214 | None |
||
| 215 | |||
| 216 | Parameters: |
||
| 217 | BYTE deviceAddress - Address of device |
||
| 218 | BYTE endpoint - Endpoint to clear error condition |
||
| 219 | |||
| 220 | Return Values: |
||
| 221 | USB_SUCCESS - Errors cleared |
||
| 222 | USB_UNKNOWN_DEVICE - Device not found |
||
| 223 | USB_ENDPOINT_NOT_FOUND - Specified endpoint not found |
||
| 224 | |||
| 225 | Remarks: |
||
| 226 | None |
||
| 227 | ***************************************************************************/ |
||
| 228 | |||
| 229 | BYTE USBHostClearEndpointErrors( BYTE deviceAddress, BYTE endpoint ) |
||
| 230 | { |
||
| 231 | USB_ENDPOINT_INFO *ep; |
||
| 232 | |||
| 233 | // Find the required device |
||
| 234 | if (deviceAddress != usbDeviceInfo.deviceAddress) |
||
| 235 | { |
||
| 236 | return USB_UNKNOWN_DEVICE; |
||
| 237 | } |
||
| 238 | |||
| 239 | ep = _USB_FindEndpoint( endpoint ); |
||
| 240 | |||
| 241 | if (ep != NULL) |
||
| 242 | { |
||
| 243 | ep->status.bfStalled = 0; |
||
| 244 | ep->status.bfError = 0; |
||
| 245 | |||
| 246 | return USB_SUCCESS; |
||
| 247 | } |
||
| 248 | return USB_ENDPOINT_NOT_FOUND; |
||
| 249 | } |
||
| 250 | |||
| 251 | |||
| 252 | /**************************************************************************** |
||
| 253 | Function: |
||
| 254 | BOOL USBHostDeviceSpecificClientDriver( BYTE deviceAddress ) |
||
| 255 | |||
| 256 | Summary: |
||
| 257 | This function indicates if the specified device has explicit client |
||
| 258 | driver support specified in the TPL. |
||
| 259 | |||
| 260 | Description: |
||
| 261 | This function indicates if the specified device has explicit client |
||
| 262 | driver support specified in the TPL. It is used in client drivers' |
||
| 263 | USB_CLIENT_INIT routines to indicate that the client driver should be |
||
| 264 | used even though the class, subclass, and protocol values may not match |
||
| 265 | those normally required by the class. For example, some printing devices |
||
| 266 | do not fulfill all of the requirements of the printer class, so their |
||
| 267 | class, subclass, and protocol fields indicate a custom driver rather than |
||
| 268 | the printer class. But the printer class driver can still be used, with |
||
| 269 | minor limitations. |
||
| 270 | |||
| 271 | Precondition: |
||
| 272 | None |
||
| 273 | |||
| 274 | Parameters: |
||
| 275 | BYTE deviceAddress - Address of device |
||
| 276 | |||
| 277 | Return Values: |
||
| 278 | TRUE - This device is listed in the TPL by VID andPID, and has explicit |
||
| 279 | client driver support. |
||
| 280 | FALSE - This device is not listed in the TPL by VID and PID. |
||
| 281 | |||
| 282 | Remarks: |
||
| 283 | This function is used so client drivers can allow certain |
||
| 284 | devices to enumerate. For example, some printer devices indicate a custom |
||
| 285 | class rather than the printer class, even though the device has only minor |
||
| 286 | limitations from the full printer class. The printer client driver will |
||
| 287 | fail to initialize the device if it does not indicate printer class support |
||
| 288 | in its interface descriptor. The printer client driver could allow any |
||
| 289 | device with an interface that matches the printer class endpoint |
||
| 290 | configuration, but both printer and mass storage devices utilize one bulk |
||
| 291 | IN and one bulk OUT endpoint. So a mass storage device would be |
||
| 292 | erroneously initialized as a printer device. This function allows a |
||
| 293 | client driver to know that the client driver support was specified |
||
| 294 | explicitly in the TPL, so for this particular device only, the class, |
||
| 295 | subclass, and protocol fields can be safely ignored. |
||
| 296 | ***************************************************************************/ |
||
| 297 | |||
| 298 | BOOL USBHostDeviceSpecificClientDriver( BYTE deviceAddress ) |
||
| 299 | { |
||
| 300 | return usbDeviceInfo.flags.bfUseDeviceClientDriver; |
||
| 301 | } |
||
| 302 | |||
| 303 | |||
| 304 | /**************************************************************************** |
||
| 305 | Function: |
||
| 306 | BYTE USBHostDeviceStatus( BYTE deviceAddress ) |
||
| 307 | |||
| 308 | Summary: |
||
| 309 | This function returns the current status of a device. |
||
| 310 | |||
| 311 | Description: |
||
| 312 | This function returns the current status of a device. If the device is |
||
| 313 | in a holding state due to an error, the error is returned. |
||
| 314 | |||
| 315 | Preconditions: |
||
| 316 | None |
||
| 317 | |||
| 318 | Parameters: |
||
| 319 | BYTE deviceAddress - Device address |
||
| 320 | |||
| 321 | Return Values: |
||
| 322 | USB_DEVICE_ATTACHED - Device is attached and running |
||
| 323 | USB_DEVICE_DETACHED - No device is attached |
||
| 324 | USB_DEVICE_ENUMERATING - Device is enumerating |
||
| 325 | USB_HOLDING_OUT_OF_MEMORY - Not enough heap space available |
||
| 326 | USB_HOLDING_UNSUPPORTED_DEVICE - Invalid configuration or |
||
| 327 | unsupported class |
||
| 328 | USB_HOLDING_UNSUPPORTED_HUB - Hubs are not supported |
||
| 329 | USB_HOLDING_INVALID_CONFIGURATION - Invalid configuration requested |
||
| 330 | USB_HOLDING_PROCESSING_CAPACITY - Processing requirement excessive |
||
| 331 | USB_HOLDING_POWER_REQUIREMENT - Power requirement excessive |
||
| 332 | USB_HOLDING_CLIENT_INIT_ERROR - Client driver failed to initialize |
||
| 333 | USB_DEVICE_SUSPENDED - Device is suspended |
||
| 334 | Other - Device is holding in an error |
||
| 335 | state. The return value |
||
| 336 | indicates the error. |
||
| 337 | |||
| 338 | Remarks: |
||
| 339 | None |
||
| 340 | ***************************************************************************/ |
||
| 341 | |||
| 342 | BYTE USBHostDeviceStatus( BYTE deviceAddress ) |
||
| 343 | { |
||
| 344 | if ((usbHostState & STATE_MASK) == STATE_DETACHED) |
||
| 345 | { |
||
| 346 | return USB_DEVICE_DETACHED; |
||
| 347 | } |
||
| 348 | |||
| 349 | if ((usbHostState & STATE_MASK) == STATE_RUNNING) |
||
| 350 | { |
||
| 351 | if ((usbHostState & SUBSTATE_MASK) == SUBSTATE_SUSPEND_AND_RESUME) |
||
| 352 | { |
||
| 353 | return USB_DEVICE_SUSPENDED; |
||
| 354 | } |
||
| 355 | else |
||
| 356 | { |
||
| 357 | return USB_DEVICE_ATTACHED; |
||
| 358 | } |
||
| 359 | } |
||
| 360 | |||
| 361 | if ((usbHostState & STATE_MASK) == STATE_HOLDING) |
||
| 362 | { |
||
| 363 | return usbDeviceInfo.errorCode; |
||
| 364 | } |
||
| 365 | |||
| 366 | return USB_DEVICE_ENUMERATING; |
||
| 367 | } |
||
| 368 | |||
| 369 | /**************************************************************************** |
||
| 370 | Function: |
||
| 371 | BOOL USBHostInit( unsigned long flags ) |
||
| 372 | |||
| 373 | Summary: |
||
| 374 | This function initializes the variables of the USB host stack. |
||
| 375 | |||
| 376 | Description: |
||
| 377 | This function initializes the variables of the USB host stack. It does |
||
| 378 | not initialize the hardware. The peripheral itself is initialized in one |
||
| 379 | of the state machine states. Therefore, USBHostTasks() should be called |
||
| 380 | soon after this function. |
||
| 381 | |||
| 382 | Precondition: |
||
| 383 | None |
||
| 384 | |||
| 385 | Parameters: |
||
| 386 | flags - reserved |
||
| 387 | |||
| 388 | Return Values: |
||
| 389 | TRUE - Initialization successful |
||
| 390 | FALSE - Could not allocate memory. |
||
| 391 | |||
| 392 | Remarks: |
||
| 393 | If the endpoint list is empty, an entry is created in the endpoint list |
||
| 394 | for EP0. If the list is not empty, free all allocated memory other than |
||
| 395 | the EP0 node. This allows the routine to be called multiple times by the |
||
| 396 | application. |
||
| 397 | ***************************************************************************/ |
||
| 398 | |||
| 399 | BOOL USBHostInit( unsigned long flags ) |
||
| 400 | { |
||
| 401 | // Allocate space for Endpoint 0. We will initialize it in the state machine, |
||
| 402 | // so we can reinitialize when another device connects. If the Endpoint 0 |
||
| 403 | // node already exists, free all other allocated memory. |
||
| 404 | if (usbDeviceInfo.pEndpoint0 == NULL) |
||
| 405 | { |
||
| 406 | if ((usbDeviceInfo.pEndpoint0 = (USB_ENDPOINT_INFO*)USB_MALLOC( sizeof(USB_ENDPOINT_INFO) )) == NULL) |
||
| 407 | { |
||
| 408 | #ifdef DEBUG_MODE |
||
| 409 | UART2PrintString( "HOST: Cannot allocate for endpoint 0.\r\n" ); |
||
| 410 | #endif |
||
| 411 | //return USB_MEMORY_ALLOCATION_ERROR; |
||
| 412 | return FALSE; |
||
| 413 | } |
||
| 414 | usbDeviceInfo.pEndpoint0->next = NULL; |
||
| 415 | } |
||
| 416 | else |
||
| 417 | { |
||
| 418 | _USB_FreeMemory(); |
||
| 419 | } |
||
| 420 | |||
| 421 | // Initialize other variables. |
||
| 422 | pCurrentEndpoint = usbDeviceInfo.pEndpoint0; |
||
| 423 | usbHostState = STATE_DETACHED; |
||
| 424 | usbOverrideHostState = NO_STATE; |
||
| 425 | usbDeviceInfo.deviceAddressAndSpeed = 0; |
||
| 426 | usbDeviceInfo.deviceAddress = 0; |
||
| 427 | usbRootHubInfo.flags.bPowerGoodPort0 = 1; |
||
| 428 | |||
| 429 | // Initialize event queue |
||
| 430 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 431 | StructQueueInit(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 432 | #endif |
||
| 433 | |||
| 434 | return TRUE; |
||
| 435 | } |
||
| 436 | |||
| 437 | |||
| 438 | /**************************************************************************** |
||
| 439 | Function: |
||
| 440 | BOOL USBHostIsochronousBuffersCreate( ISOCHRONOUS_DATA * isocData, |
||
| 441 | BYTE numberOfBuffers, WORD bufferSize ) |
||
| 442 | |||
| 443 | Description: |
||
| 444 | This function initializes the isochronous data buffer information and |
||
| 445 | allocates memory for each buffer. This function will not allocate memory |
||
| 446 | if the buffer pointer is not NULL. |
||
| 447 | |||
| 448 | Precondition: |
||
| 449 | None |
||
| 450 | |||
| 451 | Parameters: |
||
| 452 | None |
||
| 453 | |||
| 454 | Return Values: |
||
| 455 | TRUE - All buffers are allocated successfully. |
||
| 456 | FALSE - Not enough heap space to allocate all buffers - adjust the |
||
| 457 | project to provide more heap space. |
||
| 458 | |||
| 459 | Remarks: |
||
| 460 | This function is available only if USB_SUPPORT_ISOCHRONOUS_TRANSFERS |
||
| 461 | is defined in usb_config.h. |
||
| 462 | ***************************************************************************/ |
||
| 463 | #ifdef USB_SUPPORT_ISOCHRONOUS_TRANSFERS |
||
| 464 | |||
| 465 | BOOL USBHostIsochronousBuffersCreate( ISOCHRONOUS_DATA * isocData, BYTE numberOfBuffers, WORD bufferSize ) |
||
| 466 | { |
||
| 467 | BYTE i; |
||
| 468 | BYTE j; |
||
| 469 | |||
| 470 | USBHostIsochronousBuffersReset( isocData, numberOfBuffers ); |
||
| 471 | for (i=0; i<numberOfBuffers; i++) |
||
| 472 | { |
||
| 473 | if (isocData->buffers[i].pBuffer == NULL) |
||
| 474 | { |
||
| 475 | isocData->buffers[i].pBuffer = USB_MALLOC( bufferSize ); |
||
| 476 | if (isocData->buffers[i].pBuffer == NULL) |
||
| 477 | { |
||
| 478 | #ifdef DEBUG_MODE |
||
| 479 | UART2PrintString( "HOST: Not enough memory for isoc buffers.\r\n" ); |
||
| 480 | #endif |
||
| 481 | |||
| 482 | // Release all previous buffers. |
||
| 483 | for (j=0; j<i; j++) |
||
| 484 | { |
||
| 485 | USB_FREE_AND_CLEAR( isocData->buffers[j].pBuffer ); |
||
| 486 | isocData->buffers[j].pBuffer = NULL; |
||
| 487 | } |
||
| 488 | return FALSE; |
||
| 489 | } |
||
| 490 | } |
||
| 491 | } |
||
| 492 | return TRUE; |
||
| 493 | } |
||
| 494 | #endif |
||
| 495 | |||
| 496 | /**************************************************************************** |
||
| 497 | Function: |
||
| 498 | void USBHostIsochronousBuffersDestroy( ISOCHRONOUS_DATA * isocData, BYTE numberOfBuffers ) |
||
| 499 | |||
| 500 | Description: |
||
| 501 | This function releases all of the memory allocated for the isochronous |
||
| 502 | data buffers. It also resets all other information about the buffers. |
||
| 503 | |||
| 504 | Precondition: |
||
| 505 | None |
||
| 506 | |||
| 507 | Parameters: |
||
| 508 | None |
||
| 509 | |||
| 510 | Returns: |
||
| 511 | None |
||
| 512 | |||
| 513 | Remarks: |
||
| 514 | This function is available only if USB_SUPPORT_ISOCHRONOUS_TRANSFERS |
||
| 515 | is defined in usb_config.h. |
||
| 516 | ***************************************************************************/ |
||
| 517 | #ifdef USB_SUPPORT_ISOCHRONOUS_TRANSFERS |
||
| 518 | |||
| 519 | void USBHostIsochronousBuffersDestroy( ISOCHRONOUS_DATA * isocData, BYTE numberOfBuffers ) |
||
| 520 | { |
||
| 521 | BYTE i; |
||
| 522 | |||
| 523 | USBHostIsochronousBuffersReset( isocData, numberOfBuffers ); |
||
| 524 | for (i=0; i<numberOfBuffers; i++) |
||
| 525 | { |
||
| 526 | if (isocData->buffers[i].pBuffer != NULL) |
||
| 527 | { |
||
| 528 | USB_FREE_AND_CLEAR( isocData->buffers[i].pBuffer ); |
||
| 529 | isocData->buffers[i].pBuffer = NULL; |
||
| 530 | } |
||
| 531 | } |
||
| 532 | } |
||
| 533 | #endif |
||
| 534 | |||
| 535 | |||
| 536 | /**************************************************************************** |
||
| 537 | Function: |
||
| 538 | void USBHostIsochronousBuffersReset( ISOCHRONOUS_DATA * isocData, BYTE numberOfBuffers ) |
||
| 539 | |||
| 540 | Description: |
||
| 541 | This function resets all the isochronous data buffers. It does not do |
||
| 542 | anything with the space allocated for the buffers. |
||
| 543 | |||
| 544 | Precondition: |
||
| 545 | None |
||
| 546 | |||
| 547 | Parameters: |
||
| 548 | None |
||
| 549 | |||
| 550 | Returns: |
||
| 551 | None |
||
| 552 | |||
| 553 | Remarks: |
||
| 554 | This function is available only if USB_SUPPORT_ISOCHRONOUS_TRANSFERS |
||
| 555 | is defined in usb_config.h. |
||
| 556 | ***************************************************************************/ |
||
| 557 | #ifdef USB_SUPPORT_ISOCHRONOUS_TRANSFERS |
||
| 558 | |||
| 559 | void USBHostIsochronousBuffersReset( ISOCHRONOUS_DATA * isocData, BYTE numberOfBuffers ) |
||
| 560 | { |
||
| 561 | BYTE i; |
||
| 562 | |||
| 563 | for (i=0; i<numberOfBuffers; i++) |
||
| 564 | { |
||
| 565 | isocData->buffers[i].dataLength = 0; |
||
| 566 | isocData->buffers[i].bfDataLengthValid = 0; |
||
| 567 | } |
||
| 568 | |||
| 569 | isocData->totalBuffers = numberOfBuffers; |
||
| 570 | isocData->currentBufferUser = 0; |
||
| 571 | isocData->currentBufferUSB = 0; |
||
| 572 | isocData->pDataUser = NULL; |
||
| 573 | } |
||
| 574 | #endif |
||
| 575 | |||
| 576 | /**************************************************************************** |
||
| 577 | Function: |
||
| 578 | BYTE USBHostIssueDeviceRequest( BYTE deviceAddress, BYTE bmRequestType, |
||
| 579 | BYTE bRequest, WORD wValue, WORD wIndex, WORD wLength, |
||
| 580 | BYTE *data, BYTE dataDirection, BYTE clientDriverID ) |
||
| 581 | |||
| 582 | Summary: |
||
| 583 | This function sends a standard device request to the attached device. |
||
| 584 | |||
| 585 | Description: |
||
| 586 | This function sends a standard device request to the attached device. |
||
| 587 | The user must pass in the parameters of the device request. If there is |
||
| 588 | input or output data associated with the request, a pointer to the data |
||
| 589 | must be provided. The direction of the associated data (input or output) |
||
| 590 | must also be indicated. |
||
| 591 | |||
| 592 | This function does no special processing in regards to the request except |
||
| 593 | for three requests. If SET INTERFACE is sent, then DTS is reset for all |
||
| 594 | endpoints. If CLEAR FEATURE (ENDPOINT HALT) is sent, then DTS is reset |
||
| 595 | for that endpoint. |
||
| 596 | |||
| 597 | If the application wishes to change the device configuration, it should |
||
| 598 | use the function USBHostSetDeviceConfiguration() rather than this function |
||
| 599 | with the SET CONFIGURATION request, since endpoint definitions may |
||
| 600 | change. |
||
| 601 | |||
| 602 | Precondition: |
||
| 603 | The host state machine should be in the running state, and no reads or |
||
| 604 | writes to EP0 should be in progress. |
||
| 605 | |||
| 606 | Parameters: |
||
| 607 | BYTE deviceAddress - Device address |
||
| 608 | BYTE bmRequestType - The request type as defined by the USB |
||
| 609 | specification. |
||
| 610 | BYTE bRequest - The request as defined by the USB specification. |
||
| 611 | WORD wValue - The value for the request as defined by the USB |
||
| 612 | specification. |
||
| 613 | WORD wIndex - The index for the request as defined by the USB |
||
| 614 | specification. |
||
| 615 | WORD wLength - The data length for the request as defined by the |
||
| 616 | USB specification. |
||
| 617 | BYTE *data - Pointer to the data for the request. |
||
| 618 | BYTE dataDirection - USB_DEVICE_REQUEST_SET or USB_DEVICE_REQUEST_GET |
||
| 619 | BYTE clientDriverID - Client driver to send the event to. |
||
| 620 | |||
| 621 | Return Values: |
||
| 622 | USB_SUCCESS - Request processing started |
||
| 623 | USB_UNKNOWN_DEVICE - Device not found |
||
| 624 | USB_INVALID_STATE - The host must be in a normal running state |
||
| 625 | to do this request |
||
| 626 | USB_ENDPOINT_BUSY - A read or write is already in progress |
||
| 627 | USB_ILLEGAL_REQUEST - SET CONFIGURATION cannot be performed with |
||
| 628 | this function. |
||
| 629 | |||
| 630 | Remarks: |
||
| 631 | DTS reset is done before the command is issued. |
||
| 632 | ***************************************************************************/ |
||
| 633 | |||
| 634 | BYTE USBHostIssueDeviceRequest( BYTE deviceAddress, BYTE bmRequestType, BYTE bRequest, |
||
| 635 | WORD wValue, WORD wIndex, WORD wLength, BYTE *data, BYTE dataDirection, |
||
| 636 | BYTE clientDriverID ) |
||
| 637 | { |
||
| 638 | // Find the required device |
||
| 639 | if (deviceAddress != usbDeviceInfo.deviceAddress) |
||
| 640 | { |
||
| 641 | return USB_UNKNOWN_DEVICE; |
||
| 642 | } |
||
| 643 | |||
| 644 | // If we are not in a normal user running state, we cannot do this. |
||
| 645 | if ((usbHostState & STATE_MASK) != STATE_RUNNING) |
||
| 646 | { |
||
| 647 | return USB_INVALID_STATE; |
||
| 648 | } |
||
| 649 | |||
| 650 | // Make sure no other reads or writes on EP0 are in progress. |
||
| 651 | if (!usbDeviceInfo.pEndpoint0->status.bfTransferComplete) |
||
| 652 | { |
||
| 653 | return USB_ENDPOINT_BUSY; |
||
| 654 | } |
||
| 655 | |||
| 656 | // We can't do a SET CONFIGURATION here. Must use USBHostSetDeviceConfiguration(). |
||
| 657 | // ***** Some USB classes need to be able to do this, so we'll remove |
||
| 658 | // the constraint. |
||
| 659 | // if (bRequest == USB_REQUEST_SET_CONFIGURATION) |
||
| 660 | // { |
||
| 661 | // return USB_ILLEGAL_REQUEST; |
||
| 662 | // } |
||
| 663 | |||
| 664 | // If the user is doing a SET INTERFACE, we must reset DATA0 for all endpoints. |
||
| 665 | if (bRequest == USB_REQUEST_SET_INTERFACE) |
||
| 666 | { |
||
| 667 | USB_ENDPOINT_INFO *pEndpoint; |
||
| 668 | USB_INTERFACE_INFO *pInterface; |
||
| 669 | USB_INTERFACE_SETTING_INFO *pSetting; |
||
| 670 | |||
| 671 | // Make sure there are no transfers currently in progress on the current |
||
| 672 | // interface setting. |
||
| 673 | pInterface = usbDeviceInfo.pInterfaceList; |
||
| 674 | while (pInterface && (pInterface->interface != wIndex)) |
||
| 675 | { |
||
| 676 | pInterface = pInterface->next; |
||
| 677 | } |
||
| 678 | if ((pInterface == NULL) || (pInterface->pCurrentSetting == NULL)) |
||
| 679 | { |
||
| 680 | // The specified interface was not found. |
||
| 681 | return USB_ILLEGAL_REQUEST; |
||
| 682 | } |
||
| 683 | pEndpoint = pInterface->pCurrentSetting->pEndpointList; |
||
| 684 | while (pEndpoint) |
||
| 685 | { |
||
| 686 | if (!pEndpoint->status.bfTransferComplete) |
||
| 687 | { |
||
| 688 | // An endpoint on this setting is still transferring data. |
||
| 689 | return USB_ILLEGAL_REQUEST; |
||
| 690 | } |
||
| 691 | pEndpoint = pEndpoint->next; |
||
| 692 | } |
||
| 693 | |||
| 694 | // Make sure the new setting is valid. |
||
| 695 | pSetting = pInterface->pInterfaceSettings; |
||
| 696 | while( pSetting && (pSetting->interfaceAltSetting != wValue)) |
||
| 697 | { |
||
| 698 | pSetting = pSetting->next; |
||
| 699 | } |
||
| 700 | if (pSetting == NULL) |
||
| 701 | { |
||
| 702 | return USB_ILLEGAL_REQUEST; |
||
| 703 | } |
||
| 704 | |||
| 705 | // Set the pointer to the new setting. |
||
| 706 | pInterface->pCurrentSetting = pSetting; |
||
| 707 | } |
||
| 708 | |||
| 709 | // If the user is doing a CLEAR FEATURE(ENDPOINT_HALT), we must reset DATA0 for that endpoint. |
||
| 710 | if ((bRequest == USB_REQUEST_CLEAR_FEATURE) && (wValue == USB_FEATURE_ENDPOINT_HALT)) |
||
| 711 | { |
||
| 712 | switch(bmRequestType) |
||
| 713 | { |
||
| 714 | case 0x00: |
||
| 715 | case 0x01: |
||
| 716 | case 0x02: |
||
| 717 | _USB_ResetDATA0( (BYTE)wIndex ); |
||
| 718 | break; |
||
| 719 | default: |
||
| 720 | break; |
||
| 721 | } |
||
| 722 | } |
||
| 723 | |||
| 724 | // Set up the control packet. |
||
| 725 | pEP0Data[0] = bmRequestType; |
||
| 726 | pEP0Data[1] = bRequest; |
||
| 727 | pEP0Data[2] = wValue & 0xFF; |
||
| 728 | pEP0Data[3] = (wValue >> 8) & 0xFF; |
||
| 729 | pEP0Data[4] = wIndex & 0xFF; |
||
| 730 | pEP0Data[5] = (wIndex >> 8) & 0xFF; |
||
| 731 | pEP0Data[6] = wLength & 0xFF; |
||
| 732 | pEP0Data[7] = (wLength >> 8) & 0xFF; |
||
| 733 | |||
| 734 | // Set up the client driver for the event. |
||
| 735 | usbDeviceInfo.pEndpoint0->clientDriver = clientDriverID; |
||
| 736 | |||
| 737 | if (dataDirection == USB_DEVICE_REQUEST_SET) |
||
| 738 | { |
||
| 739 | // We are doing a SET command that requires data be sent. |
||
| 740 | _USB_InitControlWrite( usbDeviceInfo.pEndpoint0, pEP0Data,8, data, wLength ); |
||
| 741 | } |
||
| 742 | else |
||
| 743 | { |
||
| 744 | // We are doing a GET request. |
||
| 745 | _USB_InitControlRead( usbDeviceInfo.pEndpoint0, pEP0Data, 8, data, wLength ); |
||
| 746 | } |
||
| 747 | |||
| 748 | return USB_SUCCESS; |
||
| 749 | } |
||
| 750 | |||
| 751 | /**************************************************************************** |
||
| 752 | Function: |
||
| 753 | BYTE USBHostRead( BYTE deviceAddress, BYTE endpoint, BYTE *pData, |
||
| 754 | DWORD size ) |
||
| 755 | Summary: |
||
| 756 | This function initiates a read from the attached device. |
||
| 757 | |||
| 758 | Description: |
||
| 759 | This function initiates a read from the attached device. |
||
| 760 | |||
| 761 | If the endpoint is isochronous, special conditions apply. The pData and |
||
| 762 | size parameters have slightly different meanings, since multiple buffers |
||
| 763 | are required. Once started, an isochronous transfer will continue with |
||
| 764 | no upper layer intervention until USBHostTerminateTransfer() is called. |
||
| 765 | The ISOCHRONOUS_DATA_BUFFERS structure should not be manipulated until |
||
| 766 | the transfer is terminated. |
||
| 767 | |||
| 768 | To clarify parameter usage and to simplify casting, use the macro |
||
| 769 | USBHostReadIsochronous() when reading from an isochronous endpoint. |
||
| 770 | |||
| 771 | Precondition: |
||
| 772 | None |
||
| 773 | |||
| 774 | Parameters: |
||
| 775 | BYTE deviceAddress - Device address |
||
| 776 | BYTE endpoint - Endpoint number |
||
| 777 | BYTE *pData - Pointer to where to store the data. If the endpoint |
||
| 778 | is isochronous, this points to an |
||
| 779 | ISOCHRONOUS_DATA_BUFFERS structure, with multiple |
||
| 780 | data buffer pointers. |
||
| 781 | DWORD size - Number of data bytes to read. If the endpoint is |
||
| 782 | isochronous, this is the number of data buffer |
||
| 783 | pointers pointed to by pData. |
||
| 784 | |||
| 785 | Return Values: |
||
| 786 | USB_SUCCESS - Read started successfully. |
||
| 787 | USB_UNKNOWN_DEVICE - Device with the specified address not found. |
||
| 788 | USB_INVALID_STATE - We are not in a normal running state. |
||
| 789 | USB_ENDPOINT_ILLEGAL_TYPE - Must use USBHostControlRead to read |
||
| 790 | from a control endpoint. |
||
| 791 | USB_ENDPOINT_ILLEGAL_DIRECTION - Must read from an IN endpoint. |
||
| 792 | USB_ENDPOINT_STALLED - Endpoint is stalled. Must be cleared |
||
| 793 | by the application. |
||
| 794 | USB_ENDPOINT_ERROR - Endpoint has too many errors. Must be |
||
| 795 | cleared by the application. |
||
| 796 | USB_ENDPOINT_BUSY - A Read is already in progress. |
||
| 797 | USB_ENDPOINT_NOT_FOUND - Invalid endpoint. |
||
| 798 | |||
| 799 | Remarks: |
||
| 800 | None |
||
| 801 | ***************************************************************************/ |
||
| 802 | |||
| 803 | BYTE USBHostRead( BYTE deviceAddress, BYTE endpoint, BYTE *pData, DWORD size ) |
||
| 804 | { |
||
| 805 | USB_ENDPOINT_INFO *ep; |
||
| 806 | |||
| 807 | // Find the required device |
||
| 808 | if (deviceAddress != usbDeviceInfo.deviceAddress) |
||
| 809 | { |
||
| 810 | return USB_UNKNOWN_DEVICE; |
||
| 811 | } |
||
| 812 | |||
| 813 | // If we are not in a normal user running state, we cannot do this. |
||
| 814 | if ((usbHostState & STATE_MASK) != STATE_RUNNING) |
||
| 815 | { |
||
| 816 | return USB_INVALID_STATE; |
||
| 817 | } |
||
| 818 | |||
| 819 | ep = _USB_FindEndpoint( endpoint ); |
||
| 820 | if (ep) |
||
| 821 | { |
||
| 822 | if (ep->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_CONTROL) |
||
| 823 | { |
||
| 824 | // Must not be a control endpoint. |
||
| 825 | return USB_ENDPOINT_ILLEGAL_TYPE; |
||
| 826 | } |
||
| 827 | |||
| 828 | if (!(ep->bEndpointAddress & 0x80)) |
||
| 829 | { |
||
| 830 | // Trying to do an IN with an OUT endpoint. |
||
| 831 | return USB_ENDPOINT_ILLEGAL_DIRECTION; |
||
| 832 | } |
||
| 833 | |||
| 834 | if (ep->status.bfStalled) |
||
| 835 | { |
||
| 836 | // The endpoint is stalled. It must be restarted before a write |
||
| 837 | // can be performed. |
||
| 838 | return USB_ENDPOINT_STALLED; |
||
| 839 | } |
||
| 840 | |||
| 841 | if (ep->status.bfError) |
||
| 842 | { |
||
| 843 | // The endpoint has errored. The error must be cleared before a |
||
| 844 | // write can be performed. |
||
| 845 | return USB_ENDPOINT_ERROR; |
||
| 846 | } |
||
| 847 | |||
| 848 | if (!ep->status.bfTransferComplete) |
||
| 849 | { |
||
| 850 | // We are already processing a request for this endpoint. |
||
| 851 | return USB_ENDPOINT_BUSY; |
||
| 852 | } |
||
| 853 | |||
| 854 | _USB_InitRead( ep, pData, size ); |
||
| 855 | |||
| 856 | return USB_SUCCESS; |
||
| 857 | } |
||
| 858 | return USB_ENDPOINT_NOT_FOUND; // Endpoint not found |
||
| 859 | } |
||
| 860 | |||
| 861 | /**************************************************************************** |
||
| 862 | Function: |
||
| 863 | BYTE USBHostResetDevice( BYTE deviceAddress ) |
||
| 864 | |||
| 865 | Summary: |
||
| 866 | This function resets an attached device. |
||
| 867 | |||
| 868 | Description: |
||
| 869 | This function places the device back in the RESET state, to issue RESET |
||
| 870 | signaling. It can be called only if the state machine is not in the |
||
| 871 | DETACHED state. |
||
| 872 | |||
| 873 | Precondition: |
||
| 874 | None |
||
| 875 | |||
| 876 | Parameters: |
||
| 877 | BYTE deviceAddress - Device address |
||
| 878 | |||
| 879 | Return Values: |
||
| 880 | USB_SUCCESS - Success |
||
| 881 | USB_UNKNOWN_DEVICE - Device not found |
||
| 882 | USB_ILLEGAL_REQUEST - Device cannot RESUME unless it is suspended |
||
| 883 | |||
| 884 | Remarks: |
||
| 885 | In order to do a full clean-up, the state is set back to STATE_DETACHED |
||
| 886 | rather than a reset state. The ATTACH interrupt will automatically be |
||
| 887 | triggered when the module is re-enabled, and the proper reset will be |
||
| 888 | performed. |
||
| 889 | ***************************************************************************/ |
||
| 890 | |||
| 891 | BYTE USBHostResetDevice( BYTE deviceAddress ) |
||
| 892 | { |
||
| 893 | // Find the required device |
||
| 894 | if (deviceAddress != usbDeviceInfo.deviceAddress) |
||
| 895 | { |
||
| 896 | return USB_UNKNOWN_DEVICE; |
||
| 897 | } |
||
| 898 | |||
| 899 | if ((usbHostState & STATE_MASK) == STATE_DETACHED) |
||
| 900 | { |
||
| 901 | return USB_ILLEGAL_REQUEST; |
||
| 902 | } |
||
| 903 | |||
| 904 | usbHostState = STATE_DETACHED; |
||
| 905 | |||
| 906 | return USB_SUCCESS; |
||
| 907 | } |
||
| 908 | |||
| 909 | /**************************************************************************** |
||
| 910 | Function: |
||
| 911 | BYTE USBHostResumeDevice( BYTE deviceAddress ) |
||
| 912 | |||
| 913 | Summary: |
||
| 914 | This function issues a RESUME to the attached device. |
||
| 915 | |||
| 916 | Description: |
||
| 917 | This function issues a RESUME to the attached device. It can called only |
||
| 918 | if the state machine is in the suspend state. |
||
| 919 | |||
| 920 | Precondition: |
||
| 921 | None |
||
| 922 | |||
| 923 | Parameters: |
||
| 924 | BYTE deviceAddress - Device address |
||
| 925 | |||
| 926 | Return Values: |
||
| 927 | USB_SUCCESS - Success |
||
| 928 | USB_UNKNOWN_DEVICE - Device not found |
||
| 929 | USB_ILLEGAL_REQUEST - Device cannot RESUME unless it is suspended |
||
| 930 | |||
| 931 | Remarks: |
||
| 932 | None |
||
| 933 | ***************************************************************************/ |
||
| 934 | |||
| 935 | BYTE USBHostResumeDevice( BYTE deviceAddress ) |
||
| 936 | { |
||
| 937 | // Find the required device |
||
| 938 | if (deviceAddress != usbDeviceInfo.deviceAddress) |
||
| 939 | { |
||
| 940 | return USB_UNKNOWN_DEVICE; |
||
| 941 | } |
||
| 942 | |||
| 943 | if (usbHostState != (STATE_RUNNING | SUBSTATE_SUSPEND_AND_RESUME | SUBSUBSTATE_SUSPEND)) |
||
| 944 | { |
||
| 945 | return USB_ILLEGAL_REQUEST; |
||
| 946 | } |
||
| 947 | |||
| 948 | // Advance the state machine to issue resume signalling. |
||
| 949 | _USB_SetNextSubSubState(); |
||
| 950 | |||
| 951 | return USB_SUCCESS; |
||
| 952 | } |
||
| 953 | |||
| 954 | /**************************************************************************** |
||
| 955 | Function: |
||
| 956 | BYTE USBHostSetDeviceConfiguration( BYTE deviceAddress, BYTE configuration ) |
||
| 957 | |||
| 958 | Summary: |
||
| 959 | This function changes the device's configuration. |
||
| 960 | |||
| 961 | Description: |
||
| 962 | This function is used by the application to change the device's |
||
| 963 | Configuration. This function must be used instead of |
||
| 964 | USBHostIssueDeviceRequest(), because the endpoint definitions may change. |
||
| 965 | |||
| 966 | To see when the reconfiguration is complete, use the USBHostDeviceStatus() |
||
| 967 | function. If configuration is still in progress, this function will |
||
| 968 | return USB_DEVICE_ENUMERATING. |
||
| 969 | |||
| 970 | Precondition: |
||
| 971 | The host state machine should be in the running state, and no reads or |
||
| 972 | writes should be in progress. |
||
| 973 | |||
| 974 | Parameters: |
||
| 975 | BYTE deviceAddress - Device address |
||
| 976 | BYTE configuration - Index of the new configuration |
||
| 977 | |||
| 978 | Return Values: |
||
| 979 | USB_SUCCESS - Process of changing the configuration was started |
||
| 980 | successfully. |
||
| 981 | USB_UNKNOWN_DEVICE - Device not found |
||
| 982 | USB_INVALID_STATE - This function cannot be called during enumeration |
||
| 983 | or while performing a device request. |
||
| 984 | USB_BUSY - No IN or OUT transfers may be in progress. |
||
| 985 | |||
| 986 | Example: |
||
| 987 | <code> |
||
| 988 | rc = USBHostSetDeviceConfiguration( attachedDevice, configuration ); |
||
| 989 | if (rc) |
||
| 990 | { |
||
| 991 | // Error - cannot set configuration. |
||
| 992 | } |
||
| 993 | else |
||
| 994 | { |
||
| 995 | while (USBHostDeviceStatus( attachedDevice ) == USB_DEVICE_ENUMERATING) |
||
| 996 | { |
||
| 997 | USBHostTasks(); |
||
| 998 | } |
||
| 999 | } |
||
| 1000 | if (USBHostDeviceStatus( attachedDevice ) != USB_DEVICE_ATTACHED) |
||
| 1001 | { |
||
| 1002 | // Error - cannot set configuration. |
||
| 1003 | } |
||
| 1004 | </code> |
||
| 1005 | |||
| 1006 | Remarks: |
||
| 1007 | If an invalid configuration is specified, this function cannot return |
||
| 1008 | an error. Instead, the event USB_UNSUPPORTED_DEVICE will the sent to the |
||
| 1009 | application layer and the device will be placed in a holding state with a |
||
| 1010 | USB_HOLDING_UNSUPPORTED_DEVICE error returned by USBHostDeviceStatus(). |
||
| 1011 | ***************************************************************************/ |
||
| 1012 | |||
| 1013 | BYTE USBHostSetDeviceConfiguration( BYTE deviceAddress, BYTE configuration ) |
||
| 1014 | { |
||
| 1015 | // Find the required device |
||
| 1016 | if (deviceAddress != usbDeviceInfo.deviceAddress) |
||
| 1017 | { |
||
| 1018 | return USB_UNKNOWN_DEVICE; |
||
| 1019 | } |
||
| 1020 | |||
| 1021 | // If we are not in a normal user running state, we cannot do this. |
||
| 1022 | if ((usbHostState & STATE_MASK) != STATE_RUNNING) |
||
| 1023 | { |
||
| 1024 | return USB_INVALID_STATE; |
||
| 1025 | } |
||
| 1026 | |||
| 1027 | // Make sure no other reads or writes are in progress. |
||
| 1028 | if (_USB_TransferInProgress()) |
||
| 1029 | { |
||
| 1030 | return USB_BUSY; |
||
| 1031 | } |
||
| 1032 | |||
| 1033 | // Set the new device configuration. |
||
| 1034 | usbDeviceInfo.currentConfiguration = configuration; |
||
| 1035 | |||
| 1036 | // We're going to be sending Endpoint 0 commands, so be sure the |
||
| 1037 | // client driver indicates the host driver, so we do not send events up |
||
| 1038 | // to a client driver. |
||
| 1039 | usbDeviceInfo.pEndpoint0->clientDriver = CLIENT_DRIVER_HOST; |
||
| 1040 | |||
| 1041 | // Set the state back to configure the device. This will destroy the |
||
| 1042 | // endpoint list and terminate any current transactions. We already have |
||
| 1043 | // the configuration, so we can jump into the Select Configuration state. |
||
| 1044 | // If the configuration value is invalid, the state machine will error and |
||
| 1045 | // put the device into a holding state. |
||
| 1046 | usbHostState = STATE_CONFIGURING | SUBSTATE_SELECT_CONFIGURATION; |
||
| 1047 | |||
| 1048 | return USB_SUCCESS; |
||
| 1049 | } |
||
| 1050 | |||
| 1051 | |||
| 1052 | /**************************************************************************** |
||
| 1053 | Function: |
||
| 1054 | BYTE USBHostSetNAKTimeout( BYTE deviceAddress, BYTE endpoint, WORD flags, |
||
| 1055 | WORD timeoutCount ) |
||
| 1056 | |||
| 1057 | Summary: |
||
| 1058 | This function specifies NAK timeout capability. |
||
| 1059 | |||
| 1060 | Description: |
||
| 1061 | This function is used to set whether or not an endpoint on a device |
||
| 1062 | should time out a transaction based on the number of NAKs received, and |
||
| 1063 | if so, how many NAKs are allowed before the timeout. |
||
| 1064 | |||
| 1065 | Precondition: |
||
| 1066 | None |
||
| 1067 | |||
| 1068 | Parameters: |
||
| 1069 | BYTE deviceAddress - Device address |
||
| 1070 | BYTE endpoint - Endpoint number to configure |
||
| 1071 | WORD flags - Bit 0: |
||
| 1072 | * 0 = disable NAK timeout |
||
| 1073 | * 1 = enable NAK timeout |
||
| 1074 | WORD timeoutCount - Number of NAKs allowed before a timeout |
||
| 1075 | |||
| 1076 | Return Values: |
||
| 1077 | USB_SUCCESS - NAK timeout was configured successfully. |
||
| 1078 | USB_UNKNOWN_DEVICE - Device not found. |
||
| 1079 | USB_ENDPOINT_NOT_FOUND - The specified endpoint was not found. |
||
| 1080 | |||
| 1081 | Remarks: |
||
| 1082 | None |
||
| 1083 | ***************************************************************************/ |
||
| 1084 | |||
| 1085 | BYTE USBHostSetNAKTimeout( BYTE deviceAddress, BYTE endpoint, WORD flags, WORD timeoutCount ) |
||
| 1086 | { |
||
| 1087 | USB_ENDPOINT_INFO *ep; |
||
| 1088 | |||
| 1089 | // Find the required device |
||
| 1090 | if (deviceAddress != usbDeviceInfo.deviceAddress) |
||
| 1091 | { |
||
| 1092 | return USB_UNKNOWN_DEVICE; |
||
| 1093 | } |
||
| 1094 | |||
| 1095 | ep = _USB_FindEndpoint( endpoint ); |
||
| 1096 | if (ep) |
||
| 1097 | { |
||
| 1098 | ep->status.bfNAKTimeoutEnabled = flags & 0x01; |
||
| 1099 | ep->timeoutNAKs = timeoutCount; |
||
| 1100 | |||
| 1101 | return USB_SUCCESS; |
||
| 1102 | } |
||
| 1103 | return USB_ENDPOINT_NOT_FOUND; |
||
| 1104 | } |
||
| 1105 | |||
| 1106 | |||
| 1107 | /**************************************************************************** |
||
| 1108 | Function: |
||
| 1109 | void USBHostShutdown( void ) |
||
| 1110 | |||
| 1111 | Description: |
||
| 1112 | This function turns off the USB module and frees all unnecessary memory. |
||
| 1113 | This routine can be called by the application layer to shut down all |
||
| 1114 | USB activity, which effectively detaches all devices. The event |
||
| 1115 | EVENT_DETACH will be sent to the client drivers for the attached device, |
||
| 1116 | and the event EVENT_VBUS_RELEASE_POWER will be sent to the application |
||
| 1117 | layer. |
||
| 1118 | |||
| 1119 | Precondition: |
||
| 1120 | None |
||
| 1121 | |||
| 1122 | Parameters: |
||
| 1123 | None - None |
||
| 1124 | |||
| 1125 | Returns: |
||
| 1126 | None |
||
| 1127 | |||
| 1128 | Remarks: |
||
| 1129 | None |
||
| 1130 | ***************************************************************************/ |
||
| 1131 | |||
| 1132 | void USBHostShutdown( void ) |
||
| 1133 | { |
||
| 1134 | // Shut off the power to the module first, in case we are in an |
||
| 1135 | // overcurrent situation. |
||
| 1136 | |||
| 1137 | #ifdef USB_SUPPORT_OTG |
||
| 1138 | if (!USBOTGHnpIsActive()) |
||
| 1139 | { |
||
| 1140 | // If we currently have an attached device, notify the higher layers that |
||
| 1141 | // the device is being removed. |
||
| 1142 | if (usbDeviceInfo.deviceAddress) |
||
| 1143 | { |
||
| 1144 | USB_VBUS_POWER_EVENT_DATA powerRequest; |
||
| 1145 | |||
| 1146 | powerRequest.port = 0; // Currently was have only one port. |
||
| 1147 | |||
| 1148 | USB_HOST_APP_EVENT_HANDLER( usbDeviceInfo.deviceAddress, EVENT_VBUS_RELEASE_POWER, |
||
| 1149 | &powerRequest, sizeof(USB_VBUS_POWER_EVENT_DATA) ); |
||
| 1150 | _USB_NotifyClients(usbDeviceInfo.deviceAddress, EVENT_DETACH, |
||
| 1151 | &usbDeviceInfo.deviceAddress, sizeof(BYTE) ); |
||
| 1152 | |||
| 1153 | |||
| 1154 | } |
||
| 1155 | } |
||
| 1156 | #else |
||
| 1157 | U1PWRC = USB_NORMAL_OPERATION | USB_DISABLED; //MR - Turning off Module will cause unwanted Suspends in OTG |
||
| 1158 | |||
| 1159 | // If we currently have an attached device, notify the higher layers that |
||
| 1160 | // the device is being removed. |
||
| 1161 | if (usbDeviceInfo.deviceAddress) |
||
| 1162 | { |
||
| 1163 | USB_VBUS_POWER_EVENT_DATA powerRequest; |
||
| 1164 | |||
| 1165 | powerRequest.port = 0; // Currently was have only one port. |
||
| 1166 | |||
| 1167 | USB_HOST_APP_EVENT_HANDLER( usbDeviceInfo.deviceAddress, EVENT_VBUS_RELEASE_POWER, |
||
| 1168 | &powerRequest, sizeof(USB_VBUS_POWER_EVENT_DATA) ); |
||
| 1169 | _USB_NotifyClients(usbDeviceInfo.deviceAddress, EVENT_DETACH, |
||
| 1170 | &usbDeviceInfo.deviceAddress, sizeof(BYTE) ); |
||
| 1171 | |||
| 1172 | |||
| 1173 | } |
||
| 1174 | #endif |
||
| 1175 | |||
| 1176 | // Free all extra allocated memory, initialize variables, and reset the |
||
| 1177 | // state machine. |
||
| 1178 | USBHostInit( 0 ); |
||
| 1179 | } |
||
| 1180 | |||
| 1181 | |||
| 1182 | /**************************************************************************** |
||
| 1183 | Function: |
||
| 1184 | BYTE USBHostSuspendDevice( BYTE deviceAddress ) |
||
| 1185 | |||
| 1186 | Summary: |
||
| 1187 | This function suspends a device. |
||
| 1188 | |||
| 1189 | Description: |
||
| 1190 | This function put a device into an IDLE state. It can only be called |
||
| 1191 | while the state machine is in normal running mode. After 3ms, the |
||
| 1192 | attached device should go into SUSPEND mode. |
||
| 1193 | |||
| 1194 | Precondition: |
||
| 1195 | None |
||
| 1196 | |||
| 1197 | Parameters: |
||
| 1198 | BYTE deviceAddress - Device to suspend |
||
| 1199 | |||
| 1200 | Return Values: |
||
| 1201 | USB_SUCCESS - Success |
||
| 1202 | USB_UNKNOWN_DEVICE - Device not found |
||
| 1203 | USB_ILLEGAL_REQUEST - Cannot suspend unless device is in normal run mode |
||
| 1204 | |||
| 1205 | Remarks: |
||
| 1206 | None |
||
| 1207 | ***************************************************************************/ |
||
| 1208 | |||
| 1209 | BYTE USBHostSuspendDevice( BYTE deviceAddress ) |
||
| 1210 | { |
||
| 1211 | // Find the required device |
||
| 1212 | if (deviceAddress != usbDeviceInfo.deviceAddress) |
||
| 1213 | { |
||
| 1214 | return USB_UNKNOWN_DEVICE; |
||
| 1215 | } |
||
| 1216 | |||
| 1217 | if (usbHostState != (STATE_RUNNING | SUBSTATE_NORMAL_RUN)) |
||
| 1218 | { |
||
| 1219 | return USB_ILLEGAL_REQUEST; |
||
| 1220 | } |
||
| 1221 | |||
| 1222 | // Turn off SOF's, so the bus is idle. |
||
| 1223 | U1CONbits.SOFEN = 0; |
||
| 1224 | |||
| 1225 | // Put the state machine in suspend mode. |
||
| 1226 | usbHostState = STATE_RUNNING | SUBSTATE_SUSPEND_AND_RESUME | SUBSUBSTATE_SUSPEND; |
||
| 1227 | |||
| 1228 | return USB_SUCCESS; |
||
| 1229 | } |
||
| 1230 | |||
| 1231 | /**************************************************************************** |
||
| 1232 | Function: |
||
| 1233 | void USBHostTasks( void ) |
||
| 1234 | |||
| 1235 | Summary: |
||
| 1236 | This function executes the host tasks for USB host operation. |
||
| 1237 | |||
| 1238 | Description: |
||
| 1239 | This function executes the host tasks for USB host operation. It must be |
||
| 1240 | executed on a regular basis to keep everything functioning. |
||
| 1241 | |||
| 1242 | The primary purpose of this function is to handle device attach/detach |
||
| 1243 | and enumeration. It does not handle USB packet transmission or |
||
| 1244 | reception; that must be done in the USB interrupt handler to ensure |
||
| 1245 | timely operation. |
||
| 1246 | |||
| 1247 | This routine should be called on a regular basis, but there is no |
||
| 1248 | specific time requirement. Devices will still be able to attach, |
||
| 1249 | enumerate, and detach, but the operations will occur more slowly as the |
||
| 1250 | calling interval increases. |
||
| 1251 | |||
| 1252 | Precondition: |
||
| 1253 | USBHostInit() has been called. |
||
| 1254 | |||
| 1255 | Parameters: |
||
| 1256 | None |
||
| 1257 | |||
| 1258 | Returns: |
||
| 1259 | None |
||
| 1260 | |||
| 1261 | Remarks: |
||
| 1262 | None |
||
| 1263 | ***************************************************************************/ |
||
| 1264 | |||
| 1265 | void USBHostTasks( void ) |
||
| 1266 | { |
||
| 1267 | static USB_CONFIGURATION *pCurrentConfigurationNode; //MR - made static for OTG |
||
| 1268 | USB_INTERFACE_INFO *pCurrentInterface; |
||
| 1269 | BYTE *pTemp; |
||
| 1270 | BYTE temp; |
||
| 1271 | USB_VBUS_POWER_EVENT_DATA powerRequest; |
||
| 1272 | |||
| 1273 | #ifdef DEBUG_MODE |
||
| 1274 | // UART2PutChar('<'); |
||
| 1275 | // UART2PutHex( usbHostState>>8 ); |
||
| 1276 | // UART2PutHex( usbHostState & 0xff ); |
||
| 1277 | // UART2PutChar('-'); |
||
| 1278 | // UART2PutHex( pCurrentEndpoint->transferState ); |
||
| 1279 | // UART2PutChar('>'); |
||
| 1280 | #endif |
||
| 1281 | |||
| 1282 | // The PIC32MX detach interrupt is not reliable. If we are not in one of |
||
| 1283 | // the detached states, we'll do a check here to see if we've detached. |
||
| 1284 | // If the ATTACH bit is 0, we have detached. |
||
| 1285 | #ifdef __PIC32MX__ |
||
| 1286 | #ifdef USE_MANUAL_DETACH_DETECT |
||
| 1287 | if (((usbHostState & STATE_MASK) != STATE_DETACHED) && !U1IRbits.ATTACHIF) |
||
| 1288 | { |
||
| 1289 | #ifdef DEBUG_MODE |
||
| 1290 | UART2PutChar( '>' ); |
||
| 1291 | UART2PutChar( ']' ); |
||
| 1292 | #endif |
||
| 1293 | usbHostState = STATE_DETACHED; |
||
| 1294 | } |
||
| 1295 | #endif |
||
| 1296 | #endif |
||
| 1297 | |||
| 1298 | // Send any queued events to the client and application layers. |
||
| 1299 | #if defined ( USB_ENABLE_TRANSFER_EVENT ) |
||
| 1300 | { |
||
| 1301 | USB_EVENT_DATA *item; |
||
| 1302 | #if defined( __C30__ ) |
||
| 1303 | WORD interrupt_mask; |
||
| 1304 | #elif defined( __PIC32MX__ ) |
||
| 1305 | UINT32 interrupt_mask; |
||
| 1306 | #else |
||
| 1307 | #error Cannot save interrupt status |
||
| 1308 | #endif |
||
| 1309 | |||
| 1310 | while (StructQueueIsNotEmpty(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 1311 | { |
||
| 1312 | item = StructQueuePeekTail(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 1313 | |||
| 1314 | switch(item->event) |
||
| 1315 | { |
||
| 1316 | case EVENT_TRANSFER: |
||
| 1317 | case EVENT_BUS_ERROR: |
||
| 1318 | _USB_NotifyClients( usbDeviceInfo.deviceAddress, item->event, &item->TransferData, sizeof(HOST_TRANSFER_DATA) ); |
||
| 1319 | break; |
||
| 1320 | default: |
||
| 1321 | break; |
||
| 1322 | } |
||
| 1323 | |||
| 1324 | // Guard against USB interrupts |
||
| 1325 | interrupt_mask = U1IE; |
||
| 1326 | U1IE = 0; |
||
| 1327 | |||
| 1328 | item = StructQueueRemove(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 1329 | |||
| 1330 | // Re-enable USB interrupts |
||
| 1331 | U1IE = interrupt_mask; |
||
| 1332 | } |
||
| 1333 | } |
||
| 1334 | #endif |
||
| 1335 | |||
| 1336 | // See if we got an interrupt to change our state. |
||
| 1337 | if (usbOverrideHostState != NO_STATE) |
||
| 1338 | { |
||
| 1339 | #ifdef DEBUG_MODE |
||
| 1340 | UART2PutChar('>'); |
||
| 1341 | #endif |
||
| 1342 | usbHostState = usbOverrideHostState; |
||
| 1343 | usbOverrideHostState = NO_STATE; |
||
| 1344 | } |
||
| 1345 | |||
| 1346 | //------------------------------------------------------------------------- |
||
| 1347 | // Main State Machine |
||
| 1348 | |||
| 1349 | switch (usbHostState & STATE_MASK) |
||
| 1350 | { |
||
| 1351 | case STATE_DETACHED: |
||
| 1352 | switch (usbHostState & SUBSTATE_MASK) |
||
| 1353 | { |
||
| 1354 | case SUBSTATE_INITIALIZE: |
||
| 1355 | // We got here either from initialization or from the user |
||
| 1356 | // unplugging the device at any point in time. |
||
| 1357 | |||
| 1358 | // Turn off the module and free up memory. |
||
| 1359 | USBHostShutdown(); |
||
| 1360 | |||
| 1361 | #ifdef DEBUG_MODE |
||
| 1362 | UART2PrintString( "HOST: Initializing DETACHED state.\r\n" ); |
||
| 1363 | #endif |
||
| 1364 | |||
| 1365 | // Initialize Endpoint 0 attributes. |
||
| 1366 | usbDeviceInfo.pEndpoint0->next = NULL; |
||
| 1367 | usbDeviceInfo.pEndpoint0->status.val = 0x00; |
||
| 1368 | usbDeviceInfo.pEndpoint0->status.bfUseDTS = 1; |
||
| 1369 | usbDeviceInfo.pEndpoint0->status.bfTransferComplete = 1; // Initialize to success to allow preprocessing loops. |
||
| 1370 | usbDeviceInfo.pEndpoint0->status.bfNAKTimeoutEnabled = 1; // So we can catch devices that NAK forever during enumeration |
||
| 1371 | usbDeviceInfo.pEndpoint0->timeoutNAKs = USB_NUM_CONTROL_NAKS; |
||
| 1372 | usbDeviceInfo.pEndpoint0->wMaxPacketSize = 64; |
||
| 1373 | usbDeviceInfo.pEndpoint0->dataCount = 0; // Initialize to 0 since we set bfTransferComplete. |
||
| 1374 | usbDeviceInfo.pEndpoint0->bEndpointAddress = 0; |
||
| 1375 | usbDeviceInfo.pEndpoint0->transferState = TSTATE_IDLE; |
||
| 1376 | usbDeviceInfo.pEndpoint0->bmAttributes.bfTransferType = USB_TRANSFER_TYPE_CONTROL; |
||
| 1377 | usbDeviceInfo.pEndpoint0->clientDriver = CLIENT_DRIVER_HOST; |
||
| 1378 | |||
| 1379 | // Initialize any device specific information. |
||
| 1380 | numEnumerationTries = USB_NUM_ENUMERATION_TRIES; |
||
| 1381 | usbDeviceInfo.currentConfiguration = 0; // Will be overwritten by config process or the user later |
||
| 1382 | usbDeviceInfo.attributesOTG = 0; |
||
| 1383 | usbDeviceInfo.deviceAddressAndSpeed = 0; |
||
| 1384 | usbDeviceInfo.flags.val = 0; |
||
| 1385 | usbDeviceInfo.pInterfaceList = NULL; |
||
| 1386 | usbBusInfo.flags.val = 0; |
||
| 1387 | |||
| 1388 | // Set up the hardware. |
||
| 1389 | U1IE = 0; // Clear and turn off interrupts. |
||
| 1390 | U1IR = 0xFF; |
||
| 1391 | U1OTGIE &= 0x8C; |
||
| 1392 | U1OTGIR = 0x7D; |
||
| 1393 | U1EIE = 0; |
||
| 1394 | U1EIR = 0xFF; |
||
| 1395 | |||
| 1396 | // Initialize the Buffer Descriptor Table pointer. |
||
| 1397 | #if defined(__C30__) |
||
| 1398 | U1BDTP1 = (WORD)(&BDT) >> 8; |
||
| 1399 | #elif defined(__PIC32MX__) |
||
| 1400 | U1BDTP1 = ((DWORD)KVA_TO_PA(&BDT) & 0x0000FF00) >> 8; |
||
| 1401 | U1BDTP2 = ((DWORD)KVA_TO_PA(&BDT) & 0x00FF0000) >> 16; |
||
| 1402 | U1BDTP3 = ((DWORD)KVA_TO_PA(&BDT) & 0xFF000000) >> 24; |
||
| 1403 | #else |
||
| 1404 | #error Cannot set up the Buffer Descriptor Table pointer. |
||
| 1405 | #endif |
||
| 1406 | |||
| 1407 | // Configure the module |
||
| 1408 | U1CON = USB_HOST_MODE_ENABLE | USB_SOF_DISABLE; // Turn of SOF's to cut down noise |
||
| 1409 | U1CON = USB_HOST_MODE_ENABLE | USB_PINGPONG_RESET | USB_SOF_DISABLE; // Reset the ping-pong buffers |
||
| 1410 | U1CON = USB_HOST_MODE_ENABLE | USB_SOF_DISABLE; // Release the ping-pong buffers |
||
| 1411 | #ifdef USB_SUPPORT_OTG |
||
| 1412 | U1OTGCON |= USB_DPLUS_PULLDOWN_ENABLE | USB_DMINUS_PULLDOWN_ENABLE | USB_OTG_ENABLE; // Pull down D+ and D- |
||
| 1413 | #else |
||
| 1414 | U1OTGCON = USB_DPLUS_PULLDOWN_ENABLE | USB_DMINUS_PULLDOWN_ENABLE; // Pull down D+ and D- |
||
| 1415 | #endif |
||
| 1416 | |||
| 1417 | #if defined(__PIC32MX__) |
||
| 1418 | U1OTGCON |= USB_VBUS_ON; |
||
| 1419 | #endif |
||
| 1420 | |||
| 1421 | U1CNFG1 = USB_PING_PONG_MODE; |
||
| 1422 | #if defined(__C30__) |
||
| 1423 | U1CNFG2 = USB_VBUS_BOOST_ENABLE | USB_VBUS_COMPARE_ENABLE | USB_ONCHIP_ENABLE; |
||
| 1424 | #endif |
||
| 1425 | U1ADDR = 0; // Set default address and LSPDEN to 0 |
||
| 1426 | U1EP0bits.LSPD = 0; |
||
| 1427 | U1SOF = USB_SOF_THRESHOLD_64; // Maximum EP0 packet size |
||
| 1428 | |||
| 1429 | // Set the next substate. We do this before we enable |
||
| 1430 | // interrupts in case the interrupt changes states. |
||
| 1431 | _USB_SetNextSubState(); |
||
| 1432 | break; |
||
| 1433 | |||
| 1434 | case SUBSTATE_WAIT_FOR_POWER: |
||
| 1435 | // We will wait here until the application tells us we can |
||
| 1436 | // turn on power. |
||
| 1437 | if (usbRootHubInfo.flags.bPowerGoodPort0) |
||
| 1438 | { |
||
| 1439 | _USB_SetNextSubState(); |
||
| 1440 | } |
||
| 1441 | break; |
||
| 1442 | |||
| 1443 | case SUBSTATE_TURN_ON_POWER: |
||
| 1444 | powerRequest.port = 0; |
||
| 1445 | powerRequest.current = USB_INITIAL_VBUS_CURRENT; |
||
| 1446 | if (USB_HOST_APP_EVENT_HANDLER( USB_ROOT_HUB, EVENT_VBUS_REQUEST_POWER, |
||
| 1447 | &powerRequest, sizeof(USB_VBUS_POWER_EVENT_DATA) )) |
||
| 1448 | { |
||
| 1449 | // Power on the module |
||
| 1450 | U1PWRC = USB_NORMAL_OPERATION | USB_ENABLED; |
||
| 1451 | |||
| 1452 | #if defined( __C30__ ) |
||
| 1453 | IFS5 &= 0xFFBF; |
||
| 1454 | IPC21 &= 0xF0FF; |
||
| 1455 | IPC21 |= 0x0600; |
||
| 1456 | IEC5 |= 0x0040; |
||
| 1457 | #elif defined( __PIC32MX__ ) |
||
| 1458 | // Enable the USB interrupt. |
||
| 1459 | IFS1CLR = 0x02000000; |
||
| 1460 | IPC11CLR = 0x0000FF00; |
||
| 1461 | IPC11SET = 0x00001000; |
||
| 1462 | IEC1SET = 0x02000000; |
||
| 1463 | #else |
||
| 1464 | #error Cannot enable USB interrupt. |
||
| 1465 | #endif |
||
| 1466 | |||
| 1467 | // Set the next substate. We do this before we enable |
||
| 1468 | // interrupts in case the interrupt changes states. |
||
| 1469 | _USB_SetNextSubState(); |
||
| 1470 | |||
| 1471 | // Enable the ATTACH interrupt. |
||
| 1472 | U1IEbits.ATTACHIE = 1; |
||
| 1473 | } |
||
| 1474 | else |
||
| 1475 | { |
||
| 1476 | usbRootHubInfo.flags.bPowerGoodPort0 = 0; |
||
| 1477 | usbHostState = STATE_DETACHED | SUBSTATE_WAIT_FOR_POWER; |
||
| 1478 | } |
||
| 1479 | break; |
||
| 1480 | |||
| 1481 | case SUBSTATE_WAIT_FOR_DEVICE: |
||
| 1482 | // Wait here for the ATTACH interrupt. |
||
| 1483 | #ifdef USB_SUPPORT_OTG |
||
| 1484 | U1IEbits.ATTACHIE = 1; |
||
| 1485 | #endif |
||
| 1486 | break; |
||
| 1487 | } |
||
| 1488 | break; |
||
| 1489 | |||
| 1490 | case STATE_ATTACHED: |
||
| 1491 | switch (usbHostState & SUBSTATE_MASK) |
||
| 1492 | { |
||
| 1493 | case SUBSTATE_SETTLE: |
||
| 1494 | // Wait 100ms for the insertion process to complete and power |
||
| 1495 | // at the device to be stable. |
||
| 1496 | switch (usbHostState & SUBSUBSTATE_MASK) |
||
| 1497 | { |
||
| 1498 | case SUBSUBSTATE_START_SETTLING_DELAY: |
||
| 1499 | #ifdef DEBUG_MODE |
||
| 1500 | UART2PrintString( "HOST: Starting settling delay.\r\n" ); |
||
| 1501 | #endif |
||
| 1502 | |||
| 1503 | // Clear and turn on the DETACH interrupt. |
||
| 1504 | U1IR = USB_INTERRUPT_DETACH; // The interrupt is cleared by writing a '1' to the flag. |
||
| 1505 | U1IEbits.DETACHIE = 1; |
||
| 1506 | |||
| 1507 | // Configure and turn on the settling timer - 100ms. |
||
| 1508 | numTimerInterrupts = USB_INSERT_TIME; |
||
| 1509 | U1OTGIR = USB_INTERRUPT_T1MSECIF; // The interrupt is cleared by writing a '1' to the flag. |
||
| 1510 | U1OTGIEbits.T1MSECIE = 1; |
||
| 1511 | _USB_SetNextSubSubState(); |
||
| 1512 | break; |
||
| 1513 | |||
| 1514 | case SUBSUBSTATE_WAIT_FOR_SETTLING: |
||
| 1515 | // Wait for the timer to finish in the background. |
||
| 1516 | break; |
||
| 1517 | |||
| 1518 | case SUBSUBSTATE_SETTLING_DONE: |
||
| 1519 | _USB_SetNextSubState(); |
||
| 1520 | break; |
||
| 1521 | |||
| 1522 | default: |
||
| 1523 | // We shouldn't get here. |
||
| 1524 | break; |
||
| 1525 | } |
||
| 1526 | break; |
||
| 1527 | |||
| 1528 | case SUBSTATE_RESET_DEVICE: |
||
| 1529 | // Reset the device. We have to do the reset timing ourselves. |
||
| 1530 | switch (usbHostState & SUBSUBSTATE_MASK) |
||
| 1531 | { |
||
| 1532 | case SUBSUBSTATE_SET_RESET: |
||
| 1533 | #ifdef DEBUG_MODE |
||
| 1534 | UART2PrintString( "HOST: Resetting the device.\r\n" ); |
||
| 1535 | #endif |
||
| 1536 | |||
| 1537 | // Prepare a data buffer for us to use. We'll make it 8 bytes for now, |
||
| 1538 | // which is the minimum wMaxPacketSize for EP0. |
||
| 1539 | if (pEP0Data != NULL) |
||
| 1540 | { |
||
| 1541 | USB_FREE_AND_CLEAR( pEP0Data ); |
||
| 1542 | } |
||
| 1543 | if ((pEP0Data = (BYTE *)USB_MALLOC( 8 )) == NULL) |
||
| 1544 | { |
||
| 1545 | #ifdef DEBUG_MODE |
||
| 1546 | UART2PrintString( "HOST: Error alloc-ing pEP0Data\r\n" ); |
||
| 1547 | #endif |
||
| 1548 | _USB_SetErrorCode( USB_HOLDING_OUT_OF_MEMORY ); |
||
| 1549 | _USB_SetHoldState(); |
||
| 1550 | break; |
||
| 1551 | } |
||
| 1552 | |||
| 1553 | // Initialize the USB Device information |
||
| 1554 | usbDeviceInfo.currentConfiguration = 0; |
||
| 1555 | usbDeviceInfo.attributesOTG = 0; |
||
| 1556 | usbDeviceInfo.flags.val = 0; |
||
| 1557 | |||
| 1558 | _USB_InitErrorCounters(); |
||
| 1559 | |||
| 1560 | // Disable all EP's except EP0. |
||
| 1561 | U1EP0 = USB_ENDPOINT_CONTROL_SETUP; |
||
| 1562 | U1EP1 = USB_DISABLE_ENDPOINT; |
||
| 1563 | U1EP2 = USB_DISABLE_ENDPOINT; |
||
| 1564 | U1EP3 = USB_DISABLE_ENDPOINT; |
||
| 1565 | U1EP4 = USB_DISABLE_ENDPOINT; |
||
| 1566 | U1EP5 = USB_DISABLE_ENDPOINT; |
||
| 1567 | U1EP6 = USB_DISABLE_ENDPOINT; |
||
| 1568 | U1EP7 = USB_DISABLE_ENDPOINT; |
||
| 1569 | U1EP8 = USB_DISABLE_ENDPOINT; |
||
| 1570 | U1EP9 = USB_DISABLE_ENDPOINT; |
||
| 1571 | U1EP10 = USB_DISABLE_ENDPOINT; |
||
| 1572 | U1EP11 = USB_DISABLE_ENDPOINT; |
||
| 1573 | U1EP12 = USB_DISABLE_ENDPOINT; |
||
| 1574 | U1EP13 = USB_DISABLE_ENDPOINT; |
||
| 1575 | U1EP14 = USB_DISABLE_ENDPOINT; |
||
| 1576 | U1EP15 = USB_DISABLE_ENDPOINT; |
||
| 1577 | |||
| 1578 | // See if the device is low speed. |
||
| 1579 | if (!U1CONbits.JSTATE) |
||
| 1580 | { |
||
| 1581 | #ifdef DEBUG_MODE |
||
| 1582 | UART2PrintString( "HOST: Low Speed!\r\n" ); |
||
| 1583 | #endif |
||
| 1584 | usbDeviceInfo.flags.bfIsLowSpeed = 1; |
||
| 1585 | usbDeviceInfo.deviceAddressAndSpeed = 0x80; |
||
| 1586 | U1ADDR = 0x80; |
||
| 1587 | U1EP0bits.LSPD = 1; |
||
| 1588 | } |
||
| 1589 | |||
| 1590 | // Reset all ping-pong buffers if they are being used. |
||
| 1591 | U1CONbits.PPBRST = 1; |
||
| 1592 | U1CONbits.PPBRST = 0; |
||
| 1593 | usbDeviceInfo.flags.bfPingPongIn = 0; |
||
| 1594 | usbDeviceInfo.flags.bfPingPongOut = 0; |
||
| 1595 | |||
| 1596 | #ifdef USB_SUPPORT_OTG |
||
| 1597 | //Disable HNP |
||
| 1598 | USBOTGDisableHnp(); |
||
| 1599 | USBOTGDeactivateHnp(); |
||
| 1600 | #endif |
||
| 1601 | |||
| 1602 | // Assert reset for 10ms. Start a timer countdown. |
||
| 1603 | U1CONbits.USBRST = 1; |
||
| 1604 | numTimerInterrupts = USB_RESET_TIME; |
||
| 1605 | //U1OTGIRbits.T1MSECIF = 1; // The interrupt is cleared by writing a '1' to the flag. |
||
| 1606 | U1OTGIR = USB_INTERRUPT_T1MSECIF; // The interrupt is cleared by writing a '1' to the flag. |
||
| 1607 | U1OTGIEbits.T1MSECIE = 1; |
||
| 1608 | |||
| 1609 | _USB_SetNextSubSubState(); |
||
| 1610 | break; |
||
| 1611 | |||
| 1612 | case SUBSUBSTATE_RESET_WAIT: |
||
| 1613 | // Wait for the timer to finish in the background. |
||
| 1614 | break; |
||
| 1615 | |||
| 1616 | case SUBSUBSTATE_RESET_RECOVERY: |
||
| 1617 | #ifdef DEBUG_MODE |
||
| 1618 | UART2PrintString( "HOST: Reset complete.\r\n" ); |
||
| 1619 | #endif |
||
| 1620 | |||
| 1621 | // Deassert reset. |
||
| 1622 | U1CONbits.USBRST = 0; |
||
| 1623 | |||
| 1624 | // Start sending SOF's. |
||
| 1625 | U1CONbits.SOFEN = 1; |
||
| 1626 | |||
| 1627 | // Wait for the reset recovery time. |
||
| 1628 | numTimerInterrupts = USB_RESET_RECOVERY_TIME; |
||
| 1629 | U1OTGIR = USB_INTERRUPT_T1MSECIF; // The interrupt is cleared by writing a '1' to the flag. |
||
| 1630 | U1OTGIEbits.T1MSECIE = 1; |
||
| 1631 | |||
| 1632 | _USB_SetNextSubSubState(); |
||
| 1633 | break; |
||
| 1634 | |||
| 1635 | case SUBSUBSTATE_RECOVERY_WAIT: |
||
| 1636 | // Wait for the timer to finish in the background. |
||
| 1637 | break; |
||
| 1638 | |||
| 1639 | case SUBSUBSTATE_RESET_COMPLETE: |
||
| 1640 | #ifdef DEBUG_MODE |
||
| 1641 | UART2PrintString( "HOST: Reset complete.\r\n" ); |
||
| 1642 | #endif |
||
| 1643 | |||
| 1644 | // Enable USB interrupts |
||
| 1645 | U1IE = USB_INTERRUPT_TRANSFER | USB_INTERRUPT_SOF | USB_INTERRUPT_ERROR | USB_INTERRUPT_DETACH; |
||
| 1646 | U1EIE = 0xFF; |
||
| 1647 | |||
| 1648 | _USB_SetNextSubState(); |
||
| 1649 | break; |
||
| 1650 | |||
| 1651 | default: |
||
| 1652 | // We shouldn't get here. |
||
| 1653 | break; |
||
| 1654 | } |
||
| 1655 | break; |
||
| 1656 | |||
| 1657 | case SUBSTATE_GET_DEVICE_DESCRIPTOR_SIZE: |
||
| 1658 | // Send the GET DEVICE DESCRIPTOR command to get just the size |
||
| 1659 | // of the descriptor and the max packet size, so we can allocate |
||
| 1660 | // a large enough buffer for getting the whole thing and enough |
||
| 1661 | // buffer space for each piece. |
||
| 1662 | switch (usbHostState & SUBSUBSTATE_MASK) |
||
| 1663 | { |
||
| 1664 | case SUBSUBSTATE_SEND_GET_DEVICE_DESCRIPTOR_SIZE: |
||
| 1665 | #ifdef DEBUG_MODE |
||
| 1666 | UART2PrintString( "HOST: Getting Device Descriptor size.\r\n" ); |
||
| 1667 | #endif |
||
| 1668 | |||
| 1669 | // Set up and send GET DEVICE DESCRIPTOR |
||
| 1670 | if (pDeviceDescriptor != NULL) |
||
| 1671 | { |
||
| 1672 | USB_FREE_AND_CLEAR( pDeviceDescriptor ); |
||
| 1673 | } |
||
| 1674 | |||
| 1675 | pEP0Data[0] = USB_SETUP_DEVICE_TO_HOST | USB_SETUP_TYPE_STANDARD | USB_SETUP_RECIPIENT_DEVICE; |
||
| 1676 | pEP0Data[1] = USB_REQUEST_GET_DESCRIPTOR; |
||
| 1677 | pEP0Data[2] = 0; // Index |
||
| 1678 | pEP0Data[3] = USB_DESCRIPTOR_DEVICE; // Type |
||
| 1679 | pEP0Data[4] = 0; |
||
| 1680 | pEP0Data[5] = 0; |
||
| 1681 | pEP0Data[6] = 8; |
||
| 1682 | pEP0Data[7] = 0; |
||
| 1683 | |||
| 1684 | _USB_InitControlRead( usbDeviceInfo.pEndpoint0, pEP0Data, 8, pEP0Data, 8 ); |
||
| 1685 | _USB_SetNextSubSubState(); |
||
| 1686 | break; |
||
| 1687 | |||
| 1688 | case SUBSUBSTATE_WAIT_FOR_GET_DEVICE_DESCRIPTOR_SIZE: |
||
| 1689 | if (usbDeviceInfo.pEndpoint0->status.bfTransferComplete) |
||
| 1690 | { |
||
| 1691 | if (usbDeviceInfo.pEndpoint0->status.bfTransferSuccessful) |
||
| 1692 | { |
||
| 1693 | #ifndef USB_HUB_SUPPORT_INCLUDED |
||
| 1694 | // See if a hub is attached. Hubs are not supported. |
||
| 1695 | if (pEP0Data[4] == USB_HUB_CLASSCODE) // bDeviceClass |
||
| 1696 | { |
||
| 1697 | _USB_SetErrorCode( USB_HOLDING_UNSUPPORTED_HUB ); |
||
| 1698 | _USB_SetHoldState(); |
||
| 1699 | } |
||
| 1700 | else |
||
| 1701 | { |
||
| 1702 | _USB_SetNextSubSubState(); |
||
| 1703 | } |
||
| 1704 | #else |
||
| 1705 | _USB_SetNextSubSubState(); |
||
| 1706 | #endif |
||
| 1707 | } |
||
| 1708 | else |
||
| 1709 | { |
||
| 1710 | // We are here because of either a STALL or a NAK. See if |
||
| 1711 | // we have retries left to try the command again or try to |
||
| 1712 | // enumerate again. |
||
| 1713 | _USB_CheckCommandAndEnumerationAttempts(); |
||
| 1714 | } |
||
| 1715 | } |
||
| 1716 | break; |
||
| 1717 | |||
| 1718 | case SUBSUBSTATE_GET_DEVICE_DESCRIPTOR_SIZE_COMPLETE: |
||
| 1719 | // Allocate a buffer for the entire Device Descriptor |
||
| 1720 | if ((pDeviceDescriptor = (BYTE *)USB_MALLOC( *pEP0Data )) == NULL) |
||
| 1721 | { |
||
| 1722 | // We cannot continue. Freeze until the device is removed. |
||
| 1723 | _USB_SetErrorCode( USB_HOLDING_OUT_OF_MEMORY ); |
||
| 1724 | _USB_SetHoldState(); |
||
| 1725 | break; |
||
| 1726 | } |
||
| 1727 | // Save the descriptor size in the descriptor (bLength) |
||
| 1728 | *pDeviceDescriptor = *pEP0Data; |
||
| 1729 | |||
| 1730 | // Set the EP0 packet size. |
||
| 1731 | usbDeviceInfo.pEndpoint0->wMaxPacketSize = ((USB_DEVICE_DESCRIPTOR *)pEP0Data)->bMaxPacketSize0; |
||
| 1732 | |||
| 1733 | // Make our pEP0Data buffer the size of the max packet. |
||
| 1734 | USB_FREE_AND_CLEAR( pEP0Data ); |
||
| 1735 | if ((pEP0Data = (BYTE *)USB_MALLOC( usbDeviceInfo.pEndpoint0->wMaxPacketSize )) == NULL) |
||
| 1736 | { |
||
| 1737 | // We cannot continue. Freeze until the device is removed. |
||
| 1738 | #ifdef DEBUG_MODE |
||
| 1739 | UART2PrintString( "HOST: Error re-alloc-ing pEP0Data\r\n" ); |
||
| 1740 | #endif |
||
| 1741 | _USB_SetErrorCode( USB_HOLDING_OUT_OF_MEMORY ); |
||
| 1742 | _USB_SetHoldState(); |
||
| 1743 | break; |
||
| 1744 | } |
||
| 1745 | |||
| 1746 | // Clean up and advance to the next substate. |
||
| 1747 | _USB_InitErrorCounters(); |
||
| 1748 | _USB_SetNextSubState(); |
||
| 1749 | break; |
||
| 1750 | |||
| 1751 | default: |
||
| 1752 | break; |
||
| 1753 | } |
||
| 1754 | break; |
||
| 1755 | |||
| 1756 | case SUBSTATE_GET_DEVICE_DESCRIPTOR: |
||
| 1757 | // Send the GET DEVICE DESCRIPTOR command and receive the response |
||
| 1758 | switch (usbHostState & SUBSUBSTATE_MASK) |
||
| 1759 | { |
||
| 1760 | case SUBSUBSTATE_SEND_GET_DEVICE_DESCRIPTOR: |
||
| 1761 | #ifdef DEBUG_MODE |
||
| 1762 | UART2PrintString( "HOST: Getting device descriptor.\r\n" ); |
||
| 1763 | #endif |
||
| 1764 | |||
| 1765 | // If we are currently sending a token, we cannot do anything. |
||
| 1766 | if (usbBusInfo.flags.bfTokenAlreadyWritten) //(U1CONbits.TOKBUSY) |
||
| 1767 | break; |
||
| 1768 | |||
| 1769 | // Set up and send GET DEVICE DESCRIPTOR |
||
| 1770 | pEP0Data[0] = USB_SETUP_DEVICE_TO_HOST | USB_SETUP_TYPE_STANDARD | USB_SETUP_RECIPIENT_DEVICE; |
||
| 1771 | pEP0Data[1] = USB_REQUEST_GET_DESCRIPTOR; |
||
| 1772 | pEP0Data[2] = 0; // Index |
||
| 1773 | pEP0Data[3] = USB_DESCRIPTOR_DEVICE; // Type |
||
| 1774 | pEP0Data[4] = 0; |
||
| 1775 | pEP0Data[5] = 0; |
||
| 1776 | pEP0Data[6] = *pDeviceDescriptor; |
||
| 1777 | pEP0Data[7] = 0; |
||
| 1778 | _USB_InitControlRead( usbDeviceInfo.pEndpoint0, pEP0Data, 8, pDeviceDescriptor, *pDeviceDescriptor ); |
||
| 1779 | _USB_SetNextSubSubState(); |
||
| 1780 | break; |
||
| 1781 | |||
| 1782 | case SUBSUBSTATE_WAIT_FOR_GET_DEVICE_DESCRIPTOR: |
||
| 1783 | if (usbDeviceInfo.pEndpoint0->status.bfTransferComplete) |
||
| 1784 | { |
||
| 1785 | if (usbDeviceInfo.pEndpoint0->status.bfTransferSuccessful) |
||
| 1786 | { |
||
| 1787 | _USB_SetNextSubSubState(); |
||
| 1788 | } |
||
| 1789 | else |
||
| 1790 | { |
||
| 1791 | // We are here because of either a STALL or a NAK. See if |
||
| 1792 | // we have retries left to try the command again or try to |
||
| 1793 | // enumerate again. |
||
| 1794 | _USB_CheckCommandAndEnumerationAttempts(); |
||
| 1795 | } |
||
| 1796 | } |
||
| 1797 | break; |
||
| 1798 | |||
| 1799 | case SUBSUBSTATE_GET_DEVICE_DESCRIPTOR_COMPLETE: |
||
| 1800 | // Clean up and advance to the next substate. |
||
| 1801 | _USB_InitErrorCounters(); |
||
| 1802 | _USB_SetNextSubState(); |
||
| 1803 | break; |
||
| 1804 | |||
| 1805 | default: |
||
| 1806 | break; |
||
| 1807 | } |
||
| 1808 | break; |
||
| 1809 | |||
| 1810 | case SUBSTATE_VALIDATE_VID_PID: |
||
| 1811 | #ifdef DEBUG_MODE |
||
| 1812 | UART2PrintString( "HOST: Validating VID and PID.\r\n" ); |
||
| 1813 | #endif |
||
| 1814 | |||
| 1815 | // Search the TPL for the device's VID & PID. If a client driver is |
||
| 1816 | // available for the over-all device, use it. Otherwise, we'll search |
||
| 1817 | // again later for an appropriate class driver. |
||
| 1818 | _USB_FindDeviceLevelClientDriver(); |
||
| 1819 | |||
| 1820 | // Advance to the next state to assign an address to the device. |
||
| 1821 | // |
||
| 1822 | // Note: We assign an address to all devices and hold later if |
||
| 1823 | // we can't find a supported configuration. |
||
| 1824 | _USB_SetNextState(); |
||
| 1825 | break; |
||
| 1826 | } |
||
| 1827 | break; |
||
| 1828 | |||
| 1829 | case STATE_ADDRESSING: |
||
| 1830 | switch (usbHostState & SUBSTATE_MASK) |
||
| 1831 | { |
||
| 1832 | case SUBSTATE_SET_DEVICE_ADDRESS: |
||
| 1833 | // Send the SET ADDRESS command. We can't set the device address |
||
| 1834 | // in hardware until the entire transaction is complete. |
||
| 1835 | switch (usbHostState & SUBSUBSTATE_MASK) |
||
| 1836 | { |
||
| 1837 | case SUBSUBSTATE_SEND_SET_DEVICE_ADDRESS: |
||
| 1838 | #ifdef DEBUG_MODE |
||
| 1839 | UART2PrintString( "HOST: Setting device address.\r\n" ); |
||
| 1840 | #endif |
||
| 1841 | |||
| 1842 | // Select an address for the device. Store it so we can access it again |
||
| 1843 | // easily. We'll put the low speed indicator on later. |
||
| 1844 | // This has been broken out so when we allow multiple devices, we have |
||
| 1845 | // a single interface point to allocate a new address. |
||
| 1846 | usbDeviceInfo.deviceAddress = USB_SINGLE_DEVICE_ADDRESS; |
||
| 1847 | |||
| 1848 | // Set up and send SET ADDRESS |
||
| 1849 | pEP0Data[0] = USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_STANDARD | USB_SETUP_RECIPIENT_DEVICE; |
||
| 1850 | pEP0Data[1] = USB_REQUEST_SET_ADDRESS; |
||
| 1851 | pEP0Data[2] = usbDeviceInfo.deviceAddress; |
||
| 1852 | pEP0Data[3] = 0; |
||
| 1853 | pEP0Data[4] = 0; |
||
| 1854 | pEP0Data[5] = 0; |
||
| 1855 | pEP0Data[6] = 0; |
||
| 1856 | pEP0Data[7] = 0; |
||
| 1857 | _USB_InitControlWrite( usbDeviceInfo.pEndpoint0, pEP0Data, 8, NULL, 0 ); |
||
| 1858 | _USB_SetNextSubSubState(); |
||
| 1859 | break; |
||
| 1860 | |||
| 1861 | case SUBSUBSTATE_WAIT_FOR_SET_DEVICE_ADDRESS: |
||
| 1862 | if (usbDeviceInfo.pEndpoint0->status.bfTransferComplete) |
||
| 1863 | { |
||
| 1864 | if (usbDeviceInfo.pEndpoint0->status.bfTransferSuccessful) |
||
| 1865 | { |
||
| 1866 | _USB_SetNextSubSubState(); |
||
| 1867 | } |
||
| 1868 | else |
||
| 1869 | { |
||
| 1870 | // We are here because of either a STALL or a NAK. See if |
||
| 1871 | // we have retries left to try the command again or try to |
||
| 1872 | // enumerate again. |
||
| 1873 | _USB_CheckCommandAndEnumerationAttempts(); |
||
| 1874 | } |
||
| 1875 | } |
||
| 1876 | break; |
||
| 1877 | |||
| 1878 | case SUBSUBSTATE_SET_DEVICE_ADDRESS_COMPLETE: |
||
| 1879 | // Set the device's address here. |
||
| 1880 | usbDeviceInfo.deviceAddressAndSpeed = (usbDeviceInfo.flags.bfIsLowSpeed << 7) | usbDeviceInfo.deviceAddress; |
||
| 1881 | |||
| 1882 | // Clean up and advance to the next state. |
||
| 1883 | _USB_InitErrorCounters(); |
||
| 1884 | _USB_SetNextState(); |
||
| 1885 | break; |
||
| 1886 | |||
| 1887 | default: |
||
| 1888 | break; |
||
| 1889 | } |
||
| 1890 | break; |
||
| 1891 | } |
||
| 1892 | break; |
||
| 1893 | |||
| 1894 | case STATE_CONFIGURING: |
||
| 1895 | switch (usbHostState & SUBSTATE_MASK) |
||
| 1896 | { |
||
| 1897 | case SUBSTATE_INIT_CONFIGURATION: |
||
| 1898 | // Delete the old list of configuration descriptors and |
||
| 1899 | // initialize the counter. We will request the descriptors |
||
| 1900 | // from highest to lowest so the lowest will be first in |
||
| 1901 | // the list. |
||
| 1902 | countConfigurations = ((USB_DEVICE_DESCRIPTOR *)pDeviceDescriptor)->bNumConfigurations; |
||
| 1903 | while (usbDeviceInfo.pConfigurationDescriptorList != NULL) |
||
| 1904 | { |
||
| 1905 | pTemp = (BYTE *)usbDeviceInfo.pConfigurationDescriptorList->next; |
||
| 1906 | USB_FREE_AND_CLEAR( usbDeviceInfo.pConfigurationDescriptorList->descriptor ); |
||
| 1907 | USB_FREE_AND_CLEAR( usbDeviceInfo.pConfigurationDescriptorList ); |
||
| 1908 | usbDeviceInfo.pConfigurationDescriptorList = (USB_CONFIGURATION *)pTemp; |
||
| 1909 | } |
||
| 1910 | _USB_SetNextSubState(); |
||
| 1911 | break; |
||
| 1912 | |||
| 1913 | case SUBSTATE_GET_CONFIG_DESCRIPTOR_SIZE: |
||
| 1914 | // Get the size of the Configuration Descriptor for the current configuration |
||
| 1915 | switch (usbHostState & SUBSUBSTATE_MASK) |
||
| 1916 | { |
||
| 1917 | case SUBSUBSTATE_SEND_GET_CONFIG_DESCRIPTOR_SIZE: |
||
| 1918 | #ifdef DEBUG_MODE |
||
| 1919 | UART2PrintString( "HOST: Getting Config Descriptor size.\r\n" ); |
||
| 1920 | #endif |
||
| 1921 | |||
| 1922 | // Set up and send GET CONFIGURATION (n) DESCRIPTOR with a length of 8 |
||
| 1923 | pEP0Data[0] = USB_SETUP_DEVICE_TO_HOST | USB_SETUP_TYPE_STANDARD | USB_SETUP_RECIPIENT_DEVICE; |
||
| 1924 | pEP0Data[1] = USB_REQUEST_GET_DESCRIPTOR; |
||
| 1925 | pEP0Data[2] = countConfigurations-1; // USB 2.0 - range is 0 - count-1 |
||
| 1926 | pEP0Data[3] = USB_DESCRIPTOR_CONFIGURATION; |
||
| 1927 | pEP0Data[4] = 0; |
||
| 1928 | pEP0Data[5] = 0; |
||
| 1929 | pEP0Data[6] = 8; |
||
| 1930 | pEP0Data[7] = 0; |
||
| 1931 | _USB_InitControlRead( usbDeviceInfo.pEndpoint0, pEP0Data, 8, pEP0Data, 8 ); |
||
| 1932 | _USB_SetNextSubSubState(); |
||
| 1933 | break; |
||
| 1934 | |||
| 1935 | case SUBSUBSTATE_WAIT_FOR_GET_CONFIG_DESCRIPTOR_SIZE: |
||
| 1936 | if (usbDeviceInfo.pEndpoint0->status.bfTransferComplete) |
||
| 1937 | { |
||
| 1938 | if (usbDeviceInfo.pEndpoint0->status.bfTransferSuccessful) |
||
| 1939 | { |
||
| 1940 | _USB_SetNextSubSubState(); |
||
| 1941 | } |
||
| 1942 | else |
||
| 1943 | { |
||
| 1944 | // We are here because of either a STALL or a NAK. See if |
||
| 1945 | // we have retries left to try the command again or try to |
||
| 1946 | // enumerate again. |
||
| 1947 | _USB_CheckCommandAndEnumerationAttempts(); |
||
| 1948 | } |
||
| 1949 | } |
||
| 1950 | break; |
||
| 1951 | |||
| 1952 | case SUBSUBSTATE_GET_CONFIG_DESCRIPTOR_SIZECOMPLETE: |
||
| 1953 | // Allocate a buffer for an entry in the configuration descriptor list. |
||
| 1954 | if ((pTemp = (BYTE *)USB_MALLOC( sizeof (USB_CONFIGURATION) )) == NULL) |
||
| 1955 | { |
||
| 1956 | // We cannot continue. Freeze until the device is removed. |
||
| 1957 | _USB_SetErrorCode( USB_HOLDING_OUT_OF_MEMORY ); |
||
| 1958 | _USB_SetHoldState(); |
||
| 1959 | break; |
||
| 1960 | } |
||
| 1961 | |||
| 1962 | // Allocate a buffer for the entire Configuration Descriptor |
||
| 1963 | if ((((USB_CONFIGURATION *)pTemp)->descriptor = (BYTE *)USB_MALLOC( ((WORD)pEP0Data[3] << 8) + (WORD)pEP0Data[2] )) == NULL) |
||
| 1964 | { |
||
| 1965 | // Not enough memory for the descriptor! |
||
| 1966 | USB_FREE_AND_CLEAR( pTemp ); |
||
| 1967 | |||
| 1968 | // We cannot continue. Freeze until the device is removed. |
||
| 1969 | _USB_SetErrorCode( USB_HOLDING_OUT_OF_MEMORY ); |
||
| 1970 | _USB_SetHoldState(); |
||
| 1971 | break; |
||
| 1972 | } |
||
| 1973 | |||
| 1974 | // Save wTotalLength |
||
| 1975 | ((USB_CONFIGURATION_DESCRIPTOR *)((USB_CONFIGURATION *)pTemp)->descriptor)->wTotalLength = |
||
| 1976 | ((WORD)pEP0Data[3] << 8) + (WORD)pEP0Data[2]; |
||
| 1977 | |||
| 1978 | // Put the new node at the front of the list. |
||
| 1979 | ((USB_CONFIGURATION *)pTemp)->next = usbDeviceInfo.pConfigurationDescriptorList; |
||
| 1980 | usbDeviceInfo.pConfigurationDescriptorList = (USB_CONFIGURATION *)pTemp; |
||
| 1981 | |||
| 1982 | // Save the configuration descriptor pointer and number |
||
| 1983 | pCurrentConfigurationDescriptor = ((USB_CONFIGURATION *)pTemp)->descriptor; |
||
| 1984 | ((USB_CONFIGURATION *)pTemp)->configNumber = countConfigurations; |
||
| 1985 | |||
| 1986 | // Clean up and advance to the next state. |
||
| 1987 | _USB_InitErrorCounters(); |
||
| 1988 | _USB_SetNextSubState(); |
||
| 1989 | break; |
||
| 1990 | |||
| 1991 | default: |
||
| 1992 | break; |
||
| 1993 | } |
||
| 1994 | break; |
||
| 1995 | |||
| 1996 | case SUBSTATE_GET_CONFIG_DESCRIPTOR: |
||
| 1997 | // Get the entire Configuration Descriptor for this configuration |
||
| 1998 | switch (usbHostState & SUBSUBSTATE_MASK) |
||
| 1999 | { |
||
| 2000 | case SUBSUBSTATE_SEND_GET_CONFIG_DESCRIPTOR: |
||
| 2001 | #ifdef DEBUG_MODE |
||
| 2002 | UART2PrintString( "HOST: Getting Config Descriptor.\r\n" ); |
||
| 2003 | #endif |
||
| 2004 | |||
| 2005 | // Set up and send GET CONFIGURATION (n) DESCRIPTOR. |
||
| 2006 | pEP0Data[0] = USB_SETUP_DEVICE_TO_HOST | USB_SETUP_TYPE_STANDARD | USB_SETUP_RECIPIENT_DEVICE; |
||
| 2007 | pEP0Data[1] = USB_REQUEST_GET_DESCRIPTOR; |
||
| 2008 | pEP0Data[2] = countConfigurations-1; |
||
| 2009 | pEP0Data[3] = USB_DESCRIPTOR_CONFIGURATION; |
||
| 2010 | pEP0Data[4] = 0; |
||
| 2011 | pEP0Data[5] = 0; |
||
| 2012 | pEP0Data[6] = usbDeviceInfo.pConfigurationDescriptorList->descriptor[2]; // wTotalLength |
||
| 2013 | pEP0Data[7] = usbDeviceInfo.pConfigurationDescriptorList->descriptor[3]; |
||
| 2014 | _USB_InitControlRead( usbDeviceInfo.pEndpoint0, pEP0Data, 8, usbDeviceInfo.pConfigurationDescriptorList->descriptor, |
||
| 2015 | ((USB_CONFIGURATION_DESCRIPTOR *)usbDeviceInfo.pConfigurationDescriptorList->descriptor)->wTotalLength ); |
||
| 2016 | _USB_SetNextSubSubState(); |
||
| 2017 | break; |
||
| 2018 | |||
| 2019 | case SUBSUBSTATE_WAIT_FOR_GET_CONFIG_DESCRIPTOR: |
||
| 2020 | if (usbDeviceInfo.pEndpoint0->status.bfTransferComplete) |
||
| 2021 | { |
||
| 2022 | if (usbDeviceInfo.pEndpoint0->status.bfTransferSuccessful) |
||
| 2023 | { |
||
| 2024 | _USB_SetNextSubSubState(); |
||
| 2025 | } |
||
| 2026 | else |
||
| 2027 | { |
||
| 2028 | // We are here because of either a STALL or a NAK. See if |
||
| 2029 | // we have retries left to try the command again or try to |
||
| 2030 | // enumerate again. |
||
| 2031 | _USB_CheckCommandAndEnumerationAttempts(); |
||
| 2032 | } |
||
| 2033 | } |
||
| 2034 | break; |
||
| 2035 | |||
| 2036 | case SUBSUBSTATE_GET_CONFIG_DESCRIPTOR_COMPLETE: |
||
| 2037 | // Clean up and advance to the next state. Keep the data for later use. |
||
| 2038 | _USB_InitErrorCounters(); |
||
| 2039 | countConfigurations --; |
||
| 2040 | if (countConfigurations) |
||
| 2041 | { |
||
| 2042 | // There are more descriptors that we need to get. |
||
| 2043 | usbHostState = STATE_CONFIGURING | SUBSTATE_GET_CONFIG_DESCRIPTOR_SIZE; |
||
| 2044 | } |
||
| 2045 | else |
||
| 2046 | { |
||
| 2047 | // Start configuring the device. |
||
| 2048 | _USB_SetNextSubState(); |
||
| 2049 | } |
||
| 2050 | break; |
||
| 2051 | |||
| 2052 | default: |
||
| 2053 | break; |
||
| 2054 | } |
||
| 2055 | break; |
||
| 2056 | |||
| 2057 | case SUBSTATE_SELECT_CONFIGURATION: |
||
| 2058 | // Set the OTG configuration of the device |
||
| 2059 | switch (usbHostState & SUBSUBSTATE_MASK) |
||
| 2060 | { |
||
| 2061 | case SUBSUBSTATE_SELECT_CONFIGURATION: |
||
| 2062 | // Free the old configuration (if any) |
||
| 2063 | _USB_FreeConfigMemory(); |
||
| 2064 | |||
| 2065 | // If the configuration wasn't selected based on the VID & PID |
||
| 2066 | if (usbDeviceInfo.currentConfiguration == 0) |
||
| 2067 | { |
||
| 2068 | // Search for a supported class-specific configuration. |
||
| 2069 | pCurrentConfigurationNode = usbDeviceInfo.pConfigurationDescriptorList; |
||
| 2070 | while (pCurrentConfigurationNode) |
||
| 2071 | { |
||
| 2072 | pCurrentConfigurationDescriptor = pCurrentConfigurationNode->descriptor; |
||
| 2073 | if (_USB_ParseConfigurationDescriptor()) |
||
| 2074 | { |
||
| 2075 | break; |
||
| 2076 | } |
||
| 2077 | else |
||
| 2078 | { |
||
| 2079 | // Free the memory allocated and |
||
| 2080 | // advance to next configuration |
||
| 2081 | _USB_FreeConfigMemory(); |
||
| 2082 | pCurrentConfigurationNode = pCurrentConfigurationNode->next; |
||
| 2083 | } |
||
| 2084 | } |
||
| 2085 | } |
||
| 2086 | else |
||
| 2087 | { |
||
| 2088 | // Configuration selected by VID & PID, initialize data structures |
||
| 2089 | pCurrentConfigurationNode = usbDeviceInfo.pConfigurationDescriptorList; |
||
| 2090 | while (pCurrentConfigurationNode && pCurrentConfigurationNode->configNumber != usbDeviceInfo.currentConfiguration) |
||
| 2091 | { |
||
| 2092 | pCurrentConfigurationNode = pCurrentConfigurationNode->next; |
||
| 2093 | } |
||
| 2094 | pCurrentConfigurationDescriptor = pCurrentConfigurationNode->descriptor; |
||
| 2095 | if (!_USB_ParseConfigurationDescriptor()) |
||
| 2096 | { |
||
| 2097 | // Free the memory allocated, config attempt failed. |
||
| 2098 | _USB_FreeConfigMemory(); |
||
| 2099 | pCurrentConfigurationNode = NULL; |
||
| 2100 | } |
||
| 2101 | } |
||
| 2102 | |||
| 2103 | //If No OTG Then |
||
| 2104 | if (usbDeviceInfo.flags.bfConfiguredOTG) |
||
| 2105 | { |
||
| 2106 | // Did we fail to configure? |
||
| 2107 | if (pCurrentConfigurationNode == NULL) |
||
| 2108 | { |
||
| 2109 | // Failed to find a supported configuration. |
||
| 2110 | _USB_SetErrorCode( USB_HOLDING_UNSUPPORTED_DEVICE ); |
||
| 2111 | _USB_SetHoldState(); |
||
| 2112 | } |
||
| 2113 | else |
||
| 2114 | { |
||
| 2115 | _USB_SetNextSubSubState(); |
||
| 2116 | } |
||
| 2117 | } |
||
| 2118 | else |
||
| 2119 | { |
||
| 2120 | _USB_SetNextSubSubState(); |
||
| 2121 | } |
||
| 2122 | break; |
||
| 2123 | |||
| 2124 | case SUBSUBSTATE_SEND_SET_OTG: |
||
| 2125 | #ifdef DEBUG_MODE |
||
| 2126 | UART2PrintString( "HOST: Determine OTG capability.\r\n" ); |
||
| 2127 | #endif |
||
| 2128 | |||
| 2129 | // If the device does not support OTG, or |
||
| 2130 | // if the device has already been configured, bail. |
||
| 2131 | // Otherwise, send SET FEATURE to configure it. |
||
| 2132 | if (!usbDeviceInfo.flags.bfConfiguredOTG) |
||
| 2133 | { |
||
| 2134 | #ifdef DEBUG_MODE |
||
| 2135 | UART2PrintString( "HOST: ...OTG needs configuring.\r\n" ); |
||
| 2136 | #endif |
||
| 2137 | usbDeviceInfo.flags.bfConfiguredOTG = 1; |
||
| 2138 | |||
| 2139 | // Send SET FEATURE |
||
| 2140 | pEP0Data[0] = USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_STANDARD | USB_SETUP_RECIPIENT_DEVICE; |
||
| 2141 | pEP0Data[1] = USB_REQUEST_SET_FEATURE; |
||
| 2142 | if (usbDeviceInfo.flags.bfAllowHNP) // Needs to be set by the user |
||
| 2143 | { |
||
| 2144 | pEP0Data[2] = OTG_FEATURE_B_HNP_ENABLE; |
||
| 2145 | } |
||
| 2146 | else |
||
| 2147 | { |
||
| 2148 | pEP0Data[2] = OTG_FEATURE_A_HNP_SUPPORT; |
||
| 2149 | } |
||
| 2150 | pEP0Data[3] = 0; |
||
| 2151 | pEP0Data[4] = 0; |
||
| 2152 | pEP0Data[5] = 0; |
||
| 2153 | pEP0Data[6] = 0; |
||
| 2154 | pEP0Data[7] = 0; |
||
| 2155 | _USB_InitControlWrite( usbDeviceInfo.pEndpoint0, pEP0Data, 8, NULL, 0 ); |
||
| 2156 | _USB_SetNextSubSubState(); |
||
| 2157 | } |
||
| 2158 | else |
||
| 2159 | { |
||
| 2160 | #ifdef DEBUG_MODE |
||
| 2161 | UART2PrintString( "HOST: ...No OTG.\r\n" ); |
||
| 2162 | #endif |
||
| 2163 | _USB_SetNextSubState(); |
||
| 2164 | } |
||
| 2165 | break; |
||
| 2166 | |||
| 2167 | case SUBSUBSTATE_WAIT_FOR_SET_OTG_DONE: |
||
| 2168 | if (usbDeviceInfo.pEndpoint0->status.bfTransferComplete) |
||
| 2169 | { |
||
| 2170 | if (usbDeviceInfo.pEndpoint0->status.bfTransferSuccessful) |
||
| 2171 | { |
||
| 2172 | #ifdef USB_SUPPORT_OTG |
||
| 2173 | if (usbDeviceInfo.flags.bfAllowHNP) |
||
| 2174 | { |
||
| 2175 | USBOTGEnableHnp(); |
||
| 2176 | } |
||
| 2177 | #endif |
||
| 2178 | _USB_SetNextSubSubState(); |
||
| 2179 | } |
||
| 2180 | else |
||
| 2181 | { |
||
| 2182 | #ifdef USB_SUPPORT_OTG |
||
| 2183 | USBOTGDisableHnp(); |
||
| 2184 | #endif |
||
| 2185 | // We are here because of either a STALL or a NAK. See if |
||
| 2186 | // we have retries left to try the command again or try to |
||
| 2187 | // enumerate again. |
||
| 2188 | _USB_CheckCommandAndEnumerationAttempts(); |
||
| 2189 | |||
| 2190 | #if defined(DEBUG_MODE) && defined(USB_SUPPORT_OTG) |
||
| 2191 | UART2PrintString( "\r\n***** USB OTG Error - Set Feature B_HNP_ENABLE Stalled - Device Not Responding *****\r\n" ); |
||
| 2192 | #endif |
||
| 2193 | } |
||
| 2194 | } |
||
| 2195 | break; |
||
| 2196 | |||
| 2197 | case SUBSUBSTATE_SET_OTG_COMPLETE: |
||
| 2198 | // Clean up and advance to the next state. |
||
| 2199 | _USB_InitErrorCounters(); |
||
| 2200 | |||
| 2201 | //MR - Moved For OTG Set Feature Support For Unsupported Devices |
||
| 2202 | // Did we fail to configure? |
||
| 2203 | if (pCurrentConfigurationNode == NULL) |
||
| 2204 | { |
||
| 2205 | // Failed to find a supported configuration. |
||
| 2206 | _USB_SetErrorCode( USB_HOLDING_UNSUPPORTED_DEVICE ); |
||
| 2207 | _USB_SetHoldState(); |
||
| 2208 | } |
||
| 2209 | else |
||
| 2210 | { |
||
| 2211 | //_USB_SetNextSubSubState(); |
||
| 2212 | _USB_InitErrorCounters(); |
||
| 2213 | _USB_SetNextSubState(); |
||
| 2214 | } |
||
| 2215 | break; |
||
| 2216 | |||
| 2217 | default: |
||
| 2218 | break; |
||
| 2219 | } |
||
| 2220 | break; |
||
| 2221 | |||
| 2222 | case SUBSTATE_SET_CONFIGURATION: |
||
| 2223 | // Set the configuration to the one specified for this device |
||
| 2224 | switch (usbHostState & SUBSUBSTATE_MASK) |
||
| 2225 | { |
||
| 2226 | case SUBSUBSTATE_SEND_SET_CONFIGURATION: |
||
| 2227 | #ifdef DEBUG_MODE |
||
| 2228 | UART2PrintString( "HOST: Set configuration.\r\n" ); |
||
| 2229 | #endif |
||
| 2230 | |||
| 2231 | // Set up and send SET CONFIGURATION. |
||
| 2232 | pEP0Data[0] = USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_STANDARD | USB_SETUP_RECIPIENT_DEVICE; |
||
| 2233 | pEP0Data[1] = USB_REQUEST_SET_CONFIGURATION; |
||
| 2234 | pEP0Data[2] = usbDeviceInfo.currentConfiguration; |
||
| 2235 | pEP0Data[3] = 0; |
||
| 2236 | pEP0Data[4] = 0; |
||
| 2237 | pEP0Data[5] = 0; |
||
| 2238 | pEP0Data[6] = 0; |
||
| 2239 | pEP0Data[7] = 0; |
||
| 2240 | _USB_InitControlWrite( usbDeviceInfo.pEndpoint0, pEP0Data, 8, NULL, 0 ); |
||
| 2241 | _USB_SetNextSubSubState(); |
||
| 2242 | break; |
||
| 2243 | |||
| 2244 | case SUBSUBSTATE_WAIT_FOR_SET_CONFIGURATION: |
||
| 2245 | if (usbDeviceInfo.pEndpoint0->status.bfTransferComplete) |
||
| 2246 | { |
||
| 2247 | if (usbDeviceInfo.pEndpoint0->status.bfTransferSuccessful) |
||
| 2248 | { |
||
| 2249 | _USB_SetNextSubSubState(); |
||
| 2250 | } |
||
| 2251 | else |
||
| 2252 | { |
||
| 2253 | // We are here because of either a STALL or a NAK. See if |
||
| 2254 | // we have retries left to try the command again or try to |
||
| 2255 | // enumerate again. |
||
| 2256 | _USB_CheckCommandAndEnumerationAttempts(); |
||
| 2257 | } |
||
| 2258 | } |
||
| 2259 | break; |
||
| 2260 | |||
| 2261 | case SUBSUBSTATE_SET_CONFIGURATION_COMPLETE: |
||
| 2262 | // Clean up and advance to the next state. |
||
| 2263 | _USB_InitErrorCounters(); |
||
| 2264 | _USB_SetNextSubSubState(); |
||
| 2265 | break; |
||
| 2266 | |||
| 2267 | case SUBSUBSTATE_INIT_CLIENT_DRIVERS: |
||
| 2268 | #ifdef DEBUG_MODE |
||
| 2269 | UART2PrintString( "HOST: Initializing client drivers...\r\n" ); |
||
| 2270 | #endif |
||
| 2271 | _USB_SetNextState(); |
||
| 2272 | // Initialize client driver(s) for this configuration. |
||
| 2273 | if (usbDeviceInfo.flags.bfUseDeviceClientDriver) |
||
| 2274 | { |
||
| 2275 | // We have a device that requires only one client driver. Make sure |
||
| 2276 | // that client driver can initialize this device. If the client |
||
| 2277 | // driver initialization fails, we cannot enumerate this device. |
||
| 2278 | #ifdef DEBUG_MODE |
||
| 2279 | UART2PrintString( "HOST: Using device client driver.\r\n" ); |
||
| 2280 | #endif |
||
| 2281 | temp = usbDeviceInfo.deviceClientDriver; |
||
| 2282 | if (!usbClientDrvTable[temp].Initialize(usbDeviceInfo.deviceAddress, usbClientDrvTable[temp].flags, temp)) |
||
| 2283 | { |
||
| 2284 | _USB_SetErrorCode( USB_HOLDING_CLIENT_INIT_ERROR ); |
||
| 2285 | _USB_SetHoldState(); |
||
| 2286 | } |
||
| 2287 | } |
||
| 2288 | else |
||
| 2289 | { |
||
| 2290 | // We have a device that requires multiple client drivers. Make sure |
||
| 2291 | // every required client driver can initialize this device. If any |
||
| 2292 | // client driver initialization fails, we cannot enumerate the device. |
||
| 2293 | #ifdef DEBUG_MODE |
||
| 2294 | UART2PrintString( "HOST: Scanning interfaces.\r\n" ); |
||
| 2295 | #endif |
||
| 2296 | pCurrentInterface = usbDeviceInfo.pInterfaceList; |
||
| 2297 | while (pCurrentInterface) |
||
| 2298 | { |
||
| 2299 | temp = pCurrentInterface->clientDriver; |
||
| 2300 | if (!usbClientDrvTable[temp].Initialize(usbDeviceInfo.deviceAddress, usbClientDrvTable[temp].flags, temp)) |
||
| 2301 | { |
||
| 2302 | _USB_SetErrorCode( USB_HOLDING_CLIENT_INIT_ERROR ); |
||
| 2303 | _USB_SetHoldState(); |
||
| 2304 | } |
||
| 2305 | pCurrentInterface = pCurrentInterface->next; |
||
| 2306 | } |
||
| 2307 | } |
||
| 2308 | break; |
||
| 2309 | |||
| 2310 | default: |
||
| 2311 | break; |
||
| 2312 | } |
||
| 2313 | break; |
||
| 2314 | } |
||
| 2315 | break; |
||
| 2316 | |||
| 2317 | case STATE_RUNNING: |
||
| 2318 | switch (usbHostState & SUBSTATE_MASK) |
||
| 2319 | { |
||
| 2320 | case SUBSTATE_NORMAL_RUN: |
||
| 2321 | break; |
||
| 2322 | |||
| 2323 | case SUBSTATE_SUSPEND_AND_RESUME: |
||
| 2324 | switch (usbHostState & SUBSUBSTATE_MASK) |
||
| 2325 | { |
||
| 2326 | case SUBSUBSTATE_SUSPEND: |
||
| 2327 | // The IDLE state has already been set. We need to wait here |
||
| 2328 | // until the application decides to RESUME. |
||
| 2329 | break; |
||
| 2330 | |||
| 2331 | case SUBSUBSTATE_RESUME: |
||
| 2332 | // Issue a RESUME. |
||
| 2333 | U1CONbits.RESUME = 1; |
||
| 2334 | |||
| 2335 | // Wait for the RESUME time. |
||
| 2336 | numTimerInterrupts = USB_RESUME_TIME; |
||
| 2337 | U1OTGIR = USB_INTERRUPT_T1MSECIF; // The interrupt is cleared by writing a '1' to the flag. |
||
| 2338 | U1OTGIEbits.T1MSECIE = 1; |
||
| 2339 | |||
| 2340 | _USB_SetNextSubSubState(); |
||
| 2341 | break; |
||
| 2342 | |||
| 2343 | case SUBSUBSTATE_RESUME_WAIT: |
||
| 2344 | // Wait here until the timer expires. |
||
| 2345 | break; |
||
| 2346 | |||
| 2347 | case SUBSUBSTATE_RESUME_RECOVERY: |
||
| 2348 | // Turn off RESUME. |
||
| 2349 | U1CONbits.RESUME = 0; |
||
| 2350 | |||
| 2351 | // Start sending SOF's, so the device doesn't go back into the SUSPEND state. |
||
| 2352 | U1CONbits.SOFEN = 1; |
||
| 2353 | |||
| 2354 | // Wait for the RESUME recovery time. |
||
| 2355 | numTimerInterrupts = USB_RESUME_RECOVERY_TIME; |
||
| 2356 | U1OTGIR = USB_INTERRUPT_T1MSECIF; // The interrupt is cleared by writing a '1' to the flag. |
||
| 2357 | U1OTGIEbits.T1MSECIE = 1; |
||
| 2358 | |||
| 2359 | _USB_SetNextSubSubState(); |
||
| 2360 | break; |
||
| 2361 | |||
| 2362 | case SUBSUBSTATE_RESUME_RECOVERY_WAIT: |
||
| 2363 | // Wait here until the timer expires. |
||
| 2364 | break; |
||
| 2365 | |||
| 2366 | case SUBSUBSTATE_RESUME_COMPLETE: |
||
| 2367 | // Go back to normal running. |
||
| 2368 | usbHostState = STATE_RUNNING | SUBSTATE_NORMAL_RUN; |
||
| 2369 | break; |
||
| 2370 | } |
||
| 2371 | } |
||
| 2372 | break; |
||
| 2373 | |||
| 2374 | case STATE_HOLDING: |
||
| 2375 | switch (usbHostState & SUBSTATE_MASK) |
||
| 2376 | { |
||
| 2377 | case SUBSTATE_HOLD_INIT: |
||
| 2378 | // We're here because we cannot communicate with the current device |
||
| 2379 | // that is plugged in. Turn off SOF's and all interrupts except |
||
| 2380 | // the DETACH interrupt. |
||
| 2381 | #ifdef DEBUG_MODE |
||
| 2382 | UART2PrintString( "HOST: Holding.\r\n" ); |
||
| 2383 | #endif |
||
| 2384 | U1CON = USB_HOST_MODE_ENABLE | USB_SOF_DISABLE; // Turn of SOF's to cut down noise |
||
| 2385 | U1IE = 0; |
||
| 2386 | U1IR = 0xFF; |
||
| 2387 | U1OTGIE &= 0x8C; |
||
| 2388 | U1OTGIR = 0x7D; |
||
| 2389 | U1EIE = 0; |
||
| 2390 | U1EIR = 0xFF; |
||
| 2391 | U1IEbits.DETACHIE = 1; |
||
| 2392 | |||
| 2393 | switch (usbDeviceInfo.errorCode ) |
||
| 2394 | { |
||
| 2395 | case USB_HOLDING_UNSUPPORTED_HUB: |
||
| 2396 | temp = EVENT_HUB_ATTACH; |
||
| 2397 | break; |
||
| 2398 | |||
| 2399 | case USB_HOLDING_UNSUPPORTED_DEVICE: |
||
| 2400 | temp = EVENT_UNSUPPORTED_DEVICE; |
||
| 2401 | |||
| 2402 | #ifdef USB_SUPPORT_OTG |
||
| 2403 | //Abort HNP |
||
| 2404 | USB_OTGEventHandler (0, OTG_EVENT_HNP_ABORT , 0, 0 ); |
||
| 2405 | #endif |
||
| 2406 | |||
| 2407 | break; |
||
| 2408 | |||
| 2409 | case USB_CANNOT_ENUMERATE: |
||
| 2410 | temp = EVENT_CANNOT_ENUMERATE; |
||
| 2411 | break; |
||
| 2412 | |||
| 2413 | case USB_HOLDING_CLIENT_INIT_ERROR: |
||
| 2414 | temp = EVENT_CLIENT_INIT_ERROR; |
||
| 2415 | break; |
||
| 2416 | |||
| 2417 | case USB_HOLDING_OUT_OF_MEMORY: |
||
| 2418 | temp = EVENT_OUT_OF_MEMORY; |
||
| 2419 | break; |
||
| 2420 | |||
| 2421 | default: |
||
| 2422 | temp = EVENT_UNSPECIFIED_ERROR; // This should never occur |
||
| 2423 | break; |
||
| 2424 | } |
||
| 2425 | |||
| 2426 | // Report the problem to the application. |
||
| 2427 | USB_HOST_APP_EVENT_HANDLER( usbDeviceInfo.deviceAddress, temp, &usbDeviceInfo.currentConfigurationPower , 1 ); |
||
| 2428 | |||
| 2429 | _USB_SetNextSubState(); |
||
| 2430 | break; |
||
| 2431 | |||
| 2432 | case SUBSTATE_HOLD: |
||
| 2433 | // Hold here until a DETACH interrupt frees us. |
||
| 2434 | break; |
||
| 2435 | |||
| 2436 | default: |
||
| 2437 | break; |
||
| 2438 | } |
||
| 2439 | break; |
||
| 2440 | } |
||
| 2441 | |||
| 2442 | } |
||
| 2443 | |||
| 2444 | /**************************************************************************** |
||
| 2445 | Function: |
||
| 2446 | void USBHostTerminateTransfer( BYTE deviceAddress, BYTE endpoint ) |
||
| 2447 | |||
| 2448 | |||
| 2449 | Summary: |
||
| 2450 | This function terminates the current transfer for the given endpoint. |
||
| 2451 | |||
| 2452 | Description: |
||
| 2453 | This function terminates the current transfer for the given endpoint. It |
||
| 2454 | can be used to terminate reads or writes that the device is not |
||
| 2455 | responding to. It is also the only way to terminate an isochronous |
||
| 2456 | transfer. |
||
| 2457 | |||
| 2458 | Precondition: |
||
| 2459 | None |
||
| 2460 | |||
| 2461 | Parameters: |
||
| 2462 | BYTE deviceAddress - Device address |
||
| 2463 | BYTE endpoint - Endpoint number |
||
| 2464 | |||
| 2465 | Returns: |
||
| 2466 | None |
||
| 2467 | |||
| 2468 | Remarks: |
||
| 2469 | None |
||
| 2470 | ***************************************************************************/ |
||
| 2471 | |||
| 2472 | void USBHostTerminateTransfer( BYTE deviceAddress, BYTE endpoint ) |
||
| 2473 | { |
||
| 2474 | USB_ENDPOINT_INFO *ep; |
||
| 2475 | |||
| 2476 | // Find the required device |
||
| 2477 | if (deviceAddress != usbDeviceInfo.deviceAddress) |
||
| 2478 | { |
||
| 2479 | return; // USB_UNKNOWN_DEVICE; |
||
| 2480 | } |
||
| 2481 | |||
| 2482 | ep = _USB_FindEndpoint( endpoint ); |
||
| 2483 | if (ep != NULL) |
||
| 2484 | { |
||
| 2485 | ep->status.bfUserAbort = 1; |
||
| 2486 | ep->status.bfTransferComplete = 1; |
||
| 2487 | } |
||
| 2488 | } |
||
| 2489 | |||
| 2490 | /**************************************************************************** |
||
| 2491 | Function: |
||
| 2492 | BOOL USBHostTransferIsComplete( BYTE deviceAddress, BYTE endpoint, |
||
| 2493 | BYTE *errorCode, DWORD *byteCount ) |
||
| 2494 | |||
| 2495 | Summary: |
||
| 2496 | This function initiates whether or not the last endpoint transaction is |
||
| 2497 | complete. |
||
| 2498 | |||
| 2499 | Description: |
||
| 2500 | This function initiates whether or not the last endpoint transaction is |
||
| 2501 | complete. If it is complete, an error code and the number of bytes |
||
| 2502 | transferred are returned. |
||
| 2503 | |||
| 2504 | For isochronous transfers, byteCount is not valid. Instead, use the |
||
| 2505 | returned byte counts for each EVENT_TRANSFER event that was generated |
||
| 2506 | during the transfer. |
||
| 2507 | |||
| 2508 | Precondition: |
||
| 2509 | None |
||
| 2510 | |||
| 2511 | Parameters: |
||
| 2512 | BYTE deviceAddress - Device address |
||
| 2513 | BYTE endpoint - Endpoint number |
||
| 2514 | BYTE *errorCode - Error code indicating the status of the transfer. |
||
| 2515 | Only valid if the transfer is complete. |
||
| 2516 | DWORD *byteCount - The number of bytes sent or received. Invalid |
||
| 2517 | for isochronous transfers. |
||
| 2518 | |||
| 2519 | Return Values: |
||
| 2520 | TRUE - Transfer is complete. |
||
| 2521 | FALSE - Transfer is not complete. |
||
| 2522 | |||
| 2523 | Remarks: |
||
| 2524 | Possible values for errorCode are: |
||
| 2525 | * USB_SUCCESS - Transfer successful |
||
| 2526 | * USB_UNKNOWN_DEVICE - Device not attached |
||
| 2527 | * USB_ENDPOINT_STALLED - Endpoint STALL'd |
||
| 2528 | * USB_ENDPOINT_ERROR_ILLEGAL_PID - Illegal PID returned |
||
| 2529 | * USB_ENDPOINT_ERROR_BIT_STUFF |
||
| 2530 | * USB_ENDPOINT_ERROR_DMA |
||
| 2531 | * USB_ENDPOINT_ERROR_TIMEOUT |
||
| 2532 | * USB_ENDPOINT_ERROR_DATA_FIELD |
||
| 2533 | * USB_ENDPOINT_ERROR_CRC16 |
||
| 2534 | * USB_ENDPOINT_ERROR_END_OF_FRAME |
||
| 2535 | * USB_ENDPOINT_ERROR_PID_CHECK |
||
| 2536 | * USB_ENDPOINT_ERROR - Other error |
||
| 2537 | ***************************************************************************/ |
||
| 2538 | |||
| 2539 | BOOL USBHostTransferIsComplete( BYTE deviceAddress, BYTE endpoint, BYTE *errorCode, |
||
| 2540 | DWORD *byteCount ) |
||
| 2541 | { |
||
| 2542 | USB_ENDPOINT_INFO *ep; |
||
| 2543 | BYTE transferComplete; |
||
| 2544 | |||
| 2545 | // Find the required device |
||
| 2546 | if (deviceAddress != usbDeviceInfo.deviceAddress) |
||
| 2547 | { |
||
| 2548 | *errorCode = USB_UNKNOWN_DEVICE; |
||
| 2549 | *byteCount = 0; |
||
| 2550 | return TRUE; |
||
| 2551 | } |
||
| 2552 | |||
| 2553 | ep = _USB_FindEndpoint( endpoint ); |
||
| 2554 | if (ep != NULL) |
||
| 2555 | { |
||
| 2556 | // bfTransferComplete, the status flags, and byte count can be |
||
| 2557 | // changed in an interrupt service routine. Therefore, we'll |
||
| 2558 | // grab it first, save it locally, and then determine the rest of |
||
| 2559 | // the information. It is better to say that the transfer is not |
||
| 2560 | // yet complete, since the caller will simply try again. |
||
| 2561 | |||
| 2562 | // Save off the Transfer Complete status. That way, we won't |
||
| 2563 | // load up bad values and then say the transfer is complete. |
||
| 2564 | transferComplete = ep->status.bfTransferComplete; |
||
| 2565 | |||
| 2566 | // Set up error code. This is only valid if the transfer is complete. |
||
| 2567 | if (ep->status.bfTransferSuccessful) |
||
| 2568 | { |
||
| 2569 | *errorCode = USB_SUCCESS; |
||
| 2570 | *byteCount = ep->dataCount; |
||
| 2571 | } |
||
| 2572 | else if (ep->status.bfStalled) |
||
| 2573 | { |
||
| 2574 | *errorCode = USB_ENDPOINT_STALLED; |
||
| 2575 | } |
||
| 2576 | else if (ep->status.bfError) |
||
| 2577 | { |
||
| 2578 | *errorCode = ep->bErrorCode; |
||
| 2579 | } |
||
| 2580 | else |
||
| 2581 | { |
||
| 2582 | *errorCode = USB_ENDPOINT_UNRESOLVED_STATE; |
||
| 2583 | } |
||
| 2584 | |||
| 2585 | return transferComplete; |
||
| 2586 | } |
||
| 2587 | |||
| 2588 | // The endpoint was not found. Return TRUE so we can return a valid error code. |
||
| 2589 | *errorCode = USB_ENDPOINT_NOT_FOUND; |
||
| 2590 | return TRUE; |
||
| 2591 | } |
||
| 2592 | |||
| 2593 | /**************************************************************************** |
||
| 2594 | Function: |
||
| 2595 | BYTE USBHostVbusEvent( USB_EVENT vbusEvent, BYTE hubAddress, |
||
| 2596 | BYTE portNumber) |
||
| 2597 | |||
| 2598 | Summary: |
||
| 2599 | This function handles Vbus events that are detected by the application. |
||
| 2600 | |||
| 2601 | Description: |
||
| 2602 | This function handles Vbus events that are detected by the application. |
||
| 2603 | Since Vbus management is application dependent, the application is |
||
| 2604 | responsible for monitoring Vbus and detecting overcurrent conditions |
||
| 2605 | and removal of the overcurrent condition. If the application detects |
||
| 2606 | an overcurrent condition, it should call this function with the event |
||
| 2607 | EVENT_VBUS_OVERCURRENT with the address of the hub and port number that |
||
| 2608 | has the condition. When a port returns to normal operation, the |
||
| 2609 | application should call this function with the event |
||
| 2610 | EVENT_VBUS_POWER_AVAILABLE so the stack knows that it can allow devices |
||
| 2611 | to attach to that port. |
||
| 2612 | |||
| 2613 | Precondition: |
||
| 2614 | None |
||
| 2615 | |||
| 2616 | Parameters: |
||
| 2617 | USB_EVENT vbusEvent - Vbus event that occured. Valid events: |
||
| 2618 | * EVENT_VBUS_OVERCURRENT |
||
| 2619 | * EVENT_VBUS_POWER_AVAILABLE |
||
| 2620 | BYTE hubAddress - Address of the hub device (USB_ROOT_HUB for the |
||
| 2621 | root hub) |
||
| 2622 | BYTE portNumber - Number of the physical port on the hub (0 - based) |
||
| 2623 | |||
| 2624 | Return Values: |
||
| 2625 | USB_SUCCESS - Event handled |
||
| 2626 | USB_ILLEGAL_REQUEST - Invalid event, hub, or port |
||
| 2627 | |||
| 2628 | Remarks: |
||
| 2629 | None |
||
| 2630 | ***************************************************************************/ |
||
| 2631 | |||
| 2632 | BYTE USBHostVbusEvent(USB_EVENT vbusEvent, BYTE hubAddress, BYTE portNumber) |
||
| 2633 | { |
||
| 2634 | if ((hubAddress == USB_ROOT_HUB) && |
||
| 2635 | (portNumber == 0 )) |
||
| 2636 | { |
||
| 2637 | if (vbusEvent == EVENT_VBUS_OVERCURRENT) |
||
| 2638 | { |
||
| 2639 | USBHostShutdown(); |
||
| 2640 | usbRootHubInfo.flags.bPowerGoodPort0 = 0; |
||
| 2641 | return USB_SUCCESS; |
||
| 2642 | } |
||
| 2643 | if (vbusEvent == EVENT_VBUS_POWER_AVAILABLE) |
||
| 2644 | { |
||
| 2645 | usbRootHubInfo.flags.bPowerGoodPort0 = 1; |
||
| 2646 | return USB_SUCCESS; |
||
| 2647 | } |
||
| 2648 | } |
||
| 2649 | |||
| 2650 | return USB_ILLEGAL_REQUEST; |
||
| 2651 | } |
||
| 2652 | |||
| 2653 | |||
| 2654 | /**************************************************************************** |
||
| 2655 | Function: |
||
| 2656 | BYTE USBHostWrite( BYTE deviceAddress, BYTE endpoint, BYTE *data, |
||
| 2657 | DWORD size ) |
||
| 2658 | |||
| 2659 | Summary: |
||
| 2660 | This function initiates a write to the attached device. |
||
| 2661 | |||
| 2662 | Description: |
||
| 2663 | This function initiates a write to the attached device. The data buffer |
||
| 2664 | pointed to by *data must remain valid during the entire time that the |
||
| 2665 | write is taking place; the data is not buffered by the stack. |
||
| 2666 | |||
| 2667 | If the endpoint is isochronous, special conditions apply. The pData and |
||
| 2668 | size parameters have slightly different meanings, since multiple buffers |
||
| 2669 | are required. Once started, an isochronous transfer will continue with |
||
| 2670 | no upper layer intervention until USBHostTerminateTransfer() is called. |
||
| 2671 | The ISOCHRONOUS_DATA_BUFFERS structure should not be manipulated until |
||
| 2672 | the transfer is terminated. |
||
| 2673 | |||
| 2674 | To clarify parameter usage and to simplify casting, use the macro |
||
| 2675 | USBHostWriteIsochronous() when writing to an isochronous endpoint. |
||
| 2676 | |||
| 2677 | Precondition: |
||
| 2678 | None |
||
| 2679 | |||
| 2680 | Parameters: |
||
| 2681 | BYTE deviceAddress - Device address |
||
| 2682 | BYTE endpoint - Endpoint number |
||
| 2683 | BYTE *data - Pointer to where the data is stored. If the endpoint |
||
| 2684 | is isochronous, this points to an |
||
| 2685 | ISOCHRONOUS_DATA_BUFFERS structure, with multiple |
||
| 2686 | data buffer pointers. |
||
| 2687 | DWORD size - Number of data bytes to send. If the endpoint is |
||
| 2688 | isochronous, this is the number of data buffer |
||
| 2689 | pointers pointed to by pData. |
||
| 2690 | |||
| 2691 | Return Values: |
||
| 2692 | USB_SUCCESS - Write started successfully. |
||
| 2693 | USB_UNKNOWN_DEVICE - Device with the specified address not found. |
||
| 2694 | USB_INVALID_STATE - We are not in a normal running state. |
||
| 2695 | USB_ENDPOINT_ILLEGAL_TYPE - Must use USBHostControlWrite to write |
||
| 2696 | to a control endpoint. |
||
| 2697 | USB_ENDPOINT_ILLEGAL_DIRECTION - Must write to an OUT endpoint. |
||
| 2698 | USB_ENDPOINT_STALLED - Endpoint is stalled. Must be cleared |
||
| 2699 | by the application. |
||
| 2700 | USB_ENDPOINT_ERROR - Endpoint has too many errors. Must be |
||
| 2701 | cleared by the application. |
||
| 2702 | USB_ENDPOINT_BUSY - A Write is already in progress. |
||
| 2703 | USB_ENDPOINT_NOT_FOUND - Invalid endpoint. |
||
| 2704 | |||
| 2705 | Remarks: |
||
| 2706 | None |
||
| 2707 | ***************************************************************************/ |
||
| 2708 | |||
| 2709 | BYTE USBHostWrite( BYTE deviceAddress, BYTE endpoint, BYTE *data, DWORD size ) |
||
| 2710 | { |
||
| 2711 | USB_ENDPOINT_INFO *ep; |
||
| 2712 | |||
| 2713 | // Find the required device |
||
| 2714 | if (deviceAddress != usbDeviceInfo.deviceAddress) |
||
| 2715 | { |
||
| 2716 | return USB_UNKNOWN_DEVICE; |
||
| 2717 | } |
||
| 2718 | |||
| 2719 | // If we are not in a normal user running state, we cannot do this. |
||
| 2720 | if ((usbHostState & STATE_MASK) != STATE_RUNNING) |
||
| 2721 | { |
||
| 2722 | return USB_INVALID_STATE; |
||
| 2723 | } |
||
| 2724 | |||
| 2725 | ep = _USB_FindEndpoint( endpoint ); |
||
| 2726 | if (ep != NULL) |
||
| 2727 | { |
||
| 2728 | if (ep->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_CONTROL) |
||
| 2729 | { |
||
| 2730 | // Must not be a control endpoint. |
||
| 2731 | return USB_ENDPOINT_ILLEGAL_TYPE; |
||
| 2732 | } |
||
| 2733 | |||
| 2734 | if (ep->bEndpointAddress & 0x80) |
||
| 2735 | { |
||
| 2736 | // Trying to do an OUT with an IN endpoint. |
||
| 2737 | return USB_ENDPOINT_ILLEGAL_DIRECTION; |
||
| 2738 | } |
||
| 2739 | |||
| 2740 | if (ep->status.bfStalled) |
||
| 2741 | { |
||
| 2742 | // The endpoint is stalled. It must be restarted before a write |
||
| 2743 | // can be performed. |
||
| 2744 | return USB_ENDPOINT_STALLED; |
||
| 2745 | } |
||
| 2746 | |||
| 2747 | if (ep->status.bfError) |
||
| 2748 | { |
||
| 2749 | // The endpoint has errored. The error must be cleared before a |
||
| 2750 | // write can be performed. |
||
| 2751 | return USB_ENDPOINT_ERROR; |
||
| 2752 | } |
||
| 2753 | |||
| 2754 | if (!ep->status.bfTransferComplete) |
||
| 2755 | { |
||
| 2756 | // We are already processing a request for this endpoint. |
||
| 2757 | return USB_ENDPOINT_BUSY; |
||
| 2758 | } |
||
| 2759 | |||
| 2760 | _USB_InitWrite( ep, data, size ); |
||
| 2761 | |||
| 2762 | return USB_SUCCESS; |
||
| 2763 | } |
||
| 2764 | return USB_ENDPOINT_NOT_FOUND; // Endpoint not found |
||
| 2765 | } |
||
| 2766 | |||
| 2767 | |||
| 2768 | // ***************************************************************************** |
||
| 2769 | // ***************************************************************************** |
||
| 2770 | // Section: Internal Functions |
||
| 2771 | // ***************************************************************************** |
||
| 2772 | // ***************************************************************************** |
||
| 2773 | |||
| 2774 | /**************************************************************************** |
||
| 2775 | Function: |
||
| 2776 | void _USB_CheckCommandAndEnumerationAttempts( void ) |
||
| 2777 | |||
| 2778 | Summary: |
||
| 2779 | This function is called when we've received a STALL or a NAK when trying |
||
| 2780 | to enumerate. |
||
| 2781 | |||
| 2782 | Description: |
||
| 2783 | This function is called when we've received a STALL or a NAK when trying |
||
| 2784 | to enumerate. We allow so many attempts at each command, and so many |
||
| 2785 | attempts at enumeration. If the command fails and there are more command |
||
| 2786 | attempts, we try the command again. If the command fails and there are |
||
| 2787 | more enumeration attempts, we reset and try to enumerate again. |
||
| 2788 | Otherwise, we go to the holding state. |
||
| 2789 | |||
| 2790 | Precondition: |
||
| 2791 | usbHostState != STATE_RUNNING |
||
| 2792 | |||
| 2793 | Parameters: |
||
| 2794 | None - None |
||
| 2795 | |||
| 2796 | Returns: |
||
| 2797 | None |
||
| 2798 | |||
| 2799 | Remarks: |
||
| 2800 | None |
||
| 2801 | ***************************************************************************/ |
||
| 2802 | |||
| 2803 | void _USB_CheckCommandAndEnumerationAttempts( void ) |
||
| 2804 | { |
||
| 2805 | #ifdef DEBUG_MODE |
||
| 2806 | UART2PutChar( '=' ); |
||
| 2807 | #endif |
||
| 2808 | |||
| 2809 | // Clear the error and stall flags. A stall here does not require |
||
| 2810 | // host intervention to clear. |
||
| 2811 | pCurrentEndpoint->status.bfError = 0; |
||
| 2812 | pCurrentEndpoint->status.bfStalled = 0; |
||
| 2813 | |||
| 2814 | numCommandTries --; |
||
| 2815 | if (numCommandTries != 0) |
||
| 2816 | { |
||
| 2817 | // We still have retries left on this command. Try again. |
||
| 2818 | usbHostState &= ~SUBSUBSTATE_MASK; |
||
| 2819 | } |
||
| 2820 | else |
||
| 2821 | { |
||
| 2822 | // This command has timed out. |
||
| 2823 | // We are enumerating. See if we can try to enumerate again. |
||
| 2824 | numEnumerationTries --; |
||
| 2825 | if (numEnumerationTries != 0) |
||
| 2826 | { |
||
| 2827 | // We still have retries left to try to enumerate. Reset and try again. |
||
| 2828 | usbHostState = STATE_ATTACHED | SUBSTATE_RESET_DEVICE; |
||
| 2829 | } |
||
| 2830 | else |
||
| 2831 | { |
||
| 2832 | // Give up. The device is not responding properly. |
||
| 2833 | _USB_SetErrorCode( USB_CANNOT_ENUMERATE ); |
||
| 2834 | _USB_SetHoldState(); |
||
| 2835 | } |
||
| 2836 | } |
||
| 2837 | } |
||
| 2838 | |||
| 2839 | |||
| 2840 | /**************************************************************************** |
||
| 2841 | Function: |
||
| 2842 | BOOL _USB_FindClassDriver( BYTE bClass, BYTE bSubClass, BYTE bProtocol, BYTE *pbClientDrv ) |
||
| 2843 | |||
| 2844 | Summary: |
||
| 2845 | |||
| 2846 | |||
| 2847 | Description: |
||
| 2848 | This routine scans the TPL table looking for the entry with |
||
| 2849 | the given class, subclass, and protocol values. |
||
| 2850 | |||
| 2851 | Precondition: |
||
| 2852 | usbTPL must be define by the application. |
||
| 2853 | |||
| 2854 | Parameters: |
||
| 2855 | bClass - The class of the desired entry |
||
| 2856 | bSubClass - The subclass of the desired entry |
||
| 2857 | bProtocol - The protocol of the desired entry |
||
| 2858 | pbClientDrv - Returned index to the client driver in the client driver |
||
| 2859 | table. |
||
| 2860 | |||
| 2861 | Return Values: |
||
| 2862 | TRUE - A class driver was found. |
||
| 2863 | FALSE - A class driver was not found. |
||
| 2864 | |||
| 2865 | Remarks: |
||
| 2866 | None |
||
| 2867 | ***************************************************************************/ |
||
| 2868 | |||
| 2869 | BOOL _USB_FindClassDriver( BYTE bClass, BYTE bSubClass, BYTE bProtocol, BYTE *pbClientDrv ) |
||
| 2870 | { |
||
| 2871 | int i; |
||
| 2872 | |||
| 2873 | i = 0; |
||
| 2874 | while (i < NUM_TPL_ENTRIES) |
||
| 2875 | { |
||
| 2876 | if ((usbTPL[i].flags.bfIsClassDriver == 1 ) && |
||
| 2877 | (usbTPL[i].device.bClass == bClass ) && |
||
| 2878 | (usbTPL[i].device.bSubClass == bSubClass) && |
||
| 2879 | (usbTPL[i].device.bProtocol == bProtocol) ) |
||
| 2880 | { |
||
| 2881 | *pbClientDrv = usbTPL[i].ClientDriver; |
||
| 2882 | #ifdef DEBUG_MODE |
||
| 2883 | UART2PrintString( "HOST: Client driver found.\r\n" ); |
||
| 2884 | #endif |
||
| 2885 | return TRUE; |
||
| 2886 | } |
||
| 2887 | i++; |
||
| 2888 | } |
||
| 2889 | |||
| 2890 | #ifdef DEBUG_MODE |
||
| 2891 | UART2PrintString( "HOST: Client driver NOT found.\r\n" ); |
||
| 2892 | #endif |
||
| 2893 | return FALSE; |
||
| 2894 | |||
| 2895 | } // _USB_FindClassDriver |
||
| 2896 | |||
| 2897 | |||
| 2898 | /**************************************************************************** |
||
| 2899 | Function: |
||
| 2900 | BOOL _USB_FindDeviceLevelClientDriver( void ) |
||
| 2901 | |||
| 2902 | Description: |
||
| 2903 | This function searches the TPL to try to find a device-level client |
||
| 2904 | driver. |
||
| 2905 | |||
| 2906 | Precondition: |
||
| 2907 | * usbHostState == STATE_ATTACHED|SUBSTATE_VALIDATE_VID_PID |
||
| 2908 | * usbTPL must be define by the application. |
||
| 2909 | |||
| 2910 | Parameters: |
||
| 2911 | None - None |
||
| 2912 | |||
| 2913 | Return Values: |
||
| 2914 | TRUE - Client driver found |
||
| 2915 | FALSE - Client driver not found |
||
| 2916 | |||
| 2917 | Remarks: |
||
| 2918 | If successful, this function preserves the client's index from the client |
||
| 2919 | driver table and sets flags indicating that the device should use a |
||
| 2920 | single client driver. |
||
| 2921 | ***************************************************************************/ |
||
| 2922 | |||
| 2923 | BOOL _USB_FindDeviceLevelClientDriver( void ) |
||
| 2924 | { |
||
| 2925 | WORD i; |
||
| 2926 | USB_DEVICE_DESCRIPTOR *pDesc = (USB_DEVICE_DESCRIPTOR *)pDeviceDescriptor; |
||
| 2927 | |||
| 2928 | // Scan TPL |
||
| 2929 | i = 0; |
||
| 2930 | usbDeviceInfo.flags.bfUseDeviceClientDriver = 0; |
||
| 2931 | while (i < NUM_TPL_ENTRIES) |
||
| 2932 | { |
||
| 2933 | if (usbTPL[i].flags.bfIsClassDriver) |
||
| 2934 | { |
||
| 2935 | // Check for a device-class client driver |
||
| 2936 | if ((usbTPL[i].device.bClass == pDesc->bDeviceClass ) && |
||
| 2937 | (usbTPL[i].device.bSubClass == pDesc->bDeviceSubClass) && |
||
| 2938 | (usbTPL[i].device.bProtocol == pDesc->bDeviceProtocol) ) |
||
| 2939 | { |
||
| 2940 | #ifdef DEBUG_MODE |
||
| 2941 | UART2PrintString( "HOST: Device validated by class\r\n" ); |
||
| 2942 | #endif |
||
| 2943 | usbDeviceInfo.flags.bfUseDeviceClientDriver = 1; |
||
| 2944 | } |
||
| 2945 | } |
||
| 2946 | else |
||
| 2947 | { |
||
| 2948 | // Check for a device-specific client driver by VID & PID |
||
| 2949 | #ifdef ALLOW_GLOBAL_VID_AND_PID |
||
| 2950 | if (((usbTPL[i].device.idVendor == pDesc->idVendor ) && |
||
| 2951 | (usbTPL[i].device.idProduct == pDesc->idProduct)) || |
||
| 2952 | ((usbTPL[i].device.idVendor == 0xFFFF) && |
||
| 2953 | (usbTPL[i].device.idProduct == 0xFFFF))) |
||
| 2954 | #else |
||
| 2955 | if ((usbTPL[i].device.idVendor == pDesc->idVendor ) && |
||
| 2956 | (usbTPL[i].device.idProduct == pDesc->idProduct) ) |
||
| 2957 | #endif |
||
| 2958 | { |
||
| 2959 | #ifdef DEBUG_MODE |
||
| 2960 | UART2PrintString( "HOST: Device validated by VID/PID\r\n" ); |
||
| 2961 | #endif |
||
| 2962 | usbDeviceInfo.flags.bfUseDeviceClientDriver = 1; |
||
| 2963 | } |
||
| 2964 | } |
||
| 2965 | |||
| 2966 | if (usbDeviceInfo.flags.bfUseDeviceClientDriver) |
||
| 2967 | { |
||
| 2968 | // Save client driver info |
||
| 2969 | usbDeviceInfo.deviceClientDriver = usbTPL[i].ClientDriver; |
||
| 2970 | |||
| 2971 | // Select configuration if it is given in the TPL |
||
| 2972 | if (usbTPL[i].flags.bfSetConfiguration) |
||
| 2973 | { |
||
| 2974 | usbDeviceInfo.currentConfiguration = usbTPL[i].bConfiguration; |
||
| 2975 | } |
||
| 2976 | |||
| 2977 | return TRUE; |
||
| 2978 | } |
||
| 2979 | |||
| 2980 | i++; |
||
| 2981 | } |
||
| 2982 | |||
| 2983 | #ifdef DEBUG_MODE |
||
| 2984 | UART2PrintString( "HOST: Device not yet validated\r\n" ); |
||
| 2985 | #endif |
||
| 2986 | |||
| 2987 | return FALSE; |
||
| 2988 | } |
||
| 2989 | |||
| 2990 | |||
| 2991 | /**************************************************************************** |
||
| 2992 | Function: |
||
| 2993 | USB_ENDPOINT_INFO * _USB_FindEndpoint( BYTE endpoint ) |
||
| 2994 | |||
| 2995 | Description: |
||
| 2996 | This function searches the list of interfaces to try to find the specified |
||
| 2997 | endpoint. |
||
| 2998 | |||
| 2999 | Precondition: |
||
| 3000 | None |
||
| 3001 | |||
| 3002 | Parameters: |
||
| 3003 | BYTE endpoint - The endpoint to find. |
||
| 3004 | |||
| 3005 | Returns: |
||
| 3006 | Returns a pointer to the USB_ENDPOINT_INFO structure for the endpoint. |
||
| 3007 | |||
| 3008 | Remarks: |
||
| 3009 | None |
||
| 3010 | ***************************************************************************/ |
||
| 3011 | |||
| 3012 | USB_ENDPOINT_INFO * _USB_FindEndpoint( BYTE endpoint ) |
||
| 3013 | { |
||
| 3014 | USB_ENDPOINT_INFO *pEndpoint; |
||
| 3015 | USB_INTERFACE_INFO *pInterface; |
||
| 3016 | |||
| 3017 | if (endpoint == 0) |
||
| 3018 | { |
||
| 3019 | return usbDeviceInfo.pEndpoint0; |
||
| 3020 | } |
||
| 3021 | |||
| 3022 | pInterface = usbDeviceInfo.pInterfaceList; |
||
| 3023 | while (pInterface) |
||
| 3024 | { |
||
| 3025 | // Look for the endpoint in the currently active setting. |
||
| 3026 | if (pInterface->pCurrentSetting) |
||
| 3027 | { |
||
| 3028 | pEndpoint = pInterface->pCurrentSetting->pEndpointList; |
||
| 3029 | while (pEndpoint) |
||
| 3030 | { |
||
| 3031 | if (pEndpoint->bEndpointAddress == endpoint) |
||
| 3032 | { |
||
| 3033 | // We have found the endpoint. |
||
| 3034 | return pEndpoint; |
||
| 3035 | } |
||
| 3036 | pEndpoint = pEndpoint->next; |
||
| 3037 | } |
||
| 3038 | } |
||
| 3039 | |||
| 3040 | // Go to the next interface. |
||
| 3041 | pInterface = pInterface->next; |
||
| 3042 | } |
||
| 3043 | |||
| 3044 | return NULL; |
||
| 3045 | } |
||
| 3046 | |||
| 3047 | |||
| 3048 | /**************************************************************************** |
||
| 3049 | Function: |
||
| 3050 | USB_INTERFACE_INFO * _USB_FindInterface ( BYTE bInterface, BYTE bAltSetting ) |
||
| 3051 | |||
| 3052 | Description: |
||
| 3053 | This routine scans the interface linked list and returns a pointer to the |
||
| 3054 | node identified by the interface and alternate setting. |
||
| 3055 | |||
| 3056 | Precondition: |
||
| 3057 | None |
||
| 3058 | |||
| 3059 | Parameters: |
||
| 3060 | bInterface - Interface number |
||
| 3061 | bAltSetting - Interface alternate setting number |
||
| 3062 | |||
| 3063 | Returns: |
||
| 3064 | USB_INTERFACE_INFO * - Pointer to the interface linked list node. |
||
| 3065 | |||
| 3066 | Remarks: |
||
| 3067 | None |
||
| 3068 | ***************************************************************************/ |
||
| 3069 | /* |
||
| 3070 | USB_INTERFACE_INFO * _USB_FindInterface ( BYTE bInterface, BYTE bAltSetting ) |
||
| 3071 | { |
||
| 3072 | USB_INTERFACE_INFO *pCurIntf = usbDeviceInfo.pInterfaceList; |
||
| 3073 | |||
| 3074 | while (pCurIntf) |
||
| 3075 | { |
||
| 3076 | if (pCurIntf->interface == bInterface && |
||
| 3077 | pCurIntf->interfaceAltSetting == bAltSetting ) |
||
| 3078 | { |
||
| 3079 | return pCurIntf; |
||
| 3080 | } |
||
| 3081 | } |
||
| 3082 | |||
| 3083 | return NULL; |
||
| 3084 | |||
| 3085 | } // _USB_FindInterface |
||
| 3086 | */ |
||
| 3087 | |||
| 3088 | /**************************************************************************** |
||
| 3089 | Function: |
||
| 3090 | void _USB_FindNextToken( void ) |
||
| 3091 | |||
| 3092 | Description: |
||
| 3093 | This function determines the next token to send of all current pending |
||
| 3094 | transfers. |
||
| 3095 | |||
| 3096 | Precondition: |
||
| 3097 | None |
||
| 3098 | |||
| 3099 | Parameters: |
||
| 3100 | None - None |
||
| 3101 | |||
| 3102 | Return Values: |
||
| 3103 | TRUE - A token was sent |
||
| 3104 | FALSE - No token was found to send, so the routine can be called again. |
||
| 3105 | |||
| 3106 | Remarks: |
||
| 3107 | This routine is only called from an interrupt handler, either SOFIF or |
||
| 3108 | TRNIF. |
||
| 3109 | ***************************************************************************/ |
||
| 3110 | |||
| 3111 | void _USB_FindNextToken( void ) |
||
| 3112 | { |
||
| 3113 | BOOL illegalState = FALSE; |
||
| 3114 | |||
| 3115 | // If the device is suspended or resuming, do not send any tokens. We will |
||
| 3116 | // send the next token on an SOF interrupt after the resume recovery time |
||
| 3117 | // has expired. |
||
| 3118 | if ((usbHostState & (SUBSTATE_MASK | SUBSUBSTATE_MASK)) == (STATE_RUNNING | SUBSTATE_SUSPEND_AND_RESUME)) |
||
| 3119 | { |
||
| 3120 | return; |
||
| 3121 | } |
||
| 3122 | |||
| 3123 | // If we are currently sending a token, we cannot do anything. We will come |
||
| 3124 | // back in here when we get either the Token Done or the Start of Frame interrupt. |
||
| 3125 | if (usbBusInfo.flags.bfTokenAlreadyWritten) //(U1CONbits.TOKBUSY) |
||
| 3126 | { |
||
| 3127 | return; |
||
| 3128 | } |
||
| 3129 | |||
| 3130 | // We will handle control transfers first. We only allow one control |
||
| 3131 | // transfer per frame. |
||
| 3132 | if (!usbBusInfo.flags.bfControlTransfersDone) |
||
| 3133 | { |
||
| 3134 | // Look for any control transfers. |
||
| 3135 | if (_USB_FindServiceEndpoint( USB_TRANSFER_TYPE_CONTROL )) |
||
| 3136 | { |
||
| 3137 | switch (pCurrentEndpoint->transferState & TSTATE_MASK) |
||
| 3138 | { |
||
| 3139 | case TSTATE_CONTROL_NO_DATA: |
||
| 3140 | switch (pCurrentEndpoint->transferState & TSUBSTATE_MASK) |
||
| 3141 | { |
||
| 3142 | case TSUBSTATE_CONTROL_NO_DATA_SETUP: |
||
| 3143 | _USB_SetDATA01( DTS_DATA0 ); |
||
| 3144 | _USB_SetBDT( USB_TOKEN_SETUP ); |
||
| 3145 | _USB_SendToken( pCurrentEndpoint->bEndpointAddress, USB_TOKEN_SETUP ); |
||
| 3146 | #ifdef ONE_CONTROL_TRANSACTION_PER_FRAME |
||
| 3147 | usbBusInfo.flags.bfControlTransfersDone = 1; // Only one control transfer per frame. |
||
| 3148 | #endif |
||
| 3149 | return; |
||
| 3150 | break; |
||
| 3151 | |||
| 3152 | case TSUBSTATE_CONTROL_NO_DATA_ACK: |
||
| 3153 | pCurrentEndpoint->dataCountMax = pCurrentEndpoint->dataCount; |
||
| 3154 | _USB_SetDATA01( DTS_DATA1 ); |
||
| 3155 | _USB_SetBDT( USB_TOKEN_IN ); |
||
| 3156 | _USB_SendToken( pCurrentEndpoint->bEndpointAddress, USB_TOKEN_IN ); |
||
| 3157 | #ifdef ONE_CONTROL_TRANSACTION_PER_FRAME |
||
| 3158 | usbBusInfo.flags.bfControlTransfersDone = 1; // Only one control transfer per frame. |
||
| 3159 | #endif |
||
| 3160 | return; |
||
| 3161 | break; |
||
| 3162 | |||
| 3163 | case TSUBSTATE_CONTROL_NO_DATA_COMPLETE: |
||
| 3164 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3165 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3166 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 3167 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3168 | { |
||
| 3169 | USB_EVENT_DATA *data; |
||
| 3170 | |||
| 3171 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3172 | data->event = EVENT_TRANSFER; |
||
| 3173 | data->TransferData.dataCount = pCurrentEndpoint->dataCount; |
||
| 3174 | data->TransferData.pUserData = pCurrentEndpoint->pUserData; |
||
| 3175 | data->TransferData.bErrorCode = USB_SUCCESS; |
||
| 3176 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3177 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3178 | data->TransferData.clientDriver = pCurrentEndpoint->clientDriver; |
||
| 3179 | } |
||
| 3180 | else |
||
| 3181 | { |
||
| 3182 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3183 | } |
||
| 3184 | #endif |
||
| 3185 | break; |
||
| 3186 | |||
| 3187 | case TSUBSTATE_ERROR: |
||
| 3188 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3189 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3190 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 3191 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3192 | { |
||
| 3193 | USB_EVENT_DATA *data; |
||
| 3194 | |||
| 3195 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3196 | data->event = EVENT_BUS_ERROR; |
||
| 3197 | data->TransferData.dataCount = 0; |
||
| 3198 | data->TransferData.pUserData = NULL; |
||
| 3199 | data->TransferData.bErrorCode = pCurrentEndpoint->bErrorCode; |
||
| 3200 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3201 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3202 | data->TransferData.clientDriver = pCurrentEndpoint->clientDriver; |
||
| 3203 | } |
||
| 3204 | else |
||
| 3205 | { |
||
| 3206 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3207 | } |
||
| 3208 | #endif |
||
| 3209 | break; |
||
| 3210 | |||
| 3211 | default: |
||
| 3212 | illegalState = TRUE; |
||
| 3213 | break; |
||
| 3214 | } |
||
| 3215 | break; |
||
| 3216 | |||
| 3217 | case TSTATE_CONTROL_READ: |
||
| 3218 | switch (pCurrentEndpoint->transferState & TSUBSTATE_MASK) |
||
| 3219 | { |
||
| 3220 | case TSUBSTATE_CONTROL_READ_SETUP: |
||
| 3221 | _USB_SetDATA01( DTS_DATA0 ); |
||
| 3222 | _USB_SetBDT( USB_TOKEN_SETUP ); |
||
| 3223 | _USB_SendToken( pCurrentEndpoint->bEndpointAddress, USB_TOKEN_SETUP ); |
||
| 3224 | #ifdef ONE_CONTROL_TRANSACTION_PER_FRAME |
||
| 3225 | usbBusInfo.flags.bfControlTransfersDone = 1; // Only one control transfer per frame. |
||
| 3226 | #endif |
||
| 3227 | return; |
||
| 3228 | break; |
||
| 3229 | |||
| 3230 | case TSUBSTATE_CONTROL_READ_DATA: |
||
| 3231 | _USB_SetBDT( USB_TOKEN_IN ); |
||
| 3232 | _USB_SendToken( pCurrentEndpoint->bEndpointAddress, USB_TOKEN_IN ); |
||
| 3233 | #ifdef ONE_CONTROL_TRANSACTION_PER_FRAME |
||
| 3234 | usbBusInfo.flags.bfControlTransfersDone = 1; // Only one control transfer per frame. |
||
| 3235 | #endif |
||
| 3236 | return; |
||
| 3237 | break; |
||
| 3238 | |||
| 3239 | case TSUBSTATE_CONTROL_READ_ACK: |
||
| 3240 | pCurrentEndpoint->dataCountMax = pCurrentEndpoint->dataCount; |
||
| 3241 | _USB_SetDATA01( DTS_DATA1 ); |
||
| 3242 | _USB_SetBDT( USB_TOKEN_OUT ); |
||
| 3243 | _USB_SendToken( pCurrentEndpoint->bEndpointAddress, USB_TOKEN_OUT ); |
||
| 3244 | #ifdef ONE_CONTROL_TRANSACTION_PER_FRAME |
||
| 3245 | usbBusInfo.flags.bfControlTransfersDone = 1; // Only one control transfer per frame. |
||
| 3246 | #endif |
||
| 3247 | return; |
||
| 3248 | break; |
||
| 3249 | |||
| 3250 | case TSUBSTATE_CONTROL_READ_COMPLETE: |
||
| 3251 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3252 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3253 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 3254 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3255 | { |
||
| 3256 | USB_EVENT_DATA *data; |
||
| 3257 | |||
| 3258 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3259 | data->event = EVENT_TRANSFER; |
||
| 3260 | data->TransferData.dataCount = pCurrentEndpoint->dataCount; |
||
| 3261 | data->TransferData.pUserData = pCurrentEndpoint->pUserData; |
||
| 3262 | data->TransferData.bErrorCode = USB_SUCCESS; |
||
| 3263 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3264 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3265 | data->TransferData.clientDriver = pCurrentEndpoint->clientDriver; |
||
| 3266 | } |
||
| 3267 | else |
||
| 3268 | { |
||
| 3269 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3270 | } |
||
| 3271 | #endif |
||
| 3272 | break; |
||
| 3273 | |||
| 3274 | case TSUBSTATE_ERROR: |
||
| 3275 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3276 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3277 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 3278 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3279 | { |
||
| 3280 | USB_EVENT_DATA *data; |
||
| 3281 | |||
| 3282 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3283 | data->event = EVENT_BUS_ERROR; |
||
| 3284 | data->TransferData.dataCount = 0; |
||
| 3285 | data->TransferData.pUserData = NULL; |
||
| 3286 | data->TransferData.bErrorCode = pCurrentEndpoint->bErrorCode; |
||
| 3287 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3288 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3289 | data->TransferData.clientDriver = pCurrentEndpoint->clientDriver; |
||
| 3290 | } |
||
| 3291 | else |
||
| 3292 | { |
||
| 3293 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3294 | } |
||
| 3295 | #endif |
||
| 3296 | break; |
||
| 3297 | |||
| 3298 | default: |
||
| 3299 | illegalState = TRUE; |
||
| 3300 | break; |
||
| 3301 | } |
||
| 3302 | break; |
||
| 3303 | |||
| 3304 | case TSTATE_CONTROL_WRITE: |
||
| 3305 | switch (pCurrentEndpoint->transferState & TSUBSTATE_MASK) |
||
| 3306 | { |
||
| 3307 | case TSUBSTATE_CONTROL_WRITE_SETUP: |
||
| 3308 | _USB_SetDATA01( DTS_DATA0 ); |
||
| 3309 | _USB_SetBDT( USB_TOKEN_SETUP ); |
||
| 3310 | _USB_SendToken( pCurrentEndpoint->bEndpointAddress, USB_TOKEN_SETUP ); |
||
| 3311 | #ifdef ONE_CONTROL_TRANSACTION_PER_FRAME |
||
| 3312 | usbBusInfo.flags.bfControlTransfersDone = 1; // Only one control transfer per frame. |
||
| 3313 | #endif |
||
| 3314 | return; |
||
| 3315 | break; |
||
| 3316 | |||
| 3317 | case TSUBSTATE_CONTROL_WRITE_DATA: |
||
| 3318 | _USB_SetBDT( USB_TOKEN_OUT ); |
||
| 3319 | _USB_SendToken( pCurrentEndpoint->bEndpointAddress, USB_TOKEN_OUT ); |
||
| 3320 | #ifdef ONE_CONTROL_TRANSACTION_PER_FRAME |
||
| 3321 | usbBusInfo.flags.bfControlTransfersDone = 1; // Only one control transfer per frame. |
||
| 3322 | #endif |
||
| 3323 | return; |
||
| 3324 | break; |
||
| 3325 | |||
| 3326 | case TSUBSTATE_CONTROL_WRITE_ACK: |
||
| 3327 | pCurrentEndpoint->dataCountMax = pCurrentEndpoint->dataCount; |
||
| 3328 | _USB_SetDATA01( DTS_DATA1 ); |
||
| 3329 | _USB_SetBDT( USB_TOKEN_IN ); |
||
| 3330 | _USB_SendToken( pCurrentEndpoint->bEndpointAddress, USB_TOKEN_IN ); |
||
| 3331 | #ifdef ONE_CONTROL_TRANSACTION_PER_FRAME |
||
| 3332 | usbBusInfo.flags.bfControlTransfersDone = 1; // Only one control transfer per frame. |
||
| 3333 | #endif |
||
| 3334 | return; |
||
| 3335 | break; |
||
| 3336 | |||
| 3337 | case TSUBSTATE_CONTROL_WRITE_COMPLETE: |
||
| 3338 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3339 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3340 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 3341 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3342 | { |
||
| 3343 | USB_EVENT_DATA *data; |
||
| 3344 | |||
| 3345 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3346 | data->event = EVENT_TRANSFER; |
||
| 3347 | data->TransferData.dataCount = pCurrentEndpoint->dataCount; |
||
| 3348 | data->TransferData.pUserData = pCurrentEndpoint->pUserData; |
||
| 3349 | data->TransferData.bErrorCode = USB_SUCCESS; |
||
| 3350 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3351 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3352 | data->TransferData.clientDriver = pCurrentEndpoint->clientDriver; |
||
| 3353 | } |
||
| 3354 | else |
||
| 3355 | { |
||
| 3356 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3357 | } |
||
| 3358 | #endif |
||
| 3359 | break; |
||
| 3360 | |||
| 3361 | case TSUBSTATE_ERROR: |
||
| 3362 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3363 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3364 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 3365 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3366 | { |
||
| 3367 | USB_EVENT_DATA *data; |
||
| 3368 | |||
| 3369 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3370 | data->event = EVENT_BUS_ERROR; |
||
| 3371 | data->TransferData.dataCount = 0; |
||
| 3372 | data->TransferData.pUserData = NULL; |
||
| 3373 | data->TransferData.bErrorCode = pCurrentEndpoint->bErrorCode; |
||
| 3374 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3375 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3376 | data->TransferData.clientDriver = pCurrentEndpoint->clientDriver; |
||
| 3377 | } |
||
| 3378 | else |
||
| 3379 | { |
||
| 3380 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3381 | } |
||
| 3382 | #endif |
||
| 3383 | break; |
||
| 3384 | |||
| 3385 | default: |
||
| 3386 | illegalState = TRUE; |
||
| 3387 | break; |
||
| 3388 | } |
||
| 3389 | break; |
||
| 3390 | |||
| 3391 | default: |
||
| 3392 | illegalState = TRUE; |
||
| 3393 | } |
||
| 3394 | |||
| 3395 | if (illegalState) |
||
| 3396 | { |
||
| 3397 | // We should never use this, but in case we do, put the endpoint |
||
| 3398 | // in a recoverable state. |
||
| 3399 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3400 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3401 | } |
||
| 3402 | } |
||
| 3403 | |||
| 3404 | // If we've gone through all the endpoints, we do not have any more control transfers. |
||
| 3405 | usbBusInfo.flags.bfControlTransfersDone = 1; |
||
| 3406 | } |
||
| 3407 | |||
| 3408 | #ifdef USB_SUPPORT_ISOCHRONOUS_TRANSFERS |
||
| 3409 | // Next, we will handle isochronous transfers. We must be careful with |
||
| 3410 | // these. The maximum packet size for an isochronous transfer is 1023 |
||
| 3411 | // bytes, so we cannot use the threshold register (U1SOF) to ensure that |
||
| 3412 | // we do not write too many tokens during a frame. Instead, we must count |
||
| 3413 | // the number of bytes we are sending and stop sending isochronous |
||
| 3414 | // transfers when we reach that limit. |
||
| 3415 | |||
| 3416 | // TODO: Implement scheduling by using usbBusInfo.dBytesSentInFrame |
||
| 3417 | |||
| 3418 | // Current Limitation: The stack currently supports only one attached |
||
| 3419 | // device. We will make the assumption that the control, isochronous, and |
||
| 3420 | // interrupt transfers requested by a single device will not exceed one |
||
| 3421 | // frame, and defer the scheduler. |
||
| 3422 | |||
| 3423 | // Due to the nature of isochronous transfers, transfer events must be used. |
||
| 3424 | #if !defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 3425 | #error Transfer events are required for isochronous transfers |
||
| 3426 | #endif |
||
| 3427 | |||
| 3428 | if (!usbBusInfo.flags.bfIsochronousTransfersDone) |
||
| 3429 | { |
||
| 3430 | // Look for any isochronous operations. |
||
| 3431 | if (_USB_FindServiceEndpoint( USB_TRANSFER_TYPE_ISOCHRONOUS )) |
||
| 3432 | { |
||
| 3433 | switch (pCurrentEndpoint->transferState & TSTATE_MASK) |
||
| 3434 | { |
||
| 3435 | case TSTATE_ISOCHRONOUS_READ: |
||
| 3436 | switch (pCurrentEndpoint->transferState & TSUBSTATE_MASK) |
||
| 3437 | { |
||
| 3438 | case TSUBSTATE_ISOCHRONOUS_READ_DATA: |
||
| 3439 | if (pCurrentEndpoint->wIntervalCount == 0) |
||
| 3440 | { |
||
| 3441 | // Reset the interval count for the next packet. |
||
| 3442 | pCurrentEndpoint->wIntervalCount = pCurrentEndpoint->wInterval; |
||
| 3443 | |||
| 3444 | // Don't overwrite data the user has not yet processed. We will skip this interval. |
||
| 3445 | if (((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->buffers[((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->currentBufferUSB].bfDataLengthValid) |
||
| 3446 | { |
||
| 3447 | // We have buffer overflow. |
||
| 3448 | } |
||
| 3449 | else |
||
| 3450 | { |
||
| 3451 | // Initialize the data buffer. |
||
| 3452 | ((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->buffers[((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->currentBufferUSB].bfDataLengthValid = 0; |
||
| 3453 | pCurrentEndpoint->dataCount = 0; |
||
| 3454 | |||
| 3455 | _USB_SetDATA01( DTS_DATA0 ); // Always DATA0 for isochronous |
||
| 3456 | _USB_SetBDT( USB_TOKEN_IN ); |
||
| 3457 | _USB_SendToken( pCurrentEndpoint->bEndpointAddress, USB_TOKEN_IN ); |
||
| 3458 | return; |
||
| 3459 | } |
||
| 3460 | } |
||
| 3461 | break; |
||
| 3462 | |||
| 3463 | case TSUBSTATE_ISOCHRONOUS_READ_COMPLETE: |
||
| 3464 | // Isochronous transfers are continuous until the user stops them. |
||
| 3465 | // Send an event that there is new data, and reset for the next |
||
| 3466 | // interval. |
||
| 3467 | pCurrentEndpoint->transferState = TSTATE_ISOCHRONOUS_READ | TSUBSTATE_ISOCHRONOUS_READ_DATA; |
||
| 3468 | |||
| 3469 | // Update the valid data length for this buffer. |
||
| 3470 | ((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->buffers[((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->currentBufferUSB].dataLength = pCurrentEndpoint->dataCount; |
||
| 3471 | ((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->buffers[((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->currentBufferUSB].bfDataLengthValid = 1; |
||
| 3472 | #if defined( USB_ENABLE_ISOC_TRANSFER_EVENT ) |
||
| 3473 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3474 | { |
||
| 3475 | USB_EVENT_DATA *data; |
||
| 3476 | |||
| 3477 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3478 | data->event = EVENT_TRANSFER; |
||
| 3479 | data->TransferData.dataCount = pCurrentEndpoint->dataCount; |
||
| 3480 | data->TransferData.pUserData = ((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->buffers[((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->currentBufferUSB].pBuffer; |
||
| 3481 | data->TransferData.bErrorCode = USB_SUCCESS; |
||
| 3482 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3483 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3484 | data->TransferData.clientDriver = pCurrentEndpoint->clientDriver; |
||
| 3485 | } |
||
| 3486 | else |
||
| 3487 | { |
||
| 3488 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3489 | } |
||
| 3490 | #endif |
||
| 3491 | |||
| 3492 | // If the user wants an event from the interrupt handler to handle the data as quickly as |
||
| 3493 | // possible, send up the event. Then mark the packet as used. |
||
| 3494 | #ifdef USB_HOST_APP_DATA_EVENT_HANDLER |
||
| 3495 | usbClientDrvTable[pCurrentEndpoint->clientDriver].DataEventHandler( usbDeviceInfo.deviceAddress, EVENT_DATA_ISOC_READ, ((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->buffers[((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->currentBufferUSB].pBuffer, pCurrentEndpoint->dataCount ); |
||
| 3496 | ((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->buffers[((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->currentBufferUSB].bfDataLengthValid = 0; |
||
| 3497 | #endif |
||
| 3498 | |||
| 3499 | // Move to the next data buffer. |
||
| 3500 | ((ISOCHRONOUS_DATA *)pCurrentEndpoint->pUserData)->currentBufferUSB++; |
||
| 3501 | if (((ISOCHRONOUS_DATA *)pCurrentEndpoint->pUserData)->currentBufferUSB >= ((ISOCHRONOUS_DATA *)pCurrentEndpoint->pUserData)->totalBuffers) |
||
| 3502 | { |
||
| 3503 | ((ISOCHRONOUS_DATA *)pCurrentEndpoint->pUserData)->currentBufferUSB = 0; |
||
| 3504 | } |
||
| 3505 | break; |
||
| 3506 | |||
| 3507 | case TSUBSTATE_ERROR: |
||
| 3508 | // Isochronous transfers are continuous until the user stops them. |
||
| 3509 | // Send an event that there is an error, and reset for the next |
||
| 3510 | // interval. |
||
| 3511 | pCurrentEndpoint->transferState = TSTATE_ISOCHRONOUS_READ | TSUBSTATE_ISOCHRONOUS_READ_DATA; |
||
| 3512 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 3513 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3514 | { |
||
| 3515 | USB_EVENT_DATA *data; |
||
| 3516 | |||
| 3517 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3518 | data->event = EVENT_BUS_ERROR; |
||
| 3519 | data->TransferData.dataCount = 0; |
||
| 3520 | data->TransferData.pUserData = NULL; |
||
| 3521 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3522 | data->TransferData.bErrorCode = pCurrentEndpoint->bErrorCode; |
||
| 3523 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3524 | } |
||
| 3525 | else |
||
| 3526 | { |
||
| 3527 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3528 | } |
||
| 3529 | #endif |
||
| 3530 | break; |
||
| 3531 | |||
| 3532 | default: |
||
| 3533 | illegalState = TRUE; |
||
| 3534 | break; |
||
| 3535 | } |
||
| 3536 | break; |
||
| 3537 | |||
| 3538 | case TSTATE_ISOCHRONOUS_WRITE: |
||
| 3539 | switch (pCurrentEndpoint->transferState & TSUBSTATE_MASK) |
||
| 3540 | { |
||
| 3541 | case TSUBSTATE_ISOCHRONOUS_WRITE_DATA: |
||
| 3542 | if (pCurrentEndpoint->wIntervalCount == 0) |
||
| 3543 | { |
||
| 3544 | // Reset the interval count for the next packet. |
||
| 3545 | pCurrentEndpoint->wIntervalCount = pCurrentEndpoint->wInterval; |
||
| 3546 | |||
| 3547 | if (!((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->buffers[((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->currentBufferUSB].bfDataLengthValid) |
||
| 3548 | { |
||
| 3549 | // We have buffer underrun. |
||
| 3550 | } |
||
| 3551 | else |
||
| 3552 | { |
||
| 3553 | pCurrentEndpoint->dataCount = ((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->buffers[((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->currentBufferUSB].dataLength; |
||
| 3554 | |||
| 3555 | _USB_SetDATA01( DTS_DATA0 ); // Always DATA0 for isochronous |
||
| 3556 | _USB_SetBDT( USB_TOKEN_OUT ); |
||
| 3557 | _USB_SendToken( pCurrentEndpoint->bEndpointAddress, USB_TOKEN_OUT ); |
||
| 3558 | return; |
||
| 3559 | } |
||
| 3560 | } |
||
| 3561 | break; |
||
| 3562 | |||
| 3563 | case TSUBSTATE_ISOCHRONOUS_WRITE_COMPLETE: |
||
| 3564 | // Isochronous transfers are continuous until the user stops them. |
||
| 3565 | // Send an event that data has been sent, and reset for the next |
||
| 3566 | // interval. |
||
| 3567 | pCurrentEndpoint->transferState = TSTATE_ISOCHRONOUS_WRITE | TSUBSTATE_ISOCHRONOUS_WRITE_DATA; |
||
| 3568 | |||
| 3569 | // Update the valid data length for this buffer. |
||
| 3570 | ((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->buffers[((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->currentBufferUSB].bfDataLengthValid = 0; |
||
| 3571 | #if defined( USB_ENABLE_ISOC_TRANSFER_EVENT ) |
||
| 3572 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3573 | { |
||
| 3574 | USB_EVENT_DATA *data; |
||
| 3575 | |||
| 3576 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3577 | data->event = EVENT_TRANSFER; |
||
| 3578 | data->TransferData.dataCount = pCurrentEndpoint->dataCount; |
||
| 3579 | data->TransferData.pUserData = ((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->buffers[((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->currentBufferUSB].pBuffer; |
||
| 3580 | data->TransferData.bErrorCode = USB_SUCCESS; |
||
| 3581 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3582 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3583 | data->TransferData.clientDriver = pCurrentEndpoint->clientDriver; |
||
| 3584 | } |
||
| 3585 | else |
||
| 3586 | { |
||
| 3587 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3588 | } |
||
| 3589 | #endif |
||
| 3590 | |||
| 3591 | // If the user wants an event from the interrupt handler to handle the data as quickly as |
||
| 3592 | // possible, send up the event. |
||
| 3593 | #ifdef USB_HOST_APP_DATA_EVENT_HANDLER |
||
| 3594 | usbClientDrvTable[pCurrentEndpoint->clientDriver].DataEventHandler( usbDeviceInfo.deviceAddress, EVENT_DATA_ISOC_WRITE, ((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->buffers[((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->currentBufferUSB].pBuffer, pCurrentEndpoint->dataCount ); |
||
| 3595 | #endif |
||
| 3596 | |||
| 3597 | // Move to the next data buffer. |
||
| 3598 | ((ISOCHRONOUS_DATA *)pCurrentEndpoint->pUserData)->currentBufferUSB++; |
||
| 3599 | if (((ISOCHRONOUS_DATA *)pCurrentEndpoint->pUserData)->currentBufferUSB >= ((ISOCHRONOUS_DATA *)pCurrentEndpoint->pUserData)->totalBuffers) |
||
| 3600 | { |
||
| 3601 | ((ISOCHRONOUS_DATA *)pCurrentEndpoint->pUserData)->currentBufferUSB = 0; |
||
| 3602 | } |
||
| 3603 | break; |
||
| 3604 | |||
| 3605 | case TSUBSTATE_ERROR: |
||
| 3606 | // Isochronous transfers are continuous until the user stops them. |
||
| 3607 | // Send an event that there is an error, and reset for the next |
||
| 3608 | // interval. |
||
| 3609 | pCurrentEndpoint->transferState = TSTATE_ISOCHRONOUS_WRITE | TSUBSTATE_ISOCHRONOUS_WRITE_DATA; |
||
| 3610 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 3611 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3612 | { |
||
| 3613 | USB_EVENT_DATA *data; |
||
| 3614 | |||
| 3615 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3616 | data->event = EVENT_BUS_ERROR; |
||
| 3617 | data->TransferData.dataCount = 0; |
||
| 3618 | data->TransferData.pUserData = NULL; |
||
| 3619 | data->TransferData.bErrorCode = pCurrentEndpoint->bErrorCode; |
||
| 3620 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3621 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3622 | data->TransferData.clientDriver = pCurrentEndpoint->clientDriver; |
||
| 3623 | } |
||
| 3624 | else |
||
| 3625 | { |
||
| 3626 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3627 | } |
||
| 3628 | #endif |
||
| 3629 | break; |
||
| 3630 | |||
| 3631 | default: |
||
| 3632 | illegalState = TRUE; |
||
| 3633 | break; |
||
| 3634 | } |
||
| 3635 | break; |
||
| 3636 | |||
| 3637 | default: |
||
| 3638 | illegalState = TRUE; |
||
| 3639 | break; |
||
| 3640 | } |
||
| 3641 | |||
| 3642 | if (illegalState) |
||
| 3643 | { |
||
| 3644 | // We should never use this, but in case we do, put the endpoint |
||
| 3645 | // in a recoverable state. |
||
| 3646 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3647 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3648 | } |
||
| 3649 | } |
||
| 3650 | |||
| 3651 | // If we've gone through all the endpoints, we do not have any more isochronous transfers. |
||
| 3652 | usbBusInfo.flags.bfIsochronousTransfersDone = 1; |
||
| 3653 | } |
||
| 3654 | #endif |
||
| 3655 | |||
| 3656 | #ifdef USB_SUPPORT_INTERRUPT_TRANSFERS |
||
| 3657 | if (!usbBusInfo.flags.bfInterruptTransfersDone) |
||
| 3658 | { |
||
| 3659 | // Look for any interrupt operations. |
||
| 3660 | if (_USB_FindServiceEndpoint( USB_TRANSFER_TYPE_INTERRUPT )) |
||
| 3661 | { |
||
| 3662 | switch (pCurrentEndpoint->transferState & TSTATE_MASK) |
||
| 3663 | { |
||
| 3664 | case TSTATE_INTERRUPT_READ: |
||
| 3665 | switch (pCurrentEndpoint->transferState & TSUBSTATE_MASK) |
||
| 3666 | { |
||
| 3667 | case TSUBSTATE_INTERRUPT_READ_DATA: |
||
| 3668 | if (pCurrentEndpoint->wIntervalCount == 0) |
||
| 3669 | { |
||
| 3670 | // Reset the interval count for the next packet. |
||
| 3671 | pCurrentEndpoint->wIntervalCount = pCurrentEndpoint->wInterval; |
||
| 3672 | |||
| 3673 | _USB_SetBDT( USB_TOKEN_IN ); |
||
| 3674 | _USB_SendToken( pCurrentEndpoint->bEndpointAddress, USB_TOKEN_IN ); |
||
| 3675 | return; |
||
| 3676 | } |
||
| 3677 | break; |
||
| 3678 | |||
| 3679 | case TSUBSTATE_INTERRUPT_READ_COMPLETE: |
||
| 3680 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3681 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3682 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 3683 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3684 | { |
||
| 3685 | USB_EVENT_DATA *data; |
||
| 3686 | |||
| 3687 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3688 | data->event = EVENT_TRANSFER; |
||
| 3689 | data->TransferData.dataCount = pCurrentEndpoint->dataCount; |
||
| 3690 | data->TransferData.pUserData = pCurrentEndpoint->pUserData; |
||
| 3691 | data->TransferData.bErrorCode = USB_SUCCESS; |
||
| 3692 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3693 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3694 | data->TransferData.clientDriver = pCurrentEndpoint->clientDriver; |
||
| 3695 | } |
||
| 3696 | else |
||
| 3697 | { |
||
| 3698 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3699 | } |
||
| 3700 | #endif |
||
| 3701 | break; |
||
| 3702 | |||
| 3703 | case TSUBSTATE_ERROR: |
||
| 3704 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3705 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3706 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 3707 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3708 | { |
||
| 3709 | USB_EVENT_DATA *data; |
||
| 3710 | |||
| 3711 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3712 | data->event = EVENT_BUS_ERROR; |
||
| 3713 | data->TransferData.dataCount = 0; |
||
| 3714 | data->TransferData.pUserData = NULL; |
||
| 3715 | data->TransferData.bErrorCode = pCurrentEndpoint->bErrorCode; |
||
| 3716 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3717 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3718 | data->TransferData.clientDriver = pCurrentEndpoint->clientDriver; |
||
| 3719 | } |
||
| 3720 | else |
||
| 3721 | { |
||
| 3722 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3723 | } |
||
| 3724 | #endif |
||
| 3725 | break; |
||
| 3726 | |||
| 3727 | default: |
||
| 3728 | illegalState = TRUE; |
||
| 3729 | break; |
||
| 3730 | } |
||
| 3731 | break; |
||
| 3732 | |||
| 3733 | case TSTATE_INTERRUPT_WRITE: |
||
| 3734 | switch (pCurrentEndpoint->transferState & TSUBSTATE_MASK) |
||
| 3735 | { |
||
| 3736 | case TSUBSTATE_INTERRUPT_WRITE_DATA: |
||
| 3737 | if (pCurrentEndpoint->wIntervalCount == 0) |
||
| 3738 | { |
||
| 3739 | // Reset the interval count for the next packet. |
||
| 3740 | pCurrentEndpoint->wIntervalCount = pCurrentEndpoint->wInterval; |
||
| 3741 | |||
| 3742 | _USB_SetBDT( USB_TOKEN_OUT ); |
||
| 3743 | _USB_SendToken( pCurrentEndpoint->bEndpointAddress, USB_TOKEN_OUT ); |
||
| 3744 | return; |
||
| 3745 | } |
||
| 3746 | break; |
||
| 3747 | |||
| 3748 | case TSUBSTATE_INTERRUPT_WRITE_COMPLETE: |
||
| 3749 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3750 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3751 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 3752 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3753 | { |
||
| 3754 | USB_EVENT_DATA *data; |
||
| 3755 | |||
| 3756 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3757 | data->event = EVENT_TRANSFER; |
||
| 3758 | data->TransferData.dataCount = pCurrentEndpoint->dataCount; |
||
| 3759 | data->TransferData.pUserData = pCurrentEndpoint->pUserData; |
||
| 3760 | data->TransferData.bErrorCode = USB_SUCCESS; |
||
| 3761 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3762 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3763 | data->TransferData.clientDriver = pCurrentEndpoint->clientDriver; |
||
| 3764 | } |
||
| 3765 | else |
||
| 3766 | { |
||
| 3767 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3768 | } |
||
| 3769 | #endif |
||
| 3770 | break; |
||
| 3771 | |||
| 3772 | case TSUBSTATE_ERROR: |
||
| 3773 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3774 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3775 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 3776 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3777 | { |
||
| 3778 | USB_EVENT_DATA *data; |
||
| 3779 | |||
| 3780 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3781 | data->event = EVENT_BUS_ERROR; |
||
| 3782 | data->TransferData.dataCount = 0; |
||
| 3783 | data->TransferData.pUserData = NULL; |
||
| 3784 | data->TransferData.bErrorCode = pCurrentEndpoint->bErrorCode; |
||
| 3785 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3786 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3787 | data->TransferData.clientDriver = pCurrentEndpoint->clientDriver; |
||
| 3788 | } |
||
| 3789 | else |
||
| 3790 | { |
||
| 3791 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3792 | } |
||
| 3793 | #endif |
||
| 3794 | break; |
||
| 3795 | |||
| 3796 | default: |
||
| 3797 | illegalState = TRUE; |
||
| 3798 | break; |
||
| 3799 | } |
||
| 3800 | break; |
||
| 3801 | |||
| 3802 | default: |
||
| 3803 | illegalState = TRUE; |
||
| 3804 | break; |
||
| 3805 | } |
||
| 3806 | |||
| 3807 | if (illegalState) |
||
| 3808 | { |
||
| 3809 | // We should never use this, but in case we do, put the endpoint |
||
| 3810 | // in a recoverable state. |
||
| 3811 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3812 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3813 | } |
||
| 3814 | } |
||
| 3815 | |||
| 3816 | // If we've gone through all the endpoints, we do not have any more interrupt transfers. |
||
| 3817 | usbBusInfo.flags.bfInterruptTransfersDone = 1; |
||
| 3818 | } |
||
| 3819 | #endif |
||
| 3820 | |||
| 3821 | #ifdef USB_SUPPORT_BULK_TRANSFERS |
||
| 3822 | #ifdef ALLOW_MULTIPLE_BULK_TRANSACTIONS_PER_FRAME |
||
| 3823 | TryBulk: |
||
| 3824 | #endif |
||
| 3825 | |||
| 3826 | if (!usbBusInfo.flags.bfBulkTransfersDone) |
||
| 3827 | { |
||
| 3828 | #ifndef ALLOW_MULTIPLE_BULK_TRANSACTIONS_PER_FRAME |
||
| 3829 | // Only go through this section once if we are not allowing multiple transactions |
||
| 3830 | // per frame. |
||
| 3831 | usbBusInfo.flags.bfBulkTransfersDone = 1; |
||
| 3832 | #endif |
||
| 3833 | |||
| 3834 | // Look for any bulk operations. Try to service all pending requests within the frame. |
||
| 3835 | if (_USB_FindServiceEndpoint( USB_TRANSFER_TYPE_BULK )) |
||
| 3836 | { |
||
| 3837 | switch (pCurrentEndpoint->transferState & TSTATE_MASK) |
||
| 3838 | { |
||
| 3839 | case TSTATE_BULK_READ: |
||
| 3840 | switch (pCurrentEndpoint->transferState & TSUBSTATE_MASK) |
||
| 3841 | { |
||
| 3842 | case TSUBSTATE_BULK_READ_DATA: |
||
| 3843 | _USB_SetBDT( USB_TOKEN_IN ); |
||
| 3844 | _USB_SendToken( pCurrentEndpoint->bEndpointAddress, USB_TOKEN_IN ); |
||
| 3845 | return; |
||
| 3846 | break; |
||
| 3847 | |||
| 3848 | case TSUBSTATE_BULK_READ_COMPLETE: |
||
| 3849 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3850 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3851 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 3852 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3853 | { |
||
| 3854 | USB_EVENT_DATA *data; |
||
| 3855 | |||
| 3856 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3857 | data->event = EVENT_TRANSFER; |
||
| 3858 | data->TransferData.dataCount = pCurrentEndpoint->dataCount; |
||
| 3859 | data->TransferData.pUserData = pCurrentEndpoint->pUserData; |
||
| 3860 | data->TransferData.bErrorCode = USB_SUCCESS; |
||
| 3861 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3862 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3863 | data->TransferData.clientDriver = pCurrentEndpoint->clientDriver; |
||
| 3864 | } |
||
| 3865 | else |
||
| 3866 | { |
||
| 3867 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3868 | } |
||
| 3869 | #endif |
||
| 3870 | break; |
||
| 3871 | |||
| 3872 | case TSUBSTATE_ERROR: |
||
| 3873 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3874 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3875 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 3876 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3877 | { |
||
| 3878 | USB_EVENT_DATA *data; |
||
| 3879 | |||
| 3880 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3881 | data->event = EVENT_BUS_ERROR; |
||
| 3882 | data->TransferData.dataCount = 0; |
||
| 3883 | data->TransferData.pUserData = NULL; |
||
| 3884 | data->TransferData.bErrorCode = pCurrentEndpoint->bErrorCode; |
||
| 3885 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3886 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3887 | data->TransferData.clientDriver = pCurrentEndpoint->clientDriver; |
||
| 3888 | } |
||
| 3889 | else |
||
| 3890 | { |
||
| 3891 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3892 | } |
||
| 3893 | #endif |
||
| 3894 | break; |
||
| 3895 | |||
| 3896 | default: |
||
| 3897 | illegalState = TRUE; |
||
| 3898 | break; |
||
| 3899 | } |
||
| 3900 | break; |
||
| 3901 | |||
| 3902 | case TSTATE_BULK_WRITE: |
||
| 3903 | switch (pCurrentEndpoint->transferState & TSUBSTATE_MASK) |
||
| 3904 | { |
||
| 3905 | case TSUBSTATE_BULK_WRITE_DATA: |
||
| 3906 | _USB_SetBDT( USB_TOKEN_OUT ); |
||
| 3907 | _USB_SendToken( pCurrentEndpoint->bEndpointAddress, USB_TOKEN_OUT ); |
||
| 3908 | return; |
||
| 3909 | break; |
||
| 3910 | |||
| 3911 | case TSUBSTATE_BULK_WRITE_COMPLETE: |
||
| 3912 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3913 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3914 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 3915 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3916 | { |
||
| 3917 | USB_EVENT_DATA *data; |
||
| 3918 | |||
| 3919 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3920 | data->event = EVENT_TRANSFER; |
||
| 3921 | data->TransferData.dataCount = pCurrentEndpoint->dataCount; |
||
| 3922 | data->TransferData.pUserData = pCurrentEndpoint->pUserData; |
||
| 3923 | data->TransferData.bErrorCode = USB_SUCCESS; |
||
| 3924 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3925 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3926 | data->TransferData.clientDriver = pCurrentEndpoint->clientDriver; |
||
| 3927 | } |
||
| 3928 | else |
||
| 3929 | { |
||
| 3930 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3931 | } |
||
| 3932 | #endif |
||
| 3933 | break; |
||
| 3934 | |||
| 3935 | case TSUBSTATE_ERROR: |
||
| 3936 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3937 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3938 | #if defined( USB_ENABLE_TRANSFER_EVENT ) |
||
| 3939 | if (StructQueueIsNotFull(&usbEventQueue, USB_EVENT_QUEUE_DEPTH)) |
||
| 3940 | { |
||
| 3941 | USB_EVENT_DATA *data; |
||
| 3942 | |||
| 3943 | data = StructQueueAdd(&usbEventQueue, USB_EVENT_QUEUE_DEPTH); |
||
| 3944 | data->event = EVENT_BUS_ERROR; |
||
| 3945 | data->TransferData.dataCount = 0; |
||
| 3946 | data->TransferData.pUserData = NULL; |
||
| 3947 | data->TransferData.bErrorCode = pCurrentEndpoint->bErrorCode; |
||
| 3948 | data->TransferData.bEndpointAddress = pCurrentEndpoint->bEndpointAddress; |
||
| 3949 | data->TransferData.bmAttributes.val = pCurrentEndpoint->bmAttributes.val; |
||
| 3950 | data->TransferData.clientDriver = pCurrentEndpoint->clientDriver; |
||
| 3951 | } |
||
| 3952 | else |
||
| 3953 | { |
||
| 3954 | pCurrentEndpoint->bmAttributes.val = USB_EVENT_QUEUE_FULL; |
||
| 3955 | } |
||
| 3956 | #endif |
||
| 3957 | break; |
||
| 3958 | |||
| 3959 | default: |
||
| 3960 | illegalState = TRUE; |
||
| 3961 | break; |
||
| 3962 | } |
||
| 3963 | break; |
||
| 3964 | |||
| 3965 | default: |
||
| 3966 | illegalState = TRUE; |
||
| 3967 | break; |
||
| 3968 | } |
||
| 3969 | |||
| 3970 | if (illegalState) |
||
| 3971 | { |
||
| 3972 | // We should never use this, but in case we do, put the endpoint |
||
| 3973 | // in a recoverable state. |
||
| 3974 | pCurrentEndpoint->transferState = TSTATE_IDLE; |
||
| 3975 | pCurrentEndpoint->status.bfTransferComplete = 1; |
||
| 3976 | } |
||
| 3977 | } |
||
| 3978 | |||
| 3979 | // We've gone through all the bulk transactions, but we have time for more. |
||
| 3980 | // If we have any bulk transactions, go back to the beginning of the list |
||
| 3981 | // and start over. |
||
| 3982 | #ifdef ALLOW_MULTIPLE_BULK_TRANSACTIONS_PER_FRAME |
||
| 3983 | if (usbBusInfo.countBulkTransactions) |
||
| 3984 | { |
||
| 3985 | usbBusInfo.lastBulkTransaction = 0; |
||
| 3986 | goto TryBulk; |
||
| 3987 | |||
| 3988 | } |
||
| 3989 | #endif |
||
| 3990 | |||
| 3991 | // If we've gone through all the endpoints, we do not have any more bulk transfers. |
||
| 3992 | usbBusInfo.flags.bfBulkTransfersDone = 1; |
||
| 3993 | } |
||
| 3994 | #endif |
||
| 3995 | |||
| 3996 | return; |
||
| 3997 | } |
||
| 3998 | |||
| 3999 | |||
| 4000 | /**************************************************************************** |
||
| 4001 | Function: |
||
| 4002 | BOOL _USB_FindServiceEndpoint( BYTE transferType ) |
||
| 4003 | |||
| 4004 | Description: |
||
| 4005 | This function finds an endpoint of the specified transfer type that is |
||
| 4006 | ready for servicing. If it finds one, usbDeviceInfo.pCurrentEndpoint is |
||
| 4007 | updated to point to the endpoint information structure. |
||
| 4008 | |||
| 4009 | Precondition: |
||
| 4010 | None |
||
| 4011 | |||
| 4012 | Parameters: |
||
| 4013 | BYTE transferType - Endpoint transfer type. Valid values are: |
||
| 4014 | * USB_TRANSFER_TYPE_CONTROL |
||
| 4015 | * USB_TRANSFER_TYPE_ISOCHRONOUS |
||
| 4016 | * USB_TRANSFER_TYPE_INTERRUPT |
||
| 4017 | * USB_TRANSFER_TYPE_BULK |
||
| 4018 | |||
| 4019 | Return Values: |
||
| 4020 | TRUE - An endpoint of the indicated transfer type needs to be serviced, |
||
| 4021 | and pCurrentEndpoint has been updated to point to the endpoint. |
||
| 4022 | FALSE - No endpoints of the indicated transfer type need to be serviced. |
||
| 4023 | |||
| 4024 | Remarks: |
||
| 4025 | The EP 0 block is retained. |
||
| 4026 | ***************************************************************************/ |
||
| 4027 | BOOL _USB_FindServiceEndpoint( BYTE transferType ) |
||
| 4028 | { |
||
| 4029 | USB_ENDPOINT_INFO *pEndpoint; |
||
| 4030 | USB_INTERFACE_INFO *pInterface; |
||
| 4031 | |||
| 4032 | // Check endpoint 0. |
||
| 4033 | if ((usbDeviceInfo.pEndpoint0->bmAttributes.bfTransferType == transferType) && |
||
| 4034 | !usbDeviceInfo.pEndpoint0->status.bfTransferComplete) |
||
| 4035 | { |
||
| 4036 | pCurrentEndpoint = usbDeviceInfo.pEndpoint0; |
||
| 4037 | return TRUE; |
||
| 4038 | } |
||
| 4039 | |||
| 4040 | usbBusInfo.countBulkTransactions = 0; |
||
| 4041 | pEndpoint = NULL; |
||
| 4042 | pInterface = usbDeviceInfo.pInterfaceList; |
||
| 4043 | if (pInterface && pInterface->pCurrentSetting) |
||
| 4044 | { |
||
| 4045 | pEndpoint = pInterface->pCurrentSetting->pEndpointList; |
||
| 4046 | } |
||
| 4047 | |||
| 4048 | while (pInterface) |
||
| 4049 | { |
||
| 4050 | if (pEndpoint != NULL) |
||
| 4051 | { |
||
| 4052 | if (pEndpoint->bmAttributes.bfTransferType == transferType) |
||
| 4053 | { |
||
| 4054 | switch (transferType) |
||
| 4055 | { |
||
| 4056 | case USB_TRANSFER_TYPE_CONTROL: |
||
| 4057 | if (!pEndpoint->status.bfTransferComplete) |
||
| 4058 | { |
||
| 4059 | pCurrentEndpoint = pEndpoint; |
||
| 4060 | return TRUE; |
||
| 4061 | } |
||
| 4062 | break; |
||
| 4063 | |||
| 4064 | #ifdef USB_SUPPORT_ISOCHRONOUS_TRANSFERS |
||
| 4065 | case USB_TRANSFER_TYPE_ISOCHRONOUS: |
||
| 4066 | #endif |
||
| 4067 | #ifdef USB_SUPPORT_INTERRUPT_TRANSFERS |
||
| 4068 | case USB_TRANSFER_TYPE_INTERRUPT: |
||
| 4069 | #endif |
||
| 4070 | #if defined( USB_SUPPORT_ISOCHRONOUS_TRANSFERS ) || defined( USB_SUPPORT_INTERRUPT_TRANSFERS ) |
||
| 4071 | if (pEndpoint->status.bfTransferComplete) |
||
| 4072 | { |
||
| 4073 | // The endpoint doesn't need servicing. If the interval count |
||
| 4074 | // has reached 0 and the user has not initiated another transaction, |
||
| 4075 | // reset the interval count for the next interval. |
||
| 4076 | if (pEndpoint->wIntervalCount == 0) |
||
| 4077 | { |
||
| 4078 | // Reset the interval count for the next packet. |
||
| 4079 | pEndpoint->wIntervalCount = pEndpoint->wInterval; |
||
| 4080 | } |
||
| 4081 | } |
||
| 4082 | else |
||
| 4083 | { |
||
| 4084 | pCurrentEndpoint = pEndpoint; |
||
| 4085 | return TRUE; |
||
| 4086 | } |
||
| 4087 | break; |
||
| 4088 | #endif |
||
| 4089 | |||
| 4090 | #ifdef USB_SUPPORT_BULK_TRANSFERS |
||
| 4091 | case USB_TRANSFER_TYPE_BULK: |
||
| 4092 | #ifdef ALLOW_MULTIPLE_NAKS_PER_FRAME |
||
| 4093 | if (!pEndpoint->status.bfTransferComplete) |
||
| 4094 | #else |
||
| 4095 | if (!pEndpoint->status.bfTransferComplete && |
||
| 4096 | !pEndpoint->status.bfLastTransferNAKd) |
||
| 4097 | #endif |
||
| 4098 | { |
||
| 4099 | usbBusInfo.countBulkTransactions ++; |
||
| 4100 | if (usbBusInfo.countBulkTransactions > usbBusInfo.lastBulkTransaction) |
||
| 4101 | { |
||
| 4102 | usbBusInfo.lastBulkTransaction = usbBusInfo.countBulkTransactions; |
||
| 4103 | pCurrentEndpoint = pEndpoint; |
||
| 4104 | return TRUE; |
||
| 4105 | } |
||
| 4106 | } |
||
| 4107 | break; |
||
| 4108 | #endif |
||
| 4109 | } |
||
| 4110 | } |
||
| 4111 | |||
| 4112 | // Go to the next endpoint. |
||
| 4113 | pEndpoint = pEndpoint->next; |
||
| 4114 | } |
||
| 4115 | |||
| 4116 | if (pEndpoint == NULL) |
||
| 4117 | { |
||
| 4118 | // Go to the next interface. |
||
| 4119 | pInterface = pInterface->next; |
||
| 4120 | if (pInterface && pInterface->pCurrentSetting) |
||
| 4121 | { |
||
| 4122 | pEndpoint = pInterface->pCurrentSetting->pEndpointList; |
||
| 4123 | } |
||
| 4124 | } |
||
| 4125 | } |
||
| 4126 | |||
| 4127 | // No endpoints with the desired description are ready for servicing. |
||
| 4128 | return FALSE; |
||
| 4129 | } |
||
| 4130 | |||
| 4131 | |||
| 4132 | /**************************************************************************** |
||
| 4133 | Function: |
||
| 4134 | void _USB_FreeConfigMemory( void ) |
||
| 4135 | |||
| 4136 | Description: |
||
| 4137 | This function frees the interface and endpoint lists associated |
||
| 4138 | with a configuration. |
||
| 4139 | |||
| 4140 | Precondition: |
||
| 4141 | None |
||
| 4142 | |||
| 4143 | Parameters: |
||
| 4144 | None - None |
||
| 4145 | |||
| 4146 | Returns: |
||
| 4147 | None |
||
| 4148 | |||
| 4149 | Remarks: |
||
| 4150 | The EP 0 block is retained. |
||
| 4151 | ***************************************************************************/ |
||
| 4152 | |||
| 4153 | void _USB_FreeConfigMemory( void ) |
||
| 4154 | { |
||
| 4155 | USB_INTERFACE_INFO *pTempInterface; |
||
| 4156 | USB_INTERFACE_SETTING_INFO *pTempSetting; |
||
| 4157 | USB_ENDPOINT_INFO *pTempEndpoint; |
||
| 4158 | |||
| 4159 | while (usbDeviceInfo.pInterfaceList != NULL) |
||
| 4160 | { |
||
| 4161 | pTempInterface = usbDeviceInfo.pInterfaceList->next; |
||
| 4162 | |||
| 4163 | while (usbDeviceInfo.pInterfaceList->pInterfaceSettings != NULL) |
||
| 4164 | { |
||
| 4165 | pTempSetting = usbDeviceInfo.pInterfaceList->pInterfaceSettings->next; |
||
| 4166 | |||
| 4167 | while (usbDeviceInfo.pInterfaceList->pInterfaceSettings->pEndpointList != NULL) |
||
| 4168 | { |
||
| 4169 | pTempEndpoint = usbDeviceInfo.pInterfaceList->pInterfaceSettings->pEndpointList->next; |
||
| 4170 | USB_FREE_AND_CLEAR( usbDeviceInfo.pInterfaceList->pInterfaceSettings->pEndpointList ); |
||
| 4171 | usbDeviceInfo.pInterfaceList->pInterfaceSettings->pEndpointList = pTempEndpoint; |
||
| 4172 | } |
||
| 4173 | USB_FREE_AND_CLEAR( usbDeviceInfo.pInterfaceList->pInterfaceSettings ); |
||
| 4174 | usbDeviceInfo.pInterfaceList->pInterfaceSettings = pTempSetting; |
||
| 4175 | } |
||
| 4176 | USB_FREE_AND_CLEAR( usbDeviceInfo.pInterfaceList ); |
||
| 4177 | usbDeviceInfo.pInterfaceList = pTempInterface; |
||
| 4178 | } |
||
| 4179 | |||
| 4180 | pCurrentEndpoint = usbDeviceInfo.pEndpoint0; |
||
| 4181 | |||
| 4182 | } // _USB_FreeConfigMemory |
||
| 4183 | |||
| 4184 | |||
| 4185 | /**************************************************************************** |
||
| 4186 | Function: |
||
| 4187 | void _USB_FreeMemory( void ) |
||
| 4188 | |||
| 4189 | Description: |
||
| 4190 | This function frees all memory that can be freed. Only the EP0 |
||
| 4191 | information block is retained. |
||
| 4192 | |||
| 4193 | Precondition: |
||
| 4194 | None |
||
| 4195 | |||
| 4196 | Parameters: |
||
| 4197 | None - None |
||
| 4198 | |||
| 4199 | Returns: |
||
| 4200 | None |
||
| 4201 | |||
| 4202 | Remarks: |
||
| 4203 | None |
||
| 4204 | ***************************************************************************/ |
||
| 4205 | |||
| 4206 | void _USB_FreeMemory( void ) |
||
| 4207 | { |
||
| 4208 | BYTE *pTemp; |
||
| 4209 | |||
| 4210 | while (usbDeviceInfo.pConfigurationDescriptorList != NULL) |
||
| 4211 | { |
||
| 4212 | pTemp = (BYTE *)usbDeviceInfo.pConfigurationDescriptorList->next; |
||
| 4213 | USB_FREE_AND_CLEAR( usbDeviceInfo.pConfigurationDescriptorList->descriptor ); |
||
| 4214 | USB_FREE_AND_CLEAR( usbDeviceInfo.pConfigurationDescriptorList ); |
||
| 4215 | usbDeviceInfo.pConfigurationDescriptorList = (USB_CONFIGURATION *)pTemp; |
||
| 4216 | } |
||
| 4217 | if (pDeviceDescriptor != NULL) |
||
| 4218 | { |
||
| 4219 | USB_FREE_AND_CLEAR( pDeviceDescriptor ); |
||
| 4220 | } |
||
| 4221 | if (pEP0Data != NULL) |
||
| 4222 | { |
||
| 4223 | USB_FREE_AND_CLEAR( pEP0Data ); |
||
| 4224 | } |
||
| 4225 | |||
| 4226 | _USB_FreeConfigMemory(); |
||
| 4227 | |||
| 4228 | } |
||
| 4229 | |||
| 4230 | |||
| 4231 | /**************************************************************************** |
||
| 4232 | Function: |
||
| 4233 | void _USB_InitControlRead( USB_ENDPOINT_INFO *pEndpoint, |
||
| 4234 | BYTE *pControlData, WORD controlSize, BYTE *pData, |
||
| 4235 | WORD size ) |
||
| 4236 | |||
| 4237 | Description: |
||
| 4238 | This function sets up the endpoint information for a control (SETUP) |
||
| 4239 | transfer that will read information. |
||
| 4240 | |||
| 4241 | Precondition: |
||
| 4242 | All error checking must be done prior to calling this function. |
||
| 4243 | |||
| 4244 | Parameters: |
||
| 4245 | USB_ENDPOINT_INFO *pEndpoint - Points to the desired endpoint |
||
| 4246 | in the endpoint information list. |
||
| 4247 | BYTE *pControlData - Points to the SETUP message. |
||
| 4248 | WORD controlSize - Size of the SETUP message. |
||
| 4249 | BYTE *pData - Points to where the read data |
||
| 4250 | is to be stored. |
||
| 4251 | WORD size - Number of data bytes to read. |
||
| 4252 | |||
| 4253 | Returns: |
||
| 4254 | None |
||
| 4255 | |||
| 4256 | Remarks: |
||
| 4257 | Since endpoint servicing is interrupt driven, the bfTransferComplete |
||
| 4258 | flag must be set last. |
||
| 4259 | ***************************************************************************/ |
||
| 4260 | |||
| 4261 | void _USB_InitControlRead( USB_ENDPOINT_INFO *pEndpoint, BYTE *pControlData, WORD controlSize, |
||
| 4262 | BYTE *pData, WORD size ) |
||
| 4263 | { |
||
| 4264 | pEndpoint->status.bfStalled = 0; |
||
| 4265 | pEndpoint->status.bfError = 0; |
||
| 4266 | pEndpoint->status.bfUserAbort = 0; |
||
| 4267 | pEndpoint->status.bfTransferSuccessful = 0; |
||
| 4268 | pEndpoint->status.bfErrorCount = 0; |
||
| 4269 | pEndpoint->status.bfLastTransferNAKd = 0; |
||
| 4270 | pEndpoint->pUserData = pData; |
||
| 4271 | pEndpoint->dataCount = 0; |
||
| 4272 | pEndpoint->dataCountMax = size; |
||
| 4273 | pEndpoint->countNAKs = 0; |
||
| 4274 | |||
| 4275 | pEndpoint->pUserDataSETUP = pControlData; |
||
| 4276 | pEndpoint->dataCountMaxSETUP = controlSize; |
||
| 4277 | pEndpoint->transferState = TSTATE_CONTROL_READ; |
||
| 4278 | |||
| 4279 | // Set the flag last so all the parameters are set for an interrupt. |
||
| 4280 | pEndpoint->status.bfTransferComplete = 0; |
||
| 4281 | } |
||
| 4282 | |||
| 4283 | |||
| 4284 | /**************************************************************************** |
||
| 4285 | Function: |
||
| 4286 | void _USB_InitControlWrite( USB_ENDPOINT_INFO *pEndpoint, |
||
| 4287 | BYTE *pControlData, WORD controlSize, BYTE *pData, |
||
| 4288 | WORD size ) |
||
| 4289 | |||
| 4290 | Description: |
||
| 4291 | This function sets up the endpoint information for a control (SETUP) |
||
| 4292 | transfer that will write information. |
||
| 4293 | |||
| 4294 | Precondition: |
||
| 4295 | All error checking must be done prior to calling this function. |
||
| 4296 | |||
| 4297 | Parameters: |
||
| 4298 | USB_ENDPOINT_INFO *pEndpoint - Points to the desired endpoint |
||
| 4299 | in the endpoint information list. |
||
| 4300 | BYTE *pControlData - Points to the SETUP message. |
||
| 4301 | WORD controlSize - Size of the SETUP message. |
||
| 4302 | BYTE *pData - Points to where the write data |
||
| 4303 | is to be stored. |
||
| 4304 | WORD size - Number of data bytes to write. |
||
| 4305 | |||
| 4306 | Returns: |
||
| 4307 | None |
||
| 4308 | |||
| 4309 | Remarks: |
||
| 4310 | Since endpoint servicing is interrupt driven, the bfTransferComplete |
||
| 4311 | flag must be set last. |
||
| 4312 | ***************************************************************************/ |
||
| 4313 | |||
| 4314 | void _USB_InitControlWrite( USB_ENDPOINT_INFO *pEndpoint, BYTE *pControlData, |
||
| 4315 | WORD controlSize, BYTE *pData, WORD size ) |
||
| 4316 | { |
||
| 4317 | pEndpoint->status.bfStalled = 0; |
||
| 4318 | pEndpoint->status.bfError = 0; |
||
| 4319 | pEndpoint->status.bfUserAbort = 0; |
||
| 4320 | pEndpoint->status.bfTransferSuccessful = 0; |
||
| 4321 | pEndpoint->status.bfErrorCount = 0; |
||
| 4322 | pEndpoint->status.bfLastTransferNAKd = 0; |
||
| 4323 | pEndpoint->pUserData = pData; |
||
| 4324 | pEndpoint->dataCount = 0; |
||
| 4325 | pEndpoint->dataCountMax = size; |
||
| 4326 | pEndpoint->countNAKs = 0; |
||
| 4327 | |||
| 4328 | pEndpoint->pUserDataSETUP = pControlData; |
||
| 4329 | pEndpoint->dataCountMaxSETUP = controlSize; |
||
| 4330 | |||
| 4331 | if (size == 0) |
||
| 4332 | { |
||
| 4333 | pEndpoint->transferState = TSTATE_CONTROL_NO_DATA; |
||
| 4334 | } |
||
| 4335 | else |
||
| 4336 | { |
||
| 4337 | pEndpoint->transferState = TSTATE_CONTROL_WRITE; |
||
| 4338 | } |
||
| 4339 | |||
| 4340 | // Set the flag last so all the parameters are set for an interrupt. |
||
| 4341 | pEndpoint->status.bfTransferComplete = 0; |
||
| 4342 | } |
||
| 4343 | |||
| 4344 | |||
| 4345 | /**************************************************************************** |
||
| 4346 | Function: |
||
| 4347 | void _USB_InitRead( USB_ENDPOINT_INFO *pEndpoint, BYTE *pData, |
||
| 4348 | WORD size ) |
||
| 4349 | |||
| 4350 | Description: |
||
| 4351 | This function sets up the endpoint information for an interrupt, |
||
| 4352 | isochronous, or bulk read. If the transfer is isochronous, the pData |
||
| 4353 | and size parameters have different meaning. |
||
| 4354 | |||
| 4355 | Precondition: |
||
| 4356 | All error checking must be done prior to calling this function. |
||
| 4357 | |||
| 4358 | Parameters: |
||
| 4359 | USB_ENDPOINT_INFO *pEndpoint - Points to the desired endpoint in the |
||
| 4360 | endpoint information list. |
||
| 4361 | BYTE *pData - Points to where the data is to be |
||
| 4362 | stored. If the endpoint is isochronous, |
||
| 4363 | this points to an ISOCHRONOUS_DATA_BUFFERS |
||
| 4364 | structure. |
||
| 4365 | WORD size - Number of data bytes to read. If the |
||
| 4366 | endpoint is isochronous, this is the number |
||
| 4367 | of data buffer pointers pointed to by |
||
| 4368 | pData. |
||
| 4369 | |||
| 4370 | Returns: |
||
| 4371 | None |
||
| 4372 | |||
| 4373 | Remarks: |
||
| 4374 | * Control reads should use the routine _USB_InitControlRead(). Since |
||
| 4375 | endpoint servicing is interrupt driven, the bfTransferComplete flag |
||
| 4376 | must be set last. |
||
| 4377 | |||
| 4378 | * For interrupt and isochronous endpoints, we let the interval count |
||
| 4379 | free run. The transaction will begin when the interval count |
||
| 4380 | reaches 0. |
||
| 4381 | ***************************************************************************/ |
||
| 4382 | |||
| 4383 | void _USB_InitRead( USB_ENDPOINT_INFO *pEndpoint, BYTE *pData, WORD size ) |
||
| 4384 | { |
||
| 4385 | pEndpoint->status.bfUserAbort = 0; |
||
| 4386 | pEndpoint->status.bfTransferSuccessful = 0; |
||
| 4387 | pEndpoint->status.bfErrorCount = 0; |
||
| 4388 | pEndpoint->status.bfLastTransferNAKd = 0; |
||
| 4389 | pEndpoint->pUserData = pData; |
||
| 4390 | pEndpoint->dataCount = 0; |
||
| 4391 | pEndpoint->dataCountMax = size; // Not used for isochronous. |
||
| 4392 | pEndpoint->countNAKs = 0; |
||
| 4393 | |||
| 4394 | if (pEndpoint->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_INTERRUPT) |
||
| 4395 | { |
||
| 4396 | pEndpoint->transferState = TSTATE_INTERRUPT_READ; |
||
| 4397 | } |
||
| 4398 | else if (pEndpoint->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_ISOCHRONOUS) |
||
| 4399 | { |
||
| 4400 | pEndpoint->transferState = TSTATE_ISOCHRONOUS_READ; |
||
| 4401 | ((ISOCHRONOUS_DATA *)pEndpoint->pUserData)->currentBufferUSB = 0; |
||
| 4402 | } |
||
| 4403 | else // Bulk |
||
| 4404 | { |
||
| 4405 | pEndpoint->transferState = TSTATE_BULK_READ; |
||
| 4406 | } |
||
| 4407 | |||
| 4408 | // Set the flag last so all the parameters are set for an interrupt. |
||
| 4409 | pEndpoint->status.bfTransferComplete = 0; |
||
| 4410 | } |
||
| 4411 | |||
| 4412 | /**************************************************************************** |
||
| 4413 | Function: |
||
| 4414 | void _USB_InitWrite( USB_ENDPOINT_INFO *pEndpoint, BYTE *pData, |
||
| 4415 | WORD size ) |
||
| 4416 | |||
| 4417 | Description: |
||
| 4418 | This function sets up the endpoint information for an interrupt, |
||
| 4419 | isochronous, or bulk write. If the transfer is isochronous, the pData |
||
| 4420 | and size parameters have different meaning. |
||
| 4421 | |||
| 4422 | Precondition: |
||
| 4423 | All error checking must be done prior to calling this function. |
||
| 4424 | |||
| 4425 | Parameters: |
||
| 4426 | USB_ENDPOINT_INFO *pEndpoint - Points to the desired endpoint in the |
||
| 4427 | endpoint information list. |
||
| 4428 | BYTE *pData - Points to where the data to send is |
||
| 4429 | stored. If the endpoint is isochronous, |
||
| 4430 | this points to an ISOCHRONOUS_DATA_BUFFERS |
||
| 4431 | structure. |
||
| 4432 | WORD size - Number of data bytes to write. If the |
||
| 4433 | endpoint is isochronous, this is the number |
||
| 4434 | of data buffer pointers pointed to by |
||
| 4435 | pData. |
||
| 4436 | |||
| 4437 | Returns: |
||
| 4438 | None |
||
| 4439 | |||
| 4440 | Remarks: |
||
| 4441 | * Control writes should use the routine _USB_InitControlWrite(). Since |
||
| 4442 | endpoint servicing is interrupt driven, the bfTransferComplete flag |
||
| 4443 | must be set last. |
||
| 4444 | |||
| 4445 | * For interrupt and isochronous endpoints, we let the interval count |
||
| 4446 | free run. The transaction will begin when the interval count |
||
| 4447 | reaches 0. |
||
| 4448 | ***************************************************************************/ |
||
| 4449 | |||
| 4450 | void _USB_InitWrite( USB_ENDPOINT_INFO *pEndpoint, BYTE *pData, WORD size ) |
||
| 4451 | { |
||
| 4452 | pEndpoint->status.bfUserAbort = 0; |
||
| 4453 | pEndpoint->status.bfTransferSuccessful = 0; |
||
| 4454 | pEndpoint->status.bfErrorCount = 0; |
||
| 4455 | pEndpoint->status.bfLastTransferNAKd = 0; |
||
| 4456 | pEndpoint->pUserData = pData; |
||
| 4457 | pEndpoint->dataCount = 0; |
||
| 4458 | pEndpoint->dataCountMax = size; // Not used for isochronous. |
||
| 4459 | pEndpoint->countNAKs = 0; |
||
| 4460 | |||
| 4461 | if (pEndpoint->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_INTERRUPT) |
||
| 4462 | { |
||
| 4463 | pEndpoint->transferState = TSTATE_INTERRUPT_WRITE; |
||
| 4464 | } |
||
| 4465 | else if (pEndpoint->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_ISOCHRONOUS) |
||
| 4466 | { |
||
| 4467 | pEndpoint->transferState = TSTATE_ISOCHRONOUS_WRITE; |
||
| 4468 | ((ISOCHRONOUS_DATA *)pEndpoint->pUserData)->currentBufferUSB = 0; |
||
| 4469 | } |
||
| 4470 | else // Bulk |
||
| 4471 | { |
||
| 4472 | pEndpoint->transferState = TSTATE_BULK_WRITE; |
||
| 4473 | } |
||
| 4474 | |||
| 4475 | // Set the flag last so all the parameters are set for an interrupt. |
||
| 4476 | pEndpoint->status.bfTransferComplete = 0; |
||
| 4477 | } |
||
| 4478 | |||
| 4479 | |||
| 4480 | /**************************************************************************** |
||
| 4481 | Function: |
||
| 4482 | void _USB_NotifyClients( BYTE address, USB_EVENT event, void *data, |
||
| 4483 | unsigned int size ) |
||
| 4484 | |||
| 4485 | Description: |
||
| 4486 | This routine notifies all active client drivers for the given device of |
||
| 4487 | the given event. |
||
| 4488 | |||
| 4489 | Precondition: |
||
| 4490 | None |
||
| 4491 | |||
| 4492 | Parameters: |
||
| 4493 | BYTE address - Address of the device generating the event |
||
| 4494 | USB_EVENT event - Event ID |
||
| 4495 | void *data - Pointer to event data |
||
| 4496 | unsigned int size - Size of data pointed to by data |
||
| 4497 | |||
| 4498 | Returns: |
||
| 4499 | None |
||
| 4500 | |||
| 4501 | Remarks: |
||
| 4502 | When this driver is modified to support multiple devices, this function |
||
| 4503 | will require modification. |
||
| 4504 | ***************************************************************************/ |
||
| 4505 | |||
| 4506 | void _USB_NotifyClients( BYTE address, USB_EVENT event, void *data, unsigned int size ) |
||
| 4507 | { |
||
| 4508 | USB_INTERFACE_INFO *pInterface; |
||
| 4509 | |||
| 4510 | // Some events go to all drivers, some only to specific drivers. |
||
| 4511 | switch(event) |
||
| 4512 | { |
||
| 4513 | case EVENT_TRANSFER: |
||
| 4514 | case EVENT_BUS_ERROR: |
||
| 4515 | if (((HOST_TRANSFER_DATA *)data)->clientDriver != CLIENT_DRIVER_HOST) |
||
| 4516 | { |
||
| 4517 | usbClientDrvTable[((HOST_TRANSFER_DATA *)data)->clientDriver].EventHandler(address, event, data, size); |
||
| 4518 | } |
||
| 4519 | break; |
||
| 4520 | default: |
||
| 4521 | pInterface = usbDeviceInfo.pInterfaceList; |
||
| 4522 | while (pInterface != NULL) // Scan the interface list for all active drivers. |
||
| 4523 | { |
||
| 4524 | usbClientDrvTable[pInterface->clientDriver].EventHandler(address, event, data, size); |
||
| 4525 | pInterface = pInterface->next; |
||
| 4526 | } |
||
| 4527 | break; |
||
| 4528 | } |
||
| 4529 | } // _USB_NotifyClients |
||
| 4530 | |||
| 4531 | |||
| 4532 | /**************************************************************************** |
||
| 4533 | Function: |
||
| 4534 | BOOL _USB_ParseConfigurationDescriptor( void ) |
||
| 4535 | |||
| 4536 | Description: |
||
| 4537 | This function parses all the endpoint descriptors for the required |
||
| 4538 | setting of the required interface and sets up the internal endpoint |
||
| 4539 | information. |
||
| 4540 | |||
| 4541 | Precondition: |
||
| 4542 | pCurrentConfigurationDescriptor points to a valid Configuration |
||
| 4543 | Descriptor, which contains the endpoint descriptors. The current |
||
| 4544 | interface and the current interface settings must be set up in |
||
| 4545 | usbDeviceInfo. |
||
| 4546 | |||
| 4547 | Parameters: |
||
| 4548 | None - None |
||
| 4549 | |||
| 4550 | Returns: |
||
| 4551 | TRUE - Successful |
||
| 4552 | FALSE - Configuration not supported. |
||
| 4553 | |||
| 4554 | Remarks: |
||
| 4555 | * This function also automatically resets all endpoints (except |
||
| 4556 | endpoint 0) to DATA0, so _USB_ResetDATA0 does not have to be |
||
| 4557 | called. |
||
| 4558 | |||
| 4559 | * If the configuration is not supported, the caller will need to clean |
||
| 4560 | up, freeing memory by calling _USB_FreeConfigMemory. |
||
| 4561 | |||
| 4562 | * We do not currently implement checks for descriptors that are shorter |
||
| 4563 | than the expected length, in the case of invalid USB Peripherals. |
||
| 4564 | |||
| 4565 | * If there is not enough available heap space for storing the |
||
| 4566 | interface or endpoint information, this function will return FALSE. |
||
| 4567 | Currently, there is no other mechanism for informing the user of |
||
| 4568 | an out of dynamic memory condition. |
||
| 4569 | |||
| 4570 | * We are assuming that we can support a single interface on a single |
||
| 4571 | device. When the driver is modified to support multiple devices, |
||
| 4572 | each endpoint should be checked to ensure that we have enough |
||
| 4573 | bandwidth to support it. |
||
| 4574 | ***************************************************************************/ |
||
| 4575 | |||
| 4576 | BOOL _USB_ParseConfigurationDescriptor( void ) |
||
| 4577 | { |
||
| 4578 | BYTE bAlternateSetting; |
||
| 4579 | BYTE bDescriptorType; |
||
| 4580 | BYTE bInterfaceNumber; |
||
| 4581 | BYTE bLength; |
||
| 4582 | BYTE bNumEndpoints; |
||
| 4583 | BYTE bNumInterfaces; |
||
| 4584 | BYTE bMaxPower; |
||
| 4585 | BOOL error; |
||
| 4586 | BYTE Class; |
||
| 4587 | BYTE SubClass; |
||
| 4588 | BYTE Protocol; |
||
| 4589 | BYTE ClientDriver; |
||
| 4590 | WORD wTotalLength; |
||
| 4591 | |||
| 4592 | BYTE currentAlternateSetting; |
||
| 4593 | BYTE currentConfiguration; |
||
| 4594 | BYTE currentEndpoint; |
||
| 4595 | BYTE currentInterface; |
||
| 4596 | WORD index; |
||
| 4597 | USB_ENDPOINT_INFO *newEndpointInfo; |
||
| 4598 | USB_INTERFACE_INFO *newInterfaceInfo; |
||
| 4599 | USB_INTERFACE_SETTING_INFO *newSettingInfo; |
||
| 4600 | USB_VBUS_POWER_EVENT_DATA powerRequest; |
||
| 4601 | USB_INTERFACE_INFO *pTempInterfaceList; |
||
| 4602 | BYTE *ptr; |
||
| 4603 | |||
| 4604 | // Prime the loops. |
||
| 4605 | currentEndpoint = 0; |
||
| 4606 | error = FALSE; |
||
| 4607 | index = 0; |
||
| 4608 | ptr = pCurrentConfigurationDescriptor; |
||
| 4609 | currentInterface = 0; |
||
| 4610 | currentAlternateSetting = 0; |
||
| 4611 | pTempInterfaceList = usbDeviceInfo.pInterfaceList; // Don't set until everything is in place. |
||
| 4612 | |||
| 4613 | // Assume no OTG support (determine otherwise, below). |
||
| 4614 | usbDeviceInfo.flags.bfSupportsOTG = 0; |
||
| 4615 | usbDeviceInfo.flags.bfConfiguredOTG = 1; |
||
| 4616 | |||
| 4617 | #ifdef USB_SUPPORT_OTG |
||
| 4618 | usbDeviceInfo.flags.bfAllowHNP = 1; //Allow HNP From Host |
||
| 4619 | #endif |
||
| 4620 | |||
| 4621 | // Load up the values from the Configuration Descriptor |
||
| 4622 | bLength = *ptr++; |
||
| 4623 | bDescriptorType = *ptr++; |
||
| 4624 | wTotalLength = *ptr++; // In case these are not word aligned |
||
| 4625 | wTotalLength += (*ptr++) << 8; |
||
| 4626 | bNumInterfaces = *ptr++; |
||
| 4627 | currentConfiguration = *ptr++; // bConfigurationValue |
||
| 4628 | ptr++; // iConfiguration |
||
| 4629 | ptr++; // bmAttributes |
||
| 4630 | bMaxPower = *ptr; |
||
| 4631 | |||
| 4632 | // Check Max Power to see if we can support this configuration. |
||
| 4633 | powerRequest.current = bMaxPower; |
||
| 4634 | powerRequest.port = 0; // Port 0 |
||
| 4635 | if (!USB_HOST_APP_EVENT_HANDLER( USB_ROOT_HUB, EVENT_VBUS_REQUEST_POWER, |
||
| 4636 | &powerRequest, sizeof(USB_VBUS_POWER_EVENT_DATA) )) |
||
| 4637 | { |
||
| 4638 | usbDeviceInfo.errorCode = USB_ERROR_INSUFFICIENT_POWER; |
||
| 4639 | error = TRUE; |
||
| 4640 | } |
||
| 4641 | |||
| 4642 | // Skip over the rest of the Configuration Descriptor |
||
| 4643 | index += bLength; |
||
| 4644 | ptr = &pCurrentConfigurationDescriptor[index]; |
||
| 4645 | |||
| 4646 | while (!error && (index < wTotalLength)) |
||
| 4647 | { |
||
| 4648 | // Check the descriptor length and type |
||
| 4649 | bLength = *ptr++; |
||
| 4650 | bDescriptorType = *ptr++; |
||
| 4651 | |||
| 4652 | |||
| 4653 | // Find the OTG discriptor (if present) |
||
| 4654 | if (bDescriptorType == USB_DESCRIPTOR_OTG) |
||
| 4655 | { |
||
| 4656 | // We found an OTG Descriptor, so the device supports OTG. |
||
| 4657 | usbDeviceInfo.flags.bfSupportsOTG = 1; |
||
| 4658 | usbDeviceInfo.attributesOTG = *ptr; |
||
| 4659 | |||
| 4660 | // See if we need to send the SET FEATURE command. If we do, |
||
| 4661 | // clear the bConfiguredOTG flag. |
||
| 4662 | if ( (usbDeviceInfo.attributesOTG & OTG_HNP_SUPPORT) && (usbDeviceInfo.flags.bfAllowHNP)) |
||
| 4663 | { |
||
| 4664 | usbDeviceInfo.flags.bfConfiguredOTG = 0; |
||
| 4665 | } |
||
| 4666 | else |
||
| 4667 | { |
||
| 4668 | usbDeviceInfo.flags.bfAllowHNP = 0; |
||
| 4669 | } |
||
| 4670 | } |
||
| 4671 | |||
| 4672 | // Find an interface descriptor |
||
| 4673 | if (bDescriptorType != USB_DESCRIPTOR_INTERFACE) |
||
| 4674 | { |
||
| 4675 | // Skip over the rest of the Descriptor |
||
| 4676 | index += bLength; |
||
| 4677 | ptr = &pCurrentConfigurationDescriptor[index]; |
||
| 4678 | } |
||
| 4679 | else |
||
| 4680 | { |
||
| 4681 | // Read some data from the interface descriptor |
||
| 4682 | bInterfaceNumber = *ptr++; |
||
| 4683 | bAlternateSetting = *ptr++; |
||
| 4684 | bNumEndpoints = *ptr++; |
||
| 4685 | Class = *ptr++; |
||
| 4686 | SubClass = *ptr++; |
||
| 4687 | Protocol = *ptr++; |
||
| 4688 | |||
| 4689 | // Get client driver index |
||
| 4690 | if (usbDeviceInfo.flags.bfUseDeviceClientDriver) |
||
| 4691 | { |
||
| 4692 | ClientDriver = usbDeviceInfo.deviceClientDriver; |
||
| 4693 | } |
||
| 4694 | else |
||
| 4695 | { |
||
| 4696 | if (!_USB_FindClassDriver(Class, SubClass, Protocol, &ClientDriver)) |
||
| 4697 | { |
||
| 4698 | // If we cannot support this interface, skip it. |
||
| 4699 | index += bLength; |
||
| 4700 | ptr = &pCurrentConfigurationDescriptor[index]; |
||
| 4701 | continue; |
||
| 4702 | } |
||
| 4703 | } |
||
| 4704 | |||
| 4705 | // We can support this interface. See if we already have a USB_INTERFACE_INFO node for it. |
||
| 4706 | newInterfaceInfo = pTempInterfaceList; |
||
| 4707 | while ((newInterfaceInfo != NULL) && (newInterfaceInfo->interface != bInterfaceNumber)) |
||
| 4708 | { |
||
| 4709 | newInterfaceInfo = newInterfaceInfo->next; |
||
| 4710 | } |
||
| 4711 | if (newInterfaceInfo == NULL) |
||
| 4712 | { |
||
| 4713 | // This is the first instance of this interface, so create a new node for it. |
||
| 4714 | if ((newInterfaceInfo = (USB_INTERFACE_INFO *)USB_MALLOC( sizeof(USB_INTERFACE_INFO) )) == NULL) |
||
| 4715 | { |
||
| 4716 | // Out of memory |
||
| 4717 | error = TRUE; |
||
| 4718 | } |
||
| 4719 | |||
| 4720 | // Initialize the interface node |
||
| 4721 | newInterfaceInfo->interface = bInterfaceNumber; |
||
| 4722 | newInterfaceInfo->clientDriver = ClientDriver; |
||
| 4723 | newInterfaceInfo->pInterfaceSettings = NULL; |
||
| 4724 | newInterfaceInfo->pCurrentSetting = NULL; |
||
| 4725 | |||
| 4726 | // Insert it into the list. |
||
| 4727 | newInterfaceInfo->next = pTempInterfaceList; |
||
| 4728 | pTempInterfaceList = newInterfaceInfo; |
||
| 4729 | } |
||
| 4730 | |||
| 4731 | if (!error) |
||
| 4732 | { |
||
| 4733 | // Create a new setting for this interface, and add it to the list. |
||
| 4734 | if ((newSettingInfo = (USB_INTERFACE_SETTING_INFO *)USB_MALLOC( sizeof(USB_INTERFACE_SETTING_INFO) )) == NULL) |
||
| 4735 | { |
||
| 4736 | // Out of memory |
||
| 4737 | error = TRUE; |
||
| 4738 | } |
||
| 4739 | } |
||
| 4740 | |||
| 4741 | if (!error) |
||
| 4742 | { |
||
| 4743 | newSettingInfo->next = newInterfaceInfo->pInterfaceSettings; |
||
| 4744 | newSettingInfo->interfaceAltSetting = bAlternateSetting; |
||
| 4745 | newSettingInfo->pEndpointList = NULL; |
||
| 4746 | newInterfaceInfo->pInterfaceSettings = newSettingInfo; |
||
| 4747 | if (bAlternateSetting == 0) |
||
| 4748 | { |
||
| 4749 | newInterfaceInfo->pCurrentSetting = newSettingInfo; |
||
| 4750 | } |
||
| 4751 | |||
| 4752 | // Skip over the rest of the Interface Descriptor |
||
| 4753 | index += bLength; |
||
| 4754 | ptr = &pCurrentConfigurationDescriptor[index]; |
||
| 4755 | |||
| 4756 | // Find the Endpoint Descriptors. There might be Class and Vendor descriptors in here |
||
| 4757 | currentEndpoint = 0; |
||
| 4758 | while (!error && (index < wTotalLength) && (currentEndpoint < bNumEndpoints)) |
||
| 4759 | { |
||
| 4760 | bLength = *ptr++; |
||
| 4761 | bDescriptorType = *ptr++; |
||
| 4762 | |||
| 4763 | if (bDescriptorType != USB_DESCRIPTOR_ENDPOINT) |
||
| 4764 | { |
||
| 4765 | // Skip over the rest of the Descriptor |
||
| 4766 | index += bLength; |
||
| 4767 | ptr = &pCurrentConfigurationDescriptor[index]; |
||
| 4768 | } |
||
| 4769 | else |
||
| 4770 | { |
||
| 4771 | // Create an entry for the new endpoint. |
||
| 4772 | if ((newEndpointInfo = (USB_ENDPOINT_INFO *)USB_MALLOC( sizeof(USB_ENDPOINT_INFO) )) == NULL) |
||
| 4773 | { |
||
| 4774 | // Out of memory |
||
| 4775 | error = TRUE; |
||
| 4776 | } |
||
| 4777 | newEndpointInfo->bEndpointAddress = *ptr++; |
||
| 4778 | newEndpointInfo->bmAttributes.val = *ptr++; |
||
| 4779 | newEndpointInfo->wMaxPacketSize = *ptr++; |
||
| 4780 | newEndpointInfo->wMaxPacketSize += (*ptr++) << 8; |
||
| 4781 | newEndpointInfo->wInterval = *ptr++; |
||
| 4782 | newEndpointInfo->status.val = 0x00; |
||
| 4783 | newEndpointInfo->status.bfUseDTS = 1; |
||
| 4784 | newEndpointInfo->status.bfTransferComplete = 1; // Initialize to success to allow preprocessing loops. |
||
| 4785 | newEndpointInfo->dataCount = 0; // Initialize to 0 since we set bfTransferComplete. |
||
| 4786 | newEndpointInfo->transferState = TSTATE_IDLE; |
||
| 4787 | newEndpointInfo->clientDriver = ClientDriver; |
||
| 4788 | |||
| 4789 | // Special setup for isochronous endpoints. |
||
| 4790 | if (newEndpointInfo->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_ISOCHRONOUS) |
||
| 4791 | { |
||
| 4792 | // Validate and convert the interval to the number of frames. The value must |
||
| 4793 | // be between 1 and 16, and the frames is 2^(bInterval-1). |
||
| 4794 | if (newEndpointInfo->wInterval == 0) newEndpointInfo->wInterval = 1; |
||
| 4795 | if (newEndpointInfo->wInterval > 16) newEndpointInfo->wInterval = 16; |
||
| 4796 | newEndpointInfo->wInterval = 1 << (newEndpointInfo->wInterval-1); |
||
| 4797 | |||
| 4798 | // Disable DTS |
||
| 4799 | newEndpointInfo->status.bfUseDTS = 0; |
||
| 4800 | } |
||
| 4801 | |||
| 4802 | // Initialize interval count |
||
| 4803 | newEndpointInfo->wIntervalCount = newEndpointInfo->wInterval; |
||
| 4804 | |||
| 4805 | // Put the new endpoint in the list. |
||
| 4806 | newEndpointInfo->next = newSettingInfo->pEndpointList; |
||
| 4807 | newSettingInfo->pEndpointList = newEndpointInfo; |
||
| 4808 | |||
| 4809 | // When multiple devices are supported, check the available |
||
| 4810 | // bandwidth here to make sure that we can support this |
||
| 4811 | // endpoint. |
||
| 4812 | |||
| 4813 | // Get ready for the next endpoint. |
||
| 4814 | currentEndpoint++; |
||
| 4815 | index += bLength; |
||
| 4816 | ptr = &pCurrentConfigurationDescriptor[index]; |
||
| 4817 | } |
||
| 4818 | } |
||
| 4819 | } |
||
| 4820 | |||
| 4821 | // Ensure that we found all the endpoints for this interface. |
||
| 4822 | if (currentEndpoint != bNumEndpoints) |
||
| 4823 | { |
||
| 4824 | error = TRUE; |
||
| 4825 | } |
||
| 4826 | } |
||
| 4827 | } |
||
| 4828 | |||
| 4829 | // Ensure that we found all the interfaces in this configuration. |
||
| 4830 | // This is a nice check, but some devices have errors where they have a |
||
| 4831 | // different number of interfaces than they report they have! |
||
| 4832 | // if (currentInterface != bNumInterfaces) |
||
| 4833 | // { |
||
| 4834 | // error = TRUE; |
||
| 4835 | // } |
||
| 4836 | |||
| 4837 | if (pTempInterfaceList == NULL) |
||
| 4838 | { |
||
| 4839 | // We could find no supported interfaces. |
||
| 4840 | #ifdef DEBUG_MODE |
||
| 4841 | UART2PrintString( "HOST: No supported interfaces.\r\n" ); |
||
| 4842 | #endif |
||
| 4843 | |||
| 4844 | error = TRUE; |
||
| 4845 | } |
||
| 4846 | |||
| 4847 | if (error) |
||
| 4848 | { |
||
| 4849 | // Destroy whatever list of interfaces, settings, and endpoints we created. |
||
| 4850 | // The "new" variables point to the current node we are trying to remove. |
||
| 4851 | while (pTempInterfaceList != NULL) |
||
| 4852 | { |
||
| 4853 | newInterfaceInfo = pTempInterfaceList; |
||
| 4854 | pTempInterfaceList = pTempInterfaceList->next; |
||
| 4855 | |||
| 4856 | while (newInterfaceInfo->pInterfaceSettings != NULL) |
||
| 4857 | { |
||
| 4858 | newSettingInfo = newInterfaceInfo->pInterfaceSettings; |
||
| 4859 | newInterfaceInfo->pInterfaceSettings = newInterfaceInfo->pInterfaceSettings->next; |
||
| 4860 | |||
| 4861 | while (newSettingInfo->pEndpointList != NULL) |
||
| 4862 | { |
||
| 4863 | newEndpointInfo = newSettingInfo->pEndpointList; |
||
| 4864 | newSettingInfo->pEndpointList = newSettingInfo->pEndpointList->next; |
||
| 4865 | |||
| 4866 | USB_FREE_AND_CLEAR( newEndpointInfo ); |
||
| 4867 | } |
||
| 4868 | |||
| 4869 | USB_FREE_AND_CLEAR( newSettingInfo ); |
||
| 4870 | } |
||
| 4871 | |||
| 4872 | USB_FREE_AND_CLEAR( newInterfaceInfo ); |
||
| 4873 | } |
||
| 4874 | return FALSE; |
||
| 4875 | } |
||
| 4876 | else |
||
| 4877 | { |
||
| 4878 | // Set configuration. |
||
| 4879 | usbDeviceInfo.currentConfiguration = currentConfiguration; |
||
| 4880 | usbDeviceInfo.currentConfigurationPower = bMaxPower; |
||
| 4881 | |||
| 4882 | // Success! |
||
| 4883 | #ifdef DEBUG_MODE |
||
| 4884 | UART2PrintString( "HOST: Parse Descriptor success\r\n" ); |
||
| 4885 | #endif |
||
| 4886 | usbDeviceInfo.pInterfaceList = pTempInterfaceList; |
||
| 4887 | return TRUE; |
||
| 4888 | } |
||
| 4889 | } |
||
| 4890 | |||
| 4891 | |||
| 4892 | /**************************************************************************** |
||
| 4893 | Function: |
||
| 4894 | void _USB_ResetDATA0( BYTE endpoint ) |
||
| 4895 | |||
| 4896 | Description: |
||
| 4897 | This function resets DATA0 for the specified endpoint. If the |
||
| 4898 | specified endpoint is 0, it resets DATA0 for all endpoints. |
||
| 4899 | |||
| 4900 | Precondition: |
||
| 4901 | None |
||
| 4902 | |||
| 4903 | Parameters: |
||
| 4904 | BYTE endpoint - Endpoint number to reset. |
||
| 4905 | |||
| 4906 | |||
| 4907 | Returns: |
||
| 4908 | None |
||
| 4909 | |||
| 4910 | Remarks: |
||
| 4911 | None |
||
| 4912 | ***************************************************************************/ |
||
| 4913 | |||
| 4914 | void _USB_ResetDATA0( BYTE endpoint ) |
||
| 4915 | { |
||
| 4916 | USB_ENDPOINT_INFO *pEndpoint; |
||
| 4917 | |||
| 4918 | if (endpoint == 0) |
||
| 4919 | { |
||
| 4920 | // Reset DATA0 for all endpoints. |
||
| 4921 | USB_INTERFACE_INFO *pInterface; |
||
| 4922 | USB_INTERFACE_SETTING_INFO *pSetting; |
||
| 4923 | |||
| 4924 | pInterface = usbDeviceInfo.pInterfaceList; |
||
| 4925 | while (pInterface) |
||
| 4926 | { |
||
| 4927 | pSetting = pInterface->pInterfaceSettings; |
||
| 4928 | while (pSetting) |
||
| 4929 | { |
||
| 4930 | pEndpoint = pSetting->pEndpointList; |
||
| 4931 | while (pEndpoint) |
||
| 4932 | { |
||
| 4933 | pEndpoint->status.bfNextDATA01 = 0; |
||
| 4934 | pEndpoint = pEndpoint->next; |
||
| 4935 | } |
||
| 4936 | pSetting = pSetting->next; |
||
| 4937 | } |
||
| 4938 | pInterface = pInterface->next; |
||
| 4939 | } |
||
| 4940 | } |
||
| 4941 | else |
||
| 4942 | { |
||
| 4943 | pEndpoint = _USB_FindEndpoint( endpoint ); |
||
| 4944 | if (pEndpoint != NULL) |
||
| 4945 | { |
||
| 4946 | pEndpoint->status.bfNextDATA01 = 0; |
||
| 4947 | } |
||
| 4948 | } |
||
| 4949 | } |
||
| 4950 | |||
| 4951 | |||
| 4952 | /**************************************************************************** |
||
| 4953 | Function: |
||
| 4954 | void _USB_SendToken( BYTE endpoint, BYTE tokenType ) |
||
| 4955 | |||
| 4956 | Description: |
||
| 4957 | This function sets up the endpoint control register and sends the token. |
||
| 4958 | |||
| 4959 | Precondition: |
||
| 4960 | None |
||
| 4961 | |||
| 4962 | Parameters: |
||
| 4963 | BYTE endpoint - Endpoint number |
||
| 4964 | BYTE tokenType - Token to send |
||
| 4965 | |||
| 4966 | Returns: |
||
| 4967 | None |
||
| 4968 | |||
| 4969 | Remarks: |
||
| 4970 | If the device is low speed, the transfer must be set to low speed. If |
||
| 4971 | the endpoint is isochronous, handshaking must be disabled. |
||
| 4972 | ***************************************************************************/ |
||
| 4973 | |||
| 4974 | void _USB_SendToken( BYTE endpoint, BYTE tokenType ) |
||
| 4975 | { |
||
| 4976 | BYTE temp; |
||
| 4977 | |||
| 4978 | // Disable retries, disable control transfers, enable Rx and Tx and handshaking. |
||
| 4979 | temp = 0x5D; |
||
| 4980 | |||
| 4981 | // Enable low speed transfer if the device is low speed. |
||
| 4982 | if (usbDeviceInfo.flags.bfIsLowSpeed) |
||
| 4983 | { |
||
| 4984 | temp |= 0x80; // Set LSPD |
||
| 4985 | } |
||
| 4986 | |||
| 4987 | // Enable control transfers if necessary. |
||
| 4988 | if (pCurrentEndpoint->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_CONTROL) |
||
| 4989 | { |
||
| 4990 | temp &= 0xEF; // Clear EPCONDIS |
||
| 4991 | } |
||
| 4992 | |||
| 4993 | // Disable handshaking for isochronous endpoints. |
||
| 4994 | if (pCurrentEndpoint->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_ISOCHRONOUS) |
||
| 4995 | { |
||
| 4996 | temp &= 0xFE; // Clear EPHSHK |
||
| 4997 | } |
||
| 4998 | |||
| 4999 | U1EP0 = temp; |
||
| 5000 | |||
| 5001 | #ifdef DEBUG_MODE |
||
| 5002 | if (usbBusInfo.flags.bfTokenAlreadyWritten) UART2PutChar( '+' ); |
||
| 5003 | // if (U1CONbits.TOKBUSY) UART2PutChar( '+' ); |
||
| 5004 | #endif |
||
| 5005 | |||
| 5006 | U1ADDR = usbDeviceInfo.deviceAddressAndSpeed; |
||
| 5007 | U1TOK = (tokenType << 4) | (endpoint & 0x7F); |
||
| 5008 | |||
| 5009 | // Lock out anyone from writing another token until this one has finished. |
||
| 5010 | // U1CONbits.TOKBUSY = 1; |
||
| 5011 | usbBusInfo.flags.bfTokenAlreadyWritten = 1; |
||
| 5012 | |||
| 5013 | #ifdef DEBUG_MODE |
||
| 5014 | //UART2PutChar('('); |
||
| 5015 | //UART2PutHex(U1ADDR); |
||
| 5016 | //UART2PutHex(U1EP0); |
||
| 5017 | //UART2PutHex(U1TOK); |
||
| 5018 | //UART2PutChar(')'); |
||
| 5019 | #endif |
||
| 5020 | } |
||
| 5021 | |||
| 5022 | |||
| 5023 | /**************************************************************************** |
||
| 5024 | Function: |
||
| 5025 | void _USB_SetBDT( BYTE token ) |
||
| 5026 | |||
| 5027 | Description: |
||
| 5028 | This function sets up the BDT for the transfer. The function handles the |
||
| 5029 | different ping-pong modes. |
||
| 5030 | |||
| 5031 | Precondition: |
||
| 5032 | pCurrentEndpoint must point to the current endpoint being serviced. |
||
| 5033 | |||
| 5034 | Parameters: |
||
| 5035 | BYTE token - Token for the transfer. That way we can tell which |
||
| 5036 | ping-pong buffer and which data pointer to use. Valid |
||
| 5037 | values are: |
||
| 5038 | * USB_TOKEN_SETUP |
||
| 5039 | * USB_TOKEN_IN |
||
| 5040 | * USB_TOKEN_OUT |
||
| 5041 | |||
| 5042 | Returns: |
||
| 5043 | None |
||
| 5044 | |||
| 5045 | Remarks: |
||
| 5046 | None |
||
| 5047 | ***************************************************************************/ |
||
| 5048 | |||
| 5049 | void _USB_SetBDT( BYTE token ) |
||
| 5050 | { |
||
| 5051 | WORD currentPacketSize; |
||
| 5052 | BDT_ENTRY *pBDT; |
||
| 5053 | |||
| 5054 | if (token == USB_TOKEN_IN) |
||
| 5055 | { |
||
| 5056 | // Find the BDT we need to use. |
||
| 5057 | #if (USB_PING_PONG_MODE == USB_PING_PONG__FULL_PING_PONG) |
||
| 5058 | pBDT = BDT_IN; |
||
| 5059 | if (usbDeviceInfo.flags.bfPingPongIn) |
||
| 5060 | { |
||
| 5061 | pBDT = BDT_IN_ODD; |
||
| 5062 | } |
||
| 5063 | #else |
||
| 5064 | pBDT = BDT_IN; |
||
| 5065 | #endif |
||
| 5066 | |||
| 5067 | // Set up ping-pong for the next transfer |
||
| 5068 | #if (USB_PING_PONG_MODE == USB_PING_PONG__FULL_PING_PONG) |
||
| 5069 | usbDeviceInfo.flags.bfPingPongIn = ~usbDeviceInfo.flags.bfPingPongIn; |
||
| 5070 | #endif |
||
| 5071 | } |
||
| 5072 | else // USB_TOKEN_OUT or USB_TOKEN_SETUP |
||
| 5073 | { |
||
| 5074 | // Find the BDT we need to use. |
||
| 5075 | #if (USB_PING_PONG_MODE == USB_PING_PONG__FULL_PING_PONG) || (USB_PING_PONG_MODE == USB_PING_PONG__EP0_OUT_ONLY) |
||
| 5076 | pBDT = BDT_OUT; |
||
| 5077 | if (usbDeviceInfo.flags.bfPingPongOut) |
||
| 5078 | { |
||
| 5079 | pBDT = BDT_OUT_ODD; |
||
| 5080 | } |
||
| 5081 | #else |
||
| 5082 | pBDT = BDT_OUT; |
||
| 5083 | #endif |
||
| 5084 | |||
| 5085 | // Set up ping-pong for the next transfer |
||
| 5086 | #if (USB_PING_PONG_MODE == USB_PING_PONG__FULL_PING_PONG) || (USB_PING_PONG_MODE == USB_PING_PONG__EP0_OUT_ONLY) |
||
| 5087 | usbDeviceInfo.flags.bfPingPongOut = ~usbDeviceInfo.flags.bfPingPongOut; |
||
| 5088 | #endif |
||
| 5089 | } |
||
| 5090 | |||
| 5091 | // Determine how much data we'll transfer in this packet. |
||
| 5092 | if (token == USB_TOKEN_SETUP) |
||
| 5093 | { |
||
| 5094 | if ((pCurrentEndpoint->dataCountMaxSETUP - pCurrentEndpoint->dataCount) > pCurrentEndpoint->wMaxPacketSize) |
||
| 5095 | { |
||
| 5096 | currentPacketSize = pCurrentEndpoint->wMaxPacketSize; |
||
| 5097 | } |
||
| 5098 | else |
||
| 5099 | { |
||
| 5100 | currentPacketSize = pCurrentEndpoint->dataCountMaxSETUP - pCurrentEndpoint->dataCount; |
||
| 5101 | } |
||
| 5102 | } |
||
| 5103 | else |
||
| 5104 | { |
||
| 5105 | if (pCurrentEndpoint->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_ISOCHRONOUS) |
||
| 5106 | { |
||
| 5107 | // Isochronous transfers are always the same size, though the device may choose to send less. |
||
| 5108 | currentPacketSize = pCurrentEndpoint->wMaxPacketSize; |
||
| 5109 | } |
||
| 5110 | else |
||
| 5111 | { |
||
| 5112 | if ((pCurrentEndpoint->dataCountMax - pCurrentEndpoint->dataCount) > pCurrentEndpoint->wMaxPacketSize) |
||
| 5113 | { |
||
| 5114 | currentPacketSize = pCurrentEndpoint->wMaxPacketSize; |
||
| 5115 | } |
||
| 5116 | else |
||
| 5117 | { |
||
| 5118 | currentPacketSize = pCurrentEndpoint->dataCountMax - pCurrentEndpoint->dataCount; |
||
| 5119 | } |
||
| 5120 | } |
||
| 5121 | } |
||
| 5122 | |||
| 5123 | // Load up the BDT address. |
||
| 5124 | if (token == USB_TOKEN_SETUP) |
||
| 5125 | { |
||
| 5126 | #if defined(__C30__) || defined(__PIC32MX__) |
||
| 5127 | pBDT->ADR = ConvertToPhysicalAddress(pCurrentEndpoint->pUserDataSETUP); |
||
| 5128 | #else |
||
| 5129 | #error Cannot set BDT address. |
||
| 5130 | #endif |
||
| 5131 | } |
||
| 5132 | else |
||
| 5133 | { |
||
| 5134 | #if defined(__C30__) |
||
| 5135 | if (pCurrentEndpoint->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_ISOCHRONOUS) |
||
| 5136 | { |
||
| 5137 | pBDT->ADR = ConvertToPhysicalAddress(((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->buffers[((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->currentBufferUSB].pBuffer); |
||
| 5138 | } |
||
| 5139 | else |
||
| 5140 | { |
||
| 5141 | pBDT->ADR = ConvertToPhysicalAddress((WORD)pCurrentEndpoint->pUserData + (WORD)pCurrentEndpoint->dataCount); |
||
| 5142 | } |
||
| 5143 | #elif defined(__PIC32MX__) |
||
| 5144 | if (pCurrentEndpoint->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_ISOCHRONOUS) |
||
| 5145 | { |
||
| 5146 | pBDT->ADR = ConvertToPhysicalAddress(((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->buffers[((ISOCHRONOUS_DATA *)(pCurrentEndpoint->pUserData))->currentBufferUSB].pBuffer); |
||
| 5147 | } |
||
| 5148 | else |
||
| 5149 | { |
||
| 5150 | pBDT->ADR = ConvertToPhysicalAddress((DWORD)pCurrentEndpoint->pUserData + (DWORD)pCurrentEndpoint->dataCount); |
||
| 5151 | } |
||
| 5152 | #else |
||
| 5153 | #error Cannot set BDT address. |
||
| 5154 | #endif |
||
| 5155 | } |
||
| 5156 | |||
| 5157 | // Load up the BDT status register. |
||
| 5158 | pBDT->STAT.Val = 0; |
||
| 5159 | pBDT->count = currentPacketSize; |
||
| 5160 | pBDT->STAT.DTS = pCurrentEndpoint->status.bfNextDATA01; |
||
| 5161 | pBDT->STAT.DTSEN = pCurrentEndpoint->status.bfUseDTS; |
||
| 5162 | |||
| 5163 | // Transfer the BD to the USB OTG module. |
||
| 5164 | pBDT->STAT.UOWN = 1; |
||
| 5165 | |||
| 5166 | #ifdef DEBUG_MODE |
||
| 5167 | // UART2PutChar('{'); |
||
| 5168 | // UART2PutHex((pBDT->v[0] >> 24) & 0xff); |
||
| 5169 | // UART2PutHex((pBDT->v[0] >> 16) & 0xff); |
||
| 5170 | // UART2PutHex((pBDT->v[0] >> 8) & 0xff); |
||
| 5171 | // UART2PutHex((pBDT->v[0]) & 0xff); |
||
| 5172 | // UART2PutChar('-'); |
||
| 5173 | // UART2PutHex((currentPacketSize >> 24) & 0xff); |
||
| 5174 | // UART2PutHex((pBDT->v[1] >> 16) & 0xff); |
||
| 5175 | // UART2PutHex((currentPacketSize >> 8) & 0xff); |
||
| 5176 | // UART2PutHex(currentPacketSize & 0xff); |
||
| 5177 | // UART2PutChar('}'); |
||
| 5178 | #endif |
||
| 5179 | |||
| 5180 | } |
||
| 5181 | |||
| 5182 | |||
| 5183 | /**************************************************************************** |
||
| 5184 | Function: |
||
| 5185 | BOOL _USB_TransferInProgress( void ) |
||
| 5186 | |||
| 5187 | Description: |
||
| 5188 | This function checks to see if any read or write transfers are in |
||
| 5189 | progress. |
||
| 5190 | |||
| 5191 | Precondition: |
||
| 5192 | None |
||
| 5193 | |||
| 5194 | Parameters: |
||
| 5195 | None - None |
||
| 5196 | |||
| 5197 | Returns: |
||
| 5198 | TRUE - At least one read or write transfer is occurring. |
||
| 5199 | FALSE - No read or write transfers are occurring. |
||
| 5200 | |||
| 5201 | Remarks: |
||
| 5202 | None |
||
| 5203 | ***************************************************************************/ |
||
| 5204 | |||
| 5205 | BOOL _USB_TransferInProgress( void ) |
||
| 5206 | { |
||
| 5207 | USB_ENDPOINT_INFO *pEndpoint; |
||
| 5208 | USB_INTERFACE_INFO *pInterface; |
||
| 5209 | USB_INTERFACE_SETTING_INFO *pSetting; |
||
| 5210 | |||
| 5211 | // Check EP0. |
||
| 5212 | if (!usbDeviceInfo.pEndpoint0->status.bfTransferComplete) |
||
| 5213 | { |
||
| 5214 | return TRUE; |
||
| 5215 | } |
||
| 5216 | |||
| 5217 | // Check all of the other endpoints. |
||
| 5218 | pInterface = usbDeviceInfo.pInterfaceList; |
||
| 5219 | while (pInterface) |
||
| 5220 | { |
||
| 5221 | pSetting = pInterface->pInterfaceSettings; |
||
| 5222 | while (pSetting) |
||
| 5223 | { |
||
| 5224 | pEndpoint = pSetting->pEndpointList; |
||
| 5225 | while (pEndpoint) |
||
| 5226 | { |
||
| 5227 | if (!pEndpoint->status.bfTransferComplete) |
||
| 5228 | { |
||
| 5229 | return TRUE; |
||
| 5230 | } |
||
| 5231 | pEndpoint = pEndpoint->next; |
||
| 5232 | } |
||
| 5233 | pSetting = pSetting->next; |
||
| 5234 | } |
||
| 5235 | pInterface = pInterface->next; |
||
| 5236 | } |
||
| 5237 | |||
| 5238 | return FALSE; |
||
| 5239 | } |
||
| 5240 | |||
| 5241 | |||
| 5242 | // ***************************************************************************** |
||
| 5243 | // ***************************************************************************** |
||
| 5244 | // Section: Interrupt Handlers |
||
| 5245 | // ***************************************************************************** |
||
| 5246 | // ***************************************************************************** |
||
| 5247 | |||
| 5248 | /**************************************************************************** |
||
| 5249 | Function: |
||
| 5250 | void _USB1Interrupt( void ) |
||
| 5251 | |||
| 5252 | Summary: |
||
| 5253 | This is the interrupt service routine for the USB interrupt. |
||
| 5254 | |||
| 5255 | Description: |
||
| 5256 | This is the interrupt service routine for the USB interrupt. The |
||
| 5257 | following cases are serviced: |
||
| 5258 | * Device Attach |
||
| 5259 | * Device Detach |
||
| 5260 | * One millisecond Timer |
||
| 5261 | * Start of Frame |
||
| 5262 | * Transfer Done |
||
| 5263 | * USB Error |
||
| 5264 | |||
| 5265 | Precondition: |
||
| 5266 | In TRNIF handling, pCurrentEndpoint is still pointing to the last |
||
| 5267 | endpoint to which a token was sent. |
||
| 5268 | |||
| 5269 | Parameters: |
||
| 5270 | None - None |
||
| 5271 | |||
| 5272 | Returns: |
||
| 5273 | None |
||
| 5274 | |||
| 5275 | Remarks: |
||
| 5276 | None |
||
| 5277 | ***************************************************************************/ |
||
| 5278 | #define U1STAT_TX_MASK 0x08 // U1STAT bit mask for Tx/Rx indication |
||
| 5279 | #define U1STAT_ODD_MASK 0x04 // U1STAT bit mask for even/odd buffer bank |
||
| 5280 | |||
| 5281 | #if defined(__C30__) |
||
| 5282 | void __attribute__((__interrupt__, no_auto_psv)) _USB1Interrupt( void ) |
||
| 5283 | #elif defined(__PIC32MX__) |
||
| 5284 | #pragma interrupt _USB1Interrupt ipl4 vector 45 |
||
| 5285 | void _USB1Interrupt( void ) |
||
| 5286 | #else |
||
| 5287 | #error Cannot define timer interrupt vector. |
||
| 5288 | #endif |
||
| 5289 | { |
||
| 5290 | |||
| 5291 | #if defined( __C30__) |
||
| 5292 | IFS5 &= 0xFFBF; |
||
| 5293 | #elif defined( __PIC32MX__) |
||
| 5294 | IFS1CLR = 0x02000000; |
||
| 5295 | #else |
||
| 5296 | #error Cannot clear USB interrupt. |
||
| 5297 | #endif |
||
| 5298 | |||
| 5299 | // ------------------------------------------------------------------------- |
||
| 5300 | // One Millisecond Timer ISR |
||
| 5301 | |||
| 5302 | if (U1OTGIEbits.T1MSECIE && U1OTGIRbits.T1MSECIF) |
||
| 5303 | { |
||
| 5304 | // The interrupt is cleared by writing a '1' to it. |
||
| 5305 | U1OTGIR = USB_INTERRUPT_T1MSECIF; |
||
| 5306 | |||
| 5307 | #ifdef DEBUG_MODE |
||
| 5308 | UART2PutChar('~'); |
||
| 5309 | #endif |
||
| 5310 | |||
| 5311 | #ifdef USB_SUPPORT_OTG |
||
| 5312 | if (USBOTGGetSRPTimeOutFlag()) |
||
| 5313 | { |
||
| 5314 | if (USBOTGIsSRPTimeOutExpired()) |
||
| 5315 | { |
||
| 5316 | USB_OTGEventHandler(0,OTG_EVENT_SRP_FAILED,0,0); |
||
| 5317 | } |
||
| 5318 | |||
| 5319 | } |
||
| 5320 | |||
| 5321 | else if (USBOTGGetHNPTimeOutFlag()) |
||
| 5322 | { |
||
| 5323 | if (USBOTGIsHNPTimeOutExpired()) |
||
| 5324 | { |
||
| 5325 | USB_OTGEventHandler(0,OTG_EVENT_HNP_FAILED,0,0); |
||
| 5326 | } |
||
| 5327 | |||
| 5328 | } |
||
| 5329 | |||
| 5330 | else |
||
| 5331 | { |
||
| 5332 | numTimerInterrupts--; |
||
| 5333 | if (numTimerInterrupts == 0) |
||
| 5334 | { |
||
| 5335 | // Turn off the timer interrupt. |
||
| 5336 | U1OTGIEbits.T1MSECIE = 0; |
||
| 5337 | |||
| 5338 | // Advance to the next state. We can do this here, because the only time |
||
| 5339 | // we'll get a timer interrupt is while we are in one of the holding states. |
||
| 5340 | _USB_SetNextSubSubState(); |
||
| 5341 | } |
||
| 5342 | } |
||
| 5343 | #else |
||
| 5344 | |||
| 5345 | numTimerInterrupts--; |
||
| 5346 | if (numTimerInterrupts == 0) |
||
| 5347 | { |
||
| 5348 | // Turn off the timer interrupt. |
||
| 5349 | U1OTGIEbits.T1MSECIE = 0; |
||
| 5350 | |||
| 5351 | // Advance to the next state. We can do this here, because the only time |
||
| 5352 | // we'll get a timer interrupt is while we are in one of the holding states. |
||
| 5353 | _USB_SetNextSubSubState(); |
||
| 5354 | } |
||
| 5355 | #endif |
||
| 5356 | } |
||
| 5357 | |||
| 5358 | // ------------------------------------------------------------------------- |
||
| 5359 | // Attach ISR |
||
| 5360 | |||
| 5361 | // The attach interrupt is level, not edge, triggered. So make sure we have it enabled. |
||
| 5362 | if (U1IEbits.ATTACHIE && U1IRbits.ATTACHIF) |
||
| 5363 | { |
||
| 5364 | #ifdef DEBUG_MODE |
||
| 5365 | UART2PutChar( '[' ); |
||
| 5366 | #endif |
||
| 5367 | |||
| 5368 | // The attach interrupt is level, not edge, triggered. If we clear it, it just |
||
| 5369 | // comes right back. So clear the enable instead |
||
| 5370 | U1IEbits.ATTACHIE = 0; |
||
| 5371 | U1IR = USB_INTERRUPT_ATTACH; |
||
| 5372 | |||
| 5373 | if (usbHostState == (STATE_DETACHED | SUBSTATE_WAIT_FOR_DEVICE)) |
||
| 5374 | { |
||
| 5375 | usbOverrideHostState = STATE_ATTACHED; |
||
| 5376 | } |
||
| 5377 | |||
| 5378 | #ifdef USB_SUPPORT_OTG |
||
| 5379 | //If HNP Related Attach, Process Connect Event |
||
| 5380 | USB_OTGEventHandler(0, OTG_EVENT_CONNECT, 0, 0 ); |
||
| 5381 | |||
| 5382 | //If SRP Related A side D+ High, Process D+ High Event |
||
| 5383 | USB_OTGEventHandler (0, OTG_EVENT_SRP_DPLUS_HIGH, 0, 0 ); |
||
| 5384 | |||
| 5385 | //If SRP Related B side Attach |
||
| 5386 | USB_OTGEventHandler (0, OTG_EVENT_SRP_CONNECT, 0, 0 ); |
||
| 5387 | #endif |
||
| 5388 | } |
||
| 5389 | |||
| 5390 | // ------------------------------------------------------------------------- |
||
| 5391 | // Detach ISR |
||
| 5392 | |||
| 5393 | if (U1IEbits.DETACHIE && U1IRbits.DETACHIF) |
||
| 5394 | { |
||
| 5395 | #ifdef DEBUG_MODE |
||
| 5396 | UART2PutChar( ']' ); |
||
| 5397 | #endif |
||
| 5398 | |||
| 5399 | U1IR = USB_INTERRUPT_DETACH; |
||
| 5400 | U1IEbits.DETACHIE = 0; |
||
| 5401 | usbOverrideHostState = STATE_DETACHED; |
||
| 5402 | |||
| 5403 | #ifdef USB_SUPPORT_OTG |
||
| 5404 | //If HNP Related Detach Detected, Process Disconnect Event |
||
| 5405 | USB_OTGEventHandler (0, OTG_EVENT_DISCONNECT, 0, 0 ); |
||
| 5406 | |||
| 5407 | //If SRP Related D+ Low and SRP Is Active, Process D+ Low Event |
||
| 5408 | USB_OTGEventHandler (0, OTG_EVENT_SRP_DPLUS_LOW, 0, 0 ); |
||
| 5409 | |||
| 5410 | //Disable HNP, Detach Interrupt Could've Triggered From Cable Being Unplugged |
||
| 5411 | USBOTGDisableHnp(); |
||
| 5412 | #endif |
||
| 5413 | } |
||
| 5414 | |||
| 5415 | #ifdef USB_SUPPORT_OTG |
||
| 5416 | |||
| 5417 | // ------------------------------------------------------------------------- |
||
| 5418 | //ID Pin Change ISR |
||
| 5419 | if (U1OTGIRbits.IDIF && U1OTGIEbits.IDIE) |
||
| 5420 | { |
||
| 5421 | USBOTGInitialize(); |
||
| 5422 | |||
| 5423 | //Clear Interrupt Flag |
||
| 5424 | U1OTGIR = 0x80; |
||
| 5425 | } |
||
| 5426 | |||
| 5427 | // ------------------------------------------------------------------------- |
||
| 5428 | //VB_SESS_END ISR |
||
| 5429 | if (U1OTGIRbits.SESENDIF && U1OTGIEbits.SESENDIE) |
||
| 5430 | { |
||
| 5431 | //If B side Host And Cable Was Detached Then |
||
| 5432 | if (U1OTGSTATbits.ID == CABLE_B_SIDE && USBOTGCurrentRoleIs() == ROLE_HOST) |
||
| 5433 | { |
||
| 5434 | //Reinitialize |
||
| 5435 | USBOTGInitialize(); |
||
| 5436 | } |
||
| 5437 | |||
| 5438 | //Clear Interrupt Flag |
||
| 5439 | U1OTGIR = 0x04; |
||
| 5440 | } |
||
| 5441 | |||
| 5442 | // ------------------------------------------------------------------------- |
||
| 5443 | //VA_SESS_VLD ISR |
||
| 5444 | if (U1OTGIRbits.SESVDIF && U1OTGIEbits.SESVDIE) |
||
| 5445 | { |
||
| 5446 | //If A side Host and SRP Is Active Then |
||
| 5447 | if (USBOTGDefaultRoleIs() == ROLE_HOST && USBOTGSrpIsActive()) |
||
| 5448 | { |
||
| 5449 | //If VBUS > VA_SESS_VLD Then |
||
| 5450 | if (U1OTGSTATbits.SESVD == 1) |
||
| 5451 | { |
||
| 5452 | //Process SRP VBUS High Event |
||
| 5453 | USB_OTGEventHandler (0, OTG_EVENT_SRP_VBUS_HIGH, 0, 0 ); |
||
| 5454 | } |
||
| 5455 | |||
| 5456 | //If VBUS < VA_SESS_VLD Then |
||
| 5457 | else |
||
| 5458 | { |
||
| 5459 | //Process SRP Low Event |
||
| 5460 | USB_OTGEventHandler (0, OTG_EVENT_SRP_VBUS_LOW, 0, 0 ); |
||
| 5461 | } |
||
| 5462 | } |
||
| 5463 | |||
| 5464 | U1OTGIR = 0x08; |
||
| 5465 | } |
||
| 5466 | |||
| 5467 | // ------------------------------------------------------------------------- |
||
| 5468 | //Resume Signaling for Remote Wakeup |
||
| 5469 | if (U1IRbits.RESUMEIF && U1IEbits.RESUMEIE) |
||
| 5470 | { |
||
| 5471 | //Process SRP VBUS High Event |
||
| 5472 | USB_OTGEventHandler (0, OTG_EVENT_RESUME_SIGNALING,0, 0 ); |
||
| 5473 | |||
| 5474 | //Clear Resume Interrupt Flag |
||
| 5475 | U1IR = 0x20; |
||
| 5476 | } |
||
| 5477 | #endif |
||
| 5478 | |||
| 5479 | |||
| 5480 | // ------------------------------------------------------------------------- |
||
| 5481 | // Transfer Done ISR - only process if there was no error |
||
| 5482 | |||
| 5483 | if ((U1IEbits.TRNIE && U1IRbits.TRNIF) && |
||
| 5484 | (!(U1IEbits.UERRIE && U1IRbits.UERRIF) || (pCurrentEndpoint->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_ISOCHRONOUS))) |
||
| 5485 | { |
||
| 5486 | #if defined(__C30__) |
||
| 5487 | U1STATBITS copyU1STATbits; |
||
| 5488 | #elif defined(__PIC32MX__) |
||
| 5489 | __U1STATbits_t copyU1STATbits; |
||
| 5490 | #else |
||
| 5491 | #error Need structure name for copyU1STATbits. |
||
| 5492 | #endif |
||
| 5493 | WORD packetSize; |
||
| 5494 | BDT_ENTRY *pBDT; |
||
| 5495 | |||
| 5496 | #ifdef DEBUG_MODE |
||
| 5497 | UART2PutChar( '!' ); |
||
| 5498 | #endif |
||
| 5499 | |||
| 5500 | // The previous token has finished, so clear the way for writing a new one. |
||
| 5501 | usbBusInfo.flags.bfTokenAlreadyWritten = 0; |
||
| 5502 | |||
| 5503 | copyU1STATbits = U1STATbits; // Read the status register before clearing the flag. |
||
| 5504 | |||
| 5505 | U1IR = USB_INTERRUPT_TRANSFER; // Clear the interrupt by writing a '1' to the flag. |
||
| 5506 | |||
| 5507 | // In host mode, U1STAT does NOT reflect the endpoint. It is really the last updated |
||
| 5508 | // BDT, which, in host mode, is always 0. To get the endpoint, we either need to look |
||
| 5509 | // at U1TOK, or trust that pCurrentEndpoint is still accurate. |
||
| 5510 | if ((pCurrentEndpoint->bEndpointAddress & 0x0F) == (U1TOK & 0x0F)) |
||
| 5511 | { |
||
| 5512 | if (copyU1STATbits.DIR) // TX |
||
| 5513 | { |
||
| 5514 | // We are processing OUT or SETUP packets. |
||
| 5515 | // Set up the BDT pointer for the transaction we just received. |
||
| 5516 | #if (USB_PING_PONG_MODE == USB_PING_PONG__EP0_OUT_ONLY) || (USB_PING_PONG_MODE == USB_PING_PONG__FULL_PING_PONG) |
||
| 5517 | pBDT = BDT_OUT; |
||
| 5518 | if (copyU1STATbits.PPBI) // Odd |
||
| 5519 | { |
||
| 5520 | pBDT = BDT_OUT_ODD; |
||
| 5521 | } |
||
| 5522 | #elif (USB_PING_PONG_MODE == USB_PING_PONG__NO_PING_PONG) || (USB_PING_PONG_MODE == USB_PING_PONG__ALL_BUT_EP0) |
||
| 5523 | pBDT = BDT_OUT; |
||
| 5524 | #endif |
||
| 5525 | } |
||
| 5526 | else |
||
| 5527 | { |
||
| 5528 | // We are processing IN packets. |
||
| 5529 | // Set up the BDT pointer for the transaction we just received. |
||
| 5530 | #if (USB_PING_PONG_MODE == USB_PING_PONG__FULL_PING_PONG) |
||
| 5531 | pBDT = BDT_IN; |
||
| 5532 | if (copyU1STATbits.PPBI) // Odd |
||
| 5533 | { |
||
| 5534 | pBDT = BDT_IN_ODD; |
||
| 5535 | } |
||
| 5536 | #else |
||
| 5537 | pBDT = BDT_IN; |
||
| 5538 | #endif |
||
| 5539 | } |
||
| 5540 | |||
| 5541 | if (pBDT->STAT.PID == PID_ACK) |
||
| 5542 | { |
||
| 5543 | // We will only get this PID from an OUT or SETUP packet. |
||
| 5544 | |||
| 5545 | // Update the count of bytes tranferred. (If there was an error, this count will be 0.) |
||
| 5546 | // The Byte Count is NOT 0 if a NAK occurs. Therefore, we can only update the |
||
| 5547 | // count when an ACK, DATA0, or DATA1 is received. |
||
| 5548 | packetSize = pBDT->count; |
||
| 5549 | pCurrentEndpoint->dataCount += packetSize; |
||
| 5550 | |||
| 5551 | // Set the NAK retries for the next transaction; |
||
| 5552 | pCurrentEndpoint->countNAKs = 0; |
||
| 5553 | |||
| 5554 | // Toggle DTS for the next transfer. |
||
| 5555 | pCurrentEndpoint->status.bfNextDATA01 ^= 0x01; |
||
| 5556 | |||
| 5557 | if ((pCurrentEndpoint->transferState == (TSTATE_CONTROL_NO_DATA | TSUBSTATE_CONTROL_NO_DATA_SETUP)) || |
||
| 5558 | (pCurrentEndpoint->transferState == (TSTATE_CONTROL_READ | TSUBSTATE_CONTROL_READ_SETUP)) || |
||
| 5559 | (pCurrentEndpoint->transferState == (TSTATE_CONTROL_WRITE | TSUBSTATE_CONTROL_WRITE_SETUP))) |
||
| 5560 | { |
||
| 5561 | // We are doing SETUP transfers. See if we are done with the SETUP portion. |
||
| 5562 | if (pCurrentEndpoint->dataCount >= pCurrentEndpoint->dataCountMaxSETUP) |
||
| 5563 | { |
||
| 5564 | // We are done with the SETUP. Reset the byte count and |
||
| 5565 | // proceed to the next token. |
||
| 5566 | pCurrentEndpoint->dataCount = 0; |
||
| 5567 | _USB_SetNextTransferState(); |
||
| 5568 | } |
||
| 5569 | } |
||
| 5570 | else |
||
| 5571 | { |
||
| 5572 | // We are doing OUT transfers. See if we've written all the data. |
||
| 5573 | // We've written all the data when we send a short packet or we have |
||
| 5574 | // transferred all the data. If it's an isochronous transfer, this |
||
| 5575 | // portion is complete, so go to the next state, so we can tell the |
||
| 5576 | // next higher layer that a batch of data has been transferred. |
||
| 5577 | if ((pCurrentEndpoint->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_ISOCHRONOUS) || |
||
| 5578 | (packetSize < pCurrentEndpoint->wMaxPacketSize) || |
||
| 5579 | (pCurrentEndpoint->dataCount >= pCurrentEndpoint->dataCountMax)) |
||
| 5580 | { |
||
| 5581 | // We've written all the data. Proceed to the next step. |
||
| 5582 | pCurrentEndpoint->status.bfTransferSuccessful = 1; |
||
| 5583 | _USB_SetNextTransferState(); |
||
| 5584 | } |
||
| 5585 | else |
||
| 5586 | { |
||
| 5587 | // We need to process more data. Keep this endpoint in its current |
||
| 5588 | // transfer state. |
||
| 5589 | } |
||
| 5590 | } |
||
| 5591 | } |
||
| 5592 | else if ((pBDT->STAT.PID == PID_DATA0) || (pBDT->STAT.PID == PID_DATA1)) |
||
| 5593 | { |
||
| 5594 | // We will only get these PID's from an IN packet. |
||
| 5595 | |||
| 5596 | // Update the count of bytes tranferred. (If there was an error, this count will be 0.) |
||
| 5597 | // The Byte Count is NOT 0 if a NAK occurs. Therefore, we can only update the |
||
| 5598 | // count when an ACK, DATA0, or DATA1 is received. |
||
| 5599 | packetSize = pBDT->count; |
||
| 5600 | pCurrentEndpoint->dataCount += packetSize; |
||
| 5601 | |||
| 5602 | // Set the NAK retries for the next transaction; |
||
| 5603 | pCurrentEndpoint->countNAKs = 0; |
||
| 5604 | |||
| 5605 | // Toggle DTS for the next transfer. |
||
| 5606 | pCurrentEndpoint->status.bfNextDATA01 ^= 0x01; |
||
| 5607 | |||
| 5608 | // We are doing IN transfers. See if we've received all the data. |
||
| 5609 | // We've received all the data if it's an isochronous transfer, or when we receive a |
||
| 5610 | // short packet or we have transferred all the data. |
||
| 5611 | if ((pCurrentEndpoint->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_ISOCHRONOUS) || |
||
| 5612 | (packetSize < pCurrentEndpoint->wMaxPacketSize) || |
||
| 5613 | (pCurrentEndpoint->dataCount >= pCurrentEndpoint->dataCountMax)) |
||
| 5614 | { |
||
| 5615 | // If we've received all the data, stop the transfer. We've received all the |
||
| 5616 | // data when we receive a short or zero-length packet. If the data length is a |
||
| 5617 | // multiple of wMaxPacketSize, we will get a 0-length packet. |
||
| 5618 | pCurrentEndpoint->status.bfTransferSuccessful = 1; |
||
| 5619 | _USB_SetNextTransferState(); |
||
| 5620 | } |
||
| 5621 | else |
||
| 5622 | { |
||
| 5623 | // We need to process more data. Keep this endpoint in its current |
||
| 5624 | // transfer state. |
||
| 5625 | } |
||
| 5626 | } |
||
| 5627 | else if (pBDT->STAT.PID == PID_NAK) |
||
| 5628 | { |
||
| 5629 | #ifndef ALLOW_MULTIPLE_NAKS_PER_FRAME |
||
| 5630 | pCurrentEndpoint->status.bfLastTransferNAKd = 1; |
||
| 5631 | #endif |
||
| 5632 | |||
| 5633 | pCurrentEndpoint->countNAKs ++; |
||
| 5634 | |||
| 5635 | switch( pCurrentEndpoint->bmAttributes.bfTransferType ) |
||
| 5636 | { |
||
| 5637 | case USB_TRANSFER_TYPE_BULK: |
||
| 5638 | // Bulk IN and OUT transfers are allowed to retry NAK'd |
||
| 5639 | // transactions until a timeout (if enabled) or indefinitely |
||
| 5640 | // (if NAK timeouts disabled). |
||
| 5641 | if (pCurrentEndpoint->status.bfNAKTimeoutEnabled && |
||
| 5642 | (pCurrentEndpoint->countNAKs > pCurrentEndpoint->timeoutNAKs)) |
||
| 5643 | { |
||
| 5644 | pCurrentEndpoint->status.bfError = 1; |
||
| 5645 | pCurrentEndpoint->bErrorCode = USB_ENDPOINT_NAK_TIMEOUT; |
||
| 5646 | _USB_SetTransferErrorState( pCurrentEndpoint ); |
||
| 5647 | } |
||
| 5648 | break; |
||
| 5649 | |||
| 5650 | case USB_TRANSFER_TYPE_CONTROL: |
||
| 5651 | // Devices should not NAK the SETUP portion. If they NAK |
||
| 5652 | // the DATA portion, they are allowed to retry a fixed |
||
| 5653 | // number of times. |
||
| 5654 | if (pCurrentEndpoint->status.bfNAKTimeoutEnabled && |
||
| 5655 | (pCurrentEndpoint->countNAKs > pCurrentEndpoint->timeoutNAKs)) |
||
| 5656 | { |
||
| 5657 | pCurrentEndpoint->status.bfError = 1; |
||
| 5658 | pCurrentEndpoint->bErrorCode = USB_ENDPOINT_NAK_TIMEOUT; |
||
| 5659 | _USB_SetTransferErrorState( pCurrentEndpoint ); |
||
| 5660 | } |
||
| 5661 | break; |
||
| 5662 | |||
| 5663 | case USB_TRANSFER_TYPE_INTERRUPT: |
||
| 5664 | if ((pCurrentEndpoint->bEndpointAddress & 0x80) == 0x00) |
||
| 5665 | { |
||
| 5666 | // Interrupt OUT transfers are allowed to retry NAK'd |
||
| 5667 | // transactions until a timeout (if enabled) or indefinitely |
||
| 5668 | // (if NAK timeouts disabled). |
||
| 5669 | if (pCurrentEndpoint->status.bfNAKTimeoutEnabled && |
||
| 5670 | (pCurrentEndpoint->countNAKs > pCurrentEndpoint->timeoutNAKs)) |
||
| 5671 | { |
||
| 5672 | pCurrentEndpoint->status.bfError = 1; |
||
| 5673 | pCurrentEndpoint->bErrorCode = USB_ENDPOINT_NAK_TIMEOUT; |
||
| 5674 | _USB_SetTransferErrorState( pCurrentEndpoint ); |
||
| 5675 | } |
||
| 5676 | } |
||
| 5677 | else |
||
| 5678 | { |
||
| 5679 | // Interrupt IN transfers terminate with no error. |
||
| 5680 | pCurrentEndpoint->status.bfTransferSuccessful = 1; |
||
| 5681 | _USB_SetNextTransferState(); |
||
| 5682 | } |
||
| 5683 | break; |
||
| 5684 | |||
| 5685 | case USB_TRANSFER_TYPE_ISOCHRONOUS: |
||
| 5686 | // Isochronous transfers terminate with no error. |
||
| 5687 | pCurrentEndpoint->status.bfTransferSuccessful = 1; |
||
| 5688 | _USB_SetNextTransferState(); |
||
| 5689 | break; |
||
| 5690 | } |
||
| 5691 | } |
||
| 5692 | else if (pBDT->STAT.PID == PID_STALL) |
||
| 5693 | { |
||
| 5694 | // Device is stalled. Stop the transfer, and indicate the stall. |
||
| 5695 | // The application must clear this if not a control endpoint. |
||
| 5696 | // A stall on a control endpoint does not indicate that the |
||
| 5697 | // endpoint is halted. |
||
| 5698 | #ifdef DEBUG_MODE |
||
| 5699 | UART2PutChar( '^' ); |
||
| 5700 | #endif |
||
| 5701 | pCurrentEndpoint->status.bfStalled = 1; |
||
| 5702 | pCurrentEndpoint->bErrorCode = USB_ENDPOINT_STALLED; |
||
| 5703 | _USB_SetTransferErrorState( pCurrentEndpoint ); |
||
| 5704 | } |
||
| 5705 | else |
||
| 5706 | { |
||
| 5707 | // Module-defined PID - Bus Timeout (0x0) or Data Error (0x0F). Increment the error count. |
||
| 5708 | // NOTE: If DTS is enabled and the packet has the wrong DTS value, a PID of 0x0F is |
||
| 5709 | // returned. The hardware, however, acknowledges the packet, so the device thinks |
||
| 5710 | // that the host has received it. But the data is not actually received, and the application |
||
| 5711 | // layer is not informed of the packet. |
||
| 5712 | pCurrentEndpoint->status.bfErrorCount++; |
||
| 5713 | |||
| 5714 | if (pCurrentEndpoint->status.bfErrorCount >= USB_TRANSACTION_RETRY_ATTEMPTS) |
||
| 5715 | { |
||
| 5716 | // We have too many errors. |
||
| 5717 | |||
| 5718 | // Stop the transfer and indicate an error. |
||
| 5719 | // The application must clear this. |
||
| 5720 | pCurrentEndpoint->status.bfError = 1; |
||
| 5721 | pCurrentEndpoint->bErrorCode = USB_ENDPOINT_ERROR_ILLEGAL_PID; |
||
| 5722 | _USB_SetTransferErrorState( pCurrentEndpoint ); |
||
| 5723 | |||
| 5724 | // Avoid the error interrupt code, because we are going to |
||
| 5725 | // find another token to send. |
||
| 5726 | U1EIR = 0xFF; |
||
| 5727 | U1IR = USB_INTERRUPT_ERROR; |
||
| 5728 | } |
||
| 5729 | else |
||
| 5730 | { |
||
| 5731 | // Fall through. This will automatically cause the transfer |
||
| 5732 | // to be retried. |
||
| 5733 | } |
||
| 5734 | } |
||
| 5735 | } |
||
| 5736 | else |
||
| 5737 | { |
||
| 5738 | // We have a mismatch between the endpoint we were expecting and the one that we got. |
||
| 5739 | // The user may be trying to select a new configuration. Discard the transaction. |
||
| 5740 | } |
||
| 5741 | |||
| 5742 | _USB_FindNextToken(); |
||
| 5743 | } // U1IRbits.TRNIF |
||
| 5744 | |||
| 5745 | |||
| 5746 | // ------------------------------------------------------------------------- |
||
| 5747 | // Start-of-Frame ISR |
||
| 5748 | |||
| 5749 | if (U1IEbits.SOFIE && U1IRbits.SOFIF) |
||
| 5750 | { |
||
| 5751 | USB_ENDPOINT_INFO *pEndpoint; |
||
| 5752 | USB_INTERFACE_INFO *pInterface; |
||
| 5753 | |||
| 5754 | #ifdef DEBUG_MODE |
||
| 5755 | // UART2PutChar( '$' ); |
||
| 5756 | #endif |
||
| 5757 | U1IR = USB_INTERRUPT_SOF; // Clear the interrupt by writing a '1' to the flag. |
||
| 5758 | |||
| 5759 | pInterface = usbDeviceInfo.pInterfaceList; |
||
| 5760 | while (pInterface) |
||
| 5761 | { |
||
| 5762 | if (pInterface->pCurrentSetting) |
||
| 5763 | { |
||
| 5764 | pEndpoint = pInterface->pCurrentSetting->pEndpointList; |
||
| 5765 | while (pEndpoint) |
||
| 5766 | { |
||
| 5767 | // Decrement the interval count of all active interrupt and isochronous endpoints. |
||
| 5768 | if ((pEndpoint->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_INTERRUPT) || |
||
| 5769 | (pEndpoint->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_ISOCHRONOUS)) |
||
| 5770 | { |
||
| 5771 | if (pEndpoint->wIntervalCount != 0) |
||
| 5772 | { |
||
| 5773 | pEndpoint->wIntervalCount--; |
||
| 5774 | } |
||
| 5775 | } |
||
| 5776 | |||
| 5777 | #ifndef ALLOW_MULTIPLE_NAKS_PER_FRAME |
||
| 5778 | pEndpoint->status.bfLastTransferNAKd = 0; |
||
| 5779 | #endif |
||
| 5780 | |||
| 5781 | pEndpoint = pEndpoint->next; |
||
| 5782 | } |
||
| 5783 | } |
||
| 5784 | |||
| 5785 | pInterface = pInterface->next; |
||
| 5786 | } |
||
| 5787 | |||
| 5788 | usbBusInfo.flags.bfControlTransfersDone = 0; |
||
| 5789 | usbBusInfo.flags.bfInterruptTransfersDone = 0; |
||
| 5790 | usbBusInfo.flags.bfIsochronousTransfersDone = 0; |
||
| 5791 | usbBusInfo.flags.bfBulkTransfersDone = 0; |
||
| 5792 | //usbBusInfo.dBytesSentInFrame = 0; |
||
| 5793 | usbBusInfo.lastBulkTransaction = 0; |
||
| 5794 | |||
| 5795 | _USB_FindNextToken(); |
||
| 5796 | } |
||
| 5797 | |||
| 5798 | // ------------------------------------------------------------------------- |
||
| 5799 | // USB Error ISR |
||
| 5800 | |||
| 5801 | if (U1IEbits.UERRIE && U1IRbits.UERRIF) |
||
| 5802 | { |
||
| 5803 | #ifdef DEBUG_MODE |
||
| 5804 | UART2PutChar('#'); |
||
| 5805 | UART2PutHex( U1EIR ); |
||
| 5806 | #endif |
||
| 5807 | |||
| 5808 | // The previous token has finished, so clear the way for writing a new one. |
||
| 5809 | usbBusInfo.flags.bfTokenAlreadyWritten = 0; |
||
| 5810 | |||
| 5811 | // If we are doing isochronous transfers, ignore the error. |
||
| 5812 | if (pCurrentEndpoint->bmAttributes.bfTransferType == USB_TRANSFER_TYPE_ISOCHRONOUS) |
||
| 5813 | { |
||
| 5814 | // pCurrentEndpoint->status.bfTransferSuccessful = 1; |
||
| 5815 | // _USB_SetNextTransferState(); |
||
| 5816 | } |
||
| 5817 | else |
||
| 5818 | { |
||
| 5819 | // Increment the error count. |
||
| 5820 | pCurrentEndpoint->status.bfErrorCount++; |
||
| 5821 | |||
| 5822 | if (pCurrentEndpoint->status.bfErrorCount >= USB_TRANSACTION_RETRY_ATTEMPTS) |
||
| 5823 | { |
||
| 5824 | // We have too many errors. |
||
| 5825 | |||
| 5826 | // Check U1EIR for the appropriate error codes to return |
||
| 5827 | if (U1EIRbits.BTSEF) |
||
| 5828 | pCurrentEndpoint->bErrorCode = USB_ENDPOINT_ERROR_BIT_STUFF; |
||
| 5829 | if (U1EIRbits.DMAEF) |
||
| 5830 | pCurrentEndpoint->bErrorCode = USB_ENDPOINT_ERROR_DMA; |
||
| 5831 | if (U1EIRbits.BTOEF) |
||
| 5832 | pCurrentEndpoint->bErrorCode = USB_ENDPOINT_ERROR_TIMEOUT; |
||
| 5833 | if (U1EIRbits.DFN8EF) |
||
| 5834 | pCurrentEndpoint->bErrorCode = USB_ENDPOINT_ERROR_DATA_FIELD; |
||
| 5835 | if (U1EIRbits.CRC16EF) |
||
| 5836 | pCurrentEndpoint->bErrorCode = USB_ENDPOINT_ERROR_CRC16; |
||
| 5837 | if (U1EIRbits.EOFEF) |
||
| 5838 | pCurrentEndpoint->bErrorCode = USB_ENDPOINT_ERROR_END_OF_FRAME; |
||
| 5839 | if (U1EIRbits.PIDEF) |
||
| 5840 | pCurrentEndpoint->bErrorCode = USB_ENDPOINT_ERROR_PID_CHECK; |
||
| 5841 | #if defined(__PIC32MX__) |
||
| 5842 | if (U1EIRbits.BMXEF) |
||
| 5843 | pCurrentEndpoint->bErrorCode = USB_ENDPOINT_ERROR_BMX; |
||
| 5844 | #endif |
||
| 5845 | |||
| 5846 | pCurrentEndpoint->status.bfError = 1; |
||
| 5847 | |||
| 5848 | _USB_SetTransferErrorState( pCurrentEndpoint ); |
||
| 5849 | } |
||
| 5850 | } |
||
| 5851 | |||
| 5852 | U1EIR = 0xFF; // Clear the interrupts by writing '1' to the flags. |
||
| 5853 | U1IR = USB_INTERRUPT_ERROR; // Clear the interrupt by writing a '1' to the flag. |
||
| 5854 | } |
||
| 5855 | } |
||
| 5856 | |||
| 5857 | |||
| 5858 | /************************************************************************* |
||
| 5859 | * EOF usb_host.c |
||
| 5860 | */ |
||
| 5861 |
Powered by WebSVN v2.8.3