Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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