diff options
author | Damien George <damien.p.george@gmail.com> | 2017-01-29 15:15:16 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-01-30 12:26:07 +1100 |
commit | 56506fd64a40f9c5ea97888245df6f2cb8b20744 (patch) | |
tree | 152a6a95d1f3393e0b8b535e2a8f6f7a18692fd2 /cc3200/mods | |
parent | 6eafa544865a5d6dcb18f9161f7a18bd4fb6229f (diff) | |
download | micropython-56506fd64a40f9c5ea97888245df6f2cb8b20744.tar.gz micropython-56506fd64a40f9c5ea97888245df6f2cb8b20744.zip |
cc3200: Convert to use new VFS sub-system and new ooFatFs library.
Diffstat (limited to 'cc3200/mods')
-rw-r--r-- | cc3200/mods/moduos.c | 464 | ||||
-rw-r--r-- | cc3200/mods/moduos.h | 16 | ||||
-rw-r--r-- | cc3200/mods/pybflash.c | 110 | ||||
-rw-r--r-- | cc3200/mods/pybflash.h | 35 | ||||
-rw-r--r-- | cc3200/mods/pybsd.c | 46 |
5 files changed, 212 insertions, 459 deletions
diff --git a/cc3200/mods/moduos.c b/cc3200/mods/moduos.c index 1c9932b8e7..0adc755031 100644 --- a/cc3200/mods/moduos.c +++ b/cc3200/mods/moduos.c @@ -33,10 +33,12 @@ #include "py/objtuple.h" #include "py/objstr.h" #include "py/runtime.h" +#include "lib/oofatfs/ff.h" +#include "lib/oofatfs/diskio.h" #include "genhdr/mpversion.h" #include "moduos.h" -#include "diskio.h" #include "sflash_diskio.h" +#include "extmod/vfs.h" #include "extmod/vfs_fat.h" #include "random.h" #include "mpexception.h" @@ -59,155 +61,20 @@ /****************************************************************************** DECLARE PRIVATE DATA ******************************************************************************/ -STATIC uint32_t os_num_mounted_devices; STATIC os_term_dup_obj_t os_term_dup_obj; /****************************************************************************** - DECLARE PRIVATE FUNCTIONS - ******************************************************************************/ -STATIC void unmount (os_fs_mount_t *mount_obj); -STATIC bool path_equal(const char *path, const char *path_canonical); -STATIC void append_dir_item (mp_obj_t dirlist, const char *item, bool string); -STATIC void mount (mp_obj_t device, const char *path, uint pathlen, bool readonly); - -/****************************************************************************** DEFINE PUBLIC FUNCTIONS ******************************************************************************/ -void moduos_init0 (void) { - // initialize the mount objects list - mp_obj_list_init(&MP_STATE_PORT(mount_obj_list), 0); - os_num_mounted_devices = 0; -} - -os_fs_mount_t *osmount_find_by_path (const char *path) { - for (mp_uint_t i = 0; i < MP_STATE_PORT(mount_obj_list).len; i++) { - os_fs_mount_t *mount_obj = ((os_fs_mount_t *)(MP_STATE_PORT(mount_obj_list).items[i])); - if (!strcmp(path, mount_obj->path)) { - return mount_obj; - } - } - return NULL; -} - -os_fs_mount_t *osmount_find_by_volume (uint8_t vol) { - for (mp_uint_t i = 0; i < MP_STATE_PORT(mount_obj_list).len; i++) { - os_fs_mount_t *mount_obj = ((os_fs_mount_t *)(MP_STATE_PORT(mount_obj_list).items[i])); - if (vol == mount_obj->vol) { - return mount_obj; - } - } - return NULL; -} - -os_fs_mount_t *osmount_find_by_device (mp_obj_t device) { - for (mp_uint_t i = 0; i < MP_STATE_PORT(mount_obj_list).len; i++) { - os_fs_mount_t *mount_obj = ((os_fs_mount_t *)(MP_STATE_PORT(mount_obj_list).items[i])); - if (device == mount_obj->device) { - return mount_obj; - } - } - return NULL; -} - void osmount_unmount_all (void) { + //TODO + /* for (mp_uint_t i = 0; i < MP_STATE_PORT(mount_obj_list).len; i++) { os_fs_mount_t *mount_obj = ((os_fs_mount_t *)(MP_STATE_PORT(mount_obj_list).items[i])); unmount(mount_obj); } -} - -/****************************************************************************** - DEFINE PRIVATE FUNCTIONS - ******************************************************************************/ - -// Checks for path equality, ignoring trailing slashes: -// path_equal(/, /) -> true -// path_equal(/flash//, /flash) -> true -// second argument must be in canonical form (meaning no trailing slash, unless it's just /) -STATIC bool path_equal(const char *path, const char *path_canonical) { - for (; *path_canonical != '\0' && *path == *path_canonical; ++path, ++path_canonical) { - } - if (*path_canonical != '\0') { - return false; - } - for (; *path == '/'; ++path) { - } - return *path == '\0'; -} - -STATIC void append_dir_item (mp_obj_t dirlist, const char *item, bool string) { - // make a string object for this entry - mp_obj_t entry_o; - if (string) { - entry_o = mp_obj_new_str(item, strlen(item), false); - } else { - entry_o = mp_obj_new_bytes((const byte*)item, strlen(item)); - } - - // add the entry to the list - mp_obj_list_append(dirlist, entry_o); -} - -STATIC void mount (mp_obj_t device, const char *path, uint pathlen, bool readonly) { - // is the mount point already in use? - FILINFO fno; -#if _USE_LFN - fno.lfname = NULL; - fno.lfsize = 0; -#endif - // cannot mount twice or on existing paths - if (f_stat(path, &fno) == FR_OK || osmount_find_by_device(device)) { - mp_raise_msg(&mp_type_OSError, mpexception_os_request_not_possible); - } - - // create a new object - os_fs_mount_t *self = m_new_obj(os_fs_mount_t); - self->device = device; - self->path = path; - self->pathlen = pathlen; - self->vol = os_num_mounted_devices + 1; // '/flash' is volume 0 - - if (device == (mp_obj_t)&pybsd_obj) { - // need to make it different to NULL, otherwise it's read only by default - self->writeblocks[0] = mp_const_none; - self->sync[0] = MP_OBJ_NULL; // no need to sync the SD card - self->count[0] = MP_OBJ_NULL; - } else { - // load block protocol methods - mp_load_method(device, MP_QSTR_readblocks, self->readblocks); - mp_load_method_maybe(device, MP_QSTR_writeblocks, self->writeblocks); - mp_load_method_maybe(device, MP_QSTR_sync, self->sync); - mp_load_method(device, MP_QSTR_count, self->count); - } - - // Read-only device indicated by writeblocks[0] == MP_OBJ_NULL. - // User can specify read-only device by: - // 1. readonly=True keyword argument - // 2. nonexistent writeblocks method (then writeblocks[0] == MP_OBJ_NULL already) - if (readonly) { - self->writeblocks[0] = MP_OBJ_NULL; - } - - // we need to add it before doing the actual mount, so that the volume can be found - mp_obj_list_append(&MP_STATE_PORT(mount_obj_list), self); - - // actually mount it - if (f_mount(&self->fatfs, self->path, 1) != FR_OK) { - // remove it and raise - mp_obj_list_remove(&MP_STATE_PORT(mount_obj_list), self); - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); - } - - // mount succeeded, increment the count - os_num_mounted_devices++; -} - -STATIC void unmount (os_fs_mount_t *mount_obj) { - // remove it from the list and then call FatFs - f_mount (NULL, mount_obj->path, 1); - mp_obj_list_remove(&MP_STATE_PORT(mount_obj_list), mount_obj); - os_num_mounted_devices--; + */ } /******************************************************************************/ @@ -239,193 +106,6 @@ STATIC mp_obj_t os_uname(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_uname_obj, os_uname); -/// \function chdir(path) -/// Change current directory. -STATIC mp_obj_t os_chdir(mp_obj_t path_in) { - const char *path; - path = mp_obj_str_get_str(path_in); - - FRESULT res = f_chdrive(path); - - if (res == FR_OK) { - res = f_chdir(path); - } - - if (res != FR_OK) { - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); - } - - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_chdir_obj, os_chdir); - -STATIC mp_obj_t os_getcwd(void) { - char buf[MICROPY_ALLOC_PATH_MAX + 1]; - FRESULT res = f_getcwd(buf, sizeof buf); - if (res != FR_OK) { - mp_raise_OSError(fresult_to_errno_table[res]); - } - return mp_obj_new_str(buf, strlen(buf), false); -} -STATIC MP_DEFINE_CONST_FUN_OBJ_0(os_getcwd_obj, os_getcwd); - -STATIC mp_obj_t os_listdir(mp_uint_t n_args, const mp_obj_t *args) { - bool is_str_type = true; - const char *path; - mp_obj_t dir_list = mp_obj_new_list(0, NULL); - - if (n_args == 1) { - if (mp_obj_get_type(args[0]) == &mp_type_bytes) { - is_str_type = false; - } - path = mp_obj_str_get_str(args[0]); - } else { - path = ""; - } - - // "hack" to list the root directory - if (path[0] == '/' && path[1] == '\0') { - // add 'flash' to the list - append_dir_item (dir_list, "flash", is_str_type); - for (mp_uint_t i = 0; i < MP_STATE_PORT(mount_obj_list).len; i++) { - os_fs_mount_t *mount_obj = ((os_fs_mount_t *)(MP_STATE_PORT(mount_obj_list).items[i])); - append_dir_item (dir_list, &mount_obj->path[1], is_str_type); - } - } else { - FRESULT res; - DIR dir; - FILINFO fno; - #if _USE_LFN - char lfn_buf[_MAX_LFN + 1]; - fno.lfname = lfn_buf; - fno.lfsize = sizeof(lfn_buf); - #endif - - res = f_opendir(&dir, path); /* Open the directory */ - if (res != FR_OK) { - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); - } - - for ( ; ; ) { - res = f_readdir(&dir, &fno); /* Read a directory item */ - if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */ - if (fno.fname[0] == '.' && fno.fname[1] == 0) continue; /* Ignore . entry */ - if (fno.fname[0] == '.' && fno.fname[1] == '.' && fno.fname[2] == 0) continue; /* Ignore .. entry */ - - #if _USE_LFN - char *fn = *fno.lfname ? fno.lfname : fno.fname; - #else - char *fn = fno.fname; - #endif - - // add the entry to the list - append_dir_item (dir_list, fn, is_str_type); - } - f_closedir(&dir); - } - - return dir_list; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_listdir_obj, 0, 1, os_listdir); - -STATIC mp_obj_t os_mkdir(mp_obj_t path_o) { - const char *path = mp_obj_str_get_str(path_o); - FRESULT res = f_mkdir(path); - switch (res) { - case FR_OK: - return mp_const_none; - case FR_EXIST: - mp_raise_msg(&mp_type_OSError, mpexception_os_request_not_possible); - break; - default: - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mkdir_obj, os_mkdir); - -STATIC mp_obj_t os_rename(mp_obj_t path_in, mp_obj_t path_out) { - const char *old_path = mp_obj_str_get_str(path_in); - const char *new_path = mp_obj_str_get_str(path_out); - FRESULT res = f_rename(old_path, new_path); - switch (res) { - case FR_OK: - return mp_const_none; - default: - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_2(os_rename_obj, os_rename); - -STATIC mp_obj_t os_remove(mp_obj_t path_o) { - const char *path = mp_obj_str_get_str(path_o); - FRESULT res = f_unlink(path); - switch (res) { - case FR_OK: - return mp_const_none; - default: - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); - } -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_remove_obj, os_remove); - -STATIC mp_obj_t os_stat(mp_obj_t path_in) { - const char *path = mp_obj_str_get_str(path_in); - bool isbuilt_in = false; - FILINFO fno; - FRESULT res; -#if _USE_LFN - fno.lfname = NULL; - fno.lfsize = 0; -#endif - - // check on the user mounted devices - for (mp_uint_t i = 0; i < MP_STATE_PORT(mount_obj_list).len; i++) { - os_fs_mount_t *mount_obj = ((os_fs_mount_t *)(MP_STATE_PORT(mount_obj_list).items[i])); - if (path_equal(path, mount_obj->path)) { - isbuilt_in = true; - break; - } - } - - if (path_equal(path, "/") || path_equal(path, "/flash") || isbuilt_in) { - // stat built-in directory - fno.fsize = 0; - fno.fdate = 0; - fno.ftime = 0; - fno.fattrib = AM_DIR; - } else if ((res = f_stat(path, &fno)) != FR_OK) { - mp_raise_OSError(fresult_to_errno_table[res]); - } - - mp_obj_tuple_t *t = mp_obj_new_tuple(10, NULL); - mp_int_t mode = 0; - if (fno.fattrib & AM_DIR) { - mode |= 0x4000; // stat.S_IFDIR - } else { - mode |= 0x8000; // stat.S_IFREG - } - mp_int_t seconds = timeutils_seconds_since_2000( - 1980 + ((fno.fdate >> 9) & 0x7f), - (fno.fdate >> 5) & 0x0f, - fno.fdate & 0x1f, - (fno.ftime >> 11) & 0x1f, - (fno.ftime >> 5) & 0x3f, - 2 * (fno.ftime & 0x1f) - ); - t->items[0] = mp_obj_new_int(mode); // st_mode - t->items[1] = MP_OBJ_NEW_SMALL_INT(0); // st_ino - t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // st_dev - t->items[3] = MP_OBJ_NEW_SMALL_INT(0); // st_nlink - t->items[4] = MP_OBJ_NEW_SMALL_INT(0); // st_uid - t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // st_gid - t->items[6] = mp_obj_new_int(fno.fsize); // st_size - t->items[7] = mp_obj_new_int(seconds); // st_atime - t->items[8] = t->items[7]; // st_mtime - t->items[9] = t->items[7]; // st_ctime - return t; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_stat_obj, os_stat); - STATIC mp_obj_t os_sync(void) { sflash_disk_flush(); return mp_const_none; @@ -443,110 +123,6 @@ STATIC mp_obj_t os_urandom(mp_obj_t num) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom); -STATIC mp_obj_t os_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - static const mp_arg_t mount_args[] = { - { MP_QSTR_readonly, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, - }; - - // parse args - mp_obj_t device = pos_args[0]; - mp_obj_t mount_point = pos_args[1]; - mp_arg_val_t args[MP_ARRAY_SIZE(mount_args)]; - mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(mount_args), mount_args, args); - - // get the mount point - mp_uint_t pathlen; - const char *path_in = mp_obj_str_get_data(mount_point, &pathlen); - if (pathlen == 0) { - goto invalid_args; - } - - char *path = m_new(char, pathlen + 1); - memcpy(path, path_in, pathlen); - path[pathlen] = '\0'; - - // "remove" any extra slahes at the end - while (path[(pathlen - 1)] == '/') { - path[--pathlen] = '\0'; - } - - // is the mount point valid? - if (pathlen < 2 || path[0] !='/' || strchr(&path[1], '/')) { - goto invalid_args; - } - - // now mount it - mount(device, path, pathlen, args[0].u_bool); - - return mp_const_none; - -invalid_args: - mp_raise_msg(&mp_type_OSError, mpexception_value_invalid_arguments); -} -MP_DEFINE_CONST_FUN_OBJ_KW(os_mount_obj, 2, os_mount); - -STATIC mp_obj_t os_unmount(mp_obj_t path_o) { - const char *path = mp_obj_str_get_str(path_o); - - // '/flash' cannot be unmounted, also not the current working directory - if (path_equal(path, "/flash")) { - mp_raise_msg(&mp_type_OSError, mpexception_os_request_not_possible); - } - - // now unmount it - os_fs_mount_t *mount_obj; - if ((mount_obj = osmount_find_by_path(path))) { - unmount (mount_obj); - } else { - mp_raise_msg(&mp_type_ValueError, mpexception_value_invalid_arguments); - } - - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_unmount_obj, os_unmount); - -STATIC mp_obj_t os_mkfs(mp_obj_t device) { - const char *path = "/__mkfs__mnt__"; - os_fs_mount_t *mount_obj = NULL; - bool unmt = false; - FRESULT res; - - if (MP_OBJ_IS_STR_OR_BYTES(device)) { - path = mp_obj_str_get_str(device); - // otherwise the relative path check will pass... - if (path[0] != '/') { - mp_raise_msg(&mp_type_OSError, mpexception_value_invalid_arguments); - } - } else { - // mount it briefly - mount(device, path, strlen(path), false); - unmt = true; - } - - byte sfd = 0; - if (!memcmp(path, "/flash", strlen("/flash"))) { - sfd = 1; - } else if ((mount_obj = osmount_find_by_path(path))) { - if (mount_obj->device != (mp_obj_t)&pybsd_obj && - mp_obj_get_int(mp_call_method_n_kw(0, 0, mount_obj->count)) < 2048) { - sfd = 1; - } - } - - // now format the device - res = f_mkfs(path, sfd, 0); - - if (unmt && mount_obj) { - unmount (mount_obj); - } - - if (res != FR_OK) { - mp_raise_msg(&mp_type_OSError, mpexception_os_operation_failed); - } - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mkfs_obj, os_mkfs); - STATIC mp_obj_t os_dupterm(uint n_args, const mp_obj_t *args) { if (n_args == 0) { if (MP_STATE_PORT(os_term_dup_obj) == MP_OBJ_NULL) { @@ -576,22 +152,26 @@ STATIC const mp_map_elem_t os_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uos) }, { MP_OBJ_NEW_QSTR(MP_QSTR_uname), (mp_obj_t)&os_uname_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_chdir), (mp_obj_t)&os_chdir_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_getcwd), (mp_obj_t)&os_getcwd_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_listdir), (mp_obj_t)&os_listdir_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_mkdir), (mp_obj_t)&os_mkdir_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_rename), (mp_obj_t)&os_rename_obj}, - { MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&os_remove_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_rmdir), (mp_obj_t)&os_remove_obj }, // rmdir aliases to remove - { MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&os_stat_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_unlink), (mp_obj_t)&os_remove_obj }, // unlink aliases to remove + + { MP_OBJ_NEW_QSTR(MP_QSTR_chdir), (mp_obj_t)&mp_vfs_chdir_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_getcwd), (mp_obj_t)&mp_vfs_getcwd_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_listdir), (mp_obj_t)&mp_vfs_listdir_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_mkdir), (mp_obj_t)&mp_vfs_mkdir_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_rename), (mp_obj_t)&mp_vfs_rename_obj}, + { MP_OBJ_NEW_QSTR(MP_QSTR_remove), (mp_obj_t)&mp_vfs_remove_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_rmdir), (mp_obj_t)&mp_vfs_rmdir_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_stat), (mp_obj_t)&mp_vfs_stat_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_unlink), (mp_obj_t)&mp_vfs_remove_obj }, // unlink aliases to remove + { MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&os_sync_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_urandom), (mp_obj_t)&os_urandom_obj }, // MicroPython additions - { MP_OBJ_NEW_QSTR(MP_QSTR_mount), (mp_obj_t)&os_mount_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_unmount), (mp_obj_t)&os_unmount_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_mkfs), (mp_obj_t)&os_mkfs_obj }, + // removed: mkfs + // renamed: unmount -> umount + { MP_OBJ_NEW_QSTR(MP_QSTR_mount), (mp_obj_t)&mp_vfs_mount_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_umount), (mp_obj_t)&mp_vfs_umount_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_VfsFat), (mp_obj_t)&mp_fat_vfs_type }, { MP_OBJ_NEW_QSTR(MP_QSTR_dupterm), (mp_obj_t)&os_dupterm_obj }, /// \constant sep - separation character used in paths diff --git a/cc3200/mods/moduos.h b/cc3200/mods/moduos.h index 1ebf16cdfa..4c8bc96595 100644 --- a/cc3200/mods/moduos.h +++ b/cc3200/mods/moduos.h @@ -28,22 +28,11 @@ #ifndef MODUOS_H_ #define MODUOS_H_ -#include "ff.h" +#include "py/obj.h" /****************************************************************************** DEFINE PUBLIC TYPES ******************************************************************************/ -typedef struct _os_fs_mount_t { - mp_obj_t device; - const char *path; - mp_uint_t pathlen; - mp_obj_t readblocks[4]; - mp_obj_t writeblocks[4]; - mp_obj_t sync[2]; - mp_obj_t count[2]; - FATFS fatfs; - uint8_t vol; -} os_fs_mount_t; typedef struct _os_term_dup_obj_t { mp_obj_t stream_o; @@ -54,9 +43,6 @@ typedef struct _os_term_dup_obj_t { /****************************************************************************** DECLARE PUBLIC FUNCTIONS ******************************************************************************/ -void moduos_init0 (void); -os_fs_mount_t *osmount_find_by_path (const char *path); -os_fs_mount_t *osmount_find_by_volume (uint8_t vol); void osmount_unmount_all (void); #endif // MODUOS_H_ diff --git a/cc3200/mods/pybflash.c b/cc3200/mods/pybflash.c new file mode 100644 index 0000000000..0779f4a05c --- /dev/null +++ b/cc3200/mods/pybflash.c @@ -0,0 +1,110 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013-2017 Damien P. George + * + * 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 <stdint.h> +//#include <string.h> + +#include "py/runtime.h" +#include "lib/oofatfs/ff.h" +#include "lib/oofatfs/diskio.h" +#include "extmod/vfs_fat.h" +#include "extmod/fsusermount.h" + +#include "fatfs/src/drivers/sflash_diskio.h" +#include "mods/pybflash.h" + +/******************************************************************************/ +// MicroPython bindings to expose the internal flash as an object with the +// block protocol. + +// there is a singleton Flash object +STATIC const mp_obj_base_t pyb_flash_obj = {&pyb_flash_type}; + +STATIC mp_obj_t pyb_flash_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + // check arguments + mp_arg_check_num(n_args, n_kw, 0, 0, false); + + // return singleton object + return (mp_obj_t)&pyb_flash_obj; +} + +STATIC mp_obj_t pyb_flash_readblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE); + DRESULT res = sflash_disk_read(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SFLASH_SECTOR_SIZE); + return MP_OBJ_NEW_SMALL_INT(res != RES_OK); // return of 0 means success +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_readblocks_obj, pyb_flash_readblocks); + +STATIC mp_obj_t pyb_flash_writeblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ); + DRESULT res = sflash_disk_write(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SFLASH_SECTOR_SIZE); + return MP_OBJ_NEW_SMALL_INT(res != RES_OK); // return of 0 means success +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_writeblocks_obj, pyb_flash_writeblocks); + +STATIC mp_obj_t pyb_flash_ioctl(mp_obj_t self, mp_obj_t cmd_in, mp_obj_t arg_in) { + mp_int_t cmd = mp_obj_get_int(cmd_in); + switch (cmd) { + case BP_IOCTL_INIT: return MP_OBJ_NEW_SMALL_INT(sflash_disk_init() != RES_OK); + case BP_IOCTL_DEINIT: sflash_disk_flush(); return MP_OBJ_NEW_SMALL_INT(0); + case BP_IOCTL_SYNC: sflash_disk_flush(); return MP_OBJ_NEW_SMALL_INT(0); + case BP_IOCTL_SEC_COUNT: return MP_OBJ_NEW_SMALL_INT(SFLASH_SECTOR_COUNT); + case BP_IOCTL_SEC_SIZE: return MP_OBJ_NEW_SMALL_INT(SFLASH_SECTOR_SIZE); + default: return mp_const_none; + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_flash_ioctl_obj, pyb_flash_ioctl); + +STATIC const mp_map_elem_t pyb_flash_locals_dict_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR_readblocks), (mp_obj_t)&pyb_flash_readblocks_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_writeblocks), (mp_obj_t)&pyb_flash_writeblocks_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_ioctl), (mp_obj_t)&pyb_flash_ioctl_obj }, +}; + +STATIC MP_DEFINE_CONST_DICT(pyb_flash_locals_dict, pyb_flash_locals_dict_table); + +const mp_obj_type_t pyb_flash_type = { + { &mp_type_type }, + .name = MP_QSTR_Flash, + .make_new = pyb_flash_make_new, + .locals_dict = (mp_obj_t)&pyb_flash_locals_dict, +}; + +void pyb_flash_init_vfs(fs_user_mount_t *vfs) { + vfs->base.type = &mp_fat_vfs_type; + vfs->flags |= FSUSER_NATIVE | FSUSER_HAVE_IOCTL; + vfs->fatfs.drv = vfs; + vfs->readblocks[0] = (mp_obj_t)&pyb_flash_readblocks_obj; + vfs->readblocks[1] = (mp_obj_t)&pyb_flash_obj; + vfs->readblocks[2] = (mp_obj_t)sflash_disk_read; // native version + vfs->writeblocks[0] = (mp_obj_t)&pyb_flash_writeblocks_obj; + vfs->writeblocks[1] = (mp_obj_t)&pyb_flash_obj; + vfs->writeblocks[2] = (mp_obj_t)sflash_disk_write; // native version + vfs->u.ioctl[0] = (mp_obj_t)&pyb_flash_ioctl_obj; + vfs->u.ioctl[1] = (mp_obj_t)&pyb_flash_obj; +} diff --git a/cc3200/mods/pybflash.h b/cc3200/mods/pybflash.h new file mode 100644 index 0000000000..6f8ddf2918 --- /dev/null +++ b/cc3200/mods/pybflash.h @@ -0,0 +1,35 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Damien P. George + * + * 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_CC3200_MODS_PYBFLASH_H +#define MICROPY_INCLUDED_CC3200_MODS_PYBFLASH_H + +#include "py/obj.h" + +extern const mp_obj_type_t pyb_flash_type; + +void pyb_flash_init_vfs(fs_user_mount_t *vfs); + +#endif // MICROPY_INCLUDED_CC3200_MODS_PYBFLASH_H diff --git a/cc3200/mods/pybsd.c b/cc3200/mods/pybsd.c index b8d5b4cc1a..bac5a270c8 100644 --- a/cc3200/mods/pybsd.c +++ b/cc3200/mods/pybsd.c @@ -27,6 +27,9 @@ #include "py/mpconfig.h" #include "py/obj.h" #include "py/runtime.h" +#include "lib/oofatfs/ff.h" +#include "lib/oofatfs/diskio.h" +#include "extmod/fsusermount.h" #include "inc/hw_types.h" #include "inc/hw_gpio.h" #include "inc/hw_ints.h" @@ -36,8 +39,6 @@ #include "prcm.h" #include "gpio.h" #include "sdhost.h" -#include "ff.h" -#include "diskio.h" #include "sd_diskio.h" #include "pybsd.h" #include "mpexception.h" @@ -163,9 +164,50 @@ STATIC mp_obj_t pyb_sd_deinit (mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_sd_deinit_obj, pyb_sd_deinit); +STATIC mp_obj_t pyb_sd_readblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_WRITE); + DRESULT res = sd_disk_read(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SD_SECTOR_SIZE); + return MP_OBJ_NEW_SMALL_INT(res != RES_OK); // return of 0 means success +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sd_readblocks_obj, pyb_sd_readblocks); + +STATIC mp_obj_t pyb_sd_writeblocks(mp_obj_t self, mp_obj_t block_num, mp_obj_t buf) { + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ); + DRESULT res = sd_disk_write(bufinfo.buf, mp_obj_get_int(block_num), bufinfo.len / SD_SECTOR_SIZE); + return MP_OBJ_NEW_SMALL_INT(res != RES_OK); // return of 0 means success +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sd_writeblocks_obj, pyb_sd_writeblocks); + +STATIC mp_obj_t pyb_sd_ioctl(mp_obj_t self, mp_obj_t cmd_in, mp_obj_t arg_in) { + mp_int_t cmd = mp_obj_get_int(cmd_in); + switch (cmd) { + case BP_IOCTL_INIT: + case BP_IOCTL_DEINIT: + case BP_IOCTL_SYNC: + // nothing to do + return MP_OBJ_NEW_SMALL_INT(0); // success + + case BP_IOCTL_SEC_COUNT: + return MP_OBJ_NEW_SMALL_INT(sd_disk_info.ulNofBlock * (sd_disk_info.ulBlockSize / 512)); + + case BP_IOCTL_SEC_SIZE: + return MP_OBJ_NEW_SMALL_INT(SD_SECTOR_SIZE); + + default: // unknown command + return MP_OBJ_NEW_SMALL_INT(-1); // error + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_sd_ioctl_obj, pyb_sd_ioctl); + STATIC const mp_map_elem_t pyb_sd_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pyb_sd_init_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&pyb_sd_deinit_obj }, + // block device protocol + { MP_OBJ_NEW_QSTR(MP_QSTR_readblocks), (mp_obj_t)&pyb_sd_readblocks_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_writeblocks), (mp_obj_t)&pyb_sd_writeblocks_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_ioctl), (mp_obj_t)&pyb_sd_ioctl_obj }, }; STATIC MP_DEFINE_CONST_DICT(pyb_sd_locals_dict, pyb_sd_locals_dict_table); |