@@ -115,6 +115,7 @@ static void spi_bus_intr_disable(void *self)
115
115
void common_hal_busio_spi_construct (busio_spi_obj_t * self ,
116
116
const mcu_pin_obj_t * clock , const mcu_pin_obj_t * mosi ,
117
117
const mcu_pin_obj_t * miso ) {
118
+
118
119
spi_bus_config_t bus_config ;
119
120
bus_config .mosi_io_num = mosi != NULL ? mosi -> number : -1 ;
120
121
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) {
212
213
spi_never_reset [self -> host_id ] = true;
213
214
214
215
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
+ }
217
222
}
218
223
219
224
bool 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) {
236
241
spi_bus_free (self -> host_id );
237
242
238
243
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
+ }
241
250
self -> clock_pin = NULL ;
251
+ self -> MISO_pin = NULL ;
252
+ self -> MOSI_pin = NULL ;
242
253
}
243
254
244
255
bool 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) {
293
304
294
305
bool common_hal_busio_spi_write (busio_spi_obj_t * self ,
295
306
const uint8_t * data , size_t len ) {
307
+ if (self -> MOSI_pin == NULL ) {
308
+ mp_raise_ValueError (translate ("No MOSI Pin" ));
309
+ }
296
310
return common_hal_busio_spi_transfer (self , data , NULL , len );
297
311
}
298
312
299
313
bool common_hal_busio_spi_read (busio_spi_obj_t * self ,
300
314
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
+ }
302
325
}
303
326
304
327
bool common_hal_busio_spi_transfer (busio_spi_obj_t * self , const uint8_t * data_out , uint8_t * data_in , size_t len ) {
305
328
if (len == 0 ) {
306
329
return true;
307
330
}
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
+ }
308
338
309
339
spi_hal_context_t * hal = & self -> hal_context ;
310
340
hal -> send_buffer = NULL ;
0 commit comments