00001 /********************************************************************* 00002 * 00003 * Microchip USB C18 Firmware Version 1.0 00004 * 00005 ********************************************************************* 00006 * FileName: usbmmap.c 00007 * Dependencies: See INCLUDES section below 00008 * Processor: PIC18 00009 * Compiler: C18 2.30.01+ 00010 * Company: Microchip Technology, Inc. 00011 * 00012 * Software License Agreement 00013 * 00014 * The software supplied herewith by Microchip Technology Incorporated 00015 * (the “Company”) for its PICmicro® Microcontroller is intended and 00016 * supplied to you, the Company’s customer, for use solely and 00017 * exclusively on Microchip PICmicro Microcontroller products. The 00018 * software is owned by the Company and/or its supplier, and is 00019 * protected under applicable copyright laws. All rights are reserved. 00020 * Any use in violation of the foregoing restrictions may subject the 00021 * user to criminal sanctions under applicable laws, as well as to 00022 * civil liability for the breach of the terms and conditions of this 00023 * license. 00024 * 00025 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES, 00026 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED 00027 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00028 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, 00029 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR 00030 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. 00031 * 00032 * Author Date Comment 00033 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00034 * Rawin Rojvanit 11/19/04 Original. 00035 ********************************************************************/ 00036 00037 /****************************************************************************** 00038 * -usbmmap.c- 00039 * USB Memory Map 00040 * This file is the USB memory manager; it serves as a compile-time memory 00041 * allocator for the USB endpoints. It uses the compile time options passed 00042 * from usbcfg.h to instantiate endpoints and endpoint buffer. 00043 * 00044 * Each endpoint requires to have a set of Buffer Descriptor registers(BDT). 00045 * A BDT is 4-byte long and has a specific RAM location for each endpoint. 00046 * The BDT for endpoint 0 out is located at address 0x400 to 0x403. 00047 * The BDT for endpoint 0 in is located at address 0x404 to 0x407. 00048 * The BDT for endpoint 1 out is located at address 0x408 to 0x40B. 00049 * and so on... The above allocation assumes the Ping-Pong Buffer Mode 0 is 00050 * used. These locations are already hard-wired in the silicon. The point 00051 * of doing instantiation, i.e. volatile far BDT ep0Bo;, is to provide the 00052 * C compiler a way to address each variable directly. This is very important 00053 * because when a register can be accessed directly, it saves execution time 00054 * and reduces program size. 00055 * 00056 * Endpoints are defined using the endpoint number and the direction 00057 * of transfer. For simplicity, usbmmap.c only uses the endpoint number 00058 * in the BDT register allocation scheme. This means if the usbcfg.h states 00059 * that the MAX_EP_NUMBER is number 1, then four BDTs will be 00060 * instantiated: one each for endpoint0 in and endpoint0 out, which must 00061 * always be instantiated for control transfer by default, and one each sets 00062 * for endpoint1 in and endpoint1 out. The naming convention for instantiating 00063 * BDT is 00064 * 00065 * ep<#>B<d> 00066 * 00067 * where # is the endpoint number, and d is the direction of 00068 * transfer, which could be either <i> or <o>. 00069 * 00070 * The USB memory manager uses MAX_EP_NUMBER, as defined in usbcfg.h, to define 00071 * the endpoints to be instantiated. This represents the highest endpoint 00072 * number to be allocated, not how many endpoints are used. Since the BDTs for 00073 * endpoints have hardware-assigned addresses in Bank 4, setting this value too 00074 * high may lead to inefficient use of data RAM. For example, if an application 00075 * uses only endpoints EP0 and EP4, then the MAX_EP_NUMBER is 4, and not 2. 00076 * The in-between endpoint BDTs in this example (EP1, EP2, and EP3) go unused, 00077 * and the 24 bytes of memory associated with them are wasted. It does not make 00078 * much sense to skip endpoints, but the final decision lies with the user. 00079 * 00080 * The next step is to assign the instantiated BDTs to different 00081 * USB functions. The firmware framework fundamentally assumes that every USB 00082 * function should know which endpoint it is using, i.e., the default control 00083 * transfer should know that it is using endpoint 0 in and endpoint 0 out. 00084 * A HID class can choose which endpoint it wants to use, but once chosen, it 00085 * should always know the number of the endpoint. 00086 * 00087 * The assignment of endpoints to USB functions is managed centrally 00088 * in usbcfg.h. This helps prevent the mistake of having more 00089 * than one USB function using the same endpoint. The "Endpoint Allocation" 00090 * section in usbcfg.h provides examples for how to map USB endpoints to USB 00091 * functions. 00092 * Quite a few things can be mapped in that section. There is no 00093 * one correct way to do the mapping and the user has the choice to 00094 * choose a method that is most suitable to the application. 00095 * 00096 * Typically, however, a user will want to map the following for a given 00097 * USB interface function: 00098 * 1. The USB interface ID 00099 * 2. The endpoint control registers (UEPn) 00100 * 3. The BDT registers (ep<#>B<d>) 00101 * 4. The endpoint size 00102 * 00103 * Example: Assume a USB device class "foo", which uses one out endpoint 00104 * of size 64-byte and one in endpoint of size 64-byte, then: 00105 * 00106 * #define FOO_INTF_ID 0x00 00107 * #define FOO_UEP UEP1 00108 * #define FOO_BD_OUT ep1Bo 00109 * #define FOO_BD_IN ep1Bi 00110 * #define FOO_EP_SIZE 64 00111 * 00112 * The mapping above has chosen class "foo" to use endpoint 1. 00113 * The names are arbitrary and can be anything other than FOO_??????. 00114 * For abstraction, the code for class "foo" should use the abstract 00115 * definitions of FOO_BD_OUT,FOO_BD_IN, and not ep1Bo or ep1Bi. 00116 * 00117 * Note that the endpoint size defined in the usbcfg.h file is again 00118 * used in the usbmmap.c file. This shows that the relationship between 00119 * the two files are tightly related. 00120 * 00121 * The endpoint buffer for each USB function must be located in the 00122 * dual-port RAM area and has to come after all the BDTs have been 00123 * instantiated. An example declaration is: 00124 * volatile far unsigned char[FOO_EP_SIZE] data; 00125 * 00126 * The 'volatile' keyword tells the compiler not to perform any code 00127 * optimization on this variable because its content could be modified 00128 * by the hardware. The 'far' keyword tells the compiler that this variable 00129 * is not located in the Access RAM area (0x000 - 0x05F). 00130 * 00131 * For the variable to be globally accessible by other files, it should be 00132 * declared in the header file usbmmap.h as an extern definition, such as 00133 * extern volatile far unsigned char[FOO_EP_SIZE] data; 00134 * 00135 * Conclusion: 00136 * In a short summary, the dependencies between usbcfg and usbmmap can 00137 * be shown as: 00138 * 00139 * usbcfg[MAX_EP_NUMBER] -> usbmmap 00140 * usbmmap[ep<#>B<d>] -> usbcfg 00141 * usbcfg[EP size] -> usbmmap 00142 * usbcfg[abstract ep definitions] -> usb9/hid/cdc/etc class code 00143 * usbmmap[endpoint buffer variable] -> usb9/hid/cdc/etc class code 00144 * 00145 * Data mapping provides a means for direct addressing of BDT and endpoint 00146 * buffer. This means less usage of pointers, which equates to a faster and 00147 * smaller program code. 00148 * 00149 *****************************************************************************/ 00150 00151 /** I N C L U D E S **********************************************************/ 00152 #include "system\typedefs.h" 00153 #include "system\usb\usb.h" 00154 00155 /** U S B G L O B A L V A R I A B L E S ************************************/ 00156 #pragma udata 00157 byte usb_device_state; // Device States: DETACHED, ATTACHED, ... 00158 USB_DEVICE_STATUS usb_stat; // Global USB flags 00159 byte usb_active_cfg; // Value of current configuration 00160 byte usb_alt_intf[MAX_NUM_INT]; // Array to keep track of the current alternate 00161 // setting for each interface ID 00162 00163 /** U S B F I X E D L O C A T I O N V A R I A B L E S *********************/ 00164 #pragma udata usbram4=0x400 //See Linker Script,usb4:0x400-0x4FF(256-byte) 00165 00166 /****************************************************************************** 00167 * Section A: Buffer Descriptor Table 00168 * - 0x400 - 0x4FF(max) 00169 * - MAX_EP_NUMBER is defined in autofiles\usbcfg.h 00170 * - BDT data type is defined in system\usb\usbmmap.h 00171 *****************************************************************************/ 00172 00173 #if(0 <= MAX_EP_NUMBER) 00174 volatile far BDT ep0Bo; //Endpoint #0 BD Out 00175 volatile far BDT ep0Bi; //Endpoint #0 BD In 00176 #endif 00177 00178 #if(1 <= MAX_EP_NUMBER) 00179 volatile far BDT ep1Bo; //Endpoint #1 BD Out 00180 volatile far BDT ep1Bi; //Endpoint #1 BD In 00181 #endif 00182 00183 #if(2 <= MAX_EP_NUMBER) 00184 volatile far BDT ep2Bo; //Endpoint #2 BD Out 00185 volatile far BDT ep2Bi; //Endpoint #2 BD In 00186 #endif 00187 00188 #if(3 <= MAX_EP_NUMBER) 00189 volatile far BDT ep3Bo; //Endpoint #3 BD Out 00190 volatile far BDT ep3Bi; //Endpoint #3 BD In 00191 #endif 00192 00193 #if(4 <= MAX_EP_NUMBER) 00194 volatile far BDT ep4Bo; //Endpoint #4 BD Out 00195 volatile far BDT ep4Bi; //Endpoint #4 BD In 00196 #endif 00197 00198 #if(5 <= MAX_EP_NUMBER) 00199 volatile far BDT ep5Bo; //Endpoint #5 BD Out 00200 volatile far BDT ep5Bi; //Endpoint #5 BD In 00201 #endif 00202 00203 #if(6 <= MAX_EP_NUMBER) 00204 volatile far BDT ep6Bo; //Endpoint #6 BD Out 00205 volatile far BDT ep6Bi; //Endpoint #6 BD In 00206 #endif 00207 00208 #if(7 <= MAX_EP_NUMBER) 00209 volatile far BDT ep7Bo; //Endpoint #7 BD Out 00210 volatile far BDT ep7Bi; //Endpoint #7 BD In 00211 #endif 00212 00213 #if(8 <= MAX_EP_NUMBER) 00214 volatile far BDT ep8Bo; //Endpoint #8 BD Out 00215 volatile far BDT ep8Bi; //Endpoint #8 BD In 00216 #endif 00217 00218 #if(9 <= MAX_EP_NUMBER) 00219 volatile far BDT ep9Bo; //Endpoint #9 BD Out 00220 volatile far BDT ep9Bi; //Endpoint #9 BD In 00221 #endif 00222 00223 #if(10 <= MAX_EP_NUMBER) 00224 volatile far BDT ep10Bo; //Endpoint #10 BD Out 00225 volatile far BDT ep10Bi; //Endpoint #10 BD In 00226 #endif 00227 00228 #if(11 <= MAX_EP_NUMBER) 00229 volatile far BDT ep11Bo; //Endpoint #11 BD Out 00230 volatile far BDT ep11Bi; //Endpoint #11 BD In 00231 #endif 00232 00233 #if(12 <= MAX_EP_NUMBER) 00234 volatile far BDT ep12Bo; //Endpoint #12 BD Out 00235 volatile far BDT ep12Bi; //Endpoint #12 BD In 00236 #endif 00237 00238 #if(13 <= MAX_EP_NUMBER) 00239 volatile far BDT ep13Bo; //Endpoint #13 BD Out 00240 volatile far BDT ep13Bi; //Endpoint #13 BD In 00241 #endif 00242 00243 #if(14 <= MAX_EP_NUMBER) 00244 volatile far BDT ep14Bo; //Endpoint #14 BD Out 00245 volatile far BDT ep14Bi; //Endpoint #14 BD In 00246 #endif 00247 00248 #if(15 <= MAX_EP_NUMBER) 00249 volatile far BDT ep15Bo; //Endpoint #15 BD Out 00250 volatile far BDT ep15Bi; //Endpoint #15 BD In 00251 #endif 00252 00253 /****************************************************************************** 00254 * Section B: EP0 Buffer Space 00255 ****************************************************************************** 00256 * - Two buffer areas are defined: 00257 * 00258 * A. CTRL_TRF_SETUP 00259 * - Size = EP0_BUFF_SIZE as defined in autofiles\usbcfg.h 00260 * - Detailed data structure allows direct adddressing of bits and bytes. 00261 * 00262 * B. CTRL_TRF_DATA 00263 * - Size = EP0_BUFF_SIZE as defined in autofiles\usbcfg.h 00264 * - Data structure allows direct adddressing of the first 8 bytes. 00265 * 00266 * - Both data types are defined in system\usb\usbdefs\usbdefs_ep0_buff.h 00267 *****************************************************************************/ 00268 volatile far CTRL_TRF_SETUP SetupPkt; 00269 volatile far CTRL_TRF_DATA CtrlTrfData; 00270 00271 /****************************************************************************** 00272 * Section C: CDC Buffer 00273 ****************************************************************************** 00274 * 00275 *****************************************************************************/ 00276 #pragma udata usbram5a=0x500 //See Linker Script,usb5:0x500-... 00277 #if defined(USB_USE_CDC) 00278 volatile far unsigned char cdc_notice[CDC_INT_EP_SIZE]; 00279 volatile far unsigned char cdc_data_rx[CDC_BULK_OUT_EP_SIZE]; 00280 volatile far unsigned char cdc_data_tx[CDC_BULK_IN_EP_SIZE]; 00281 #endif 00282 #pragma udata 00283 00284 /** EOF usbmmap.c ************************************************************/