|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Damien P. George |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <stdio.h> |
| 28 | +#include <stdint.h> |
| 29 | +#include <string.h> |
| 30 | +#include <errno.h> |
| 31 | + |
| 32 | +#include "ets_sys.h" |
| 33 | +#include "uart.h" |
| 34 | + |
| 35 | +#include "py/runtime.h" |
| 36 | +#include "py/stream.h" |
| 37 | +#include "modpyb.h" |
| 38 | + |
| 39 | +typedef struct _pyb_uart_obj_t { |
| 40 | + mp_obj_base_t base; |
| 41 | + uint8_t uart_id; |
| 42 | +} pyb_uart_obj_t; |
| 43 | + |
| 44 | +/******************************************************************************/ |
| 45 | +// MicroPython bindings for UART |
| 46 | + |
| 47 | +STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 48 | + pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 49 | + mp_printf(print, "UART(%u)", self->uart_id); |
| 50 | +} |
| 51 | + |
| 52 | +STATIC void pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 53 | + /* |
| 54 | + enum { ARG_baudrate, ARG_bits, ARG_parity, ARG_stop }; |
| 55 | + static const mp_arg_t allowed_args[] = { |
| 56 | + { MP_QSTR_baudrate, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 9600} }, |
| 57 | + { MP_QSTR_bits, MP_ARG_INT, {.u_int = 8} }, |
| 58 | + { MP_QSTR_parity, MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 59 | + { MP_QSTR_stop, MP_ARG_INT, {.u_int = 1} }, |
| 60 | + { MP_QSTR_tx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 61 | + { MP_QSTR_rx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 62 | + }; |
| 63 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 64 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 65 | + */ |
| 66 | + // not implemented |
| 67 | +} |
| 68 | + |
| 69 | +STATIC mp_obj_t pyb_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 70 | + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); |
| 71 | + |
| 72 | + // get uart id |
| 73 | + mp_int_t uart_id = mp_obj_get_int(args[0]); |
| 74 | + if (uart_id != 0 && uart_id != 1) { |
| 75 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "UART(%d) does not exist", uart_id)); |
| 76 | + } |
| 77 | + |
| 78 | + // create instance |
| 79 | + pyb_uart_obj_t *self = m_new_obj(pyb_uart_obj_t); |
| 80 | + self->base.type = &pyb_uart_type; |
| 81 | + self->uart_id = uart_id; |
| 82 | + |
| 83 | + if (n_args > 1 || n_kw > 0) { |
| 84 | + // init the peripheral |
| 85 | + mp_map_t kw_args; |
| 86 | + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); |
| 87 | + pyb_uart_init_helper(self, n_args - 1, args + 1, &kw_args); |
| 88 | + } |
| 89 | + |
| 90 | + return MP_OBJ_FROM_PTR(self); |
| 91 | +} |
| 92 | + |
| 93 | +STATIC mp_obj_t pyb_uart_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { |
| 94 | + pyb_uart_init_helper(args[0], n_args - 1, args + 1, kw_args); |
| 95 | + return mp_const_none; |
| 96 | +} |
| 97 | +MP_DEFINE_CONST_FUN_OBJ_KW(pyb_uart_init_obj, 1, pyb_uart_init); |
| 98 | + |
| 99 | +STATIC const mp_rom_map_elem_t pyb_uart_locals_dict_table[] = { |
| 100 | + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_uart_init_obj) }, |
| 101 | + |
| 102 | + { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, |
| 103 | + { MP_ROM_QSTR(MP_QSTR_readall), MP_ROM_PTR(&mp_stream_readall_obj) }, |
| 104 | + { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) }, |
| 105 | + { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, |
| 106 | + { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, |
| 107 | +}; |
| 108 | + |
| 109 | +STATIC MP_DEFINE_CONST_DICT(pyb_uart_locals_dict, pyb_uart_locals_dict_table); |
| 110 | + |
| 111 | +STATIC mp_uint_t pyb_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { |
| 112 | + mp_not_implemented("reading from UART"); |
| 113 | +} |
| 114 | + |
| 115 | +STATIC mp_uint_t pyb_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) { |
| 116 | + pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 117 | + const byte *buf = buf_in; |
| 118 | + |
| 119 | + /* TODO implement non-blocking |
| 120 | + // wait to be able to write the first character |
| 121 | + if (!uart_tx_wait(self, timeout)) { |
| 122 | + *errcode = EAGAIN; |
| 123 | + return MP_STREAM_ERROR; |
| 124 | + } |
| 125 | + */ |
| 126 | + |
| 127 | + // write the data |
| 128 | + for (size_t i = 0; i < size; ++i) { |
| 129 | + uart_tx_one_char(self->uart_id, *buf++); |
| 130 | + } |
| 131 | + |
| 132 | + // return number of bytes written |
| 133 | + return size; |
| 134 | +} |
| 135 | + |
| 136 | +STATIC mp_uint_t pyb_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { |
| 137 | + *errcode = EINVAL; |
| 138 | + return MP_STREAM_ERROR; |
| 139 | +} |
| 140 | + |
| 141 | +STATIC const mp_stream_p_t uart_stream_p = { |
| 142 | + .read = pyb_uart_read, |
| 143 | + .write = pyb_uart_write, |
| 144 | + .ioctl = pyb_uart_ioctl, |
| 145 | + .is_text = false, |
| 146 | +}; |
| 147 | + |
| 148 | +const mp_obj_type_t pyb_uart_type = { |
| 149 | + { &mp_type_type }, |
| 150 | + .name = MP_QSTR_UART, |
| 151 | + .print = pyb_uart_print, |
| 152 | + .make_new = pyb_uart_make_new, |
| 153 | + .getiter = mp_identity, |
| 154 | + .iternext = mp_stream_unbuffered_iter, |
| 155 | + .stream_p = &uart_stream_p, |
| 156 | + .locals_dict = (mp_obj_dict_t*)&pyb_uart_locals_dict, |
| 157 | +}; |
0 commit comments