Teensy 4.1 core updated for C++20
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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