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.

454 line
14KB

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