Files
tinyUSB/src/class/midi/midi_device.c

552 lines
16 KiB
C
Raw Normal View History

2020-01-14 23:30:39 -05:00
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach (tinyusb.org)
*
* 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:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* 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.
*
* This file is part of the TinyUSB stack.
*/
#include "tusb_option.h"
#if (CFG_TUD_ENABLED && CFG_TUD_MIDI)
2020-01-14 23:30:39 -05:00
//--------------------------------------------------------------------+
// INCLUDE
//--------------------------------------------------------------------+
2021-05-27 17:40:39 +07:00
#include "device/usbd.h"
2020-01-14 23:30:39 -05:00
#include "device/usbd_pvt.h"
2021-05-27 17:40:39 +07:00
#include "midi_device.h"
2020-01-14 23:30:39 -05:00
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
2021-04-02 13:55:51 +07:00
typedef struct
{
uint8_t buffer[4];
uint8_t index;
uint8_t total;
}midid_stream_t;
2020-01-14 23:30:39 -05:00
typedef struct
{
uint8_t itf_num;
uint8_t ep_in;
uint8_t ep_out;
// For Stream read()/write() API
// Messages are always 4 bytes long, queue them for reading and writing so the
// callers can use the Stream interface with single-byte read/write calls.
midid_stream_t stream_write;
midid_stream_t stream_read;
2020-01-14 23:30:39 -05:00
/*------------- From this point, data is not cleared by bus reset -------------*/
// FIFO
tu_fifo_t rx_ff;
tu_fifo_t tx_ff;
uint8_t rx_ff_buf[CFG_TUD_MIDI_RX_BUFSIZE];
uint8_t tx_ff_buf[CFG_TUD_MIDI_TX_BUFSIZE];
#if CFG_FIFO_MUTEX
osal_mutex_def_t rx_ff_mutex;
osal_mutex_def_t tx_ff_mutex;
#endif
// Endpoint Transfer buffer
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_MIDI_EP_BUFSIZE];
CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_MIDI_EP_BUFSIZE];
2020-01-14 23:30:39 -05:00
} midid_interface_t;
#define ITF_MEM_RESET_SIZE offsetof(midid_interface_t, rx_ff)
//--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION
//--------------------------------------------------------------------+
CFG_TUSB_MEM_SECTION midid_interface_t _midid_itf[CFG_TUD_MIDI];
bool tud_midi_n_mounted (uint8_t itf)
{
midid_interface_t* midi = &_midid_itf[itf];
return midi->ep_in && midi->ep_out;
}
2021-01-30 04:11:08 +03:00
static void _prep_out_transaction (midid_interface_t* p_midi)
{
uint8_t const rhport = 0;
2021-01-30 04:11:08 +03:00
uint16_t available = tu_fifo_remaining(&p_midi->rx_ff);
// Prepare for incoming data but only allow what we can store in the ring buffer.
// TODO Actually we can still carry out the transfer, keeping count of received bytes
// and slowly move it to the FIFO when read().
// This pre-check reduces endpoint claiming
TU_VERIFY(available >= sizeof(p_midi->epout_buf), );
// claim endpoint
TU_VERIFY(usbd_edpt_claim(rhport, p_midi->ep_out), );
// fifo can be changed before endpoint is claimed
available = tu_fifo_remaining(&p_midi->rx_ff);
if ( available >= sizeof(p_midi->epout_buf) ) {
usbd_edpt_xfer(rhport, p_midi->ep_out, p_midi->epout_buf, sizeof(p_midi->epout_buf));
}else
{
// Release endpoint since we don't make any transfer
usbd_edpt_release(rhport, p_midi->ep_out);
}
}
2020-01-14 23:30:39 -05:00
//--------------------------------------------------------------------+
// READ API
//--------------------------------------------------------------------+
uint32_t tud_midi_n_available(uint8_t itf, uint8_t cable_num)
2020-01-14 23:30:39 -05:00
{
(void) cable_num;
2021-08-02 18:58:27 +07:00
midid_interface_t* midi = &_midid_itf[itf];
midid_stream_t const* stream = &midi->stream_read;
// when using with packet API stream total & index are both zero
return tu_fifo_count(&midi->rx_ff) + (uint8_t) (stream->total - stream->index);
2020-01-14 23:30:39 -05:00
}
uint32_t tud_midi_n_stream_read(uint8_t itf, uint8_t cable_num, void* buffer, uint32_t bufsize)
2020-01-14 23:30:39 -05:00
{
(void) cable_num;
TU_VERIFY(bufsize, 0);
uint8_t* buf8 = (uint8_t*) buffer;
midid_interface_t* midi = &_midid_itf[itf];
2021-04-02 14:52:44 +07:00
midid_stream_t* stream = &midi->stream_read;
uint32_t total_read = 0;
while( bufsize )
{
// Get new packet from fifo, then set packet expected bytes
if ( stream->total == 0 )
{
// return if there is no more data from fifo
if ( !tud_midi_n_packet_read(itf, stream->buffer) ) return total_read;
2021-04-02 13:55:51 +07:00
uint8_t const code_index = stream->buffer[0] & 0x0f;
2020-01-14 23:30:39 -05:00
// MIDI 1.0 Table 4-1: Code Index Number Classifications
switch(code_index)
{
case MIDI_CIN_MISC:
case MIDI_CIN_CABLE_EVENT:
// These are reserved and unused, possibly issue somewhere, skip this packet
return 0;
break;
case MIDI_CIN_SYSEX_END_1BYTE:
case MIDI_CIN_1BYTE_DATA:
stream->total = 1;
break;
case MIDI_CIN_SYSCOM_2BYTE :
case MIDI_CIN_SYSEX_END_2BYTE :
case MIDI_CIN_PROGRAM_CHANGE :
case MIDI_CIN_CHANNEL_PRESSURE :
stream->total = 2;
break;
2021-04-02 13:55:51 +07:00
default:
stream->total = 3;
break;
2020-01-14 23:30:39 -05:00
}
}
// Copy data up to bufsize
uint8_t const count = (uint8_t) tu_min32(stream->total - stream->index, bufsize);
// Skip the header (1st byte) in the buffer
memcpy(buf8, stream->buffer + 1 + stream->index, count);
total_read += count;
stream->index += count;
buf8 += count;
bufsize -= count;
// complete current event packet, reset stream
if ( stream->total == stream->index )
{
stream->index = 0;
stream->total = 0;
}
2020-01-14 23:30:39 -05:00
}
return total_read;
}
bool tud_midi_n_packet_read (uint8_t itf, uint8_t packet[4])
{
midid_interface_t* midi = &_midid_itf[itf];
TU_VERIFY(midi->ep_out);
uint32_t const num_read = tu_fifo_read_n(&midi->rx_ff, packet, 4);
_prep_out_transaction(midi);
2021-01-30 04:11:08 +03:00
return (num_read == 4);
2020-01-14 23:30:39 -05:00
}
//--------------------------------------------------------------------+
// WRITE API
//--------------------------------------------------------------------+
static uint32_t write_flush(midid_interface_t* midi)
2020-01-14 23:30:39 -05:00
{
2020-09-10 23:32:08 +07:00
// No data to send
if ( !tu_fifo_count(&midi->tx_ff) ) return 0;
uint8_t const rhport = 0;
2020-09-10 23:32:08 +07:00
2020-01-14 23:30:39 -05:00
// skip if previous transfer not complete
2020-09-10 23:32:08 +07:00
TU_VERIFY( usbd_edpt_claim(rhport, midi->ep_in), 0 );
2020-01-14 23:30:39 -05:00
uint16_t count = tu_fifo_read_n(&midi->tx_ff, midi->epin_buf, CFG_TUD_MIDI_EP_BUFSIZE);
if (count)
2020-01-14 23:30:39 -05:00
{
2020-09-10 23:32:08 +07:00
TU_ASSERT( usbd_edpt_xfer(rhport, midi->ep_in, midi->epin_buf, count), 0 );
return count;
}else
{
// Release endpoint since we don't make any transfer
usbd_edpt_release(rhport, midi->ep_in);
return 0;
2020-01-14 23:30:39 -05:00
}
}
uint32_t tud_midi_n_stream_write(uint8_t itf, uint8_t cable_num, uint8_t const* buffer, uint32_t bufsize)
2020-01-14 23:30:39 -05:00
{
midid_interface_t* midi = &_midid_itf[itf];
TU_VERIFY(midi->ep_in, 0);
2020-01-14 23:30:39 -05:00
2021-04-02 14:26:55 +07:00
midid_stream_t* stream = &midi->stream_write;
2021-04-02 13:55:51 +07:00
2020-01-14 23:30:39 -05:00
uint32_t i = 0;
while ( (i < bufsize) && (tu_fifo_remaining(&midi->tx_ff) >= 4) )
2021-03-31 00:34:09 +07:00
{
uint8_t const data = buffer[i];
i++;
2021-04-02 14:26:55 +07:00
if ( stream->index == 0 )
2021-03-31 00:34:09 +07:00
{
//------------- New event packet -------------//
uint8_t const msg = data >> 4;
2021-04-02 14:26:55 +07:00
stream->index = 2;
stream->buffer[1] = data;
2021-03-31 00:34:09 +07:00
// Check to see if we're still in a SysEx transmit.
2021-04-02 14:26:55 +07:00
if ( stream->buffer[0] == MIDI_CIN_SYSEX_START )
2021-03-31 00:34:09 +07:00
{
if ( data == MIDI_STATUS_SYSEX_END )
2021-03-31 00:34:09 +07:00
{
2021-04-02 14:26:55 +07:00
stream->buffer[0] = MIDI_CIN_SYSEX_END_1BYTE;
stream->total = 2;
2021-03-31 00:34:09 +07:00
}
else
{
2021-04-02 14:26:55 +07:00
stream->total = 4;
2021-03-31 00:34:09 +07:00
}
}
else if ( (msg >= 0x8 && msg <= 0xB) || msg == 0xE )
{
// Channel Voice Messages
stream->buffer[0] = (uint8_t) ((cable_num << 4) | msg);
2021-04-02 14:26:55 +07:00
stream->total = 4;
2021-03-31 00:34:09 +07:00
}
else if ( msg == 0xC || msg == 0xD)
{
// Channel Voice Messages, two-byte variants (Program Change and Channel Pressure)
stream->buffer[0] = (uint8_t) ((cable_num << 4) | msg);
stream->total = 3;
}
2021-03-31 00:34:09 +07:00
else if ( msg == 0xf )
{
// System message
if ( data == MIDI_STATUS_SYSEX_START )
2021-03-31 00:34:09 +07:00
{
2021-04-02 14:26:55 +07:00
stream->buffer[0] = MIDI_CIN_SYSEX_START;
stream->total = 4;
2020-01-14 23:30:39 -05:00
}
else if ( data == MIDI_STATUS_SYSCOM_TIME_CODE_QUARTER_FRAME || data == MIDI_STATUS_SYSCOM_SONG_SELECT )
2021-03-31 00:34:09 +07:00
{
2021-04-02 14:26:55 +07:00
stream->buffer[0] = MIDI_CIN_SYSCOM_2BYTE;
stream->total = 3;
2020-01-14 23:30:39 -05:00
}
else if ( data == MIDI_STATUS_SYSCOM_SONG_POSITION_POINTER )
2021-03-31 00:34:09 +07:00
{
2021-04-02 14:26:55 +07:00
stream->buffer[0] = MIDI_CIN_SYSCOM_3BYTE;
stream->total = 4;
2020-01-14 23:30:39 -05:00
}
2021-03-31 00:34:09 +07:00
else
{
2021-04-02 14:26:55 +07:00
stream->buffer[0] = MIDI_CIN_SYSEX_END_1BYTE;
stream->total = 2;
2020-01-14 23:30:39 -05:00
}
2021-03-31 00:34:09 +07:00
}
else
{
// Pack individual bytes if we don't support packing them into words.
stream->buffer[0] = (uint8_t) (cable_num << 4 | 0xf);
2021-04-02 14:26:55 +07:00
stream->buffer[2] = 0;
stream->buffer[3] = 0;
stream->index = 2;
stream->total = 2;
2021-03-31 00:34:09 +07:00
}
2020-01-14 23:30:39 -05:00
}
2021-03-31 00:34:09 +07:00
else
{
//------------- On-going (buffering) packet -------------//
2020-01-14 23:30:39 -05:00
TU_ASSERT(stream->index < 4, i);
2021-04-02 14:26:55 +07:00
stream->buffer[stream->index] = data;
stream->index++;
2021-03-31 00:34:09 +07:00
// See if this byte ends a SysEx.
2021-04-02 14:26:55 +07:00
if ( stream->buffer[0] == MIDI_CIN_SYSEX_START && data == MIDI_STATUS_SYSEX_END )
2021-03-31 00:34:09 +07:00
{
2021-04-02 14:26:55 +07:00
stream->buffer[0] = MIDI_CIN_SYSEX_START + (stream->index - 1);
stream->total = stream->index;
2021-03-31 00:34:09 +07:00
}
2020-01-14 23:30:39 -05:00
}
// Send out packet
2021-04-02 14:26:55 +07:00
if ( stream->index == stream->total )
2021-03-31 00:34:09 +07:00
{
// zeroes unused bytes
2021-04-02 14:26:55 +07:00
for(uint8_t idx = stream->total; idx < 4; idx++) stream->buffer[idx] = 0;
2021-04-02 14:26:55 +07:00
uint16_t const count = tu_fifo_write_n(&midi->tx_ff, stream->buffer, 4);
// complete current event packet, reset stream
2021-04-02 14:26:55 +07:00
stream->index = stream->total = 0;
// FIFO overflown, since we already check fifo remaining. It is probably race condition
TU_ASSERT(count == 4, i);
2020-01-14 23:30:39 -05:00
}
}
write_flush(midi);
2020-01-14 23:30:39 -05:00
return i;
2020-01-14 23:30:39 -05:00
}
bool tud_midi_n_packet_write (uint8_t itf, uint8_t const packet[4])
{
midid_interface_t* midi = &_midid_itf[itf];
TU_VERIFY(midi->ep_in);
if (tu_fifo_remaining(&midi->tx_ff) < 4) return false;
tu_fifo_write_n(&midi->tx_ff, packet, 4);
write_flush(midi);
return true;
}
2020-01-14 23:30:39 -05:00
//--------------------------------------------------------------------+
// USBD Driver API
//--------------------------------------------------------------------+
void midid_init(void)
{
tu_memclr(_midid_itf, sizeof(_midid_itf));
for(uint8_t i=0; i<CFG_TUD_MIDI; i++)
{
midid_interface_t* midi = &_midid_itf[i];
// config fifo
2021-01-30 04:11:08 +03:00
tu_fifo_config(&midi->rx_ff, midi->rx_ff_buf, CFG_TUD_MIDI_RX_BUFSIZE, 1, false); // true, true
tu_fifo_config(&midi->tx_ff, midi->tx_ff_buf, CFG_TUD_MIDI_TX_BUFSIZE, 1, false); // OBVS.
2020-01-14 23:30:39 -05:00
#if CFG_FIFO_MUTEX
tu_fifo_config_mutex(&midi->rx_ff, NULL, osal_mutex_create(&midi->rx_ff_mutex));
tu_fifo_config_mutex(&midi->tx_ff, osal_mutex_create(&midi->tx_ff_mutex), NULL);
2020-01-14 23:30:39 -05:00
#endif
}
}
void midid_reset(uint8_t rhport)
{
(void) rhport;
for(uint8_t i=0; i<CFG_TUD_MIDI; i++)
{
midid_interface_t* midi = &_midid_itf[i];
tu_memclr(midi, ITF_MEM_RESET_SIZE);
tu_fifo_clear(&midi->rx_ff);
tu_fifo_clear(&midi->tx_ff);
}
}
2020-05-28 11:41:37 +07:00
uint16_t midid_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t max_len)
2020-01-14 23:30:39 -05:00
{
uint16_t drv_len = 0;
uint8_t const * p_desc = (uint8_t const *)desc_itf;
2020-04-15 01:01:31 +07:00
// 1st Interface is Audio Control v1
if (TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass &&
AUDIO_SUBCLASS_CONTROL == desc_itf->bInterfaceSubClass &&
AUDIO_FUNC_PROTOCOL_CODE_UNDEF == desc_itf->bInterfaceProtocol)
{
drv_len = tu_desc_len(desc_itf);
p_desc = tu_desc_next(desc_itf);
// Skip Class Specific descriptors
while ( TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) && drv_len <= max_len )
{
drv_len += tu_desc_len(p_desc);
p_desc = tu_desc_next(p_desc);
}
}
else
2020-04-15 01:01:31 +07:00
{
TU_LOG1("Warning: MIDI Device has no Audio Control Interface");
2020-01-14 23:30:39 -05:00
}
2020-04-15 01:01:31 +07:00
// 2nd Interface is MIDI Streaming
2020-05-28 11:41:37 +07:00
TU_VERIFY(TUSB_DESC_INTERFACE == tu_desc_type(p_desc), 0);
2020-04-15 01:01:31 +07:00
tusb_desc_interface_t const * desc_midi = (tusb_desc_interface_t const *) p_desc;
TU_VERIFY(TUSB_CLASS_AUDIO == desc_midi->bInterfaceClass &&
AUDIO_SUBCLASS_MIDI_STREAMING == desc_midi->bInterfaceSubClass &&
AUDIO_FUNC_PROTOCOL_CODE_UNDEF == desc_midi->bInterfaceProtocol, 0);
2020-01-14 23:30:39 -05:00
// Find available interface
midid_interface_t * p_midi = NULL;
for(uint8_t i=0; i<CFG_TUD_MIDI; i++)
{
if ( _midid_itf[i].ep_in == 0 && _midid_itf[i].ep_out == 0 )
{
p_midi = &_midid_itf[i];
break;
}
}
2021-10-18 01:11:27 +07:00
TU_ASSERT(p_midi);
2020-01-14 23:30:39 -05:00
2020-05-28 11:41:37 +07:00
p_midi->itf_num = desc_midi->bInterfaceNumber;
(void) p_midi->itf_num;
2020-01-14 23:30:39 -05:00
2020-04-15 01:01:31 +07:00
// next descriptor
drv_len += tu_desc_len(p_desc);
2020-05-28 11:41:37 +07:00
p_desc = tu_desc_next(p_desc);
2020-01-14 23:30:39 -05:00
2020-04-15 01:01:31 +07:00
// Find and open endpoint descriptors
2020-01-14 23:30:39 -05:00
uint8_t found_endpoints = 0;
2020-05-28 11:41:37 +07:00
while ( (found_endpoints < desc_midi->bNumEndpoints) && (drv_len <= max_len) )
2020-01-14 23:30:39 -05:00
{
2020-05-28 11:41:37 +07:00
if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) )
2020-01-14 23:30:39 -05:00
{
2020-05-28 11:41:37 +07:00
TU_ASSERT(usbd_edpt_open(rhport, (tusb_desc_endpoint_t const *) p_desc), 0);
uint8_t ep_addr = ((tusb_desc_endpoint_t const *) p_desc)->bEndpointAddress;
if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN)
{
p_midi->ep_in = ep_addr;
} else {
p_midi->ep_out = ep_addr;
}
2021-03-04 19:30:08 +07:00
// Class Specific MIDI Stream endpoint descriptor
2020-05-28 11:41:37 +07:00
drv_len += tu_desc_len(p_desc);
p_desc = tu_desc_next(p_desc);
2020-01-14 23:30:39 -05:00
2020-05-28 11:41:37 +07:00
found_endpoints += 1;
2020-01-14 23:30:39 -05:00
}
2020-05-28 11:41:37 +07:00
drv_len += tu_desc_len(p_desc);
p_desc = tu_desc_next(p_desc);
}
2020-04-15 01:01:31 +07:00
2020-01-14 23:30:39 -05:00
// Prepare for incoming data
2021-01-30 04:11:08 +03:00
_prep_out_transaction(p_midi);
2020-01-14 23:30:39 -05:00
2020-05-28 11:41:37 +07:00
return drv_len;
2020-01-14 23:30:39 -05:00
}
2020-11-20 15:32:16 +07:00
// Invoked when a control transfer occurred on an interface of this class
// Driver response accordingly to the request and the transfer stage (setup/data/ack)
// return false to stall control endpoint (e.g unsupported request)
bool midid_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
2020-01-14 23:30:39 -05:00
{
(void) rhport;
(void) stage;
(void) request;
2020-01-14 23:30:39 -05:00
// driver doesn't support any request yet
return false;
}
bool midid_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
{
(void) result;
2021-01-30 04:11:08 +03:00
(void) rhport;
2020-01-14 23:30:39 -05:00
2020-08-24 15:29:34 +07:00
uint8_t itf;
midid_interface_t* p_midi;
2020-01-14 23:30:39 -05:00
// Identify which interface to use
for (itf = 0; itf < CFG_TUD_MIDI; itf++)
2020-01-14 23:30:39 -05:00
{
p_midi = &_midid_itf[itf];
if ( ( ep_addr == p_midi->ep_out ) || ( ep_addr == p_midi->ep_in ) ) break;
2020-01-14 23:30:39 -05:00
}
2020-08-24 15:29:34 +07:00
TU_ASSERT(itf < CFG_TUD_MIDI);
2020-01-14 23:30:39 -05:00
// receive new data
if ( ep_addr == p_midi->ep_out )
{
tu_fifo_write_n(&p_midi->rx_ff, p_midi->epout_buf, (uint16_t) xferred_bytes);
// invoke receive callback if available
if (tud_midi_rx_cb) tud_midi_rx_cb(itf);
2020-01-14 23:30:39 -05:00
// prepare for next
2020-09-10 23:32:08 +07:00
// TODO for now ep_out is not used by public API therefore there is no race condition,
// and does not need to claim like ep_in
2021-01-30 04:11:08 +03:00
_prep_out_transaction(p_midi);
}
else if ( ep_addr == p_midi->ep_in )
{
if (0 == write_flush(p_midi))
{
2020-09-10 23:32:08 +07:00
// If there is no data left, a ZLP should be sent if
// xferred_bytes is multiple of EP size and not zero
2020-09-10 23:32:08 +07:00
if ( !tu_fifo_count(&p_midi->tx_ff) && xferred_bytes && (0 == (xferred_bytes % CFG_TUD_MIDI_EP_BUFSIZE)) )
{
2020-09-10 23:32:08 +07:00
if ( usbd_edpt_claim(rhport, p_midi->ep_in) )
{
usbd_edpt_xfer(rhport, p_midi->ep_in, NULL, 0);
}
}
}
2020-01-14 23:30:39 -05:00
}
2020-01-16 11:12:26 +07:00
return true;
2020-01-14 23:30:39 -05:00
}
#endif