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 }