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

366 lines
10KB

  1. /* USB EHCI Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <Arduino.h>
  24. #include "USBHost_t36.h" // Read this header first for key info
  25. #define print USBHost::print_
  26. #define println USBHost::println_
  27. void MIDIDevice::init()
  28. {
  29. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  30. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  31. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  32. handleNoteOff = NULL;
  33. handleNoteOn = NULL;
  34. handleVelocityChange = NULL;
  35. handleControlChange = NULL;
  36. handleProgramChange = NULL;
  37. handleAfterTouch = NULL;
  38. handlePitchChange = NULL;
  39. handleSysEx = NULL;
  40. handleRealTimeSystem = NULL;
  41. handleTimeCodeQuarterFrame = NULL;
  42. rx_head = 0;
  43. rx_tail = 0;
  44. driver_ready_for_device(this);
  45. }
  46. // Audio Class-Specific Descriptor Types (audio 1.0, page 99)
  47. // CS_UNDEFINED 0x20
  48. // CS_DEVICE 0x21
  49. // CS_CONFIGURATION 0x22
  50. // CS_STRING 0x23
  51. // CS_INTERFACE 0x24
  52. // CS_ENDPOINT 0x25
  53. // MS Class-Specific Interface Descriptor Subtypes (midi 1.0, page 36)
  54. // MS_DESCRIPTOR_UNDEFINED 0x00
  55. // MS_HEADER 0x01
  56. // MIDI_IN_JACK 0x02
  57. // MIDI_OUT_JACK 0x03
  58. // ELEMENT 0x04
  59. // MS Class-Specific Endpoint Descriptor Subtypes (midi 1.0, page 36)
  60. // DESCRIPTOR_UNDEFINED 0x00
  61. // MS_GENERAL 0x01
  62. // MS MIDI IN and OUT Jack types (midi 1.0, page 36)
  63. // JACK_TYPE_UNDEFINED 0x00
  64. // EMBEDDED 0x01
  65. // EXTERNAL 0x02
  66. // Endpoint Control Selectors (midi 1.0, page 36)
  67. // EP_CONTROL_UNDEFINED 0x00
  68. // ASSOCIATION_CONTROL 0x01
  69. bool MIDIDevice::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  70. {
  71. // only claim at interface level
  72. if (type != 1) return false;
  73. println("MIDIDevice claim this=", (uint32_t)this, HEX);
  74. println("len = ", len);
  75. const uint8_t *p = descriptors;
  76. const uint8_t *end = p + len;
  77. if (p[0] != 9 || p[1] != 4) return false; // interface descriptor
  78. //println(" bInterfaceClass=", p[5]);
  79. //println(" bInterfaceSubClass=", p[6]);
  80. if (p[5] != 1) return false; // bInterfaceClass: 1 = Audio class
  81. if (p[6] != 3) return false; // bInterfaceSubClass: 3 = MIDI
  82. p += 9;
  83. println(" Interface is MIDI");
  84. rx_ep = 0;
  85. tx_ep = 0;
  86. while (p < end) {
  87. len = *p;
  88. if (len < 4) return false; // all audio desc are at least 4 bytes
  89. if (p + len > end) return false; // reject if beyond end of data
  90. uint32_t type = p[1];
  91. //println("type: ", type);
  92. if (type == 4 || type == 11) break; // interface or IAD, not for us
  93. if (type == 0x24) { // 0x24 = Audio CS_INTERFACE, audio 1.0, page 99
  94. uint32_t subtype = p[2];
  95. //println("subtype: ", subtype);
  96. if (subtype == 1) {
  97. // Interface Header, midi 1.0, page 21
  98. println(" MIDI Header (ignored)");
  99. } else if (subtype == 2) {
  100. // MIDI IN Jack, midi 1.0, page 22
  101. println(" MIDI IN Jack (ignored)");
  102. } else if (subtype == 3) {
  103. // MIDI OUT Jack, midi 1.0, page 22
  104. println(" MIDI OUT Jack (ignored)");
  105. } else if (subtype == 4) {
  106. // Element Descriptor, midi 1.0, page 23-24
  107. println(" MIDI Element (ignored)");
  108. } else {
  109. return false; // unknown
  110. }
  111. } else if (type == 5) {
  112. // endpoint descriptor
  113. if (p[0] < 7) return false; // at least 7 bytes
  114. if (p[3] != 2) return false; // must be bulk type
  115. println(" MIDI Endpoint: ", p[2], HEX);
  116. switch (p[2] & 0xF0) {
  117. case 0x80:
  118. // IN endpoint
  119. if (rx_ep == 0) {
  120. rx_ep = p[2] & 0x0F;
  121. rx_size = p[4] | (p[5] << 8);
  122. println(" rx_size = ", rx_size);
  123. }
  124. break;
  125. case 0x00:
  126. // OUT endpoint
  127. if (tx_ep == 0) {
  128. tx_ep = p[2];
  129. tx_size = p[4] | (p[5] << 8);
  130. println(" tx_size = ", tx_size);
  131. }
  132. break;
  133. default:
  134. return false;
  135. }
  136. } else if (type == 37) {
  137. // MIDI endpoint info, midi 1.0: 6.2.2, page 26
  138. println(" MIDI Endpoint Jack Association (ignored)");
  139. } else {
  140. return false; // unknown
  141. }
  142. p += len;
  143. }
  144. // if an IN endpoint was found, create its pipe
  145. if (rx_ep && rx_size <= MAX_PACKET_SIZE) {
  146. rxpipe = new_Pipe(dev, 2, rx_ep, 1, rx_size);
  147. if (rxpipe) {
  148. rxpipe->callback_function = rx_callback;
  149. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  150. rx_packet_queued = true;
  151. }
  152. } else {
  153. rxpipe = NULL;
  154. }
  155. // if an OUT endpoint was found, create its pipe
  156. if (tx_ep && tx_size <= MAX_PACKET_SIZE) {
  157. txpipe = new_Pipe(dev, 2, tx_ep, 0, tx_size);
  158. if (txpipe) {
  159. txpipe->callback_function = tx_callback;
  160. }
  161. } else {
  162. txpipe = NULL;
  163. }
  164. rx_head = 0;
  165. rx_tail = 0;
  166. msg_channel = 0;
  167. msg_type = 0;
  168. msg_data1 = 0;
  169. msg_data2 = 0;
  170. msg_sysex_len = 0;
  171. // claim if either pipe created
  172. return (rxpipe || txpipe);
  173. }
  174. void MIDIDevice::rx_callback(const Transfer_t *transfer)
  175. {
  176. if (transfer->driver) {
  177. ((MIDIDevice *)(transfer->driver))->rx_data(transfer);
  178. }
  179. }
  180. void MIDIDevice::tx_callback(const Transfer_t *transfer)
  181. {
  182. if (transfer->driver) {
  183. ((MIDIDevice *)(transfer->driver))->tx_data(transfer);
  184. }
  185. }
  186. void MIDIDevice::rx_data(const Transfer_t *transfer)
  187. {
  188. println("MIDIDevice Receive");
  189. print(" MIDI Data: ");
  190. print_hexbytes(transfer->buffer, rx_size);
  191. uint32_t head = rx_head;
  192. uint32_t tail = rx_tail;
  193. uint32_t len = (transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF)) >> 2;
  194. for (uint32_t i=0; i < len; i++) {
  195. uint32_t msg = rx_buffer[i];
  196. if (msg) {
  197. if (++head >= RX_QUEUE_SIZE) head = 0;
  198. rx_queue[head] = msg;
  199. }
  200. }
  201. rx_head = head;
  202. rx_tail = tail;
  203. uint32_t avail = (head < tail) ? tail - head - 1 : RX_QUEUE_SIZE - 1 - head + tail;
  204. println("rx_size = ", rx_size);
  205. println("avail = ", avail);
  206. if (avail >= (uint32_t)(rx_size>>2)) {
  207. // enough space to accept another full packet
  208. println("queue another receive packet");
  209. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  210. rx_packet_queued = true;
  211. } else {
  212. // queue can't accept another packet's data, so leave
  213. // the data waiting on the device until we can accept it
  214. println("wait to receive more packets");
  215. rx_packet_queued = false;
  216. }
  217. }
  218. void MIDIDevice::tx_data(const Transfer_t *transfer)
  219. {
  220. println("MIDIDevice transmit complete");
  221. print(" MIDI Data: ");
  222. print_hexbytes(transfer->buffer, tx_size);
  223. // TODO: return the buffer to the pool...
  224. }
  225. void MIDIDevice::disconnect()
  226. {
  227. // should rx_queue be cleared?
  228. // as-is, the user can still read MIDI messages
  229. // which arrived before the device disconnected.
  230. rxpipe = NULL;
  231. txpipe = NULL;
  232. }
  233. bool MIDIDevice::read(uint8_t channel, uint8_t cable)
  234. {
  235. uint32_t n, head, tail, avail, ch, type1, type2;
  236. head = rx_head;
  237. tail = rx_tail;
  238. if (head == tail) return false;
  239. if (++tail >= RX_QUEUE_SIZE) tail = 0;
  240. n = rx_queue[tail];
  241. rx_tail = tail;
  242. if (!rx_packet_queued && rxpipe) {
  243. avail = (head < tail) ? tail - head - 1 : RX_QUEUE_SIZE - 1 - head + tail;
  244. if (avail >= (uint32_t)(rx_size>>2)) {
  245. __disable_irq();
  246. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  247. __enable_irq();
  248. }
  249. }
  250. println("read: ", n, HEX);
  251. type1 = n & 15;
  252. type2 = (n >> 12) & 15;
  253. ch = ((n >> 8) & 15) + 1;
  254. if (type1 >= 0x08 && type1 <= 0x0E) {
  255. if (channel && channel != ch) {
  256. // ignore other channels when user wants single channel read
  257. return false;
  258. }
  259. if (type1 == 0x08 && type2 == 0x08) {
  260. msg_type = 8; // 8 = Note off
  261. if (handleNoteOff)
  262. (*handleNoteOff)(ch, (n >> 16), (n >> 24));
  263. } else
  264. if (type1 == 0x09 && type2 == 0x09) {
  265. if ((n >> 24) > 0) {
  266. msg_type = 9; // 9 = Note on
  267. if (handleNoteOn)
  268. (*handleNoteOn)(ch, (n >> 16), (n >> 24));
  269. } else {
  270. msg_type = 8; // 8 = Note off
  271. if (handleNoteOff)
  272. (*handleNoteOff)(ch, (n >> 16), (n >> 24));
  273. }
  274. } else
  275. if (type1 == 0x0A && type2 == 0x0A) {
  276. msg_type = 10; // 10 = Poly Pressure
  277. if (handleVelocityChange)
  278. (*handleVelocityChange)(ch, (n >> 16), (n >> 24));
  279. } else
  280. if (type1 == 0x0B && type2 == 0x0B) {
  281. msg_type = 11; // 11 = Control Change
  282. if (handleControlChange)
  283. (*handleControlChange)(ch, (n >> 16), (n >> 24));
  284. } else
  285. if (type1 == 0x0C && type2 == 0x0C) {
  286. msg_type = 12; // 12 = Program Change
  287. if (handleProgramChange) (*handleProgramChange)(ch, (n >> 16));
  288. } else
  289. if (type1 == 0x0D && type2 == 0x0D) {
  290. msg_type = 13; // 13 = After Touch
  291. if (handleAfterTouch) (*handleAfterTouch)(ch, (n >> 16));
  292. } else
  293. if (type1 == 0x0E && type2 == 0x0E) {
  294. msg_type = 14; // 14 = Pitch Bend
  295. if (handlePitchChange)
  296. (*handlePitchChange)(ch, ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
  297. } else {
  298. return false;
  299. }
  300. msg_channel = ch;
  301. msg_data1 = (n >> 16);
  302. msg_data2 = (n >> 24);
  303. return true;
  304. }
  305. if (type1 == 0x04) {
  306. sysex_byte(n >> 8);
  307. sysex_byte(n >> 16);
  308. sysex_byte(n >> 24);
  309. return false;
  310. }
  311. if (type1 >= 0x05 && type1 <= 0x07) {
  312. sysex_byte(n >> 8);
  313. if (type1 >= 0x06) sysex_byte(n >> 16);
  314. if (type1 == 0x07) sysex_byte(n >> 24);
  315. msg_data1 = msg_sysex_len;
  316. msg_sysex_len = 0;
  317. msg_type = 15; // 15 = Sys Ex
  318. if (handleSysEx)
  319. (*handleSysEx)(msg_sysex, msg_data1, 1);
  320. return true;
  321. }
  322. // TODO: single byte messages
  323. // TODO: time code messages?
  324. return false;
  325. }
  326. void MIDIDevice::sysex_byte(uint8_t b)
  327. {
  328. // when buffer is full, send another chunk to handler.
  329. if (msg_sysex_len >= SYSEX_MAX_LEN) {
  330. if (handleSysEx) {
  331. (*handleSysEx)(msg_sysex, msg_sysex_len, 0);
  332. msg_sysex_len = 0;
  333. }
  334. }
  335. if (msg_sysex_len < SYSEX_MAX_LEN) {
  336. msg_sysex[msg_sysex_len++] = b;
  337. }
  338. }