cc3200: Default peripheral ID support on I2C.

This commit is contained in:
Daniel Campora 2015-09-11 11:05:58 +02:00
parent c69642a460
commit e77abc261b
5 changed files with 46 additions and 30 deletions

View File

@ -279,19 +279,7 @@ STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
} }
} }
/// \method init() STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, mp_arg_val_t *args) {
STATIC const mp_arg_t pyb_i2c_init_args[] = {
{ MP_QSTR_mode, MP_ARG_INT, {.u_int = PYBI2C_MASTER} },
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
{ MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
};
#define PYB_I2C_INIT_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_init_args)
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) {
// parse args
mp_arg_val_t args[PYB_I2C_INIT_NUM_ARGS];
mp_arg_parse_all(n_args, pos_args, kw_args, PYB_I2C_INIT_NUM_ARGS, pyb_i2c_init_args, args);
// verify that mode is master // verify that mode is master
if (args[0].u_int != PYBI2C_MASTER) { if (args[0].u_int != PYBI2C_MASTER) {
goto invalid_args; goto invalid_args;
@ -329,32 +317,49 @@ invalid_args:
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
} }
/// \classmethod \constructor(bus, ...) STATIC const mp_arg_t pyb_i2c_init_args[] = {
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) { { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} },
// check arguments { MP_QSTR_mode, MP_ARG_INT, {.u_int = PYBI2C_MASTER} },
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
{ MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
};
#define PYB_I2C_INIT_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_init_args)
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) {
// parse args
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args);
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_init_args)];
mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), pyb_i2c_init_args, args);
// work out the uart id
uint8_t i2c_id;
if (args[0].u_obj == mp_const_none) {
// default id
i2c_id = 0;
} else {
i2c_id = mp_obj_get_int(args[0].u_obj);
}
// check the peripheral id
if (i2c_id != 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
}
// setup the object // setup the object
pyb_i2c_obj_t *self = &pyb_i2c_obj; pyb_i2c_obj_t *self = &pyb_i2c_obj;
self->base.type = &pyb_i2c_type; self->base.type = &pyb_i2c_type;
// check the peripheral id // start the peripheral
if (mp_obj_get_int(args[0]) != 0) { pyb_i2c_init_helper(self, &args[1]);
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
}
if (n_args > 1 || n_kw > 0) {
// start the peripheral
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
pyb_i2c_init_helper(self, n_args - 1, args + 1, &kw_args);
}
return (mp_obj_t)self; return (mp_obj_t)self;
} }
STATIC mp_obj_t pyb_i2c_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { STATIC mp_obj_t pyb_i2c_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
return pyb_i2c_init_helper(args[0], n_args - 1, args + 1, kw_args); // parse args
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_init_args) - 1];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), &pyb_i2c_init_args[1], args);
return pyb_i2c_init_helper(pos_args[0], args);
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_init_obj, 1, pyb_i2c_init); STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_init_obj, 1, pyb_i2c_init);

View File

@ -153,6 +153,7 @@ Q(RX_ANY)
// for I2C class // for I2C class
Q(I2C) Q(I2C)
Q(id)
Q(mode) Q(mode)
Q(baudrate) Q(baudrate)
Q(pins) Q(pins)

View File

@ -16,6 +16,13 @@ elif 'WiPy' in machine:
else: else:
raise Exception('Board not supported!') raise Exception('Board not supported!')
i2c = I2C(0, I2C.MASTER, baudrate=400000)
# try initing without the peripheral id
i2c = I2C()
print(i2c)
i2c = I2C(mode=I2C.MASTER, baudrate=50000, pins=i2c_pins)
print(i2c)
i2c = I2C(0, I2C.MASTER, baudrate=100000) i2c = I2C(0, I2C.MASTER, baudrate=100000)
print(i2c) print(i2c)
i2c = I2C(0, mode=I2C.MASTER, baudrate=400000) i2c = I2C(0, mode=I2C.MASTER, baudrate=400000)

View File

@ -1,4 +1,6 @@
I2C(0, I2C.MASTER, baudrate=100000) I2C(0, I2C.MASTER, baudrate=100000)
I2C(0, I2C.MASTER, baudrate=50000)
I2C(0, I2C.MASTER, baudrate=100000)
I2C(0, I2C.MASTER, baudrate=400000) I2C(0, I2C.MASTER, baudrate=400000)
I2C(0, I2C.MASTER, baudrate=400000) I2C(0, I2C.MASTER, baudrate=400000)
104 104

View File

@ -30,6 +30,7 @@ for uart_id in uart_id_range:
uart = UART(baudrate=1000000) uart = UART(baudrate=1000000)
uart.sendbreak() uart.sendbreak()
uart = UART(baudrate=1000000)
uart = UART() uart = UART()
print(uart) print(uart)
uart = UART(baudrate=38400, pins=('GP12', 'GP13')) uart = UART(baudrate=38400, pins=('GP12', 'GP13'))