Skip to content

Commit

Permalink
- change signature of tuh_midi_mount/umount_cb()
Browse files Browse the repository at this point in the history
- rename midi_stream_t to midi_driver_stream_t and move to midi.h (common for device and host)
  • Loading branch information
hathach committed Feb 14, 2025
1 parent ed88fc9 commit 31a2696
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 59 deletions.
22 changes: 6 additions & 16 deletions examples/host/midi_rx/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
26 changes: 15 additions & 11 deletions src/class/midi/midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -39,7 +34,7 @@
#endif

//--------------------------------------------------------------------+
// Class Specific Descriptor
// Constants
//--------------------------------------------------------------------+

typedef enum
Expand Down Expand Up @@ -111,6 +106,10 @@ enum
MIDI_MAX_DATA_VAL = 0x7F,
};

//--------------------------------------------------------------------+
// Class Specific Descriptor
//--------------------------------------------------------------------+

/// MIDI Interface Header Descriptor
typedef struct TU_ATTR_PACKED
{
Expand Down Expand Up @@ -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

/** @} */
16 changes: 6 additions & 10 deletions src/class/midi/midi_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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 )
Expand Down Expand Up @@ -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) )
Expand Down
24 changes: 8 additions & 16 deletions src/class/midi/midi_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -445,15 +437,15 @@ 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)) {
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;
midi_driver_stream_t streamrt;
streamrt.buffer[0] = MIDI_CIN_SYSEX_END_1BYTE;
streamrt.buffer[1] = data;
streamrt.index = 2;
Expand Down Expand Up @@ -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;
}

Expand Down
8 changes: 2 additions & 6 deletions src/class/midi/midi_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 31a2696

Please sign in to comment.