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.

454 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. if (index < MIDI_TX_SIZE/4) {
  121. tx_packet->index = index;
  122. } else {
  123. tx_packet->len = MIDI_TX_SIZE;
  124. usb_tx(MIDI_TX_ENDPOINT, tx_packet);
  125. tx_packet = usb_malloc();
  126. }
  127. tx_noautoflush = 0;
  128. }
  129. void usb_midi_send_sysex_buffer_has_term(const uint8_t *data, uint32_t length, uint8_t cable)
  130. {
  131. cable = (cable & 0x0F) << 4;
  132. while (length > 3) {
  133. usb_midi_write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  134. data += 3;
  135. length -= 3;
  136. }
  137. if (length == 3) {
  138. usb_midi_write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  139. } else if (length == 2) {
  140. usb_midi_write_packed(0x06 | cable | (data[0] << 8) | (data[1] << 16));
  141. } else if (length == 1) {
  142. usb_midi_write_packed(0x05 | cable | (data[0] << 8));
  143. }
  144. }
  145. void usb_midi_send_sysex_add_term_bytes(const uint8_t *data, uint32_t length, uint8_t cable)
  146. {
  147. cable = (cable & 0x0F) << 4;
  148. if (length == 0) {
  149. usb_midi_write_packed(0x06 | cable | (0xF0 << 8) | (0xF7 << 16));
  150. return;
  151. } else if (length == 1) {
  152. usb_midi_write_packed(0x07 | cable | (0xF0 << 8) | (data[0] << 16) | (0xF7 << 24));
  153. return;
  154. } else {
  155. usb_midi_write_packed(0x04 | cable | (0xF0 << 8) | (data[0] << 16) | (data[1] << 24));
  156. data += 2;
  157. length -= 2;
  158. }
  159. while (length >= 3) {
  160. usb_midi_write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  161. data += 3;
  162. length -= 3;
  163. }
  164. if (length == 2) {
  165. usb_midi_write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (0xF7 << 24));
  166. } else if (length == 1) {
  167. usb_midi_write_packed(0x06 | cable | (data[0] << 8) | (0xF7 << 16));
  168. } else {
  169. usb_midi_write_packed(0x05 | cable | (0xF7 << 8));
  170. }
  171. }
  172. void usb_midi_flush_output(void)
  173. {
  174. if (tx_noautoflush == 0 && tx_packet && tx_packet->index > 0) {
  175. tx_packet->len = tx_packet->index * 4;
  176. usb_tx(MIDI_TX_ENDPOINT, tx_packet);
  177. tx_packet = usb_malloc();
  178. }
  179. }
  180. void static sysex_byte(uint8_t b)
  181. {
  182. if (usb_midi_handleSysExPartial && usb_midi_msg_sysex_len >= USB_MIDI_SYSEX_MAX) {
  183. // when buffer is full, send another chunk to partial handler.
  184. (*usb_midi_handleSysExPartial)(usb_midi_msg_sysex, usb_midi_msg_sysex_len, 0);
  185. usb_midi_msg_sysex_len = 0;
  186. }
  187. if (usb_midi_msg_sysex_len < USB_MIDI_SYSEX_MAX) {
  188. usb_midi_msg_sysex[usb_midi_msg_sysex_len++] = b;
  189. }
  190. }
  191. uint32_t usb_midi_available(void)
  192. {
  193. uint32_t index;
  194. if (!rx_packet) {
  195. if (!usb_configuration) return 0;
  196. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  197. if (!rx_packet) return 0;
  198. if (rx_packet->len == 0) {
  199. usb_free(rx_packet);
  200. rx_packet = NULL;
  201. return 0;
  202. }
  203. }
  204. index = rx_packet->index;
  205. return rx_packet->len - index;
  206. }
  207. uint32_t usb_midi_read_message(void)
  208. {
  209. uint32_t n, index;
  210. if (!rx_packet) {
  211. if (!usb_configuration) return 0;
  212. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  213. if (!rx_packet) return 0;
  214. if (rx_packet->len == 0) {
  215. usb_free(rx_packet);
  216. rx_packet = NULL;
  217. return 0;
  218. }
  219. }
  220. index = rx_packet->index;
  221. n = ((uint32_t *)rx_packet->buf)[index/4];
  222. index += 4;
  223. if (index < rx_packet->len) {
  224. rx_packet->index = index;
  225. } else {
  226. usb_free(rx_packet);
  227. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  228. }
  229. return n;
  230. }
  231. int usb_midi_read(uint32_t channel)
  232. {
  233. uint32_t n, index, ch, type1, type2, b1;
  234. if (!rx_packet) {
  235. if (!usb_configuration) return 0;
  236. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  237. if (!rx_packet) return 0;
  238. if (rx_packet->len == 0) {
  239. usb_free(rx_packet);
  240. rx_packet = NULL;
  241. return 0;
  242. }
  243. }
  244. index = rx_packet->index;
  245. n = ((uint32_t *)rx_packet->buf)[index/4];
  246. //serial_print("midi rx, n=");
  247. //serial_phex32(n);
  248. //serial_print("\n");
  249. index += 4;
  250. if (index < rx_packet->len) {
  251. rx_packet->index = index;
  252. } else {
  253. usb_free(rx_packet);
  254. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  255. }
  256. type1 = n & 15;
  257. type2 = (n >> 12) & 15;
  258. b1 = (n >> 8) & 0xFF;
  259. ch = (b1 & 15) + 1;
  260. usb_midi_msg_cable = (n >> 4) & 15;
  261. if (type1 >= 0x08 && type1 <= 0x0E) {
  262. if (channel && channel != ch) {
  263. // ignore other channels when user wants single channel read
  264. return 0;
  265. }
  266. if (type1 == 0x08 && type2 == 0x08) {
  267. usb_midi_msg_type = 0x80; // 0x80 = usbMIDI.NoteOff
  268. if (usb_midi_handleNoteOff)
  269. (*usb_midi_handleNoteOff)(ch, (n >> 16), (n >> 24));
  270. } else
  271. if (type1 == 0x09 && type2 == 0x09) {
  272. if ((n >> 24) > 0) {
  273. usb_midi_msg_type = 0x90; // 0x90 = usbMIDI.NoteOn
  274. if (usb_midi_handleNoteOn)
  275. (*usb_midi_handleNoteOn)(ch, (n >> 16), (n >> 24));
  276. } else {
  277. usb_midi_msg_type = 0x80; // 0x80 = usbMIDI.NoteOff
  278. if (usb_midi_handleNoteOff)
  279. (*usb_midi_handleNoteOff)(ch, (n >> 16), (n >> 24));
  280. }
  281. } else
  282. if (type1 == 0x0A && type2 == 0x0A) {
  283. usb_midi_msg_type = 0xA0; // 0xA0 = usbMIDI.AfterTouchPoly
  284. if (usb_midi_handleVelocityChange)
  285. (*usb_midi_handleVelocityChange)(ch, (n >> 16), (n >> 24));
  286. } else
  287. if (type1 == 0x0B && type2 == 0x0B) {
  288. usb_midi_msg_type = 0xB0; // 0xB0 = usbMIDI.ControlChange
  289. if (usb_midi_handleControlChange)
  290. (*usb_midi_handleControlChange)(ch, (n >> 16), (n >> 24));
  291. } else
  292. if (type1 == 0x0C && type2 == 0x0C) {
  293. usb_midi_msg_type = 0xC0; // 0xC0 = usbMIDI.ProgramChange
  294. if (usb_midi_handleProgramChange)
  295. (*usb_midi_handleProgramChange)(ch, (n >> 16));
  296. } else
  297. if (type1 == 0x0D && type2 == 0x0D) {
  298. usb_midi_msg_type = 0xD0; // 0xD0 = usbMIDI.AfterTouchChannel
  299. if (usb_midi_handleAfterTouch)
  300. (*usb_midi_handleAfterTouch)(ch, (n >> 16));
  301. } else
  302. if (type1 == 0x0E && type2 == 0x0E) {
  303. usb_midi_msg_type = 0xE0; // 0xE0 = usbMIDI.PitchBend
  304. if (usb_midi_handlePitchChange) {
  305. int value = ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80);
  306. value -= 8192; // 0 to 16383 --> -8192 to +8191
  307. (*usb_midi_handlePitchChange)(ch, value);
  308. }
  309. } else {
  310. return 0;
  311. }
  312. return_message:
  313. usb_midi_msg_channel = ch;
  314. usb_midi_msg_data1 = (n >> 16);
  315. usb_midi_msg_data2 = (n >> 24);
  316. return 1;
  317. }
  318. if (type1 == 0x02 || type1 == 0x03 || (type1 == 0x05 && b1 >= 0xF1 && b1 != 0xF7)) {
  319. // system common or system realtime message
  320. system_common_or_realtime:
  321. switch (b1) {
  322. case 0xF1: // usbMIDI.TimeCodeQuarterFrame
  323. if (usb_midi_handleTimeCodeQuarterFrame) {
  324. (*usb_midi_handleTimeCodeQuarterFrame)(n >> 16);
  325. }
  326. break;
  327. case 0xF2: // usbMIDI.SongPosition
  328. if (usb_midi_handleSongPosition) {
  329. (*usb_midi_handleSongPosition)(
  330. ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
  331. }
  332. break;
  333. case 0xF3: // usbMIDI.SongSelect
  334. if (usb_midi_handleSongSelect) {
  335. (*usb_midi_handleSongSelect)(n >> 16);
  336. }
  337. break;
  338. case 0xF6: // usbMIDI.TuneRequest
  339. if (usb_midi_handleTuneRequest) {
  340. (*usb_midi_handleTuneRequest)();
  341. }
  342. break;
  343. case 0xF8: // usbMIDI.Clock
  344. if (usb_midi_handleClock) {
  345. (*usb_midi_handleClock)();
  346. } else if (usb_midi_handleRealTimeSystem) {
  347. (*usb_midi_handleRealTimeSystem)(0xF8);
  348. }
  349. break;
  350. case 0xFA: // usbMIDI.Start
  351. if (usb_midi_handleStart) {
  352. (*usb_midi_handleStart)();
  353. } else if (usb_midi_handleRealTimeSystem) {
  354. (*usb_midi_handleRealTimeSystem)(0xFA);
  355. }
  356. break;
  357. case 0xFB: // usbMIDI.Continue
  358. if (usb_midi_handleContinue) {
  359. (*usb_midi_handleContinue)();
  360. } else if (usb_midi_handleRealTimeSystem) {
  361. (*usb_midi_handleRealTimeSystem)(0xFB);
  362. }
  363. break;
  364. case 0xFC: // usbMIDI.Stop
  365. if (usb_midi_handleStop) {
  366. (*usb_midi_handleStop)();
  367. } else if (usb_midi_handleRealTimeSystem) {
  368. (*usb_midi_handleRealTimeSystem)(0xFC);
  369. }
  370. break;
  371. case 0xFE: // usbMIDI.ActiveSensing
  372. if (usb_midi_handleActiveSensing) {
  373. (*usb_midi_handleActiveSensing)();
  374. } else if (usb_midi_handleRealTimeSystem) {
  375. (*usb_midi_handleRealTimeSystem)(0xFE);
  376. }
  377. break;
  378. case 0xFF: // usbMIDI.SystemReset
  379. if (usb_midi_handleSystemReset) {
  380. (*usb_midi_handleSystemReset)();
  381. } else if (usb_midi_handleRealTimeSystem) {
  382. (*usb_midi_handleRealTimeSystem)(0xFF);
  383. }
  384. break;
  385. default:
  386. return 0; // unknown message, ignore it
  387. }
  388. usb_midi_msg_type = b1;
  389. goto return_message;
  390. }
  391. if (type1 == 0x04) {
  392. sysex_byte(n >> 8);
  393. sysex_byte(n >> 16);
  394. sysex_byte(n >> 24);
  395. return 0;
  396. }
  397. if (type1 >= 0x05 && type1 <= 0x07) {
  398. sysex_byte(b1);
  399. if (type1 >= 0x06) sysex_byte(n >> 16);
  400. if (type1 == 0x07) sysex_byte(n >> 24);
  401. uint16_t len = usb_midi_msg_sysex_len;
  402. usb_midi_msg_data1 = len;
  403. usb_midi_msg_data2 = len >> 8;
  404. usb_midi_msg_sysex_len = 0;
  405. usb_midi_msg_type = 0xF0; // 0xF0 = usbMIDI.SystemExclusive
  406. if (usb_midi_handleSysExPartial) {
  407. (*usb_midi_handleSysExPartial)(usb_midi_msg_sysex, len, 1);
  408. } else if (usb_midi_handleSysExComplete) {
  409. (*usb_midi_handleSysExComplete)(usb_midi_msg_sysex, len);
  410. }
  411. return 1;
  412. }
  413. if (type1 == 0x0F) {
  414. if (b1 >= 0xF8) {
  415. // From Sebastian Tomczak, seb.tomczak at gmail.com
  416. // http://little-scale.blogspot.com/2011/08/usb-midi-game-boy-sync-for-16.html
  417. goto system_common_or_realtime;
  418. }
  419. if (b1 == 0xF0 || usb_midi_msg_sysex_len > 0) {
  420. // From David Sorlien, dsorlien at gmail.com, http://axe4live.wordpress.com
  421. // OSX sometimes uses Single Byte Unparsed to
  422. // send bytes in the middle of a SYSEX message.
  423. sysex_byte(b1);
  424. }
  425. }
  426. return 0;
  427. }
  428. #endif // F_CPU
  429. #endif // MIDI_INTERFACE