Переглянути джерело

1、【增加】mp_import_stat 实现机制。

Signed-off-by: armink <armink.ztl@gmail.com>
armink 8 роки тому
батько
коміт
153f9bbb41
3 змінених файлів з 29 додано та 5 видалено
  1. 16 0
      port/moduos_file.c
  2. 6 1
      port/mpconfigport.h
  3. 7 4
      port/mpy_main.c

+ 16 - 0
port/moduos_file.c

@@ -222,5 +222,21 @@ mp_obj_t mp_posix_stat(mp_obj_t path_in) {
 }
 MP_DEFINE_CONST_FUN_OBJ_1(mp_posix_stat_obj, mp_posix_stat);
 
+//TODO
+mp_import_stat_t mp_posix_import_stat(const char *path) {
+    const char *path_out;
+    mp_vfs_mount_t *vfs = mp_vfs_lookup_path(path, &path_out);
+    if (vfs == MP_VFS_NONE || vfs == MP_VFS_ROOT) {
+        return MP_IMPORT_STAT_NO_EXIST;
+    }
+    #if MICROPY_VFS_FAT
+    // fast paths for known VFS types
+    if (mp_obj_get_type(vfs->obj) == &mp_fat_vfs_type) {
+        return fat_vfs_import_stat(MP_OBJ_TO_PTR(vfs->obj), path_out);
+    }
+    #endif
+    // TODO delegate to vfs.stat() method
+    return MP_IMPORT_STAT_NO_EXIST;
+}
 
 #endif //MICROPY_MODUOS_FILE

+ 6 - 1
port/mpconfigport.h

@@ -206,12 +206,17 @@ typedef long mp_off_t;
 
 #define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len)
 
+// use vfs's functions for import stat and builtin open
+extern mp_import_stat_t mp_posix_import_stat(const char *path);
+#define mp_import_stat mp_posix_import_stat
+
 // extra built in names to add to the global namespace
 #define MICROPY_PORT_BUILTINS \
     { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) },
 
 #define MICROPY_HW_BOARD_NAME "RT-Thread Board"
-#define MICROPY_HW_MCU_NAME "stm32f4"
+#define MICROPY_HW_MCU_NAME   "stm32f4"
+#define MICROPY_PY_PATH       "/libs/mpy/"
 
 #ifdef __linux__
 #define MICROPY_MIN_USE_STDOUT (1)

+ 7 - 4
port/mpy_main.c

@@ -76,8 +76,15 @@ void mpy_main(const char *filename) {
     gc_init(heap, heap + sizeof(heap));
     #endif
 
+    /* MicroPython initialization */
     mp_init();
 
+    /* system path initialization */
+    mp_obj_list_init(mp_sys_path, 0);
+    mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
+    mp_obj_list_append(mp_sys_path, mp_obj_new_str(MICROPY_PY_PATH, strlen(MICROPY_PY_PATH), false));
+    mp_obj_list_init(mp_sys_argv, 0);
+
     if (filename) {
         pyexec_file(filename);
     } else {
@@ -118,10 +125,6 @@ void gc_collect(void) {
 //    mp_raise_OSError(ENOENT);
 //}
 
-mp_import_stat_t mp_import_stat(const char *path) {
-    return MP_IMPORT_STAT_NO_EXIST;
-}
-
 mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
     return mp_const_none;
 }