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

426 lines
13KB

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