@@ -55,9 +55,8 @@ mp_obj_t common_hal_board_get_i2c(void) {
5555}
5656
5757mp_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
8180mp_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