Просмотр исходного кода

【增加】RT-Thread console 输出拦截功能。

Signed-off-by: armink <armink.ztl@gmail.com>
armink 6 лет назад
Родитель
Сommit
3b2dd7f391
6 измененных файлов с 122 добавлено и 22 удалено
  1. 4 4
      port/mpgetcharport.c
  2. 6 6
      port/mpgetcharport.h
  3. 6 9
      port/mphalport.c
  4. 66 0
      port/mpputsnport.c
  5. 34 0
      port/mpputsnport.h
  6. 6 3
      port/mpy_main.c

+ 4 - 4
port/rtt_getchar.c → port/mpgetcharport.c

@@ -28,8 +28,8 @@
 #include <rtthread.h>
 #include <rtdevice.h>
 #include <rthw.h>
-#include "rtt_getchar.h"
 #include "lib/utils/interrupt_char.h"
+#include "mpgetcharport.h"
 
 #define UART_FIFO_SIZE 256
 
@@ -56,7 +56,7 @@ static rt_err_t getchar_rx_ind(rt_device_t dev, rt_size_t size) {
     return RT_EOK;
 }
 
-void rtt_getchar_init(void) {
+void mp_getchar_init(void) {
     rt_base_t int_lvl;
     rt_device_t console;
 
@@ -76,7 +76,7 @@ void rtt_getchar_init(void) {
 
 }
 
-void rtt_getchar_deinit(void) {
+void mp_getchar_deinit(void) {
     rt_base_t int_lvl;
     rt_device_t console;
 
@@ -91,7 +91,7 @@ void rtt_getchar_deinit(void) {
     rt_hw_interrupt_enable(int_lvl);
 }
 
-int rtt_getchar(void) {
+int mp_getchar(void) {
     uint8_t ch;
     rt_base_t int_lvl;
 

+ 6 - 6
port/rtt_getchar.h → port/mpgetcharport.h

@@ -24,11 +24,11 @@
  * THE SOFTWARE.
  */
 
-#ifndef _RTT_GETCHAR_H_
-#define _RTT_GETCHAR_H_
+#ifndef _MPGETCHARPORT_H_
+#define _MPGETCHARPORT_H_
 
-void rtt_getchar_init(void);
-void rtt_getchar_deinit(void);
-int rtt_getchar(void);
+void mp_getchar_init(void);
+void mp_getchar_deinit(void);
+int mp_getchar(void);
 
-#endif /* _RTT_GETCHAR_H_ */
+#endif /* _MPGETCHARPORT_H_ */

+ 6 - 9
port/mphalport.c

@@ -31,12 +31,13 @@
 #include <py/mpconfig.h>
 #include <py/runtime.h>
 #include "mphalport.h"
-#include "rtt_getchar.h"
+#include "mpgetcharport.h"
+#include "mpputsnport.h"
 
 int mp_hal_stdin_rx_chr(void) {
     char ch;
     while (1) {
-        ch = rtt_getchar();
+        ch = mp_getchar();
         if (ch != (char)0xFF) {
             break;
         }
@@ -46,18 +47,14 @@ int mp_hal_stdin_rx_chr(void) {
     return ch;
 }
 
+
 // Send string of given length
 void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
-    rt_device_t console;
-
-    console = rt_console_get_device();
-    if (console) {
-        rt_device_write(console, 0, str, len);
-    }
+    mp_putsn(str, len);
 }
 
 void mp_hal_stdout_tx_strn_stream(const char *str, size_t len) {
-    rt_kprintf("%.*s", len, str);
+    mp_putsn(str, len);
 }
 
 mp_uint_t mp_hal_ticks_us(void) {

+ 66 - 0
port/mpputsnport.c

@@ -0,0 +1,66 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Armink (armink.ztl@gmail.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.
+ */
+
+#include <rtthread.h>
+#include <rtdevice.h>
+
+static rt_device_t console_dev = NULL;
+static struct rt_device dummy_console = { 0 };
+
+void mp_putsn(const char *str, size_t len) {
+    if (console_dev) {
+        rt_device_write(console_dev, 0, str, len);
+    }
+}
+
+void mp_putsn_init(void) {
+    {/* register dummy console device */
+#ifdef RT_USING_DEVICE_OPS
+        static struct rt_device_ops _ops = {.ops = &_ops};
+#endif
+
+        dummy_console.type = RT_Device_Class_Char;
+
+        rt_device_register(&dummy_console, "dummy", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STREAM);
+    }
+
+    /* backup the console device */
+    console_dev = rt_console_get_device();
+
+    /* set the new console device to dummy console */
+    rt_console_set_device(dummy_console.parent.name);
+    /* reopen the old console device for mp_putsn */
+    rt_device_open(console_dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM);
+}
+
+void mp_putsn_deinit(void) {
+    /* close the old console, it's already in mp_putsn_init */
+    rt_device_close(console_dev);
+    /* restore the old console device */
+    rt_console_set_device(console_dev->parent.name);
+
+    rt_device_unregister(&dummy_console);
+}

+ 34 - 0
port/mpputsnport.h

@@ -0,0 +1,34 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Armink (armink.ztl@gmail.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 _MPPUTCHARPORT_H_
+#define _MPPUTCHARPORT_H_
+
+void mp_putsn_init(void);
+void mp_putsn_deinit(void);
+void mp_putsn(const char *str, size_t len);
+
+#endif /* _MPPUTCHARPORT_H_ */

+ 6 - 3
port/mpy_main.c

@@ -43,7 +43,8 @@
 #include <py/frozenmod.h>
 #include <lib/mp-readline/readline.h>
 #include <lib/utils/pyexec.h>
-#include "rtt_getchar.h"
+#include "mpgetcharport.h"
+#include "mpputsnport.h"
 
 #if MICROPY_ENABLE_COMPILER
 void do_str(const char *src, mp_parse_input_kind_t input_kind) {
@@ -70,7 +71,8 @@ void mpy_main(const char *filename) {
     stack_top = (void *)&stack_dummy;
     rt_uint16_t old_flag;
 
-    rtt_getchar_init();
+    mp_getchar_init();
+    mp_putsn_init();
 
     if (rt_thread_self()->stack_size < 4096) {
         rt_kprintf("The stack (%.*s) size for executing MicroPython must be >=4096\n", RT_NAME_MAX, rt_thread_self()->name);
@@ -160,7 +162,8 @@ void mpy_main(const char *filename) {
 
     rt_free(heap);
 
-    rtt_getchar_deinit();
+    mp_putsn_deinit();
+    mp_getchar_deinit();
 }
 
 #if !MICROPY_PY_MODUOS_FILE