|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- /* Teensyduino Core Library
- * http://www.pjrc.com/teensy/
- * Copyright (c) 2017 PJRC.COM, LLC.
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * 1. The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * 2. If the Software is incorporated into a build system that allows
- * selection among a list of target devices, then similar target
- * devices manufactured by PJRC.COM must be included in the list of
- * target devices and selectable in the same manner.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
- * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- #include "usb_dev.h"
- #include "usb_midi.h"
- #include "core_pins.h" // for yield()
- #include <string.h> // for memcpy()
- #include "avr/pgmspace.h" // for PROGMEM, DMAMEM, FASTRUN
- #include "debug/printf.h"
- #include "core_pins.h"
-
- #ifdef MIDI_INTERFACE // defined by usb_dev.h -> usb_desc.h
-
- uint8_t usb_midi_msg_cable;
- uint8_t usb_midi_msg_channel;
- uint8_t usb_midi_msg_type;
- uint8_t usb_midi_msg_data1;
- uint8_t usb_midi_msg_data2;
- // TODO: separate sysex buffers for each cable...
- uint8_t usb_midi_msg_sysex[USB_MIDI_SYSEX_MAX];
- uint16_t usb_midi_msg_sysex_len;
- void (*usb_midi_handleNoteOff)(uint8_t ch, uint8_t note, uint8_t vel) = NULL;
- void (*usb_midi_handleNoteOn)(uint8_t ch, uint8_t note, uint8_t vel) = NULL;
- void (*usb_midi_handleVelocityChange)(uint8_t ch, uint8_t note, uint8_t vel) = NULL;
- void (*usb_midi_handleControlChange)(uint8_t ch, uint8_t control, uint8_t value) = NULL;
- void (*usb_midi_handleProgramChange)(uint8_t ch, uint8_t program) = NULL;
- void (*usb_midi_handleAfterTouch)(uint8_t ch, uint8_t pressure) = NULL;
- void (*usb_midi_handlePitchChange)(uint8_t ch, int pitch) = NULL;
- void (*usb_midi_handleSysExPartial)(const uint8_t *data, uint16_t length, uint8_t complete) = NULL;
- void (*usb_midi_handleSysExComplete)(uint8_t *data, unsigned int size) = NULL;
- void (*usb_midi_handleTimeCodeQuarterFrame)(uint8_t data) = NULL;
- void (*usb_midi_handleSongPosition)(uint16_t beats) = NULL;
- void (*usb_midi_handleSongSelect)(uint8_t songnumber) = NULL;
- void (*usb_midi_handleTuneRequest)(void) = NULL;
- void (*usb_midi_handleClock)(void) = NULL;
- void (*usb_midi_handleStart)(void) = NULL;
- void (*usb_midi_handleContinue)(void) = NULL;
- void (*usb_midi_handleStop)(void) = NULL;
- void (*usb_midi_handleActiveSensing)(void) = NULL;
- void (*usb_midi_handleSystemReset)(void) = NULL;
- void (*usb_midi_handleRealTimeSystem)(uint8_t rtb) = NULL;
-
-
- //static usb_packet_t *rx_packet=NULL;
- //static usb_packet_t *tx_packet=NULL;
- static uint8_t transmit_previous_timeout=0;
- static uint8_t tx_noautoflush=0;
- extern volatile uint8_t usb_high_speed;
-
-
- #define TX_NUM 4
- #define TX_SIZE 512 /* should be a multiple of MIDI_TX_SIZE_480 */
- static transfer_t tx_transfer[TX_NUM] __attribute__ ((used, aligned(32)));
- DMAMEM static uint8_t txbuffer[TX_SIZE * TX_NUM] __attribute__ ((aligned(32)));
- static uint8_t tx_head=0;
- static uint16_t tx_available=0;
- static uint16_t tx_packet_size=0;
-
- #define RX_NUM 6
- static transfer_t rx_transfer[RX_NUM] __attribute__ ((used, aligned(32)));
- DMAMEM static uint8_t rx_buffer[RX_NUM * MIDI_RX_SIZE_480] __attribute__ ((aligned(32)));
- static uint16_t rx_count[RX_NUM];
- static uint16_t rx_index[RX_NUM];
- static uint16_t rx_packet_size=0;
- static volatile uint8_t rx_head;
- static volatile uint8_t rx_tail;
- static uint8_t rx_list[RX_NUM + 1];
- static volatile uint32_t rx_available;
- static void rx_queue_transfer(int i);
- static void rx_event(transfer_t *t);
-
-
- void usb_midi_configure(void)
- {
- printf("usb_midi_configure\n");
- if (usb_high_speed) {
- tx_packet_size = MIDI_TX_SIZE_480;
- rx_packet_size = MIDI_RX_SIZE_480;
- } else {
- tx_packet_size = MIDI_TX_SIZE_12;
- rx_packet_size = MIDI_RX_SIZE_12;
- }
- memset(tx_transfer, 0, sizeof(tx_transfer));
- tx_head = 0;
- tx_available = 0;
- memset(rx_transfer, 0, sizeof(rx_transfer));
- memset(rx_count, 0, sizeof(rx_count));
- memset(rx_index, 0, sizeof(rx_index));
- rx_head = 0;
- rx_tail = 0;
- rx_available = 0;
- usb_config_rx(MIDI_RX_ENDPOINT, rx_packet_size, 0, rx_event);
- usb_config_tx(MIDI_TX_ENDPOINT, tx_packet_size, 0, NULL); // TODO: is ZLP needed?
- //int i;
- //for (i=0; i < RX_NUM; i++) rx_queue_transfer(i);
- // TODO: set up SOF interrupt....
- transmit_previous_timeout = 0;
- tx_noautoflush = 0;
- }
-
-
-
- // When the PC isn't listening, how long do we wait before discarding data?
- #define TX_TIMEOUT_MSEC 40
-
-
- void usb_midi_write_packed(uint32_t n)
- {
- printf("usb_midi_write_packed\n");
- if (!usb_configuration) return;
- tx_noautoflush = 1;
- uint32_t head = tx_head;
- transfer_t *xfer = tx_transfer + head;
- uint32_t wait_begin_at = systick_millis_count;
- while (!tx_available) {
- uint32_t status = usb_transfer_status(xfer);
- if (!(status & 0x80)) {
- if (status & 0x68) {
- // TODO: what if status has errors???
- }
- tx_available = tx_packet_size;
- transmit_previous_timeout = 0;
- break;
- }
- if (systick_millis_count - wait_begin_at > TX_TIMEOUT_MSEC) {
- transmit_previous_timeout = 1;
- }
- if (transmit_previous_timeout) return;
- if (!usb_configuration) return;
- yield();
- }
- uint32_t *txdata = (uint32_t *)(txbuffer + (tx_head * TX_SIZE) + (TX_SIZE - tx_available));
- *txdata = n;
- tx_available -= 4;
- if (tx_available == 0) {
- uint8_t *txbuf = txbuffer + (tx_head * TX_SIZE);
- usb_prepare_transfer(xfer, txbuf, tx_packet_size, 0);
- arm_dcache_flush_delete(txbuf, TX_SIZE);
- usb_transmit(MIDI_TX_ENDPOINT, xfer);
- if (++head >= TX_NUM) head = 0;
- tx_head = head;
- usb_stop_sof_interrupts(MIDI_INTERFACE);
- } else {
- usb_start_sof_interrupts(MIDI_INTERFACE);
- }
- tx_noautoflush = 0;
- }
-
- void usb_midi_flush_output(void)
- {
- //printf("usb_midi_flush_output\n");
- if (tx_noautoflush == 0 && tx_available > 0) {
- printf(" tx, %d %d\n", tx_packet_size, tx_available);
- uint32_t head = tx_head;
- transfer_t *xfer = tx_transfer + head;
- uint8_t *txbuf = txbuffer + (head * TX_SIZE);
- uint32_t len = tx_packet_size - tx_available;
- usb_prepare_transfer(xfer, txbuf, len, 0);
- arm_dcache_flush_delete(txbuf, TX_SIZE);
- usb_transmit(MIDI_TX_ENDPOINT, xfer);
- if (++head >= TX_NUM) head = 0;
- tx_head = head;
- tx_available = 0;
- usb_stop_sof_interrupts(MIDI_INTERFACE);
- }
- }
-
- void usb_midi_send_sysex_buffer_has_term(const uint8_t *data, uint32_t length, uint8_t cable)
- {
- cable = (cable & 0x0F) << 4;
- while (length > 3) {
- usb_midi_write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
- data += 3;
- length -= 3;
- }
- if (length == 3) {
- usb_midi_write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
- } else if (length == 2) {
- usb_midi_write_packed(0x06 | cable | (data[0] << 8) | (data[1] << 16));
- } else if (length == 1) {
- usb_midi_write_packed(0x05 | cable | (data[0] << 8));
- }
- }
-
- void usb_midi_send_sysex_add_term_bytes(const uint8_t *data, uint32_t length, uint8_t cable)
- {
- cable = (cable & 0x0F) << 4;
-
- if (length == 0) {
- usb_midi_write_packed(0x06 | cable | (0xF0 << 8) | (0xF7 << 16));
- return;
- } else if (length == 1) {
- usb_midi_write_packed(0x07 | cable | (0xF0 << 8) | (data[0] << 16) | (0xF7 << 24));
- return;
- } else {
- usb_midi_write_packed(0x04 | cable | (0xF0 << 8) | (data[0] << 16) | (data[1] << 24));
- data += 2;
- length -= 2;
- }
- while (length >= 3) {
- usb_midi_write_packed(0x04 | cable | (data[0] << 8) | (data[1] << 16) | (data[2] << 24));
- data += 3;
- length -= 3;
- }
- if (length == 2) {
- usb_midi_write_packed(0x07 | cable | (data[0] << 8) | (data[1] << 16) | (0xF7 << 24));
- } else if (length == 1) {
- usb_midi_write_packed(0x06 | cable | (data[0] << 8) | (0xF7 << 16));
- } else {
- usb_midi_write_packed(0x05 | cable | (0xF7 << 8));
- }
- }
-
- void static sysex_byte(uint8_t b)
- {
- if (usb_midi_handleSysExPartial && usb_midi_msg_sysex_len >= USB_MIDI_SYSEX_MAX) {
- // when buffer is full, send another chunk to partial handler.
- (*usb_midi_handleSysExPartial)(usb_midi_msg_sysex, usb_midi_msg_sysex_len, 0);
- usb_midi_msg_sysex_len = 0;
- }
- if (usb_midi_msg_sysex_len < USB_MIDI_SYSEX_MAX) {
- usb_midi_msg_sysex[usb_midi_msg_sysex_len++] = b;
- }
- }
-
-
- // called by USB interrupt when any packet is received
- static void rx_event(transfer_t *t)
- {
- }
-
-
- uint32_t usb_midi_available(void)
- {
- #if 0
- uint32_t index;
-
- if (!rx_packet) {
- if (!usb_configuration) return 0;
- rx_packet = usb_rx(MIDI_RX_ENDPOINT);
- if (!rx_packet) return 0;
- if (rx_packet->len == 0) {
- usb_free(rx_packet);
- rx_packet = NULL;
- return 0;
- }
- }
- index = rx_packet->index;
- return rx_packet->len - index;
- #endif
- return 0;
- }
-
- uint32_t usb_midi_read_message(void)
- {
- #if 0
- uint32_t n, index;
-
- if (!rx_packet) {
- if (!usb_configuration) return 0;
- rx_packet = usb_rx(MIDI_RX_ENDPOINT);
- if (!rx_packet) return 0;
- if (rx_packet->len == 0) {
- usb_free(rx_packet);
- rx_packet = NULL;
- return 0;
- }
- }
- index = rx_packet->index;
- n = ((uint32_t *)rx_packet->buf)[index/4];
- index += 4;
- if (index < rx_packet->len) {
- rx_packet->index = index;
- } else {
- usb_free(rx_packet);
- rx_packet = usb_rx(MIDI_RX_ENDPOINT);
- }
- return n;
- #endif
- return 0;
- }
-
- int usb_midi_read(uint32_t channel)
- {
- //uint32_t n, index, ch, type1, type2, b1;
- uint32_t n, ch, type1, type2, b1;
-
- return 0;
- #if 0
- if (!rx_packet) {
- if (!usb_configuration) return 0;
- rx_packet = usb_rx(MIDI_RX_ENDPOINT);
- if (!rx_packet) return 0;
- if (rx_packet->len == 0) {
- usb_free(rx_packet);
- rx_packet = NULL;
- return 0;
- }
- }
- index = rx_packet->index;
- n = ((uint32_t *)rx_packet->buf)[index/4];
- //serial_print("midi rx, n=");
- //serial_phex32(n);
- //serial_print("\n");
- index += 4;
- if (index < rx_packet->len) {
- rx_packet->index = index;
- } else {
- usb_free(rx_packet);
- rx_packet = usb_rx(MIDI_RX_ENDPOINT);
- }
- #endif
- type1 = n & 15;
- type2 = (n >> 12) & 15;
- b1 = (n >> 8) & 0xFF;
- ch = (b1 & 15) + 1;
- usb_midi_msg_cable = (n >> 4) & 15;
- if (type1 >= 0x08 && type1 <= 0x0E) {
- if (channel && channel != ch) {
- // ignore other channels when user wants single channel read
- return 0;
- }
- if (type1 == 0x08 && type2 == 0x08) {
- usb_midi_msg_type = 0x80; // 0x80 = usbMIDI.NoteOff
- if (usb_midi_handleNoteOff)
- (*usb_midi_handleNoteOff)(ch, (n >> 16), (n >> 24));
- } else
- if (type1 == 0x09 && type2 == 0x09) {
- if ((n >> 24) > 0) {
- usb_midi_msg_type = 0x90; // 0x90 = usbMIDI.NoteOn
- if (usb_midi_handleNoteOn)
- (*usb_midi_handleNoteOn)(ch, (n >> 16), (n >> 24));
- } else {
- usb_midi_msg_type = 0x80; // 0x80 = usbMIDI.NoteOff
- if (usb_midi_handleNoteOff)
- (*usb_midi_handleNoteOff)(ch, (n >> 16), (n >> 24));
- }
- } else
- if (type1 == 0x0A && type2 == 0x0A) {
- usb_midi_msg_type = 0xA0; // 0xA0 = usbMIDI.AfterTouchPoly
- if (usb_midi_handleVelocityChange)
- (*usb_midi_handleVelocityChange)(ch, (n >> 16), (n >> 24));
- } else
- if (type1 == 0x0B && type2 == 0x0B) {
- usb_midi_msg_type = 0xB0; // 0xB0 = usbMIDI.ControlChange
- if (usb_midi_handleControlChange)
- (*usb_midi_handleControlChange)(ch, (n >> 16), (n >> 24));
- } else
- if (type1 == 0x0C && type2 == 0x0C) {
- usb_midi_msg_type = 0xC0; // 0xC0 = usbMIDI.ProgramChange
- if (usb_midi_handleProgramChange)
- (*usb_midi_handleProgramChange)(ch, (n >> 16));
- } else
- if (type1 == 0x0D && type2 == 0x0D) {
- usb_midi_msg_type = 0xD0; // 0xD0 = usbMIDI.AfterTouchChannel
- if (usb_midi_handleAfterTouch)
- (*usb_midi_handleAfterTouch)(ch, (n >> 16));
- } else
- if (type1 == 0x0E && type2 == 0x0E) {
- usb_midi_msg_type = 0xE0; // 0xE0 = usbMIDI.PitchBend
- if (usb_midi_handlePitchChange) {
- int value = ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80);
- value -= 8192; // 0 to 16383 --> -8192 to +8191
- (*usb_midi_handlePitchChange)(ch, value);
- }
- } else {
- return 0;
- }
- return_message:
- usb_midi_msg_channel = ch;
- usb_midi_msg_data1 = (n >> 16);
- usb_midi_msg_data2 = (n >> 24);
- return 1;
- }
- if (type1 == 0x02 || type1 == 0x03 || (type1 == 0x05 && b1 >= 0xF1 && b1 != 0xF7)) {
- // system common or system realtime message
- system_common_or_realtime:
- switch (b1) {
- case 0xF1: // usbMIDI.TimeCodeQuarterFrame
- if (usb_midi_handleTimeCodeQuarterFrame) {
- (*usb_midi_handleTimeCodeQuarterFrame)(n >> 16);
- }
- break;
- case 0xF2: // usbMIDI.SongPosition
- if (usb_midi_handleSongPosition) {
- (*usb_midi_handleSongPosition)(
- ((n >> 16) & 0x7F) | ((n >> 17) & 0x3F80));
- }
- break;
- case 0xF3: // usbMIDI.SongSelect
- if (usb_midi_handleSongSelect) {
- (*usb_midi_handleSongSelect)(n >> 16);
- }
- break;
- case 0xF6: // usbMIDI.TuneRequest
- if (usb_midi_handleTuneRequest) {
- (*usb_midi_handleTuneRequest)();
- }
- break;
- case 0xF8: // usbMIDI.Clock
- if (usb_midi_handleClock) {
- (*usb_midi_handleClock)();
- } else if (usb_midi_handleRealTimeSystem) {
- (*usb_midi_handleRealTimeSystem)(0xF8);
- }
- break;
- case 0xFA: // usbMIDI.Start
- if (usb_midi_handleStart) {
- (*usb_midi_handleStart)();
- } else if (usb_midi_handleRealTimeSystem) {
- (*usb_midi_handleRealTimeSystem)(0xFA);
- }
- break;
- case 0xFB: // usbMIDI.Continue
- if (usb_midi_handleContinue) {
- (*usb_midi_handleContinue)();
- } else if (usb_midi_handleRealTimeSystem) {
- (*usb_midi_handleRealTimeSystem)(0xFB);
- }
- break;
- case 0xFC: // usbMIDI.Stop
- if (usb_midi_handleStop) {
- (*usb_midi_handleStop)();
- } else if (usb_midi_handleRealTimeSystem) {
- (*usb_midi_handleRealTimeSystem)(0xFC);
- }
- break;
- case 0xFE: // usbMIDI.ActiveSensing
- if (usb_midi_handleActiveSensing) {
- (*usb_midi_handleActiveSensing)();
- } else if (usb_midi_handleRealTimeSystem) {
- (*usb_midi_handleRealTimeSystem)(0xFE);
- }
- break;
- case 0xFF: // usbMIDI.SystemReset
- if (usb_midi_handleSystemReset) {
- (*usb_midi_handleSystemReset)();
- } else if (usb_midi_handleRealTimeSystem) {
- (*usb_midi_handleRealTimeSystem)(0xFF);
- }
- break;
- default:
- return 0; // unknown message, ignore it
- }
- usb_midi_msg_type = b1;
- goto return_message;
- }
- if (type1 == 0x04) {
- sysex_byte(n >> 8);
- sysex_byte(n >> 16);
- sysex_byte(n >> 24);
- return 0;
- }
- if (type1 >= 0x05 && type1 <= 0x07) {
- sysex_byte(b1);
- if (type1 >= 0x06) sysex_byte(n >> 16);
- if (type1 == 0x07) sysex_byte(n >> 24);
- uint16_t len = usb_midi_msg_sysex_len;
- usb_midi_msg_data1 = len;
- usb_midi_msg_data2 = len >> 8;
- usb_midi_msg_sysex_len = 0;
- usb_midi_msg_type = 0xF0; // 0xF0 = usbMIDI.SystemExclusive
- if (usb_midi_handleSysExPartial) {
- (*usb_midi_handleSysExPartial)(usb_midi_msg_sysex, len, 1);
- } else if (usb_midi_handleSysExComplete) {
- (*usb_midi_handleSysExComplete)(usb_midi_msg_sysex, len);
- }
- return 1;
- }
- if (type1 == 0x0F) {
- if (b1 >= 0xF8) {
- // From Sebastian Tomczak, seb.tomczak at gmail.com
- // http://little-scale.blogspot.com/2011/08/usb-midi-game-boy-sync-for-16.html
- goto system_common_or_realtime;
- }
- if (b1 == 0xF0 || usb_midi_msg_sysex_len > 0) {
- // From David Sorlien, dsorlien at gmail.com, http://axe4live.wordpress.com
- // OSX sometimes uses Single Byte Unparsed to
- // send bytes in the middle of a SYSEX message.
- sysex_byte(b1);
- }
- }
- return 0;
- }
-
-
- #endif // MIDI_INTERFACE
|