@@ -201,12 +201,18 @@ STATIC inline bool in_master_mode(pyb_i2c_obj_t *self) { return self->i2c->Init.
201201STATIC const pyb_i2c_obj_t pyb_i2c_obj [] = {
202202 #if defined(MICROPY_HW_I2C1_SCL )
203203 {{& pyb_i2c_type }, & I2CHandle1 },
204+ #else
205+ {{& pyb_i2c_type }, NULL },
204206 #endif
205207 #if defined(MICROPY_HW_I2C2_SCL )
206208 {{& pyb_i2c_type }, & I2CHandle2 },
209+ #else
210+ {{& pyb_i2c_type }, NULL },
207211 #endif
208212 #if defined(MICROPY_HW_I2C3_SCL )
209213 {{& pyb_i2c_type }, & I2CHandle3 },
214+ #else
215+ {{& pyb_i2c_type }, NULL },
210216 #endif
211217};
212218
@@ -297,16 +303,38 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
297303 // check arguments
298304 mp_arg_check_num (n_args , n_kw , 1 , MP_OBJ_FUN_ARGS_MAX , true);
299305
300- // get i2c number
301- mp_int_t i2c_id = mp_obj_get_int (args [0 ]) - 1 ;
302-
303- // check i2c number
304- if (!(0 <= i2c_id && i2c_id < MP_ARRAY_SIZE (pyb_i2c_obj ) && pyb_i2c_obj [i2c_id ].i2c != NULL )) {
305- nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ValueError , "I2C bus %d does not exist" , i2c_id + 1 ));
306+ // work out i2c bus
307+ int i2c_id = 0 ;
308+ if (MP_OBJ_IS_STR (args [0 ])) {
309+ const char * port = mp_obj_str_get_str (args [0 ]);
310+ if (0 ) {
311+ #ifdef MICROPY_HW_I2C1_NAME
312+ } else if (strcmp (port , MICROPY_HW_I2C1_NAME ) == 0 ) {
313+ i2c_id = 1 ;
314+ #endif
315+ #ifdef MICROPY_HW_I2C2_NAME
316+ } else if (strcmp (port , MICROPY_HW_I2C2_NAME ) == 0 ) {
317+ i2c_id = 2 ;
318+ #endif
319+ #ifdef MICROPY_HW_I2C3_NAME
320+ } else if (strcmp (port , MICROPY_HW_I2C3_NAME ) == 0 ) {
321+ i2c_id = 3 ;
322+ #endif
323+ } else {
324+ nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ValueError ,
325+ "I2C(%s) does not exist" , port ));
326+ }
327+ } else {
328+ i2c_id = mp_obj_get_int (args [0 ]);
329+ if (i2c_id < 1 || i2c_id > MP_ARRAY_SIZE (pyb_i2c_obj )
330+ || pyb_i2c_obj [i2c_id ].i2c == NULL ) {
331+ nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_ValueError ,
332+ "I2C(%d) does not exist" , i2c_id ));
333+ }
306334 }
307335
308336 // get I2C object
309- const pyb_i2c_obj_t * i2c_obj = & pyb_i2c_obj [i2c_id ];
337+ const pyb_i2c_obj_t * i2c_obj = & pyb_i2c_obj [i2c_id - 1 ];
310338
311339 if (n_args > 1 || n_kw > 0 ) {
312340 // start the peripheral
0 commit comments