From 25707d311a5161047a4d2dbc0045a40907290b43 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Sun, 30 Jan 2022 23:12:49 -0500 Subject: [PATCH 1/2] Track creation of board.I2C(), etc. --- shared-module/board/__init__.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/shared-module/board/__init__.c b/shared-module/board/__init__.c index cd599e7448c4..65441025f469 100644 --- a/shared-module/board/__init__.c +++ b/shared-module/board/__init__.c @@ -57,6 +57,7 @@ typedef struct { static const board_i2c_pin_t i2c_pin[CIRCUITPY_BOARD_I2C] = CIRCUITPY_BOARD_I2C_PIN; static busio_i2c_obj_t i2c_obj[CIRCUITPY_BOARD_I2C]; +static bool i2c_obj_created[CIRCUITPY_BOARD_I2C]; bool common_hal_board_is_i2c(mp_obj_t obj) { for (uint8_t instance = 0; instance < CIRCUITPY_BOARD_I2C; instance++) { @@ -68,7 +69,7 @@ bool common_hal_board_is_i2c(mp_obj_t obj) { } mp_obj_t common_hal_board_get_i2c(const mp_int_t instance) { - return &i2c_obj[instance]; + return i2c_obj_created[instance] ? &i2c_obj[instance] : NULL; } mp_obj_t common_hal_board_create_i2c(const mp_int_t instance) { @@ -85,6 +86,7 @@ mp_obj_t common_hal_board_create_i2c(const mp_int_t instance) { common_hal_busio_i2c_construct(self, i2c_pin[instance].scl, i2c_pin[instance].sda, 100000, 255); + i2c_obj_created[instance] = true; return &i2c_obj[instance]; } #endif @@ -101,6 +103,7 @@ typedef struct { static const board_spi_pin_t spi_pin[CIRCUITPY_BOARD_SPI] = CIRCUITPY_BOARD_SPI_PIN; static busio_spi_obj_t spi_obj[CIRCUITPY_BOARD_SPI]; +static bool spi_obj_created[CIRCUITPY_BOARD_SPI]; bool common_hal_board_is_spi(mp_obj_t obj) { for (uint8_t instance = 0; instance < CIRCUITPY_BOARD_SPI; instance++) { @@ -112,7 +115,7 @@ bool common_hal_board_is_spi(mp_obj_t obj) { } mp_obj_t common_hal_board_get_spi(const mp_int_t instance) { - return &spi_obj[instance]; + return spi_obj_created[instance] ? &spi_obj[instance] : NULL; } mp_obj_t common_hal_board_create_spi(const mp_int_t instance) { @@ -130,6 +133,7 @@ mp_obj_t common_hal_board_create_spi(const mp_int_t instance) { common_hal_busio_spi_construct(self, spi_pin[instance].clock, spi_pin[instance].mosi, spi_pin[instance].miso); + spi_obj_created[instance] = true; return &spi_obj[instance]; } #endif @@ -143,6 +147,7 @@ typedef struct { static const board_uart_pin_t uart_pin[CIRCUITPY_BOARD_UART] = CIRCUITPY_BOARD_UART_PIN; static busio_uart_obj_t uart_obj[CIRCUITPY_BOARD_UART]; +static bool uart_obj_created[CIRCUITPY_BOARD_UART]; bool common_hal_board_is_uart(mp_obj_t obj) { for (uint8_t instance = 0; instance < CIRCUITPY_BOARD_UART; instance++) { @@ -154,7 +159,7 @@ bool common_hal_board_is_uart(mp_obj_t obj) { } mp_obj_t common_hal_board_get_uart(const mp_int_t instance) { - return &uart_obj[instance]; + return uart_obj_created[instance] ? &uart_obj[instance] : NULL; } mp_obj_t common_hal_board_create_uart(const mp_int_t instance) { @@ -174,6 +179,7 @@ mp_obj_t common_hal_board_create_uart(const mp_int_t instance) { common_hal_busio_uart_construct(self, uart_pin[instance].tx, uart_pin[instance].rx, NULL, NULL, NULL, false, 9600, 8, BUSIO_UART_PARITY_NONE, 1, 1.0f, 64, NULL, false); + uart_obj_created[instance] = true; return &uart_obj[instance]; } #endif @@ -190,11 +196,12 @@ void reset_board_buses(void) { } } #endif - if (!common_hal_busio_i2c_deinited(&i2c_obj[instance])) { + if (i2c_obj_created[instance]) { // make sure I2C lock is not held over a soft reset common_hal_busio_i2c_unlock(&i2c_obj[instance]); if (!display_using_i2c) { common_hal_busio_i2c_deinit(&i2c_obj[instance]); + i2c_obj_created[instance] = false; } } } @@ -217,18 +224,22 @@ void reset_board_buses(void) { #endif } #endif - if (!common_hal_busio_spi_deinited(&spi_obj[instance])) { + if (spi_obj_created[instance]) { // make sure SPI lock is not held over a soft reset common_hal_busio_spi_unlock(&spi_obj[instance]); if (!display_using_spi) { common_hal_busio_spi_deinit(&spi_obj[instance]); + spi_obj_created[instance] = false; } } } #endif #if CIRCUITPY_BOARD_UART for (uint8_t instance = 0; instance < CIRCUITPY_BOARD_UART; instance++) { - common_hal_busio_uart_deinit(&uart_obj[instance]); + if (uart_obj_created[instance]) { + common_hal_busio_uart_deinit(&uart_obj[instance]); + uart_obj_created[instance] = false; + } } #endif } From 20a17bec25ed063a637e9e8c470cdb4efe843f0e Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Mon, 31 Jan 2022 09:14:12 -0500 Subject: [PATCH 2/2] shrink arduino_mkrzero and sparkfun_samd21_dev --- ports/atmel-samd/boards/arduino_mkrzero/mpconfigboard.mk | 2 ++ ports/atmel-samd/boards/sparkfun_samd21_dev/mpconfigboard.mk | 2 ++ 2 files changed, 4 insertions(+) diff --git a/ports/atmel-samd/boards/arduino_mkrzero/mpconfigboard.mk b/ports/atmel-samd/boards/arduino_mkrzero/mpconfigboard.mk index eea3a27f13e6..2c8910a66ac1 100644 --- a/ports/atmel-samd/boards/arduino_mkrzero/mpconfigboard.mk +++ b/ports/atmel-samd/boards/arduino_mkrzero/mpconfigboard.mk @@ -9,3 +9,5 @@ CHIP_FAMILY = samd21 INTERNAL_FLASH_FILESYSTEM = 1 LONGINT_IMPL = NONE CIRCUITPY_FULL_BUILD = 0 + +CIRCUITPY_ONEWIREIO = 0 diff --git a/ports/atmel-samd/boards/sparkfun_samd21_dev/mpconfigboard.mk b/ports/atmel-samd/boards/sparkfun_samd21_dev/mpconfigboard.mk index b8ac2dc2569c..8d1de11be754 100644 --- a/ports/atmel-samd/boards/sparkfun_samd21_dev/mpconfigboard.mk +++ b/ports/atmel-samd/boards/sparkfun_samd21_dev/mpconfigboard.mk @@ -9,3 +9,5 @@ CHIP_FAMILY = samd21 INTERNAL_FLASH_FILESYSTEM = 1 LONGINT_IMPL = NONE CIRCUITPY_FULL_BUILD = 0 + +CIRCUITPY_ONEWIREIO = 0