Skip to content

Commit 35e3394

Browse files
committed
Fix lost board.SPI and board.I2C after explicitly deiniting them.
After calling board.SPI().deinit(), calling board.SPI() again would return the unusable deinited object and there was no way of getting it back into an initialized state until the end of the session.
1 parent 3d53e62 commit 35e3394

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

shared-bindings/board/__init__.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
#include "py/runtime.h"
2929

3030
#include "shared-bindings/board/__init__.h"
31+
#if BOARD_I2C
32+
#include "shared-bindings/busio/I2C.h"
33+
#endif
34+
#if BOARD_SPI
35+
#include "shared-bindings/busio/SPI.h"
36+
#endif
3137

3238
//| """Board specific pin names
3339
//|
@@ -45,7 +51,7 @@
4551
#if BOARD_I2C
4652
mp_obj_t board_i2c(void) {
4753
mp_obj_t singleton = common_hal_board_get_i2c();
48-
if (singleton != NULL) {
54+
if (singleton != NULL && !common_hal_busio_i2c_deinited(singleton)) {
4955
return singleton;
5056
}
5157
assert_pin_free(DEFAULT_I2C_BUS_SDA);
@@ -69,7 +75,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c);
6975
#if BOARD_SPI
7076
mp_obj_t board_spi(void) {
7177
mp_obj_t singleton = common_hal_board_get_spi();
72-
if (singleton != NULL) {
78+
if (singleton != NULL && !common_hal_busio_spi_deinited(singleton)) {
7379
return singleton;
7480
}
7581
assert_pin_free(DEFAULT_SPI_BUS_SCK);

shared-module/board/__init__.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ mp_obj_t common_hal_board_get_i2c(void) {
5555
}
5656

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

@@ -79,9 +78,8 @@ mp_obj_t common_hal_board_get_spi(void) {
7978
}
8079

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

0 commit comments

Comments
 (0)