Skip to content

Commit 563a893

Browse files
authored
Merge pull request #3603 from cwalther/boardspi
Fix lost board.SPI and board.I2C
2 parents fa35c3f + 99a3750 commit 563a893

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,12 @@ void cleanup_after_vm(supervisor_allocation* heap) {
234234
common_hal_canio_reset();
235235
#endif
236236

237-
reset_port();
237+
// reset_board_busses() first because it may release pins from the never_reset state, so that
238+
// reset_port() can reset them.
238239
#if CIRCUITPY_BOARD
239240
reset_board_busses();
240241
#endif
242+
reset_port();
241243
reset_board();
242244
reset_status_led();
243245
}

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: 14 additions & 12 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

@@ -139,14 +137,17 @@ void reset_board_busses(void) {
139137
bool display_using_i2c = false;
140138
#if CIRCUITPY_DISPLAYIO
141139
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
142-
if (displays[i].i2cdisplay_bus.bus == i2c_singleton) {
140+
if (displays[i].bus_base.type == &displayio_i2cdisplay_type && displays[i].i2cdisplay_bus.bus == i2c_singleton) {
143141
display_using_i2c = true;
144142
break;
145143
}
146144
}
147145
#endif
148-
if (!display_using_i2c) {
149-
i2c_singleton = NULL;
146+
if (i2c_singleton != NULL) {
147+
if (!display_using_i2c) {
148+
common_hal_busio_i2c_deinit(i2c_singleton);
149+
i2c_singleton = NULL;
150+
}
150151
}
151152
#endif
152153
#if BOARD_SPI
@@ -169,9 +170,10 @@ void reset_board_busses(void) {
169170
// make sure SPI lock is not held over a soft reset
170171
if (spi_singleton != NULL) {
171172
common_hal_busio_spi_unlock(spi_singleton);
172-
}
173-
if (!display_using_spi) {
174-
spi_singleton = NULL;
173+
if (!display_using_spi) {
174+
common_hal_busio_spi_deinit(spi_singleton);
175+
spi_singleton = NULL;
176+
}
175177
}
176178
#endif
177179
#if BOARD_UART

0 commit comments

Comments
 (0)