Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

411 lines
12KB

  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. rxpipe = NULL;
  45. txpipe = NULL;
  46. driver_ready_for_device(this);
  47. }
  48. // Audio Class-Specific Descriptor Types (audio 1.0, page 99)
  49. // CS_UNDEFINED 0x20
  50. // CS_DEVICE 0x21
  51. // CS_CONFIGURATION 0x22
  52. // CS_STRING 0x23
  53. // CS_INTERFACE 0x24
  54. // CS_ENDPOINT 0x25
  55. // MS Class-Specific Interface Descriptor Subtypes (midi 1.0, page 36)
  56. // MS_DESCRIPTOR_UNDEFINED 0x00
  57. // MS_HEADER 0x01
  58. // MIDI_IN_JACK 0x02
  59. // MIDI_OUT_JACK 0x03
  60. // ELEMENT 0x04
  61. // MS Class-Specific Endpoint Descriptor Subtypes (midi 1.0, page 36)
  62. // DESCRIPTOR_UNDEFINED 0x00
  63. // MS_GENERAL 0x01
  64. // MS MIDI IN and OUT Jack types (midi 1.0, page 36)
  65. // JACK_TYPE_UNDEFINED 0x00
  66. // EMBEDDED 0x01
  67. // EXTERNAL 0x02
  68. // Endpoint Control Selectors (midi 1.0, page 36)
  69. // EP_CONTROL_UNDEFINED 0x00
  70. // ASSOCIATION_CONTROL 0x01
  71. bool MIDIDevice::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  72. {
  73. // only claim at interface level
  74. if (type != 1) return false;
  75. println("MIDIDevice claim this=", (uint32_t)this, HEX);
  76. println("len = ", len);
  77. const uint8_t *p = descriptors;
  78. const uint8_t *end = p + len;
  79. if (p[0] != 9 || p[1] != 4) return false; // interface descriptor
  80. //println(" bInterfaceClass=", p[5]);
  81. //println(" bInterfaceSubClass=", p[6]);
  82. if (p[5] != 1) return false; // bInterfaceClass: 1 = Audio class
  83. if (p[6] != 3) return false; // bInterfaceSubClass: 3 = MIDI
  84. p += 9;
  85. println(" Interface is MIDI");
  86. rx_ep = 0;
  87. tx_ep = 0;
  88. while (p < end) {
  89. len = *p;
  90. if (len < 4) return false; // all audio desc are at least 4 bytes
  91. if (p + len > end) return false; // reject if beyond end of data
  92. uint32_t type = p[1];
  93. //println("type: ", type);
  94. if (type == 4 || type == 11) break; // interface or IAD, not for us
  95. if (type == 0x24) { // 0x24 = Audio CS_INTERFACE, audio 1.0, page 99
  96. uint32_t subtype = p[2];
  97. //println("subtype: ", subtype);
  98. if (subtype == 1) {
  99. // Interface Header, midi 1.0, page 21
  100. println(" MIDI Header (ignored)");
  101. } else if (subtype == 2) {
  102. // MIDI IN Jack, midi 1.0, page 22
  103. println(" MIDI IN Jack (ignored)");
  104. } else if (subtype == 3) {
  105. // MIDI OUT Jack, midi 1.0, page 22
  106. println(" MIDI OUT Jack (ignored)");
  107. } else if (subtype == 4) {
  108. // Element Descriptor, midi 1.0, page 23-24
  109. println(" MIDI Element (ignored)");
  110. } else {
  111. return false; // unknown
  112. }
  113. } else if (type == 5) {
  114. // endpoint descriptor
  115. if (p[0] < 7) return false; // at least 7 bytes
  116. if (p[3] != 2) return false; // must be bulk type
  117. println(" MIDI Endpoint: ", p[2], HEX);
  118. switch (p[2] & 0xF0) {
  119. case 0x80:
  120. // IN endpoint
  121. if (rx_ep == 0) {
  122. rx_ep = p[2] & 0x0F;
  123. rx_size = p[4] | (p[5] << 8);
  124. println(" rx_size = ", rx_size);
  125. }
  126. break;
  127. case 0x00:
  128. // OUT endpoint
  129. if (tx_ep == 0) {
  130. tx_ep = p[2];
  131. tx_size = p[4] | (p[5] << 8);
  132. println(" tx_size = ", tx_size);
  133. }
  134. break;
  135. default:
  136. return false;
  137. }
  138. } else if (type == 37) {
  139. // MIDI endpoint info, midi 1.0: 6.2.2, page 26
  140. println(" MIDI Endpoint Jack Association (ignored)");
  141. } else {
  142. return false; // unknown
  143. }
  144. p += len;
  145. }
  146. // if an IN endpoint was found, create its pipe
  147. if (rx_ep && rx_size <= MAX_PACKET_SIZE) {
  148. rxpipe = new_Pipe(dev, 2, rx_ep, 1, rx_size);
  149. if (rxpipe) {
  150. rxpipe->callback_function = rx_callback;
  151. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  152. rx_packet_queued = true;
  153. }
  154. } else {
  155. rxpipe = NULL;
  156. }
  157. // if an OUT endpoint was found, create its pipe
  158. if (tx_ep && tx_size <= MAX_PACKET_SIZE) {
  159. txpipe = new_Pipe(dev, 2, tx_ep, 0, tx_size);
  160. if (txpipe) {
  161. txpipe->callback_function = tx_callback;
  162. tx1_count = 0;
  163. tx2_count = 0;
  164. }
  165. } else {
  166. txpipe = NULL;
  167. }
  168. rx_head = 0;
  169. rx_tail = 0;
  170. msg_channel = 0;
  171. msg_type = 0;
  172. msg_data1 = 0;
  173. msg_data2 = 0;
  174. msg_sysex_len = 0;
  175. // claim if either pipe created
  176. return (rxpipe || txpipe);
  177. }
  178. void MIDIDevice::rx_callback(const Transfer_t *transfer)
  179. {
  180. if (transfer->driver) {
  181. ((MIDIDevice *)(transfer->driver))->rx_data(transfer);
  182. }
  183. }
  184. void MIDIDevice::tx_callback(const Transfer_t *transfer)
  185. {
  186. if (transfer->driver) {
  187. ((MIDIDevice *)(transfer->driver))->tx_data(transfer);
  188. }
  189. }
  190. void MIDIDevice::rx_data(const Transfer_t *transfer)
  191. {
  192. println("MIDIDevice Receive");
  193. print(" MIDI Data: ");
  194. print_hexbytes(transfer->buffer, rx_size);
  195. uint32_t head = rx_head;
  196. uint32_t tail = rx_tail;
  197. uint32_t len = (transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF)) >> 2;
  198. for (uint32_t i=0; i < len; i++) {
  199. uint32_t msg = rx_buffer[i];
  200. if (msg) {
  201. if (++head >= RX_QUEUE_SIZE) head = 0;
  202. rx_queue[head] = msg;
  203. }
  204. }
  205. rx_head = head;
  206. rx_tail = tail;
  207. uint32_t avail = (head < tail) ? tail - head - 1 : RX_QUEUE_SIZE - 1 - head + tail;
  208. println("rx_size = ", rx_size);
  209. println("avail = ", avail);
  210. if (avail >= (uint32_t)(rx_size>>2)) {
  211. // enough space to accept another full packet
  212. println("queue another receive packet");
  213. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  214. rx_packet_queued = true;
  215. } else {
  216. // queue can't accept another packet's data, so leave
  217. // the data waiting on the device until we can accept it
  218. println("wait to receive more packets");
  219. rx_packet_queued = false;
  220. }
  221. }
  222. void MIDIDevice::tx_data(const Transfer_t *transfer)
  223. {
  224. println("MIDIDevice transmit complete");
  225. print(" MIDI Data: ");
  226. print_hexbytes(transfer->buffer, tx_size);
  227. if (transfer->buffer == tx_buffer1) {
  228. tx1_count = 0;
  229. } else if (transfer->buffer == tx_buffer2) {
  230. tx2_count = 0;
  231. }
  232. }
  233. void MIDIDevice::disconnect()
  234. {
  235. // should rx_queue be cleared?
  236. // as-is, the user can still read MIDI messages
  237. // which arrived before the device disconnected.
  238. rxpipe = NULL;
  239. txpipe = NULL;
  240. }
  241. void MIDIDevice::write_packed(uint32_t data)
  242. {
  243. if (!txpipe) return;
  244. uint32_t tx_max = tx_size / 4;
  245. while (1) {
  246. uint32_t tx1 = tx1_count;
  247. uint32_t tx2 = tx2_count;
  248. if (tx1 < tx_max && (tx2 == 0 || tx2 >= tx_max)) {
  249. // use tx_buffer1
  250. tx_buffer1[tx1++] = data;
  251. tx1_count = tx1;
  252. if (tx1 >= tx_max) {
  253. queue_Data_Transfer(txpipe, tx_buffer1, tx_max*4, this);
  254. } else {
  255. // TODO: start a timer, rather than sending the buffer
  256. // before it's full, to make best use of bandwidth
  257. tx1_count = tx_max;
  258. queue_Data_Transfer(txpipe, tx_buffer1, tx_max*4, this);
  259. }
  260. return;
  261. }
  262. if (tx2 < tx_max) {
  263. // use tx_buffer2
  264. tx_buffer2[tx2++] = data;
  265. tx2_count = tx2;
  266. if (tx2 >= tx_max) {
  267. queue_Data_Transfer(txpipe, tx_buffer2, tx_max*4, this);
  268. } else {
  269. // TODO: start a timer, rather than sending the buffer
  270. // before it's full, to make best use of bandwidth
  271. tx2_count = tx_max;
  272. queue_Data_Transfer(txpipe, tx_buffer2, tx_max*4, this);
  273. }
  274. return;
  275. }
  276. }
  277. }
  278. bool MIDIDevice::read(uint8_t channel, uint8_t cable)
  279. {
  280. uint32_t n, head, tail, avail, ch, type1, type2;
  281. head = rx_head;
  282. tail = rx_tail;
  283. if (head == tail) return false;
  284. if (++tail >= RX_QUEUE_SIZE) tail = 0;
  285. n = rx_queue[tail];
  286. rx_tail = tail;
  287. if (!rx_packet_queued && rxpipe) {
  288. avail = (head < tail) ? tail - head - 1 : RX_QUEUE_SIZE - 1 - head + tail;
  289. if (avail >= (uint32_t)(rx_size>>2)) {
  290. __disable_irq();
  291. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  292. __enable_irq();
  293. }
  294. }
  295. println("read: ", n, HEX);
  296. type1 = n & 15;
  297. type2 = (n >> 12) & 15;
  298. ch = ((n >> 8) & 15) + 1;
  299. if (type1 >= 0x08 && type1 <= 0x0E) {
  300. if (channel && channel != ch) {
  301. // ignore other channels when user wants single channel read
  302. return false;
  303. }
  304. if (type1 == 0x08 && type2 == 0x08) {
  305. msg_type = 8; // 8 = Note off
  306. if (handleNoteOff)
  307. (*handleNoteOff)(ch, (n >> 16), (n >> 24));
  308. } else
  309. if (type1 == 0x09 && type2 == 0x09) {
  310. if ((n >> 24) > 0) {
  311. msg_type = 9; // 9 = Note on
  312. if (handleNoteOn)
  313. (*handleNoteOn)(ch, (n >> 16), (n >> 24));
  314. } else {
  315. msg_type = 8; // 8 = Note off
  316. if (handleNoteOff)
  317. (*handleNoteOff)(ch, (n >> 16), (n >> 24));
  318. }
  319. } else
  320. if (type1 == 0x0A && type2 == 0x0A) {
  321. msg_type = 10; // 10 = Poly Pressure
  322. if (handleVelocityChange)
  323. (*handleVelocityChange)(ch, (n >> 16), (n >> 24));
  324. } else
  325. if (type1 == 0x0B && type2 == 0x0B) {
  326. msg_type = 11; // 11 = Control Change
  327. if (handleControlChange)
  328. (*handleControlChange)(ch, (n >> 16), (n >> 24));
  329. } else
  330. if (type1 == 0x0C && type2 == 0x0C) {
  331. msg_type = 12; // 12 = Program Change
  332. if (handleProgramChange) (*handleProgramChange)(ch, (n >> 16));
  333. } else
  334. if (type1 == 0x0D && type2 == 0x0D) {
  335. msg_type = 13; // 13 = After Touch
  336. if (handleAfterTouch) (*handleAfterTouch)(ch, (n >> 16));
  337. } else
  338. if (type1 == 0x0E && type2 == 0x0E) {
  339. msg_type = 14; // 14 = Pitch Bend
  340. if (handlePitchChange)
  341. (*handlePitchChange)(ch, ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
  342. } else {
  343. return false;
  344. }
  345. msg_channel = ch;
  346. msg_data1 = (n >> 16);
  347. msg_data2 = (n >> 24);
  348. return true;
  349. }
  350. if (type1 == 0x04) {
  351. sysex_byte(n >> 8);
  352. sysex_byte(n >> 16);
  353. sysex_byte(n >> 24);
  354. return false;
  355. }
  356. if (type1 >= 0x05 && type1 <= 0x07) {
  357. sysex_byte(n >> 8);
  358. if (type1 >= 0x06) sysex_byte(n >> 16);
  359. if (type1 == 0x07) sysex_byte(n >> 24);
  360. msg_data1 = msg_sysex_len;
  361. msg_sysex_len = 0;
  362. msg_type = 15; // 15 = Sys Ex
  363. if (handleSysEx)
  364. (*handleSysEx)(msg_sysex, msg_data1, 1);
  365. return true;
  366. }
  367. // TODO: single byte messages
  368. // TODO: time code messages?
  369. return false;
  370. }
  371. void MIDIDevice::sysex_byte(uint8_t b)
  372. {
  373. // when buffer is full, send another chunk to handler.
  374. if (msg_sysex_len >= SYSEX_MAX_LEN) {
  375. if (handleSysEx) {
  376. (*handleSysEx)(msg_sysex, msg_sysex_len, 0);
  377. msg_sysex_len = 0;
  378. }
  379. }
  380. if (msg_sysex_len < SYSEX_MAX_LEN) {
  381. msg_sysex[msg_sysex_len++] = b;
  382. }
  383. }