summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Campora <daniel@wipy.io>2015-09-13 15:59:45 +0200
committerDaniel Campora <daniel@wipy.io>2015-09-16 10:10:26 +0200
commit8332044f754aa0743fb38404dae55508a0550e6a (patch)
tree2159aedfa602c2c4335b2f4ea5eabaa5647ed7bc
parentd5ec336eefc8d892a1182da747d825155fcecb15 (diff)
cc3200: Add UART.ODD and UART.EVEN to select parity.
-rw-r--r--cc3200/mods/pybuart.c10
-rw-r--r--cc3200/qstrdefsport.h2
-rw-r--r--docs/library/pyb.UART.rst4
-rw-r--r--tests/wipy/uart.py8
4 files changed, 16 insertions, 8 deletions
diff --git a/cc3200/mods/pybuart.c b/cc3200/mods/pybuart.c
index 7fd647c1d..5c4aa1923 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 c0c8069b3..337ea9b06 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 741c4e491..ca1912ef3 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 5b01c4518..aa2f18b98 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')