| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | /****************************************************************************** |
| 2 | |||
| 3 | USB Host Generic Client Driver |
||
| 4 | |||
| 5 | This is the Generic client driver file for a USB Embedded Host device. This |
||
| 6 | driver should be used in a project with usb_host.c to provided the USB hardware |
||
| 7 | interface. |
||
| 8 | |||
| 9 | To interface with USB Embedded Host layer, the routine USBHostGenericInit() |
||
| 10 | should be specified as the Initialize() function, and |
||
| 11 | USBHostGenericEventHandler() should be specified as the EventHandler() function |
||
| 12 | in the usbClientDrvTable[] array declared in usb_config.c. |
||
| 13 | |||
| 14 | This driver can be configured to either use transfer events from usb_host.c or |
||
| 15 | use a polling mechanism. If USB_ENABLE_TRANSFER_EVENT is defined, this |
||
| 16 | driver will utilize transfer events. Otherwise, this driver will utilize |
||
| 17 | polling. |
||
| 18 | |||
| 19 | Since the generic class is performed with interrupt transfers, |
||
| 20 | USB_SUPPORT_INTERRUPT_TRANSFERS must be defined. |
||
| 21 | |||
| 22 | *******************************************************************************/ |
||
| 23 | //DOM-IGNORE-BEGIN |
||
| 24 | /****************************************************************************** |
||
| 25 | |||
| 26 | FileName: usb_client_generic.c |
||
| 27 | Dependencies: None |
||
| 28 | Processor: PIC24/dsPIC30/dsPIC33/PIC32MX |
||
| 29 | Compiler: C30/C32 |
||
| 30 | Company: Microchip Technology, Inc. |
||
| 31 | |||
| 32 | Software License Agreement |
||
| 33 | |||
| 34 | The software supplied herewith by Microchip Technology Incorporated |
||
| 35 | (the Company) for its PICmicro® Microcontroller is intended and |
||
| 36 | supplied to you, the Companys customer, for use solely and |
||
| 37 | exclusively on Microchip PICmicro Microcontroller products. The |
||
| 38 | software is owned by the Company and/or its supplier, and is |
||
| 39 | protected under applicable copyright laws. All rights are reserved. |
||
| 40 | Any use in violation of the foregoing restrictions may subject the |
||
| 41 | user to criminal sanctions under applicable laws, as well as to |
||
| 42 | civil liability for the breach of the terms and conditions of this |
||
| 43 | license. |
||
| 44 | |||
| 45 | THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES, |
||
| 46 | WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED |
||
| 47 | TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
||
| 48 | PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, |
||
| 49 | IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR |
||
| 50 | CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
||
| 51 | |||
| 52 | Change History: |
||
| 53 | Rev Description |
||
| 54 | ----- ----------- |
||
| 55 | 2.6-2.7a No change |
||
| 56 | |||
| 57 | *******************************************************************************/ |
||
| 58 | //DOM-IGNORE-END |
||
| 59 | |||
| 60 | #include <stdlib.h> |
||
| 61 | #include <string.h> |
||
| 62 | #include "GenericTypeDefs.h" |
||
| 63 | #include "USB\usb.h" |
||
| 64 | #include "USB\usb_host_generic.h" |
||
| 65 | |||
| 66 | //#define DEBUG_MODE |
||
| 67 | #ifdef DEBUG_MODE |
||
| 68 | #include "uart2.h" |
||
| 69 | #endif |
||
| 70 | |||
| 71 | |||
| 72 | // ***************************************************************************** |
||
| 73 | // ***************************************************************************** |
||
| 74 | // Section: Configuration |
||
| 75 | // ***************************************************************************** |
||
| 76 | // ***************************************************************************** |
||
| 77 | |||
| 78 | // ***************************************************************************** |
||
| 79 | /* Max Number of Supported Devices |
||
| 80 | |||
| 81 | This value represents the maximum number of attached devices this class driver |
||
| 82 | can support. If the user does not define a value, it will be set to 1. |
||
| 83 | Currently this must be set to 1, due to limitations in the USB Host layer. |
||
| 84 | */ |
||
| 85 | #ifndef USB_MAX_GENERIC_DEVICES |
||
| 86 | #define USB_MAX_GENERIC_DEVICES 1 |
||
| 87 | #endif |
||
| 88 | |||
| 89 | #if USB_MAX_GENERIC_DEVICES != 1 |
||
| 90 | #error The Generic client driver supports only one attached device. |
||
| 91 | #endif |
||
| 92 | |||
| 93 | // ***************************************************************************** |
||
| 94 | // ***************************************************************************** |
||
| 95 | // Section: Global Variables |
||
| 96 | // ***************************************************************************** |
||
| 97 | // ***************************************************************************** |
||
| 98 | |||
| 99 | GENERIC_DEVICE gc_DevData; |
||
| 100 | |||
| 101 | #ifdef USB_GENERIC_SUPPORT_SERIAL_NUMBERS |
||
| 102 | #ifndef USB_GENERIC_MAX_SERIAL_NUMBER |
||
| 103 | #define USB_GENERIC_MAX_SERIAL_NUMBER 64 |
||
| 104 | #endif |
||
| 105 | |||
| 106 | WORD serialNumbers[USB_MAX_GENERIC_DEVICES][USB_GENERIC_MAX_SERIAL_NUMBER]; |
||
| 107 | #endif |
||
| 108 | |||
| 109 | |||
| 110 | // ***************************************************************************** |
||
| 111 | // ***************************************************************************** |
||
| 112 | // Section: Host Stack Interface Functions |
||
| 113 | // ***************************************************************************** |
||
| 114 | // ***************************************************************************** |
||
| 115 | |||
| 116 | /**************************************************************************** |
||
| 117 | Function: |
||
| 118 | BOOL USBHostGenericInit ( BYTE address, DWORD flags, BYTE clientDriverID ) |
||
| 119 | |||
| 120 | Summary: |
||
| 121 | This function is called by the USB Embedded Host layer when a "generic" |
||
| 122 | device attaches. |
||
| 123 | |||
| 124 | Description: |
||
| 125 | This routine is a call out from the USB Embedded Host layer to the USB |
||
| 126 | generic client driver. It is called when a "generic" device has been |
||
| 127 | connected to the host. Its purpose is to initialize and activate the USB |
||
| 128 | Generic client driver. |
||
| 129 | |||
| 130 | Preconditions: |
||
| 131 | The device has been configured. |
||
| 132 | |||
| 133 | Parameters: |
||
| 134 | BYTE address - Device's address on the bus |
||
| 135 | DWORD flags - Initialization flags |
||
| 136 | BYTE clientDriverID - ID to send when issuing a Device Request via |
||
| 137 | USBHostIssueDeviceRequest(), USBHostSetDeviceConfiguration(), |
||
| 138 | or USBHostSetDeviceInterface(). |
||
| 139 | |||
| 140 | Return Values: |
||
| 141 | TRUE - Initialization was successful |
||
| 142 | FALSE - Initialization failed |
||
| 143 | |||
| 144 | Remarks: |
||
| 145 | Multiple client drivers may be used in a single application. The USB |
||
| 146 | Embedded Host layer will call the initialize routine required for the |
||
| 147 | attached device. |
||
| 148 | ***************************************************************************/ |
||
| 149 | |||
| 150 | BOOL USBHostGenericInit ( BYTE address, DWORD flags, BYTE clientDriverID ) |
||
| 151 | { |
||
| 152 | BYTE *pDesc; |
||
| 153 | |||
| 154 | // Initialize state |
||
| 155 | gc_DevData.rxLength = 0; |
||
| 156 | gc_DevData.flags.val = 0; |
||
| 157 | |||
| 158 | // Save device the address, VID, & PID |
||
| 159 | gc_DevData.ID.deviceAddress = address; |
||
| 160 | pDesc = USBHostGetDeviceDescriptor(address); |
||
| 161 | pDesc += 8; |
||
| 162 | gc_DevData.ID.vid = (WORD)*pDesc; pDesc++; |
||
| 163 | gc_DevData.ID.vid |= ((WORD)*pDesc) << 8; pDesc++; |
||
| 164 | gc_DevData.ID.pid = (WORD)*pDesc; pDesc++; |
||
| 165 | gc_DevData.ID.pid |= ((WORD)*pDesc) << 8; pDesc++; |
||
| 166 | |||
| 167 | // Save the Client Driver ID |
||
| 168 | gc_DevData.clientDriverID = clientDriverID; |
||
| 169 | |||
| 170 | #ifdef DEBUG_MODE |
||
| 171 | UART2PrintString( "GEN: USB Generic Client Initalized: flags=0x" ); |
||
| 172 | UART2PutHex( flags ); |
||
| 173 | UART2PrintString( " address=" ); |
||
| 174 | UART2PutDec( address ); |
||
| 175 | UART2PrintString( " VID=0x" ); |
||
| 176 | UART2PutHex( gc_DevData.ID.vid >> 8 ); |
||
| 177 | UART2PutHex( gc_DevData.ID.vid & 0xFF ); |
||
| 178 | UART2PrintString( " PID=0x" ); |
||
| 179 | UART2PutHex( gc_DevData.ID.pid >> 8 ); |
||
| 180 | UART2PutHex( gc_DevData.ID.pid & 0xFF ); |
||
| 181 | UART2PrintString( "\r\n" ); |
||
| 182 | #endif |
||
| 183 | |||
| 184 | #ifdef USB_GENERIC_SUPPORT_SERIAL_NUMBERS |
||
| 185 | { |
||
| 186 | BYTE *deviceDescriptor; |
||
| 187 | BYTE serialNumberIndex; |
||
| 188 | |||
| 189 | // TODO when multiple devices are implemented, this init will change |
||
| 190 | // to find a free slot |
||
| 191 | gc_DevData.flags.serialNumberValid = 0; |
||
| 192 | |||
| 193 | gc_DevData.ID.serialNumber = &(serialNumbers[0][0]); |
||
| 194 | gc_DevData.ID.serialNumberLength = 0; |
||
| 195 | |||
| 196 | deviceDescriptor = USBHostGetDeviceDescriptor( deviceAddress ); |
||
| 197 | serialNumberIndex = deviceDescriptor[16]; |
||
| 198 | |||
| 199 | if (serialNumberIndex) |
||
| 200 | { |
||
| 201 | #ifdef DEBUG_MODE |
||
| 202 | UART2PrintString( "GEN: Getting serial number...\r\n" ); |
||
| 203 | #endif |
||
| 204 | } |
||
| 205 | else |
||
| 206 | { |
||
| 207 | serialNumberIndex = 1; |
||
| 208 | #ifdef DEBUG_MODE |
||
| 209 | UART2PrintString( "GEN: Getting string descriptor...\r\n" ); |
||
| 210 | #endif |
||
| 211 | } |
||
| 212 | |||
| 213 | if (USBHostGetStringDescriptor( address, serialNumberIndex, (BYTE *)gc_DevData.ID.serialNumber, USB_GENERIC_MAX_SERIAL_NUMBER*2 )) |
||
| 214 | { |
||
| 215 | // We can't get the serial number. Just set the pointer to null |
||
| 216 | // and call it good. We have to call the SN valid so we don't get trapped |
||
| 217 | // in the event handler. |
||
| 218 | gc_DevData.ID.serialNumber = NULL; |
||
| 219 | gc_DevData.flags.initialized = 1; |
||
| 220 | gc_DevData.flags.serialNumberValid = 1; |
||
| 221 | |||
| 222 | // Tell the application layer that we have a device. |
||
| 223 | USB_HOST_APP_EVENT_HANDLER(address, EVENT_GENERIC_ATTACH, &(gc_DevData.ID), sizeof(GENERIC_DEVICE_ID) ); |
||
| 224 | |||
| 225 | #ifdef DEBUG_MODE |
||
| 226 | UART2PrintString( "GEN: Cannot get string descriptor!\r\n" ); |
||
| 227 | #endif |
||
| 228 | } |
||
| 229 | } |
||
| 230 | #else |
||
| 231 | // Generic Client Driver Init Complete. |
||
| 232 | gc_DevData.flags.initialized = 1; |
||
| 233 | |||
| 234 | // Notify that application that we've been attached to a device. |
||
| 235 | USB_HOST_APP_EVENT_HANDLER(address, EVENT_GENERIC_ATTACH, &(gc_DevData.ID), sizeof(GENERIC_DEVICE_ID) ); |
||
| 236 | #endif |
||
| 237 | |||
| 238 | // TBD |
||
| 239 | |||
| 240 | return TRUE; |
||
| 241 | |||
| 242 | } // USBHostGenericInit |
||
| 243 | |||
| 244 | |||
| 245 | /**************************************************************************** |
||
| 246 | Function: |
||
| 247 | BOOL USBHostGenericEventHandler ( BYTE address, USB_EVENT event, |
||
| 248 | void *data, DWORD size ) |
||
| 249 | |||
| 250 | Summary: |
||
| 251 | This routine is called by the Host layer to notify the general client of |
||
| 252 | events that occur. |
||
| 253 | |||
| 254 | Description: |
||
| 255 | This routine is called by the Host layer to notify the general client of |
||
| 256 | events that occur. If the event is recognized, it is handled and the |
||
| 257 | routine returns TRUE. Otherwise, it is ignored and the routine returns |
||
| 258 | FALSE. |
||
| 259 | |||
| 260 | Preconditions: |
||
| 261 | None |
||
| 262 | |||
| 263 | Parameters: |
||
| 264 | BYTE address - Address of device with the event |
||
| 265 | USB_EVENT event - The bus event that occured |
||
| 266 | void *data - Pointer to event-specific data |
||
| 267 | DWORD size - Size of the event-specific data |
||
| 268 | |||
| 269 | Return Values: |
||
| 270 | TRUE - The event was handled |
||
| 271 | FALSE - The event was not handled |
||
| 272 | |||
| 273 | Remarks: |
||
| 274 | None |
||
| 275 | ***************************************************************************/ |
||
| 276 | |||
| 277 | BOOL USBHostGenericEventHandler ( BYTE address, USB_EVENT event, void *data, DWORD size ) |
||
| 278 | { |
||
| 279 | // Make sure it was for our device |
||
| 280 | if ( address != gc_DevData.ID.deviceAddress) |
||
| 281 | { |
||
| 282 | return FALSE; |
||
| 283 | } |
||
| 284 | |||
| 285 | // Handle specific events. |
||
| 286 | switch (event) |
||
| 287 | { |
||
| 288 | case EVENT_DETACH: |
||
| 289 | // Notify that application that the device has been detached. |
||
| 290 | USB_HOST_APP_EVENT_HANDLER(gc_DevData.ID.deviceAddress, EVENT_GENERIC_DETACH, &gc_DevData.ID.deviceAddress, sizeof(BYTE) ); |
||
| 291 | gc_DevData.flags.val = 0; |
||
| 292 | gc_DevData.ID.deviceAddress = 0; |
||
| 293 | #ifdef DEBUG_MODE |
||
| 294 | UART2PrintString( "USB Generic Client Device Detached: address=" ); |
||
| 295 | UART2PutDec( address ); |
||
| 296 | UART2PrintString( "\r\n" ); |
||
| 297 | #endif |
||
| 298 | return TRUE; |
||
| 299 | |||
| 300 | #ifdef USB_ENABLE_TRANSFER_EVENT |
||
| 301 | case EVENT_TRANSFER: |
||
| 302 | if ( (data != NULL) && (size == sizeof(HOST_TRANSFER_DATA)) ) |
||
| 303 | { |
||
| 304 | DWORD dataCount = ((HOST_TRANSFER_DATA *)data)->dataCount; |
||
| 305 | |||
| 306 | if ( ((HOST_TRANSFER_DATA *)data)->bEndpointAddress == (USB_IN_EP|USB_GENERIC_EP) ) |
||
| 307 | { |
||
| 308 | gc_DevData.flags.rxBusy = 0; |
||
| 309 | gc_DevData.rxLength = dataCount; |
||
| 310 | USB_HOST_APP_EVENT_HANDLER(gc_DevData.ID.deviceAddress, EVENT_GENERIC_RX_DONE, &dataCount, sizeof(DWORD) ); |
||
| 311 | } |
||
| 312 | else if ( ((HOST_TRANSFER_DATA *)data)->bEndpointAddress == (USB_OUT_EP|USB_GENERIC_EP) ) |
||
| 313 | { |
||
| 314 | gc_DevData.flags.txBusy = 0; |
||
| 315 | USB_HOST_APP_EVENT_HANDLER(gc_DevData.ID.deviceAddress, EVENT_GENERIC_TX_DONE, &dataCount, sizeof(DWORD) ); |
||
| 316 | } |
||
| 317 | else |
||
| 318 | #ifdef USB_GENERIC_SUPPORT_SERIAL_NUMBERS |
||
| 319 | if (((((HOST_TRANSFER_DATA *)data)->bEndpointAddress & 0x7F) == 0) && !gc_DevData.flags.serialNumberValid) |
||
| 320 | { |
||
| 321 | #ifdef DEBUG_MODE |
||
| 322 | UART2PrintString( "GEN: Got serial number!\r\n" ); |
||
| 323 | #endif |
||
| 324 | // Set the serial number information |
||
| 325 | gc_DevData.ID.serialNumberLength = dataCount; |
||
| 326 | gc_DevData.flags.serialNumberValid = 1; |
||
| 327 | gc_DevData.flags.initialized = 1; |
||
| 328 | |||
| 329 | // Tell the application layer that we have a device. |
||
| 330 | USB_HOST_APP_EVENT_HANDLER(address, EVENT_GENERIC_ATTACH, &(gc_DevData.ID), sizeof(GENERIC_DEVICE_ID) ); |
||
| 331 | } |
||
| 332 | else |
||
| 333 | #endif |
||
| 334 | { |
||
| 335 | return FALSE; |
||
| 336 | } |
||
| 337 | |||
| 338 | return TRUE; |
||
| 339 | |||
| 340 | } |
||
| 341 | else |
||
| 342 | return FALSE; |
||
| 343 | #endif |
||
| 344 | |||
| 345 | case EVENT_SUSPEND: |
||
| 346 | case EVENT_RESUME: |
||
| 347 | case EVENT_BUS_ERROR: |
||
| 348 | default: |
||
| 349 | break; |
||
| 350 | } |
||
| 351 | |||
| 352 | return FALSE; |
||
| 353 | } // USBHostGenericEventHandler |
||
| 354 | |||
| 355 | |||
| 356 | // ***************************************************************************** |
||
| 357 | // ***************************************************************************** |
||
| 358 | // Section: Application Callable Functions |
||
| 359 | // ***************************************************************************** |
||
| 360 | // ***************************************************************************** |
||
| 361 | |||
| 362 | /**************************************************************************** |
||
| 363 | Function: |
||
| 364 | BOOL USBHostGenericDeviceDetached( BYTE deviceAddress ) |
||
| 365 | |||
| 366 | Description: |
||
| 367 | This interface is used to check if the devich has been detached from the |
||
| 368 | bus. |
||
| 369 | |||
| 370 | Preconditions: |
||
| 371 | None |
||
| 372 | |||
| 373 | Parameters: |
||
| 374 | deviceAddress - USB Address of the device. |
||
| 375 | |||
| 376 | Return Values: |
||
| 377 | TRUE - The device has been detached, or an invalid deviceAddress is given. |
||
| 378 | FALSE - The device is attached |
||
| 379 | |||
| 380 | Example: |
||
| 381 | <code> |
||
| 382 | if (USBHostGenericDeviceDetached( deviceAddress )) |
||
| 383 | { |
||
| 384 | // Handle detach |
||
| 385 | } |
||
| 386 | </code> |
||
| 387 | |||
| 388 | Remarks: |
||
| 389 | None |
||
| 390 | ***************************************************************************/ |
||
| 391 | |||
| 392 | // Implemented as a macro. See usb_client_generic.h |
||
| 393 | |||
| 394 | |||
| 395 | /**************************************************************************** |
||
| 396 | Function: |
||
| 397 | BOOL USBHostGenericGetDeviceAddress(GENERIC_DEVICE_ID *pDevID) |
||
| 398 | |||
| 399 | Description: |
||
| 400 | This interface is used get the address of a specific generic device on |
||
| 401 | the USB. |
||
| 402 | |||
| 403 | Preconditions: |
||
| 404 | The device must be connected and enumerated. |
||
| 405 | |||
| 406 | Parameters: |
||
| 407 | pDevID - Pointer to a structure containing the Device ID Info (VID, |
||
| 408 | PID, serial number, and device address). |
||
| 409 | |||
| 410 | Return Values: |
||
| 411 | TRUE - The device is connected |
||
| 412 | FALSE - The device is not connected. |
||
| 413 | |||
| 414 | Example: |
||
| 415 | <code> |
||
| 416 | GENERIC_DEVICE_ID deviceID; |
||
| 417 | WORD serialNumber[] = { '1', '2', '3', '4', '5', '6' }; |
||
| 418 | BYTE deviceAddress; |
||
| 419 | |||
| 420 | deviceID.vid = 0x1234; |
||
| 421 | deviceID.pid = 0x5678; |
||
| 422 | deviceID.serialNumber = &serialNumber; |
||
| 423 | |||
| 424 | if (USBHostGenericGetDeviceAddress(&deviceID)) |
||
| 425 | { |
||
| 426 | deviceAddress = deviceID.deviceAddress; |
||
| 427 | } |
||
| 428 | </code> |
||
| 429 | |||
| 430 | Remarks: |
||
| 431 | None |
||
| 432 | ***************************************************************************/ |
||
| 433 | |||
| 434 | BOOL USBHostGenericGetDeviceAddress(GENERIC_DEVICE_ID *pDevID) |
||
| 435 | { |
||
| 436 | if (!gc_DevData.flags.initialized) return FALSE; |
||
| 437 | |||
| 438 | if (gc_DevData.ID.deviceAddress != 0 && pDevID != NULL) |
||
| 439 | { |
||
| 440 | if (pDevID->vid == gc_DevData.ID.vid && pDevID->pid == gc_DevData.ID.pid) |
||
| 441 | { |
||
| 442 | #ifdef USB_GENERIC_SUPPORT_SERIAL_NUMBERS |
||
| 443 | if (!strncmp( (char *)gc_DevData.ID.serialNumber, (char *)pDevID->serialNumber, gc_DevData.ID.serialNumberLength*2 )) |
||
| 444 | #endif |
||
| 445 | { |
||
| 446 | pDevID->deviceAddress = gc_DevData.ID.deviceAddress; |
||
| 447 | return TRUE; |
||
| 448 | } |
||
| 449 | } |
||
| 450 | } |
||
| 451 | |||
| 452 | return FALSE; |
||
| 453 | |||
| 454 | } // USBHostGenericGetDeviceAddress |
||
| 455 | |||
| 456 | |||
| 457 | /**************************************************************************** |
||
| 458 | Function: |
||
| 459 | DWORD USBHostGenericGetRxLength( BYTE deviceAddress ) |
||
| 460 | |||
| 461 | Description: |
||
| 462 | This function retrieves the number of bytes copied to user's buffer by |
||
| 463 | the most recent call to the USBHostGenericRead() function. |
||
| 464 | |||
| 465 | Preconditions: |
||
| 466 | The device must be connected and enumerated. |
||
| 467 | |||
| 468 | Parameters: |
||
| 469 | deviceAddress - USB Address of the device |
||
| 470 | |||
| 471 | Returns: |
||
| 472 | Returns the number of bytes most recently received from the Generic |
||
| 473 | device with address deviceAddress. |
||
| 474 | |||
| 475 | Remarks: |
||
| 476 | This function can only be called once per transfer. Subsequent calls will |
||
| 477 | return zero until new data has been received. |
||
| 478 | ***************************************************************************/ |
||
| 479 | |||
| 480 | // Implemented as a macro. See usb_client_generic.h |
||
| 481 | |||
| 482 | |||
| 483 | /**************************************************************************** |
||
| 484 | Function: |
||
| 485 | void USBHostGenericRead( BYTE deviceAddress, BYTE *buffer, DWORD length ) |
||
| 486 | |||
| 487 | Description: |
||
| 488 | Use this routine to receive from the device and store it into memory. |
||
| 489 | |||
| 490 | Preconditions: |
||
| 491 | The device must be connected and enumerated. |
||
| 492 | |||
| 493 | Parameters: |
||
| 494 | deviceAddress - USB Address of the device. |
||
| 495 | buffer - Pointer to the data buffer |
||
| 496 | length - Number of bytes to be transferred |
||
| 497 | |||
| 498 | Return Values: |
||
| 499 | USB_SUCCESS - The Read was started successfully |
||
| 500 | (USB error code) - The Read was not started. See USBHostRead() for |
||
| 501 | a list of errors. |
||
| 502 | |||
| 503 | Example: |
||
| 504 | <code> |
||
| 505 | if (!USBHostGenericRxIsBusy( deviceAddress )) |
||
| 506 | { |
||
| 507 | USBHostGenericRead( deviceAddress, &buffer, sizeof(buffer) ); |
||
| 508 | } |
||
| 509 | </code> |
||
| 510 | |||
| 511 | Remarks: |
||
| 512 | None |
||
| 513 | ***************************************************************************/ |
||
| 514 | |||
| 515 | BYTE USBHostGenericRead( BYTE deviceAddress, void *buffer, DWORD length ) |
||
| 516 | { |
||
| 517 | BYTE RetVal; |
||
| 518 | |||
| 519 | // Validate the call |
||
| 520 | if (!API_VALID(deviceAddress)) return USB_INVALID_STATE; |
||
| 521 | if (gc_DevData.flags.rxBusy) return USB_BUSY; |
||
| 522 | |||
| 523 | // Set the busy flag, clear the count and start a new IN transfer. |
||
| 524 | gc_DevData.flags.rxBusy = 1; |
||
| 525 | gc_DevData.rxLength = 0; |
||
| 526 | RetVal = USBHostRead( deviceAddress, USB_IN_EP|USB_GENERIC_EP, (BYTE *)buffer, length ); |
||
| 527 | if (RetVal != USB_SUCCESS) |
||
| 528 | { |
||
| 529 | gc_DevData.flags.rxBusy = 0; // Clear flag to allow re-try |
||
| 530 | } |
||
| 531 | |||
| 532 | return RetVal; |
||
| 533 | |||
| 534 | } // USBHostGenericRead |
||
| 535 | |||
| 536 | /**************************************************************************** |
||
| 537 | Function: |
||
| 538 | BOOL USBHostGenericRxIsBusy( BYTE deviceAddress ) |
||
| 539 | |||
| 540 | Summary: |
||
| 541 | This interface is used to check if the client driver is currently busy |
||
| 542 | receiving data from the device. |
||
| 543 | |||
| 544 | Description: |
||
| 545 | This interface is used to check if the client driver is currently busy |
||
| 546 | receiving data from the device. This function is intended for use with |
||
| 547 | transfer events. With polling, the function USBHostGenericRxIsComplete() |
||
| 548 | should be used. |
||
| 549 | |||
| 550 | Preconditions: |
||
| 551 | The device must be connected and enumerated. |
||
| 552 | |||
| 553 | Parameters: |
||
| 554 | deviceAddress - USB Address of the device |
||
| 555 | |||
| 556 | Return Values: |
||
| 557 | TRUE - The device is receiving data or an invalid deviceAddress is |
||
| 558 | given. |
||
| 559 | FALSE - The device is not receiving data |
||
| 560 | |||
| 561 | Example: |
||
| 562 | <code> |
||
| 563 | if (!USBHostGenericRxIsBusy( deviceAddress )) |
||
| 564 | { |
||
| 565 | USBHostGenericRead( deviceAddress, &Buffer, sizeof( Buffer ) ); |
||
| 566 | } |
||
| 567 | </code> |
||
| 568 | |||
| 569 | Remarks: |
||
| 570 | None |
||
| 571 | ***************************************************************************/ |
||
| 572 | |||
| 573 | // Implemented as a macro. See usb_client_generic.h |
||
| 574 | |||
| 575 | |||
| 576 | /**************************************************************************** |
||
| 577 | Function: |
||
| 578 | BOOL USBHostGenericRxIsComplete( BYTE deviceAddress, BYTE *errorCode, |
||
| 579 | DWORD *byteCount ) |
||
| 580 | |||
| 581 | Summary: |
||
| 582 | This routine indicates whether or not the last IN transfer is complete. |
||
| 583 | |||
| 584 | Description: |
||
| 585 | This routine indicates whether or not the last IN transfer is complete. |
||
| 586 | If it is, then the returned errorCode and byteCount are valid, and |
||
| 587 | reflect the error code and the number of bytes received. |
||
| 588 | |||
| 589 | This function is intended for use with polling. With transfer events, |
||
| 590 | the function USBHostGenericRxIsBusy() should be used. |
||
| 591 | |||
| 592 | Preconditions: |
||
| 593 | None |
||
| 594 | |||
| 595 | Parameters: |
||
| 596 | BYTE deviceAddress - Address of the attached peripheral |
||
| 597 | BYTE *errorCode - Error code of the last transfer, if complete |
||
| 598 | DWORD *byteCount - Bytes transferred during the last transfer, if |
||
| 599 | complete |
||
| 600 | |||
| 601 | Return Values: |
||
| 602 | TRUE - The IN transfer is complete. errorCode and byteCount are valid. |
||
| 603 | FALSE - The IN transfer is not complete. errorCode and byteCount are |
||
| 604 | invalid. |
||
| 605 | |||
| 606 | Remarks: |
||
| 607 | None |
||
| 608 | ***************************************************************************/ |
||
| 609 | |||
| 610 | #ifndef USB_ENABLE_TRANSFER_EVENT |
||
| 611 | BOOL USBHostGenericRxIsComplete( BYTE deviceAddress, |
||
| 612 | BYTE *errorCode, DWORD *byteCount ) |
||
| 613 | { |
||
| 614 | if (gc_DevData.flags.rxBusy) |
||
| 615 | { |
||
| 616 | return FALSE; |
||
| 617 | } |
||
| 618 | else |
||
| 619 | { |
||
| 620 | *byteCount = gc_DevData.rxLength; |
||
| 621 | *errorCode = gc_DevData.rxErrorCode; |
||
| 622 | return TRUE; |
||
| 623 | } |
||
| 624 | } |
||
| 625 | #endif |
||
| 626 | |||
| 627 | /**************************************************************************** |
||
| 628 | Function: |
||
| 629 | void USBHostGenericTasks( void ) |
||
| 630 | |||
| 631 | Summary: |
||
| 632 | This routine is used if transfer events are not utilized. It monitors the |
||
| 633 | host status and updates the transmit and receive flags. |
||
| 634 | |||
| 635 | Description: |
||
| 636 | This routine is used if transfer events are not utilized. It monitors the |
||
| 637 | host status and updates the transmit and receive flags. If serial |
||
| 638 | numbers are supported, then this routine handles the reception of the |
||
| 639 | serial number. |
||
| 640 | |||
| 641 | Preconditions: |
||
| 642 | None |
||
| 643 | |||
| 644 | Parameters: |
||
| 645 | None |
||
| 646 | |||
| 647 | Returns: |
||
| 648 | None |
||
| 649 | |||
| 650 | Remarks: |
||
| 651 | This function is compiled only if USB_ENABLE_TRANSFER_EVENT is not |
||
| 652 | defined. |
||
| 653 | ***************************************************************************/ |
||
| 654 | |||
| 655 | #ifndef USB_ENABLE_TRANSFER_EVENT |
||
| 656 | void USBHostGenericTasks( void ) |
||
| 657 | { |
||
| 658 | DWORD byteCount; |
||
| 659 | BYTE errorCode; |
||
| 660 | |||
| 661 | if (gc_DevData.ID.deviceAddress && gc_DevData.flags.initialized) |
||
| 662 | { |
||
| 663 | #ifdef USB_GENERIC_SUPPORT_SERIAL_NUMBERS |
||
| 664 | if (!gc_DevData.flags.serialNumberValid) |
||
| 665 | { |
||
| 666 | if (USBHostTransferIsComplete( gc_DevData.ID.deviceAddress, USB_IN_EP, &errorCode, &byteCount ); |
||
| 667 | { |
||
| 668 | #ifdef DEBUG_MODE |
||
| 669 | UART2PrintString( "GEN: Got serial number!\r\n" ); |
||
| 670 | #endif |
||
| 671 | // Set the serial number information |
||
| 672 | gc_DevData.ID.serialNumberLength = byteCount; |
||
| 673 | gc_DevData.flags.serialNumberValid = 1; |
||
| 674 | gc_DevData.flags.initialized = 1; |
||
| 675 | |||
| 676 | // Tell the application layer that we have a device. |
||
| 677 | USB_HOST_APP_EVENT_HANDLER(address, EVENT_GENERIC_ATTACH, &(gc_DevData.ID), sizeof(GENERIC_DEVICE_ID) ); |
||
| 678 | } |
||
| 679 | } |
||
| 680 | #endif |
||
| 681 | |||
| 682 | if (gc_DevData.flags.rxBusy) |
||
| 683 | { |
||
| 684 | if (USBHostTransferIsComplete( gc_DevData.ID.deviceAddress, USB_IN_EP|USB_GENERIC_EP, &errorCode, &byteCount )) |
||
| 685 | { |
||
| 686 | gc_DevData.flags.rxBusy = 0; |
||
| 687 | gc_DevData.rxLength = byteCount; |
||
| 688 | gc_DevData.rxErrorCode = errorCode; |
||
| 689 | } |
||
| 690 | } |
||
| 691 | |||
| 692 | if (gc_DevData.flags.txBusy) |
||
| 693 | { |
||
| 694 | if (USBHostTransferIsComplete( gc_DevData.ID.deviceAddress, USB_OUT_EP|USB_GENERIC_EP, &errorCode, &byteCount )) |
||
| 695 | { |
||
| 696 | gc_DevData.flags.txBusy = 0; |
||
| 697 | gc_DevData.txErrorCode = errorCode; |
||
| 698 | } |
||
| 699 | } |
||
| 700 | } |
||
| 701 | } |
||
| 702 | #endif |
||
| 703 | |||
| 704 | /**************************************************************************** |
||
| 705 | Function: |
||
| 706 | BOOL USBHostGenericTxIsBusy( BYTE deviceAddress ) |
||
| 707 | |||
| 708 | Summary: |
||
| 709 | This interface is used to check if the client driver is currently busy |
||
| 710 | transmitting data to the device. |
||
| 711 | |||
| 712 | Description: |
||
| 713 | This interface is used to check if the client driver is currently busy |
||
| 714 | transmitting data to the device. This function is intended for use with |
||
| 715 | transfer events. With polling, the function USBHostGenericTxIsComplete() |
||
| 716 | should be used. |
||
| 717 | |||
| 718 | Preconditions: |
||
| 719 | The device must be connected and enumerated. |
||
| 720 | |||
| 721 | Parameters: |
||
| 722 | deviceAddress - USB Address of the device |
||
| 723 | |||
| 724 | Return Values: |
||
| 725 | TRUE - The device is transmitting data or an invalid deviceAddress |
||
| 726 | is given. |
||
| 727 | FALSE - The device is not transmitting data |
||
| 728 | |||
| 729 | Example: |
||
| 730 | <code> |
||
| 731 | if (!USBHostGenericTxIsBusy( deviceAddress ) ) |
||
| 732 | { |
||
| 733 | USBHostGenericWrite( deviceAddress, &buffer, sizeof( buffer ) ); |
||
| 734 | } |
||
| 735 | </code> |
||
| 736 | |||
| 737 | Remarks: |
||
| 738 | None |
||
| 739 | ***************************************************************************/ |
||
| 740 | |||
| 741 | // Implemented as a macro. See usb_client_generic.h |
||
| 742 | |||
| 743 | |||
| 744 | /**************************************************************************** |
||
| 745 | Function: |
||
| 746 | BOOL USBHostGenericTxIsComplete( BYTE deviceAddress, BYTE *errorCode ) |
||
| 747 | |||
| 748 | Summary: |
||
| 749 | This routine indicates whether or not the last OUT transfer is complete. |
||
| 750 | |||
| 751 | Description: |
||
| 752 | This routine indicates whether or not the last OUT transfer is complete. |
||
| 753 | If it is, then the returned errorCode is valid, and reflect the error |
||
| 754 | code of the transfer. |
||
| 755 | |||
| 756 | This function is intended for use with polling. With transfer events, |
||
| 757 | the function USBHostGenericTxIsBusy() should be used. |
||
| 758 | |||
| 759 | Preconditions: |
||
| 760 | None |
||
| 761 | |||
| 762 | Parameters: |
||
| 763 | BYTE deviceAddress - Address of the attached peripheral |
||
| 764 | BYTE *errorCode - Error code of the last transfer, if complete |
||
| 765 | |||
| 766 | Return Values: |
||
| 767 | TRUE - The OUT transfer is complete. errorCode is valid. |
||
| 768 | FALSE - The OUT transfer is not complete. errorCode is invalid. |
||
| 769 | |||
| 770 | Remarks: |
||
| 771 | None |
||
| 772 | ***************************************************************************/ |
||
| 773 | |||
| 774 | #ifndef USB_ENABLE_TRANSFER_EVENT |
||
| 775 | BOOL USBHostGenericTxIsComplete( BYTE deviceAddress, BYTE *errorCode ) |
||
| 776 | { |
||
| 777 | if (gc_DevData.flags.txBusy) |
||
| 778 | { |
||
| 779 | return FALSE; |
||
| 780 | } |
||
| 781 | else |
||
| 782 | { |
||
| 783 | *errorCode = gc_DevData.txErrorCode; |
||
| 784 | return TRUE; |
||
| 785 | } |
||
| 786 | } |
||
| 787 | #endif |
||
| 788 | |||
| 789 | |||
| 790 | /**************************************************************************** |
||
| 791 | Function: |
||
| 792 | void USBHostGenericWrite( BYTE deviceAddress, BYTE *buffer, DWORD length ) |
||
| 793 | |||
| 794 | Description: |
||
| 795 | Use this routine to transmit data from memory to the device. |
||
| 796 | |||
| 797 | Preconditions: |
||
| 798 | The device must be connected and enumerated. |
||
| 799 | |||
| 800 | Parameters: |
||
| 801 | deviceAddress - USB Address of the device. |
||
| 802 | buffer - Pointer to the data buffer |
||
| 803 | length - Number of bytes to be transferred |
||
| 804 | |||
| 805 | Return Values: |
||
| 806 | USB_SUCCESS - The Write was started successfully |
||
| 807 | (USB error code) - The Write was not started. See USBHostWrite() for |
||
| 808 | a list of errors. |
||
| 809 | |||
| 810 | Example: |
||
| 811 | <code> |
||
| 812 | if (!USBHostGenericTxIsBusy( deviceAddress )) |
||
| 813 | { |
||
| 814 | USBHostGenericWrite( deviceAddress, &buffer, sizeof(buffer) ); |
||
| 815 | } |
||
| 816 | </code> |
||
| 817 | |||
| 818 | Remarks: |
||
| 819 | None |
||
| 820 | ***************************************************************************/ |
||
| 821 | |||
| 822 | BYTE USBHostGenericWrite( BYTE deviceAddress, void *buffer, DWORD length ) |
||
| 823 | { |
||
| 824 | BYTE RetVal; |
||
| 825 | |||
| 826 | // Validate the call |
||
| 827 | if (!API_VALID(deviceAddress)) return USB_INVALID_STATE; |
||
| 828 | if (gc_DevData.flags.txBusy) return USB_BUSY; |
||
| 829 | |||
| 830 | // Set the busy flag and start a new OUT transfer. |
||
| 831 | gc_DevData.flags.txBusy = 1; |
||
| 832 | RetVal = USBHostWrite( deviceAddress, USB_OUT_EP|USB_GENERIC_EP, (BYTE *)buffer, length ); |
||
| 833 | if (RetVal != USB_SUCCESS) |
||
| 834 | { |
||
| 835 | gc_DevData.flags.txBusy = 0; // Clear flag to allow re-try |
||
| 836 | } |
||
| 837 | |||
| 838 | return RetVal; |
||
| 839 | |||
| 840 | } // USBHostGenericWrite |
||
| 841 | |||
| 842 | |||
| 843 | /************************************************************************* |
||
| 844 | * EOF usb_client_generic.c |
||
| 845 | */ |
||
| 846 | |||
| 847 |
Powered by WebSVN v2.8.3