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: Proprietary, free under certain conditions. See Documentation. 00008 * This Revision: $Id: usbdrv.h 158 2006-03-14 15:34:17Z 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. Line D- MUST be wired 00020 to bit number 0. D+ must also be connected to INT0. D- requires a pullup of 00021 1.5k to +3.5V (and the device must be powered at 3.5V) to identify as 00022 low-speed USB device. A pullup of 1M SHOULD be connected from D+ to +3.5V to 00023 prevent interference when no USB master is connected. We use D+ as interrupt 00024 source and not D- because it does not trigger on keep-alive and RESET states. 00025 00026 As a compile time option, the 1.5k pullup resistor on D- can be made 00027 switchable to allow the device to disconnect at will. See the definition of 00028 usbDeviceConnect() and usbDeviceDisconnect() further down in this file. 00029 00030 Please adapt the values in usbconfig.h according to your hardware! 00031 00032 The device MUST be clocked at 12 MHz. This is more than the 10 MHz allowed by 00033 an AT90S2313 powered at 4.5V. However, if the supply voltage to maximum clock 00034 relation is interpolated linearly, an ATtiny2313 meets the requirement by 00035 specification. In practice, the AT90S2313 can be overclocked and works well. 00036 00037 00038 Limitations: 00039 ============ 00040 Compiling: 00041 You should link the usbdrv.o module first because it has special alignment 00042 requirements for the receive buffer (the buffer must not cross a 256 byte 00043 page boundary, it must not even touch it at the end). If you can't link it 00044 first, you must use other measures to ensure alignment. 00045 Note: gcc does not always assign variable addresses in the order as the modules 00046 are linked or the variables are declared. You can choose a memory section for 00047 the receive buffer with the configuration option "USB_BUFFER_SECTION". This 00048 option defaults to ".bss". If you use your own section, you can place it at 00049 an arbitrary location with a linker option similar to 00050 "-Wl,--section-start=.mybuffer=0x800060". Use "avr-nm -ng" on the binary and 00051 search for "usbRxBuf" to find tbe base address of the 22 bytes rx buffer. 00052 00053 Robustness with respect to communication errors: 00054 The driver assumes error-free communication. It DOES check for errors in 00055 the PID, but does NOT check bit stuffing errors, SE0 in middle of a byte, 00056 token CRC (5 bit) and data CRC (16 bit). CRC checks can not be performed due 00057 to timing constraints: We must start sending a reply within 7 bit times. 00058 Bit stuffing and misplaced SE0 would have to be checked in real-time, but CPU 00059 performance does not permit that. The driver does not check Data0/Data1 00060 toggling, but application software can implement the check. 00061 00062 Sampling jitter: 00063 The driver guarantees a sampling window of 1/2 bit. The USB spec requires 00064 that the receiver has at most 1/4 bit sampling window. The 1/2 bit window 00065 should still work reliably enough because we work at low speed. If you want 00066 to meet the spec, define the macro "USB_CFG_SAMPLE_EXACT" to 1 in usbconfig.h. 00067 This will unroll a loop which results in bigger code size. 00068 00069 Input characteristics: 00070 Since no differential receiver circuit is used, electrical interference 00071 robustness may suffer. The driver samples only one of the data lines with 00072 an ordinary I/O pin's input characteristics. However, since this is only a 00073 low speed USB implementation and the specification allows for 8 times the 00074 bit rate over the same hardware, we should be on the safe side. Even the spec 00075 requires detection of asymmetric states at high bit rate for SE0 detection. 00076 00077 Number of endpoints: 00078 The driver supports up to two endpoints: One control endpoint (endpoint 0) and 00079 one interrupt-in endpoint (endpoint 1) where the device can send interrupt 00080 data to the host. Endpoint 1 is only compiled in if 00081 USB_CFG_HAVE_INTRIN_ENDPOINT is defined to 1 in usbconfig.h. 00082 00083 Maximum data payload: 00084 Data payload of control in and out transfers may be up to 254 bytes. In order 00085 to accept payload data of out transfers, you need to implement 00086 'usbFunctionWrite()'. 00087 00088 USB Suspend Mode supply current: 00089 The USB standard limits power consumption to 500uA when the bus is in suspend 00090 mode. This is not a problem for self-powered devices since they don't need 00091 bus power anyway. Bus-powered devices can achieve this only by putting the 00092 CPU in sleep mode. The driver does not implement suspend handling by itself. 00093 However, the application may implement activity monitoring and wakeup from 00094 sleep. The host sends regular SE0 states on the bus to keep it active. These 00095 SE0 states can be detected by wiring the INT1 pin to D+. It is not necessary 00096 to enable the interrupt, checking the interrupt pending flag should suffice. 00097 Before entering sleep mode, the application should enable INT1 for a wakeup 00098 on the next bus activity. 00099 00100 Operation without an USB master: 00101 The driver behaves neutral without connection to an USB master if D- reads 00102 as 1. To avoid spurious interrupts, we recommend a high impedance (e.g. 1M) 00103 pullup resistor on D+. If D- becomes statically 0, the driver may block in 00104 the interrupt routine. 00105 00106 Interrupt latency: 00107 The application must ensure that the USB interrupt is not disabled for more 00108 than 20 cycles. This implies that all interrupt routines must either be 00109 declared as "INTERRUPT" instead of "SIGNAL" (see "avr/signal.h") or that they 00110 are written in assembler 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 = 100us if the host 00116 conforms to the standard. The driver will consume CPU cycles for all USB 00117 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 20060314 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 #ifndef __ASSEMBLER__ 00134 00135 #ifndef uchar 00136 #define uchar unsigned char 00137 #endif 00138 #ifndef schar 00139 #define schar signed char 00140 #endif 00141 /* shortcuts for well defined 8 bit integer types */ 00142 00143 extern void usbInit(void); 00144 /* This function must be called before interrupts are enabled and the main 00145 * loop is entered. 00146 */ 00147 extern void usbPoll(void); 00148 /* This function must be called at regular intervals from the main loop. 00149 * Maximum delay between calls is somewhat less than 50ms (USB timeout for 00150 * accepting a Setup message). Otherwise the device will not be recognized. 00151 * Please note that debug outputs through the UART take ~ 0.5ms per byte 00152 * at 19200 bps. 00153 */ 00154 extern uchar *usbMsgPtr; 00155 /* This variable may be used to pass transmit data to the driver from the 00156 * implementation of usbFunctionWrite(). It is also used internally by the 00157 * driver for standard control requests. 00158 */ 00159 extern uchar usbFunctionSetup(uchar data[8]); 00160 /* This function is called when the driver receives a SETUP transaction from 00161 * the host which is not answered by the driver itself (in practice: class and 00162 * vendor requests). All control transfers start with a SETUP transaction where 00163 * the host communicates the parameters of the following (optional) data 00164 * transfer. The SETUP data is available in the 'data' parameter which can 00165 * (and should) be casted to 'usbRequest_t *' for a more user-friendly access 00166 * to parameters. 00167 * 00168 * If the SETUP indicates a control-in transfer, you should provide the 00169 * requested data to the driver. There are two ways to transfer this data: 00170 * (1) Set the global pointer 'usbMsgPtr' to the base of the static RAM data 00171 * block and return the length of the data in 'usbFunctionSetup()'. The driver 00172 * will handle the rest. Or (2) return 0xff in 'usbFunctionSetup()'. The driver 00173 * will then call 'usbFunctionRead()' when data is needed. See the 00174 * documentation for usbFunctionRead() for details. 00175 * 00176 * If the SETUP indicates a control-out transfer, the only way to receive the 00177 * data from the host is through the 'usbFunctionWrite()' call. If you 00178 * implement this function, you must return 0xff in 'usbFunctionSetup()' to 00179 * indicate that 'usbFunctionWrite()' should be used. See the documentation of 00180 * this function for more information. If you just want to ignore the data sent 00181 * by the host, return 0 in 'usbFunctionSetup()'. 00182 * 00183 * Note that calls to the functions usbFunctionRead() and usbFunctionWrite() 00184 * are only done if enabled by the configuration in usbconfig.h. 00185 */ 00186 #if USB_CFG_HAVE_INTRIN_ENDPOINT 00187 void usbSetInterrupt(uchar *data, uchar len); 00188 /* This function sets the message which will be sent during the next interrupt 00189 * IN transfer. The message is copied to an internal buffer and must not exceed 00190 * a length of 8 bytes. The message may be 0 bytes long just to indicate the 00191 * interrupt status to the host. 00192 * If you need to transfer more bytes, use a control read after the interrupt. 00193 */ 00194 extern volatile schar usbTxLen1; 00195 #define usbInterruptIsReady() (usbTxLen1 == -1) 00196 /* This macro indicates whether the last interrupt message has already been 00197 * sent. If you set a new interrupt message before the old was sent, the 00198 * message already buffered will be lost. 00199 */ 00200 #endif /* USB_CFG_HAVE_INTRIN_ENDPOINT */ 00201 #if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH 00202 extern PROGMEM const char usbHidReportDescriptor[]; 00203 /* If you implement an HID device, you need to provide a report descriptor. 00204 * The HID report descriptor syntax is a bit complex. If you understand how 00205 * report descriptors are constructed, we recommend that you use the HID 00206 * Descriptor Tool from usb.org, see http://www.usb.org/developers/hidpage/. 00207 * Otherwise you should probably start with a working example. 00208 */ 00209 #endif /* USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH */ 00210 #if USB_CFG_IMPLEMENT_FN_WRITE 00211 extern uchar usbFunctionWrite(uchar *data, uchar len); 00212 /* This function is called by the driver to provide a control transfer's 00213 * payload data (control-out). It is called in chunks of up to 8 bytes. The 00214 * total count provided in the current control transfer can be obtained from 00215 * the 'length' property in the setup data. If an error occurred during 00216 * processing, return 0xff (== -1). The driver will answer the entire transfer 00217 * with a STALL token in this case. If you have received the entire payload 00218 * successfully, return 1. If you expect more data, return 0. If you don't 00219 * know whether the host will send more data (you should know, the total is 00220 * provided in the usbFunctionSetup() call!), return 1. 00221 * NOTE: If you return 0xff for STALL, 'usbFunctionWrite()' may still be called 00222 * for the remaining data. You must continue to return 0xff for STALL in these 00223 * calls. 00224 * In order to get usbFunctionWrite() called, define USB_CFG_IMPLEMENT_FN_WRITE 00225 * to 1 in usbconfig.h and return 0xff in usbFunctionSetup().. 00226 */ 00227 #endif /* USB_CFG_IMPLEMENT_FN_WRITE */ 00228 #if USB_CFG_IMPLEMENT_FN_READ 00229 extern uchar usbFunctionRead(uchar *data, uchar len); 00230 /* This function is called by the driver to ask the application for a control 00231 * transfer's payload data (control-in). It is called in chunks of up to 8 00232 * bytes each. You should copy the data to the location given by 'data' and 00233 * return the actual number of bytes copied. If you return less than requested, 00234 * the control-in transfer is terminated. If you return 0xff, the driver aborts 00235 * the transfer with a STALL token. 00236 * In order to get usbFunctionRead() called, define USB_CFG_IMPLEMENT_FN_READ 00237 * to 1 in usbconfig.h and return 0xff in usbFunctionSetup().. 00238 */ 00239 #endif /* USB_CFG_IMPLEMENT_FN_READ */ 00240 #ifdef USB_CFG_PULLUP_IOPORT 00241 #define usbDeviceConnect() ((USB_PULLUP_DDR |= (1<<USB_CFG_PULLUP_BIT)), \ 00242 (USB_PULLUP_OUT |= (1<<USB_CFG_PULLUP_BIT))) 00243 /* This macro (intended to look like a function) connects the device to the 00244 * USB bus. It is only available if you have defined the constants 00245 * USB_CFG_PULLUP_IOPORT and USB_CFG_PULLUP_BIT in usbconfig.h. 00246 */ 00247 #define usbDeviceDisconnect() (USB_PULLUP_OUT &= ~(1<<USB_CFG_PULLUP_BIT)) 00248 /* This macro (intended to look like a function) disconnects the device from 00249 * the USB bus. It is only available if you have defined the constants 00250 * USB_CFG_PULLUP_IOPORT and USB_CFG_PULLUP_BIT in usbconfig.h. 00251 */ 00252 #endif /* USB_CFG_PULLUP_IOPORT */ 00253 extern unsigned usbCrc16(uchar *data, uchar len); 00254 /* This function calculates the binary complement of the data CRC used in 00255 * USB data packets. The value is used to build raw transmit packets. 00256 * You may want to use this function for data checksums or to verify received 00257 * data. 00258 */ 00259 extern unsigned usbCrc16Append(unsigned char *data, unsigned char len); 00260 /* This function is equivalent to usbCrc16() above, except that it appends 00261 * the 2 bytes CRC (lowbyte first) in the 'data' buffer after reading 'len' 00262 * bytes. 00263 */ 00264 extern uchar usbConfiguration; 00265 /* This value contains the current configuration set by the host. The driver 00266 * allows setting and querying of this variable with the USB SET_CONFIGURATION 00267 * and GET_CONFIGURATION requests, but does not use it otherwise. 00268 * You may want to reflect the "configured" status with a LED on the device or 00269 * switch on high power parts of the circuit only if the device is configured. 00270 */ 00271 #define USB_STRING_DESCRIPTOR_HEADER(stringLength) ((2*(stringLength)+2) | (3<<8)) 00272 /* This macro builds a descriptor header for a string descriptor given the 00273 * string's length. See usbdrv.c for an example how to use it. 00274 */ 00275 #if USB_CFG_SERIAL_NUMBER_LENGTH 00276 extern PROGMEM int usbCfgSerialNumberStringDescriptor[]; 00277 /* This array of unicode characters (prefixed by a string descriptor header as 00278 * explained above) represents the serial number of the device. 00279 */ 00280 #endif 00281 00282 #endif /* __ASSEMBLER__ */ 00283 00284 /* ------------------------------------------------------------------------- */ 00285 /* ------------------------- Constant definitions -------------------------- */ 00286 /* ------------------------------------------------------------------------- */ 00287 00288 #if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH && (!defined USB_CFG_VENDOR_ID || !defined USB_CFG_DEVICE_ID) 00289 #error "You MUST NOT use obdev's shared VID/PID with HID class devices!" 00290 /* The shared VID/PID must be used in conjunction with libusb (see license for 00291 * the IDs). This contradicts HID usage (at least on Windows). 00292 */ 00293 #endif 00294 00295 /* make sure we have a VID and PID defined, byte order is lowbyte, highbyte */ 00296 #ifndef USB_CFG_VENDOR_ID 00297 # define USB_CFG_VENDOR_ID 0xc0, 0x16 /* 5824 in dec, stands for VOTI */ 00298 #endif 00299 00300 #ifndef USB_CFG_DEVICE_ID 00301 # define USB_CFG_DEVICE_ID 0xdc, 0x05 /* 1500 in dec, obdev's free PID */ 00302 #endif 00303 00304 #ifndef USB_BUFFER_SECTION 00305 # define USB_BUFFER_SECTION ".bss" /* if user has not selected a named section */ 00306 #endif 00307 00308 /* I/O definitions for assembler module */ 00309 #define USBOUT USB_CFG_IOPORT /* output port for USB bits */ 00310 #define USB_PULLUP_OUT USB_CFG_PULLUP_IOPORT 00311 #ifdef __ASSEMBLER__ 00312 /* the following two lines must start in column 0 for IAR assembler */ 00313 USBIN = (USB_CFG_IOPORT - 2) /* input port for USB bits */ 00314 USBDDR = (USB_CFG_IOPORT - 1) /* data direction for USB bits */ 00315 #else 00316 #define USBIN (*(&USB_CFG_IOPORT - 2)) /* input port for USB bits */ 00317 #define USBDDR (*(&USB_CFG_IOPORT - 1)) /* data direction for USB bits */ 00318 #define USB_PULLUP_DDR (*(&USB_CFG_PULLUP_IOPORT - 1)) 00319 #endif 00320 #if USB_CFG_DMINUS_BIT != 0 00321 # error "USB_CFG_DMINUS_BIT MUST be 0!" 00322 #endif 00323 #define USBMINUS 0 /* D- MUST be on bit 0 */ 00324 #define USBIDLE 0x01 /* value representing J state */ 00325 #define USBMASK ((1<<USB_CFG_DPLUS_BIT) | 1) /* mask for USB I/O bits */ 00326 00327 #define USB_BUFSIZE 11 /* PID, 8 bytes data, 2 bytes CRC */ 00328 00329 /* Try to find registers and bits responsible for ext interrupt 0 */ 00330 00331 #if defined EICRA 00332 # define USB_INTR_CFG EICRA 00333 #else 00334 # define USB_INTR_CFG MCUCR 00335 #endif 00336 #define USB_INTR_CFG_SET ((1 << ISC00) | (1 << ISC01)) /* cfg for rising edge */ 00337 #define USB_INTR_CFG_CLR 0 /* no bits to clear */ 00338 00339 #if defined GIMSK 00340 # define USB_INTR_ENABLE GIMSK 00341 #elif defined EIMSK 00342 # define USB_INTR_ENABLE EIMSK 00343 #else 00344 # define USB_INTR_ENABLE GICR 00345 #endif 00346 #define USB_INTR_ENABLE_BIT INT0 00347 00348 #if defined EIFR 00349 # define USB_INTR_PENDING EIFR 00350 #else 00351 # define USB_INTR_PENDING GIFR 00352 #endif 00353 #define USB_INTR_PENDING_BIT INTF0 00354 00355 /* 00356 The defines above don't work for the following chips 00357 at90c8534: no ISC0?, no PORTB, can't find a data sheet 00358 at86rf401: no PORTB, no MCUCR etc, low clock rate 00359 atmega103: no ISC0? (maybe omission in header, can't find data sheet) 00360 atmega603: not defined in avr-libc 00361 at43usb320, at43usb355, at76c711: have USB anyway 00362 at94k: is different... 00363 00364 at90s1200, attiny11, attiny12, attiny15, attiny28: these have no RAM 00365 */ 00366 00367 /* ------------------------------------------------------------------------- */ 00368 /* ----------------- USB Specification Constants and Types ----------------- */ 00369 /* ------------------------------------------------------------------------- */ 00370 00371 /* USB Token values */ 00372 #define USBPID_SETUP 0x2d 00373 #define USBPID_OUT 0xe1 00374 #define USBPID_IN 0x69 00375 #define USBPID_DATA0 0xc3 00376 #define USBPID_DATA1 0x4b 00377 00378 #define USBPID_ACK 0xd2 00379 #define USBPID_NAK 0x5a 00380 #define USBPID_STALL 0x1e 00381 00382 #ifndef __ASSEMBLER__ 00383 typedef union usbWord{ 00384 unsigned word; 00385 uchar bytes[2]; 00386 }usbWord_t; 00387 00388 typedef struct usbRequest{ 00389 uchar bmRequestType; 00390 uchar bRequest; 00391 usbWord_t wValue; 00392 usbWord_t wIndex; 00393 usbWord_t wLength; 00394 }usbRequest_t; 00395 /* This structure matches the 8 byte setup request */ 00396 #endif 00397 00398 /* bmRequestType field in USB setup: 00399 * d t t r r r r r, where 00400 * d ..... direction: 0=host->device, 1=device->host 00401 * t ..... type: 0=standard, 1=class, 2=vendor, 3=reserved 00402 * r ..... recipient: 0=device, 1=interface, 2=endpoint, 3=other 00403 */ 00404 00405 /* USB setup recipient values */ 00406 #define USBRQ_RCPT_MASK 0x1f 00407 #define USBRQ_RCPT_DEVICE 0 00408 #define USBRQ_RCPT_INTERFACE 1 00409 #define USBRQ_RCPT_ENDPOINT 2 00410 00411 /* USB request type values */ 00412 #define USBRQ_TYPE_MASK 0x60 00413 #define USBRQ_TYPE_STANDARD (0<<5) 00414 #define USBRQ_TYPE_CLASS (1<<5) 00415 #define USBRQ_TYPE_VENDOR (2<<5) 00416 00417 /* USB direction values: */ 00418 #define USBRQ_DIR_MASK 0x80 00419 #define USBRQ_DIR_HOST_TO_DEVICE (0<<7) 00420 #define USBRQ_DIR_DEVICE_TO_HOST (1<<7) 00421 00422 /* USB Standard Requests */ 00423 #define USBRQ_GET_STATUS 0 00424 #define USBRQ_CLEAR_FEATURE 1 00425 #define USBRQ_SET_FEATURE 3 00426 #define USBRQ_SET_ADDRESS 5 00427 #define USBRQ_GET_DESCRIPTOR 6 00428 #define USBRQ_SET_DESCRIPTOR 7 00429 #define USBRQ_GET_CONFIGURATION 8 00430 #define USBRQ_SET_CONFIGURATION 9 00431 #define USBRQ_GET_INTERFACE 10 00432 #define USBRQ_SET_INTERFACE 11 00433 #define USBRQ_SYNCH_FRAME 12 00434 00435 /* USB descriptor constants */ 00436 #define USBDESCR_DEVICE 1 00437 #define USBDESCR_CONFIG 2 00438 #define USBDESCR_STRING 3 00439 #define USBDESCR_INTERFACE 4 00440 #define USBDESCR_ENDPOINT 5 00441 #define USBDESCR_HID 0x21 00442 #define USBDESCR_HID_REPORT 0x22 00443 #define USBDESCR_HID_PHYS 0x23 00444 00445 #define USBATTR_BUSPOWER 0x80 00446 #define USBATTR_SELFPOWER 0x40 00447 #define USBATTR_REMOTEWAKE 0x20 00448 00449 /* USB HID Requests */ 00450 #define USBRQ_HID_GET_REPORT 0x01 00451 #define USBRQ_HID_GET_IDLE 0x02 00452 #define USBRQ_HID_GET_PROTOCOL 0x03 00453 #define USBRQ_HID_SET_REPORT 0x09 00454 #define USBRQ_HID_SET_IDLE 0x0a 00455 #define USBRQ_HID_SET_PROTOCOL 0x0b 00456 00457 /* ------------------------------------------------------------------------- */ 00458 00459 #endif /* __usbdrv_h_included__ */