|
|
|
|
@@ -25,18 +25,16 @@
|
|
|
|
|
* THE SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
// This file is never compiled standalone, it's included directly from
|
|
|
|
|
// extmod/machine_uart.c via MICROPY_PY_MACHINE_UART_INCLUDEFILE.
|
|
|
|
|
|
|
|
|
|
#include <zephyr/zephyr.h>
|
|
|
|
|
#include <zephyr/drivers/uart.h>
|
|
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
#include "py/stream.h"
|
|
|
|
|
#include "py/mperrno.h"
|
|
|
|
|
#include "py/objstr.h"
|
|
|
|
|
#include "modmachine.h"
|
|
|
|
|
|
|
|
|
|
// The UART class doesn't have any constants for this port.
|
|
|
|
|
#define MICROPY_PY_MACHINE_UART_CLASS_CONSTANTS
|
|
|
|
|
|
|
|
|
|
typedef struct _machine_uart_obj_t {
|
|
|
|
|
mp_obj_base_t base;
|
|
|
|
|
@@ -50,7 +48,7 @@ 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) {
|
|
|
|
|
STATIC void mp_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);
|
|
|
|
|
@@ -60,7 +58,7 @@ STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_pri
|
|
|
|
|
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) {
|
|
|
|
|
STATIC void mp_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} },
|
|
|
|
|
@@ -73,7 +71,7 @@ STATIC void machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, co
|
|
|
|
|
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) {
|
|
|
|
|
STATIC mp_obj_t mp_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);
|
|
|
|
|
|
|
|
|
|
machine_uart_obj_t *self = mp_obj_malloc(machine_uart_obj_t, &machine_uart_type);
|
|
|
|
|
@@ -84,20 +82,26 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
mp_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 void mp_machine_uart_deinit(machine_uart_obj_t *self) {
|
|
|
|
|
(void)self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
|
|
|
|
|
STATIC mp_int_t mp_machine_uart_any(machine_uart_obj_t *self) {
|
|
|
|
|
(void)self;
|
|
|
|
|
mp_raise_NotImplementedError(NULL); // TODO
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
STATIC bool mp_machine_uart_txdone(machine_uart_obj_t *self) {
|
|
|
|
|
(void)self;
|
|
|
|
|
mp_raise_NotImplementedError(NULL); // TODO
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
STATIC mp_uint_t mp_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;
|
|
|
|
|
@@ -118,7 +122,7 @@ STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t siz
|
|
|
|
|
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) {
|
|
|
|
|
STATIC mp_uint_t mp_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;
|
|
|
|
|
|
|
|
|
|
@@ -129,7 +133,7 @@ STATIC mp_uint_t machine_uart_write(mp_obj_t self_in, const void *buf_in, mp_uin
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
|
|
|
|
|
STATIC mp_uint_t mp_machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
|
|
|
|
|
mp_uint_t ret;
|
|
|
|
|
|
|
|
|
|
if (request == MP_STREAM_POLL) {
|
|
|
|
|
@@ -146,20 +150,3 @@ STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr
|
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
|
machine_uart_type,
|
|
|
|
|
MP_QSTR_UART,
|
|
|
|
|
MP_TYPE_FLAG_ITER_IS_STREAM,
|
|
|
|
|
make_new, machine_uart_make_new,
|
|
|
|
|
print, machine_uart_print,
|
|
|
|
|
protocol, &uart_stream_p,
|
|
|
|
|
locals_dict, &machine_uart_locals_dict
|
|
|
|
|
);
|
|
|
|
|
|