00001 /* Name: usbdrv.h 00002 * Project: AVR USB driver 00003 * Author: Christian Starkjohann 00004 * Creation Date: 2004-12-29 00005 * Tabsize: 4 00006 * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH 00007 * License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt) 00008 * This Revision: $Id: usbdrv.h 563 2008-04-18 18:02:44Z cs $ 00009 */ 00010 00011 #ifndef __usbdrv_h_included__ 00012 #define __usbdrv_h_included__ 00013 #include "usbconfig.h" 00014 #include "iarcompat.h" 00015 00016 /* 00017 Hardware Prerequisites: 00018 ======================= 00019 USB lines D+ and D- MUST be wired to the same I/O port. We recommend that D+ 00020 triggers the interrupt (best achieved by using INT0 for D+), but it is also 00021 possible to trigger the interrupt from D-. If D- is used, interrupts are also 00022 triggered by SOF packets. D- requires a pull-up of 1.5k to +3.5V (and the 00023 device must be powered at 3.5V) to identify as low-speed USB device. A 00024 pull-down or pull-up of 1M SHOULD be connected from D+ to +3.5V to prevent 00025 interference when no USB master is connected. If you use Zener diodes to limit 00026 the voltage on D+ and D-, you MUST use a pull-down resistor, not a pull-up. 00027 We use D+ as interrupt source and not D- because it does not trigger on 00028 keep-alive and RESET states. If you want to count keep-alive events with 00029 USB_COUNT_SOF, you MUST use D- as an interrupt source. 00030 00031 As a compile time option, the 1.5k pull-up resistor on D- can be made 00032 switchable to allow the device to disconnect at will. See the definition of 00033 usbDeviceConnect() and usbDeviceDisconnect() further down in this file. 00034 00035 Please adapt the values in usbconfig.h according to your hardware! 00036 00037 The device MUST be clocked at exactly 12 MHz, 15 MHz or 16 MHz 00038 or at 16.5 MHz +/- 1%. See usbconfig-prototype.h for details. 00039 00040 00041 Limitations: 00042 ============ 00043 Robustness with respect to communication errors: 00044 The driver assumes error-free communication. It DOES check for errors in 00045 the PID, but does NOT check bit stuffing errors, SE0 in middle of a byte, 00046 token CRC (5 bit) and data CRC (16 bit). CRC checks can not be performed due 00047 to timing constraints: We must start sending a reply within 7 bit times. 00048 Bit stuffing and misplaced SE0 would have to be checked in real-time, but CPU 00049 performance does not permit that. The driver does not check Data0/Data1 00050 toggling, but application software can implement the check. 00051 00052 Input characteristics: 00053 Since no differential receiver circuit is used, electrical interference 00054 robustness may suffer. The driver samples only one of the data lines with 00055 an ordinary I/O pin's input characteristics. However, since this is only a 00056 low speed USB implementation and the specification allows for 8 times the 00057 bit rate over the same hardware, we should be on the safe side. Even the spec 00058 requires detection of asymmetric states at high bit rate for SE0 detection. 00059 00060 Number of endpoints: 00061 The driver supports the following endpoints: 00062 00063 - Endpoint 0, the default control endpoint. 00064 - Any number of interrupt- or bulk-out endpoints. The data is sent to 00065 usbFunctionWriteOut() and USB_CFG_IMPLEMENT_FN_WRITEOUT must be defined 00066 to 1 to activate this feature. The endpoint number can be found in the 00067 global variable 'usbRxToken'. 00068 - One default interrupt- or bulk-in endpoint. This endpoint is used for 00069 interrupt- or bulk-in transfers which are not handled by any other endpoint. 00070 You must define USB_CFG_HAVE_INTRIN_ENDPOINT in order to activate this 00071 feature and call usbSetInterrupt() to send interrupt/bulk data. 00072 - One additional interrupt- or bulk-in endpoint. This was endpoint 3 in 00073 previous versions of this driver but can now be configured to any endpoint 00074 number. You must define USB_CFG_HAVE_INTRIN_ENDPOINT3 in order to activate 00075 this feature and call usbSetInterrupt3() to send interrupt/bulk data. The 00076 endpoint number can be set with USB_CFG_EP3_NUMBER. 00077 00078 Please note that the USB standard forbids bulk endpoints for low speed devices! 00079 Most operating systems allow them anyway, but the AVR will spend 90% of the CPU 00080 time in the USB interrupt polling for bulk data. 00081 00082 Maximum data payload: 00083 Data payload of control in and out transfers may be up to 254 bytes. In order 00084 to accept payload data of out transfers, you need to implement 00085 'usbFunctionWrite()'. 00086 00087 USB Suspend Mode supply current: 00088 The USB standard limits power consumption to 500uA when the bus is in suspend 00089 mode. This is not a problem for self-powered devices since they don't need 00090 bus power anyway. Bus-powered devices can achieve this only by putting the 00091 CPU in sleep mode. The driver does not implement suspend handling by itself. 00092 However, the application may implement activity monitoring and wakeup from 00093 sleep. The host sends regular SE0 states on the bus to keep it active. These 00094 SE0 states can be detected by using D- as the interrupt source. Define 00095 USB_COUNT_SOF to 1 and use the global variable usbSofCount to check for bus 00096 activity. 00097 00098 Operation without an USB master: 00099 The driver behaves neutral without connection to an USB master if D- reads 00100 as 1. To avoid spurious interrupts, we recommend a high impedance (e.g. 1M) 00101 pull-down or pull-up resistor on D+ (interrupt). If Zener diodes are used, 00102 use a pull-down. If D- becomes statically 0, the driver may block in the 00103 interrupt routine. 00104 00105 Interrupt latency: 00106 The application must ensure that the USB interrupt is not disabled for more 00107 than 25 cycles (this is for 12 MHz, faster clocks allow longer latency). 00108 This implies that all interrupt routines must either be declared as "INTERRUPT" 00109 instead of "SIGNAL" (see "avr/signal.h") or that they are written in assembler 00110 with "sei" as the first instruction. 00111 00112 Maximum interrupt duration / CPU cycle consumption: 00113 The driver handles all USB communication during the interrupt service 00114 routine. The routine will not return before an entire USB message is received 00115 and the reply is sent. This may be up to ca. 1200 cycles @ 12 MHz (= 100us) if 00116 the host conforms to the standard. The driver will consume CPU cycles for all 00117 USB messages, even if they address another (low-speed) device on the same bus. 00118 00119 */ 00120 00121 /* ------------------------------------------------------------------------- */ 00122 /* --------------------------- Module Interface ---------------------------- */ 00123 /* ------------------------------------------------------------------------- */ 00124 00125 #define USBDRV_VERSION 20080418 00126 /* This define uniquely identifies a driver version. It is a decimal number 00127 * constructed from the driver's release date in the form YYYYMMDD. If the 00128 * driver's behavior or interface changes, you can use this constant to 00129 * distinguish versions. If it is not defined, the driver's release date is 00130 * older than 2006-01-25. 00131 */ 00132 00133 00134 #ifndef USB_PUBLIC 00135 #define USB_PUBLIC 00136 #endif 00137 /* USB_PUBLIC is used as declaration attribute for all functions exported by 00138 * the USB driver. The default is no attribute (see above). You may define it 00139 * to static either in usbconfig.h or from the command line if you include 00140 * usbdrv.c instead of linking against it. Including the C module of the driver 00141 * directly in your code saves a couple of bytes in flash memory. 00142 */ 00143 00144 #ifndef __ASSEMBLER__ 00145 #ifndef uchar 00146 #define uchar unsigned char 00147 #endif 00148 #ifndef schar 00149 #define schar signed char 00150 #endif 00151 /* shortcuts for well defined 8 bit integer types */ 00152 00153 struct usbRequest; /* forward declaration */ 00154 00155 USB_PUBLIC void usbInit(void); 00156 /* This function must be called before interrupts are enabled and the main 00157 * loop is entered. 00158 */ 00159 USB_PUBLIC void usbPoll(void); 00160 /* This function must be called at regular intervals from the main loop. 00161 * Maximum delay between calls is somewhat less than 50ms (USB timeout for 00162 * accepting a Setup message). Otherwise the device will not be recognized. 00163 * Please note that debug outputs through the UART take ~ 0.5ms per byte 00164 * at 19200 bps. 00165 */ 00166 extern uchar *usbMsgPtr; 00167 /* This variable may be used to pass transmit data to the driver from the 00168 * implementation of usbFunctionWrite(). It is also used internally by the 00169 * driver for standard control requests. 00170 */ 00171 USB_PUBLIC uchar usbFunctionSetup(uchar data[8]); 00172 /* This function is called when the driver receives a SETUP transaction from 00173 * the host which is not answered by the driver itself (in practice: class and 00174 * vendor requests). All control transfers start with a SETUP transaction where 00175 * the host communicates the parameters of the following (optional) data 00176 * transfer. The SETUP data is available in the 'data' parameter which can 00177 * (and should) be casted to 'usbRequest_t *' for a more user-friendly access 00178 * to parameters. 00179 * 00180 * If the SETUP indicates a control-in transfer, you should provide the 00181 * requested data to the driver. There are two ways to transfer this data: 00182 * (1) Set the global pointer 'usbMsgPtr' to the base of the static RAM data 00183 * block and return the length of the data in 'usbFunctionSetup()'. The driver 00184 * will handle the rest. Or (2) return 0xff in 'usbFunctionSetup()'. The driver 00185 * will then call 'usbFunctionRead()' when data is needed. See the 00186 * documentation for usbFunctionRead() for details. 00187 * 00188 * If the SETUP indicates a control-out transfer, the only way to receive the 00189 * data from the host is through the 'usbFunctionWrite()' call. If you 00190 * implement this function, you must return 0xff in 'usbFunctionSetup()' to 00191 * indicate that 'usbFunctionWrite()' should be used. See the documentation of 00192 * this function for more information. If you just want to ignore the data sent 00193 * by the host, return 0 in 'usbFunctionSetup()'. 00194 * 00195 * Note that calls to the functions usbFunctionRead() and usbFunctionWrite() 00196 * are only done if enabled by the configuration in usbconfig.h. 00197 */ 00198 USB_PUBLIC uchar usbFunctionDescriptor(struct usbRequest *rq); 00199 /* You need to implement this function ONLY if you provide USB descriptors at 00200 * runtime (which is an expert feature). It is very similar to 00201 * usbFunctionSetup() above, but it is called only to request USB descriptor 00202 * data. See the documentation of usbFunctionSetup() above for more info. 00203 */ 00204 #if USB_CFG_HAVE_INTRIN_ENDPOINT 00205 USB_PUBLIC void usbSetInterrupt(uchar *data, uchar len); 00206 /* This function sets the message which will be sent during the next interrupt 00207 * IN transfer. The message is copied to an internal buffer and must not exceed 00208 * a length of 8 bytes. The message may be 0 bytes long just to indicate the 00209 * interrupt status to the host. 00210 * If you need to transfer more bytes, use a control read after the interrupt. 00211 */ 00212 extern volatile uchar usbTxLen1; 00213 #define usbInterruptIsReady() (usbTxLen1 & 0x10) 00214 /* This macro indicates whether the last interrupt message has already been 00215 * sent. If you set a new interrupt message before the old was sent, the 00216 * message already buffered will be lost. 00217 */ 00218 #if USB_CFG_HAVE_INTRIN_ENDPOINT3 00219 USB_PUBLIC void usbSetInterrupt3(uchar *data, uchar len); 00220 extern volatile uchar usbTxLen3; 00221 #define usbInterruptIsReady3() (usbTxLen3 & 0x10) 00222 /* Same as above for endpoint 3 */ 00223 #endif 00224 #endif /* USB_CFG_HAVE_INTRIN_ENDPOINT */ 00225 #if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH /* simplified interface for backward compatibility */ 00226 #define usbHidReportDescriptor usbDescriptorHidReport 00227 /* should be declared as: PROGMEM char usbHidReportDescriptor[]; */ 00228 /* If you implement an HID device, you need to provide a report descriptor. 00229 * The HID report descriptor syntax is a bit complex. If you understand how 00230 * report descriptors are constructed, we recommend that you use the HID 00231 * Descriptor Tool from usb.org, see http://www.usb.org/developers/hidpage/. 00232 * Otherwise you should probably start with a working example. 00233 */ 00234 #endif /* USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH */ 00235 #if USB_CFG_IMPLEMENT_FN_WRITE 00236 USB_PUBLIC uchar usbFunctionWrite(uchar *data, uchar len); 00237 /* This function is called by the driver to provide a control transfer's 00238 * payload data (control-out). It is called in chunks of up to 8 bytes. The 00239 * total count provided in the current control transfer can be obtained from 00240 * the 'length' property in the setup data. If an error occurred during 00241 * processing, return 0xff (== -1). The driver will answer the entire transfer 00242 * with a STALL token in this case. If you have received the entire payload 00243 * successfully, return 1. If you expect more data, return 0. If you don't 00244 * know whether the host will send more data (you should know, the total is 00245 * provided in the usbFunctionSetup() call!), return 1. 00246 * NOTE: If you return 0xff for STALL, 'usbFunctionWrite()' may still be called 00247 * for the remaining data. You must continue to return 0xff for STALL in these 00248 * calls. 00249 * In order to get usbFunctionWrite() called, define USB_CFG_IMPLEMENT_FN_WRITE 00250 * to 1 in usbconfig.h and return 0xff in usbFunctionSetup().. 00251 */ 00252 #endif /* USB_CFG_IMPLEMENT_FN_WRITE */ 00253 #if USB_CFG_IMPLEMENT_FN_READ 00254 USB_PUBLIC uchar usbFunctionRead(uchar *data, uchar len); 00255 /* This function is called by the driver to ask the application for a control 00256 * transfer's payload data (control-in). It is called in chunks of up to 8 00257 * bytes each. You should copy the data to the location given by 'data' and 00258 * return the actual number of bytes copied. If you return less than requested, 00259 * the control-in transfer is terminated. If you return 0xff, the driver aborts 00260 * the transfer with a STALL token. 00261 * In order to get usbFunctionRead() called, define USB_CFG_IMPLEMENT_FN_READ 00262 * to 1 in usbconfig.h and return 0xff in usbFunctionSetup().. 00263 */ 00264 #endif /* USB_CFG_IMPLEMENT_FN_READ */ 00265 #if USB_CFG_IMPLEMENT_FN_WRITEOUT 00266 USB_PUBLIC void usbFunctionWriteOut(uchar *data, uchar len); 00267 /* This function is called by the driver when data is received on an interrupt- 00268 * or bulk-out endpoint. The endpoint number can be found in the global 00269 * variable usbRxToken. You must define USB_CFG_IMPLEMENT_FN_WRITEOUT to 1 in 00270 * usbconfig.h to get this function called. 00271 */ 00272 #endif /* USB_CFG_IMPLEMENT_FN_WRITEOUT */ 00273 #ifdef USB_CFG_PULLUP_IOPORTNAME 00274 #define usbDeviceConnect() ((USB_PULLUP_DDR |= (1<<USB_CFG_PULLUP_BIT)), \ 00275 (USB_PULLUP_OUT |= (1<<USB_CFG_PULLUP_BIT))) 00276 #define usbDeviceDisconnect() ((USB_PULLUP_DDR &= ~(1<<USB_CFG_PULLUP_BIT)), \ 00277 (USB_PULLUP_OUT &= ~(1<<USB_CFG_PULLUP_BIT))) 00278 #else /* USB_CFG_PULLUP_IOPORTNAME */ 00279 #define usbDeviceConnect() (USBDDR &= ~(1<<USBMINUS)) 00280 #define usbDeviceDisconnect() (USBDDR |= (1<<USBMINUS)) 00281 #endif /* USB_CFG_PULLUP_IOPORTNAME */ 00282 /* The macros usbDeviceConnect() and usbDeviceDisconnect() (intended to look 00283 * like a function) connect resp. disconnect the device from the host's USB. 00284 * If the constants USB_CFG_PULLUP_IOPORT and USB_CFG_PULLUP_BIT are defined 00285 * in usbconfig.h, a disconnect consists of removing the pull-up resisitor 00286 * from D-, otherwise the disconnect is done by brute-force pulling D- to GND. 00287 * This does not conform to the spec, but it works. 00288 * Please note that the USB interrupt must be disabled while the device is 00289 * in disconnected state, or the interrupt handler will hang! You can either 00290 * turn off the USB interrupt selectively with 00291 * USB_INTR_ENABLE &= ~(1 << USB_INTR_ENABLE_BIT) 00292 * or use cli() to disable interrupts globally. 00293 */ 00294 extern unsigned usbCrc16(unsigned data, uchar len); 00295 #define usbCrc16(data, len) usbCrc16((unsigned)(data), len) 00296 /* This function calculates the binary complement of the data CRC used in 00297 * USB data packets. The value is used to build raw transmit packets. 00298 * You may want to use this function for data checksums or to verify received 00299 * data. We enforce 16 bit calling conventions for compatibility with IAR's 00300 * tiny memory model. 00301 */ 00302 extern unsigned usbCrc16Append(unsigned data, uchar len); 00303 #define usbCrc16Append(data, len) usbCrc16Append((unsigned)(data), len) 00304 /* This function is equivalent to usbCrc16() above, except that it appends 00305 * the 2 bytes CRC (lowbyte first) in the 'data' buffer after reading 'len' 00306 * bytes. 00307 */ 00308 #if USB_CFG_HAVE_MEASURE_FRAME_LENGTH 00309 extern unsigned usbMeasureFrameLength(void); 00310 /* This function MUST be called IMMEDIATELY AFTER USB reset and measures 1/7 of 00311 * the number of CPU cycles during one USB frame minus one low speed bit 00312 * length. In other words: return value = 1499 * (F_CPU / 10.5 MHz) 00313 * Since this is a busy wait, you MUST disable all interrupts with cli() before 00314 * calling this function. 00315 * This can be used to calibrate the AVR's RC oscillator. 00316 */ 00317 #endif 00318 extern uchar usbConfiguration; 00319 /* This value contains the current configuration set by the host. The driver 00320 * allows setting and querying of this variable with the USB SET_CONFIGURATION 00321 * and GET_CONFIGURATION requests, but does not use it otherwise. 00322 * You may want to reflect the "configured" status with a LED on the device or 00323 * switch on high power parts of the circuit only if the device is configured. 00324 */ 00325 #if USB_COUNT_SOF 00326 extern volatile uchar usbSofCount; 00327 /* This variable is incremented on every SOF packet. It is only available if 00328 * the macro USB_COUNT_SOF is defined to a value != 0. 00329 */ 00330 #endif 00331 00332 #define USB_STRING_DESCRIPTOR_HEADER(stringLength) ((2*(stringLength)+2) | (3<<8)) 00333 /* This macro builds a descriptor header for a string descriptor given the 00334 * string's length. See usbdrv.c for an example how to use it. 00335 */ 00336 #if USB_CFG_HAVE_FLOWCONTROL 00337 extern volatile schar usbRxLen; 00338 #define usbDisableAllRequests() usbRxLen = -1 00339 /* Must be called from usbFunctionWrite(). This macro disables all data input 00340 * from the USB interface. Requests from the host are answered with a NAK 00341 * while they are disabled. 00342 */ 00343 #define usbEnableAllRequests() usbRxLen = 0 00344 /* May only be called if requests are disabled. This macro enables input from 00345 * the USB interface after it has been disabled with usbDisableAllRequests(). 00346 */ 00347 #define usbAllRequestsAreDisabled() (usbRxLen < 0) 00348 /* Use this macro to find out whether requests are disabled. It may be needed 00349 * to ensure that usbEnableAllRequests() is never called when requests are 00350 * enabled. 00351 */ 00352 #endif 00353 00354 #define USB_SET_DATATOKEN1(token) usbTxBuf1[0] = token 00355 #define USB_SET_DATATOKEN3(token) usbTxBuf3[0] = token 00356 /* These two macros can be used by application software to reset data toggling 00357 * for interrupt-in endpoints 1 and 3. Since the token is toggled BEFORE 00358 * sending data, you must set the opposite value of the token which should come 00359 * first. 00360 */ 00361 00362 #endif /* __ASSEMBLER__ */ 00363 00364 00365 /* ------------------------------------------------------------------------- */ 00366 /* ----------------- Definitions for Descriptor Properties ----------------- */ 00367 /* ------------------------------------------------------------------------- */ 00368 /* This is advanced stuff. See usbconfig-prototype.h for more information 00369 * about the various methods to define USB descriptors. If you do nothing, 00370 * the default descriptors will be used. 00371 */ 00372 #define USB_PROP_IS_DYNAMIC (1 << 8) 00373 /* If this property is set for a descriptor, usbFunctionDescriptor() will be 00374 * used to obtain the particular descriptor. 00375 */ 00376 #define USB_PROP_IS_RAM (1 << 9) 00377 /* If this property is set for a descriptor, the data is read from RAM 00378 * memory instead of Flash. The property is used for all methods to provide 00379 * external descriptors. 00380 */ 00381 #define USB_PROP_LENGTH(len) ((len) & 0xff) 00382 /* If a static external descriptor is used, this is the total length of the 00383 * descriptor in bytes. 00384 */ 00385 00386 /* all descriptors which may have properties: */ 00387 #ifndef USB_CFG_DESCR_PROPS_DEVICE 00388 #define USB_CFG_DESCR_PROPS_DEVICE 0 00389 #endif 00390 #ifndef USB_CFG_DESCR_PROPS_CONFIGURATION 00391 #define USB_CFG_DESCR_PROPS_CONFIGURATION 0 00392 #endif 00393 #ifndef USB_CFG_DESCR_PROPS_STRINGS 00394 #define USB_CFG_DESCR_PROPS_STRINGS 0 00395 #endif 00396 #ifndef USB_CFG_DESCR_PROPS_STRING_0 00397 #define USB_CFG_DESCR_PROPS_STRING_0 0 00398 #endif 00399 #ifndef USB_CFG_DESCR_PROPS_STRING_VENDOR 00400 #define USB_CFG_DESCR_PROPS_STRING_VENDOR 0 00401 #endif 00402 #ifndef USB_CFG_DESCR_PROPS_STRING_PRODUCT 00403 #define USB_CFG_DESCR_PROPS_STRING_PRODUCT 0 00404 #endif 00405 #ifndef USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER 00406 #define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER 0 00407 #endif 00408 #ifndef USB_CFG_DESCR_PROPS_HID 00409 #define USB_CFG_DESCR_PROPS_HID 0 00410 #endif 00411 #if !(USB_CFG_DESCR_PROPS_HID_REPORT) 00412 # undef USB_CFG_DESCR_PROPS_HID_REPORT 00413 # if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH /* do some backward compatibility tricks */ 00414 # define USB_CFG_DESCR_PROPS_HID_REPORT USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH 00415 # else 00416 # define USB_CFG_DESCR_PROPS_HID_REPORT 0 00417 # endif 00418 #endif 00419 #ifndef USB_CFG_DESCR_PROPS_UNKNOWN 00420 #define USB_CFG_DESCR_PROPS_UNKNOWN 0 00421 #endif 00422 00423 /* ------------------ forward declaration of descriptors ------------------- */ 00424 /* If you use external static descriptors, they must be stored in global 00425 * arrays as declared below: 00426 */ 00427 #ifndef __ASSEMBLER__ 00428 extern 00429 #if !(USB_CFG_DESCR_PROPS_DEVICE & USB_PROP_IS_RAM) 00430 PROGMEM 00431 #endif 00432 char usbDescriptorDevice[]; 00433 00434 extern 00435 #if !(USB_CFG_DESCR_PROPS_CONFIGURATION & USB_PROP_IS_RAM) 00436 PROGMEM 00437 #endif 00438 char usbDescriptorConfiguration[]; 00439 00440 extern 00441 #if !(USB_CFG_DESCR_PROPS_HID_REPORT & USB_PROP_IS_RAM) 00442 PROGMEM 00443 #endif 00444 char usbDescriptorHidReport[]; 00445 00446 extern 00447 #if !(USB_CFG_DESCR_PROPS_STRING_0 & USB_PROP_IS_RAM) 00448 PROGMEM 00449 #endif 00450 char usbDescriptorString0[]; 00451 00452 extern 00453 #if !(USB_CFG_DESCR_PROPS_STRING_VENDOR & USB_PROP_IS_RAM) 00454 PROGMEM 00455 #endif 00456 int usbDescriptorStringVendor[]; 00457 00458 extern 00459 #if !(USB_CFG_DESCR_PROPS_STRING_PRODUCT & USB_PROP_IS_RAM) 00460 PROGMEM 00461 #endif 00462 int usbDescriptorStringDevice[]; 00463 00464 extern 00465 #if !(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER & USB_PROP_IS_RAM) 00466 PROGMEM 00467 #endif 00468 int usbDescriptorStringSerialNumber[]; 00469 00470 #endif /* __ASSEMBLER__ */ 00471 00472 /* ------------------------------------------------------------------------- */ 00473 /* ------------------------ General Purpose Macros ------------------------- */ 00474 /* ------------------------------------------------------------------------- */ 00475 00476 #define USB_CONCAT(a, b) a ## b 00477 #define USB_CONCAT_EXPANDED(a, b) USB_CONCAT(a, b) 00478 00479 #define USB_OUTPORT(name) USB_CONCAT(PORT, name) 00480 #define USB_INPORT(name) USB_CONCAT(PIN, name) 00481 #define USB_DDRPORT(name) USB_CONCAT(DDR, name) 00482 /* The double-define trick above lets us concatenate strings which are 00483 * defined by macros. 00484 */ 00485 00486 /* ------------------------------------------------------------------------- */ 00487 /* ------------------------- Constant definitions -------------------------- */ 00488 /* ------------------------------------------------------------------------- */ 00489 00490 #if !defined __ASSEMBLER__ && (!defined USB_CFG_VENDOR_ID || !defined USB_CFG_DEVICE_ID) 00491 #warning "You should define USB_CFG_VENDOR_ID and USB_CFG_DEVICE_ID in usbconfig.h" 00492 /* If the user has not defined IDs, we default to obdev's free IDs. 00493 * See USBID-License.txt for details. 00494 */ 00495 #endif 00496 00497 /* make sure we have a VID and PID defined, byte order is lowbyte, highbyte */ 00498 #ifndef USB_CFG_VENDOR_ID 00499 # define USB_CFG_VENDOR_ID 0xc0, 0x16 /* 5824 in dec, stands for VOTI */ 00500 #endif 00501 00502 #ifndef USB_CFG_DEVICE_ID 00503 # if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH 00504 # define USB_CFG_DEVICE_ID 0xdf, 0x05 /* 1503 in dec, shared PID for HIDs */ 00505 # elif USB_CFG_INTERFACE_CLASS == 2 00506 # define USB_CFG_DEVICE_ID 0xe1, 0x05 /* 1505 in dec, shared PID for CDC Modems */ 00507 # else 00508 # define USB_CFG_DEVICE_ID 0xdc, 0x05 /* 1500 in dec, obdev's free PID */ 00509 # endif 00510 #endif 00511 00512 /* Derive Output, Input and DataDirection ports from port names */ 00513 #ifndef USB_CFG_IOPORTNAME 00514 #error "You must define USB_CFG_IOPORTNAME in usbconfig.h, see usbconfig-prototype.h" 00515 #endif 00516 00517 #define USBOUT USB_OUTPORT(USB_CFG_IOPORTNAME) 00518 #define USB_PULLUP_OUT USB_OUTPORT(USB_CFG_PULLUP_IOPORTNAME) 00519 #define USBIN USB_INPORT(USB_CFG_IOPORTNAME) 00520 #define USBDDR USB_DDRPORT(USB_CFG_IOPORTNAME) 00521 #define USB_PULLUP_DDR USB_DDRPORT(USB_CFG_PULLUP_IOPORTNAME) 00522 00523 #define USBMINUS USB_CFG_DMINUS_BIT 00524 #define USBPLUS USB_CFG_DPLUS_BIT 00525 #define USBIDLE (1<<USB_CFG_DMINUS_BIT) /* value representing J state */ 00526 #define USBMASK ((1<<USB_CFG_DPLUS_BIT) | (1<<USB_CFG_DMINUS_BIT)) /* mask for USB I/O bits */ 00527 00528 /* defines for backward compatibility with older driver versions: */ 00529 #define USB_CFG_IOPORT USB_OUTPORT(USB_CFG_IOPORTNAME) 00530 #ifdef USB_CFG_PULLUP_IOPORTNAME 00531 #define USB_CFG_PULLUP_IOPORT USB_OUTPORT(USB_CFG_PULLUP_IOPORTNAME) 00532 #endif 00533 00534 #ifndef USB_CFG_EP3_NUMBER /* if not defined in usbconfig.h */ 00535 #define USB_CFG_EP3_NUMBER 3 00536 #endif 00537 00538 #define USB_BUFSIZE 11 /* PID, 8 bytes data, 2 bytes CRC */ 00539 00540 /* ----- Try to find registers and bits responsible for ext interrupt 0 ----- */ 00541 00542 #ifndef USB_INTR_CFG /* allow user to override our default */ 00543 # if defined EICRA 00544 # define USB_INTR_CFG EICRA 00545 # else 00546 # define USB_INTR_CFG MCUCR 00547 # endif 00548 #endif 00549 #ifndef USB_INTR_CFG_SET /* allow user to override our default */ 00550 # define USB_INTR_CFG_SET ((1 << ISC00) | (1 << ISC01)) /* cfg for rising edge */ 00551 #endif 00552 #ifndef USB_INTR_CFG_CLR /* allow user to override our default */ 00553 # define USB_INTR_CFG_CLR 0 /* no bits to clear */ 00554 #endif 00555 00556 #ifndef USB_INTR_ENABLE /* allow user to override our default */ 00557 # if defined GIMSK 00558 # define USB_INTR_ENABLE GIMSK 00559 # elif defined EIMSK 00560 # define USB_INTR_ENABLE EIMSK 00561 # else 00562 # define USB_INTR_ENABLE GICR 00563 # endif 00564 #endif 00565 #ifndef USB_INTR_ENABLE_BIT /* allow user to override our default */ 00566 # define USB_INTR_ENABLE_BIT INT0 00567 #endif 00568 00569 #ifndef USB_INTR_PENDING /* allow user to override our default */ 00570 # if defined EIFR 00571 # define USB_INTR_PENDING EIFR 00572 # else 00573 # define USB_INTR_PENDING GIFR 00574 # endif 00575 #endif 00576 #ifndef USB_INTR_PENDING_BIT /* allow user to override our default */ 00577 # define USB_INTR_PENDING_BIT INTF0 00578 #endif 00579 00580 /* 00581 The defines above don't work for the following chips 00582 at90c8534: no ISC0?, no PORTB, can't find a data sheet 00583 at86rf401: no PORTB, no MCUCR etc, low clock rate 00584 atmega103: no ISC0? (maybe omission in header, can't find data sheet) 00585 atmega603: not defined in avr-libc 00586 at43usb320, at43usb355, at76c711: have USB anyway 00587 at94k: is different... 00588 00589 at90s1200, attiny11, attiny12, attiny15, attiny28: these have no RAM 00590 */ 00591 00592 /* ------------------------------------------------------------------------- */ 00593 /* ----------------- USB Specification Constants and Types ----------------- */ 00594 /* ------------------------------------------------------------------------- */ 00595 00596 /* USB Token values */ 00597 #define USBPID_SETUP 0x2d 00598 #define USBPID_OUT 0xe1 00599 #define USBPID_IN 0x69 00600 #define USBPID_DATA0 0xc3 00601 #define USBPID_DATA1 0x4b 00602 00603 #define USBPID_ACK 0xd2 00604 #define USBPID_NAK 0x5a 00605 #define USBPID_STALL 0x1e 00606 00607 #ifndef USB_INITIAL_DATATOKEN 00608 #define USB_INITIAL_DATATOKEN USBPID_DATA1 00609 #endif 00610 00611 #ifndef __ASSEMBLER__ 00612 00613 extern uchar usbTxBuf1[USB_BUFSIZE], usbTxBuf3[USB_BUFSIZE]; 00614 00615 typedef union usbWord{ 00616 unsigned word; 00617 uchar bytes[2]; 00618 }usbWord_t; 00619 00620 typedef struct usbRequest{ 00621 uchar bmRequestType; 00622 uchar bRequest; 00623 usbWord_t wValue; 00624 usbWord_t wIndex; 00625 usbWord_t wLength; 00626 }usbRequest_t; 00627 /* This structure matches the 8 byte setup request */ 00628 #endif 00629 00630 /* bmRequestType field in USB setup: 00631 * d t t r r r r r, where 00632 * d ..... direction: 0=host->device, 1=device->host 00633 * t ..... type: 0=standard, 1=class, 2=vendor, 3=reserved 00634 * r ..... recipient: 0=device, 1=interface, 2=endpoint, 3=other 00635 */ 00636 00637 /* USB setup recipient values */ 00638 #define USBRQ_RCPT_MASK 0x1f 00639 #define USBRQ_RCPT_DEVICE 0 00640 #define USBRQ_RCPT_INTERFACE 1 00641 #define USBRQ_RCPT_ENDPOINT 2 00642 00643 /* USB request type values */ 00644 #define USBRQ_TYPE_MASK 0x60 00645 #define USBRQ_TYPE_STANDARD (0<<5) 00646 #define USBRQ_TYPE_CLASS (1<<5) 00647 #define USBRQ_TYPE_VENDOR (2<<5) 00648 00649 /* USB direction values: */ 00650 #define USBRQ_DIR_MASK 0x80 00651 #define USBRQ_DIR_HOST_TO_DEVICE (0<<7) 00652 #define USBRQ_DIR_DEVICE_TO_HOST (1<<7) 00653 00654 /* USB Standard Requests */ 00655 #define USBRQ_GET_STATUS 0 00656 #define USBRQ_CLEAR_FEATURE 1 00657 #define USBRQ_SET_FEATURE 3 00658 #define USBRQ_SET_ADDRESS 5 00659 #define USBRQ_GET_DESCRIPTOR 6 00660 #define USBRQ_SET_DESCRIPTOR 7 00661 #define USBRQ_GET_CONFIGURATION 8 00662 #define USBRQ_SET_CONFIGURATION 9 00663 #define USBRQ_GET_INTERFACE 10 00664 #define USBRQ_SET_INTERFACE 11 00665 #define USBRQ_SYNCH_FRAME 12 00666 00667 /* USB descriptor constants */ 00668 #define USBDESCR_DEVICE 1 00669 #define USBDESCR_CONFIG 2 00670 #define USBDESCR_STRING 3 00671 #define USBDESCR_INTERFACE 4 00672 #define USBDESCR_ENDPOINT 5 00673 #define USBDESCR_HID 0x21 00674 #define USBDESCR_HID_REPORT 0x22 00675 #define USBDESCR_HID_PHYS 0x23 00676 00677 #define USBATTR_BUSPOWER 0x80 00678 #define USBATTR_SELFPOWER 0x40 00679 #define USBATTR_REMOTEWAKE 0x20 00680 00681 /* USB HID Requests */ 00682 #define USBRQ_HID_GET_REPORT 0x01 00683 #define USBRQ_HID_GET_IDLE 0x02 00684 #define USBRQ_HID_GET_PROTOCOL 0x03 00685 #define USBRQ_HID_SET_REPORT 0x09 00686 #define USBRQ_HID_SET_IDLE 0x0a 00687 #define USBRQ_HID_SET_PROTOCOL 0x0b 00688 00689 /* ------------------------------------------------------------------------- */ 00690 00691 #endif /* __usbdrv_h_included__ */