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.

265 lines
8.4KB

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