SummerGift 7 лет назад
Родитель
Сommit
bd8be4d1df
2 измененных файлов с 68 добавлено и 25 удалено
  1. 67 13
      port/machine_uart.c
  2. 1 12
      port/machine_uart.h

+ 67 - 13
port/machine_uart.c

@@ -40,38 +40,93 @@
 /* MicroPython bindings                                                       */
 
 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 = (machine_uart_obj_t*) self_in;
+    mp_printf(print, "uart( device port : %s,baud_rate = %d, data_bits = %d, parity = %d, stop_bits = %d )",
+            self->uart_device->parent.parent.name,
+            self->uart_device->config.baud_rate,
+            self->uart_device->config.data_bits,
+            self->uart_device->config.parity,
+            self->uart_device->config.stop_bits);
 }
 
 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);
+    char uart_dev_name[RT_NAME_MAX];
+    snprintf(uart_dev_name, sizeof(uart_dev_name), "uart%d", mp_obj_get_int(args[0]));
+
+    struct rt_serial_device *rt_serial_device = (struct rt_serial_device *) rt_device_find(uart_dev_name);
+    if (rt_serial_device == RT_NULL || rt_serial_device->parent.type != RT_Device_Class_Char) {
+        rt_kprintf("ERROR: UART device %s not found!\n", uart_dev_name);
+        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "UART(%s) doesn't exist", uart_dev_name));
+    }
+
+    // create new uart object
+    machine_uart_obj_t *self = m_new_obj(machine_uart_obj_t);
+    self->base.type = &machine_uart_type;
+    self->uart_device = rt_serial_device;
+    return (mp_obj_t) self;
 }
 
-STATIC mp_obj_t machine_uart_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
+STATIC mp_obj_t machine_uart_init(size_t n_args, const mp_obj_t *args) {
+    mp_obj_base_t *self = (mp_obj_base_t*) MP_OBJ_TO_PTR(args[0]);
+    struct rt_serial_device *uart_p = ((machine_uart_obj_t *) self)->uart_device;
+    struct serial_configure config;
+
+    int baudrate  = mp_obj_get_int(args[1]);
+    int bits = mp_obj_get_int(args[2]);
+    int parity  = mp_obj_get_int(args[3]);
+    int stop  = mp_obj_get_int(args[4]);
+
+    config.baud_rate = baudrate;
+    config.data_bits = bits;
+    config.parity = parity;
+    config.stop_bits = stop;
+
+    rt_device_control((struct rt_device *) uart_p, RT_DEVICE_CTRL_CONFIG, &config);
+
+    return mp_const_none;
 }
-STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_uart_init_obj, 1, machine_uart_init);
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_uart_init_obj, 5, 5, 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);
+    rt_size_t len = 0;
+    rt_uint32_t timeout = 0;
+    do
+    {
+        len = rt_device_write((struct rt_device *)(self->uart_device), 0, &data, 1);
+        timeout++;
+    }
+    while (len != 1 && timeout < 500);
 
     return mp_const_none;
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_uart_writechar_obj, machine_uart_writechar);
 
+/* 串口接收事件标志 */
+#define UART_RX_EVENT (1 << 0)
+/* 事件控制块 */
+static struct rt_event event;
+
 STATIC mp_obj_t machine_uart_readchar(mp_obj_t self_in) {
     machine_uart_obj_t *self = self_in;
+    rt_uint32_t e;
+    rt_uint8_t ch;
+    /* 读取1字节数据 */
+    while (rt_device_read((struct rt_device *)(self->uart_device), 0, &ch, 1) != 1) {
+        /* 接收事件 */
+        rt_event_recv(&event, UART_RX_EVENT, RT_EVENT_FLAG_AND |
+        RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e);
+    }
+
+    return MP_OBJ_NEW_SMALL_INT(ch);
 }
 STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_readchar_obj, machine_uart_readchar);
 
@@ -80,7 +135,7 @@ STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = {
 
     { 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) },
+//    { 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) },
@@ -99,19 +154,18 @@ STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = {
 //    { 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;
-
+    return rt_device_read((struct rt_device *)(self->uart_device), -1, buf, size);
 }
 
 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;
-
+    return rt_device_write((struct rt_device *)(self->uart_device), -1, buf, size);
 }
 
 STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {

+ 1 - 12
port/machine_uart.h

@@ -30,20 +30,9 @@
 #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
+    struct rt_serial_device *uart_device;
 };
 
 extern const mp_obj_type_t machine_uart_type;