From 4232a0a196feb08588dda3199012a166ad65aec4 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 26 Oct 2017 09:30:59 -0400 Subject: [PATCH 1/6] Sync with https://github.com/adafruit/asf4/pull/3 --- ports/atmel-samd/asf4 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/atmel-samd/asf4 b/ports/atmel-samd/asf4 index 8e3e471f481ef..e1d02fde5d23a 160000 --- a/ports/atmel-samd/asf4 +++ b/ports/atmel-samd/asf4 @@ -1 +1 @@ -Subproject commit 8e3e471f481effddd5c23da2342e1e4aab618e1f +Subproject commit e1d02fde5d23a704d33d74e18fcf624eb56132c5 From f0a12d96203784e4bd636f201d6d050f48c537e7 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 26 Oct 2017 22:31:56 -0400 Subject: [PATCH 2/6] Check INTERNAL_LIBM make flag in a safer way. --- ports/atmel-samd/Makefile | 8 ++++---- ports/atmel-samd/mpconfigport.mk | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index f9751415a1abe..ef168dbc90b58 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -238,10 +238,10 @@ SRC_C = \ # Choose which flash filesystem impl to use. # (Right now INTERNAL_FLASH_FILESYSTEM and SPI_FLASH_FILESYSTEM are mutually exclusive. # But that might not be true in the future.) -ifdef INTERNAL_FLASH_FILESYSTEM +ifeq ($(INTERNAL_FLASH_FILESYSTEM),1) SRC_C += internal_flash.c endif -ifdef SPI_FLASH_FILESYSTEM +ifeq ($(SPI_FLASH_FILESYSTEM),1) SRC_C += spi_flash.c endif @@ -278,7 +278,7 @@ SRC_COMMON_HAL = \ usb_hid/__init__.c \ usb_hid/Device.c -ifdef INTERNAL_LIBM +ifeq ($(INTERNAL_LIBM),1) SRC_LIBM = $(addprefix lib/,\ libm/math.c \ libm/fmodf.c \ @@ -338,7 +338,7 @@ OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_ASF:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_EXPANDED:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED_MODULE_EXPANDED:.c=.o)) -ifdef INTERNAL_LIBM +ifeq ($(INTERNAL_LIBM),1) OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o)) endif diff --git a/ports/atmel-samd/mpconfigport.mk b/ports/atmel-samd/mpconfigport.mk index acb0916fccb6c..c3b2f22d4f992 100644 --- a/ports/atmel-samd/mpconfigport.mk +++ b/ports/atmel-samd/mpconfigport.mk @@ -3,5 +3,5 @@ # This should correspond to the MICROPY_LONGINT_IMPL definition in mpconfigport.h. MPY_TOOL_LONGINT_IMPL = -mlongint-impl=none -INTERNAL_LIBM = (1) +INTERNAL_LIBM = 1 From 2487226dff08ba3ec75019a10374eda07fc1a50d Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 26 Oct 2017 22:53:28 -0400 Subject: [PATCH 3/6] Read serial input as a background task so we can check for the interrupt character. --- main.c | 1 + ports/atmel-samd/background.c | 2 ++ ports/atmel-samd/usb.c | 27 +++++++++++++++++++-------- ports/atmel-samd/usb.h | 1 + 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/main.c b/main.c index ba1e0e4013399..a2e4b9f729761 100644 --- a/main.c +++ b/main.c @@ -127,6 +127,7 @@ bool start_mp(safe_mode_t safe_mode) { pyexec_result_t result; bool found_main = false; + if (safe_mode != NO_SAFE_MODE) { serial_write(MSG_SAFE_MODE_NO_MAIN); } else { diff --git a/ports/atmel-samd/background.c b/ports/atmel-samd/background.c index 6515e448b4991..2fdf059823b18 100644 --- a/ports/atmel-samd/background.c +++ b/ports/atmel-samd/background.c @@ -26,9 +26,11 @@ #include "background.h" // #include "common-hal/audioio/AudioOut.h" +#include "usb.h" #include "usb_mass_storage.h" void run_background_tasks(void) { // audioout_background(); usb_msc_background(); + usb_cdc_background(); } diff --git a/ports/atmel-samd/usb.c b/ports/atmel-samd/usb.c index e0b9ddc41ca2f..bd712a5d2c071 100644 --- a/ports/atmel-samd/usb.c +++ b/ports/atmel-samd/usb.c @@ -56,13 +56,13 @@ static uint8_t usb_rx_buf[USB_RX_BUF_SIZE]; // Receive buffer head -static volatile uint8_t usb_rx_buf_head; +static volatile uint8_t usb_rx_buf_head = 0; // Receive buffer tail -static volatile uint8_t usb_rx_buf_tail; +static volatile uint8_t usb_rx_buf_tail = 0; // Number of bytes in receive buffer -volatile uint8_t usb_rx_count; +volatile uint8_t usb_rx_count = 0; volatile bool mp_cdc_enabled = false; volatile bool usb_transmitting = false; @@ -133,8 +133,11 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u uint8_t c = cdc_packet_buffer[i]; if (c == mp_interrupt_char) { mp_keyboard_interrupt(); - // Don't put the interrupt into the buffer, just continue. - continue; + // If interrupted, flush all the input. + usb_rx_count = 0; + usb_rx_buf_head = 0; + usb_rx_buf_tail = 0; + break; } else { // The count of characters present in receive buffer is // incremented. @@ -144,7 +147,7 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u if (usb_rx_buf_tail == USB_RX_BUF_SIZE) { // Reached the end of buffer, revert back to beginning of // buffer. - usb_rx_buf_tail = 0x00; + usb_rx_buf_tail = 0; } } } @@ -219,8 +222,7 @@ void init_usb(void) { mscdf_register_callback(MSCDF_CB_TEST_DISK_READY, (FUNC_PTR)usb_msc_disk_is_ready); mscdf_register_callback(MSCDF_CB_XFER_BLOCKS_DONE, (FUNC_PTR)usb_msc_xfer_done); - int32_t result = usbdc_start(&multi_desc); - while (result != ERR_NONE) {} + usbdc_start(&multi_desc); usbdc_attach(); } @@ -315,3 +317,12 @@ void usb_write(const char* buffer, uint32_t len) { bool usb_connected(void) { return cdc_enabled(); } + +// Poll for input if keyboard interrupts are enabled, +// so that we can check for the interrupt char. read_complete() does the checking. +void usb_cdc_background() { + // + if (mp_interrupt_char != -1 && cdc_enabled() && !pending_read) { + start_read(); + } +} diff --git a/ports/atmel-samd/usb.h b/ports/atmel-samd/usb.h index 754331211d6a1..45c05df9c0290 100644 --- a/ports/atmel-samd/usb.h +++ b/ports/atmel-samd/usb.h @@ -37,5 +37,6 @@ int usb_read(void); void usb_write(const char* buffer, uint32_t len); bool usb_bytes_available(void); bool usb_connected(void); +void usb_cdc_background(void); #endif // __MICROPY_INCLUDED_ATMEL_SAMD_USB_H__ From 7f88ba3b2642c5b326b3df1821a055ef47e95c3a Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 27 Oct 2017 17:14:57 -0400 Subject: [PATCH 4/6] Revert "Allow main.py to be interrupted by ctrl-C" (#381) * Revert "Read serial input as a background task so we can check for the interrupt character." This reverts commit 046092e8a29822ae5d761fbaac1a3af181b1297c. * Revert "Check INTERNAL_LIBM make flag in a safer way." This reverts commit 2b80add22f5d22544d8362c19d4332a489325ac9. --- main.c | 1 - ports/atmel-samd/Makefile | 8 ++++---- ports/atmel-samd/background.c | 2 -- ports/atmel-samd/mpconfigport.mk | 2 +- ports/atmel-samd/usb.c | 27 ++++++++------------------- ports/atmel-samd/usb.h | 1 - 6 files changed, 13 insertions(+), 28 deletions(-) diff --git a/main.c b/main.c index a2e4b9f729761..ba1e0e4013399 100644 --- a/main.c +++ b/main.c @@ -127,7 +127,6 @@ bool start_mp(safe_mode_t safe_mode) { pyexec_result_t result; bool found_main = false; - if (safe_mode != NO_SAFE_MODE) { serial_write(MSG_SAFE_MODE_NO_MAIN); } else { diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index ef168dbc90b58..f9751415a1abe 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -238,10 +238,10 @@ SRC_C = \ # Choose which flash filesystem impl to use. # (Right now INTERNAL_FLASH_FILESYSTEM and SPI_FLASH_FILESYSTEM are mutually exclusive. # But that might not be true in the future.) -ifeq ($(INTERNAL_FLASH_FILESYSTEM),1) +ifdef INTERNAL_FLASH_FILESYSTEM SRC_C += internal_flash.c endif -ifeq ($(SPI_FLASH_FILESYSTEM),1) +ifdef SPI_FLASH_FILESYSTEM SRC_C += spi_flash.c endif @@ -278,7 +278,7 @@ SRC_COMMON_HAL = \ usb_hid/__init__.c \ usb_hid/Device.c -ifeq ($(INTERNAL_LIBM),1) +ifdef INTERNAL_LIBM SRC_LIBM = $(addprefix lib/,\ libm/math.c \ libm/fmodf.c \ @@ -338,7 +338,7 @@ OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_ASF:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_EXPANDED:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED_MODULE_EXPANDED:.c=.o)) -ifeq ($(INTERNAL_LIBM),1) +ifdef INTERNAL_LIBM OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o)) endif diff --git a/ports/atmel-samd/background.c b/ports/atmel-samd/background.c index 2fdf059823b18..6515e448b4991 100644 --- a/ports/atmel-samd/background.c +++ b/ports/atmel-samd/background.c @@ -26,11 +26,9 @@ #include "background.h" // #include "common-hal/audioio/AudioOut.h" -#include "usb.h" #include "usb_mass_storage.h" void run_background_tasks(void) { // audioout_background(); usb_msc_background(); - usb_cdc_background(); } diff --git a/ports/atmel-samd/mpconfigport.mk b/ports/atmel-samd/mpconfigport.mk index c3b2f22d4f992..acb0916fccb6c 100644 --- a/ports/atmel-samd/mpconfigport.mk +++ b/ports/atmel-samd/mpconfigport.mk @@ -3,5 +3,5 @@ # This should correspond to the MICROPY_LONGINT_IMPL definition in mpconfigport.h. MPY_TOOL_LONGINT_IMPL = -mlongint-impl=none -INTERNAL_LIBM = 1 +INTERNAL_LIBM = (1) diff --git a/ports/atmel-samd/usb.c b/ports/atmel-samd/usb.c index bd712a5d2c071..e0b9ddc41ca2f 100644 --- a/ports/atmel-samd/usb.c +++ b/ports/atmel-samd/usb.c @@ -56,13 +56,13 @@ static uint8_t usb_rx_buf[USB_RX_BUF_SIZE]; // Receive buffer head -static volatile uint8_t usb_rx_buf_head = 0; +static volatile uint8_t usb_rx_buf_head; // Receive buffer tail -static volatile uint8_t usb_rx_buf_tail = 0; +static volatile uint8_t usb_rx_buf_tail; // Number of bytes in receive buffer -volatile uint8_t usb_rx_count = 0; +volatile uint8_t usb_rx_count; volatile bool mp_cdc_enabled = false; volatile bool usb_transmitting = false; @@ -133,11 +133,8 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u uint8_t c = cdc_packet_buffer[i]; if (c == mp_interrupt_char) { mp_keyboard_interrupt(); - // If interrupted, flush all the input. - usb_rx_count = 0; - usb_rx_buf_head = 0; - usb_rx_buf_tail = 0; - break; + // Don't put the interrupt into the buffer, just continue. + continue; } else { // The count of characters present in receive buffer is // incremented. @@ -147,7 +144,7 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u if (usb_rx_buf_tail == USB_RX_BUF_SIZE) { // Reached the end of buffer, revert back to beginning of // buffer. - usb_rx_buf_tail = 0; + usb_rx_buf_tail = 0x00; } } } @@ -222,7 +219,8 @@ void init_usb(void) { mscdf_register_callback(MSCDF_CB_TEST_DISK_READY, (FUNC_PTR)usb_msc_disk_is_ready); mscdf_register_callback(MSCDF_CB_XFER_BLOCKS_DONE, (FUNC_PTR)usb_msc_xfer_done); - usbdc_start(&multi_desc); + int32_t result = usbdc_start(&multi_desc); + while (result != ERR_NONE) {} usbdc_attach(); } @@ -317,12 +315,3 @@ void usb_write(const char* buffer, uint32_t len) { bool usb_connected(void) { return cdc_enabled(); } - -// Poll for input if keyboard interrupts are enabled, -// so that we can check for the interrupt char. read_complete() does the checking. -void usb_cdc_background() { - // - if (mp_interrupt_char != -1 && cdc_enabled() && !pending_read) { - start_read(); - } -} diff --git a/ports/atmel-samd/usb.h b/ports/atmel-samd/usb.h index 45c05df9c0290..754331211d6a1 100644 --- a/ports/atmel-samd/usb.h +++ b/ports/atmel-samd/usb.h @@ -37,6 +37,5 @@ int usb_read(void); void usb_write(const char* buffer, uint32_t len); bool usb_bytes_available(void); bool usb_connected(void); -void usb_cdc_background(void); #endif // __MICROPY_INCLUDED_ATMEL_SAMD_USB_H__ From 10b3a901899c4725bf263ad4cda13c58751deccf Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 26 Oct 2017 22:31:56 -0400 Subject: [PATCH 5/6] Check INTERNAL_LIBM make flag in a safer way. --- ports/atmel-samd/Makefile | 8 ++++---- ports/atmel-samd/mpconfigport.mk | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index f9751415a1abe..ef168dbc90b58 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -238,10 +238,10 @@ SRC_C = \ # Choose which flash filesystem impl to use. # (Right now INTERNAL_FLASH_FILESYSTEM and SPI_FLASH_FILESYSTEM are mutually exclusive. # But that might not be true in the future.) -ifdef INTERNAL_FLASH_FILESYSTEM +ifeq ($(INTERNAL_FLASH_FILESYSTEM),1) SRC_C += internal_flash.c endif -ifdef SPI_FLASH_FILESYSTEM +ifeq ($(SPI_FLASH_FILESYSTEM),1) SRC_C += spi_flash.c endif @@ -278,7 +278,7 @@ SRC_COMMON_HAL = \ usb_hid/__init__.c \ usb_hid/Device.c -ifdef INTERNAL_LIBM +ifeq ($(INTERNAL_LIBM),1) SRC_LIBM = $(addprefix lib/,\ libm/math.c \ libm/fmodf.c \ @@ -338,7 +338,7 @@ OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_ASF:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_EXPANDED:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED_MODULE_EXPANDED:.c=.o)) -ifdef INTERNAL_LIBM +ifeq ($(INTERNAL_LIBM),1) OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o)) endif diff --git a/ports/atmel-samd/mpconfigport.mk b/ports/atmel-samd/mpconfigport.mk index acb0916fccb6c..c3b2f22d4f992 100644 --- a/ports/atmel-samd/mpconfigport.mk +++ b/ports/atmel-samd/mpconfigport.mk @@ -3,5 +3,5 @@ # This should correspond to the MICROPY_LONGINT_IMPL definition in mpconfigport.h. MPY_TOOL_LONGINT_IMPL = -mlongint-impl=none -INTERNAL_LIBM = (1) +INTERNAL_LIBM = 1 From 8e45dc041f8d98c62a580b7d6a34768664a972b8 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 26 Oct 2017 22:53:28 -0400 Subject: [PATCH 6/6] Read serial input as a background task so we can check for the interrupt character. --- main.c | 1 + ports/atmel-samd/background.c | 2 ++ ports/atmel-samd/usb.c | 27 +++++++++++++++++++-------- ports/atmel-samd/usb.h | 1 + 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/main.c b/main.c index ba1e0e4013399..a2e4b9f729761 100644 --- a/main.c +++ b/main.c @@ -127,6 +127,7 @@ bool start_mp(safe_mode_t safe_mode) { pyexec_result_t result; bool found_main = false; + if (safe_mode != NO_SAFE_MODE) { serial_write(MSG_SAFE_MODE_NO_MAIN); } else { diff --git a/ports/atmel-samd/background.c b/ports/atmel-samd/background.c index 6515e448b4991..2fdf059823b18 100644 --- a/ports/atmel-samd/background.c +++ b/ports/atmel-samd/background.c @@ -26,9 +26,11 @@ #include "background.h" // #include "common-hal/audioio/AudioOut.h" +#include "usb.h" #include "usb_mass_storage.h" void run_background_tasks(void) { // audioout_background(); usb_msc_background(); + usb_cdc_background(); } diff --git a/ports/atmel-samd/usb.c b/ports/atmel-samd/usb.c index e0b9ddc41ca2f..bd712a5d2c071 100644 --- a/ports/atmel-samd/usb.c +++ b/ports/atmel-samd/usb.c @@ -56,13 +56,13 @@ static uint8_t usb_rx_buf[USB_RX_BUF_SIZE]; // Receive buffer head -static volatile uint8_t usb_rx_buf_head; +static volatile uint8_t usb_rx_buf_head = 0; // Receive buffer tail -static volatile uint8_t usb_rx_buf_tail; +static volatile uint8_t usb_rx_buf_tail = 0; // Number of bytes in receive buffer -volatile uint8_t usb_rx_count; +volatile uint8_t usb_rx_count = 0; volatile bool mp_cdc_enabled = false; volatile bool usb_transmitting = false; @@ -133,8 +133,11 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u uint8_t c = cdc_packet_buffer[i]; if (c == mp_interrupt_char) { mp_keyboard_interrupt(); - // Don't put the interrupt into the buffer, just continue. - continue; + // If interrupted, flush all the input. + usb_rx_count = 0; + usb_rx_buf_head = 0; + usb_rx_buf_tail = 0; + break; } else { // The count of characters present in receive buffer is // incremented. @@ -144,7 +147,7 @@ static bool read_complete(const uint8_t ep, const enum usb_xfer_code rc, const u if (usb_rx_buf_tail == USB_RX_BUF_SIZE) { // Reached the end of buffer, revert back to beginning of // buffer. - usb_rx_buf_tail = 0x00; + usb_rx_buf_tail = 0; } } } @@ -219,8 +222,7 @@ void init_usb(void) { mscdf_register_callback(MSCDF_CB_TEST_DISK_READY, (FUNC_PTR)usb_msc_disk_is_ready); mscdf_register_callback(MSCDF_CB_XFER_BLOCKS_DONE, (FUNC_PTR)usb_msc_xfer_done); - int32_t result = usbdc_start(&multi_desc); - while (result != ERR_NONE) {} + usbdc_start(&multi_desc); usbdc_attach(); } @@ -315,3 +317,12 @@ void usb_write(const char* buffer, uint32_t len) { bool usb_connected(void) { return cdc_enabled(); } + +// Poll for input if keyboard interrupts are enabled, +// so that we can check for the interrupt char. read_complete() does the checking. +void usb_cdc_background() { + // + if (mp_interrupt_char != -1 && cdc_enabled() && !pending_read) { + start_read(); + } +} diff --git a/ports/atmel-samd/usb.h b/ports/atmel-samd/usb.h index 754331211d6a1..45c05df9c0290 100644 --- a/ports/atmel-samd/usb.h +++ b/ports/atmel-samd/usb.h @@ -37,5 +37,6 @@ int usb_read(void); void usb_write(const char* buffer, uint32_t len); bool usb_bytes_available(void); bool usb_connected(void); +void usb_cdc_background(void); #endif // __MICROPY_INCLUDED_ATMEL_SAMD_USB_H__