|
| 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 | + |
| 31 | +#include "ets_sys.h" |
| 32 | +#include "uart.h" |
| 33 | + |
| 34 | +#include "py/runtime.h" |
| 35 | +#include "py/stream.h" |
| 36 | +#include "py/mperrno.h" |
| 37 | +#include "modmachine.h" |
| 38 | + |
| 39 | +// UartDev is defined and initialized in rom code. |
| 40 | +extern UartDevice UartDev; |
| 41 | + |
| 42 | +typedef struct _pyb_uart_obj_t { |
| 43 | + mp_obj_base_t base; |
| 44 | + uint8_t uart_id; |
| 45 | + uint8_t bits; |
| 46 | + uint8_t parity; |
| 47 | + uint8_t stop; |
| 48 | + uint32_t baudrate; |
| 49 | + uint16_t timeout; // timeout waiting for first char (in ms) |
| 50 | + uint16_t timeout_char; // timeout waiting between chars (in ms) |
| 51 | +} pyb_uart_obj_t; |
| 52 | + |
| 53 | +STATIC const char *_parity_name[] = {"None", "1", "0"}; |
| 54 | + |
| 55 | +/******************************************************************************/ |
| 56 | +// MicroPython bindings for UART |
| 57 | + |
| 58 | +STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 59 | + pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 60 | + mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, timeout=%u, timeout_char=%u)", |
| 61 | + self->uart_id, self->baudrate, self->bits, _parity_name[self->parity], |
| 62 | + self->stop, self->timeout, self->timeout_char); |
| 63 | +} |
| 64 | + |
| 65 | +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) { |
| 66 | + enum { ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_timeout, ARG_timeout_char }; |
| 67 | + static const mp_arg_t allowed_args[] = { |
| 68 | + { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 0} }, |
| 69 | + { MP_QSTR_bits, MP_ARG_INT, {.u_int = 0} }, |
| 70 | + { MP_QSTR_parity, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 71 | + { MP_QSTR_stop, MP_ARG_INT, {.u_int = 0} }, |
| 72 | + //{ MP_QSTR_tx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 73 | + //{ MP_QSTR_rx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, |
| 74 | + { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 75 | + { MP_QSTR_timeout_char, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 76 | + }; |
| 77 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 78 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 79 | + |
| 80 | + // set baudrate |
| 81 | + if (args[ARG_baudrate].u_int > 0) { |
| 82 | + self->baudrate = args[ARG_baudrate].u_int; |
| 83 | + UartDev.baut_rate = self->baudrate; // Sic! |
| 84 | + } |
| 85 | + |
| 86 | + // set data bits |
| 87 | + switch (args[ARG_bits].u_int) { |
| 88 | + case 0: |
| 89 | + break; |
| 90 | + case 5: |
| 91 | + UartDev.data_bits = UART_FIVE_BITS; |
| 92 | + self->bits = 5; |
| 93 | + break; |
| 94 | + case 6: |
| 95 | + UartDev.data_bits = UART_SIX_BITS; |
| 96 | + self->bits = 6; |
| 97 | + break; |
| 98 | + case 7: |
| 99 | + UartDev.data_bits = UART_SEVEN_BITS; |
| 100 | + self->bits = 7; |
| 101 | + break; |
| 102 | + case 8: |
| 103 | + UartDev.data_bits = UART_EIGHT_BITS; |
| 104 | + self->bits = 8; |
| 105 | + break; |
| 106 | + default: |
| 107 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid data bits")); |
| 108 | + break; |
| 109 | + } |
| 110 | + |
| 111 | + // set parity |
| 112 | + if (args[ARG_parity].u_obj != MP_OBJ_NULL) { |
| 113 | + if (args[ARG_parity].u_obj == mp_const_none) { |
| 114 | + UartDev.parity = UART_NONE_BITS; |
| 115 | + UartDev.exist_parity = UART_STICK_PARITY_DIS; |
| 116 | + self->parity = 0; |
| 117 | + } else { |
| 118 | + mp_int_t parity = mp_obj_get_int(args[ARG_parity].u_obj); |
| 119 | + UartDev.exist_parity = UART_STICK_PARITY_EN; |
| 120 | + if (parity & 1) { |
| 121 | + UartDev.parity = UART_ODD_BITS; |
| 122 | + self->parity = 1; |
| 123 | + } else { |
| 124 | + UartDev.parity = UART_EVEN_BITS; |
| 125 | + self->parity = 2; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // set stop bits |
| 131 | + switch (args[ARG_stop].u_int) { |
| 132 | + case 0: |
| 133 | + break; |
| 134 | + case 1: |
| 135 | + UartDev.stop_bits = UART_ONE_STOP_BIT; |
| 136 | + self->stop = 1; |
| 137 | + break; |
| 138 | + case 2: |
| 139 | + UartDev.stop_bits = UART_TWO_STOP_BIT; |
| 140 | + self->stop = 2; |
| 141 | + break; |
| 142 | + default: |
| 143 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid stop bits")); |
| 144 | + break; |
| 145 | + } |
| 146 | + |
| 147 | + // set timeout |
| 148 | + self->timeout = args[ARG_timeout].u_int; |
| 149 | + |
| 150 | + // set timeout_char |
| 151 | + // make sure it is at least as long as a whole character (13 bits to be safe) |
| 152 | + self->timeout_char = args[ARG_timeout_char].u_int; |
| 153 | + uint32_t min_timeout_char = 13000 / self->baudrate + 1; |
| 154 | + if (self->timeout_char < min_timeout_char) { |
| 155 | + self->timeout_char = min_timeout_char; |
| 156 | + } |
| 157 | + |
| 158 | + // setup |
| 159 | + uart_setup(self->uart_id); |
| 160 | +} |
| 161 | + |
| 162 | +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) { |
| 163 | + mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); |
| 164 | + |
| 165 | + // get uart id |
| 166 | + mp_int_t uart_id = mp_obj_get_int(args[0]); |
| 167 | + if (uart_id != 0 && uart_id != 1) { |
| 168 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "UART(%d) does not exist", uart_id)); |
| 169 | + } |
| 170 | + |
| 171 | + // create instance |
| 172 | + pyb_uart_obj_t *self = m_new_obj(pyb_uart_obj_t); |
| 173 | + self->base.type = &pyb_uart_type; |
| 174 | + self->uart_id = uart_id; |
| 175 | + self->baudrate = 115200; |
| 176 | + self->bits = 8; |
| 177 | + self->parity = 0; |
| 178 | + self->stop = 1; |
| 179 | + self->timeout = 0; |
| 180 | + self->timeout_char = 0; |
| 181 | + |
| 182 | + // init the peripheral |
| 183 | + mp_map_t kw_args; |
| 184 | + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); |
| 185 | + pyb_uart_init_helper(self, n_args - 1, args + 1, &kw_args); |
| 186 | + |
| 187 | + return MP_OBJ_FROM_PTR(self); |
| 188 | +} |
| 189 | + |
| 190 | +STATIC mp_obj_t pyb_uart_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { |
| 191 | + pyb_uart_init_helper(args[0], n_args - 1, args + 1, kw_args); |
| 192 | + return mp_const_none; |
| 193 | +} |
| 194 | +MP_DEFINE_CONST_FUN_OBJ_KW(pyb_uart_init_obj, 1, pyb_uart_init); |
| 195 | + |
| 196 | +STATIC const mp_rom_map_elem_t pyb_uart_locals_dict_table[] = { |
| 197 | + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_uart_init_obj) }, |
| 198 | + |
| 199 | + { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) }, |
| 200 | + { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) }, |
| 201 | + { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) }, |
| 202 | + { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) }, |
| 203 | +}; |
| 204 | + |
| 205 | +STATIC MP_DEFINE_CONST_DICT(pyb_uart_locals_dict, pyb_uart_locals_dict_table); |
| 206 | + |
| 207 | +STATIC mp_uint_t pyb_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) { |
| 208 | + pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 209 | + |
| 210 | + if (self->uart_id == 1) { |
| 211 | + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "UART(1) can't read")); |
| 212 | + } |
| 213 | + |
| 214 | + // make sure we want at least 1 char |
| 215 | + if (size == 0) { |
| 216 | + return 0; |
| 217 | + } |
| 218 | + |
| 219 | + // wait for first char to become available |
| 220 | + if (!uart_rx_wait(self->timeout * 1000)) { |
| 221 | + *errcode = MP_EAGAIN; |
| 222 | + return MP_STREAM_ERROR; |
| 223 | + } |
| 224 | + |
| 225 | + // read the data |
| 226 | + uint8_t *buf = buf_in; |
| 227 | + for (;;) { |
| 228 | + *buf++ = uart_rx_char(); |
| 229 | + if (--size == 0 || !uart_rx_wait(self->timeout_char * 1000)) { |
| 230 | + // return number of bytes read |
| 231 | + return buf - (uint8_t*)buf_in; |
| 232 | + } |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +STATIC mp_uint_t pyb_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) { |
| 237 | + pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 238 | + const byte *buf = buf_in; |
| 239 | + |
| 240 | + /* TODO implement non-blocking |
| 241 | + // wait to be able to write the first character |
| 242 | + if (!uart_tx_wait(self, timeout)) { |
| 243 | + *errcode = EAGAIN; |
| 244 | + return MP_STREAM_ERROR; |
| 245 | + } |
| 246 | + */ |
| 247 | + |
| 248 | + // write the data |
| 249 | + for (size_t i = 0; i < size; ++i) { |
| 250 | + uart_tx_one_char(self->uart_id, *buf++); |
| 251 | + } |
| 252 | + |
| 253 | + // return number of bytes written |
| 254 | + return size; |
| 255 | +} |
| 256 | + |
| 257 | +STATIC mp_uint_t pyb_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) { |
| 258 | + pyb_uart_obj_t *self = self_in; |
| 259 | + mp_uint_t ret; |
| 260 | + if (request == MP_STREAM_POLL) { |
| 261 | + mp_uint_t flags = arg; |
| 262 | + ret = 0; |
| 263 | + if ((flags & MP_STREAM_POLL_RD) && uart_rx_any(self->uart_id)) { |
| 264 | + ret |= MP_STREAM_POLL_RD; |
| 265 | + } |
| 266 | + if ((flags & MP_STREAM_POLL_WR) && uart_tx_any_room(self->uart_id)) { |
| 267 | + ret |= MP_STREAM_POLL_WR; |
| 268 | + } |
| 269 | + } else { |
| 270 | + *errcode = MP_EINVAL; |
| 271 | + ret = MP_STREAM_ERROR; |
| 272 | + } |
| 273 | + return ret; |
| 274 | +} |
| 275 | + |
| 276 | +STATIC const mp_stream_p_t uart_stream_p = { |
| 277 | + .read = pyb_uart_read, |
| 278 | + .write = pyb_uart_write, |
| 279 | + .ioctl = pyb_uart_ioctl, |
| 280 | + .is_text = false, |
| 281 | +}; |
| 282 | + |
| 283 | +const mp_obj_type_t pyb_uart_type = { |
| 284 | + { &mp_type_type }, |
| 285 | + .name = MP_QSTR_UART, |
| 286 | + .print = pyb_uart_print, |
| 287 | + .make_new = pyb_uart_make_new, |
| 288 | + .getiter = mp_identity_getiter, |
| 289 | + .iternext = mp_stream_unbuffered_iter, |
| 290 | + .protocol = &uart_stream_p, |
| 291 | + .locals_dict = (mp_obj_dict_t*)&pyb_uart_locals_dict, |
| 292 | +}; |
0 commit comments