@@ -30,31 +30,8 @@ static SPI0_Type *spi[NUM_SPI] = {SPI0, NULL, NULL};
3030static SPI1_Type * aux_spi [NUM_SPI ] = {NULL , SPI1 , SPI2 };
3131#endif
3232
33- static bool never_reset_spi [NUM_SPI ];
3433static bool spi_in_use [NUM_SPI ];
3534
36- void reset_spi (void ) {
37- for (size_t i = 0 ; i < NUM_SPI ; i ++ ) {
38- if (never_reset_spi [i ]) {
39- continue ;
40- }
41-
42- if (i == 1 || i == 2 ) {
43- if (i == 1 ) {
44- AUX -> ENABLES_b .SPI_1 = false;
45- } else {
46- AUX -> ENABLES_b .SPI_2 = false;
47- }
48- aux_spi [i ]-> CNTL0 = 0 ;
49- } else {
50- // Set CS back to default. All 0 except read enable.
51- spi [i ]-> CS = SPI0_CS_REN_Msk ;
52- }
53-
54- spi_in_use [i ] = false;
55- }
56- }
57-
5835void common_hal_busio_spi_construct (busio_spi_obj_t * self ,
5936 const mcu_pin_obj_t * clock , const mcu_pin_obj_t * mosi ,
6037 const mcu_pin_obj_t * miso , bool half_duplex ) {
@@ -67,6 +44,9 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
6744 mp_raise_NotImplementedError (MP_ERROR_TEXT ("Half duplex SPI is not implemented" ));
6845 }
6946
47+ // Ensure the object starts in its deinit state.
48+ common_hal_busio_spi_mark_deinit (self );
49+
7050 // BCM_VERSION != 2711 have 3 SPI but as listed in peripherals/gen/pins.c two are on
7151 // index 0, once one index 0 SPI is found the other will throw an invalid_pins error.
7252 for (size_t i = 0 ; i < NUM_SPI ; i ++ ) {
@@ -118,8 +98,6 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
11898}
11999
120100void common_hal_busio_spi_never_reset (busio_spi_obj_t * self ) {
121- never_reset_spi [self -> index ] = true;
122-
123101 common_hal_never_reset_pin (self -> clock );
124102 common_hal_never_reset_pin (self -> MOSI );
125103 common_hal_never_reset_pin (self -> MISO );
@@ -129,16 +107,19 @@ bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) {
129107 return self -> clock == NULL ;
130108}
131109
110+ void common_hal_busio_spi_mark_deinit (busio_spi_obj_t * self ) {
111+ self -> clock = NULL ;
112+ }
113+
132114void common_hal_busio_spi_deinit (busio_spi_obj_t * self ) {
133115 if (common_hal_busio_spi_deinited (self )) {
134116 return ;
135117 }
136- never_reset_spi [self -> index ] = false;
137118
138119 common_hal_reset_pin (self -> clock );
139120 common_hal_reset_pin (self -> MOSI );
140121 common_hal_reset_pin (self -> MISO );
141- self -> clock = NULL ;
122+
142123 spi_in_use [self -> index ] = false;
143124
144125 if (self -> index == 1 ||
@@ -149,7 +130,12 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
149130 } else if (self -> index == 2 ) {
150131 AUX -> ENABLES_b .SPI_2 = false;
151132 }
133+ } else {
134+ // Set CS back to default. All 0 except read enable.
135+ spi [self -> index ]-> CS = SPI0_CS_REN_Msk ;
152136 }
137+
138+ common_hal_busio_spi_mark_deinit (self );
153139}
154140
155141bool common_hal_busio_spi_configure (busio_spi_obj_t * self ,
0 commit comments