Skip to content

Commit e77abc2

Browse files
author
Daniel Campora
committed
cc3200: Default peripheral ID support on I2C.
1 parent c69642a commit e77abc2

5 files changed

Lines changed: 46 additions & 30 deletions

File tree

cc3200/mods/pybi2c.c

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -279,19 +279,7 @@ STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
279279
}
280280
}
281281

282-
/// \method init()
283-
STATIC const mp_arg_t pyb_i2c_init_args[] = {
284-
{ MP_QSTR_mode, MP_ARG_INT, {.u_int = PYBI2C_MASTER} },
285-
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
286-
{ MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
287-
};
288-
#define PYB_I2C_INIT_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_init_args)
289-
290-
STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
291-
// parse args
292-
mp_arg_val_t args[PYB_I2C_INIT_NUM_ARGS];
293-
mp_arg_parse_all(n_args, pos_args, kw_args, PYB_I2C_INIT_NUM_ARGS, pyb_i2c_init_args, args);
294-
282+
STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, mp_arg_val_t *args) {
295283
// verify that mode is master
296284
if (args[0].u_int != PYBI2C_MASTER) {
297285
goto invalid_args;
@@ -329,32 +317,49 @@ STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, mp_uint_t n_args, const
329317
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
330318
}
331319

332-
/// \classmethod \constructor(bus, ...)
333-
STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
334-
// check arguments
335-
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
336-
337-
// setup the object
338-
pyb_i2c_obj_t *self = &pyb_i2c_obj;
339-
self->base.type = &pyb_i2c_type;
320+
STATIC const mp_arg_t pyb_i2c_init_args[] = {
321+
{ MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} },
322+
{ MP_QSTR_mode, MP_ARG_INT, {.u_int = PYBI2C_MASTER} },
323+
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
324+
{ MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
325+
};
326+
#define PYB_I2C_INIT_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_init_args)
327+
STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *all_args) {
328+
// parse args
329+
mp_map_t kw_args;
330+
mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args);
331+
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_init_args)];
332+
mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), pyb_i2c_init_args, args);
333+
334+
// work out the uart id
335+
uint8_t i2c_id;
336+
if (args[0].u_obj == mp_const_none) {
337+
// default id
338+
i2c_id = 0;
339+
} else {
340+
i2c_id = mp_obj_get_int(args[0].u_obj);
341+
}
340342

341343
// check the peripheral id
342-
if (mp_obj_get_int(args[0]) != 0) {
344+
if (i2c_id != 0) {
343345
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
344346
}
345347

346-
if (n_args > 1 || n_kw > 0) {
347-
// start the peripheral
348-
mp_map_t kw_args;
349-
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
350-
pyb_i2c_init_helper(self, n_args - 1, args + 1, &kw_args);
351-
}
348+
// setup the object
349+
pyb_i2c_obj_t *self = &pyb_i2c_obj;
350+
self->base.type = &pyb_i2c_type;
351+
352+
// start the peripheral
353+
pyb_i2c_init_helper(self, &args[1]);
352354

353355
return (mp_obj_t)self;
354356
}
355357

356-
STATIC mp_obj_t pyb_i2c_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
357-
return pyb_i2c_init_helper(args[0], n_args - 1, args + 1, kw_args);
358+
STATIC mp_obj_t pyb_i2c_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
359+
// parse args
360+
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_init_args) - 1];
361+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), &pyb_i2c_init_args[1], args);
362+
return pyb_i2c_init_helper(pos_args[0], args);
358363
}
359364
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_init_obj, 1, pyb_i2c_init);
360365

cc3200/qstrdefsport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ Q(RX_ANY)
153153

154154
// for I2C class
155155
Q(I2C)
156+
Q(id)
156157
Q(mode)
157158
Q(baudrate)
158159
Q(pins)

tests/wipy/i2c.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
else:
1717
raise Exception('Board not supported!')
1818

19+
i2c = I2C(0, I2C.MASTER, baudrate=400000)
20+
# try initing without the peripheral id
21+
i2c = I2C()
22+
print(i2c)
23+
i2c = I2C(mode=I2C.MASTER, baudrate=50000, pins=i2c_pins)
24+
print(i2c)
25+
1926
i2c = I2C(0, I2C.MASTER, baudrate=100000)
2027
print(i2c)
2128
i2c = I2C(0, mode=I2C.MASTER, baudrate=400000)

tests/wipy/i2c.py.exp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
I2C(0, I2C.MASTER, baudrate=100000)
2+
I2C(0, I2C.MASTER, baudrate=50000)
3+
I2C(0, I2C.MASTER, baudrate=100000)
24
I2C(0, I2C.MASTER, baudrate=400000)
35
I2C(0, I2C.MASTER, baudrate=400000)
46
104

tests/wipy/uart.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
uart = UART(baudrate=1000000)
3131
uart.sendbreak()
3232

33+
uart = UART(baudrate=1000000)
3334
uart = UART()
3435
print(uart)
3536
uart = UART(baudrate=38400, pins=('GP12', 'GP13'))

0 commit comments

Comments
 (0)