follow up to pr #46

This commit is contained in:
hathach 2019-03-25 11:38:16 +07:00
parent fad8720215
commit aaf5714268

View File

@ -75,22 +75,19 @@ typedef struct
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
CFG_TUSB_MEM_SECTION static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC] = { { 0 } }; CFG_TUSB_MEM_SECTION static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC] = { { 0 } };
bool pending_read_from_host; static void _prep_out_transaction (uint8_t itf)
static void _prep_next_transaction(void) { {
uint8_t const itf = 0; cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
// skip if previous transfer not complete // skip if previous transfer not complete
if (pending_read_from_host) { if ( dcd_edpt_busy(TUD_OPT_RHPORT, p_cdc->ep_out) ) return;
return;
}
// Prepare for incoming data but only allow what we can store in the ring buffer. // Prepare for incoming data but only allow what we can store in the ring buffer.
uint16_t max_read = tu_fifo_remaining(&p_cdc->rx_ff); uint16_t max_read = tu_fifo_remaining(&p_cdc->rx_ff);
if (max_read >= CFG_TUD_CDC_EPSIZE) { if ( max_read >= CFG_TUD_CDC_EPSIZE )
dcd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_out, p_cdc->epout_buf, CFG_TUD_CDC_EPSIZE); {
pending_read_from_host = true; dcd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_out, p_cdc->epout_buf, CFG_TUD_CDC_EPSIZE);
} }
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@ -129,18 +126,13 @@ uint32_t tud_cdc_n_available(uint8_t itf)
char tud_cdc_n_read_char(uint8_t itf) char tud_cdc_n_read_char(uint8_t itf)
{ {
char ch; char ch;
if (!tu_fifo_read(&_cdcd_itf[itf].rx_ff, &ch)) { return tud_cdc_n_read(itf, &ch, 1) ? ch : (-1);
ch = -1;
}
_prep_next_transaction();
return ch;
} }
uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize) uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize)
{ {
uint32_t num_read = tu_fifo_read_n(&_cdcd_itf[itf].rx_ff, buffer, bufsize); uint32_t num_read = tu_fifo_read_n(&_cdcd_itf[itf].rx_ff, buffer, bufsize);
_prep_next_transaction(); _prep_out_transaction(itf);
return num_read; return num_read;
} }
@ -153,7 +145,7 @@ char tud_cdc_n_peek(uint8_t itf, int pos)
void tud_cdc_n_read_flush (uint8_t itf) void tud_cdc_n_read_flush (uint8_t itf)
{ {
tu_fifo_clear(&_cdcd_itf[itf].rx_ff); tu_fifo_clear(&_cdcd_itf[itf].rx_ff);
_prep_next_transaction(); _prep_out_transaction(itf);
} }
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@ -254,11 +246,12 @@ bool cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t
// Find available interface // Find available interface
cdcd_interface_t * p_cdc = NULL; cdcd_interface_t * p_cdc = NULL;
for(uint8_t i=0; i<CFG_TUD_CDC; i++) uint8_t cdc_id;
for(cdc_id=0; cdc_id<CFG_TUD_CDC; cdc_id++)
{ {
if ( _cdcd_itf[i].ep_in == 0 ) if ( _cdcd_itf[cdc_id].ep_in == 0 )
{ {
p_cdc = &_cdcd_itf[i]; p_cdc = &_cdcd_itf[cdc_id];
break; break;
} }
} }
@ -304,8 +297,7 @@ bool cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t
} }
// Prepare for incoming data // Prepare for incoming data
pending_read_from_host = false; _prep_out_transaction(cdc_id);
_prep_next_transaction();
return true; return true;
} }
@ -398,11 +390,11 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
// invoke receive callback (if there is still data) // invoke receive callback (if there is still data)
if (tud_cdc_rx_cb && tu_fifo_count(&p_cdc->rx_ff) ) tud_cdc_rx_cb(itf); if (tud_cdc_rx_cb && tu_fifo_count(&p_cdc->rx_ff) ) tud_cdc_rx_cb(itf);
pending_read_from_host = false; // prepare for OUT transaction
_prep_next_transaction(); _prep_out_transaction(itf);
} }
// nothing to do with in and notif endpoint // nothing to do with in and notif endpoint for now
return true; return true;
} }