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

Merge remote-tracking branch 'remotes/origin/master' into 723

SummerGift 6 лет назад
Родитель
Сommit
c12d94e5a9
43 измененных файлов с 167 добавлено и 160 удалено
  1. 4 0
      SConscript
  2. 0 40
      port/fdfile.h
  3. 0 40
      port/help.c
  4. 0 0
      port/modules/machine/machine_adc.c
  5. 0 0
      port/modules/machine/machine_adc.h
  6. 1 1
      port/modules/machine/machine_hw_i2c.c
  7. 1 1
      port/modules/machine/machine_hw_spi.c
  8. 0 0
      port/modules/machine/machine_lcd.c
  9. 0 0
      port/modules/machine/machine_lcd.h
  10. 0 0
      port/modules/machine/machine_pin.c
  11. 0 0
      port/modules/machine/machine_pwm.c
  12. 0 0
      port/modules/machine/machine_pwm.h
  13. 0 0
      port/modules/machine/machine_rtc.c
  14. 0 0
      port/modules/machine/machine_rtc.h
  15. 0 0
      port/modules/machine/machine_timer.c
  16. 0 0
      port/modules/machine/machine_timer.h
  17. 2 2
      port/modules/machine/machine_uart.c
  18. 0 0
      port/modules/machine/machine_uart.h
  19. 0 0
      port/modules/machine/machine_wdt.c
  20. 0 0
      port/modules/machine/machine_wdt.h
  21. 10 10
      port/modules/machine/modmachine.c
  22. 0 0
      port/modules/machine/modmachine.h
  23. 0 0
      port/modules/modffi.c
  24. 5 2
      port/modules/modfile.c
  25. 0 0
      port/modules/modnetwork.c
  26. 0 0
      port/modules/modnetwork.h
  27. 2 2
      port/modules/modnetwork_wlan.c
  28. 0 1
      port/modules/modpyb.c
  29. 1 1
      port/modules/modrtthread.c
  30. 0 1
      port/modules/moduos.c
  31. 9 9
      port/modules/moduos_file.c
  32. 0 0
      port/modules/moduos_file.h
  33. 1 1
      port/modules/modusocket.c
  34. 0 0
      port/modules/modutils.c
  35. 0 0
      port/modules/modutime.c
  36. 1 1
      port/mpconfigport.h
  37. 4 4
      port/mpgetcharport.c
  38. 6 6
      port/mpgetcharport.h
  39. 18 9
      port/mphalport.c
  40. 80 0
      port/mpputsnport.c
  41. 8 9
      port/mpputsnport.h
  42. 13 19
      port/mpy_main.c
  43. 1 1
      port/native/native_module.c

+ 4 - 0
SConscript

@@ -9,6 +9,8 @@ src    += Glob('lib/mp-readline/*.c')
 src    += Glob('lib/utils/*.c')
 src    += Glob('extmod/*.c')
 src    += Glob('port/*.c')
+src    += Glob('port/modules/*.c')
+src    += Glob('port/modules/machine/*.c')
 src    += Glob('lib/netutils/*.c')
 src    += Glob('lib/timeutils/*.c')
 src    += Glob('drivers/bus/*.c')
@@ -16,6 +18,8 @@ src    += Glob('port/native/*.c')
 
 path    = [cwd + '/']
 path   += [cwd + '/port']
+path   += [cwd + '/port/modules']
+path   += [cwd + '/port/modules/machine']
 
 LOCAL_CCFLAGS = ''
 

+ 0 - 40
port/fdfile.h

@@ -1,40 +0,0 @@
-/*
- * This file is part of the MicroPython project, http://micropython.org/
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2013, 2014 Damien P. George
- * Copyright (c) 2016 Paul Sokolovsky
- *
- * 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 MICROPY_INCLUDED_UNIX_FDFILE_H
-#define MICROPY_INCLUDED_UNIX_FDFILE_H
-
-#include "py/obj.h"
-
-typedef struct _mp_obj_fdfile_t {
-    mp_obj_base_t base;
-    int fd;
-} mp_obj_fdfile_t;
-
-extern const mp_obj_type_t mp_type_fileio;
-extern const mp_obj_type_t mp_type_textio;
-
-#endif // MICROPY_INCLUDED_UNIX_FDFILE_H

+ 0 - 40
port/help.c

@@ -1,40 +0,0 @@
-/*
- * This file is part of the MicroPython project, http://micropython.org/
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2017 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 "py/builtin.h"
-
-const char rtthread_help_text[] =
-"Welcome to MicroPython on RT-Thread!\n"
-"\n"
-"Control commands:\n"
-"  CTRL-A        -- on a blank line, enter raw REPL mode\n"
-"  CTRL-B        -- on a blank line, enter normal REPL mode\n"
-"  CTRL-C        -- interrupt a running program\n"
-"  CTRL-D        -- on a blank line, do a soft reset of the board\n"
-"  CTRL-E        -- on a blank line, enter paste mode\n"
-"\n"
-"For further help on a specific object, type help(obj)\n"
-;

+ 0 - 0
port/machine_adc.c → port/modules/machine/machine_adc.c


+ 0 - 0
port/machine_adc.h → port/modules/machine/machine_adc.h


+ 1 - 1
port/machine_hw_i2c.c → port/modules/machine/machine_hw_i2c.c

@@ -83,7 +83,7 @@ mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, siz
     struct rt_i2c_bus_device *i2c_bus = rt_i2c_bus_device_find(iic_device);
 
     if (i2c_bus == RT_NULL) {
-        rt_kprintf("can't find %s device\r\n", iic_device);
+        mp_printf(&mp_plat_print, "can't find %s device\r\n", iic_device);
         nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "I2C(%s) doesn't exist", iic_device));
     }
 

+ 1 - 1
port/machine_hw_spi.c → port/modules/machine/machine_hw_spi.c

@@ -56,7 +56,7 @@ mp_obj_t machine_hard_spi_make_new(const mp_obj_type_t *type, size_t n_args, siz
 
     struct rt_spi_device *rt_spi_device = (struct rt_spi_device *) rt_device_find(spi_dev_name);
     if (rt_spi_device == RT_NULL || rt_spi_device->parent.type != RT_Device_Class_SPIDevice) {
-        rt_kprintf("ERROR: SPI device %s not found!\n", spi_dev_name);
+        mp_printf(&mp_plat_print, "ERROR: SPI device %s not found!\n", spi_dev_name);
         nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "SPI(%s) doesn't exist", spi_dev_name));
     }
 

+ 0 - 0
port/machine_lcd.c → port/modules/machine/machine_lcd.c


+ 0 - 0
port/machine_lcd.h → port/modules/machine/machine_lcd.h


+ 0 - 0
port/machine_pin.c → port/modules/machine/machine_pin.c


+ 0 - 0
port/machine_pwm.c → port/modules/machine/machine_pwm.c


+ 0 - 0
port/machine_pwm.h → port/modules/machine/machine_pwm.h


+ 0 - 0
port/machine_rtc.c → port/modules/machine/machine_rtc.c


+ 0 - 0
port/machine_rtc.h → port/modules/machine/machine_rtc.h


+ 0 - 0
port/machine_timer.c → port/modules/machine/machine_timer.c


+ 0 - 0
port/machine_timer.h → port/modules/machine/machine_timer.h


+ 2 - 2
port/machine_uart.c → port/modules/machine/machine_uart.c

@@ -63,7 +63,7 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
 
     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);
+        mp_printf(&mp_plat_print, "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));
     }
 	
@@ -71,7 +71,7 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
     result = rt_device_open((rt_device_t)rt_serial_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX );
     if (result != RT_EOK)
     {
-        rt_kprintf("ERROR: UART device %s can't open!\n", uart_dev_name);
+        mp_printf(&mp_plat_print, "ERROR: UART device %s can't open!\n", uart_dev_name);
         nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "UART(%s) can't open", uart_dev_name));
     }
 

+ 0 - 0
port/machine_uart.h → port/modules/machine/machine_uart.h


+ 0 - 0
port/machine_wdt.c → port/modules/machine/machine_wdt.c


+ 0 - 0
port/machine_wdt.h → port/modules/machine/machine_wdt.h


+ 10 - 10
port/modmachine.c → port/modules/machine/modmachine.c

@@ -55,9 +55,9 @@ STATIC mp_obj_t machine_info(uint n_args, const mp_obj_t *args) {
 #endif
     // RT-Thread info
     {
-        rt_kprintf("---------------------------------------------\n");
-        rt_kprintf("RT-Thread\n");
-        rt_kprintf("---------------------------------------------\n");
+        mp_printf(&mp_plat_print, "---------------------------------------------\n");
+        mp_printf(&mp_plat_print, "RT-Thread\n");
+        mp_printf(&mp_plat_print, "---------------------------------------------\n");
 
 #ifdef RT_USING_FINSH
         extern void list_mem(void);
@@ -71,25 +71,25 @@ STATIC mp_obj_t machine_info(uint n_args, const mp_obj_t *args) {
 
         list_thread();
 #endif
-        rt_kprintf("---------------------------------------------\n");
+        mp_printf(&mp_plat_print, "---------------------------------------------\n");
     }
 
     // qstr info
     {
         mp_uint_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
         qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
-        rt_kprintf("qstr:\n  n_pool=" UINT_FMT "\n  n_qstr=" UINT_FMT "\n  n_str_data_bytes=" UINT_FMT "\n  n_total_bytes=" UINT_FMT "\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
+        mp_printf(&mp_plat_print, "qstr:\n  n_pool=" UINT_FMT "\n  n_qstr=" UINT_FMT "\n  n_str_data_bytes=" UINT_FMT "\n  n_total_bytes=" UINT_FMT "\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
     }
-    rt_kprintf("---------------------------------------------\n");
+    mp_printf(&mp_plat_print, "---------------------------------------------\n");
 
     // GC info
     {
         gc_info_t info;
         gc_info(&info);
-        rt_kprintf("GC:\n");
-        rt_kprintf("  " UINT_FMT " total\n", info.total);
-        rt_kprintf("  " UINT_FMT " : " UINT_FMT "\n", info.used, info.free);
-        rt_kprintf("  1=" UINT_FMT " 2=" UINT_FMT " m=" UINT_FMT "\n", info.num_1block, info.num_2block, info.max_block);
+        mp_printf(&mp_plat_print, "GC:\n");
+        mp_printf(&mp_plat_print, "  " UINT_FMT " total\n", info.total);
+        mp_printf(&mp_plat_print, "  " UINT_FMT " : " UINT_FMT "\n", info.used, info.free);
+        mp_printf(&mp_plat_print, "  1=" UINT_FMT " 2=" UINT_FMT " m=" UINT_FMT "\n", info.num_1block, info.num_2block, info.max_block);
     }
 
     // free space on flash

+ 0 - 0
port/modmachine.h → port/modules/machine/modmachine.h


+ 0 - 0
port/modffi.c → port/modules/modffi.c


+ 5 - 2
port/file.c → port/modules/modfile.c

@@ -32,7 +32,6 @@
 #include "py/stream.h"
 #include "py/builtin.h"
 #include "py/mphal.h"
-#include "fdfile.h"
 
 #include <stdio.h>
 #include <unistd.h>
@@ -41,11 +40,15 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 
-
 #ifdef _WIN32
 #define fsync _commit
 #endif
 
+typedef struct _mp_obj_fdfile_t {
+    mp_obj_base_t base;
+    int fd;
+} mp_obj_fdfile_t;
+
 #ifdef MICROPY_CPYTHON_COMPAT
 STATIC void check_fd_is_open(const mp_obj_fdfile_t *o) {
     if (o->fd < 0) {

+ 0 - 0
port/modnetwork.c → port/modules/modnetwork.c


+ 0 - 0
port/modnetwork.h → port/modules/modnetwork.h


+ 2 - 2
port/network_wlan.c → port/modules/modnetwork_wlan.c

@@ -280,7 +280,7 @@ void wlan_station_scan(void)
     }
     else
     {
-        rt_kprintf("wifi scan result is null\n");
+        mp_printf(&mp_plat_print, ("wifi scan result is null\n");
         *wlan_scan_list = MP_OBJ_NULL;
     }
 }
@@ -323,7 +323,7 @@ STATIC mp_obj_t wlan_ifconfig(size_t n_args, const mp_obj_t *args) {
     struct netdev *netdev = netdev_default;
     if (netdev == RT_NULL)
     {
-        rt_kprintf("not find wlan interface device.\n");
+        mp_printf(&mp_plat_print, ("not find wlan interface device.\n");
         return MP_OBJ_NEW_SMALL_INT(-1);
     }
 

+ 0 - 1
port/modpyb.c → port/modules/modpyb.c

@@ -32,7 +32,6 @@
 #include "py/builtin.h"
 #include "py/mphal.h"
 #include "lib/utils/pyexec.h"
-#include "portmodules.h"
 #include "modmachine.h"
 #include "extmod/vfs.h"
 #include "extmod/utime_mphal.h"

+ 1 - 1
port/modrtthread.c → port/modules/modrtthread.c

@@ -55,7 +55,7 @@ STATIC mp_obj_t mod_stacks_analyze(void) {
     extern long list_thread(void);
     list_thread();
 #else
-    rt_kprintf("Not available when FINSH module disable\n");
+    mp_printf(&mp_plat_print, "Not available when FINSH module disable\n");
 #endif
 
     return mp_const_none;

+ 0 - 1
port/moduos.c → port/modules/moduos.c

@@ -38,7 +38,6 @@
 #include "lib/timeutils/timeutils.h"
 #include "extmod/misc.h"
 #include "genhdr/mpversion.h"
-#include "portmodules.h"
 
 #if !MICROPY_VFS
 #if MICROPY_PY_MODUOS_FILE

+ 9 - 9
port/moduos_file.c → port/modules/moduos_file.c

@@ -74,7 +74,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_posix_mkfs_obj, 1, 2, mp_posix_mkfs);
 mp_obj_t mp_posix_chdir(mp_obj_t path_in) {
     const char *changepath = mp_obj_str_get_str(path_in);
     if (chdir(changepath) != 0) {
-        rt_kprintf("No such directory: %s\n", changepath);
+        mp_printf(&mp_plat_print, "No such directory: %s\n", changepath);
     }
     return mp_const_none;
 }
@@ -152,7 +152,7 @@ mp_obj_t mp_posix_listdir(size_t n_args, const mp_obj_t *args) {
                     mp_obj_get_array_fixed_n(next, 3, &items);
                     mp_obj_list_append(dir_list, items[0]);
                 } else {
-                    rt_kprintf("BAD file: %s\n", dirent.d_name);
+                    mp_printf(&mp_plat_print, "BAD file: %s\n", dirent.d_name);
                 }
                 rt_free(fullpath);
             }
@@ -162,7 +162,7 @@ mp_obj_t mp_posix_listdir(size_t n_args, const mp_obj_t *args) {
     }
     else
     {
-//        rt_kprintf("No such directory\n");
+//        mp_printf(&mp_plat_print, "No such directory\n");
     }
     if (pathname == NULL)
         rt_free(path);
@@ -184,12 +184,12 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_posix_mkdir_obj, mp_posix_mkdir);
 mp_obj_t mp_posix_remove(uint n_args, const mp_obj_t *arg) {
     int index;
     if (n_args == 0) {
-        rt_kprintf("Usage: rm FILE...\n");
-        rt_kprintf("Remove (unlink) the FILE(s).\n");
+        mp_printf(&mp_plat_print, "Usage: rm FILE...\n");
+        mp_printf(&mp_plat_print, "Remove (unlink) the FILE(s).\n");
         return mp_const_none;
     }
     for (index = 0; index < n_args; index++) {
-        //rt_kprintf("Remove %s.\n", mp_obj_str_get_str(arg[index]));
+        //mp_printf(&mp_plat_print, "Remove %s.\n", mp_obj_str_get_str(arg[index]));
         unlink(mp_obj_str_get_str(arg[index]));
     }
     // TODO  recursive deletion
@@ -211,12 +211,12 @@ MP_DEFINE_CONST_FUN_OBJ_2(mp_posix_rename_obj, mp_posix_rename);
 mp_obj_t mp_posix_rmdir(uint n_args, const mp_obj_t *arg) {
     int index;
     if (n_args == 0) {
-        rt_kprintf("Usage: rm FILE...\n");
-        rt_kprintf("Remove (unlink) the FILE(s).\n");
+        mp_printf(&mp_plat_print, "Usage: rm FILE...\n");
+        mp_printf(&mp_plat_print, "Remove (unlink) the FILE(s).\n");
         return mp_const_none;
     }
     for (index = 0; index < n_args; index++) {
-        //rt_kprintf("Remove %s.\n", mp_obj_str_get_str(arg[index]));
+        //mp_printf(&mp_plat_print, "Remove %s.\n", mp_obj_str_get_str(arg[index]));
         rmdir(mp_obj_str_get_str(arg[index]));
     }
     // TODO  recursive deletion

+ 0 - 0
port/moduos_file.h → port/modules/moduos_file.h


+ 1 - 1
port/modusocket.c → port/modules/modusocket.c

@@ -548,7 +548,7 @@ STATIC mp_obj_t mod_usocket_getaddrinfo(uint n_args, const mp_obj_t *arg) {
     ret = getaddrinfo(host, NULL, &hint, &res);
     MP_THREAD_GIL_ENTER();
     if (ret != 0) {
-        rt_kprintf("getaddrinfo err: %d '%s'\n", ret, host);
+        mp_printf(&mp_plat_print, "getaddrinfo err: %d '%s'\n", ret, host);
         nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "no available netif"));
     }
 

+ 0 - 0
port/modutils.c → port/modules/modutils.c


+ 0 - 0
port/modutime.c → port/modules/modutime.c


+ 1 - 1
port/mpconfigport.h

@@ -512,5 +512,5 @@ extern const struct _mp_obj_module_t mp_module_network;
     MODUZLIB_PORT_BUILTIN_MODULE_WEAK_LINKS \
     MODUSTRUCT_PORT_BUILTIN_MODULE_WEAK_LINKS \
 
-#define MP_RTT_NOT_IMPL_PRINT rt_kprintf("Not implement on %s:%ld, Please add for your board!\n", __FILE__, __LINE__)
+#define MP_RTT_NOT_IMPL_PRINT mp_printf(&mp_plat_print, "Not implement on %s:%ld, Please add for your board!\n", __FILE__, __LINE__)
 

+ 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_ */

+ 18 - 9
port/mphalport.c

@@ -31,12 +31,26 @@
 #include <py/mpconfig.h>
 #include <py/runtime.h>
 #include "mphalport.h"
-#include "rtt_getchar.h"
+#include "mpgetcharport.h"
+#include "mpputsnport.h"
+
+const char rtthread_help_text[] =
+"Welcome to MicroPython on RT-Thread!\n"
+"\n"
+"Control commands:\n"
+"  CTRL-A        -- on a blank line, enter raw REPL mode\n"
+"  CTRL-B        -- on a blank line, enter normal REPL mode\n"
+"  CTRL-C        -- interrupt a running program\n"
+"  CTRL-D        -- on a blank line, do a soft reset of the board\n"
+"  CTRL-E        -- on a blank line, enter paste mode\n"
+"\n"
+"For further help on a specific object, type help(obj)\n"
+;
 
 int mp_hal_stdin_rx_chr(void) {
     char ch;
     while (1) {
-        ch = rtt_getchar();
+        ch = mp_getchar();
         if (ch != (char)0xFF) {
             break;
         }
@@ -48,16 +62,11 @@ int mp_hal_stdin_rx_chr(void) {
 
 // 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_stream(str, len);
 }
 
 mp_uint_t mp_hal_ticks_us(void) {

+ 80 - 0
port/mpputsnport.c

@@ -0,0 +1,80 @@
+/*
+ * 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 };
+static rt_uint16_t console_open_flag;
+
+void mp_putsn(const char *str, size_t len) {
+    if (console_dev) {
+        rt_device_write(console_dev, 0, str, len);
+    }
+}
+
+void mp_putsn_stream(const char *str, size_t len) {
+    if (console_dev) {
+        rt_uint16_t old_flag = console_dev->open_flag;
+
+        console_dev->open_flag |= RT_DEVICE_FLAG_STREAM;
+        rt_device_write(console_dev, 0, str, len);
+        console_dev->open_flag = old_flag;
+    }
+}
+
+void mp_putsn_init(void) {
+    {/* register dummy console device */
+#ifdef RT_USING_DEVICE_OPS
+        static struct rt_device_ops _ops = {0};
+        dummy_console.ops = &_ops
+#endif
+
+        dummy_console.type = RT_Device_Class_Char;
+
+        rt_device_register(&dummy_console, "dummy", RT_DEVICE_FLAG_RDWR);
+    }
+
+    /* backup the console device */
+    console_dev = rt_console_get_device();
+    console_open_flag = console_dev->open_flag;
+
+    /* 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);
+}
+
+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);
+    console_dev->open_flag = console_open_flag;
+
+    rt_device_unregister(&dummy_console);
+}

+ 8 - 9
port/portmodules.h → port/mpputsnport.h

@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (c) 2017 Armink (armink.ztl@gmail.com)
+ * 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
@@ -24,13 +24,12 @@
  * THE SOFTWARE.
  */
 
-#ifndef _PORTMODULES_H
-#define _PORTMODULES_H
+#ifndef _MPPUTCHARPORT_H_
+#define _MPPUTCHARPORT_H_
 
-MP_DECLARE_CONST_FUN_OBJ_1(time_sleep_ms_obj);
-MP_DECLARE_CONST_FUN_OBJ_1(time_sleep_us_obj);
+void mp_putsn_init(void);
+void mp_putsn_deinit(void);
+void mp_putsn(const char *str, size_t len);
+void mp_putsn_stream(const char *str, size_t len);
 
-MP_DECLARE_CONST_FUN_OBJ_0(mod_os_sync_obj);
-MP_DECLARE_CONST_FUN_OBJ_KW(mp_os_mount_obj);
-
-#endif // _PORTMODULES_H
+#endif /* _MPPUTCHARPORT_H_ */

+ 13 - 19
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) {
@@ -68,12 +69,12 @@ static char *heap = RT_NULL;
 void mpy_main(const char *filename) {
     int stack_dummy;
     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);
+        mp_printf(&mp_plat_print, "The stack (%.*s) size for executing MicroPython must be >=4096\n", RT_NAME_MAX, rt_thread_self()->name);
     }
 
 #if MICROPY_PY_THREAD
@@ -87,7 +88,7 @@ void mpy_main(const char *filename) {
 #if MICROPY_ENABLE_GC
     heap = rt_malloc(MICROPY_HEAP_SIZE);
     if (!heap) {
-        rt_kprintf("No memory for MicroPython Heap!\n");
+        mp_printf(&mp_plat_print, "No memory for MicroPython Heap!\n");
         return;
     }
     gc_init(heap, heap + MICROPY_HEAP_SIZE);
@@ -103,14 +104,9 @@ void mpy_main(const char *filename) {
     mp_obj_list_init(mp_sys_argv, 0);
     readline_init0();
 
-    /* Save the open flag */
-    old_flag = rt_console_get_device()->open_flag;
-    /* clean the stream flag. stream flag will automatically append '\r' */
-    rt_console_get_device()->open_flag &= ~RT_DEVICE_FLAG_STREAM;
-
     if (filename) {
 #ifndef MICROPYTHON_USING_UOS
-        rt_kprintf("Please enable uos module in sys module option first.\n");
+        mp_printf(&mp_plat_print, "Please enable uos module in sys module option first.\n");
 #else
         pyexec_file(filename);
 #endif
@@ -133,7 +129,7 @@ void mpy_main(const char *filename) {
         }
 #endif /* MICROPYTHON_USING_UOS */
 
-        rt_kprintf("\n");
+        mp_printf(&mp_plat_print, "\n");
         for (;;) {
             if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
                 if (pyexec_raw_repl() != 0) {
@@ -147,9 +143,6 @@ void mpy_main(const char *filename) {
         }
     }
 
-    /* restore the open flag */
-    rt_console_get_device()->open_flag = old_flag;
-
     gc_sweep_all();
 
     mp_deinit();
@@ -160,7 +153,8 @@ void mpy_main(const char *filename) {
 
     rt_free(heap);
 
-    rtt_getchar_deinit();
+    mp_putsn_deinit();
+    mp_getchar_deinit();
 }
 
 #if !MICROPY_PY_MODUOS_FILE
@@ -170,13 +164,13 @@ mp_import_stat_t mp_import_stat(const char *path) {
 #endif
 
 NORETURN void nlr_jump_fail(void *val) {
-    DEBUG_printf("nlr_jump_fail\n");
+    mp_printf(MICROPY_ERROR_PRINTER, "nlr_jump_fail\n");
     while (1);
 }
 
 #ifndef NDEBUG
 void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {
-    rt_kprintf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);
+    mp_printf(MICROPY_ERROR_PRINTER, "Assertion '%s' failed, at file %s:%d\n", expr, file, line);
     RT_ASSERT(0);
 }
 #endif
@@ -192,7 +186,7 @@ int DEBUG_printf(const char *format, ...)
     va_start(args, format);
     /* must use vprintf to print */
     rt_vsprintf(log_buf, format, args);
-    rt_kprintf("%s", log_buf);
+    mp_printf(&mp_plat_print, "%s", log_buf);
     va_end(args);
 
     return 0;

+ 1 - 1
port/native/native_module.c

@@ -50,7 +50,7 @@
  */
 
 void native_module_show(const char *str) {
-    rt_kprintf("Native module show: %s\n", str);
+    mp_printf(&mp_plat_print, "Native module show: %s\n", str);
 }
 RTM_EXPORT(native_module_show)