1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/firmware/usbdsc.c Tue Jan 29 22:31:52 2008 +0100
1.3 @@ -0,0 +1,268 @@
1.4 +/*********************************************************************
1.5 + *
1.6 + * Microchip USB C18 Firmware Version 1.0
1.7 + *
1.8 + *********************************************************************
1.9 + * FileName: usbdsc.c
1.10 + * Dependencies: See INCLUDES section below
1.11 + * Processor: PIC18
1.12 + * Compiler: C18 2.30.01+
1.13 + * Company: Microchip Technology, Inc.
1.14 + *
1.15 + * Software License Agreement
1.16 + *
1.17 + * The software supplied herewith by Microchip Technology Incorporated
1.18 + * (the “Company”) for its PICmicro® Microcontroller is intended and
1.19 + * supplied to you, the Company’s customer, for use solely and
1.20 + * exclusively on Microchip PICmicro Microcontroller products. The
1.21 + * software is owned by the Company and/or its supplier, and is
1.22 + * protected under applicable copyright laws. All rights are reserved.
1.23 + * Any use in violation of the foregoing restrictions may subject the
1.24 + * user to criminal sanctions under applicable laws, as well as to
1.25 + * civil liability for the breach of the terms and conditions of this
1.26 + * license.
1.27 + *
1.28 + * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
1.29 + * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
1.30 + * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
1.31 + * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
1.32 + * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
1.33 + * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
1.34 + *
1.35 + ********************************************************************/
1.36 +
1.37 +/*********************************************************************
1.38 + * -usbdsc.c-
1.39 + * This file contains the USB descriptor information. It is used
1.40 + * in conjunction with the usbdsc.h file. When a descriptor is added
1.41 + * or removed from the main configuration descriptor, i.e. CFG01,
1.42 + * the user must also change the descriptor structure defined in
1.43 + * the usbdsc.h file. The structure is used to calculate the
1.44 + * descriptor size, i.e. sizeof(CFG01).
1.45 + *
1.46 + * A typical configuration descriptor consists of:
1.47 + * At least one configuration descriptor (USB_CFG_DSC)
1.48 + * One or more interface descriptors (USB_INTF_DSC)
1.49 + * One or more endpoint descriptors (USB_EP_DSC)
1.50 + *
1.51 + * Naming Convention:
1.52 + * To resolve ambiguity, the naming convention are as followed:
1.53 + * - USB_CFG_DSC type should be named cdxx, where xx is the
1.54 + * configuration number. This number should match the actual
1.55 + * index value of this configuration.
1.56 + * - USB_INTF_DSC type should be named i<yy>a<zz>, where yy is the
1.57 + * interface number and zz is the alternate interface number.
1.58 + * - USB_EP_DSC type should be named ep<##><d>_i<yy>a<zz>, where
1.59 + * ## is the endpoint number and d is the direction of transfer.
1.60 + * The interface name should also be listed as a suffix to identify
1.61 + * which interface does the endpoint belong to.
1.62 + *
1.63 + * Example:
1.64 + * If a device has one configuration, two interfaces; interface 0
1.65 + * has two endpoints (in and out), and interface 1 has one endpoint(in).
1.66 + * Then the CFG01 structure in the usbdsc.h should be:
1.67 + *
1.68 + * #define CFG01 rom struct \
1.69 + * { USB_CFG_DSC cd01; \
1.70 + * USB_INTF_DSC i00a00; \
1.71 + * USB_EP_DSC ep01o_i00a00; \
1.72 + * USB_EP_DSC ep01i_i00a00; \
1.73 + * USB_INTF_DSC i01a00; \
1.74 + * USB_EP_DSC ep02i_i01a00; \
1.75 + * } cfg01
1.76 + *
1.77 + * Note the hierarchy of the descriptors above, it follows the USB
1.78 + * specification requirement. All endpoints belonging to an interface
1.79 + * should be listed immediately after that interface.
1.80 + *
1.81 + * -------------------------------------------------------------------
1.82 + * Filling in the descriptor values in the usbdsc.c file:
1.83 + * -------------------------------------------------------------------
1.84 + * Most items should be self-explanatory, however, a few will be
1.85 + * explained for clarification.
1.86 + *
1.87 + * [Configuration Descriptor(USB_CFG_DSC)]
1.88 + * The configuration attribute must always have the _DEFAULT
1.89 + * definition at the minimum. Additional options can be ORed
1.90 + * to the _DEFAULT attribute. Available options are _SELF and _RWU.
1.91 + * These definitions are defined in the usbdefs_std_dsc.h file. The
1.92 + * _SELF tells the USB host that this device is self-powered. The
1.93 + * _RWU tells the USB host that this device supports Remote Wakeup.
1.94 + *
1.95 + * [Endpoint Descriptor(USB_EP_DSC)]
1.96 + * Assume the following example:
1.97 + * sizeof(USB_EP_DSC),DSC_EP,_EP01_OUT,_BULK,64,0x00
1.98 + *
1.99 + * The first two parameters are self-explanatory. They specify the
1.100 + * length of this endpoint descriptor (7) and the descriptor type.
1.101 + * The next parameter identifies the endpoint, the definitions are
1.102 + * defined in usbdefs_std_dsc.h and has the following naming
1.103 + * convention:
1.104 + * _EP<##>_<dir>
1.105 + * where ## is the endpoint number and dir is the direction of
1.106 + * transfer. The dir has the value of either 'OUT' or 'IN'.
1.107 + * The next parameter identifies the type of the endpoint. Available
1.108 + * options are _BULK, _INT, _ISO, and _CTRL. The _CTRL is not
1.109 + * typically used because the default control transfer endpoint is
1.110 + * not defined in the USB descriptors. When _ISO option is used,
1.111 + * addition options can be ORed to _ISO. Example:
1.112 + * _ISO|_AD|_FE
1.113 + * This describes the endpoint as an isochronous pipe with adaptive
1.114 + * and feedback attributes. See usbdefs_std_dsc.h and the USB
1.115 + * specification for details. The next parameter defines the size of
1.116 + * the endpoint. The last parameter in the polling interval.
1.117 + *
1.118 + * -------------------------------------------------------------------
1.119 + * Adding a USB String
1.120 + * -------------------------------------------------------------------
1.121 + * A string descriptor array should have the following format:
1.122 + *
1.123 + * rom struct{byte bLength;byte bDscType;word string[size];}sdxxx={
1.124 + * sizeof(sdxxx),DSC_STR,<text>};
1.125 + *
1.126 + * The above structure provides a means for the C compiler to
1.127 + * calculate the length of string descriptor sdxxx, where xxx is the
1.128 + * index number. The first two bytes of the descriptor are descriptor
1.129 + * length and type. The rest <text> are string texts which must be
1.130 + * in the unicode format. The unicode format is achieved by declaring
1.131 + * each character as a word type. The whole text string is declared
1.132 + * as a word array with the number of characters equals to <size>.
1.133 + * <size> has to be manually counted and entered into the array
1.134 + * declaration. Let's study this through an example:
1.135 + * if the string is "USB" , then the string descriptor should be:
1.136 + * (Using index 02)
1.137 + * rom struct{byte bLength;byte bDscType;word string[3];}sd002={
1.138 + * sizeof(sd002),DSC_STR,'U','S','B'};
1.139 + *
1.140 + * A USB project may have multiple strings and the firmware supports
1.141 + * the management of multiple strings through a look-up table.
1.142 + * The look-up table is defined as:
1.143 + * rom const unsigned char *rom USB_SD_Ptr[]={&sd000,&sd001,&sd002};
1.144 + *
1.145 + * The above declaration has 3 strings, sd000, sd001, and sd002.
1.146 + * Strings can be removed or added. sd000 is a specialized string
1.147 + * descriptor. It defines the language code, usually this is
1.148 + * US English (0x0409). The index of the string must match the index
1.149 + * position of the USB_SD_Ptr array, &sd000 must be in position
1.150 + * USB_SD_Ptr[0], &sd001 must be in position USB_SD_Ptr[1] and so on.
1.151 + * The look-up table USB_SD_Ptr is used by the get string handler
1.152 + * function in usb9.c.
1.153 + *
1.154 + * -------------------------------------------------------------------
1.155 + *
1.156 + * The look-up table scheme also applies to the configuration
1.157 + * descriptor. A USB device may have multiple configuration
1.158 + * descriptors, i.e. CFG01, CFG02, etc. To add a configuration
1.159 + * descriptor, user must implement a structure similar to CFG01.
1.160 + * The next step is to add the configuration descriptor name, i.e.
1.161 + * cfg01, cfg02,.., to the look-up table USB_CD_Ptr. USB_CD_Ptr[0]
1.162 + * is a dummy place holder since configuration 0 is the un-configured
1.163 + * state according to the definition in the USB specification.
1.164 + *
1.165 + ********************************************************************/
1.166 +
1.167 +/*********************************************************************
1.168 + * Descriptor specific type definitions are defined in:
1.169 + * system\usb\usbdefs\usbdefs_std_dsc.h
1.170 + *
1.171 + * Configuration information is defined in:
1.172 + * autofiles\usbcfg.h
1.173 + ********************************************************************/
1.174 +
1.175 +/** I N C L U D E S *************************************************/
1.176 +#include "typedefs.h"
1.177 +#include "usb.h"
1.178 +
1.179 +/** C O N S T A N T S ************************************************/
1.180 +#pragma romdata
1.181 +
1.182 +/* Device Descriptor */
1.183 +rom USB_DEV_DSC device_dsc=
1.184 +{
1.185 + sizeof(USB_DEV_DSC), // Size of this descriptor in bytes
1.186 + DSC_DEV, // DEVICE descriptor type
1.187 + 0x0200, // USB Spec Release Number in BCD format
1.188 + CDC_DEVICE, // Class Code
1.189 + 0x00, // Subclass code
1.190 + 0x00, // Protocol code
1.191 + EP0_BUFF_SIZE, // Max packet size for EP0, see usbcfg.h
1.192 + 0x04D8, // Vendor ID
1.193 + 0x000A, // Product ID: CDC RS-232 Emulation Demo
1.194 + 0x0000, // Device release number in BCD format
1.195 + 0x01, // Manufacturer string index
1.196 + 0x02, // Product string index
1.197 + 0x00, // Device serial number string index
1.198 + 0x01 // Number of possible configurations
1.199 +};
1.200 +
1.201 +/* Configuration 1 Descriptor */
1.202 +CFG01=
1.203 +{
1.204 + /* Configuration Descriptor */
1.205 + sizeof(USB_CFG_DSC), // Size of this descriptor in bytes
1.206 + DSC_CFG, // CONFIGURATION descriptor type
1.207 + sizeof(cfg01), // Total length of data for this cfg
1.208 + 2, // Number of interfaces in this cfg
1.209 + 1, // Index value of this configuration
1.210 + 0, // Configuration string index
1.211 + _DEFAULT, // Attributes, see usbdefs_std_dsc.h
1.212 + 50, // Max power consumption (2X mA)
1.213 +
1.214 + /* Interface Descriptor */
1.215 + sizeof(USB_INTF_DSC), // Size of this descriptor in bytes
1.216 + DSC_INTF, // INTERFACE descriptor type
1.217 + 0, // Interface Number
1.218 + 0, // Alternate Setting Number
1.219 + 1, // Number of endpoints in this intf
1.220 + COMM_INTF, // Class code
1.221 + ABSTRACT_CONTROL_MODEL, // Subclass code
1.222 + V25TER, // Protocol code
1.223 + 0, // Interface string index
1.224 +
1.225 + /* CDC Class-Specific Descriptors */
1.226 + sizeof(USB_CDC_HEADER_FN_DSC),CS_INTERFACE,DSC_FN_HEADER,0x0110,
1.227 + sizeof(USB_CDC_ACM_FN_DSC),CS_INTERFACE,DSC_FN_ACM,0x02,
1.228 + sizeof(USB_CDC_UNION_FN_DSC),CS_INTERFACE,DSC_FN_UNION,CDC_COMM_INTF_ID,CDC_DATA_INTF_ID,
1.229 + sizeof(USB_CDC_CALL_MGT_FN_DSC),CS_INTERFACE,DSC_FN_CALL_MGT,0x00,CDC_DATA_INTF_ID,
1.230 +
1.231 + /* Endpoint Descriptor */
1.232 + sizeof(USB_EP_DSC),DSC_EP,_EP02_IN,_INT,CDC_INT_EP_SIZE,0x02,
1.233 +
1.234 + /* Interface Descriptor */
1.235 + sizeof(USB_INTF_DSC), // Size of this descriptor in bytes
1.236 + DSC_INTF, // INTERFACE descriptor type
1.237 + 1, // Interface Number
1.238 + 0, // Alternate Setting Number
1.239 + 2, // Number of endpoints in this intf
1.240 + DATA_INTF, // Class code
1.241 + 0, // Subclass code
1.242 + NO_PROTOCOL, // Protocol code
1.243 + 0, // Interface string index
1.244 +
1.245 + /* Endpoint Descriptors */
1.246 + sizeof(USB_EP_DSC),DSC_EP,_EP03_OUT,_BULK,CDC_BULK_OUT_EP_SIZE,0x00,
1.247 + sizeof(USB_EP_DSC),DSC_EP,_EP03_IN,_BULK,CDC_BULK_IN_EP_SIZE,0x00
1.248 +};
1.249 +
1.250 +rom struct{byte bLength;byte bDscType;word string[1];}sd000={
1.251 +sizeof(sd000),DSC_STR,0x0409};
1.252 +
1.253 +rom struct{byte bLength;byte bDscType;word string[25];}sd001={
1.254 +sizeof(sd001),DSC_STR,
1.255 +'C','a','r','s','t','e','n',' ','P','r','e','s','s','e','r'};
1.256 +
1.257 +rom struct{byte bLength;byte bDscType;word string[25];}sd002={
1.258 +sizeof(sd002),DSC_STR,
1.259 +'V','i','d','e','o',' ','v','2',' ','F','r','o','n','t','p','l','a','t','t','t','e'};
1.260 +
1.261 +rom const unsigned char *rom USB_CD_Ptr[]={&cfg01,&cfg01};
1.262 +rom const unsigned char *rom USB_SD_Ptr[]={&sd000,&sd001,&sd002};
1.263 +
1.264 +rom pFunc ClassReqHandler[1]=
1.265 +{
1.266 + &USBCheckCDCRequest
1.267 +};
1.268 +
1.269 +#pragma code
1.270 +
1.271 +/** EOF usbdsc.c ****************************************************/