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.

499 lines
16KB

  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 <string.h> // for memcpy()
  34. #include "avr/pgmspace.h" // for PROGMEM, DMAMEM, FASTRUN
  35. #include "debug/printf.h"
  36. #include "core_pins.h"
  37. #ifdef MIDI_INTERFACE // defined by usb_dev.h -> usb_desc.h
  38. uint8_t usb_midi_msg_cable;
  39. uint8_t usb_midi_msg_channel;
  40. uint8_t usb_midi_msg_type;
  41. uint8_t usb_midi_msg_data1;
  42. uint8_t usb_midi_msg_data2;
  43. // TODO: separate sysex buffers for each cable...
  44. uint8_t usb_midi_msg_sysex[USB_MIDI_SYSEX_MAX];
  45. uint16_t usb_midi_msg_sysex_len;
  46. void (*usb_midi_handleNoteOff)(uint8_t ch, uint8_t note, uint8_t vel) = NULL;
  47. void (*usb_midi_handleNoteOn)(uint8_t ch, uint8_t note, uint8_t vel) = NULL;
  48. void (*usb_midi_handleVelocityChange)(uint8_t ch, uint8_t note, uint8_t vel) = NULL;
  49. void (*usb_midi_handleControlChange)(uint8_t ch, uint8_t control, uint8_t value) = NULL;
  50. void (*usb_midi_handleProgramChange)(uint8_t ch, uint8_t program) = NULL;
  51. void (*usb_midi_handleAfterTouch)(uint8_t ch, uint8_t pressure) = NULL;
  52. void (*usb_midi_handlePitchChange)(uint8_t ch, int pitch) = NULL;
  53. void (*usb_midi_handleSysExPartial)(const uint8_t *data, uint16_t length, uint8_t complete) = NULL;
  54. void (*usb_midi_handleSysExComplete)(uint8_t *data, unsigned int size) = NULL;
  55. void (*usb_midi_handleTimeCodeQuarterFrame)(uint8_t data) = NULL;
  56. void (*usb_midi_handleSongPosition)(uint16_t beats) = NULL;
  57. void (*usb_midi_handleSongSelect)(uint8_t songnumber) = NULL;
  58. void (*usb_midi_handleTuneRequest)(void) = NULL;
  59. void (*usb_midi_handleClock)(void) = NULL;
  60. void (*usb_midi_handleStart)(void) = NULL;
  61. void (*usb_midi_handleContinue)(void) = NULL;
  62. void (*usb_midi_handleStop)(void) = NULL;
  63. void (*usb_midi_handleActiveSensing)(void) = NULL;
  64. void (*usb_midi_handleSystemReset)(void) = NULL;
  65. void (*usb_midi_handleRealTimeSystem)(uint8_t rtb) = NULL;
  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. extern volatile uint8_t usb_high_speed;
  71. #define TX_NUM 4
  72. #define TX_SIZE 512 /* should be a multiple of MIDI_TX_SIZE_480 */
  73. static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32)));
  74. DMAMEM static uint8_t txbuffer[TX_SIZE * TX_NUM] __attribute__ ((aligned(32)));
  75. static uint8_t tx_head=0;
  76. static uint16_t tx_available=0;
  77. static uint16_t tx_packet_size=0;
  78. #define RX_NUM 6
  79. static transfer_t rx_transfer[RX_NUM] __attribute__ ((used, aligned(32)));
  80. DMAMEM static uint8_t rx_buffer[RX_NUM * MIDI_RX_SIZE_480] __attribute__ ((aligned(32)));
  81. static uint16_t rx_count[RX_NUM];
  82. static uint16_t rx_index[RX_NUM];
  83. static uint16_t rx_packet_size=0;
  84. static volatile uint8_t rx_head;
  85. static volatile uint8_t rx_tail;
  86. static uint8_t rx_list[RX_NUM + 1];
  87. static volatile uint32_t rx_available;
  88. static void rx_queue_transfer(int i);
  89. static void rx_event(transfer_t *t);
  90. void usb_midi_configure(void)
  91. {
  92. printf("usb_midi_configure\n");
  93. if (usb_high_speed) {
  94. tx_packet_size = MIDI_TX_SIZE_480;
  95. rx_packet_size = MIDI_RX_SIZE_480;
  96. } else {
  97. tx_packet_size = MIDI_TX_SIZE_12;
  98. rx_packet_size = MIDI_RX_SIZE_12;
  99. }
  100. memset(tx_transfer, 0, sizeof(tx_transfer));
  101. tx_head = 0;
  102. tx_available = 0;
  103. memset(rx_transfer, 0, sizeof(rx_transfer));
  104. memset(rx_count, 0, sizeof(rx_count));
  105. memset(rx_index, 0, sizeof(rx_index));
  106. rx_head = 0;
  107. rx_tail = 0;
  108. rx_available = 0;
  109. usb_config_rx(MIDI_RX_ENDPOINT, rx_packet_size, 0, rx_event);
  110. usb_config_tx(MIDI_TX_ENDPOINT, tx_packet_size, 0, NULL); // TODO: is ZLP needed?
  111. int i;
  112. for (i=0; i < RX_NUM; i++) rx_queue_transfer(i);
  113. transmit_previous_timeout = 0;
  114. tx_noautoflush = 0;
  115. }
  116. // When the PC isn't listening, how long do we wait before discarding data?
  117. #define TX_TIMEOUT_MSEC 40
  118. void usb_midi_write_packed(uint32_t n)
  119. {
  120. printf("usb_midi_write_packed\n");
  121. if (!usb_configuration) return;
  122. tx_noautoflush = 1;
  123. uint32_t head = tx_head;
  124. transfer_t *xfer = tx_transfer + head;
  125. uint32_t wait_begin_at = systick_millis_count;
  126. while (!tx_available) {
  127. uint32_t status = usb_transfer_status(xfer);
  128. if (!(status & 0x80)) {
  129. if (status & 0x68) {
  130. // TODO: what if status has errors???
  131. }
  132. tx_available = tx_packet_size;
  133. transmit_previous_timeout = 0;
  134. break;
  135. }
  136. if (systick_millis_count - wait_begin_at > TX_TIMEOUT_MSEC) {
  137. transmit_previous_timeout = 1;
  138. }
  139. if (transmit_previous_timeout) return;
  140. if (!usb_configuration) return;
  141. yield();
  142. }
  143. uint32_t *txdata = (uint32_t *)(txbuffer + (tx_head * TX_SIZE) + (TX_SIZE - tx_available));
  144. *txdata = n;
  145. tx_available -= 4;
  146. if (tx_available == 0) {
  147. uint8_t *txbuf = txbuffer + (tx_head * TX_SIZE);
  148. usb_prepare_transfer(xfer, txbuf, tx_packet_size, 0);
  149. arm_dcache_flush_delete(txbuf, TX_SIZE);
  150. usb_transmit(MIDI_TX_ENDPOINT, xfer);
  151. if (++head >= TX_NUM) head = 0;
  152. tx_head = head;
  153. usb_stop_sof_interrupts(MIDI_INTERFACE);
  154. } else {
  155. usb_start_sof_interrupts(MIDI_INTERFACE);
  156. }
  157. tx_noautoflush = 0;
  158. }
  159. void usb_midi_flush_output(void)
  160. {
  161. //printf("usb_midi_flush_output\n");
  162. if (tx_noautoflush == 0 && tx_available > 0) {
  163. printf(" tx, %d %d\n", tx_packet_size, tx_available);
  164. uint32_t head = tx_head;
  165. transfer_t *xfer = tx_transfer + head;
  166. uint8_t *txbuf = txbuffer + (head * TX_SIZE);
  167. uint32_t len = tx_packet_size - tx_available;
  168. usb_prepare_transfer(xfer, txbuf, len, 0);
  169. arm_dcache_flush_delete(txbuf, TX_SIZE);
  170. usb_transmit(MIDI_TX_ENDPOINT, xfer);
  171. if (++head >= TX_NUM) head = 0;
  172. tx_head = head;
  173. tx_available = 0;
  174. usb_stop_sof_interrupts(MIDI_INTERFACE);
  175. }
  176. }
  177. void usb_midi_send_sysex_buffer_has_term(const uint8_t *data, uint32_t length, uint8_t cable)
  178. {
  179. cable = (cable & 0x0F) << 4;
  180. while (length > 3) {
  181. usb_midi_write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  182. data += 3;
  183. length -= 3;
  184. }
  185. if (length == 3) {
  186. usb_midi_write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  187. } else if (length == 2) {
  188. usb_midi_write_packed(0x06 | cable | (data[0] << 8) | (data[1] << 16));
  189. } else if (length == 1) {
  190. usb_midi_write_packed(0x05 | cable | (data[0] << 8));
  191. }
  192. }
  193. void usb_midi_send_sysex_add_term_bytes(const uint8_t *data, uint32_t length, uint8_t cable)
  194. {
  195. cable = (cable & 0x0F) << 4;
  196. if (length == 0) {
  197. usb_midi_write_packed(0x06 | cable | (0xF0 << 8) | (0xF7 << 16));
  198. return;
  199. } else if (length == 1) {
  200. usb_midi_write_packed(0x07 | cable | (0xF0 << 8) | (data[0] << 16) | (0xF7 << 24));
  201. return;
  202. } else {
  203. usb_midi_write_packed(0x04 | cable | (0xF0 << 8) | (data[0] << 16) | (data[1] << 24));
  204. data += 2;
  205. length -= 2;
  206. }
  207. while (length >= 3) {
  208. usb_midi_write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  209. data += 3;
  210. length -= 3;
  211. }
  212. if (length == 2) {
  213. usb_midi_write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (0xF7 << 24));
  214. } else if (length == 1) {
  215. usb_midi_write_packed(0x06 | cable | (data[0] << 8) | (0xF7 << 16));
  216. } else {
  217. usb_midi_write_packed(0x05 | cable | (0xF7 << 8));
  218. }
  219. }
  220. void static sysex_byte(uint8_t b)
  221. {
  222. if (usb_midi_handleSysExPartial && usb_midi_msg_sysex_len >= USB_MIDI_SYSEX_MAX) {
  223. // when buffer is full, send another chunk to partial handler.
  224. (*usb_midi_handleSysExPartial)(usb_midi_msg_sysex, usb_midi_msg_sysex_len, 0);
  225. usb_midi_msg_sysex_len = 0;
  226. }
  227. if (usb_midi_msg_sysex_len < USB_MIDI_SYSEX_MAX) {
  228. usb_midi_msg_sysex[usb_midi_msg_sysex_len++] = b;
  229. }
  230. }
  231. static void rx_queue_transfer(int i)
  232. {
  233. NVIC_DISABLE_IRQ(IRQ_USB1);
  234. void *buffer = rx_buffer + i * MIDI_RX_SIZE_480;
  235. usb_prepare_transfer(rx_transfer + i, buffer, rx_packet_size, i);
  236. arm_dcache_delete(buffer, rx_packet_size);
  237. usb_receive(MIDI_RX_ENDPOINT, rx_transfer + i);
  238. NVIC_ENABLE_IRQ(IRQ_USB1);
  239. }
  240. // called by USB interrupt when any packet is received
  241. static void rx_event(transfer_t *t)
  242. {
  243. int len = rx_packet_size - ((t->status >> 16) & 0x7FFF);
  244. len &= 0xFFFC; // MIDI packets must be multiple of 4 bytes
  245. int i = t->callback_param;
  246. printf("rx event, len=%d, i=%d\n", len, i);
  247. if (len > 0) {
  248. uint32_t head = rx_head;
  249. rx_count[i] = len;
  250. rx_index[i] = 0;
  251. if (++head > RX_NUM) head = 0;
  252. rx_list[head] = i;
  253. rx_head = head;
  254. rx_available += len;
  255. } else {
  256. // received a zero length packet
  257. rx_queue_transfer(i);
  258. }
  259. }
  260. uint32_t usb_midi_available(void)
  261. {
  262. return rx_available / 4;
  263. }
  264. uint32_t usb_midi_read_message(void)
  265. {
  266. uint32_t n = 0;
  267. NVIC_DISABLE_IRQ(IRQ_USB1);
  268. uint32_t tail = rx_tail;
  269. if (tail != rx_head) {
  270. if (++tail > RX_NUM) tail = 0;
  271. uint32_t i = rx_list[tail];
  272. //uint32_t avail = (rx_count[i] - rx_index[i]) / 4;
  273. void *p = rx_buffer + i * MIDI_RX_SIZE_480 + rx_index[i];
  274. n = *(uint32_t *)p;
  275. rx_available -= 4;
  276. rx_index[i] += 4;
  277. if (rx_index[i] >= rx_count[i]) {
  278. rx_tail = tail;
  279. rx_queue_transfer(i);
  280. }
  281. }
  282. NVIC_ENABLE_IRQ(IRQ_USB1);
  283. return n;
  284. }
  285. int usb_midi_read(uint32_t channel)
  286. {
  287. uint32_t n, ch, type1, type2, b1;
  288. n = usb_midi_read_message();
  289. if (n == 0) return 0;
  290. type1 = n & 15;
  291. type2 = (n >> 12) & 15;
  292. b1 = (n >> 8) & 0xFF;
  293. ch = (b1 & 15) + 1;
  294. usb_midi_msg_cable = (n >> 4) & 15;
  295. if (type1 >= 0x08 && type1 <= 0x0E) {
  296. if (channel && channel != ch) {
  297. // ignore other channels when user wants single channel read
  298. return 0;
  299. }
  300. if (type1 == 0x08 && type2 == 0x08) {
  301. usb_midi_msg_type = 0x80; // 0x80 = usbMIDI.NoteOff
  302. if (usb_midi_handleNoteOff)
  303. (*usb_midi_handleNoteOff)(ch, (n >> 16), (n >> 24));
  304. } else
  305. if (type1 == 0x09 && type2 == 0x09) {
  306. if ((n >> 24) > 0) {
  307. usb_midi_msg_type = 0x90; // 0x90 = usbMIDI.NoteOn
  308. if (usb_midi_handleNoteOn)
  309. (*usb_midi_handleNoteOn)(ch, (n >> 16), (n >> 24));
  310. } else {
  311. usb_midi_msg_type = 0x80; // 0x80 = usbMIDI.NoteOff
  312. if (usb_midi_handleNoteOff)
  313. (*usb_midi_handleNoteOff)(ch, (n >> 16), (n >> 24));
  314. }
  315. } else
  316. if (type1 == 0x0A && type2 == 0x0A) {
  317. usb_midi_msg_type = 0xA0; // 0xA0 = usbMIDI.AfterTouchPoly
  318. if (usb_midi_handleVelocityChange)
  319. (*usb_midi_handleVelocityChange)(ch, (n >> 16), (n >> 24));
  320. } else
  321. if (type1 == 0x0B && type2 == 0x0B) {
  322. usb_midi_msg_type = 0xB0; // 0xB0 = usbMIDI.ControlChange
  323. if (usb_midi_handleControlChange)
  324. (*usb_midi_handleControlChange)(ch, (n >> 16), (n >> 24));
  325. } else
  326. if (type1 == 0x0C && type2 == 0x0C) {
  327. usb_midi_msg_type = 0xC0; // 0xC0 = usbMIDI.ProgramChange
  328. if (usb_midi_handleProgramChange)
  329. (*usb_midi_handleProgramChange)(ch, (n >> 16));
  330. } else
  331. if (type1 == 0x0D && type2 == 0x0D) {
  332. usb_midi_msg_type = 0xD0; // 0xD0 = usbMIDI.AfterTouchChannel
  333. if (usb_midi_handleAfterTouch)
  334. (*usb_midi_handleAfterTouch)(ch, (n >> 16));
  335. } else
  336. if (type1 == 0x0E && type2 == 0x0E) {
  337. usb_midi_msg_type = 0xE0; // 0xE0 = usbMIDI.PitchBend
  338. if (usb_midi_handlePitchChange) {
  339. int value = ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80);
  340. value -= 8192; // 0 to 16383 --> -8192 to +8191
  341. (*usb_midi_handlePitchChange)(ch, value);
  342. }
  343. } else {
  344. return 0;
  345. }
  346. return_message:
  347. usb_midi_msg_channel = ch;
  348. usb_midi_msg_data1 = (n >> 16);
  349. usb_midi_msg_data2 = (n >> 24);
  350. return 1;
  351. }
  352. if (type1 == 0x02 || type1 == 0x03 || (type1 == 0x05 && b1 >= 0xF1 && b1 != 0xF7)) {
  353. // system common or system realtime message
  354. system_common_or_realtime:
  355. switch (b1) {
  356. case 0xF1: // usbMIDI.TimeCodeQuarterFrame
  357. if (usb_midi_handleTimeCodeQuarterFrame) {
  358. (*usb_midi_handleTimeCodeQuarterFrame)(n >> 16);
  359. }
  360. break;
  361. case 0xF2: // usbMIDI.SongPosition
  362. if (usb_midi_handleSongPosition) {
  363. (*usb_midi_handleSongPosition)(
  364. ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
  365. }
  366. break;
  367. case 0xF3: // usbMIDI.SongSelect
  368. if (usb_midi_handleSongSelect) {
  369. (*usb_midi_handleSongSelect)(n >> 16);
  370. }
  371. break;
  372. case 0xF6: // usbMIDI.TuneRequest
  373. if (usb_midi_handleTuneRequest) {
  374. (*usb_midi_handleTuneRequest)();
  375. }
  376. break;
  377. case 0xF8: // usbMIDI.Clock
  378. if (usb_midi_handleClock) {
  379. (*usb_midi_handleClock)();
  380. } else if (usb_midi_handleRealTimeSystem) {
  381. (*usb_midi_handleRealTimeSystem)(0xF8);
  382. }
  383. break;
  384. case 0xFA: // usbMIDI.Start
  385. if (usb_midi_handleStart) {
  386. (*usb_midi_handleStart)();
  387. } else if (usb_midi_handleRealTimeSystem) {
  388. (*usb_midi_handleRealTimeSystem)(0xFA);
  389. }
  390. break;
  391. case 0xFB: // usbMIDI.Continue
  392. if (usb_midi_handleContinue) {
  393. (*usb_midi_handleContinue)();
  394. } else if (usb_midi_handleRealTimeSystem) {
  395. (*usb_midi_handleRealTimeSystem)(0xFB);
  396. }
  397. break;
  398. case 0xFC: // usbMIDI.Stop
  399. if (usb_midi_handleStop) {
  400. (*usb_midi_handleStop)();
  401. } else if (usb_midi_handleRealTimeSystem) {
  402. (*usb_midi_handleRealTimeSystem)(0xFC);
  403. }
  404. break;
  405. case 0xFE: // usbMIDI.ActiveSensing
  406. if (usb_midi_handleActiveSensing) {
  407. (*usb_midi_handleActiveSensing)();
  408. } else if (usb_midi_handleRealTimeSystem) {
  409. (*usb_midi_handleRealTimeSystem)(0xFE);
  410. }
  411. break;
  412. case 0xFF: // usbMIDI.SystemReset
  413. if (usb_midi_handleSystemReset) {
  414. (*usb_midi_handleSystemReset)();
  415. } else if (usb_midi_handleRealTimeSystem) {
  416. (*usb_midi_handleRealTimeSystem)(0xFF);
  417. }
  418. break;
  419. default:
  420. return 0; // unknown message, ignore it
  421. }
  422. usb_midi_msg_type = b1;
  423. goto return_message;
  424. }
  425. if (type1 == 0x04) {
  426. sysex_byte(n >> 8);
  427. sysex_byte(n >> 16);
  428. sysex_byte(n >> 24);
  429. return 0;
  430. }
  431. if (type1 >= 0x05 && type1 <= 0x07) {
  432. sysex_byte(b1);
  433. if (type1 >= 0x06) sysex_byte(n >> 16);
  434. if (type1 == 0x07) sysex_byte(n >> 24);
  435. uint16_t len = usb_midi_msg_sysex_len;
  436. usb_midi_msg_data1 = len;
  437. usb_midi_msg_data2 = len >> 8;
  438. usb_midi_msg_sysex_len = 0;
  439. usb_midi_msg_type = 0xF0; // 0xF0 = usbMIDI.SystemExclusive
  440. if (usb_midi_handleSysExPartial) {
  441. (*usb_midi_handleSysExPartial)(usb_midi_msg_sysex, len, 1);
  442. } else if (usb_midi_handleSysExComplete) {
  443. (*usb_midi_handleSysExComplete)(usb_midi_msg_sysex, len);
  444. }
  445. return 1;
  446. }
  447. if (type1 == 0x0F) {
  448. if (b1 >= 0xF8) {
  449. // From Sebastian Tomczak, seb.tomczak at gmail.com
  450. // http://little-scale.blogspot.com/2011/08/usb-midi-game-boy-sync-for-16.html
  451. goto system_common_or_realtime;
  452. }
  453. if (b1 == 0xF0 || usb_midi_msg_sysex_len > 0) {
  454. // From David Sorlien, dsorlien at gmail.com, http://axe4live.wordpress.com
  455. // OSX sometimes uses Single Byte Unparsed to
  456. // send bytes in the middle of a SYSEX message.
  457. sysex_byte(b1);
  458. }
  459. }
  460. return 0;
  461. }
  462. #endif // MIDI_INTERFACE