Merge pull request #3603 from cwalther/boardspi

Fix lost board.SPI and board.I2C
This commit is contained in:
Scott Shawcroft 2020-10-27 13:53:05 -07:00 committed by GitHub
commit 563a8934b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 15 deletions

4
main.c
View File

@ -234,10 +234,12 @@ void cleanup_after_vm(supervisor_allocation* heap) {
common_hal_canio_reset(); common_hal_canio_reset();
#endif #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 #if CIRCUITPY_BOARD
reset_board_busses(); reset_board_busses();
#endif #endif
reset_port();
reset_board(); reset_board();
reset_status_led(); reset_status_led();
} }

View File

@ -28,6 +28,12 @@
#include "py/runtime.h" #include "py/runtime.h"
#include "shared-bindings/board/__init__.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 //| """Board specific pin names
//| //|
@ -45,7 +51,7 @@
#if BOARD_I2C #if BOARD_I2C
mp_obj_t board_i2c(void) { mp_obj_t board_i2c(void) {
mp_obj_t singleton = common_hal_board_get_i2c(); mp_obj_t singleton = common_hal_board_get_i2c();
if (singleton != NULL) { if (singleton != NULL && !common_hal_busio_i2c_deinited(singleton)) {
return singleton; return singleton;
} }
assert_pin_free(DEFAULT_I2C_BUS_SDA); assert_pin_free(DEFAULT_I2C_BUS_SDA);
@ -69,7 +75,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c);
#if BOARD_SPI #if BOARD_SPI
mp_obj_t board_spi(void) { mp_obj_t board_spi(void) {
mp_obj_t singleton = common_hal_board_get_spi(); mp_obj_t singleton = common_hal_board_get_spi();
if (singleton != NULL) { if (singleton != NULL && !common_hal_busio_spi_deinited(singleton)) {
return singleton; return singleton;
} }
assert_pin_free(DEFAULT_SPI_BUS_SCK); assert_pin_free(DEFAULT_SPI_BUS_SCK);

View File

@ -55,9 +55,8 @@ mp_obj_t common_hal_board_get_i2c(void) {
} }
mp_obj_t common_hal_board_create_i2c(void) { mp_obj_t common_hal_board_create_i2c(void) {
if (i2c_singleton != NULL) { // All callers have either already verified this or come so early that it can't be otherwise.
return i2c_singleton; assert(i2c_singleton == NULL || common_hal_busio_i2c_deinited(i2c_singleton));
}
busio_i2c_obj_t *self = &i2c_obj; busio_i2c_obj_t *self = &i2c_obj;
self->base.type = &busio_i2c_type; self->base.type = &busio_i2c_type;
@ -79,9 +78,8 @@ mp_obj_t common_hal_board_get_spi(void) {
} }
mp_obj_t common_hal_board_create_spi(void) { mp_obj_t common_hal_board_create_spi(void) {
if (spi_singleton != NULL) { // All callers have either already verified this or come so early that it can't be otherwise.
return spi_singleton; assert(spi_singleton == NULL || common_hal_busio_spi_deinited(spi_singleton));
}
busio_spi_obj_t *self = &spi_obj; busio_spi_obj_t *self = &spi_obj;
self->base.type = &busio_spi_type; self->base.type = &busio_spi_type;
@ -139,15 +137,18 @@ void reset_board_busses(void) {
bool display_using_i2c = false; bool display_using_i2c = false;
#if CIRCUITPY_DISPLAYIO #if CIRCUITPY_DISPLAYIO
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { 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; display_using_i2c = true;
break; break;
} }
} }
#endif #endif
if (i2c_singleton != NULL) {
if (!display_using_i2c) { if (!display_using_i2c) {
common_hal_busio_i2c_deinit(i2c_singleton);
i2c_singleton = NULL; i2c_singleton = NULL;
} }
}
#endif #endif
#if BOARD_SPI #if BOARD_SPI
bool display_using_spi = false; bool display_using_spi = false;
@ -169,10 +170,11 @@ void reset_board_busses(void) {
// make sure SPI lock is not held over a soft reset // make sure SPI lock is not held over a soft reset
if (spi_singleton != NULL) { if (spi_singleton != NULL) {
common_hal_busio_spi_unlock(spi_singleton); common_hal_busio_spi_unlock(spi_singleton);
}
if (!display_using_spi) { if (!display_using_spi) {
common_hal_busio_spi_deinit(spi_singleton);
spi_singleton = NULL; spi_singleton = NULL;
} }
}
#endif #endif
#if BOARD_UART #if BOARD_UART
MP_STATE_VM(shared_uart_bus) = NULL; MP_STATE_VM(shared_uart_bus) = NULL;