Skip to content

Commit ed88fc9

Browse files
committed
- 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()
1 parent bad6cbe commit ed88fc9

File tree

3 files changed

+49
-72
lines changed

3 files changed

+49
-72
lines changed

examples/host/midi_rx/src/main.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
static uint8_t midi_dev_addr = 0;
3939

4040
//--------------------------------------------------------------------+
41-
// MACRO CONSTANT TYPEDEF PROTYPES
41+
// MACRO CONSTANT TYPEDEF PROTOTYPES
4242
//--------------------------------------------------------------------+
4343
void led_blinking_task(void);
4444
void midi_host_rx_task(void);
@@ -92,10 +92,9 @@ void midi_host_rx_task(void) {
9292
if (!midi_dev_addr || !tuh_midi_configured(midi_dev_addr)) {
9393
return;
9494
}
95-
if (tuh_midih_get_num_rx_cables(midi_dev_addr) < 1) {
95+
if (tuh_midi_get_num_rx_cables(midi_dev_addr) < 1) {
9696
return;
9797
}
98-
tuh_midi_read_poll(midi_dev_addr);
9998
}
10099

101100
//--------------------------------------------------------------------+

src/class/midi/midi_host.c

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
#include "tusb_option.h"
2828

29-
#if (TUSB_OPT_HOST_ENABLED && CFG_TUH_MIDI)
29+
#if (CFG_TUH_ENABLED && CFG_TUH_MIDI)
3030

3131
#include "host/usbh.h"
3232
#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
195195
}
196196
}
197197

198-
// prepare for next transfer if needed
199-
tu_edpt_stream_read_xfer(dev_addr, &p_midi_host->ep_stream.rx);
198+
tu_edpt_stream_read_xfer(dev_addr, &p_midi_host->ep_stream.rx); // prepare for next transfer
200199
} else if (ep_addr == p_midi_host->ep_stream.tx.ep_addr) {
201200
if (tuh_midi_tx_cb) {
202201
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
416415
return true;
417416
}
418417

419-
bool tuh_midi_configured(uint8_t dev_addr) {
420-
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
421-
TU_VERIFY(p_midi_host != NULL);
422-
return p_midi_host->configured;
423-
}
424-
425418
bool midih_set_config(uint8_t dev_addr, uint8_t itf_num) {
426419
(void) itf_num;
427420
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) {
432425
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);
433426
}
434427

428+
tu_edpt_stream_read_xfer(dev_addr, &p_midi_host->ep_stream.rx); // prepare for incoming data
429+
435430
// No special config things to do for MIDI
436431
usbh_driver_set_config_complete(dev_addr, p_midi_host->itf_num);
437432
return true;
438433
}
439434

440435
//--------------------------------------------------------------------+
441-
// Stream API
436+
// API
442437
//--------------------------------------------------------------------+
443-
bool tuh_midi_read_poll(uint8_t dev_addr) {
438+
bool tuh_midi_configured(uint8_t dev_addr) {
444439
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
445440
TU_VERIFY(p_midi_host != NULL);
446-
return tu_edpt_stream_read_xfer(dev_addr, &p_midi_host->ep_stream.rx) > 0;
441+
return p_midi_host->configured;
447442
}
448443

449444
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) {
557552
//--------------------------------------------------------------------+
558553
// Helper
559554
//--------------------------------------------------------------------+
560-
uint8_t tuh_midih_get_num_tx_cables (uint8_t dev_addr)
561-
{
555+
uint8_t tuh_midi_get_num_tx_cables (uint8_t dev_addr) {
562556
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
563-
TU_VERIFY(p_midi_host != NULL);
564-
TU_VERIFY(p_midi_host->ep_out != 0); // returns 0 if fails
557+
TU_VERIFY(p_midi_host != NULL, 0);
558+
TU_VERIFY(p_midi_host->ep_out != 0, 0);
565559
return p_midi_host->num_cables_tx;
566560
}
567561

568-
uint8_t tuh_midih_get_num_rx_cables (uint8_t dev_addr)
569-
{
562+
uint8_t tuh_midi_get_num_rx_cables (uint8_t dev_addr) {
570563
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
571-
TU_VERIFY(p_midi_host != NULL);
572-
TU_VERIFY(p_midi_host->ep_in != 0); // returns 0 if fails
564+
TU_VERIFY(p_midi_host != NULL, 0);
565+
TU_VERIFY(p_midi_host->ep_in != 0, 0);
573566
return p_midi_host->num_cables_rx;
574567
}
575568

576-
bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4])
577-
{
569+
uint32_t tuh_midi_read_available(uint8_t dev_addr) {
570+
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
571+
TU_VERIFY(p_midi_host != NULL);
572+
return tu_edpt_stream_read_available(&p_midi_host->ep_stream.rx);
573+
}
574+
575+
bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]) {
578576
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
579577
TU_VERIFY(p_midi_host != NULL);
580578
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
661659
bytes_to_add_to_stream = 1;
662660
}
663661
}
662+
664663
for (uint8_t idx = 1; idx <= bytes_to_add_to_stream; idx++) {
665664
*p_buffer++ = p_midi_host->stream_read.buffer[idx];
666665
}
@@ -678,30 +677,6 @@ uint32_t tuh_midi_stream_read(uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p
678677
return bytes_buffered;
679678
}
680679

681-
uint8_t tuh_midi_get_num_rx_cables(uint8_t dev_addr)
682-
{
683-
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
684-
TU_VERIFY(p_midi_host != NULL);
685-
uint8_t num_cables = 0;
686-
if (p_midi_host)
687-
{
688-
num_cables = p_midi_host->num_cables_rx;
689-
}
690-
return num_cables;
691-
}
692-
693-
uint8_t tuh_midi_get_num_tx_cables(uint8_t dev_addr)
694-
{
695-
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
696-
TU_VERIFY(p_midi_host != NULL);
697-
uint8_t num_cables = 0;
698-
if (p_midi_host)
699-
{
700-
num_cables = p_midi_host->num_cables_tx;
701-
}
702-
return num_cables;
703-
}
704-
705680
#if CFG_MIDI_HOST_DEVSTRINGS
706681
static uint8_t find_string_index(midih_interface_t *ptr, uint8_t jack_id)
707682
{

src/class/midi/midi_host.h

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,34 @@
5858
#endif
5959

6060
//--------------------------------------------------------------------+
61-
// Application API (Single Interface)
61+
// Application API
6262
//--------------------------------------------------------------------+
6363
bool tuh_midi_configured (uint8_t dev_addr);
6464

65+
// return the number of virtual midi cables on the device's IN endpoint
66+
uint8_t tuh_midi_get_num_rx_cables(uint8_t dev_addr);
67+
6568
// return the number of virtual midi cables on the device's OUT endpoint
66-
uint8_t tuh_midih_get_num_tx_cables (uint8_t dev_addr);
69+
uint8_t tuh_midi_get_num_tx_cables(uint8_t dev_addr);
6770

68-
// return the number of virtual midi cables on the device's IN endpoint
69-
uint8_t tuh_midih_get_num_rx_cables (uint8_t dev_addr);
71+
#if CFG_MIDI_HOST_DEVSTRINGS
72+
uint8_t tuh_midi_get_rx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings);
73+
uint8_t tuh_midi_get_tx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings);
74+
uint8_t tuh_midi_get_all_istrings(uint8_t dev_addr, const uint8_t** istrings);
75+
#endif
76+
77+
// return the raw number of bytes available from device endpoint.
78+
// Note: this is related but not the same as number of stream bytes available.
79+
uint32_t tuh_midi_read_available(uint8_t dev_addr);
80+
81+
//--------------------------------------------------------------------+
82+
// Packet API
83+
//--------------------------------------------------------------------+
7084

71-
// request available data from the device. tuh_midi_message_received_cb() will
72-
// be called if the device has any data to send. Otherwise, the device will
73-
// respond NAK. This function blocks until the transfer completes or the
74-
// devices sends NAK.
75-
// This function will return false if the hardware is busy.
76-
bool tuh_midi_read_poll( uint8_t dev_addr );
85+
// Read a raw MIDI packet from the connected device
86+
// This function does not parse the packet format
87+
// Return true if a packet was returned
88+
bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]);
7789

7890
// Queue a packet to the device. The application
7991
// must call tuh_midi_stream_flush to actually have the
@@ -82,6 +94,10 @@ bool tuh_midi_read_poll( uint8_t dev_addr );
8294
// Returns true if the packet was successfully queued.
8395
bool tuh_midi_packet_write (uint8_t dev_addr, uint8_t const packet[4]);
8496

97+
//--------------------------------------------------------------------+
98+
// Stream API
99+
//--------------------------------------------------------------------+
100+
85101
// Queue a message to the device. The application
86102
// must call tuh_midi_stream_flush to actually have the
87103
// data go out.
@@ -101,19 +117,6 @@ uint32_t tuh_midi_stream_flush( uint8_t dev_addr);
101117
// it properly.
102118
uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p_buffer, uint16_t bufsize);
103119

104-
// Read a raw MIDI packet from the connected device
105-
// This function does not parse the packet format
106-
// Return true if a packet was returned
107-
bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]);
108-
109-
uint8_t tuh_midi_get_num_rx_cables(uint8_t dev_addr);
110-
uint8_t tuh_midi_get_num_tx_cables(uint8_t dev_addr);
111-
#if CFG_MIDI_HOST_DEVSTRINGS
112-
uint8_t tuh_midi_get_rx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings);
113-
uint8_t tuh_midi_get_tx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings);
114-
uint8_t tuh_midi_get_all_istrings(uint8_t dev_addr, const uint8_t** istrings);
115-
#endif
116-
117120
//--------------------------------------------------------------------+
118121
// Callbacks (Weak is optional)
119122
//--------------------------------------------------------------------+

0 commit comments

Comments
 (0)