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.

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