Teensy 4.1 core updated for C++20
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.

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