Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

usbdrv.h 28KB

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