Skip to content

Commit 8332044

Browse files
author
Daniel Campora
committed
cc3200: Add UART.ODD and UART.EVEN to select parity.
1 parent d5ec336 commit 8332044

4 files changed

Lines changed: 16 additions & 8 deletions

File tree

cc3200/mods/pybuart.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k
343343
if ((self->config & UART_CONFIG_PAR_MASK) == UART_CONFIG_PAR_NONE) {
344344
mp_print_str(print, ", parity=None");
345345
} else {
346-
mp_printf(print, ", parity=%u", (self->config & UART_CONFIG_PAR_MASK) == UART_CONFIG_PAR_EVEN ? 0 : 1);
346+
mp_printf(print, ", parity=UART.%q", (self->config & UART_CONFIG_PAR_MASK) == UART_CONFIG_PAR_EVEN ? MP_QSTR_EVEN : MP_QSTR_ODD);
347347
}
348348
mp_printf(print, ", stop=%u)", (self->config & UART_CONFIG_STOP_MASK) == UART_CONFIG_STOP_ONE ? 1 : 2);
349349
}
@@ -380,7 +380,11 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_arg_val_t *args) {
380380
if (args[2].u_obj == mp_const_none) {
381381
config |= UART_CONFIG_PAR_NONE;
382382
} else {
383-
config |= ((mp_obj_get_int(args[2].u_obj) & 1) ? UART_CONFIG_PAR_ODD : UART_CONFIG_PAR_EVEN);
383+
uint parity = mp_obj_get_int(args[2].u_obj);
384+
if (parity != UART_CONFIG_PAR_ODD && parity != UART_CONFIG_PAR_EVEN) {
385+
goto error;
386+
}
387+
config |= parity;
384388
}
385389
// stop bits
386390
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[] = {
575579
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj },
576580

577581
// class constants
582+
{ MP_OBJ_NEW_QSTR(MP_QSTR_EVEN), MP_OBJ_NEW_SMALL_INT(UART_CONFIG_PAR_EVEN) },
583+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ODD), MP_OBJ_NEW_SMALL_INT(UART_CONFIG_PAR_ODD) },
578584
{ MP_OBJ_NEW_QSTR(MP_QSTR_RX_ANY), MP_OBJ_NEW_SMALL_INT(E_UART_TRIGGER_RX_ANY) },
579585
};
580586

cc3200/qstrdefsport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ Q(bits)
148148
Q(stop)
149149
Q(parity)
150150
Q(pins)
151+
Q(EVEN)
152+
Q(ODD)
151153
Q(RX_ANY)
152154

153155
// for I2C class

docs/library/pyb.UART.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ UART objects can be created and initialised using::
2424

2525
.. only:: port_wipy
2626

27-
Bits can be 5, 6, 7, 8. Parity can be None, 0 (even) or 1 (odd). Stop can be 1 or 2.
27+
Bits can be 5, 6, 7, 8. Parity can be ``None``, ``UART.EVEN`` or ``UART.ODD``. Stop can be 1 or 2.
2828

2929

3030
A UART object acts like a stream object and reading and writing is done
@@ -122,7 +122,7 @@ Methods
122122

123123
- ``baudrate`` is the clock rate.
124124
- ``bits`` is the number of bits per character, 7, 8 or 9.
125-
- ``parity`` is the parity, ``None``, 0 (even) or 1 (odd).
125+
- ``parity`` is the parity, ``None``, ``UART.EVEN`` or ``UART.ODD``.
126126
- ``stop`` is the number of stop bits, 1 or 2.
127127
- ``pins`` is a 4 or 2 item list indicating the TX, RX, RTS and CTS pins (in that order).
128128
Any of the pins can be None if one wants the UART to operate with limited functionality.

tests/wipy/uart.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
uart = UART(uart_id, 38400)
2626
print(uart)
2727
uart.init(57600, 8, None, 1, pins=uart_pins[uart_id][0])
28-
uart.init(baudrate=9600, stop=2, parity=0, pins=uart_pins[uart_id][1])
29-
uart.init(baudrate=115200, parity=1, stop=1, pins=uart_pins[uart_id][0])
28+
uart.init(baudrate=9600, stop=2, parity=UART.EVEN, pins=uart_pins[uart_id][1])
29+
uart.init(baudrate=115200, parity=UART.ODD, stop=0, pins=uart_pins[uart_id][0])
3030
uart = UART(baudrate=1000000)
3131
uart.sendbreak()
3232

@@ -111,12 +111,12 @@
111111

112112
# next ones must raise
113113
try:
114-
UART(0, 9600, parity=2, pins=('GP12', 'GP13', 'GP7'))
114+
UART(0, 9600, parity=None, pins=('GP12', 'GP13', 'GP7'))
115115
except Exception:
116116
print('Exception')
117117

118118
try:
119-
UART(0, 9600, parity=2, pins=('GP12', 'GP7'))
119+
UART(0, 9600, parity=UART.ODD, pins=('GP12', 'GP7'))
120120
except Exception:
121121
print('Exception')
122122

0 commit comments

Comments
 (0)