firmware/usbdsc.c
author root@rika
Thu, 23 Apr 2009 19:10:12 +0200
changeset 30 7fd00015f62f
parent 2 2f55e5dd591d
permissions -rw-r--r--
several changes..
     1 /*********************************************************************
     2  *
     3  *                Microchip USB C18 Firmware Version 1.0
     4  *
     5  *********************************************************************
     6  * FileName:        usbdsc.c
     7  * Dependencies:    See INCLUDES section below
     8  * Processor:       PIC18
     9  * Compiler:        C18 2.30.01+
    10  * Company:         Microchip Technology, Inc.
    11  *
    12  * Software License Agreement
    13  *
    14  * The software supplied herewith by Microchip Technology Incorporated
    15  * (the “Company”) for its PICmicro® Microcontroller is intended and
    16  * supplied to you, the Company’s customer, for use solely and
    17  * exclusively on Microchip PICmicro Microcontroller products. The
    18  * software is owned by the Company and/or its supplier, and is
    19  * protected under applicable copyright laws. All rights are reserved.
    20  * Any use in violation of the foregoing restrictions may subject the
    21  * user to criminal sanctions under applicable laws, as well as to
    22  * civil liability for the breach of the terms and conditions of this
    23  * license.
    24  *
    25  * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
    26  * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
    27  * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    28  * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
    29  * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
    30  * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
    31  *
    32  ********************************************************************/
    33 
    34 /*********************************************************************
    35  * -usbdsc.c-
    36  * This file contains the USB descriptor information. It is used
    37  * in conjunction with the usbdsc.h file. When a descriptor is added
    38  * or removed from the main configuration descriptor, i.e. CFG01,
    39  * the user must also change the descriptor structure defined in
    40  * the usbdsc.h file. The structure is used to calculate the 
    41  * descriptor size, i.e. sizeof(CFG01).
    42  * 
    43  * A typical configuration descriptor consists of:
    44  * At least one configuration descriptor (USB_CFG_DSC)
    45  * One or more interface descriptors (USB_INTF_DSC)
    46  * One or more endpoint descriptors (USB_EP_DSC)
    47  *
    48  * Naming Convention:
    49  * To resolve ambiguity, the naming convention are as followed:
    50  * - USB_CFG_DSC type should be named cdxx, where xx is the
    51  *   configuration number. This number should match the actual
    52  *   index value of this configuration.
    53  * - USB_INTF_DSC type should be named i<yy>a<zz>, where yy is the
    54  *   interface number and zz is the alternate interface number.
    55  * - USB_EP_DSC type should be named ep<##><d>_i<yy>a<zz>, where
    56  *   ## is the endpoint number and d is the direction of transfer.
    57  *   The interface name should also be listed as a suffix to identify
    58  *   which interface does the endpoint belong to.
    59  *
    60  * Example:
    61  * If a device has one configuration, two interfaces; interface 0
    62  * has two endpoints (in and out), and interface 1 has one endpoint(in).
    63  * Then the CFG01 structure in the usbdsc.h should be:
    64  *
    65  * #define CFG01 rom struct                            \
    66  * {   USB_CFG_DSC             cd01;                   \
    67  *     USB_INTF_DSC            i00a00;                 \
    68  *     USB_EP_DSC              ep01o_i00a00;           \
    69  *     USB_EP_DSC              ep01i_i00a00;           \
    70  *     USB_INTF_DSC            i01a00;                 \
    71  *     USB_EP_DSC              ep02i_i01a00;           \
    72  * } cfg01
    73  * 
    74  * Note the hierarchy of the descriptors above, it follows the USB
    75  * specification requirement. All endpoints belonging to an interface
    76  * should be listed immediately after that interface.
    77  *
    78  * -------------------------------------------------------------------
    79  * Filling in the descriptor values in the usbdsc.c file:
    80  * -------------------------------------------------------------------
    81  * Most items should be self-explanatory, however, a few will be
    82  * explained for clarification.
    83  *
    84  * [Configuration Descriptor(USB_CFG_DSC)]
    85  * The configuration attribute must always have the _DEFAULT
    86  * definition at the minimum. Additional options can be ORed
    87  * to the _DEFAULT attribute. Available options are _SELF and _RWU.
    88  * These definitions are defined in the usbdefs_std_dsc.h file. The
    89  * _SELF tells the USB host that this device is self-powered. The
    90  * _RWU tells the USB host that this device supports Remote Wakeup.
    91  *
    92  * [Endpoint Descriptor(USB_EP_DSC)]
    93  * Assume the following example:
    94  * sizeof(USB_EP_DSC),DSC_EP,_EP01_OUT,_BULK,64,0x00
    95  *
    96  * The first two parameters are self-explanatory. They specify the
    97  * length of this endpoint descriptor (7) and the descriptor type.
    98  * The next parameter identifies the endpoint, the definitions are
    99  * defined in usbdefs_std_dsc.h and has the following naming
   100  * convention:
   101  * _EP<##>_<dir>
   102  * where ## is the endpoint number and dir is the direction of
   103  * transfer. The dir has the value of either 'OUT' or 'IN'.
   104  * The next parameter identifies the type of the endpoint. Available
   105  * options are _BULK, _INT, _ISO, and _CTRL. The _CTRL is not
   106  * typically used because the default control transfer endpoint is
   107  * not defined in the USB descriptors. When _ISO option is used,
   108  * addition options can be ORed to _ISO. Example:
   109  * _ISO|_AD|_FE
   110  * This describes the endpoint as an isochronous pipe with adaptive
   111  * and feedback attributes. See usbdefs_std_dsc.h and the USB
   112  * specification for details. The next parameter defines the size of
   113  * the endpoint. The last parameter in the polling interval.
   114  *
   115  * -------------------------------------------------------------------
   116  * Adding a USB String
   117  * -------------------------------------------------------------------
   118  * A string descriptor array should have the following format:
   119  *
   120  * rom struct{byte bLength;byte bDscType;word string[size];}sdxxx={
   121  * sizeof(sdxxx),DSC_STR,<text>};
   122  *
   123  * The above structure provides a means for the C compiler to
   124  * calculate the length of string descriptor sdxxx, where xxx is the
   125  * index number. The first two bytes of the descriptor are descriptor
   126  * length and type. The rest <text> are string texts which must be
   127  * in the unicode format. The unicode format is achieved by declaring
   128  * each character as a word type. The whole text string is declared
   129  * as a word array with the number of characters equals to <size>.
   130  * <size> has to be manually counted and entered into the array
   131  * declaration. Let's study this through an example:
   132  * if the string is "USB" , then the string descriptor should be:
   133  * (Using index 02)
   134  * rom struct{byte bLength;byte bDscType;word string[3];}sd002={
   135  * sizeof(sd002),DSC_STR,'U','S','B'};
   136  *
   137  * A USB project may have multiple strings and the firmware supports
   138  * the management of multiple strings through a look-up table.
   139  * The look-up table is defined as:
   140  * rom const unsigned char *rom USB_SD_Ptr[]={&sd000,&sd001,&sd002};
   141  *
   142  * The above declaration has 3 strings, sd000, sd001, and sd002.
   143  * Strings can be removed or added. sd000 is a specialized string
   144  * descriptor. It defines the language code, usually this is
   145  * US English (0x0409). The index of the string must match the index
   146  * position of the USB_SD_Ptr array, &sd000 must be in position
   147  * USB_SD_Ptr[0], &sd001 must be in position USB_SD_Ptr[1] and so on.
   148  * The look-up table USB_SD_Ptr is used by the get string handler
   149  * function in usb9.c.
   150  *
   151  * -------------------------------------------------------------------
   152  *
   153  * The look-up table scheme also applies to the configuration
   154  * descriptor. A USB device may have multiple configuration
   155  * descriptors, i.e. CFG01, CFG02, etc. To add a configuration
   156  * descriptor, user must implement a structure similar to CFG01.
   157  * The next step is to add the configuration descriptor name, i.e.
   158  * cfg01, cfg02,.., to the look-up table USB_CD_Ptr. USB_CD_Ptr[0]
   159  * is a dummy place holder since configuration 0 is the un-configured
   160  * state according to the definition in the USB specification.
   161  *
   162  ********************************************************************/
   163  
   164 /*********************************************************************
   165  * Descriptor specific type definitions are defined in:
   166  * system\usb\usbdefs\usbdefs_std_dsc.h
   167  *
   168  * Configuration information is defined in:
   169  * autofiles\usbcfg.h
   170  ********************************************************************/
   171  
   172 /** I N C L U D E S *************************************************/
   173 #include "typedefs.h"
   174 #include "usb.h"
   175 
   176 /** C O N S T A N T S ************************************************/
   177 #pragma romdata
   178 
   179 /* Device Descriptor */
   180 rom USB_DEV_DSC device_dsc=
   181 {    
   182     sizeof(USB_DEV_DSC),    // Size of this descriptor in bytes
   183     DSC_DEV,                // DEVICE descriptor type
   184     0x0200,                 // USB Spec Release Number in BCD format
   185     CDC_DEVICE,             // Class Code
   186     0x00,                   // Subclass code
   187     0x00,                   // Protocol code
   188     EP0_BUFF_SIZE,          // Max packet size for EP0, see usbcfg.h
   189     0x04D8,                 // Vendor ID
   190     0x000A,                 // Product ID: CDC RS-232 Emulation Demo
   191     0x0000,                 // Device release number in BCD format
   192     0x01,                   // Manufacturer string index
   193     0x02,                   // Product string index
   194     0x00,                   // Device serial number string index
   195     0x01                    // Number of possible configurations
   196 };
   197 
   198 /* Configuration 1 Descriptor */
   199 CFG01=
   200 {
   201     /* Configuration Descriptor */
   202     sizeof(USB_CFG_DSC),    // Size of this descriptor in bytes
   203     DSC_CFG,                // CONFIGURATION descriptor type
   204     sizeof(cfg01),          // Total length of data for this cfg
   205     2,                      // Number of interfaces in this cfg
   206     1,                      // Index value of this configuration
   207     0,                      // Configuration string index
   208     _DEFAULT,               // Attributes, see usbdefs_std_dsc.h
   209     50,                     // Max power consumption (2X mA)
   210     
   211     /* Interface Descriptor */
   212     sizeof(USB_INTF_DSC),   // Size of this descriptor in bytes
   213     DSC_INTF,               // INTERFACE descriptor type
   214     0,                      // Interface Number
   215     0,                      // Alternate Setting Number
   216     1,                      // Number of endpoints in this intf
   217     COMM_INTF,              // Class code
   218     ABSTRACT_CONTROL_MODEL, // Subclass code
   219     V25TER,                 // Protocol code
   220     0,                      // Interface string index
   221 
   222     /* CDC Class-Specific Descriptors */
   223     sizeof(USB_CDC_HEADER_FN_DSC),CS_INTERFACE,DSC_FN_HEADER,0x0110,
   224     sizeof(USB_CDC_ACM_FN_DSC),CS_INTERFACE,DSC_FN_ACM,0x02,
   225     sizeof(USB_CDC_UNION_FN_DSC),CS_INTERFACE,DSC_FN_UNION,CDC_COMM_INTF_ID,CDC_DATA_INTF_ID,
   226     sizeof(USB_CDC_CALL_MGT_FN_DSC),CS_INTERFACE,DSC_FN_CALL_MGT,0x00,CDC_DATA_INTF_ID,
   227 
   228     /* Endpoint Descriptor */
   229     sizeof(USB_EP_DSC),DSC_EP,_EP02_IN,_INT,CDC_INT_EP_SIZE,0x02,
   230     
   231     /* Interface Descriptor */
   232     sizeof(USB_INTF_DSC),   // Size of this descriptor in bytes
   233     DSC_INTF,               // INTERFACE descriptor type
   234     1,                      // Interface Number
   235     0,                      // Alternate Setting Number
   236     2,                      // Number of endpoints in this intf
   237     DATA_INTF,              // Class code
   238     0,                      // Subclass code
   239     NO_PROTOCOL,            // Protocol code
   240     0,                      // Interface string index
   241     
   242     /* Endpoint Descriptors */
   243     sizeof(USB_EP_DSC),DSC_EP,_EP03_OUT,_BULK,CDC_BULK_OUT_EP_SIZE,0x00,
   244     sizeof(USB_EP_DSC),DSC_EP,_EP03_IN,_BULK,CDC_BULK_IN_EP_SIZE,0x00
   245 };
   246 
   247 rom struct{byte bLength;byte bDscType;word string[1];}sd000={
   248 sizeof(sd000),DSC_STR,0x0409};
   249 
   250 rom struct{byte bLength;byte bDscType;word string[25];}sd001={
   251 sizeof(sd001),DSC_STR,
   252 'C','a','r','s','t','e','n',' ','P','r','e','s','s','e','r'};
   253 
   254 rom struct{byte bLength;byte bDscType;word string[25];}sd002={
   255 sizeof(sd002),DSC_STR,
   256 'V','i','d','e','o',' ','v','2',' ','F','r','o','n','t','p','l','a','t','t','t','e'};
   257 
   258 rom const unsigned char *rom USB_CD_Ptr[]={&cfg01,&cfg01};
   259 rom const unsigned char *rom USB_SD_Ptr[]={&sd000,&sd001,&sd002};
   260 
   261 rom pFunc ClassReqHandler[1]=
   262 {
   263     &USBCheckCDCRequest
   264 };
   265 
   266 #pragma code
   267 
   268 /** EOF usbdsc.c ****************************************************/