summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Campora <daniel@wipy.io>2015-09-11 11:05:58 +0200
committerDaniel Campora <daniel@wipy.io>2015-09-16 10:10:19 +0200
commite77abc261bf47343d14c4b5e26c80a0136b93e7c (patch)
treed9b51ab5a911f91f84931462fec3f063c94380d8
parentc69642a4606bb70ea22756fea1ef6e965d1bbd9b (diff)
cc3200: Default peripheral ID support on I2C.
-rw-r--r--cc3200/mods/pybi2c.c65
-rw-r--r--cc3200/qstrdefsport.h1
-rw-r--r--tests/wipy/i2c.py7
-rw-r--r--tests/wipy/i2c.py.exp2
-rw-r--r--tests/wipy/uart.py1
5 files changed, 46 insertions, 30 deletions
diff --git a/cc3200/mods/pybi2c.c b/cc3200/mods/pybi2c.c
index 6c5f0cae5..04ed0d2c2 100644
--- a/cc3200/mods/pybi2c.c
+++ b/cc3200/mods/pybi2c.c
@@ -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 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);
-
+STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, mp_arg_val_t *args) {
// verify that mode is master
if (args[0].u_int != PYBI2C_MASTER) {
goto invalid_args;
@@ -329,32 +317,49 @@ invalid_args:
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
}
-/// \classmethod \constructor(bus, ...)
-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) {
- // check arguments
- mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
-
- // setup the object
- pyb_i2c_obj_t *self = &pyb_i2c_obj;
- self->base.type = &pyb_i2c_type;
+STATIC const mp_arg_t pyb_i2c_init_args[] = {
+ { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} },
+ { 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_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 (mp_obj_get_int(args[0]) != 0) {
+ if (i2c_id != 0) {
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);
- }
+ // setup the object
+ pyb_i2c_obj_t *self = &pyb_i2c_obj;
+ self->base.type = &pyb_i2c_type;
+
+ // start the peripheral
+ pyb_i2c_init_helper(self, &args[1]);
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) {
- return pyb_i2c_init_helper(args[0], n_args - 1, args + 1, 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) {
+ // 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);
diff --git a/cc3200/qstrdefsport.h b/cc3200/qstrdefsport.h
index 2495e566a..c73038f68 100644
--- a/cc3200/qstrdefsport.h
+++ b/cc3200/qstrdefsport.h
@@ -153,6 +153,7 @@ Q(RX_ANY)
// for I2C class
Q(I2C)
+Q(id)
Q(mode)
Q(baudrate)
Q(pins)
diff --git a/tests/wipy/i2c.py b/tests/wipy/i2c.py
index 0673b8000..9e2645947 100644
--- a/tests/wipy/i2c.py
+++ b/tests/wipy/i2c.py
@@ -16,6 +16,13 @@ elif 'WiPy' in machine:
else:
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)
print(i2c)
i2c = I2C(0, mode=I2C.MASTER, baudrate=400000)
diff --git a/tests/wipy/i2c.py.exp b/tests/wipy/i2c.py.exp
index c50cbf0e5..54afbdbd2 100644
--- a/tests/wipy/i2c.py.exp
+++ b/tests/wipy/i2c.py.exp
@@ -1,4 +1,6 @@
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)
104
diff --git a/tests/wipy/uart.py b/tests/wipy/uart.py
index 2c92de664..5b01c4518 100644
--- a/tests/wipy/uart.py
+++ b/tests/wipy/uart.py
@@ -30,6 +30,7 @@ for uart_id in uart_id_range:
uart = UART(baudrate=1000000)
uart.sendbreak()
+uart = UART(baudrate=1000000)
uart = UART()
print(uart)
uart = UART(baudrate=38400, pins=('GP12', 'GP13'))