cc3200: Make UART choose default id when not given.

This commit is contained in:
Daniel Campora 2015-09-11 09:33:19 +02:00
parent 4ba9b34012
commit 7d6b6f6681
6 changed files with 80 additions and 26 deletions

View File

@ -174,6 +174,16 @@ void pin_assign_pins_af (mp_obj_t *pins, uint32_t n_pins, uint32_t pull, uint32_
}
}
uint8_t pin_find_peripheral_unit (const mp_obj_t pin, uint8_t fn, uint8_t type) {
pin_obj_t *pin_o = pin_find(pin);
for (int i = 0; i < pin_o->num_afs; i++) {
if (pin_o->af_list[i].fn == fn && pin_o->af_list[i].type == type) {
return pin_o->af_list[i].unit;
}
}
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
}
/******************************************************************************
DEFINE PRIVATE FUNCTIONS
******************************************************************************/

View File

@ -135,5 +135,6 @@ void pin_init0(void);
void pin_config(pin_obj_t *self, int af, uint mode, uint type, int value, uint strength);
pin_obj_t *pin_find(mp_obj_t user_obj);
void pin_assign_pins_af (mp_obj_t *pins, uint32_t n_pins, uint32_t pull, uint32_t fn, uint32_t unit);
uint8_t pin_find_peripheral_unit (const mp_obj_t pin, uint8_t fn, uint8_t type);
#endif // PYBPIN_H_

View File

@ -352,18 +352,7 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k
}
}
STATIC const mp_arg_t pyb_uart_init_args[] = {
{ MP_QSTR_baudrate, MP_ARG_REQUIRED | MP_ARG_INT, },
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
{ MP_QSTR_parity, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
{ MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
};
STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_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[MP_ARRAY_SIZE(pyb_uart_init_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(pyb_uart_init_args), pyb_uart_init_args, args);
STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_arg_val_t *args) {
// get the baudrate
if (args[0].u_int <= 0) {
goto error;
@ -445,12 +434,41 @@ error:
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
}
STATIC mp_obj_t pyb_uart_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_ARRAY_SIZE(pyb_uart_init_args), true);
STATIC const mp_arg_t pyb_uart_init_args[] = {
{ MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 9600} },
{ MP_QSTR_bits, MP_ARG_INT, {.u_int = 8} },
{ MP_QSTR_parity, MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_stop, MP_ARG_INT, {.u_int = 1} },
{ MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
};
STATIC mp_obj_t pyb_uart_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_uart_init_args)];
mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), pyb_uart_init_args, args);
// work out the uart id
int32_t uart_id = mp_obj_get_int(args[0]);
uint8_t uart_id;
if (args[0].u_obj == mp_const_none) {
if (args[5].u_obj != MP_OBJ_NULL) {
mp_obj_t *pins;
mp_uint_t n_pins = 2;
mp_obj_get_array(args[5].u_obj, &n_pins, &pins);
// check the Tx pin (or the Rx if Tx is None)
if (pins[0] == mp_const_none) {
uart_id = pin_find_peripheral_unit(pins[1], PIN_FN_UART, PIN_TYPE_UART_RX);
} else {
uart_id = pin_find_peripheral_unit(pins[0], PIN_FN_UART, PIN_TYPE_UART_TX);
}
} else {
// default id
uart_id = 0;
}
} else {
uart_id = mp_obj_get_int(args[0].u_obj);
}
if (uart_id < PYB_UART_0 || uart_id > PYB_UART_1) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
@ -461,18 +479,17 @@ STATIC mp_obj_t pyb_uart_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
self->base.type = &pyb_uart_type;
self->uart_id = uart_id;
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_uart_init_helper(self, n_args - 1, args + 1, &kw_args);
}
// start the peripheral
pyb_uart_init_helper(self, &args[1]);
return self;
}
STATIC mp_obj_t pyb_uart_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
return pyb_uart_init_helper(args[0], n_args - 1, args + 1, kw_args);
STATIC mp_obj_t pyb_uart_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_uart_init_args) - 1];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), &pyb_uart_init_args[1], args);
return pyb_uart_init_helper(pos_args[0], args);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_uart_init_obj, 1, pyb_uart_init);

View File

@ -143,6 +143,7 @@ Q(init)
Q(deinit)
Q(any)
Q(sendbreak)
Q(id)
Q(baudrate)
Q(bits)
Q(stop)

View File

@ -24,11 +24,23 @@ pyb.repl_uart(None)
for uart_id in uart_id_range:
uart = UART(uart_id, 38400)
print(uart)
uart.init(baudrate=57600, stop=1, parity=None, pins=uart_pins[uart_id][0])
uart.init(57600, 8, None, 1, pins=uart_pins[uart_id][0])
uart.init(baudrate=9600, stop=2, parity=0, pins=uart_pins[uart_id][1])
uart.init(baudrate=115200, parity=1, pins=uart_pins[uart_id][0])
uart.init(baudrate=115200, parity=1, stop=1, pins=uart_pins[uart_id][0])
uart = UART(baudrate=1000000)
uart.sendbreak()
uart = UART()
print(uart)
uart = UART(baudrate=38400, pins=('GP12', 'GP13'))
print(uart)
uart = UART(pins=('GP12', 'GP13'))
print(uart)
uart = UART(pins=(None, 'GP17'))
print(uart)
uart = UART(baudrate=57600, pins=('GP16', 'GP17'))
print(uart)
# now it's time for some loopback tests between the uarts
uart0 = UART(0, 1000000, pins=uart_pins[0][0])
print(uart0)
@ -50,6 +62,8 @@ print(buf)
print(uart1.readinto(buf) == 2)
print(buf)
# try initializing without the id
uart0 = UART(baudrate=1000000, pins=uart_pins[0][0])
uart0.write(b'1234567890')
pyb.delay(2) # because of the fifo interrupt levels
print(uart1.any() == 10)
@ -127,6 +141,11 @@ try:
except Exception:
print('Exception')
try:
UART(2, 9600)
except Exception:
print('Exception')
for uart_id in uart_id_range:
uart = UART(uart_id, 1000000)
uart.deinit()

View File

@ -1,5 +1,10 @@
UART(0, baudrate=38400, bits=8, parity=None, stop=1)
UART(1, baudrate=38400, bits=8, parity=None, stop=1)
UART(0, baudrate=9600, bits=8, parity=None, stop=1)
UART(0, baudrate=38400, bits=8, parity=None, stop=1)
UART(0, baudrate=9600, bits=8, parity=None, stop=1)
UART(1, baudrate=9600, bits=8, parity=None, stop=1)
UART(1, baudrate=57600, bits=8, parity=None, stop=1)
UART(0, baudrate=1000000, bits=8, parity=None, stop=1)
UART(1, baudrate=1000000, bits=8, parity=None, stop=1)
True
@ -40,6 +45,7 @@ Exception
Exception
Exception
Exception
Exception
UART(0)
UART(0, baudrate=115200, bits=8, parity=None, stop=1)
UART(1)