Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track creation of board.I2C(), etc. #5951

Merged
merged 2 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/arduino_mkrzero/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ CHIP_FAMILY = samd21
INTERNAL_FLASH_FILESYSTEM = 1
LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

CIRCUITPY_ONEWIREIO = 0
2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/sparkfun_samd21_dev/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ CHIP_FAMILY = samd21
INTERNAL_FLASH_FILESYSTEM = 1
LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

CIRCUITPY_ONEWIREIO = 0
23 changes: 17 additions & 6 deletions shared-module/board/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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++) {
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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++) {
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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;
}
}
}
Expand All @@ -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
}