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.

преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /* USB Serial Example for Teensy USB Development Board
  2. * http://www.pjrc.com/teensy/usb_serial.html
  3. * Copyright (c) 2008 PJRC.COM, LLC
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include "usb_common.h"
  24. #include "usb_private.h"
  25. /**************************************************************************
  26. *
  27. * Endpoint Buffer Configuration
  28. *
  29. **************************************************************************/
  30. static const uint8_t PROGMEM endpoint_config_table[] = {
  31. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(DEBUG_TX_SIZE) | DEBUG_TX_BUFFER,
  32. 1, EP_TYPE_INTERRUPT_OUT, EP_SIZE(DEBUG_RX_SIZE) | DEBUG_RX_BUFFER,
  33. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(KEYBOARD_SIZE) | KEYBOARD_BUFFER,
  34. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(MOUSE_SIZE) | MOUSE_BUFFER,
  35. #ifdef JOYSTICK_INTERFACE
  36. 1, EP_TYPE_INTERRUPT_IN, EP_SIZE(JOYSTICK_SIZE) | JOYSTICK_BUFFER,
  37. 0
  38. #endif
  39. };
  40. /**************************************************************************
  41. *
  42. * Descriptor Data
  43. *
  44. **************************************************************************/
  45. // Descriptors are the data that your computer reads when it auto-detects
  46. // this USB device (called "enumeration" in USB lingo). The most commonly
  47. // changed items are editable at the top of this file. Changing things
  48. // in here should only be done by those who've read chapter 9 of the USB
  49. // spec and relevant portions of any USB class specifications!
  50. static const uint8_t PROGMEM device_descriptor[] = {
  51. 18, // bLength
  52. 1, // bDescriptorType
  53. 0x00, 0x02, // bcdUSB
  54. 0, // bDeviceClass
  55. 0, // bDeviceSubClass
  56. 0, // bDeviceProtocol
  57. ENDPOINT0_SIZE, // bMaxPacketSize0
  58. LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
  59. LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
  60. 0x05, 0x01, // bcdDevice
  61. 0, // iManufacturer
  62. 1, // iProduct
  63. 0, // iSerialNumber
  64. 1 // bNumConfigurations
  65. };
  66. // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
  67. static const uint8_t PROGMEM keyboard_hid_report_desc[] = {
  68. 0x05, 0x01, // Usage Page (Generic Desktop),
  69. 0x09, 0x06, // Usage (Keyboard),
  70. 0xA1, 0x01, // Collection (Application),
  71. 0x75, 0x01, // Report Size (1),
  72. 0x95, 0x08, // Report Count (8),
  73. 0x05, 0x07, // Usage Page (Key Codes),
  74. 0x19, 0xE0, // Usage Minimum (224),
  75. 0x29, 0xE7, // Usage Maximum (231),
  76. 0x15, 0x00, // Logical Minimum (0),
  77. 0x25, 0x01, // Logical Maximum (1),
  78. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
  79. 0x95, 0x08, // Report Count (8),
  80. 0x75, 0x01, // Report Size (1),
  81. 0x15, 0x00, // Logical Minimum (0),
  82. 0x25, 0x01, // Logical Maximum (1),
  83. 0x05, 0x0C, // Usage Page (Consumer),
  84. 0x09, 0xE9, // Usage (Volume Increment),
  85. 0x09, 0xEA, // Usage (Volume Decrement),
  86. 0x09, 0xE2, // Usage (Mute),
  87. 0x09, 0xCD, // Usage (Play/Pause),
  88. 0x09, 0xB5, // Usage (Scan Next Track),
  89. 0x09, 0xB6, // Usage (Scan Previous Track),
  90. 0x09, 0xB7, // Usage (Stop),
  91. 0x09, 0xB8, // Usage (Eject),
  92. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Media keys
  93. 0x95, 0x05, // Report Count (5),
  94. 0x75, 0x01, // Report Size (1),
  95. 0x05, 0x08, // Usage Page (LEDs),
  96. 0x19, 0x01, // Usage Minimum (1),
  97. 0x29, 0x05, // Usage Maximum (5),
  98. 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
  99. 0x95, 0x01, // Report Count (1),
  100. 0x75, 0x03, // Report Size (3),
  101. 0x91, 0x03, // Output (Constant), ;LED report padding
  102. 0x95, 0x06, // Report Count (6),
  103. 0x75, 0x08, // Report Size (8),
  104. 0x15, 0x00, // Logical Minimum (0),
  105. 0x25, 0x7F, // Logical Maximum(104),
  106. 0x05, 0x07, // Usage Page (Key Codes),
  107. 0x19, 0x00, // Usage Minimum (0),
  108. 0x29, 0x7F, // Usage Maximum (104),
  109. 0x81, 0x00, // Input (Data, Array), ;Normal keys
  110. 0xc0 // End Collection
  111. };
  112. // Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
  113. static const uint8_t PROGMEM mouse_hid_report_desc[] = {
  114. 0x05, 0x01, // Usage Page (Generic Desktop)
  115. 0x09, 0x02, // Usage (Mouse)
  116. 0xA1, 0x01, // Collection (Application)
  117. 0x05, 0x09, // Usage Page (Button)
  118. 0x19, 0x01, // Usage Minimum (Button #1)
  119. 0x29, 0x03, // Usage Maximum (Button #3)
  120. 0x15, 0x00, // Logical Minimum (0)
  121. 0x25, 0x01, // Logical Maximum (1)
  122. 0x95, 0x03, // Report Count (3)
  123. 0x75, 0x01, // Report Size (1)
  124. 0x81, 0x02, // Input (Data, Variable, Absolute)
  125. 0x95, 0x01, // Report Count (1)
  126. 0x75, 0x05, // Report Size (5)
  127. 0x81, 0x03, // Input (Constant)
  128. 0x05, 0x01, // Usage Page (Generic Desktop)
  129. 0x09, 0x30, // Usage (X)
  130. 0x09, 0x31, // Usage (Y)
  131. 0x15, 0x81, // Logical Minimum (-127)
  132. 0x25, 0x7F, // Logical Maximum (127)
  133. 0x75, 0x08, // Report Size (8),
  134. 0x95, 0x02, // Report Count (2),
  135. 0x81, 0x06, // Input (Data, Variable, Relative)
  136. 0x09, 0x38, // Usage (Wheel)
  137. 0x95, 0x01, // Report Count (1),
  138. 0x81, 0x06, // Input (Data, Variable, Relative)
  139. 0xC0 // End Collection
  140. };
  141. #ifdef JOYSTICK_INTERFACE
  142. static const uint8_t PROGMEM joystick_hid_report_desc[] = {
  143. 0x05, 0x01, // Usage Page (Generic Desktop)
  144. 0x09, 0x04, // Usage (Joystick)
  145. 0xA1, 0x01, // Collection (Application)
  146. 0x15, 0x00, // Logical Minimum (0)
  147. 0x25, 0x01, // Logical Maximum (1)
  148. 0x75, 0x01, // Report Size (1)
  149. 0x95, 0x20, // Report Count (32)
  150. 0x05, 0x09, // Usage Page (Button)
  151. 0x19, 0x01, // Usage Minimum (Button #1)
  152. 0x29, 0x20, // Usage Maximum (Button #32)
  153. 0x81, 0x02, // Input (variable,absolute)
  154. 0x15, 0x00, // Logical Minimum (0)
  155. 0x25, 0x07, // Logical Maximum (7)
  156. 0x35, 0x00, // Physical Minimum (0)
  157. 0x46, 0x3B, 0x01, // Physical Maximum (315)
  158. 0x75, 0x04, // Report Size (4)
  159. 0x95, 0x01, // Report Count (1)
  160. 0x65, 0x14, // Unit (20)
  161. 0x05, 0x01, // Usage Page (Generic Desktop)
  162. 0x09, 0x39, // Usage (Hat switch)
  163. 0x81, 0x42, // Input (variable,absolute,null_state)
  164. 0x05, 0x01, // Usage Page (Generic Desktop)
  165. 0x09, 0x01, // Usage (Pointer)
  166. 0xA1, 0x00, // Collection ()
  167. 0x15, 0x00, // Logical Minimum (0)
  168. 0x26, 0xFF, 0x03, // Logical Maximum (1023)
  169. 0x75, 0x0A, // Report Size (10)
  170. 0x95, 0x04, // Report Count (4)
  171. 0x09, 0x30, // Usage (X)
  172. 0x09, 0x31, // Usage (Y)
  173. 0x09, 0x32, // Usage (Z)
  174. 0x09, 0x35, // Usage (Rz)
  175. 0x81, 0x02, // Input (variable,absolute)
  176. 0xC0, // End Collection
  177. 0x15, 0x00, // Logical Minimum (0)
  178. 0x26, 0xFF, 0x03, // Logical Maximum (1023)
  179. 0x75, 0x0A, // Report Size (10)
  180. 0x95, 0x02, // Report Count (2)
  181. 0x09, 0x36, // Usage (Slider)
  182. 0x09, 0x36, // Usage (Slider)
  183. 0x81, 0x02, // Input (variable,absolute)
  184. 0xC0 // End Collection
  185. };
  186. #endif
  187. static const uint8_t PROGMEM debug_hid_report_desc[] = {
  188. 0x06, 0xC9, 0xFF, // Usage Page 0xFFC9 (vendor defined)
  189. 0x09, 0x04, // Usage 0x04
  190. 0xA1, 0x5C, // Collection 0x5C
  191. 0x75, 0x08, // report size = 8 bits (global)
  192. 0x15, 0x00, // logical minimum = 0 (global)
  193. 0x26, 0xFF, 0x00, // logical maximum = 255 (global)
  194. 0x95, DEBUG_TX_SIZE, // report count (global)
  195. 0x09, 0x75, // usage (local)
  196. 0x81, 0x02, // Input
  197. 0x95, DEBUG_RX_SIZE, // report count (global)
  198. 0x09, 0x76, // usage (local)
  199. 0x91, 0x02, // Output
  200. 0x95, 0x04, // report count (global)
  201. 0x09, 0x76, // usage (local)
  202. 0xB1, 0x02, // Feature
  203. 0xC0 // end collection
  204. };
  205. #define KEYBOARD_HID_DESC_OFFSET ( 9 + 9 )
  206. #define MOUSE_HID_DESC_OFFSET ( 9 + 9+9+7 + 9 )
  207. #define DEBUG_HID_DESC_OFFSET ( 9 + 9+9+7 + 9+9+7 + 9 )
  208. #ifdef JOYSTICK_INTERFACE
  209. #define JOYSTICK_HID_DESC_OFFSET ( 9 + 9+9+7 + 9+9+7 + 9+9+7+7 + 9 )
  210. #define CONFIG1_DESC_SIZE ( 9 + 9+9+7 + 9+9+7 + 9+9+7+7 + 9+9+7)
  211. #else
  212. #define CONFIG1_DESC_SIZE ( 9 + 9+9+7 + 9+9+7 + 9+9+7+7 )
  213. #endif
  214. static const uint8_t PROGMEM config1_descriptor[CONFIG1_DESC_SIZE] = {
  215. // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
  216. 9, // bLength;
  217. 2, // bDescriptorType;
  218. LSB(CONFIG1_DESC_SIZE), // wTotalLength
  219. MSB(CONFIG1_DESC_SIZE),
  220. NUM_INTERFACE, // bNumInterfaces
  221. 1, // bConfigurationValue
  222. 0, // iConfiguration
  223. 0xC0, // bmAttributes
  224. 50, // bMaxPower
  225. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  226. 9, // bLength
  227. 4, // bDescriptorType
  228. KEYBOARD_INTERFACE, // bInterfaceNumber
  229. 0, // bAlternateSetting
  230. 1, // bNumEndpoints
  231. 0x03, // bInterfaceClass (0x03 = HID)
  232. 0x01, // bInterfaceSubClass (0x01 = Boot)
  233. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  234. 0, // iInterface
  235. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  236. 9, // bLength
  237. 0x21, // bDescriptorType
  238. 0x11, 0x01, // bcdHID
  239. 0, // bCountryCode
  240. 1, // bNumDescriptors
  241. 0x22, // bDescriptorType
  242. sizeof(keyboard_hid_report_desc), // wDescriptorLength
  243. 0,
  244. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  245. 7, // bLength
  246. 5, // bDescriptorType
  247. KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
  248. 0x03, // bmAttributes (0x03=intr)
  249. KEYBOARD_SIZE, 0, // wMaxPacketSize
  250. KEYBOARD_INTERVAL, // bInterval
  251. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  252. 9, // bLength
  253. 4, // bDescriptorType
  254. MOUSE_INTERFACE, // bInterfaceNumber
  255. 0, // bAlternateSetting
  256. 1, // bNumEndpoints
  257. 0x03, // bInterfaceClass (0x03 = HID)
  258. 0x01, // bInterfaceSubClass (0x01 = Boot)
  259. 0x02, // bInterfaceProtocol (0x02 = Mouse)
  260. 0, // iInterface
  261. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  262. 9, // bLength
  263. 0x21, // bDescriptorType
  264. 0x11, 0x01, // bcdHID
  265. 0, // bCountryCode
  266. 1, // bNumDescriptors
  267. 0x22, // bDescriptorType
  268. sizeof(mouse_hid_report_desc), // wDescriptorLength
  269. 0,
  270. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  271. 7, // bLength
  272. 5, // bDescriptorType
  273. MOUSE_ENDPOINT | 0x80, // bEndpointAddress
  274. 0x03, // bmAttributes (0x03=intr)
  275. MOUSE_SIZE, 0, // wMaxPacketSize
  276. MOUSE_INTERVAL, // bInterval
  277. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  278. 9, // bLength
  279. 4, // bDescriptorType
  280. DEBUG_INTERFACE, // bInterfaceNumber
  281. 0, // bAlternateSetting
  282. 2, // bNumEndpoints
  283. 0x03, // bInterfaceClass (0x03 = HID)
  284. 0x00, // bInterfaceSubClass
  285. 0x00, // bInterfaceProtocol
  286. 0, // iInterface
  287. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  288. 9, // bLength
  289. 0x21, // bDescriptorType
  290. 0x11, 0x01, // bcdHID
  291. 0, // bCountryCode
  292. 1, // bNumDescriptors
  293. 0x22, // bDescriptorType
  294. sizeof(debug_hid_report_desc), // wDescriptorLength
  295. 0,
  296. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  297. 7, // bLength
  298. 5, // bDescriptorType
  299. DEBUG_TX_ENDPOINT | 0x80, // bEndpointAddress
  300. 0x03, // bmAttributes (0x03=intr)
  301. DEBUG_TX_SIZE, 0, // wMaxPacketSize
  302. DEBUG_TX_INTERVAL, // bInterval
  303. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  304. 7, // bLength
  305. 5, // bDescriptorType
  306. DEBUG_RX_ENDPOINT, // bEndpointAddress
  307. 0x03, // bmAttributes (0x03=intr)
  308. DEBUG_RX_SIZE, 0, // wMaxPacketSize
  309. DEBUG_RX_INTERVAL, // bInterval
  310. #ifdef JOYSTICK_INTERFACE
  311. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  312. 9, // bLength
  313. 4, // bDescriptorType
  314. JOYSTICK_INTERFACE, // bInterfaceNumber
  315. 0, // bAlternateSetting
  316. 1, // bNumEndpoints
  317. 0x03, // bInterfaceClass (0x03 = HID)
  318. 0x00, // bInterfaceSubClass
  319. 0x00, // bInterfaceProtocol
  320. 0, // iInterface
  321. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  322. 9, // bLength
  323. 0x21, // bDescriptorType
  324. 0x11, 0x01, // bcdHID
  325. 0, // bCountryCode
  326. 1, // bNumDescriptors
  327. 0x22, // bDescriptorType
  328. sizeof(joystick_hid_report_desc), // wDescriptorLength
  329. 0,
  330. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  331. 7, // bLength
  332. 5, // bDescriptorType
  333. JOYSTICK_ENDPOINT | 0x80, // bEndpointAddress
  334. 0x03, // bmAttributes (0x03=intr)
  335. 12, 0, // wMaxPacketSize
  336. JOYSTICK_INTERVAL, // bInterval
  337. #endif
  338. };
  339. // If you're desperate for a little extra code memory, these strings
  340. // can be completely removed if iManufacturer, iProduct, iSerialNumber
  341. // in the device desciptor are changed to zeros.
  342. struct usb_string_descriptor_struct {
  343. uint8_t bLength;
  344. uint8_t bDescriptorType;
  345. int16_t wString[];
  346. };
  347. static const struct usb_string_descriptor_struct PROGMEM string0 = {
  348. 4,
  349. 3,
  350. {0x0409}
  351. };
  352. static const struct usb_string_descriptor_struct PROGMEM string1 = {
  353. sizeof(STR_PRODUCT),
  354. 3,
  355. STR_PRODUCT
  356. };
  357. // This table defines which descriptor data is sent for each specific
  358. // request from the host (in wValue and wIndex).
  359. static const struct descriptor_list_struct {
  360. uint16_t wValue;
  361. uint16_t wIndex;
  362. const uint8_t *addr;
  363. uint8_t length;
  364. } PROGMEM descriptor_list[] = {
  365. {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
  366. {0x0200, 0x0000, config1_descriptor, sizeof(config1_descriptor)},
  367. {0x2200, KEYBOARD_INTERFACE, keyboard_hid_report_desc, sizeof(keyboard_hid_report_desc)},
  368. {0x2100, KEYBOARD_INTERFACE, config1_descriptor+KEYBOARD_HID_DESC_OFFSET, 9},
  369. {0x2200, MOUSE_INTERFACE, mouse_hid_report_desc, sizeof(mouse_hid_report_desc)},
  370. {0x2100, MOUSE_INTERFACE, config1_descriptor+MOUSE_HID_DESC_OFFSET, 9},
  371. {0x2200, DEBUG_INTERFACE, debug_hid_report_desc, sizeof(debug_hid_report_desc)},
  372. {0x2100, DEBUG_INTERFACE, config1_descriptor+DEBUG_HID_DESC_OFFSET, 9},
  373. #ifdef JOYSTICK_INTERFACE
  374. {0x2200, JOYSTICK_INTERFACE, joystick_hid_report_desc, sizeof(joystick_hid_report_desc)},
  375. {0x2100, JOYSTICK_INTERFACE, config1_descriptor+JOYSTICK_HID_DESC_OFFSET, 9},
  376. #endif
  377. {0x0300, 0x0000, (const uint8_t *)&string0, 4},
  378. {0x0301, 0x0409, (const uint8_t *)&string1, sizeof(STR_PRODUCT)},
  379. };
  380. #define NUM_DESC_LIST (sizeof(descriptor_list)/sizeof(struct descriptor_list_struct))
  381. /**************************************************************************
  382. *
  383. * Variables - these are the only non-stack RAM usage
  384. *
  385. **************************************************************************/
  386. // zero when we are not configured, non-zero when enumerated
  387. volatile uint8_t usb_configuration USBSTATE;
  388. volatile uint8_t usb_suspended USBSTATE;
  389. // the time remaining before we transmit any partially full
  390. // packet, or send a zero length packet.
  391. volatile uint8_t debug_flush_timer USBSTATE;
  392. // byte0: which modifier keys are currently pressed
  393. // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
  394. // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
  395. // byte1: media keys (TODO: document these)
  396. // bytes2-7: which keys are currently pressed, up to 6 keys may be down at once
  397. uint8_t keyboard_report_data[8] USBSTATE;
  398. // protocol setting from the host. We use exactly the same report
  399. // either way, so this variable only stores the setting since we
  400. // are required to be able to report which setting is in use.
  401. static uint8_t keyboard_protocol USBSTATE;
  402. // the idle configuration, how often we send the report to the
  403. // host (ms * 4) even when it hasn't changed
  404. static uint8_t keyboard_idle_config USBSTATE;
  405. // count until idle timeout
  406. uint8_t keyboard_idle_count USBSTATE;
  407. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  408. volatile uint8_t keyboard_leds USBSTATE;
  409. // which buttons are currently pressed
  410. uint8_t mouse_buttons USBSTATE;
  411. // protocol setting from the host. We use exactly the same report
  412. // either way, so this variable only stores the setting since we
  413. // are required to be able to report which setting is in use.
  414. static uint8_t mouse_protocol USBSTATE;
  415. // joystick data
  416. #ifdef JOYSTICK_INTERFACE
  417. uint8_t joystick_report_data[12] USBSTATE;
  418. #endif
  419. /**************************************************************************
  420. *
  421. * Public Functions - these are the API intended for the user
  422. *
  423. **************************************************************************/
  424. // initialize USB serial
  425. void usb_init(void)
  426. {
  427. uint8_t u;
  428. u = USBCON;
  429. if ((u & (1<<USBE)) && !(u & (1<<FRZCLK))) return;
  430. HW_CONFIG();
  431. USB_FREEZE(); // enable USB
  432. PLL_CONFIG(); // config PLL
  433. while (!(PLLCSR & (1<<PLOCK))) ; // wait for PLL lock
  434. USB_CONFIG(); // start USB clock
  435. UDCON = 0; // enable attach resistor
  436. usb_configuration = 0;
  437. usb_suspended = 0;
  438. debug_flush_timer = 0;
  439. keyboard_report_data[0] = 0;
  440. keyboard_report_data[1] = 0;
  441. keyboard_report_data[2] = 0;
  442. keyboard_report_data[3] = 0;
  443. keyboard_report_data[4] = 0;
  444. keyboard_report_data[5] = 0;
  445. keyboard_report_data[6] = 0;
  446. keyboard_report_data[7] = 0;
  447. keyboard_protocol = 1;
  448. keyboard_idle_config = 125;
  449. keyboard_idle_count = 0;
  450. keyboard_leds = 0;
  451. mouse_buttons = 0;
  452. mouse_protocol = 1;
  453. #ifdef JOYSTICK_INTERFACE
  454. joystick_report_data[0] = 0;
  455. joystick_report_data[1] = 0;
  456. joystick_report_data[2] = 0;
  457. joystick_report_data[3] = 0;
  458. joystick_report_data[4] = 0x0F;
  459. joystick_report_data[5] = 0x20;
  460. joystick_report_data[6] = 0x80;
  461. joystick_report_data[7] = 0x00;
  462. joystick_report_data[8] = 0x02;
  463. joystick_report_data[9] = 0x08;
  464. joystick_report_data[10] = 0x20;
  465. joystick_report_data[11] = 0x80;
  466. #endif
  467. UDINT = 0;
  468. UDIEN = (1<<EORSTE)|(1<<SOFE);
  469. //sei(); // init() in wiring.c does this
  470. }
  471. void usb_shutdown(void)
  472. {
  473. UDIEN = 0; // disable interrupts
  474. UDCON = 1; // disconnect attach resistor
  475. USBCON = 0; // shut off USB periperal
  476. PLLCSR = 0; // shut off PLL
  477. usb_configuration = 0;
  478. usb_suspended = 1;
  479. }
  480. // Public API functions moved to usb_api.cpp
  481. /**************************************************************************
  482. *
  483. * Private Functions - not intended for general user consumption....
  484. *
  485. **************************************************************************/
  486. // USB Device Interrupt - handle all device-level events
  487. // the transmit buffer flushing is triggered by the start of frame
  488. //
  489. ISR(USB_GEN_vect)
  490. {
  491. uint8_t intbits, t, i;
  492. static uint8_t div4=0;
  493. intbits = UDINT;
  494. UDINT = 0;
  495. if (intbits & (1<<EORSTI)) {
  496. UENUM = 0;
  497. UECONX = 1;
  498. UECFG0X = EP_TYPE_CONTROL;
  499. UECFG1X = EP_SIZE(ENDPOINT0_SIZE) | EP_SINGLE_BUFFER;
  500. UEIENX = (1<<RXSTPE);
  501. usb_configuration = 0;
  502. }
  503. if ((intbits & (1<<SOFI)) && usb_configuration) {
  504. t = debug_flush_timer;
  505. if (t) {
  506. debug_flush_timer = -- t;
  507. if (!t) {
  508. UENUM = DEBUG_TX_ENDPOINT;
  509. while ((UEINTX & (1<<RWAL))) {
  510. UEDATX = 0;
  511. }
  512. UEINTX = 0x3A;
  513. }
  514. }
  515. if (keyboard_idle_config && (++div4 & 3) == 0) {
  516. UENUM = KEYBOARD_ENDPOINT;
  517. if (UEINTX & (1<<RWAL)) {
  518. keyboard_idle_count++;
  519. if (keyboard_idle_count == keyboard_idle_config) {
  520. keyboard_idle_count = 0;
  521. //len = keyboard_protocol ? sizeof(keyboard_keys) : 8;
  522. for (i=0; i < 8; i++) {
  523. UEDATX = keyboard_report_data[i];
  524. }
  525. UEINTX = 0x3A;
  526. }
  527. }
  528. }
  529. }
  530. if (intbits & (1<<SUSPI)) {
  531. // USB Suspend (inactivity for 3ms)
  532. UDIEN = (1<<WAKEUPE);
  533. usb_configuration = 0;
  534. usb_suspended = 1;
  535. #if (F_CPU >= 8000000L)
  536. // WAKEUPI does not work with USB clock freeze
  537. // when CPU is running less than 8 MHz.
  538. // Is this a hardware bug?
  539. USB_FREEZE(); // shut off USB
  540. PLLCSR = 0; // shut off PLL
  541. #endif
  542. // to properly meet the USB spec, current must
  543. // reduce to less than 2.5 mA, which means using
  544. // powerdown mode, but that breaks the Arduino
  545. // user's paradigm....
  546. }
  547. if (usb_suspended && (intbits & (1<<WAKEUPI))) {
  548. // USB Resume (pretty much any activity)
  549. #if (F_CPU >= 8000000L)
  550. PLL_CONFIG();
  551. while (!(PLLCSR & (1<<PLOCK))) ;
  552. USB_CONFIG();
  553. #endif
  554. UDIEN = (1<<EORSTE)|(1<<SOFE)|(1<<SUSPE);
  555. usb_suspended = 0;
  556. return;
  557. }
  558. }
  559. // Misc functions to wait for ready and send/receive packets
  560. static inline void usb_wait_in_ready(void)
  561. {
  562. while (!(UEINTX & (1<<TXINI))) ;
  563. }
  564. static inline void usb_send_in(void)
  565. {
  566. UEINTX = ~(1<<TXINI);
  567. }
  568. static inline void usb_wait_receive_out(void)
  569. {
  570. while (!(UEINTX & (1<<RXOUTI))) ;
  571. }
  572. static inline void usb_ack_out(void)
  573. {
  574. UEINTX = ~(1<<RXOUTI);
  575. }
  576. // USB Endpoint Interrupt - endpoint 0 is handled here. The
  577. // other endpoints are manipulated by the user-callable
  578. // functions, and the start-of-frame interrupt.
  579. //
  580. ISR(USB_COM_vect)
  581. {
  582. uint8_t intbits;
  583. const uint8_t *list;
  584. const uint8_t *cfg;
  585. uint8_t i, n, len, en;
  586. uint8_t bmRequestType;
  587. uint8_t bRequest;
  588. uint16_t wValue;
  589. uint16_t wIndex;
  590. uint16_t wLength;
  591. uint16_t desc_val;
  592. const uint8_t *desc_addr;
  593. uint8_t desc_length;
  594. UENUM = 0;
  595. intbits = UEINTX;
  596. if (intbits & (1<<RXSTPI)) {
  597. bmRequestType = UEDATX;
  598. bRequest = UEDATX;
  599. read_word_lsbfirst(wValue, UEDATX);
  600. read_word_lsbfirst(wIndex, UEDATX);
  601. read_word_lsbfirst(wLength, UEDATX);
  602. UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
  603. if (bRequest == GET_DESCRIPTOR) {
  604. list = (const uint8_t *)descriptor_list;
  605. for (i=0; ; i++) {
  606. if (i >= NUM_DESC_LIST) {
  607. UECONX = (1<<STALLRQ)|(1<<EPEN); //stall
  608. return;
  609. }
  610. pgm_read_word_postinc(desc_val, list);
  611. if (desc_val != wValue) {
  612. list += sizeof(struct descriptor_list_struct)-2;
  613. continue;
  614. }
  615. pgm_read_word_postinc(desc_val, list);
  616. if (desc_val != wIndex) {
  617. list += sizeof(struct descriptor_list_struct)-4;
  618. continue;
  619. }
  620. pgm_read_word_postinc(desc_addr, list);
  621. desc_length = pgm_read_byte(list);
  622. break;
  623. }
  624. len = (wLength < 256) ? wLength : 255;
  625. if (len > desc_length) len = desc_length;
  626. list = desc_addr;
  627. do {
  628. // wait for host ready for IN packet
  629. do {
  630. i = UEINTX;
  631. } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
  632. if (i & (1<<RXOUTI)) return; // abort
  633. // send IN packet
  634. n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
  635. for (i = n; i; i--) {
  636. pgm_read_byte_postinc(UEDATX, list);
  637. }
  638. len -= n;
  639. usb_send_in();
  640. } while (len || n == ENDPOINT0_SIZE);
  641. return;
  642. }
  643. if (bRequest == SET_ADDRESS) {
  644. usb_send_in();
  645. usb_wait_in_ready();
  646. UDADDR = wValue | (1<<ADDEN);
  647. return;
  648. }
  649. if (bRequest == SET_CONFIGURATION && bmRequestType == 0) {
  650. usb_configuration = wValue;
  651. debug_flush_timer = 0;
  652. usb_send_in();
  653. cfg = endpoint_config_table;
  654. for (i=1; i<NUM_ENDPOINTS; i++) {
  655. UENUM = i;
  656. pgm_read_byte_postinc(en, cfg);
  657. UECONX = en;
  658. if (en) {
  659. pgm_read_byte_postinc(UECFG0X, cfg);
  660. pgm_read_byte_postinc(UECFG1X, cfg);
  661. }
  662. }
  663. UERST = 0x1E;
  664. UERST = 0;
  665. return;
  666. }
  667. if (bRequest == GET_CONFIGURATION && bmRequestType == 0x80) {
  668. usb_wait_in_ready();
  669. UEDATX = usb_configuration;
  670. usb_send_in();
  671. return;
  672. }
  673. if (bRequest == GET_STATUS) {
  674. usb_wait_in_ready();
  675. i = 0;
  676. if (bmRequestType == 0x82) {
  677. UENUM = wIndex;
  678. if (UECONX & (1<<STALLRQ)) i = 1;
  679. UENUM = 0;
  680. }
  681. UEDATX = i;
  682. UEDATX = 0;
  683. usb_send_in();
  684. return;
  685. }
  686. if ((bRequest == CLEAR_FEATURE || bRequest == SET_FEATURE)
  687. && bmRequestType == 0x02 && wValue == 0) {
  688. i = wIndex & 0x7F;
  689. if (i >= 1 && i <= NUM_ENDPOINTS) {
  690. usb_send_in();
  691. UENUM = i;
  692. if (bRequest == SET_FEATURE) {
  693. UECONX = (1<<STALLRQ)|(1<<EPEN);
  694. } else {
  695. UECONX = (1<<STALLRQC)|(1<<RSTDT)|(1<<EPEN);
  696. UERST = (1 << i);
  697. UERST = 0;
  698. }
  699. return;
  700. }
  701. }
  702. if (wIndex == KEYBOARD_INTERFACE) {
  703. if (bmRequestType == 0xA1) {
  704. if (bRequest == HID_GET_REPORT) {
  705. usb_wait_in_ready();
  706. //len = keyboard_protocol ? sizeof(keyboard_keys) : 8;
  707. for (i=0; i < 8; i++) {
  708. UEDATX = keyboard_report_data[i];
  709. }
  710. usb_send_in();
  711. return;
  712. }
  713. if (bRequest == HID_GET_IDLE) {
  714. usb_wait_in_ready();
  715. UEDATX = keyboard_idle_config;
  716. usb_send_in();
  717. return;
  718. }
  719. if (bRequest == HID_GET_PROTOCOL) {
  720. usb_wait_in_ready();
  721. UEDATX = keyboard_protocol;
  722. usb_send_in();
  723. return;
  724. }
  725. }
  726. if (bmRequestType == 0x21) {
  727. if (bRequest == HID_SET_REPORT) {
  728. usb_wait_receive_out();
  729. keyboard_leds = UEDATX;
  730. usb_ack_out();
  731. usb_send_in();
  732. return;
  733. }
  734. if (bRequest == HID_SET_IDLE) {
  735. keyboard_idle_config = (wValue >> 8);
  736. keyboard_idle_count = 0;
  737. //usb_wait_in_ready();
  738. usb_send_in();
  739. return;
  740. }
  741. if (bRequest == HID_SET_PROTOCOL) {
  742. keyboard_protocol = wValue;
  743. //usb_wait_in_ready();
  744. usb_send_in();
  745. return;
  746. }
  747. }
  748. }
  749. if (wIndex == MOUSE_INTERFACE) {
  750. if (bmRequestType == 0xA1) {
  751. if (bRequest == HID_GET_REPORT) {
  752. usb_wait_in_ready();
  753. UEDATX = mouse_buttons;
  754. UEDATX = 0;
  755. UEDATX = 0;
  756. UEDATX = 0;
  757. usb_send_in();
  758. return;
  759. }
  760. if (bRequest == HID_GET_PROTOCOL) {
  761. usb_wait_in_ready();
  762. UEDATX = mouse_protocol;
  763. usb_send_in();
  764. return;
  765. }
  766. }
  767. if (bmRequestType == 0x21) {
  768. if (bRequest == HID_SET_PROTOCOL) {
  769. mouse_protocol = wValue;
  770. usb_send_in();
  771. return;
  772. }
  773. }
  774. }
  775. #ifdef JOYSTICK_INTERFACE
  776. if (wIndex == JOYSTICK_INTERFACE) {
  777. if (bmRequestType == 0xA1) {
  778. if (bRequest == HID_GET_REPORT) {
  779. usb_wait_in_ready();
  780. for (i=0; i<12; i++) {
  781. UEDATX = joystick_report_data[i];
  782. }
  783. usb_send_in();
  784. return;
  785. }
  786. }
  787. }
  788. #endif
  789. if (wIndex == DEBUG_INTERFACE) {
  790. if (bRequest == HID_GET_REPORT && bmRequestType == 0xA1) {
  791. len = wLength;
  792. do {
  793. // wait for host ready for IN packet
  794. do {
  795. i = UEINTX;
  796. } while (!(i & ((1<<TXINI)|(1<<RXOUTI))));
  797. if (i & (1<<RXOUTI)) return; // abort
  798. // send IN packet
  799. n = len < ENDPOINT0_SIZE ? len : ENDPOINT0_SIZE;
  800. for (i = n; i; i--) {
  801. UEDATX = 0;
  802. }
  803. len -= n;
  804. usb_send_in();
  805. } while (len || n == ENDPOINT0_SIZE);
  806. return;
  807. }
  808. if (bRequest == HID_SET_REPORT && bmRequestType == 0x21) {
  809. if (wValue == 0x0300 && wLength == 0x0004) {
  810. uint8_t b1, b2, b3, b4;
  811. usb_wait_receive_out();
  812. b1 = UEDATX;
  813. b2 = UEDATX;
  814. b3 = UEDATX;
  815. b4 = UEDATX;
  816. usb_ack_out();
  817. usb_send_in();
  818. if (b1 == 0xA9 && b2 == 0x45 && b3 == 0xC2 && b4 == 0x6B)
  819. _reboot_Teensyduino_();
  820. if (b1 == 0x8B && b2 == 0xC5 && b3 == 0x1D && b4 == 0x70)
  821. _restart_Teensyduino_();
  822. }
  823. }
  824. }
  825. if (bRequest == 0xC9 && bmRequestType == 0x40) {
  826. usb_send_in();
  827. usb_wait_in_ready();
  828. _restart_Teensyduino_();
  829. }
  830. }
  831. UECONX = (1<<STALLRQ) | (1<<EPEN); // stall
  832. }