- 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)
This commit is contained in:
hathach
2025-02-14 11:09:50 +07:00
parent ed88fc983f
commit 31a2696de7
5 changed files with 37 additions and 59 deletions

View File

@@ -101,30 +101,20 @@ void midi_host_rx_task(void) {
// TinyUSB Callbacks // TinyUSB Callbacks
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Invoked when device with hid interface is mounted // Invoked when device with MIDI interface is mounted.
// Report descriptor is also available for use. tuh_hid_parse_report_descriptor() void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t num_cables_rx, uint16_t num_cables_tx) {
// 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_rx;
(void) num_cables_tx; (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; 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 // 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) dev_addr;
(void) instance;
TU_LOG1("MIDI device address = %d, instance = %d is unmounted\r\n", dev_addr, instance);
midi_dev_addr = 0; 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) { void tuh_midi_rx_cb(uint8_t dev_addr, uint32_t num_packets) {

View File

@@ -24,13 +24,8 @@
* This file is part of the TinyUSB stack. * This file is part of the TinyUSB stack.
*/ */
/** \ingroup group_class #ifndef TUSB_MIDI_H_
* \defgroup ClassDriver_CDC Communication Device Class (CDC) #define TUSB_MIDI_H_
* Currently only Abstract Control Model subclass is supported
* @{ */
#ifndef _TUSB_MIDI_H__
#define _TUSB_MIDI_H__
#include "common/tusb_common.h" #include "common/tusb_common.h"
@@ -39,7 +34,7 @@
#endif #endif
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Class Specific Descriptor // Constants
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
typedef enum typedef enum
@@ -111,6 +106,10 @@ enum
MIDI_MAX_DATA_VAL = 0x7F, MIDI_MAX_DATA_VAL = 0x7F,
}; };
//--------------------------------------------------------------------+
// Class Specific Descriptor
//--------------------------------------------------------------------+
/// MIDI Interface Header Descriptor /// MIDI Interface Header Descriptor
typedef struct TU_ATTR_PACKED typedef struct TU_ATTR_PACKED
{ {
@@ -216,12 +215,17 @@ typedef struct
uint8_t baAssocJackID[]; ; ///< A list of associated jacks uint8_t baAssocJackID[]; ; ///< A list of associated jacks
} midi_cs_desc_endpoint_t; } 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 #ifdef __cplusplus
} }
#endif #endif
#endif #endif
/** @} */

View File

@@ -40,11 +40,7 @@
// MACRO CONSTANT TYPEDEF // MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
typedef struct {
uint8_t buffer[4];
uint8_t index;
uint8_t total;
} midid_stream_t;
typedef struct { typedef struct {
uint8_t itf_num; uint8_t itf_num;
@@ -54,8 +50,8 @@ typedef struct {
// For Stream read()/write() API // For Stream read()/write() API
// Messages are always 4 bytes long, queue them for reading and writing so the // 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. // callers can use the Stream interface with single-byte read/write calls.
midid_stream_t stream_write; midi_driver_stream_t stream_write;
midid_stream_t stream_read; midi_driver_stream_t stream_read;
/*------------- From this point, data is not cleared by bus reset -------------*/ /*------------- From this point, data is not cleared by bus reset -------------*/
// FIFO // FIFO
@@ -122,7 +118,7 @@ uint32_t tud_midi_n_available(uint8_t itf, uint8_t cable_num)
(void) cable_num; (void) cable_num;
midid_interface_t* midi = &_midid_itf[itf]; 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 // when using with packet API stream total & index are both zero
return tu_fifo_count(&midi->rx_ff) + (uint8_t) (stream->total - stream->index); 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; uint8_t* buf8 = (uint8_t*) buffer;
midid_interface_t* midi = &_midid_itf[itf]; 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; uint32_t total_read = 0;
while( bufsize ) 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]; midid_interface_t* midi = &_midid_itf[itf];
TU_VERIFY(midi->ep_in, 0); 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; uint32_t i = 0;
while ( (i < bufsize) && (tu_fifo_remaining(&midi->tx_ff) >= 4) ) while ( (i < bufsize) && (tu_fifo_remaining(&midi->tx_ff) >= 4) )

View File

@@ -37,14 +37,6 @@
// MACRO CONSTANT TYPEDEF // 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 { typedef struct {
uint8_t dev_addr; uint8_t dev_addr;
uint8_t itf_num; uint8_t itf_num;
@@ -58,8 +50,8 @@ typedef struct {
// For Stream read()/write() API // For Stream read()/write() API
// Messages are always 4 bytes long, queue them for reading and writing so the // 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. // callers can use the Stream interface with single-byte read/write calls.
midi_stream_t stream_write; midi_driver_stream_t stream_write;
midi_stream_t stream_read; midi_driver_stream_t stream_read;
/*------------- From this point, data is not cleared by bus reset -------------*/ /*------------- From this point, data is not cleared by bus reset -------------*/
// Endpoint stream // Endpoint stream
@@ -152,7 +144,7 @@ void midih_close(uint8_t dev_addr) {
return; return;
} }
if (tuh_midi_umount_cb) { 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_in = 0;
p_midi_host->ep_out = 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; p_midi_host->configured = true;
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); 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 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); midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
TU_VERIFY(p_midi_host != NULL); TU_VERIFY(p_midi_host != NULL);
TU_VERIFY(cable_num < p_midi_host->num_cables_tx); 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; uint32_t i = 0;
while ((i < bufsize) && (tu_edpt_stream_write_available(dev_addr, &p_midi_host->ep_stream.tx) >= 4)) { 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++; i++;
if (data >= MIDI_STATUS_SYSREAL_TIMING_CLOCK) { if (data >= MIDI_STATUS_SYSREAL_TIMING_CLOCK) {
// real-time messages need to be sent right away // 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[0] = MIDI_CIN_SYSEX_END_1BYTE;
streamrt.buffer[1] = data; streamrt.buffer[1] = data;
streamrt.index = 2; 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) { uint8_t tuh_midi_get_num_tx_cables (uint8_t dev_addr) {
midih_interface_t *p_midi_host = find_midi_by_daddr(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 != 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; return p_midi_host->num_cables_tx;
} }
uint8_t tuh_midi_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); midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
TU_VERIFY(p_midi_host != NULL, 0); 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; return p_midi_host->num_cables_rx;
} }

View File

@@ -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. // Invoked when device with MIDI interface is mounted.
// If the MIDI host application requires MIDI IN, it should request an TU_ATTR_WEAK void tuh_midi_mount_cb(uint8_t dev_addr, uint8_t num_cables_rx, uint16_t num_cables_tx);
// 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 // 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);
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_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_tx_cb(uint8_t dev_addr);