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.

usb_midi.c 14KB

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