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.

462 lines
15KB

  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 == 256000000
  73. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1706)
  74. #elif F_CPU == 240000000
  75. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600)
  76. #elif F_CPU == 216000000
  77. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440)
  78. #elif F_CPU == 192000000
  79. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1280)
  80. #elif F_CPU == 180000000
  81. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1200)
  82. #elif F_CPU == 168000000
  83. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  84. #elif F_CPU == 144000000
  85. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  86. #elif F_CPU == 120000000
  87. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  88. #elif F_CPU == 96000000
  89. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  90. #elif F_CPU == 72000000
  91. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  92. #elif F_CPU == 48000000
  93. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  94. #elif F_CPU == 24000000
  95. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  96. #endif
  97. // This 32 bit input format is documented in the "Universal Serial Bus Device Class
  98. // Definition for MIDI Devices" specification, version 1.0, Nov 1, 1999. It can be
  99. // downloaded from www.usb.org. https://www.usb.org/sites/default/files/midi10.pdf
  100. // If the USB-IF reorganizes their website and this link no longer works, Google
  101. // search the name to find it. This data format is shown on page 16 in Figure #8.
  102. // Byte 0 (shown on the left hand side of Figure #8) is the least significant byte
  103. // of this 32 bit input.
  104. void usb_midi_write_packed(uint32_t n)
  105. {
  106. uint32_t index, wait_count=0;
  107. tx_noautoflush = 1;
  108. if (!tx_packet) {
  109. while (1) {
  110. if (!usb_configuration) {
  111. //serial_print("error1\n");
  112. return;
  113. }
  114. if (usb_tx_packet_count(MIDI_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  115. tx_packet = usb_malloc();
  116. if (tx_packet) break;
  117. }
  118. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  119. transmit_previous_timeout = 1;
  120. //serial_print("error2\n");
  121. return;
  122. }
  123. yield();
  124. }
  125. }
  126. transmit_previous_timeout = 0;
  127. index = tx_packet->index;
  128. ((uint32_t *)(tx_packet->buf))[index++] = n;
  129. if (index < MIDI_TX_SIZE/4) {
  130. tx_packet->index = index;
  131. } else {
  132. tx_packet->len = MIDI_TX_SIZE;
  133. usb_tx(MIDI_TX_ENDPOINT, tx_packet);
  134. tx_packet = NULL;
  135. }
  136. tx_noautoflush = 0;
  137. }
  138. void usb_midi_send_sysex_buffer_has_term(const uint8_t *data, uint32_t length, uint8_t cable)
  139. {
  140. cable = (cable & 0x0F) << 4;
  141. while (length > 3) {
  142. usb_midi_write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  143. data += 3;
  144. length -= 3;
  145. }
  146. if (length == 3) {
  147. usb_midi_write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  148. } else if (length == 2) {
  149. usb_midi_write_packed(0x06 | cable | (data[0] << 8) | (data[1] << 16));
  150. } else if (length == 1) {
  151. usb_midi_write_packed(0x05 | cable | (data[0] << 8));
  152. }
  153. }
  154. void usb_midi_send_sysex_add_term_bytes(const uint8_t *data, uint32_t length, uint8_t cable)
  155. {
  156. cable = (cable & 0x0F) << 4;
  157. if (length == 0) {
  158. usb_midi_write_packed(0x06 | cable | (0xF0 << 8) | (0xF7 << 16));
  159. return;
  160. } else if (length == 1) {
  161. usb_midi_write_packed(0x07 | cable | (0xF0 << 8) | (data[0] << 16) | (0xF7 << 24));
  162. return;
  163. } else {
  164. usb_midi_write_packed(0x04 | cable | (0xF0 << 8) | (data[0] << 16) | (data[1] << 24));
  165. data += 2;
  166. length -= 2;
  167. }
  168. while (length >= 3) {
  169. usb_midi_write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  170. data += 3;
  171. length -= 3;
  172. }
  173. if (length == 2) {
  174. usb_midi_write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (0xF7 << 24));
  175. } else if (length == 1) {
  176. usb_midi_write_packed(0x06 | cable | (data[0] << 8) | (0xF7 << 16));
  177. } else {
  178. usb_midi_write_packed(0x05 | cable | (0xF7 << 8));
  179. }
  180. }
  181. void usb_midi_flush_output(void)
  182. {
  183. if (tx_noautoflush == 0 && tx_packet && tx_packet->index > 0) {
  184. tx_packet->len = tx_packet->index * 4;
  185. usb_tx(MIDI_TX_ENDPOINT, tx_packet);
  186. tx_packet = NULL;
  187. }
  188. }
  189. void static sysex_byte(uint8_t b)
  190. {
  191. if (usb_midi_handleSysExPartial && usb_midi_msg_sysex_len >= USB_MIDI_SYSEX_MAX) {
  192. // when buffer is full, send another chunk to partial handler.
  193. (*usb_midi_handleSysExPartial)(usb_midi_msg_sysex, usb_midi_msg_sysex_len, 0);
  194. usb_midi_msg_sysex_len = 0;
  195. }
  196. if (usb_midi_msg_sysex_len < USB_MIDI_SYSEX_MAX) {
  197. usb_midi_msg_sysex[usb_midi_msg_sysex_len++] = b;
  198. }
  199. }
  200. uint32_t usb_midi_available(void)
  201. {
  202. uint32_t index;
  203. if (!rx_packet) {
  204. if (!usb_configuration) return 0;
  205. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  206. if (!rx_packet) return 0;
  207. if (rx_packet->len == 0) {
  208. usb_free(rx_packet);
  209. rx_packet = NULL;
  210. return 0;
  211. }
  212. }
  213. index = rx_packet->index;
  214. return rx_packet->len - index;
  215. }
  216. uint32_t usb_midi_read_message(void)
  217. {
  218. uint32_t n, index;
  219. if (!rx_packet) {
  220. if (!usb_configuration) return 0;
  221. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  222. if (!rx_packet) return 0;
  223. if (rx_packet->len == 0) {
  224. usb_free(rx_packet);
  225. rx_packet = NULL;
  226. return 0;
  227. }
  228. }
  229. index = rx_packet->index;
  230. n = ((uint32_t *)rx_packet->buf)[index/4];
  231. index += 4;
  232. if (index < rx_packet->len) {
  233. rx_packet->index = index;
  234. } else {
  235. usb_free(rx_packet);
  236. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  237. }
  238. return n;
  239. }
  240. int usb_midi_read(uint32_t channel)
  241. {
  242. uint32_t n, index, ch, type1, type2, b1;
  243. if (!rx_packet) {
  244. if (!usb_configuration) return 0;
  245. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  246. if (!rx_packet) return 0;
  247. if (rx_packet->len == 0) {
  248. usb_free(rx_packet);
  249. rx_packet = NULL;
  250. return 0;
  251. }
  252. }
  253. index = rx_packet->index;
  254. n = ((uint32_t *)rx_packet->buf)[index/4];
  255. //serial_print("midi rx, n=");
  256. //serial_phex32(n);
  257. //serial_print("\n");
  258. index += 4;
  259. if (index < rx_packet->len) {
  260. rx_packet->index = index;
  261. } else {
  262. usb_free(rx_packet);
  263. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  264. }
  265. type1 = n & 15;
  266. type2 = (n >> 12) & 15;
  267. b1 = (n >> 8) & 0xFF;
  268. ch = (b1 & 15) + 1;
  269. usb_midi_msg_cable = (n >> 4) & 15;
  270. if (type1 >= 0x08 && type1 <= 0x0E) {
  271. if (channel && channel != ch) {
  272. // ignore other channels when user wants single channel read
  273. return 0;
  274. }
  275. if (type1 == 0x08 && type2 == 0x08) {
  276. usb_midi_msg_type = 0x80; // 0x80 = usbMIDI.NoteOff
  277. if (usb_midi_handleNoteOff)
  278. (*usb_midi_handleNoteOff)(ch, (n >> 16), (n >> 24));
  279. } else
  280. if (type1 == 0x09 && type2 == 0x09) {
  281. if ((n >> 24) > 0) {
  282. usb_midi_msg_type = 0x90; // 0x90 = usbMIDI.NoteOn
  283. if (usb_midi_handleNoteOn)
  284. (*usb_midi_handleNoteOn)(ch, (n >> 16), (n >> 24));
  285. } else {
  286. usb_midi_msg_type = 0x80; // 0x80 = usbMIDI.NoteOff
  287. if (usb_midi_handleNoteOff)
  288. (*usb_midi_handleNoteOff)(ch, (n >> 16), (n >> 24));
  289. }
  290. } else
  291. if (type1 == 0x0A && type2 == 0x0A) {
  292. usb_midi_msg_type = 0xA0; // 0xA0 = usbMIDI.AfterTouchPoly
  293. if (usb_midi_handleVelocityChange)
  294. (*usb_midi_handleVelocityChange)(ch, (n >> 16), (n >> 24));
  295. } else
  296. if (type1 == 0x0B && type2 == 0x0B) {
  297. usb_midi_msg_type = 0xB0; // 0xB0 = usbMIDI.ControlChange
  298. if (usb_midi_handleControlChange)
  299. (*usb_midi_handleControlChange)(ch, (n >> 16), (n >> 24));
  300. } else
  301. if (type1 == 0x0C && type2 == 0x0C) {
  302. usb_midi_msg_type = 0xC0; // 0xC0 = usbMIDI.ProgramChange
  303. if (usb_midi_handleProgramChange)
  304. (*usb_midi_handleProgramChange)(ch, (n >> 16));
  305. } else
  306. if (type1 == 0x0D && type2 == 0x0D) {
  307. usb_midi_msg_type = 0xD0; // 0xD0 = usbMIDI.AfterTouchChannel
  308. if (usb_midi_handleAfterTouch)
  309. (*usb_midi_handleAfterTouch)(ch, (n >> 16));
  310. } else
  311. if (type1 == 0x0E && type2 == 0x0E) {
  312. usb_midi_msg_type = 0xE0; // 0xE0 = usbMIDI.PitchBend
  313. if (usb_midi_handlePitchChange) {
  314. int value = ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80);
  315. value -= 8192; // 0 to 16383 --> -8192 to +8191
  316. (*usb_midi_handlePitchChange)(ch, value);
  317. }
  318. } else {
  319. return 0;
  320. }
  321. return_message:
  322. usb_midi_msg_channel = ch;
  323. usb_midi_msg_data1 = (n >> 16);
  324. usb_midi_msg_data2 = (n >> 24);
  325. return 1;
  326. }
  327. if (type1 == 0x02 || type1 == 0x03 || (type1 == 0x05 && b1 >= 0xF1 && b1 != 0xF7)) {
  328. // system common or system realtime message
  329. system_common_or_realtime:
  330. switch (b1) {
  331. case 0xF1: // usbMIDI.TimeCodeQuarterFrame
  332. if (usb_midi_handleTimeCodeQuarterFrame) {
  333. (*usb_midi_handleTimeCodeQuarterFrame)(n >> 16);
  334. }
  335. break;
  336. case 0xF2: // usbMIDI.SongPosition
  337. if (usb_midi_handleSongPosition) {
  338. (*usb_midi_handleSongPosition)(
  339. ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
  340. }
  341. break;
  342. case 0xF3: // usbMIDI.SongSelect
  343. if (usb_midi_handleSongSelect) {
  344. (*usb_midi_handleSongSelect)(n >> 16);
  345. }
  346. break;
  347. case 0xF6: // usbMIDI.TuneRequest
  348. if (usb_midi_handleTuneRequest) {
  349. (*usb_midi_handleTuneRequest)();
  350. }
  351. break;
  352. case 0xF8: // usbMIDI.Clock
  353. if (usb_midi_handleClock) {
  354. (*usb_midi_handleClock)();
  355. } else if (usb_midi_handleRealTimeSystem) {
  356. (*usb_midi_handleRealTimeSystem)(0xF8);
  357. }
  358. break;
  359. case 0xFA: // usbMIDI.Start
  360. if (usb_midi_handleStart) {
  361. (*usb_midi_handleStart)();
  362. } else if (usb_midi_handleRealTimeSystem) {
  363. (*usb_midi_handleRealTimeSystem)(0xFA);
  364. }
  365. break;
  366. case 0xFB: // usbMIDI.Continue
  367. if (usb_midi_handleContinue) {
  368. (*usb_midi_handleContinue)();
  369. } else if (usb_midi_handleRealTimeSystem) {
  370. (*usb_midi_handleRealTimeSystem)(0xFB);
  371. }
  372. break;
  373. case 0xFC: // usbMIDI.Stop
  374. if (usb_midi_handleStop) {
  375. (*usb_midi_handleStop)();
  376. } else if (usb_midi_handleRealTimeSystem) {
  377. (*usb_midi_handleRealTimeSystem)(0xFC);
  378. }
  379. break;
  380. case 0xFE: // usbMIDI.ActiveSensing
  381. if (usb_midi_handleActiveSensing) {
  382. (*usb_midi_handleActiveSensing)();
  383. } else if (usb_midi_handleRealTimeSystem) {
  384. (*usb_midi_handleRealTimeSystem)(0xFE);
  385. }
  386. break;
  387. case 0xFF: // usbMIDI.SystemReset
  388. if (usb_midi_handleSystemReset) {
  389. (*usb_midi_handleSystemReset)();
  390. } else if (usb_midi_handleRealTimeSystem) {
  391. (*usb_midi_handleRealTimeSystem)(0xFF);
  392. }
  393. break;
  394. default:
  395. return 0; // unknown message, ignore it
  396. }
  397. usb_midi_msg_type = b1;
  398. goto return_message;
  399. }
  400. if (type1 == 0x04) {
  401. sysex_byte(n >> 8);
  402. sysex_byte(n >> 16);
  403. sysex_byte(n >> 24);
  404. return 0;
  405. }
  406. if (type1 >= 0x05 && type1 <= 0x07) {
  407. sysex_byte(b1);
  408. if (type1 >= 0x06) sysex_byte(n >> 16);
  409. if (type1 == 0x07) sysex_byte(n >> 24);
  410. uint16_t len = usb_midi_msg_sysex_len;
  411. usb_midi_msg_data1 = len;
  412. usb_midi_msg_data2 = len >> 8;
  413. usb_midi_msg_sysex_len = 0;
  414. usb_midi_msg_type = 0xF0; // 0xF0 = usbMIDI.SystemExclusive
  415. if (usb_midi_handleSysExPartial) {
  416. (*usb_midi_handleSysExPartial)(usb_midi_msg_sysex, len, 1);
  417. } else if (usb_midi_handleSysExComplete) {
  418. (*usb_midi_handleSysExComplete)(usb_midi_msg_sysex, len);
  419. }
  420. return 1;
  421. }
  422. if (type1 == 0x0F) {
  423. if (b1 >= 0xF8) {
  424. // From Sebastian Tomczak, seb.tomczak at gmail.com
  425. // http://little-scale.blogspot.com/2011/08/usb-midi-game-boy-sync-for-16.html
  426. goto system_common_or_realtime;
  427. }
  428. if (b1 == 0xF0 || usb_midi_msg_sysex_len > 0) {
  429. // From David Sorlien, dsorlien at gmail.com, http://axe4live.wordpress.com
  430. // OSX sometimes uses Single Byte Unparsed to
  431. // send bytes in the middle of a SYSEX message.
  432. sysex_byte(b1);
  433. }
  434. }
  435. return 0;
  436. }
  437. #endif // F_CPU
  438. #endif // MIDI_INTERFACE