slime@2
|
1 |
/*********************************************************************
|
slime@2
|
2 |
*
|
slime@2
|
3 |
* Microchip USB C18 Firmware Version 1.0
|
slime@2
|
4 |
*
|
slime@2
|
5 |
*********************************************************************
|
slime@2
|
6 |
* FileName: usbmmap.c
|
slime@2
|
7 |
* Dependencies: See INCLUDES section below
|
slime@2
|
8 |
* Processor: PIC18
|
slime@2
|
9 |
* Compiler: C18 2.30.01+
|
slime@2
|
10 |
* Company: Microchip Technology, Inc.
|
slime@2
|
11 |
*
|
slime@2
|
12 |
* Software License Agreement
|
slime@2
|
13 |
*
|
slime@2
|
14 |
* The software supplied herewith by Microchip Technology Incorporated
|
slime@2
|
15 |
* (the “Company”) for its PICmicro® Microcontroller is intended and
|
slime@2
|
16 |
* supplied to you, the Company’s customer, for use solely and
|
slime@2
|
17 |
* exclusively on Microchip PICmicro Microcontroller products. The
|
slime@2
|
18 |
* software is owned by the Company and/or its supplier, and is
|
slime@2
|
19 |
* protected under applicable copyright laws. All rights are reserved.
|
slime@2
|
20 |
* Any use in violation of the foregoing restrictions may subject the
|
slime@2
|
21 |
* user to criminal sanctions under applicable laws, as well as to
|
slime@2
|
22 |
* civil liability for the breach of the terms and conditions of this
|
slime@2
|
23 |
* license.
|
slime@2
|
24 |
*
|
slime@2
|
25 |
* THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
|
slime@2
|
26 |
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
|
slime@2
|
27 |
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
slime@2
|
28 |
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
|
slime@2
|
29 |
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
|
slime@2
|
30 |
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
|
slime@2
|
31 |
*
|
slime@2
|
32 |
* Author Date Comment
|
slime@2
|
33 |
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
slime@2
|
34 |
* Rawin Rojvanit 11/19/04 Original.
|
slime@2
|
35 |
********************************************************************/
|
slime@2
|
36 |
|
slime@2
|
37 |
/******************************************************************************
|
slime@2
|
38 |
* -usbmmap.c-
|
slime@2
|
39 |
* USB Memory Map
|
slime@2
|
40 |
* This file is the USB memory manager; it serves as a compile-time memory
|
slime@2
|
41 |
* allocator for the USB endpoints. It uses the compile time options passed
|
slime@2
|
42 |
* from usbcfg.h to instantiate endpoints and endpoint buffer.
|
slime@2
|
43 |
*
|
slime@2
|
44 |
* Each endpoint requires to have a set of Buffer Descriptor registers(BDT).
|
slime@2
|
45 |
* A BDT is 4-byte long and has a specific RAM location for each endpoint.
|
slime@2
|
46 |
* The BDT for endpoint 0 out is located at address 0x400 to 0x403.
|
slime@2
|
47 |
* The BDT for endpoint 0 in is located at address 0x404 to 0x407.
|
slime@2
|
48 |
* The BDT for endpoint 1 out is located at address 0x408 to 0x40B.
|
slime@2
|
49 |
* and so on... The above allocation assumes the Ping-Pong Buffer Mode 0 is
|
slime@2
|
50 |
* used. These locations are already hard-wired in the silicon. The point
|
slime@2
|
51 |
* of doing instantiation, i.e. volatile far BDT ep0Bo;, is to provide the
|
slime@2
|
52 |
* C compiler a way to address each variable directly. This is very important
|
slime@2
|
53 |
* because when a register can be accessed directly, it saves execution time
|
slime@2
|
54 |
* and reduces program size.
|
slime@2
|
55 |
*
|
slime@2
|
56 |
* Endpoints are defined using the endpoint number and the direction
|
slime@2
|
57 |
* of transfer. For simplicity, usbmmap.c only uses the endpoint number
|
slime@2
|
58 |
* in the BDT register allocation scheme. This means if the usbcfg.h states
|
slime@2
|
59 |
* that the MAX_EP_NUMBER is number 1, then four BDTs will be
|
slime@2
|
60 |
* instantiated: one each for endpoint0 in and endpoint0 out, which must
|
slime@2
|
61 |
* always be instantiated for control transfer by default, and one each sets
|
slime@2
|
62 |
* for endpoint1 in and endpoint1 out. The naming convention for instantiating
|
slime@2
|
63 |
* BDT is
|
slime@2
|
64 |
*
|
slime@2
|
65 |
* ep<#>B<d>
|
slime@2
|
66 |
*
|
slime@2
|
67 |
* where # is the endpoint number, and d is the direction of
|
slime@2
|
68 |
* transfer, which could be either <i> or <o>.
|
slime@2
|
69 |
*
|
slime@2
|
70 |
* The USB memory manager uses MAX_EP_NUMBER, as defined in usbcfg.h, to define
|
slime@2
|
71 |
* the endpoints to be instantiated. This represents the highest endpoint
|
slime@2
|
72 |
* number to be allocated, not how many endpoints are used. Since the BDTs for
|
slime@2
|
73 |
* endpoints have hardware-assigned addresses in Bank 4, setting this value too
|
slime@2
|
74 |
* high may lead to inefficient use of data RAM. For example, if an application
|
slime@2
|
75 |
* uses only endpoints EP0 and EP4, then the MAX_EP_NUMBER is 4, and not 2.
|
slime@2
|
76 |
* The in-between endpoint BDTs in this example (EP1, EP2, and EP3) go unused,
|
slime@2
|
77 |
* and the 24 bytes of memory associated with them are wasted. It does not make
|
slime@2
|
78 |
* much sense to skip endpoints, but the final decision lies with the user.
|
slime@2
|
79 |
*
|
slime@2
|
80 |
* The next step is to assign the instantiated BDTs to different
|
slime@2
|
81 |
* USB functions. The firmware framework fundamentally assumes that every USB
|
slime@2
|
82 |
* function should know which endpoint it is using, i.e., the default control
|
slime@2
|
83 |
* transfer should know that it is using endpoint 0 in and endpoint 0 out.
|
slime@2
|
84 |
* A HID class can choose which endpoint it wants to use, but once chosen, it
|
slime@2
|
85 |
* should always know the number of the endpoint.
|
slime@2
|
86 |
*
|
slime@2
|
87 |
* The assignment of endpoints to USB functions is managed centrally
|
slime@2
|
88 |
* in usbcfg.h. This helps prevent the mistake of having more
|
slime@2
|
89 |
* than one USB function using the same endpoint. The "Endpoint Allocation"
|
slime@2
|
90 |
* section in usbcfg.h provides examples for how to map USB endpoints to USB
|
slime@2
|
91 |
* functions.
|
slime@2
|
92 |
* Quite a few things can be mapped in that section. There is no
|
slime@2
|
93 |
* one correct way to do the mapping and the user has the choice to
|
slime@2
|
94 |
* choose a method that is most suitable to the application.
|
slime@2
|
95 |
*
|
slime@2
|
96 |
* Typically, however, a user will want to map the following for a given
|
slime@2
|
97 |
* USB interface function:
|
slime@2
|
98 |
* 1. The USB interface ID
|
slime@2
|
99 |
* 2. The endpoint control registers (UEPn)
|
slime@2
|
100 |
* 3. The BDT registers (ep<#>B<d>)
|
slime@2
|
101 |
* 4. The endpoint size
|
slime@2
|
102 |
*
|
slime@2
|
103 |
* Example: Assume a USB device class "foo", which uses one out endpoint
|
slime@2
|
104 |
* of size 64-byte and one in endpoint of size 64-byte, then:
|
slime@2
|
105 |
*
|
slime@2
|
106 |
* #define FOO_INTF_ID 0x00
|
slime@2
|
107 |
* #define FOO_UEP UEP1
|
slime@2
|
108 |
* #define FOO_BD_OUT ep1Bo
|
slime@2
|
109 |
* #define FOO_BD_IN ep1Bi
|
slime@2
|
110 |
* #define FOO_EP_SIZE 64
|
slime@2
|
111 |
*
|
slime@2
|
112 |
* The mapping above has chosen class "foo" to use endpoint 1.
|
slime@2
|
113 |
* The names are arbitrary and can be anything other than FOO_??????.
|
slime@2
|
114 |
* For abstraction, the code for class "foo" should use the abstract
|
slime@2
|
115 |
* definitions of FOO_BD_OUT,FOO_BD_IN, and not ep1Bo or ep1Bi.
|
slime@2
|
116 |
*
|
slime@2
|
117 |
* Note that the endpoint size defined in the usbcfg.h file is again
|
slime@2
|
118 |
* used in the usbmmap.c file. This shows that the relationship between
|
slime@2
|
119 |
* the two files are tightly related.
|
slime@2
|
120 |
*
|
slime@2
|
121 |
* The endpoint buffer for each USB function must be located in the
|
slime@2
|
122 |
* dual-port RAM area and has to come after all the BDTs have been
|
slime@2
|
123 |
* instantiated. An example declaration is:
|
slime@2
|
124 |
* volatile far unsigned char[FOO_EP_SIZE] data;
|
slime@2
|
125 |
*
|
slime@2
|
126 |
* The 'volatile' keyword tells the compiler not to perform any code
|
slime@2
|
127 |
* optimization on this variable because its content could be modified
|
slime@2
|
128 |
* by the hardware. The 'far' keyword tells the compiler that this variable
|
slime@2
|
129 |
* is not located in the Access RAM area (0x000 - 0x05F).
|
slime@2
|
130 |
*
|
slime@2
|
131 |
* For the variable to be globally accessible by other files, it should be
|
slime@2
|
132 |
* declared in the header file usbmmap.h as an extern definition, such as
|
slime@2
|
133 |
* extern volatile far unsigned char[FOO_EP_SIZE] data;
|
slime@2
|
134 |
*
|
slime@2
|
135 |
* Conclusion:
|
slime@2
|
136 |
* In a short summary, the dependencies between usbcfg and usbmmap can
|
slime@2
|
137 |
* be shown as:
|
slime@2
|
138 |
*
|
slime@2
|
139 |
* usbcfg[MAX_EP_NUMBER] -> usbmmap
|
slime@2
|
140 |
* usbmmap[ep<#>B<d>] -> usbcfg
|
slime@2
|
141 |
* usbcfg[EP size] -> usbmmap
|
slime@2
|
142 |
* usbcfg[abstract ep definitions] -> usb9/hid/cdc/etc class code
|
slime@2
|
143 |
* usbmmap[endpoint buffer variable] -> usb9/hid/cdc/etc class code
|
slime@2
|
144 |
*
|
slime@2
|
145 |
* Data mapping provides a means for direct addressing of BDT and endpoint
|
slime@2
|
146 |
* buffer. This means less usage of pointers, which equates to a faster and
|
slime@2
|
147 |
* smaller program code.
|
slime@2
|
148 |
*
|
slime@2
|
149 |
*****************************************************************************/
|
slime@2
|
150 |
|
slime@2
|
151 |
/** I N C L U D E S **********************************************************/
|
slime@2
|
152 |
#include "typedefs.h"
|
slime@2
|
153 |
#include "usb.h"
|
slime@2
|
154 |
|
slime@2
|
155 |
/** U S B G L O B A L V A R I A B L E S ************************************/
|
slime@2
|
156 |
#pragma udata
|
slime@2
|
157 |
byte usb_device_state; // Device States: DETACHED, ATTACHED, ...
|
slime@2
|
158 |
USB_DEVICE_STATUS usb_stat; // Global USB flags
|
slime@2
|
159 |
byte usb_active_cfg; // Value of current configuration
|
slime@2
|
160 |
byte usb_alt_intf[MAX_NUM_INT]; // Array to keep track of the current alternate
|
slime@2
|
161 |
// setting for each interface ID
|
slime@2
|
162 |
|
slime@2
|
163 |
/** 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 *********************/
|
slime@2
|
164 |
#pragma udata usbram4=0x400 //See Linker Script,usb4:0x400-0x4FF(256-byte)
|
slime@2
|
165 |
|
slime@2
|
166 |
/******************************************************************************
|
slime@2
|
167 |
* Section A: Buffer Descriptor Table
|
slime@2
|
168 |
* - 0x400 - 0x4FF(max)
|
slime@2
|
169 |
* - MAX_EP_NUMBER is defined in autofiles\usbcfg.h
|
slime@2
|
170 |
* - BDT data type is defined in system\usb\usbmmap.h
|
slime@2
|
171 |
*****************************************************************************/
|
slime@2
|
172 |
|
slime@2
|
173 |
#if(0 <= MAX_EP_NUMBER)
|
slime@2
|
174 |
volatile far BDT ep0Bo; //Endpoint #0 BD Out
|
slime@2
|
175 |
volatile far BDT ep0Bi; //Endpoint #0 BD In
|
slime@2
|
176 |
#endif
|
slime@2
|
177 |
|
slime@2
|
178 |
#if(1 <= MAX_EP_NUMBER)
|
slime@2
|
179 |
volatile far BDT ep1Bo; //Endpoint #1 BD Out
|
slime@2
|
180 |
volatile far BDT ep1Bi; //Endpoint #1 BD In
|
slime@2
|
181 |
#endif
|
slime@2
|
182 |
|
slime@2
|
183 |
#if(2 <= MAX_EP_NUMBER)
|
slime@2
|
184 |
volatile far BDT ep2Bo; //Endpoint #2 BD Out
|
slime@2
|
185 |
volatile far BDT ep2Bi; //Endpoint #2 BD In
|
slime@2
|
186 |
#endif
|
slime@2
|
187 |
|
slime@2
|
188 |
#if(3 <= MAX_EP_NUMBER)
|
slime@2
|
189 |
volatile far BDT ep3Bo; //Endpoint #3 BD Out
|
slime@2
|
190 |
volatile far BDT ep3Bi; //Endpoint #3 BD In
|
slime@2
|
191 |
#endif
|
slime@2
|
192 |
|
slime@2
|
193 |
#if(4 <= MAX_EP_NUMBER)
|
slime@2
|
194 |
volatile far BDT ep4Bo; //Endpoint #4 BD Out
|
slime@2
|
195 |
volatile far BDT ep4Bi; //Endpoint #4 BD In
|
slime@2
|
196 |
#endif
|
slime@2
|
197 |
|
slime@2
|
198 |
#if(5 <= MAX_EP_NUMBER)
|
slime@2
|
199 |
volatile far BDT ep5Bo; //Endpoint #5 BD Out
|
slime@2
|
200 |
volatile far BDT ep5Bi; //Endpoint #5 BD In
|
slime@2
|
201 |
#endif
|
slime@2
|
202 |
|
slime@2
|
203 |
#if(6 <= MAX_EP_NUMBER)
|
slime@2
|
204 |
volatile far BDT ep6Bo; //Endpoint #6 BD Out
|
slime@2
|
205 |
volatile far BDT ep6Bi; //Endpoint #6 BD In
|
slime@2
|
206 |
#endif
|
slime@2
|
207 |
|
slime@2
|
208 |
#if(7 <= MAX_EP_NUMBER)
|
slime@2
|
209 |
volatile far BDT ep7Bo; //Endpoint #7 BD Out
|
slime@2
|
210 |
volatile far BDT ep7Bi; //Endpoint #7 BD In
|
slime@2
|
211 |
#endif
|
slime@2
|
212 |
|
slime@2
|
213 |
#if(8 <= MAX_EP_NUMBER)
|
slime@2
|
214 |
volatile far BDT ep8Bo; //Endpoint #8 BD Out
|
slime@2
|
215 |
volatile far BDT ep8Bi; //Endpoint #8 BD In
|
slime@2
|
216 |
#endif
|
slime@2
|
217 |
|
slime@2
|
218 |
#if(9 <= MAX_EP_NUMBER)
|
slime@2
|
219 |
volatile far BDT ep9Bo; //Endpoint #9 BD Out
|
slime@2
|
220 |
volatile far BDT ep9Bi; //Endpoint #9 BD In
|
slime@2
|
221 |
#endif
|
slime@2
|
222 |
|
slime@2
|
223 |
#if(10 <= MAX_EP_NUMBER)
|
slime@2
|
224 |
volatile far BDT ep10Bo; //Endpoint #10 BD Out
|
slime@2
|
225 |
volatile far BDT ep10Bi; //Endpoint #10 BD In
|
slime@2
|
226 |
#endif
|
slime@2
|
227 |
|
slime@2
|
228 |
#if(11 <= MAX_EP_NUMBER)
|
slime@2
|
229 |
volatile far BDT ep11Bo; //Endpoint #11 BD Out
|
slime@2
|
230 |
volatile far BDT ep11Bi; //Endpoint #11 BD In
|
slime@2
|
231 |
#endif
|
slime@2
|
232 |
|
slime@2
|
233 |
#if(12 <= MAX_EP_NUMBER)
|
slime@2
|
234 |
volatile far BDT ep12Bo; //Endpoint #12 BD Out
|
slime@2
|
235 |
volatile far BDT ep12Bi; //Endpoint #12 BD In
|
slime@2
|
236 |
#endif
|
slime@2
|
237 |
|
slime@2
|
238 |
#if(13 <= MAX_EP_NUMBER)
|
slime@2
|
239 |
volatile far BDT ep13Bo; //Endpoint #13 BD Out
|
slime@2
|
240 |
volatile far BDT ep13Bi; //Endpoint #13 BD In
|
slime@2
|
241 |
#endif
|
slime@2
|
242 |
|
slime@2
|
243 |
#if(14 <= MAX_EP_NUMBER)
|
slime@2
|
244 |
volatile far BDT ep14Bo; //Endpoint #14 BD Out
|
slime@2
|
245 |
volatile far BDT ep14Bi; //Endpoint #14 BD In
|
slime@2
|
246 |
#endif
|
slime@2
|
247 |
|
slime@2
|
248 |
#if(15 <= MAX_EP_NUMBER)
|
slime@2
|
249 |
volatile far BDT ep15Bo; //Endpoint #15 BD Out
|
slime@2
|
250 |
volatile far BDT ep15Bi; //Endpoint #15 BD In
|
slime@2
|
251 |
#endif
|
slime@2
|
252 |
|
slime@2
|
253 |
/******************************************************************************
|
slime@2
|
254 |
* Section B: EP0 Buffer Space
|
slime@2
|
255 |
******************************************************************************
|
slime@2
|
256 |
* - Two buffer areas are defined:
|
slime@2
|
257 |
*
|
slime@2
|
258 |
* A. CTRL_TRF_SETUP
|
slime@2
|
259 |
* - Size = EP0_BUFF_SIZE as defined in autofiles\usbcfg.h
|
slime@2
|
260 |
* - Detailed data structure allows direct adddressing of bits and bytes.
|
slime@2
|
261 |
*
|
slime@2
|
262 |
* B. CTRL_TRF_DATA
|
slime@2
|
263 |
* - Size = EP0_BUFF_SIZE as defined in autofiles\usbcfg.h
|
slime@2
|
264 |
* - Data structure allows direct adddressing of the first 8 bytes.
|
slime@2
|
265 |
*
|
slime@2
|
266 |
* - Both data types are defined in system\usb\usbdefs\usbdefs_ep0_buff.h
|
slime@2
|
267 |
*****************************************************************************/
|
slime@2
|
268 |
volatile far CTRL_TRF_SETUP SetupPkt;
|
slime@2
|
269 |
volatile far CTRL_TRF_DATA CtrlTrfData;
|
slime@2
|
270 |
|
slime@2
|
271 |
/******************************************************************************
|
slime@2
|
272 |
* Section C: CDC Buffer
|
slime@2
|
273 |
******************************************************************************
|
slime@2
|
274 |
*
|
slime@2
|
275 |
*****************************************************************************/
|
slime@2
|
276 |
#pragma udata usbram5a=0x500 //See Linker Script,usb5:0x500-...
|
slime@2
|
277 |
#if defined(USB_USE_CDC)
|
slime@2
|
278 |
volatile far unsigned char cdc_notice[CDC_INT_EP_SIZE];
|
slime@2
|
279 |
volatile far unsigned char cdc_data_rx[CDC_BULK_OUT_EP_SIZE];
|
slime@2
|
280 |
volatile far unsigned char cdc_data_tx[CDC_BULK_IN_EP_SIZE];
|
slime@2
|
281 |
#endif
|
slime@2
|
282 |
#pragma udata
|
slime@2
|
283 |
|
slime@2
|
284 |
/** EOF usbmmap.c ************************************************************/
|