From 31a2696de77d4642d28086ccbe476f40b1bdea7d Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 14 Feb 2025 11:09:50 +0700 Subject: [PATCH] - 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 0b07ba6e79..fed782947d 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 68a784c601..9591f13c49 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 dd1883e37b..7b67056406 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 7d6eb35fa9..b6ebaa7a93 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 6529f2bbc4..ab6c43908b 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);