| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 32 | kaklik | /****************************************************************************** |
| 2 | File Information: |
||
| 3 | FileName: usb_function_ccid.c |
||
| 4 | Dependencies: See INCLUDES section below |
||
| 5 | Processor: PIC18, PIC24, or PIC32 |
||
| 6 | Compiler: C18, C30, or C32 |
||
| 7 | Company: Microchip Technology, Inc. |
||
| 8 | |||
| 9 | Software License Agreement |
||
| 10 | |||
| 11 | The software supplied herewith by Microchip Technology Incorporated |
||
| 12 | (the Company) for its PICmicro® Microcontroller is intended and |
||
| 13 | supplied to you, the Companys customer, for use solely and |
||
| 14 | exclusively on Microchip PICmicro Microcontroller products. The |
||
| 15 | software is owned by the Company and/or its supplier, and is |
||
| 16 | protected under applicable copyright laws. All rights are reserved. |
||
| 17 | Any use in violation of the foregoing restrictions may subject the |
||
| 18 | user to criminal sanctions under applicable laws, as well as to |
||
| 19 | civil liability for the breach of the terms and conditions of this |
||
| 20 | license. |
||
| 21 | |||
| 22 | THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES, |
||
| 23 | WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED |
||
| 24 | TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
||
| 25 | PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, |
||
| 26 | IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR |
||
| 27 | CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
||
| 28 | |||
| 29 | Summary: |
||
| 30 | This file contains functions, macros, definitions, variables, |
||
| 31 | datatypes, etc. that are required for use of CCID class function |
||
| 32 | drivers. This file should be included in projects that use CCID class |
||
| 33 | \function drivers. |
||
| 34 | |||
| 35 | |||
| 36 | |||
| 37 | This file is located in the "\<Install Directory\>\\Microchip\\USB\\CCID Device Driver" directory. |
||
| 38 | |||
| 39 | Description: |
||
| 40 | USB CCID Class Driver Header File |
||
| 41 | |||
| 42 | This file contains functions, macros, definitions, variables, |
||
| 43 | datatypes, etc. that are required for use of CCID class function |
||
| 44 | drivers. This file should be included in projects that use CCID class |
||
| 45 | \function drivers. |
||
| 46 | |||
| 47 | This file is located in the "\<Install Directory\>\\Microchip\\USB\\CCID |
||
| 48 | Device Driver" directory. |
||
| 49 | |||
| 50 | When including this file in a new project, this file can either be |
||
| 51 | referenced from the directory in which it was installed or copied |
||
| 52 | directly into the user application folder. If the first method is |
||
| 53 | chosen to keep the file located in the folder in which it is installed |
||
| 54 | then include paths need to be added so that the library and the |
||
| 55 | application both know where to reference each others files. If the |
||
| 56 | application folder is located in the same folder as the Microchip |
||
| 57 | folder (like the current demo folders), then the following include |
||
| 58 | paths need to be added to the application's project: |
||
| 59 | |||
| 60 | . |
||
| 61 | |||
| 62 | ..\\..\\Microchip\\Include |
||
| 63 | |||
| 64 | If a different directory structure is used, modify the paths as |
||
| 65 | required. An example using absolute paths instead of relative paths |
||
| 66 | would be the following: |
||
| 67 | |||
| 68 | C:\\Microchip Solutions\\Microchip\\Include |
||
| 69 | |||
| 70 | C:\\Microchip Solutions\\My Demo Application |
||
| 71 | ******************************************************************************/ |
||
| 72 | |||
| 73 | /******************************************************************** |
||
| 74 | Change History: |
||
| 75 | Rev Description |
||
| 76 | ---- ----------- |
||
| 77 | 2.7 Initial revision |
||
| 78 | |||
| 79 | 2.7a No change |
||
| 80 | |||
| 81 | ********************************************************************/ |
||
| 82 | |||
| 83 | /** I N C L U D E S **********************************************************/ |
||
| 84 | #include "USB\usb.h" |
||
| 85 | #include "USB\usb_function_ccid.h" |
||
| 86 | #if defined(USB_USE_CCID) |
||
| 87 | |||
| 88 | /** V A R I A B L E S ********************************************************/ |
||
| 89 | BYTE usbCcidBulkInTrfState; |
||
| 90 | WORD usbCcidBulkInLen; |
||
| 91 | POINTER pCCIDDst; // Dedicated destination pointer |
||
| 92 | POINTER pCCIDSrc; // Dedicated source pointer |
||
| 93 | |||
| 94 | |||
| 95 | /** P R I V A T E P R O T O T Y P E S ***************************************/ |
||
| 96 | #if defined USB_CCID_SUPPORT_ABORT_REQUEST |
||
| 97 | void USB_CCID_ABORT_REQUEST_HANDLER(void); |
||
| 98 | #endif |
||
| 99 | |||
| 100 | #if defined USB_CCID_SUPPORT_GET_CLOCK_FREQUENCIES_REQUEST |
||
| 101 | void USB_CCID_GET_CLOCK_FREQUENCIES_REQUEST_HANDLER(void); |
||
| 102 | #endif |
||
| 103 | |||
| 104 | #if defined USB_CCID_SUPPORT_GET_DATA_RATES_REQUEST |
||
| 105 | void USB_CCID_GET_DATA_RATES_REQUEST_HANDLER(void); |
||
| 106 | #endif |
||
| 107 | |||
| 108 | |||
| 109 | |||
| 110 | /** D E C L A R A T I O N S **************************************************/ |
||
| 111 | |||
| 112 | /** C L A S S S P E C I F I C R E Q ****************************************/ |
||
| 113 | /****************************************************************************** |
||
| 114 | Function: |
||
| 115 | void USBCheckCCIDRequest(void) |
||
| 116 | |||
| 117 | Description: |
||
| 118 | This routine checks the setup data packet to see if it |
||
| 119 | knows how to handle it |
||
| 120 | |||
| 121 | PreCondition: |
||
| 122 | None |
||
| 123 | |||
| 124 | Parameters: |
||
| 125 | None |
||
| 126 | |||
| 127 | Return Values: |
||
| 128 | None |
||
| 129 | |||
| 130 | Remarks: |
||
| 131 | None |
||
| 132 | |||
| 133 | *****************************************************************************/ |
||
| 134 | void USBCheckCCIDRequest(void) |
||
| 135 | { |
||
| 136 | /* |
||
| 137 | * If request recipient is not an interface then return |
||
| 138 | */ |
||
| 139 | if(SetupPkt.Recipient != USB_SETUP_RECIPIENT_INTERFACE_BITFIELD) return; |
||
| 140 | /* |
||
| 141 | * If request type is not class-specific then return |
||
| 142 | */ |
||
| 143 | if(SetupPkt.RequestType != USB_SETUP_TYPE_CLASS_BITFIELD) return; |
||
| 144 | /* |
||
| 145 | * Interface ID must match interface number associated with |
||
| 146 | * CCID class, else return |
||
| 147 | */ |
||
| 148 | if(SetupPkt.bIntfID != USB_CCID_INTERFACE_ID) |
||
| 149 | return; |
||
| 150 | switch (SetupPkt.bRequest)// checking for the request ID |
||
| 151 | { |
||
| 152 | #if defined (USB_CCID_SUPPORT_ABORT_REQUEST) |
||
| 153 | case USB_CCID_ABORT: |
||
| 154 | USB_CCID_ABORT_REQUEST_HANDLER(); |
||
| 155 | break; |
||
| 156 | #endif |
||
| 157 | |||
| 158 | #if defined (USB_CCID_SUPPORT_GET_CLOCK_FREQUENCIES_REQUEST) |
||
| 159 | case USB_CCID_GET_CLOCK_FREQUENCIES: |
||
| 160 | USB_CCID_GET_CLOCK_FREQUENCIES_REQUEST_HANDLER(); |
||
| 161 | break; |
||
| 162 | #endif |
||
| 163 | |||
| 164 | #if defined (USB_CCID_SUPPORT_GET_DATA_RATES_REQUEST) |
||
| 165 | case USB_CCID_GET_DATA_RATES: |
||
| 166 | USB_CCID_GET_DATA_RATES_REQUEST_HANDLER(); |
||
| 167 | break; |
||
| 168 | #endif |
||
| 169 | default: |
||
| 170 | break; |
||
| 171 | }//end switch(SetupPkt.bRequest) |
||
| 172 | }//end USBCheckCCIDRequest |
||
| 173 | |||
| 174 | |||
| 175 | /************************************************************************** |
||
| 176 | Function: |
||
| 177 | void USBCCIDInitEP(void) |
||
| 178 | |||
| 179 | Summary: |
||
| 180 | This function initializes the CCID function driver. This function should |
||
| 181 | be called after the SET_CONFIGURATION command. |
||
| 182 | Description: |
||
| 183 | This function initializes the CCID function driver. This function sets |
||
| 184 | the default line coding (baud rate, bit parity, number of data bits, |
||
| 185 | and format). This function also enables the endpoints and prepares for |
||
| 186 | the first transfer from the host. |
||
| 187 | |||
| 188 | This function should be called after the SET_CONFIGURATION command. |
||
| 189 | This is most simply done by calling this function from the |
||
| 190 | USBCBInitEP() function. |
||
| 191 | |||
| 192 | Typical Usage: |
||
| 193 | <code> |
||
| 194 | void USBCBInitEP(void) |
||
| 195 | { |
||
| 196 | USBCCIDInitEP(); |
||
| 197 | } |
||
| 198 | </code> |
||
| 199 | Conditions: |
||
| 200 | None |
||
| 201 | Remarks: |
||
| 202 | None |
||
| 203 | **************************************************************************/ |
||
| 204 | void USBCCIDInitEP(void) |
||
| 205 | { |
||
| 206 | |||
| 207 | usbCcidBulkInTrfState = USB_CCID_BULK_IN_READY; |
||
| 208 | usbCcidBulkInLen =0; |
||
| 209 | |||
| 210 | /* |
||
| 211 | * Do not have to init Cnt of IN pipes here. |
||
| 212 | * Reason: Number of BYTEs to send to the host |
||
| 213 | * varies from one transaction to |
||
| 214 | * another. Cnt should equal the exact |
||
| 215 | * number of BYTEs to transmit for |
||
| 216 | * a given IN transaction. |
||
| 217 | * This number of BYTEs will only |
||
| 218 | * be known right before the data is |
||
| 219 | * sent. |
||
| 220 | */ |
||
| 221 | |||
| 222 | USBEnableEndpoint(USB_EP_INT_IN,USB_IN_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP); |
||
| 223 | USBEnableEndpoint(USB_EP_BULK_IN,USB_IN_ENABLED|USB_OUT_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP); |
||
| 224 | |||
| 225 | usbCcidBulkInHandle = 0; |
||
| 226 | usbCcidInterruptInHandle = 0; |
||
| 227 | usbCcidBulkOutHandle = USBRxOnePacket(USB_EP_BULK_OUT,(BYTE*)&usbCcidBulkOutEndpoint,USB_EP_SIZE); |
||
| 228 | |||
| 229 | }//end CCIDInitEP |
||
| 230 | |||
| 231 | |||
| 232 | /************************************************************************ |
||
| 233 | Function: |
||
| 234 | void USBCCIDBulkInService(void) |
||
| 235 | |||
| 236 | Summary: |
||
| 237 | USBCCIDBulkInService handles device-to-host transaction(s). This function |
||
| 238 | should be called once per Main Program loop after the device reaches |
||
| 239 | the configured state. |
||
| 240 | Description: |
||
| 241 | USBCCIDBulkInService handles device-to-host transaction(s). This function |
||
| 242 | should be called once per Main Program loop after the device reaches |
||
| 243 | the configured state. |
||
| 244 | |||
| 245 | Typical Usage: |
||
| 246 | <code> |
||
| 247 | void main(void) |
||
| 248 | { |
||
| 249 | USBDeviceInit(); |
||
| 250 | while(1) |
||
| 251 | { |
||
| 252 | USBDeviceTasks(); |
||
| 253 | if((USBGetDeviceState() \< CONFIGURED_STATE) || |
||
| 254 | (USBIsDeviceSuspended() == TRUE)) |
||
| 255 | { |
||
| 256 | //Either the device is not configured or we are suspended |
||
| 257 | // so we don't want to do execute any application code |
||
| 258 | continue; //go back to the top of the while loop |
||
| 259 | } |
||
| 260 | else |
||
| 261 | { |
||
| 262 | //Run application code. |
||
| 263 | UserApplication(); |
||
| 264 | |||
| 265 | //Keep trying to send data to the PC as required |
||
| 266 | USBCCIDBulkInService(); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | } |
||
| 270 | </code> |
||
| 271 | Conditions: |
||
| 272 | None |
||
| 273 | Remarks: |
||
| 274 | None |
||
| 275 | ************************************************************************/ |
||
| 276 | |||
| 277 | void USBCCIDBulkInService(void) |
||
| 278 | { |
||
| 279 | WORD byte_to_send; |
||
| 280 | BYTE i; |
||
| 281 | |||
| 282 | USBMaskInterrupts(); |
||
| 283 | if(USBHandleBusy(usbCcidBulkInHandle)) |
||
| 284 | { |
||
| 285 | USBUnmaskInterrupts(); |
||
| 286 | return; |
||
| 287 | } |
||
| 288 | |||
| 289 | |||
| 290 | if(usbCcidBulkInTrfState == USB_CCID_BULK_IN_COMPLETING) |
||
| 291 | usbCcidBulkInTrfState = USB_CCID_BULK_IN_READY; |
||
| 292 | |||
| 293 | /* |
||
| 294 | * If USB_CCID_BULK_IN_READY state, nothing to do, just return. |
||
| 295 | */ |
||
| 296 | if(usbCcidBulkInTrfState == USB_CCID_BULK_IN_READY) |
||
| 297 | { |
||
| 298 | USBUnmaskInterrupts(); |
||
| 299 | return; |
||
| 300 | } |
||
| 301 | |||
| 302 | /* |
||
| 303 | * If USB_CCID_BULK_IN_BUSY_ZLP state, send zero length packet |
||
| 304 | */ |
||
| 305 | if(usbCcidBulkInTrfState == USB_CCID_BULK_IN_BUSY_ZLP) |
||
| 306 | { |
||
| 307 | usbCcidBulkInHandle = USBTxOnePacket(USB_EP_BULK_IN,NULL,0); |
||
| 308 | usbCcidBulkInTrfState = USB_CCID_BULK_IN_COMPLETING; |
||
| 309 | } |
||
| 310 | else if(usbCcidBulkInTrfState == USB_CCID_BULK_IN_BUSY) |
||
| 311 | { |
||
| 312 | /* |
||
| 313 | * First, have to figure out how many byte of data to send. |
||
| 314 | */ |
||
| 315 | if(usbCcidBulkInLen > sizeof(usbCcidBulkInEndpoint)) |
||
| 316 | byte_to_send = sizeof(usbCcidBulkInEndpoint); |
||
| 317 | else |
||
| 318 | byte_to_send = usbCcidBulkInLen; |
||
| 319 | |||
| 320 | /* |
||
| 321 | * Subtract the number of bytes just about to be sent from the total. |
||
| 322 | */ |
||
| 323 | usbCcidBulkInLen = usbCcidBulkInLen - byte_to_send; |
||
| 324 | |||
| 325 | pCCIDDst.bRam = (BYTE*)usbCcidBulkInEndpoint; // Set destination pointer |
||
| 326 | |||
| 327 | i = byte_to_send; |
||
| 328 | |||
| 329 | while(i) |
||
| 330 | { |
||
| 331 | *pCCIDDst.bRam = *pCCIDSrc.bRam; |
||
| 332 | pCCIDDst.bRam++; |
||
| 333 | pCCIDSrc.bRam++; |
||
| 334 | i--; |
||
| 335 | }//end while(byte_to_send._word) |
||
| 336 | |||
| 337 | |||
| 338 | /* |
||
| 339 | * Lastly, determine if a zero length packet state is necessary. |
||
| 340 | * See explanation in USB Specification 2.0: Section 5.8.3 |
||
| 341 | */ |
||
| 342 | if(usbCcidBulkInLen == 0) |
||
| 343 | { |
||
| 344 | if(byte_to_send == USB_EP_SIZE) |
||
| 345 | usbCcidBulkInTrfState = USB_CCID_BULK_IN_BUSY_ZLP; |
||
| 346 | else |
||
| 347 | usbCcidBulkInTrfState = USB_CCID_BULK_IN_COMPLETING; |
||
| 348 | }//end if(usbCcidBulkInLen...) |
||
| 349 | usbCcidBulkInHandle = USBTxOnePacket(USB_EP_BULK_IN,(BYTE*)usbCcidBulkInEndpoint,byte_to_send); |
||
| 350 | |||
| 351 | }//end if(cdc_tx_sate == USB_CCID_BULK_IN_BUSY) |
||
| 352 | USBUnmaskInterrupts(); |
||
| 353 | }//end USBCCIDBulkInService |
||
| 354 | |||
| 355 | |||
| 356 | /****************************************************************************** |
||
| 357 | Function: |
||
| 358 | void USBCCIDSendDataToHost(char *data, BYTE length) |
||
| 359 | |||
| 360 | Summary: |
||
| 361 | USBCCIDSendDataToHost writes an array of data to the USB. Use this version, is |
||
| 362 | capable of transfering 0x00 (what is typically a NULL character in any of |
||
| 363 | the string transfer functions). |
||
| 364 | |||
| 365 | Description: |
||
| 366 | USBCCIDSendDataToHost writes an array of data to the USB. Use this version, is |
||
| 367 | capable of transfering 0x00 (what is typically a NULL character in any of |
||
| 368 | the string transfer functions). |
||
| 369 | |||
| 370 | |||
| 371 | The transfer mechanism for device-to-host(put) is more flexible than |
||
| 372 | host-to-device(get). It can handle a string of data larger than the |
||
| 373 | maximum size of bulk IN endpoint. A state machine is used to transfer a |
||
| 374 | \long string of data over multiple USB transactions. USBCCIDBulkInService() |
||
| 375 | must be called periodically to keep sending blocks of data to the host. |
||
| 376 | |||
| 377 | Conditions: |
||
| 378 | |||
| 379 | |||
| 380 | Input: |
||
| 381 | char *data - pointer to a RAM array of data to be transfered to the host |
||
| 382 | BYTE length - the number of bytes to be transfered |
||
| 383 | |||
| 384 | *****************************************************************************/ |
||
| 385 | void USBCCIDSendDataToHost(BYTE *pData, WORD len) |
||
| 386 | { |
||
| 387 | |||
| 388 | mUSBCCIDBulkInRam((BYTE*)pData, len); |
||
| 389 | |||
| 390 | } |
||
| 391 | |||
| 392 | |||
| 393 | #endif //def USB_USE_CCID |
||
| 394 | |||
| 395 | /** EOF usb_function_ccid.c *************************************************************/ |
Powered by WebSVN v2.8.3