Skip to content

Commit 7d6b6f6

Browse files
author
Daniel Campora
committed
cc3200: Make UART choose default id when not given.
1 parent 4ba9b34 commit 7d6b6f6

File tree

6 files changed

+80
-26
lines changed

6 files changed

+80
-26
lines changed

cc3200/mods/pybpin.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ void pin_assign_pins_af (mp_obj_t *pins, uint32_t n_pins, uint32_t pull, uint32_
174174
}
175175
}
176176

177+
uint8_t pin_find_peripheral_unit (const mp_obj_t pin, uint8_t fn, uint8_t type) {
178+
pin_obj_t *pin_o = pin_find(pin);
179+
for (int i = 0; i < pin_o->num_afs; i++) {
180+
if (pin_o->af_list[i].fn == fn && pin_o->af_list[i].type == type) {
181+
return pin_o->af_list[i].unit;
182+
}
183+
}
184+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
185+
}
186+
177187
/******************************************************************************
178188
DEFINE PRIVATE FUNCTIONS
179189
******************************************************************************/

cc3200/mods/pybpin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,6 @@ void pin_init0(void);
135135
void pin_config(pin_obj_t *self, int af, uint mode, uint type, int value, uint strength);
136136
pin_obj_t *pin_find(mp_obj_t user_obj);
137137
void pin_assign_pins_af (mp_obj_t *pins, uint32_t n_pins, uint32_t pull, uint32_t fn, uint32_t unit);
138+
uint8_t pin_find_peripheral_unit (const mp_obj_t pin, uint8_t fn, uint8_t type);
138139

139140
#endif // PYBPIN_H_

cc3200/mods/pybuart.c

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -352,18 +352,7 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k
352352
}
353353
}
354354

355-
STATIC const mp_arg_t pyb_uart_init_args[] = {
356-
{ MP_QSTR_baudrate, MP_ARG_REQUIRED | MP_ARG_INT, },
357-
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
358-
{ MP_QSTR_parity, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
359-
{ MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
360-
{ MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
361-
};
362-
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) {
363-
// parse args
364-
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_uart_init_args)];
365-
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(pyb_uart_init_args), pyb_uart_init_args, args);
366-
355+
STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_arg_val_t *args) {
367356
// get the baudrate
368357
if (args[0].u_int <= 0) {
369358
goto error;
@@ -445,12 +434,41 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con
445434
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
446435
}
447436

448-
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) {
449-
// check arguments
450-
mp_arg_check_num(n_args, n_kw, 1, MP_ARRAY_SIZE(pyb_uart_init_args), true);
437+
STATIC const mp_arg_t pyb_uart_init_args[] = {
438+
{ MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} },
439+
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 9600} },
440+
{ MP_QSTR_bits, MP_ARG_INT, {.u_int = 8} },
441+
{ MP_QSTR_parity, MP_ARG_OBJ, {.u_obj = mp_const_none} },
442+
{ MP_QSTR_stop, MP_ARG_INT, {.u_int = 1} },
443+
{ MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
444+
};
445+
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) {
446+
// parse args
447+
mp_map_t kw_args;
448+
mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args);
449+
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_uart_init_args)];
450+
mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), pyb_uart_init_args, args);
451451

452452
// work out the uart id
453-
int32_t uart_id = mp_obj_get_int(args[0]);
453+
uint8_t uart_id;
454+
if (args[0].u_obj == mp_const_none) {
455+
if (args[5].u_obj != MP_OBJ_NULL) {
456+
mp_obj_t *pins;
457+
mp_uint_t n_pins = 2;
458+
mp_obj_get_array(args[5].u_obj, &n_pins, &pins);
459+
// check the Tx pin (or the Rx if Tx is None)
460+
if (pins[0] == mp_const_none) {
461+
uart_id = pin_find_peripheral_unit(pins[1], PIN_FN_UART, PIN_TYPE_UART_RX);
462+
} else {
463+
uart_id = pin_find_peripheral_unit(pins[0], PIN_FN_UART, PIN_TYPE_UART_TX);
464+
}
465+
} else {
466+
// default id
467+
uart_id = 0;
468+
}
469+
} else {
470+
uart_id = mp_obj_get_int(args[0].u_obj);
471+
}
454472

455473
if (uart_id < PYB_UART_0 || uart_id > PYB_UART_1) {
456474
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
461479
self->base.type = &pyb_uart_type;
462480
self->uart_id = uart_id;
463481

464-
if (n_args > 1 || n_kw > 0) {
465-
// start the peripheral
466-
mp_map_t kw_args;
467-
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
468-
pyb_uart_init_helper(self, n_args - 1, args + 1, &kw_args);
469-
}
482+
// start the peripheral
483+
pyb_uart_init_helper(self, &args[1]);
470484

471485
return self;
472486
}
473487

474-
STATIC mp_obj_t pyb_uart_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
475-
return pyb_uart_init_helper(args[0], n_args - 1, args + 1, kw_args);
488+
STATIC mp_obj_t pyb_uart_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
489+
// parse args
490+
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_uart_init_args) - 1];
491+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), &pyb_uart_init_args[1], args);
492+
return pyb_uart_init_helper(pos_args[0], args);
476493
}
477494
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_uart_init_obj, 1, pyb_uart_init);
478495

cc3200/qstrdefsport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ Q(init)
143143
Q(deinit)
144144
Q(any)
145145
Q(sendbreak)
146+
Q(id)
146147
Q(baudrate)
147148
Q(bits)
148149
Q(stop)

tests/wipy/uart.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,23 @@
2424
for uart_id in uart_id_range:
2525
uart = UART(uart_id, 38400)
2626
print(uart)
27-
uart.init(baudrate=57600, stop=1, parity=None, pins=uart_pins[uart_id][0])
27+
uart.init(57600, 8, None, 1, pins=uart_pins[uart_id][0])
2828
uart.init(baudrate=9600, stop=2, parity=0, pins=uart_pins[uart_id][1])
29-
uart.init(baudrate=115200, parity=1, pins=uart_pins[uart_id][0])
29+
uart.init(baudrate=115200, parity=1, stop=1, pins=uart_pins[uart_id][0])
30+
uart = UART(baudrate=1000000)
3031
uart.sendbreak()
3132

33+
uart = UART()
34+
print(uart)
35+
uart = UART(baudrate=38400, pins=('GP12', 'GP13'))
36+
print(uart)
37+
uart = UART(pins=('GP12', 'GP13'))
38+
print(uart)
39+
uart = UART(pins=(None, 'GP17'))
40+
print(uart)
41+
uart = UART(baudrate=57600, pins=('GP16', 'GP17'))
42+
print(uart)
43+
3244
# now it's time for some loopback tests between the uarts
3345
uart0 = UART(0, 1000000, pins=uart_pins[0][0])
3446
print(uart0)
@@ -50,6 +62,8 @@
5062
print(uart1.readinto(buf) == 2)
5163
print(buf)
5264

65+
# try initializing without the id
66+
uart0 = UART(baudrate=1000000, pins=uart_pins[0][0])
5367
uart0.write(b'1234567890')
5468
pyb.delay(2) # because of the fifo interrupt levels
5569
print(uart1.any() == 10)
@@ -127,6 +141,11 @@
127141
except Exception:
128142
print('Exception')
129143

144+
try:
145+
UART(2, 9600)
146+
except Exception:
147+
print('Exception')
148+
130149
for uart_id in uart_id_range:
131150
uart = UART(uart_id, 1000000)
132151
uart.deinit()

tests/wipy/uart.py.exp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
UART(0, baudrate=38400, bits=8, parity=None, stop=1)
22
UART(1, baudrate=38400, bits=8, parity=None, stop=1)
3+
UART(0, baudrate=9600, bits=8, parity=None, stop=1)
4+
UART(0, baudrate=38400, bits=8, parity=None, stop=1)
5+
UART(0, baudrate=9600, bits=8, parity=None, stop=1)
6+
UART(1, baudrate=9600, bits=8, parity=None, stop=1)
7+
UART(1, baudrate=57600, bits=8, parity=None, stop=1)
38
UART(0, baudrate=1000000, bits=8, parity=None, stop=1)
49
UART(1, baudrate=1000000, bits=8, parity=None, stop=1)
510
True
@@ -40,6 +45,7 @@ Exception
4045
Exception
4146
Exception
4247
Exception
48+
Exception
4349
UART(0)
4450
UART(0, baudrate=115200, bits=8, parity=None, stop=1)
4551
UART(1)

0 commit comments

Comments
 (0)