66be5ed2ed7e68c72776f66c2c314e38dc07b228
2 * Project: AVR USB driver
3 * Author: Christian Starkjohann
4 * Creation Date: 2004-12-29
6 * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH
7 * License: Proprietary, free under certain conditions. See Documentation.
8 * This Revision: $Id: usbdrv.h 52 2005-04-12 16:57:29Z cs $
11 #ifndef __usbdrv_h_included__
12 #define __usbdrv_h_included__
13 #include "usbconfig.h"
16 Hardware Prerequisites:
17 =======================
18 USB lines D+ and D- MUST be wired to the same I/O port. Line D- MUST be wired
19 to bit number 0. D+ must also be connected to INT0. D- requires a pullup of
20 1.5k to +3.5V (and the device must be powered at 3.5V) to identify as
21 low-speed USB device. A pullup of 1M SHOULD be connected from D+ to +3.5V to
22 prevent interference when no USB master is connected. We use D+ as interrupt
23 source and not D- because it does not trigger on keep-alive and RESET states.
25 Please adapt the values in usbconfig.h according to your hardware!
27 The device MUST be clocked at 12 MHz. This is more than the 10 MHz allowed by
28 an AT90S2313 powered at 4.5V. However, if the supply voltage to maximum clock
29 relation is interpolated linearly, an ATtiny2313 meets the requirement by
30 specification. In practice, the AT90S2313 can be overclocked and works well.
36 The bss segment of the driver must be in the first 256 bytes of the address
37 space because byte wide variables are used as pointers for efficiency reasons.
38 This is not a problem on devices with 128 byte RAM since the entire RAM
39 meets this condition. For larger devices please link usbdrv first.
41 Robustness with respect to communication errors:
42 The driver assumes error-free communication. It DOES check for errors in
43 the PID, but does NOT check bit stuffing errors, SE0 in middle of a byte,
44 token CRC (5 bit) and data CRC (16 bit). CRC checks can not be performed due
45 to timing constraints: We must start sending a reply within 7 bit times.
46 Bit stuffing and misplaced SE0 would have to be checked in real-time, but CPU
47 performance does not permit that. The driver does not check Data0/Data1
48 toggling, but application software can implement the check.
51 The driver guarantees a sampling window of 1/2 bit. The USB spec requires
52 that the receiver has at most 1/4 bit sampling window. The 1/2 bit window
53 should still work reliably enough because we work at low speed. If you want
54 to meet the spec, define the macro "USB_CFG_SAMPLE_EXACT" to 1 in usbconfig.h.
55 This will unroll a loop which results in bigger code size.
57 Input characteristics:
58 Since no differential receiver circuit is used, electrical interference
59 robustness may suffer. The driver samples only one of the data lines with
60 an ordinary I/O pin's input characteristics. However, since this is only a
61 low speed USB implementation and the specification allows for 8 times the
62 bit rate over the same hardware, we should be on the safe side. Even the spec
63 requires detection of asymmetric states at high bit rate for SE0 detection.
66 The driver supports up to two endpoints: One control endpoint (endpoint 0) and
67 one interrupt-in endpoint (endpoint 1) where the device can send interrupt
68 data to the host. Endpoint 1 is only compiled in if
69 USB_CFG_HAVE_INTRIN_ENDPOINT is defined to 1 in usbconfig.h.
72 Data payload of control in and out transfers may be up to 255 bytes. In order
73 to accept payload data of out transfers, you need to implement
76 USB Suspend Mode supply current:
77 The USB standard limits power consumption to 500uA when the bus is in suspend
78 mode. This is not a problem for self-powered devices since they don't need
79 bus power anyway. Bus-powered devices can achieve this only by putting the
80 CPU in sleep mode. The driver does not implement suspend handling by itself.
81 However, the application may implement activity monitoring and wakeup from
82 sleep. The host sends regular SE0 states on the bus to keep it active. These
83 SE0 states can be detected by wiring the INT1 pin to D+. It is not necessary
84 to enable the interrupt, checking the interrupt pending flag should suffice.
85 Before entering sleep mode, the application should enable INT1 for a wakeup
86 on the next bus activity.
88 Operation without an USB master:
89 The driver behaves neutral without connection to an USB master if D- reads
90 as 1. To avoid spurious interrupts, we recommend a high impedance (e.g. 1M)
91 pullup resistor on D+. If D- becomes statically 0, the driver may block in
92 the interrupt routine.
95 The application must ensure that the USB interrupt is not disabled for more
98 Maximum interrupt duration / CPU cycle consumption:
99 The driver handles all USB communication during the interrupt service
100 routine. The routine will not return before an entire USB message is received
101 and the reply is sent. This may be up to ca. 1200 cycles = 100us if the host
102 conforms to the standard. The driver will consume CPU cycles for all USB
103 messages, even if they address an other (low-speed) device on the same bus.
107 /* ------------------------------------------------------------------------- */
108 /* --------------------------- Module Interface ---------------------------- */
109 /* ------------------------------------------------------------------------- */
111 #ifndef __ASSEMBLER__
114 #define uchar unsigned char
117 #if USB_CFG_HAVE_INTRIN_ENDPOINT
118 void usbSetInterrupt(uchar
*data
, uchar len
);
119 /* This function sets the message which will be sent during the next interrupt
120 * IN transfer. The message is copied to an internal buffer and must not exceed
121 * a length of 7 bytes. The message may be 0 bytes long just to indicate the
122 * interrupt status to the host.
123 * If you need to transfer more bytes, use a control read after the interrupt.
125 #endif /* USB_CFG_HAVE_INTRIN_ENDPOINT */
127 extern void usbInit(void);
128 /* This function must be called before interrupts are enabled and the main
131 extern void usbPoll(void);
132 /* This function must be called at regular intervals from the main loop.
133 * Maximum delay between calls is somewhat less than 50ms (USB timeout between
134 * packages of a message).
136 extern uchar
*usbMsgPtr
;
137 /* This variable may be used to pass transmit data to the driver from the
138 * implementation of usbFunctionWrite(). It is also used internally by the
139 * driver for standard control requests.
141 extern uchar
usbFunctionSetup(uchar data
[8]);
142 /* This function is called for all setup requests which are not of type
143 * "Standard" (in practice: class and vendor requests). The 8 bytes setup
144 * data is passed in 'data'. Data for control-out transfers is passed to the
145 * application in separate calls to usbFunctionWrite() (unless you have turned
146 * this option off). You should store the setup context in global/static
147 * variables to have it available in usbFunctionWrite(). Data for control-in
148 * transfers can be provided in two ways: (1) immediately as a result of
149 * usbFunctionSetup() or (2) on demand of the driver in calls to the separate
150 * function usbFunctionRead() (if enabled). For (1) write the data to a static
151 * buffer, set the global variable 'usbMsgPtr' to this buffer and return the
152 * data length (may be 0). To implement (2), simply return 0xff (== -1) in
153 * usbFunctionSetup(). The driver will call usbFunctionRead() when data is
154 * needed. You may use 'usbMsgPtr' to save your own status in this case.
155 * The data passed in 'data' has the following content (see USB 1.1 spec):
156 * struct usbControlData{
157 * uchar requestType; //[0]
158 * uchar request; //[1]
159 * unsigned value; //[2], [3]
160 * unsigned index; //[4], [5]
161 * unsigned length; //[6], [7]
164 #if USB_CFG_IMPLEMENT_FN_WRITE
165 extern uchar
usbFunctionWrite(uchar
*data
, uchar len
);
166 /* This function is called by the driver to provide a control transfer's
167 * payload data (control-out). It is called in chunks of up to 8 bytes. The
168 * total count provided in the current control transfer can be obtained from
169 * the 'length' property in the setup data. If an error occurred during
170 * processing, return 0xff (== -1). The driver will answer the entire transfer
171 * with a STALL token in this case. Otherwise return any number which is not
172 * 0xff. NOTE: Only the return value of the LAST usbFunctionWrite() call
173 * (the one immediately before the status phase) is used.
175 #endif /* USB_CFG_IMPLEMENT_FN_WRITE */
176 #if USB_CFG_IMPLEMENT_FN_READ
177 extern uchar
usbFunctionRead(uchar
*data
, uchar len
);
178 /* This function is called by the driver to ask the application for a control
179 * transfer's payload data (control-in). You should supply up to 'len' bytes of
180 * data in this chunk. 'len' will be 8 bytes for all but the last chunk. If
181 * you return less than 8 bytes, the control transfer ends. If you return an
182 * invalid value (e.g. -1), the driver sends a STALL token.
184 #endif /* USB_CFG_IMPLEMENT_FN_READ */
185 extern unsigned usbCrc16(uchar
*data
, uchar len
);
186 /* This function calculates the binary complement of the data CRC used in
187 * USB data packets. The value is used to build raw transmit packets.
188 * You may want to use this function for data checksums.
191 #endif /* __ASSEMBLER__ */
193 /* ------------------------------------------------------------------------- */
194 /* ------------------------- Constant definitions -------------------------- */
195 /* ------------------------------------------------------------------------- */
197 /* I/O definitions for assembler module */
198 #define USBOUT USB_CFG_IOPORT /* output port for USB bits */
200 #define USBIN (USB_CFG_IOPORT - 2) /* input port for USB bits */
201 #define USBDDR (USB_CFG_IOPORT - 1) /* data direction for USB bits */
203 #define USBIN (*(&USB_CFG_IOPORT - 2)) /* input port for USB bits */
204 #define USBDDR (*(&USB_CFG_IOPORT - 1)) /* data direction for USB bits */
206 #if USB_CFG_DMINUS_BIT != 0
207 # error "USB_CFG_DMINUS_BIT MUST be 0!"
209 #define USBMINUS 0 /* D- MUST be on bit 0 */
210 #define USBIDLE 0x01 /* value representing J state */
211 #define USBMASK ((1<<USB_CFG_DPLUS_BIT) | 1) /* mask for USB I/O bits */
213 #define USB_BUFSIZE 11 /* PID, 8 bytes data, 2 bytes CRC */
215 /* Try to find registers and bits responsible for ext interrupt 0 */
218 # define USB_INTR_CFG EICRA
220 # define USB_INTR_CFG MCUCR
222 #define USB_INTR_CFG_SET ((1 << ISC00) | (1 << ISC01)) /* cfg for rising edge */
223 #define USB_INTR_CFG_CLR 0 /* no bits to clear */
226 # define USB_INTR_ENABLE GIMSK
228 # define USB_INTR_ENABLE EIMSK
230 # define USB_INTR_ENABLE GICR
232 #define USB_INTR_ENABLE_BIT INT0
235 # define USB_INTR_PENDING EIFR
237 # define USB_INTR_PENDING GIFR
239 #define USB_INTR_PENDING_BIT INTF0
242 The defines above don't work for the following chips
243 at90c8534: no ISC0?, no PORTB, can't find a data sheet
244 at86rf401: no PORTB, no MCUCR etc
245 atmega103: no ISC0? (maybe omission in header, can't find data sheet)
246 atmega603: not defined in avr-libc
247 at43usb320, at43usb355, at76c711: have USB anyway
248 at94k: is different...
250 at90s1200, attiny11, attiny12, attiny15, attiny28: these have no RAM
253 /* ------------------------------------------------------------------------- */
254 /* ---------------------- USB Specification Constants ---------------------- */
255 /* ------------------------------------------------------------------------- */
257 /* USB Token values */
258 #define USBPID_SETUP 0x2d
259 #define USBPID_OUT 0xe1
260 #define USBPID_IN 0x69
261 #define USBPID_DATA0 0xc3
262 #define USBPID_DATA1 0x4b
264 #define USBPID_ACK 0xd2
265 #define USBPID_NAK 0x5a
266 #define USBPID_STALL 0x1e
268 /* USB descriptor constants */
269 #define USBATTR_BUSPOWER 0x80
270 #define USBATTR_SELFPOWER 0x40
271 #define USBATTR_REMOTEWAKE 0x20
273 /* USB setup recipient values */
274 #define USBRQ_RCPT_DEVICE 0
275 #define USBRQ_RCPT_INTERFACE 1
276 #define USBRQ_RCPT_ENDPOINT 2
278 /* USB request type values */
279 #define USBRQ_TYPE_STANDARD 0
280 #define USBRQ_TYPE_CLASS 1
281 #define USBRQ_TYPE_VENDOR 2
284 /* ------------------------------------------------------------------------- */
286 #endif /* __usbdrv_h_included__ */