mirror of
https://github.com/hathach/tinyusb.git
synced 2025-05-09 07:11:16 +08:00
midi host: skip rx data with all zeroes
This commit is contained in:
parent
8c0c21189c
commit
39e6375b74
@ -110,7 +110,7 @@ void tuh_midi_rx_cb(uint8_t idx, uint32_t xferred_bytes) {
|
|||||||
uint8_t cable_num = 0;
|
uint8_t cable_num = 0;
|
||||||
uint32_t bytes_read = tuh_midi_stream_read(idx, &cable_num, buffer, sizeof(buffer));
|
uint32_t bytes_read = tuh_midi_stream_read(idx, &cable_num, buffer, sizeof(buffer));
|
||||||
|
|
||||||
printf("Cable %u rx %lu bytes: ", cable_num, bytes_read);
|
printf("Cable %u rx: ", cable_num);
|
||||||
for (uint32_t i = 0; i < bytes_read; i++) {
|
for (uint32_t i = 0; i < bytes_read; i++) {
|
||||||
printf("%02X ", buffer[i]);
|
printf("%02X ", buffer[i]);
|
||||||
}
|
}
|
||||||
|
@ -166,23 +166,24 @@ bool midih_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint
|
|||||||
(void) result;
|
(void) result;
|
||||||
const uint8_t idx = get_idx_by_ep_addr(dev_addr, ep_addr);
|
const uint8_t idx = get_idx_by_ep_addr(dev_addr, ep_addr);
|
||||||
TU_VERIFY(idx < CFG_TUH_MIDI);
|
TU_VERIFY(idx < CFG_TUH_MIDI);
|
||||||
midih_interface_t *p_midi_host = &_midi_host[idx];
|
midih_interface_t *p_midi = &_midi_host[idx];
|
||||||
|
|
||||||
if (ep_addr == p_midi_host->ep_stream.rx.ep_addr) {
|
if (ep_addr == p_midi->ep_stream.rx.ep_addr) {
|
||||||
// receive new data, put it into FIFO and invoke callback if available
|
// receive new data, put it into FIFO and invoke callback if available
|
||||||
if (xferred_bytes) {
|
// Note: some devices send back all zero packets even if there is no data ready
|
||||||
tu_edpt_stream_read_xfer_complete(&p_midi_host->ep_stream.rx, xferred_bytes);
|
if (xferred_bytes && !tu_mem_is_zero(p_midi->ep_stream.rx.ep_buf, xferred_bytes)) {
|
||||||
|
tu_edpt_stream_read_xfer_complete(&p_midi->ep_stream.rx, xferred_bytes);
|
||||||
tuh_midi_rx_cb(idx, xferred_bytes);
|
tuh_midi_rx_cb(idx, xferred_bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
tu_edpt_stream_read_xfer(dev_addr, &p_midi_host->ep_stream.rx); // prepare for next transfer
|
tu_edpt_stream_read_xfer(dev_addr, &p_midi->ep_stream.rx); // prepare for next transfer
|
||||||
} else if (ep_addr == p_midi_host->ep_stream.tx.ep_addr) {
|
} else if (ep_addr == p_midi->ep_stream.tx.ep_addr) {
|
||||||
tuh_midi_tx_cb(idx, xferred_bytes);
|
tuh_midi_tx_cb(idx, xferred_bytes);
|
||||||
|
|
||||||
if (0 == tu_edpt_stream_write_xfer(dev_addr, &p_midi_host->ep_stream.tx)) {
|
if (0 == tu_edpt_stream_write_xfer(dev_addr, &p_midi->ep_stream.tx)) {
|
||||||
// If there is no data left, a ZLP should be sent if
|
// If there is no data left, a ZLP should be sent if
|
||||||
// xferred_bytes is multiple of EP size and not zero
|
// xferred_bytes is multiple of EP size and not zero
|
||||||
tu_edpt_stream_write_zlp_if_needed(dev_addr, &p_midi_host->ep_stream.tx, xferred_bytes);
|
tu_edpt_stream_write_zlp_if_needed(dev_addr, &p_midi->ep_stream.tx, xferred_bytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,6 +120,22 @@ TU_ATTR_ALWAYS_INLINE static inline int tu_memcpy_s(void *dest, size_t destsz, c
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TU_ATTR_ALWAYS_INLINE static inline bool tu_mem_is_zero(const void *buffer, size_t size) {
|
||||||
|
const uint8_t* buf8 = (const uint8_t*) buffer;
|
||||||
|
for (size_t i = 0; i < size; i++) {
|
||||||
|
if (buf8[i] != 0) { return false; }
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
TU_ATTR_ALWAYS_INLINE static inline bool tu_mem_is_ff(const void *buffer, size_t size) {
|
||||||
|
const uint8_t* buf8 = (const uint8_t*) buffer;
|
||||||
|
for (size_t i = 0; i < size; i++) {
|
||||||
|
if (buf8[i] != 0xff) { return false; }
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//------------- Bytes -------------//
|
//------------- Bytes -------------//
|
||||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_u32(uint8_t b3, uint8_t b2, uint8_t b1, uint8_t b0) {
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_u32(uint8_t b3, uint8_t b2, uint8_t b1, uint8_t b0) {
|
||||||
@ -181,8 +197,7 @@ TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_round_up(uint32_t v, uint32_t f)
|
|||||||
|
|
||||||
// log2 of a value is its MSB's position
|
// log2 of a value is its MSB's position
|
||||||
// TODO use clz TODO remove
|
// TODO use clz TODO remove
|
||||||
static inline uint8_t tu_log2(uint32_t value)
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_log2(uint32_t value) {
|
||||||
{
|
|
||||||
uint8_t result = 0;
|
uint8_t result = 0;
|
||||||
while (value >>= 1) { result++; }
|
while (value >>= 1) { result++; }
|
||||||
return result;
|
return result;
|
||||||
@ -193,8 +208,7 @@ static inline uint8_t tu_log2(uint32_t value)
|
|||||||
// return sizeof(uint32_t) * CHAR_BIT - __builtin_clz(x) - 1;
|
// return sizeof(uint32_t) * CHAR_BIT - __builtin_clz(x) - 1;
|
||||||
//}
|
//}
|
||||||
|
|
||||||
static inline bool tu_is_power_of_two(uint32_t value)
|
TU_ATTR_ALWAYS_INLINE static inline bool tu_is_power_of_two(uint32_t value) {
|
||||||
{
|
|
||||||
return (value != 0) && ((value & (value - 1)) == 0);
|
return (value != 0) && ((value & (value - 1)) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,27 +219,23 @@ static inline bool tu_is_power_of_two(uint32_t value)
|
|||||||
typedef struct { uint16_t val; } TU_ATTR_PACKED tu_unaligned_uint16_t;
|
typedef struct { uint16_t val; } TU_ATTR_PACKED tu_unaligned_uint16_t;
|
||||||
typedef struct { uint32_t val; } TU_ATTR_PACKED tu_unaligned_uint32_t;
|
typedef struct { uint32_t val; } TU_ATTR_PACKED tu_unaligned_uint32_t;
|
||||||
|
|
||||||
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_unaligned_read32(const void* mem)
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_unaligned_read32(const void *mem) {
|
||||||
{
|
tu_unaligned_uint32_t const *ua32 = (tu_unaligned_uint32_t const *) mem;
|
||||||
tu_unaligned_uint32_t const* ua32 = (tu_unaligned_uint32_t const*) mem;
|
|
||||||
return ua32->val;
|
return ua32->val;
|
||||||
}
|
}
|
||||||
|
|
||||||
TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write32(void* mem, uint32_t value)
|
TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write32(void *mem, uint32_t value) {
|
||||||
{
|
tu_unaligned_uint32_t *ua32 = (tu_unaligned_uint32_t *) mem;
|
||||||
tu_unaligned_uint32_t* ua32 = (tu_unaligned_uint32_t*) mem;
|
|
||||||
ua32->val = value;
|
ua32->val = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_unaligned_read16(const void* mem)
|
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_unaligned_read16(const void *mem) {
|
||||||
{
|
tu_unaligned_uint16_t const *ua16 = (tu_unaligned_uint16_t const *) mem;
|
||||||
tu_unaligned_uint16_t const* ua16 = (tu_unaligned_uint16_t const*) mem;
|
|
||||||
return ua16->val;
|
return ua16->val;
|
||||||
}
|
}
|
||||||
|
|
||||||
TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write16(void* mem, uint16_t value)
|
TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write16(void *mem, uint16_t value) {
|
||||||
{
|
tu_unaligned_uint16_t *ua16 = (tu_unaligned_uint16_t *) mem;
|
||||||
tu_unaligned_uint16_t* ua16 = (tu_unaligned_uint16_t*) mem;
|
|
||||||
ua16->val = value;
|
ua16->val = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user