Explorar o código

【添加】:machine 模块下的 UART 类

SummerGift %!s(int64=7) %!d(string=hai) anos
pai
achega
bc649903d9
Modificáronse 5 ficheiros con 205 adicións e 1 borrados
  1. 6 0
      port/genhdr/qstrdefs.generated.h
  2. 139 0
      port/machine_uart.c
  3. 52 0
      port/machine_uart.h
  4. 4 1
      port/modmachine.c
  5. 4 0
      port/mpconfigport.h

+ 6 - 0
port/genhdr/qstrdefs.generated.h

@@ -638,5 +638,11 @@ QDEF(MP_QSTR_get_ident, (const byte*)"\xfe\x09" "get_ident")
 QDEF(MP_QSTR_stack_size, (const byte*)"\x31\x0a" "stack_size")
 QDEF(MP_QSTR_start_new_thread, (const byte*)"\xd7\x10" "start_new_thread")
 QDEF(MP_QSTR_allocate_lock, (const byte*)"\xec\x0d" "allocate_lock")
+QDEF(MP_QSTR_UART, (const byte*)"\xb7\x04" "UART")
+QDEF(MP_QSTR_writechar, (const byte*)"\x40\x09" "writechar")
+QDEF(MP_QSTR_readchar, (const byte*)"\xef\x08" "readchar")
+QDEF(MP_QSTR_sendbreak, (const byte*)"\xc6\x09" "sendbreak")
+QDEF(MP_QSTR_RTS, (const byte*)"\xb0\x03" "RTS")
+QDEF(MP_QSTR_CTS, (const byte*)"\x61\x03" "CTS")
 
 // This file was automatically generated by makeqstrdata.py

+ 139 - 0
port/machine_uart.c

@@ -0,0 +1,139 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 SummerGift <zhangyuan@rt-thread.com>
+ *
+ * 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.
+ */
+#ifdef MICROPYTHON_USING_MACHINE_UART
+
+#include <stdio.h>
+#include <string.h>
+
+#include "py/runtime.h"
+#include "py/mphal.h"
+#include "py/mperrno.h"
+#include "py/stream.h"
+
+#include <stdarg.h>
+#include "machine_uart.h"
+
+/******************************************************************************/
+/* MicroPython bindings                                                       */
+
+STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
+}
+
+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);
+}
+
+STATIC mp_obj_t machine_uart_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_uart_init_obj, 1, machine_uart_init);
+
+STATIC mp_obj_t machine_uart_deinit(mp_obj_t self_in) {
+    machine_uart_obj_t *self = self_in;
+    return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_deinit_obj, machine_uart_deinit);
+
+STATIC mp_obj_t machine_uart_any(mp_obj_t self_in) {
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_any_obj, machine_uart_any);
+
+STATIC mp_obj_t machine_uart_writechar(mp_obj_t self_in, mp_obj_t char_in) {
+    machine_uart_obj_t *self = self_in;
+
+    // get the character to write (might be 9 bits)
+    uint16_t data = mp_obj_get_int(char_in);
+
+    return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_uart_writechar_obj, machine_uart_writechar);
+
+STATIC mp_obj_t machine_uart_readchar(mp_obj_t self_in) {
+    machine_uart_obj_t *self = self_in;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_readchar_obj, machine_uart_readchar);
+
+STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = {
+    // instance methods
+
+    { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_uart_init_obj) },
+    { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_uart_deinit_obj) },
+    { MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&machine_uart_any_obj) },
+
+    /// \method read([nbytes])
+    { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
+    /// \method readline()
+    { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj)},
+    /// \method readinto(buf[, nbytes])
+    { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
+    /// \method write(buf)
+    { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
+
+    { MP_ROM_QSTR(MP_QSTR_writechar), MP_ROM_PTR(&machine_uart_writechar_obj) },
+    { MP_ROM_QSTR(MP_QSTR_readchar), MP_ROM_PTR(&machine_uart_readchar_obj) },
+//    { MP_ROM_QSTR(MP_QSTR_sendbreak), MP_ROM_PTR(&pyb_uart_sendbreak_obj) },
+
+//    class constants
+//    { MP_ROM_QSTR(MP_QSTR_RTS), MP_ROM_INT(UART_HWCONTROL_RTS) },
+//    { MP_ROM_QSTR(MP_QSTR_CTS), MP_ROM_INT(UART_HWCONTROL_CTS) },
+};
+
+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 = self_in;
+    byte *buf = buf_in;
+
+}
+
+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 = self_in;
+    const byte *buf = buf_in;
+
+}
+
+STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
+    return NULL;
+}
+
+STATIC const mp_stream_p_t uart_stream_p = {
+    .read = machine_uart_read,
+    .write = machine_uart_write,
+    .ioctl = NULL,
+    .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,
+};
+
+#endif // MICROPYTHON_USING_MACHINE_UART

+ 52 - 0
port/machine_uart.h

@@ -0,0 +1,52 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 SummerGift <zhangyuan@rt-thread.com>
+ *
+ * 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.
+ */
+
+#ifndef _MACHINE_UART_H
+#define _MACHINE_UART_H
+
+#include "py/obj.h"
+#include <rtthread.h>
+
+#define CHAR_WIDTH_9BIT (1)
+#define CHAR_WIDTH_8BIT (0)
+
+typedef struct _machine_uart_obj_t {
+    mp_obj_base_t base;
+    bool is_enabled : 1;
+    byte char_width;
+    uint16_t char_mask;                 // 0x7f for 7 bit, 0xff for 8 bit, 0x1ff for 9 bit
+    uint16_t timeout;                   // timeout waiting for first char
+    uint16_t timeout_char;              // timeout waiting between chars
+    uint16_t read_buf_len;              // len in chars; buf can hold len-1 chars
+    volatile uint16_t read_buf_head;    // indexes first empty slot
+    uint16_t read_buf_tail;             // indexes first full slot (not full if equals head)
+    byte *read_buf;                     // byte or uint16_t, depending on char size
+};
+
+extern const mp_obj_type_t machine_uart_type;
+typedef struct _machine_uart_obj_t machine_uart_obj_t;
+
+#endif // _MACHINE_UART_H

+ 4 - 1
port/modmachine.c

@@ -37,6 +37,7 @@
 #include "extmod/machine_i2c.h"
 #include "extmod/machine_spi.h"
 #include "modmachine.h"
+#include "machine_uart.h"
 
 #include <rthw.h>
 
@@ -206,7 +207,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
 #if MICROPY_PY_MACHINE_SPI
     { MP_ROM_QSTR(MP_QSTR_SPI),                 MP_ROM_PTR(&mp_machine_soft_spi_type) },
 #endif
-//    { MP_ROM_QSTR(MP_QSTR_UART),                MP_ROM_PTR(&pyb_uart_type) },
+#if MICROPY_PY_MACHINE_UART
+    { MP_ROM_QSTR(MP_QSTR_UART),                MP_ROM_PTR(&machine_uart_type ) },
+#endif
 //    { MP_ROM_QSTR(MP_QSTR_WDT),                 MP_ROM_PTR(&pyb_wdt_type) },
     { MP_ROM_QSTR(MP_QSTR_PWRON_RESET),         MP_ROM_INT(PYB_RESET_POWER_ON) },
     { MP_ROM_QSTR(MP_QSTR_HARD_RESET),          MP_ROM_INT(PYB_RESET_HARD) },

+ 4 - 0
port/mpconfigport.h

@@ -138,6 +138,10 @@
 #define MICROPY_PY_MACHINE_SPI_MAKE_NEW machine_hard_spi_make_new
 #endif
 
+#ifdef MICROPYTHON_USING_MACHINE_UART
+#define MICROPY_PY_MACHINE_UART      (1)
+#endif
+
 /*****************************************************************************/
 /* System Module                                                             */