From d434635822ed7d712c60b3c7a17b957bc09d919e Mon Sep 17 00:00:00 2001 From: sommersoft Date: Fri, 23 Mar 2018 15:07:02 +0000 Subject: [PATCH 1/6] add buffer check before triggering new usb read --- ports/atmel-samd/usb.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/ports/atmel-samd/usb.c b/ports/atmel-samd/usb.c index 740486aa1ebf9..a90308564e10a 100644 --- a/ports/atmel-samd/usb.c +++ b/ports/atmel-samd/usb.c @@ -25,6 +25,7 @@ */ #include "usb.h" +#include #include @@ -49,6 +50,7 @@ #include "usb_mass_storage.h" #include "supervisor/shared/autoreload.h" +#include "supervisor/serial.h" // Store received characters on our own so that we can filter control characters // and act immediately on CTRL-C for example. @@ -126,7 +128,7 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u atomic_leave_critical(&flags); return true; } - + for (uint16_t i = 0; i < count; i++) { uint8_t c = cdc_packet_buffer[i]; if (c == mp_interrupt_char) { @@ -150,15 +152,15 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u } } atomic_leave_critical(&flags); - + // Trigger a follow up read if we have space. - if (usb_rx_count < USB_RX_BUF_SIZE) { + /*if (usb_rx_count < USB_RX_BUF_SIZE - 64) { int32_t result = start_read(); if (result != ERR_NONE) { return true; } - } - + }*/ + /* No error. */ return false; } @@ -170,7 +172,9 @@ static bool write_complete(const uint8_t ep, return false; // No errors. } // This is called after writes are finished. + usb_transmitting = false; + /* No error. */ return false; } @@ -243,9 +247,16 @@ static bool cdc_enabled(void) { } bool usb_bytes_available(void) { + // Check if the buffer has data, but not enough + // space to hold another read. + if (usb_rx_count > 64) { + return usb_rx_count > 0; + } + // Buffer has enough room if (cdc_enabled() && !pending_read) { start_read(); } + // Buffer is empty and/or no new data is available if (usb_rx_count == 0) { return false; } @@ -263,16 +274,16 @@ int usb_read(void) { data = usb_rx_buf[usb_rx_buf_head]; usb_rx_buf_head++; usb_rx_count--; - if ((USB_RX_BUF_SIZE) == usb_rx_buf_head) { + if (usb_rx_buf_head == USB_RX_BUF_SIZE) { usb_rx_buf_head = 0; } CRITICAL_SECTION_LEAVE(); // Trigger a new read because we just cleared some space. - if (!pending_read && usb_rx_count == USB_RX_BUF_SIZE - 1) { + /*if (!pending_read && usb_rx_count == USB_RX_BUF_SIZE - 1) { start_read(); - } - + }*/ + return data; } From ccbe557e3026f0cf53f2e9e40137552b2f0f08cd Mon Sep 17 00:00:00 2001 From: sommersoft Date: Fri, 23 Mar 2018 15:45:30 +0000 Subject: [PATCH 2/6] removed leftover debugging bits --- ports/atmel-samd/usb.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/ports/atmel-samd/usb.c b/ports/atmel-samd/usb.c index a90308564e10a..c95ec06b788e6 100644 --- a/ports/atmel-samd/usb.c +++ b/ports/atmel-samd/usb.c @@ -25,7 +25,6 @@ */ #include "usb.h" -#include #include @@ -50,7 +49,6 @@ #include "usb_mass_storage.h" #include "supervisor/shared/autoreload.h" -#include "supervisor/serial.h" // Store received characters on our own so that we can filter control characters // and act immediately on CTRL-C for example. @@ -128,7 +126,7 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u atomic_leave_critical(&flags); return true; } - + for (uint16_t i = 0; i < count; i++) { uint8_t c = cdc_packet_buffer[i]; if (c == mp_interrupt_char) { @@ -152,7 +150,7 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u } } atomic_leave_critical(&flags); - + // Trigger a follow up read if we have space. /*if (usb_rx_count < USB_RX_BUF_SIZE - 64) { int32_t result = start_read(); @@ -160,7 +158,7 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u return true; } }*/ - + /* No error. */ return false; } @@ -283,7 +281,7 @@ int usb_read(void) { /*if (!pending_read && usb_rx_count == USB_RX_BUF_SIZE - 1) { start_read(); }*/ - + return data; } From f237657e5ebc592c2a8f37546c19a421ee5f37f8 Mon Sep 17 00:00:00 2001 From: sommersoft Date: Fri, 23 Mar 2018 18:41:27 +0000 Subject: [PATCH 3/6] extended buffer check to usb_cdc_background --- ports/atmel-samd/usb.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ports/atmel-samd/usb.c b/ports/atmel-samd/usb.c index c95ec06b788e6..6148c56ae68fa 100644 --- a/ports/atmel-samd/usb.c +++ b/ports/atmel-samd/usb.c @@ -331,6 +331,9 @@ bool usb_connected(void) { void usb_cdc_background() { // if (mp_interrupt_char != -1 && cdc_enabled() && !pending_read) { - start_read(); + // Make sure we have space in the buffer + if (usb_rx_count < 64) { + start_read(); + } } } From ef16109c5dbde3136ae901b55525eb59f1d1fb4f Mon Sep 17 00:00:00 2001 From: sommersoft Date: Sat, 24 Mar 2018 00:55:48 +0000 Subject: [PATCH 4/6] updated with requested changes --- ports/atmel-samd/usb.c | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/ports/atmel-samd/usb.c b/ports/atmel-samd/usb.c index 6148c56ae68fa..af70be99af3c3 100644 --- a/ports/atmel-samd/usb.c +++ b/ports/atmel-samd/usb.c @@ -102,12 +102,13 @@ static void init_hardware(void) { #endif } -COMPILER_ALIGNED(4) uint8_t cdc_packet_buffer[64]; +#define CDC_BULKOUT_SIZE CONF_USB_COMPOSITE_CDC_ACM_DATA_BULKOUT_MAXPKSZ +COMPILER_ALIGNED(4) uint8_t cdc_packet_buffer[CDC_BULKOUT_SIZE]; static volatile bool pending_read; static int32_t start_read(void) { pending_read = true; - int32_t result = cdcdf_acm_read(cdc_packet_buffer, 64); + int32_t result = cdcdf_acm_read(cdc_packet_buffer, CDC_BULKOUT_SIZE); if (result != ERR_NONE) { pending_read = false; } @@ -151,14 +152,6 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u } atomic_leave_critical(&flags); - // Trigger a follow up read if we have space. - /*if (usb_rx_count < USB_RX_BUF_SIZE - 64) { - int32_t result = start_read(); - if (result != ERR_NONE) { - return true; - } - }*/ - /* No error. */ return false; } @@ -247,7 +240,7 @@ static bool cdc_enabled(void) { bool usb_bytes_available(void) { // Check if the buffer has data, but not enough // space to hold another read. - if (usb_rx_count > 64) { + if (usb_rx_count > CDC_BULKOUT_SIZE) { return usb_rx_count > 0; } // Buffer has enough room @@ -277,11 +270,6 @@ int usb_read(void) { } CRITICAL_SECTION_LEAVE(); - // Trigger a new read because we just cleared some space. - /*if (!pending_read && usb_rx_count == USB_RX_BUF_SIZE - 1) { - start_read(); - }*/ - return data; } @@ -328,12 +316,10 @@ bool usb_connected(void) { // Poll for input if keyboard interrupts are enabled, // so that we can check for the interrupt char. read_complete() does the checking. +// also make sure we have enough room in the local buffer void usb_cdc_background() { // - if (mp_interrupt_char != -1 && cdc_enabled() && !pending_read) { - // Make sure we have space in the buffer - if (usb_rx_count < 64) { - start_read(); - } + if (mp_interrupt_char != -1 && cdc_enabled() && !pending_read && usb_rx_count < CDC_BULKOUT_SIZE) { + start_read(); } } From 23009fdd6343d9536ae644faca27be5a253aa08e Mon Sep 17 00:00:00 2001 From: sommersoft Date: Mon, 26 Mar 2018 06:25:04 +0000 Subject: [PATCH 5/6] future-proof for buffer size changes --- ports/atmel-samd/usb.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ports/atmel-samd/usb.c b/ports/atmel-samd/usb.c index af70be99af3c3..0d0a7a2932c68 100644 --- a/ports/atmel-samd/usb.c +++ b/ports/atmel-samd/usb.c @@ -222,17 +222,17 @@ void init_usb(void) { } static bool cdc_enabled(void) { - if (mp_cdc_enabled) { - return true; - } if (!cdcdf_acm_is_enabled()) { + mp_cdc_enabled = false; return false; } - cdcdf_acm_register_callback(CDCDF_ACM_CB_READ, (FUNC_PTR)read_complete); - cdcdf_acm_register_callback(CDCDF_ACM_CB_WRITE, (FUNC_PTR)write_complete); - cdcdf_acm_register_callback(CDCDF_ACM_CB_STATE_C, (FUNC_PTR)usb_device_cb_state_c); - cdcdf_acm_register_callback(CDCDF_ACM_CB_LINE_CODING_C, (FUNC_PTR)usb_device_cb_line_coding_c); - mp_cdc_enabled = true; + if (!mp_cdc_enabled) { + cdcdf_acm_register_callback(CDCDF_ACM_CB_READ, (FUNC_PTR)read_complete); + cdcdf_acm_register_callback(CDCDF_ACM_CB_WRITE, (FUNC_PTR)write_complete); + cdcdf_acm_register_callback(CDCDF_ACM_CB_STATE_C, (FUNC_PTR)usb_device_cb_state_c); + cdcdf_acm_register_callback(CDCDF_ACM_CB_LINE_CODING_C, (FUNC_PTR)usb_device_cb_line_coding_c); + mp_cdc_enabled = true; + } return true; } @@ -240,7 +240,7 @@ static bool cdc_enabled(void) { bool usb_bytes_available(void) { // Check if the buffer has data, but not enough // space to hold another read. - if (usb_rx_count > CDC_BULKOUT_SIZE) { + if (usb_rx_count > USB_RX_BUF_SIZE - CDC_BULKOUT_SIZE) { return usb_rx_count > 0; } // Buffer has enough room @@ -319,7 +319,7 @@ bool usb_connected(void) { // also make sure we have enough room in the local buffer void usb_cdc_background() { // - if (mp_interrupt_char != -1 && cdc_enabled() && !pending_read && usb_rx_count < CDC_BULKOUT_SIZE) { + if (mp_interrupt_char != -1 && cdc_enabled() && !pending_read && (usb_rx_count < USB_RX_BUF_SIZE - CDC_BULKOUT_SIZE)) { start_read(); } } From 9bd55cf4c7af0d75cd678d9c9b15601d5b7b768d Mon Sep 17 00:00:00 2001 From: sommersoft Date: Mon, 26 Mar 2018 08:14:37 -0500 Subject: [PATCH 6/6] minor cleanup --- ports/atmel-samd/usb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/atmel-samd/usb.c b/ports/atmel-samd/usb.c index 0d0a7a2932c68..fd939e22df882 100644 --- a/ports/atmel-samd/usb.c +++ b/ports/atmel-samd/usb.c @@ -241,7 +241,7 @@ bool usb_bytes_available(void) { // Check if the buffer has data, but not enough // space to hold another read. if (usb_rx_count > USB_RX_BUF_SIZE - CDC_BULKOUT_SIZE) { - return usb_rx_count > 0; + return true; } // Buffer has enough room if (cdc_enabled() && !pending_read) {