summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYonatan Schachter <yonatan.schachter@gmail.com>2020-11-24 16:59:04 +0200
committerDamien George <damien@micropython.org>2021-01-23 16:31:00 +1100
commit063d7cc0e2f076bd4299ab487563d398c3d212b6 (patch)
tree464e70c3659a71982858798ae1d9f5f16f151ef2
parentaa136b4d78c7f81e63bde20ae17ab69bbbf456db (diff)
zephyr: Add basic UART functionality to machine module.
Currently supports only polling read and write. Signed-off-by: Yonatan Schachter <yonatan.schachter@gmail.com>
-rw-r--r--ports/zephyr/Makefile1
-rw-r--r--ports/zephyr/machine_uart.c168
-rw-r--r--ports/zephyr/modmachine.c1
-rw-r--r--ports/zephyr/modmachine.h1
4 files changed, 171 insertions, 0 deletions
diff --git a/ports/zephyr/Makefile b/ports/zephyr/Makefile
index 169aebc26..c200062a3 100644
--- a/ports/zephyr/Makefile
+++ b/ports/zephyr/Makefile
@@ -49,6 +49,7 @@ SRC_C = main.c \
modmachine.c \
machine_i2c.c \
machine_pin.c \
+ machine_uart.c \
uart_core.c \
zephyr_storage.c \
lib/timeutils/timeutils.c \
diff --git a/ports/zephyr/machine_uart.c b/ports/zephyr/machine_uart.c
new file mode 100644
index 000000000..6ae8707d7
--- /dev/null
+++ b/ports/zephyr/machine_uart.c
@@ -0,0 +1,168 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 Damien P. George
+ * Copyright (c) 2020 Yonatan Schachter
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+
+#include <zephyr.h>
+#include <drivers/uart.h>
+
+#include "py/runtime.h"
+#include "py/stream.h"
+#include "py/mperrno.h"
+#include "py/objstr.h"
+#include "modmachine.h"
+
+typedef struct _machine_uart_obj_t {
+ mp_obj_base_t base;
+ const struct device *dev;
+ uint16_t timeout; // timeout waiting for first char (in ms)
+ uint16_t timeout_char; // timeout waiting between chars (in ms)
+} machine_uart_obj_t;
+
+STATIC const char *_parity_name[] = {"None", "Odd", "Even", "Mark", "Space"};
+STATIC const char *_stop_bits_name[] = {"0.5", "1", "1.5", "2"};
+STATIC const char *_data_bits_name[] = {"5", "6", "7", "8", "9"};
+STATIC const char *_flow_control_name[] = {"None", "RTS/CTS", "DTR/DSR"};
+
+STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
+ machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ struct uart_config config;
+ uart_config_get(self->dev, &config);
+ mp_printf(print, "UART(\"%s\", baudrate=%u, data_bits=%s, parity_bits=%s, stop_bits=%s, flow_control=%s, timeout=%u, timeout_char=%u)",
+ self->dev->name, config.baudrate, _data_bits_name[config.data_bits],
+ _parity_name[config.parity], _stop_bits_name[config.stop_bits], _flow_control_name[config.flow_ctrl],
+ self->timeout, self->timeout_char);
+}
+
+STATIC void machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ enum { ARG_timeout, ARG_timeout_char };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
+ { MP_QSTR_timeout_char, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
+ };
+ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+ mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+ self->timeout = args[ARG_timeout].u_int;
+ self->timeout_char = args[ARG_timeout_char].u_int;
+}
+
+STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
+ mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
+ GET_STR_DATA_LEN(args[0], name, name_len);
+
+ machine_uart_obj_t *self = m_new_obj(machine_uart_obj_t);
+ self->base.type = &machine_uart_type;
+ self->dev = device_get_binding(name);
+ if (!self->dev) {
+ mp_raise_ValueError(MP_ERROR_TEXT("Bad device name"));
+ }
+
+ mp_map_t kw_args;
+ mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
+ machine_uart_init_helper(self, n_args - 1, args + 1, &kw_args);
+
+ return MP_OBJ_FROM_PTR(self);
+}
+
+STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = {
+ { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
+ { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
+ { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
+ { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
+};
+STATIC MP_DEFINE_CONST_DICT(machine_uart_locals_dict, machine_uart_locals_dict_table);
+
+STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
+ machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ uint8_t *buffer = (uint8_t *)buf_in;
+ uint8_t data;
+ mp_uint_t bytes_read = 0;
+ size_t elapsed_ms = 0;
+ size_t time_to_wait = self->timeout;
+
+ while ((elapsed_ms < time_to_wait) && (bytes_read < size)) {
+ if (!uart_poll_in(self->dev, &data)) {
+ buffer[bytes_read++] = data;
+ elapsed_ms = 0;
+ time_to_wait = self->timeout_char;
+ } else {
+ k_msleep(1);
+ elapsed_ms++;
+ }
+ }
+ return bytes_read;
+}
+
+STATIC mp_uint_t machine_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) {
+ machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ uint8_t *buffer = (uint8_t *)buf_in;
+
+ for (mp_uint_t i = 0; i < size; i++) {
+ uart_poll_out(self->dev, buffer[i]);
+ }
+
+ return size;
+}
+
+STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
+ mp_uint_t ret;
+
+ if (request == MP_STREAM_POLL) {
+ ret = 0;
+ // read is always blocking
+
+ if (arg & MP_STREAM_POLL_WR) {
+ ret |= MP_STREAM_POLL_WR;
+ }
+ return ret;
+ } else {
+ *errcode = MP_EINVAL;
+ ret = MP_STREAM_ERROR;
+ }
+ return ret;
+}
+
+STATIC const mp_stream_p_t uart_stream_p = {
+ .read = machine_uart_read,
+ .write = machine_uart_write,
+ .ioctl = machine_uart_ioctl,
+ .is_text = false,
+};
+
+const mp_obj_type_t machine_uart_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_UART,
+ .print = machine_uart_print,
+ .make_new = machine_uart_make_new,
+ .getiter = mp_identity_getiter,
+ .iternext = mp_stream_unbuffered_iter,
+ .protocol = &uart_stream_p,
+ .locals_dict = (mp_obj_dict_t *)&machine_uart_locals_dict,
+};
diff --git a/ports/zephyr/modmachine.c b/ports/zephyr/modmachine.c
index 968f758b9..107895bea 100644
--- a/ports/zephyr/modmachine.c
+++ b/ports/zephyr/modmachine.c
@@ -63,6 +63,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
#if MICROPY_PY_MACHINE_I2C
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_hard_i2c_type) },
#endif
+ { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) },
{ MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) },
{ MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) },
diff --git a/ports/zephyr/modmachine.h b/ports/zephyr/modmachine.h
index b67da5533..957f2dd4f 100644
--- a/ports/zephyr/modmachine.h
+++ b/ports/zephyr/modmachine.h
@@ -5,6 +5,7 @@
extern const mp_obj_type_t machine_pin_type;
extern const mp_obj_type_t machine_hard_i2c_type;
+extern const mp_obj_type_t machine_uart_type;
MP_DECLARE_CONST_FUN_OBJ_0(machine_info_obj);