From 2ddd74fada9b2f272923fce9c3b1926a6e9f5c80 Mon Sep 17 00:00:00 2001 From: rppicomidi Date: Fri, 12 Aug 2022 11:06:36 -0700 Subject: [PATCH 01/30] Add MIDI host support to tinyusb --- hw/bsp/rp2040/family.cmake | 1 + src/class/midi/README_midi_host.md | 111 ++++ src/class/midi/midi.h | 19 +- src/class/midi/midi_host.c | 944 +++++++++++++++++++++++++++++ src/class/midi/midi_host.h | 137 +++++ src/host/hcd.h | 2 +- src/host/usbh.c | 11 + src/tusb.h | 4 + 8 files changed, 1226 insertions(+), 3 deletions(-) create mode 100644 src/class/midi/README_midi_host.md create mode 100644 src/class/midi/midi_host.c create mode 100644 src/class/midi/midi_host.h diff --git a/hw/bsp/rp2040/family.cmake b/hw/bsp/rp2040/family.cmake index 5dba9dc39..964855c05 100644 --- a/hw/bsp/rp2040/family.cmake +++ b/hw/bsp/rp2040/family.cmake @@ -93,6 +93,7 @@ if (NOT TARGET _rp2040_family_inclusion_marker) ${TOP}/src/class/cdc/cdc_host.c ${TOP}/src/class/hid/hid_host.c ${TOP}/src/class/msc/msc_host.c + ${TOP}/src/class/midi/midi_host.c ${TOP}/src/class/vendor/vendor_host.c ) diff --git a/src/class/midi/README_midi_host.md b/src/class/midi/README_midi_host.md new file mode 100644 index 000000000..9df2249b3 --- /dev/null +++ b/src/class/midi/README_midi_host.md @@ -0,0 +1,111 @@ +# MIDI HOST DRIVER +This README file contains the design notes and limitations of the +MIDI host driver. + +# MAXIMUM NUMBER OF MIDI DEVICES ATTACHED TO HOST +In this version of the driver, only one MIDI device is supported. This +constraint may change in the future. + +# MAXIMUM NUMBER OF ENDPOINTS +Although the USB MIDI 1.0 Class specification allows an arbitrary number +of endpoints, this driver supports at most one USB BULK DATA IN endpoint +and one USB BULK DATA OUT endpoint. Each endpoint can support up to 16 +virtual cables. If a device has multiple IN endpoints or multiple OUT +endpoints, it will fail to enumerate. + +Most USB MIDI devices contain both an IN endpoint and an OUT endpoint, +but not all do. For example, some USB pedals only support an OUT endpoint. +This driver allows that. + +# PUBLIC API +Applications interact with this driver via 8-bit buffers of MIDI messages +formed using the rules for sending bytes on a 5-pin DIN cable per the +original MIDI 1.0 specification. + +To send a message to a device, the Host application composes a sequence +of status and data bytes in a byte array and calls the API function. +The arguments of the function are a pointer to the byte array, the number +of bytes in the array, and the target virtual cable number 0-15. + +When the host driver receives a message from the device, the host driver +will call a callback function that the host application registers. This +callback function contains a pointer to a message buffer, a message length, +and the virtual cable number of the message buffer. One complete bulk IN +endpoint transfer might contain multiple messages targeted to different +virtual cables. + +# SUBCLASS AUDIO CONTROL +A MIDI device does not absolutely need to have an Audio Control Interface, +unless it adheres to the USB Audio Class 2 spec, but many devices +have them even if the devices do not have an audio streaming interface. +Because this driver does not support audio streaming, the descriptor parser +will skip past any audio control interface and audio streaming interface +and open only the MIDI interface. + +An audio streaming host driver can use this driver by passing a pointer +to the MIDI interface descriptor that is found after the audio streaming +interface to the midih_open() function. That is, an audio streaming host +driver would parse the audio control interface descriptor and then the +audio streaming interface and endpoint descriptors. When the next descriptor +pointer points to a MIDI interface descriptor, call midih_open() with that +descriptor pointer. + +# CLASS SPECIFIC INTERFACE AND REQUESTS +The host driver does not make use of the informaton in the class specific +interface descriptors. In the future, a public API could be created to +retrieve the string descriptors for the names of each ELEMENT, +IN JACK and OUT JACK, and how the device describes the connections. + +This driver also does not support class specific requests to control +ELEMENT items, nor does it support non-MIDI Streaming bulk endpoints. + +# MIDI CLASS SPECIFIC DESCRIPTOR TOTAL LENGTH FIELD IGNORED +I have observed at least one keyboard by a leading manufacturer that +sets the wTotalLength field of the Class-Specific MS Interface Header +Descriptor to include the length of the MIDIStreaming Endpoint +Descriptors. This is wrong per my reading of the specification. + +# MESSAGE BUFFER DETAILS +Messages buffers composed from USB data received on the IN endpoint will never contain +running status because USB MIDI 1.0 class does not support that. Messages +buffers to be sent to the device on the OUT endpont may contain running status +(the message might come from a UART data stream from a 5-pin DIN MIDI IN +cable on the host, for example). The driver may in the future correctly compose +4-byte USB MIDI Class packets using the running status if need be. However, +it does not currently do that. Also, use of running status is not a good idea +overall because a single byte error can really mess up the data stream with no +way to recover until the next non-real time status byte is in the message buffer. + +Message buffers to be sent to the device may contain Real time messages +such as MIDI clock. Real time messages may be inserted in the message +byte stream between status and data bytes of another message without disrupting +the running status. However, because MIDI 1.0 class messages are sent +as four byte packets, a real-time message so inserted will be re-ordered +to be sent to the device in a new 4-byte packet immediately before the +interrupted data stream. + +Real time messages the device sends to the host can only appear between +the status byte and data bytes of the message in System Exclusive messages +that are longer than 3 bytes. + +# POORLY FORMED USB MIDI DATA PACKETS FROM THE DEVICE +Some devices do not properly encode the code index number (CIN) for the +MIDI message status byte even though the 3-byte data payload correctly encodes +the MIDI message. This driver looks to the byte after the CIN byte to decide +how many bytes to place in the message buffer. + +Some devices do not properly encode the virtual cable number. If the virtual +cable number in the CIN data byte of the packet is not less than bNumEmbMIDIJack +for that endpoint, then the host driver assumes virtual cable 0 and does not +report an error. + +Some MIDI devices will always send back exactly wMaxPacketSize bytes on +every endpoint even if only one 4-byte packet is required (e.g., NOTE ON). +These devices send packets with 4 packet bytes 0. This driver ignores all +zero packets without reporting an error. + +# ENUMERATION FAILURES +The host may fail to enumerate a device if it has too many endpoints, if it has +if it has a Standard MS Transfer Bulk Data Endpoint Descriptor (not supported), +if it has a poorly formed descriptor, or if the descriptor is too long for +the host to read the whole thing. diff --git a/src/class/midi/midi.h b/src/class/midi/midi.h index 74dc41749..ea2cfec75 100644 --- a/src/class/midi/midi.h +++ b/src/class/midi/midi.h @@ -71,8 +71,8 @@ typedef enum MIDI_CIN_SYSEX_END_1BYTE = 5, // SysEx ends with 1 data, or 1 byte system common message MIDI_CIN_SYSEX_END_2BYTE = 6, // SysEx ends with 2 data MIDI_CIN_SYSEX_END_3BYTE = 7, // SysEx ends with 3 data - MIDI_CIN_NOTE_ON = 8, - MIDI_CIN_NOTE_OFF = 9, + MIDI_CIN_NOTE_ON = 9, + MIDI_CIN_NOTE_OFF = 8, MIDI_CIN_POLY_KEYPRESS = 10, MIDI_CIN_CONTROL_CHANGE = 11, MIDI_CIN_PROGRAM_CHANGE = 12, @@ -106,6 +106,11 @@ enum MIDI_STATUS_SYSREAL_SYSTEM_RESET = 0xFF, }; +enum +{ + MIDI_MAX_DATA_VAL = 0x7F, +}; + /// MIDI Interface Header Descriptor typedef struct TU_ATTR_PACKED { @@ -201,6 +206,16 @@ typedef struct TU_ATTR_PACKED uint8_t iElement; \ } +// This descriptor follows the standard bulk data endpoint descriptor +typedef struct +{ + uint8_t bLength ; ///< Size of this descriptor in bytes (4+bNumEmbMIDIJack) + uint8_t bDescriptorType ; ///< Descriptor Type, must be CS_ENDPOINT + uint8_t bDescriptorSubType ; ///< Descriptor SubType, must be MS_GENERAL + uint8_t bNumEmbMIDIJack; ; ///< Number of embedded MIDI jacks associated with this endpoint + uint8_t baAssocJackID[]; ; ///< A list of associated jacks +} midi_cs_desc_endpoint_t; + /** @} */ #ifdef __cplusplus diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c new file mode 100644 index 000000000..e482f5265 --- /dev/null +++ b/src/class/midi/midi_host.c @@ -0,0 +1,944 @@ +/* + * 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 (TUSB_OPT_HOST_ENABLED && CFG_TUH_MIDI) + +#include "host/usbh.h" +#include "host/usbh_classdriver.h" + +#include "midi_host.h" + +//--------------------------------------------------------------------+ +// MACRO CONSTANT TYPEDEF +//--------------------------------------------------------------------+ +#ifndef CFG_TUH_MAX_CABLES + #define CFG_TUH_MAX_CABLES 16 +#endif +#define CFG_TUH_MIDI_RX_BUFSIZE 64 +#define CFG_TUH_MIDI_TX_BUFSIZE 64 +#ifndef CFG_TUH_MIDI_EP_BUFSIZE + #define CFG_TUH_MIDI_EP_BUFSIZE 64 +#endif + +// TODO: refactor to share code with the MIDI Device driver +typedef struct +{ + uint8_t buffer[4]; + uint8_t index; + uint8_t total; +}midi_stream_t; + +typedef struct +{ + uint8_t dev_addr; + uint8_t itf_num; + + uint8_t ep_in; // IN endpoint address + uint8_t ep_out; // OUT endpoint address + uint16_t ep_in_max; // min( CFG_TUH_MIDI_RX_BUFSIZE, wMaxPacketSize of the IN endpoint) + uint16_t ep_out_max; // min( CFG_TUH_MIDI_TX_BUFSIZE, wMaxPacketSize of the OUT endpoint) + + uint8_t num_cables_rx; // IN endpoint CS descriptor bNumEmbMIDIJack value + uint8_t num_cables_tx; // OUT endpoint CS descriptor bNumEmbMIDIJack value + + // 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. + midi_stream_t stream_write; + midi_stream_t stream_read; + + /*------------- From this point, data is not cleared by bus reset -------------*/ + // Endpoint FIFOs + tu_fifo_t rx_ff; + tu_fifo_t tx_ff; + + + uint8_t rx_ff_buf[CFG_TUH_MIDI_RX_BUFSIZE]; + uint8_t tx_ff_buf[CFG_TUH_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_TUH_MIDI_EP_BUFSIZE]; + CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUH_MIDI_EP_BUFSIZE]; + + bool configured; + +#if CFG_MIDI_HOST_DEVSTRINGS +#define MAX_STRING_INDICES 32 + uint8_t all_string_indices[MAX_STRING_INDICES]; + uint8_t num_string_indices; +#define MAX_IN_JACKS 8 +#define MAX_OUT_JACKS 8 + struct { + uint8_t jack_id; + int8_t jack_type; + uint8_t string_index; + } in_jack_info[MAX_IN_JACKS]; + uint8_t next_in_jack; + struct { + uint8_t jack_id; + uint8_t jack_type; + uint8_t num_source_ids; + uint8_t source_ids[MAX_IN_JACKS/4]; + uint8_t string_index; + } out_jack_info[MAX_OUT_JACKS]; + uint8_t next_out_jack; + uint8_t ep_in_associated_jacks[MAX_OUT_JACKS/2]; + uint8_t ep_out_associated_jacks[MAX_IN_JACKS/2]; +#endif +}midih_interface_t; + +static midih_interface_t _midi_host[CFG_TUH_DEVICE_MAX]; + +static midih_interface_t *get_midi_host(uint8_t dev_addr) +{ + TU_VERIFY(dev_addr >0 && dev_addr <= CFG_TUH_DEVICE_MAX); + return (_midi_host + dev_addr - 1); +} + +//------------- Internal prototypes -------------// +static uint32_t write_flush(uint8_t dev_addr, midih_interface_t* midi); + +//--------------------------------------------------------------------+ +// USBH API +//--------------------------------------------------------------------+ +void midih_init(void) +{ + tu_memclr(&_midi_host, sizeof(_midi_host)); + // config fifos + for (int inst = 0; inst < CFG_TUH_DEVICE_MAX; inst++) + { + midih_interface_t *p_midi_host = &_midi_host[inst]; + tu_fifo_config(&p_midi_host->rx_ff, p_midi_host->rx_ff_buf, CFG_TUH_MIDI_RX_BUFSIZE, 1, false); // true, true + tu_fifo_config(&p_midi_host->tx_ff, p_midi_host->tx_ff_buf, CFG_TUH_MIDI_TX_BUFSIZE, 1, false); // OBVS. + + #if CFG_FIFO_MUTEX + tu_fifo_config_mutex(&p_midi_host->rx_ff, NULL, osal_mutex_create(&p_midi_host->rx_ff_mutex)); + tu_fifo_config_mutex(&p_midi_host->tx_ff, osal_mutex_create(&p_midi_host->tx_ff_mutex), NULL); + #endif + } +} + +bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) +{ + (void)result; + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + TU_VERIFY(p_midi_host != NULL); + if ( ep_addr == p_midi_host->ep_in) + { + if (0 == xferred_bytes) + { + return true; // No data to handle + } + + // receive new data if available + uint32_t packets_queued = 0; + if (xferred_bytes) + { + // put in the RX FIFO only non-zero MIDI IN 4-byte packets + uint8_t* buf = p_midi_host->epin_buf; + uint32_t npackets = xferred_bytes / 4; + uint32_t packet_num; + for (packet_num = 0; packet_num < npackets; packet_num++) + { + // some devices send back all zero packets even if there is no data ready + uint32_t packet = (uint32_t)((*buf)<<24) | ((uint32_t)(*(buf+1))<<16) | ((uint32_t)(*(buf+2))<<8) | ((uint32_t)(*(buf+3))); + if (packet != 0) + { + tu_fifo_write_n(&p_midi_host->rx_ff, buf, 4); + ++packets_queued; + TU_LOG3("MIDI RX=%08x\r\n", packet); + } + buf += 4; + } + } + // invoke receive callback if available + if (tuh_midi_rx_cb) + { + tuh_midi_rx_cb(dev_addr, packets_queued); + } + } + else if ( ep_addr == p_midi_host->ep_out ) + { + if (0 == write_flush(dev_addr, p_midi_host)) + { + // If there is no data left, a ZLP should be sent if + // xferred_bytes is multiple of EP size and not zero + if ( !tu_fifo_count(&p_midi_host->tx_ff) && xferred_bytes && (0 == (xferred_bytes % p_midi_host->ep_out_max)) ) + { + if ( usbh_edpt_claim(dev_addr, p_midi_host->ep_out) ) + { + TU_ASSERT(usbh_edpt_xfer(dev_addr, p_midi_host->ep_out, XFER_RESULT_SUCCESS, 0)); + } + } + } + if (tuh_midi_tx_cb) + { + tuh_midi_tx_cb(dev_addr); + } + } + + return true; +} + +void midih_close(uint8_t dev_addr) +{ + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + if (p_midi_host == NULL) + return; + if (tuh_midi_umount_cb) + tuh_midi_umount_cb(dev_addr, 0); + tu_fifo_clear(&p_midi_host->rx_ff); + tu_fifo_clear(&p_midi_host->tx_ff); + p_midi_host->ep_in = 0; + p_midi_host->ep_in_max = 0; + p_midi_host->ep_out = 0; + p_midi_host->ep_out_max = 0; + p_midi_host->itf_num = 0; + p_midi_host->num_cables_rx = 0; + p_midi_host->num_cables_tx = 0; + p_midi_host->dev_addr = 255; // invalid + p_midi_host->configured = false; + tu_memclr(&p_midi_host->stream_read, sizeof(p_midi_host->stream_read)); + tu_memclr(&p_midi_host->stream_write, sizeof(p_midi_host->stream_write)); +} + +//--------------------------------------------------------------------+ +// Enumeration +//--------------------------------------------------------------------+ +bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len) +{ + (void) rhport; + + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + + TU_VERIFY(p_midi_host != NULL); + p_midi_host->num_string_indices = 0; + TU_VERIFY(TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass); + // There can be just a MIDI interface or an audio and a MIDI interface. Only open the MIDI interface + uint8_t const *p_desc = (uint8_t const *) desc_itf; + uint16_t len_parsed = 0; + if (AUDIO_SUBCLASS_CONTROL == desc_itf->bInterfaceSubClass) + { + // Keep track of any string descriptor that might be here + if (desc_itf->iInterface != 0) + p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = desc_itf->iInterface; + // This driver does not support audio streaming. However, if this is the audio control interface + // there might be a MIDI interface following it. Search through every descriptor until a MIDI + // interface is found or the end of the descriptor is found + while (len_parsed < max_len && (desc_itf->bInterfaceClass != TUSB_CLASS_AUDIO || desc_itf->bInterfaceSubClass != AUDIO_SUBCLASS_MIDI_STREAMING)) + { + len_parsed += desc_itf->bLength; + p_desc = tu_desc_next(p_desc); + desc_itf = (tusb_desc_interface_t const *)p_desc; + } + + TU_VERIFY(TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass); + } + TU_VERIFY(AUDIO_SUBCLASS_MIDI_STREAMING == desc_itf->bInterfaceSubClass); + len_parsed += desc_itf->bLength; + + // Keep track of any string descriptor that might be here + if (desc_itf->iInterface != 0) + p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = desc_itf->iInterface; + + p_desc = tu_desc_next(p_desc); + TU_LOG1("MIDI opening Interface %u (addr = %u)\r\n", desc_itf->bInterfaceNumber, dev_addr); + // Find out if getting the MIDI class specific interface header or an endpoint descriptor + // or a class-specific endpoint descriptor + // Jack descriptors or element descriptors must follow the cs interface header, + // but this driver does not support devices that contain element descriptors + + // assume it is an interface header + midi_desc_header_t const *p_mdh = (midi_desc_header_t const *)p_desc; + TU_VERIFY((p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE && p_mdh->bDescriptorSubType == MIDI_CS_INTERFACE_HEADER) || + (p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT && p_mdh->bDescriptorSubType == MIDI_CS_ENDPOINT_GENERAL) || + p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT); + + uint8_t prev_ep_addr = 0; // the CS endpoint descriptor is associated with the previous endpoint descrptor + p_midi_host->itf_num = desc_itf->bInterfaceNumber; + tusb_desc_endpoint_t const* in_desc = NULL; + tusb_desc_endpoint_t const* out_desc = NULL; + while (len_parsed < max_len) + { + TU_VERIFY((p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE) || + (p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT && p_mdh->bDescriptorSubType == MIDI_CS_ENDPOINT_GENERAL) || + p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT); + + if (p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE) { + // The USB host doesn't really need this information unless it uses + // the string descriptor for a jack or Element + + // assume it is an input jack + midi_desc_in_jack_t const *p_mdij = (midi_desc_in_jack_t const *)p_desc; + if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_HEADER) + { + TU_LOG2("Found MIDI Interface Header\r\b"); + } + else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_IN_JACK) + { + // Then it is an in jack. + TU_LOG2("Found in jack\r\n"); +#if CFG_MIDI_HOST_DEVSTRINGS + if (p_midi_host->next_in_jack < MAX_IN_JACKS) + { + p_midi_host->in_jack_info[p_midi_host->next_in_jack].jack_id = p_mdij->bJackID; + p_midi_host->in_jack_info[p_midi_host->next_in_jack].jack_type = p_mdij->bJackType; + p_midi_host->in_jack_info[p_midi_host->next_in_jack].string_index = p_mdij->iJack; + ++p_midi_host->next_in_jack; + // Keep track of any string descriptor that might be here + if (p_mdij->iJack != 0) + p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = p_mdij->iJack; + + } +#endif + } + else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_OUT_JACK) + { + // then it is an out jack + TU_LOG2("Found out jack\r\n"); +#if CFG_MIDI_HOST_DEVSTRINGS + if (p_midi_host->next_out_jack < MAX_OUT_JACKS) + { + midi_desc_out_jack_t const *p_mdoj = (midi_desc_out_jack_t const *)p_desc; + p_midi_host->out_jack_info[p_midi_host->next_out_jack].jack_id = p_mdoj->bJackID; + p_midi_host->out_jack_info[p_midi_host->next_out_jack].jack_type = p_mdoj->bJackType; + p_midi_host->out_jack_info[p_midi_host->next_out_jack].num_source_ids = p_mdoj->bNrInputPins; + struct associated_jack_s { + uint8_t id; + uint8_t pin; + } *associated_jack = (struct associated_jack_s *)(p_desc+6); + int jack; + for (jack = 0; jack < p_mdoj->bNrInputPins; jack++) + { + p_midi_host->out_jack_info[p_midi_host->next_out_jack].source_ids[jack] = associated_jack->id; + } + p_midi_host->out_jack_info[p_midi_host->next_out_jack].string_index = *(p_desc+6+p_mdoj->bNrInputPins*2); + ++p_midi_host->next_out_jack; + if (p_mdoj->iJack != 0) + p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = p_mdoj->iJack; + } +#endif + } + else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_ELEMENT) + { + // the it is an element; + #if CFG_MIDI_HOST_DEVSTRINGS + TU_LOG1("Found element; strings not supported\r\n"); + #else + TU_LOG2("Found element\r\n"); + #endif + } + else + { + TU_LOG2("Unknown CS Interface sub-type %u\r\n", p_mdij->bDescriptorSubType); + TU_VERIFY(false); // unknown CS Interface sub-type + } + len_parsed += p_mdij->bLength; + } + else if (p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT) + { + TU_LOG2("found CS_ENDPOINT Descriptor for %u\r\n", prev_ep_addr); + TU_VERIFY(prev_ep_addr != 0); + // parse out the mapping between the device's embedded jacks and the endpoints + // Each embedded IN jack is assocated with an OUT endpoint + midi_cs_desc_endpoint_t const* p_csep = (midi_cs_desc_endpoint_t const*)p_mdh; + if (tu_edpt_dir(prev_ep_addr) == TUSB_DIR_OUT) + { + TU_VERIFY(p_midi_host->ep_out == prev_ep_addr); + TU_VERIFY(p_midi_host->num_cables_tx == 0); + p_midi_host->num_cables_tx = p_csep->bNumEmbMIDIJack; +#if CFG_MIDI_HOST_DEVSTRINGS + uint8_t jack; + uint8_t max_jack = p_midi_host->num_cables_tx; + if (max_jack > sizeof(p_midi_host->ep_out_associated_jacks)) + { + max_jack = sizeof(p_midi_host->ep_out_associated_jacks); + } + for (jack = 0; jack < max_jack; jack++) + { + p_midi_host->ep_out_associated_jacks[jack] = p_csep->baAssocJackID[jack]; + } +#endif + } + else + { + TU_VERIFY(p_midi_host->ep_in == prev_ep_addr); + TU_VERIFY(p_midi_host->num_cables_rx == 0); + p_midi_host->num_cables_rx = p_csep->bNumEmbMIDIJack; +#if CFG_MIDI_HOST_DEVSTRINGS + uint8_t jack; + uint8_t max_jack = p_midi_host->num_cables_rx; + if (max_jack > sizeof(p_midi_host->ep_in_associated_jacks)) + { + max_jack = sizeof(p_midi_host->ep_in_associated_jacks); + } + for (jack = 0; jack < max_jack; jack++) + { + p_midi_host->ep_in_associated_jacks[jack] = p_csep->baAssocJackID[jack]; + } +#endif + } + len_parsed += p_csep->bLength; + prev_ep_addr = 0; + } + else if (p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT) { + // parse out the bulk endpoint info + tusb_desc_endpoint_t const *p_ep = (tusb_desc_endpoint_t const *)p_mdh; + TU_LOG2("found ENDPOINT Descriptor for %u\r\n", p_ep->bEndpointAddress); + if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_OUT) + { + TU_VERIFY(p_midi_host->ep_out == 0); + TU_VERIFY(p_midi_host->num_cables_tx == 0); + p_midi_host->ep_out = p_ep->bEndpointAddress; + p_midi_host->ep_out_max = p_ep->wMaxPacketSize; + if (p_midi_host->ep_out_max > CFG_TUH_MIDI_TX_BUFSIZE) + p_midi_host->ep_out_max = CFG_TUH_MIDI_TX_BUFSIZE; + prev_ep_addr = p_midi_host->ep_out; + out_desc = p_ep; + } + else + { + TU_VERIFY(p_midi_host->ep_in == 0); + TU_VERIFY(p_midi_host->num_cables_rx == 0); + p_midi_host->ep_in = p_ep->bEndpointAddress; + p_midi_host->ep_in_max = p_ep->wMaxPacketSize; + if (p_midi_host->ep_in_max > CFG_TUH_MIDI_RX_BUFSIZE) + p_midi_host->ep_in_max = CFG_TUH_MIDI_RX_BUFSIZE; + prev_ep_addr = p_midi_host->ep_in; + in_desc = p_ep; + } + len_parsed += p_mdh->bLength; + } + p_desc = tu_desc_next(p_desc); + p_mdh = (midi_desc_header_t const *)p_desc; + } + TU_VERIFY((p_midi_host->ep_out != 0 && p_midi_host->num_cables_tx != 0) || + (p_midi_host->ep_in != 0 && p_midi_host->num_cables_rx != 0)); + TU_LOG1("MIDI descriptor parsed successfully\r\n"); + // remove duplicate string indices + for (int idx=0; idx < p_midi_host->num_string_indices; idx++) { + for (int jdx = idx+1; jdx < p_midi_host->num_string_indices; jdx++) { + while (jdx < p_midi_host->num_string_indices && p_midi_host->all_string_indices[idx] == p_midi_host->all_string_indices[jdx]) { + // delete the duplicate by overwriting it with the last entry and reducing the number of entries by 1 + p_midi_host->all_string_indices[jdx] = p_midi_host->all_string_indices[p_midi_host->num_string_indices-1]; + --p_midi_host->num_string_indices; + } + } + } + if (in_desc) + { + TU_ASSERT(tuh_edpt_open(dev_addr, in_desc)); + // Some devices always return exactly the request length so transfers won't complete + // unless you assume every transfer is the last one. + // TODO usbh_edpt_force_last_buffer(dev_addr, p_midi_host->ep_in, true); + } + if (out_desc) + { + TU_ASSERT(tuh_edpt_open(dev_addr, out_desc)); + } + p_midi_host->dev_addr = dev_addr; + + if (tuh_midi_mount_cb) + { + tuh_midi_mount_cb(dev_addr, p_midi_host->ep_in, p_midi_host->ep_out, p_midi_host->num_cables_rx, p_midi_host->num_cables_tx); + } + return true; +} + +bool tuh_midi_configured(uint8_t dev_addr) +{ + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + TU_VERIFY(p_midi_host != NULL); + return p_midi_host->configured; +} + +bool midih_set_config(uint8_t dev_addr, uint8_t itf_num) +{ + (void) itf_num; + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + TU_VERIFY(p_midi_host != NULL); + p_midi_host->configured = true; + + // TODO I don't think there are any special config things to do for MIDI + + return true; +} + +//--------------------------------------------------------------------+ +// Stream API +//--------------------------------------------------------------------+ +static uint32_t write_flush(uint8_t dev_addr, midih_interface_t* midi) +{ + // No data to send + if ( !tu_fifo_count(&midi->tx_ff) ) return 0; + + // skip if previous transfer not complete + TU_VERIFY( usbh_edpt_claim(dev_addr, midi->ep_out) ); + + uint16_t count = tu_fifo_read_n(&midi->tx_ff, midi->epout_buf, midi->ep_out_max); + + if (count) + { + TU_ASSERT( usbh_edpt_xfer(dev_addr, midi->ep_out, midi->epout_buf, count), 0 ); + return count; + }else + { + // Release endpoint since we don't make any transfer + usbh_edpt_release(dev_addr, midi->ep_out); + return 0; + } +} + +bool tuh_midi_read_poll( uint8_t dev_addr ) +{ + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + TU_VERIFY(p_midi_host != NULL); + bool result = false; + + bool in_edpt_not_busy = !usbh_edpt_busy(dev_addr, p_midi_host->ep_in); + if (in_edpt_not_busy) + { + TU_LOG2("Requesting poll IN endpoint %d\r\n", p_midi_host->ep_in); + TU_ASSERT(usbh_edpt_xfer(p_midi_host->dev_addr, p_midi_host->ep_in, _midi_host->epin_buf, _midi_host->ep_in_max), 0); + result = true; + } + else + { + // Maybe the IN endpoint is only busy because the RP2040 host hardware + // is retrying a NAK'd IN transfer forever. Try aborting the NAK'd + // transfer to allow other transfers to happen on the one shared + // epx endpoint. + // TODO for RP2040 USB shared endpoint: usbh_edpt_clear_in_on_nak(p_midi_host->dev_addr, p_midi_host->ep_in); + } + return result; +} + +uint32_t tuh_midi_stream_write (uint8_t dev_addr, uint8_t cable_num, uint8_t const* buffer, uint32_t bufsize) +{ + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + TU_VERIFY(p_midi_host != NULL); + TU_VERIFY(cable_num < p_midi_host->num_cables_tx); + midi_stream_t *stream = &p_midi_host->stream_write; + + uint32_t i = 0; + while ( (i < bufsize) && (tu_fifo_remaining(&p_midi_host->tx_ff) >= 4) ) + { + uint8_t const data = buffer[i]; + i++; + if (data >= MIDI_STATUS_SYSREAL_TIMING_CLOCK) + { + // real-time messages need to be sent right away + midi_stream_t streamrt; + streamrt.buffer[0] = MIDI_CIN_SYSEX_END_1BYTE; + streamrt.buffer[1] = data; + streamrt.index = 2; + streamrt.total = 2; + uint16_t const count = tu_fifo_write_n(&p_midi_host->tx_ff, streamrt.buffer, 4); + // FIFO overflown, since we already check fifo remaining. It is probably race condition + TU_ASSERT(count == 4, i); + } + else if ( stream->index == 0 ) + { + //------------- New event packet -------------// + + uint8_t const msg = data >> 4; + + stream->index = 2; + stream->buffer[1] = data; + + // Check to see if we're still in a SysEx transmit. + if ( stream->buffer[0] == MIDI_CIN_SYSEX_START ) + { + if ( data == MIDI_STATUS_SYSEX_END ) + { + stream->buffer[0] = MIDI_CIN_SYSEX_END_1BYTE; + stream->total = 2; + } + else + { + stream->total = 4; + } + } + else if ( (msg >= 0x8 && msg <= 0xB) || msg == 0xE ) + { + // Channel Voice Messages + stream->buffer[0] = (cable_num << 4) | msg; + stream->total = 4; + } + else if ( msg == 0xC || msg == 0xD) + { + // Channel Voice Messages, two-byte variants (Program Change and Channel Pressure) + stream->buffer[0] = (cable_num << 4) | msg; + stream->total = 3; + } + else if ( msg == 0xf ) + { + // System message + if ( data == MIDI_STATUS_SYSEX_START ) + { + stream->buffer[0] = MIDI_CIN_SYSEX_START; + stream->total = 4; + } + else if ( data == MIDI_STATUS_SYSCOM_TIME_CODE_QUARTER_FRAME || data == MIDI_STATUS_SYSCOM_SONG_SELECT ) + { + stream->buffer[0] = MIDI_CIN_SYSCOM_2BYTE; + stream->total = 3; + } + else if ( data == MIDI_STATUS_SYSCOM_SONG_POSITION_POINTER ) + { + stream->buffer[0] = MIDI_CIN_SYSCOM_3BYTE; + stream->total = 4; + } + else + { + stream->buffer[0] = MIDI_CIN_SYSEX_END_1BYTE; + stream->total = 2; + } + } + else + { + // Pack individual bytes if we don't support packing them into words. + stream->buffer[0] = cable_num << 4 | 0xf; + stream->buffer[2] = 0; + stream->buffer[3] = 0; + stream->index = 2; + stream->total = 2; + } + } + else + { + //------------- On-going (buffering) packet -------------// + + TU_ASSERT(stream->index < 4, i); + stream->buffer[stream->index] = data; + stream->index++; + // See if this byte ends a SysEx. + if ( stream->buffer[0] == MIDI_CIN_SYSEX_START && data == MIDI_STATUS_SYSEX_END ) + { + stream->buffer[0] = MIDI_CIN_SYSEX_START + (stream->index - 1); + stream->total = stream->index; + } + } + + // Send out packet + if ( stream->index >= 2 && stream->index == stream->total ) + { + // zeroes unused bytes + for(uint8_t idx = stream->total; idx < 4; idx++) stream->buffer[idx] = 0; + TU_LOG3_MEM(stream->buffer, 4, 2); + + uint16_t const count = tu_fifo_write_n(&p_midi_host->tx_ff, stream->buffer, 4); + + // complete current event packet, reset stream + stream->index = 0; + stream->total = 0; + + // FIFO overflown, since we already check fifo remaining. It is probably race condition + TU_ASSERT(count == 4, i); + } + } + return i; +} + +bool tuh_midi_packet_write (uint8_t dev_addr, uint8_t const packet[4]) +{ + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + TU_VERIFY(p_midi_host != NULL); + + if (tu_fifo_remaining(&p_midi_host->tx_ff) < 4) + { + return false; + } + + tu_fifo_write_n(&p_midi_host->tx_ff, packet, 4); + + return true; +} + +uint32_t tuh_midi_stream_flush( uint8_t dev_addr ) +{ + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + TU_VERIFY(p_midi_host != NULL); + + uint32_t bytes_flushed = 0; + if (!usbh_edpt_busy(p_midi_host->dev_addr, p_midi_host->ep_out)) + { + bytes_flushed = write_flush(dev_addr, p_midi_host); + } + return bytes_flushed; +} +//--------------------------------------------------------------------+ +// Helper +//--------------------------------------------------------------------+ +uint8_t tuh_midih_get_num_tx_cables (uint8_t dev_addr) +{ + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + TU_VERIFY(p_midi_host != NULL); + TU_VERIFY(p_midi_host->ep_out != 0); // returns 0 if fails + return p_midi_host->num_cables_tx; +} + +uint8_t tuh_midih_get_num_rx_cables (uint8_t dev_addr) +{ + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + TU_VERIFY(p_midi_host != NULL); + TU_VERIFY(p_midi_host->ep_in != 0); // returns 0 if fails + return p_midi_host->num_cables_rx; +} + +bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]) +{ + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + TU_VERIFY(p_midi_host != NULL); + TU_VERIFY(tu_fifo_count(&p_midi_host->rx_ff) >= 4); + return tu_fifo_read_n(&p_midi_host->rx_ff, packet, 4) == 4; +} + +uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p_buffer, uint16_t bufsize) +{ + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + TU_VERIFY(p_midi_host != NULL); + uint32_t bytes_buffered = 0; + TU_ASSERT(p_cable_num); + TU_ASSERT(p_buffer); + TU_ASSERT(bufsize); + uint8_t one_byte; + if (!tu_fifo_peek(&p_midi_host->rx_ff, &one_byte)) + { + return 0; + } + *p_cable_num = (one_byte >> 4) & 0xf; + uint32_t nread = tu_fifo_read_n(&p_midi_host->rx_ff, p_midi_host->stream_read.buffer, 4); + static uint16_t cable_sysex_in_progress; // bit i is set if received MIDI_STATUS_SYSEX_START but not MIDI_STATUS_SYSEX_END + while (nread == 4 && bytes_buffered < bufsize) + { + *p_cable_num=(p_midi_host->stream_read.buffer[0] >> 4) & 0x0f; + uint8_t bytes_to_add_to_stream = 0; + if (*p_cable_num < p_midi_host->num_cables_rx) + { + // ignore the CIN field; too many devices out there encode this wrong + uint8_t status = p_midi_host->stream_read.buffer[1]; + uint16_t cable_mask = 1 << *p_cable_num; + if (status <= MIDI_MAX_DATA_VAL || status == MIDI_STATUS_SYSEX_START) + { + if (status == MIDI_STATUS_SYSEX_START) + { + cable_sysex_in_progress |= cable_mask; + } + // only add the packet if a sysex message is in progress + if (cable_sysex_in_progress & cable_mask) + { + ++bytes_to_add_to_stream; + uint8_t idx; + for (idx = 2; idx < 4; idx++) + { + if (p_midi_host->stream_read.buffer[idx] <= MIDI_MAX_DATA_VAL) + { + ++bytes_to_add_to_stream; + } + else if (p_midi_host->stream_read.buffer[idx] == MIDI_STATUS_SYSEX_END) + { + ++bytes_to_add_to_stream; + cable_sysex_in_progress &= ~cable_mask; + idx = 4; // force the loop to exit; I hate break statements in loops + } + } + } + } + else if (status < MIDI_STATUS_SYSEX_START) + { + // then it is a channel message either three bytes or two + uint8_t fake_cin = (status & 0xf0) >> 4; + switch (fake_cin) + { + case MIDI_CIN_NOTE_OFF: + case MIDI_CIN_NOTE_ON: + case MIDI_CIN_POLY_KEYPRESS: + case MIDI_CIN_CONTROL_CHANGE: + case MIDI_CIN_PITCH_BEND_CHANGE: + bytes_to_add_to_stream = 3; + break; + case MIDI_CIN_PROGRAM_CHANGE: + case MIDI_CIN_CHANNEL_PRESSURE: + bytes_to_add_to_stream = 2; + break; + default: + break; // Should not get this + } + cable_sysex_in_progress &= ~cable_mask; + } + else if (status < MIDI_STATUS_SYSREAL_TIMING_CLOCK) + { + switch (status) + { + case MIDI_STATUS_SYSCOM_TIME_CODE_QUARTER_FRAME: + case MIDI_STATUS_SYSCOM_SONG_SELECT: + bytes_to_add_to_stream = 2; + break; + case MIDI_STATUS_SYSCOM_SONG_POSITION_POINTER: + bytes_to_add_to_stream = 3; + break; + case MIDI_STATUS_SYSCOM_TUNE_REQUEST: + case MIDI_STATUS_SYSEX_END: + bytes_to_add_to_stream = 1; + break; + default: + break; + cable_sysex_in_progress &= ~cable_mask; + } + } + else + { + // Real-time message: can be inserted into a sysex message, + // so do don't clear cable_sysex_in_progress bit + bytes_to_add_to_stream = 1; + } + } + uint8_t idx; + for (idx = 1; idx <= bytes_to_add_to_stream; idx++) + { + *p_buffer++ = p_midi_host->stream_read.buffer[idx]; + } + bytes_buffered += bytes_to_add_to_stream; + nread = 0; + if (tu_fifo_peek(&p_midi_host->rx_ff, &one_byte)) + { + uint8_t new_cable = (one_byte >> 4) & 0xf; + if (new_cable == *p_cable_num) + { + // still on the same cable. Continue reading the stream + nread = tu_fifo_read_n(&p_midi_host->rx_ff, p_midi_host->stream_read.buffer, 4); + } + } + } + + return bytes_buffered; +} + +uint8_t tuh_midi_get_num_rx_cables(uint8_t dev_addr) +{ + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + TU_VERIFY(p_midi_host != NULL); + uint8_t num_cables = 0; + if (p_midi_host) + { + num_cables = p_midi_host->num_cables_rx; + } + return num_cables; +} + +uint8_t tuh_midi_get_num_tx_cables(uint8_t dev_addr) +{ + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + TU_VERIFY(p_midi_host != NULL); + uint8_t num_cables = 0; + if (p_midi_host) + { + num_cables = p_midi_host->num_cables_tx; + } + return num_cables; +} + +#if CFG_MIDI_HOST_DEVSTRINGS +static uint8_t find_string_index(midih_interface_t *ptr, uint8_t jack_id) +{ + uint8_t index = 0; + uint8_t assoc; + for (assoc = 0; index == 0 && assoc < ptr->next_in_jack; assoc++) + { + if (jack_id == ptr->in_jack_info[assoc].jack_id) + { + index = ptr->in_jack_info[assoc].string_index; + } + } + for (assoc = 0; index == 0 && assoc < ptr->next_out_jack; assoc++) + { + if (jack_id == ptr->out_jack_info[assoc].jack_id) + { + index = ptr->out_jack_info[assoc].string_index; + } + } + return index; +} +#endif + +#if CFG_MIDI_HOST_DEVSTRINGS +uint8_t tuh_midi_get_rx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings) +{ + uint8_t nstrings = 0; + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + TU_VERIFY(p_midi_host != NULL); + nstrings = p_midi_host->num_cables_rx; + if (nstrings > max_istrings) + { + nstrings = max_istrings; + } + uint8_t jack; + for (jack=0; jackep_in_associated_jacks[jack]; + istrings[jack] = find_string_index(p_midi_host, jack_id); + } + return nstrings; +} + +uint8_t tuh_midi_get_tx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings) +{ + uint8_t nstrings = 0; + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + TU_VERIFY(p_midi_host != NULL); + nstrings = p_midi_host->num_cables_tx; + if (nstrings > max_istrings) + { + nstrings = max_istrings; + } + uint8_t jack; + for (jack=0; jackep_out_associated_jacks[jack]; + istrings[jack] = find_string_index(p_midi_host, jack_id); + } + return nstrings; +} +#endif + +uint8_t tuh_midi_get_all_istrings(uint8_t dev_addr, const uint8_t** istrings) +{ + midih_interface_t *p_midi_host = get_midi_host(dev_addr); + TU_VERIFY(p_midi_host != NULL); + uint8_t nstrings = p_midi_host->num_string_indices; + if (nstrings) + *istrings = p_midi_host->all_string_indices; + return nstrings; +} +#endif diff --git a/src/class/midi/midi_host.h b/src/class/midi/midi_host.h new file mode 100644 index 000000000..fc661939a --- /dev/null +++ b/src/class/midi/midi_host.h @@ -0,0 +1,137 @@ +/* + * 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. + */ + +#ifndef _TUSB_MIDI_HOST_H_ +#define _TUSB_MIDI_HOST_H_ + +#include "class/audio/audio.h" +#include "midi.h" + +#ifdef __cplusplus + extern "C" { +#endif + +//--------------------------------------------------------------------+ +// Class Driver Configuration +//--------------------------------------------------------------------+ + +// TODO Highspeed bulk transfer can be up to 512 bytes +#ifndef CFG_TUH_HID_EPIN_BUFSIZE +#define CFG_TUH_HID_EPIN_BUFSIZE 64 +#endif + +#ifndef CFG_TUH_HID_EPOUT_BUFSIZE +#define CFG_TUH_HID_EPOUT_BUFSIZE 64 +#endif + + +//--------------------------------------------------------------------+ +// Application API (Single Interface) +//--------------------------------------------------------------------+ +bool tuh_midi_configured (uint8_t dev_addr); +uint32_t tuh_midi_available (uint8_t dev_addr); + +// return the number of virtual midi cables on the device's OUT endpoint +uint8_t tuh_midih_get_num_tx_cables (uint8_t dev_addr); + +// return the number of virtual midi cables on the device's IN endpoint +uint8_t tuh_midih_get_num_rx_cables (uint8_t dev_addr); + +// request available data from the device. tuh_midi_message_received_cb() will +// be called if the device has any data to send. Otherwise, the device will +// respond NAK. This function blocks until the transfer completes or the +// devices sends NAK. +// This function will return false if the hardware is busy. +bool tuh_midi_read_poll( uint8_t dev_addr ); + +// Queue a packet to the device. The application +// must call tuh_midi_stream_flush to actually have the +// data go out. It is up to the application to properly +// format this packet; this function does not check. +// Returns true if the packet was successfully queued. +bool tuh_midi_packet_write (uint8_t dev_addr, uint8_t const packet[4]); + +// Queue a message to the device. The application +// must call tuh_midi_stream_flush to actually have the +// data go out. +uint32_t tuh_midi_stream_write (uint8_t dev_addr, uint8_t cable_num, uint8_t const* p_buffer, uint32_t bufsize); + +// Send any queued packets to the device if the host hardware is able to do it +// Returns the number of bytes flushed to the host hardware or 0 if +// the host hardware is busy or there is nothing in queue to send. +uint32_t tuh_midi_stream_flush( uint8_t dev_addr); + +// Get the MIDI stream from the device. Set the value pointed +// to by p_cable_num to the MIDI cable number intended to receive it. +// The MIDI stream will be stored in the buffer pointed to by p_buffer. +// Return the number of bytes added to the buffer. +// Note that this function ignores the CIN field of the MIDI packet +// because a number of commercial devices out there do not encode +// it properly. +uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p_buffer, uint16_t bufsize); + +// Read a raw MIDI packet from the connected device +// This function does not parse the packet format +// Return true if a packet was returned +bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]); + +uint8_t tuh_midi_get_num_rx_cables(uint8_t dev_addr); +uint8_t tuh_midi_get_num_tx_cables(uint8_t dev_addr); +#if CFG_MIDI_HOST_DEVSTRINGS +uint8_t tuh_midi_get_rx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings); +uint8_t tuh_midi_get_tx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings); +uint8_t tuh_midi_get_all_istrings(uint8_t dev_addr, const uint8_t** istrings); +#endif +//--------------------------------------------------------------------+ +// Internal Class Driver API +//--------------------------------------------------------------------+ +void midih_init (void); +bool midih_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len); +bool midih_set_config (uint8_t dev_addr, uint8_t itf_num); +bool midih_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); +void midih_close (uint8_t dev_addr); + +//--------------------------------------------------------------------+ +// Callbacks (Weak is optional) +//--------------------------------------------------------------------+ + +// Invoked when device with MIDI interface is mounted. +// If the MIDI host application requires MIDI IN, it should requst an +// IN transfer here. The device will likely NAK this transfer. How the driver +// handles the NAK is hardware dependent. +TU_ATTR_WEAK void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t in_ep, uint8_t out_ep, uint8_t num_cables_rx, uint16_t num_cables_tx); + +// Invoked when device with MIDI interface is un-mounted +// For now, the instance parameter is always 0 and can be ignored +TU_ATTR_WEAK void tuh_midi_umount_cb(uint8_t dev_addr, uint8_t instance); + +TU_ATTR_WEAK void tuh_midi_rx_cb(uint8_t dev_addr, uint32_t num_packets); +TU_ATTR_WEAK void tuh_midi_tx_cb(uint8_t dev_addr); +#ifdef __cplusplus +} +#endif + +#endif /* _TUSB_MIDI_HOST_H_ */ diff --git a/src/host/hcd.h b/src/host/hcd.h index deebc59d4..97b668d79 100644 --- a/src/host/hcd.h +++ b/src/host/hcd.h @@ -40,7 +40,7 @@ //--------------------------------------------------------------------+ #ifndef CFG_TUH_ENDPOINT_MAX - #define CFG_TUH_ENDPOINT_MAX (CFG_TUH_HUB + CFG_TUH_HID*2 + CFG_TUH_MSC*2 + CFG_TUH_CDC*3) + #define CFG_TUH_ENDPOINT_MAX (CFG_TUH_HUB + CFG_TUH_HID*2 + CFG_TUH_MSC*2 + CFG_TUH_CDC*3 + CFG_TUH_MIDI*2) // #ifdef TUP_HCD_ENDPOINT_MAX // #define CFG_TUH_ENDPPOINT_MAX TUP_HCD_ENDPOINT_MAX // #else diff --git a/src/host/usbh.c b/src/host/usbh.c index 9d618db92..c59f31596 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -166,6 +166,17 @@ static usbh_class_driver_t const usbh_class_drivers[] = }, #endif + #if CFG_TUH_MIDI + { + DRIVER_NAME("MIDI") + .init = midih_init, + .open = midih_open, + .set_config = midih_set_config, + .xfer_cb = midih_xfer_cb, + .close = midih_close + }, + #endif + #if CFG_TUH_HUB { DRIVER_NAME("HUB") diff --git a/src/tusb.h b/src/tusb.h index b776d7d01..093c053f7 100644 --- a/src/tusb.h +++ b/src/tusb.h @@ -56,6 +56,10 @@ #include "class/cdc/cdc_host.h" #endif + #if CFG_TUH_MIDI + #include "class/midi/midi_host.h" + #endif + #if CFG_TUH_VENDOR #include "class/vendor/vendor_host.h" #endif From 70eefcb849542f386a1349da7a4944d2d02dcd40 Mon Sep 17 00:00:00 2001 From: rppicomidi Date: Thu, 25 Aug 2022 15:08:00 -0700 Subject: [PATCH 02/30] Add hooks to allow cloning an attached USB device descriptor --- src/device/usbd_control.c | 2 +- src/host/usbh.c | 7 +++++-- src/host/usbh.h | 6 +++++- src/tusb_option.h | 6 +++--- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/device/usbd_control.c b/src/device/usbd_control.c index 0995ef669..187547ae3 100644 --- a/src/device/usbd_control.c +++ b/src/device/usbd_control.c @@ -56,7 +56,7 @@ typedef struct static usbd_control_xfer_t _ctrl_xfer; CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN -static uint8_t _usbd_ctrl_buf[CFG_TUD_ENDPOINT0_SIZE]; +static uint8_t _usbd_ctrl_buf[/*CFG_TUD_ENDPOINT0_SIZE*/ 64]; //--------------------------------------------------------------------+ // Application API diff --git a/src/host/usbh.c b/src/host/usbh.c index c59f31596..44b4d3217 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -1323,7 +1323,7 @@ static void process_enumeration(tuh_xfer_t* xfer) dev->i_product = desc_device->iProduct; dev->i_serial = desc_device->iSerialNumber; - // if (tuh_attach_cb) tuh_attach_cb((tusb_desc_device_t*) _usbh_ctrl_buf); + if (tuh_desc_device_cb) tuh_desc_device_cb(daddr, (tusb_desc_device_t const*) _usbh_ctrl_buf); // Get 9-byte for total length uint8_t const config_idx = CONFIG_NUM - 1; @@ -1350,9 +1350,12 @@ static void process_enumeration(tuh_xfer_t* xfer) break; case ENUM_SET_CONFIG: + // Got the whole configuration descriptor. Make a copy + if (tuh_desc_config_cb) tuh_desc_config_cb(daddr, (const tusb_desc_configuration_t*) _usbh_ctrl_buf); + // Parse configuration & set up drivers // Driver open aren't allowed to make any usb transfer yet - TU_ASSERT( _parse_configuration_descriptor(daddr, (tusb_desc_configuration_t*) _usbh_ctrl_buf), ); + TU_ASSERT( _parse_configuration_descriptor(daddr, (const tusb_desc_configuration_t* ) _usbh_ctrl_buf), ); TU_ASSERT( tuh_configuration_set(daddr, CONFIG_NUM, process_enumeration, ENUM_CONFIG_DRIVER), ); break; diff --git a/src/host/usbh.h b/src/host/usbh.h index 560a1ea23..7355c83ee 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -78,7 +78,11 @@ enum // APPLICATION CALLBACK //--------------------------------------------------------------------+ -//TU_ATTR_WEAK uint8_t tuh_attach_cb (tusb_desc_device_t const *desc_device); +// Give the application an opportunity to grab the device descriptor +TU_ATTR_WEAK void tuh_desc_device_cb(uint8_t daddr, const tusb_desc_device_t *desc_device); + +// Give the application an opportunity to grab the configuration descriptor +TU_ATTR_WEAK void tuh_desc_config_cb(uint8_t daddr, const tusb_desc_configuration_t *desc_config); // Invoked when device is mounted (configured) TU_ATTR_WEAK void tuh_mount_cb (uint8_t daddr); diff --git a/src/tusb_option.h b/src/tusb_option.h index f95ae6273..b905fc7ce 100644 --- a/src/tusb_option.h +++ b/src/tusb_option.h @@ -427,9 +427,9 @@ typedef int make_iso_compilers_happy; //------------------------------------------------------------------ // Configuration Validation //------------------------------------------------------------------ -#if CFG_TUD_ENDPOINT0_SIZE > 64 - #error Control Endpoint Max Packet Size cannot be larger than 64 -#endif +//#if CFG_TUD_ENDPOINT0_SIZE > 64 +// #error Control Endpoint Max Packet Size cannot be larger than 64 +//#endif #endif /* _TUSB_OPTION_H_ */ From 3325e2613d4bc68ac2443445e62520baa4bbe6cb Mon Sep 17 00:00:00 2001 From: rppicomidi Date: Thu, 25 Aug 2022 15:09:27 -0700 Subject: [PATCH 03/30] Make USB MIDI device code allow a device with no Audio Control interface --- src/class/midi/midi_device.c | 26 ++++++++++++++++---------- src/device/usbd.c | 10 +++++++++- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/class/midi/midi_device.c b/src/class/midi/midi_device.c index de41706e8..c6c99db29 100644 --- a/src/class/midi/midi_device.c +++ b/src/class/midi/midi_device.c @@ -406,19 +406,25 @@ void midid_reset(uint8_t rhport) uint16_t midid_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t max_len) { + uint16_t drv_len = 0; + uint8_t const * p_desc = (uint8_t const *)desc_itf; // 1st Interface is Audio Control v1 - TU_VERIFY(TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass && + if (TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass && AUDIO_SUBCLASS_CONTROL == desc_itf->bInterfaceSubClass && - AUDIO_FUNC_PROTOCOL_CODE_UNDEF == desc_itf->bInterfaceProtocol, 0); - - uint16_t drv_len = tu_desc_len(desc_itf); - uint8_t const * 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 ) + AUDIO_FUNC_PROTOCOL_CODE_UNDEF == desc_itf->bInterfaceProtocol) { - drv_len += tu_desc_len(p_desc); - p_desc = tu_desc_next(p_desc); + 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 + { + TU_LOG1("Warning: MIDI Device has no Audio Control Interface"); } // 2nd Interface is MIDI Streaming diff --git a/src/device/usbd.c b/src/device/usbd.c index c199e647e..1cede1289 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -920,7 +920,15 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num) #endif #if CFG_TUD_MIDI - if ( driver->open == midid_open ) assoc_itf_count = 2; + if ( driver->open == midid_open ) + { + // If there is a class-compliant Audio Control Class, then 2 interfaces + // Otherwise, only one + if (TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass && + AUDIO_SUBCLASS_CONTROL == desc_itf->bInterfaceSubClass && + AUDIO_FUNC_PROTOCOL_CODE_UNDEF == desc_itf->bInterfaceProtocol) + assoc_itf_count = 2; + } #endif #if CFG_TUD_BTH && CFG_TUD_BTH_ISO_ALT_COUNT From 4fbf99621d5a5b8b5de698a792578a2066c667f3 Mon Sep 17 00:00:00 2001 From: atok Date: Thu, 8 Sep 2022 17:51:01 +0200 Subject: [PATCH 04/30] Get rid of implicit uint conversions --- src/class/midi/midi_host.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index e482f5265..a6ad1303a 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -100,7 +100,7 @@ typedef struct #define MAX_OUT_JACKS 8 struct { uint8_t jack_id; - int8_t jack_type; + uint8_t jack_type; uint8_t string_index; } in_jack_info[MAX_IN_JACKS]; uint8_t next_in_jack; From 19563b441463aed9b3d622f2d57d61082199fbdd Mon Sep 17 00:00:00 2001 From: atok Date: Thu, 8 Sep 2022 21:13:55 +0200 Subject: [PATCH 05/30] Simple MIDI rx example --- examples/host/CMakeLists.txt | 1 + examples/host/midi_rx/CMakeLists.txt | 27 +++++ examples/host/midi_rx/only.txt | 1 + examples/host/midi_rx/src/main.c | 152 ++++++++++++++++++++++++ examples/host/midi_rx/src/tusb_config.h | 96 +++++++++++++++ src/class/midi/midi_host.c | 4 +- 6 files changed, 279 insertions(+), 2 deletions(-) create mode 100644 examples/host/midi_rx/CMakeLists.txt create mode 100644 examples/host/midi_rx/only.txt create mode 100644 examples/host/midi_rx/src/main.c create mode 100644 examples/host/midi_rx/src/tusb_config.h diff --git a/examples/host/CMakeLists.txt b/examples/host/CMakeLists.txt index 5c63ec0c0..39a41c4ae 100644 --- a/examples/host/CMakeLists.txt +++ b/examples/host/CMakeLists.txt @@ -8,3 +8,4 @@ family_initialize_project(tinyusb_host_examples ${CMAKE_CURRENT_LIST_DIR}) # family_add_subdirectory will filter what to actually add based on selected FAMILY family_add_subdirectory(cdc_msc_hid) family_add_subdirectory(hid_controller) +family_add_subdirectory(midi_rx) \ No newline at end of file diff --git a/examples/host/midi_rx/CMakeLists.txt b/examples/host/midi_rx/CMakeLists.txt new file mode 100644 index 000000000..4856e0a47 --- /dev/null +++ b/examples/host/midi_rx/CMakeLists.txt @@ -0,0 +1,27 @@ +cmake_minimum_required(VERSION 3.5) + +include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake) + +# gets PROJECT name for the example (e.g. -) +family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR}) + +project(${PROJECT}) + +# Checks this example is valid for the family and initializes the project +family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR}) + +add_executable(${PROJECT}) + +# Example source +target_sources(${PROJECT} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c + ) + +# Example include +target_include_directories(${PROJECT} PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/src + ) + +# Configure compilation flags and libraries for the example... see the corresponding function +# in hw/bsp/FAMILY/family.cmake for details. +family_configure_host_example(${PROJECT}) \ No newline at end of file diff --git a/examples/host/midi_rx/only.txt b/examples/host/midi_rx/only.txt new file mode 100644 index 000000000..2ba4438fd --- /dev/null +++ b/examples/host/midi_rx/only.txt @@ -0,0 +1 @@ +mcu:RP2040 diff --git a/examples/host/midi_rx/src/main.c b/examples/host/midi_rx/src/main.c new file mode 100644 index 000000000..64428932f --- /dev/null +++ b/examples/host/midi_rx/src/main.c @@ -0,0 +1,152 @@ +/* + * 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. + * + */ + +#include +#include +#include + +#include "bsp/board.h" +#include "tusb.h" +#include "class/midi/midi_host.h" + + +#if CFG_TUH_MIDI + +//--------------------------------------------------------------------+ +// STATIC GLOBALS DECLARATION +//--------------------------------------------------------------------+ +static uint8_t midi_dev_addr = 0; + +//--------------------------------------------------------------------+ +// MACRO CONSTANT TYPEDEF PROTYPES +//--------------------------------------------------------------------+ +void led_blinking_task(void); +void midi_host_rx_task(void); + +/*------------- MAIN -------------*/ +int main(void) +{ + board_init(); + + printf("TinyUSB Host MIDI Example\r\n"); + + tusb_init(); + + while (1) + { + tuh_task(); + led_blinking_task(); + midi_host_rx_task(); + } + + return 0; +} + +#endif + +//--------------------------------------------------------------------+ +// Blinking Task +//--------------------------------------------------------------------+ +void led_blinking_task(void) +{ + const uint32_t interval_ms = 1000; + static uint32_t start_ms = 0; + + static bool led_state = false; + + // Blink every interval ms + if ( board_millis() - start_ms < interval_ms) return; // not enough time + start_ms += interval_ms; + + board_led_write(led_state); + led_state = 1 - led_state; // toggle +} + +//--------------------------------------------------------------------+ +// MIDI host receive task +//--------------------------------------------------------------------+ +void midi_host_rx_task(void) +{ + // device must be attached and have at least one endpoint ready to receive a message + if (!midi_dev_addr || !tuh_midi_configured(midi_dev_addr)) + { + return; + } + if (tuh_midih_get_num_rx_cables(midi_dev_addr) < 1) + { + return; + } + tuh_midi_read_poll(midi_dev_addr); +} + +//--------------------------------------------------------------------+ +// TinyUSB Callbacks +//--------------------------------------------------------------------+ + +// Invoked when device with hid interface is mounted +// Report descriptor is also available for use. tuh_hid_parse_report_descriptor() +// can be used to parse common/simple enough descriptor. +// Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE, it will be skipped +// therefore report_desc = NULL, desc_len = 0 +void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t in_ep, uint8_t out_ep, uint8_t num_cables_rx, uint16_t num_cables_tx) +{ + TU_LOG1("MIDI device address = %u, IN endpoint %u has %u cables, OUT endpoint %u has %u cables\r\n", + dev_addr, in_ep & 0xf, num_cables_rx, out_ep & 0xf, num_cables_tx); + + midi_dev_addr = dev_addr; +} + +// Invoked when device with hid interface is un-mounted +void tuh_midi_umount_cb(uint8_t dev_addr, uint8_t instance) +{ + TU_LOG1("MIDI device address = %d, instance = %d is unmounted\r\n", dev_addr, instance); + + midi_dev_addr = 0; +} + +void tuh_midi_rx_cb(uint8_t dev_addr, uint32_t num_packets) +{ + if (midi_dev_addr != dev_addr) + { + return; + } + + if(num_packets == 0) + { + return; + } + + uint8_t cable_num; + uint8_t buffer[48]; + uint32_t bytes_read = tuh_midi_stream_read(dev_addr, &cable_num, buffer, sizeof(buffer)); + + TU_LOG1("Read bytes %u cable %u", bytes_read, cable_num); + TU_LOG1_MEM(buffer, bytes_read, 2); +} + +void tuh_midi_tx_cb(uint8_t dev_addr) +{ + (void ) dev_addr; +} \ No newline at end of file diff --git a/examples/host/midi_rx/src/tusb_config.h b/examples/host/midi_rx/src/tusb_config.h new file mode 100644 index 000000000..5cc354ea9 --- /dev/null +++ b/examples/host/midi_rx/src/tusb_config.h @@ -0,0 +1,96 @@ +/* + * 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. + * + */ + +#ifndef _TUSB_CONFIG_H_ +#define _TUSB_CONFIG_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +//-------------------------------------------------------------------- +// COMMON CONFIGURATION +//-------------------------------------------------------------------- + +// defined by compiler flags for flexibility +#ifndef CFG_TUSB_MCU +#error CFG_TUSB_MCU must be defined +#endif + +#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX +#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | OPT_MODE_HIGH_SPEED) +#else +#define CFG_TUSB_RHPORT0_MODE OPT_MODE_HOST +#endif + +#ifndef CFG_TUSB_OS +#define CFG_TUSB_OS OPT_OS_NONE +#endif + +// CFG_TUSB_DEBUG is defined by compiler in DEBUG build +// #define CFG_TUSB_DEBUG 0 + +/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment. + * Tinyusb use follows macros to declare transferring memory so that they can be put + * into those specific section. + * e.g + * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") )) + * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4))) + */ +#ifndef CFG_TUSB_MEM_SECTION +#define CFG_TUSB_MEM_SECTION +#endif + +#ifndef CFG_TUSB_MEM_ALIGN +#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) +#endif + +//-------------------------------------------------------------------- +// CONFIGURATION +//-------------------------------------------------------------------- + +// Size of buffer to hold descriptors and other data used for enumeration +#define CFG_TUH_ENUMERATION_BUFSIZE 256 + +#define CFG_TUH_HUB 1 +#define CFG_TUH_CDC 0 +#define CFG_TUH_HID 0 // typical keyboard + mouse device can have 3-4 HID interfaces +#define CFG_TUH_MIDI 1 // there will be at most one MIDIStreaming Interface descriptor +#define CFG_TUH_MSC 0 +#define CFG_TUH_VENDOR 0 + +// max device support (excluding hub device) +#define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1) // hub typically has 4 ports +#define CFG_MIDI_HOST_DEVSTRINGS 1 + +//------------- HID -------------// +#define CFG_TUH_HID_EPIN_BUFSIZE 64 +#define CFG_TUH_HID_EPOUT_BUFSIZE 64 + +#ifdef __cplusplus +} +#endif + +#endif /* _TUSB_CONFIG_H_ */ diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index a6ad1303a..aafa340cd 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -333,10 +333,10 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d p_midi_host->out_jack_info[p_midi_host->next_out_jack].jack_id = p_mdoj->bJackID; p_midi_host->out_jack_info[p_midi_host->next_out_jack].jack_type = p_mdoj->bJackType; p_midi_host->out_jack_info[p_midi_host->next_out_jack].num_source_ids = p_mdoj->bNrInputPins; - struct associated_jack_s { + const struct associated_jack_s { uint8_t id; uint8_t pin; - } *associated_jack = (struct associated_jack_s *)(p_desc+6); + } *associated_jack = (const struct associated_jack_s *)(p_desc+6); int jack; for (jack = 0; jack < p_mdoj->bNrInputPins; jack++) { From 81de7f3d23ed148a661264e88e10b3bb7c1c0473 Mon Sep 17 00:00:00 2001 From: atok Date: Sun, 11 Sep 2022 18:17:22 +0200 Subject: [PATCH 06/30] Add Makefile for the midi host example --- examples/host/midi_rx/Makefile | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 examples/host/midi_rx/Makefile diff --git a/examples/host/midi_rx/Makefile b/examples/host/midi_rx/Makefile new file mode 100644 index 000000000..f830617ac --- /dev/null +++ b/examples/host/midi_rx/Makefile @@ -0,0 +1,9 @@ +include ../../../tools/top.mk +include ../../make.mk +INC += \ + src \ + $(TOP)/hw \ +# Example source +EXAMPLE_SOURCE += $(wildcard src/*.c) +SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE)) +include ../../rules.mk \ No newline at end of file From 0763bc59794f8d8b4850399a89766e44ddcb742b Mon Sep 17 00:00:00 2001 From: atok Date: Sun, 11 Sep 2022 18:18:10 +0200 Subject: [PATCH 07/30] Fix printf statement --- examples/host/midi_rx/src/main.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/host/midi_rx/src/main.c b/examples/host/midi_rx/src/main.c index 64428932f..c12df33f2 100644 --- a/examples/host/midi_rx/src/main.c +++ b/examples/host/midi_rx/src/main.c @@ -122,7 +122,6 @@ void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t in_ep, uint8_t out_ep, uint8_t void tuh_midi_umount_cb(uint8_t dev_addr, uint8_t instance) { TU_LOG1("MIDI device address = %d, instance = %d is unmounted\r\n", dev_addr, instance); - midi_dev_addr = 0; } @@ -142,7 +141,7 @@ void tuh_midi_rx_cb(uint8_t dev_addr, uint32_t num_packets) uint8_t buffer[48]; uint32_t bytes_read = tuh_midi_stream_read(dev_addr, &cable_num, buffer, sizeof(buffer)); - TU_LOG1("Read bytes %u cable %u", bytes_read, cable_num); + TU_LOG1("Read bytes %lu cable %u", bytes_read, cable_num); TU_LOG1_MEM(buffer, bytes_read, 2); } From c14e3e25019b0487f84125b42f682c7dae484578 Mon Sep 17 00:00:00 2001 From: atok Date: Sun, 11 Sep 2022 18:33:43 +0200 Subject: [PATCH 08/30] Fix unused errors when LOG=0 --- examples/host/midi_rx/src/main.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/examples/host/midi_rx/src/main.c b/examples/host/midi_rx/src/main.c index c12df33f2..6fe17255a 100644 --- a/examples/host/midi_rx/src/main.c +++ b/examples/host/midi_rx/src/main.c @@ -112,6 +112,11 @@ void midi_host_rx_task(void) // therefore report_desc = NULL, desc_len = 0 void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t in_ep, uint8_t out_ep, uint8_t num_cables_rx, uint16_t num_cables_tx) { + (void ) in_ep; + (void ) out_ep; + (void ) num_cables_rx; + (void ) num_cables_tx; + TU_LOG1("MIDI device address = %u, IN endpoint %u has %u cables, OUT endpoint %u has %u cables\r\n", dev_addr, in_ep & 0xf, num_cables_rx, out_ep & 0xf, num_cables_tx); @@ -121,6 +126,9 @@ void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t in_ep, uint8_t out_ep, uint8_t // Invoked when device with hid interface is un-mounted void tuh_midi_umount_cb(uint8_t dev_addr, uint8_t instance) { + (void ) dev_addr; + (void ) instance; + TU_LOG1("MIDI device address = %d, instance = %d is unmounted\r\n", dev_addr, instance); midi_dev_addr = 0; } @@ -140,6 +148,7 @@ void tuh_midi_rx_cb(uint8_t dev_addr, uint32_t num_packets) uint8_t cable_num; uint8_t buffer[48]; uint32_t bytes_read = tuh_midi_stream_read(dev_addr, &cable_num, buffer, sizeof(buffer)); + (void ) bytes_read; TU_LOG1("Read bytes %lu cable %u", bytes_read, cable_num); TU_LOG1_MEM(buffer, bytes_read, 2); From a6d4b64beccc586d1bfb6e4c171d560762221645 Mon Sep 17 00:00:00 2001 From: atok Date: Sun, 2 Oct 2022 09:53:37 +0200 Subject: [PATCH 09/30] Removed unused function prototype --- src/class/midi/midi_host.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/class/midi/midi_host.h b/src/class/midi/midi_host.h index fc661939a..8180e3309 100644 --- a/src/class/midi/midi_host.h +++ b/src/class/midi/midi_host.h @@ -52,7 +52,6 @@ // Application API (Single Interface) //--------------------------------------------------------------------+ bool tuh_midi_configured (uint8_t dev_addr); -uint32_t tuh_midi_available (uint8_t dev_addr); // return the number of virtual midi cables on the device's OUT endpoint uint8_t tuh_midih_get_num_tx_cables (uint8_t dev_addr); From 21e003af2234b3211fb9b4da42202c79949e4caa Mon Sep 17 00:00:00 2001 From: atoktoto Date: Sun, 13 Nov 2022 14:38:33 +0100 Subject: [PATCH 10/30] Added explicit conversions --- src/class/midi/midi_host.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index aafa340cd..d242d33c5 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -592,13 +592,13 @@ uint32_t tuh_midi_stream_write (uint8_t dev_addr, uint8_t cable_num, uint8_t con else if ( (msg >= 0x8 && msg <= 0xB) || msg == 0xE ) { // Channel Voice Messages - stream->buffer[0] = (cable_num << 4) | msg; + stream->buffer[0] = (uint8_t) ((cable_num << 4) | msg); stream->total = 4; } else if ( msg == 0xC || msg == 0xD) { // Channel Voice Messages, two-byte variants (Program Change and Channel Pressure) - stream->buffer[0] = (cable_num << 4) | msg; + stream->buffer[0] = (uint8_t) ((cable_num << 4) | msg); stream->total = 3; } else if ( msg == 0xf ) @@ -628,7 +628,7 @@ uint32_t tuh_midi_stream_write (uint8_t dev_addr, uint8_t cable_num, uint8_t con else { // Pack individual bytes if we don't support packing them into words. - stream->buffer[0] = cable_num << 4 | 0xf; + stream->buffer[0] = (uint8_t) (cable_num << 4 | 0xf); stream->buffer[2] = 0; stream->buffer[3] = 0; stream->index = 2; @@ -748,7 +748,7 @@ uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t * { // ignore the CIN field; too many devices out there encode this wrong uint8_t status = p_midi_host->stream_read.buffer[1]; - uint16_t cable_mask = 1 << *p_cable_num; + uint16_t cable_mask = (uint16_t) (1 << *p_cable_num); if (status <= MIDI_MAX_DATA_VAL || status == MIDI_STATUS_SYSEX_START) { if (status == MIDI_STATUS_SYSEX_START) From 0a844f2a3019df04a346232b0e54a9fc89e4c551 Mon Sep 17 00:00:00 2001 From: atoktoto Date: Sat, 19 Nov 2022 18:08:31 +0100 Subject: [PATCH 11/30] Fix usb hubs --- src/class/midi/midi_host.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index d242d33c5..bc87898cc 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -490,7 +490,7 @@ bool midih_set_config(uint8_t dev_addr, uint8_t itf_num) p_midi_host->configured = true; // TODO I don't think there are any special config things to do for MIDI - + usbh_driver_set_config_complete(dev_addr, p_midi_host->itf_num); return true; } From 34729b1dc46ef9cee3446e22c88e8484395f1fa7 Mon Sep 17 00:00:00 2001 From: atoktoto Date: Mon, 21 Nov 2022 10:29:26 +0100 Subject: [PATCH 12/30] Fix usb-hub data transfer problems by AndrewCapon --- src/class/midi/midi_host.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index bc87898cc..8df1b2005 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -529,7 +529,7 @@ bool tuh_midi_read_poll( uint8_t dev_addr ) if (in_edpt_not_busy) { TU_LOG2("Requesting poll IN endpoint %d\r\n", p_midi_host->ep_in); - TU_ASSERT(usbh_edpt_xfer(p_midi_host->dev_addr, p_midi_host->ep_in, _midi_host->epin_buf, _midi_host->ep_in_max), 0); + TU_ASSERT(usbh_edpt_xfer(p_midi_host->dev_addr, p_midi_host->ep_in, p_midi_host->epin_buf, p_midi_host->ep_in_max), 0); result = true; } else From 3ba03110782747dead880f334f4fd65c03cbca9d Mon Sep 17 00:00:00 2001 From: atoktoto Date: Mon, 21 Nov 2022 12:36:50 +0100 Subject: [PATCH 13/30] Explicit type conversion --- src/class/midi/midi_host.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index 8df1b2005..308120ea2 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -769,7 +769,7 @@ uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t * else if (p_midi_host->stream_read.buffer[idx] == MIDI_STATUS_SYSEX_END) { ++bytes_to_add_to_stream; - cable_sysex_in_progress &= ~cable_mask; + cable_sysex_in_progress &= (uint16_t) ~cable_mask; idx = 4; // force the loop to exit; I hate break statements in loops } } From 89eac75085134d570dc26dffaf3d8f204aef5359 Mon Sep 17 00:00:00 2001 From: atoktoto Date: Mon, 21 Nov 2022 13:49:43 +0100 Subject: [PATCH 14/30] Explicit type conversion --- src/class/midi/midi_host.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index 308120ea2..32311556e 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -795,7 +795,7 @@ uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t * default: break; // Should not get this } - cable_sysex_in_progress &= ~cable_mask; + cable_sysex_in_progress &= (uint16_t)~cable_mask; } else if (status < MIDI_STATUS_SYSREAL_TIMING_CLOCK) { @@ -814,7 +814,7 @@ uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t * break; default: break; - cable_sysex_in_progress &= ~cable_mask; + cable_sysex_in_progress &= (uint16_t)~cable_mask; } } else From 85247e50ddc17347fca1f6c9b9fedb1227eda25f Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 12 Feb 2025 11:34:32 +0700 Subject: [PATCH 15/30] clean up --- hw/bsp/rp2040/family.cmake | 30 +++++++++++++++--------------- src/class/midi/midi_device.c | 2 +- src/device/usbd.c | 13 ++++++------- src/host/usbh.c | 8 ++++++-- src/tusb_option.h | 6 +++--- 5 files changed, 31 insertions(+), 28 deletions(-) diff --git a/hw/bsp/rp2040/family.cmake b/hw/bsp/rp2040/family.cmake index cf5295f22..1cbc0742c 100644 --- a/hw/bsp/rp2040/family.cmake +++ b/hw/bsp/rp2040/family.cmake @@ -100,21 +100,21 @@ target_sources(tinyusb_device_base INTERFACE ${TOP}/src/class/video/video_device.c ) - #------------------------------------ - # Base config for host mode; wrapped by SDK's tinyusb_host - #------------------------------------ - add_library(tinyusb_host_base INTERFACE) - target_sources(tinyusb_host_base INTERFACE - ${TOP}/src/portable/raspberrypi/rp2040/hcd_rp2040.c - ${TOP}/src/portable/raspberrypi/rp2040/rp2040_usb.c - ${TOP}/src/host/usbh.c - ${TOP}/src/host/hub.c - ${TOP}/src/class/cdc/cdc_host.c - ${TOP}/src/class/hid/hid_host.c - ${TOP}/src/class/midi/midi_host.c - ${TOP}/src/class/msc/msc_host.c - ${TOP}/src/class/vendor/vendor_host.c - ) +#------------------------------------ +# Base config for host mode; wrapped by SDK's tinyusb_host +#------------------------------------ +add_library(tinyusb_host_base INTERFACE) +target_sources(tinyusb_host_base INTERFACE + ${TOP}/src/portable/raspberrypi/rp2040/hcd_rp2040.c + ${TOP}/src/portable/raspberrypi/rp2040/rp2040_usb.c + ${TOP}/src/host/usbh.c + ${TOP}/src/host/hub.c + ${TOP}/src/class/cdc/cdc_host.c + ${TOP}/src/class/hid/hid_host.c + ${TOP}/src/class/midi/midi_host.c + ${TOP}/src/class/msc/msc_host.c + ${TOP}/src/class/vendor/vendor_host.c + ) # Sometimes have to do host specific actions in mostly common functions target_compile_definitions(tinyusb_host_base INTERFACE diff --git a/src/class/midi/midi_device.c b/src/class/midi/midi_device.c index 4ae8ea8fa..dd1883e37 100644 --- a/src/class/midi/midi_device.c +++ b/src/class/midi/midi_device.c @@ -440,7 +440,7 @@ uint16_t midid_open(uint8_t rhport, const tusb_desc_interface_t* desc_itf, uint1 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 ) { + 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); } diff --git a/src/device/usbd.c b/src/device/usbd.c index 20f803563..faf926855 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -1032,14 +1032,13 @@ static bool process_set_config(uint8_t rhport, uint8_t cfg_num) #endif #if CFG_TUD_MIDI - if ( driver->open == midid_open ) - { - // If there is a class-compliant Audio Control Class, then 2 interfaces - // Otherwise, only one + if (driver->open == midid_open) { + // If there is a class-compliant Audio Control Class, then 2 interfaces. Otherwise, only one if (TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass && - AUDIO_SUBCLASS_CONTROL == desc_itf->bInterfaceSubClass && - AUDIO_FUNC_PROTOCOL_CODE_UNDEF == desc_itf->bInterfaceProtocol) - assoc_itf_count = 2; + AUDIO_SUBCLASS_CONTROL == desc_itf->bInterfaceSubClass && + AUDIO_FUNC_PROTOCOL_CODE_UNDEF == desc_itf->bInterfaceProtocol) { + assoc_itf_count = 2; + } } #endif diff --git a/src/host/usbh.c b/src/host/usbh.c index 6f7a8b8bf..30a4f5f63 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -1488,7 +1488,9 @@ static void process_enumeration(tuh_xfer_t* xfer) { dev->i_product = desc_device->iProduct; dev->i_serial = desc_device->iSerialNumber; - if (tuh_desc_device_cb) tuh_desc_device_cb(daddr, (tusb_desc_device_t const*) _usbh_ctrl_buf); + if (tuh_desc_device_cb) { + tuh_desc_device_cb(daddr, (tusb_desc_device_t const*) _usbh_ctrl_buf); + } // Get 9-byte for total length uint8_t const config_idx = CONFIG_NUM - 1; @@ -1517,7 +1519,9 @@ static void process_enumeration(tuh_xfer_t* xfer) { } case ENUM_SET_CONFIG: - if (tuh_desc_config_cb) tuh_desc_config_cb(daddr, (const tusb_desc_configuration_t*) _usbh_ctrl_buf); + if (tuh_desc_config_cb) { + tuh_desc_config_cb(daddr, (const tusb_desc_configuration_t*) _usbh_ctrl_buf); + } TU_ASSERT(tuh_configuration_set(daddr, CONFIG_NUM, process_enumeration, ENUM_CONFIG_DRIVER),); break; diff --git a/src/tusb_option.h b/src/tusb_option.h index 84319939a..29fdcb0d6 100644 --- a/src/tusb_option.h +++ b/src/tusb_option.h @@ -657,9 +657,9 @@ //------------------------------------------------------------------ // Configuration Validation //------------------------------------------------------------------ -//#if CFG_TUD_ENDPOINT0_SIZE > 64 -// #error Control Endpoint Max Packet Size cannot be larger than 64 -//#endif +#if CFG_TUD_ENDPOINT0_SIZE > 64 + #error Control Endpoint Max Packet Size cannot be larger than 64 +#endif // To avoid GCC compiler warnings when -pedantic option is used (strict ISO C) typedef int make_iso_compilers_happy; From 294fb268d7bf36d163363fa669f3984fc95bfd32 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 12 Feb 2025 11:39:26 +0700 Subject: [PATCH 16/30] pre-commit fix --- examples/host/midi_rx/CMakeLists.txt | 2 +- examples/host/midi_rx/Makefile | 2 +- examples/host/midi_rx/src/main.c | 2 +- src/class/midi/README_midi_host.md | 14 +++++++------- src/class/midi/midi_host.c | 14 +++++++------- src/class/midi/midi_host.h | 4 ++-- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/examples/host/midi_rx/CMakeLists.txt b/examples/host/midi_rx/CMakeLists.txt index 4856e0a47..b5e5f6bc8 100644 --- a/examples/host/midi_rx/CMakeLists.txt +++ b/examples/host/midi_rx/CMakeLists.txt @@ -24,4 +24,4 @@ target_include_directories(${PROJECT} PUBLIC # Configure compilation flags and libraries for the example... see the corresponding function # in hw/bsp/FAMILY/family.cmake for details. -family_configure_host_example(${PROJECT}) \ No newline at end of file +family_configure_host_example(${PROJECT}) diff --git a/examples/host/midi_rx/Makefile b/examples/host/midi_rx/Makefile index f830617ac..af8ef4275 100644 --- a/examples/host/midi_rx/Makefile +++ b/examples/host/midi_rx/Makefile @@ -6,4 +6,4 @@ INC += \ # Example source EXAMPLE_SOURCE += $(wildcard src/*.c) SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE)) -include ../../rules.mk \ No newline at end of file +include ../../rules.mk diff --git a/examples/host/midi_rx/src/main.c b/examples/host/midi_rx/src/main.c index 6fe17255a..6c3311043 100644 --- a/examples/host/midi_rx/src/main.c +++ b/examples/host/midi_rx/src/main.c @@ -157,4 +157,4 @@ void tuh_midi_rx_cb(uint8_t dev_addr, uint32_t num_packets) void tuh_midi_tx_cb(uint8_t dev_addr) { (void ) dev_addr; -} \ No newline at end of file +} diff --git a/src/class/midi/README_midi_host.md b/src/class/midi/README_midi_host.md index 9df2249b3..efaf7a13d 100644 --- a/src/class/midi/README_midi_host.md +++ b/src/class/midi/README_midi_host.md @@ -9,7 +9,7 @@ constraint may change in the future. # MAXIMUM NUMBER OF ENDPOINTS Although the USB MIDI 1.0 Class specification allows an arbitrary number of endpoints, this driver supports at most one USB BULK DATA IN endpoint -and one USB BULK DATA OUT endpoint. Each endpoint can support up to 16 +and one USB BULK DATA OUT endpoint. Each endpoint can support up to 16 virtual cables. If a device has multiple IN endpoints or multiple OUT endpoints, it will fail to enumerate. @@ -51,9 +51,9 @@ pointer points to a MIDI interface descriptor, call midih_open() with that descriptor pointer. # CLASS SPECIFIC INTERFACE AND REQUESTS -The host driver does not make use of the informaton in the class specific +The host driver does not make use of the information in the class specific interface descriptors. In the future, a public API could be created to -retrieve the string descriptors for the names of each ELEMENT, +retrieve the string descriptors for the names of each ELEMENT, IN JACK and OUT JACK, and how the device describes the connections. This driver also does not support class specific requests to control @@ -68,18 +68,18 @@ Descriptors. This is wrong per my reading of the specification. # MESSAGE BUFFER DETAILS Messages buffers composed from USB data received on the IN endpoint will never contain running status because USB MIDI 1.0 class does not support that. Messages -buffers to be sent to the device on the OUT endpont may contain running status +buffers to be sent to the device on the OUT endpoint may contain running status (the message might come from a UART data stream from a 5-pin DIN MIDI IN -cable on the host, for example). The driver may in the future correctly compose +cable on the host, for example). The driver may in the future correctly compose 4-byte USB MIDI Class packets using the running status if need be. However, it does not currently do that. Also, use of running status is not a good idea overall because a single byte error can really mess up the data stream with no way to recover until the next non-real time status byte is in the message buffer. Message buffers to be sent to the device may contain Real time messages -such as MIDI clock. Real time messages may be inserted in the message +such as MIDI clock. Real time messages may be inserted in the message byte stream between status and data bytes of another message without disrupting -the running status. However, because MIDI 1.0 class messages are sent +the running status. However, because MIDI 1.0 class messages are sent as four byte packets, a real-time message so inserted will be re-ordered to be sent to the device in a new 4-byte packet immediately before the interrupted data stream. diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index 32311556e..061e17fee 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -1,4 +1,4 @@ -/* +/* * The MIT License (MIT) * * Copyright (c) 2019 Ha Thach (tinyusb.org) @@ -76,7 +76,7 @@ typedef struct // Endpoint FIFOs tu_fifo_t rx_ff; tu_fifo_t tx_ff; - + uint8_t rx_ff_buf[CFG_TUH_MIDI_RX_BUFSIZE]; uint8_t tx_ff_buf[CFG_TUH_MIDI_TX_BUFSIZE]; @@ -280,17 +280,17 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d // assume it is an interface header midi_desc_header_t const *p_mdh = (midi_desc_header_t const *)p_desc; - TU_VERIFY((p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE && p_mdh->bDescriptorSubType == MIDI_CS_INTERFACE_HEADER) || + TU_VERIFY((p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE && p_mdh->bDescriptorSubType == MIDI_CS_INTERFACE_HEADER) || (p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT && p_mdh->bDescriptorSubType == MIDI_CS_ENDPOINT_GENERAL) || p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT); - uint8_t prev_ep_addr = 0; // the CS endpoint descriptor is associated with the previous endpoint descrptor + uint8_t prev_ep_addr = 0; // the CS endpoint descriptor is associated with the previous endpoint descriptor p_midi_host->itf_num = desc_itf->bInterfaceNumber; tusb_desc_endpoint_t const* in_desc = NULL; tusb_desc_endpoint_t const* out_desc = NULL; while (len_parsed < max_len) { - TU_VERIFY((p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE) || + TU_VERIFY((p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE) || (p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT && p_mdh->bDescriptorSubType == MIDI_CS_ENDPOINT_GENERAL) || p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT); @@ -306,7 +306,7 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d } else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_IN_JACK) { - // Then it is an in jack. + // Then it is an in jack. TU_LOG2("Found in jack\r\n"); #if CFG_MIDI_HOST_DEVSTRINGS if (p_midi_host->next_in_jack < MAX_IN_JACKS) @@ -370,7 +370,7 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d TU_LOG2("found CS_ENDPOINT Descriptor for %u\r\n", prev_ep_addr); TU_VERIFY(prev_ep_addr != 0); // parse out the mapping between the device's embedded jacks and the endpoints - // Each embedded IN jack is assocated with an OUT endpoint + // Each embedded IN jack is associated with an OUT endpoint midi_cs_desc_endpoint_t const* p_csep = (midi_cs_desc_endpoint_t const*)p_mdh; if (tu_edpt_dir(prev_ep_addr) == TUSB_DIR_OUT) { diff --git a/src/class/midi/midi_host.h b/src/class/midi/midi_host.h index 8180e3309..292c596e4 100644 --- a/src/class/midi/midi_host.h +++ b/src/class/midi/midi_host.h @@ -1,4 +1,4 @@ -/* +/* * The MIT License (MIT) * * Copyright (c) 2019 Ha Thach (tinyusb.org) @@ -118,7 +118,7 @@ void midih_close (uint8_t dev_addr); //--------------------------------------------------------------------+ // Invoked when device with MIDI interface is mounted. -// If the MIDI host application requires MIDI IN, it should requst an +// If the MIDI host application requires MIDI IN, it should request an // IN transfer here. The device will likely NAK this transfer. How the driver // handles the NAK is hardware dependent. TU_ATTR_WEAK void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t in_ep, uint8_t out_ep, uint8_t num_cables_rx, uint16_t num_cables_tx); From 7c405236cfcf0850032ec0190eea1f01fb631ef9 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 12 Feb 2025 11:50:57 +0700 Subject: [PATCH 17/30] fix host midi build --- examples/host/midi_rx/CMakeLists.txt | 25 +++++++++++++++---------- examples/host/midi_rx/Makefile | 12 ++++++++---- examples/host/midi_rx/only.txt | 19 +++++++++++++++++++ examples/host/midi_rx/src/main.c | 4 +--- src/CMakeLists.txt | 1 + src/class/midi/midi_host.c | 5 +++-- src/class/midi/midi_host.h | 2 +- src/host/usbh.c | 4 ++-- src/tinyusb.mk | 1 + 9 files changed, 51 insertions(+), 22 deletions(-) diff --git a/examples/host/midi_rx/CMakeLists.txt b/examples/host/midi_rx/CMakeLists.txt index b5e5f6bc8..33953233d 100644 --- a/examples/host/midi_rx/CMakeLists.txt +++ b/examples/host/midi_rx/CMakeLists.txt @@ -1,27 +1,32 @@ -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.20) include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake) -# gets PROJECT name for the example (e.g. -) +# gets PROJECT name for the example family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR}) -project(${PROJECT}) +project(${PROJECT} C CXX ASM) # Checks this example is valid for the family and initializes the project family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR}) +# Espressif has its own cmake build system +if(FAMILY STREQUAL "espressif") + return() +endif() + add_executable(${PROJECT}) # Example source target_sources(${PROJECT} PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c - ) + ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c + ) # Example include target_include_directories(${PROJECT} PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/src - ) + ${CMAKE_CURRENT_SOURCE_DIR}/src + ) -# Configure compilation flags and libraries for the example... see the corresponding function -# in hw/bsp/FAMILY/family.cmake for details. -family_configure_host_example(${PROJECT}) +# Configure compilation flags and libraries for the example without RTOS. +# See the corresponding function in hw/bsp/FAMILY/family.cmake for details. +family_configure_host_example(${PROJECT} noos) diff --git a/examples/host/midi_rx/Makefile b/examples/host/midi_rx/Makefile index af8ef4275..0235e08c3 100644 --- a/examples/host/midi_rx/Makefile +++ b/examples/host/midi_rx/Makefile @@ -1,9 +1,13 @@ -include ../../../tools/top.mk -include ../../make.mk +include ../../build_system/make/make.mk + INC += \ src \ $(TOP)/hw \ + # Example source -EXAMPLE_SOURCE += $(wildcard src/*.c) +EXAMPLE_SOURCE += \ + src/main.c + SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE)) -include ../../rules.mk + +include ../../build_system/make/rules.mk diff --git a/examples/host/midi_rx/only.txt b/examples/host/midi_rx/only.txt index 2ba4438fd..b6f87f423 100644 --- a/examples/host/midi_rx/only.txt +++ b/examples/host/midi_rx/only.txt @@ -1 +1,20 @@ +mcu:ESP32S2 +mcu:ESP32S3 +mcu:ESP32P4 +mcu:KINETIS_KL +mcu:LPC175X_6X +mcu:LPC177X_8X +mcu:LPC18XX +mcu:LPC40XX +mcu:LPC43XX +mcu:MAX3421 +mcu:MIMXRT1XXX +mcu:MIMXRT10XX +mcu:MIMXRT11XX +mcu:MSP432E4 mcu:RP2040 +mcu:RX65X +mcu:RAXXX +mcu:STM32F4 +mcu:STM32F7 +mcu:STM32H7 diff --git a/examples/host/midi_rx/src/main.c b/examples/host/midi_rx/src/main.c index 6c3311043..8b83977e2 100644 --- a/examples/host/midi_rx/src/main.c +++ b/examples/host/midi_rx/src/main.c @@ -27,10 +27,8 @@ #include #include -#include "bsp/board.h" +#include "bsp/board_api.h" #include "tusb.h" -#include "class/midi/midi_host.h" - #if CFG_TUH_MIDI diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7b3ab4d42..55c52033c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -29,6 +29,7 @@ function(tinyusb_target_add TARGET) ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/host/hub.c ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/cdc/cdc_host.c ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/hid/hid_host.c + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/midi/midi_host.c ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/msc/msc_host.c ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/vendor/vendor_host.c # typec diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index 061e17fee..fbb994ade 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -29,7 +29,7 @@ #if (TUSB_OPT_HOST_ENABLED && CFG_TUH_MIDI) #include "host/usbh.h" -#include "host/usbh_classdriver.h" +#include "host/usbh_pvt.h" #include "midi_host.h" @@ -131,7 +131,7 @@ static uint32_t write_flush(uint8_t dev_addr, midih_interface_t* midi); //--------------------------------------------------------------------+ // USBH API //--------------------------------------------------------------------+ -void midih_init(void) +bool midih_init(void) { tu_memclr(&_midi_host, sizeof(_midi_host)); // config fifos @@ -146,6 +146,7 @@ void midih_init(void) tu_fifo_config_mutex(&p_midi_host->tx_ff, osal_mutex_create(&p_midi_host->tx_ff_mutex), NULL); #endif } + return true; } bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) diff --git a/src/class/midi/midi_host.h b/src/class/midi/midi_host.h index 292c596e4..edf57bfcb 100644 --- a/src/class/midi/midi_host.h +++ b/src/class/midi/midi_host.h @@ -107,7 +107,7 @@ uint8_t tuh_midi_get_all_istrings(uint8_t dev_addr, const uint8_t** istrings); //--------------------------------------------------------------------+ // Internal Class Driver API //--------------------------------------------------------------------+ -void midih_init (void); +bool midih_init (void); bool midih_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len); bool midih_set_config (uint8_t dev_addr, uint8_t itf_num); bool midih_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); diff --git a/src/host/usbh.c b/src/host/usbh.c index 30a4f5f63..e38ee3187 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -1489,7 +1489,7 @@ static void process_enumeration(tuh_xfer_t* xfer) { dev->i_serial = desc_device->iSerialNumber; if (tuh_desc_device_cb) { - tuh_desc_device_cb(daddr, (tusb_desc_device_t const*) _usbh_ctrl_buf); + tuh_desc_device_cb(daddr, (tusb_desc_device_t const*) _usbh_epbuf.ctrl); } // Get 9-byte for total length @@ -1520,7 +1520,7 @@ static void process_enumeration(tuh_xfer_t* xfer) { case ENUM_SET_CONFIG: if (tuh_desc_config_cb) { - tuh_desc_config_cb(daddr, (const tusb_desc_configuration_t*) _usbh_ctrl_buf); + tuh_desc_config_cb(daddr, (const tusb_desc_configuration_t*) _usbh_epbuf.ctrl); } TU_ASSERT(tuh_configuration_set(daddr, CONFIG_NUM, process_enumeration, ENUM_CONFIG_DRIVER),); diff --git a/src/tinyusb.mk b/src/tinyusb.mk index 89ea0212c..a9f623c24 100644 --- a/src/tinyusb.mk +++ b/src/tinyusb.mk @@ -21,6 +21,7 @@ TINYUSB_SRC_C += \ src/host/hub.c \ src/class/cdc/cdc_host.c \ src/class/hid/hid_host.c \ + src/class/midi/midi_host.c \ src/class/msc/msc_host.c \ src/class/vendor/vendor_host.c \ src/typec/usbc.c \ From 86d371fb79ca8b1ee6794c33b6a74d4319d9c1a6 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 12 Feb 2025 13:58:01 +0700 Subject: [PATCH 18/30] more ci fix --- examples/host/device_info/src/tusb_config.h | 3 +- examples/host/midi_rx/src/main.c | 131 +++++++++----------- examples/host/midi_rx/src/tusb_config.h | 77 ++++++++---- hw/bsp/rp2040/family.c | 4 +- 4 files changed, 117 insertions(+), 98 deletions(-) diff --git a/examples/host/device_info/src/tusb_config.h b/examples/host/device_info/src/tusb_config.h index 5190b5b1f..27dd07e25 100644 --- a/examples/host/device_info/src/tusb_config.h +++ b/examples/host/device_info/src/tusb_config.h @@ -109,8 +109,7 @@ // only hub class is enabled #define CFG_TUH_HUB 1 -// max device support (excluding hub device) -// 1 hub typically has 4 ports +// max device support (excluding hub device): 1 hub typically has 4 ports #define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1) #ifdef __cplusplus diff --git a/examples/host/midi_rx/src/main.c b/examples/host/midi_rx/src/main.c index 8b83977e2..a03fd588f 100644 --- a/examples/host/midi_rx/src/main.c +++ b/examples/host/midi_rx/src/main.c @@ -23,8 +23,8 @@ * */ -#include #include +#include #include #include "bsp/board_api.h" @@ -44,22 +44,25 @@ void led_blinking_task(void); void midi_host_rx_task(void); /*------------- MAIN -------------*/ -int main(void) -{ - board_init(); +int main(void) { + board_init(); - printf("TinyUSB Host MIDI Example\r\n"); + printf("TinyUSB Host MIDI Example\r\n"); - tusb_init(); + // init host stack on configured roothub port + tusb_rhport_init_t host_init = { + .role = TUSB_ROLE_HOST, + .speed = TUSB_SPEED_AUTO + }; + tusb_init(BOARD_TUH_RHPORT, &host_init); - while (1) - { - tuh_task(); - led_blinking_task(); - midi_host_rx_task(); - } + while (1) { + tuh_task(); + led_blinking_task(); + midi_host_rx_task(); + } - return 0; + return 0; } #endif @@ -67,36 +70,32 @@ int main(void) //--------------------------------------------------------------------+ // Blinking Task //--------------------------------------------------------------------+ -void led_blinking_task(void) -{ - const uint32_t interval_ms = 1000; - static uint32_t start_ms = 0; +void led_blinking_task(void) { + const uint32_t interval_ms = 1000; + static uint32_t start_ms = 0; - static bool led_state = false; + static bool led_state = false; - // Blink every interval ms - if ( board_millis() - start_ms < interval_ms) return; // not enough time - start_ms += interval_ms; + // Blink every interval ms + if (board_millis() - start_ms < interval_ms) return;// not enough time + start_ms += interval_ms; - board_led_write(led_state); - led_state = 1 - led_state; // toggle + board_led_write(led_state); + led_state = 1 - led_state;// toggle } //--------------------------------------------------------------------+ // MIDI host receive task //--------------------------------------------------------------------+ -void midi_host_rx_task(void) -{ - // device must be attached and have at least one endpoint ready to receive a message - if (!midi_dev_addr || !tuh_midi_configured(midi_dev_addr)) - { - return; - } - if (tuh_midih_get_num_rx_cables(midi_dev_addr) < 1) - { - return; - } - tuh_midi_read_poll(midi_dev_addr); +void midi_host_rx_task(void) { + // device must be attached and have at least one endpoint ready to receive a message + if (!midi_dev_addr || !tuh_midi_configured(midi_dev_addr)) { + return; + } + if (tuh_midih_get_num_rx_cables(midi_dev_addr) < 1) { + return; + } + tuh_midi_read_poll(midi_dev_addr); } //--------------------------------------------------------------------+ @@ -108,51 +107,45 @@ void midi_host_rx_task(void) // can be used to parse common/simple enough descriptor. // Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE, it will be skipped // therefore report_desc = NULL, desc_len = 0 -void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t in_ep, uint8_t out_ep, uint8_t num_cables_rx, uint16_t num_cables_tx) -{ - (void ) in_ep; - (void ) out_ep; - (void ) num_cables_rx; - (void ) num_cables_tx; +void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t in_ep, uint8_t out_ep, uint8_t num_cables_rx, uint16_t num_cables_tx) { + (void) in_ep; + (void) out_ep; + (void) num_cables_rx; + (void) num_cables_tx; - TU_LOG1("MIDI device address = %u, IN endpoint %u has %u cables, OUT endpoint %u has %u cables\r\n", - dev_addr, in_ep & 0xf, num_cables_rx, out_ep & 0xf, num_cables_tx); + TU_LOG1("MIDI device address = %u, IN endpoint %u has %u cables, OUT endpoint %u has %u cables\r\n", + dev_addr, in_ep & 0xf, num_cables_rx, out_ep & 0xf, num_cables_tx); - midi_dev_addr = dev_addr; + midi_dev_addr = dev_addr; } // Invoked when device with hid interface is un-mounted -void tuh_midi_umount_cb(uint8_t dev_addr, uint8_t instance) -{ - (void ) dev_addr; - (void ) instance; +void tuh_midi_umount_cb(uint8_t dev_addr, uint8_t instance) { + (void) dev_addr; + (void) instance; - TU_LOG1("MIDI device address = %d, instance = %d is unmounted\r\n", dev_addr, instance); - midi_dev_addr = 0; + TU_LOG1("MIDI device address = %d, instance = %d is unmounted\r\n", dev_addr, instance); + midi_dev_addr = 0; } -void tuh_midi_rx_cb(uint8_t dev_addr, uint32_t num_packets) -{ - if (midi_dev_addr != dev_addr) - { - return; - } +void tuh_midi_rx_cb(uint8_t dev_addr, uint32_t num_packets) { + if (midi_dev_addr != dev_addr) { + return; + } - if(num_packets == 0) - { - return; - } + if (num_packets == 0) { + return; + } - uint8_t cable_num; - uint8_t buffer[48]; - uint32_t bytes_read = tuh_midi_stream_read(dev_addr, &cable_num, buffer, sizeof(buffer)); - (void ) bytes_read; + uint8_t cable_num; + uint8_t buffer[48]; + uint32_t bytes_read = tuh_midi_stream_read(dev_addr, &cable_num, buffer, sizeof(buffer)); + (void) bytes_read; - TU_LOG1("Read bytes %lu cable %u", bytes_read, cable_num); - TU_LOG1_MEM(buffer, bytes_read, 2); + TU_LOG1("Read bytes %lu cable %u", bytes_read, cable_num); + TU_LOG1_MEM(buffer, bytes_read, 2); } -void tuh_midi_tx_cb(uint8_t dev_addr) -{ - (void ) dev_addr; +void tuh_midi_tx_cb(uint8_t dev_addr) { + (void) dev_addr; } diff --git a/examples/host/midi_rx/src/tusb_config.h b/examples/host/midi_rx/src/tusb_config.h index 5cc354ea9..1afcbbd63 100644 --- a/examples/host/midi_rx/src/tusb_config.h +++ b/examples/host/midi_rx/src/tusb_config.h @@ -23,15 +23,15 @@ * */ -#ifndef _TUSB_CONFIG_H_ -#define _TUSB_CONFIG_H_ +#ifndef TUSB_CONFIG_H_ +#define TUSB_CONFIG_H_ #ifdef __cplusplus extern "C" { #endif //-------------------------------------------------------------------- -// COMMON CONFIGURATION +// Common Configuration //-------------------------------------------------------------------- // defined by compiler flags for flexibility @@ -39,18 +39,18 @@ extern "C" { #error CFG_TUSB_MCU must be defined #endif -#if CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX -#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_HOST | OPT_MODE_HIGH_SPEED) -#else -#define CFG_TUSB_RHPORT0_MODE OPT_MODE_HOST +// Espressif IDF requires "freertos/" prefix in include path +#if TUSB_MCU_VENDOR_ESPRESSIF +#define CFG_TUSB_OS_INC_PATH freertos/ #endif #ifndef CFG_TUSB_OS -#define CFG_TUSB_OS OPT_OS_NONE +#define CFG_TUSB_OS OPT_OS_NONE #endif -// CFG_TUSB_DEBUG is defined by compiler in DEBUG build -// #define CFG_TUSB_DEBUG 0 +#ifndef CFG_TUSB_DEBUG +#define CFG_TUSB_DEBUG 0 +#endif /* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment. * Tinyusb use follows macros to declare transferring memory so that they can be put @@ -59,38 +59,63 @@ extern "C" { * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") )) * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4))) */ -#ifndef CFG_TUSB_MEM_SECTION -#define CFG_TUSB_MEM_SECTION +#ifndef CFG_TUH_MEM_SECTION +#define CFG_TUH_MEM_SECTION #endif -#ifndef CFG_TUSB_MEM_ALIGN -#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) +#ifndef CFG_TUH_MEM_ALIGN +#define CFG_TUH_MEM_ALIGN __attribute__ ((aligned(4))) #endif //-------------------------------------------------------------------- -// CONFIGURATION +// Host Configuration +//-------------------------------------------------------------------- + +// Enable Host stack +#define CFG_TUH_ENABLED 1 + +#if CFG_TUSB_MCU == OPT_MCU_RP2040 + // #define CFG_TUH_RPI_PIO_USB 1 // use pio-usb as host controller + // #define CFG_TUH_MAX3421 1 // use max3421 as host controller + + // host roothub port is 1 if using either pio-usb or max3421 + #if (defined(CFG_TUH_RPI_PIO_USB) && CFG_TUH_RPI_PIO_USB) || (defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421) + #define BOARD_TUH_RHPORT 1 + #endif +#endif + +// Default is max speed that hardware controller could support with on-chip PHY +#define CFG_TUH_MAX_SPEED BOARD_TUH_MAX_SPEED + +//------------------------- Board Specific -------------------------- + +// RHPort number used for host can be defined by board.mk, default to port 0 +#ifndef BOARD_TUH_RHPORT +#define BOARD_TUH_RHPORT 0 +#endif + +// RHPort max operational speed can defined by board.mk +#ifndef BOARD_TUH_MAX_SPEED +#define BOARD_TUH_MAX_SPEED OPT_MODE_DEFAULT_SPEED +#endif + +//-------------------------------------------------------------------- +// Driver Configuration //-------------------------------------------------------------------- // Size of buffer to hold descriptors and other data used for enumeration #define CFG_TUH_ENUMERATION_BUFSIZE 256 #define CFG_TUH_HUB 1 -#define CFG_TUH_CDC 0 -#define CFG_TUH_HID 0 // typical keyboard + mouse device can have 3-4 HID interfaces #define CFG_TUH_MIDI 1 // there will be at most one MIDIStreaming Interface descriptor -#define CFG_TUH_MSC 0 -#define CFG_TUH_VENDOR 0 -// max device support (excluding hub device) -#define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1) // hub typically has 4 ports +// max device support (excluding hub device): 1 hub typically has 4 ports +#define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1) + #define CFG_MIDI_HOST_DEVSTRINGS 1 -//------------- HID -------------// -#define CFG_TUH_HID_EPIN_BUFSIZE 64 -#define CFG_TUH_HID_EPOUT_BUFSIZE 64 - #ifdef __cplusplus } #endif -#endif /* _TUSB_CONFIG_H_ */ +#endif diff --git a/hw/bsp/rp2040/family.c b/hw/bsp/rp2040/family.c index 24aa0b616..689e264bd 100644 --- a/hw/bsp/rp2040/family.c +++ b/hw/bsp/rp2040/family.c @@ -266,7 +266,9 @@ int board_getchar(void) { #if CFG_TUH_ENABLED && defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421 void max3421_int_handler(uint gpio, uint32_t event_mask) { - if (!(gpio == MAX3421_INTR_PIN && event_mask & GPIO_IRQ_EDGE_FALL)) return; + if (!(gpio == MAX3421_INTR_PIN && event_mask & GPIO_IRQ_EDGE_FALL)) { + return; + } tuh_int_handler(BOARD_TUH_RHPORT, true); } From e0b192b633e70338ef5ac88c7f5a49f37867b289 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 12 Feb 2025 22:16:08 +0700 Subject: [PATCH 19/30] - use CFG_TUH_MIDI as number of midi host instance - comment out tuh_descriptor_device_cb/tuh_desc_configuration_cb since it is unrelated to this PR --- examples/host/midi_rx/src/tusb_config.h | 3 +- src/class/midi/midi_host.c | 289 +++++++++++------------- src/class/midi/midi_host.h | 21 +- src/host/usbh.c | 8 +- src/host/usbh.h | 8 +- 5 files changed, 152 insertions(+), 177 deletions(-) diff --git a/examples/host/midi_rx/src/tusb_config.h b/examples/host/midi_rx/src/tusb_config.h index 1afcbbd63..24b3b3ae6 100644 --- a/examples/host/midi_rx/src/tusb_config.h +++ b/examples/host/midi_rx/src/tusb_config.h @@ -107,10 +107,9 @@ extern "C" { #define CFG_TUH_ENUMERATION_BUFSIZE 256 #define CFG_TUH_HUB 1 -#define CFG_TUH_MIDI 1 // there will be at most one MIDIStreaming Interface descriptor - // max device support (excluding hub device): 1 hub typically has 4 ports #define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1) +#define CFG_TUH_MIDI CFG_TUH_DEVICE_MAX #define CFG_MIDI_HOST_DEVSTRINGS 1 diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index fbb994ade..21fe09143 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -36,14 +36,6 @@ //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ -#ifndef CFG_TUH_MAX_CABLES - #define CFG_TUH_MAX_CABLES 16 -#endif -#define CFG_TUH_MIDI_RX_BUFSIZE 64 -#define CFG_TUH_MIDI_TX_BUFSIZE 64 -#ifndef CFG_TUH_MIDI_EP_BUFSIZE - #define CFG_TUH_MIDI_EP_BUFSIZE 64 -#endif // TODO: refactor to share code with the MIDI Device driver typedef struct @@ -117,34 +109,44 @@ typedef struct #endif }midih_interface_t; -static midih_interface_t _midi_host[CFG_TUH_DEVICE_MAX]; - -static midih_interface_t *get_midi_host(uint8_t dev_addr) -{ - TU_VERIFY(dev_addr >0 && dev_addr <= CFG_TUH_DEVICE_MAX); - return (_midi_host + dev_addr - 1); -} +static midih_interface_t _midi_host[CFG_TUH_MIDI]; //------------- Internal prototypes -------------// static uint32_t write_flush(uint8_t dev_addr, midih_interface_t* midi); +//--------------------------------------------------------------------+ +// Helper +//--------------------------------------------------------------------+ + +TU_ATTR_ALWAYS_INLINE static inline midih_interface_t* find_midi_by_daddr(uint8_t dev_addr) { + for (uint8_t i = 0; i < CFG_TUH_MIDI; i++) { + if (_midi_host[i].dev_addr == dev_addr) { + return &_midi_host[i]; + } + } + return NULL; +} + +TU_ATTR_ALWAYS_INLINE static inline midih_interface_t* find_new_midi(void) { + return find_midi_by_daddr(0); +} + + //--------------------------------------------------------------------+ // USBH API //--------------------------------------------------------------------+ -bool midih_init(void) -{ +bool midih_init(void) { tu_memclr(&_midi_host, sizeof(_midi_host)); // config fifos - for (int inst = 0; inst < CFG_TUH_DEVICE_MAX; inst++) - { + for (int inst = 0; inst < CFG_TUH_MIDI; inst++) { midih_interface_t *p_midi_host = &_midi_host[inst]; - tu_fifo_config(&p_midi_host->rx_ff, p_midi_host->rx_ff_buf, CFG_TUH_MIDI_RX_BUFSIZE, 1, false); // true, true - tu_fifo_config(&p_midi_host->tx_ff, p_midi_host->tx_ff_buf, CFG_TUH_MIDI_TX_BUFSIZE, 1, false); // OBVS. + tu_fifo_config(&p_midi_host->rx_ff, p_midi_host->rx_ff_buf, CFG_TUH_MIDI_RX_BUFSIZE, 1, false);// true, true + tu_fifo_config(&p_midi_host->tx_ff, p_midi_host->tx_ff_buf, CFG_TUH_MIDI_TX_BUFSIZE, 1, false);// OBVS. - #if CFG_FIFO_MUTEX + #if CFG_FIFO_MUTEX tu_fifo_config_mutex(&p_midi_host->rx_ff, NULL, osal_mutex_create(&p_midi_host->rx_ff_mutex)); tu_fifo_config_mutex(&p_midi_host->tx_ff, osal_mutex_create(&p_midi_host->tx_ff_mutex), NULL); - #endif + #endif } return true; } @@ -152,7 +154,7 @@ bool midih_init(void) bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { (void)result; - midih_interface_t *p_midi_host = get_midi_host(dev_addr); + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); if ( ep_addr == p_midi_host->ep_in) { @@ -211,13 +213,14 @@ bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint return true; } -void midih_close(uint8_t dev_addr) -{ - midih_interface_t *p_midi_host = get_midi_host(dev_addr); - if (p_midi_host == NULL) +void midih_close(uint8_t dev_addr) { + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); + if (p_midi_host == NULL) { return; - if (tuh_midi_umount_cb) + } + if (tuh_midi_umount_cb) { tuh_midi_umount_cb(dev_addr, 0); + } tu_fifo_clear(&p_midi_host->rx_ff); tu_fifo_clear(&p_midi_host->tx_ff); p_midi_host->ep_in = 0; @@ -240,24 +243,24 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d { (void) rhport; - midih_interface_t *p_midi_host = get_midi_host(dev_addr); - + midih_interface_t *p_midi_host = find_new_midi(); TU_VERIFY(p_midi_host != NULL); + p_midi_host->num_string_indices = 0; TU_VERIFY(TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass); // There can be just a MIDI interface or an audio and a MIDI interface. Only open the MIDI interface uint8_t const *p_desc = (uint8_t const *) desc_itf; uint16_t len_parsed = 0; - if (AUDIO_SUBCLASS_CONTROL == desc_itf->bInterfaceSubClass) - { + if (AUDIO_SUBCLASS_CONTROL == desc_itf->bInterfaceSubClass) { // Keep track of any string descriptor that might be here - if (desc_itf->iInterface != 0) - p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = desc_itf->iInterface; + if (desc_itf->iInterface != 0) { + p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = desc_itf->iInterface; + } // This driver does not support audio streaming. However, if this is the audio control interface // there might be a MIDI interface following it. Search through every descriptor until a MIDI // interface is found or the end of the descriptor is found - while (len_parsed < max_len && (desc_itf->bInterfaceClass != TUSB_CLASS_AUDIO || desc_itf->bInterfaceSubClass != AUDIO_SUBCLASS_MIDI_STREAMING)) - { + while (len_parsed < max_len && + (desc_itf->bInterfaceClass != TUSB_CLASS_AUDIO || desc_itf->bInterfaceSubClass != AUDIO_SUBCLASS_MIDI_STREAMING)) { len_parsed += desc_itf->bLength; p_desc = tu_desc_next(p_desc); desc_itf = (tusb_desc_interface_t const *)p_desc; @@ -269,8 +272,9 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d len_parsed += desc_itf->bLength; // Keep track of any string descriptor that might be here - if (desc_itf->iInterface != 0) - p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = desc_itf->iInterface; + if (desc_itf->iInterface != 0) { + p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = desc_itf->iInterface; + } p_desc = tu_desc_next(p_desc); TU_LOG1("MIDI opening Interface %u (addr = %u)\r\n", desc_itf->bInterfaceNumber, dev_addr); @@ -282,195 +286,169 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d // assume it is an interface header midi_desc_header_t const *p_mdh = (midi_desc_header_t const *)p_desc; TU_VERIFY((p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE && p_mdh->bDescriptorSubType == MIDI_CS_INTERFACE_HEADER) || - (p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT && p_mdh->bDescriptorSubType == MIDI_CS_ENDPOINT_GENERAL) || - p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT); + (p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT && p_mdh->bDescriptorSubType == MIDI_CS_ENDPOINT_GENERAL) || + p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT); uint8_t prev_ep_addr = 0; // the CS endpoint descriptor is associated with the previous endpoint descriptor p_midi_host->itf_num = desc_itf->bInterfaceNumber; tusb_desc_endpoint_t const* in_desc = NULL; tusb_desc_endpoint_t const* out_desc = NULL; - while (len_parsed < max_len) - { + while (len_parsed < max_len) { TU_VERIFY((p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE) || - (p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT && p_mdh->bDescriptorSubType == MIDI_CS_ENDPOINT_GENERAL) || - p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT); + (p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT && p_mdh->bDescriptorSubType == MIDI_CS_ENDPOINT_GENERAL) || + p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT); if (p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE) { // The USB host doesn't really need this information unless it uses // the string descriptor for a jack or Element // assume it is an input jack - midi_desc_in_jack_t const *p_mdij = (midi_desc_in_jack_t const *)p_desc; - if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_HEADER) - { + midi_desc_in_jack_t const *p_mdij = (midi_desc_in_jack_t const *) p_desc; + if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_HEADER) { TU_LOG2("Found MIDI Interface Header\r\b"); - } - else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_IN_JACK) - { + } else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_IN_JACK) { // Then it is an in jack. TU_LOG2("Found in jack\r\n"); -#if CFG_MIDI_HOST_DEVSTRINGS - if (p_midi_host->next_in_jack < MAX_IN_JACKS) - { + #if CFG_MIDI_HOST_DEVSTRINGS + if (p_midi_host->next_in_jack < MAX_IN_JACKS) { p_midi_host->in_jack_info[p_midi_host->next_in_jack].jack_id = p_mdij->bJackID; p_midi_host->in_jack_info[p_midi_host->next_in_jack].jack_type = p_mdij->bJackType; p_midi_host->in_jack_info[p_midi_host->next_in_jack].string_index = p_mdij->iJack; ++p_midi_host->next_in_jack; // Keep track of any string descriptor that might be here - if (p_mdij->iJack != 0) + if (p_mdij->iJack != 0) { p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = p_mdij->iJack; - + } } -#endif - } - else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_OUT_JACK) - { + #endif + } else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_OUT_JACK) { // then it is an out jack TU_LOG2("Found out jack\r\n"); -#if CFG_MIDI_HOST_DEVSTRINGS - if (p_midi_host->next_out_jack < MAX_OUT_JACKS) - { - midi_desc_out_jack_t const *p_mdoj = (midi_desc_out_jack_t const *)p_desc; + #if CFG_MIDI_HOST_DEVSTRINGS + if (p_midi_host->next_out_jack < MAX_OUT_JACKS) { + midi_desc_out_jack_t const *p_mdoj = (midi_desc_out_jack_t const *) p_desc; p_midi_host->out_jack_info[p_midi_host->next_out_jack].jack_id = p_mdoj->bJackID; p_midi_host->out_jack_info[p_midi_host->next_out_jack].jack_type = p_mdoj->bJackType; p_midi_host->out_jack_info[p_midi_host->next_out_jack].num_source_ids = p_mdoj->bNrInputPins; const struct associated_jack_s { - uint8_t id; - uint8_t pin; - } *associated_jack = (const struct associated_jack_s *)(p_desc+6); + uint8_t id; + uint8_t pin; + } *associated_jack = (const struct associated_jack_s *) (p_desc + 6); int jack; - for (jack = 0; jack < p_mdoj->bNrInputPins; jack++) - { + for (jack = 0; jack < p_mdoj->bNrInputPins; jack++) { p_midi_host->out_jack_info[p_midi_host->next_out_jack].source_ids[jack] = associated_jack->id; } - p_midi_host->out_jack_info[p_midi_host->next_out_jack].string_index = *(p_desc+6+p_mdoj->bNrInputPins*2); + p_midi_host->out_jack_info[p_midi_host->next_out_jack].string_index = *(p_desc + 6 + p_mdoj->bNrInputPins * 2); ++p_midi_host->next_out_jack; - if (p_mdoj->iJack != 0) + if (p_mdoj->iJack != 0) { p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = p_mdoj->iJack; + } } -#endif - } - else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_ELEMENT) - { + #endif + } else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_ELEMENT) { // the it is an element; - #if CFG_MIDI_HOST_DEVSTRINGS + #if CFG_MIDI_HOST_DEVSTRINGS TU_LOG1("Found element; strings not supported\r\n"); - #else + #else TU_LOG2("Found element\r\n"); - #endif - } - else - { + #endif + } else { TU_LOG2("Unknown CS Interface sub-type %u\r\n", p_mdij->bDescriptorSubType); - TU_VERIFY(false); // unknown CS Interface sub-type + TU_VERIFY(false);// unknown CS Interface sub-type } len_parsed += p_mdij->bLength; - } - else if (p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT) - { + } else if (p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT) { TU_LOG2("found CS_ENDPOINT Descriptor for %u\r\n", prev_ep_addr); TU_VERIFY(prev_ep_addr != 0); // parse out the mapping between the device's embedded jacks and the endpoints // Each embedded IN jack is associated with an OUT endpoint - midi_cs_desc_endpoint_t const* p_csep = (midi_cs_desc_endpoint_t const*)p_mdh; - if (tu_edpt_dir(prev_ep_addr) == TUSB_DIR_OUT) - { + midi_cs_desc_endpoint_t const *p_csep = (midi_cs_desc_endpoint_t const *) p_mdh; + if (tu_edpt_dir(prev_ep_addr) == TUSB_DIR_OUT) { TU_VERIFY(p_midi_host->ep_out == prev_ep_addr); TU_VERIFY(p_midi_host->num_cables_tx == 0); p_midi_host->num_cables_tx = p_csep->bNumEmbMIDIJack; -#if CFG_MIDI_HOST_DEVSTRINGS + #if CFG_MIDI_HOST_DEVSTRINGS uint8_t jack; uint8_t max_jack = p_midi_host->num_cables_tx; - if (max_jack > sizeof(p_midi_host->ep_out_associated_jacks)) - { - max_jack = sizeof(p_midi_host->ep_out_associated_jacks); + if (max_jack > sizeof(p_midi_host->ep_out_associated_jacks)) { + max_jack = sizeof(p_midi_host->ep_out_associated_jacks); } - for (jack = 0; jack < max_jack; jack++) - { + for (jack = 0; jack < max_jack; jack++) { p_midi_host->ep_out_associated_jacks[jack] = p_csep->baAssocJackID[jack]; } -#endif - } - else - { + #endif + } else { TU_VERIFY(p_midi_host->ep_in == prev_ep_addr); TU_VERIFY(p_midi_host->num_cables_rx == 0); p_midi_host->num_cables_rx = p_csep->bNumEmbMIDIJack; -#if CFG_MIDI_HOST_DEVSTRINGS + #if CFG_MIDI_HOST_DEVSTRINGS uint8_t jack; uint8_t max_jack = p_midi_host->num_cables_rx; - if (max_jack > sizeof(p_midi_host->ep_in_associated_jacks)) - { - max_jack = sizeof(p_midi_host->ep_in_associated_jacks); + if (max_jack > sizeof(p_midi_host->ep_in_associated_jacks)) { + max_jack = sizeof(p_midi_host->ep_in_associated_jacks); } - for (jack = 0; jack < max_jack; jack++) - { + for (jack = 0; jack < max_jack; jack++) { p_midi_host->ep_in_associated_jacks[jack] = p_csep->baAssocJackID[jack]; } -#endif + #endif } len_parsed += p_csep->bLength; prev_ep_addr = 0; - } - else if (p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT) { + } else if (p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT) { // parse out the bulk endpoint info - tusb_desc_endpoint_t const *p_ep = (tusb_desc_endpoint_t const *)p_mdh; + tusb_desc_endpoint_t const *p_ep = (tusb_desc_endpoint_t const *) p_mdh; TU_LOG2("found ENDPOINT Descriptor for %u\r\n", p_ep->bEndpointAddress); - if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_OUT) - { + if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_OUT) { TU_VERIFY(p_midi_host->ep_out == 0); TU_VERIFY(p_midi_host->num_cables_tx == 0); p_midi_host->ep_out = p_ep->bEndpointAddress; p_midi_host->ep_out_max = p_ep->wMaxPacketSize; - if (p_midi_host->ep_out_max > CFG_TUH_MIDI_TX_BUFSIZE) + if (p_midi_host->ep_out_max > CFG_TUH_MIDI_TX_BUFSIZE) { p_midi_host->ep_out_max = CFG_TUH_MIDI_TX_BUFSIZE; + } prev_ep_addr = p_midi_host->ep_out; out_desc = p_ep; - } - else - { + } else { TU_VERIFY(p_midi_host->ep_in == 0); TU_VERIFY(p_midi_host->num_cables_rx == 0); p_midi_host->ep_in = p_ep->bEndpointAddress; p_midi_host->ep_in_max = p_ep->wMaxPacketSize; - if (p_midi_host->ep_in_max > CFG_TUH_MIDI_RX_BUFSIZE) + if (p_midi_host->ep_in_max > CFG_TUH_MIDI_RX_BUFSIZE) { p_midi_host->ep_in_max = CFG_TUH_MIDI_RX_BUFSIZE; + } prev_ep_addr = p_midi_host->ep_in; in_desc = p_ep; } len_parsed += p_mdh->bLength; } p_desc = tu_desc_next(p_desc); - p_mdh = (midi_desc_header_t const *)p_desc; + p_mdh = (midi_desc_header_t const *) p_desc; } TU_VERIFY((p_midi_host->ep_out != 0 && p_midi_host->num_cables_tx != 0) || (p_midi_host->ep_in != 0 && p_midi_host->num_cables_rx != 0)); TU_LOG1("MIDI descriptor parsed successfully\r\n"); // remove duplicate string indices - for (int idx=0; idx < p_midi_host->num_string_indices; idx++) { - for (int jdx = idx+1; jdx < p_midi_host->num_string_indices; jdx++) { - while (jdx < p_midi_host->num_string_indices && p_midi_host->all_string_indices[idx] == p_midi_host->all_string_indices[jdx]) { - // delete the duplicate by overwriting it with the last entry and reducing the number of entries by 1 - p_midi_host->all_string_indices[jdx] = p_midi_host->all_string_indices[p_midi_host->num_string_indices-1]; - --p_midi_host->num_string_indices; - } + for (int idx = 0; idx < p_midi_host->num_string_indices; idx++) { + for (int jdx = idx + 1; jdx < p_midi_host->num_string_indices; jdx++) { + while (jdx < p_midi_host->num_string_indices && p_midi_host->all_string_indices[idx] == p_midi_host->all_string_indices[jdx]) { + // delete the duplicate by overwriting it with the last entry and reducing the number of entries by 1 + p_midi_host->all_string_indices[jdx] = p_midi_host->all_string_indices[p_midi_host->num_string_indices - 1]; + --p_midi_host->num_string_indices; } + } } - if (in_desc) - { + if (in_desc) { TU_ASSERT(tuh_edpt_open(dev_addr, in_desc)); // Some devices always return exactly the request length so transfers won't complete // unless you assume every transfer is the last one. // TODO usbh_edpt_force_last_buffer(dev_addr, p_midi_host->ep_in, true); } - if (out_desc) - { + if (out_desc) { TU_ASSERT(tuh_edpt_open(dev_addr, out_desc)); } p_midi_host->dev_addr = dev_addr; - if (tuh_midi_mount_cb) - { + if (tuh_midi_mount_cb) { tuh_midi_mount_cb(dev_addr, p_midi_host->ep_in, p_midi_host->ep_out, p_midi_host->num_cables_rx, p_midi_host->num_cables_tx); } return true; @@ -478,7 +456,7 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d bool tuh_midi_configured(uint8_t dev_addr) { - midih_interface_t *p_midi_host = get_midi_host(dev_addr); + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); return p_midi_host->configured; } @@ -486,7 +464,7 @@ bool tuh_midi_configured(uint8_t dev_addr) bool midih_set_config(uint8_t dev_addr, uint8_t itf_num) { (void) itf_num; - midih_interface_t *p_midi_host = get_midi_host(dev_addr); + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); p_midi_host->configured = true; @@ -498,43 +476,36 @@ bool midih_set_config(uint8_t dev_addr, uint8_t itf_num) //--------------------------------------------------------------------+ // Stream API //--------------------------------------------------------------------+ -static uint32_t write_flush(uint8_t dev_addr, midih_interface_t* midi) -{ +static uint32_t write_flush(uint8_t dev_addr, midih_interface_t* midi) { // No data to send - if ( !tu_fifo_count(&midi->tx_ff) ) return 0; + if ( !tu_fifo_count(&midi->tx_ff) ) { return 0; } // skip if previous transfer not complete TU_VERIFY( usbh_edpt_claim(dev_addr, midi->ep_out) ); uint16_t count = tu_fifo_read_n(&midi->tx_ff, midi->epout_buf, midi->ep_out_max); - if (count) - { - TU_ASSERT( usbh_edpt_xfer(dev_addr, midi->ep_out, midi->epout_buf, count), 0 ); + if (count) { + TU_ASSERT(usbh_edpt_xfer(dev_addr, midi->ep_out, midi->epout_buf, count), 0); return count; - }else - { + } else { // Release endpoint since we don't make any transfer usbh_edpt_release(dev_addr, midi->ep_out); return 0; } } -bool tuh_midi_read_poll( uint8_t dev_addr ) -{ - midih_interface_t *p_midi_host = get_midi_host(dev_addr); +bool tuh_midi_read_poll(uint8_t dev_addr) { + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); bool result = false; bool in_edpt_not_busy = !usbh_edpt_busy(dev_addr, p_midi_host->ep_in); - if (in_edpt_not_busy) - { + if (in_edpt_not_busy) { TU_LOG2("Requesting poll IN endpoint %d\r\n", p_midi_host->ep_in); TU_ASSERT(usbh_edpt_xfer(p_midi_host->dev_addr, p_midi_host->ep_in, p_midi_host->epin_buf, p_midi_host->ep_in_max), 0); result = true; - } - else - { + } else { // Maybe the IN endpoint is only busy because the RP2040 host hardware // is retrying a NAK'd IN transfer forever. Try aborting the NAK'd // transfer to allow other transfers to happen on the one shared @@ -546,7 +517,7 @@ bool tuh_midi_read_poll( uint8_t dev_addr ) uint32_t tuh_midi_stream_write (uint8_t dev_addr, uint8_t cable_num, uint8_t const* buffer, uint32_t bufsize) { - midih_interface_t *p_midi_host = get_midi_host(dev_addr); + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); TU_VERIFY(cable_num < p_midi_host->num_cables_tx); midi_stream_t *stream = &p_midi_host->stream_write; @@ -673,7 +644,7 @@ uint32_t tuh_midi_stream_write (uint8_t dev_addr, uint8_t cable_num, uint8_t con bool tuh_midi_packet_write (uint8_t dev_addr, uint8_t const packet[4]) { - midih_interface_t *p_midi_host = get_midi_host(dev_addr); + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); if (tu_fifo_remaining(&p_midi_host->tx_ff) < 4) @@ -688,7 +659,7 @@ bool tuh_midi_packet_write (uint8_t dev_addr, uint8_t const packet[4]) uint32_t tuh_midi_stream_flush( uint8_t dev_addr ) { - midih_interface_t *p_midi_host = get_midi_host(dev_addr); + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); uint32_t bytes_flushed = 0; @@ -703,7 +674,7 @@ uint32_t tuh_midi_stream_flush( uint8_t dev_addr ) //--------------------------------------------------------------------+ uint8_t tuh_midih_get_num_tx_cables (uint8_t dev_addr) { - midih_interface_t *p_midi_host = get_midi_host(dev_addr); + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); TU_VERIFY(p_midi_host->ep_out != 0); // returns 0 if fails return p_midi_host->num_cables_tx; @@ -711,7 +682,7 @@ uint8_t tuh_midih_get_num_tx_cables (uint8_t dev_addr) uint8_t tuh_midih_get_num_rx_cables (uint8_t dev_addr) { - midih_interface_t *p_midi_host = get_midi_host(dev_addr); + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); TU_VERIFY(p_midi_host->ep_in != 0); // returns 0 if fails return p_midi_host->num_cables_rx; @@ -719,7 +690,7 @@ uint8_t tuh_midih_get_num_rx_cables (uint8_t dev_addr) bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]) { - midih_interface_t *p_midi_host = get_midi_host(dev_addr); + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); TU_VERIFY(tu_fifo_count(&p_midi_host->rx_ff) >= 4); return tu_fifo_read_n(&p_midi_host->rx_ff, packet, 4) == 4; @@ -727,7 +698,7 @@ bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]) uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p_buffer, uint16_t bufsize) { - midih_interface_t *p_midi_host = get_midi_host(dev_addr); + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); uint32_t bytes_buffered = 0; TU_ASSERT(p_cable_num); @@ -848,7 +819,7 @@ uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t * uint8_t tuh_midi_get_num_rx_cables(uint8_t dev_addr) { - midih_interface_t *p_midi_host = get_midi_host(dev_addr); + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); uint8_t num_cables = 0; if (p_midi_host) @@ -860,7 +831,7 @@ uint8_t tuh_midi_get_num_rx_cables(uint8_t dev_addr) uint8_t tuh_midi_get_num_tx_cables(uint8_t dev_addr) { - midih_interface_t *p_midi_host = get_midi_host(dev_addr); + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); uint8_t num_cables = 0; if (p_midi_host) @@ -897,7 +868,7 @@ static uint8_t find_string_index(midih_interface_t *ptr, uint8_t jack_id) uint8_t tuh_midi_get_rx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings) { uint8_t nstrings = 0; - midih_interface_t *p_midi_host = get_midi_host(dev_addr); + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); nstrings = p_midi_host->num_cables_rx; if (nstrings > max_istrings) @@ -916,7 +887,7 @@ uint8_t tuh_midi_get_rx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint uint8_t tuh_midi_get_tx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings) { uint8_t nstrings = 0; - midih_interface_t *p_midi_host = get_midi_host(dev_addr); + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); nstrings = p_midi_host->num_cables_tx; if (nstrings > max_istrings) @@ -935,7 +906,7 @@ uint8_t tuh_midi_get_tx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint uint8_t tuh_midi_get_all_istrings(uint8_t dev_addr, const uint8_t** istrings) { - midih_interface_t *p_midi_host = get_midi_host(dev_addr); + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); uint8_t nstrings = p_midi_host->num_string_indices; if (nstrings) diff --git a/src/class/midi/midi_host.h b/src/class/midi/midi_host.h index edf57bfcb..bf2378168 100644 --- a/src/class/midi/midi_host.h +++ b/src/class/midi/midi_host.h @@ -37,16 +37,25 @@ //--------------------------------------------------------------------+ // Class Driver Configuration //--------------------------------------------------------------------+ - -// TODO Highspeed bulk transfer can be up to 512 bytes -#ifndef CFG_TUH_HID_EPIN_BUFSIZE -#define CFG_TUH_HID_EPIN_BUFSIZE 64 +#ifndef CFG_TUH_MAX_CABLES +#define CFG_TUH_MAX_CABLES 16 #endif -#ifndef CFG_TUH_HID_EPOUT_BUFSIZE -#define CFG_TUH_HID_EPOUT_BUFSIZE 64 +#ifndef CFG_TUH_MIDI_RX_BUFSIZE +#define CFG_TUH_MIDI_RX_BUFSIZE 64 #endif +#ifndef CFG_TUH_MIDI_TX_BUFSIZE +#define CFG_TUH_MIDI_TX_BUFSIZE 64 +#endif + +#ifndef CFG_TUH_MIDI_EP_BUFSIZE +#define CFG_TUH_MIDI_EP_BUFSIZE 64 +#endif + +#ifndef CFG_MIDI_HOST_DEVSTRINGS +#define CFG_MIDI_HOST_DEVSTRINGS 0 +#endif //--------------------------------------------------------------------+ // Application API (Single Interface) diff --git a/src/host/usbh.c b/src/host/usbh.c index e38ee3187..f874121a3 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -1488,9 +1488,7 @@ static void process_enumeration(tuh_xfer_t* xfer) { dev->i_product = desc_device->iProduct; dev->i_serial = desc_device->iSerialNumber; - if (tuh_desc_device_cb) { - tuh_desc_device_cb(daddr, (tusb_desc_device_t const*) _usbh_epbuf.ctrl); - } + // tuh_descriptor_device_cb(daddr, (tusb_desc_device_t const*) _usbh_epbuf.ctrl); // Get 9-byte for total length uint8_t const config_idx = CONFIG_NUM - 1; @@ -1519,9 +1517,7 @@ static void process_enumeration(tuh_xfer_t* xfer) { } case ENUM_SET_CONFIG: - if (tuh_desc_config_cb) { - tuh_desc_config_cb(daddr, (const tusb_desc_configuration_t*) _usbh_epbuf.ctrl); - } + // tuh_desc_configuration_cb(daddr, CONFIG_NUM-1, (const tusb_desc_configuration_t*) _usbh_epbuf.ctrl); TU_ASSERT(tuh_configuration_set(daddr, CONFIG_NUM, process_enumeration, ENUM_CONFIG_DRIVER),); break; diff --git a/src/host/usbh.h b/src/host/usbh.h index 500b044f8..3bcdd3657 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -96,11 +96,11 @@ typedef union { // APPLICATION CALLBACK //--------------------------------------------------------------------+ -// Give the application an opportunity to grab the device descriptor -TU_ATTR_WEAK void tuh_desc_device_cb(uint8_t daddr, const tusb_desc_device_t *desc_device); +// Invoked when enumeration get device descriptor +// TU_ATTR_WEAK void tuh_descriptor_device_cb(uint8_t daddr, const tusb_desc_device_t *desc_device); -// Give the application an opportunity to grab the configuration descriptor -TU_ATTR_WEAK void tuh_desc_config_cb(uint8_t daddr, const tusb_desc_configuration_t *desc_config); +// Invoked when enumeration get configuration descriptor +// TU_ATTR_WEAK void tuh_desc_configuration_cb(uint8_t daddr, uint8_t cfg_index, const tusb_desc_configuration_t *desc_config); // Invoked when a device is mounted (configured) TU_ATTR_WEAK void tuh_mount_cb (uint8_t daddr); From bad6cbe48930c0c5250b8a717a46cc1f6d83de56 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 13 Feb 2025 15:52:30 +0700 Subject: [PATCH 20/30] update midi host to use endpoint stream API --- src/class/cdc/cdc_host.c | 4 +- src/class/midi/midi_host.c | 445 ++++++++++++------------------------- src/class/midi/midi_host.h | 25 ++- src/common/tusb_private.h | 10 +- src/host/usbh.c | 3 +- src/host/usbh.h | 3 + 6 files changed, 174 insertions(+), 316 deletions(-) diff --git a/src/class/cdc/cdc_host.c b/src/class/cdc/cdc_host.c index e817ebc7e..4058857c5 100644 --- a/src/class/cdc/cdc_host.c +++ b/src/class/cdc/cdc_host.c @@ -691,10 +691,10 @@ bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t } } else if ( ep_addr == p_cdc->stream.rx.ep_addr ) { #if CFG_TUH_CDC_FTDI - if (p_cdc->serial_drid == SERIAL_DRIVER_FTDI) { + if (p_cdc->serial_drid == SERIAL_DRIVER_FTDI && xferred_bytes > 2) { // FTDI reserve 2 bytes for status // uint8_t status[2] = {p_cdc->stream.rx.ep_buf[0], p_cdc->stream.rx.ep_buf[1]}; - tu_edpt_stream_read_xfer_complete_offset(&p_cdc->stream.rx, xferred_bytes, 2); + tu_edpt_stream_read_xfer_complete_with_buf(&p_cdc->stream.rx, p_cdc->stream.rx.ep_buf+2, xferred_bytes-2); }else #endif { diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index 21fe09143..eb3f0b070 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -45,15 +45,12 @@ typedef struct uint8_t total; }midi_stream_t; -typedef struct -{ +typedef struct { uint8_t dev_addr; uint8_t itf_num; uint8_t ep_in; // IN endpoint address uint8_t ep_out; // OUT endpoint address - uint16_t ep_in_max; // min( CFG_TUH_MIDI_RX_BUFSIZE, wMaxPacketSize of the IN endpoint) - uint16_t ep_out_max; // min( CFG_TUH_MIDI_TX_BUFSIZE, wMaxPacketSize of the OUT endpoint) uint8_t num_cables_rx; // IN endpoint CS descriptor bNumEmbMIDIJack value uint8_t num_cables_tx; // OUT endpoint CS descriptor bNumEmbMIDIJack value @@ -65,22 +62,14 @@ typedef struct midi_stream_t stream_read; /*------------- From this point, data is not cleared by bus reset -------------*/ - // Endpoint FIFOs - tu_fifo_t rx_ff; - tu_fifo_t tx_ff; + // Endpoint stream + struct { + tu_edpt_stream_t tx; + tu_edpt_stream_t rx; - - uint8_t rx_ff_buf[CFG_TUH_MIDI_RX_BUFSIZE]; - uint8_t tx_ff_buf[CFG_TUH_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_TUH_MIDI_EP_BUFSIZE]; - CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUH_MIDI_EP_BUFSIZE]; + uint8_t rx_ff_buf[CFG_TUH_MIDI_RX_BUFSIZE]; + uint8_t tx_ff_buf[CFG_TUH_MIDI_TX_BUFSIZE]; + } ep_stream; bool configured; @@ -109,15 +98,17 @@ typedef struct #endif }midih_interface_t; -static midih_interface_t _midi_host[CFG_TUH_MIDI]; +typedef struct { + TUH_EPBUF_DEF(tx, TUH_EPSIZE_BULK_MPS); + TUH_EPBUF_DEF(rx, TUH_EPSIZE_BULK_MPS); +} midih_epbuf_t; -//------------- Internal prototypes -------------// -static uint32_t write_flush(uint8_t dev_addr, midih_interface_t* midi); +static midih_interface_t _midi_host[CFG_TUH_MIDI]; +CFG_TUH_MEM_SECTION static midih_epbuf_t _midi_epbuf[CFG_TUH_MIDI]; //--------------------------------------------------------------------+ // Helper //--------------------------------------------------------------------+ - TU_ATTR_ALWAYS_INLINE static inline midih_interface_t* find_midi_by_daddr(uint8_t dev_addr) { for (uint8_t i = 0; i < CFG_TUH_MIDI; i++) { if (_midi_host[i].dev_addr == dev_addr) { @@ -131,85 +122,27 @@ TU_ATTR_ALWAYS_INLINE static inline midih_interface_t* find_new_midi(void) { return find_midi_by_daddr(0); } - //--------------------------------------------------------------------+ // USBH API //--------------------------------------------------------------------+ bool midih_init(void) { tu_memclr(&_midi_host, sizeof(_midi_host)); - // config fifos for (int inst = 0; inst < CFG_TUH_MIDI; inst++) { midih_interface_t *p_midi_host = &_midi_host[inst]; - tu_fifo_config(&p_midi_host->rx_ff, p_midi_host->rx_ff_buf, CFG_TUH_MIDI_RX_BUFSIZE, 1, false);// true, true - tu_fifo_config(&p_midi_host->tx_ff, p_midi_host->tx_ff_buf, CFG_TUH_MIDI_TX_BUFSIZE, 1, false);// OBVS. - - #if CFG_FIFO_MUTEX - tu_fifo_config_mutex(&p_midi_host->rx_ff, NULL, osal_mutex_create(&p_midi_host->rx_ff_mutex)); - tu_fifo_config_mutex(&p_midi_host->tx_ff, osal_mutex_create(&p_midi_host->tx_ff_mutex), NULL); - #endif + tu_edpt_stream_init(&p_midi_host->ep_stream.rx, true, false, false, + p_midi_host->ep_stream.rx_ff_buf, CFG_TUH_MIDI_RX_BUFSIZE, _midi_epbuf->rx, TUH_EPSIZE_BULK_MPS); + tu_edpt_stream_init(&p_midi_host->ep_stream.tx, true, true, false, + p_midi_host->ep_stream.tx_ff_buf, CFG_TUH_MIDI_TX_BUFSIZE, _midi_epbuf->tx, TUH_EPSIZE_BULK_MPS); } return true; } -bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) -{ - (void)result; - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - if ( ep_addr == p_midi_host->ep_in) - { - if (0 == xferred_bytes) - { - return true; // No data to handle - } - - // receive new data if available - uint32_t packets_queued = 0; - if (xferred_bytes) - { - // put in the RX FIFO only non-zero MIDI IN 4-byte packets - uint8_t* buf = p_midi_host->epin_buf; - uint32_t npackets = xferred_bytes / 4; - uint32_t packet_num; - for (packet_num = 0; packet_num < npackets; packet_num++) - { - // some devices send back all zero packets even if there is no data ready - uint32_t packet = (uint32_t)((*buf)<<24) | ((uint32_t)(*(buf+1))<<16) | ((uint32_t)(*(buf+2))<<8) | ((uint32_t)(*(buf+3))); - if (packet != 0) - { - tu_fifo_write_n(&p_midi_host->rx_ff, buf, 4); - ++packets_queued; - TU_LOG3("MIDI RX=%08x\r\n", packet); - } - buf += 4; - } - } - // invoke receive callback if available - if (tuh_midi_rx_cb) - { - tuh_midi_rx_cb(dev_addr, packets_queued); - } +bool midih_deinit(void) { + for (size_t i = 0; i < CFG_TUH_MIDI; i++) { + midih_interface_t* p_midi = &_midi_host[i]; + tu_edpt_stream_deinit(&p_midi->ep_stream.rx); + tu_edpt_stream_deinit(&p_midi->ep_stream.tx); } - else if ( ep_addr == p_midi_host->ep_out ) - { - if (0 == write_flush(dev_addr, p_midi_host)) - { - // If there is no data left, a ZLP should be sent if - // xferred_bytes is multiple of EP size and not zero - if ( !tu_fifo_count(&p_midi_host->tx_ff) && xferred_bytes && (0 == (xferred_bytes % p_midi_host->ep_out_max)) ) - { - if ( usbh_edpt_claim(dev_addr, p_midi_host->ep_out) ) - { - TU_ASSERT(usbh_edpt_xfer(dev_addr, p_midi_host->ep_out, XFER_RESULT_SUCCESS, 0)); - } - } - } - if (tuh_midi_tx_cb) - { - tuh_midi_tx_cb(dev_addr); - } - } - return true; } @@ -221,26 +154,67 @@ void midih_close(uint8_t dev_addr) { if (tuh_midi_umount_cb) { tuh_midi_umount_cb(dev_addr, 0); } - tu_fifo_clear(&p_midi_host->rx_ff); - tu_fifo_clear(&p_midi_host->tx_ff); p_midi_host->ep_in = 0; - p_midi_host->ep_in_max = 0; p_midi_host->ep_out = 0; - p_midi_host->ep_out_max = 0; p_midi_host->itf_num = 0; p_midi_host->num_cables_rx = 0; p_midi_host->num_cables_tx = 0; - p_midi_host->dev_addr = 255; // invalid + p_midi_host->dev_addr = 0; p_midi_host->configured = false; tu_memclr(&p_midi_host->stream_read, sizeof(p_midi_host->stream_read)); tu_memclr(&p_midi_host->stream_write, sizeof(p_midi_host->stream_write)); + + tu_edpt_stream_close(&p_midi_host->ep_stream.rx); + tu_edpt_stream_close(&p_midi_host->ep_stream.tx); +} + +bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { + (void) result; + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); + TU_VERIFY(p_midi_host != NULL); + if (ep_addr == p_midi_host->ep_stream.rx.ep_addr) { + // receive new data if available + if (xferred_bytes) { + // put in the RX FIFO only non-zero MIDI IN 4-byte packets + uint32_t packets_queued = 0; + uint8_t *buf = _midi_epbuf->rx; + const uint32_t npackets = xferred_bytes / 4; + for (uint32_t p = 0; p < npackets; p++) { + // some devices send back all zero packets even if there is no data ready + const uint32_t packet = tu_unaligned_read32(buf); + if (packet != 0) { + tu_edpt_stream_read_xfer_complete_with_buf(&p_midi_host->ep_stream.rx, buf, 4); + ++packets_queued; + TU_LOG3("MIDI RX=%08x\r\n", packet); + } + buf += 4; + } + + if (tuh_midi_rx_cb) { + tuh_midi_rx_cb(dev_addr, packets_queued); // invoke receive callback + } + } + + // prepare for next transfer if needed + tu_edpt_stream_read_xfer(dev_addr, &p_midi_host->ep_stream.rx); + } else if (ep_addr == p_midi_host->ep_stream.tx.ep_addr) { + if (tuh_midi_tx_cb) { + tuh_midi_tx_cb(dev_addr); + } + if (0 == tu_edpt_stream_write_xfer(dev_addr, &p_midi_host->ep_stream.tx)) { + // If there is no data left, a ZLP should be sent if + // xferred_bytes is multiple of EP size and not zero + tu_edpt_stream_write_zlp_if_needed(dev_addr, &p_midi_host->ep_stream.tx, xferred_bytes); + } + } + + return true; } //--------------------------------------------------------------------+ // Enumeration //--------------------------------------------------------------------+ -bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len) -{ +bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len) { (void) rhport; midih_interface_t *p_midi_host = find_new_midi(); @@ -402,20 +376,12 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d TU_VERIFY(p_midi_host->ep_out == 0); TU_VERIFY(p_midi_host->num_cables_tx == 0); p_midi_host->ep_out = p_ep->bEndpointAddress; - p_midi_host->ep_out_max = p_ep->wMaxPacketSize; - if (p_midi_host->ep_out_max > CFG_TUH_MIDI_TX_BUFSIZE) { - p_midi_host->ep_out_max = CFG_TUH_MIDI_TX_BUFSIZE; - } prev_ep_addr = p_midi_host->ep_out; out_desc = p_ep; } else { TU_VERIFY(p_midi_host->ep_in == 0); TU_VERIFY(p_midi_host->num_cables_rx == 0); p_midi_host->ep_in = p_ep->bEndpointAddress; - p_midi_host->ep_in_max = p_ep->wMaxPacketSize; - if (p_midi_host->ep_in_max > CFG_TUH_MIDI_RX_BUFSIZE) { - p_midi_host->ep_in_max = CFG_TUH_MIDI_RX_BUFSIZE; - } prev_ep_addr = p_midi_host->ep_in; in_desc = p_ep; } @@ -439,36 +405,34 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d } if (in_desc) { TU_ASSERT(tuh_edpt_open(dev_addr, in_desc)); - // Some devices always return exactly the request length so transfers won't complete - // unless you assume every transfer is the last one. - // TODO usbh_edpt_force_last_buffer(dev_addr, p_midi_host->ep_in, true); + tu_edpt_stream_open(&p_midi_host->ep_stream.rx, in_desc); } if (out_desc) { TU_ASSERT(tuh_edpt_open(dev_addr, out_desc)); + tu_edpt_stream_open(&p_midi_host->ep_stream.tx, out_desc); } p_midi_host->dev_addr = dev_addr; - if (tuh_midi_mount_cb) { - tuh_midi_mount_cb(dev_addr, p_midi_host->ep_in, p_midi_host->ep_out, p_midi_host->num_cables_rx, p_midi_host->num_cables_tx); - } return true; } -bool tuh_midi_configured(uint8_t dev_addr) -{ +bool tuh_midi_configured(uint8_t dev_addr) { midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); return p_midi_host->configured; } -bool midih_set_config(uint8_t dev_addr, uint8_t itf_num) -{ +bool midih_set_config(uint8_t dev_addr, uint8_t itf_num) { (void) itf_num; midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); p_midi_host->configured = true; - // TODO I don't think there are any special config things to do for MIDI + if (tuh_midi_mount_cb) { + tuh_midi_mount_cb(dev_addr, p_midi_host->ep_in, p_midi_host->ep_out, p_midi_host->num_cables_rx, p_midi_host->num_cables_tx); + } + + // No special config things to do for MIDI usbh_driver_set_config_complete(dev_addr, p_midi_host->itf_num); return true; } @@ -476,71 +440,32 @@ bool midih_set_config(uint8_t dev_addr, uint8_t itf_num) //--------------------------------------------------------------------+ // Stream API //--------------------------------------------------------------------+ -static uint32_t write_flush(uint8_t dev_addr, midih_interface_t* midi) { - // No data to send - if ( !tu_fifo_count(&midi->tx_ff) ) { return 0; } - - // skip if previous transfer not complete - TU_VERIFY( usbh_edpt_claim(dev_addr, midi->ep_out) ); - - uint16_t count = tu_fifo_read_n(&midi->tx_ff, midi->epout_buf, midi->ep_out_max); - - if (count) { - TU_ASSERT(usbh_edpt_xfer(dev_addr, midi->ep_out, midi->epout_buf, count), 0); - return count; - } else { - // Release endpoint since we don't make any transfer - usbh_edpt_release(dev_addr, midi->ep_out); - return 0; - } -} - bool tuh_midi_read_poll(uint8_t dev_addr) { midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); - bool result = false; - - bool in_edpt_not_busy = !usbh_edpt_busy(dev_addr, p_midi_host->ep_in); - if (in_edpt_not_busy) { - TU_LOG2("Requesting poll IN endpoint %d\r\n", p_midi_host->ep_in); - TU_ASSERT(usbh_edpt_xfer(p_midi_host->dev_addr, p_midi_host->ep_in, p_midi_host->epin_buf, p_midi_host->ep_in_max), 0); - result = true; - } else { - // Maybe the IN endpoint is only busy because the RP2040 host hardware - // is retrying a NAK'd IN transfer forever. Try aborting the NAK'd - // transfer to allow other transfers to happen on the one shared - // epx endpoint. - // TODO for RP2040 USB shared endpoint: usbh_edpt_clear_in_on_nak(p_midi_host->dev_addr, p_midi_host->ep_in); - } - return result; + return tu_edpt_stream_read_xfer(dev_addr, &p_midi_host->ep_stream.rx) > 0; } -uint32_t tuh_midi_stream_write (uint8_t dev_addr, uint8_t cable_num, uint8_t const* buffer, uint32_t bufsize) -{ +uint32_t tuh_midi_stream_write(uint8_t dev_addr, uint8_t cable_num, uint8_t const *buffer, uint32_t bufsize) { midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); TU_VERIFY(cable_num < p_midi_host->num_cables_tx); midi_stream_t *stream = &p_midi_host->stream_write; uint32_t i = 0; - while ( (i < bufsize) && (tu_fifo_remaining(&p_midi_host->tx_ff) >= 4) ) - { + while ((i < bufsize) && (tu_edpt_stream_write_available(dev_addr, &p_midi_host->ep_stream.tx) >= 4)) { uint8_t const data = buffer[i]; i++; - if (data >= MIDI_STATUS_SYSREAL_TIMING_CLOCK) - { + if (data >= MIDI_STATUS_SYSREAL_TIMING_CLOCK) { // real-time messages need to be sent right away midi_stream_t streamrt; streamrt.buffer[0] = MIDI_CIN_SYSEX_END_1BYTE; streamrt.buffer[1] = data; streamrt.index = 2; streamrt.total = 2; - uint16_t const count = tu_fifo_write_n(&p_midi_host->tx_ff, streamrt.buffer, 4); - // FIFO overflown, since we already check fifo remaining. It is probably race condition - TU_ASSERT(count == 4, i); - } - else if ( stream->index == 0 ) - { + uint32_t const count = tu_edpt_stream_write(dev_addr, &p_midi_host->ep_stream.tx, streamrt.buffer, 4); + TU_ASSERT(count == 4, i); // Check FIFO overflown, since we already check fifo remaining. It is probably race condition + } else if (stream->index == 0) { //------------- New event packet -------------// uint8_t const msg = data >> 4; @@ -549,56 +474,37 @@ uint32_t tuh_midi_stream_write (uint8_t dev_addr, uint8_t cable_num, uint8_t con stream->buffer[1] = data; // Check to see if we're still in a SysEx transmit. - if ( stream->buffer[0] == MIDI_CIN_SYSEX_START ) - { - if ( data == MIDI_STATUS_SYSEX_END ) - { + if (stream->buffer[0] == MIDI_CIN_SYSEX_START) { + if (data == MIDI_STATUS_SYSEX_END) { stream->buffer[0] = MIDI_CIN_SYSEX_END_1BYTE; stream->total = 2; - } - else - { + } else { stream->total = 4; } - } - else if ( (msg >= 0x8 && msg <= 0xB) || msg == 0xE ) - { + } else if ((msg >= 0x8 && msg <= 0xB) || msg == 0xE) { // Channel Voice Messages stream->buffer[0] = (uint8_t) ((cable_num << 4) | msg); stream->total = 4; - } - else if ( msg == 0xC || msg == 0xD) - { + } 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; - } - else if ( msg == 0xf ) - { + } else if (msg == 0xf) { // System message - if ( data == MIDI_STATUS_SYSEX_START ) - { + if (data == MIDI_STATUS_SYSEX_START) { stream->buffer[0] = MIDI_CIN_SYSEX_START; stream->total = 4; - } - else if ( data == MIDI_STATUS_SYSCOM_TIME_CODE_QUARTER_FRAME || data == MIDI_STATUS_SYSCOM_SONG_SELECT ) - { + } else if (data == MIDI_STATUS_SYSCOM_TIME_CODE_QUARTER_FRAME || data == MIDI_STATUS_SYSCOM_SONG_SELECT) { stream->buffer[0] = MIDI_CIN_SYSCOM_2BYTE; stream->total = 3; - } - else if ( data == MIDI_STATUS_SYSCOM_SONG_POSITION_POINTER ) - { + } else if (data == MIDI_STATUS_SYSCOM_SONG_POSITION_POINTER) { stream->buffer[0] = MIDI_CIN_SYSCOM_3BYTE; stream->total = 4; - } - else - { + } else { stream->buffer[0] = MIDI_CIN_SYSEX_END_1BYTE; stream->total = 2; } - } - else - { + } else { // Pack individual bytes if we don't support packing them into words. stream->buffer[0] = (uint8_t) (cable_num << 4 | 0xf); stream->buffer[2] = 0; @@ -606,30 +512,25 @@ uint32_t tuh_midi_stream_write (uint8_t dev_addr, uint8_t cable_num, uint8_t con stream->index = 2; stream->total = 2; } - } - else - { + } else { //------------- On-going (buffering) packet -------------// - TU_ASSERT(stream->index < 4, i); stream->buffer[stream->index] = data; stream->index++; // See if this byte ends a SysEx. - if ( stream->buffer[0] == MIDI_CIN_SYSEX_START && data == MIDI_STATUS_SYSEX_END ) - { + if (stream->buffer[0] == MIDI_CIN_SYSEX_START && data == MIDI_STATUS_SYSEX_END) { stream->buffer[0] = MIDI_CIN_SYSEX_START + (stream->index - 1); stream->total = stream->index; } } // Send out packet - if ( stream->index >= 2 && stream->index == stream->total ) - { + if (stream->index >= 2 && stream->index == stream->total) { // zeroes unused bytes - for(uint8_t idx = stream->total; idx < 4; idx++) stream->buffer[idx] = 0; + for (uint8_t idx = stream->total; idx < 4; idx++) { stream->buffer[idx] = 0; } TU_LOG3_MEM(stream->buffer, 4, 2); - uint16_t const count = tu_fifo_write_n(&p_midi_host->tx_ff, stream->buffer, 4); + uint32_t const count = tu_edpt_stream_write(dev_addr, &p_midi_host->ep_stream.tx, stream->buffer, 4); // complete current event packet, reset stream stream->index = 0; @@ -642,32 +543,16 @@ uint32_t tuh_midi_stream_write (uint8_t dev_addr, uint8_t cable_num, uint8_t con return i; } -bool tuh_midi_packet_write (uint8_t dev_addr, uint8_t const packet[4]) -{ +bool tuh_midi_packet_write (uint8_t dev_addr, uint8_t const packet[4]) { midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); - - if (tu_fifo_remaining(&p_midi_host->tx_ff) < 4) - { - return false; - } - - tu_fifo_write_n(&p_midi_host->tx_ff, packet, 4); - - return true; + return 4 == tu_edpt_stream_write(dev_addr, &p_midi_host->ep_stream.tx, packet, 4); } -uint32_t tuh_midi_stream_flush( uint8_t dev_addr ) -{ - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - - uint32_t bytes_flushed = 0; - if (!usbh_edpt_busy(p_midi_host->dev_addr, p_midi_host->ep_out)) - { - bytes_flushed = write_flush(dev_addr, p_midi_host); - } - return bytes_flushed; +uint32_t tuh_midi_stream_flush(uint8_t dev_addr) { + midih_interface_t *p_midi = find_midi_by_daddr(dev_addr); + TU_VERIFY(p_midi != NULL); + return tu_edpt_stream_write_xfer(p_midi->dev_addr, &p_midi->ep_stream.tx); } //--------------------------------------------------------------------+ // Helper @@ -692,12 +577,11 @@ bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]) { midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); - TU_VERIFY(tu_fifo_count(&p_midi_host->rx_ff) >= 4); - return tu_fifo_read_n(&p_midi_host->rx_ff, packet, 4) == 4; + TU_VERIFY(tu_edpt_stream_read_available(&p_midi_host->ep_stream.rx) >= 4); + return 4 == tu_edpt_stream_read(dev_addr, &p_midi_host->ep_stream.rx, packet, 4); } -uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p_buffer, uint16_t bufsize) -{ +uint32_t tuh_midi_stream_read(uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p_buffer, uint16_t bufsize) { midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); uint32_t bytes_buffered = 0; @@ -705,54 +589,40 @@ uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t * TU_ASSERT(p_buffer); TU_ASSERT(bufsize); uint8_t one_byte; - if (!tu_fifo_peek(&p_midi_host->rx_ff, &one_byte)) - { + if (!tu_edpt_stream_peek(&p_midi_host->ep_stream.rx, &one_byte)) { return 0; } *p_cable_num = (one_byte >> 4) & 0xf; - uint32_t nread = tu_fifo_read_n(&p_midi_host->rx_ff, p_midi_host->stream_read.buffer, 4); + uint32_t nread = tu_edpt_stream_read(dev_addr, &p_midi_host->ep_stream.rx, p_midi_host->stream_read.buffer, 4); static uint16_t cable_sysex_in_progress; // bit i is set if received MIDI_STATUS_SYSEX_START but not MIDI_STATUS_SYSEX_END - while (nread == 4 && bytes_buffered < bufsize) - { - *p_cable_num=(p_midi_host->stream_read.buffer[0] >> 4) & 0x0f; + while (nread == 4 && bytes_buffered < bufsize) { + *p_cable_num = (p_midi_host->stream_read.buffer[0] >> 4) & 0x0f; uint8_t bytes_to_add_to_stream = 0; - if (*p_cable_num < p_midi_host->num_cables_rx) - { + if (*p_cable_num < p_midi_host->num_cables_rx) { // ignore the CIN field; too many devices out there encode this wrong uint8_t status = p_midi_host->stream_read.buffer[1]; uint16_t cable_mask = (uint16_t) (1 << *p_cable_num); - if (status <= MIDI_MAX_DATA_VAL || status == MIDI_STATUS_SYSEX_START) - { - if (status == MIDI_STATUS_SYSEX_START) - { + if (status <= MIDI_MAX_DATA_VAL || status == MIDI_STATUS_SYSEX_START) { + if (status == MIDI_STATUS_SYSEX_START) { cable_sysex_in_progress |= cable_mask; } // only add the packet if a sysex message is in progress - if (cable_sysex_in_progress & cable_mask) - { + if (cable_sysex_in_progress & cable_mask) { ++bytes_to_add_to_stream; - uint8_t idx; - for (idx = 2; idx < 4; idx++) - { - if (p_midi_host->stream_read.buffer[idx] <= MIDI_MAX_DATA_VAL) - { + for (uint8_t idx = 2; idx < 4; idx++) { + if (p_midi_host->stream_read.buffer[idx] <= MIDI_MAX_DATA_VAL) { ++bytes_to_add_to_stream; - } - else if (p_midi_host->stream_read.buffer[idx] == MIDI_STATUS_SYSEX_END) - { + } else if (p_midi_host->stream_read.buffer[idx] == MIDI_STATUS_SYSEX_END) { ++bytes_to_add_to_stream; cable_sysex_in_progress &= (uint16_t) ~cable_mask; - idx = 4; // force the loop to exit; I hate break statements in loops + idx = 4;// force the loop to exit; I hate break statements in loops } } } - } - else if (status < MIDI_STATUS_SYSEX_START) - { + } else if (status < MIDI_STATUS_SYSEX_START) { // then it is a channel message either three bytes or two uint8_t fake_cin = (status & 0xf0) >> 4; - switch (fake_cin) - { + switch (fake_cin) { case MIDI_CIN_NOTE_OFF: case MIDI_CIN_NOTE_ON: case MIDI_CIN_POLY_KEYPRESS: @@ -765,14 +635,11 @@ uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t * bytes_to_add_to_stream = 2; break; default: - break; // Should not get this + break;// Should not get this } - cable_sysex_in_progress &= (uint16_t)~cable_mask; - } - else if (status < MIDI_STATUS_SYSREAL_TIMING_CLOCK) - { - switch (status) - { + cable_sysex_in_progress &= (uint16_t) ~cable_mask; + } else if (status < MIDI_STATUS_SYSREAL_TIMING_CLOCK) { + switch (status) { case MIDI_STATUS_SYSCOM_TIME_CODE_QUARTER_FRAME: case MIDI_STATUS_SYSCOM_SONG_SELECT: bytes_to_add_to_stream = 2; @@ -786,30 +653,24 @@ uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t * break; default: break; - cable_sysex_in_progress &= (uint16_t)~cable_mask; + cable_sysex_in_progress &= (uint16_t) ~cable_mask; } - } - else - { + } else { // Real-time message: can be inserted into a sysex message, // so do don't clear cable_sysex_in_progress bit bytes_to_add_to_stream = 1; } } - uint8_t idx; - for (idx = 1; idx <= bytes_to_add_to_stream; idx++) - { + for (uint8_t idx = 1; idx <= bytes_to_add_to_stream; idx++) { *p_buffer++ = p_midi_host->stream_read.buffer[idx]; } bytes_buffered += bytes_to_add_to_stream; nread = 0; - if (tu_fifo_peek(&p_midi_host->rx_ff, &one_byte)) - { + if (tu_edpt_stream_peek(&p_midi_host->ep_stream.rx, &one_byte)) { uint8_t new_cable = (one_byte >> 4) & 0xf; - if (new_cable == *p_cable_num) - { + if (new_cable == *p_cable_num) { // still on the same cable. Continue reading the stream - nread = tu_fifo_read_n(&p_midi_host->rx_ff, p_midi_host->stream_read.buffer, 4); + nread = tu_edpt_stream_read(dev_addr, &p_midi_host->ep_stream.rx, p_midi_host->stream_read.buffer, 4); } } } @@ -865,19 +726,14 @@ static uint8_t find_string_index(midih_interface_t *ptr, uint8_t jack_id) #endif #if CFG_MIDI_HOST_DEVSTRINGS -uint8_t tuh_midi_get_rx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings) -{ - uint8_t nstrings = 0; +uint8_t tuh_midi_get_rx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings) { midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - nstrings = p_midi_host->num_cables_rx; - if (nstrings > max_istrings) - { - nstrings = max_istrings; + TU_VERIFY(p_midi_host != NULL, 0); + uint8_t nstrings = p_midi_host->num_cables_rx; + if (nstrings > max_istrings) { + nstrings = max_istrings; } - uint8_t jack; - for (jack=0; jackep_in_associated_jacks[jack]; istrings[jack] = find_string_index(p_midi_host, jack_id); } @@ -886,17 +742,13 @@ uint8_t tuh_midi_get_rx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint uint8_t tuh_midi_get_tx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings) { - uint8_t nstrings = 0; midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - nstrings = p_midi_host->num_cables_tx; - if (nstrings > max_istrings) - { - nstrings = max_istrings; + TU_VERIFY(p_midi_host != NULL, 0); + uint8_t nstrings = p_midi_host->num_cables_tx; + if (nstrings > max_istrings) { + nstrings = max_istrings; } - uint8_t jack; - for (jack=0; jackep_out_associated_jacks[jack]; istrings[jack] = find_string_index(p_midi_host, jack_id); } @@ -909,8 +761,7 @@ uint8_t tuh_midi_get_all_istrings(uint8_t dev_addr, const uint8_t** istrings) midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); uint8_t nstrings = p_midi_host->num_string_indices; - if (nstrings) - *istrings = p_midi_host->all_string_indices; + if (nstrings) { *istrings = p_midi_host->all_string_indices; } return nstrings; } #endif diff --git a/src/class/midi/midi_host.h b/src/class/midi/midi_host.h index bf2378168..6401eb0c3 100644 --- a/src/class/midi/midi_host.h +++ b/src/class/midi/midi_host.h @@ -42,15 +42,15 @@ #endif #ifndef CFG_TUH_MIDI_RX_BUFSIZE -#define CFG_TUH_MIDI_RX_BUFSIZE 64 +#define CFG_TUH_MIDI_RX_BUFSIZE TUH_EPSIZE_BULK_MPS #endif #ifndef CFG_TUH_MIDI_TX_BUFSIZE -#define CFG_TUH_MIDI_TX_BUFSIZE 64 +#define CFG_TUH_MIDI_TX_BUFSIZE TUH_EPSIZE_BULK_MPS #endif #ifndef CFG_TUH_MIDI_EP_BUFSIZE -#define CFG_TUH_MIDI_EP_BUFSIZE 64 +#define CFG_TUH_MIDI_EP_BUFSIZE TUH_EPSIZE_BULK_MPS #endif #ifndef CFG_MIDI_HOST_DEVSTRINGS @@ -113,14 +113,6 @@ uint8_t tuh_midi_get_rx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint uint8_t tuh_midi_get_tx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings); uint8_t tuh_midi_get_all_istrings(uint8_t dev_addr, const uint8_t** istrings); #endif -//--------------------------------------------------------------------+ -// Internal Class Driver API -//--------------------------------------------------------------------+ -bool midih_init (void); -bool midih_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len); -bool midih_set_config (uint8_t dev_addr, uint8_t itf_num); -bool midih_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); -void midih_close (uint8_t dev_addr); //--------------------------------------------------------------------+ // Callbacks (Weak is optional) @@ -138,6 +130,17 @@ TU_ATTR_WEAK void tuh_midi_umount_cb(uint8_t dev_addr, uint8_t instance); TU_ATTR_WEAK void tuh_midi_rx_cb(uint8_t dev_addr, uint32_t num_packets); TU_ATTR_WEAK void tuh_midi_tx_cb(uint8_t dev_addr); + +//--------------------------------------------------------------------+ +// Internal Class Driver API +//--------------------------------------------------------------------+ +bool midih_init (void); +bool midih_deinit (void); +bool midih_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len); +bool midih_set_config (uint8_t dev_addr, uint8_t itf_num); +bool midih_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); +void midih_close (uint8_t dev_addr); + #ifdef __cplusplus } #endif diff --git a/src/common/tusb_private.h b/src/common/tusb_private.h index c71775abb..445882243 100644 --- a/src/common/tusb_private.h +++ b/src/common/tusb_private.h @@ -138,7 +138,7 @@ uint32_t tu_edpt_stream_read(uint8_t hwid, tu_edpt_stream_t* s, void* buffer, ui // Start an usb transfer if endpoint is not busy uint32_t tu_edpt_stream_read_xfer(uint8_t hwid, tu_edpt_stream_t* s); -// Must be called in the transfer complete callback +// Complete read transfer by writing EP -> FIFO. Must be called in the transfer complete callback TU_ATTR_ALWAYS_INLINE static inline void tu_edpt_stream_read_xfer_complete(tu_edpt_stream_t* s, uint32_t xferred_bytes) { if (tu_fifo_depth(&s->ff)) { @@ -146,11 +146,11 @@ void tu_edpt_stream_read_xfer_complete(tu_edpt_stream_t* s, uint32_t xferred_byt } } -// Same as tu_edpt_stream_read_xfer_complete but skip the first n bytes +// Complete read transfer with provided buffer TU_ATTR_ALWAYS_INLINE static inline -void tu_edpt_stream_read_xfer_complete_offset(tu_edpt_stream_t* s, uint32_t xferred_bytes, uint32_t skip_offset) { - if (tu_fifo_depth(&s->ff) && (skip_offset < xferred_bytes)) { - tu_fifo_write_n(&s->ff, s->ep_buf + skip_offset, (uint16_t) (xferred_bytes - skip_offset)); +void tu_edpt_stream_read_xfer_complete_with_buf(tu_edpt_stream_t* s, const void * buf, uint32_t xferred_bytes) { + if (tu_fifo_depth(&s->ff)) { + tu_fifo_write_n(&s->ff, buf, (uint16_t) xferred_bytes); } } diff --git a/src/host/usbh.c b/src/host/usbh.c index f874121a3..e6b6cd91c 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -192,6 +192,7 @@ static usbh_class_driver_t const usbh_class_drivers[] = { { .name = DRIVER_NAME("MIDI"), .init = midih_init, + .deinit = midih_deinit, .open = midih_open, .set_config = midih_set_config, .xfer_cb = midih_xfer_cb, @@ -1672,7 +1673,7 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur if ( 0 == tu_desc_len(p_desc) ) { // A zero length descriptor indicates that the device is off spec (e.g. wrong wTotalLength). // Parsed interfaces should still be usable - TU_LOG_USBH("Encountered a zero-length descriptor after %u bytes\r\n", (uint32_t)p_desc - (uint32_t)desc_cfg); + TU_LOG_USBH("Encountered a zero-length descriptor after %" PRIu32 " bytes\r\n", (uint32_t)p_desc - (uint32_t)desc_cfg); break; } diff --git a/src/host/usbh.h b/src/host/usbh.h index 3bcdd3657..7fc8ea826 100644 --- a/src/host/usbh.h +++ b/src/host/usbh.h @@ -37,6 +37,9 @@ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ +// Endpoint Bulk size depending on host mx speed +#define TUH_EPSIZE_BULK_MPS (TUD_OPT_HIGH_SPEED ? TUSB_EPSIZE_BULK_HS : TUSB_EPSIZE_BULK_FS) + // forward declaration struct tuh_xfer_s; typedef struct tuh_xfer_s tuh_xfer_t; From ed88fc983fcc8bd0bcc27b9982b21223a580f59d Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 14 Feb 2025 10:41:05 +0700 Subject: [PATCH 21/30] - remove tuh_midi_read_poll(), auto schedule EP in when set_config() and xfer_cb as well as ep read() - de-dup tuh_midi_get_num_rx/tx_cables - add tuh_midi_read_available() --- examples/host/midi_rx/src/main.c | 5 +-- src/class/midi/midi_host.c | 67 ++++++++++---------------------- src/class/midi/midi_host.h | 51 ++++++++++++------------ 3 files changed, 50 insertions(+), 73 deletions(-) diff --git a/examples/host/midi_rx/src/main.c b/examples/host/midi_rx/src/main.c index a03fd588f..0b07ba6e7 100644 --- a/examples/host/midi_rx/src/main.c +++ b/examples/host/midi_rx/src/main.c @@ -38,7 +38,7 @@ static uint8_t midi_dev_addr = 0; //--------------------------------------------------------------------+ -// MACRO CONSTANT TYPEDEF PROTYPES +// MACRO CONSTANT TYPEDEF PROTOTYPES //--------------------------------------------------------------------+ void led_blinking_task(void); void midi_host_rx_task(void); @@ -92,10 +92,9 @@ void midi_host_rx_task(void) { if (!midi_dev_addr || !tuh_midi_configured(midi_dev_addr)) { return; } - if (tuh_midih_get_num_rx_cables(midi_dev_addr) < 1) { + if (tuh_midi_get_num_rx_cables(midi_dev_addr) < 1) { return; } - tuh_midi_read_poll(midi_dev_addr); } //--------------------------------------------------------------------+ diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index eb3f0b070..7d6eb35fa 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -26,7 +26,7 @@ #include "tusb_option.h" -#if (TUSB_OPT_HOST_ENABLED && CFG_TUH_MIDI) +#if (CFG_TUH_ENABLED && CFG_TUH_MIDI) #include "host/usbh.h" #include "host/usbh_pvt.h" @@ -195,8 +195,7 @@ bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint } } - // prepare for next transfer if needed - tu_edpt_stream_read_xfer(dev_addr, &p_midi_host->ep_stream.rx); + tu_edpt_stream_read_xfer(dev_addr, &p_midi_host->ep_stream.rx); // prepare for next transfer } else if (ep_addr == p_midi_host->ep_stream.tx.ep_addr) { if (tuh_midi_tx_cb) { tuh_midi_tx_cb(dev_addr); @@ -416,12 +415,6 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d return true; } -bool tuh_midi_configured(uint8_t dev_addr) { - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - return p_midi_host->configured; -} - bool midih_set_config(uint8_t dev_addr, uint8_t itf_num) { (void) itf_num; midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); @@ -432,18 +425,20 @@ bool midih_set_config(uint8_t dev_addr, uint8_t itf_num) { tuh_midi_mount_cb(dev_addr, p_midi_host->ep_in, p_midi_host->ep_out, p_midi_host->num_cables_rx, p_midi_host->num_cables_tx); } + tu_edpt_stream_read_xfer(dev_addr, &p_midi_host->ep_stream.rx); // prepare for incoming data + // No special config things to do for MIDI usbh_driver_set_config_complete(dev_addr, p_midi_host->itf_num); return true; } //--------------------------------------------------------------------+ -// Stream API +// API //--------------------------------------------------------------------+ -bool tuh_midi_read_poll(uint8_t dev_addr) { +bool tuh_midi_configured(uint8_t dev_addr) { midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); - return tu_edpt_stream_read_xfer(dev_addr, &p_midi_host->ep_stream.rx) > 0; + return p_midi_host->configured; } uint32_t tuh_midi_stream_write(uint8_t dev_addr, uint8_t cable_num, uint8_t const *buffer, uint32_t bufsize) { @@ -557,24 +552,27 @@ uint32_t tuh_midi_stream_flush(uint8_t dev_addr) { //--------------------------------------------------------------------+ // Helper //--------------------------------------------------------------------+ -uint8_t tuh_midih_get_num_tx_cables (uint8_t dev_addr) -{ +uint8_t tuh_midi_get_num_tx_cables (uint8_t dev_addr) { midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - TU_VERIFY(p_midi_host->ep_out != 0); // returns 0 if fails + TU_VERIFY(p_midi_host != NULL, 0); + TU_VERIFY(p_midi_host->ep_out != 0, 0); return p_midi_host->num_cables_tx; } -uint8_t tuh_midih_get_num_rx_cables (uint8_t dev_addr) -{ +uint8_t tuh_midi_get_num_rx_cables (uint8_t dev_addr) { midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - TU_VERIFY(p_midi_host->ep_in != 0); // returns 0 if fails + TU_VERIFY(p_midi_host != NULL, 0); + TU_VERIFY(p_midi_host->ep_in != 0, 0); return p_midi_host->num_cables_rx; } -bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]) -{ +uint32_t tuh_midi_read_available(uint8_t dev_addr) { + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); + TU_VERIFY(p_midi_host != NULL); + return tu_edpt_stream_read_available(&p_midi_host->ep_stream.rx); +} + +bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]) { midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); TU_VERIFY(tu_edpt_stream_read_available(&p_midi_host->ep_stream.rx) >= 4); @@ -661,6 +659,7 @@ uint32_t tuh_midi_stream_read(uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p bytes_to_add_to_stream = 1; } } + for (uint8_t idx = 1; idx <= bytes_to_add_to_stream; idx++) { *p_buffer++ = p_midi_host->stream_read.buffer[idx]; } @@ -678,30 +677,6 @@ uint32_t tuh_midi_stream_read(uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p return bytes_buffered; } -uint8_t tuh_midi_get_num_rx_cables(uint8_t dev_addr) -{ - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - uint8_t num_cables = 0; - if (p_midi_host) - { - num_cables = p_midi_host->num_cables_rx; - } - return num_cables; -} - -uint8_t tuh_midi_get_num_tx_cables(uint8_t dev_addr) -{ - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - uint8_t num_cables = 0; - if (p_midi_host) - { - num_cables = p_midi_host->num_cables_tx; - } - return num_cables; -} - #if CFG_MIDI_HOST_DEVSTRINGS static uint8_t find_string_index(midih_interface_t *ptr, uint8_t jack_id) { diff --git a/src/class/midi/midi_host.h b/src/class/midi/midi_host.h index 6401eb0c3..6529f2bbc 100644 --- a/src/class/midi/midi_host.h +++ b/src/class/midi/midi_host.h @@ -58,22 +58,34 @@ #endif //--------------------------------------------------------------------+ -// Application API (Single Interface) +// Application API //--------------------------------------------------------------------+ bool tuh_midi_configured (uint8_t dev_addr); -// return the number of virtual midi cables on the device's OUT endpoint -uint8_t tuh_midih_get_num_tx_cables (uint8_t dev_addr); - // return the number of virtual midi cables on the device's IN endpoint -uint8_t tuh_midih_get_num_rx_cables (uint8_t dev_addr); +uint8_t tuh_midi_get_num_rx_cables(uint8_t dev_addr); -// request available data from the device. tuh_midi_message_received_cb() will -// be called if the device has any data to send. Otherwise, the device will -// respond NAK. This function blocks until the transfer completes or the -// devices sends NAK. -// This function will return false if the hardware is busy. -bool tuh_midi_read_poll( uint8_t dev_addr ); +// return the number of virtual midi cables on the device's OUT endpoint +uint8_t tuh_midi_get_num_tx_cables(uint8_t dev_addr); + +#if CFG_MIDI_HOST_DEVSTRINGS +uint8_t tuh_midi_get_rx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings); +uint8_t tuh_midi_get_tx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings); +uint8_t tuh_midi_get_all_istrings(uint8_t dev_addr, const uint8_t** istrings); +#endif + +// return the raw number of bytes available from device endpoint. +// Note: this is related but not the same as number of stream bytes available. +uint32_t tuh_midi_read_available(uint8_t dev_addr); + +//--------------------------------------------------------------------+ +// Packet API +//--------------------------------------------------------------------+ + +// Read a raw MIDI packet from the connected device +// This function does not parse the packet format +// Return true if a packet was returned +bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]); // Queue a packet to the device. The application // must call tuh_midi_stream_flush to actually have the @@ -82,6 +94,10 @@ bool tuh_midi_read_poll( uint8_t dev_addr ); // Returns true if the packet was successfully queued. bool tuh_midi_packet_write (uint8_t dev_addr, uint8_t const packet[4]); +//--------------------------------------------------------------------+ +// Stream API +//--------------------------------------------------------------------+ + // Queue a message to the device. The application // must call tuh_midi_stream_flush to actually have the // data go out. @@ -101,19 +117,6 @@ uint32_t tuh_midi_stream_flush( uint8_t dev_addr); // it properly. uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p_buffer, uint16_t bufsize); -// Read a raw MIDI packet from the connected device -// This function does not parse the packet format -// Return true if a packet was returned -bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]); - -uint8_t tuh_midi_get_num_rx_cables(uint8_t dev_addr); -uint8_t tuh_midi_get_num_tx_cables(uint8_t dev_addr); -#if CFG_MIDI_HOST_DEVSTRINGS -uint8_t tuh_midi_get_rx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings); -uint8_t tuh_midi_get_tx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings); -uint8_t tuh_midi_get_all_istrings(uint8_t dev_addr, const uint8_t** istrings); -#endif - //--------------------------------------------------------------------+ // Callbacks (Weak is optional) //--------------------------------------------------------------------+ From 31a2696de77d4642d28086ccbe476f40b1bdea7d Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 14 Feb 2025 11:09:50 +0700 Subject: [PATCH 22/30] - change signature of tuh_midi_mount/umount_cb() - rename midi_stream_t to midi_driver_stream_t and move to midi.h (common for device and host) --- examples/host/midi_rx/src/main.c | 22 ++++++---------------- src/class/midi/midi.h | 26 +++++++++++++++----------- src/class/midi/midi_device.c | 16 ++++++---------- src/class/midi/midi_host.c | 24 ++++++++---------------- src/class/midi/midi_host.h | 8 ++------ 5 files changed, 37 insertions(+), 59 deletions(-) diff --git a/examples/host/midi_rx/src/main.c b/examples/host/midi_rx/src/main.c index 0b07ba6e7..fed782947 100644 --- a/examples/host/midi_rx/src/main.c +++ b/examples/host/midi_rx/src/main.c @@ -101,30 +101,20 @@ void midi_host_rx_task(void) { // TinyUSB Callbacks //--------------------------------------------------------------------+ -// Invoked when device with hid interface is mounted -// Report descriptor is also available for use. tuh_hid_parse_report_descriptor() -// can be used to parse common/simple enough descriptor. -// Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE, it will be skipped -// therefore report_desc = NULL, desc_len = 0 -void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t in_ep, uint8_t out_ep, uint8_t num_cables_rx, uint16_t num_cables_tx) { - (void) in_ep; - (void) out_ep; +// Invoked when device with MIDI interface is mounted. +void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t num_cables_rx, uint16_t num_cables_tx) { (void) num_cables_rx; (void) num_cables_tx; - - TU_LOG1("MIDI device address = %u, IN endpoint %u has %u cables, OUT endpoint %u has %u cables\r\n", - dev_addr, in_ep & 0xf, num_cables_rx, out_ep & 0xf, num_cables_tx); - midi_dev_addr = dev_addr; + TU_LOG1("MIDI device address = %u, Number of RX cables = %u, Number of TX cables = %u\r\n", + dev_addr, num_cables_rx, num_cables_tx); } // Invoked when device with hid interface is un-mounted -void tuh_midi_umount_cb(uint8_t dev_addr, uint8_t instance) { +void tuh_midi_umount_cb(uint8_t dev_addr) { (void) dev_addr; - (void) instance; - - TU_LOG1("MIDI device address = %d, instance = %d is unmounted\r\n", dev_addr, instance); midi_dev_addr = 0; + TU_LOG1("MIDI device address = %d is unmounted\r\n", dev_addr); } void tuh_midi_rx_cb(uint8_t dev_addr, uint32_t num_packets) { diff --git a/src/class/midi/midi.h b/src/class/midi/midi.h index 68a784c60..9591f13c4 100644 --- a/src/class/midi/midi.h +++ b/src/class/midi/midi.h @@ -24,13 +24,8 @@ * This file is part of the TinyUSB stack. */ -/** \ingroup group_class - * \defgroup ClassDriver_CDC Communication Device Class (CDC) - * Currently only Abstract Control Model subclass is supported - * @{ */ - -#ifndef _TUSB_MIDI_H__ -#define _TUSB_MIDI_H__ +#ifndef TUSB_MIDI_H_ +#define TUSB_MIDI_H_ #include "common/tusb_common.h" @@ -39,7 +34,7 @@ #endif //--------------------------------------------------------------------+ -// Class Specific Descriptor +// Constants //--------------------------------------------------------------------+ typedef enum @@ -111,6 +106,10 @@ enum MIDI_MAX_DATA_VAL = 0x7F, }; +//--------------------------------------------------------------------+ +// Class Specific Descriptor +//--------------------------------------------------------------------+ + /// MIDI Interface Header Descriptor typedef struct TU_ATTR_PACKED { @@ -216,12 +215,17 @@ typedef struct uint8_t baAssocJackID[]; ; ///< A list of associated jacks } midi_cs_desc_endpoint_t; -/** @} */ +//--------------------------------------------------------------------+ +// For Internal Driver Use +//--------------------------------------------------------------------+ +typedef struct { + uint8_t buffer[4]; + uint8_t index; + uint8_t total; +} midi_driver_stream_t; #ifdef __cplusplus } #endif #endif - -/** @} */ diff --git a/src/class/midi/midi_device.c b/src/class/midi/midi_device.c index dd1883e37..7b6705640 100644 --- a/src/class/midi/midi_device.c +++ b/src/class/midi/midi_device.c @@ -40,11 +40,7 @@ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ -typedef struct { - uint8_t buffer[4]; - uint8_t index; - uint8_t total; -} midid_stream_t; + typedef struct { uint8_t itf_num; @@ -54,8 +50,8 @@ typedef struct { // 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; + midi_driver_stream_t stream_write; + midi_driver_stream_t stream_read; /*------------- From this point, data is not cleared by bus reset -------------*/ // FIFO @@ -122,7 +118,7 @@ uint32_t tud_midi_n_available(uint8_t itf, uint8_t cable_num) (void) cable_num; midid_interface_t* midi = &_midid_itf[itf]; - const midid_stream_t* stream = &midi->stream_read; + const midi_driver_stream_t* 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); @@ -136,7 +132,7 @@ uint32_t tud_midi_n_stream_read(uint8_t itf, uint8_t cable_num, void* buffer, ui uint8_t* buf8 = (uint8_t*) buffer; midid_interface_t* midi = &_midid_itf[itf]; - midid_stream_t* stream = &midi->stream_read; + midi_driver_stream_t* stream = &midi->stream_read; uint32_t total_read = 0; while( bufsize ) @@ -241,7 +237,7 @@ uint32_t tud_midi_n_stream_write(uint8_t itf, uint8_t cable_num, const uint8_t* midid_interface_t* midi = &_midid_itf[itf]; TU_VERIFY(midi->ep_in, 0); - midid_stream_t* stream = &midi->stream_write; + midi_driver_stream_t* stream = &midi->stream_write; uint32_t i = 0; while ( (i < bufsize) && (tu_fifo_remaining(&midi->tx_ff) >= 4) ) diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index 7d6eb35fa..b6ebaa7a9 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -37,14 +37,6 @@ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ -// TODO: refactor to share code with the MIDI Device driver -typedef struct -{ - uint8_t buffer[4]; - uint8_t index; - uint8_t total; -}midi_stream_t; - typedef struct { uint8_t dev_addr; uint8_t itf_num; @@ -58,8 +50,8 @@ typedef struct { // 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. - midi_stream_t stream_write; - midi_stream_t stream_read; + midi_driver_stream_t stream_write; + midi_driver_stream_t stream_read; /*------------- From this point, data is not cleared by bus reset -------------*/ // Endpoint stream @@ -152,7 +144,7 @@ void midih_close(uint8_t dev_addr) { return; } if (tuh_midi_umount_cb) { - tuh_midi_umount_cb(dev_addr, 0); + tuh_midi_umount_cb(dev_addr); } p_midi_host->ep_in = 0; p_midi_host->ep_out = 0; @@ -422,7 +414,7 @@ bool midih_set_config(uint8_t dev_addr, uint8_t itf_num) { p_midi_host->configured = true; if (tuh_midi_mount_cb) { - tuh_midi_mount_cb(dev_addr, p_midi_host->ep_in, p_midi_host->ep_out, p_midi_host->num_cables_rx, p_midi_host->num_cables_tx); + tuh_midi_mount_cb(dev_addr, p_midi_host->num_cables_rx, p_midi_host->num_cables_tx); } tu_edpt_stream_read_xfer(dev_addr, &p_midi_host->ep_stream.rx); // prepare for incoming data @@ -445,7 +437,7 @@ uint32_t tuh_midi_stream_write(uint8_t dev_addr, uint8_t cable_num, uint8_t cons midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); TU_VERIFY(cable_num < p_midi_host->num_cables_tx); - midi_stream_t *stream = &p_midi_host->stream_write; + midi_driver_stream_t *stream = &p_midi_host->stream_write; uint32_t i = 0; while ((i < bufsize) && (tu_edpt_stream_write_available(dev_addr, &p_midi_host->ep_stream.tx) >= 4)) { @@ -453,7 +445,7 @@ uint32_t tuh_midi_stream_write(uint8_t dev_addr, uint8_t cable_num, uint8_t cons i++; if (data >= MIDI_STATUS_SYSREAL_TIMING_CLOCK) { // real-time messages need to be sent right away - midi_stream_t streamrt; + midi_driver_stream_t streamrt; streamrt.buffer[0] = MIDI_CIN_SYSEX_END_1BYTE; streamrt.buffer[1] = data; streamrt.index = 2; @@ -555,14 +547,14 @@ uint32_t tuh_midi_stream_flush(uint8_t dev_addr) { uint8_t tuh_midi_get_num_tx_cables (uint8_t dev_addr) { midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL, 0); - TU_VERIFY(p_midi_host->ep_out != 0, 0); + TU_VERIFY(p_midi_host->ep_stream.tx.ep_addr != 0, 0); return p_midi_host->num_cables_tx; } uint8_t tuh_midi_get_num_rx_cables (uint8_t dev_addr) { midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL, 0); - TU_VERIFY(p_midi_host->ep_in != 0, 0); + TU_VERIFY(p_midi_host->ep_stream.rx.ep_addr != 0, 0); return p_midi_host->num_cables_rx; } diff --git a/src/class/midi/midi_host.h b/src/class/midi/midi_host.h index 6529f2bbc..ab6c43908 100644 --- a/src/class/midi/midi_host.h +++ b/src/class/midi/midi_host.h @@ -122,14 +122,10 @@ uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t * //--------------------------------------------------------------------+ // Invoked when device with MIDI interface is mounted. -// If the MIDI host application requires MIDI IN, it should request an -// IN transfer here. The device will likely NAK this transfer. How the driver -// handles the NAK is hardware dependent. -TU_ATTR_WEAK void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t in_ep, uint8_t out_ep, uint8_t num_cables_rx, uint16_t num_cables_tx); +TU_ATTR_WEAK void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t num_cables_rx, uint16_t num_cables_tx); // Invoked when device with MIDI interface is un-mounted -// For now, the instance parameter is always 0 and can be ignored -TU_ATTR_WEAK void tuh_midi_umount_cb(uint8_t dev_addr, uint8_t instance); +TU_ATTR_WEAK void tuh_midi_umount_cb(uint8_t dev_addr); TU_ATTR_WEAK void tuh_midi_rx_cb(uint8_t dev_addr, uint32_t num_packets); TU_ATTR_WEAK void tuh_midi_tx_cb(uint8_t dev_addr); From 997771fdbaddd4943515e8cdade00c1252b351de Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 14 Feb 2025 16:21:48 +0700 Subject: [PATCH 23/30] - rename tuh_midi_stream_flush() to tuh_midi_write_flush() - add tuh_midi_packet_read_n() and tuh_midi_packet_write_n() - add CFG_TUH_MIDI_STREAM_API to opt out stream API --- src/class/midi/midi_host.c | 102 +++++++++++++++++++++---------------- src/class/midi/midi_host.h | 63 +++++++++++++++-------- 2 files changed, 99 insertions(+), 66 deletions(-) diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index b6ebaa7a9..55146accb 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -47,13 +47,14 @@ typedef struct { uint8_t num_cables_rx; // IN endpoint CS descriptor bNumEmbMIDIJack value uint8_t num_cables_tx; // OUT endpoint CS descriptor bNumEmbMIDIJack value + #if CFG_TUH_MIDI_STREAM_API // 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. midi_driver_stream_t stream_write; midi_driver_stream_t stream_read; + #endif - /*------------- From this point, data is not cleared by bus reset -------------*/ // Endpoint stream struct { tu_edpt_stream_t tx; @@ -433,6 +434,57 @@ bool tuh_midi_configured(uint8_t dev_addr) { return p_midi_host->configured; } +uint8_t tuh_midi_get_num_tx_cables (uint8_t dev_addr) { + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); + TU_VERIFY(p_midi_host != NULL, 0); + TU_VERIFY(p_midi_host->ep_stream.tx.ep_addr != 0, 0); + return p_midi_host->num_cables_tx; +} + +uint8_t tuh_midi_get_num_rx_cables (uint8_t dev_addr) { + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); + TU_VERIFY(p_midi_host != NULL, 0); + TU_VERIFY(p_midi_host->ep_stream.rx.ep_addr != 0, 0); + return p_midi_host->num_cables_rx; +} + +uint32_t tuh_midi_read_available(uint8_t dev_addr) { + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); + TU_VERIFY(p_midi_host != NULL); + return tu_edpt_stream_read_available(&p_midi_host->ep_stream.rx); +} + +uint32_t tuh_midi_write_flush(uint8_t dev_addr) { + midih_interface_t *p_midi = find_midi_by_daddr(dev_addr); + TU_VERIFY(p_midi != NULL); + return tu_edpt_stream_write_xfer(p_midi->dev_addr, &p_midi->ep_stream.tx); +} + +//--------------------------------------------------------------------+ +// Packet API +//--------------------------------------------------------------------+ + +uint32_t tuh_midi_packet_read_n(uint8_t dev_addr, uint8_t* buffer, uint32_t bufsize) { + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); + TU_VERIFY(p_midi_host != NULL); + + uint32_t count4 = tu_min32(bufsize, tu_edpt_stream_read_available(&p_midi_host->ep_stream.rx)); + count4 = tu_align4(count4); // round down to multiple of 4 + TU_VERIFY(count4 > 0, 0); + return tu_edpt_stream_read(dev_addr, &p_midi_host->ep_stream.rx, buffer, count4); +} + +uint32_t tuh_midi_packet_write_n(uint8_t dev_addr, const uint8_t* buffer, uint32_t bufsize) { + midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); + TU_VERIFY(p_midi_host != NULL, 0); + uint32_t bufsize4 = tu_align4(bufsize); + return tu_edpt_stream_write(dev_addr, &p_midi_host->ep_stream.tx, buffer, bufsize4); +} + +//--------------------------------------------------------------------+ +// Stream API +//--------------------------------------------------------------------+ +#if CFG_TUH_MIDI_STREAM_API uint32_t tuh_midi_stream_write(uint8_t dev_addr, uint8_t cable_num, uint8_t const *buffer, uint32_t bufsize) { midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); @@ -530,47 +582,6 @@ uint32_t tuh_midi_stream_write(uint8_t dev_addr, uint8_t cable_num, uint8_t cons return i; } -bool tuh_midi_packet_write (uint8_t dev_addr, uint8_t const packet[4]) { - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - return 4 == tu_edpt_stream_write(dev_addr, &p_midi_host->ep_stream.tx, packet, 4); -} - -uint32_t tuh_midi_stream_flush(uint8_t dev_addr) { - midih_interface_t *p_midi = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi != NULL); - return tu_edpt_stream_write_xfer(p_midi->dev_addr, &p_midi->ep_stream.tx); -} -//--------------------------------------------------------------------+ -// Helper -//--------------------------------------------------------------------+ -uint8_t tuh_midi_get_num_tx_cables (uint8_t dev_addr) { - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL, 0); - TU_VERIFY(p_midi_host->ep_stream.tx.ep_addr != 0, 0); - return p_midi_host->num_cables_tx; -} - -uint8_t tuh_midi_get_num_rx_cables (uint8_t dev_addr) { - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL, 0); - TU_VERIFY(p_midi_host->ep_stream.rx.ep_addr != 0, 0); - return p_midi_host->num_cables_rx; -} - -uint32_t tuh_midi_read_available(uint8_t dev_addr) { - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - return tu_edpt_stream_read_available(&p_midi_host->ep_stream.rx); -} - -bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]) { - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - TU_VERIFY(tu_edpt_stream_read_available(&p_midi_host->ep_stream.rx) >= 4); - return 4 == tu_edpt_stream_read(dev_addr, &p_midi_host->ep_stream.rx, packet, 4); -} - uint32_t tuh_midi_stream_read(uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p_buffer, uint16_t bufsize) { midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); @@ -669,6 +680,9 @@ uint32_t tuh_midi_stream_read(uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p return bytes_buffered; } +//--------------------------------------------------------------------+ +// String API +//--------------------------------------------------------------------+ #if CFG_MIDI_HOST_DEVSTRINGS static uint8_t find_string_index(midih_interface_t *ptr, uint8_t jack_id) { @@ -690,9 +704,7 @@ static uint8_t find_string_index(midih_interface_t *ptr, uint8_t jack_id) } return index; } -#endif -#if CFG_MIDI_HOST_DEVSTRINGS uint8_t tuh_midi_get_rx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings) { midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL, 0); @@ -732,3 +744,5 @@ uint8_t tuh_midi_get_all_istrings(uint8_t dev_addr, const uint8_t** istrings) return nstrings; } #endif + +#endif diff --git a/src/class/midi/midi_host.h b/src/class/midi/midi_host.h index ab6c43908..6e2595e44 100644 --- a/src/class/midi/midi_host.h +++ b/src/class/midi/midi_host.h @@ -24,8 +24,8 @@ * This file is part of the TinyUSB stack. */ -#ifndef _TUSB_MIDI_HOST_H_ -#define _TUSB_MIDI_HOST_H_ +#ifndef TUSB_MIDI_HOST_H_ +#define TUSB_MIDI_HOST_H_ #include "class/audio/audio.h" #include "midi.h" @@ -53,6 +53,12 @@ #define CFG_TUH_MIDI_EP_BUFSIZE TUH_EPSIZE_BULK_MPS #endif +// Enable the MIDI stream read/write API. Some library can work with raw USB MIDI packet +// Disable this can save driver footprint. +#ifndef CFG_TUH_MIDI_STREAM_API +#define CFG_TUH_MIDI_STREAM_API 1 +#endif + #ifndef CFG_MIDI_HOST_DEVSTRINGS #define CFG_MIDI_HOST_DEVSTRINGS 0 #endif @@ -60,7 +66,7 @@ //--------------------------------------------------------------------+ // Application API //--------------------------------------------------------------------+ -bool tuh_midi_configured (uint8_t dev_addr); +bool tuh_midi_configured (uint8_t dev_addr); // return the number of virtual midi cables on the device's IN endpoint uint8_t tuh_midi_get_num_rx_cables(uint8_t dev_addr); @@ -74,40 +80,51 @@ uint8_t tuh_midi_get_tx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint uint8_t tuh_midi_get_all_istrings(uint8_t dev_addr, const uint8_t** istrings); #endif -// return the raw number of bytes available from device endpoint. +// return the raw number of bytes available. // Note: this is related but not the same as number of stream bytes available. uint32_t tuh_midi_read_available(uint8_t dev_addr); +// Send any queued packets to the device if the host hardware is able to do it +// Returns the number of bytes flushed to the host hardware or 0 if +// the host hardware is busy or there is nothing in queue to send. +uint32_t tuh_midi_write_flush( uint8_t dev_addr); + //--------------------------------------------------------------------+ // Packet API //--------------------------------------------------------------------+ -// Read a raw MIDI packet from the connected device -// This function does not parse the packet format -// Return true if a packet was returned -bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]); +// Read all available MIDI packets from the connected device +// Return number of bytes read (always multiple of 4) +uint32_t tuh_midi_packet_read_n(uint8_t dev_addr, uint8_t* buffer, uint32_t bufsize); -// Queue a packet to the device. The application -// must call tuh_midi_stream_flush to actually have the -// data go out. It is up to the application to properly -// format this packet; this function does not check. +// Read a raw MIDI packet from the connected device +// Return true if a packet was returned +TU_ATTR_ALWAYS_INLINE static inline +bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]) { + return 4 == tuh_midi_packet_read_n(dev_addr, packet, 4); +} + +// Write all 4-byte packets, data is locally buffered and only transferred when buffered bytes +// reach the endpoint packet size or tuh_midi_write_flush() is called +uint32_t tuh_midi_packet_write_n(uint8_t dev_addr, const uint8_t* buffer, uint32_t bufsize); + +// Write a 4-bytes packet to the device. // Returns true if the packet was successfully queued. -bool tuh_midi_packet_write (uint8_t dev_addr, uint8_t const packet[4]); +TU_ATTR_ALWAYS_INLINE static inline +bool tuh_midi_packet_write (uint8_t dev_addr, uint8_t const packet[4]) { + return 4 == tuh_midi_packet_write_n(dev_addr, packet, 4); +} //--------------------------------------------------------------------+ // Stream API //--------------------------------------------------------------------+ +#if CFG_TUH_MIDI_STREAM_API -// Queue a message to the device. The application -// must call tuh_midi_stream_flush to actually have the -// data go out. +// Queue a message to the device using stream API. data is locally buffered and only transferred when buffered bytes +// reach the endpoint packet size or tuh_midi_write_flush() is called +// Returns number of bytes was successfully queued. uint32_t tuh_midi_stream_write (uint8_t dev_addr, uint8_t cable_num, uint8_t const* p_buffer, uint32_t bufsize); -// Send any queued packets to the device if the host hardware is able to do it -// Returns the number of bytes flushed to the host hardware or 0 if -// the host hardware is busy or there is nothing in queue to send. -uint32_t tuh_midi_stream_flush( uint8_t dev_addr); - // Get the MIDI stream from the device. Set the value pointed // to by p_cable_num to the MIDI cable number intended to receive it. // The MIDI stream will be stored in the buffer pointed to by p_buffer. @@ -117,6 +134,8 @@ uint32_t tuh_midi_stream_flush( uint8_t dev_addr); // it properly. uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p_buffer, uint16_t bufsize); +#endif + //--------------------------------------------------------------------+ // Callbacks (Weak is optional) //--------------------------------------------------------------------+ @@ -144,4 +163,4 @@ void midih_close (uint8_t dev_addr); } #endif -#endif /* _TUSB_MIDI_HOST_H_ */ +#endif From b12c8a90125b99b71ab3409c10424d3f8ad0736b Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 21 Feb 2025 17:31:13 +0700 Subject: [PATCH 24/30] remove CFG_MIDI_HOST_DEVSTRINGS support, we will leave that for application to parse/extract this information if needed rename tuh_midi_configure() to mounted() for consistency --- examples/host/midi_rx/src/main.c | 2 +- src/class/midi/midi.h | 127 +++++++-------- src/class/midi/midi_host.c | 256 ++++++------------------------- src/class/midi/midi_host.h | 18 +-- 4 files changed, 117 insertions(+), 286 deletions(-) diff --git a/examples/host/midi_rx/src/main.c b/examples/host/midi_rx/src/main.c index fed782947..57e9df29e 100644 --- a/examples/host/midi_rx/src/main.c +++ b/examples/host/midi_rx/src/main.c @@ -89,7 +89,7 @@ void led_blinking_task(void) { //--------------------------------------------------------------------+ void midi_host_rx_task(void) { // device must be attached and have at least one endpoint ready to receive a message - if (!midi_dev_addr || !tuh_midi_configured(midi_dev_addr)) { + if (!midi_dev_addr || !tuh_midi_mounted(midi_dev_addr)) { return; } if (tuh_midi_get_num_rx_cables(midi_dev_addr) < 1) { diff --git a/src/class/midi/midi.h b/src/class/midi/midi.h index 9591f13c4..a7da98831 100644 --- a/src/class/midi/midi.h +++ b/src/class/midi/midi.h @@ -36,28 +36,29 @@ //--------------------------------------------------------------------+ // Constants //--------------------------------------------------------------------+ +enum { + MIDI_VERSION_1_0 = 0x0100, + MIDI_VERSION_2_0 = 0x0200, +}; -typedef enum -{ +typedef enum { MIDI_CS_INTERFACE_HEADER = 0x01, MIDI_CS_INTERFACE_IN_JACK = 0x02, MIDI_CS_INTERFACE_OUT_JACK = 0x03, MIDI_CS_INTERFACE_ELEMENT = 0x04, } midi_cs_interface_subtype_t; -typedef enum -{ - MIDI_CS_ENDPOINT_GENERAL = 0x01 +typedef enum { + MIDI_CS_ENDPOINT_GENERAL = 0x01, + MIDI_CS_ENDPOINT_GENERAL_2_0 = 0x02, } midi_cs_endpoint_subtype_t; -typedef enum -{ +typedef enum { MIDI_JACK_EMBEDDED = 0x01, MIDI_JACK_EXTERNAL = 0x02 } midi_jack_type_t; -typedef enum -{ +typedef enum { MIDI_CIN_MISC = 0, MIDI_CIN_CABLE_EVENT = 1, MIDI_CIN_SYSCOM_2BYTE = 2, // 2 byte system common message e.g MTC, SongSelect @@ -77,8 +78,7 @@ typedef enum } midi_code_index_number_t; // MIDI 1.0 status byte -enum -{ +enum { //------------- System Exclusive -------------// MIDI_STATUS_SYSEX_START = 0xF0, MIDI_STATUS_SYSEX_END = 0xF7, @@ -101,8 +101,7 @@ enum MIDI_STATUS_SYSREAL_SYSTEM_RESET = 0xFF, }; -enum -{ +enum { MIDI_MAX_DATA_VAL = 0x7F, }; @@ -111,62 +110,48 @@ enum //--------------------------------------------------------------------+ /// MIDI Interface Header Descriptor -typedef struct TU_ATTR_PACKED -{ - uint8_t bLength ; ///< Size of this descriptor in bytes. - uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific - uint8_t bDescriptorSubType ; ///< Descriptor SubType - uint16_t bcdMSC ; ///< MidiStreaming SubClass release number in Binary-Coded Decimal - uint16_t wTotalLength ; +typedef struct TU_ATTR_PACKED { + uint8_t bLength; ///< Size of this descriptor in bytes. + uint8_t bDescriptorType; ///< must be TUSB_DESC_CS_INTERFACE + uint8_t bDescriptorSubType;///< Descriptor SubType + uint16_t bcdMSC; ///< MidiStreaming SubClass release number in Binary-Coded Decimal + uint16_t wTotalLength; } midi_desc_header_t; +TU_VERIFY_STATIC(sizeof(midi_desc_header_t) == 7, "size is not correct"); /// MIDI In Jack Descriptor -typedef struct TU_ATTR_PACKED -{ - uint8_t bLength ; ///< Size of this descriptor in bytes. - uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific - uint8_t bDescriptorSubType ; ///< Descriptor SubType - uint8_t bJackType ; ///< Embedded or External - uint8_t bJackID ; ///< Unique ID for MIDI IN Jack - uint8_t iJack ; ///< string descriptor +typedef struct TU_ATTR_PACKED { + uint8_t bLength; ///< Size of this descriptor in bytes. + uint8_t bDescriptorType; ///< Descriptor Type, must be Class-Specific + uint8_t bDescriptorSubType;///< Descriptor SubType + uint8_t bJackType; ///< Embedded or External + uint8_t bJackID; ///< Unique ID for MIDI IN Jack + uint8_t iJack; ///< string descriptor } midi_desc_in_jack_t; +TU_VERIFY_STATIC(sizeof(midi_desc_in_jack_t) == 6, "size is not correct"); - -/// MIDI Out Jack Descriptor with single pin -typedef struct TU_ATTR_PACKED -{ - uint8_t bLength ; ///< Size of this descriptor in bytes. - uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific - uint8_t bDescriptorSubType ; ///< Descriptor SubType - uint8_t bJackType ; ///< Embedded or External - uint8_t bJackID ; ///< Unique ID for MIDI IN Jack - uint8_t bNrInputPins; - - uint8_t baSourceID; - uint8_t baSourcePin; - - uint8_t iJack ; ///< string descriptor -} midi_desc_out_jack_t ; - -/// MIDI Out Jack Descriptor with multiple pins +/// MIDI Out Jack Descriptor with multiple input pins #define midi_desc_out_jack_n_t(input_num) \ - struct TU_ATTR_PACKED { \ - uint8_t bLength ; \ - uint8_t bDescriptorType ; \ - uint8_t bDescriptorSubType ; \ - uint8_t bJackType ; \ - uint8_t bJackID ; \ - uint8_t bNrInputPins ; \ - struct TU_ATTR_PACKED { \ - uint8_t baSourceID; \ - uint8_t baSourcePin; \ - } pins[input_num]; \ - uint8_t iJack ; \ + struct TU_ATTR_PACKED { \ + uint8_t bLength; \ + uint8_t bDescriptorType; \ + uint8_t bDescriptorSubType; \ + uint8_t bJackType; \ + uint8_t bJackID; \ + uint8_t bNrInputPins; \ + struct TU_ATTR_PACKED { \ + uint8_t baSourceID; \ + uint8_t baSourcePin; \ + } input[input_num]; \ + uint8_t iJack; \ } +typedef midi_desc_out_jack_n_t(1) midi_desc_out_jack_1in_t; // 1 input +typedef midi_desc_out_jack_1in_t midi_desc_out_jack_t; // backward compatible +TU_VERIFY_STATIC(sizeof(midi_desc_out_jack_1in_t) == 7 + 2 * 1, "size is not correct"); + /// MIDI Element Descriptor -typedef struct TU_ATTR_PACKED -{ +typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes. uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific uint8_t bDescriptorSubType ; ///< Descriptor SubType @@ -184,6 +169,7 @@ typedef struct TU_ATTR_PACKED uint16_t bmElementCaps; uint8_t iElement; } midi_desc_element_t; +TU_VERIFY_STATIC(sizeof(midi_desc_element_t) == 14, "size is not correct"); /// MIDI Element Descriptor with multiple pins #define midi_desc_element_n_t(input_num) \ @@ -206,14 +192,19 @@ typedef struct TU_ATTR_PACKED } // This descriptor follows the standard bulk data endpoint descriptor -typedef struct -{ - uint8_t bLength ; ///< Size of this descriptor in bytes (4+bNumEmbMIDIJack) - uint8_t bDescriptorType ; ///< Descriptor Type, must be CS_ENDPOINT - uint8_t bDescriptorSubType ; ///< Descriptor SubType, must be MS_GENERAL - uint8_t bNumEmbMIDIJack; ; ///< Number of embedded MIDI jacks associated with this endpoint - uint8_t baAssocJackID[]; ; ///< A list of associated jacks -} midi_cs_desc_endpoint_t; +#define midi_desc_cs_endpoint_n_t(jack_num) \ + struct TU_ATTR_PACKED { \ + uint8_t bLength; \ + uint8_t bDescriptorType; \ + uint8_t bDescriptorSubType; \ + uint8_t bNumEmbMIDIJack; \ + uint8_t baAssocJackID[jack_num]; \ + } + +typedef midi_desc_cs_endpoint_n_t() midi_desc_cs_endpoint_t; // empty/flexible jack list +typedef midi_desc_cs_endpoint_n_t(1) midi_desc_cs_endpoint_1jack_t; + +TU_VERIFY_STATIC(sizeof(midi_desc_cs_endpoint_1jack_t) == 4+1, "size is not correct"); //--------------------------------------------------------------------+ // For Internal Driver Use diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index 55146accb..810c413e3 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -33,6 +33,13 @@ #include "midi_host.h" +// Level where CFG_TUSB_DEBUG must be at least for this driver is logged +#ifndef CFG_TUH_MIDI_LOG_LEVEL + #define CFG_TUH_MIDI_LOG_LEVEL CFG_TUH_LOG_LEVEL +#endif + +#define TU_LOG_DRV(...) TU_LOG(CFG_TUH_MIDI_LOG_LEVEL, __VA_ARGS__) + //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ @@ -65,30 +72,6 @@ typedef struct { } ep_stream; bool configured; - -#if CFG_MIDI_HOST_DEVSTRINGS -#define MAX_STRING_INDICES 32 - uint8_t all_string_indices[MAX_STRING_INDICES]; - uint8_t num_string_indices; -#define MAX_IN_JACKS 8 -#define MAX_OUT_JACKS 8 - struct { - uint8_t jack_id; - uint8_t jack_type; - uint8_t string_index; - } in_jack_info[MAX_IN_JACKS]; - uint8_t next_in_jack; - struct { - uint8_t jack_id; - uint8_t jack_type; - uint8_t num_source_ids; - uint8_t source_ids[MAX_IN_JACKS/4]; - uint8_t string_index; - } out_jack_info[MAX_OUT_JACKS]; - uint8_t next_out_jack; - uint8_t ep_in_associated_jacks[MAX_OUT_JACKS/2]; - uint8_t ep_out_associated_jacks[MAX_IN_JACKS/2]; -#endif }midih_interface_t; typedef struct { @@ -209,19 +192,14 @@ bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len) { (void) rhport; - midih_interface_t *p_midi_host = find_new_midi(); - TU_VERIFY(p_midi_host != NULL); + midih_interface_t *p_midi = find_new_midi(); + TU_VERIFY(p_midi != NULL); - p_midi_host->num_string_indices = 0; TU_VERIFY(TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass); // There can be just a MIDI interface or an audio and a MIDI interface. Only open the MIDI interface uint8_t const *p_desc = (uint8_t const *) desc_itf; uint16_t len_parsed = 0; if (AUDIO_SUBCLASS_CONTROL == desc_itf->bInterfaceSubClass) { - // Keep track of any string descriptor that might be here - if (desc_itf->iInterface != 0) { - p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = desc_itf->iInterface; - } // This driver does not support audio streaming. However, if this is the audio control interface // there might be a MIDI interface following it. Search through every descriptor until a MIDI // interface is found or the end of the descriptor is found @@ -237,26 +215,18 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d TU_VERIFY(AUDIO_SUBCLASS_MIDI_STREAMING == desc_itf->bInterfaceSubClass); len_parsed += desc_itf->bLength; - // Keep track of any string descriptor that might be here - if (desc_itf->iInterface != 0) { - p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = desc_itf->iInterface; - } + TU_LOG_DRV("MIDI opening Interface %u (addr = %u)\r\n", desc_itf->bInterfaceNumber, dev_addr); + p_midi->itf_num = desc_itf->bInterfaceNumber; + // CS Header descriptor p_desc = tu_desc_next(p_desc); - TU_LOG1("MIDI opening Interface %u (addr = %u)\r\n", desc_itf->bInterfaceNumber, dev_addr); - // Find out if getting the MIDI class specific interface header or an endpoint descriptor - // or a class-specific endpoint descriptor - // Jack descriptors or element descriptors must follow the cs interface header, - // but this driver does not support devices that contain element descriptors - - // assume it is an interface header - midi_desc_header_t const *p_mdh = (midi_desc_header_t const *)p_desc; - TU_VERIFY((p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE && p_mdh->bDescriptorSubType == MIDI_CS_INTERFACE_HEADER) || - (p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT && p_mdh->bDescriptorSubType == MIDI_CS_ENDPOINT_GENERAL) || - p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT); + midi_desc_header_t const *p_mdh = (midi_desc_header_t const *) p_desc; + TU_VERIFY(p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE && + p_mdh->bDescriptorSubType == MIDI_CS_INTERFACE_HEADER); + TU_LOG_DRV(" Interface Header descriptor\r\n"); + // p_desc = tu_desc_next(p_desc); uint8_t prev_ep_addr = 0; // the CS endpoint descriptor is associated with the previous endpoint descriptor - p_midi_host->itf_num = desc_itf->bInterfaceNumber; tusb_desc_endpoint_t const* in_desc = NULL; tusb_desc_endpoint_t const* out_desc = NULL; while (len_parsed < max_len) { @@ -271,110 +241,53 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d // assume it is an input jack midi_desc_in_jack_t const *p_mdij = (midi_desc_in_jack_t const *) p_desc; if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_HEADER) { - TU_LOG2("Found MIDI Interface Header\r\b"); + TU_LOG_DRV(" Interface Header descriptor\r\n"); } else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_IN_JACK) { // Then it is an in jack. - TU_LOG2("Found in jack\r\n"); - #if CFG_MIDI_HOST_DEVSTRINGS - if (p_midi_host->next_in_jack < MAX_IN_JACKS) { - p_midi_host->in_jack_info[p_midi_host->next_in_jack].jack_id = p_mdij->bJackID; - p_midi_host->in_jack_info[p_midi_host->next_in_jack].jack_type = p_mdij->bJackType; - p_midi_host->in_jack_info[p_midi_host->next_in_jack].string_index = p_mdij->iJack; - ++p_midi_host->next_in_jack; - // Keep track of any string descriptor that might be here - if (p_mdij->iJack != 0) { - p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = p_mdij->iJack; - } - } - #endif + TU_LOG_DRV(" IN Jack %s descriptor \r\n", p_mdij->bJackType == MIDI_JACK_EXTERNAL ? "External" : "Embedded"); } else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_OUT_JACK) { // then it is an out jack - TU_LOG2("Found out jack\r\n"); - #if CFG_MIDI_HOST_DEVSTRINGS - if (p_midi_host->next_out_jack < MAX_OUT_JACKS) { - midi_desc_out_jack_t const *p_mdoj = (midi_desc_out_jack_t const *) p_desc; - p_midi_host->out_jack_info[p_midi_host->next_out_jack].jack_id = p_mdoj->bJackID; - p_midi_host->out_jack_info[p_midi_host->next_out_jack].jack_type = p_mdoj->bJackType; - p_midi_host->out_jack_info[p_midi_host->next_out_jack].num_source_ids = p_mdoj->bNrInputPins; - const struct associated_jack_s { - uint8_t id; - uint8_t pin; - } *associated_jack = (const struct associated_jack_s *) (p_desc + 6); - int jack; - for (jack = 0; jack < p_mdoj->bNrInputPins; jack++) { - p_midi_host->out_jack_info[p_midi_host->next_out_jack].source_ids[jack] = associated_jack->id; - } - p_midi_host->out_jack_info[p_midi_host->next_out_jack].string_index = *(p_desc + 6 + p_mdoj->bNrInputPins * 2); - ++p_midi_host->next_out_jack; - if (p_mdoj->iJack != 0) { - p_midi_host->all_string_indices[p_midi_host->num_string_indices++] = p_mdoj->iJack; - } - } - #endif + TU_LOG_DRV(" OUT Jack %s descriptor\r\n", p_mdij->bJackType == MIDI_JACK_EXTERNAL ? "External" : "Embedded"); } else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_ELEMENT) { // the it is an element; - #if CFG_MIDI_HOST_DEVSTRINGS - TU_LOG1("Found element; strings not supported\r\n"); - #else - TU_LOG2("Found element\r\n"); - #endif + TU_LOG_DRV("Found element\r\n"); } else { - TU_LOG2("Unknown CS Interface sub-type %u\r\n", p_mdij->bDescriptorSubType); + TU_LOG_DRV(" Unknown CS Interface sub-type %u\r\n", p_mdij->bDescriptorSubType); TU_VERIFY(false);// unknown CS Interface sub-type } len_parsed += p_mdij->bLength; } else if (p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT) { - TU_LOG2("found CS_ENDPOINT Descriptor for %u\r\n", prev_ep_addr); + TU_LOG_DRV(" CS_ENDPOINT descriptor\r\n"); TU_VERIFY(prev_ep_addr != 0); // parse out the mapping between the device's embedded jacks and the endpoints // Each embedded IN jack is associated with an OUT endpoint - midi_cs_desc_endpoint_t const *p_csep = (midi_cs_desc_endpoint_t const *) p_mdh; + midi_desc_cs_endpoint_t const *p_csep = (midi_desc_cs_endpoint_t const *) p_mdh; if (tu_edpt_dir(prev_ep_addr) == TUSB_DIR_OUT) { - TU_VERIFY(p_midi_host->ep_out == prev_ep_addr); - TU_VERIFY(p_midi_host->num_cables_tx == 0); - p_midi_host->num_cables_tx = p_csep->bNumEmbMIDIJack; - #if CFG_MIDI_HOST_DEVSTRINGS - uint8_t jack; - uint8_t max_jack = p_midi_host->num_cables_tx; - if (max_jack > sizeof(p_midi_host->ep_out_associated_jacks)) { - max_jack = sizeof(p_midi_host->ep_out_associated_jacks); - } - for (jack = 0; jack < max_jack; jack++) { - p_midi_host->ep_out_associated_jacks[jack] = p_csep->baAssocJackID[jack]; - } - #endif + TU_VERIFY(p_midi->ep_out == prev_ep_addr); + TU_VERIFY(p_midi->num_cables_tx == 0); + p_midi->num_cables_tx = p_csep->bNumEmbMIDIJack; } else { - TU_VERIFY(p_midi_host->ep_in == prev_ep_addr); - TU_VERIFY(p_midi_host->num_cables_rx == 0); - p_midi_host->num_cables_rx = p_csep->bNumEmbMIDIJack; - #if CFG_MIDI_HOST_DEVSTRINGS - uint8_t jack; - uint8_t max_jack = p_midi_host->num_cables_rx; - if (max_jack > sizeof(p_midi_host->ep_in_associated_jacks)) { - max_jack = sizeof(p_midi_host->ep_in_associated_jacks); - } - for (jack = 0; jack < max_jack; jack++) { - p_midi_host->ep_in_associated_jacks[jack] = p_csep->baAssocJackID[jack]; - } - #endif + TU_VERIFY(p_midi->ep_in == prev_ep_addr); + TU_VERIFY(p_midi->num_cables_rx == 0); + p_midi->num_cables_rx = p_csep->bNumEmbMIDIJack; } len_parsed += p_csep->bLength; prev_ep_addr = 0; } else if (p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT) { // parse out the bulk endpoint info tusb_desc_endpoint_t const *p_ep = (tusb_desc_endpoint_t const *) p_mdh; - TU_LOG2("found ENDPOINT Descriptor for %u\r\n", p_ep->bEndpointAddress); + TU_LOG_DRV(" Endpoint descriptor %02x\r\n", p_ep->bEndpointAddress); if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_OUT) { - TU_VERIFY(p_midi_host->ep_out == 0); - TU_VERIFY(p_midi_host->num_cables_tx == 0); - p_midi_host->ep_out = p_ep->bEndpointAddress; - prev_ep_addr = p_midi_host->ep_out; + TU_VERIFY(p_midi->ep_out == 0); + TU_VERIFY(p_midi->num_cables_tx == 0); + p_midi->ep_out = p_ep->bEndpointAddress; + prev_ep_addr = p_midi->ep_out; out_desc = p_ep; } else { - TU_VERIFY(p_midi_host->ep_in == 0); - TU_VERIFY(p_midi_host->num_cables_rx == 0); - p_midi_host->ep_in = p_ep->bEndpointAddress; - prev_ep_addr = p_midi_host->ep_in; + TU_VERIFY(p_midi->ep_in == 0); + TU_VERIFY(p_midi->num_cables_rx == 0); + p_midi->ep_in = p_ep->bEndpointAddress; + prev_ep_addr = p_midi->ep_in; in_desc = p_ep; } len_parsed += p_mdh->bLength; @@ -382,28 +295,22 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d p_desc = tu_desc_next(p_desc); p_mdh = (midi_desc_header_t const *) p_desc; } - TU_VERIFY((p_midi_host->ep_out != 0 && p_midi_host->num_cables_tx != 0) || - (p_midi_host->ep_in != 0 && p_midi_host->num_cables_rx != 0)); - TU_LOG1("MIDI descriptor parsed successfully\r\n"); - // remove duplicate string indices - for (int idx = 0; idx < p_midi_host->num_string_indices; idx++) { - for (int jdx = idx + 1; jdx < p_midi_host->num_string_indices; jdx++) { - while (jdx < p_midi_host->num_string_indices && p_midi_host->all_string_indices[idx] == p_midi_host->all_string_indices[jdx]) { - // delete the duplicate by overwriting it with the last entry and reducing the number of entries by 1 - p_midi_host->all_string_indices[jdx] = p_midi_host->all_string_indices[p_midi_host->num_string_indices - 1]; - --p_midi_host->num_string_indices; - } - } - } + TU_VERIFY((p_midi->ep_out != 0 && p_midi->num_cables_tx != 0) || + (p_midi->ep_in != 0 && p_midi->num_cables_rx != 0)); + if (in_desc) { TU_ASSERT(tuh_edpt_open(dev_addr, in_desc)); - tu_edpt_stream_open(&p_midi_host->ep_stream.rx, in_desc); + tu_edpt_stream_open(&p_midi->ep_stream.rx, in_desc); } if (out_desc) { TU_ASSERT(tuh_edpt_open(dev_addr, out_desc)); - tu_edpt_stream_open(&p_midi_host->ep_stream.tx, out_desc); + tu_edpt_stream_open(&p_midi->ep_stream.tx, out_desc); } - p_midi_host->dev_addr = dev_addr; + p_midi->dev_addr = dev_addr; + + // if (tuh_midi_interface_descriptor_cb) { + // tuh_midi_interface_descriptor_cb(dev_addr, desc_itf, ); + // } return true; } @@ -428,7 +335,7 @@ bool midih_set_config(uint8_t dev_addr, uint8_t itf_num) { //--------------------------------------------------------------------+ // API //--------------------------------------------------------------------+ -bool tuh_midi_configured(uint8_t dev_addr) { +bool tuh_midi_mounted(uint8_t dev_addr) { midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); TU_VERIFY(p_midi_host != NULL); return p_midi_host->configured; @@ -680,69 +587,6 @@ uint32_t tuh_midi_stream_read(uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p return bytes_buffered; } -//--------------------------------------------------------------------+ -// String API -//--------------------------------------------------------------------+ -#if CFG_MIDI_HOST_DEVSTRINGS -static uint8_t find_string_index(midih_interface_t *ptr, uint8_t jack_id) -{ - uint8_t index = 0; - uint8_t assoc; - for (assoc = 0; index == 0 && assoc < ptr->next_in_jack; assoc++) - { - if (jack_id == ptr->in_jack_info[assoc].jack_id) - { - index = ptr->in_jack_info[assoc].string_index; - } - } - for (assoc = 0; index == 0 && assoc < ptr->next_out_jack; assoc++) - { - if (jack_id == ptr->out_jack_info[assoc].jack_id) - { - index = ptr->out_jack_info[assoc].string_index; - } - } - return index; -} - -uint8_t tuh_midi_get_rx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings) { - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL, 0); - uint8_t nstrings = p_midi_host->num_cables_rx; - if (nstrings > max_istrings) { - nstrings = max_istrings; - } - for (uint8_t jack = 0; jackep_in_associated_jacks[jack]; - istrings[jack] = find_string_index(p_midi_host, jack_id); - } - return nstrings; -} - -uint8_t tuh_midi_get_tx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings) -{ - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL, 0); - uint8_t nstrings = p_midi_host->num_cables_tx; - if (nstrings > max_istrings) { - nstrings = max_istrings; - } - for (uint8_t jack = 0; jackep_out_associated_jacks[jack]; - istrings[jack] = find_string_index(p_midi_host, jack_id); - } - return nstrings; -} -#endif - -uint8_t tuh_midi_get_all_istrings(uint8_t dev_addr, const uint8_t** istrings) -{ - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - uint8_t nstrings = p_midi_host->num_string_indices; - if (nstrings) { *istrings = p_midi_host->all_string_indices; } - return nstrings; -} #endif #endif diff --git a/src/class/midi/midi_host.h b/src/class/midi/midi_host.h index 6e2595e44..adf41d205 100644 --- a/src/class/midi/midi_host.h +++ b/src/class/midi/midi_host.h @@ -59,14 +59,12 @@ #define CFG_TUH_MIDI_STREAM_API 1 #endif -#ifndef CFG_MIDI_HOST_DEVSTRINGS -#define CFG_MIDI_HOST_DEVSTRINGS 0 -#endif - //--------------------------------------------------------------------+ // Application API //--------------------------------------------------------------------+ -bool tuh_midi_configured (uint8_t dev_addr); + +// Check if MIDI interface is mounted +bool tuh_midi_mounted(uint8_t dev_addr); // return the number of virtual midi cables on the device's IN endpoint uint8_t tuh_midi_get_num_rx_cables(uint8_t dev_addr); @@ -74,12 +72,6 @@ uint8_t tuh_midi_get_num_rx_cables(uint8_t dev_addr); // return the number of virtual midi cables on the device's OUT endpoint uint8_t tuh_midi_get_num_tx_cables(uint8_t dev_addr); -#if CFG_MIDI_HOST_DEVSTRINGS -uint8_t tuh_midi_get_rx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings); -uint8_t tuh_midi_get_tx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings); -uint8_t tuh_midi_get_all_istrings(uint8_t dev_addr, const uint8_t** istrings); -#endif - // return the raw number of bytes available. // Note: this is related but not the same as number of stream bytes available. uint32_t tuh_midi_read_available(uint8_t dev_addr); @@ -140,6 +132,10 @@ uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t * // Callbacks (Weak is optional) //--------------------------------------------------------------------+ +// Invoked when MIDI interface is detected in enumeration. Application can copy/parse descriptor if needed. +// Note: may be fired before tuh_midi_mount_cb(), therefore midi interface is not mounted/ready. +// TU_ATTR_WEAK void tuh_midi_interface_descriptor_cb(uint8_t dev_addr, const uint8_t* desc_itf, uint16_t desc_len); + // Invoked when device with MIDI interface is mounted. TU_ATTR_WEAK void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t num_cables_rx, uint16_t num_cables_tx); From 71e046d9ffb6e388052b02f8230de51bfeb5b0ed Mon Sep 17 00:00:00 2001 From: hathach Date: Sun, 23 Feb 2025 22:21:55 +0700 Subject: [PATCH 25/30] add tuh_midi_descriptor_cb() --- src/class/audio/audio.h | 1 + src/class/audio/audio_device.c | 4 - src/class/midi/midi.h | 21 ---- src/class/midi/midi_host.c | 175 +++++++++++++++------------------ src/class/midi/midi_host.h | 14 ++- src/common/tusb_types.h | 14 ++- src/tusb.c | 12 ++- 7 files changed, 113 insertions(+), 128 deletions(-) diff --git a/src/class/audio/audio.h b/src/class/audio/audio.h index 2f97c0f23..0d1acadcc 100644 --- a/src/class/audio/audio.h +++ b/src/class/audio/audio.h @@ -661,6 +661,7 @@ typedef struct TU_ATTR_PACKED uint16_t wTotalLength ; ///< Total number of bytes returned for the class-specific AudioControl interface descriptor. Includes the combined length of this descriptor header and all Clock Source, Unit and Terminal descriptors. uint8_t bmControls ; ///< See: audio_cs_ac_interface_control_pos_t. } audio_desc_cs_ac_interface_t; +TU_VERIFY_STATIC(sizeof(audio_desc_cs_ac_interface_t) == 9, "size is not correct"); /// AUDIO Clock Source Descriptor (4.7.2.1) typedef struct TU_ATTR_PACKED diff --git a/src/class/audio/audio_device.c b/src/class/audio/audio_device.c index 8a130b7e2..388168424 100644 --- a/src/class/audio/audio_device.c +++ b/src/class/audio/audio_device.c @@ -628,10 +628,6 @@ static uint8_t audiod_get_audio_fct_idx(audiod_function_t *audio); #if (CFG_TUD_AUDIO_ENABLE_EP_IN && (CFG_TUD_AUDIO_EP_IN_FLOW_CONTROL || CFG_TUD_AUDIO_ENABLE_ENCODING)) || (CFG_TUD_AUDIO_ENABLE_EP_OUT && CFG_TUD_AUDIO_ENABLE_DECODING) static void audiod_parse_for_AS_params(audiod_function_t *audio, uint8_t const *p_desc, uint8_t const *p_desc_end, uint8_t const as_itf); - -static inline uint8_t tu_desc_subtype(void const *desc) { - return ((uint8_t const *) desc)[2]; -} #endif #if CFG_TUD_AUDIO_ENABLE_EP_IN && CFG_TUD_AUDIO_EP_IN_FLOW_CONTROL diff --git a/src/class/midi/midi.h b/src/class/midi/midi.h index a7da98831..cd67640e4 100644 --- a/src/class/midi/midi.h +++ b/src/class/midi/midi.h @@ -150,27 +150,6 @@ typedef midi_desc_out_jack_n_t(1) midi_desc_out_jack_1in_t; // 1 input typedef midi_desc_out_jack_1in_t midi_desc_out_jack_t; // backward compatible TU_VERIFY_STATIC(sizeof(midi_desc_out_jack_1in_t) == 7 + 2 * 1, "size is not correct"); -/// MIDI Element Descriptor -typedef struct TU_ATTR_PACKED { - uint8_t bLength ; ///< Size of this descriptor in bytes. - uint8_t bDescriptorType ; ///< Descriptor Type, must be Class-Specific - uint8_t bDescriptorSubType ; ///< Descriptor SubType - uint8_t bElementID; - - uint8_t bNrInputPins; - uint8_t baSourceID; - uint8_t baSourcePin; - - uint8_t bNrOutputPins; - uint8_t bInTerminalLink; - uint8_t bOutTerminalLink; - uint8_t bElCapsSize; - - uint16_t bmElementCaps; - uint8_t iElement; -} midi_desc_element_t; -TU_VERIFY_STATIC(sizeof(midi_desc_element_t) == 14, "size is not correct"); - /// MIDI Element Descriptor with multiple pins #define midi_desc_element_n_t(input_num) \ struct TU_ATTR_PACKED { \ diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index 810c413e3..2fdf41b9d 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -194,123 +194,108 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d midih_interface_t *p_midi = find_new_midi(); TU_VERIFY(p_midi != NULL); - TU_VERIFY(TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass); - // There can be just a MIDI interface or an audio and a MIDI interface. Only open the MIDI interface + + // There can be just a MIDI or an Audio + MIDI interface + // const uint8_t* p_start = ((uint8_t const*) desc_itf); + const uint8_t* p_end = ((uint8_t const*) desc_itf) + max_len; uint8_t const *p_desc = (uint8_t const *) desc_itf; uint16_t len_parsed = 0; - if (AUDIO_SUBCLASS_CONTROL == desc_itf->bInterfaceSubClass) { - // This driver does not support audio streaming. However, if this is the audio control interface - // there might be a MIDI interface following it. Search through every descriptor until a MIDI - // interface is found or the end of the descriptor is found - while (len_parsed < max_len && - (desc_itf->bInterfaceClass != TUSB_CLASS_AUDIO || desc_itf->bInterfaceSubClass != AUDIO_SUBCLASS_MIDI_STREAMING)) { - len_parsed += desc_itf->bLength; - p_desc = tu_desc_next(p_desc); - desc_itf = (tusb_desc_interface_t const *)p_desc; - } + tuh_midi_descriptor_cb_t desc_cb = { 0 }; + desc_cb.jack_num = 0; + + // If there is Audio Control Interface + Audio Header descriptor, skip it + if (AUDIO_SUBCLASS_CONTROL == desc_itf->bInterfaceSubClass) { + TU_VERIFY(max_len > 2*sizeof(tusb_desc_interface_t) + sizeof(audio_desc_cs_ac_interface_t)); + + p_desc = tu_desc_next(p_desc); + TU_VERIFY(tu_desc_type(p_desc) == TUSB_DESC_CS_INTERFACE && + tu_desc_subtype(p_desc) == AUDIO_CS_AC_INTERFACE_HEADER); + + p_desc = tu_desc_next(p_desc); + desc_itf = (tusb_desc_interface_t const *)p_desc; TU_VERIFY(TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass); } TU_VERIFY(AUDIO_SUBCLASS_MIDI_STREAMING == desc_itf->bInterfaceSubClass); + desc_cb.desc_interface = desc_itf; + len_parsed += desc_itf->bLength; TU_LOG_DRV("MIDI opening Interface %u (addr = %u)\r\n", desc_itf->bInterfaceNumber, dev_addr); p_midi->itf_num = desc_itf->bInterfaceNumber; - - // CS Header descriptor p_desc = tu_desc_next(p_desc); - midi_desc_header_t const *p_mdh = (midi_desc_header_t const *) p_desc; - TU_VERIFY(p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE && - p_mdh->bDescriptorSubType == MIDI_CS_INTERFACE_HEADER); - TU_LOG_DRV(" Interface Header descriptor\r\n"); - // p_desc = tu_desc_next(p_desc); - uint8_t prev_ep_addr = 0; // the CS endpoint descriptor is associated with the previous endpoint descriptor - tusb_desc_endpoint_t const* in_desc = NULL; - tusb_desc_endpoint_t const* out_desc = NULL; - while (len_parsed < max_len) { - TU_VERIFY((p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE) || - (p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT && p_mdh->bDescriptorSubType == MIDI_CS_ENDPOINT_GENERAL) || - p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT); + bool found_new_interface = false; + while ((p_desc < p_end) && (tu_desc_next(p_desc) <= p_end) && !found_new_interface) { + switch (tu_desc_type(p_desc)) { + case TUSB_DESC_INTERFACE: + found_new_interface = true; + break; - if (p_mdh->bDescriptorType == TUSB_DESC_CS_INTERFACE) { - // The USB host doesn't really need this information unless it uses - // the string descriptor for a jack or Element + case TUSB_DESC_CS_INTERFACE: + switch (tu_desc_subtype(p_desc)) { + case MIDI_CS_INTERFACE_HEADER: + TU_LOG_DRV(" Interface Header descriptor\r\n"); + desc_cb.desc_header = p_desc; + break; - // assume it is an input jack - midi_desc_in_jack_t const *p_mdij = (midi_desc_in_jack_t const *) p_desc; - if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_HEADER) { - TU_LOG_DRV(" Interface Header descriptor\r\n"); - } else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_IN_JACK) { - // Then it is an in jack. - TU_LOG_DRV(" IN Jack %s descriptor \r\n", p_mdij->bJackType == MIDI_JACK_EXTERNAL ? "External" : "Embedded"); - } else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_OUT_JACK) { - // then it is an out jack - TU_LOG_DRV(" OUT Jack %s descriptor\r\n", p_mdij->bJackType == MIDI_JACK_EXTERNAL ? "External" : "Embedded"); - } else if (p_mdij->bDescriptorSubType == MIDI_CS_INTERFACE_ELEMENT) { - // the it is an element; - TU_LOG_DRV("Found element\r\n"); - } else { - TU_LOG_DRV(" Unknown CS Interface sub-type %u\r\n", p_mdij->bDescriptorSubType); - TU_VERIFY(false);// unknown CS Interface sub-type + case MIDI_CS_INTERFACE_IN_JACK: + case MIDI_CS_INTERFACE_OUT_JACK: { + TU_LOG_DRV(" Jack %s %s descriptor \r\n", + tu_desc_subtype(p_desc) == MIDI_CS_INTERFACE_IN_JACK ? "IN" : "OUT", + p_desc[3] == MIDI_JACK_EXTERNAL ? "External" : "Embedded"); + desc_cb.desc_jack[desc_cb.jack_num++] = p_desc; + break; + } + + case MIDI_CS_INTERFACE_ELEMENT: + TU_LOG_DRV(" Element descriptor\r\n"); + desc_cb.desc_element = p_desc; + break; + + default: + TU_LOG_DRV(" Unknown CS Interface sub-type %u\r\n", tu_desc_subtype(p_desc)); + break; + } + break; + + case TUSB_DESC_ENDPOINT: { + tusb_desc_endpoint_t const *p_ep = (tusb_desc_endpoint_t const *) p_desc; + p_desc = tu_desc_next(p_desc); // next to CS endpoint + TU_VERIFY(p_desc < p_end && tu_desc_next(p_desc) <= p_end); + midi_desc_cs_endpoint_t const *p_csep = (midi_desc_cs_endpoint_t const *) p_desc; + + TU_LOG_DRV(" Endpoint and CS_Endpoint descriptor %02x\r\n", p_ep->bEndpointAddress); + if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_OUT) { + p_midi->ep_out = p_ep->bEndpointAddress; + p_midi->num_cables_tx = p_csep->bNumEmbMIDIJack; + desc_cb.desc_epout = p_ep; + + TU_ASSERT(tuh_edpt_open(dev_addr, p_ep)); + tu_edpt_stream_open(&p_midi->ep_stream.tx, p_ep); + } else { + p_midi->ep_in = p_ep->bEndpointAddress; + p_midi->num_cables_rx = p_csep->bNumEmbMIDIJack; + desc_cb.desc_epin = p_ep; + + TU_ASSERT(tuh_edpt_open(dev_addr, p_ep)); + tu_edpt_stream_open(&p_midi->ep_stream.rx, p_ep); + } + break; } - len_parsed += p_mdij->bLength; - } else if (p_mdh->bDescriptorType == TUSB_DESC_CS_ENDPOINT) { - TU_LOG_DRV(" CS_ENDPOINT descriptor\r\n"); - TU_VERIFY(prev_ep_addr != 0); - // parse out the mapping between the device's embedded jacks and the endpoints - // Each embedded IN jack is associated with an OUT endpoint - midi_desc_cs_endpoint_t const *p_csep = (midi_desc_cs_endpoint_t const *) p_mdh; - if (tu_edpt_dir(prev_ep_addr) == TUSB_DIR_OUT) { - TU_VERIFY(p_midi->ep_out == prev_ep_addr); - TU_VERIFY(p_midi->num_cables_tx == 0); - p_midi->num_cables_tx = p_csep->bNumEmbMIDIJack; - } else { - TU_VERIFY(p_midi->ep_in == prev_ep_addr); - TU_VERIFY(p_midi->num_cables_rx == 0); - p_midi->num_cables_rx = p_csep->bNumEmbMIDIJack; - } - len_parsed += p_csep->bLength; - prev_ep_addr = 0; - } else if (p_mdh->bDescriptorType == TUSB_DESC_ENDPOINT) { - // parse out the bulk endpoint info - tusb_desc_endpoint_t const *p_ep = (tusb_desc_endpoint_t const *) p_mdh; - TU_LOG_DRV(" Endpoint descriptor %02x\r\n", p_ep->bEndpointAddress); - if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_OUT) { - TU_VERIFY(p_midi->ep_out == 0); - TU_VERIFY(p_midi->num_cables_tx == 0); - p_midi->ep_out = p_ep->bEndpointAddress; - prev_ep_addr = p_midi->ep_out; - out_desc = p_ep; - } else { - TU_VERIFY(p_midi->ep_in == 0); - TU_VERIFY(p_midi->num_cables_rx == 0); - p_midi->ep_in = p_ep->bEndpointAddress; - prev_ep_addr = p_midi->ep_in; - in_desc = p_ep; - } - len_parsed += p_mdh->bLength; + + default: break; // skip unknown descriptor } p_desc = tu_desc_next(p_desc); - p_mdh = (midi_desc_header_t const *) p_desc; } - TU_VERIFY((p_midi->ep_out != 0 && p_midi->num_cables_tx != 0) || - (p_midi->ep_in != 0 && p_midi->num_cables_rx != 0)); + desc_cb.desc_interface_len = (uint16_t) ((uintptr_t)p_desc - (uintptr_t) desc_itf); - if (in_desc) { - TU_ASSERT(tuh_edpt_open(dev_addr, in_desc)); - tu_edpt_stream_open(&p_midi->ep_stream.rx, in_desc); - } - if (out_desc) { - TU_ASSERT(tuh_edpt_open(dev_addr, out_desc)); - tu_edpt_stream_open(&p_midi->ep_stream.tx, out_desc); - } p_midi->dev_addr = dev_addr; - // if (tuh_midi_interface_descriptor_cb) { - // tuh_midi_interface_descriptor_cb(dev_addr, desc_itf, ); - // } + if (tuh_midi_descriptor_cb) { + tuh_midi_descriptor_cb(dev_addr, &desc_cb); + } return true; } diff --git a/src/class/midi/midi_host.h b/src/class/midi/midi_host.h index adf41d205..286e034f1 100644 --- a/src/class/midi/midi_host.h +++ b/src/class/midi/midi_host.h @@ -62,6 +62,18 @@ //--------------------------------------------------------------------+ // Application API //--------------------------------------------------------------------+ +typedef struct { + const tusb_desc_interface_t* desc_interface; // start of whole midi interface descriptor + uint16_t desc_interface_len; + + const uint8_t* desc_header; + const uint8_t* desc_element; + const tusb_desc_endpoint_t* desc_epin; // endpoint IN descriptor, CS_ENDPOINT is right after + const tusb_desc_endpoint_t* desc_epout; // endpoint OUT descriptor, CS_ENDPOINT is right after + + uint8_t jack_num; + const uint8_t* desc_jack[16]; // list of jack descriptors (embedded + external) +} tuh_midi_descriptor_cb_t; // Check if MIDI interface is mounted bool tuh_midi_mounted(uint8_t dev_addr); @@ -134,7 +146,7 @@ uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t * // Invoked when MIDI interface is detected in enumeration. Application can copy/parse descriptor if needed. // Note: may be fired before tuh_midi_mount_cb(), therefore midi interface is not mounted/ready. -// TU_ATTR_WEAK void tuh_midi_interface_descriptor_cb(uint8_t dev_addr, const uint8_t* desc_itf, uint16_t desc_len); +TU_ATTR_WEAK void tuh_midi_descriptor_cb(uint8_t dev_addr, tuh_midi_descriptor_cb_t const* desc_cb); // Invoked when device with MIDI interface is mounted. TU_ATTR_WEAK void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t num_cables_rx, uint16_t num_cables_tx); diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h index 42ce5fc12..95ee489a6 100644 --- a/src/common/tusb_types.h +++ b/src/common/tusb_types.h @@ -281,7 +281,8 @@ typedef enum { // TODO remove enum { DESC_OFFSET_LEN = 0, - DESC_OFFSET_TYPE = 1 + DESC_OFFSET_TYPE = 1, + DESC_OFFSET_SUBTYPE = 2 }; enum { @@ -570,14 +571,19 @@ TU_ATTR_ALWAYS_INLINE static inline uint8_t const * tu_desc_next(void const* des return desc8 + desc8[DESC_OFFSET_LEN]; } +// get descriptor length +TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_len(void const* desc) { + return ((uint8_t const*) desc)[DESC_OFFSET_LEN]; +} + // get descriptor type TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_type(void const* desc) { return ((uint8_t const*) desc)[DESC_OFFSET_TYPE]; } -// get descriptor length -TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_len(void const* desc) { - return ((uint8_t const*) desc)[DESC_OFFSET_LEN]; +// get descriptor subtype +TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_subtype(void const* desc) { + return ((uint8_t const*) desc)[DESC_OFFSET_SUBTYPE]; } // find descriptor that match byte1 (type) diff --git a/src/tusb.c b/src/tusb.c index 85ab1d6ae..9303e9bc4 100644 --- a/src/tusb.c +++ b/src/tusb.c @@ -142,7 +142,9 @@ void tusb_int_handler(uint8_t rhport, bool in_isr) { uint8_t const* tu_desc_find(uint8_t const* desc, uint8_t const* end, uint8_t byte1) { while (desc + 1 < end) { - if (desc[1] == byte1) return desc; + if (desc[1] == byte1) { + return desc; + } desc += desc[DESC_OFFSET_LEN]; } return NULL; @@ -150,7 +152,9 @@ uint8_t const* tu_desc_find(uint8_t const* desc, uint8_t const* end, uint8_t byt uint8_t const* tu_desc_find2(uint8_t const* desc, uint8_t const* end, uint8_t byte1, uint8_t byte2) { while (desc + 2 < end) { - if (desc[1] == byte1 && desc[2] == byte2) return desc; + if (desc[1] == byte1 && desc[2] == byte2) { + return desc; + } desc += desc[DESC_OFFSET_LEN]; } return NULL; @@ -158,7 +162,9 @@ uint8_t const* tu_desc_find2(uint8_t const* desc, uint8_t const* end, uint8_t by uint8_t const* tu_desc_find3(uint8_t const* desc, uint8_t const* end, uint8_t byte1, uint8_t byte2, uint8_t byte3) { while (desc + 3 < end) { - if (desc[1] == byte1 && desc[2] == byte2 && desc[3] == byte3) return desc; + if (desc[1] == byte1 && desc[2] == byte2 && desc[3] == byte3) { + return desc; + } desc += desc[DESC_OFFSET_LEN]; } return NULL; From 8c70475c23233e70652c5852088fc539bfb337ad Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 24 Feb 2025 12:39:02 +0700 Subject: [PATCH 26/30] change API to take index instead of dev address, this allow to support more than 1 midi per device. --- examples/host/midi_rx/src/main.c | 48 ++---- src/class/midi/midi_host.c | 275 +++++++++++++++++-------------- src/class/midi/midi_host.h | 42 ++--- 3 files changed, 189 insertions(+), 176 deletions(-) diff --git a/examples/host/midi_rx/src/main.c b/examples/host/midi_rx/src/main.c index 57e9df29e..a8a5b40ca 100644 --- a/examples/host/midi_rx/src/main.c +++ b/examples/host/midi_rx/src/main.c @@ -30,12 +30,9 @@ #include "bsp/board_api.h" #include "tusb.h" -#if CFG_TUH_MIDI - //--------------------------------------------------------------------+ // STATIC GLOBALS DECLARATION //--------------------------------------------------------------------+ -static uint8_t midi_dev_addr = 0; //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF PROTOTYPES @@ -65,8 +62,6 @@ int main(void) { return 0; } -#endif - //--------------------------------------------------------------------+ // Blinking Task //--------------------------------------------------------------------+ @@ -88,13 +83,7 @@ void led_blinking_task(void) { // MIDI host receive task //--------------------------------------------------------------------+ void midi_host_rx_task(void) { - // device must be attached and have at least one endpoint ready to receive a message - if (!midi_dev_addr || !tuh_midi_mounted(midi_dev_addr)) { - return; - } - if (tuh_midi_get_num_rx_cables(midi_dev_addr) < 1) { - return; - } + // nothing to do, we just print out received data in callback } //--------------------------------------------------------------------+ @@ -102,39 +91,32 @@ void midi_host_rx_task(void) { //--------------------------------------------------------------------+ // Invoked when device with MIDI interface is mounted. -void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t num_cables_rx, uint16_t num_cables_tx) { - (void) num_cables_rx; - (void) num_cables_tx; - midi_dev_addr = dev_addr; - TU_LOG1("MIDI device address = %u, Number of RX cables = %u, Number of TX cables = %u\r\n", - dev_addr, num_cables_rx, num_cables_tx); +void tuh_midi_mount_cb(uint8_t idx, uint8_t num_cables_rx, uint16_t num_cables_tx) { + printf("MIDI Interface Index = %u, Number of RX cables = %u, Number of TX cables = %u\r\n", + idx, num_cables_rx, num_cables_tx); } // Invoked when device with hid interface is un-mounted -void tuh_midi_umount_cb(uint8_t dev_addr) { - (void) dev_addr; - midi_dev_addr = 0; - TU_LOG1("MIDI device address = %d is unmounted\r\n", dev_addr); +void tuh_midi_umount_cb(uint8_t idx) { + printf("MIDI Interface Index = %u is unmounted\r\n", idx); } -void tuh_midi_rx_cb(uint8_t dev_addr, uint32_t num_packets) { - if (midi_dev_addr != dev_addr) { - return; - } - +void tuh_midi_rx_cb(uint8_t idx, uint32_t num_packets) { if (num_packets == 0) { return; } uint8_t cable_num; uint8_t buffer[48]; - uint32_t bytes_read = tuh_midi_stream_read(dev_addr, &cable_num, buffer, sizeof(buffer)); - (void) bytes_read; + uint32_t bytes_read = tuh_midi_stream_read(idx, &cable_num, buffer, sizeof(buffer)); - TU_LOG1("Read bytes %lu cable %u", bytes_read, cable_num); - TU_LOG1_MEM(buffer, bytes_read, 2); + printf("Cable %u rx %lu bytes: ", cable_num, bytes_read); + for (uint32_t i = 0; i < bytes_read; i++) { + printf("%02X ", buffer[i]); + } + printf("\r\n"); } -void tuh_midi_tx_cb(uint8_t dev_addr) { - (void) dev_addr; +void tuh_midi_tx_cb(uint8_t idx) { + (void) idx; } diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index 2fdf41b9d..8fb325c2b 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -45,8 +45,9 @@ //--------------------------------------------------------------------+ typedef struct { - uint8_t dev_addr; - uint8_t itf_num; + uint8_t daddr; + uint8_t bInterfaceNumber; // interface number of MIDI streaming + uint8_t itf_count; // number of interface including Audio Control + MIDI streaming uint8_t ep_in; // IN endpoint address uint8_t ep_out; // OUT endpoint address @@ -71,7 +72,7 @@ typedef struct { uint8_t tx_ff_buf[CFG_TUH_MIDI_TX_BUFSIZE]; } ep_stream; - bool configured; + bool mounted; }midih_interface_t; typedef struct { @@ -85,17 +86,24 @@ CFG_TUH_MEM_SECTION static midih_epbuf_t _midi_epbuf[CFG_TUH_MIDI]; //--------------------------------------------------------------------+ // Helper //--------------------------------------------------------------------+ -TU_ATTR_ALWAYS_INLINE static inline midih_interface_t* find_midi_by_daddr(uint8_t dev_addr) { - for (uint8_t i = 0; i < CFG_TUH_MIDI; i++) { - if (_midi_host[i].dev_addr == dev_addr) { - return &_midi_host[i]; +TU_ATTR_ALWAYS_INLINE static inline uint8_t find_new_midi_index(void) { + for (uint8_t idx = 0; idx < CFG_TUH_MIDI; idx++) { + if (_midi_host[idx].daddr == 0) { + return idx; } } - return NULL; + return TUSB_INDEX_INVALID_8; } -TU_ATTR_ALWAYS_INLINE static inline midih_interface_t* find_new_midi(void) { - return find_midi_by_daddr(0); +static inline uint8_t get_idx_by_ep_addr(uint8_t daddr, uint8_t ep_addr) { + for (uint8_t idx = 0; idx < CFG_TUH_MIDI; idx++) { + const midih_interface_t *p_midi = &_midi_host[idx]; + if ((p_midi->daddr == daddr) && + (ep_addr == p_midi->ep_stream.rx.ep_addr || ep_addr == p_midi->ep_stream.tx.ep_addr)) { + return idx; + } + } + return TUSB_INDEX_INVALID_8; } //--------------------------------------------------------------------+ @@ -122,32 +130,38 @@ bool midih_deinit(void) { return true; } -void midih_close(uint8_t dev_addr) { - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - if (p_midi_host == NULL) { - return; - } - if (tuh_midi_umount_cb) { - tuh_midi_umount_cb(dev_addr); - } - p_midi_host->ep_in = 0; - p_midi_host->ep_out = 0; - p_midi_host->itf_num = 0; - p_midi_host->num_cables_rx = 0; - p_midi_host->num_cables_tx = 0; - p_midi_host->dev_addr = 0; - p_midi_host->configured = false; - tu_memclr(&p_midi_host->stream_read, sizeof(p_midi_host->stream_read)); - tu_memclr(&p_midi_host->stream_write, sizeof(p_midi_host->stream_write)); +void midih_close(uint8_t daddr) { + for (uint8_t idx = 0; idx < CFG_TUH_MIDI; idx++) { + midih_interface_t* p_midi = &_midi_host[idx]; + if (p_midi->daddr == daddr) { + TU_LOG_DRV(" MIDI close addr = %u index = %u\r\n", daddr, idx); - tu_edpt_stream_close(&p_midi_host->ep_stream.rx); - tu_edpt_stream_close(&p_midi_host->ep_stream.tx); + if (tuh_midi_umount_cb) { + tuh_midi_umount_cb(idx); + } + + p_midi->ep_in = 0; + p_midi->ep_out = 0; + p_midi->bInterfaceNumber = 0; + p_midi->num_cables_rx = 0; + p_midi->num_cables_tx = 0; + p_midi->daddr = 0; + p_midi->mounted = false; + tu_memclr(&p_midi->stream_read, sizeof(p_midi->stream_read)); + tu_memclr(&p_midi->stream_write, sizeof(p_midi->stream_write)); + + tu_edpt_stream_close(&p_midi->ep_stream.rx); + tu_edpt_stream_close(&p_midi->ep_stream.tx); + } + } } bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { (void) result; - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); + const uint8_t idx = get_idx_by_ep_addr(dev_addr, ep_addr); + TU_VERIFY(idx < CFG_TUH_MIDI); + midih_interface_t *p_midi_host = &_midi_host[idx]; + if (ep_addr == p_midi_host->ep_stream.rx.ep_addr) { // receive new data if available if (xferred_bytes) { @@ -167,14 +181,14 @@ bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint } if (tuh_midi_rx_cb) { - tuh_midi_rx_cb(dev_addr, packets_queued); // invoke receive callback + tuh_midi_rx_cb(idx, packets_queued); } } tu_edpt_stream_read_xfer(dev_addr, &p_midi_host->ep_stream.rx); // prepare for next transfer } else if (ep_addr == p_midi_host->ep_stream.tx.ep_addr) { if (tuh_midi_tx_cb) { - tuh_midi_tx_cb(dev_addr); + tuh_midi_tx_cb(idx); } if (0 == tu_edpt_stream_write_xfer(dev_addr, &p_midi_host->ep_stream.tx)) { // If there is no data left, a ZLP should be sent if @@ -192,19 +206,20 @@ bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len) { (void) rhport; - midih_interface_t *p_midi = find_new_midi(); - TU_VERIFY(p_midi != NULL); TU_VERIFY(TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass); - - // There can be just a MIDI or an Audio + MIDI interface // const uint8_t* p_start = ((uint8_t const*) desc_itf); - const uint8_t* p_end = ((uint8_t const*) desc_itf) + max_len; - uint8_t const *p_desc = (uint8_t const *) desc_itf; - uint16_t len_parsed = 0; + const uint8_t *p_end = ((const uint8_t *) desc_itf) + max_len; + const uint8_t *p_desc = (const uint8_t *) desc_itf; + + const uint8_t idx = find_new_midi_index(); + TU_VERIFY(idx < CFG_TUH_MIDI); + midih_interface_t *p_midi = &_midi_host[idx]; + p_midi->itf_count = 0; tuh_midi_descriptor_cb_t desc_cb = { 0 }; desc_cb.jack_num = 0; + // There can be just a MIDI or an Audio + MIDI interface // If there is Audio Control Interface + Audio Header descriptor, skip it if (AUDIO_SUBCLASS_CONTROL == desc_itf->bInterfaceSubClass) { TU_VERIFY(max_len > 2*sizeof(tusb_desc_interface_t) + sizeof(audio_desc_cs_ac_interface_t)); @@ -216,15 +231,16 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d p_desc = tu_desc_next(p_desc); desc_itf = (tusb_desc_interface_t const *)p_desc; TU_VERIFY(TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass); + p_midi->itf_count = 1; } TU_VERIFY(AUDIO_SUBCLASS_MIDI_STREAMING == desc_itf->bInterfaceSubClass); - desc_cb.desc_interface = desc_itf; - - len_parsed += desc_itf->bLength; TU_LOG_DRV("MIDI opening Interface %u (addr = %u)\r\n", desc_itf->bInterfaceNumber, dev_addr); - p_midi->itf_num = desc_itf->bInterfaceNumber; - p_desc = tu_desc_next(p_desc); + p_midi->bInterfaceNumber = desc_itf->bInterfaceNumber; + p_midi->itf_count++; + desc_cb.desc_interface = desc_itf; + + p_desc = tu_desc_next(p_desc); // next to CS Header bool found_new_interface = false; while ((p_desc < p_end) && (tu_desc_next(p_desc) <= p_end) && !found_new_interface) { @@ -291,102 +307,115 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d } desc_cb.desc_interface_len = (uint16_t) ((uintptr_t)p_desc - (uintptr_t) desc_itf); - p_midi->dev_addr = dev_addr; + p_midi->daddr = dev_addr; if (tuh_midi_descriptor_cb) { - tuh_midi_descriptor_cb(dev_addr, &desc_cb); + tuh_midi_descriptor_cb(idx, &desc_cb); } return true; } bool midih_set_config(uint8_t dev_addr, uint8_t itf_num) { - (void) itf_num; - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - p_midi_host->configured = true; + uint8_t idx = tuh_midi_itf_get_index(dev_addr, itf_num); + TU_ASSERT(idx < CFG_TUH_MIDI); + midih_interface_t *p_midi = &_midi_host[idx]; + p_midi->mounted = true; if (tuh_midi_mount_cb) { - tuh_midi_mount_cb(dev_addr, p_midi_host->num_cables_rx, p_midi_host->num_cables_tx); + tuh_midi_mount_cb(idx, p_midi->num_cables_rx, p_midi->num_cables_tx); } - tu_edpt_stream_read_xfer(dev_addr, &p_midi_host->ep_stream.rx); // prepare for incoming data + tu_edpt_stream_read_xfer(dev_addr, &p_midi->ep_stream.rx); // prepare for incoming data // No special config things to do for MIDI - usbh_driver_set_config_complete(dev_addr, p_midi_host->itf_num); + usbh_driver_set_config_complete(dev_addr, p_midi->bInterfaceNumber); return true; } //--------------------------------------------------------------------+ // API //--------------------------------------------------------------------+ -bool tuh_midi_mounted(uint8_t dev_addr) { - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - return p_midi_host->configured; +bool tuh_midi_mounted(uint8_t idx) { + TU_VERIFY(idx < CFG_TUH_MIDI); + midih_interface_t *p_midi = &_midi_host[idx]; + return p_midi->mounted; } -uint8_t tuh_midi_get_num_tx_cables (uint8_t dev_addr) { - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL, 0); - TU_VERIFY(p_midi_host->ep_stream.tx.ep_addr != 0, 0); - return p_midi_host->num_cables_tx; +uint8_t tuh_midi_itf_get_index(uint8_t daddr, uint8_t itf_num) { + for (uint8_t idx = 0; idx < CFG_TUH_MIDI; idx++) { + const midih_interface_t *p_midi = &_midi_host[idx]; + if (p_midi->daddr == daddr && + (p_midi->bInterfaceNumber == itf_num || + p_midi->bInterfaceNumber == (uint8_t) (itf_num + p_midi->itf_count - 1))) { + return idx; + } + } + return TUSB_INDEX_INVALID_8; } -uint8_t tuh_midi_get_num_rx_cables (uint8_t dev_addr) { - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL, 0); - TU_VERIFY(p_midi_host->ep_stream.rx.ep_addr != 0, 0); - return p_midi_host->num_cables_rx; +uint8_t tuh_midi_get_num_tx_cables (uint8_t idx) { + TU_VERIFY(idx < CFG_TUH_MIDI); + midih_interface_t *p_midi = &_midi_host[idx]; + TU_VERIFY(p_midi->ep_stream.tx.ep_addr != 0, 0); + return p_midi->num_cables_tx; } -uint32_t tuh_midi_read_available(uint8_t dev_addr) { - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - return tu_edpt_stream_read_available(&p_midi_host->ep_stream.rx); +uint8_t tuh_midi_get_num_rx_cables (uint8_t idx) { + TU_VERIFY(idx < CFG_TUH_MIDI); + midih_interface_t *p_midi = &_midi_host[idx]; + TU_VERIFY(p_midi->ep_stream.rx.ep_addr != 0, 0); + return p_midi->num_cables_rx; } -uint32_t tuh_midi_write_flush(uint8_t dev_addr) { - midih_interface_t *p_midi = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi != NULL); - return tu_edpt_stream_write_xfer(p_midi->dev_addr, &p_midi->ep_stream.tx); +uint32_t tuh_midi_read_available(uint8_t idx) { + TU_VERIFY(idx < CFG_TUH_MIDI); + midih_interface_t *p_midi = &_midi_host[idx]; + return tu_edpt_stream_read_available(&p_midi->ep_stream.rx); +} + +uint32_t tuh_midi_write_flush(uint8_t idx) { + TU_VERIFY(idx < CFG_TUH_MIDI); + midih_interface_t *p_midi = &_midi_host[idx]; + return tu_edpt_stream_write_xfer(p_midi->daddr, &p_midi->ep_stream.tx); } //--------------------------------------------------------------------+ // Packet API //--------------------------------------------------------------------+ +uint32_t tuh_midi_packet_read_n(uint8_t idx, uint8_t* buffer, uint32_t bufsize) { + TU_VERIFY(idx < CFG_TUH_MIDI && buffer && bufsize > 0, 0); + midih_interface_t *p_midi = &_midi_host[idx]; -uint32_t tuh_midi_packet_read_n(uint8_t dev_addr, uint8_t* buffer, uint32_t bufsize) { - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - - uint32_t count4 = tu_min32(bufsize, tu_edpt_stream_read_available(&p_midi_host->ep_stream.rx)); + uint32_t count4 = tu_min32(bufsize, tu_edpt_stream_read_available(&p_midi->ep_stream.rx)); count4 = tu_align4(count4); // round down to multiple of 4 TU_VERIFY(count4 > 0, 0); - return tu_edpt_stream_read(dev_addr, &p_midi_host->ep_stream.rx, buffer, count4); + return tu_edpt_stream_read(p_midi->daddr, &p_midi->ep_stream.rx, buffer, count4); } -uint32_t tuh_midi_packet_write_n(uint8_t dev_addr, const uint8_t* buffer, uint32_t bufsize) { - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL, 0); - uint32_t bufsize4 = tu_align4(bufsize); - return tu_edpt_stream_write(dev_addr, &p_midi_host->ep_stream.tx, buffer, bufsize4); +uint32_t tuh_midi_packet_write_n(uint8_t idx, const uint8_t* buffer, uint32_t bufsize) { + TU_VERIFY(idx < CFG_TUH_MIDI && buffer && bufsize > 0, 0); + midih_interface_t *p_midi = &_midi_host[idx]; + + const uint32_t bufsize4 = tu_align4(bufsize); + TU_VERIFY(bufsize4 > 0, 0); + return tu_edpt_stream_write(p_midi->daddr, &p_midi->ep_stream.tx, buffer, bufsize4); } //--------------------------------------------------------------------+ // Stream API //--------------------------------------------------------------------+ #if CFG_TUH_MIDI_STREAM_API -uint32_t tuh_midi_stream_write(uint8_t dev_addr, uint8_t cable_num, uint8_t const *buffer, uint32_t bufsize) { - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); - TU_VERIFY(cable_num < p_midi_host->num_cables_tx); - midi_driver_stream_t *stream = &p_midi_host->stream_write; +uint32_t tuh_midi_stream_write(uint8_t idx, uint8_t cable_num, uint8_t const *buffer, uint32_t bufsize) { + TU_VERIFY(idx < CFG_TUH_MIDI && buffer && bufsize > 0); + midih_interface_t *p_midi = &_midi_host[idx]; + TU_VERIFY(cable_num < p_midi->num_cables_tx); + midi_driver_stream_t *stream = &p_midi->stream_write; - uint32_t i = 0; - while ((i < bufsize) && (tu_edpt_stream_write_available(dev_addr, &p_midi_host->ep_stream.tx) >= 4)) { - uint8_t const data = buffer[i]; - i++; + uint32_t byte_count = 0; + while ((byte_count < bufsize) && (tu_edpt_stream_write_available(p_midi->daddr, &p_midi->ep_stream.tx) >= 4)) { + uint8_t const data = buffer[byte_count]; + byte_count++; if (data >= MIDI_STATUS_SYSREAL_TIMING_CLOCK) { // real-time messages need to be sent right away midi_driver_stream_t streamrt; @@ -394,8 +423,8 @@ uint32_t tuh_midi_stream_write(uint8_t dev_addr, uint8_t cable_num, uint8_t cons streamrt.buffer[1] = data; streamrt.index = 2; streamrt.total = 2; - uint32_t const count = tu_edpt_stream_write(dev_addr, &p_midi_host->ep_stream.tx, streamrt.buffer, 4); - TU_ASSERT(count == 4, i); // Check FIFO overflown, since we already check fifo remaining. It is probably race condition + uint32_t const count = tu_edpt_stream_write(p_midi->daddr, &p_midi->ep_stream.tx, streamrt.buffer, 4); + TU_ASSERT(count == 4, byte_count); // Check FIFO overflown, since we already check fifo remaining. It is probably race condition } else if (stream->index == 0) { //------------- New event packet -------------// @@ -445,7 +474,7 @@ uint32_t tuh_midi_stream_write(uint8_t dev_addr, uint8_t cable_num, uint8_t cons } } else { //------------- On-going (buffering) packet -------------// - TU_ASSERT(stream->index < 4, i); + TU_ASSERT(stream->index < 4, byte_count); stream->buffer[stream->index] = data; stream->index++; // See if this byte ends a SysEx. @@ -458,42 +487,41 @@ uint32_t tuh_midi_stream_write(uint8_t dev_addr, uint8_t cable_num, uint8_t cons // Send out packet if (stream->index >= 2 && stream->index == stream->total) { // zeroes unused bytes - for (uint8_t idx = stream->total; idx < 4; idx++) { stream->buffer[idx] = 0; } + for (uint8_t i = stream->total; i < 4; i++) { + stream->buffer[i] = 0; + } TU_LOG3_MEM(stream->buffer, 4, 2); - uint32_t const count = tu_edpt_stream_write(dev_addr, &p_midi_host->ep_stream.tx, stream->buffer, 4); + const uint32_t count = tu_edpt_stream_write(p_midi->daddr, &p_midi->ep_stream.tx, stream->buffer, 4); // complete current event packet, reset stream stream->index = 0; stream->total = 0; // FIFO overflown, since we already check fifo remaining. It is probably race condition - TU_ASSERT(count == 4, i); + TU_ASSERT(count == 4, byte_count); } } - return i; + return byte_count; } -uint32_t tuh_midi_stream_read(uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p_buffer, uint16_t bufsize) { - midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr); - TU_VERIFY(p_midi_host != NULL); +uint32_t tuh_midi_stream_read(uint8_t idx, uint8_t *p_cable_num, uint8_t *p_buffer, uint16_t bufsize) { + TU_VERIFY(idx < CFG_TUH_MIDI && p_cable_num && p_buffer && bufsize > 0); + midih_interface_t *p_midi = &_midi_host[idx]; uint32_t bytes_buffered = 0; - TU_ASSERT(p_cable_num); - TU_ASSERT(p_buffer); - TU_ASSERT(bufsize); uint8_t one_byte; - if (!tu_edpt_stream_peek(&p_midi_host->ep_stream.rx, &one_byte)) { + if (!tu_edpt_stream_peek(&p_midi->ep_stream.rx, &one_byte)) { return 0; } *p_cable_num = (one_byte >> 4) & 0xf; - uint32_t nread = tu_edpt_stream_read(dev_addr, &p_midi_host->ep_stream.rx, p_midi_host->stream_read.buffer, 4); - static uint16_t cable_sysex_in_progress; // bit i is set if received MIDI_STATUS_SYSEX_START but not MIDI_STATUS_SYSEX_END + uint32_t nread = tu_edpt_stream_read(p_midi->daddr, &p_midi->ep_stream.rx, p_midi->stream_read.buffer, 4); + static uint16_t cable_sysex_in_progress;// bit i is set if received MIDI_STATUS_SYSEX_START but not MIDI_STATUS_SYSEX_END while (nread == 4 && bytes_buffered < bufsize) { - *p_cable_num = (p_midi_host->stream_read.buffer[0] >> 4) & 0x0f; + *p_cable_num = (p_midi->stream_read.buffer[0] >> 4) & 0x0f; uint8_t bytes_to_add_to_stream = 0; - if (*p_cable_num < p_midi_host->num_cables_rx) { + if (*p_cable_num < p_midi->num_cables_rx) { // ignore the CIN field; too many devices out there encode this wrong - uint8_t status = p_midi_host->stream_read.buffer[1]; + uint8_t status = p_midi->stream_read.buffer[1]; uint16_t cable_mask = (uint16_t) (1 << *p_cable_num); if (status <= MIDI_MAX_DATA_VAL || status == MIDI_STATUS_SYSEX_START) { if (status == MIDI_STATUS_SYSEX_START) { @@ -502,13 +530,13 @@ uint32_t tuh_midi_stream_read(uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p // only add the packet if a sysex message is in progress if (cable_sysex_in_progress & cable_mask) { ++bytes_to_add_to_stream; - for (uint8_t idx = 2; idx < 4; idx++) { - if (p_midi_host->stream_read.buffer[idx] <= MIDI_MAX_DATA_VAL) { + for (uint8_t i = 2; i < 4; i++) { + if (p_midi->stream_read.buffer[i] <= MIDI_MAX_DATA_VAL) { ++bytes_to_add_to_stream; - } else if (p_midi_host->stream_read.buffer[idx] == MIDI_STATUS_SYSEX_END) { + } else if (p_midi->stream_read.buffer[i] == MIDI_STATUS_SYSEX_END) { ++bytes_to_add_to_stream; cable_sysex_in_progress &= (uint16_t) ~cable_mask; - idx = 4;// force the loop to exit; I hate break statements in loops + i = 4;// force the loop to exit; I hate break statements in loops } } } @@ -555,23 +583,22 @@ uint32_t tuh_midi_stream_read(uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p } } - for (uint8_t idx = 1; idx <= bytes_to_add_to_stream; idx++) { - *p_buffer++ = p_midi_host->stream_read.buffer[idx]; + for (uint8_t i = 1; i <= bytes_to_add_to_stream; i++) { + *p_buffer++ = p_midi->stream_read.buffer[i]; } bytes_buffered += bytes_to_add_to_stream; nread = 0; - if (tu_edpt_stream_peek(&p_midi_host->ep_stream.rx, &one_byte)) { + if (tu_edpt_stream_peek(&p_midi->ep_stream.rx, &one_byte)) { uint8_t new_cable = (one_byte >> 4) & 0xf; if (new_cable == *p_cable_num) { // still on the same cable. Continue reading the stream - nread = tu_edpt_stream_read(dev_addr, &p_midi_host->ep_stream.rx, p_midi_host->stream_read.buffer, 4); + nread = tu_edpt_stream_read(p_midi->daddr, &p_midi->ep_stream.rx, p_midi->stream_read.buffer, 4); } } } return bytes_buffered; } - #endif #endif diff --git a/src/class/midi/midi_host.h b/src/class/midi/midi_host.h index 286e034f1..c9780a9e4 100644 --- a/src/class/midi/midi_host.h +++ b/src/class/midi/midi_host.h @@ -76,22 +76,26 @@ typedef struct { } tuh_midi_descriptor_cb_t; // Check if MIDI interface is mounted -bool tuh_midi_mounted(uint8_t dev_addr); +bool tuh_midi_mounted(uint8_t idx); + +// Get Interface index from device address + interface number +// return TUSB_INDEX_INVALID_8 (0xFF) if not found +uint8_t tuh_midi_itf_get_index(uint8_t daddr, uint8_t itf_num); // return the number of virtual midi cables on the device's IN endpoint -uint8_t tuh_midi_get_num_rx_cables(uint8_t dev_addr); +uint8_t tuh_midi_get_num_rx_cables(uint8_t idx); // return the number of virtual midi cables on the device's OUT endpoint -uint8_t tuh_midi_get_num_tx_cables(uint8_t dev_addr); +uint8_t tuh_midi_get_num_tx_cables(uint8_t idx); // return the raw number of bytes available. // Note: this is related but not the same as number of stream bytes available. -uint32_t tuh_midi_read_available(uint8_t dev_addr); +uint32_t tuh_midi_read_available(uint8_t idx); // Send any queued packets to the device if the host hardware is able to do it // Returns the number of bytes flushed to the host hardware or 0 if // the host hardware is busy or there is nothing in queue to send. -uint32_t tuh_midi_write_flush( uint8_t dev_addr); +uint32_t tuh_midi_write_flush(uint8_t idx); //--------------------------------------------------------------------+ // Packet API @@ -99,24 +103,24 @@ uint32_t tuh_midi_write_flush( uint8_t dev_addr); // Read all available MIDI packets from the connected device // Return number of bytes read (always multiple of 4) -uint32_t tuh_midi_packet_read_n(uint8_t dev_addr, uint8_t* buffer, uint32_t bufsize); +uint32_t tuh_midi_packet_read_n(uint8_t idx, uint8_t* buffer, uint32_t bufsize); // Read a raw MIDI packet from the connected device // Return true if a packet was returned TU_ATTR_ALWAYS_INLINE static inline -bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]) { - return 4 == tuh_midi_packet_read_n(dev_addr, packet, 4); +bool tuh_midi_packet_read (uint8_t idx, uint8_t packet[4]) { + return 4 == tuh_midi_packet_read_n(idx, packet, 4); } // Write all 4-byte packets, data is locally buffered and only transferred when buffered bytes // reach the endpoint packet size or tuh_midi_write_flush() is called -uint32_t tuh_midi_packet_write_n(uint8_t dev_addr, const uint8_t* buffer, uint32_t bufsize); +uint32_t tuh_midi_packet_write_n(uint8_t idx, const uint8_t* buffer, uint32_t bufsize); // Write a 4-bytes packet to the device. // Returns true if the packet was successfully queued. TU_ATTR_ALWAYS_INLINE static inline -bool tuh_midi_packet_write (uint8_t dev_addr, uint8_t const packet[4]) { - return 4 == tuh_midi_packet_write_n(dev_addr, packet, 4); +bool tuh_midi_packet_write (uint8_t idx, uint8_t const packet[4]) { + return 4 == tuh_midi_packet_write_n(idx, packet, 4); } //--------------------------------------------------------------------+ @@ -127,7 +131,7 @@ bool tuh_midi_packet_write (uint8_t dev_addr, uint8_t const packet[4]) { // Queue a message to the device using stream API. data is locally buffered and only transferred when buffered bytes // reach the endpoint packet size or tuh_midi_write_flush() is called // Returns number of bytes was successfully queued. -uint32_t tuh_midi_stream_write (uint8_t dev_addr, uint8_t cable_num, uint8_t const* p_buffer, uint32_t bufsize); +uint32_t tuh_midi_stream_write(uint8_t idx, uint8_t cable_num, uint8_t const *p_buffer, uint32_t bufsize); // Get the MIDI stream from the device. Set the value pointed // to by p_cable_num to the MIDI cable number intended to receive it. @@ -136,7 +140,7 @@ uint32_t tuh_midi_stream_write (uint8_t dev_addr, uint8_t cable_num, uint8_t con // Note that this function ignores the CIN field of the MIDI packet // because a number of commercial devices out there do not encode // it properly. -uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p_buffer, uint16_t bufsize); +uint32_t tuh_midi_stream_read(uint8_t idx, uint8_t *p_cable_num, uint8_t *p_buffer, uint16_t bufsize); #endif @@ -146,16 +150,16 @@ uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t * // Invoked when MIDI interface is detected in enumeration. Application can copy/parse descriptor if needed. // Note: may be fired before tuh_midi_mount_cb(), therefore midi interface is not mounted/ready. -TU_ATTR_WEAK void tuh_midi_descriptor_cb(uint8_t dev_addr, tuh_midi_descriptor_cb_t const* desc_cb); +TU_ATTR_WEAK void tuh_midi_descriptor_cb(uint8_t idx, const tuh_midi_descriptor_cb_t * desc_cb); // Invoked when device with MIDI interface is mounted. -TU_ATTR_WEAK void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t num_cables_rx, uint16_t num_cables_tx); +TU_ATTR_WEAK void tuh_midi_mount_cb(uint8_t idx, uint8_t num_cables_rx, uint16_t num_cables_tx); // Invoked when device with MIDI interface is un-mounted -TU_ATTR_WEAK void tuh_midi_umount_cb(uint8_t dev_addr); +TU_ATTR_WEAK void tuh_midi_umount_cb(uint8_t idx); -TU_ATTR_WEAK void tuh_midi_rx_cb(uint8_t dev_addr, uint32_t num_packets); -TU_ATTR_WEAK void tuh_midi_tx_cb(uint8_t dev_addr); +TU_ATTR_WEAK void tuh_midi_rx_cb(uint8_t idx, uint32_t num_packets); +TU_ATTR_WEAK void tuh_midi_tx_cb(uint8_t idx); //--------------------------------------------------------------------+ // Internal Class Driver API @@ -165,7 +169,7 @@ bool midih_deinit (void); bool midih_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len); bool midih_set_config (uint8_t dev_addr, uint8_t itf_num); bool midih_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); -void midih_close (uint8_t dev_addr); +void midih_close (uint8_t daddr); #ifdef __cplusplus } From 56e84bd1a605e3835f29e2e7fd17f65e0f3b5431 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 24 Feb 2025 12:42:45 +0700 Subject: [PATCH 27/30] add desc_audio_control to midi descriptor callback --- examples/host/midi_rx/src/main.c | 2 +- examples/host/midi_rx/src/tusb_config.h | 2 -- src/class/midi/midi_host.c | 7 ++++--- src/class/midi/midi_host.h | 5 +++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/host/midi_rx/src/main.c b/examples/host/midi_rx/src/main.c index a8a5b40ca..6db329ab5 100644 --- a/examples/host/midi_rx/src/main.c +++ b/examples/host/midi_rx/src/main.c @@ -106,8 +106,8 @@ void tuh_midi_rx_cb(uint8_t idx, uint32_t num_packets) { return; } - uint8_t cable_num; uint8_t buffer[48]; + uint8_t cable_num = 0; uint32_t bytes_read = tuh_midi_stream_read(idx, &cable_num, buffer, sizeof(buffer)); printf("Cable %u rx %lu bytes: ", cable_num, bytes_read); diff --git a/examples/host/midi_rx/src/tusb_config.h b/examples/host/midi_rx/src/tusb_config.h index 24b3b3ae6..c9b430388 100644 --- a/examples/host/midi_rx/src/tusb_config.h +++ b/examples/host/midi_rx/src/tusb_config.h @@ -111,8 +111,6 @@ extern "C" { #define CFG_TUH_DEVICE_MAX (3*CFG_TUH_HUB + 1) #define CFG_TUH_MIDI CFG_TUH_DEVICE_MAX -#define CFG_MIDI_HOST_DEVSTRINGS 1 - #ifdef __cplusplus } #endif diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index 8fb325c2b..770a39a8d 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -227,9 +227,10 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d p_desc = tu_desc_next(p_desc); TU_VERIFY(tu_desc_type(p_desc) == TUSB_DESC_CS_INTERFACE && tu_desc_subtype(p_desc) == AUDIO_CS_AC_INTERFACE_HEADER); + desc_cb.desc_audio_control = desc_itf; p_desc = tu_desc_next(p_desc); - desc_itf = (tusb_desc_interface_t const *)p_desc; + desc_itf = (const tusb_desc_interface_t *)p_desc; TU_VERIFY(TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass); p_midi->itf_count = 1; } @@ -238,7 +239,7 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d TU_LOG_DRV("MIDI opening Interface %u (addr = %u)\r\n", desc_itf->bInterfaceNumber, dev_addr); p_midi->bInterfaceNumber = desc_itf->bInterfaceNumber; p_midi->itf_count++; - desc_cb.desc_interface = desc_itf; + desc_cb.desc_midi = desc_itf; p_desc = tu_desc_next(p_desc); // next to CS Header @@ -305,7 +306,7 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d } p_desc = tu_desc_next(p_desc); } - desc_cb.desc_interface_len = (uint16_t) ((uintptr_t)p_desc - (uintptr_t) desc_itf); + desc_cb.desc_midi_total_len = (uint16_t) ((uintptr_t)p_desc - (uintptr_t) desc_itf); p_midi->daddr = dev_addr; diff --git a/src/class/midi/midi_host.h b/src/class/midi/midi_host.h index c9780a9e4..717e6ba67 100644 --- a/src/class/midi/midi_host.h +++ b/src/class/midi/midi_host.h @@ -63,8 +63,9 @@ // Application API //--------------------------------------------------------------------+ typedef struct { - const tusb_desc_interface_t* desc_interface; // start of whole midi interface descriptor - uint16_t desc_interface_len; + const tusb_desc_interface_t* desc_audio_control; + const tusb_desc_interface_t* desc_midi; // start of whole midi interface descriptor + uint16_t desc_midi_total_len; const uint8_t* desc_header; const uint8_t* desc_element; From d132044b7587f7e846bbec6e7b897d0842a21d13 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 24 Feb 2025 15:40:48 +0700 Subject: [PATCH 28/30] add tuh_midi_mount_cb_t struct for tuh_midi_mount_cb() change tuh_midi_rx/tx_cb() to have xferred_bytes rename tuh_midi_get_num_rx/tx_cables() to tuh_midi_get_rx/tx_cable_count() use default empty callback instead of weak null to be compatible with keil compiler --- examples/host/cdc_msc_hid/src/main.c | 4 +- examples/host/midi_rx/src/main.c | 13 ++--- src/class/cdc/cdc_host.h | 8 +-- src/class/midi/midi_device.c | 5 -- src/class/midi/midi_host.c | 81 ++++++++++++---------------- src/class/midi/midi_host.h | 34 +++++++----- src/host/usbh_pvt.h | 4 -- 7 files changed, 70 insertions(+), 79 deletions(-) diff --git a/examples/host/cdc_msc_hid/src/main.c b/examples/host/cdc_msc_hid/src/main.c index 71257c0fe..2d5df23f9 100644 --- a/examples/host/cdc_msc_hid/src/main.c +++ b/examples/host/cdc_msc_hid/src/main.c @@ -82,12 +82,12 @@ int main(void) { void tuh_mount_cb(uint8_t dev_addr) { // application set-up - printf("A device with address %d is mounted\r\n", dev_addr); + printf("A device with address %u is mounted\r\n", dev_addr); } void tuh_umount_cb(uint8_t dev_addr) { // application tear-down - printf("A device with address %d is unmounted \r\n", dev_addr); + printf("A device with address %u is unmounted \r\n", dev_addr); } diff --git a/examples/host/midi_rx/src/main.c b/examples/host/midi_rx/src/main.c index 6db329ab5..d26321e5d 100644 --- a/examples/host/midi_rx/src/main.c +++ b/examples/host/midi_rx/src/main.c @@ -91,9 +91,9 @@ void midi_host_rx_task(void) { //--------------------------------------------------------------------+ // Invoked when device with MIDI interface is mounted. -void tuh_midi_mount_cb(uint8_t idx, uint8_t num_cables_rx, uint16_t num_cables_tx) { - printf("MIDI Interface Index = %u, Number of RX cables = %u, Number of TX cables = %u\r\n", - idx, num_cables_rx, num_cables_tx); +void tuh_midi_mount_cb(uint8_t idx, const tuh_midi_mount_cb_t* mount_cb_data) { + printf("MIDI Interface Index = %u, Address = %u, Number of RX cables = %u, Number of TX cables = %u\r\n", + idx, mount_cb_data->daddr, mount_cb_data->rx_cable_count, mount_cb_data->tx_cable_count); } // Invoked when device with hid interface is un-mounted @@ -101,8 +101,8 @@ void tuh_midi_umount_cb(uint8_t idx) { printf("MIDI Interface Index = %u is unmounted\r\n", idx); } -void tuh_midi_rx_cb(uint8_t idx, uint32_t num_packets) { - if (num_packets == 0) { +void tuh_midi_rx_cb(uint8_t idx, uint32_t xferred_bytes) { + if (xferred_bytes == 0) { return; } @@ -117,6 +117,7 @@ void tuh_midi_rx_cb(uint8_t idx, uint32_t num_packets) { printf("\r\n"); } -void tuh_midi_tx_cb(uint8_t idx) { +void tuh_midi_tx_cb(uint8_t idx, uint32_t xferred_bytes) { (void) idx; + (void) xferred_bytes; } diff --git a/src/class/cdc/cdc_host.h b/src/class/cdc/cdc_host.h index df975b2f0..8c5399534 100644 --- a/src/class/cdc/cdc_host.h +++ b/src/class/cdc/cdc_host.h @@ -49,22 +49,22 @@ // RX FIFO size #ifndef CFG_TUH_CDC_RX_BUFSIZE -#define CFG_TUH_CDC_RX_BUFSIZE USBH_EPSIZE_BULK_MAX +#define CFG_TUH_CDC_RX_BUFSIZE TUH_EPSIZE_BULK_MPS #endif // RX Endpoint size #ifndef CFG_TUH_CDC_RX_EPSIZE -#define CFG_TUH_CDC_RX_EPSIZE USBH_EPSIZE_BULK_MAX +#define CFG_TUH_CDC_RX_EPSIZE TUH_EPSIZE_BULK_MPS #endif // TX FIFO size #ifndef CFG_TUH_CDC_TX_BUFSIZE -#define CFG_TUH_CDC_TX_BUFSIZE USBH_EPSIZE_BULK_MAX +#define CFG_TUH_CDC_TX_BUFSIZE TUH_EPSIZE_BULK_MPS #endif // TX Endpoint size #ifndef CFG_TUH_CDC_TX_EPSIZE -#define CFG_TUH_CDC_TX_EPSIZE USBH_EPSIZE_BULK_MAX +#define CFG_TUH_CDC_TX_EPSIZE TUH_EPSIZE_BULK_MPS #endif //--------------------------------------------------------------------+ diff --git a/src/class/midi/midi_device.c b/src/class/midi/midi_device.c index 7b6705640..0bbb3caf4 100644 --- a/src/class/midi/midi_device.c +++ b/src/class/midi/midi_device.c @@ -39,9 +39,6 @@ //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ - - - typedef struct { uint8_t itf_num; uint8_t ep_in; @@ -440,8 +437,6 @@ uint16_t midid_open(uint8_t rhport, const tusb_desc_interface_t* desc_itf, uint1 drv_len += tu_desc_len(p_desc); p_desc = tu_desc_next(p_desc); } - } else { - TU_LOG2("Warning: MIDI Device has no Audio Control Interface"); } // 2nd Interface is MIDI Streaming diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index 770a39a8d..a4b1ebd2e 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -40,6 +40,15 @@ #define TU_LOG_DRV(...) TU_LOG(CFG_TUH_MIDI_LOG_LEVEL, __VA_ARGS__) +//--------------------------------------------------------------------+ +// Weak stubs: invoked if no strong implementation is available +//--------------------------------------------------------------------+ +TU_ATTR_WEAK void tuh_midi_descriptor_cb(uint8_t idx, const tuh_midi_descriptor_cb_t * desc_cb_data) { (void) idx; (void) desc_cb_data; } +TU_ATTR_WEAK void tuh_midi_mount_cb(uint8_t idx, const tuh_midi_mount_cb_t* mount_cb_data) { (void) idx; (void) mount_cb_data; } +TU_ATTR_WEAK void tuh_midi_umount_cb(uint8_t idx) { (void) idx; } +TU_ATTR_WEAK void tuh_midi_rx_cb(uint8_t idx, uint32_t xferred_bytes) { (void) idx; (void) xferred_bytes; } +TU_ATTR_WEAK void tuh_midi_tx_cb(uint8_t idx, uint32_t xferred_bytes) { (void) idx; (void) xferred_bytes; } + //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ @@ -52,8 +61,8 @@ typedef struct { uint8_t ep_in; // IN endpoint address uint8_t ep_out; // OUT endpoint address - uint8_t num_cables_rx; // IN endpoint CS descriptor bNumEmbMIDIJack value - uint8_t num_cables_tx; // OUT endpoint CS descriptor bNumEmbMIDIJack value + uint8_t rx_cable_count; // IN endpoint CS descriptor bNumEmbMIDIJack value + uint8_t tx_cable_count; // OUT endpoint CS descriptor bNumEmbMIDIJack value #if CFG_TUH_MIDI_STREAM_API // For Stream read()/write() API @@ -135,16 +144,13 @@ void midih_close(uint8_t daddr) { midih_interface_t* p_midi = &_midi_host[idx]; if (p_midi->daddr == daddr) { TU_LOG_DRV(" MIDI close addr = %u index = %u\r\n", daddr, idx); - - if (tuh_midi_umount_cb) { - tuh_midi_umount_cb(idx); - } + tuh_midi_umount_cb(idx); p_midi->ep_in = 0; p_midi->ep_out = 0; p_midi->bInterfaceNumber = 0; - p_midi->num_cables_rx = 0; - p_midi->num_cables_tx = 0; + p_midi->rx_cable_count = 0; + p_midi->tx_cable_count = 0; p_midi->daddr = 0; p_midi->mounted = false; tu_memclr(&p_midi->stream_read, sizeof(p_midi->stream_read)); @@ -163,33 +169,16 @@ bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint midih_interface_t *p_midi_host = &_midi_host[idx]; if (ep_addr == p_midi_host->ep_stream.rx.ep_addr) { - // receive new data if available + // receive new data, put it into FIFO and invoke callback if available if (xferred_bytes) { - // put in the RX FIFO only non-zero MIDI IN 4-byte packets - uint32_t packets_queued = 0; - uint8_t *buf = _midi_epbuf->rx; - const uint32_t npackets = xferred_bytes / 4; - for (uint32_t p = 0; p < npackets; p++) { - // some devices send back all zero packets even if there is no data ready - const uint32_t packet = tu_unaligned_read32(buf); - if (packet != 0) { - tu_edpt_stream_read_xfer_complete_with_buf(&p_midi_host->ep_stream.rx, buf, 4); - ++packets_queued; - TU_LOG3("MIDI RX=%08x\r\n", packet); - } - buf += 4; - } - - if (tuh_midi_rx_cb) { - tuh_midi_rx_cb(idx, packets_queued); - } + tu_edpt_stream_read_xfer_complete(&p_midi_host->ep_stream.rx, xferred_bytes); + tuh_midi_rx_cb(idx, xferred_bytes); } tu_edpt_stream_read_xfer(dev_addr, &p_midi_host->ep_stream.rx); // prepare for next transfer } else if (ep_addr == p_midi_host->ep_stream.tx.ep_addr) { - if (tuh_midi_tx_cb) { - tuh_midi_tx_cb(idx); - } + tuh_midi_tx_cb(idx, xferred_bytes); + if (0 == tu_edpt_stream_write_xfer(dev_addr, &p_midi_host->ep_stream.tx)) { // If there is no data left, a ZLP should be sent if // xferred_bytes is multiple of EP size and not zero @@ -207,7 +196,6 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d (void) rhport; TU_VERIFY(TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass); - // const uint8_t* p_start = ((uint8_t const*) desc_itf); const uint8_t *p_end = ((const uint8_t *) desc_itf) + max_len; const uint8_t *p_desc = (const uint8_t *) desc_itf; @@ -286,14 +274,14 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d TU_LOG_DRV(" Endpoint and CS_Endpoint descriptor %02x\r\n", p_ep->bEndpointAddress); if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_OUT) { p_midi->ep_out = p_ep->bEndpointAddress; - p_midi->num_cables_tx = p_csep->bNumEmbMIDIJack; + p_midi->tx_cable_count = p_csep->bNumEmbMIDIJack; desc_cb.desc_epout = p_ep; TU_ASSERT(tuh_edpt_open(dev_addr, p_ep)); tu_edpt_stream_open(&p_midi->ep_stream.tx, p_ep); } else { p_midi->ep_in = p_ep->bEndpointAddress; - p_midi->num_cables_rx = p_csep->bNumEmbMIDIJack; + p_midi->rx_cable_count = p_csep->bNumEmbMIDIJack; desc_cb.desc_epin = p_ep; TU_ASSERT(tuh_edpt_open(dev_addr, p_ep)); @@ -309,10 +297,7 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d desc_cb.desc_midi_total_len = (uint16_t) ((uintptr_t)p_desc - (uintptr_t) desc_itf); p_midi->daddr = dev_addr; - - if (tuh_midi_descriptor_cb) { - tuh_midi_descriptor_cb(idx, &desc_cb); - } + tuh_midi_descriptor_cb(idx, &desc_cb); return true; } @@ -323,9 +308,13 @@ bool midih_set_config(uint8_t dev_addr, uint8_t itf_num) { midih_interface_t *p_midi = &_midi_host[idx]; p_midi->mounted = true; - if (tuh_midi_mount_cb) { - tuh_midi_mount_cb(idx, p_midi->num_cables_rx, p_midi->num_cables_tx); - } + const tuh_midi_mount_cb_t mount_cb_data = { + .daddr = dev_addr, + .bInterfaceNumber = itf_num, + .rx_cable_count = p_midi->rx_cable_count, + .tx_cable_count = p_midi->tx_cable_count, + }; + tuh_midi_mount_cb(idx, &mount_cb_data); tu_edpt_stream_read_xfer(dev_addr, &p_midi->ep_stream.rx); // prepare for incoming data @@ -355,18 +344,18 @@ uint8_t tuh_midi_itf_get_index(uint8_t daddr, uint8_t itf_num) { return TUSB_INDEX_INVALID_8; } -uint8_t tuh_midi_get_num_tx_cables (uint8_t idx) { +uint8_t tuh_midi_get_tx_cable_count (uint8_t idx) { TU_VERIFY(idx < CFG_TUH_MIDI); midih_interface_t *p_midi = &_midi_host[idx]; TU_VERIFY(p_midi->ep_stream.tx.ep_addr != 0, 0); - return p_midi->num_cables_tx; + return p_midi->tx_cable_count; } -uint8_t tuh_midi_get_num_rx_cables (uint8_t idx) { +uint8_t tuh_midi_get_rx_cable_count (uint8_t idx) { TU_VERIFY(idx < CFG_TUH_MIDI); midih_interface_t *p_midi = &_midi_host[idx]; TU_VERIFY(p_midi->ep_stream.rx.ep_addr != 0, 0); - return p_midi->num_cables_rx; + return p_midi->rx_cable_count; } uint32_t tuh_midi_read_available(uint8_t idx) { @@ -410,7 +399,7 @@ uint32_t tuh_midi_packet_write_n(uint8_t idx, const uint8_t* buffer, uint32_t bu uint32_t tuh_midi_stream_write(uint8_t idx, uint8_t cable_num, uint8_t const *buffer, uint32_t bufsize) { TU_VERIFY(idx < CFG_TUH_MIDI && buffer && bufsize > 0); midih_interface_t *p_midi = &_midi_host[idx]; - TU_VERIFY(cable_num < p_midi->num_cables_tx); + TU_VERIFY(cable_num < p_midi->tx_cable_count); midi_driver_stream_t *stream = &p_midi->stream_write; uint32_t byte_count = 0; @@ -520,7 +509,7 @@ uint32_t tuh_midi_stream_read(uint8_t idx, uint8_t *p_cable_num, uint8_t *p_buff while (nread == 4 && bytes_buffered < bufsize) { *p_cable_num = (p_midi->stream_read.buffer[0] >> 4) & 0x0f; uint8_t bytes_to_add_to_stream = 0; - if (*p_cable_num < p_midi->num_cables_rx) { + if (*p_cable_num < p_midi->rx_cable_count) { // ignore the CIN field; too many devices out there encode this wrong uint8_t status = p_midi->stream_read.buffer[1]; uint16_t cable_mask = (uint16_t) (1 << *p_cable_num); diff --git a/src/class/midi/midi_host.h b/src/class/midi/midi_host.h index 717e6ba67..b06bb43d7 100644 --- a/src/class/midi/midi_host.h +++ b/src/class/midi/midi_host.h @@ -37,10 +37,6 @@ //--------------------------------------------------------------------+ // Class Driver Configuration //--------------------------------------------------------------------+ -#ifndef CFG_TUH_MAX_CABLES -#define CFG_TUH_MAX_CABLES 16 -#endif - #ifndef CFG_TUH_MIDI_RX_BUFSIZE #define CFG_TUH_MIDI_RX_BUFSIZE TUH_EPSIZE_BULK_MPS #endif @@ -60,7 +56,7 @@ #endif //--------------------------------------------------------------------+ -// Application API +// Application Types //--------------------------------------------------------------------+ typedef struct { const tusb_desc_interface_t* desc_audio_control; @@ -76,6 +72,17 @@ typedef struct { const uint8_t* desc_jack[16]; // list of jack descriptors (embedded + external) } tuh_midi_descriptor_cb_t; +typedef struct { + uint8_t daddr; + uint8_t bInterfaceNumber; // interface number of MIDI streaming + uint8_t rx_cable_count; + uint8_t tx_cable_count; +} tuh_midi_mount_cb_t; + +//--------------------------------------------------------------------+ +// Application API +//--------------------------------------------------------------------+ + // Check if MIDI interface is mounted bool tuh_midi_mounted(uint8_t idx); @@ -84,10 +91,10 @@ bool tuh_midi_mounted(uint8_t idx); uint8_t tuh_midi_itf_get_index(uint8_t daddr, uint8_t itf_num); // return the number of virtual midi cables on the device's IN endpoint -uint8_t tuh_midi_get_num_rx_cables(uint8_t idx); +uint8_t tuh_midi_get_rx_cable_count(uint8_t idx); // return the number of virtual midi cables on the device's OUT endpoint -uint8_t tuh_midi_get_num_tx_cables(uint8_t idx); +uint8_t tuh_midi_get_tx_cable_count(uint8_t idx); // return the raw number of bytes available. // Note: this is related but not the same as number of stream bytes available. @@ -151,16 +158,19 @@ uint32_t tuh_midi_stream_read(uint8_t idx, uint8_t *p_cable_num, uint8_t *p_buff // Invoked when MIDI interface is detected in enumeration. Application can copy/parse descriptor if needed. // Note: may be fired before tuh_midi_mount_cb(), therefore midi interface is not mounted/ready. -TU_ATTR_WEAK void tuh_midi_descriptor_cb(uint8_t idx, const tuh_midi_descriptor_cb_t * desc_cb); +void tuh_midi_descriptor_cb(uint8_t idx, const tuh_midi_descriptor_cb_t * desc_cb_data); // Invoked when device with MIDI interface is mounted. -TU_ATTR_WEAK void tuh_midi_mount_cb(uint8_t idx, uint8_t num_cables_rx, uint16_t num_cables_tx); +void tuh_midi_mount_cb(uint8_t idx, const tuh_midi_mount_cb_t* mount_cb_data); // Invoked when device with MIDI interface is un-mounted -TU_ATTR_WEAK void tuh_midi_umount_cb(uint8_t idx); +void tuh_midi_umount_cb(uint8_t idx); -TU_ATTR_WEAK void tuh_midi_rx_cb(uint8_t idx, uint32_t num_packets); -TU_ATTR_WEAK void tuh_midi_tx_cb(uint8_t idx); +// Invoked when received new data +void tuh_midi_rx_cb(uint8_t idx, uint32_t xferred_bytes); + +// Invoked when a TX is complete and therefore space becomes available in TX buffer +void tuh_midi_tx_cb(uint8_t idx, uint32_t xferred_bytes); //--------------------------------------------------------------------+ // Internal Class Driver API diff --git a/src/host/usbh_pvt.h b/src/host/usbh_pvt.h index d27c4baa2..bfa1fb2ba 100644 --- a/src/host/usbh_pvt.h +++ b/src/host/usbh_pvt.h @@ -41,10 +41,6 @@ #define TU_LOG_INT_USBH(...) TU_LOG_INT(CFG_TUH_LOG_LEVEL, __VA_ARGS__) #define TU_LOG_HEX_USBH(...) TU_LOG_HEX(CFG_TUH_LOG_LEVEL, __VA_ARGS__) -enum { - USBH_EPSIZE_BULK_MAX = (TUH_OPT_HIGH_SPEED ? TUSB_EPSIZE_BULK_HS : TUSB_EPSIZE_BULK_FS) -}; - //--------------------------------------------------------------------+ // Class Driver API //--------------------------------------------------------------------+ From 39e6375b74b38b01e7868c283874b3a305b524e1 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 6 Mar 2025 10:26:45 +0700 Subject: [PATCH 29/30] midi host: skip rx data with all zeroes --- examples/host/midi_rx/src/main.c | 2 +- src/class/midi/midi_host.c | 17 +++++++------ src/common/tusb_common.h | 42 ++++++++++++++++++++------------ 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/examples/host/midi_rx/src/main.c b/examples/host/midi_rx/src/main.c index d26321e5d..78b1a11b9 100644 --- a/examples/host/midi_rx/src/main.c +++ b/examples/host/midi_rx/src/main.c @@ -110,7 +110,7 @@ void tuh_midi_rx_cb(uint8_t idx, uint32_t xferred_bytes) { uint8_t cable_num = 0; uint32_t bytes_read = tuh_midi_stream_read(idx, &cable_num, buffer, sizeof(buffer)); - printf("Cable %u rx %lu bytes: ", cable_num, bytes_read); + printf("Cable %u rx: ", cable_num); for (uint32_t i = 0; i < bytes_read; i++) { printf("%02X ", buffer[i]); } diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index a4b1ebd2e..7f7d536d8 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -166,23 +166,24 @@ bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint (void) result; const uint8_t idx = get_idx_by_ep_addr(dev_addr, ep_addr); TU_VERIFY(idx < CFG_TUH_MIDI); - midih_interface_t *p_midi_host = &_midi_host[idx]; + midih_interface_t *p_midi = &_midi_host[idx]; - if (ep_addr == p_midi_host->ep_stream.rx.ep_addr) { + if (ep_addr == p_midi->ep_stream.rx.ep_addr) { // receive new data, put it into FIFO and invoke callback if available - if (xferred_bytes) { - tu_edpt_stream_read_xfer_complete(&p_midi_host->ep_stream.rx, xferred_bytes); + // Note: some devices send back all zero packets even if there is no data ready + if (xferred_bytes && !tu_mem_is_zero(p_midi->ep_stream.rx.ep_buf, xferred_bytes)) { + tu_edpt_stream_read_xfer_complete(&p_midi->ep_stream.rx, xferred_bytes); tuh_midi_rx_cb(idx, xferred_bytes); } - tu_edpt_stream_read_xfer(dev_addr, &p_midi_host->ep_stream.rx); // prepare for next transfer - } else if (ep_addr == p_midi_host->ep_stream.tx.ep_addr) { + tu_edpt_stream_read_xfer(dev_addr, &p_midi->ep_stream.rx); // prepare for next transfer + } else if (ep_addr == p_midi->ep_stream.tx.ep_addr) { tuh_midi_tx_cb(idx, xferred_bytes); - if (0 == tu_edpt_stream_write_xfer(dev_addr, &p_midi_host->ep_stream.tx)) { + if (0 == tu_edpt_stream_write_xfer(dev_addr, &p_midi->ep_stream.tx)) { // If there is no data left, a ZLP should be sent if // xferred_bytes is multiple of EP size and not zero - tu_edpt_stream_write_zlp_if_needed(dev_addr, &p_midi_host->ep_stream.tx, xferred_bytes); + tu_edpt_stream_write_zlp_if_needed(dev_addr, &p_midi->ep_stream.tx, xferred_bytes); } } diff --git a/src/common/tusb_common.h b/src/common/tusb_common.h index 74cac965d..0351a3d8f 100644 --- a/src/common/tusb_common.h +++ b/src/common/tusb_common.h @@ -120,6 +120,22 @@ TU_ATTR_ALWAYS_INLINE static inline int tu_memcpy_s(void *dest, size_t destsz, c return 0; } +TU_ATTR_ALWAYS_INLINE static inline bool tu_mem_is_zero(const void *buffer, size_t size) { + const uint8_t* buf8 = (const uint8_t*) buffer; + for (size_t i = 0; i < size; i++) { + if (buf8[i] != 0) { return false; } + } + return true; +} + +TU_ATTR_ALWAYS_INLINE static inline bool tu_mem_is_ff(const void *buffer, size_t size) { + const uint8_t* buf8 = (const uint8_t*) buffer; + for (size_t i = 0; i < size; i++) { + if (buf8[i] != 0xff) { return false; } + } + return true; +} + //------------- Bytes -------------// TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_u32(uint8_t b3, uint8_t b2, uint8_t b1, uint8_t b0) { @@ -181,8 +197,7 @@ TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_round_up(uint32_t v, uint32_t f) // log2 of a value is its MSB's position // TODO use clz TODO remove -static inline uint8_t tu_log2(uint32_t value) -{ +TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_log2(uint32_t value) { uint8_t result = 0; while (value >>= 1) { result++; } return result; @@ -193,8 +208,7 @@ static inline uint8_t tu_log2(uint32_t value) // return sizeof(uint32_t) * CHAR_BIT - __builtin_clz(x) - 1; //} -static inline bool tu_is_power_of_two(uint32_t value) -{ +TU_ATTR_ALWAYS_INLINE static inline bool tu_is_power_of_two(uint32_t value) { return (value != 0) && ((value & (value - 1)) == 0); } @@ -205,27 +219,23 @@ static inline bool tu_is_power_of_two(uint32_t value) typedef struct { uint16_t val; } TU_ATTR_PACKED tu_unaligned_uint16_t; typedef struct { uint32_t val; } TU_ATTR_PACKED tu_unaligned_uint32_t; -TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_unaligned_read32(const void* mem) -{ - tu_unaligned_uint32_t const* ua32 = (tu_unaligned_uint32_t const*) mem; +TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_unaligned_read32(const void *mem) { + tu_unaligned_uint32_t const *ua32 = (tu_unaligned_uint32_t const *) mem; return ua32->val; } -TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write32(void* mem, uint32_t value) -{ - tu_unaligned_uint32_t* ua32 = (tu_unaligned_uint32_t*) mem; +TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write32(void *mem, uint32_t value) { + tu_unaligned_uint32_t *ua32 = (tu_unaligned_uint32_t *) mem; ua32->val = value; } -TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_unaligned_read16(const void* mem) -{ - tu_unaligned_uint16_t const* ua16 = (tu_unaligned_uint16_t const*) mem; +TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_unaligned_read16(const void *mem) { + tu_unaligned_uint16_t const *ua16 = (tu_unaligned_uint16_t const *) mem; return ua16->val; } -TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write16(void* mem, uint16_t value) -{ - tu_unaligned_uint16_t* ua16 = (tu_unaligned_uint16_t*) mem; +TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write16(void *mem, uint16_t value) { + tu_unaligned_uint16_t *ua16 = (tu_unaligned_uint16_t *) mem; ua16->val = value; } From ee234a84caf59d23395ed70f5e1c9d772e3f958b Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 6 Mar 2025 11:03:47 +0700 Subject: [PATCH 30/30] hack: force/overwrite endpoint mps to 64 for device that incorrectly report 512 bytes for bulk in fullspeed mode. --- src/class/midi/midi_host.c | 4 ++-- src/common/tusb_private.h | 2 +- src/device/usbd.c | 4 ++-- src/host/usbh.c | 2 +- src/tusb.c | 15 ++++++++++++--- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/class/midi/midi_host.c b/src/class/midi/midi_host.c index 7f7d536d8..03144eb45 100644 --- a/src/class/midi/midi_host.c +++ b/src/class/midi/midi_host.c @@ -267,10 +267,10 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d break; case TUSB_DESC_ENDPOINT: { - tusb_desc_endpoint_t const *p_ep = (tusb_desc_endpoint_t const *) p_desc; + const tusb_desc_endpoint_t *p_ep = (const tusb_desc_endpoint_t *) p_desc; p_desc = tu_desc_next(p_desc); // next to CS endpoint TU_VERIFY(p_desc < p_end && tu_desc_next(p_desc) <= p_end); - midi_desc_cs_endpoint_t const *p_csep = (midi_desc_cs_endpoint_t const *) p_desc; + const midi_desc_cs_endpoint_t *p_csep = (const midi_desc_cs_endpoint_t *) p_desc; TU_LOG_DRV(" Endpoint and CS_Endpoint descriptor %02x\r\n", p_ep->bEndpointAddress); if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_OUT) { diff --git a/src/common/tusb_private.h b/src/common/tusb_private.h index 445882243..31aca8a31 100644 --- a/src/common/tusb_private.h +++ b/src/common/tusb_private.h @@ -67,7 +67,7 @@ typedef struct { //--------------------------------------------------------------------+ // Check if endpoint descriptor is valid per USB specs -bool tu_edpt_validate(tusb_desc_endpoint_t const * desc_ep, tusb_speed_t speed); +bool tu_edpt_validate(tusb_desc_endpoint_t const * desc_ep, tusb_speed_t speed, bool is_host); // Bind all endpoint of a interface descriptor to class driver void tu_edpt_bind_driver(uint8_t ep2drv[][2], tusb_desc_interface_t const* p_desc, uint16_t desc_len, uint8_t driver_id); diff --git a/src/device/usbd.c b/src/device/usbd.c index faf926855..fb5cec49d 100644 --- a/src/device/usbd.c +++ b/src/device/usbd.c @@ -1296,7 +1296,7 @@ bool usbd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const* desc_ep) { rhport = _usbd_rhport; TU_ASSERT(tu_edpt_number(desc_ep->bEndpointAddress) < CFG_TUD_ENDPPOINT_MAX); - TU_ASSERT(tu_edpt_validate(desc_ep, (tusb_speed_t) _usbd_dev.speed)); + TU_ASSERT(tu_edpt_validate(desc_ep, (tusb_speed_t) _usbd_dev.speed, false)); return dcd_edpt_open(rhport, desc_ep); } @@ -1497,7 +1497,7 @@ bool usbd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const* desc_ep) uint8_t const dir = tu_edpt_dir(desc_ep->bEndpointAddress); TU_ASSERT(epnum < CFG_TUD_ENDPPOINT_MAX); - TU_ASSERT(tu_edpt_validate(desc_ep, (tusb_speed_t) _usbd_dev.speed)); + TU_ASSERT(tu_edpt_validate(desc_ep, (tusb_speed_t) _usbd_dev.speed, false)); _usbd_dev.ep_status[epnum][dir].stalled = 0; _usbd_dev.ep_status[epnum][dir].busy = 0; diff --git a/src/host/usbh.c b/src/host/usbh.c index 609fe5000..c87f058cd 100644 --- a/src/host/usbh.c +++ b/src/host/usbh.c @@ -986,7 +986,7 @@ static bool usbh_edpt_control_open(uint8_t dev_addr, uint8_t max_packet_size) { } bool tuh_edpt_open(uint8_t dev_addr, tusb_desc_endpoint_t const* desc_ep) { - TU_ASSERT(tu_edpt_validate(desc_ep, tuh_speed_get(dev_addr))); + TU_ASSERT(tu_edpt_validate(desc_ep, tuh_speed_get(dev_addr), true)); return hcd_edpt_open(usbh_get_rhport(dev_addr), dev_addr, desc_ep); } diff --git a/src/tusb.c b/src/tusb.c index 9303e9bc4..13b89997c 100644 --- a/src/tusb.c +++ b/src/tusb.c @@ -205,7 +205,7 @@ bool tu_edpt_release(tu_edpt_state_t* ep_state, osal_mutex_t mutex) { return ret; } -bool tu_edpt_validate(tusb_desc_endpoint_t const* desc_ep, tusb_speed_t speed) { +bool tu_edpt_validate(tusb_desc_endpoint_t const* desc_ep, tusb_speed_t speed, bool is_host) { uint16_t const max_packet_size = tu_edpt_packet_size(desc_ep); TU_LOG2(" Open EP %02X with Size = %u\r\n", desc_ep->bEndpointAddress, max_packet_size); @@ -221,8 +221,17 @@ bool tu_edpt_validate(tusb_desc_endpoint_t const* desc_ep, tusb_speed_t speed) { // Bulk highspeed must be EXACTLY 512 TU_ASSERT(max_packet_size == 512); } else { - // TODO Bulk fullspeed can only be 8, 16, 32, 64 - TU_ASSERT(max_packet_size <= 64); + // Bulk fullspeed can only be 8, 16, 32, 64 + if (is_host && max_packet_size == 512) { + // HACK: while in host mode, some device incorrectly always report 512 regardless of link speed + // overwrite descriptor to force 64 + TU_LOG1(" WARN: EP max packet size is 512 in fullspeed, force to 64\r\n"); + tusb_desc_endpoint_t* hacked_ep = (tusb_desc_endpoint_t*) (uintptr_t) desc_ep; + hacked_ep->wMaxPacketSize = tu_htole16(64); + } else { + TU_ASSERT(max_packet_size == 8 || max_packet_size == 16 || + max_packet_size == 32 || max_packet_size == 64); + } } break;