@@ -106,7 +106,7 @@ typedef struct _machine_hw_spi_bus_obj_t {
106
106
int8_t sck ;
107
107
int8_t mosi ;
108
108
int8_t miso ;
109
- uint8_t active_devices ;
109
+ int16_t active_devices ;
110
110
enum {
111
111
MACHINE_HW_SPI_STATE_NONE ,
112
112
MACHINE_HW_SPI_STATE_INIT ,
@@ -299,7 +299,7 @@ STATIC void machine_hw_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_
299
299
300
300
mp_obj_t machine_hw_spi_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * all_args ) {
301
301
302
- enum { ARG_id , ARG_baudrate , ARG_polarity , ARG_phase , ARG_bits , ARG_firstbit , ARG_sck , ARG_mosi , ARG_miso ARG_cs };
302
+ enum { ARG_id , ARG_baudrate , ARG_polarity , ARG_phase , ARG_bits , ARG_firstbit , ARG_sck , ARG_mosi , ARG_miso , ARG_cs };
303
303
static const mp_arg_t allowed_args [] = {
304
304
{ MP_QSTR_id , MP_ARG_REQUIRED | MP_ARG_INT , {.u_int = -1 } },
305
305
{ MP_QSTR_baudrate , MP_ARG_INT , {.u_int = 500000 } },
@@ -327,6 +327,7 @@ mp_obj_t machine_hw_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_
327
327
default_pins = & machine_hw_spi_default_pins [spi_id - 1 ];
328
328
} else {
329
329
mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("SPI(%d) doesn't exist" ), spi_id );
330
+ return mp_const_none ;
330
331
}
331
332
332
333
self -> base .type = & machine_spi_type ;
@@ -366,15 +367,18 @@ mp_obj_t machine_hw_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_
366
367
cs = machine_pin_get_id (args [ARG_cs ].u_obj );
367
368
}
368
369
370
+ esp_err_t ret ;
371
+
369
372
if (self -> spi_bus -> state == MACHINE_HW_SPI_STATE_INIT ) {
370
373
if (self -> spi_bus -> sck != sck || self -> spi_bus -> miso != miso || self -> spi_bus -> mosi != mosi ) {
371
374
mp_raise_msg (& mp_type_OSError , MP_ERROR_TEXT ("bus already initilized using different pins" ));
375
+ return mp_const_none ;
372
376
}
373
377
} else {
374
- self -> spi_bus -> host = args [ARG_id ].u_int
375
- self -> spi_bus -> miso = miso
376
- self -> spi_bus -> mosi = mosi
377
- self -> spi_bus -> sck = sck
378
+ self -> spi_bus -> host = args [ARG_id ].u_int ;
379
+ self -> spi_bus -> miso = miso ;
380
+ self -> spi_bus -> mosi = mosi ;
381
+ self -> spi_bus -> sck = sck ;
378
382
379
383
spi_bus_config_t buscfg = {
380
384
.miso_io_num = miso ,
@@ -400,7 +404,7 @@ mp_obj_t machine_hw_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_
400
404
switch (ret ) {
401
405
case ESP_ERR_INVALID_ARG :
402
406
mp_raise_msg (& mp_type_OSError , MP_ERROR_TEXT ("invalid configuration" ));
403
- return ;
407
+ return mp_const_none ;
404
408
405
409
case ESP_ERR_INVALID_STATE :
406
410
break ;
@@ -429,15 +433,15 @@ mp_obj_t machine_hw_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_
429
433
switch (ret ) {
430
434
case ESP_ERR_INVALID_ARG :
431
435
mp_raise_msg (& mp_type_OSError , MP_ERROR_TEXT ("invalid configuration" ));
432
- return ;
436
+ return mp_const_none ;
433
437
434
438
case ESP_ERR_NO_MEM :
435
439
mp_raise_msg (& mp_type_OSError , MP_ERROR_TEXT ("out of memory" ));
436
- return ;
440
+ return mp_const_none ;
437
441
438
442
case ESP_ERR_NOT_FOUND :
439
443
mp_raise_msg (& mp_type_OSError , MP_ERROR_TEXT ("no free slots" ));
440
- return ;
444
+ return mp_const_none ;
441
445
}
442
446
443
447
self -> spi_bus -> active_devices ++ ;
0 commit comments