Teensy 4.1 core updated for C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

299 lines
9.8KB

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