Skip to content

Commit 4bcbfaf

Browse files
committed
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
1 parent 56e84bd commit 4bcbfaf

File tree

8 files changed

+71
-76
lines changed

8 files changed

+71
-76
lines changed

examples/host/cdc_msc_hid/src/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ int main(void) {
8282

8383
void tuh_mount_cb(uint8_t dev_addr) {
8484
// application set-up
85-
printf("A device with address %d is mounted\r\n", dev_addr);
85+
printf("A device with address %u is mounted\r\n", dev_addr);
8686
}
8787

8888
void tuh_umount_cb(uint8_t dev_addr) {
8989
// application tear-down
90-
printf("A device with address %d is unmounted \r\n", dev_addr);
90+
printf("A device with address %u is unmounted \r\n", dev_addr);
9191
}
9292

9393

examples/host/midi_rx/src/main.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,18 @@ void midi_host_rx_task(void) {
9191
//--------------------------------------------------------------------+
9292

9393
// Invoked when device with MIDI interface is mounted.
94-
void tuh_midi_mount_cb(uint8_t idx, uint8_t num_cables_rx, uint16_t num_cables_tx) {
95-
printf("MIDI Interface Index = %u, Number of RX cables = %u, Number of TX cables = %u\r\n",
96-
idx, num_cables_rx, num_cables_tx);
94+
void tuh_midi_mount_cb(uint8_t idx, const tuh_midi_mount_cb_t* mount_cb_data) {
95+
printf("MIDI Interface Index = %u, Address = %u, Number of RX cables = %u, Number of TX cables = %u\r\n",
96+
idx, mount_cb_data->daddr, mount_cb_data->rx_cable_count, mount_cb_data->tx_cable_count);
9797
}
9898

9999
// Invoked when device with hid interface is un-mounted
100100
void tuh_midi_umount_cb(uint8_t idx) {
101101
printf("MIDI Interface Index = %u is unmounted\r\n", idx);
102102
}
103103

104-
void tuh_midi_rx_cb(uint8_t idx, uint32_t num_packets) {
105-
if (num_packets == 0) {
104+
void tuh_midi_rx_cb(uint8_t idx, uint32_t xferred_bytes) {
105+
if (xferred_bytes == 0) {
106106
return;
107107
}
108108

@@ -117,6 +117,7 @@ void tuh_midi_rx_cb(uint8_t idx, uint32_t num_packets) {
117117
printf("\r\n");
118118
}
119119

120-
void tuh_midi_tx_cb(uint8_t idx) {
120+
void tuh_midi_tx_cb(uint8_t idx, uint32_t xferred_bytes) {
121121
(void) idx;
122+
(void) xferred_bytes;
122123
}

examples/host/midi_rx/src/tusb_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ extern "C" {
7575
#define CFG_TUH_ENABLED 1
7676

7777
#if CFG_TUSB_MCU == OPT_MCU_RP2040
78-
// #define CFG_TUH_RPI_PIO_USB 1 // use pio-usb as host controller
78+
#define CFG_TUH_RPI_PIO_USB 1 // use pio-usb as host controller
7979
// #define CFG_TUH_MAX3421 1 // use max3421 as host controller
8080

8181
// host roothub port is 1 if using either pio-usb or max3421

src/class/cdc/cdc_host.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@
4949

5050
// RX FIFO size
5151
#ifndef CFG_TUH_CDC_RX_BUFSIZE
52-
#define CFG_TUH_CDC_RX_BUFSIZE USBH_EPSIZE_BULK_MAX
52+
#define CFG_TUH_CDC_RX_BUFSIZE TUH_EPSIZE_BULK_MPS
5353
#endif
5454

5555
// RX Endpoint size
5656
#ifndef CFG_TUH_CDC_RX_EPSIZE
57-
#define CFG_TUH_CDC_RX_EPSIZE USBH_EPSIZE_BULK_MAX
57+
#define CFG_TUH_CDC_RX_EPSIZE TUH_EPSIZE_BULK_MPS
5858
#endif
5959

6060
// TX FIFO size
6161
#ifndef CFG_TUH_CDC_TX_BUFSIZE
62-
#define CFG_TUH_CDC_TX_BUFSIZE USBH_EPSIZE_BULK_MAX
62+
#define CFG_TUH_CDC_TX_BUFSIZE TUH_EPSIZE_BULK_MPS
6363
#endif
6464

6565
// TX Endpoint size
6666
#ifndef CFG_TUH_CDC_TX_EPSIZE
67-
#define CFG_TUH_CDC_TX_EPSIZE USBH_EPSIZE_BULK_MAX
67+
#define CFG_TUH_CDC_TX_EPSIZE TUH_EPSIZE_BULK_MPS
6868
#endif
6969

7070
//--------------------------------------------------------------------+

src/class/midi/midi_device.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
//--------------------------------------------------------------------+
4040
// MACRO CONSTANT TYPEDEF
4141
//--------------------------------------------------------------------+
42-
43-
44-
4542
typedef struct {
4643
uint8_t itf_num;
4744
uint8_t ep_in;
@@ -440,8 +437,6 @@ uint16_t midid_open(uint8_t rhport, const tusb_desc_interface_t* desc_itf, uint1
440437
drv_len += tu_desc_len(p_desc);
441438
p_desc = tu_desc_next(p_desc);
442439
}
443-
} else {
444-
TU_LOG2("Warning: MIDI Device has no Audio Control Interface");
445440
}
446441

447442
// 2nd Interface is MIDI Streaming

src/class/midi/midi_host.c

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@
4040

4141
#define TU_LOG_DRV(...) TU_LOG(CFG_TUH_MIDI_LOG_LEVEL, __VA_ARGS__)
4242

43+
//--------------------------------------------------------------------+
44+
// Weak stubs: invoked if no strong implementation is available
45+
//--------------------------------------------------------------------+
46+
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; }
47+
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; }
48+
TU_ATTR_WEAK void tuh_midi_umount_cb(uint8_t idx) { (void) idx; }
49+
TU_ATTR_WEAK void tuh_midi_rx_cb(uint8_t idx, uint32_t xferred_bytes) { (void) idx; (void) xferred_bytes; }
50+
TU_ATTR_WEAK void tuh_midi_tx_cb(uint8_t idx, uint32_t xferred_bytes) { (void) idx; (void) xferred_bytes; }
51+
4352
//--------------------------------------------------------------------+
4453
// MACRO CONSTANT TYPEDEF
4554
//--------------------------------------------------------------------+
@@ -52,8 +61,8 @@ typedef struct {
5261
uint8_t ep_in; // IN endpoint address
5362
uint8_t ep_out; // OUT endpoint address
5463

55-
uint8_t num_cables_rx; // IN endpoint CS descriptor bNumEmbMIDIJack value
56-
uint8_t num_cables_tx; // OUT endpoint CS descriptor bNumEmbMIDIJack value
64+
uint8_t rx_cable_count; // IN endpoint CS descriptor bNumEmbMIDIJack value
65+
uint8_t tx_cable_count; // OUT endpoint CS descriptor bNumEmbMIDIJack value
5766

5867
#if CFG_TUH_MIDI_STREAM_API
5968
// For Stream read()/write() API
@@ -135,16 +144,13 @@ void midih_close(uint8_t daddr) {
135144
midih_interface_t* p_midi = &_midi_host[idx];
136145
if (p_midi->daddr == daddr) {
137146
TU_LOG_DRV(" MIDI close addr = %u index = %u\r\n", daddr, idx);
138-
139-
if (tuh_midi_umount_cb) {
140-
tuh_midi_umount_cb(idx);
141-
}
147+
tuh_midi_umount_cb(idx);
142148

143149
p_midi->ep_in = 0;
144150
p_midi->ep_out = 0;
145151
p_midi->bInterfaceNumber = 0;
146-
p_midi->num_cables_rx = 0;
147-
p_midi->num_cables_tx = 0;
152+
p_midi->rx_cable_count = 0;
153+
p_midi->tx_cable_count = 0;
148154
p_midi->daddr = 0;
149155
p_midi->mounted = false;
150156
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
163169
midih_interface_t *p_midi_host = &_midi_host[idx];
164170

165171
if (ep_addr == p_midi_host->ep_stream.rx.ep_addr) {
166-
// receive new data if available
172+
// receive new data, put it into FIFO and invoke callback if available
167173
if (xferred_bytes) {
168-
// put in the RX FIFO only non-zero MIDI IN 4-byte packets
169-
uint32_t packets_queued = 0;
170-
uint8_t *buf = _midi_epbuf->rx;
171-
const uint32_t npackets = xferred_bytes / 4;
172-
for (uint32_t p = 0; p < npackets; p++) {
173-
// some devices send back all zero packets even if there is no data ready
174-
const uint32_t packet = tu_unaligned_read32(buf);
175-
if (packet != 0) {
176-
tu_edpt_stream_read_xfer_complete_with_buf(&p_midi_host->ep_stream.rx, buf, 4);
177-
++packets_queued;
178-
TU_LOG3("MIDI RX=%08x\r\n", packet);
179-
}
180-
buf += 4;
181-
}
182-
183-
if (tuh_midi_rx_cb) {
184-
tuh_midi_rx_cb(idx, packets_queued);
185-
}
174+
tu_edpt_stream_read_xfer_complete(&p_midi_host->ep_stream.rx, xferred_bytes);
175+
tuh_midi_rx_cb(idx, xferred_bytes);
186176
}
187177

188178
tu_edpt_stream_read_xfer(dev_addr, &p_midi_host->ep_stream.rx); // prepare for next transfer
189179
} else if (ep_addr == p_midi_host->ep_stream.tx.ep_addr) {
190-
if (tuh_midi_tx_cb) {
191-
tuh_midi_tx_cb(idx);
192-
}
180+
tuh_midi_tx_cb(idx, xferred_bytes);
181+
193182
if (0 == tu_edpt_stream_write_xfer(dev_addr, &p_midi_host->ep_stream.tx)) {
194183
// If there is no data left, a ZLP should be sent if
195184
// 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
207196
(void) rhport;
208197

209198
TU_VERIFY(TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass);
210-
// const uint8_t* p_start = ((uint8_t const*) desc_itf);
211199
const uint8_t *p_end = ((const uint8_t *) desc_itf) + max_len;
212200
const uint8_t *p_desc = (const uint8_t *) desc_itf;
213201

@@ -286,14 +274,14 @@ bool midih_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *d
286274
TU_LOG_DRV(" Endpoint and CS_Endpoint descriptor %02x\r\n", p_ep->bEndpointAddress);
287275
if (tu_edpt_dir(p_ep->bEndpointAddress) == TUSB_DIR_OUT) {
288276
p_midi->ep_out = p_ep->bEndpointAddress;
289-
p_midi->num_cables_tx = p_csep->bNumEmbMIDIJack;
277+
p_midi->tx_cable_count = p_csep->bNumEmbMIDIJack;
290278
desc_cb.desc_epout = p_ep;
291279

292280
TU_ASSERT(tuh_edpt_open(dev_addr, p_ep));
293281
tu_edpt_stream_open(&p_midi->ep_stream.tx, p_ep);
294282
} else {
295283
p_midi->ep_in = p_ep->bEndpointAddress;
296-
p_midi->num_cables_rx = p_csep->bNumEmbMIDIJack;
284+
p_midi->rx_cable_count = p_csep->bNumEmbMIDIJack;
297285
desc_cb.desc_epin = p_ep;
298286

299287
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
309297
desc_cb.desc_midi_total_len = (uint16_t) ((uintptr_t)p_desc - (uintptr_t) desc_itf);
310298

311299
p_midi->daddr = dev_addr;
312-
313-
if (tuh_midi_descriptor_cb) {
314-
tuh_midi_descriptor_cb(idx, &desc_cb);
315-
}
300+
tuh_midi_descriptor_cb(idx, &desc_cb);
316301

317302
return true;
318303
}
@@ -323,9 +308,13 @@ bool midih_set_config(uint8_t dev_addr, uint8_t itf_num) {
323308
midih_interface_t *p_midi = &_midi_host[idx];
324309
p_midi->mounted = true;
325310

326-
if (tuh_midi_mount_cb) {
327-
tuh_midi_mount_cb(idx, p_midi->num_cables_rx, p_midi->num_cables_tx);
328-
}
311+
const tuh_midi_mount_cb_t mount_cb_data = {
312+
.daddr = dev_addr,
313+
.bInterfaceNumber = itf_num,
314+
.rx_cable_count = p_midi->rx_cable_count,
315+
.tx_cable_count = p_midi->tx_cable_count,
316+
};
317+
tuh_midi_mount_cb(idx, &mount_cb_data);
329318

330319
tu_edpt_stream_read_xfer(dev_addr, &p_midi->ep_stream.rx); // prepare for incoming data
331320

@@ -355,18 +344,18 @@ uint8_t tuh_midi_itf_get_index(uint8_t daddr, uint8_t itf_num) {
355344
return TUSB_INDEX_INVALID_8;
356345
}
357346

358-
uint8_t tuh_midi_get_num_tx_cables (uint8_t idx) {
347+
uint8_t tuh_midi_get_tx_cable_count (uint8_t idx) {
359348
TU_VERIFY(idx < CFG_TUH_MIDI);
360349
midih_interface_t *p_midi = &_midi_host[idx];
361350
TU_VERIFY(p_midi->ep_stream.tx.ep_addr != 0, 0);
362-
return p_midi->num_cables_tx;
351+
return p_midi->tx_cable_count;
363352
}
364353

365-
uint8_t tuh_midi_get_num_rx_cables (uint8_t idx) {
354+
uint8_t tuh_midi_get_rx_cable_count (uint8_t idx) {
366355
TU_VERIFY(idx < CFG_TUH_MIDI);
367356
midih_interface_t *p_midi = &_midi_host[idx];
368357
TU_VERIFY(p_midi->ep_stream.rx.ep_addr != 0, 0);
369-
return p_midi->num_cables_rx;
358+
return p_midi->rx_cable_count;
370359
}
371360

372361
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
410399
uint32_t tuh_midi_stream_write(uint8_t idx, uint8_t cable_num, uint8_t const *buffer, uint32_t bufsize) {
411400
TU_VERIFY(idx < CFG_TUH_MIDI && buffer && bufsize > 0);
412401
midih_interface_t *p_midi = &_midi_host[idx];
413-
TU_VERIFY(cable_num < p_midi->num_cables_tx);
402+
TU_VERIFY(cable_num < p_midi->tx_cable_count);
414403
midi_driver_stream_t *stream = &p_midi->stream_write;
415404

416405
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
520509
while (nread == 4 && bytes_buffered < bufsize) {
521510
*p_cable_num = (p_midi->stream_read.buffer[0] >> 4) & 0x0f;
522511
uint8_t bytes_to_add_to_stream = 0;
523-
if (*p_cable_num < p_midi->num_cables_rx) {
512+
if (*p_cable_num < p_midi->rx_cable_count) {
524513
// ignore the CIN field; too many devices out there encode this wrong
525514
uint8_t status = p_midi->stream_read.buffer[1];
526515
uint16_t cable_mask = (uint16_t) (1 << *p_cable_num);

src/class/midi/midi_host.h

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
#endif
6161

6262
//--------------------------------------------------------------------+
63-
// Application API
63+
// Application Types
6464
//--------------------------------------------------------------------+
6565
typedef struct {
6666
const tusb_desc_interface_t* desc_audio_control;
@@ -76,6 +76,17 @@ typedef struct {
7676
const uint8_t* desc_jack[16]; // list of jack descriptors (embedded + external)
7777
} tuh_midi_descriptor_cb_t;
7878

79+
typedef struct {
80+
uint8_t daddr;
81+
uint8_t bInterfaceNumber; // interface number of MIDI streaming
82+
uint8_t rx_cable_count;
83+
uint8_t tx_cable_count;
84+
} tuh_midi_mount_cb_t;
85+
86+
//--------------------------------------------------------------------+
87+
// Application API
88+
//--------------------------------------------------------------------+
89+
7990
// Check if MIDI interface is mounted
8091
bool tuh_midi_mounted(uint8_t idx);
8192

@@ -84,10 +95,10 @@ bool tuh_midi_mounted(uint8_t idx);
8495
uint8_t tuh_midi_itf_get_index(uint8_t daddr, uint8_t itf_num);
8596

8697
// return the number of virtual midi cables on the device's IN endpoint
87-
uint8_t tuh_midi_get_num_rx_cables(uint8_t idx);
98+
uint8_t tuh_midi_get_rx_cable_count(uint8_t idx);
8899

89100
// return the number of virtual midi cables on the device's OUT endpoint
90-
uint8_t tuh_midi_get_num_tx_cables(uint8_t idx);
101+
uint8_t tuh_midi_get_tx_cable_count(uint8_t idx);
91102

92103
// return the raw number of bytes available.
93104
// Note: this is related but not the same as number of stream bytes available.
@@ -151,16 +162,19 @@ uint32_t tuh_midi_stream_read(uint8_t idx, uint8_t *p_cable_num, uint8_t *p_buff
151162

152163
// Invoked when MIDI interface is detected in enumeration. Application can copy/parse descriptor if needed.
153164
// Note: may be fired before tuh_midi_mount_cb(), therefore midi interface is not mounted/ready.
154-
TU_ATTR_WEAK void tuh_midi_descriptor_cb(uint8_t idx, const tuh_midi_descriptor_cb_t * desc_cb);
165+
void tuh_midi_descriptor_cb(uint8_t idx, const tuh_midi_descriptor_cb_t * desc_cb_data);
155166

156167
// Invoked when device with MIDI interface is mounted.
157-
TU_ATTR_WEAK void tuh_midi_mount_cb(uint8_t idx, uint8_t num_cables_rx, uint16_t num_cables_tx);
168+
void tuh_midi_mount_cb(uint8_t idx, const tuh_midi_mount_cb_t* mount_cb_data);
158169

159170
// Invoked when device with MIDI interface is un-mounted
160-
TU_ATTR_WEAK void tuh_midi_umount_cb(uint8_t idx);
171+
void tuh_midi_umount_cb(uint8_t idx);
172+
173+
// Invoked when received new data
174+
void tuh_midi_rx_cb(uint8_t idx, uint32_t xferred_bytes);
161175

162-
TU_ATTR_WEAK void tuh_midi_rx_cb(uint8_t idx, uint32_t num_packets);
163-
TU_ATTR_WEAK void tuh_midi_tx_cb(uint8_t idx);
176+
// Invoked when a TX is complete and therefore space becomes available in TX buffer
177+
void tuh_midi_tx_cb(uint8_t idx, uint32_t xferred_bytes);
164178

165179
//--------------------------------------------------------------------+
166180
// Internal Class Driver API

src/host/usbh_pvt.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@
4141
#define TU_LOG_INT_USBH(...) TU_LOG_INT(CFG_TUH_LOG_LEVEL, __VA_ARGS__)
4242
#define TU_LOG_HEX_USBH(...) TU_LOG_HEX(CFG_TUH_LOG_LEVEL, __VA_ARGS__)
4343

44-
enum {
45-
USBH_EPSIZE_BULK_MAX = (TUH_OPT_HIGH_SPEED ? TUSB_EPSIZE_BULK_HS : TUSB_EPSIZE_BULK_FS)
46-
};
47-
4844
//--------------------------------------------------------------------+
4945
// Class Driver API
5046
//--------------------------------------------------------------------+

0 commit comments

Comments
 (0)