@@ -115,6 +115,16 @@ 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
+
119
+ //SCK is not optional. MOSI and MISO are
120
+ if (!clock ) {
121
+ mp_raise_ValueError (translate ("Must provide SCK pin" ));
122
+ }
123
+
124
+ if (!miso && !mosi ) {
125
+ mp_raise_ValueError (translate ("Must provide MISO or MOSI pin" ));
126
+ }
127
+
118
128
spi_bus_config_t bus_config ;
119
129
bus_config .mosi_io_num = mosi != NULL ? mosi -> number : -1 ;
120
130
bus_config .miso_io_num = miso != NULL ? miso -> number : -1 ;
@@ -212,8 +222,12 @@ void common_hal_busio_spi_never_reset(busio_spi_obj_t *self) {
212
222
spi_never_reset [self -> host_id ] = true;
213
223
214
224
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 );
225
+ if (self -> MOSI_pin != NULL ) {
226
+ common_hal_never_reset_pin (self -> MOSI_pin );
227
+ }
228
+ if (self -> MISO_pin != NULL ) {
229
+ common_hal_never_reset_pin (self -> MISO_pin );
230
+ }
217
231
}
218
232
219
233
bool common_hal_busio_spi_deinited (busio_spi_obj_t * self ) {
@@ -236,9 +250,15 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) {
236
250
spi_bus_free (self -> host_id );
237
251
238
252
common_hal_reset_pin (self -> clock_pin );
239
- common_hal_reset_pin (self -> MOSI_pin );
240
- common_hal_reset_pin (self -> MISO_pin );
253
+ if (self -> MOSI_pin != NULL ) {
254
+ common_hal_reset_pin (self -> MOSI_pin );
255
+ }
256
+ if (self -> MISO_pin != NULL ) {
257
+ common_hal_reset_pin (self -> MISO_pin );
258
+ }
241
259
self -> clock_pin = NULL ;
260
+ self -> MISO_pin = NULL ;
261
+ self -> MOSI_pin = NULL ;
242
262
}
243
263
244
264
bool common_hal_busio_spi_configure (busio_spi_obj_t * self ,
@@ -293,18 +313,37 @@ void common_hal_busio_spi_unlock(busio_spi_obj_t *self) {
293
313
294
314
bool common_hal_busio_spi_write (busio_spi_obj_t * self ,
295
315
const uint8_t * data , size_t len ) {
316
+ if (self -> MOSI_pin == NULL ) {
317
+ mp_raise_ValueError (translate ("No MOSI Pin" ));
318
+ }
296
319
return common_hal_busio_spi_transfer (self , data , NULL , len );
297
320
}
298
321
299
322
bool common_hal_busio_spi_read (busio_spi_obj_t * self ,
300
323
uint8_t * data , size_t len , uint8_t write_value ) {
301
- return common_hal_busio_spi_transfer (self , NULL , data , len );
324
+
325
+ if (self -> MISO_pin == NULL ) {
326
+ mp_raise_ValueError (translate ("No MISO Pin" ));
327
+ }
328
+ if (self -> MOSI_pin == NULL ) {
329
+ return common_hal_busio_spi_transfer (self , NULL , data , len );
330
+ } else {
331
+ memset (data , write_value , len );
332
+ return common_hal_busio_spi_transfer (self , data , data , len );
333
+ }
302
334
}
303
335
304
336
bool common_hal_busio_spi_transfer (busio_spi_obj_t * self , const uint8_t * data_out , uint8_t * data_in , size_t len ) {
305
337
if (len == 0 ) {
306
338
return true;
307
339
}
340
+ // Other than the read special case, stop transfers that don't have a pin/array match
341
+ if (!self -> MOSI_pin && (data_out != data_in )) {
342
+ mp_raise_ValueError (translate ("No MOSI Pin" ));
343
+ }
344
+ if (!self -> MISO_pin && data_in ) {
345
+ mp_raise_ValueError (translate ("No MISO Pin" ));
346
+ }
308
347
309
348
spi_hal_context_t * hal = & self -> hal_context ;
310
349
hal -> send_buffer = NULL ;
0 commit comments