Skip to content

Commit 8f205c2

Browse files
committed
cc3200/mods/pybi2c: Make machine.I2C constructor/init conform to HW API.
This is a user-facing change to the cc3200's API, to make it conform to the new machine hardware API. The changes are: - change I2C constructor to: I2C(id=0, *, freq=100000, scl=None, sda=None) - change I2C init to: init(*, freq, scl, sda) - removal of machine.I2C.MASTER constant - I2C str/repr no longer prints I2C.MASTER To update existing code it should be enough to just remove the I2C.MASTER constant from contructor/init for I2C.
1 parent fabaa61 commit 8f205c2

1 file changed

Lines changed: 30 additions & 45 deletions

File tree

cc3200/mods/pybi2c.c

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ typedef struct _pyb_i2c_obj_t {
5959
/******************************************************************************
6060
DEFINE CONSTANTS
6161
******************************************************************************/
62-
#define PYBI2C_MASTER (0)
63-
6462
#define PYBI2C_MIN_BAUD_RATE_HZ (50000)
6563
#define PYBI2C_MAX_BAUD_RATE_HZ (400000)
6664

@@ -79,7 +77,6 @@ typedef struct _pyb_i2c_obj_t {
7977
DECLARE PRIVATE DATA
8078
******************************************************************************/
8179
STATIC pyb_i2c_obj_t pyb_i2c_obj = {.baudrate = 0};
82-
STATIC const mp_obj_t pyb_i2c_def_pin[2] = {&pin_GP13, &pin_GP23};
8380

8481
/******************************************************************************
8582
DECLARE PRIVATE FUNCTIONS
@@ -289,33 +286,34 @@ STATIC void pyb_i2c_readmem_into (mp_arg_val_t *args, vstr_t *vstr) {
289286
STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
290287
pyb_i2c_obj_t *self = self_in;
291288
if (self->baudrate > 0) {
292-
mp_printf(print, "I2C(0, I2C.MASTER, baudrate=%u)", self->baudrate);
289+
mp_printf(print, "I2C(0, baudrate=%u)", self->baudrate);
293290
} else {
294291
mp_print_str(print, "I2C(0)");
295292
}
296293
}
297294

298-
STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, const mp_arg_val_t *args) {
299-
// verify that mode is master
300-
if (args[0].u_int != PYBI2C_MASTER) {
301-
goto invalid_args;
302-
}
295+
STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
296+
enum { ARG_scl, ARG_sda, ARG_freq };
297+
static const mp_arg_t allowed_args[] = {
298+
{ MP_QSTR_scl, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
299+
{ MP_QSTR_sda, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
300+
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
301+
};
302+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
303+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
303304

304305
// make sure the baudrate is between the valid range
305-
self->baudrate = MIN(MAX(args[1].u_int, PYBI2C_MIN_BAUD_RATE_HZ), PYBI2C_MAX_BAUD_RATE_HZ);
306+
self->baudrate = MIN(MAX(args[ARG_freq].u_int, PYBI2C_MIN_BAUD_RATE_HZ), PYBI2C_MAX_BAUD_RATE_HZ);
306307

307308
// assign the pins
308-
mp_obj_t pins_o = args[2].u_obj;
309-
if (pins_o != mp_const_none) {
310-
mp_obj_t *pins;
311-
if (pins_o == MP_OBJ_NULL) {
312-
// use the default pins
313-
pins = (mp_obj_t *)pyb_i2c_def_pin;
314-
} else {
315-
mp_obj_get_array_fixed_n(pins_o, 2, &pins);
316-
}
317-
pin_assign_pins_af (pins, 2, PIN_TYPE_STD_PU, PIN_FN_I2C, 0);
309+
mp_obj_t pins[2] = {&pin_GP13, &pin_GP23}; // default (SDA, SCL) pins
310+
if (args[ARG_scl].u_obj != MP_OBJ_NULL) {
311+
pins[1] = args[ARG_scl].u_obj;
312+
}
313+
if (args[ARG_sda].u_obj != MP_OBJ_NULL) {
314+
pins[0] = args[ARG_sda].u_obj;
318315
}
316+
pin_assign_pins_af(pins, 2, PIN_TYPE_STD_PU, PIN_FN_I2C, 0);
319317

320318
// init the I2C bus
321319
i2c_init(self);
@@ -324,44 +322,34 @@ STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, const mp_arg_val_t *arg
324322
pyb_sleep_add ((const mp_obj_t)self, (WakeUpCB_t)i2c_init);
325323

326324
return mp_const_none;
327-
328-
invalid_args:
329-
mp_raise_ValueError(mpexception_value_invalid_arguments);
330325
}
331326

332-
STATIC const mp_arg_t pyb_i2c_init_args[] = {
333-
{ MP_QSTR_id, MP_ARG_INT, {.u_int = 0} },
334-
{ MP_QSTR_mode, MP_ARG_INT, {.u_int = PYBI2C_MASTER} },
335-
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
336-
{ MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
337-
};
338327
STATIC mp_obj_t pyb_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
328+
// check the id argument, if given
329+
if (n_args > 0) {
330+
if (all_args[0] != MP_OBJ_NEW_SMALL_INT(0)) {
331+
mp_raise_OSError(MP_ENODEV);
332+
}
333+
--n_args;
334+
++all_args;
335+
}
336+
339337
// parse args
340338
mp_map_t kw_args;
341339
mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args);
342-
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_init_args)];
343-
mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), pyb_i2c_init_args, args);
344-
345-
// check the peripheral id
346-
if (args[0].u_int != 0) {
347-
mp_raise_OSError(MP_ENODEV);
348-
}
349340

350341
// setup the object
351342
pyb_i2c_obj_t *self = &pyb_i2c_obj;
352343
self->base.type = &pyb_i2c_type;
353344

354345
// start the peripheral
355-
pyb_i2c_init_helper(self, &args[1]);
346+
pyb_i2c_init_helper(self, n_args, all_args, &kw_args);
356347

357348
return (mp_obj_t)self;
358349
}
359350

360-
STATIC mp_obj_t pyb_i2c_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
361-
// parse args
362-
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_init_args) - 1];
363-
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), &pyb_i2c_init_args[1], args);
364-
return pyb_i2c_init_helper(pos_args[0], args);
351+
STATIC mp_obj_t pyb_i2c_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
352+
return pyb_i2c_init_helper(pos_args[0], n_args - 1, pos_args + 1, kw_args);
365353
}
366354
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_init_obj, 1, pyb_i2c_init);
367355

@@ -532,9 +520,6 @@ STATIC const mp_map_elem_t pyb_i2c_locals_dict_table[] = {
532520
{ MP_OBJ_NEW_QSTR(MP_QSTR_readfrom_mem), (mp_obj_t)&pyb_i2c_readfrom_mem_obj },
533521
{ MP_OBJ_NEW_QSTR(MP_QSTR_readfrom_mem_into), (mp_obj_t)&pyb_i2c_readfrom_mem_into_obj },
534522
{ MP_OBJ_NEW_QSTR(MP_QSTR_writeto_mem), (mp_obj_t)&pyb_i2c_writeto_mem_obj },
535-
536-
// class constants
537-
{ MP_OBJ_NEW_QSTR(MP_QSTR_MASTER), MP_OBJ_NEW_SMALL_INT(PYBI2C_MASTER) },
538523
};
539524

540525
STATIC MP_DEFINE_CONST_DICT(pyb_i2c_locals_dict, pyb_i2c_locals_dict_table);

0 commit comments

Comments
 (0)