@@ -325,7 +325,6 @@ STATIC const pyb_spi_obj_t pyb_spi_obj[] = {
325325 {{& pyb_spi_type }, NULL },
326326#endif
327327};
328- #define PYB_NUM_SPI MP_ARRAY_SIZE(pyb_spi_obj)
329328
330329SPI_HandleTypeDef * spi_get_handle (mp_obj_t o ) {
331330 if (!MP_OBJ_IS_TYPE (o , & pyb_spi_type )) {
@@ -462,16 +461,38 @@ STATIC mp_obj_t pyb_spi_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
462461 // check arguments
463462 mp_arg_check_num (n_args , n_kw , 1 , MP_OBJ_FUN_ARGS_MAX , true);
464463
465- // get SPI number
466- mp_int_t spi_id = mp_obj_get_int (args [0 ]) - 1 ;
467-
468- // check SPI number
469- if (!(0 <= spi_id && spi_id < PYB_NUM_SPI && pyb_spi_obj [spi_id ].spi != NULL )) {
470- nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ValueError , "SPI bus %d does not exist" , spi_id + 1 ));
464+ // work out SPI bus
465+ int spi_id = 0 ;
466+ if (MP_OBJ_IS_STR (args [0 ])) {
467+ const char * port = mp_obj_str_get_str (args [0 ]);
468+ if (0 ) {
469+ #ifdef MICROPY_HW_SPI1_NAME
470+ } else if (strcmp (port , MICROPY_HW_SPI1_NAME ) == 0 ) {
471+ spi_id = 1 ;
472+ #endif
473+ #ifdef MICROPY_HW_SPI2_NAME
474+ } else if (strcmp (port , MICROPY_HW_SPI2_NAME ) == 0 ) {
475+ spi_id = 2 ;
476+ #endif
477+ #ifdef MICROPY_HW_SPI3_NAME
478+ } else if (strcmp (port , MICROPY_HW_SPI3_NAME ) == 0 ) {
479+ spi_id = 3 ;
480+ #endif
481+ } else {
482+ nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ValueError ,
483+ "SPI(%s) does not exist" , port ));
484+ }
485+ } else {
486+ spi_id = mp_obj_get_int (args [0 ]);
487+ if (spi_id < 1 || spi_id > MP_ARRAY_SIZE (pyb_spi_obj )
488+ || pyb_spi_obj [spi_id ].spi == NULL ) {
489+ nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ValueError ,
490+ "SPI(%d) does not exist" , spi_id ));
491+ }
471492 }
472493
473494 // get SPI object
474- const pyb_spi_obj_t * spi_obj = & pyb_spi_obj [spi_id ];
495+ const pyb_spi_obj_t * spi_obj = & pyb_spi_obj [spi_id - 1 ];
475496
476497 if (n_args > 1 || n_kw > 0 ) {
477498 // start the peripheral
0 commit comments