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.

273 lines
8.7KB

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