DUE USB CDC: Do not send any character if no program on the PC is listening to them. This avoids Marlin waiting until the user actually opens a program that is able to consume the output of Marlin

This commit is contained in:
etagle
2018-03-15 00:56:28 -03:00
parent 4aebe3d82e
commit 404fc94705
4 changed files with 45 additions and 5 deletions

View File

@@ -36,6 +36,7 @@
// Imports from Atmel USB Stack/CDC implementation
extern "C" {
bool usb_task_cdc_isenabled(void);
bool usb_task_cdc_dtr_active(void);
bool udi_cdc_is_rx_ready(void);
int udi_cdc_getc(void);
bool udi_cdc_is_tx_ready(void);
@@ -56,9 +57,11 @@ int MarlinSerialUSB::peek(void) {
if (pending_char >= 0)
return pending_char;
// If USB CDC not enumerated or not configured on the PC side
if (!usb_task_cdc_isenabled())
return -1;
// If no bytes sent from the PC
if (!udi_cdc_is_rx_ready())
return -1;
@@ -73,9 +76,11 @@ int MarlinSerialUSB::read(void) {
return ret;
}
// If USB CDC not enumerated or not configured on the PC side
if (!usb_task_cdc_isenabled())
return -1;
// If no bytes sent from the PC
if (!udi_cdc_is_rx_ready())
return -1;
@@ -83,7 +88,10 @@ int MarlinSerialUSB::read(void) {
}
bool MarlinSerialUSB::available(void) {
/* If Pending chars */
return pending_char >= 0 ||
/* or USB CDC enumerated and configured on the PC side and some
bytes where sent to us */
(usb_task_cdc_isenabled() && udi_cdc_is_rx_ready());
}
@@ -92,11 +100,22 @@ void MarlinSerialUSB::flush(void) {
void MarlinSerialUSB::write(const uint8_t c) {
/* Do not even bother sending anything if USB CDC is not enumerated
or not configured on the PC side or there is no program on the PC
listening to our messages */
if (!usb_task_cdc_isenabled() || !usb_task_cdc_dtr_active())
return;
/* Wait until the PC has read the pending to be sent data */
while (usb_task_cdc_isenabled() &&
usb_task_cdc_dtr_active() &&
!udi_cdc_is_tx_ready()) {
};
if (!usb_task_cdc_isenabled())
/* Do not even bother sending anything if USB CDC is not enumerated
or not configured on the PC side or there is no program on the PC
listening to our messages at this point */
if (!usb_task_cdc_isenabled() || !usb_task_cdc_dtr_active())
return;
// Fifo full