Teensy 4.1 core updated for C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

351 lines
11KB

  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_handleSysEx)(const uint8_t *data, uint16_t length, uint8_t complete) = NULL;
  52. void (*usb_midi_handleRealTimeSystem)(uint8_t rtb) = NULL;
  53. void (*usb_midi_handleTimeCodeQuarterFrame)(uint16_t data) = NULL;
  54. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  55. #define TX_PACKET_LIMIT 6
  56. static usb_packet_t *rx_packet=NULL;
  57. static usb_packet_t *tx_packet=NULL;
  58. static uint8_t transmit_previous_timeout=0;
  59. static uint8_t tx_noautoflush=0;
  60. // When the PC isn't listening, how long do we wait before discarding data?
  61. #define TX_TIMEOUT_MSEC 40
  62. #if F_CPU == 240000000
  63. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600)
  64. #elif F_CPU == 216000000
  65. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440)
  66. #elif F_CPU == 192000000
  67. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1280)
  68. #elif F_CPU == 180000000
  69. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1200)
  70. #elif F_CPU == 168000000
  71. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  72. #elif F_CPU == 144000000
  73. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  74. #elif F_CPU == 120000000
  75. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  76. #elif F_CPU == 96000000
  77. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  78. #elif F_CPU == 72000000
  79. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  80. #elif F_CPU == 48000000
  81. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  82. #elif F_CPU == 24000000
  83. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  84. #endif
  85. void usb_midi_write_packed(uint32_t n)
  86. {
  87. uint32_t index, wait_count=0;
  88. tx_noautoflush = 1;
  89. if (!tx_packet) {
  90. while (1) {
  91. if (!usb_configuration) {
  92. //serial_print("error1\n");
  93. return;
  94. }
  95. if (usb_tx_packet_count(MIDI_TX_ENDPOINT) < TX_PACKET_LIMIT) {
  96. tx_packet = usb_malloc();
  97. if (tx_packet) break;
  98. }
  99. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  100. transmit_previous_timeout = 1;
  101. //serial_print("error2\n");
  102. return;
  103. }
  104. yield();
  105. }
  106. }
  107. transmit_previous_timeout = 0;
  108. index = tx_packet->index;
  109. //*((uint32_t *)(tx_packet->buf) + index++) = n;
  110. ((uint32_t *)(tx_packet->buf))[index++] = n;
  111. if (index < MIDI_TX_SIZE/4) {
  112. tx_packet->index = index;
  113. } else {
  114. tx_packet->len = MIDI_TX_SIZE;
  115. usb_tx(MIDI_TX_ENDPOINT, tx_packet);
  116. tx_packet = usb_malloc();
  117. }
  118. tx_noautoflush = 0;
  119. }
  120. void usb_midi_send_sysex(const uint8_t *data, uint32_t length, uint8_t cable)
  121. {
  122. // TODO: MIDI 2.5 lib automatically adds start and stop bytes
  123. cable = (cable & 0x0F) << 4;
  124. while (length > 3) {
  125. usb_midi_write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  126. data += 3;
  127. length -= 3;
  128. }
  129. if (length == 3) {
  130. usb_midi_write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
  131. } else if (length == 2) {
  132. usb_midi_write_packed(0x06 | cable | (data[0] << 8) | (data[1] << 16));
  133. } else if (length == 1) {
  134. usb_midi_write_packed(0x05 | cable | (data[0] << 8));
  135. }
  136. }
  137. void usb_midi_flush_output(void)
  138. {
  139. if (tx_noautoflush == 0 && tx_packet && tx_packet->index > 0) {
  140. tx_packet->len = tx_packet->index * 4;
  141. usb_tx(MIDI_TX_ENDPOINT, tx_packet);
  142. tx_packet = usb_malloc();
  143. }
  144. }
  145. void static sysex_byte(uint8_t b)
  146. {
  147. // when buffer is full, send another chunk to handler.
  148. if (usb_midi_msg_sysex_len == USB_MIDI_SYSEX_MAX) {
  149. if (usb_midi_handleSysEx) {
  150. (*usb_midi_handleSysEx)(usb_midi_msg_sysex, usb_midi_msg_sysex_len, 0);
  151. usb_midi_msg_sysex_len = 0;
  152. }
  153. }
  154. if (usb_midi_msg_sysex_len < USB_MIDI_SYSEX_MAX) {
  155. usb_midi_msg_sysex[usb_midi_msg_sysex_len++] = b;
  156. }
  157. }
  158. uint32_t usb_midi_available(void)
  159. {
  160. uint32_t index;
  161. if (!rx_packet) {
  162. if (!usb_configuration) return 0;
  163. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  164. if (!rx_packet) return 0;
  165. if (rx_packet->len == 0) {
  166. usb_free(rx_packet);
  167. rx_packet = NULL;
  168. return 0;
  169. }
  170. }
  171. index = rx_packet->index;
  172. return rx_packet->len - index;
  173. }
  174. uint32_t usb_midi_read_message(void)
  175. {
  176. uint32_t n, index;
  177. if (!rx_packet) {
  178. if (!usb_configuration) return 0;
  179. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  180. if (!rx_packet) return 0;
  181. if (rx_packet->len == 0) {
  182. usb_free(rx_packet);
  183. rx_packet = NULL;
  184. return 0;
  185. }
  186. }
  187. index = rx_packet->index;
  188. n = ((uint32_t *)rx_packet->buf)[index/4];
  189. index += 4;
  190. if (index < rx_packet->len) {
  191. rx_packet->index = index;
  192. } else {
  193. usb_free(rx_packet);
  194. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  195. }
  196. return n;
  197. }
  198. int usb_midi_read(uint32_t channel)
  199. {
  200. uint32_t n, index, ch, type1, type2;
  201. if (!rx_packet) {
  202. if (!usb_configuration) return 0;
  203. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  204. if (!rx_packet) return 0;
  205. if (rx_packet->len == 0) {
  206. usb_free(rx_packet);
  207. rx_packet = NULL;
  208. return 0;
  209. }
  210. }
  211. index = rx_packet->index;
  212. //n = *(uint32_t *)(rx_packet->buf + index);
  213. n = ((uint32_t *)rx_packet->buf)[index/4];
  214. //serial_print("midi rx, n=");
  215. //serial_phex32(n);
  216. //serial_print("\n");
  217. index += 4;
  218. if (index < rx_packet->len) {
  219. rx_packet->index = index;
  220. } else {
  221. usb_free(rx_packet);
  222. rx_packet = usb_rx(MIDI_RX_ENDPOINT);
  223. }
  224. type1 = n & 15;
  225. type2 = (n >> 12) & 15;
  226. ch = ((n >> 8) & 15) + 1;
  227. usb_midi_msg_cable = (n >> 4) & 15;
  228. if (type1 >= 0x08 && type1 <= 0x0E) {
  229. if (channel && channel != ch) {
  230. // ignore other channels when user wants single channel read
  231. return 0;
  232. }
  233. if (type1 == 0x08 && type2 == 0x08) {
  234. usb_midi_msg_type = 0; // 0 = Note off
  235. if (usb_midi_handleNoteOff)
  236. (*usb_midi_handleNoteOff)(ch, (n >> 16), (n >> 24));
  237. } else
  238. if (type1 == 0x09 && type2 == 0x09) {
  239. if ((n >> 24) > 0) {
  240. usb_midi_msg_type = 1; // 1 = Note on
  241. if (usb_midi_handleNoteOn)
  242. (*usb_midi_handleNoteOn)(ch, (n >> 16), (n >> 24));
  243. } else {
  244. usb_midi_msg_type = 0; // 0 = Note off
  245. if (usb_midi_handleNoteOff)
  246. (*usb_midi_handleNoteOff)(ch, (n >> 16), (n >> 24));
  247. }
  248. } else
  249. if (type1 == 0x0A && type2 == 0x0A) {
  250. usb_midi_msg_type = 2; // 2 = Poly Pressure
  251. if (usb_midi_handleVelocityChange)
  252. (*usb_midi_handleVelocityChange)(ch, (n >> 16), (n >> 24));
  253. } else
  254. if (type1 == 0x0B && type2 == 0x0B) {
  255. usb_midi_msg_type = 3; // 3 = Control Change
  256. if (usb_midi_handleControlChange)
  257. (*usb_midi_handleControlChange)(ch, (n >> 16), (n >> 24));
  258. } else
  259. if (type1 == 0x0C && type2 == 0x0C) {
  260. usb_midi_msg_type = 4; // 4 = Program Change
  261. if (usb_midi_handleProgramChange)
  262. (*usb_midi_handleProgramChange)(ch, (n >> 16));
  263. } else
  264. if (type1 == 0x0D && type2 == 0x0D) {
  265. usb_midi_msg_type = 5; // 5 = After Touch
  266. if (usb_midi_handleAfterTouch)
  267. (*usb_midi_handleAfterTouch)(ch, (n >> 16));
  268. } else
  269. if (type1 == 0x0E && type2 == 0x0E) {
  270. usb_midi_msg_type = 6; // 6 = Pitch Bend
  271. if (usb_midi_handlePitchChange)
  272. (*usb_midi_handlePitchChange)(ch,
  273. ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
  274. } else {
  275. return 0;
  276. }
  277. return_message:
  278. usb_midi_msg_channel = ch;
  279. usb_midi_msg_data1 = (n >> 16);
  280. usb_midi_msg_data2 = (n >> 24);
  281. return 1;
  282. }
  283. if (type1 == 0x04) {
  284. sysex_byte(n >> 8);
  285. sysex_byte(n >> 16);
  286. sysex_byte(n >> 24);
  287. return 0;
  288. }
  289. if (type1 >= 0x05 && type1 <= 0x07) {
  290. sysex_byte(n >> 8);
  291. if (type1 >= 0x06) sysex_byte(n >> 16);
  292. if (type1 == 0x07) sysex_byte(n >> 24);
  293. usb_midi_msg_data1 = usb_midi_msg_sysex_len;
  294. usb_midi_msg_data2 = usb_midi_msg_sysex_len >> 8;
  295. usb_midi_msg_sysex_len = 0;
  296. usb_midi_msg_type = 7; // 7 = Sys Ex
  297. if (usb_midi_handleSysEx)
  298. (*usb_midi_handleSysEx)(usb_midi_msg_sysex, usb_midi_msg_data1, 1);
  299. return 1;
  300. }
  301. if (type1 == 0x0F) {
  302. // TODO: does this need to be a full MIDI parser?
  303. // What software actually uses this message type in practice?
  304. if (usb_midi_msg_sysex_len > 0) {
  305. // From David Sorlien, dsorlien at gmail.com, http://axe4live.wordpress.com
  306. // OSX sometimes uses Single Byte Unparsed to
  307. // send bytes in the middle of a SYSEX message.
  308. sysex_byte(n >> 8);
  309. } else {
  310. // From Sebastian Tomczak, seb.tomczak at gmail.com
  311. // http://little-scale.blogspot.com/2011/08/usb-midi-game-boy-sync-for-16.html
  312. usb_midi_msg_type = 8;
  313. if (usb_midi_handleRealTimeSystem)
  314. (*usb_midi_handleRealTimeSystem)(n >> 8);
  315. goto return_message;
  316. }
  317. }
  318. if (type1 == 0x02) {
  319. // From Timm Schlegelmilch, karg.music at gmail.com
  320. // http://karg-music.blogspot.de/2015/06/receiving-midi-time-codes-over-usb-with.html
  321. usb_midi_msg_type = 9;
  322. if (usb_midi_handleTimeCodeQuarterFrame)
  323. (*usb_midi_handleTimeCodeQuarterFrame)(n >> 16);
  324. return 1;
  325. }
  326. return 0;
  327. }
  328. #endif // F_CPU
  329. #endif // MIDI_INTERFACE