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.

преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #if F_CPU >= 20000000
  31. #include "usb_desc.h"
  32. #include "usb_names.h"
  33. #include "mk20dx128.h"
  34. #include "avr_functions.h"
  35. // USB Descriptors are binary data which the USB host reads to
  36. // automatically detect a USB device's capabilities. The format
  37. // and meaning of every field is documented in numerous USB
  38. // standards. When working with USB descriptors, despite the
  39. // complexity of the standards and poor writing quality in many
  40. // of those documents, remember descriptors are nothing more
  41. // than constant binary data that tells the USB host what the
  42. // device can do. Computers will load drivers based on this data.
  43. // Those drivers then communicate on the endpoints specified by
  44. // the descriptors.
  45. // To configure a new combination of interfaces or make minor
  46. // changes to existing configuration (eg, change the name or ID
  47. // numbers), usually you would edit "usb_desc.h". This file
  48. // is meant to be configured by the header, so generally it is
  49. // only edited to add completely new USB interfaces or features.
  50. // **************************************************************
  51. // USB Device
  52. // **************************************************************
  53. #define LSB(n) ((n) & 255)
  54. #define MSB(n) (((n) >> 8) & 255)
  55. // USB Device Descriptor. The USB host reads this first, to learn
  56. // what type of device is connected.
  57. static uint8_t device_descriptor[] = {
  58. 18, // bLength
  59. 1, // bDescriptorType
  60. 0x00, 0x02, // bcdUSB
  61. #ifdef DEVICE_CLASS
  62. DEVICE_CLASS, // bDeviceClass
  63. #else
  64. 0,
  65. #endif
  66. #ifdef DEVICE_SUBCLASS
  67. DEVICE_SUBCLASS, // bDeviceSubClass
  68. #else
  69. 0,
  70. #endif
  71. #ifdef DEVICE_PROTOCOL
  72. DEVICE_PROTOCOL, // bDeviceProtocol
  73. #else
  74. 0,
  75. #endif
  76. EP0_SIZE, // bMaxPacketSize0
  77. LSB(VENDOR_ID), MSB(VENDOR_ID), // idVendor
  78. LSB(PRODUCT_ID), MSB(PRODUCT_ID), // idProduct
  79. 0x00, 0x01, // bcdDevice
  80. 1, // iManufacturer
  81. 2, // iProduct
  82. 3, // iSerialNumber
  83. 1 // bNumConfigurations
  84. };
  85. // These descriptors must NOT be "const", because the USB DMA
  86. // has trouble accessing flash memory with enough bandwidth
  87. // while the processor is executing from flash.
  88. // **************************************************************
  89. // HID Report Descriptors
  90. // **************************************************************
  91. // Each HID interface needs a special report descriptor that tells
  92. // the meaning and format of the data.
  93. #ifdef KEYBOARD_INTERFACE
  94. // Keyboard Protocol 1, HID 1.11 spec, Appendix B, page 59-60
  95. static uint8_t keyboard_report_desc[] = {
  96. 0x05, 0x01, // Usage Page (Generic Desktop),
  97. 0x09, 0x06, // Usage (Keyboard),
  98. 0xA1, 0x01, // Collection (Application),
  99. 0x75, 0x01, // Report Size (1),
  100. 0x95, 0x08, // Report Count (8),
  101. 0x05, 0x07, // Usage Page (Key Codes),
  102. 0x19, 0xE0, // Usage Minimum (224),
  103. 0x29, 0xE7, // Usage Maximum (231),
  104. 0x15, 0x00, // Logical Minimum (0),
  105. 0x25, 0x01, // Logical Maximum (1),
  106. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Modifier byte
  107. 0x95, 0x08, // Report Count (8),
  108. 0x75, 0x01, // Report Size (1),
  109. 0x15, 0x00, // Logical Minimum (0),
  110. 0x25, 0x01, // Logical Maximum (1),
  111. 0x05, 0x0C, // Usage Page (Consumer),
  112. 0x09, 0xE9, // Usage (Volume Increment),
  113. 0x09, 0xEA, // Usage (Volume Decrement),
  114. 0x09, 0xE2, // Usage (Mute),
  115. 0x09, 0xCD, // Usage (Play/Pause),
  116. 0x09, 0xB5, // Usage (Scan Next Track),
  117. 0x09, 0xB6, // Usage (Scan Previous Track),
  118. 0x09, 0xB7, // Usage (Stop),
  119. 0x09, 0xB8, // Usage (Eject),
  120. 0x81, 0x02, // Input (Data, Variable, Absolute), ;Media keys
  121. 0x95, 0x05, // Report Count (5),
  122. 0x75, 0x01, // Report Size (1),
  123. 0x05, 0x08, // Usage Page (LEDs),
  124. 0x19, 0x01, // Usage Minimum (1),
  125. 0x29, 0x05, // Usage Maximum (5),
  126. 0x91, 0x02, // Output (Data, Variable, Absolute), ;LED report
  127. 0x95, 0x01, // Report Count (1),
  128. 0x75, 0x03, // Report Size (3),
  129. 0x91, 0x03, // Output (Constant), ;LED report padding
  130. 0x95, 0x06, // Report Count (6),
  131. 0x75, 0x08, // Report Size (8),
  132. 0x15, 0x00, // Logical Minimum (0),
  133. 0x25, 0x7F, // Logical Maximum(104),
  134. 0x05, 0x07, // Usage Page (Key Codes),
  135. 0x19, 0x00, // Usage Minimum (0),
  136. 0x29, 0x7F, // Usage Maximum (104),
  137. 0x81, 0x00, // Input (Data, Array), ;Normal keys
  138. 0xc0 // End Collection
  139. };
  140. #endif
  141. #ifdef MOUSE_INTERFACE
  142. // Mouse Protocol 1, HID 1.11 spec, Appendix B, page 59-60, with wheel extension
  143. static uint8_t mouse_report_desc[] = {
  144. 0x05, 0x01, // Usage Page (Generic Desktop)
  145. 0x09, 0x02, // Usage (Mouse)
  146. 0xA1, 0x01, // Collection (Application)
  147. 0x85, 0x01, // REPORT_ID (1)
  148. 0x05, 0x09, // Usage Page (Button)
  149. 0x19, 0x01, // Usage Minimum (Button #1)
  150. 0x29, 0x08, // Usage Maximum (Button #8)
  151. 0x15, 0x00, // Logical Minimum (0)
  152. 0x25, 0x01, // Logical Maximum (1)
  153. 0x95, 0x08, // Report Count (8)
  154. 0x75, 0x01, // Report Size (1)
  155. 0x81, 0x02, // Input (Data, Variable, Absolute)
  156. 0x05, 0x01, // Usage Page (Generic Desktop)
  157. 0x09, 0x30, // Usage (X)
  158. 0x09, 0x31, // Usage (Y)
  159. 0x09, 0x38, // Usage (Wheel)
  160. 0x15, 0x81, // Logical Minimum (-127)
  161. 0x25, 0x7F, // Logical Maximum (127)
  162. 0x75, 0x08, // Report Size (8),
  163. 0x95, 0x03, // Report Count (3),
  164. 0x81, 0x06, // Input (Data, Variable, Relative)
  165. 0xC0, // End Collection
  166. 0x05, 0x01, // Usage Page (Generic Desktop)
  167. 0x09, 0x02, // Usage (Mouse)
  168. 0xA1, 0x01, // Collection (Application)
  169. 0x85, 0x02, // REPORT_ID (2)
  170. 0x05, 0x01, // Usage Page (Generic Desktop)
  171. 0x09, 0x30, // Usage (X)
  172. 0x09, 0x31, // Usage (Y)
  173. 0x15, 0x00, // Logical Minimum (0)
  174. 0x26, 0xFF, 0x7F, // Logical Maximum (32767)
  175. 0x75, 0x10, // Report Size (16),
  176. 0x95, 0x02, // Report Count (2),
  177. 0x81, 0x02, // Input (Data, Variable, Absolute)
  178. 0xC0 // End Collection
  179. };
  180. #endif
  181. #ifdef JOYSTICK_INTERFACE
  182. static uint8_t joystick_report_desc[] = {
  183. 0x05, 0x01, // Usage Page (Generic Desktop)
  184. 0x09, 0x04, // Usage (Joystick)
  185. 0xA1, 0x01, // Collection (Application)
  186. 0x15, 0x00, // Logical Minimum (0)
  187. 0x25, 0x01, // Logical Maximum (1)
  188. 0x75, 0x01, // Report Size (1)
  189. 0x95, 0x20, // Report Count (32)
  190. 0x05, 0x09, // Usage Page (Button)
  191. 0x19, 0x01, // Usage Minimum (Button #1)
  192. 0x29, 0x20, // Usage Maximum (Button #32)
  193. 0x81, 0x02, // Input (variable,absolute)
  194. 0x15, 0x00, // Logical Minimum (0)
  195. 0x25, 0x07, // Logical Maximum (7)
  196. 0x35, 0x00, // Physical Minimum (0)
  197. 0x46, 0x3B, 0x01, // Physical Maximum (315)
  198. 0x75, 0x04, // Report Size (4)
  199. 0x95, 0x01, // Report Count (1)
  200. 0x65, 0x14, // Unit (20)
  201. 0x05, 0x01, // Usage Page (Generic Desktop)
  202. 0x09, 0x39, // Usage (Hat switch)
  203. 0x81, 0x42, // Input (variable,absolute,null_state)
  204. 0x05, 0x01, // Usage Page (Generic Desktop)
  205. 0x09, 0x01, // Usage (Pointer)
  206. 0xA1, 0x00, // Collection ()
  207. 0x15, 0x00, // Logical Minimum (0)
  208. 0x26, 0xFF, 0x03, // Logical Maximum (1023)
  209. 0x75, 0x0A, // Report Size (10)
  210. 0x95, 0x04, // Report Count (4)
  211. 0x09, 0x30, // Usage (X)
  212. 0x09, 0x31, // Usage (Y)
  213. 0x09, 0x32, // Usage (Z)
  214. 0x09, 0x35, // Usage (Rz)
  215. 0x81, 0x02, // Input (variable,absolute)
  216. 0xC0, // End Collection
  217. 0x15, 0x00, // Logical Minimum (0)
  218. 0x26, 0xFF, 0x03, // Logical Maximum (1023)
  219. 0x75, 0x0A, // Report Size (10)
  220. 0x95, 0x02, // Report Count (2)
  221. 0x09, 0x36, // Usage (Slider)
  222. 0x09, 0x36, // Usage (Slider)
  223. 0x81, 0x02, // Input (variable,absolute)
  224. 0xC0 // End Collection
  225. };
  226. #endif
  227. #ifdef SEREMU_INTERFACE
  228. static uint8_t seremu_report_desc[] = {
  229. 0x06, 0xC9, 0xFF, // Usage Page 0xFFC9 (vendor defined)
  230. 0x09, 0x04, // Usage 0x04
  231. 0xA1, 0x5C, // Collection 0x5C
  232. 0x75, 0x08, // report size = 8 bits (global)
  233. 0x15, 0x00, // logical minimum = 0 (global)
  234. 0x26, 0xFF, 0x00, // logical maximum = 255 (global)
  235. 0x95, SEREMU_TX_SIZE, // report count (global)
  236. 0x09, 0x75, // usage (local)
  237. 0x81, 0x02, // Input
  238. 0x95, SEREMU_RX_SIZE, // report count (global)
  239. 0x09, 0x76, // usage (local)
  240. 0x91, 0x02, // Output
  241. 0x95, 0x04, // report count (global)
  242. 0x09, 0x76, // usage (local)
  243. 0xB1, 0x02, // Feature
  244. 0xC0 // end collection
  245. };
  246. #endif
  247. #ifdef RAWHID_INTERFACE
  248. static uint8_t rawhid_report_desc[] = {
  249. 0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE),
  250. 0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE),
  251. 0xA1, 0x01, // Collection 0x01
  252. 0x75, 0x08, // report size = 8 bits
  253. 0x15, 0x00, // logical minimum = 0
  254. 0x26, 0xFF, 0x00, // logical maximum = 255
  255. 0x95, RAWHID_TX_SIZE, // report count
  256. 0x09, 0x01, // usage
  257. 0x81, 0x02, // Input (array)
  258. 0x95, RAWHID_RX_SIZE, // report count
  259. 0x09, 0x02, // usage
  260. 0x91, 0x02, // Output (array)
  261. 0xC0 // end collection
  262. };
  263. #endif
  264. #ifdef FLIGHTSIM_INTERFACE
  265. static uint8_t flightsim_report_desc[] = {
  266. 0x06, 0x1C, 0xFF, // Usage page = 0xFF1C
  267. 0x0A, 0x39, 0xA7, // Usage = 0xA739
  268. 0xA1, 0x01, // Collection 0x01
  269. 0x75, 0x08, // report size = 8 bits
  270. 0x15, 0x00, // logical minimum = 0
  271. 0x26, 0xFF, 0x00, // logical maximum = 255
  272. 0x95, FLIGHTSIM_TX_SIZE, // report count
  273. 0x09, 0x01, // usage
  274. 0x81, 0x02, // Input (array)
  275. 0x95, FLIGHTSIM_RX_SIZE, // report count
  276. 0x09, 0x02, // usage
  277. 0x91, 0x02, // Output (array)
  278. 0xC0 // end collection
  279. };
  280. #endif
  281. // **************************************************************
  282. // USB Configuration
  283. // **************************************************************
  284. // USB Configuration Descriptor. This huge descriptor tells all
  285. // of the devices capbilities.
  286. static uint8_t config_descriptor[CONFIG_DESC_SIZE] = {
  287. // configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
  288. 9, // bLength;
  289. 2, // bDescriptorType;
  290. LSB(CONFIG_DESC_SIZE), // wTotalLength
  291. MSB(CONFIG_DESC_SIZE),
  292. NUM_INTERFACE, // bNumInterfaces
  293. 1, // bConfigurationValue
  294. 0, // iConfiguration
  295. 0xC0, // bmAttributes
  296. 50, // bMaxPower
  297. #ifdef CDC_IAD_DESCRIPTOR
  298. // interface association descriptor, USB ECN, Table 9-Z
  299. 8, // bLength
  300. 11, // bDescriptorType
  301. CDC_STATUS_INTERFACE, // bFirstInterface
  302. 2, // bInterfaceCount
  303. 0x02, // bFunctionClass
  304. 0x02, // bFunctionSubClass
  305. 0x01, // bFunctionProtocol
  306. 4, // iFunction
  307. #endif
  308. #ifdef CDC_DATA_INTERFACE
  309. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  310. 9, // bLength
  311. 4, // bDescriptorType
  312. CDC_STATUS_INTERFACE, // bInterfaceNumber
  313. 0, // bAlternateSetting
  314. 1, // bNumEndpoints
  315. 0x02, // bInterfaceClass
  316. 0x02, // bInterfaceSubClass
  317. 0x01, // bInterfaceProtocol
  318. 0, // iInterface
  319. // CDC Header Functional Descriptor, CDC Spec 5.2.3.1, Table 26
  320. 5, // bFunctionLength
  321. 0x24, // bDescriptorType
  322. 0x00, // bDescriptorSubtype
  323. 0x10, 0x01, // bcdCDC
  324. // Call Management Functional Descriptor, CDC Spec 5.2.3.2, Table 27
  325. 5, // bFunctionLength
  326. 0x24, // bDescriptorType
  327. 0x01, // bDescriptorSubtype
  328. 0x01, // bmCapabilities
  329. 1, // bDataInterface
  330. // Abstract Control Management Functional Descriptor, CDC Spec 5.2.3.3, Table 28
  331. 4, // bFunctionLength
  332. 0x24, // bDescriptorType
  333. 0x02, // bDescriptorSubtype
  334. 0x06, // bmCapabilities
  335. // Union Functional Descriptor, CDC Spec 5.2.3.8, Table 33
  336. 5, // bFunctionLength
  337. 0x24, // bDescriptorType
  338. 0x06, // bDescriptorSubtype
  339. CDC_STATUS_INTERFACE, // bMasterInterface
  340. CDC_DATA_INTERFACE, // bSlaveInterface0
  341. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  342. 7, // bLength
  343. 5, // bDescriptorType
  344. CDC_ACM_ENDPOINT | 0x80, // bEndpointAddress
  345. 0x03, // bmAttributes (0x03=intr)
  346. CDC_ACM_SIZE, 0, // wMaxPacketSize
  347. 64, // bInterval
  348. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  349. 9, // bLength
  350. 4, // bDescriptorType
  351. CDC_DATA_INTERFACE, // bInterfaceNumber
  352. 0, // bAlternateSetting
  353. 2, // bNumEndpoints
  354. 0x0A, // bInterfaceClass
  355. 0x00, // bInterfaceSubClass
  356. 0x00, // bInterfaceProtocol
  357. 0, // iInterface
  358. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  359. 7, // bLength
  360. 5, // bDescriptorType
  361. CDC_RX_ENDPOINT, // bEndpointAddress
  362. 0x02, // bmAttributes (0x02=bulk)
  363. CDC_RX_SIZE, 0, // wMaxPacketSize
  364. 0, // bInterval
  365. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  366. 7, // bLength
  367. 5, // bDescriptorType
  368. CDC_TX_ENDPOINT | 0x80, // bEndpointAddress
  369. 0x02, // bmAttributes (0x02=bulk)
  370. CDC_TX_SIZE, 0, // wMaxPacketSize
  371. 0, // bInterval
  372. #endif // CDC_DATA_INTERFACE
  373. #ifdef MIDI_INTERFACE
  374. // Standard MS Interface Descriptor,
  375. 9, // bLength
  376. 4, // bDescriptorType
  377. MIDI_INTERFACE, // bInterfaceNumber
  378. 0, // bAlternateSetting
  379. 2, // bNumEndpoints
  380. 0x01, // bInterfaceClass (0x01 = Audio)
  381. 0x03, // bInterfaceSubClass (0x03 = MIDI)
  382. 0x00, // bInterfaceProtocol (unused for MIDI)
  383. 0, // iInterface
  384. // MIDI MS Interface Header, USB MIDI 6.1.2.1, page 21, Table 6-2
  385. 7, // bLength
  386. 0x24, // bDescriptorType = CS_INTERFACE
  387. 0x01, // bDescriptorSubtype = MS_HEADER
  388. 0x00, 0x01, // bcdMSC = revision 01.00
  389. 0x41, 0x00, // wTotalLength
  390. // MIDI IN Jack Descriptor, B.4.3, Table B-7 (embedded), page 40
  391. 6, // bLength
  392. 0x24, // bDescriptorType = CS_INTERFACE
  393. 0x02, // bDescriptorSubtype = MIDI_IN_JACK
  394. 0x01, // bJackType = EMBEDDED
  395. 1, // bJackID, ID = 1
  396. 0, // iJack
  397. // MIDI IN Jack Descriptor, B.4.3, Table B-8 (external), page 40
  398. 6, // bLength
  399. 0x24, // bDescriptorType = CS_INTERFACE
  400. 0x02, // bDescriptorSubtype = MIDI_IN_JACK
  401. 0x02, // bJackType = EXTERNAL
  402. 2, // bJackID, ID = 2
  403. 0, // iJack
  404. // MIDI OUT Jack Descriptor, B.4.4, Table B-9, page 41
  405. 9,
  406. 0x24, // bDescriptorType = CS_INTERFACE
  407. 0x03, // bDescriptorSubtype = MIDI_OUT_JACK
  408. 0x01, // bJackType = EMBEDDED
  409. 3, // bJackID, ID = 3
  410. 1, // bNrInputPins = 1 pin
  411. 2, // BaSourceID(1) = 2
  412. 1, // BaSourcePin(1) = first pin
  413. 0, // iJack
  414. // MIDI OUT Jack Descriptor, B.4.4, Table B-10, page 41
  415. 9,
  416. 0x24, // bDescriptorType = CS_INTERFACE
  417. 0x03, // bDescriptorSubtype = MIDI_OUT_JACK
  418. 0x02, // bJackType = EXTERNAL
  419. 4, // bJackID, ID = 4
  420. 1, // bNrInputPins = 1 pin
  421. 1, // BaSourceID(1) = 1
  422. 1, // BaSourcePin(1) = first pin
  423. 0, // iJack
  424. // Standard Bulk OUT Endpoint Descriptor, B.5.1, Table B-11, pae 42
  425. 9, // bLength
  426. 5, // bDescriptorType = ENDPOINT
  427. MIDI_RX_ENDPOINT, // bEndpointAddress
  428. 0x02, // bmAttributes (0x02=bulk)
  429. MIDI_RX_SIZE, 0, // wMaxPacketSize
  430. 0, // bInterval
  431. 0, // bRefresh
  432. 0, // bSynchAddress
  433. // Class-specific MS Bulk OUT Endpoint Descriptor, B.5.2, Table B-12, page 42
  434. 5, // bLength
  435. 0x25, // bDescriptorSubtype = CS_ENDPOINT
  436. 0x01, // bJackType = MS_GENERAL
  437. 1, // bNumEmbMIDIJack = 1 jack
  438. 1, // BaAssocJackID(1) = jack ID #1
  439. // Standard Bulk IN Endpoint Descriptor, B.5.1, Table B-11, pae 42
  440. 9, // bLength
  441. 5, // bDescriptorType = ENDPOINT
  442. MIDI_TX_ENDPOINT | 0x80, // bEndpointAddress
  443. 0x02, // bmAttributes (0x02=bulk)
  444. MIDI_TX_SIZE, 0, // wMaxPacketSize
  445. 0, // bInterval
  446. 0, // bRefresh
  447. 0, // bSynchAddress
  448. // Class-specific MS Bulk IN Endpoint Descriptor, B.5.2, Table B-12, page 42
  449. 5, // bLength
  450. 0x25, // bDescriptorSubtype = CS_ENDPOINT
  451. 0x01, // bJackType = MS_GENERAL
  452. 1, // bNumEmbMIDIJack = 1 jack
  453. 3, // BaAssocJackID(1) = jack ID #3
  454. #endif // MIDI_INTERFACE
  455. #ifdef KEYBOARD_INTERFACE
  456. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  457. 9, // bLength
  458. 4, // bDescriptorType
  459. KEYBOARD_INTERFACE, // bInterfaceNumber
  460. 0, // bAlternateSetting
  461. 1, // bNumEndpoints
  462. 0x03, // bInterfaceClass (0x03 = HID)
  463. 0x01, // bInterfaceSubClass (0x01 = Boot)
  464. 0x01, // bInterfaceProtocol (0x01 = Keyboard)
  465. 0, // iInterface
  466. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  467. 9, // bLength
  468. 0x21, // bDescriptorType
  469. 0x11, 0x01, // bcdHID
  470. 0, // bCountryCode
  471. 1, // bNumDescriptors
  472. 0x22, // bDescriptorType
  473. LSB(sizeof(keyboard_report_desc)), // wDescriptorLength
  474. MSB(sizeof(keyboard_report_desc)),
  475. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  476. 7, // bLength
  477. 5, // bDescriptorType
  478. KEYBOARD_ENDPOINT | 0x80, // bEndpointAddress
  479. 0x03, // bmAttributes (0x03=intr)
  480. KEYBOARD_SIZE, 0, // wMaxPacketSize
  481. KEYBOARD_INTERVAL, // bInterval
  482. #endif // KEYBOARD_INTERFACE
  483. #ifdef MOUSE_INTERFACE
  484. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  485. 9, // bLength
  486. 4, // bDescriptorType
  487. MOUSE_INTERFACE, // bInterfaceNumber
  488. 0, // bAlternateSetting
  489. 1, // bNumEndpoints
  490. 0x03, // bInterfaceClass (0x03 = HID)
  491. 0x00, // bInterfaceSubClass (0x01 = Boot)
  492. 0x00, // bInterfaceProtocol (0x02 = Mouse)
  493. 0, // iInterface
  494. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  495. 9, // bLength
  496. 0x21, // bDescriptorType
  497. 0x11, 0x01, // bcdHID
  498. 0, // bCountryCode
  499. 1, // bNumDescriptors
  500. 0x22, // bDescriptorType
  501. LSB(sizeof(mouse_report_desc)), // wDescriptorLength
  502. MSB(sizeof(mouse_report_desc)),
  503. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  504. 7, // bLength
  505. 5, // bDescriptorType
  506. MOUSE_ENDPOINT | 0x80, // bEndpointAddress
  507. 0x03, // bmAttributes (0x03=intr)
  508. MOUSE_SIZE, 0, // wMaxPacketSize
  509. MOUSE_INTERVAL, // bInterval
  510. #endif // MOUSE_INTERFACE
  511. #ifdef RAWHID_INTERFACE
  512. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  513. 9, // bLength
  514. 4, // bDescriptorType
  515. RAWHID_INTERFACE, // bInterfaceNumber
  516. 0, // bAlternateSetting
  517. 2, // bNumEndpoints
  518. 0x03, // bInterfaceClass (0x03 = HID)
  519. 0x00, // bInterfaceSubClass
  520. 0x00, // bInterfaceProtocol
  521. 0, // iInterface
  522. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  523. 9, // bLength
  524. 0x21, // bDescriptorType
  525. 0x11, 0x01, // bcdHID
  526. 0, // bCountryCode
  527. 1, // bNumDescriptors
  528. 0x22, // bDescriptorType
  529. LSB(sizeof(rawhid_report_desc)), // wDescriptorLength
  530. MSB(sizeof(rawhid_report_desc)),
  531. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  532. 7, // bLength
  533. 5, // bDescriptorType
  534. RAWHID_TX_ENDPOINT | 0x80, // bEndpointAddress
  535. 0x03, // bmAttributes (0x03=intr)
  536. RAWHID_TX_SIZE, 0, // wMaxPacketSize
  537. RAWHID_TX_INTERVAL, // bInterval
  538. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  539. 7, // bLength
  540. 5, // bDescriptorType
  541. RAWHID_RX_ENDPOINT, // bEndpointAddress
  542. 0x03, // bmAttributes (0x03=intr)
  543. RAWHID_RX_SIZE, 0, // wMaxPacketSize
  544. RAWHID_RX_INTERVAL, // bInterval
  545. #endif // RAWHID_INTERFACE
  546. #ifdef FLIGHTSIM_INTERFACE
  547. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  548. 9, // bLength
  549. 4, // bDescriptorType
  550. FLIGHTSIM_INTERFACE, // bInterfaceNumber
  551. 0, // bAlternateSetting
  552. 2, // bNumEndpoints
  553. 0x03, // bInterfaceClass (0x03 = HID)
  554. 0x00, // bInterfaceSubClass
  555. 0x00, // bInterfaceProtocol
  556. 0, // iInterface
  557. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  558. 9, // bLength
  559. 0x21, // bDescriptorType
  560. 0x11, 0x01, // bcdHID
  561. 0, // bCountryCode
  562. 1, // bNumDescriptors
  563. 0x22, // bDescriptorType
  564. LSB(sizeof(flightsim_report_desc)), // wDescriptorLength
  565. MSB(sizeof(flightsim_report_desc)),
  566. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  567. 7, // bLength
  568. 5, // bDescriptorType
  569. FLIGHTSIM_TX_ENDPOINT | 0x80, // bEndpointAddress
  570. 0x03, // bmAttributes (0x03=intr)
  571. FLIGHTSIM_TX_SIZE, 0, // wMaxPacketSize
  572. FLIGHTSIM_TX_INTERVAL, // bInterval
  573. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  574. 7, // bLength
  575. 5, // bDescriptorType
  576. FLIGHTSIM_RX_ENDPOINT, // bEndpointAddress
  577. 0x03, // bmAttributes (0x03=intr)
  578. FLIGHTSIM_RX_SIZE, 0, // wMaxPacketSize
  579. FLIGHTSIM_RX_INTERVAL, // bInterval
  580. #endif // FLIGHTSIM_INTERFACE
  581. #ifdef SEREMU_INTERFACE
  582. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  583. 9, // bLength
  584. 4, // bDescriptorType
  585. SEREMU_INTERFACE, // bInterfaceNumber
  586. 0, // bAlternateSetting
  587. 2, // bNumEndpoints
  588. 0x03, // bInterfaceClass (0x03 = HID)
  589. 0x00, // bInterfaceSubClass
  590. 0x00, // bInterfaceProtocol
  591. 0, // iInterface
  592. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  593. 9, // bLength
  594. 0x21, // bDescriptorType
  595. 0x11, 0x01, // bcdHID
  596. 0, // bCountryCode
  597. 1, // bNumDescriptors
  598. 0x22, // bDescriptorType
  599. LSB(sizeof(seremu_report_desc)), // wDescriptorLength
  600. MSB(sizeof(seremu_report_desc)),
  601. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  602. 7, // bLength
  603. 5, // bDescriptorType
  604. SEREMU_TX_ENDPOINT | 0x80, // bEndpointAddress
  605. 0x03, // bmAttributes (0x03=intr)
  606. SEREMU_TX_SIZE, 0, // wMaxPacketSize
  607. SEREMU_TX_INTERVAL, // bInterval
  608. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  609. 7, // bLength
  610. 5, // bDescriptorType
  611. SEREMU_RX_ENDPOINT, // bEndpointAddress
  612. 0x03, // bmAttributes (0x03=intr)
  613. SEREMU_RX_SIZE, 0, // wMaxPacketSize
  614. SEREMU_RX_INTERVAL, // bInterval
  615. #endif // SEREMU_INTERFACE
  616. #ifdef JOYSTICK_INTERFACE
  617. // interface descriptor, USB spec 9.6.5, page 267-269, Table 9-12
  618. 9, // bLength
  619. 4, // bDescriptorType
  620. JOYSTICK_INTERFACE, // bInterfaceNumber
  621. 0, // bAlternateSetting
  622. 1, // bNumEndpoints
  623. 0x03, // bInterfaceClass (0x03 = HID)
  624. 0x00, // bInterfaceSubClass
  625. 0x00, // bInterfaceProtocol
  626. 0, // iInterface
  627. // HID interface descriptor, HID 1.11 spec, section 6.2.1
  628. 9, // bLength
  629. 0x21, // bDescriptorType
  630. 0x11, 0x01, // bcdHID
  631. 0, // bCountryCode
  632. 1, // bNumDescriptors
  633. 0x22, // bDescriptorType
  634. LSB(sizeof(joystick_report_desc)), // wDescriptorLength
  635. MSB(sizeof(joystick_report_desc)),
  636. // endpoint descriptor, USB spec 9.6.6, page 269-271, Table 9-13
  637. 7, // bLength
  638. 5, // bDescriptorType
  639. JOYSTICK_ENDPOINT | 0x80, // bEndpointAddress
  640. 0x03, // bmAttributes (0x03=intr)
  641. JOYSTICK_SIZE, 0, // wMaxPacketSize
  642. JOYSTICK_INTERVAL, // bInterval
  643. #endif // JOYSTICK_INTERFACE
  644. };
  645. // **************************************************************
  646. // String Descriptors
  647. // **************************************************************
  648. // The descriptors above can provide human readable strings,
  649. // referenced by index numbers. These descriptors are the
  650. // actual string data
  651. /* defined in usb_names.h
  652. struct usb_string_descriptor_struct {
  653. uint8_t bLength;
  654. uint8_t bDescriptorType;
  655. uint16_t wString[];
  656. };
  657. */
  658. extern struct usb_string_descriptor_struct usb_string_manufacturer_name
  659. __attribute__ ((weak, alias("usb_string_manufacturer_name_default")));
  660. extern struct usb_string_descriptor_struct usb_string_product_name
  661. __attribute__ ((weak, alias("usb_string_product_name_default")));
  662. extern struct usb_string_descriptor_struct usb_string_serial_number
  663. __attribute__ ((weak, alias("usb_string_serial_number_default")));
  664. struct usb_string_descriptor_struct string0 = {
  665. 4,
  666. 3,
  667. {0x0409}
  668. };
  669. struct usb_string_descriptor_struct usb_string_manufacturer_name_default = {
  670. 2 + MANUFACTURER_NAME_LEN * 2,
  671. 3,
  672. MANUFACTURER_NAME
  673. };
  674. struct usb_string_descriptor_struct usb_string_product_name_default = {
  675. 2 + PRODUCT_NAME_LEN * 2,
  676. 3,
  677. PRODUCT_NAME
  678. };
  679. struct usb_string_descriptor_struct usb_string_serial_number_default = {
  680. 12,
  681. 3,
  682. {0,0,0,0,0,0,0,0,0,0}
  683. };
  684. void usb_init_serialnumber(void)
  685. {
  686. char buf[11];
  687. uint32_t i, num;
  688. __disable_irq();
  689. FTFL_FSTAT = FTFL_FSTAT_RDCOLERR | FTFL_FSTAT_ACCERR | FTFL_FSTAT_FPVIOL;
  690. FTFL_FCCOB0 = 0x41;
  691. FTFL_FCCOB1 = 15;
  692. FTFL_FSTAT = FTFL_FSTAT_CCIF;
  693. while (!(FTFL_FSTAT & FTFL_FSTAT_CCIF)) ; // wait
  694. num = *(uint32_t *)&FTFL_FCCOB7;
  695. __enable_irq();
  696. // add extra zero to work around OS-X CDC-ACM driver bug
  697. if (num < 10000000) num = num * 10;
  698. ultoa(num, buf, 10);
  699. for (i=0; i<10; i++) {
  700. char c = buf[i];
  701. if (!c) break;
  702. usb_string_serial_number_default.wString[i] = c;
  703. }
  704. usb_string_serial_number_default.bLength = i * 2 + 2;
  705. }
  706. // **************************************************************
  707. // Descriptors List
  708. // **************************************************************
  709. // This table provides access to all the descriptor data above.
  710. const usb_descriptor_list_t usb_descriptor_list[] = {
  711. //wValue, wIndex, address, length
  712. {0x0100, 0x0000, device_descriptor, sizeof(device_descriptor)},
  713. {0x0200, 0x0000, config_descriptor, sizeof(config_descriptor)},
  714. #ifdef SEREMU_INTERFACE
  715. {0x2200, SEREMU_INTERFACE, seremu_report_desc, sizeof(seremu_report_desc)},
  716. {0x2100, SEREMU_INTERFACE, config_descriptor+SEREMU_DESC_OFFSET, 9},
  717. #endif
  718. #ifdef KEYBOARD_INTERFACE
  719. {0x2200, KEYBOARD_INTERFACE, keyboard_report_desc, sizeof(keyboard_report_desc)},
  720. {0x2100, KEYBOARD_INTERFACE, config_descriptor+KEYBOARD_DESC_OFFSET, 9},
  721. #endif
  722. #ifdef MOUSE_INTERFACE
  723. {0x2200, MOUSE_INTERFACE, mouse_report_desc, sizeof(mouse_report_desc)},
  724. {0x2100, MOUSE_INTERFACE, config_descriptor+MOUSE_DESC_OFFSET, 9},
  725. #endif
  726. #ifdef JOYSTICK_INTERFACE
  727. {0x2200, JOYSTICK_INTERFACE, joystick_report_desc, sizeof(joystick_report_desc)},
  728. {0x2100, JOYSTICK_INTERFACE, config_descriptor+JOYSTICK_DESC_OFFSET, 9},
  729. #endif
  730. #ifdef RAWHID_INTERFACE
  731. {0x2200, RAWHID_INTERFACE, rawhid_report_desc, sizeof(rawhid_report_desc)},
  732. {0x2100, RAWHID_INTERFACE, config_descriptor+RAWHID_DESC_OFFSET, 9},
  733. #endif
  734. #ifdef FLIGHTSIM_INTERFACE
  735. {0x2200, FLIGHTSIM_INTERFACE, flightsim_report_desc, sizeof(flightsim_report_desc)},
  736. {0x2100, FLIGHTSIM_INTERFACE, config_descriptor+FLIGHTSIM_DESC_OFFSET, 9},
  737. #endif
  738. {0x0300, 0x0000, (const uint8_t *)&string0, 0},
  739. {0x0301, 0x0409, (const uint8_t *)&usb_string_manufacturer_name, 0},
  740. {0x0302, 0x0409, (const uint8_t *)&usb_string_product_name, 0},
  741. {0x0303, 0x0409, (const uint8_t *)&usb_string_serial_number, 0},
  742. //{0x0301, 0x0409, (const uint8_t *)&string1, 0},
  743. //{0x0302, 0x0409, (const uint8_t *)&string2, 0},
  744. //{0x0303, 0x0409, (const uint8_t *)&string3, 0},
  745. {0, 0, NULL, 0}
  746. };
  747. // **************************************************************
  748. // Endpoint Configuration
  749. // **************************************************************
  750. #if 0
  751. // 0x00 = not used
  752. // 0x19 = Recieve only
  753. // 0x15 = Transmit only
  754. // 0x1D = Transmit & Recieve
  755. //
  756. const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] =
  757. {
  758. 0x00, 0x15, 0x19, 0x15, 0x00, 0x00, 0x00, 0x00,
  759. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  760. };
  761. #endif
  762. const uint8_t usb_endpoint_config_table[NUM_ENDPOINTS] =
  763. {
  764. #if (defined(ENDPOINT1_CONFIG) && NUM_ENDPOINTS >= 1)
  765. ENDPOINT1_CONFIG,
  766. #elif (NUM_ENDPOINTS >= 1)
  767. ENDPOINT_UNUSED,
  768. #endif
  769. #if (defined(ENDPOINT2_CONFIG) && NUM_ENDPOINTS >= 2)
  770. ENDPOINT2_CONFIG,
  771. #elif (NUM_ENDPOINTS >= 2)
  772. ENDPOINT_UNUSED,
  773. #endif
  774. #if (defined(ENDPOINT3_CONFIG) && NUM_ENDPOINTS >= 3)
  775. ENDPOINT3_CONFIG,
  776. #elif (NUM_ENDPOINTS >= 3)
  777. ENDPOINT_UNUSED,
  778. #endif
  779. #if (defined(ENDPOINT4_CONFIG) && NUM_ENDPOINTS >= 4)
  780. ENDPOINT4_CONFIG,
  781. #elif (NUM_ENDPOINTS >= 4)
  782. ENDPOINT_UNUSED,
  783. #endif
  784. #if (defined(ENDPOINT5_CONFIG) && NUM_ENDPOINTS >= 5)
  785. ENDPOINT5_CONFIG,
  786. #elif (NUM_ENDPOINTS >= 5)
  787. ENDPOINT_UNUSED,
  788. #endif
  789. #if (defined(ENDPOINT6_CONFIG) && NUM_ENDPOINTS >= 6)
  790. ENDPOINT6_CONFIG,
  791. #elif (NUM_ENDPOINTS >= 6)
  792. ENDPOINT_UNUSED,
  793. #endif
  794. #if (defined(ENDPOINT7_CONFIG) && NUM_ENDPOINTS >= 7)
  795. ENDPOINT7_CONFIG,
  796. #elif (NUM_ENDPOINTS >= 7)
  797. ENDPOINT_UNUSED,
  798. #endif
  799. #if (defined(ENDPOINT8_CONFIG) && NUM_ENDPOINTS >= 8)
  800. ENDPOINT8_CONFIG,
  801. #elif (NUM_ENDPOINTS >= 8)
  802. ENDPOINT_UNUSED,
  803. #endif
  804. #if (defined(ENDPOINT9_CONFIG) && NUM_ENDPOINTS >= 9)
  805. ENDPOINT9_CONFIG,
  806. #elif (NUM_ENDPOINTS >= 9)
  807. ENDPOINT_UNUSED,
  808. #endif
  809. #if (defined(ENDPOINT10_CONFIG) && NUM_ENDPOINTS >= 10)
  810. ENDPOINT10_CONFIG,
  811. #elif (NUM_ENDPOINTS >= 10)
  812. ENDPOINT_UNUSED,
  813. #endif
  814. #if (defined(ENDPOINT11_CONFIG) && NUM_ENDPOINTS >= 11)
  815. ENDPOINT11_CONFIG,
  816. #elif (NUM_ENDPOINTS >= 11)
  817. ENDPOINT_UNUSED,
  818. #endif
  819. #if (defined(ENDPOINT12_CONFIG) && NUM_ENDPOINTS >= 12)
  820. ENDPOINT12_CONFIG,
  821. #elif (NUM_ENDPOINTS >= 12)
  822. ENDPOINT_UNUSED,
  823. #endif
  824. #if (defined(ENDPOINT13_CONFIG) && NUM_ENDPOINTS >= 13)
  825. ENDPOINT13_CONFIG,
  826. #elif (NUM_ENDPOINTS >= 13)
  827. ENDPOINT_UNUSED,
  828. #endif
  829. #if (defined(ENDPOINT14_CONFIG) && NUM_ENDPOINTS >= 14)
  830. ENDPOINT14_CONFIG,
  831. #elif (NUM_ENDPOINTS >= 14)
  832. ENDPOINT_UNUSED,
  833. #endif
  834. #if (defined(ENDPOINT15_CONFIG) && NUM_ENDPOINTS >= 15)
  835. ENDPOINT15_CONFIG,
  836. #elif (NUM_ENDPOINTS >= 15)
  837. ENDPOINT_UNUSED,
  838. #endif
  839. };
  840. #endif // F_CPU >= 20 MHz