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.

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