@@ -115,6 +115,7 @@ static void spi_bus_intr_disable(void *self)
115115void common_hal_busio_spi_construct (busio_spi_obj_t * self ,
116116 const mcu_pin_obj_t * clock , const mcu_pin_obj_t * mosi ,
117117 const mcu_pin_obj_t * miso ) {
118+
118119 spi_bus_config_t bus_config ;
119120 bus_config .mosi_io_num = mosi != NULL ? mosi -> number : -1 ;
120121 bus_config .miso_io_num = miso != NULL ? miso -> number : -1 ;
@@ -212,8 +213,12 @@ void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
212213 spi_never_reset [self -> host_id ] = true;
213214
214215 common_hal_never_reset_pin (self -> clock_pin );
215- common_hal_never_reset_pin (self -> MOSI_pin );
216- common_hal_never_reset_pin (self -> MISO_pin );
216+ if (self -> MOSI_pin != NULL ) {
217+ common_hal_never_reset_pin (self -> MOSI_pin );
218+ }
219+ if (self -> MISO_pin != NULL ) {
220+ common_hal_never_reset_pin (self -> MISO_pin );
221+ }
217222}
218223
219224bool common_hal_busio_spi_deinited (busio_spi_obj_t * self ) {
@@ -236,9 +241,15 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
236241 spi_bus_free (self -> host_id );
237242
238243 common_hal_reset_pin (self -> clock_pin );
239- common_hal_reset_pin (self -> MOSI_pin );
240- common_hal_reset_pin (self -> MISO_pin );
244+ if (self -> MOSI_pin != NULL ) {
245+ common_hal_reset_pin (self -> MOSI_pin );
246+ }
247+ if (self -> MISO_pin != NULL ) {
248+ common_hal_reset_pin (self -> MISO_pin );
249+ }
241250 self -> clock_pin = NULL ;
251+ self -> MISO_pin = NULL ;
252+ self -> MOSI_pin = NULL ;
242253}
243254
244255bool common_hal_busio_spi_configure (busio_spi_obj_t * self ,
@@ -293,18 +304,37 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
293304
294305bool common_hal_busio_spi_write (busio_spi_obj_t * self ,
295306 const uint8_t * data , size_t len ) {
307+ if (self -> MOSI_pin == NULL ) {
308+ mp_raise_ValueError (translate ("No MOSI Pin" ));
309+ }
296310 return common_hal_busio_spi_transfer (self , data , NULL , len );
297311}
298312
299313bool common_hal_busio_spi_read (busio_spi_obj_t * self ,
300314 uint8_t * data , size_t len , uint8_t write_value ) {
301- return common_hal_busio_spi_transfer (self , NULL , data , len );
315+
316+ if (self -> MISO_pin == NULL ) {
317+ mp_raise_ValueError (translate ("No MISO Pin" ));
318+ }
319+ if (self -> MOSI_pin == NULL ) {
320+ return common_hal_busio_spi_transfer (self , NULL , data , len );
321+ } else {
322+ memset (data , write_value , len );
323+ return common_hal_busio_spi_transfer (self , data , data , len );
324+ }
302325}
303326
304327bool common_hal_busio_spi_transfer (busio_spi_obj_t * self , const uint8_t * data_out , uint8_t * data_in , size_t len ) {
305328 if (len == 0 ) {
306329 return true;
307330 }
331+ // Other than the read special case, stop transfers that don't have a pin/array match
332+ if (!self -> MOSI_pin && (data_out != data_in )) {
333+ mp_raise_ValueError (translate ("No MOSI Pin" ));
334+ }
335+ if (!self -> MISO_pin && data_in ) {
336+ mp_raise_ValueError (translate ("No MISO Pin" ));
337+ }
308338
309339 spi_hal_context_t * hal = & self -> hal_context ;
310340 hal -> send_buffer = NULL ;
0 commit comments