Teensy 4.1 core updated for C++20
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.

925 lines
36KB

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