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.

444 lines
14KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 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. #include "usb_dev.h"
  31. #include "usb_midi.h"
  32. #include "core_pins.h" // for yield()
  33. #include "HardwareSerial.h"
  34. #ifdef MIDI_INTERFACE // defined by usb_dev.h -> usb_desc.h
  35. #if F_CPU >= 20000000
  36. uint8_t usb_midi_msg_cable;
  37. uint8_t usb_midi_msg_channel;
  38. uint8_t usb_midi_msg_type;
  39. uint8_t usb_midi_msg_data1;
  40. uint8_t usb_midi_msg_data2;
  41. // TODO: separate sysex buffers for each cable...
  42. uint8_t usb_midi_msg_sysex[USB_MIDI_SYSEX_MAX];
  43. uint16_t usb_midi_msg_sysex_len;
  44. void (*usb_midi_handleNoteOff)(uint8_t ch, uint8_t note, uint8_t vel) = NULL;
  45. void (*usb_midi_handleNoteOn)(uint8_t ch, uint8_t note, uint8_t vel) = NULL;
  46. void (*usb_midi_handleVelocityChange)(uint8_t ch, uint8_t note, uint8_t vel) = NULL;
  47. void (*usb_midi_handleControlChange)(uint8_t ch, uint8_t control, uint8_t value) = NULL;
  48. void (*usb_midi_handleProgramChange)(uint8_t ch, uint8_t program) = NULL;
  49. void (*usb_midi_handleAfterTouch)(uint8_t ch, uint8_t pressure) = NULL;
  50. void (*usb_midi_handlePitchChange)(uint8_t ch, int pitch) = NULL;
  51. void (*usb_midi_handleSysExPartial)(const uint8_t *data, uint16_t length, uint8_t complete) = NULL;
  52. void (*usb_midi_handleSysExComplete)(uint8_t *data, unsigned int size) = NULL;
  53. void (*usb_midi_handleTimeCodeQuarterFrame)(uint8_t data) = NULL;
  54. void (*usb_midi_handleSongPosition)(uint16_t beats) = NULL;
  55. void (*usb_midi_handleSongSelect)(uint8_t songnumber) = NULL;
  56. void (*usb_midi_handleTuneRequest)(void) = NULL;
  57. void (*usb_midi_handleClock)(void) = NULL;
  58. void (*usb_midi_handleStart)(void) = NULL;
  59. void (*usb_midi_handleContinue)(void) = NULL;
  60. void (*usb_midi_handleStop)(void) = NULL;
  61. void (*usb_midi_handleActiveSensing)(void) = NULL;
  62. void (*usb_midi_handleSystemReset)(void) = NULL;
  63. void (*usb_midi_handleRealTimeSystem)(uint8_t rtb) = NULL;
  64. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  65. #define TX_PACKET_LIMIT 6
  66. static usb_packet_t *rx_packet=NULL;
  67. static usb_packet_t *tx_packet=NULL;
  68. static uint8_t transmit_previous_timeout=0;
  69. static uint8_t tx_noautoflush=0;
  70. // When the PC isn't listening, how long do we wait before discarding data?
  71. #define TX_TIMEOUT_MSEC 40
  72. #if F_CPU == 240000000
  73. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600)
  74. #elif F_CPU == 216000000
  75. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440)
  76. #elif F_CPU == 192000000
  77. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1280)
  78. #elif F_CPU == 180000000
  79. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1200)
  80. #elif F_CPU == 168000000
  81. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  82. #elif F_CPU == 144000000
  83. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  84. #elif F_CPU == 120000000
  85. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  86. #elif F_CPU == 96000000
  87. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  88. #elif F_CPU == 72000000
  89. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  90. #elif F_CPU == 48000000
  91. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  92. #elif F_CPU == 24000000
  93. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  94. #endif
  95. void usb_midi_write_packed(uint32_t n)
  96. {
  97. uint32_t index, wait_count=0;
  98. tx_noautoflush = 1;
  99. if (!tx_packet) {
  100. while (1) {
  101. if (!usb_configuration) {
  102. //serial_print("error1\n");
  103. return;
  104. }
  105. if (usb_tx_packet_count(MIDI_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  106. tx_packet = usb_malloc();
  107. if (tx_packet) break;
  108. }
  109. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  110. transmit_previous_timeout = 1;
  111. //serial_print("error2\n");
  112. return;
  113. }
  114. yield();
  115. }
  116. }
  117. transmit_previous_timeout = 0;
  118. index = tx_packet->index;
  119. //*((uint32_t *)(tx_packet->buf) + index++) = n;
  120. ((uint32_t *)(tx_packet->buf))[index++] = n;
  121. if (index < MIDI_TX_SIZE/4) {
  122. tx_packet->index = index;
  123. } else {
  124. tx_packet->len = MIDI_TX_SIZE;
  125. usb_tx(MIDI_TX_ENDPOINT, tx_packet);
  126. tx_packet = usb_malloc();
  127. }
  128. tx_noautoflush = 0;
  129. }
  130. void usb_midi_send_sysex(const uint8_t *data, uint32_t length, uint8_t cable)
  131. {
  132. // TODO: MIDI 2.5 lib automatically adds start and stop bytes
  133. cable = (cable & 0x0F) << 4;
  134. while (length > 3) {
  135. usb_midi_write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  136. data += 3;
  137. length -= 3;
  138. }
  139. if (length == 3) {
  140. usb_midi_write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  141. } else if (length == 2) {
  142. usb_midi_write_packed(0x06 | cable | (data[0] << 8) | (data[1] << 16));
  143. } else if (length == 1) {
  144. usb_midi_write_packed(0x05 | cable | (data[0] << 8));
  145. }
  146. }
  147. void usb_midi_flush_output(void)
  148. {
  149. if (tx_noautoflush == 0 && tx_packet && tx_packet->index > 0) {
  150. tx_packet->len = tx_packet->index * 4;
  151. usb_tx(MIDI_TX_ENDPOINT, tx_packet);
  152. tx_packet = usb_malloc();
  153. }
  154. }
  155. void static sysex_byte(uint8_t b)
  156. {
  157. if (usb_midi_handleSysExPartial && usb_midi_msg_sysex_len >= USB_MIDI_SYSEX_MAX) {
  158. // when buffer is full, send another chunk to partial handler.
  159. (*usb_midi_handleSysExPartial)(usb_midi_msg_sysex, usb_midi_msg_sysex_len, 0);
  160. usb_midi_msg_sysex_len = 0;
  161. }
  162. if (usb_midi_msg_sysex_len < USB_MIDI_SYSEX_MAX) {
  163. usb_midi_msg_sysex[usb_midi_msg_sysex_len++] = b;
  164. }
  165. }
  166. uint32_t usb_midi_available(void)
  167. {
  168. uint32_t index;
  169. if (!rx_packet) {
  170. if (!usb_configuration) return 0;
  171. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  172. if (!rx_packet) return 0;
  173. if (rx_packet->len == 0) {
  174. usb_free(rx_packet);
  175. rx_packet = NULL;
  176. return 0;
  177. }
  178. }
  179. index = rx_packet->index;
  180. return rx_packet->len - index;
  181. }
  182. uint32_t usb_midi_read_message(void)
  183. {
  184. uint32_t n, index;
  185. if (!rx_packet) {
  186. if (!usb_configuration) return 0;
  187. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  188. if (!rx_packet) return 0;
  189. if (rx_packet->len == 0) {
  190. usb_free(rx_packet);
  191. rx_packet = NULL;
  192. return 0;
  193. }
  194. }
  195. index = rx_packet->index;
  196. n = ((uint32_t *)rx_packet->buf)[index/4];
  197. index += 4;
  198. if (index < rx_packet->len) {
  199. rx_packet->index = index;
  200. } else {
  201. usb_free(rx_packet);
  202. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  203. }
  204. return n;
  205. }
  206. int usb_midi_read(uint32_t channel)
  207. {
  208. uint32_t n, index, ch, type1, type2;
  209. if (!rx_packet) {
  210. if (!usb_configuration) return 0;
  211. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  212. if (!rx_packet) return 0;
  213. if (rx_packet->len == 0) {
  214. usb_free(rx_packet);
  215. rx_packet = NULL;
  216. return 0;
  217. }
  218. }
  219. index = rx_packet->index;
  220. //n = *(uint32_t *)(rx_packet->buf + index);
  221. n = ((uint32_t *)rx_packet->buf)[index/4];
  222. //serial_print("midi rx, n=");
  223. //serial_phex32(n);
  224. //serial_print("\n");
  225. index += 4;
  226. if (index < rx_packet->len) {
  227. rx_packet->index = index;
  228. } else {
  229. usb_free(rx_packet);
  230. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  231. }
  232. type1 = n & 15;
  233. type2 = (n >> 12) & 15;
  234. ch = ((n >> 8) & 15) + 1;
  235. usb_midi_msg_cable = (n >> 4) & 15;
  236. if (type1 >= 0x08 && type1 <= 0x0E) {
  237. if (channel && channel != ch) {
  238. // ignore other channels when user wants single channel read
  239. return 0;
  240. }
  241. if (type1 == 0x08 && type2 == 0x08) {
  242. usb_midi_msg_type = 0x80; // 0x80 = usbMIDI.NoteOff
  243. if (usb_midi_handleNoteOff)
  244. (*usb_midi_handleNoteOff)(ch, (n >> 16), (n >> 24));
  245. } else
  246. if (type1 == 0x09 && type2 == 0x09) {
  247. if ((n >> 24) > 0) {
  248. usb_midi_msg_type = 0x90; // 0x90 = usbMIDI.NoteOn
  249. if (usb_midi_handleNoteOn)
  250. (*usb_midi_handleNoteOn)(ch, (n >> 16), (n >> 24));
  251. } else {
  252. usb_midi_msg_type = 0x80; // 0x80 = usbMIDI.NoteOff
  253. if (usb_midi_handleNoteOff)
  254. (*usb_midi_handleNoteOff)(ch, (n >> 16), (n >> 24));
  255. }
  256. } else
  257. if (type1 == 0x0A && type2 == 0x0A) {
  258. usb_midi_msg_type = 0xA0; // 0xA0 = usbMIDI.AfterTouchPoly
  259. if (usb_midi_handleVelocityChange)
  260. (*usb_midi_handleVelocityChange)(ch, (n >> 16), (n >> 24));
  261. } else
  262. if (type1 == 0x0B && type2 == 0x0B) {
  263. usb_midi_msg_type = 0xB0; // 0xB0 = usbMIDI.ControlChange
  264. if (usb_midi_handleControlChange)
  265. (*usb_midi_handleControlChange)(ch, (n >> 16), (n >> 24));
  266. } else
  267. if (type1 == 0x0C && type2 == 0x0C) {
  268. usb_midi_msg_type = 0xC0; // 0xC0 = usbMIDI.ProgramChange
  269. if (usb_midi_handleProgramChange)
  270. (*usb_midi_handleProgramChange)(ch, (n >> 16));
  271. } else
  272. if (type1 == 0x0D && type2 == 0x0D) {
  273. usb_midi_msg_type = 0xD0; // 0xD0 = usbMIDI.AfterTouchChannel
  274. if (usb_midi_handleAfterTouch)
  275. (*usb_midi_handleAfterTouch)(ch, (n >> 16));
  276. } else
  277. if (type1 == 0x0E && type2 == 0x0E) {
  278. usb_midi_msg_type = 0xE0; // 0xE0 = usbMIDI.PitchBend
  279. if (usb_midi_handlePitchChange)
  280. (*usb_midi_handlePitchChange)(ch,
  281. ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
  282. } else {
  283. return 0;
  284. }
  285. return_message:
  286. usb_midi_msg_channel = ch;
  287. usb_midi_msg_data1 = (n >> 16);
  288. usb_midi_msg_data2 = (n >> 24);
  289. return 1;
  290. }
  291. if (type1 == 0x02 || type1 == 0x03 || (type1 == 0x05 && type2 == 0x0F)) {
  292. // system common or system realtime message
  293. uint8_t type;
  294. system_common_or_realtime:
  295. type = n >> 8;
  296. switch (type) {
  297. case 0xF1: // usbMIDI.TimeCodeQuarterFrame
  298. if (usb_midi_handleTimeCodeQuarterFrame) {
  299. (*usb_midi_handleTimeCodeQuarterFrame)(n >> 16);
  300. }
  301. break;
  302. case 0xF2: // usbMIDI.SongPosition
  303. if (usb_midi_handleSongPosition) {
  304. (*usb_midi_handleSongPosition)(
  305. ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
  306. }
  307. break;
  308. case 0xF3: // usbMIDI.SongSelect
  309. if (usb_midi_handleSongSelect) {
  310. (*usb_midi_handleSongSelect)(n >> 16);
  311. }
  312. break;
  313. case 0xF6: // usbMIDI.TuneRequest
  314. if (usb_midi_handleTuneRequest) {
  315. (*usb_midi_handleTuneRequest)();
  316. }
  317. break;
  318. case 0xF8: // usbMIDI.Clock
  319. if (usb_midi_handleClock) {
  320. (*usb_midi_handleClock)();
  321. } else if (usb_midi_handleRealTimeSystem) {
  322. (*usb_midi_handleRealTimeSystem)(0xF8);
  323. }
  324. break;
  325. case 0xFA: // usbMIDI.Start
  326. if (usb_midi_handleStart) {
  327. (*usb_midi_handleStart)();
  328. } else if (usb_midi_handleRealTimeSystem) {
  329. (*usb_midi_handleRealTimeSystem)(0xFA);
  330. }
  331. break;
  332. case 0xFB: // usbMIDI.Continue
  333. if (usb_midi_handleContinue) {
  334. (*usb_midi_handleContinue)();
  335. } else if (usb_midi_handleRealTimeSystem) {
  336. (*usb_midi_handleRealTimeSystem)(0xFB);
  337. }
  338. break;
  339. case 0xFC: // usbMIDI.Stop
  340. if (usb_midi_handleStop) {
  341. (*usb_midi_handleStop)();
  342. } else if (usb_midi_handleRealTimeSystem) {
  343. (*usb_midi_handleRealTimeSystem)(0xFC);
  344. }
  345. break;
  346. case 0xFE: // usbMIDI.ActiveSensing
  347. if (usb_midi_handleActiveSensing) {
  348. (*usb_midi_handleActiveSensing)();
  349. } else if (usb_midi_handleRealTimeSystem) {
  350. (*usb_midi_handleRealTimeSystem)(0xFE);
  351. }
  352. break;
  353. case 0xFF: // usbMIDI.SystemReset
  354. if (usb_midi_handleSystemReset) {
  355. (*usb_midi_handleSystemReset)();
  356. } else if (usb_midi_handleRealTimeSystem) {
  357. (*usb_midi_handleRealTimeSystem)(0xFF);
  358. }
  359. break;
  360. default:
  361. return 0; // unknown message, ignore it
  362. }
  363. usb_midi_msg_type = type;
  364. goto return_message;
  365. }
  366. if (type1 == 0x04) {
  367. sysex_byte(n >> 8);
  368. sysex_byte(n >> 16);
  369. sysex_byte(n >> 24);
  370. return 0;
  371. }
  372. if (type1 >= 0x05 && type1 <= 0x07) {
  373. sysex_byte(n >> 8);
  374. if (type1 >= 0x06) sysex_byte(n >> 16);
  375. if (type1 == 0x07) sysex_byte(n >> 24);
  376. uint16_t len = usb_midi_msg_sysex_len;
  377. usb_midi_msg_data1 = len;
  378. usb_midi_msg_data2 = len >> 8;
  379. usb_midi_msg_sysex_len = 0;
  380. usb_midi_msg_type = 0xF0; // 0xF0 = usbMIDI.SystemExclusive
  381. if (usb_midi_handleSysExPartial) {
  382. (*usb_midi_handleSysExPartial)(usb_midi_msg_sysex, len, 1);
  383. } else if (usb_midi_handleSysExComplete) {
  384. (*usb_midi_handleSysExComplete)(usb_midi_msg_sysex, len);
  385. }
  386. return 1;
  387. }
  388. if (type1 == 0x0F) {
  389. // TODO: does this need to be a full MIDI parser?
  390. // What software actually uses this message type in practice?
  391. uint8_t b = n >> 8;
  392. if (b >= 0xF8) {
  393. // From Sebastian Tomczak, seb.tomczak at gmail.com
  394. // http://little-scale.blogspot.com/2011/08/usb-midi-game-boy-sync-for-16.html
  395. goto system_common_or_realtime;
  396. }
  397. if (usb_midi_msg_sysex_len > 0) {
  398. // From David Sorlien, dsorlien at gmail.com, http://axe4live.wordpress.com
  399. // OSX sometimes uses Single Byte Unparsed to
  400. // send bytes in the middle of a SYSEX message.
  401. sysex_byte(n >> 8);
  402. }
  403. //} else {
  404. // usb_midi_msg_type = 8;
  405. // if (usb_midi_handleRealTimeSystem)
  406. // (*usb_midi_handleRealTimeSystem)(n >> 8);
  407. // goto return_message;
  408. //}
  409. }
  410. //if (type1 == 0x02) {
  411. // From Timm Schlegelmilch, karg.music at gmail.com
  412. // http://karg-music.blogspot.de/2015/06/receiving-midi-time-codes-over-usb-with.html
  413. // usb_midi_msg_type = 9;
  414. // if (usb_midi_handleTimeCodeQuarterFrame)
  415. // (*usb_midi_handleTimeCodeQuarterFrame)(n >> 16);
  416. // return 1;
  417. //}
  418. return 0;
  419. }
  420. #endif // F_CPU
  421. #endif // MIDI_INTERFACE