Teensy 4.1 core updated for C++20
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

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