usbdrv.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /* Name: usbdrv.h
  2. * Project: V-USB, virtual USB port for Atmel's(r) AVR(r) microcontrollers
  3. * Author: Christian Starkjohann
  4. * Creation Date: 2004-12-29
  5. * Tabsize: 4
  6. * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH
  7. * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
  8. */
  9. #ifndef __usbdrv_h_included__
  10. #define __usbdrv_h_included__
  11. #include "usbconfig.h"
  12. #include "usbportability.h"
  13. /*
  14. Hardware Prerequisites:
  15. =======================
  16. USB lines D+ and D- MUST be wired to the same I/O port. We recommend that D+
  17. triggers the interrupt (best achieved by using INT0 for D+), but it is also
  18. possible to trigger the interrupt from D-. If D- is used, interrupts are also
  19. triggered by SOF packets. D- requires a pull-up of 1.5k to +3.5V (and the
  20. device must be powered at 3.5V) to identify as low-speed USB device. A
  21. pull-down or pull-up of 1M SHOULD be connected from D+ to +3.5V to prevent
  22. interference when no USB master is connected. If you use Zener diodes to limit
  23. the voltage on D+ and D-, you MUST use a pull-down resistor, not a pull-up.
  24. We use D+ as interrupt source and not D- because it does not trigger on
  25. keep-alive and RESET states. If you want to count keep-alive events with
  26. USB_COUNT_SOF, you MUST use D- as an interrupt source.
  27. As a compile time option, the 1.5k pull-up resistor on D- can be made
  28. switchable to allow the device to disconnect at will. See the definition of
  29. usbDeviceConnect() and usbDeviceDisconnect() further down in this file.
  30. Please adapt the values in usbconfig.h according to your hardware!
  31. The device MUST be clocked at exactly 12 MHz, 15 MHz, 16 MHz or 20 MHz
  32. or at 12.8 MHz resp. 16.5 MHz +/- 1%. See usbconfig-prototype.h for details.
  33. Limitations:
  34. ============
  35. Robustness with respect to communication errors:
  36. The driver assumes error-free communication. It DOES check for errors in
  37. the PID, but does NOT check bit stuffing errors, SE0 in middle of a byte,
  38. token CRC (5 bit) and data CRC (16 bit). CRC checks can not be performed due
  39. to timing constraints: We must start sending a reply within 7 bit times.
  40. Bit stuffing and misplaced SE0 would have to be checked in real-time, but CPU
  41. performance does not permit that. The driver does not check Data0/Data1
  42. toggling, but application software can implement the check.
  43. Input characteristics:
  44. Since no differential receiver circuit is used, electrical interference
  45. robustness may suffer. The driver samples only one of the data lines with
  46. an ordinary I/O pin's input characteristics. However, since this is only a
  47. low speed USB implementation and the specification allows for 8 times the
  48. bit rate over the same hardware, we should be on the safe side. Even the spec
  49. requires detection of asymmetric states at high bit rate for SE0 detection.
  50. Number of endpoints:
  51. The driver supports the following endpoints:
  52. - Endpoint 0, the default control endpoint.
  53. - Any number of interrupt- or bulk-out endpoints. The data is sent to
  54. usbFunctionWriteOut() and USB_CFG_IMPLEMENT_FN_WRITEOUT must be defined
  55. to 1 to activate this feature. The endpoint number can be found in the
  56. global variable 'usbRxToken'.
  57. - One default interrupt- or bulk-in endpoint. This endpoint is used for
  58. interrupt- or bulk-in transfers which are not handled by any other endpoint.
  59. You must define USB_CFG_HAVE_INTRIN_ENDPOINT in order to activate this
  60. feature and call usbSetInterrupt() to send interrupt/bulk data.
  61. - One additional interrupt- or bulk-in endpoint. This was endpoint 3 in
  62. previous versions of this driver but can now be configured to any endpoint
  63. number. You must define USB_CFG_HAVE_INTRIN_ENDPOINT3 in order to activate
  64. this feature and call usbSetInterrupt3() to send interrupt/bulk data. The
  65. endpoint number can be set with USB_CFG_EP3_NUMBER.
  66. Please note that the USB standard forbids bulk endpoints for low speed devices!
  67. Most operating systems allow them anyway, but the AVR will spend 90% of the CPU
  68. time in the USB interrupt polling for bulk data.
  69. Maximum data payload:
  70. Data payload of control in and out transfers may be up to 254 bytes. In order
  71. to accept payload data of out transfers, you need to implement
  72. 'usbFunctionWrite()'.
  73. USB Suspend Mode supply current:
  74. The USB standard limits power consumption to 500uA when the bus is in suspend
  75. mode. This is not a problem for self-powered devices since they don't need
  76. bus power anyway. Bus-powered devices can achieve this only by putting the
  77. CPU in sleep mode. The driver does not implement suspend handling by itself.
  78. However, the application may implement activity monitoring and wakeup from
  79. sleep. The host sends regular SE0 states on the bus to keep it active. These
  80. SE0 states can be detected by using D- as the interrupt source. Define
  81. USB_COUNT_SOF to 1 and use the global variable usbSofCount to check for bus
  82. activity.
  83. Operation without an USB master:
  84. The driver behaves neutral without connection to an USB master if D- reads
  85. as 1. To avoid spurious interrupts, we recommend a high impedance (e.g. 1M)
  86. pull-down or pull-up resistor on D+ (interrupt). If Zener diodes are used,
  87. use a pull-down. If D- becomes statically 0, the driver may block in the
  88. interrupt routine.
  89. Interrupt latency:
  90. The application must ensure that the USB interrupt is not disabled for more
  91. than 25 cycles (this is for 12 MHz, faster clocks allow longer latency).
  92. This implies that all interrupt routines must either have the "ISR_NOBLOCK"
  93. attribute set (see "avr/interrupt.h") or be written in assembler with "sei"
  94. as the first instruction.
  95. Maximum interrupt duration / CPU cycle consumption:
  96. The driver handles all USB communication during the interrupt service
  97. routine. The routine will not return before an entire USB message is received
  98. and the reply is sent. This may be up to ca. 1200 cycles @ 12 MHz (= 100us) if
  99. the host conforms to the standard. The driver will consume CPU cycles for all
  100. USB messages, even if they address another (low-speed) device on the same bus.
  101. */
  102. /* ------------------------------------------------------------------------- */
  103. /* --------------------------- Module Interface ---------------------------- */
  104. /* ------------------------------------------------------------------------- */
  105. #define USBDRV_VERSION 20121206
  106. /* This define uniquely identifies a driver version. It is a decimal number
  107. * constructed from the driver's release date in the form YYYYMMDD. If the
  108. * driver's behavior or interface changes, you can use this constant to
  109. * distinguish versions. If it is not defined, the driver's release date is
  110. * older than 2006-01-25.
  111. */
  112. #ifndef USB_PUBLIC
  113. #define USB_PUBLIC
  114. #endif
  115. /* USB_PUBLIC is used as declaration attribute for all functions exported by
  116. * the USB driver. The default is no attribute (see above). You may define it
  117. * to static either in usbconfig.h or from the command line if you include
  118. * usbdrv.c instead of linking against it. Including the C module of the driver
  119. * directly in your code saves a couple of bytes in flash memory.
  120. */
  121. #ifndef __ASSEMBLER__
  122. #ifndef uchar
  123. #define uchar unsigned char
  124. #endif
  125. #ifndef schar
  126. #define schar signed char
  127. #endif
  128. /* shortcuts for well defined 8 bit integer types */
  129. #if USB_CFG_LONG_TRANSFERS /* if more than 254 bytes transfer size required */
  130. # define usbMsgLen_t unsigned
  131. #else
  132. # define usbMsgLen_t uchar
  133. #endif
  134. /* usbMsgLen_t is the data type used for transfer lengths. By default, it is
  135. * defined to uchar, allowing a maximum of 254 bytes (255 is reserved for
  136. * USB_NO_MSG below). If the usbconfig.h defines USB_CFG_LONG_TRANSFERS to 1,
  137. * a 16 bit data type is used, allowing up to 16384 bytes (the rest is used
  138. * for flags in the descriptor configuration).
  139. */
  140. #define USB_NO_MSG ((usbMsgLen_t)-1) /* constant meaning "no message" */
  141. #ifndef usbMsgPtr_t
  142. #define usbMsgPtr_t uchar *
  143. #endif
  144. /* Making usbMsgPtr_t a define allows the user of this library to define it to
  145. * an 8 bit type on tiny devices. This reduces code size, especially if the
  146. * compiler supports a tiny memory model.
  147. * The type can be a pointer or scalar type, casts are made where necessary.
  148. * Although it's paradoxical, Gcc 4 generates slightly better code for scalar
  149. * types than for pointers.
  150. */
  151. struct usbRequest; /* forward declaration */
  152. USB_PUBLIC void usbInit(void);
  153. /* This function must be called before interrupts are enabled and the main
  154. * loop is entered. We exepct that the PORT and DDR bits for D+ and D- have
  155. * not been changed from their default status (which is 0). If you have changed
  156. * them, set both back to 0 (configure them as input with no internal pull-up).
  157. */
  158. USB_PUBLIC void usbPoll(void);
  159. /* This function must be called at regular intervals from the main loop.
  160. * Maximum delay between calls is somewhat less than 50ms (USB timeout for
  161. * accepting a Setup message). Otherwise the device will not be recognized.
  162. * Please note that debug outputs through the UART take ~ 0.5ms per byte
  163. * at 19200 bps.
  164. */
  165. extern usbMsgPtr_t usbMsgPtr;
  166. /* This variable may be used to pass transmit data to the driver from the
  167. * implementation of usbFunctionWrite(). It is also used internally by the
  168. * driver for standard control requests.
  169. */
  170. USB_PUBLIC usbMsgLen_t usbFunctionSetup(uchar data[8]);
  171. /* This function is called when the driver receives a SETUP transaction from
  172. * the host which is not answered by the driver itself (in practice: class and
  173. * vendor requests). All control transfers start with a SETUP transaction where
  174. * the host communicates the parameters of the following (optional) data
  175. * transfer. The SETUP data is available in the 'data' parameter which can
  176. * (and should) be casted to 'usbRequest_t *' for a more user-friendly access
  177. * to parameters.
  178. *
  179. * If the SETUP indicates a control-in transfer, you should provide the
  180. * requested data to the driver. There are two ways to transfer this data:
  181. * (1) Set the global pointer 'usbMsgPtr' to the base of the static RAM data
  182. * block and return the length of the data in 'usbFunctionSetup()'. The driver
  183. * will handle the rest. Or (2) return USB_NO_MSG in 'usbFunctionSetup()'. The
  184. * driver will then call 'usbFunctionRead()' when data is needed. See the
  185. * documentation for usbFunctionRead() for details.
  186. *
  187. * If the SETUP indicates a control-out transfer, the only way to receive the
  188. * data from the host is through the 'usbFunctionWrite()' call. If you
  189. * implement this function, you must return USB_NO_MSG in 'usbFunctionSetup()'
  190. * to indicate that 'usbFunctionWrite()' should be used. See the documentation
  191. * of this function for more information. If you just want to ignore the data
  192. * sent by the host, return 0 in 'usbFunctionSetup()'.
  193. *
  194. * Note that calls to the functions usbFunctionRead() and usbFunctionWrite()
  195. * are only done if enabled by the configuration in usbconfig.h.
  196. */
  197. USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq);
  198. /* You need to implement this function ONLY if you provide USB descriptors at
  199. * runtime (which is an expert feature). It is very similar to
  200. * usbFunctionSetup() above, but it is called only to request USB descriptor
  201. * data. See the documentation of usbFunctionSetup() above for more info.
  202. */
  203. #if USB_CFG_HAVE_INTRIN_ENDPOINT
  204. USB_PUBLIC void usbSetInterrupt(uchar *data, uchar len);
  205. /* This function sets the message which will be sent during the next interrupt
  206. * IN transfer. The message is copied to an internal buffer and must not exceed
  207. * a length of 8 bytes. The message may be 0 bytes long just to indicate the
  208. * interrupt status to the host.
  209. * If you need to transfer more bytes, use a control read after the interrupt.
  210. */
  211. #define usbInterruptIsReady() (usbTxLen1 & 0x10)
  212. /* This macro indicates whether the last interrupt message has already been
  213. * sent. If you set a new interrupt message before the old was sent, the
  214. * message already buffered will be lost.
  215. */
  216. #if USB_CFG_HAVE_INTRIN_ENDPOINT3
  217. USB_PUBLIC void usbSetInterrupt3(uchar *data, uchar len);
  218. #define usbInterruptIsReady3() (usbTxLen3 & 0x10)
  219. /* Same as above for endpoint 3 */
  220. #endif
  221. #endif /* USB_CFG_HAVE_INTRIN_ENDPOINT */
  222. #if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH /* simplified interface for backward compatibility */
  223. #define usbHidReportDescriptor usbDescriptorHidReport
  224. /* should be declared as: PROGMEM char usbHidReportDescriptor[]; */
  225. /* If you implement an HID device, you need to provide a report descriptor.
  226. * The HID report descriptor syntax is a bit complex. If you understand how
  227. * report descriptors are constructed, we recommend that you use the HID
  228. * Descriptor Tool from usb.org, see http://www.usb.org/developers/hidpage/.
  229. * Otherwise you should probably start with a working example.
  230. */
  231. #endif /* USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH */
  232. #if USB_CFG_IMPLEMENT_FN_WRITE
  233. USB_PUBLIC uchar usbFunctionWrite(uchar *data, uchar len);
  234. /* This function is called by the driver to provide a control transfer's
  235. * payload data (control-out). It is called in chunks of up to 8 bytes. The
  236. * total count provided in the current control transfer can be obtained from
  237. * the 'length' property in the setup data. If an error occurred during
  238. * processing, return 0xff (== -1). The driver will answer the entire transfer
  239. * with a STALL token in this case. If you have received the entire payload
  240. * successfully, return 1. If you expect more data, return 0. If you don't
  241. * know whether the host will send more data (you should know, the total is
  242. * provided in the usbFunctionSetup() call!), return 1.
  243. * NOTE: If you return 0xff for STALL, 'usbFunctionWrite()' may still be called
  244. * for the remaining data. You must continue to return 0xff for STALL in these
  245. * calls.
  246. * In order to get usbFunctionWrite() called, define USB_CFG_IMPLEMENT_FN_WRITE
  247. * to 1 in usbconfig.h and return 0xff in usbFunctionSetup()..
  248. */
  249. #endif /* USB_CFG_IMPLEMENT_FN_WRITE */
  250. #if USB_CFG_IMPLEMENT_FN_READ
  251. USB_PUBLIC uchar usbFunctionRead(uchar *data, uchar len);
  252. /* This function is called by the driver to ask the application for a control
  253. * transfer's payload data (control-in). It is called in chunks of up to 8
  254. * bytes each. You should copy the data to the location given by 'data' and
  255. * return the actual number of bytes copied. If you return less than requested,
  256. * the control-in transfer is terminated. If you return 0xff, the driver aborts
  257. * the transfer with a STALL token.
  258. * In order to get usbFunctionRead() called, define USB_CFG_IMPLEMENT_FN_READ
  259. * to 1 in usbconfig.h and return 0xff in usbFunctionSetup()..
  260. */
  261. #endif /* USB_CFG_IMPLEMENT_FN_READ */
  262. extern uchar usbRxToken; /* may be used in usbFunctionWriteOut() below */
  263. #if USB_CFG_IMPLEMENT_FN_WRITEOUT
  264. USB_PUBLIC void usbFunctionWriteOut(uchar *data, uchar len);
  265. /* This function is called by the driver when data is received on an interrupt-
  266. * or bulk-out endpoint. The endpoint number can be found in the global
  267. * variable usbRxToken. You must define USB_CFG_IMPLEMENT_FN_WRITEOUT to 1 in
  268. * usbconfig.h to get this function called.
  269. */
  270. #endif /* USB_CFG_IMPLEMENT_FN_WRITEOUT */
  271. #ifdef USB_CFG_PULLUP_IOPORTNAME
  272. #define usbDeviceConnect() ((USB_PULLUP_DDR |= (1<<USB_CFG_PULLUP_BIT)), \
  273. (USB_PULLUP_OUT |= (1<<USB_CFG_PULLUP_BIT)))
  274. #define usbDeviceDisconnect() ((USB_PULLUP_DDR &= ~(1<<USB_CFG_PULLUP_BIT)), \
  275. (USB_PULLUP_OUT &= ~(1<<USB_CFG_PULLUP_BIT)))
  276. #else /* USB_CFG_PULLUP_IOPORTNAME */
  277. #define usbDeviceConnect() (USBDDR &= ~(1<<USBMINUS))
  278. #define usbDeviceDisconnect() (USBDDR |= (1<<USBMINUS))
  279. #endif /* USB_CFG_PULLUP_IOPORTNAME */
  280. /* The macros usbDeviceConnect() and usbDeviceDisconnect() (intended to look
  281. * like a function) connect resp. disconnect the device from the host's USB.
  282. * If the constants USB_CFG_PULLUP_IOPORT and USB_CFG_PULLUP_BIT are defined
  283. * in usbconfig.h, a disconnect consists of removing the pull-up resisitor
  284. * from D-, otherwise the disconnect is done by brute-force pulling D- to GND.
  285. * This does not conform to the spec, but it works.
  286. * Please note that the USB interrupt must be disabled while the device is
  287. * in disconnected state, or the interrupt handler will hang! You can either
  288. * turn off the USB interrupt selectively with
  289. * USB_INTR_ENABLE &= ~(1 << USB_INTR_ENABLE_BIT)
  290. * or use cli() to disable interrupts globally.
  291. */
  292. extern unsigned usbCrc16(unsigned data, uchar len);
  293. #define usbCrc16(data, len) usbCrc16((unsigned)(data), len)
  294. /* This function calculates the binary complement of the data CRC used in
  295. * USB data packets. The value is used to build raw transmit packets.
  296. * You may want to use this function for data checksums or to verify received
  297. * data. We enforce 16 bit calling conventions for compatibility with IAR's
  298. * tiny memory model.
  299. */
  300. extern unsigned usbCrc16Append(unsigned data, uchar len);
  301. #define usbCrc16Append(data, len) usbCrc16Append((unsigned)(data), len)
  302. /* This function is equivalent to usbCrc16() above, except that it appends
  303. * the 2 bytes CRC (lowbyte first) in the 'data' buffer after reading 'len'
  304. * bytes.
  305. */
  306. #if USB_CFG_HAVE_MEASURE_FRAME_LENGTH
  307. extern unsigned usbMeasureFrameLength(void);
  308. /* This function MUST be called IMMEDIATELY AFTER USB reset and measures 1/7 of
  309. * the number of CPU cycles during one USB frame minus one low speed bit
  310. * length. In other words: return value = 1499 * (F_CPU / 10.5 MHz)
  311. * Since this is a busy wait, you MUST disable all interrupts with cli() before
  312. * calling this function.
  313. * This can be used to calibrate the AVR's RC oscillator.
  314. */
  315. #endif
  316. extern uchar usbConfiguration;
  317. /* This value contains the current configuration set by the host. The driver
  318. * allows setting and querying of this variable with the USB SET_CONFIGURATION
  319. * and GET_CONFIGURATION requests, but does not use it otherwise.
  320. * You may want to reflect the "configured" status with a LED on the device or
  321. * switch on high power parts of the circuit only if the device is configured.
  322. */
  323. #if USB_COUNT_SOF
  324. extern volatile uchar usbSofCount;
  325. /* This variable is incremented on every SOF packet. It is only available if
  326. * the macro USB_COUNT_SOF is defined to a value != 0.
  327. */
  328. #endif
  329. #if USB_CFG_CHECK_DATA_TOGGLING
  330. extern uchar usbCurrentDataToken;
  331. /* This variable can be checked in usbFunctionWrite() and usbFunctionWriteOut()
  332. * to ignore duplicate packets.
  333. */
  334. #endif
  335. #define USB_STRING_DESCRIPTOR_HEADER(stringLength) ((2*(stringLength)+2) | (3<<8))
  336. /* This macro builds a descriptor header for a string descriptor given the
  337. * string's length. See usbdrv.c for an example how to use it.
  338. */
  339. #if USB_CFG_HAVE_FLOWCONTROL
  340. extern volatile schar usbRxLen;
  341. #define usbDisableAllRequests() usbRxLen = -1
  342. /* Must be called from usbFunctionWrite(). This macro disables all data input
  343. * from the USB interface. Requests from the host are answered with a NAK
  344. * while they are disabled.
  345. */
  346. #define usbEnableAllRequests() usbRxLen = 0
  347. /* May only be called if requests are disabled. This macro enables input from
  348. * the USB interface after it has been disabled with usbDisableAllRequests().
  349. */
  350. #define usbAllRequestsAreDisabled() (usbRxLen < 0)
  351. /* Use this macro to find out whether requests are disabled. It may be needed
  352. * to ensure that usbEnableAllRequests() is never called when requests are
  353. * enabled.
  354. */
  355. #endif
  356. #define USB_SET_DATATOKEN1(token) usbTxBuf1[0] = token
  357. #define USB_SET_DATATOKEN3(token) usbTxBuf3[0] = token
  358. /* These two macros can be used by application software to reset data toggling
  359. * for interrupt-in endpoints 1 and 3. Since the token is toggled BEFORE
  360. * sending data, you must set the opposite value of the token which should come
  361. * first.
  362. */
  363. #endif /* __ASSEMBLER__ */
  364. /* ------------------------------------------------------------------------- */
  365. /* ----------------- Definitions for Descriptor Properties ----------------- */
  366. /* ------------------------------------------------------------------------- */
  367. /* This is advanced stuff. See usbconfig-prototype.h for more information
  368. * about the various methods to define USB descriptors. If you do nothing,
  369. * the default descriptors will be used.
  370. */
  371. #define USB_PROP_IS_DYNAMIC (1u << 14)
  372. /* If this property is set for a descriptor, usbFunctionDescriptor() will be
  373. * used to obtain the particular descriptor. Data directly returned via
  374. * usbMsgPtr are FLASH data by default, combine (OR) with USB_PROP_IS_RAM to
  375. * return RAM data.
  376. */
  377. #define USB_PROP_IS_RAM (1u << 15)
  378. /* If this property is set for a descriptor, the data is read from RAM
  379. * memory instead of Flash. The property is used for all methods to provide
  380. * external descriptors.
  381. */
  382. #define USB_PROP_LENGTH(len) ((len) & 0x3fff)
  383. /* If a static external descriptor is used, this is the total length of the
  384. * descriptor in bytes.
  385. */
  386. /* all descriptors which may have properties: */
  387. #ifndef USB_CFG_DESCR_PROPS_DEVICE
  388. #define USB_CFG_DESCR_PROPS_DEVICE 0
  389. #endif
  390. #ifndef USB_CFG_DESCR_PROPS_CONFIGURATION
  391. #define USB_CFG_DESCR_PROPS_CONFIGURATION 0
  392. #endif
  393. #ifndef USB_CFG_DESCR_PROPS_STRINGS
  394. #define USB_CFG_DESCR_PROPS_STRINGS 0
  395. #endif
  396. #ifndef USB_CFG_DESCR_PROPS_STRING_0
  397. #define USB_CFG_DESCR_PROPS_STRING_0 0
  398. #endif
  399. #ifndef USB_CFG_DESCR_PROPS_STRING_VENDOR
  400. #define USB_CFG_DESCR_PROPS_STRING_VENDOR 0
  401. #endif
  402. #ifndef USB_CFG_DESCR_PROPS_STRING_PRODUCT
  403. #define USB_CFG_DESCR_PROPS_STRING_PRODUCT 0
  404. #endif
  405. #ifndef USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER
  406. #define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER 0
  407. #endif
  408. #ifndef USB_CFG_DESCR_PROPS_HID
  409. #define USB_CFG_DESCR_PROPS_HID 0
  410. #endif
  411. #if !(USB_CFG_DESCR_PROPS_HID_REPORT)
  412. # undef USB_CFG_DESCR_PROPS_HID_REPORT
  413. # if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH /* do some backward compatibility tricks */
  414. # define USB_CFG_DESCR_PROPS_HID_REPORT USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH
  415. # else
  416. # define USB_CFG_DESCR_PROPS_HID_REPORT 0
  417. # endif
  418. #endif
  419. #ifndef USB_CFG_DESCR_PROPS_UNKNOWN
  420. #define USB_CFG_DESCR_PROPS_UNKNOWN 0
  421. #endif
  422. /* ------------------ forward declaration of descriptors ------------------- */
  423. /* If you use external static descriptors, they must be stored in global
  424. * arrays as declared below:
  425. */
  426. #ifndef __ASSEMBLER__
  427. extern
  428. #if !(USB_CFG_DESCR_PROPS_DEVICE & USB_PROP_IS_RAM)
  429. PROGMEM const
  430. #endif
  431. char usbDescriptorDevice[];
  432. extern
  433. #if !(USB_CFG_DESCR_PROPS_CONFIGURATION & USB_PROP_IS_RAM)
  434. PROGMEM const
  435. #endif
  436. char usbDescriptorConfiguration[];
  437. extern
  438. #if !(USB_CFG_DESCR_PROPS_HID_REPORT & USB_PROP_IS_RAM)
  439. PROGMEM const
  440. #endif
  441. char usbDescriptorHidReport[];
  442. extern
  443. #if !(USB_CFG_DESCR_PROPS_STRING_0 & USB_PROP_IS_RAM)
  444. PROGMEM const
  445. #endif
  446. char usbDescriptorString0[];
  447. extern
  448. #if !(USB_CFG_DESCR_PROPS_STRING_VENDOR & USB_PROP_IS_RAM)
  449. PROGMEM const
  450. #endif
  451. int usbDescriptorStringVendor[];
  452. extern
  453. #if !(USB_CFG_DESCR_PROPS_STRING_PRODUCT & USB_PROP_IS_RAM)
  454. PROGMEM const
  455. #endif
  456. int usbDescriptorStringDevice[];
  457. extern
  458. #if !(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER & USB_PROP_IS_RAM)
  459. PROGMEM const
  460. #endif
  461. int usbDescriptorStringSerialNumber[];
  462. #endif /* __ASSEMBLER__ */
  463. /* ------------------------------------------------------------------------- */
  464. /* ------------------------ General Purpose Macros ------------------------- */
  465. /* ------------------------------------------------------------------------- */
  466. #define USB_CONCAT(a, b) a ## b
  467. #define USB_CONCAT_EXPANDED(a, b) USB_CONCAT(a, b)
  468. #define USB_OUTPORT(name) USB_CONCAT(PORT, name)
  469. #define USB_INPORT(name) USB_CONCAT(PIN, name)
  470. #define USB_DDRPORT(name) USB_CONCAT(DDR, name)
  471. /* The double-define trick above lets us concatenate strings which are
  472. * defined by macros.
  473. */
  474. /* ------------------------------------------------------------------------- */
  475. /* ------------------------- Constant definitions -------------------------- */
  476. /* ------------------------------------------------------------------------- */
  477. #if !defined __ASSEMBLER__ && (!defined USB_CFG_VENDOR_ID || !defined USB_CFG_DEVICE_ID)
  478. #warning "You should define USB_CFG_VENDOR_ID and USB_CFG_DEVICE_ID in usbconfig.h"
  479. /* If the user has not defined IDs, we default to obdev's free IDs.
  480. * See USB-IDs-for-free.txt for details.
  481. */
  482. #endif
  483. /* make sure we have a VID and PID defined, byte order is lowbyte, highbyte */
  484. #ifndef USB_CFG_VENDOR_ID
  485. # define USB_CFG_VENDOR_ID 0xc0, 0x16 /* = 0x16c0 = 5824 = voti.nl */
  486. #endif
  487. #ifndef USB_CFG_DEVICE_ID
  488. # if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH
  489. # define USB_CFG_DEVICE_ID 0xdf, 0x05 /* = 0x5df = 1503, shared PID for HIDs */
  490. # elif USB_CFG_INTERFACE_CLASS == 2
  491. # define USB_CFG_DEVICE_ID 0xe1, 0x05 /* = 0x5e1 = 1505, shared PID for CDC Modems */
  492. # else
  493. # define USB_CFG_DEVICE_ID 0xdc, 0x05 /* = 0x5dc = 1500, obdev's free PID */
  494. # endif
  495. #endif
  496. /* Derive Output, Input and DataDirection ports from port names */
  497. #ifndef USB_CFG_IOPORTNAME
  498. #error "You must define USB_CFG_IOPORTNAME in usbconfig.h, see usbconfig-prototype.h"
  499. #endif
  500. #define USBOUT USB_OUTPORT(USB_CFG_IOPORTNAME)
  501. #define USB_PULLUP_OUT USB_OUTPORT(USB_CFG_PULLUP_IOPORTNAME)
  502. #define USBIN USB_INPORT(USB_CFG_IOPORTNAME)
  503. #define USBDDR USB_DDRPORT(USB_CFG_IOPORTNAME)
  504. #define USB_PULLUP_DDR USB_DDRPORT(USB_CFG_PULLUP_IOPORTNAME)
  505. #define USBMINUS USB_CFG_DMINUS_BIT
  506. #define USBPLUS USB_CFG_DPLUS_BIT
  507. #define USBIDLE (1<<USB_CFG_DMINUS_BIT) /* value representing J state */
  508. #define USBMASK ((1<<USB_CFG_DPLUS_BIT) | (1<<USB_CFG_DMINUS_BIT)) /* mask for USB I/O bits */
  509. /* defines for backward compatibility with older driver versions: */
  510. #define USB_CFG_IOPORT USB_OUTPORT(USB_CFG_IOPORTNAME)
  511. #ifdef USB_CFG_PULLUP_IOPORTNAME
  512. #define USB_CFG_PULLUP_IOPORT USB_OUTPORT(USB_CFG_PULLUP_IOPORTNAME)
  513. #endif
  514. #ifndef USB_CFG_EP3_NUMBER /* if not defined in usbconfig.h */
  515. #define USB_CFG_EP3_NUMBER 3
  516. #endif
  517. #ifndef USB_CFG_HAVE_INTRIN_ENDPOINT3
  518. #define USB_CFG_HAVE_INTRIN_ENDPOINT3 0
  519. #endif
  520. #define USB_BUFSIZE 11 /* PID, 8 bytes data, 2 bytes CRC */
  521. /* ----- Try to find registers and bits responsible for ext interrupt 0 ----- */
  522. #ifndef USB_INTR_CFG /* allow user to override our default */
  523. # if defined EICRA
  524. # define USB_INTR_CFG EICRA
  525. # else
  526. # define USB_INTR_CFG MCUCR
  527. # endif
  528. #endif
  529. #ifndef USB_INTR_CFG_SET /* allow user to override our default */
  530. # if defined(USB_COUNT_SOF) || defined(USB_SOF_HOOK)
  531. # define USB_INTR_CFG_SET (1 << ISC01) /* cfg for falling edge */
  532. /* If any SOF logic is used, the interrupt must be wired to D- where
  533. * we better trigger on falling edge
  534. */
  535. # else
  536. # define USB_INTR_CFG_SET ((1 << ISC00) | (1 << ISC01)) /* cfg for rising edge */
  537. # endif
  538. #endif
  539. #ifndef USB_INTR_CFG_CLR /* allow user to override our default */
  540. # define USB_INTR_CFG_CLR 0 /* no bits to clear */
  541. #endif
  542. #ifndef USB_INTR_ENABLE /* allow user to override our default */
  543. # if defined GIMSK
  544. # define USB_INTR_ENABLE GIMSK
  545. # elif defined EIMSK
  546. # define USB_INTR_ENABLE EIMSK
  547. # else
  548. # define USB_INTR_ENABLE GICR
  549. # endif
  550. #endif
  551. #ifndef USB_INTR_ENABLE_BIT /* allow user to override our default */
  552. # define USB_INTR_ENABLE_BIT INT0
  553. #endif
  554. #ifndef USB_INTR_PENDING /* allow user to override our default */
  555. # if defined EIFR
  556. # define USB_INTR_PENDING EIFR
  557. # else
  558. # define USB_INTR_PENDING GIFR
  559. # endif
  560. #endif
  561. #ifndef USB_INTR_PENDING_BIT /* allow user to override our default */
  562. # define USB_INTR_PENDING_BIT INTF0
  563. #endif
  564. /*
  565. The defines above don't work for the following chips
  566. at90c8534: no ISC0?, no PORTB, can't find a data sheet
  567. at86rf401: no PORTB, no MCUCR etc, low clock rate
  568. atmega103: no ISC0? (maybe omission in header, can't find data sheet)
  569. atmega603: not defined in avr-libc
  570. at43usb320, at43usb355, at76c711: have USB anyway
  571. at94k: is different...
  572. at90s1200, attiny11, attiny12, attiny15, attiny28: these have no RAM
  573. */
  574. /* ------------------------------------------------------------------------- */
  575. /* ----------------- USB Specification Constants and Types ----------------- */
  576. /* ------------------------------------------------------------------------- */
  577. /* USB Token values */
  578. #define USBPID_SETUP 0x2d
  579. #define USBPID_OUT 0xe1
  580. #define USBPID_IN 0x69
  581. #define USBPID_DATA0 0xc3
  582. #define USBPID_DATA1 0x4b
  583. #define USBPID_ACK 0xd2
  584. #define USBPID_NAK 0x5a
  585. #define USBPID_STALL 0x1e
  586. #ifndef USB_INITIAL_DATATOKEN
  587. #define USB_INITIAL_DATATOKEN USBPID_DATA1
  588. #endif
  589. #ifndef __ASSEMBLER__
  590. typedef struct usbTxStatus{
  591. volatile uchar len;
  592. uchar buffer[USB_BUFSIZE];
  593. }usbTxStatus_t;
  594. extern usbTxStatus_t usbTxStatus1, usbTxStatus3;
  595. #define usbTxLen1 usbTxStatus1.len
  596. #define usbTxBuf1 usbTxStatus1.buffer
  597. #define usbTxLen3 usbTxStatus3.len
  598. #define usbTxBuf3 usbTxStatus3.buffer
  599. typedef union usbWord{
  600. unsigned word;
  601. uchar bytes[2];
  602. }usbWord_t;
  603. typedef struct usbRequest{
  604. uchar bmRequestType;
  605. uchar bRequest;
  606. usbWord_t wValue;
  607. usbWord_t wIndex;
  608. usbWord_t wLength;
  609. }usbRequest_t;
  610. /* This structure matches the 8 byte setup request */
  611. #endif
  612. /* bmRequestType field in USB setup:
  613. * d t t r r r r r, where
  614. * d ..... direction: 0=host->device, 1=device->host
  615. * t ..... type: 0=standard, 1=class, 2=vendor, 3=reserved
  616. * r ..... recipient: 0=device, 1=interface, 2=endpoint, 3=other
  617. */
  618. /* USB setup recipient values */
  619. #define USBRQ_RCPT_MASK 0x1f
  620. #define USBRQ_RCPT_DEVICE 0
  621. #define USBRQ_RCPT_INTERFACE 1
  622. #define USBRQ_RCPT_ENDPOINT 2
  623. /* USB request type values */
  624. #define USBRQ_TYPE_MASK 0x60
  625. #define USBRQ_TYPE_STANDARD (0<<5)
  626. #define USBRQ_TYPE_CLASS (1<<5)
  627. #define USBRQ_TYPE_VENDOR (2<<5)
  628. /* USB direction values: */
  629. #define USBRQ_DIR_MASK 0x80
  630. #define USBRQ_DIR_HOST_TO_DEVICE (0<<7)
  631. #define USBRQ_DIR_DEVICE_TO_HOST (1<<7)
  632. /* USB Standard Requests */
  633. #define USBRQ_GET_STATUS 0
  634. #define USBRQ_CLEAR_FEATURE 1
  635. #define USBRQ_SET_FEATURE 3
  636. #define USBRQ_SET_ADDRESS 5
  637. #define USBRQ_GET_DESCRIPTOR 6
  638. #define USBRQ_SET_DESCRIPTOR 7
  639. #define USBRQ_GET_CONFIGURATION 8
  640. #define USBRQ_SET_CONFIGURATION 9
  641. #define USBRQ_GET_INTERFACE 10
  642. #define USBRQ_SET_INTERFACE 11
  643. #define USBRQ_SYNCH_FRAME 12
  644. /* USB descriptor constants */
  645. #define USBDESCR_DEVICE 1
  646. #define USBDESCR_CONFIG 2
  647. #define USBDESCR_STRING 3
  648. #define USBDESCR_INTERFACE 4
  649. #define USBDESCR_ENDPOINT 5
  650. #define USBDESCR_HID 0x21
  651. #define USBDESCR_HID_REPORT 0x22
  652. #define USBDESCR_HID_PHYS 0x23
  653. //#define USBATTR_BUSPOWER 0x80 // USB 1.1 does not define this value any more
  654. #define USBATTR_BUSPOWER 0
  655. #define USBATTR_SELFPOWER 0x40
  656. #define USBATTR_REMOTEWAKE 0x20
  657. /* USB HID Requests */
  658. #define USBRQ_HID_GET_REPORT 0x01
  659. #define USBRQ_HID_GET_IDLE 0x02
  660. #define USBRQ_HID_GET_PROTOCOL 0x03
  661. #define USBRQ_HID_SET_REPORT 0x09
  662. #define USBRQ_HID_SET_IDLE 0x0a
  663. #define USBRQ_HID_SET_PROTOCOL 0x0b
  664. /* ------------------------------------------------------------------------- */
  665. #endif /* __usbdrv_h_included__ */