Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. * This Revision: $Id: usbdrv.h 769 2009-08-22 11:49:05Z cs $
  9. */
  10. #ifndef __usbdrv_h_included__
  11. #define __usbdrv_h_included__
  12. #include "usbconfig.h"
  13. #include "usbportability.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, 16 MHz or 20 MHz
  33. or at 12.8 MHz resp. 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 20090822
  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. #if USB_CFG_LONG_TRANSFERS /* if more than 254 bytes transfer size required */
  131. # define usbMsgLen_t unsigned
  132. #else
  133. # define usbMsgLen_t uchar
  134. #endif
  135. /* usbMsgLen_t is the data type used for transfer lengths. By default, it is
  136. * defined to uchar, allowing a maximum of 254 bytes (255 is reserved for
  137. * USB_NO_MSG below). If the usbconfig.h defines USB_CFG_LONG_TRANSFERS to 1,
  138. * a 16 bit data type is used, allowing up to 16384 bytes (the rest is used
  139. * for flags in the descriptor configuration).
  140. */
  141. #define USB_NO_MSG ((usbMsgLen_t)-1) /* constant meaning "no message" */
  142. struct usbRequest; /* forward declaration */
  143. #ifdef __cplusplus
  144. extern "C"{
  145. #endif
  146. USB_PUBLIC void usbInit(void);
  147. /* This function must be called before interrupts are enabled and the main
  148. * loop is entered. We exepct that the PORT and DDR bits for D+ and D- have
  149. * not been changed from their default status (which is 0). If you have changed
  150. * them, set both back to 0 (configure them as input with no internal pull-up).
  151. */
  152. USB_PUBLIC void usbPoll(void);
  153. /* This function must be called at regular intervals from the main loop.
  154. * Maximum delay between calls is somewhat less than 50ms (USB timeout for
  155. * accepting a Setup message). Otherwise the device will not be recognized.
  156. * Please note that debug outputs through the UART take ~ 0.5ms per byte
  157. * at 19200 bps.
  158. */
  159. #ifdef __cplusplus
  160. } // extern "C"
  161. #endif
  162. extern uchar *usbMsgPtr;
  163. /* This variable may be used to pass transmit data to the driver from the
  164. * implementation of usbFunctionWrite(). It is also used internally by the
  165. * driver for standard control requests.
  166. */
  167. #ifdef __cplusplus
  168. extern "C"{
  169. #endif
  170. USB_PUBLIC usbMsgLen_t usbFunctionSetup(uchar data[8]);
  171. #ifdef __cplusplus
  172. } // extern "C"
  173. #endif
  174. /* This function is called when the driver receives a SETUP transaction from
  175. * the host which is not answered by the driver itself (in practice: class and
  176. * vendor requests). All control transfers start with a SETUP transaction where
  177. * the host communicates the parameters of the following (optional) data
  178. * transfer. The SETUP data is available in the 'data' parameter which can
  179. * (and should) be casted to 'usbRequest_t *' for a more user-friendly access
  180. * to parameters.
  181. *
  182. * If the SETUP indicates a control-in transfer, you should provide the
  183. * requested data to the driver. There are two ways to transfer this data:
  184. * (1) Set the global pointer 'usbMsgPtr' to the base of the static RAM data
  185. * block and return the length of the data in 'usbFunctionSetup()'. The driver
  186. * will handle the rest. Or (2) return USB_NO_MSG in 'usbFunctionSetup()'. The
  187. * driver will then call 'usbFunctionRead()' when data is needed. See the
  188. * documentation for usbFunctionRead() for details.
  189. *
  190. * If the SETUP indicates a control-out transfer, the only way to receive the
  191. * data from the host is through the 'usbFunctionWrite()' call. If you
  192. * implement this function, you must return USB_NO_MSG in 'usbFunctionSetup()'
  193. * to indicate that 'usbFunctionWrite()' should be used. See the documentation
  194. * of this function for more information. If you just want to ignore the data
  195. * sent by the host, return 0 in 'usbFunctionSetup()'.
  196. *
  197. * Note that calls to the functions usbFunctionRead() and usbFunctionWrite()
  198. * are only done if enabled by the configuration in usbconfig.h.
  199. */
  200. USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq);
  201. /* You need to implement this function ONLY if you provide USB descriptors at
  202. * runtime (which is an expert feature). It is very similar to
  203. * usbFunctionSetup() above, but it is called only to request USB descriptor
  204. * data. See the documentation of usbFunctionSetup() above for more info.
  205. */
  206. #if USB_CFG_HAVE_INTRIN_ENDPOINT
  207. #ifdef __cplusplus
  208. extern "C"{
  209. #endif
  210. USB_PUBLIC void usbSetInterrupt(uchar *data, uchar len);
  211. #ifdef __cplusplus
  212. } // extern "C"
  213. #endif
  214. /* This function sets the message which will be sent during the next interrupt
  215. * IN transfer. The message is copied to an internal buffer and must not exceed
  216. * a length of 8 bytes. The message may be 0 bytes long just to indicate the
  217. * interrupt status to the host.
  218. * If you need to transfer more bytes, use a control read after the interrupt.
  219. */
  220. #define usbInterruptIsReady() (usbTxLen1 & 0x10)
  221. /* This macro indicates whether the last interrupt message has already been
  222. * sent. If you set a new interrupt message before the old was sent, the
  223. * message already buffered will be lost.
  224. */
  225. #if USB_CFG_HAVE_INTRIN_ENDPOINT3
  226. USB_PUBLIC void usbSetInterrupt3(uchar *data, uchar len);
  227. #define usbInterruptIsReady3() (usbTxLen3 & 0x10)
  228. /* Same as above for endpoint 3 */
  229. #endif
  230. #endif /* USB_CFG_HAVE_INTRIN_ENDPOINT */
  231. #if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH /* simplified interface for backward compatibility */
  232. #define usbHidReportDescriptor usbDescriptorHidReport
  233. /* should be declared as: PROGMEM char usbHidReportDescriptor[]; */
  234. /* If you implement an HID device, you need to provide a report descriptor.
  235. * The HID report descriptor syntax is a bit complex. If you understand how
  236. * report descriptors are constructed, we recommend that you use the HID
  237. * Descriptor Tool from usb.org, see http://www.usb.org/developers/hidpage/.
  238. * Otherwise you should probably start with a working example.
  239. */
  240. #endif /* USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH */
  241. #if USB_CFG_IMPLEMENT_FN_WRITE
  242. USB_PUBLIC uchar usbFunctionWrite(uchar *data, uchar len);
  243. /* This function is called by the driver to provide a control transfer's
  244. * payload data (control-out). It is called in chunks of up to 8 bytes. The
  245. * total count provided in the current control transfer can be obtained from
  246. * the 'length' property in the setup data. If an error occurred during
  247. * processing, return 0xff (== -1). The driver will answer the entire transfer
  248. * with a STALL token in this case. If you have received the entire payload
  249. * successfully, return 1. If you expect more data, return 0. If you don't
  250. * know whether the host will send more data (you should know, the total is
  251. * provided in the usbFunctionSetup() call!), return 1.
  252. * NOTE: If you return 0xff for STALL, 'usbFunctionWrite()' may still be called
  253. * for the remaining data. You must continue to return 0xff for STALL in these
  254. * calls.
  255. * In order to get usbFunctionWrite() called, define USB_CFG_IMPLEMENT_FN_WRITE
  256. * to 1 in usbconfig.h and return 0xff in usbFunctionSetup()..
  257. */
  258. #endif /* USB_CFG_IMPLEMENT_FN_WRITE */
  259. #if USB_CFG_IMPLEMENT_FN_READ
  260. USB_PUBLIC uchar usbFunctionRead(uchar *data, uchar len);
  261. /* This function is called by the driver to ask the application for a control
  262. * transfer's payload data (control-in). It is called in chunks of up to 8
  263. * bytes each. You should copy the data to the location given by 'data' and
  264. * return the actual number of bytes copied. If you return less than requested,
  265. * the control-in transfer is terminated. If you return 0xff, the driver aborts
  266. * the transfer with a STALL token.
  267. * In order to get usbFunctionRead() called, define USB_CFG_IMPLEMENT_FN_READ
  268. * to 1 in usbconfig.h and return 0xff in usbFunctionSetup()..
  269. */
  270. #endif /* USB_CFG_IMPLEMENT_FN_READ */
  271. extern uchar usbRxToken; /* may be used in usbFunctionWriteOut() below */
  272. #if USB_CFG_IMPLEMENT_FN_WRITEOUT
  273. USB_PUBLIC void usbFunctionWriteOut(uchar *data, uchar len);
  274. /* This function is called by the driver when data is received on an interrupt-
  275. * or bulk-out endpoint. The endpoint number can be found in the global
  276. * variable usbRxToken. You must define USB_CFG_IMPLEMENT_FN_WRITEOUT to 1 in
  277. * usbconfig.h to get this function called.
  278. */
  279. #endif /* USB_CFG_IMPLEMENT_FN_WRITEOUT */
  280. #ifdef USB_CFG_PULLUP_IOPORTNAME
  281. #define usbDeviceConnect() ((USB_PULLUP_DDR |= (1<<USB_CFG_PULLUP_BIT)), \
  282. (USB_PULLUP_OUT |= (1<<USB_CFG_PULLUP_BIT)))
  283. #define usbDeviceDisconnect() ((USB_PULLUP_DDR &= ~(1<<USB_CFG_PULLUP_BIT)), \
  284. (USB_PULLUP_OUT &= ~(1<<USB_CFG_PULLUP_BIT)))
  285. #else /* USB_CFG_PULLUP_IOPORTNAME */
  286. #define usbDeviceConnect() (USBDDR &= ~(1<<USBMINUS))
  287. #define usbDeviceDisconnect() (USBDDR |= (1<<USBMINUS))
  288. #endif /* USB_CFG_PULLUP_IOPORTNAME */
  289. /* The macros usbDeviceConnect() and usbDeviceDisconnect() (intended to look
  290. * like a function) connect resp. disconnect the device from the host's USB.
  291. * If the constants USB_CFG_PULLUP_IOPORT and USB_CFG_PULLUP_BIT are defined
  292. * in usbconfig.h, a disconnect consists of removing the pull-up resisitor
  293. * from D-, otherwise the disconnect is done by brute-force pulling D- to GND.
  294. * This does not conform to the spec, but it works.
  295. * Please note that the USB interrupt must be disabled while the device is
  296. * in disconnected state, or the interrupt handler will hang! You can either
  297. * turn off the USB interrupt selectively with
  298. * USB_INTR_ENABLE &= ~(1 << USB_INTR_ENABLE_BIT)
  299. * or use cli() to disable interrupts globally.
  300. */
  301. extern unsigned usbCrc16(unsigned data, uchar len);
  302. #define usbCrc16(data, len) usbCrc16((unsigned)(data), len)
  303. /* This function calculates the binary complement of the data CRC used in
  304. * USB data packets. The value is used to build raw transmit packets.
  305. * You may want to use this function for data checksums or to verify received
  306. * data. We enforce 16 bit calling conventions for compatibility with IAR's
  307. * tiny memory model.
  308. */
  309. #ifdef __cplusplus
  310. extern "C"{
  311. #endif
  312. extern unsigned usbCrc16Append(unsigned data, uchar len);
  313. #ifdef __cplusplus
  314. } // extern "C"
  315. #endif
  316. #define usbCrc16Append(data, len) usbCrc16Append((unsigned)(data), len)
  317. /* This function is equivalent to usbCrc16() above, except that it appends
  318. * the 2 bytes CRC (lowbyte first) in the 'data' buffer after reading 'len'
  319. * bytes.
  320. */
  321. #if USB_CFG_HAVE_MEASURE_FRAME_LENGTH
  322. extern unsigned usbMeasureFrameLength(void);
  323. /* This function MUST be called IMMEDIATELY AFTER USB reset and measures 1/7 of
  324. * the number of CPU cycles during one USB frame minus one low speed bit
  325. * length. In other words: return value = 1499 * (F_CPU / 10.5 MHz)
  326. * Since this is a busy wait, you MUST disable all interrupts with cli() before
  327. * calling this function.
  328. * This can be used to calibrate the AVR's RC oscillator.
  329. */
  330. #endif
  331. extern uchar usbConfiguration;
  332. /* This value contains the current configuration set by the host. The driver
  333. * allows setting and querying of this variable with the USB SET_CONFIGURATION
  334. * and GET_CONFIGURATION requests, but does not use it otherwise.
  335. * You may want to reflect the "configured" status with a LED on the device or
  336. * switch on high power parts of the circuit only if the device is configured.
  337. */
  338. #if USB_COUNT_SOF
  339. extern volatile uchar usbSofCount;
  340. /* This variable is incremented on every SOF packet. It is only available if
  341. * the macro USB_COUNT_SOF is defined to a value != 0.
  342. */
  343. #endif
  344. #if USB_CFG_CHECK_DATA_TOGGLING
  345. extern uchar usbCurrentDataToken;
  346. /* This variable can be checked in usbFunctionWrite() and usbFunctionWriteOut()
  347. * to ignore duplicate packets.
  348. */
  349. #endif
  350. #define USB_STRING_DESCRIPTOR_HEADER(stringLength) ((2*(stringLength)+2) | (3<<8))
  351. /* This macro builds a descriptor header for a string descriptor given the
  352. * string's length. See usbdrv.c for an example how to use it.
  353. */
  354. #if USB_CFG_HAVE_FLOWCONTROL
  355. extern volatile schar usbRxLen;
  356. #define usbDisableAllRequests() usbRxLen = -1
  357. /* Must be called from usbFunctionWrite(). This macro disables all data input
  358. * from the USB interface. Requests from the host are answered with a NAK
  359. * while they are disabled.
  360. */
  361. #define usbEnableAllRequests() usbRxLen = 0
  362. /* May only be called if requests are disabled. This macro enables input from
  363. * the USB interface after it has been disabled with usbDisableAllRequests().
  364. */
  365. #define usbAllRequestsAreDisabled() (usbRxLen < 0)
  366. /* Use this macro to find out whether requests are disabled. It may be needed
  367. * to ensure that usbEnableAllRequests() is never called when requests are
  368. * enabled.
  369. */
  370. #endif
  371. #define USB_SET_DATATOKEN1(token) usbTxBuf1[0] = token
  372. #define USB_SET_DATATOKEN3(token) usbTxBuf3[0] = token
  373. /* These two macros can be used by application software to reset data toggling
  374. * for interrupt-in endpoints 1 and 3. Since the token is toggled BEFORE
  375. * sending data, you must set the opposite value of the token which should come
  376. * first.
  377. */
  378. #endif /* __ASSEMBLER__ */
  379. /* ------------------------------------------------------------------------- */
  380. /* ----------------- Definitions for Descriptor Properties ----------------- */
  381. /* ------------------------------------------------------------------------- */
  382. /* This is advanced stuff. See usbconfig-prototype.h for more information
  383. * about the various methods to define USB descriptors. If you do nothing,
  384. * the default descriptors will be used.
  385. */
  386. #define USB_PROP_IS_DYNAMIC (1 << 14)
  387. /* If this property is set for a descriptor, usbFunctionDescriptor() will be
  388. * used to obtain the particular descriptor. Data directly returned via
  389. * usbMsgPtr are FLASH data by default, combine (OR) with USB_PROP_IS_RAM to
  390. * return RAM data.
  391. */
  392. #define USB_PROP_IS_RAM (1 << 15)
  393. /* If this property is set for a descriptor, the data is read from RAM
  394. * memory instead of Flash. The property is used for all methods to provide
  395. * external descriptors.
  396. */
  397. #define USB_PROP_LENGTH(len) ((len) & 0x3fff)
  398. /* If a static external descriptor is used, this is the total length of the
  399. * descriptor in bytes.
  400. */
  401. /* all descriptors which may have properties: */
  402. #ifndef USB_CFG_DESCR_PROPS_DEVICE
  403. #define USB_CFG_DESCR_PROPS_DEVICE 0
  404. #endif
  405. #ifndef USB_CFG_DESCR_PROPS_CONFIGURATION
  406. #define USB_CFG_DESCR_PROPS_CONFIGURATION 0
  407. #endif
  408. #ifndef USB_CFG_DESCR_PROPS_STRINGS
  409. #define USB_CFG_DESCR_PROPS_STRINGS 0
  410. #endif
  411. #ifndef USB_CFG_DESCR_PROPS_STRING_0
  412. #define USB_CFG_DESCR_PROPS_STRING_0 0
  413. #endif
  414. #ifndef USB_CFG_DESCR_PROPS_STRING_VENDOR
  415. #define USB_CFG_DESCR_PROPS_STRING_VENDOR 0
  416. #endif
  417. #ifndef USB_CFG_DESCR_PROPS_STRING_PRODUCT
  418. #define USB_CFG_DESCR_PROPS_STRING_PRODUCT 0
  419. #endif
  420. #ifndef USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER
  421. #define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER 0
  422. #endif
  423. #ifndef USB_CFG_DESCR_PROPS_HID
  424. #define USB_CFG_DESCR_PROPS_HID 0
  425. #endif
  426. #if !(USB_CFG_DESCR_PROPS_HID_REPORT)
  427. # undef USB_CFG_DESCR_PROPS_HID_REPORT
  428. # if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH /* do some backward compatibility tricks */
  429. # define USB_CFG_DESCR_PROPS_HID_REPORT USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH
  430. # else
  431. # define USB_CFG_DESCR_PROPS_HID_REPORT 0
  432. # endif
  433. #endif
  434. #ifndef USB_CFG_DESCR_PROPS_UNKNOWN
  435. #define USB_CFG_DESCR_PROPS_UNKNOWN 0
  436. #endif
  437. /* ------------------ forward declaration of descriptors ------------------- */
  438. /* If you use external static descriptors, they must be stored in global
  439. * arrays as declared below:
  440. */
  441. #ifndef __ASSEMBLER__
  442. extern
  443. #if !(USB_CFG_DESCR_PROPS_DEVICE & USB_PROP_IS_RAM)
  444. PROGMEM
  445. #endif
  446. const char usbDescriptorDevice[];
  447. extern
  448. #if !(USB_CFG_DESCR_PROPS_CONFIGURATION & USB_PROP_IS_RAM)
  449. PROGMEM
  450. #endif
  451. const char usbDescriptorConfiguration[];
  452. extern
  453. #if !(USB_CFG_DESCR_PROPS_HID_REPORT & USB_PROP_IS_RAM)
  454. PROGMEM
  455. #endif
  456. const char usbDescriptorHidReport[];
  457. extern
  458. #if !(USB_CFG_DESCR_PROPS_STRING_0 & USB_PROP_IS_RAM)
  459. PROGMEM
  460. #endif
  461. const char usbDescriptorString0[];
  462. extern
  463. #if !(USB_CFG_DESCR_PROPS_STRING_VENDOR & USB_PROP_IS_RAM)
  464. PROGMEM
  465. #endif
  466. const int usbDescriptorStringVendor[];
  467. extern
  468. #if !(USB_CFG_DESCR_PROPS_STRING_PRODUCT & USB_PROP_IS_RAM)
  469. PROGMEM
  470. #endif
  471. const int usbDescriptorStringDevice[];
  472. extern
  473. #if !(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER & USB_PROP_IS_RAM)
  474. PROGMEM
  475. #endif
  476. const int usbDescriptorStringSerialNumber[];
  477. #endif /* __ASSEMBLER__ */
  478. /* ------------------------------------------------------------------------- */
  479. /* ------------------------ General Purpose Macros ------------------------- */
  480. /* ------------------------------------------------------------------------- */
  481. #define USB_CONCAT(a, b) a ## b
  482. #define USB_CONCAT_EXPANDED(a, b) USB_CONCAT(a, b)
  483. #define USB_OUTPORT(name) USB_CONCAT(PORT, name)
  484. #define USB_INPORT(name) USB_CONCAT(PIN, name)
  485. #define USB_DDRPORT(name) USB_CONCAT(DDR, name)
  486. /* The double-define trick above lets us concatenate strings which are
  487. * defined by macros.
  488. */
  489. /* ------------------------------------------------------------------------- */
  490. /* ------------------------- Constant definitions -------------------------- */
  491. /* ------------------------------------------------------------------------- */
  492. #if !defined __ASSEMBLER__ && (!defined USB_CFG_VENDOR_ID || !defined USB_CFG_DEVICE_ID)
  493. #warning "You should define USB_CFG_VENDOR_ID and USB_CFG_DEVICE_ID in usbconfig.h"
  494. /* If the user has not defined IDs, we default to obdev's free IDs.
  495. * See USB-IDs-for-free.txt for details.
  496. */
  497. #endif
  498. /* make sure we have a VID and PID defined, byte order is lowbyte, highbyte */
  499. #ifndef USB_CFG_VENDOR_ID
  500. # define USB_CFG_VENDOR_ID 0xc0, 0x16 /* = 0x16c0 = 5824 = voti.nl */
  501. #endif
  502. #ifndef USB_CFG_DEVICE_ID
  503. # if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH
  504. # define USB_CFG_DEVICE_ID 0xdf, 0x05 /* = 0x5df = 1503, shared PID for HIDs */
  505. # elif USB_CFG_INTERFACE_CLASS == 2
  506. # define USB_CFG_DEVICE_ID 0xe1, 0x05 /* = 0x5e1 = 1505, shared PID for CDC Modems */
  507. # else
  508. # define USB_CFG_DEVICE_ID 0xdc, 0x05 /* = 0x5dc = 1500, obdev's free PID */
  509. # endif
  510. #endif
  511. /* Derive Output, Input and DataDirection ports from port names */
  512. #ifndef USB_CFG_IOPORTNAME
  513. #error "You must define USB_CFG_IOPORTNAME in usbconfig.h, see usbconfig-prototype.h"
  514. #endif
  515. #define USBOUT USB_OUTPORT(USB_CFG_IOPORTNAME)
  516. #define USB_PULLUP_OUT USB_OUTPORT(USB_CFG_PULLUP_IOPORTNAME)
  517. #define USBIN USB_INPORT(USB_CFG_IOPORTNAME)
  518. #define USBDDR USB_DDRPORT(USB_CFG_IOPORTNAME)
  519. #define USB_PULLUP_DDR USB_DDRPORT(USB_CFG_PULLUP_IOPORTNAME)
  520. #define USBMINUS USB_CFG_DMINUS_BIT
  521. #define USBPLUS USB_CFG_DPLUS_BIT
  522. #define USBIDLE (1<<USB_CFG_DMINUS_BIT) /* value representing J state */
  523. #define USBMASK ((1<<USB_CFG_DPLUS_BIT) | (1<<USB_CFG_DMINUS_BIT)) /* mask for USB I/O bits */
  524. /* defines for backward compatibility with older driver versions: */
  525. #define USB_CFG_IOPORT USB_OUTPORT(USB_CFG_IOPORTNAME)
  526. #ifdef USB_CFG_PULLUP_IOPORTNAME
  527. #define USB_CFG_PULLUP_IOPORT USB_OUTPORT(USB_CFG_PULLUP_IOPORTNAME)
  528. #endif
  529. #ifndef USB_CFG_EP3_NUMBER /* if not defined in usbconfig.h */
  530. #define USB_CFG_EP3_NUMBER 3
  531. #endif
  532. #ifndef USB_CFG_HAVE_INTRIN_ENDPOINT3
  533. #define USB_CFG_HAVE_INTRIN_ENDPOINT3 0
  534. #endif
  535. #define USB_BUFSIZE 11 /* PID, 8 bytes data, 2 bytes CRC */
  536. /* ----- Try to find registers and bits responsible for ext interrupt 0 ----- */
  537. #ifndef USB_INTR_CFG /* allow user to override our default */
  538. # if defined EICRA
  539. # define USB_INTR_CFG EICRA
  540. # else
  541. # define USB_INTR_CFG MCUCR
  542. # endif
  543. #endif
  544. #ifndef USB_INTR_CFG_SET /* allow user to override our default */
  545. # if defined(USB_COUNT_SOF) || defined(USB_SOF_HOOK)
  546. # define USB_INTR_CFG_SET (1 << ISC01) /* cfg for falling edge */
  547. /* If any SOF logic is used, the interrupt must be wired to D- where
  548. * we better trigger on falling edge
  549. */
  550. # else
  551. # define USB_INTR_CFG_SET ((1 << ISC00) | (1 << ISC01)) /* cfg for rising edge */
  552. # endif
  553. #endif
  554. #ifndef USB_INTR_CFG_CLR /* allow user to override our default */
  555. # define USB_INTR_CFG_CLR 0 /* no bits to clear */
  556. #endif
  557. #ifndef USB_INTR_ENABLE /* allow user to override our default */
  558. # if defined GIMSK
  559. # define USB_INTR_ENABLE GIMSK
  560. # elif defined EIMSK
  561. # define USB_INTR_ENABLE EIMSK
  562. # else
  563. # define USB_INTR_ENABLE GICR
  564. # endif
  565. #endif
  566. #ifndef USB_INTR_ENABLE_BIT /* allow user to override our default */
  567. # define USB_INTR_ENABLE_BIT INT0
  568. #endif
  569. #ifndef USB_INTR_PENDING /* allow user to override our default */
  570. # if defined EIFR
  571. # define USB_INTR_PENDING EIFR
  572. # else
  573. # define USB_INTR_PENDING GIFR
  574. # endif
  575. #endif
  576. #ifndef USB_INTR_PENDING_BIT /* allow user to override our default */
  577. # define USB_INTR_PENDING_BIT INTF0
  578. #endif
  579. /*
  580. The defines above don't work for the following chips
  581. at90c8534: no ISC0?, no PORTB, can't find a data sheet
  582. at86rf401: no PORTB, no MCUCR etc, low clock rate
  583. atmega103: no ISC0? (maybe omission in header, can't find data sheet)
  584. atmega603: not defined in avr-libc
  585. at43usb320, at43usb355, at76c711: have USB anyway
  586. at94k: is different...
  587. at90s1200, attiny11, attiny12, attiny15, attiny28: these have no RAM
  588. */
  589. /* ------------------------------------------------------------------------- */
  590. /* ----------------- USB Specification Constants and Types ----------------- */
  591. /* ------------------------------------------------------------------------- */
  592. /* USB Token values */
  593. #define USBPID_SETUP 0x2d
  594. #define USBPID_OUT 0xe1
  595. #define USBPID_IN 0x69
  596. #define USBPID_DATA0 0xc3
  597. #define USBPID_DATA1 0x4b
  598. #define USBPID_ACK 0xd2
  599. #define USBPID_NAK 0x5a
  600. #define USBPID_STALL 0x1e
  601. #ifndef USB_INITIAL_DATATOKEN
  602. #define USB_INITIAL_DATATOKEN USBPID_DATA1
  603. #endif
  604. #ifndef __ASSEMBLER__
  605. typedef struct usbTxStatus{
  606. volatile uchar len;
  607. uchar buffer[USB_BUFSIZE];
  608. }usbTxStatus_t;
  609. extern usbTxStatus_t usbTxStatus1, usbTxStatus3;
  610. #define usbTxLen1 usbTxStatus1.len
  611. #define usbTxBuf1 usbTxStatus1.buffer
  612. #define usbTxLen3 usbTxStatus3.len
  613. #define usbTxBuf3 usbTxStatus3.buffer
  614. typedef union usbWord{
  615. unsigned word;
  616. uchar bytes[2];
  617. }usbWord_t;
  618. typedef struct usbRequest{
  619. uchar bmRequestType;
  620. uchar bRequest;
  621. usbWord_t wValue;
  622. usbWord_t wIndex;
  623. usbWord_t wLength;
  624. }usbRequest_t;
  625. /* This structure matches the 8 byte setup request */
  626. #endif
  627. /* bmRequestType field in USB setup:
  628. * d t t r r r r r, where
  629. * d ..... direction: 0=host->device, 1=device->host
  630. * t ..... type: 0=standard, 1=class, 2=vendor, 3=reserved
  631. * r ..... recipient: 0=device, 1=interface, 2=endpoint, 3=other
  632. */
  633. /* USB setup recipient values */
  634. #define USBRQ_RCPT_MASK 0x1f
  635. #define USBRQ_RCPT_DEVICE 0
  636. #define USBRQ_RCPT_INTERFACE 1
  637. #define USBRQ_RCPT_ENDPOINT 2
  638. /* USB request type values */
  639. #define USBRQ_TYPE_MASK 0x60
  640. #define USBRQ_TYPE_STANDARD (0<<5)
  641. #define USBRQ_TYPE_CLASS (1<<5)
  642. #define USBRQ_TYPE_VENDOR (2<<5)
  643. /* USB direction values: */
  644. #define USBRQ_DIR_MASK 0x80
  645. #define USBRQ_DIR_HOST_TO_DEVICE (0<<7)
  646. #define USBRQ_DIR_DEVICE_TO_HOST (1<<7)
  647. /* USB Standard Requests */
  648. #define USBRQ_GET_STATUS 0
  649. #define USBRQ_CLEAR_FEATURE 1
  650. #define USBRQ_SET_FEATURE 3
  651. #define USBRQ_SET_ADDRESS 5
  652. #define USBRQ_GET_DESCRIPTOR 6
  653. #define USBRQ_SET_DESCRIPTOR 7
  654. #define USBRQ_GET_CONFIGURATION 8
  655. #define USBRQ_SET_CONFIGURATION 9
  656. #define USBRQ_GET_INTERFACE 10
  657. #define USBRQ_SET_INTERFACE 11
  658. #define USBRQ_SYNCH_FRAME 12
  659. /* USB descriptor constants */
  660. #define USBDESCR_DEVICE 1
  661. #define USBDESCR_CONFIG 2
  662. #define USBDESCR_STRING 3
  663. #define USBDESCR_INTERFACE 4
  664. #define USBDESCR_ENDPOINT 5
  665. #define USBDESCR_HID 0x21
  666. #define USBDESCR_HID_REPORT 0x22
  667. #define USBDESCR_HID_PHYS 0x23
  668. //#define USBATTR_BUSPOWER 0x80 // USB 1.1 does not define this value any more
  669. #define USBATTR_SELFPOWER 0x40
  670. #define USBATTR_REMOTEWAKE 0x20
  671. /* USB HID Requests */
  672. #define USBRQ_HID_GET_REPORT 0x01
  673. #define USBRQ_HID_GET_IDLE 0x02
  674. #define USBRQ_HID_GET_PROTOCOL 0x03
  675. #define USBRQ_HID_SET_REPORT 0x09
  676. #define USBRQ_HID_SET_IDLE 0x0a
  677. #define USBRQ_HID_SET_PROTOCOL 0x0b
  678. /* ------------------------------------------------------------------------- */
  679. #endif /* __usbdrv_h_included__ */