Skip to content

Commit 997771f

Browse files
committed
- rename tuh_midi_stream_flush() to tuh_midi_write_flush()
- add tuh_midi_packet_read_n() and tuh_midi_packet_write_n() - add CFG_TUH_MIDI_STREAM_API to opt out stream API
1 parent 31a2696 commit 997771f

File tree

2 files changed

+97
-64
lines changed

2 files changed

+97
-64
lines changed

src/class/midi/midi_host.c

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@ typedef struct {
4747
uint8_t num_cables_rx; // IN endpoint CS descriptor bNumEmbMIDIJack value
4848
uint8_t num_cables_tx; // OUT endpoint CS descriptor bNumEmbMIDIJack value
4949

50+
#if CFG_TUH_MIDI_STREAM_API
5051
// For Stream read()/write() API
5152
// Messages are always 4 bytes long, queue them for reading and writing so the
5253
// callers can use the Stream interface with single-byte read/write calls.
5354
midi_driver_stream_t stream_write;
5455
midi_driver_stream_t stream_read;
56+
#endif
5557

56-
/*------------- From this point, data is not cleared by bus reset -------------*/
5758
// Endpoint stream
5859
struct {
5960
tu_edpt_stream_t tx;
@@ -433,6 +434,57 @@ bool tuh_midi_configured(uint8_t dev_addr) {
433434
return p_midi_host->configured;
434435
}
435436

437+
uint8_t tuh_midi_get_num_tx_cables (uint8_t dev_addr) {
438+
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
439+
TU_VERIFY(p_midi_host != NULL, 0);
440+
TU_VERIFY(p_midi_host->ep_stream.tx.ep_addr != 0, 0);
441+
return p_midi_host->num_cables_tx;
442+
}
443+
444+
uint8_t tuh_midi_get_num_rx_cables (uint8_t dev_addr) {
445+
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
446+
TU_VERIFY(p_midi_host != NULL, 0);
447+
TU_VERIFY(p_midi_host->ep_stream.rx.ep_addr != 0, 0);
448+
return p_midi_host->num_cables_rx;
449+
}
450+
451+
uint32_t tuh_midi_read_available(uint8_t dev_addr) {
452+
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
453+
TU_VERIFY(p_midi_host != NULL);
454+
return tu_edpt_stream_read_available(&p_midi_host->ep_stream.rx);
455+
}
456+
457+
uint32_t tuh_midi_write_flush(uint8_t dev_addr) {
458+
midih_interface_t *p_midi = find_midi_by_daddr(dev_addr);
459+
TU_VERIFY(p_midi != NULL);
460+
return tu_edpt_stream_write_xfer(p_midi->dev_addr, &p_midi->ep_stream.tx);
461+
}
462+
463+
//--------------------------------------------------------------------+
464+
// Packet API
465+
//--------------------------------------------------------------------+
466+
467+
uint32_t tuh_midi_packet_read_n(uint8_t dev_addr, uint8_t* buffer, uint32_t bufsize) {
468+
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
469+
TU_VERIFY(p_midi_host != NULL);
470+
471+
uint32_t count4 = tu_min32(bufsize, tu_edpt_stream_read_available(&p_midi_host->ep_stream.rx));
472+
count4 = tu_align4(count4); // round down to multiple of 4
473+
TU_VERIFY(count4 > 0, 0);
474+
return tu_edpt_stream_read(dev_addr, &p_midi_host->ep_stream.rx, buffer, count4);
475+
}
476+
477+
uint32_t tuh_midi_packet_write_n(uint8_t dev_addr, const uint8_t* buffer, uint32_t bufsize) {
478+
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
479+
TU_VERIFY(p_midi_host != NULL, 0);
480+
uint32_t bufsize4 = tu_align4(bufsize);
481+
return tu_edpt_stream_write(dev_addr, &p_midi_host->ep_stream.tx, buffer, bufsize4);
482+
}
483+
484+
//--------------------------------------------------------------------+
485+
// Stream API
486+
//--------------------------------------------------------------------+
487+
#if CFG_TUH_MIDI_STREAM_API
436488
uint32_t tuh_midi_stream_write(uint8_t dev_addr, uint8_t cable_num, uint8_t const *buffer, uint32_t bufsize) {
437489
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
438490
TU_VERIFY(p_midi_host != NULL);
@@ -530,47 +582,6 @@ uint32_t tuh_midi_stream_write(uint8_t dev_addr, uint8_t cable_num, uint8_t cons
530582
return i;
531583
}
532584

533-
bool tuh_midi_packet_write (uint8_t dev_addr, uint8_t const packet[4]) {
534-
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
535-
TU_VERIFY(p_midi_host != NULL);
536-
return 4 == tu_edpt_stream_write(dev_addr, &p_midi_host->ep_stream.tx, packet, 4);
537-
}
538-
539-
uint32_t tuh_midi_stream_flush(uint8_t dev_addr) {
540-
midih_interface_t *p_midi = find_midi_by_daddr(dev_addr);
541-
TU_VERIFY(p_midi != NULL);
542-
return tu_edpt_stream_write_xfer(p_midi->dev_addr, &p_midi->ep_stream.tx);
543-
}
544-
//--------------------------------------------------------------------+
545-
// Helper
546-
//--------------------------------------------------------------------+
547-
uint8_t tuh_midi_get_num_tx_cables (uint8_t dev_addr) {
548-
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
549-
TU_VERIFY(p_midi_host != NULL, 0);
550-
TU_VERIFY(p_midi_host->ep_stream.tx.ep_addr != 0, 0);
551-
return p_midi_host->num_cables_tx;
552-
}
553-
554-
uint8_t tuh_midi_get_num_rx_cables (uint8_t dev_addr) {
555-
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
556-
TU_VERIFY(p_midi_host != NULL, 0);
557-
TU_VERIFY(p_midi_host->ep_stream.rx.ep_addr != 0, 0);
558-
return p_midi_host->num_cables_rx;
559-
}
560-
561-
uint32_t tuh_midi_read_available(uint8_t dev_addr) {
562-
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
563-
TU_VERIFY(p_midi_host != NULL);
564-
return tu_edpt_stream_read_available(&p_midi_host->ep_stream.rx);
565-
}
566-
567-
bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]) {
568-
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
569-
TU_VERIFY(p_midi_host != NULL);
570-
TU_VERIFY(tu_edpt_stream_read_available(&p_midi_host->ep_stream.rx) >= 4);
571-
return 4 == tu_edpt_stream_read(dev_addr, &p_midi_host->ep_stream.rx, packet, 4);
572-
}
573-
574585
uint32_t tuh_midi_stream_read(uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p_buffer, uint16_t bufsize) {
575586
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
576587
TU_VERIFY(p_midi_host != NULL);
@@ -669,6 +680,9 @@ uint32_t tuh_midi_stream_read(uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p
669680
return bytes_buffered;
670681
}
671682

683+
//--------------------------------------------------------------------+
684+
// String API
685+
//--------------------------------------------------------------------+
672686
#if CFG_MIDI_HOST_DEVSTRINGS
673687
static uint8_t find_string_index(midih_interface_t *ptr, uint8_t jack_id)
674688
{
@@ -690,9 +704,7 @@ static uint8_t find_string_index(midih_interface_t *ptr, uint8_t jack_id)
690704
}
691705
return index;
692706
}
693-
#endif
694707

695-
#if CFG_MIDI_HOST_DEVSTRINGS
696708
uint8_t tuh_midi_get_rx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint8_t max_istrings) {
697709
midih_interface_t *p_midi_host = find_midi_by_daddr(dev_addr);
698710
TU_VERIFY(p_midi_host != NULL, 0);
@@ -732,3 +744,5 @@ uint8_t tuh_midi_get_all_istrings(uint8_t dev_addr, const uint8_t** istrings)
732744
return nstrings;
733745
}
734746
#endif
747+
748+
#endif

src/class/midi/midi_host.h

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
* This file is part of the TinyUSB stack.
2525
*/
2626

27-
#ifndef _TUSB_MIDI_HOST_H_
28-
#define _TUSB_MIDI_HOST_H_
27+
#ifndef TUSB_MIDI_HOST_H_
28+
#define TUSB_MIDI_HOST_H_
2929

3030
#include "class/audio/audio.h"
3131
#include "midi.h"
@@ -53,14 +53,20 @@
5353
#define CFG_TUH_MIDI_EP_BUFSIZE TUH_EPSIZE_BULK_MPS
5454
#endif
5555

56+
// Enable the MIDI stream read/write API. Some library can work with raw USB MIDI packet
57+
// Disable this can save driver footprint.
58+
#ifndef CFG_TUH_MIDI_STREAM_API
59+
#define CFG_TUH_MIDI_STREAM_API 1
60+
#endif
61+
5662
#ifndef CFG_MIDI_HOST_DEVSTRINGS
5763
#define CFG_MIDI_HOST_DEVSTRINGS 0
5864
#endif
5965

6066
//--------------------------------------------------------------------+
6167
// Application API
6268
//--------------------------------------------------------------------+
63-
bool tuh_midi_configured (uint8_t dev_addr);
69+
bool tuh_midi_configured (uint8_t dev_addr);
6470

6571
// return the number of virtual midi cables on the device's IN endpoint
6672
uint8_t tuh_midi_get_num_rx_cables(uint8_t dev_addr);
@@ -74,40 +80,51 @@ uint8_t tuh_midi_get_tx_cable_istrings(uint8_t dev_addr, uint8_t* istrings, uint
7480
uint8_t tuh_midi_get_all_istrings(uint8_t dev_addr, const uint8_t** istrings);
7581
#endif
7682

77-
// return the raw number of bytes available from device endpoint.
83+
// return the raw number of bytes available.
7884
// Note: this is related but not the same as number of stream bytes available.
7985
uint32_t tuh_midi_read_available(uint8_t dev_addr);
8086

87+
// Send any queued packets to the device if the host hardware is able to do it
88+
// Returns the number of bytes flushed to the host hardware or 0 if
89+
// the host hardware is busy or there is nothing in queue to send.
90+
uint32_t tuh_midi_write_flush( uint8_t dev_addr);
91+
8192
//--------------------------------------------------------------------+
8293
// Packet API
8394
//--------------------------------------------------------------------+
8495

96+
// Read all available MIDI packets from the connected device
97+
// Return number of bytes read (always multiple of 4)
98+
uint32_t tuh_midi_packet_read_n(uint8_t dev_addr, uint8_t* buffer, uint32_t bufsize);
99+
85100
// Read a raw MIDI packet from the connected device
86-
// This function does not parse the packet format
87101
// Return true if a packet was returned
88-
bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]);
102+
TU_ATTR_ALWAYS_INLINE static inline
103+
bool tuh_midi_packet_read (uint8_t dev_addr, uint8_t packet[4]) {
104+
return 4 == tuh_midi_packet_read_n(dev_addr, packet, 4);
105+
}
106+
107+
// Write all 4-byte packets, data is locally buffered and only transferred when buffered bytes
108+
// reach the endpoint packet size or tuh_midi_write_flush() is called
109+
uint32_t tuh_midi_packet_write_n(uint8_t dev_addr, const uint8_t* buffer, uint32_t bufsize);
89110

90-
// Queue a packet to the device. The application
91-
// must call tuh_midi_stream_flush to actually have the
92-
// data go out. It is up to the application to properly
93-
// format this packet; this function does not check.
111+
// Write a 4-bytes packet to the device.
94112
// Returns true if the packet was successfully queued.
95-
bool tuh_midi_packet_write (uint8_t dev_addr, uint8_t const packet[4]);
113+
TU_ATTR_ALWAYS_INLINE static inline
114+
bool tuh_midi_packet_write (uint8_t dev_addr, uint8_t const packet[4]) {
115+
return 4 == tuh_midi_packet_write_n(dev_addr, packet, 4);
116+
}
96117

97118
//--------------------------------------------------------------------+
98119
// Stream API
99120
//--------------------------------------------------------------------+
121+
#if CFG_TUH_MIDI_STREAM_API
100122

101-
// Queue a message to the device. The application
102-
// must call tuh_midi_stream_flush to actually have the
103-
// data go out.
123+
// Queue a message to the device using stream API. data is locally buffered and only transferred when buffered bytes
124+
// reach the endpoint packet size or tuh_midi_write_flush() is called
125+
// Returns number of bytes was successfully queued.
104126
uint32_t tuh_midi_stream_write (uint8_t dev_addr, uint8_t cable_num, uint8_t const* p_buffer, uint32_t bufsize);
105127

106-
// Send any queued packets to the device if the host hardware is able to do it
107-
// Returns the number of bytes flushed to the host hardware or 0 if
108-
// the host hardware is busy or there is nothing in queue to send.
109-
uint32_t tuh_midi_stream_flush( uint8_t dev_addr);
110-
111128
// Get the MIDI stream from the device. Set the value pointed
112129
// to by p_cable_num to the MIDI cable number intended to receive it.
113130
// The MIDI stream will be stored in the buffer pointed to by p_buffer.
@@ -117,6 +134,8 @@ uint32_t tuh_midi_stream_flush( uint8_t dev_addr);
117134
// it properly.
118135
uint32_t tuh_midi_stream_read (uint8_t dev_addr, uint8_t *p_cable_num, uint8_t *p_buffer, uint16_t bufsize);
119136

137+
#endif
138+
120139
//--------------------------------------------------------------------+
121140
// Callbacks (Weak is optional)
122141
//--------------------------------------------------------------------+
@@ -144,4 +163,4 @@ void midih_close (uint8_t dev_addr);
144163
}
145164
#endif
146165

147-
#endif /* _TUSB_MIDI_HOST_H_ */
166+
#endif

0 commit comments

Comments
 (0)