Skip to content

Fix lost board.SPI and board.I2C #3603

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

Merged
merged 3 commits into from
Oct 27, 2020
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
4 changes: 3 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,12 @@ void cleanup_after_vm(supervisor_allocation* heap) {
common_hal_canio_reset();
#endif

reset_port();
// reset_board_busses() first because it may release pins from the never_reset state, so that
// reset_port() can reset them.
#if CIRCUITPY_BOARD
reset_board_busses();
#endif
reset_port();
reset_board();
reset_status_led();
}
Expand Down
10 changes: 8 additions & 2 deletions shared-bindings/board/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
#include "py/runtime.h"

#include "shared-bindings/board/__init__.h"
#if BOARD_I2C
#include "shared-bindings/busio/I2C.h"
#endif
#if BOARD_SPI
#include "shared-bindings/busio/SPI.h"
#endif

//| """Board specific pin names
//|
Expand All @@ -45,7 +51,7 @@
#if BOARD_I2C
mp_obj_t board_i2c(void) {
mp_obj_t singleton = common_hal_board_get_i2c();
if (singleton != NULL) {
if (singleton != NULL && !common_hal_busio_i2c_deinited(singleton)) {
return singleton;
}
assert_pin_free(DEFAULT_I2C_BUS_SDA);
Expand All @@ -69,7 +75,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c);
#if BOARD_SPI
mp_obj_t board_spi(void) {
mp_obj_t singleton = common_hal_board_get_spi();
if (singleton != NULL) {
if (singleton != NULL && !common_hal_busio_spi_deinited(singleton)) {
return singleton;
}
assert_pin_free(DEFAULT_SPI_BUS_SCK);
Expand Down
26 changes: 14 additions & 12 deletions shared-module/board/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ mp_obj_t common_hal_board_get_i2c(void) {
}

mp_obj_t common_hal_board_create_i2c(void) {
if (i2c_singleton != NULL) {
return i2c_singleton;
}
// All callers have either already verified this or come so early that it can't be otherwise.
assert(i2c_singleton == NULL || common_hal_busio_i2c_deinited(i2c_singleton));
busio_i2c_obj_t *self = &i2c_obj;
self->base.type = &busio_i2c_type;

Expand All @@ -79,9 +78,8 @@ mp_obj_t common_hal_board_get_spi(void) {
}

mp_obj_t common_hal_board_create_spi(void) {
if (spi_singleton != NULL) {
return spi_singleton;
}
// All callers have either already verified this or come so early that it can't be otherwise.
assert(spi_singleton == NULL || common_hal_busio_spi_deinited(spi_singleton));
busio_spi_obj_t *self = &spi_obj;
self->base.type = &busio_spi_type;

Expand Down Expand Up @@ -139,14 +137,17 @@ void reset_board_busses(void) {
bool display_using_i2c = false;
#if CIRCUITPY_DISPLAYIO
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
if (displays[i].i2cdisplay_bus.bus == i2c_singleton) {
if (displays[i].bus_base.type == &displayio_i2cdisplay_type && displays[i].i2cdisplay_bus.bus == i2c_singleton) {
display_using_i2c = true;
break;
}
}
#endif
if (!display_using_i2c) {
i2c_singleton = NULL;
if (i2c_singleton != NULL) {
if (!display_using_i2c) {
common_hal_busio_i2c_deinit(i2c_singleton);
i2c_singleton = NULL;
}
}
#endif
#if BOARD_SPI
Expand All @@ -169,9 +170,10 @@ void reset_board_busses(void) {
// make sure SPI lock is not held over a soft reset
if (spi_singleton != NULL) {
common_hal_busio_spi_unlock(spi_singleton);
}
if (!display_using_spi) {
spi_singleton = NULL;
if (!display_using_spi) {
common_hal_busio_spi_deinit(spi_singleton);
spi_singleton = NULL;
}
}
#endif
#if BOARD_UART
Expand Down