diff --git a/cc3200/mods/pybuart.c b/cc3200/mods/pybuart.c index 7fd647c1d7..5c4aa19238 100644 --- a/cc3200/mods/pybuart.c +++ b/cc3200/mods/pybuart.c @@ -343,7 +343,7 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k if ((self->config & UART_CONFIG_PAR_MASK) == UART_CONFIG_PAR_NONE) { mp_print_str(print, ", parity=None"); } else { - mp_printf(print, ", parity=%u", (self->config & UART_CONFIG_PAR_MASK) == UART_CONFIG_PAR_EVEN ? 0 : 1); + mp_printf(print, ", parity=UART.%q", (self->config & UART_CONFIG_PAR_MASK) == UART_CONFIG_PAR_EVEN ? MP_QSTR_EVEN : MP_QSTR_ODD); } mp_printf(print, ", stop=%u)", (self->config & UART_CONFIG_STOP_MASK) == UART_CONFIG_STOP_ONE ? 1 : 2); } @@ -380,7 +380,11 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_arg_val_t *args) { if (args[2].u_obj == mp_const_none) { config |= UART_CONFIG_PAR_NONE; } else { - config |= ((mp_obj_get_int(args[2].u_obj) & 1) ? UART_CONFIG_PAR_ODD : UART_CONFIG_PAR_EVEN); + uint parity = mp_obj_get_int(args[2].u_obj); + if (parity != UART_CONFIG_PAR_ODD && parity != UART_CONFIG_PAR_EVEN) { + goto error; + } + config |= parity; } // stop bits config |= (args[3].u_int == 1 ? UART_CONFIG_STOP_ONE : UART_CONFIG_STOP_TWO); @@ -575,6 +579,8 @@ STATIC const mp_map_elem_t pyb_uart_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, // class constants + { MP_OBJ_NEW_QSTR(MP_QSTR_EVEN), MP_OBJ_NEW_SMALL_INT(UART_CONFIG_PAR_EVEN) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_ODD), MP_OBJ_NEW_SMALL_INT(UART_CONFIG_PAR_ODD) }, { MP_OBJ_NEW_QSTR(MP_QSTR_RX_ANY), MP_OBJ_NEW_SMALL_INT(E_UART_TRIGGER_RX_ANY) }, }; diff --git a/cc3200/qstrdefsport.h b/cc3200/qstrdefsport.h index c0c8069b3c..337ea9b06b 100644 --- a/cc3200/qstrdefsport.h +++ b/cc3200/qstrdefsport.h @@ -148,6 +148,8 @@ Q(bits) Q(stop) Q(parity) Q(pins) +Q(EVEN) +Q(ODD) Q(RX_ANY) // for I2C class diff --git a/docs/library/pyb.UART.rst b/docs/library/pyb.UART.rst index 741c4e4912..ca1912ef3b 100644 --- a/docs/library/pyb.UART.rst +++ b/docs/library/pyb.UART.rst @@ -24,7 +24,7 @@ UART objects can be created and initialised using:: .. only:: port_wipy - Bits can be 5, 6, 7, 8. Parity can be None, 0 (even) or 1 (odd). Stop can be 1 or 2. + Bits can be 5, 6, 7, 8. Parity can be ``None``, ``UART.EVEN`` or ``UART.ODD``. Stop can be 1 or 2. A UART object acts like a stream object and reading and writing is done @@ -122,7 +122,7 @@ Methods - ``baudrate`` is the clock rate. - ``bits`` is the number of bits per character, 7, 8 or 9. - - ``parity`` is the parity, ``None``, 0 (even) or 1 (odd). + - ``parity`` is the parity, ``None``, ``UART.EVEN`` or ``UART.ODD``. - ``stop`` is the number of stop bits, 1 or 2. - ``pins`` is a 4 or 2 item list indicating the TX, RX, RTS and CTS pins (in that order). Any of the pins can be None if one wants the UART to operate with limited functionality. diff --git a/tests/wipy/uart.py b/tests/wipy/uart.py index 5b01c4518c..aa2f18b985 100644 --- a/tests/wipy/uart.py +++ b/tests/wipy/uart.py @@ -25,8 +25,8 @@ for uart_id in uart_id_range: uart = UART(uart_id, 38400) print(uart) 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, stop=1, pins=uart_pins[uart_id][0]) + uart.init(baudrate=9600, stop=2, parity=UART.EVEN, pins=uart_pins[uart_id][1]) + uart.init(baudrate=115200, parity=UART.ODD, stop=0, pins=uart_pins[uart_id][0]) uart = UART(baudrate=1000000) uart.sendbreak() @@ -111,12 +111,12 @@ for i in range (0, 1000): # next ones must raise try: - UART(0, 9600, parity=2, pins=('GP12', 'GP13', 'GP7')) + UART(0, 9600, parity=None, pins=('GP12', 'GP13', 'GP7')) except Exception: print('Exception') try: - UART(0, 9600, parity=2, pins=('GP12', 'GP7')) + UART(0, 9600, parity=UART.ODD, pins=('GP12', 'GP7')) except Exception: print('Exception')