From e2d13d934a19651bb7c06c0212a1b875d8b9be1b Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 23 Jan 2017 14:35:00 +1100 Subject: extmod/modframebuf: Clip pixels drawn by line method. --- extmod/modframebuf.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'extmod') diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index 93d4922c9..e2441f194 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -302,9 +302,13 @@ STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args) { mp_int_t e = 2 * dy - dx; for (mp_int_t i = 0; i < dx; ++i) { if (steep) { - setpixel(self, y1, x1, col); + if (0 <= y1 && y1 < self->width && 0 <= x1 && x1 < self->height) { + setpixel(self, y1, x1, col); + } } else { - setpixel(self, x1, y1, col); + if (0 <= x1 && x1 < self->width && 0 <= y1 && y1 < self->height) { + setpixel(self, x1, y1, col); + } } while (e >= 0) { y1 += sy; @@ -314,7 +318,9 @@ STATIC mp_obj_t framebuf_line(size_t n_args, const mp_obj_t *args) { e += 2 * dy; } - setpixel(self, x2, y2, col); + if (0 <= x2 && x2 < self->width && 0 <= y2 && y2 < self->height) { + setpixel(self, x2, y2, col); + } return mp_const_none; } -- cgit v1.2.3 From fd99690f1859a46aee690355170ad13eed9f0122 Mon Sep 17 00:00:00 2001 From: Oleg Korsak Date: Sun, 1 Jan 2017 04:24:56 +0200 Subject: extmod/modframebuf: Add GS4_HMSB format. --- extmod/modframebuf.c | 59 +++++++++++++++++++++++++++++++++++++++++++++-- tests/extmod/framebuf1.py | 2 +- 2 files changed, 58 insertions(+), 3 deletions(-) (limited to 'extmod') diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index e2441f194..792a3a7fa 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -97,13 +97,66 @@ STATIC void rgb565_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, i } } +// Functions for GS4_HMSB format + +STATIC void gs4_hmsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t col) { + uint8_t *pixel = &((uint8_t*)fb->buf)[(x + y * fb->stride) >> 1]; + + if (x % 2) { + *pixel = ((uint8_t)col & 0x0f) | (*pixel & 0xf0); + } else { + *pixel = ((uint8_t)col << 4) | (*pixel & 0x0f); + } +} + +STATIC uint32_t gs4_hmsb_getpixel(const mp_obj_framebuf_t *fb, int x, int y) { + if (x % 2) { + return ((uint8_t*)fb->buf)[(x + y * fb->stride) >> 1] & 0x0f; + } + + return ((uint8_t*)fb->buf)[(x + y * fb->stride) >> 1] >> 4; +} + +STATIC void gs4_hmsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) { + col &= 0x0f; + uint8_t *pixel_pair = &((uint8_t*)fb->buf)[(x + y * fb->stride) >> 1]; + uint8_t col_shifted_left = col << 4; + uint8_t colored_pixel_pair = col_shifted_left | col; + int pixel_count_till_next_line = (fb->stride - w) >> 1; + bool odd_x = (x % 2 == 1); + + while (h--) { + int ww = w; + + if (odd_x && ww > 0) { + *pixel_pair = (*pixel_pair & 0xf0) | col; + pixel_pair++; + ww--; + } + + memset(pixel_pair, colored_pixel_pair, ww >> 1); + pixel_pair += ww >> 1; + + if (ww % 2) { + *pixel_pair = col_shifted_left | (*pixel_pair & 0x0f); + if (!odd_x) { + pixel_pair++; + } + } + + pixel_pair += pixel_count_till_next_line; + } +} + // constants for formats -#define FRAMEBUF_MVLSB (0) -#define FRAMEBUF_RGB565 (1) +#define FRAMEBUF_MVLSB (0) +#define FRAMEBUF_RGB565 (1) +#define FRAMEBUF_GS4_HMSB (2) STATIC mp_framebuf_p_t formats[] = { [FRAMEBUF_MVLSB] = {mvlsb_setpixel, mvlsb_getpixel, mvlsb_fill_rect}, [FRAMEBUF_RGB565] = {rgb565_setpixel, rgb565_getpixel, rgb565_fill_rect}, + [FRAMEBUF_GS4_HMSB] = {gs4_hmsb_setpixel, gs4_hmsb_getpixel, gs4_hmsb_fill_rect}, }; static inline void setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t color) { @@ -152,6 +205,7 @@ STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size switch (o->format) { case FRAMEBUF_MVLSB: case FRAMEBUF_RGB565: + case FRAMEBUF_GS4_HMSB: break; default: nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, @@ -490,6 +544,7 @@ STATIC const mp_rom_map_elem_t framebuf_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_FrameBuffer1), MP_ROM_PTR(&legacy_framebuffer1_obj) }, { MP_ROM_QSTR(MP_QSTR_MVLSB), MP_OBJ_NEW_SMALL_INT(FRAMEBUF_MVLSB) }, { MP_ROM_QSTR(MP_QSTR_RGB565), MP_OBJ_NEW_SMALL_INT(FRAMEBUF_RGB565) }, + { MP_ROM_QSTR(MP_QSTR_GS4_HMSB), MP_OBJ_NEW_SMALL_INT(FRAMEBUF_GS4_HMSB) }, }; STATIC MP_DEFINE_CONST_DICT(framebuf_module_globals, framebuf_module_globals_table); diff --git a/tests/extmod/framebuf1.py b/tests/extmod/framebuf1.py index 47dd98d7e..9fed33809 100644 --- a/tests/extmod/framebuf1.py +++ b/tests/extmod/framebuf1.py @@ -91,7 +91,7 @@ print(buf) # test invalid constructor try: - fbuf = framebuf.FrameBuffer(buf, w, h, 2, framebuf.MVLSB) + fbuf = framebuf.FrameBuffer(buf, w, h, 3, framebuf.MVLSB) except ValueError: print("ValueError") -- cgit v1.2.3 From f5f4cdae89ed040ae9209a380cf968254434e819 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 1 Jun 2016 17:00:28 +0100 Subject: extmod/vfs_fat: Rework so it can optionally use OO version of FatFS. If MICROPY_VFS_FAT is enabled by a port then the port must switch to using MICROPY_FATFS_OO. Otherwise a port can continue to use the FatFs code without any changes. --- extmod/fsusermount.c | 41 +++++++++++++++++++++++-- extmod/vfs_fat.c | 81 ++++++++++++++++++++++--------------------------- extmod/vfs_fat_diskio.c | 61 ++++++++++++++++++++++++++++++++----- extmod/vfs_fat_ffconf.c | 40 ++++++++++++++++++++++-- extmod/vfs_fat_file.c | 36 ++++++++++++++++++++-- extmod/vfs_fat_file.h | 6 ++++ extmod/vfs_fat_misc.c | 30 +++++++++++++++--- extmod/vfs_fat_reader.c | 13 ++++++++ 8 files changed, 243 insertions(+), 65 deletions(-) (limited to 'extmod') diff --git a/extmod/fsusermount.c b/extmod/fsusermount.c index 5882aba99..4ca9b80a6 100644 --- a/extmod/fsusermount.c +++ b/extmod/fsusermount.c @@ -32,7 +32,11 @@ #include "py/nlr.h" #include "py/runtime.h" #include "py/mperrno.h" +#if MICROPY_FATFS_OO +#include "lib/oofatfs/ff.h" +#else #include "lib/fatfs/ff.h" +#endif #include "extmod/fsusermount.h" fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, bool mkfs) { @@ -57,7 +61,11 @@ fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp for (size_t i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) { fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i]; if (vfs != NULL && !memcmp(mnt_str, vfs->str, mnt_len + 1)) { + #if MICROPY_FATFS_OO + res = f_umount(&vfs->fatfs); + #else res = f_mount(NULL, vfs->str, 0); + #endif if (vfs->flags & FSUSER_FREE_OBJ) { m_del_obj(fs_user_mount_t, vfs); } @@ -86,6 +94,9 @@ fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp vfs->str = mnt_str; vfs->len = mnt_len; vfs->flags = FSUSER_FREE_OBJ; + #if MICROPY_FATFS_OO + vfs->fatfs.drv = vfs; + #endif // load block protocol methods mp_load_method(device, MP_QSTR_readblocks, vfs->readblocks); @@ -114,15 +125,30 @@ fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp MP_STATE_PORT(fs_user_mount)[i] = vfs; // mount the block device (if mkfs, only pre-mount) - FRESULT res = f_mount(&vfs->fatfs, vfs->str, !mkfs); + FRESULT res; + #if MICROPY_FATFS_OO + if (mkfs) { + res = FR_OK; + } else { + res = f_mount(&vfs->fatfs); + } + #else + res = f_mount(&vfs->fatfs, vfs->str, !mkfs); + #endif + // check the result if (res == FR_OK) { if (mkfs) { goto mkfs; } } else if (res == FR_NO_FILESYSTEM && args[1].u_bool) { -mkfs: +mkfs:; + #if MICROPY_FATFS_OO + uint8_t working_buf[_MAX_SS]; + res = f_mkfs(&vfs->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf)); + #else res = f_mkfs(vfs->str, 1, 0); + #endif if (res != FR_OK) { mkfs_error: MP_STATE_PORT(fs_user_mount)[i] = NULL; @@ -130,7 +156,11 @@ mkfs_error: } if (mkfs) { // If requested to only mkfs, unmount pre-mounted device + #if MICROPY_FATFS_OO + res = FR_OK; + #else res = f_mount(NULL, vfs->str, 0); + #endif if (res != FR_OK) { goto mkfs_error; } @@ -188,7 +218,12 @@ mp_obj_t fatfs_umount(mp_obj_t bdev_or_path_in) { } fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i]; - FRESULT res = f_mount(NULL, vfs->str, 0); + FRESULT res; + #if MICROPY_FATFS_OO + res = f_umount(&vfs->fatfs); + #else + res = f_mount(NULL, vfs->str, 0); + #endif if (vfs->flags & FSUSER_FREE_OBJ) { m_del_obj(fs_user_mount_t, vfs); } diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index bd88bcf1b..36bdb5dbd 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -28,12 +28,15 @@ #include "py/mpconfig.h" #if MICROPY_VFS_FAT +#if !MICROPY_FATFS_OO +#error "with MICROPY_VFS_FAT enabled, must also enable MICROPY_FATFS_OO" +#endif + #include #include "py/nlr.h" #include "py/runtime.h" #include "py/mperrno.h" -#include "lib/fatfs/ff.h" -#include "lib/fatfs/diskio.h" +#include "lib/oofatfs/ff.h" #include "extmod/vfs_fat_file.h" #include "extmod/fsusermount.h" #include "timeutils.h" @@ -55,13 +58,10 @@ STATIC mp_obj_t fat_vfs_mkfs(mp_obj_t bdev_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_mkfs_fun_obj, fat_vfs_mkfs); STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(fat_vfs_mkfs_obj, MP_ROM_PTR(&fat_vfs_mkfs_fun_obj)); -STATIC mp_obj_t fat_vfs_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { - // Skip self - return fatfs_builtin_open(n_args - 1, args + 1, kwargs); -} -MP_DEFINE_CONST_FUN_OBJ_KW(fat_vfs_open_obj, 2, fat_vfs_open); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(fat_vfs_open_obj, 2, fatfs_builtin_open_self); STATIC mp_obj_t fat_vfs_listdir_func(size_t n_args, const mp_obj_t *args) { + mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(args[0]); bool is_str_type = true; const char *path; if (n_args == 2) { @@ -73,19 +73,16 @@ STATIC mp_obj_t fat_vfs_listdir_func(size_t n_args, const mp_obj_t *args) { path = ""; } - return fat_vfs_listdir(path, is_str_type); + return fat_vfs_listdir2(self, path, is_str_type); } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_listdir_obj, 1, 2, fat_vfs_listdir_func); -STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t path_in, mp_int_t attr) { +STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t vfs_in, mp_obj_t path_in, mp_int_t attr) { + mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); const char *path = mp_obj_str_get_str(path_in); FILINFO fno; -#if _USE_LFN - fno.lfname = NULL; - fno.lfsize = 0; -#endif - FRESULT res = f_stat(path, &fno); + FRESULT res = f_stat(&self->fatfs, path, &fno); if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); @@ -93,7 +90,7 @@ STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t path_in, mp_int_t attr) { // check if path is a file or directory if ((fno.fattrib & AM_DIR) == attr) { - res = f_unlink(path); + res = f_unlink(&self->fatfs, path); if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); @@ -105,27 +102,25 @@ STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t path_in, mp_int_t attr) { } STATIC mp_obj_t fat_vfs_remove(mp_obj_t vfs_in, mp_obj_t path_in) { - (void)vfs_in; - return fat_vfs_remove_internal(path_in, 0); // 0 == file attribute + return fat_vfs_remove_internal(vfs_in, path_in, 0); // 0 == file attribute } STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_remove_obj, fat_vfs_remove); STATIC mp_obj_t fat_vfs_rmdir(mp_obj_t vfs_in, mp_obj_t path_in) { - (void) vfs_in; - return fat_vfs_remove_internal(path_in, AM_DIR); + return fat_vfs_remove_internal(vfs_in, path_in, AM_DIR); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_rmdir_obj, fat_vfs_rmdir); STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_out) { - (void)vfs_in; + mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); 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); + FRESULT res = f_rename(&self->fatfs, old_path, new_path); if (res == FR_EXIST) { // if new_path exists then try removing it (but only if it's a file) - fat_vfs_remove_internal(path_out, 0); // 0 == file attribute + fat_vfs_remove_internal(vfs_in, path_out, 0); // 0 == file attribute // try to rename again - res = f_rename(old_path, new_path); + res = f_rename(&self->fatfs, old_path, new_path); } if (res == FR_OK) { return mp_const_none; @@ -137,9 +132,9 @@ STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_ STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_rename_obj, fat_vfs_rename); STATIC mp_obj_t fat_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_o) { - (void)vfs_in; + mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); const char *path = mp_obj_str_get_str(path_o); - FRESULT res = f_mkdir(path); + FRESULT res = f_mkdir(&self->fatfs, path); if (res == FR_OK) { return mp_const_none; } else { @@ -150,15 +145,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_mkdir_obj, fat_vfs_mkdir); /// Change current directory. STATIC mp_obj_t fat_vfs_chdir(mp_obj_t vfs_in, mp_obj_t path_in) { - (void)vfs_in; + mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_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); - } + FRESULT res = f_chdir(&self->fatfs, path); if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); @@ -170,14 +161,18 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_chdir_obj, fat_vfs_chdir); /// Get the current directory. STATIC mp_obj_t fat_vfs_getcwd(mp_obj_t vfs_in) { - (void)vfs_in; + mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); char buf[MICROPY_ALLOC_PATH_MAX + 1]; - FRESULT res = f_getcwd(buf, sizeof buf); - + memcpy(buf, self->str, self->len); + FRESULT res = f_getcwd(&self->fatfs, buf + self->len, sizeof(buf) - self->len); if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); } - + // remove trailing / if in root dir, because we prepended the mount point + size_t l = strlen(buf); + if (res == FR_OK && buf[l - 1] == '/') { + buf[l - 1] = 0; + } return mp_obj_new_str(buf, strlen(buf), false); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getcwd_obj, fat_vfs_getcwd); @@ -202,14 +197,10 @@ STATIC bool path_equal(const char *path, const char *path_canonical) { /// \function stat(path) /// Get the status of a file or directory. STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) { - (void)vfs_in; + mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); const char *path = mp_obj_str_get_str(path_in); FILINFO fno; -#if _USE_LFN - fno.lfname = NULL; - fno.lfsize = 0; -#endif FRESULT res; if (path_equal(path, "/")) { @@ -233,7 +224,7 @@ STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) { } if (res == FR_NO_PATH) { // stat normal file - res = f_stat(path, &fno); + res = f_stat(&self->fatfs, path, &fno); } if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); @@ -272,12 +263,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_stat_obj, fat_vfs_stat); // Get the status of a VFS. STATIC mp_obj_t fat_vfs_statvfs(mp_obj_t vfs_in, mp_obj_t path_in) { - (void)vfs_in; - const char *path = mp_obj_str_get_str(path_in); + mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); + (void)path_in; - FATFS *fatfs; DWORD nclst; - FRESULT res = f_getfree(path, &nclst, &fatfs); + FATFS *fatfs = &self->fatfs; + FRESULT res = f_getfree(fatfs, &nclst); if (FR_OK != res) { mp_raise_OSError(fresult_to_errno_table[res]); } diff --git a/extmod/vfs_fat_diskio.c b/extmod/vfs_fat_diskio.c index 3f1902b2e..9e91b26f7 100644 --- a/extmod/vfs_fat_diskio.c +++ b/extmod/vfs_fat_diskio.c @@ -36,8 +36,13 @@ #include "py/mphal.h" #include "py/runtime.h" +#if MICROPY_FATFS_OO +#include "lib/oofatfs/ff.h" +#include "lib/oofatfs/diskio.h" +#else #include "lib/fatfs/ff.h" /* FatFs lower layer API */ #include "lib/fatfs/diskio.h" /* FatFs lower layer API */ +#endif #include "extmod/fsusermount.h" #if _MAX_SS == _MIN_SS @@ -46,6 +51,13 @@ #define SECSIZE(fs) ((fs)->ssize) #endif +#if MICROPY_FATFS_OO +typedef void *bdev_t; +STATIC fs_user_mount_t *disk_get_device(void *bdev) { + return (fs_user_mount_t*)bdev; +} +#else +typedef BYTE bdev_t; STATIC fs_user_mount_t *disk_get_device(uint id) { if (id < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount))) { return MP_STATE_PORT(fs_user_mount)[id]; @@ -53,13 +65,17 @@ STATIC fs_user_mount_t *disk_get_device(uint id) { return NULL; } } +#endif /*-----------------------------------------------------------------------*/ /* Initialize a Drive */ /*-----------------------------------------------------------------------*/ +#if MICROPY_FATFS_OO +STATIC +#endif DSTATUS disk_initialize ( - BYTE pdrv /* Physical drive nmuber (0..) */ + bdev_t pdrv /* Physical drive nmuber (0..) */ ) { fs_user_mount_t *vfs = disk_get_device(pdrv); @@ -89,8 +105,11 @@ DSTATUS disk_initialize ( /* Get Disk Status */ /*-----------------------------------------------------------------------*/ +#if MICROPY_FATFS_OO +STATIC +#endif DSTATUS disk_status ( - BYTE pdrv /* Physical drive nmuber (0..) */ + bdev_t pdrv /* Physical drive nmuber (0..) */ ) { fs_user_mount_t *vfs = disk_get_device(pdrv); @@ -110,7 +129,7 @@ DSTATUS disk_status ( /*-----------------------------------------------------------------------*/ DRESULT disk_read ( - BYTE pdrv, /* Physical drive nmuber (0..) */ + bdev_t pdrv, /* Physical drive nmuber (0..) */ BYTE *buff, /* Data buffer to store read data */ DWORD sector, /* Sector address (LBA) */ UINT count /* Number of sectors to read (1..128) */ @@ -140,9 +159,9 @@ DRESULT disk_read ( /* Write Sector(s) */ /*-----------------------------------------------------------------------*/ -#if _USE_WRITE +#if MICROPY_FATFS_OO || _USE_WRITE DRESULT disk_write ( - BYTE pdrv, /* Physical drive nmuber (0..) */ + bdev_t pdrv, /* Physical drive nmuber (0..) */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address (LBA) */ UINT count /* Number of sectors to write (1..128) */ @@ -179,9 +198,9 @@ DRESULT disk_write ( /* Miscellaneous Functions */ /*-----------------------------------------------------------------------*/ -#if _USE_IOCTL +#if MICROPY_FATFS_OO || _USE_IOCTL DRESULT disk_ioctl ( - BYTE pdrv, /* Physical drive nmuber (0..) */ + bdev_t pdrv, /* Physical drive nmuber (0..) */ BYTE cmd, /* Control code */ void *buff /* Buffer to send/receive control data */ ) @@ -218,6 +237,10 @@ DRESULT disk_ioctl ( } else { *((WORD*)buff) = mp_obj_get_int(ret); } + #if MICROPY_FATFS_OO && _MAX_SS != _MIN_SS + // need to store ssize because we use it in disk_read/disk_write + vfs->fatfs.ssize = *((WORD*)buff); + #endif return RES_OK; } @@ -225,6 +248,16 @@ DRESULT disk_ioctl ( *((DWORD*)buff) = 1; // erase block size in units of sector size return RES_OK; + #if MICROPY_FATFS_OO + case IOCTL_INIT: + *((DSTATUS*)buff) = disk_initialize(pdrv); + return RES_OK; + + case IOCTL_STATUS: + *((DSTATUS*)buff) = disk_status(pdrv); + return RES_OK; + #endif + default: return RES_PARERR; } @@ -245,12 +278,26 @@ DRESULT disk_ioctl ( case GET_SECTOR_SIZE: *((WORD*)buff) = 512; // old protocol had fixed sector size + #if MICROPY_FATFS_OO && _MAX_SS != _MIN_SS + // need to store ssize because we use it in disk_read/disk_write + vfs->fatfs.ssize = 512; + #endif return RES_OK; case GET_BLOCK_SIZE: *((DWORD*)buff) = 1; // erase block size in units of sector size return RES_OK; + #if MICROPY_FATFS_OO + case IOCTL_INIT: + *((DSTATUS*)buff) = disk_initialize(pdrv); + return RES_OK; + + case IOCTL_STATUS: + *((DSTATUS*)buff) = disk_status(pdrv); + return RES_OK; + #endif + default: return RES_PARERR; } diff --git a/extmod/vfs_fat_ffconf.c b/extmod/vfs_fat_ffconf.c index f8935af75..ddcdd8844 100644 --- a/extmod/vfs_fat_ffconf.c +++ b/extmod/vfs_fat_ffconf.c @@ -30,10 +30,13 @@ #include #include "py/mpstate.h" +#if MICROPY_FATFS_OO +#include "lib/oofatfs/ff.h" +#else #include "lib/fatfs/ff.h" -#include "lib/fatfs/ffconf.h" -#include "lib/fatfs/diskio.h" +#endif #include "extmod/fsusermount.h" +#include "extmod/vfs_fat_file.h" STATIC bool check_path(const TCHAR **path, const char *mount_point_str, mp_uint_t mount_point_len) { if (strncmp(*path, mount_point_str, mount_point_len) == 0) { @@ -48,6 +51,37 @@ STATIC bool check_path(const TCHAR **path, const char *mount_point_str, mp_uint_ return false; } +#if MICROPY_FATFS_OO + +STATIC fs_user_mount_t *vfs_cur_obj = NULL; + +// "path" is the path to lookup; will advance this pointer beyond the volume name. +// Returns a pointer to the VFS object, NULL means path not found. +fs_user_mount_t *ff_get_vfs(const char **path) { + if (!(*path)) { + return NULL; + } + + if (**path != '/') { + #if _FS_RPATH + return vfs_cur_obj; + #else + return NULL; + #endif + } + + for (size_t i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) { + fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i]; + if (vfs != NULL && check_path(path, vfs->str, vfs->len)) { + return vfs; + } + } + + return NULL; +} + +#else + // "path" is the path to lookup; will advance this pointer beyond the volume name. // Returns logical drive number (-1 means invalid path). int ff_get_ldnumber (const TCHAR **path) { @@ -79,4 +113,6 @@ void ff_get_volname(BYTE vol, TCHAR **dest) { *dest += vfs->len; } +#endif + #endif // MICROPY_FSUSERMOUNT diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index 77848b5a5..6651d70d0 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -36,7 +36,12 @@ #include "py/runtime.h" #include "py/stream.h" #include "py/mperrno.h" +#if MICROPY_FATFS_OO +#include "lib/oofatfs/ff.h" +#else #include "lib/fatfs/ff.h" +#endif +#include "extmod/fsusermount.h" #include "extmod/vfs_fat_file.h" #if MICROPY_VFS_FAT @@ -112,7 +117,11 @@ STATIC mp_uint_t file_obj_write(mp_obj_t self_in, const void *buf, mp_uint_t siz STATIC mp_obj_t file_obj_close(mp_obj_t self_in) { pyb_file_obj_t *self = MP_OBJ_TO_PTR(self_in); // if fs==NULL then the file is closed and in that case this method is a no-op + #if MICROPY_FATFS_OO + if (self->fp.obj.fs != NULL) { + #else if (self->fp.fs != NULL) { + #endif FRESULT res = f_close(&self->fp); if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); @@ -178,7 +187,7 @@ STATIC const mp_arg_t file_open_args[] = { }; #define FILE_OPEN_NUM_ARGS MP_ARRAY_SIZE(file_open_args) -STATIC mp_obj_t file_open(const mp_obj_type_t *type, mp_arg_val_t *args) { +STATIC mp_obj_t file_open(fs_user_mount_t *vfs, const mp_obj_type_t *type, mp_arg_val_t *args) { int mode = 0; const char *mode_s = mp_obj_str_get_str(args[1].u_obj); // TODO make sure only one of r, w, x, a, and b, t are specified @@ -214,7 +223,19 @@ STATIC mp_obj_t file_open(const mp_obj_type_t *type, mp_arg_val_t *args) { o->base.type = type; const char *fname = mp_obj_str_get_str(args[0].u_obj); + #if MICROPY_FATFS_OO + if (vfs == NULL) { + vfs = ff_get_vfs(&fname); + if (vfs == NULL) { + m_del_obj(pyb_file_obj_t, o); + mp_raise_OSError(MP_ENOENT); + } + } + FRESULT res = f_open(&vfs->fatfs, &o->fp, fname, mode); + #else + (void)vfs; FRESULT res = f_open(&o->fp, fname, mode); + #endif if (res != FR_OK) { m_del_obj(pyb_file_obj_t, o); mp_raise_OSError(fresult_to_errno_table[res]); @@ -231,7 +252,7 @@ STATIC mp_obj_t file_open(const mp_obj_type_t *type, mp_arg_val_t *args) { STATIC mp_obj_t file_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_val_t arg_vals[FILE_OPEN_NUM_ARGS]; mp_arg_parse_all_kw_array(n_args, n_kw, args, FILE_OPEN_NUM_ARGS, file_open_args, arg_vals); - return file_open(type, arg_vals); + return file_open(NULL, type, arg_vals); } // TODO gc hook to close the file if not already closed @@ -295,7 +316,16 @@ mp_obj_t fatfs_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw // TODO: analyze buffering args and instantiate appropriate type mp_arg_val_t arg_vals[FILE_OPEN_NUM_ARGS]; mp_arg_parse_all(n_args, args, kwargs, FILE_OPEN_NUM_ARGS, file_open_args, arg_vals); - return file_open(&mp_type_textio, arg_vals); + return file_open(NULL, &mp_type_textio, arg_vals); +} + +// Factory function for I/O stream classes +mp_obj_t fatfs_builtin_open_self(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { + // TODO: analyze buffering args and instantiate appropriate type + fs_user_mount_t *self = MP_OBJ_TO_PTR(args[0]); + mp_arg_val_t arg_vals[FILE_OPEN_NUM_ARGS]; + mp_arg_parse_all(n_args - 1, args + 1, kwargs, FILE_OPEN_NUM_ARGS, file_open_args, arg_vals); + return file_open(self, &mp_type_textio, arg_vals); } #endif // MICROPY_FSUSERMOUNT diff --git a/extmod/vfs_fat_file.h b/extmod/vfs_fat_file.h index 5c271b6ee..9693aa04a 100644 --- a/extmod/vfs_fat_file.h +++ b/extmod/vfs_fat_file.h @@ -24,9 +24,15 @@ * THE SOFTWARE. */ +struct _fs_user_mount_t; + extern const byte fresult_to_errno_table[20]; +struct _fs_user_mount_t *ff_get_vfs(const char **path); + mp_obj_t fatfs_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs); +mp_obj_t fatfs_builtin_open_self(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs); MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj); mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type); +mp_obj_t fat_vfs_listdir2(struct _fs_user_mount_t *vfs, const char *path, bool is_str_type); diff --git a/extmod/vfs_fat_misc.c b/extmod/vfs_fat_misc.c index d3507a85f..5e89009cd 100644 --- a/extmod/vfs_fat_misc.c +++ b/extmod/vfs_fat_misc.c @@ -32,27 +32,39 @@ #include #include "py/nlr.h" #include "py/runtime.h" +#if MICROPY_FATFS_OO +#include "lib/oofatfs/ff.h" +#else #include "lib/fatfs/ff.h" -#include "lib/fatfs/diskio.h" +#endif #include "extmod/vfs_fat_file.h" #include "extmod/fsusermount.h" #include "py/lexer.h" -#if _USE_LFN +#if !MICROPY_FATFS_OO && _USE_LFN STATIC char lfn[_MAX_LFN + 1]; /* Buffer to store the LFN */ #endif // TODO: actually, the core function should be ilistdir() + mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type) { + return fat_vfs_listdir2(NULL, path, is_str_type); +} + +mp_obj_t fat_vfs_listdir2(fs_user_mount_t *vfs, const char *path, bool is_str_type) { FRESULT res; FILINFO fno; DIR dir; -#if _USE_LFN +#if !MICROPY_FATFS_OO && _USE_LFN fno.lfname = lfn; fno.lfsize = sizeof lfn; #endif + #if MICROPY_FATFS_OO + res = f_opendir(&vfs->fatfs, &dir, path); + #else res = f_opendir(&dir, path); /* Open the directory */ + #endif if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); } @@ -65,7 +77,7 @@ mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type) { 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 +#if !MICROPY_FATFS_OO && _USE_LFN char *fn = *fno.lfname ? fno.lfname : fno.fname; #else char *fn = fno.fname; @@ -100,11 +112,19 @@ mp_import_stat_t fat_vfs_import_stat(const char *path); mp_import_stat_t fat_vfs_import_stat(const char *path) { FILINFO fno; -#if _USE_LFN +#if !MICROPY_FATFS_OO && _USE_LFN fno.lfname = NULL; fno.lfsize = 0; #endif + #if MICROPY_FATFS_OO + fs_user_mount_t *vfs = ff_get_vfs(&path); + if (vfs == NULL) { + return MP_IMPORT_STAT_NO_EXIST; + } + FRESULT res = f_stat(&vfs->fatfs, path, &fno); + #else FRESULT res = f_stat(path, &fno); + #endif if (res == FR_OK) { if ((fno.fattrib & AM_DIR) != 0) { return MP_IMPORT_STAT_DIR; diff --git a/extmod/vfs_fat_reader.c b/extmod/vfs_fat_reader.c index 7a00f18de..efd2de0c1 100644 --- a/extmod/vfs_fat_reader.c +++ b/extmod/vfs_fat_reader.c @@ -32,7 +32,12 @@ #if MICROPY_READER_FATFS +#if MICROPY_FATFS_OO +#include "lib/oofatfs/ff.h" +#else #include "lib/fatfs/ff.h" +#endif +#include "extmod/fsusermount.h" #include "extmod/vfs_fat_file.h" typedef struct _mp_reader_fatfs_t { @@ -71,7 +76,15 @@ int mp_reader_new_file(mp_reader_t *reader, const char *filename) { if (rf == NULL) { return MP_ENOMEM; } + #if MICROPY_FATFS_OO + fs_user_mount_t *vfs = ff_get_vfs(&filename); + if (vfs == NULL) { + return MP_ENOENT; + } + FRESULT res = f_open(&vfs->fatfs, &rf->fp, filename, FA_READ); + #else FRESULT res = f_open(&rf->fp, filename, FA_READ); + #endif if (res != FR_OK) { return fresult_to_errno_table[res]; } -- cgit v1.2.3 From 32a1138b9f66b76808906064a76c5f9533cc825c Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 27 Jan 2017 15:04:17 +1100 Subject: extmod: Rename vfs_fat_file.h to vfs_fat.h. And move declaration of mp_fat_vfs_type to this file. --- cc3200/mods/moduos.c | 2 +- esp8266/moduos.c | 3 +-- extmod/vfs_fat.c | 2 +- extmod/vfs_fat.h | 39 +++++++++++++++++++++++++++++++++++++++ extmod/vfs_fat_ffconf.c | 2 +- extmod/vfs_fat_file.c | 2 +- extmod/vfs_fat_file.h | 38 -------------------------------------- extmod/vfs_fat_misc.c | 2 +- extmod/vfs_fat_reader.c | 2 +- stmhal/builtin_open.c | 2 +- stmhal/moduos.c | 2 +- unix/modos.c | 2 +- 12 files changed, 49 insertions(+), 49 deletions(-) create mode 100644 extmod/vfs_fat.h delete mode 100644 extmod/vfs_fat_file.h (limited to 'extmod') diff --git a/cc3200/mods/moduos.c b/cc3200/mods/moduos.c index d5b29336a..1c9932b8e 100644 --- a/cc3200/mods/moduos.c +++ b/cc3200/mods/moduos.c @@ -37,7 +37,7 @@ #include "moduos.h" #include "diskio.h" #include "sflash_diskio.h" -#include "extmod/vfs_fat_file.h" +#include "extmod/vfs_fat.h" #include "random.h" #include "mpexception.h" #include "version.h" diff --git a/esp8266/moduos.c b/esp8266/moduos.c index e9c4c3e8c..5f5aab166 100644 --- a/esp8266/moduos.c +++ b/esp8266/moduos.c @@ -34,12 +34,11 @@ #include "py/runtime.h" #include "py/mperrno.h" #include "extmod/misc.h" +#include "extmod/vfs_fat.h" #include "genhdr/mpversion.h" #include "esp_mphal.h" #include "user_interface.h" -extern const mp_obj_type_t mp_fat_vfs_type; - STATIC const qstr os_uname_info_fields[] = { MP_QSTR_sysname, MP_QSTR_nodename, MP_QSTR_release, MP_QSTR_version, MP_QSTR_machine diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index 36bdb5dbd..36e5031a8 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -37,7 +37,7 @@ #include "py/runtime.h" #include "py/mperrno.h" #include "lib/oofatfs/ff.h" -#include "extmod/vfs_fat_file.h" +#include "extmod/vfs_fat.h" #include "extmod/fsusermount.h" #include "timeutils.h" diff --git a/extmod/vfs_fat.h b/extmod/vfs_fat.h new file mode 100644 index 000000000..441b35c04 --- /dev/null +++ b/extmod/vfs_fat.h @@ -0,0 +1,39 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013, 2014 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. + */ + +struct _fs_user_mount_t; + +extern const byte fresult_to_errno_table[20]; +extern const mp_obj_type_t mp_fat_vfs_type; + +struct _fs_user_mount_t *ff_get_vfs(const char **path); + +mp_obj_t fatfs_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs); +mp_obj_t fatfs_builtin_open_self(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs); +MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj); + +mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type); +mp_obj_t fat_vfs_listdir2(struct _fs_user_mount_t *vfs, const char *path, bool is_str_type); diff --git a/extmod/vfs_fat_ffconf.c b/extmod/vfs_fat_ffconf.c index ddcdd8844..89081380e 100644 --- a/extmod/vfs_fat_ffconf.c +++ b/extmod/vfs_fat_ffconf.c @@ -36,7 +36,7 @@ #include "lib/fatfs/ff.h" #endif #include "extmod/fsusermount.h" -#include "extmod/vfs_fat_file.h" +#include "extmod/vfs_fat.h" STATIC bool check_path(const TCHAR **path, const char *mount_point_str, mp_uint_t mount_point_len) { if (strncmp(*path, mount_point_str, mount_point_len) == 0) { diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index 6651d70d0..ea332709e 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -42,7 +42,7 @@ #include "lib/fatfs/ff.h" #endif #include "extmod/fsusermount.h" -#include "extmod/vfs_fat_file.h" +#include "extmod/vfs_fat.h" #if MICROPY_VFS_FAT #define mp_type_fileio fatfs_type_fileio diff --git a/extmod/vfs_fat_file.h b/extmod/vfs_fat_file.h deleted file mode 100644 index 9693aa04a..000000000 --- a/extmod/vfs_fat_file.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 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. - */ - -struct _fs_user_mount_t; - -extern const byte fresult_to_errno_table[20]; - -struct _fs_user_mount_t *ff_get_vfs(const char **path); - -mp_obj_t fatfs_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs); -mp_obj_t fatfs_builtin_open_self(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs); -MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj); - -mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type); -mp_obj_t fat_vfs_listdir2(struct _fs_user_mount_t *vfs, const char *path, bool is_str_type); diff --git a/extmod/vfs_fat_misc.c b/extmod/vfs_fat_misc.c index 5e89009cd..ea267a15f 100644 --- a/extmod/vfs_fat_misc.c +++ b/extmod/vfs_fat_misc.c @@ -37,7 +37,7 @@ #else #include "lib/fatfs/ff.h" #endif -#include "extmod/vfs_fat_file.h" +#include "extmod/vfs_fat.h" #include "extmod/fsusermount.h" #include "py/lexer.h" diff --git a/extmod/vfs_fat_reader.c b/extmod/vfs_fat_reader.c index efd2de0c1..b9abf3ad7 100644 --- a/extmod/vfs_fat_reader.c +++ b/extmod/vfs_fat_reader.c @@ -38,7 +38,7 @@ #include "lib/fatfs/ff.h" #endif #include "extmod/fsusermount.h" -#include "extmod/vfs_fat_file.h" +#include "extmod/vfs_fat.h" typedef struct _mp_reader_fatfs_t { FIL fp; diff --git a/stmhal/builtin_open.c b/stmhal/builtin_open.c index 697eec8ea..56b98ea61 100644 --- a/stmhal/builtin_open.c +++ b/stmhal/builtin_open.c @@ -25,6 +25,6 @@ */ #include "py/runtime.h" -#include "extmod/vfs_fat_file.h" +#include "extmod/vfs_fat.h" MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, fatfs_builtin_open); diff --git a/stmhal/moduos.c b/stmhal/moduos.c index b3c6570a5..158e530ff 100644 --- a/stmhal/moduos.c +++ b/stmhal/moduos.c @@ -37,7 +37,7 @@ #include "timeutils.h" #include "rng.h" #include "uart.h" -#include "extmod/vfs_fat_file.h" +#include "extmod/vfs_fat.h" #include "sdcard.h" #include "extmod/fsusermount.h" #include "portmodules.h" diff --git a/unix/modos.c b/unix/modos.c index 72f5d872e..c35b246dd 100644 --- a/unix/modos.c +++ b/unix/modos.c @@ -39,6 +39,7 @@ #include "py/objtuple.h" #include "py/mphal.h" #include "extmod/misc.h" +#include "extmod/vfs_fat.h" // Can't include this, as FATFS structure definition is required, // and FatFs header defining it conflicts with POSIX. @@ -46,7 +47,6 @@ MP_DECLARE_CONST_FUN_OBJ_KW(fsuser_mount_obj); MP_DECLARE_CONST_FUN_OBJ_1(fsuser_umount_obj); MP_DECLARE_CONST_FUN_OBJ_KW(fsuser_mkfs_obj); -extern const mp_obj_type_t mp_fat_vfs_type; #ifdef __ANDROID__ #define USE_STATFS 1 -- cgit v1.2.3 From dcb9ea72157f1d9f3b0dc306c2c31cbd647f5ee1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 27 Jan 2017 15:10:09 +1100 Subject: extmod: Add generic VFS sub-system. This provides mp_vfs_XXX functions (eg mount, open, listdir) which are agnostic to the underlying filesystem type, and just require an object with the relevant filesystem-like methods (eg .mount, .open, .listidr) which can then be mounted. These mp_vfs_XXX functions would typically be used by a port to implement the "uos" module, and mp_vfs_open would be the builtin open function. This feature is controlled by MICROPY_VFS, disabled by default. --- extmod/vfs.c | 310 ++++++++++++++++++++++++++++++++++++++++++++++++++++ extmod/vfs.h | 60 ++++++++++ extmod/vfs_reader.c | 97 ++++++++++++++++ py/lexer.c | 2 +- py/mpconfig.h | 10 ++ py/mpstate.h | 5 + py/py.mk | 2 + py/qstrdefs.h | 1 + py/runtime.c | 6 + 9 files changed, 492 insertions(+), 1 deletion(-) create mode 100644 extmod/vfs.c create mode 100644 extmod/vfs.h create mode 100644 extmod/vfs_reader.c (limited to 'extmod') diff --git a/extmod/vfs.c b/extmod/vfs.c new file mode 100644 index 000000000..2880271c6 --- /dev/null +++ b/extmod/vfs.c @@ -0,0 +1,310 @@ +/* + * 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. + */ + +#include +#include + +#include "py/runtime.h" +#include "py/objstr.h" +#include "py/mperrno.h" +#include "extmod/vfs.h" + +#if MICROPY_VFS + +// ROOT is 0 so that the default current directory is the root directory +#define VFS_NONE ((vfs_mount_t*)1) +#define VFS_ROOT ((vfs_mount_t*)0) + +typedef struct _vfs_mount_t { + const char *str; // mount point with leading / + size_t len; + mp_obj_t obj; + struct _vfs_mount_t *next; +} vfs_mount_t; + +// path is the path to lookup and *path_out holds the path within the VFS +// object (starts with / if an absolute path). +// Returns VFS_ROOT for root dir (and then path_out is undefined) and VFS_NONE +// for path not found. +STATIC vfs_mount_t *lookup_path_raw(const char *path, const char **path_out) { + if (path[0] == '/' && path[1] == 0) { + return VFS_ROOT; + } else if (MP_STATE_VM(vfs_cur) == VFS_ROOT) { + // in root dir + if (path[0] == 0) { + return VFS_ROOT; + } + } else if (*path != '/') { + // a relative path within a mounted device + *path_out = path; + return MP_STATE_VM(vfs_cur); + } + + for (vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) { + if (strncmp(path, vfs->str, vfs->len) == 0) { + if (path[vfs->len] == '/') { + *path_out = path + vfs->len; + return vfs; + } else if (path[vfs->len] == '\0') { + *path_out = "/"; + return vfs; + } + } + } + + // mount point not found + return VFS_NONE; +} + +// Version of lookup_path_raw that takes and returns uPy string objects. +STATIC vfs_mount_t *lookup_path(mp_obj_t path_in, mp_obj_t *path_out) { + const char *path = mp_obj_str_get_str(path_in); + const char *p_out; + vfs_mount_t *vfs = lookup_path_raw(path, &p_out); + if (vfs != VFS_NONE && vfs != VFS_ROOT) { + *path_out = mp_obj_new_str_of_type(mp_obj_get_type(path_in), + (const byte*)p_out, strlen(p_out)); + } + return vfs; +} + +STATIC mp_obj_t mp_vfs_proxy_call(vfs_mount_t *vfs, qstr meth_name, size_t n_args, const mp_obj_t *args) { + if (vfs == VFS_NONE) { + // mount point not found + mp_raise_OSError(MP_ENODEV); + } + if (vfs == VFS_ROOT) { + // can't do operation on root dir + mp_raise_OSError(MP_EPERM); + } + mp_obj_t meth[n_args + 2]; + mp_load_method(vfs->obj, meth_name, meth); + if (args != NULL) { + memcpy(meth + 2, args, n_args * sizeof(*args)); + } + return mp_call_method_n_kw(n_args, 0, meth); +} + +mp_import_stat_t mp_vfs_import_stat(const char *path) { + const char *path_out; + vfs_mount_t *vfs = lookup_path_raw(path, &path_out); + if (vfs == VFS_NONE || vfs == VFS_ROOT) { + return MP_IMPORT_STAT_NO_EXIST; + } + // TODO delegate to vfs.stat() method + return MP_IMPORT_STAT_NO_EXIST; +} + +mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_readonly, ARG_mkfs }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_readonly, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_false} }, + { MP_QSTR_mkfs, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_false} }, + }; + + // parse args + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // get the mount point + mp_uint_t mnt_len; + const char *mnt_str = mp_obj_str_get_data(pos_args[1], &mnt_len); + + // create new object + vfs_mount_t *vfs = m_new_obj(vfs_mount_t); + vfs->str = mnt_str; + vfs->len = mnt_len; + vfs->obj = pos_args[0]; + vfs->next = NULL; + + // call the underlying object to do any mounting operation + mp_vfs_proxy_call(vfs, MP_QSTR_mount, 2, (mp_obj_t*)&args); + + // check that the destination mount point is unused + const char *path_out; + if (lookup_path_raw(mp_obj_str_get_str(pos_args[1]), &path_out) != VFS_NONE) { + mp_raise_OSError(MP_EPERM); + } + + // insert the vfs into the mount table + vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table); + while (*vfsp != NULL) { + vfsp = &(*vfsp)->next; + } + *vfsp = vfs; + + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_KW(mp_vfs_mount_obj, 2, mp_vfs_mount); + +mp_obj_t mp_vfs_umount(mp_obj_t mnt_in) { + // remove vfs from the mount table + vfs_mount_t *vfs = NULL; + mp_uint_t mnt_len; + const char *mnt_str = NULL; + if (MP_OBJ_IS_STR(mnt_in)) { + mnt_str = mp_obj_str_get_data(mnt_in, &mnt_len); + } + for (vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table); *vfsp != NULL; vfsp = &(*vfsp)->next) { + if ((mnt_str != NULL && !memcmp(mnt_str, (*vfsp)->str, mnt_len + 1)) || (*vfsp)->obj == mnt_in) { + vfs = *vfsp; + *vfsp = (*vfsp)->next; + break; + } + } + + if (vfs == NULL) { + mp_raise_OSError(MP_EINVAL); + } + + // if we unmounted the current device then set current to root + if (MP_STATE_VM(vfs_cur) == vfs) { + MP_STATE_VM(vfs_cur) = VFS_ROOT; + } + + // call the underlying object to do any unmounting operation + mp_vfs_proxy_call(vfs, MP_QSTR_umount, 0, NULL); + + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_umount_obj, mp_vfs_umount); + +mp_obj_t mp_vfs_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_file, ARG_mode, ARG_encoding }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_file, MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} }, + { MP_QSTR_mode, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_QSTR(MP_QSTR_r)} }, + }; + + // parse args + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + vfs_mount_t *vfs = lookup_path((mp_obj_t)args[ARG_file].u_rom_obj, &args[ARG_file].u_obj); + return mp_vfs_proxy_call(vfs, MP_QSTR_open, 2, (mp_obj_t*)&args); +} +MP_DEFINE_CONST_FUN_OBJ_KW(mp_vfs_open_obj, 0, mp_vfs_open); + +mp_obj_t mp_vfs_chdir(mp_obj_t path_in) { + mp_obj_t path_out; + vfs_mount_t *vfs = lookup_path(path_in, &path_out); + if (vfs != VFS_ROOT) { + mp_vfs_proxy_call(vfs, MP_QSTR_chdir, 1, &path_out); + } + MP_STATE_VM(vfs_cur) = vfs; + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_chdir_obj, mp_vfs_chdir); + +mp_obj_t mp_vfs_getcwd(void) { + if (MP_STATE_VM(vfs_cur) == VFS_ROOT) { + return MP_OBJ_NEW_QSTR(MP_QSTR__slash_); + } + mp_obj_t cwd_o = mp_vfs_proxy_call(MP_STATE_VM(vfs_cur), MP_QSTR_getcwd, 0, NULL); + const char *cwd = mp_obj_str_get_str(cwd_o); + vstr_t vstr; + vstr_init(&vstr, MP_STATE_VM(vfs_cur)->len + strlen(cwd) + 1); + vstr_add_strn(&vstr, MP_STATE_VM(vfs_cur)->str, MP_STATE_VM(vfs_cur)->len); + if (!(cwd[0] == '/' && cwd[1] == 0)) { + vstr_add_str(&vstr, cwd); + } + return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); +} +MP_DEFINE_CONST_FUN_OBJ_0(mp_vfs_getcwd_obj, mp_vfs_getcwd); + +mp_obj_t mp_vfs_listdir(size_t n_args, const mp_obj_t *args) { + mp_obj_t path_in; + if (n_args == 1) { + path_in = args[0]; + } else { + path_in = MP_OBJ_NEW_QSTR(MP_QSTR_); + } + + mp_obj_t path_out; + vfs_mount_t *vfs = lookup_path(path_in, &path_out); + + if (vfs == VFS_ROOT) { + // list the root directory + mp_obj_t dir_list = mp_obj_new_list(0, NULL); + for (vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) { + mp_obj_list_append(dir_list, mp_obj_new_str_of_type(mp_obj_get_type(path_in), + (const byte*)vfs->str + 1, vfs->len - 1)); + } + return dir_list; + } + + return mp_vfs_proxy_call(vfs, MP_QSTR_listdir, 1, &path_out); +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_vfs_listdir_obj, 0, 1, mp_vfs_listdir); + +mp_obj_t mp_vfs_mkdir(mp_obj_t path_in) { + mp_obj_t path_out; + vfs_mount_t *vfs = lookup_path(path_in, &path_out); + return mp_vfs_proxy_call(vfs, MP_QSTR_mkdir, 1, &path_out); +} +MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_mkdir_obj, mp_vfs_mkdir); + +mp_obj_t mp_vfs_remove(mp_obj_t path_in) { + mp_obj_t path_out; + vfs_mount_t *vfs = lookup_path(path_in, &path_out); + return mp_vfs_proxy_call(vfs, MP_QSTR_remove, 1, &path_out); +} +MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_remove_obj, mp_vfs_remove); + +mp_obj_t mp_vfs_rename(mp_obj_t old_path_in, mp_obj_t new_path_in) { + mp_obj_t args[2]; + vfs_mount_t *old_vfs = lookup_path(old_path_in, &args[0]); + vfs_mount_t *new_vfs = lookup_path(new_path_in, &args[1]); + if (old_vfs != new_vfs) { + // can't rename across filesystems + mp_raise_OSError(MP_EPERM); + } + return mp_vfs_proxy_call(old_vfs, MP_QSTR_rename, 2, args); +} +MP_DEFINE_CONST_FUN_OBJ_2(mp_vfs_rename_obj, mp_vfs_rename); + +mp_obj_t mp_vfs_rmdir(mp_obj_t path_in) { + mp_obj_t path_out; + vfs_mount_t *vfs = lookup_path(path_in, &path_out); + return mp_vfs_proxy_call(vfs, MP_QSTR_rmdir, 1, &path_out); +} +MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_rmdir_obj, mp_vfs_rmdir); + +mp_obj_t mp_vfs_stat(mp_obj_t path_in) { + mp_obj_t path_out; + vfs_mount_t *vfs = lookup_path(path_in, &path_out); + return mp_vfs_proxy_call(vfs, MP_QSTR_stat, 1, &path_out); +} +MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_stat_obj, mp_vfs_stat); + +mp_obj_t mp_vfs_statvfs(mp_obj_t path_in) { + mp_obj_t path_out; + vfs_mount_t *vfs = lookup_path(path_in, &path_out); + return mp_vfs_proxy_call(vfs, MP_QSTR_statvfs, 1, &path_out); +} +MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_statvfs_obj, mp_vfs_statvfs); + +#endif // MICROPY_VFS diff --git a/extmod/vfs.h b/extmod/vfs.h new file mode 100644 index 000000000..68f0b7154 --- /dev/null +++ b/extmod/vfs.h @@ -0,0 +1,60 @@ +/* + * 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_EXTMOD_VFS_H +#define MICROPY_INCLUDED_EXTMOD_VFS_H + +#include "py/lexer.h" +#include "py/obj.h" + +mp_import_stat_t mp_vfs_import_stat(const char *path); +mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); +mp_obj_t mp_vfs_umount(mp_obj_t mnt_in); +mp_obj_t mp_vfs_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); +mp_obj_t mp_vfs_chdir(mp_obj_t path_in); +mp_obj_t mp_vfs_getcwd(void); +mp_obj_t mp_vfs_listdir(size_t n_args, const mp_obj_t *args); +mp_obj_t mp_vfs_mkdir(mp_obj_t path_in); +mp_obj_t mp_vfs_remove(mp_obj_t path_in); +mp_obj_t mp_vfs_rename(mp_obj_t old_path_in, mp_obj_t new_path_in); +mp_obj_t mp_vfs_rmdir(mp_obj_t path_in); +mp_obj_t mp_vfs_stat(mp_obj_t path_in); +mp_obj_t mp_vfs_statvfs(mp_obj_t path_in); + +MP_DECLARE_CONST_FUN_OBJ_KW(mp_vfs_mount_obj); +MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_umount_obj); +MP_DECLARE_CONST_FUN_OBJ_KW(mp_vfs_open_obj); +MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_chdir_obj); +MP_DECLARE_CONST_FUN_OBJ_0(mp_vfs_getcwd_obj); +MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_vfs_listdir_obj); +MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_mkdir_obj); +MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_remove_obj); +MP_DECLARE_CONST_FUN_OBJ_2(mp_vfs_rename_obj); +MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_rmdir_obj); +MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_stat_obj); +MP_DECLARE_CONST_FUN_OBJ_1(mp_vfs_statvfs_obj); + +#endif // MICROPY_INCLUDED_EXTMOD_VFS_H diff --git a/extmod/vfs_reader.c b/extmod/vfs_reader.c new file mode 100644 index 000000000..718bdeeb6 --- /dev/null +++ b/extmod/vfs_reader.c @@ -0,0 +1,97 @@ +/* + * 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 +#include + +#include "py/nlr.h" +#include "py/stream.h" +#include "py/reader.h" +#include "extmod/vfs.h" + +#if MICROPY_READER_VFS + +typedef struct _mp_reader_vfs_t { + mp_obj_t file; + uint16_t len; + uint16_t pos; + byte buf[24]; +} mp_reader_vfs_t; + +STATIC mp_uint_t mp_reader_vfs_readbyte(void *data) { + mp_reader_vfs_t *reader = (mp_reader_vfs_t*)data; + if (reader->pos >= reader->len) { + if (reader->len < sizeof(reader->buf)) { + return MP_READER_EOF; + } else { + int errcode; + reader->len = mp_stream_rw(reader->file, reader->buf, sizeof(reader->buf), + &errcode, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE); + if (errcode != 0) { + // TODO handle errors properly + return MP_READER_EOF; + } + if (reader->len == 0) { + return MP_READER_EOF; + } + reader->pos = 0; + } + } + return reader->buf[reader->pos++]; +} + +STATIC void mp_reader_vfs_close(void *data) { + mp_reader_vfs_t *reader = (mp_reader_vfs_t*)data; + mp_stream_close(reader->file); + m_del_obj(mp_reader_vfs_t, reader); +} + +int mp_reader_new_file(mp_reader_t *reader, const char *filename) { + mp_reader_vfs_t *rf = m_new_obj_maybe(mp_reader_vfs_t); + if (rf == NULL) { + return MP_ENOMEM; + } + // TODO we really should just let this function raise a uPy exception + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + mp_obj_t arg = mp_obj_new_str(filename, strlen(filename), false); + rf->file = mp_vfs_open(1, &arg, (mp_map_t*)&mp_const_empty_map); + int errcode; + rf->len = mp_stream_rw(rf->file, rf->buf, sizeof(rf->buf), &errcode, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE); + if (errcode != 0) { + return errcode; + } + } else { + return MP_ENOENT; // assume error was "file not found" + } + rf->pos = 0; + reader->data = rf; + reader->readbyte = mp_reader_vfs_readbyte; + reader->close = mp_reader_vfs_close; + return 0; // success +} + +#endif // MICROPY_READER_VFS diff --git a/py/lexer.c b/py/lexer.c index 458fba090..e9b571ca4 100644 --- a/py/lexer.c +++ b/py/lexer.c @@ -753,7 +753,7 @@ mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, mp_uint_t return mp_lexer_new(src_name, reader); } -#if MICROPY_READER_POSIX || MICROPY_READER_FATFS +#if MICROPY_READER_POSIX || MICROPY_READER_VFS || MICROPY_READER_FATFS mp_lexer_t *mp_lexer_new_from_file(const char *filename) { mp_reader_t reader; diff --git a/py/mpconfig.h b/py/mpconfig.h index 3bccada11..a924eda0c 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -398,6 +398,11 @@ #define MICROPY_READER_POSIX (0) #endif +// Whether to use the VFS reader for importing files +#ifndef MICROPY_READER_VFS +#define MICROPY_READER_VFS (0) +#endif + // Whether to use the FatFS reader for importing files #ifndef MICROPY_READER_FATFS #define MICROPY_READER_FATFS (0) @@ -621,6 +626,11 @@ typedef double mp_float_t; #define MICROPY_FSUSERMOUNT (0) #endif +// Support for generic VFS sub-system +#ifndef MICROPY_VFS +#define MICROPY_VFS (0) +#endif + /*****************************************************************************/ /* Fine control over Python builtins, classes, modules, etc */ diff --git a/py/mpstate.h b/py/mpstate.h index 91fb68b3a..9c73f7778 100644 --- a/py/mpstate.h +++ b/py/mpstate.h @@ -165,6 +165,11 @@ typedef struct _mp_state_vm_t { struct _fs_user_mount_t *fs_user_mount[MICROPY_FATFS_VOLUMES]; #endif + #if MICROPY_VFS + struct _vfs_mount_t *vfs_cur; + struct _vfs_mount_t *vfs_mount_table; + #endif + // // END ROOT POINTER SECTION //////////////////////////////////////////////////////////// diff --git a/py/py.mk b/py/py.mk index 69819054b..94265c3f4 100644 --- a/py/py.mk +++ b/py/py.mk @@ -233,6 +233,8 @@ PY_O_BASENAME = \ ../extmod/modwebrepl.o \ ../extmod/modframebuf.o \ ../extmod/fsusermount.o \ + ../extmod/vfs.o \ + ../extmod/vfs_reader.o \ ../extmod/vfs_fat.o \ ../extmod/vfs_fat_ffconf.o \ ../extmod/vfs_fat_diskio.o \ diff --git a/py/qstrdefs.h b/py/qstrdefs.h index c98a253a6..4581e5e1b 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -36,6 +36,7 @@ QCFG(BYTES_IN_HASH, MICROPY_QSTR_BYTES_IN_HASH) Q() Q(*) Q(_) +Q(/) Q(%#o) Q(%#x) Q({:#b}) diff --git a/py/runtime.c b/py/runtime.c index 0ccfd8d87..e6aef21d7 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -105,6 +105,12 @@ void mp_init(void) { memset(MP_STATE_VM(fs_user_mount), 0, sizeof(MP_STATE_VM(fs_user_mount))); #endif + #if MICROPY_VFS + // initialise the VFS sub-system + MP_STATE_VM(vfs_cur) = NULL; + MP_STATE_VM(vfs_mount_table) = NULL; + #endif + #if MICROPY_PY_THREAD_GIL mp_thread_mutex_init(&MP_STATE_VM(gil_mutex)); #endif -- cgit v1.2.3 From fb3ae1784e1905709f82aadb7f1c8994682f9759 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 27 Jan 2017 15:13:32 +1100 Subject: extmod/vfs_fat: Rework to support new generic VFS sub-system. The VfsFat object can now be mounted by the generic VFS sub-system. --- extmod/vfs_fat.c | 127 ++++++++++++++++++++++++++++-------------------- extmod/vfs_fat.h | 2 +- extmod/vfs_fat_diskio.c | 4 +- extmod/vfs_fat_file.c | 20 +++----- 4 files changed, 86 insertions(+), 67 deletions(-) (limited to 'extmod') diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index 36e5031a8..45f2991da 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -28,6 +28,10 @@ #include "py/mpconfig.h" #if MICROPY_VFS_FAT +#if !MICROPY_VFS +#error "with MICROPY_VFS_FAT enabled, must also enable MICROPY_VFS" +#endif + #if !MICROPY_FATFS_OO #error "with MICROPY_VFS_FAT enabled, must also enable MICROPY_FATFS_OO" #endif @@ -44,21 +48,49 @@ #define mp_obj_fat_vfs_t fs_user_mount_t STATIC mp_obj_t fat_vfs_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, 2, 2, false); - mp_obj_fat_vfs_t *vfs = fatfs_mount_mkfs(n_args, args, (mp_map_t*)&mp_const_empty_map, false); + mp_arg_check_num(n_args, n_kw, 1, 1, false); + + // create new object + fs_user_mount_t *vfs = m_new_obj(fs_user_mount_t); vfs->base.type = type; + vfs->flags = FSUSER_FREE_OBJ; + vfs->str = NULL; + vfs->len = 0; + vfs->fatfs.drv = vfs; + + // load block protocol methods + mp_load_method(args[0], MP_QSTR_readblocks, vfs->readblocks); + mp_load_method_maybe(args[0], MP_QSTR_writeblocks, vfs->writeblocks); + mp_load_method_maybe(args[0], MP_QSTR_ioctl, vfs->u.ioctl); + if (vfs->u.ioctl[0] != MP_OBJ_NULL) { + // device supports new block protocol, so indicate it + vfs->flags |= FSUSER_HAVE_IOCTL; + } else { + // no ioctl method, so assume the device uses the old block protocol + mp_load_method_maybe(args[0], MP_QSTR_sync, vfs->u.old.sync); + mp_load_method(args[0], MP_QSTR_count, vfs->u.old.count); + } + return MP_OBJ_FROM_PTR(vfs); } STATIC mp_obj_t fat_vfs_mkfs(mp_obj_t bdev_in) { - mp_obj_t args[] = {bdev_in, MP_OBJ_NEW_QSTR(MP_QSTR_mkfs)}; - fatfs_mount_mkfs(2, args, (mp_map_t*)&mp_const_empty_map, true); + // create new object + fs_user_mount_t *vfs = MP_OBJ_TO_PTR(fat_vfs_make_new(&mp_fat_vfs_type, 1, 0, &bdev_in)); + + // make the filesystem + uint8_t working_buf[_MAX_SS]; + FRESULT res = f_mkfs(&vfs->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf)); + if (res != FR_OK) { + mp_raise_OSError(fresult_to_errno_table[res]); + } + return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_mkfs_fun_obj, fat_vfs_mkfs); STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(fat_vfs_mkfs_obj, MP_ROM_PTR(&fat_vfs_mkfs_fun_obj)); -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(fat_vfs_open_obj, 2, fatfs_builtin_open_self); +STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_open_obj, fatfs_builtin_open_self); STATIC mp_obj_t fat_vfs_listdir_func(size_t n_args, const mp_obj_t *args) { mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(args[0]); @@ -163,37 +195,14 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_chdir_obj, fat_vfs_chdir); STATIC mp_obj_t fat_vfs_getcwd(mp_obj_t vfs_in) { mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in); char buf[MICROPY_ALLOC_PATH_MAX + 1]; - memcpy(buf, self->str, self->len); - FRESULT res = f_getcwd(&self->fatfs, buf + self->len, sizeof(buf) - self->len); + FRESULT res = f_getcwd(&self->fatfs, buf, sizeof(buf)); if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); } - // remove trailing / if in root dir, because we prepended the mount point - size_t l = strlen(buf); - if (res == FR_OK && buf[l - 1] == '/') { - buf[l - 1] = 0; - } return mp_obj_new_str(buf, strlen(buf), false); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getcwd_obj, fat_vfs_getcwd); -// Checks for path equality, ignoring trailing slashes: -// path_equal(/, /) -> 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) { - while (*path_canonical != '\0' && *path == *path_canonical) { - ++path; - ++path_canonical; - } - if (*path_canonical != '\0') { - return false; - } - while (*path == '/') { - ++path; - } - return *path == '\0'; -} - /// \function stat(path) /// Get the status of a file or directory. STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) { @@ -201,31 +210,14 @@ STATIC mp_obj_t fat_vfs_stat(mp_obj_t vfs_in, mp_obj_t path_in) { const char *path = mp_obj_str_get_str(path_in); FILINFO fno; - FRESULT res; - - if (path_equal(path, "/")) { + if (path[0] == 0 || (path[0] == '/' && path[1] == 0)) { // stat root directory fno.fsize = 0; fno.fdate = 0x2821; // Jan 1, 2000 fno.ftime = 0; fno.fattrib = AM_DIR; } else { - res = FR_NO_PATH; - for (size_t i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) { - fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i]; - if (vfs != NULL && path_equal(path, vfs->str)) { - // stat mounted device directory - fno.fsize = 0; - fno.fdate = 0x2821; // Jan 1, 2000 - fno.ftime = 0; - fno.fattrib = AM_DIR; - res = FR_OK; - } - } - if (res == FR_NO_PATH) { - // stat normal file - res = f_stat(&self->fatfs, path, &fno); - } + FRESULT res = f_stat(&self->fatfs, path, &fno); if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); } @@ -290,12 +282,42 @@ STATIC mp_obj_t fat_vfs_statvfs(mp_obj_t vfs_in, mp_obj_t path_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_statvfs_obj, fat_vfs_statvfs); -// Unmount the filesystem -STATIC mp_obj_t fat_vfs_umount(mp_obj_t vfs_in) { - fatfs_umount(((fs_user_mount_t *)vfs_in)->readblocks[1]); +STATIC mp_obj_t vfs_fat_mount(mp_obj_t self_in, mp_obj_t readonly, mp_obj_t mkfs) { + fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in); + + // 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 (mp_obj_is_true(readonly)) { + self->writeblocks[0] = MP_OBJ_NULL; + } + + // mount the block device + FRESULT res = f_mount(&self->fatfs); + + // check if we need to make the filesystem + if (res == FR_NO_FILESYSTEM && mp_obj_is_true(mkfs)) { + uint8_t working_buf[_MAX_SS]; + res = f_mkfs(&self->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf)); + } + if (res != FR_OK) { + mp_raise_OSError(fresult_to_errno_table[res]); + } + + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(vfs_fat_mount_obj, vfs_fat_mount); + +STATIC mp_obj_t vfs_fat_umount(mp_obj_t self_in) { + fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in); + FRESULT res = f_umount(&self->fatfs); + if (res != FR_OK) { + mp_raise_OSError(fresult_to_errno_table[res]); + } return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_umount_obj, fat_vfs_umount); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_umount_obj, vfs_fat_umount); STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_mkfs), MP_ROM_PTR(&fat_vfs_mkfs_obj) }, @@ -309,6 +331,7 @@ STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&fat_vfs_rename_obj) }, { MP_ROM_QSTR(MP_QSTR_stat), MP_ROM_PTR(&fat_vfs_stat_obj) }, { MP_ROM_QSTR(MP_QSTR_statvfs), MP_ROM_PTR(&fat_vfs_statvfs_obj) }, + { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&vfs_fat_mount_obj) }, { MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&fat_vfs_umount_obj) }, }; STATIC MP_DEFINE_CONST_DICT(fat_vfs_locals_dict, fat_vfs_locals_dict_table); diff --git a/extmod/vfs_fat.h b/extmod/vfs_fat.h index 441b35c04..52674eb6e 100644 --- a/extmod/vfs_fat.h +++ b/extmod/vfs_fat.h @@ -32,7 +32,7 @@ extern const mp_obj_type_t mp_fat_vfs_type; struct _fs_user_mount_t *ff_get_vfs(const char **path); mp_obj_t fatfs_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs); -mp_obj_t fatfs_builtin_open_self(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs); +mp_obj_t fatfs_builtin_open_self(mp_obj_t self_in, mp_obj_t path, mp_obj_t mode); MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj); mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type); diff --git a/extmod/vfs_fat_diskio.c b/extmod/vfs_fat_diskio.c index 9e91b26f7..3bbd37435 100644 --- a/extmod/vfs_fat_diskio.c +++ b/extmod/vfs_fat_diskio.c @@ -28,7 +28,7 @@ */ #include "py/mpconfig.h" -#if MICROPY_FSUSERMOUNT +#if MICROPY_VFS || MICROPY_FSUSERMOUNT #include #include @@ -305,4 +305,4 @@ DRESULT disk_ioctl ( } #endif -#endif // MICROPY_FSUSERMOUNT +#endif // MICROPY_VFS || MICROPY_FSUSERMOUNT diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index ea332709e..f5a188036 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -27,7 +27,7 @@ #include "py/mpconfig.h" // *_ADHOC part is for cc3200 port which doesn't use general uPy // infrastructure and instead duplicates code. TODO: Resolve. -#if MICROPY_FSUSERMOUNT || MICROPY_FSUSERMOUNT_ADHOC +#if MICROPY_VFS || MICROPY_FSUSERMOUNT || MICROPY_FSUSERMOUNT_ADHOC #include #include @@ -224,13 +224,7 @@ STATIC mp_obj_t file_open(fs_user_mount_t *vfs, const mp_obj_type_t *type, mp_ar const char *fname = mp_obj_str_get_str(args[0].u_obj); #if MICROPY_FATFS_OO - if (vfs == NULL) { - vfs = ff_get_vfs(&fname); - if (vfs == NULL) { - m_del_obj(pyb_file_obj_t, o); - mp_raise_OSError(MP_ENOENT); - } - } + assert(vfs != NULL); FRESULT res = f_open(&vfs->fatfs, &o->fp, fname, mode); #else (void)vfs; @@ -320,12 +314,14 @@ mp_obj_t fatfs_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw } // Factory function for I/O stream classes -mp_obj_t fatfs_builtin_open_self(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { +mp_obj_t fatfs_builtin_open_self(mp_obj_t self_in, mp_obj_t path, mp_obj_t mode) { // TODO: analyze buffering args and instantiate appropriate type - fs_user_mount_t *self = MP_OBJ_TO_PTR(args[0]); + fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in); mp_arg_val_t arg_vals[FILE_OPEN_NUM_ARGS]; - mp_arg_parse_all(n_args - 1, args + 1, kwargs, FILE_OPEN_NUM_ARGS, file_open_args, arg_vals); + arg_vals[0].u_obj = path; + arg_vals[1].u_obj = mode; + arg_vals[2].u_obj = mp_const_none; return file_open(self, &mp_type_textio, arg_vals); } -#endif // MICROPY_FSUSERMOUNT +#endif // MICROPY_VFS || MICROPY_FSUSERMOUNT -- cgit v1.2.3 From 6c23c7587f1c02f58e9246ec59fe4f6544728b50 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 27 Jan 2017 17:17:54 +1100 Subject: extmod/vfs: Add ability for VFS sub-system to import using VfsFat. --- extmod/vfs.c | 7 +++++++ extmod/vfs_fat.h | 3 +++ extmod/vfs_fat_misc.c | 10 +++------- stmhal/import.c | 5 ++--- 4 files changed, 15 insertions(+), 10 deletions(-) (limited to 'extmod') diff --git a/extmod/vfs.c b/extmod/vfs.c index 2880271c6..0cba0bc58 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -31,6 +31,7 @@ #include "py/objstr.h" #include "py/mperrno.h" #include "extmod/vfs.h" +#include "extmod/vfs_fat.h" #if MICROPY_VFS @@ -114,6 +115,12 @@ mp_import_stat_t mp_vfs_import_stat(const char *path) { if (vfs == VFS_NONE || vfs == 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; } diff --git a/extmod/vfs_fat.h b/extmod/vfs_fat.h index 52674eb6e..1ea8a9637 100644 --- a/extmod/vfs_fat.h +++ b/extmod/vfs_fat.h @@ -24,6 +24,8 @@ * THE SOFTWARE. */ +#include "py/lexer.h" + struct _fs_user_mount_t; extern const byte fresult_to_errno_table[20]; @@ -31,6 +33,7 @@ extern const mp_obj_type_t mp_fat_vfs_type; struct _fs_user_mount_t *ff_get_vfs(const char **path); +mp_import_stat_t fat_vfs_import_stat(struct _fs_user_mount_t *vfs, const char *path); mp_obj_t fatfs_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs); mp_obj_t fatfs_builtin_open_self(mp_obj_t self_in, mp_obj_t path, mp_obj_t mode); MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj); diff --git a/extmod/vfs_fat_misc.c b/extmod/vfs_fat_misc.c index ea267a15f..489e53586 100644 --- a/extmod/vfs_fat_misc.c +++ b/extmod/vfs_fat_misc.c @@ -108,21 +108,17 @@ mp_obj_t fat_vfs_listdir2(fs_user_mount_t *vfs, const char *path, bool is_str_ty return dir_list; } -mp_import_stat_t fat_vfs_import_stat(const char *path); - -mp_import_stat_t fat_vfs_import_stat(const char *path) { +mp_import_stat_t fat_vfs_import_stat(fs_user_mount_t *vfs, const char *path) { FILINFO fno; #if !MICROPY_FATFS_OO && _USE_LFN fno.lfname = NULL; fno.lfsize = 0; #endif #if MICROPY_FATFS_OO - fs_user_mount_t *vfs = ff_get_vfs(&path); - if (vfs == NULL) { - return MP_IMPORT_STAT_NO_EXIST; - } + assert(vfs != NULL); FRESULT res = f_stat(&vfs->fatfs, path, &fno); #else + (void)vfs; FRESULT res = f_stat(path, &fno); #endif if (res == FR_OK) { diff --git a/stmhal/import.c b/stmhal/import.c index 1edbe2caa..2bc282e7b 100644 --- a/stmhal/import.c +++ b/stmhal/import.c @@ -28,9 +28,8 @@ #include "py/lexer.h" #include "lib/fatfs/ff.h" - -mp_import_stat_t fat_vfs_import_stat(const char *path); +#include "extmod/vfs_fat.h" mp_import_stat_t mp_import_stat(const char *path) { - return fat_vfs_import_stat(path); + return fat_vfs_import_stat(NULL, path); } -- cgit v1.2.3 From f488fa29e485964d15f9f9dbfad5180c580e4b24 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 27 Jan 2017 21:01:18 +1100 Subject: extmod/modlwip: Add socket.readinto() method. --- extmod/modlwip.c | 1 + 1 file changed, 1 insertion(+) (limited to 'extmod') diff --git a/extmod/modlwip.c b/extmod/modlwip.c index 62699bd41..0f2a6c64b 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -1172,6 +1172,7 @@ STATIC const mp_map_elem_t lwip_socket_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_makefile), (mp_obj_t)&lwip_socket_makefile_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&mp_stream_read_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_readinto), (mp_obj_t)&mp_stream_readinto_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_readline), (mp_obj_t)&mp_stream_unbuffered_readline_obj}, { MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&mp_stream_write_obj }, }; -- cgit v1.2.3 From 3f6b4e08e38d3205f7a5a21f7ff81ab9f3c3c497 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 27 Jan 2017 22:40:15 +1100 Subject: extmod/vfs: Expose mp_vfs_mount_t type. It should only be used for low-level things and with caution, for example putting mounted VFS data in ROM or the static data section. --- extmod/vfs.c | 51 ++++++++++++++++++++++----------------------------- extmod/vfs.h | 7 +++++++ py/mpstate.h | 4 ++-- 3 files changed, 31 insertions(+), 31 deletions(-) (limited to 'extmod') diff --git a/extmod/vfs.c b/extmod/vfs.c index 0cba0bc58..43f4456ad 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -36,21 +36,14 @@ #if MICROPY_VFS // ROOT is 0 so that the default current directory is the root directory -#define VFS_NONE ((vfs_mount_t*)1) -#define VFS_ROOT ((vfs_mount_t*)0) - -typedef struct _vfs_mount_t { - const char *str; // mount point with leading / - size_t len; - mp_obj_t obj; - struct _vfs_mount_t *next; -} vfs_mount_t; +#define VFS_NONE ((mp_vfs_mount_t*)1) +#define VFS_ROOT ((mp_vfs_mount_t*)0) // path is the path to lookup and *path_out holds the path within the VFS // object (starts with / if an absolute path). // Returns VFS_ROOT for root dir (and then path_out is undefined) and VFS_NONE // for path not found. -STATIC vfs_mount_t *lookup_path_raw(const char *path, const char **path_out) { +STATIC mp_vfs_mount_t *lookup_path_raw(const char *path, const char **path_out) { if (path[0] == '/' && path[1] == 0) { return VFS_ROOT; } else if (MP_STATE_VM(vfs_cur) == VFS_ROOT) { @@ -64,7 +57,7 @@ STATIC vfs_mount_t *lookup_path_raw(const char *path, const char **path_out) { return MP_STATE_VM(vfs_cur); } - for (vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) { + for (mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) { if (strncmp(path, vfs->str, vfs->len) == 0) { if (path[vfs->len] == '/') { *path_out = path + vfs->len; @@ -81,10 +74,10 @@ STATIC vfs_mount_t *lookup_path_raw(const char *path, const char **path_out) { } // Version of lookup_path_raw that takes and returns uPy string objects. -STATIC vfs_mount_t *lookup_path(mp_obj_t path_in, mp_obj_t *path_out) { +STATIC mp_vfs_mount_t *lookup_path(mp_obj_t path_in, mp_obj_t *path_out) { const char *path = mp_obj_str_get_str(path_in); const char *p_out; - vfs_mount_t *vfs = lookup_path_raw(path, &p_out); + mp_vfs_mount_t *vfs = lookup_path_raw(path, &p_out); if (vfs != VFS_NONE && vfs != VFS_ROOT) { *path_out = mp_obj_new_str_of_type(mp_obj_get_type(path_in), (const byte*)p_out, strlen(p_out)); @@ -92,7 +85,7 @@ STATIC vfs_mount_t *lookup_path(mp_obj_t path_in, mp_obj_t *path_out) { return vfs; } -STATIC mp_obj_t mp_vfs_proxy_call(vfs_mount_t *vfs, qstr meth_name, size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t mp_vfs_proxy_call(mp_vfs_mount_t *vfs, qstr meth_name, size_t n_args, const mp_obj_t *args) { if (vfs == VFS_NONE) { // mount point not found mp_raise_OSError(MP_ENODEV); @@ -111,7 +104,7 @@ STATIC mp_obj_t mp_vfs_proxy_call(vfs_mount_t *vfs, qstr meth_name, size_t n_arg mp_import_stat_t mp_vfs_import_stat(const char *path) { const char *path_out; - vfs_mount_t *vfs = lookup_path_raw(path, &path_out); + mp_vfs_mount_t *vfs = lookup_path_raw(path, &path_out); if (vfs == VFS_NONE || vfs == VFS_ROOT) { return MP_IMPORT_STAT_NO_EXIST; } @@ -141,7 +134,7 @@ mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args const char *mnt_str = mp_obj_str_get_data(pos_args[1], &mnt_len); // create new object - vfs_mount_t *vfs = m_new_obj(vfs_mount_t); + mp_vfs_mount_t *vfs = m_new_obj(mp_vfs_mount_t); vfs->str = mnt_str; vfs->len = mnt_len; vfs->obj = pos_args[0]; @@ -157,7 +150,7 @@ mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args } // insert the vfs into the mount table - vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table); + mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table); while (*vfsp != NULL) { vfsp = &(*vfsp)->next; } @@ -169,13 +162,13 @@ MP_DEFINE_CONST_FUN_OBJ_KW(mp_vfs_mount_obj, 2, mp_vfs_mount); mp_obj_t mp_vfs_umount(mp_obj_t mnt_in) { // remove vfs from the mount table - vfs_mount_t *vfs = NULL; + mp_vfs_mount_t *vfs = NULL; mp_uint_t mnt_len; const char *mnt_str = NULL; if (MP_OBJ_IS_STR(mnt_in)) { mnt_str = mp_obj_str_get_data(mnt_in, &mnt_len); } - for (vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table); *vfsp != NULL; vfsp = &(*vfsp)->next) { + for (mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table); *vfsp != NULL; vfsp = &(*vfsp)->next) { if ((mnt_str != NULL && !memcmp(mnt_str, (*vfsp)->str, mnt_len + 1)) || (*vfsp)->obj == mnt_in) { vfs = *vfsp; *vfsp = (*vfsp)->next; @@ -210,14 +203,14 @@ mp_obj_t mp_vfs_open(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - vfs_mount_t *vfs = lookup_path((mp_obj_t)args[ARG_file].u_rom_obj, &args[ARG_file].u_obj); + mp_vfs_mount_t *vfs = lookup_path((mp_obj_t)args[ARG_file].u_rom_obj, &args[ARG_file].u_obj); return mp_vfs_proxy_call(vfs, MP_QSTR_open, 2, (mp_obj_t*)&args); } MP_DEFINE_CONST_FUN_OBJ_KW(mp_vfs_open_obj, 0, mp_vfs_open); mp_obj_t mp_vfs_chdir(mp_obj_t path_in) { mp_obj_t path_out; - vfs_mount_t *vfs = lookup_path(path_in, &path_out); + mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out); if (vfs != VFS_ROOT) { mp_vfs_proxy_call(vfs, MP_QSTR_chdir, 1, &path_out); } @@ -251,7 +244,7 @@ mp_obj_t mp_vfs_listdir(size_t n_args, const mp_obj_t *args) { } mp_obj_t path_out; - vfs_mount_t *vfs = lookup_path(path_in, &path_out); + mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out); if (vfs == VFS_ROOT) { // list the root directory @@ -269,22 +262,22 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_vfs_listdir_obj, 0, 1, mp_vfs_listdir); mp_obj_t mp_vfs_mkdir(mp_obj_t path_in) { mp_obj_t path_out; - vfs_mount_t *vfs = lookup_path(path_in, &path_out); + mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out); return mp_vfs_proxy_call(vfs, MP_QSTR_mkdir, 1, &path_out); } MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_mkdir_obj, mp_vfs_mkdir); mp_obj_t mp_vfs_remove(mp_obj_t path_in) { mp_obj_t path_out; - vfs_mount_t *vfs = lookup_path(path_in, &path_out); + mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out); return mp_vfs_proxy_call(vfs, MP_QSTR_remove, 1, &path_out); } MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_remove_obj, mp_vfs_remove); mp_obj_t mp_vfs_rename(mp_obj_t old_path_in, mp_obj_t new_path_in) { mp_obj_t args[2]; - vfs_mount_t *old_vfs = lookup_path(old_path_in, &args[0]); - vfs_mount_t *new_vfs = lookup_path(new_path_in, &args[1]); + mp_vfs_mount_t *old_vfs = lookup_path(old_path_in, &args[0]); + mp_vfs_mount_t *new_vfs = lookup_path(new_path_in, &args[1]); if (old_vfs != new_vfs) { // can't rename across filesystems mp_raise_OSError(MP_EPERM); @@ -295,21 +288,21 @@ MP_DEFINE_CONST_FUN_OBJ_2(mp_vfs_rename_obj, mp_vfs_rename); mp_obj_t mp_vfs_rmdir(mp_obj_t path_in) { mp_obj_t path_out; - vfs_mount_t *vfs = lookup_path(path_in, &path_out); + mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out); return mp_vfs_proxy_call(vfs, MP_QSTR_rmdir, 1, &path_out); } MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_rmdir_obj, mp_vfs_rmdir); mp_obj_t mp_vfs_stat(mp_obj_t path_in) { mp_obj_t path_out; - vfs_mount_t *vfs = lookup_path(path_in, &path_out); + mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out); return mp_vfs_proxy_call(vfs, MP_QSTR_stat, 1, &path_out); } MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_stat_obj, mp_vfs_stat); mp_obj_t mp_vfs_statvfs(mp_obj_t path_in) { mp_obj_t path_out; - vfs_mount_t *vfs = lookup_path(path_in, &path_out); + mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out); return mp_vfs_proxy_call(vfs, MP_QSTR_statvfs, 1, &path_out); } MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_statvfs_obj, mp_vfs_statvfs); diff --git a/extmod/vfs.h b/extmod/vfs.h index 68f0b7154..ac3ca6fc4 100644 --- a/extmod/vfs.h +++ b/extmod/vfs.h @@ -30,6 +30,13 @@ #include "py/lexer.h" #include "py/obj.h" +typedef struct _mp_vfs_mount_t { + const char *str; // mount point with leading / + size_t len; + mp_obj_t obj; + struct _mp_vfs_mount_t *next; +} mp_vfs_mount_t; + mp_import_stat_t mp_vfs_import_stat(const char *path); mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); mp_obj_t mp_vfs_umount(mp_obj_t mnt_in); diff --git a/py/mpstate.h b/py/mpstate.h index 9c73f7778..daf085a06 100644 --- a/py/mpstate.h +++ b/py/mpstate.h @@ -166,8 +166,8 @@ typedef struct _mp_state_vm_t { #endif #if MICROPY_VFS - struct _vfs_mount_t *vfs_cur; - struct _vfs_mount_t *vfs_mount_table; + struct _mp_vfs_mount_t *vfs_cur; + struct _mp_vfs_mount_t *vfs_mount_table; #endif // -- cgit v1.2.3 From 8aa8a0a660451e8eafc4aa0bca2116d561cebe4a Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 27 Jan 2017 22:42:06 +1100 Subject: extmod/vfs_fat: Use SECSIZE macro to determine FatFs sector size. --- extmod/vfs_fat.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'extmod') diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index 45f2991da..a6f2ae806 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -45,6 +45,12 @@ #include "extmod/fsusermount.h" #include "timeutils.h" +#if _MAX_SS == _MIN_SS +#define SECSIZE(fs) (_MIN_SS) +#else +#define SECSIZE(fs) ((fs)->ssize) +#endif + #define mp_obj_fat_vfs_t fs_user_mount_t STATIC mp_obj_t fat_vfs_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { @@ -267,7 +273,7 @@ STATIC mp_obj_t fat_vfs_statvfs(mp_obj_t vfs_in, mp_obj_t path_in) { mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL)); - t->items[0] = MP_OBJ_NEW_SMALL_INT(fatfs->csize * fatfs->ssize); // f_bsize + t->items[0] = MP_OBJ_NEW_SMALL_INT(fatfs->csize * SECSIZE(fatfs)); // f_bsize t->items[1] = t->items[0]; // f_frsize t->items[2] = MP_OBJ_NEW_SMALL_INT((fatfs->n_fatent - 2) * fatfs->csize); // f_blocks t->items[3] = MP_OBJ_NEW_SMALL_INT(nclst); // f_bfree -- cgit v1.2.3 From 7a7516d40ddc00b051dd8dcf8ab38b5f845dcec4 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 1 Jan 2017 19:09:25 +0300 Subject: extmod/machine_signal: Implement "signal" abstraction for machine module. A signal is like a pin, but ca also be inverted (active low). As such, it abstracts properties of various physical devices, like LEDs, buttons, relays, buzzers, etc. To instantiate a Signal: pin = machine.Pin(...) signal = machine.Signal(pin, inverted=True) signal has the same .value() and __call__() methods as a pin. --- extmod/machine_signal.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++ extmod/machine_signal.h | 35 +++++++++++++++ py/py.mk | 1 + 3 files changed, 150 insertions(+) create mode 100644 extmod/machine_signal.c create mode 100644 extmod/machine_signal.h (limited to 'extmod') diff --git a/extmod/machine_signal.c b/extmod/machine_signal.c new file mode 100644 index 000000000..fb179c438 --- /dev/null +++ b/extmod/machine_signal.c @@ -0,0 +1,114 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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. + */ + +#include "py/mpconfig.h" +#if MICROPY_PY_MACHINE + +#include "py/obj.h" +#include "py/runtime.h" +#include "extmod/virtpin.h" +#include "extmod/machine_signal.h" + +// Signal class + +typedef struct _machine_signal_t { + mp_obj_base_t base; + mp_obj_t pin; + bool inverted; +} machine_signal_t; + +STATIC mp_obj_t signal_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { + enum { ARG_pin, ARG_inverted }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_, MP_ARG_OBJ | MP_ARG_REQUIRED }, + { MP_QSTR_inverted, MP_ARG_BOOL, {.u_bool = false} }, + }; + + mp_arg_val_t parsed_args[MP_ARRAY_SIZE(allowed_args)]; + + mp_arg_parse_all_kw_array(n_args, n_kw, args, MP_ARRAY_SIZE(allowed_args), allowed_args, parsed_args); + + machine_signal_t *o = m_new_obj(machine_signal_t); + o->base.type = type; + o->pin = parsed_args[ARG_pin].u_obj; + o->inverted = parsed_args[ARG_inverted].u_bool; + return MP_OBJ_FROM_PTR(o); +} + +STATIC mp_uint_t signal_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) { + (void)errcode; + machine_signal_t *self = MP_OBJ_TO_PTR(self_in); + + switch (request) { + case MP_PIN_READ: { + return mp_virtual_pin_read(self->pin) ^ self->inverted; + } + case MP_PIN_WRITE: { + mp_virtual_pin_write(self->pin, arg ^ self->inverted); + return 0; + } + } + return -1; +} + +// fast method for getting/setting signal value +STATIC mp_obj_t signal_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { + mp_arg_check_num(n_args, n_kw, 0, 1, false); + if (n_args == 0) { + // get pin + return MP_OBJ_NEW_SMALL_INT(mp_virtual_pin_read(self_in)); + } else { + // set pin + mp_virtual_pin_write(self_in, mp_obj_is_true(args[0])); + return mp_const_none; + } +} + +STATIC mp_obj_t signal_value(size_t n_args, const mp_obj_t *args) { + return signal_call(args[0], n_args - 1, 0, args + 1); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(signal_value_obj, 1, 2, signal_value); + +STATIC const mp_rom_map_elem_t signal_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&signal_value_obj) }, +}; + +STATIC MP_DEFINE_CONST_DICT(signal_locals_dict, signal_locals_dict_table); + +STATIC const mp_pin_p_t signal_pin_p = { + .ioctl = signal_ioctl, +}; + +const mp_obj_type_t machine_signal_type = { + { &mp_type_type }, + .name = MP_QSTR_Signal, + .make_new = signal_make_new, + .call = signal_call, + .protocol = &signal_pin_p, + .locals_dict = (void*)&signal_locals_dict, +}; + +#endif // MICROPY_PY_MACHINE diff --git a/extmod/machine_signal.h b/extmod/machine_signal.h new file mode 100644 index 000000000..7f88cbaa8 --- /dev/null +++ b/extmod/machine_signal.h @@ -0,0 +1,35 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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_EXTMOD_MACHINE_SIGNAL_H__ +#define __MICROPY_INCLUDED_EXTMOD_MACHINE_SIGNAL_H__ + +#include "py/obj.h" + +extern const mp_obj_type_t machine_signal_type; + +#endif // __MICROPY_INCLUDED_EXTMOD_MACHINE_SIGNAL_H__ diff --git a/py/py.mk b/py/py.mk index 94265c3f4..e7e4fb9b7 100644 --- a/py/py.mk +++ b/py/py.mk @@ -222,6 +222,7 @@ PY_O_BASENAME = \ ../extmod/virtpin.o \ ../extmod/machine_mem.o \ ../extmod/machine_pinbase.o \ + ../extmod/machine_signal.o \ ../extmod/machine_pulse.o \ ../extmod/machine_i2c.o \ ../extmod/machine_spi.o \ -- cgit v1.2.3 From ec3274324b0f7460f0276957184dc4b9f33a9bc7 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 29 Jan 2017 23:05:33 +1100 Subject: extmod/vfs_fat: Update to use FF_DIR instead of DIR. --- extmod/vfs_fat_misc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'extmod') diff --git a/extmod/vfs_fat_misc.c b/extmod/vfs_fat_misc.c index 489e53586..2f06d9f63 100644 --- a/extmod/vfs_fat_misc.c +++ b/extmod/vfs_fat_misc.c @@ -54,7 +54,7 @@ mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type) { mp_obj_t fat_vfs_listdir2(fs_user_mount_t *vfs, const char *path, bool is_str_type) { FRESULT res; FILINFO fno; - DIR dir; + FF_DIR dir; #if !MICROPY_FATFS_OO && _USE_LFN fno.lfname = lfn; fno.lfsize = sizeof lfn; -- cgit v1.2.3 From 6eafa544865a5d6dcb18f9161f7a18bd4fb6229f Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 29 Jan 2017 15:14:15 +1100 Subject: extmod/vfs: Expose lookup_path_raw as mp_vfs_lookup_path. It can be useful for low-level lookup of paths by ports. --- extmod/vfs.c | 42 +++++++++++++++++++----------------------- extmod/vfs.h | 6 ++++++ 2 files changed, 25 insertions(+), 23 deletions(-) (limited to 'extmod') diff --git a/extmod/vfs.c b/extmod/vfs.c index 43f4456ad..97c9077a2 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -35,21 +35,17 @@ #if MICROPY_VFS -// ROOT is 0 so that the default current directory is the root directory -#define VFS_NONE ((mp_vfs_mount_t*)1) -#define VFS_ROOT ((mp_vfs_mount_t*)0) - // path is the path to lookup and *path_out holds the path within the VFS // object (starts with / if an absolute path). -// Returns VFS_ROOT for root dir (and then path_out is undefined) and VFS_NONE -// for path not found. -STATIC mp_vfs_mount_t *lookup_path_raw(const char *path, const char **path_out) { +// Returns MP_VFS_ROOT for root dir (and then path_out is undefined) and +// MP_VFS_NONE for path not found. +mp_vfs_mount_t *mp_vfs_lookup_path(const char *path, const char **path_out) { if (path[0] == '/' && path[1] == 0) { - return VFS_ROOT; - } else if (MP_STATE_VM(vfs_cur) == VFS_ROOT) { + return MP_VFS_ROOT; + } else if (MP_STATE_VM(vfs_cur) == MP_VFS_ROOT) { // in root dir if (path[0] == 0) { - return VFS_ROOT; + return MP_VFS_ROOT; } } else if (*path != '/') { // a relative path within a mounted device @@ -70,15 +66,15 @@ STATIC mp_vfs_mount_t *lookup_path_raw(const char *path, const char **path_out) } // mount point not found - return VFS_NONE; + return MP_VFS_NONE; } -// Version of lookup_path_raw that takes and returns uPy string objects. +// Version of mp_vfs_lookup_path that takes and returns uPy string objects. STATIC mp_vfs_mount_t *lookup_path(mp_obj_t path_in, mp_obj_t *path_out) { const char *path = mp_obj_str_get_str(path_in); const char *p_out; - mp_vfs_mount_t *vfs = lookup_path_raw(path, &p_out); - if (vfs != VFS_NONE && vfs != VFS_ROOT) { + mp_vfs_mount_t *vfs = mp_vfs_lookup_path(path, &p_out); + if (vfs != MP_VFS_NONE && vfs != MP_VFS_ROOT) { *path_out = mp_obj_new_str_of_type(mp_obj_get_type(path_in), (const byte*)p_out, strlen(p_out)); } @@ -86,11 +82,11 @@ STATIC mp_vfs_mount_t *lookup_path(mp_obj_t path_in, mp_obj_t *path_out) { } STATIC mp_obj_t mp_vfs_proxy_call(mp_vfs_mount_t *vfs, qstr meth_name, size_t n_args, const mp_obj_t *args) { - if (vfs == VFS_NONE) { + if (vfs == MP_VFS_NONE) { // mount point not found mp_raise_OSError(MP_ENODEV); } - if (vfs == VFS_ROOT) { + if (vfs == MP_VFS_ROOT) { // can't do operation on root dir mp_raise_OSError(MP_EPERM); } @@ -104,8 +100,8 @@ STATIC mp_obj_t mp_vfs_proxy_call(mp_vfs_mount_t *vfs, qstr meth_name, size_t n_ mp_import_stat_t mp_vfs_import_stat(const char *path) { const char *path_out; - mp_vfs_mount_t *vfs = lookup_path_raw(path, &path_out); - if (vfs == VFS_NONE || vfs == VFS_ROOT) { + 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 @@ -145,7 +141,7 @@ mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args // check that the destination mount point is unused const char *path_out; - if (lookup_path_raw(mp_obj_str_get_str(pos_args[1]), &path_out) != VFS_NONE) { + if (mp_vfs_lookup_path(mp_obj_str_get_str(pos_args[1]), &path_out) != MP_VFS_NONE) { mp_raise_OSError(MP_EPERM); } @@ -182,7 +178,7 @@ mp_obj_t mp_vfs_umount(mp_obj_t mnt_in) { // if we unmounted the current device then set current to root if (MP_STATE_VM(vfs_cur) == vfs) { - MP_STATE_VM(vfs_cur) = VFS_ROOT; + MP_STATE_VM(vfs_cur) = MP_VFS_ROOT; } // call the underlying object to do any unmounting operation @@ -211,7 +207,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(mp_vfs_open_obj, 0, mp_vfs_open); mp_obj_t mp_vfs_chdir(mp_obj_t path_in) { mp_obj_t path_out; mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out); - if (vfs != VFS_ROOT) { + if (vfs != MP_VFS_ROOT) { mp_vfs_proxy_call(vfs, MP_QSTR_chdir, 1, &path_out); } MP_STATE_VM(vfs_cur) = vfs; @@ -220,7 +216,7 @@ mp_obj_t mp_vfs_chdir(mp_obj_t path_in) { MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_chdir_obj, mp_vfs_chdir); mp_obj_t mp_vfs_getcwd(void) { - if (MP_STATE_VM(vfs_cur) == VFS_ROOT) { + if (MP_STATE_VM(vfs_cur) == MP_VFS_ROOT) { return MP_OBJ_NEW_QSTR(MP_QSTR__slash_); } mp_obj_t cwd_o = mp_vfs_proxy_call(MP_STATE_VM(vfs_cur), MP_QSTR_getcwd, 0, NULL); @@ -246,7 +242,7 @@ mp_obj_t mp_vfs_listdir(size_t n_args, const mp_obj_t *args) { mp_obj_t path_out; mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out); - if (vfs == VFS_ROOT) { + if (vfs == MP_VFS_ROOT) { // list the root directory mp_obj_t dir_list = mp_obj_new_list(0, NULL); for (vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) { diff --git a/extmod/vfs.h b/extmod/vfs.h index ac3ca6fc4..92e53b305 100644 --- a/extmod/vfs.h +++ b/extmod/vfs.h @@ -30,6 +30,11 @@ #include "py/lexer.h" #include "py/obj.h" +// return values of mp_vfs_lookup_path +// ROOT is 0 so that the default current directory is the root directory +#define MP_VFS_NONE ((mp_vfs_mount_t*)1) +#define MP_VFS_ROOT ((mp_vfs_mount_t*)0) + typedef struct _mp_vfs_mount_t { const char *str; // mount point with leading / size_t len; @@ -37,6 +42,7 @@ typedef struct _mp_vfs_mount_t { struct _mp_vfs_mount_t *next; } mp_vfs_mount_t; +mp_vfs_mount_t *mp_vfs_lookup_path(const char *path, const char **path_out); mp_import_stat_t mp_vfs_import_stat(const char *path); mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); mp_obj_t mp_vfs_umount(mp_obj_t mnt_in); -- cgit v1.2.3 From 8beba7310f5aee77c179ec1852471f041937b54b Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 29 Jan 2017 15:16:51 +1100 Subject: extmod/vfs_fat: Remove MICROPY_READER_FATFS component. --- extmod/vfs_fat_reader.c | 101 ------------------------------------------------ py/lexer.c | 2 +- py/mpconfig.h | 5 --- py/py.mk | 1 - 4 files changed, 1 insertion(+), 108 deletions(-) delete mode 100644 extmod/vfs_fat_reader.c (limited to 'extmod') diff --git a/extmod/vfs_fat_reader.c b/extmod/vfs_fat_reader.c deleted file mode 100644 index b9abf3ad7..000000000 --- a/extmod/vfs_fat_reader.c +++ /dev/null @@ -1,101 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013-2016 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 -#include - -#include "py/mperrno.h" -#include "py/reader.h" - -#if MICROPY_READER_FATFS - -#if MICROPY_FATFS_OO -#include "lib/oofatfs/ff.h" -#else -#include "lib/fatfs/ff.h" -#endif -#include "extmod/fsusermount.h" -#include "extmod/vfs_fat.h" - -typedef struct _mp_reader_fatfs_t { - FIL fp; - uint16_t len; - uint16_t pos; - byte buf[20]; -} mp_reader_fatfs_t; - -STATIC mp_uint_t mp_reader_fatfs_readbyte(void *data) { - mp_reader_fatfs_t *reader = (mp_reader_fatfs_t*)data; - if (reader->pos >= reader->len) { - if (reader->len < sizeof(reader->buf)) { - return MP_READER_EOF; - } else { - UINT n; - f_read(&reader->fp, reader->buf, sizeof(reader->buf), &n); - if (n == 0) { - return MP_READER_EOF; - } - reader->len = n; - reader->pos = 0; - } - } - return reader->buf[reader->pos++]; -} - -STATIC void mp_reader_fatfs_close(void *data) { - mp_reader_fatfs_t *reader = (mp_reader_fatfs_t*)data; - f_close(&reader->fp); - m_del_obj(mp_reader_fatfs_t, reader); -} - -int mp_reader_new_file(mp_reader_t *reader, const char *filename) { - mp_reader_fatfs_t *rf = m_new_obj_maybe(mp_reader_fatfs_t); - if (rf == NULL) { - return MP_ENOMEM; - } - #if MICROPY_FATFS_OO - fs_user_mount_t *vfs = ff_get_vfs(&filename); - if (vfs == NULL) { - return MP_ENOENT; - } - FRESULT res = f_open(&vfs->fatfs, &rf->fp, filename, FA_READ); - #else - FRESULT res = f_open(&rf->fp, filename, FA_READ); - #endif - if (res != FR_OK) { - return fresult_to_errno_table[res]; - } - UINT n; - f_read(&rf->fp, rf->buf, sizeof(rf->buf), &n); - rf->len = n; - rf->pos = 0; - reader->data = rf; - reader->readbyte = mp_reader_fatfs_readbyte; - reader->close = mp_reader_fatfs_close; - return 0; // success -} - -#endif diff --git a/py/lexer.c b/py/lexer.c index e9b571ca4..33af21e9c 100644 --- a/py/lexer.c +++ b/py/lexer.c @@ -753,7 +753,7 @@ mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, mp_uint_t return mp_lexer_new(src_name, reader); } -#if MICROPY_READER_POSIX || MICROPY_READER_VFS || MICROPY_READER_FATFS +#if MICROPY_READER_POSIX || MICROPY_READER_VFS mp_lexer_t *mp_lexer_new_from_file(const char *filename) { mp_reader_t reader; diff --git a/py/mpconfig.h b/py/mpconfig.h index a924eda0c..d078e9301 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -403,11 +403,6 @@ #define MICROPY_READER_VFS (0) #endif -// Whether to use the FatFS reader for importing files -#ifndef MICROPY_READER_FATFS -#define MICROPY_READER_FATFS (0) -#endif - // Hook for the VM at the start of the opcode loop (can contain variable // definitions usable by the other hook functions) #ifndef MICROPY_VM_HOOK_INIT diff --git a/py/py.mk b/py/py.mk index e7e4fb9b7..0e5a9667d 100644 --- a/py/py.mk +++ b/py/py.mk @@ -240,7 +240,6 @@ PY_O_BASENAME = \ ../extmod/vfs_fat_ffconf.o \ ../extmod/vfs_fat_diskio.o \ ../extmod/vfs_fat_file.o \ - ../extmod/vfs_fat_reader.o \ ../extmod/vfs_fat_misc.o \ ../extmod/utime_mphal.o \ ../extmod/uos_dupterm.o \ -- cgit v1.2.3 From 3d6f9572084a8bcba762899ee4f8ea15ddf010ab Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 29 Jan 2017 15:17:55 +1100 Subject: extmod/vfs_fat: Remove MICROPY_FSUSERMOUNT_ADHOC config option. --- extmod/vfs_fat_file.c | 4 +--- extmod/vfs_fat_misc.c | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) (limited to 'extmod') diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index f5a188036..d3d437823 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -25,9 +25,7 @@ */ #include "py/mpconfig.h" -// *_ADHOC part is for cc3200 port which doesn't use general uPy -// infrastructure and instead duplicates code. TODO: Resolve. -#if MICROPY_VFS || MICROPY_FSUSERMOUNT || MICROPY_FSUSERMOUNT_ADHOC +#if MICROPY_VFS || MICROPY_FSUSERMOUNT #include #include diff --git a/extmod/vfs_fat_misc.c b/extmod/vfs_fat_misc.c index 2f06d9f63..7e7576398 100644 --- a/extmod/vfs_fat_misc.c +++ b/extmod/vfs_fat_misc.c @@ -25,9 +25,7 @@ */ #include "py/mpconfig.h" -// *_ADHOC part is for cc3200 port which doesn't use general uPy -// infrastructure and instead duplicates code. TODO: Resolve. -#if MICROPY_VFS_FAT || MICROPY_FSUSERMOUNT || MICROPY_FSUSERMOUNT_ADHOC +#if MICROPY_VFS_FAT || MICROPY_FSUSERMOUNT #include #include "py/nlr.h" -- cgit v1.2.3 From 1808b2e8d5c9fff8020628a7849a537ffa9790e3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 29 Jan 2017 15:21:46 +1100 Subject: extmod: Remove MICROPY_FSUSERMOUNT and related files. Replaced by MICROPY_VFS and the VFS sub-system. --- extmod/fsusermount.c | 244 ------------------------------------------------ extmod/vfs_fat.h | 2 - extmod/vfs_fat_diskio.c | 4 +- extmod/vfs_fat_ffconf.c | 118 ----------------------- extmod/vfs_fat_file.c | 4 +- extmod/vfs_fat_misc.c | 2 +- py/mpconfig.h | 5 - py/mpstate.h | 5 - py/py.mk | 2 - unix/modos.c | 12 --- unix/mpconfigport.h | 1 - 11 files changed, 5 insertions(+), 394 deletions(-) delete mode 100644 extmod/fsusermount.c delete mode 100644 extmod/vfs_fat_ffconf.c (limited to 'extmod') diff --git a/extmod/fsusermount.c b/extmod/fsusermount.c deleted file mode 100644 index 4ca9b80a6..000000000 --- a/extmod/fsusermount.c +++ /dev/null @@ -1,244 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2014 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 "py/mpconfig.h" -#if MICROPY_FSUSERMOUNT -#include -#include - -#include "py/nlr.h" -#include "py/runtime.h" -#include "py/mperrno.h" -#if MICROPY_FATFS_OO -#include "lib/oofatfs/ff.h" -#else -#include "lib/fatfs/ff.h" -#endif -#include "extmod/fsusermount.h" - -fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, bool mkfs) { - static const mp_arg_t allowed_args[] = { - { MP_QSTR_readonly, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, - { MP_QSTR_mkfs, 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(allowed_args)]; - mp_arg_parse_all(n_args - 2, pos_args + 2, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - - // get the mount point - mp_uint_t mnt_len; - const char *mnt_str = mp_obj_str_get_data(mount_point, &mnt_len); - - if (device == mp_const_none) { - // umount - FRESULT res = FR_NO_FILESYSTEM; - for (size_t i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) { - fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i]; - if (vfs != NULL && !memcmp(mnt_str, vfs->str, mnt_len + 1)) { - #if MICROPY_FATFS_OO - res = f_umount(&vfs->fatfs); - #else - res = f_mount(NULL, vfs->str, 0); - #endif - if (vfs->flags & FSUSER_FREE_OBJ) { - m_del_obj(fs_user_mount_t, vfs); - } - MP_STATE_PORT(fs_user_mount)[i] = NULL; - break; - } - } - if (res != FR_OK) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't umount")); - } - return NULL; - } else { - // mount - size_t i = 0; - for (; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) { - if (MP_STATE_PORT(fs_user_mount)[i] == NULL) { - break; - } - } - if (i == MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount))) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "too many devices mounted")); - } - - // create new object - fs_user_mount_t *vfs = m_new_obj(fs_user_mount_t); - vfs->str = mnt_str; - vfs->len = mnt_len; - vfs->flags = FSUSER_FREE_OBJ; - #if MICROPY_FATFS_OO - vfs->fatfs.drv = vfs; - #endif - - // load block protocol methods - mp_load_method(device, MP_QSTR_readblocks, vfs->readblocks); - mp_load_method_maybe(device, MP_QSTR_writeblocks, vfs->writeblocks); - mp_load_method_maybe(device, MP_QSTR_ioctl, vfs->u.ioctl); - if (vfs->u.ioctl[0] != MP_OBJ_NULL) { - // device supports new block protocol, so indicate it - vfs->flags |= FSUSER_HAVE_IOCTL; - } else { - // no ioctl method, so assume the device uses the old block protocol - mp_load_method_maybe(device, MP_QSTR_sync, vfs->u.old.sync); - mp_load_method(device, MP_QSTR_count, vfs->u.old.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 (args[0].u_bool) { - vfs->writeblocks[0] = MP_OBJ_NULL; - } - - // Register the vfs object so that it can be found by the FatFS driver using - // ff_get_ldnumber. We don't register it any earlier than this point in case there - // is an exception, in which case there would remain a partially mounted device. - MP_STATE_PORT(fs_user_mount)[i] = vfs; - - // mount the block device (if mkfs, only pre-mount) - FRESULT res; - #if MICROPY_FATFS_OO - if (mkfs) { - res = FR_OK; - } else { - res = f_mount(&vfs->fatfs); - } - #else - res = f_mount(&vfs->fatfs, vfs->str, !mkfs); - #endif - - // check the result - if (res == FR_OK) { - if (mkfs) { - goto mkfs; - } - } else if (res == FR_NO_FILESYSTEM && args[1].u_bool) { -mkfs:; - #if MICROPY_FATFS_OO - uint8_t working_buf[_MAX_SS]; - res = f_mkfs(&vfs->fatfs, FM_FAT | FM_SFD, 0, working_buf, sizeof(working_buf)); - #else - res = f_mkfs(vfs->str, 1, 0); - #endif - if (res != FR_OK) { -mkfs_error: - MP_STATE_PORT(fs_user_mount)[i] = NULL; - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't mkfs")); - } - if (mkfs) { - // If requested to only mkfs, unmount pre-mounted device - #if MICROPY_FATFS_OO - res = FR_OK; - #else - res = f_mount(NULL, vfs->str, 0); - #endif - if (res != FR_OK) { - goto mkfs_error; - } - MP_STATE_PORT(fs_user_mount)[i] = NULL; - return NULL; - } - } else { - MP_STATE_PORT(fs_user_mount)[i] = NULL; - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't mount")); - } - - /* - if (vfs->writeblocks[0] == MP_OBJ_NULL) { - printf("mounted read-only"); - } else { - printf("mounted read-write"); - } - DWORD nclst; - FATFS *fatfs; - f_getfree(vfs->str, &nclst, &fatfs); - printf(" on %s with %u bytes free\n", vfs->str, (uint)(nclst * fatfs->csize * 512)); - */ - return vfs; - } -} - -STATIC mp_obj_t fatfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - fatfs_mount_mkfs(n_args, pos_args, kw_args, false); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_KW(fsuser_mount_obj, 2, fatfs_mount); - -mp_obj_t fatfs_umount(mp_obj_t bdev_or_path_in) { - size_t i = 0; - if (MP_OBJ_IS_STR(bdev_or_path_in)) { - mp_uint_t mnt_len; - const char *mnt_str = mp_obj_str_get_data(bdev_or_path_in, &mnt_len); - for (; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) { - fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i]; - if (vfs != NULL && !memcmp(mnt_str, vfs->str, mnt_len + 1)) { - break; - } - } - } else { - for (; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) { - fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i]; - if (vfs != NULL && bdev_or_path_in == vfs->readblocks[1]) { - break; - } - } - } - - if (i == MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount))) { - mp_raise_OSError(MP_EINVAL); - } - - fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i]; - FRESULT res; - #if MICROPY_FATFS_OO - res = f_umount(&vfs->fatfs); - #else - res = f_mount(NULL, vfs->str, 0); - #endif - if (vfs->flags & FSUSER_FREE_OBJ) { - m_del_obj(fs_user_mount_t, vfs); - } - MP_STATE_PORT(fs_user_mount)[i] = NULL; - if (res != FR_OK) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't umount")); - } - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(fsuser_umount_obj, fatfs_umount); - -STATIC mp_obj_t fatfs_mkfs(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - fatfs_mount_mkfs(n_args, pos_args, kw_args, true); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_KW(fsuser_mkfs_obj, 2, fatfs_mkfs); - -#endif // MICROPY_FSUSERMOUNT diff --git a/extmod/vfs_fat.h b/extmod/vfs_fat.h index 1ea8a9637..bc5be0c67 100644 --- a/extmod/vfs_fat.h +++ b/extmod/vfs_fat.h @@ -31,8 +31,6 @@ struct _fs_user_mount_t; extern const byte fresult_to_errno_table[20]; extern const mp_obj_type_t mp_fat_vfs_type; -struct _fs_user_mount_t *ff_get_vfs(const char **path); - mp_import_stat_t fat_vfs_import_stat(struct _fs_user_mount_t *vfs, const char *path); mp_obj_t fatfs_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs); mp_obj_t fatfs_builtin_open_self(mp_obj_t self_in, mp_obj_t path, mp_obj_t mode); diff --git a/extmod/vfs_fat_diskio.c b/extmod/vfs_fat_diskio.c index 3bbd37435..c8a6e1533 100644 --- a/extmod/vfs_fat_diskio.c +++ b/extmod/vfs_fat_diskio.c @@ -28,7 +28,7 @@ */ #include "py/mpconfig.h" -#if MICROPY_VFS || MICROPY_FSUSERMOUNT +#if MICROPY_VFS #include #include @@ -305,4 +305,4 @@ DRESULT disk_ioctl ( } #endif -#endif // MICROPY_VFS || MICROPY_FSUSERMOUNT +#endif // MICROPY_VFS diff --git a/extmod/vfs_fat_ffconf.c b/extmod/vfs_fat_ffconf.c deleted file mode 100644 index 89081380e..000000000 --- a/extmod/vfs_fat_ffconf.c +++ /dev/null @@ -1,118 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 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 "py/mpconfig.h" -#if MICROPY_FSUSERMOUNT - -#include - -#include "py/mpstate.h" -#if MICROPY_FATFS_OO -#include "lib/oofatfs/ff.h" -#else -#include "lib/fatfs/ff.h" -#endif -#include "extmod/fsusermount.h" -#include "extmod/vfs_fat.h" - -STATIC bool check_path(const TCHAR **path, const char *mount_point_str, mp_uint_t mount_point_len) { - if (strncmp(*path, mount_point_str, mount_point_len) == 0) { - if ((*path)[mount_point_len] == '/') { - *path += mount_point_len; - return true; - } else if ((*path)[mount_point_len] == '\0') { - *path = "/"; - return true; - } - } - return false; -} - -#if MICROPY_FATFS_OO - -STATIC fs_user_mount_t *vfs_cur_obj = NULL; - -// "path" is the path to lookup; will advance this pointer beyond the volume name. -// Returns a pointer to the VFS object, NULL means path not found. -fs_user_mount_t *ff_get_vfs(const char **path) { - if (!(*path)) { - return NULL; - } - - if (**path != '/') { - #if _FS_RPATH - return vfs_cur_obj; - #else - return NULL; - #endif - } - - for (size_t i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) { - fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i]; - if (vfs != NULL && check_path(path, vfs->str, vfs->len)) { - return vfs; - } - } - - return NULL; -} - -#else - -// "path" is the path to lookup; will advance this pointer beyond the volume name. -// Returns logical drive number (-1 means invalid path). -int ff_get_ldnumber (const TCHAR **path) { - if (!(*path)) { - return -1; - } - - if (**path != '/') { - #if _FS_RPATH - return ff_CurrVol; - #else - return -1; - #endif - } - - for (size_t i = 0; i < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount)); ++i) { - fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[i]; - if (vfs != NULL && check_path(path, vfs->str, vfs->len)) { - return i; - } - } - - return -1; -} - -void ff_get_volname(BYTE vol, TCHAR **dest) { - fs_user_mount_t *vfs = MP_STATE_PORT(fs_user_mount)[vol]; - memcpy(*dest, vfs->str, vfs->len); - *dest += vfs->len; -} - -#endif - -#endif // MICROPY_FSUSERMOUNT diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index d3d437823..dccd12035 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -25,7 +25,7 @@ */ #include "py/mpconfig.h" -#if MICROPY_VFS || MICROPY_FSUSERMOUNT +#if MICROPY_VFS #include #include @@ -322,4 +322,4 @@ mp_obj_t fatfs_builtin_open_self(mp_obj_t self_in, mp_obj_t path, mp_obj_t mode) return file_open(self, &mp_type_textio, arg_vals); } -#endif // MICROPY_VFS || MICROPY_FSUSERMOUNT +#endif // MICROPY_VFS diff --git a/extmod/vfs_fat_misc.c b/extmod/vfs_fat_misc.c index 7e7576398..82ef91a1f 100644 --- a/extmod/vfs_fat_misc.c +++ b/extmod/vfs_fat_misc.c @@ -25,7 +25,7 @@ */ #include "py/mpconfig.h" -#if MICROPY_VFS_FAT || MICROPY_FSUSERMOUNT +#if MICROPY_VFS_FAT #include #include "py/nlr.h" diff --git a/py/mpconfig.h b/py/mpconfig.h index d078e9301..993ad1db8 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -616,11 +616,6 @@ typedef double mp_float_t; #define MICROPY_USE_INTERNAL_PRINTF (1) #endif -// Support for user-space VFS mount (selected ports) -#ifndef MICROPY_FSUSERMOUNT -#define MICROPY_FSUSERMOUNT (0) -#endif - // Support for generic VFS sub-system #ifndef MICROPY_VFS #define MICROPY_VFS (0) diff --git a/py/mpstate.h b/py/mpstate.h index daf085a06..54392a994 100644 --- a/py/mpstate.h +++ b/py/mpstate.h @@ -160,11 +160,6 @@ typedef struct _mp_state_vm_t { mp_obj_t lwip_slip_stream; #endif - #if MICROPY_FSUSERMOUNT - // for user-mountable block device (max fixed at compile time) - struct _fs_user_mount_t *fs_user_mount[MICROPY_FATFS_VOLUMES]; - #endif - #if MICROPY_VFS struct _mp_vfs_mount_t *vfs_cur; struct _mp_vfs_mount_t *vfs_mount_table; diff --git a/py/py.mk b/py/py.mk index 0e5a9667d..01a802674 100644 --- a/py/py.mk +++ b/py/py.mk @@ -233,11 +233,9 @@ PY_O_BASENAME = \ ../extmod/modwebsocket.o \ ../extmod/modwebrepl.o \ ../extmod/modframebuf.o \ - ../extmod/fsusermount.o \ ../extmod/vfs.o \ ../extmod/vfs_reader.o \ ../extmod/vfs_fat.o \ - ../extmod/vfs_fat_ffconf.o \ ../extmod/vfs_fat_diskio.o \ ../extmod/vfs_fat_file.o \ ../extmod/vfs_fat_misc.o \ diff --git a/unix/modos.c b/unix/modos.c index c35b246dd..1584b0d20 100644 --- a/unix/modos.c +++ b/unix/modos.c @@ -41,13 +41,6 @@ #include "extmod/misc.h" #include "extmod/vfs_fat.h" -// Can't include this, as FATFS structure definition is required, -// and FatFs header defining it conflicts with POSIX. -//#include "extmod/fsusermount.h" -MP_DECLARE_CONST_FUN_OBJ_KW(fsuser_mount_obj); -MP_DECLARE_CONST_FUN_OBJ_1(fsuser_umount_obj); -MP_DECLARE_CONST_FUN_OBJ_KW(fsuser_mkfs_obj); - #ifdef __ANDROID__ #define USE_STATFS 1 #endif @@ -233,11 +226,6 @@ STATIC const mp_rom_map_elem_t mp_module_os_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_getenv), MP_ROM_PTR(&mod_os_getenv_obj) }, { MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&mod_os_mkdir_obj) }, { MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&mod_os_ilistdir_obj) }, - #if MICROPY_FSUSERMOUNT - { MP_ROM_QSTR(MP_QSTR_vfs_mount), MP_ROM_PTR(&fsuser_mount_obj) }, - { MP_ROM_QSTR(MP_QSTR_vfs_umount), MP_ROM_PTR(&fsuser_umount_obj) }, - { MP_ROM_QSTR(MP_QSTR_vfs_mkfs), MP_ROM_PTR(&fsuser_mkfs_obj) }, - #endif #if MICROPY_VFS_FAT { MP_ROM_QSTR(MP_QSTR_VfsFat), MP_ROM_PTR(&mp_fat_vfs_type) }, #endif diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h index f61a381a3..3aff893d6 100644 --- a/unix/mpconfigport.h +++ b/unix/mpconfigport.h @@ -139,7 +139,6 @@ #define MICROPY_FATFS_VOLUMES (3) #define MICROPY_FATFS_MAX_SS (4096) #define MICROPY_FATFS_LFN_CODE_PAGE (437) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */ -#define MICROPY_FSUSERMOUNT (0) #define MICROPY_VFS_FAT (0) // Define to MICROPY_ERROR_REPORTING_DETAILED to get function, etc. -- cgit v1.2.3 From 0bd61d23b9b21819da9d10290dfccd4ae4a69e1a Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 29 Jan 2017 15:26:24 +1100 Subject: extmod/vfs_fat: Remove MICROPY_FATFS_OO config option. Everyone should now be using the new ooFatFs library. The old one is no longer supported and will be removed. --- cc3200/mpconfigport.h | 1 - esp8266/mpconfigport.h | 1 - extmod/vfs_fat.c | 4 ---- extmod/vfs_fat_diskio.c | 32 ++------------------------------ extmod/vfs_fat_file.c | 13 ------------- extmod/vfs_fat_misc.c | 29 ----------------------------- stmhal/mpconfigport.h | 1 - unix/mpconfigport.h | 1 - 8 files changed, 2 insertions(+), 80 deletions(-) (limited to 'extmod') diff --git a/cc3200/mpconfigport.h b/cc3200/mpconfigport.h index 939d9f62b..49ea64403 100644 --- a/cc3200/mpconfigport.h +++ b/cc3200/mpconfigport.h @@ -64,7 +64,6 @@ #define MICROPY_QSTR_BYTES_IN_HASH (1) // fatfs configuration used in ffconf.h -#define MICROPY_FATFS_OO (1) #define MICROPY_FATFS_ENABLE_LFN (2) #define MICROPY_FATFS_MAX_LFN (MICROPY_ALLOC_PATH_MAX) #define MICROPY_FATFS_LFN_CODE_PAGE (437) // 1=SFN/ANSI 437=LFN/U.S.(OEM) diff --git a/esp8266/mpconfigport.h b/esp8266/mpconfigport.h index 89d9dae76..04b9792eb 100644 --- a/esp8266/mpconfigport.h +++ b/esp8266/mpconfigport.h @@ -95,7 +95,6 @@ #define MICROPY_QSTR_EXTRA_POOL mp_qstr_frozen_const_pool #define MICROPY_VFS (1) -#define MICROPY_FATFS_OO (1) #define MICROPY_FATFS_ENABLE_LFN (1) #define MICROPY_FATFS_RPATH (2) #define MICROPY_FATFS_MAX_SS (4096) diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index a6f2ae806..ecbbdb59a 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -32,10 +32,6 @@ #error "with MICROPY_VFS_FAT enabled, must also enable MICROPY_VFS" #endif -#if !MICROPY_FATFS_OO -#error "with MICROPY_VFS_FAT enabled, must also enable MICROPY_FATFS_OO" -#endif - #include #include "py/nlr.h" #include "py/runtime.h" diff --git a/extmod/vfs_fat_diskio.c b/extmod/vfs_fat_diskio.c index c8a6e1533..e12c4597e 100644 --- a/extmod/vfs_fat_diskio.c +++ b/extmod/vfs_fat_diskio.c @@ -36,13 +36,8 @@ #include "py/mphal.h" #include "py/runtime.h" -#if MICROPY_FATFS_OO #include "lib/oofatfs/ff.h" #include "lib/oofatfs/diskio.h" -#else -#include "lib/fatfs/ff.h" /* FatFs lower layer API */ -#include "lib/fatfs/diskio.h" /* FatFs lower layer API */ -#endif #include "extmod/fsusermount.h" #if _MAX_SS == _MIN_SS @@ -51,29 +46,16 @@ #define SECSIZE(fs) ((fs)->ssize) #endif -#if MICROPY_FATFS_OO typedef void *bdev_t; STATIC fs_user_mount_t *disk_get_device(void *bdev) { return (fs_user_mount_t*)bdev; } -#else -typedef BYTE bdev_t; -STATIC fs_user_mount_t *disk_get_device(uint id) { - if (id < MP_ARRAY_SIZE(MP_STATE_PORT(fs_user_mount))) { - return MP_STATE_PORT(fs_user_mount)[id]; - } else { - return NULL; - } -} -#endif /*-----------------------------------------------------------------------*/ /* Initialize a Drive */ /*-----------------------------------------------------------------------*/ -#if MICROPY_FATFS_OO STATIC -#endif DSTATUS disk_initialize ( bdev_t pdrv /* Physical drive nmuber (0..) */ ) @@ -105,9 +87,7 @@ DSTATUS disk_initialize ( /* Get Disk Status */ /*-----------------------------------------------------------------------*/ -#if MICROPY_FATFS_OO STATIC -#endif DSTATUS disk_status ( bdev_t pdrv /* Physical drive nmuber (0..) */ ) @@ -159,7 +139,6 @@ DRESULT disk_read ( /* Write Sector(s) */ /*-----------------------------------------------------------------------*/ -#if MICROPY_FATFS_OO || _USE_WRITE DRESULT disk_write ( bdev_t pdrv, /* Physical drive nmuber (0..) */ const BYTE *buff, /* Data to be written */ @@ -191,14 +170,12 @@ DRESULT disk_write ( return RES_OK; } -#endif /*-----------------------------------------------------------------------*/ /* Miscellaneous Functions */ /*-----------------------------------------------------------------------*/ -#if MICROPY_FATFS_OO || _USE_IOCTL DRESULT disk_ioctl ( bdev_t pdrv, /* Physical drive nmuber (0..) */ BYTE cmd, /* Control code */ @@ -237,7 +214,7 @@ DRESULT disk_ioctl ( } else { *((WORD*)buff) = mp_obj_get_int(ret); } - #if MICROPY_FATFS_OO && _MAX_SS != _MIN_SS + #if _MAX_SS != _MIN_SS // need to store ssize because we use it in disk_read/disk_write vfs->fatfs.ssize = *((WORD*)buff); #endif @@ -248,7 +225,6 @@ DRESULT disk_ioctl ( *((DWORD*)buff) = 1; // erase block size in units of sector size return RES_OK; - #if MICROPY_FATFS_OO case IOCTL_INIT: *((DSTATUS*)buff) = disk_initialize(pdrv); return RES_OK; @@ -256,7 +232,6 @@ DRESULT disk_ioctl ( case IOCTL_STATUS: *((DSTATUS*)buff) = disk_status(pdrv); return RES_OK; - #endif default: return RES_PARERR; @@ -278,7 +253,7 @@ DRESULT disk_ioctl ( case GET_SECTOR_SIZE: *((WORD*)buff) = 512; // old protocol had fixed sector size - #if MICROPY_FATFS_OO && _MAX_SS != _MIN_SS + #if _MAX_SS != _MIN_SS // need to store ssize because we use it in disk_read/disk_write vfs->fatfs.ssize = 512; #endif @@ -288,7 +263,6 @@ DRESULT disk_ioctl ( *((DWORD*)buff) = 1; // erase block size in units of sector size return RES_OK; - #if MICROPY_FATFS_OO case IOCTL_INIT: *((DSTATUS*)buff) = disk_initialize(pdrv); return RES_OK; @@ -296,13 +270,11 @@ DRESULT disk_ioctl ( case IOCTL_STATUS: *((DSTATUS*)buff) = disk_status(pdrv); return RES_OK; - #endif default: return RES_PARERR; } } } -#endif #endif // MICROPY_VFS diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index dccd12035..bb5903575 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -34,11 +34,7 @@ #include "py/runtime.h" #include "py/stream.h" #include "py/mperrno.h" -#if MICROPY_FATFS_OO #include "lib/oofatfs/ff.h" -#else -#include "lib/fatfs/ff.h" -#endif #include "extmod/fsusermount.h" #include "extmod/vfs_fat.h" @@ -115,11 +111,7 @@ STATIC mp_uint_t file_obj_write(mp_obj_t self_in, const void *buf, mp_uint_t siz STATIC mp_obj_t file_obj_close(mp_obj_t self_in) { pyb_file_obj_t *self = MP_OBJ_TO_PTR(self_in); // if fs==NULL then the file is closed and in that case this method is a no-op - #if MICROPY_FATFS_OO if (self->fp.obj.fs != NULL) { - #else - if (self->fp.fs != NULL) { - #endif FRESULT res = f_close(&self->fp); if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); @@ -221,13 +213,8 @@ STATIC mp_obj_t file_open(fs_user_mount_t *vfs, const mp_obj_type_t *type, mp_ar o->base.type = type; const char *fname = mp_obj_str_get_str(args[0].u_obj); - #if MICROPY_FATFS_OO assert(vfs != NULL); FRESULT res = f_open(&vfs->fatfs, &o->fp, fname, mode); - #else - (void)vfs; - FRESULT res = f_open(&o->fp, fname, mode); - #endif if (res != FR_OK) { m_del_obj(pyb_file_obj_t, o); mp_raise_OSError(fresult_to_errno_table[res]); diff --git a/extmod/vfs_fat_misc.c b/extmod/vfs_fat_misc.c index 82ef91a1f..ba513ef92 100644 --- a/extmod/vfs_fat_misc.c +++ b/extmod/vfs_fat_misc.c @@ -30,19 +30,11 @@ #include #include "py/nlr.h" #include "py/runtime.h" -#if MICROPY_FATFS_OO #include "lib/oofatfs/ff.h" -#else -#include "lib/fatfs/ff.h" -#endif #include "extmod/vfs_fat.h" #include "extmod/fsusermount.h" #include "py/lexer.h" -#if !MICROPY_FATFS_OO && _USE_LFN -STATIC char lfn[_MAX_LFN + 1]; /* Buffer to store the LFN */ -#endif - // TODO: actually, the core function should be ilistdir() mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type) { @@ -53,16 +45,8 @@ mp_obj_t fat_vfs_listdir2(fs_user_mount_t *vfs, const char *path, bool is_str_ty FRESULT res; FILINFO fno; FF_DIR dir; -#if !MICROPY_FATFS_OO && _USE_LFN - fno.lfname = lfn; - fno.lfsize = sizeof lfn; -#endif - #if MICROPY_FATFS_OO res = f_opendir(&vfs->fatfs, &dir, path); - #else - res = f_opendir(&dir, path); /* Open the directory */ - #endif if (res != FR_OK) { mp_raise_OSError(fresult_to_errno_table[res]); } @@ -75,11 +59,7 @@ mp_obj_t fat_vfs_listdir2(fs_user_mount_t *vfs, const char *path, bool is_str_ty 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 !MICROPY_FATFS_OO && _USE_LFN - char *fn = *fno.lfname ? fno.lfname : fno.fname; -#else char *fn = fno.fname; -#endif /* if (fno.fattrib & AM_DIR) { @@ -108,17 +88,8 @@ mp_obj_t fat_vfs_listdir2(fs_user_mount_t *vfs, const char *path, bool is_str_ty mp_import_stat_t fat_vfs_import_stat(fs_user_mount_t *vfs, const char *path) { FILINFO fno; -#if !MICROPY_FATFS_OO && _USE_LFN - fno.lfname = NULL; - fno.lfsize = 0; -#endif - #if MICROPY_FATFS_OO assert(vfs != NULL); FRESULT res = f_stat(&vfs->fatfs, path, &fno); - #else - (void)vfs; - FRESULT res = f_stat(path, &fno); - #endif if (res == FR_OK) { if ((fno.fattrib & AM_DIR) != 0) { return MP_IMPORT_STAT_DIR; diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index 53369a182..873215458 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -135,7 +135,6 @@ #endif // fatfs configuration used in ffconf.h -#define MICROPY_FATFS_OO (1) #define MICROPY_FATFS_ENABLE_LFN (1) #define MICROPY_FATFS_LFN_CODE_PAGE (437) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */ #define MICROPY_FATFS_USE_LABEL (1) diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h index 9e418fe0a..ba2b5ce98 100644 --- a/unix/mpconfigport.h +++ b/unix/mpconfigport.h @@ -131,7 +131,6 @@ #define MICROPY_MACHINE_MEM_GET_READ_ADDR mod_machine_mem_get_addr #define MICROPY_MACHINE_MEM_GET_WRITE_ADDR mod_machine_mem_get_addr -#define MICROPY_FATFS_OO (1) #define MICROPY_FATFS_ENABLE_LFN (1) #define MICROPY_FATFS_RPATH (2) #define MICROPY_FATFS_MAX_SS (4096) -- cgit v1.2.3 From b697c890096805d9ccaf7553dbff5b82f5332609 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 29 Jan 2017 19:20:27 +1100 Subject: extmod: Merge old fsusermount.h header into vfs.h and vfs_fat.h. vfs.h is for generic VFS declarations, and vfs_fat.h is for VfsFat specific things. --- cc3200/ftp/ftp.c | 2 +- cc3200/mods/pybflash.c | 1 - cc3200/mods/pybsd.c | 2 +- cc3200/mptask.c | 2 +- extmod/fsusermount.h | 62 ------------------------------------------------- extmod/vfs.c | 5 +++- extmod/vfs.h | 7 ++++++ extmod/vfs_fat.c | 1 - extmod/vfs_fat.h | 26 ++++++++++++++++++++- extmod/vfs_fat_diskio.c | 2 +- extmod/vfs_fat_file.c | 1 - extmod/vfs_fat_misc.c | 1 - stmhal/main.c | 2 +- stmhal/modmachine.c | 2 +- stmhal/sdcard.c | 1 - stmhal/storage.c | 1 - 16 files changed, 42 insertions(+), 76 deletions(-) delete mode 100644 extmod/fsusermount.h (limited to 'extmod') diff --git a/cc3200/ftp/ftp.c b/cc3200/ftp/ftp.c index c8a52149c..679c32561 100644 --- a/cc3200/ftp/ftp.c +++ b/cc3200/ftp/ftp.c @@ -32,7 +32,7 @@ #include "py/obj.h" #include "lib/oofatfs/ff.h" #include "extmod/vfs.h" -#include "extmod/fsusermount.h" +#include "extmod/vfs_fat.h" #include "inc/hw_types.h" #include "inc/hw_ints.h" #include "inc/hw_memmap.h" diff --git a/cc3200/mods/pybflash.c b/cc3200/mods/pybflash.c index 0779f4a05..f5af79dbf 100644 --- a/cc3200/mods/pybflash.c +++ b/cc3200/mods/pybflash.c @@ -31,7 +31,6 @@ #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" diff --git a/cc3200/mods/pybsd.c b/cc3200/mods/pybsd.c index bac5a270c..937b8599d 100644 --- a/cc3200/mods/pybsd.c +++ b/cc3200/mods/pybsd.c @@ -29,7 +29,7 @@ #include "py/runtime.h" #include "lib/oofatfs/ff.h" #include "lib/oofatfs/diskio.h" -#include "extmod/fsusermount.h" +#include "extmod/vfs_fat.h" #include "inc/hw_types.h" #include "inc/hw_gpio.h" #include "inc/hw_ints.h" diff --git a/cc3200/mptask.c b/cc3200/mptask.c index 476561c6d..c7c1832ed 100644 --- a/cc3200/mptask.c +++ b/cc3200/mptask.c @@ -36,7 +36,7 @@ #include "lib/oofatfs/ff.h" #include "lib/oofatfs/diskio.h" #include "extmod/vfs.h" -#include "extmod/fsusermount.h" +#include "extmod/vfs_fat.h" #include "inc/hw_memmap.h" #include "inc/hw_types.h" #include "inc/hw_ints.h" diff --git a/extmod/fsusermount.h b/extmod/fsusermount.h deleted file mode 100644 index af6867d23..000000000 --- a/extmod/fsusermount.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * This file is part of the Micro Python project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2014 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. - */ - -// these are the values for fs_user_mount_t.flags -#define FSUSER_NATIVE (0x0001) // readblocks[2]/writeblocks[2] contain native func -#define FSUSER_FREE_OBJ (0x0002) // fs_user_mount_t obj should be freed on umount -#define FSUSER_HAVE_IOCTL (0x0004) // new protocol with ioctl - -// constants for block protocol ioctl -#define BP_IOCTL_INIT (1) -#define BP_IOCTL_DEINIT (2) -#define BP_IOCTL_SYNC (3) -#define BP_IOCTL_SEC_COUNT (4) -#define BP_IOCTL_SEC_SIZE (5) - -typedef struct _fs_user_mount_t { - mp_obj_base_t base; - const char *str; - uint16_t len; // length of str - uint16_t flags; - mp_obj_t readblocks[4]; - mp_obj_t writeblocks[4]; - // new protocol uses just ioctl, old uses sync (optional) and count - union { - mp_obj_t ioctl[4]; - struct { - mp_obj_t sync[2]; - mp_obj_t count[2]; - } old; - } u; - FATFS fatfs; -} fs_user_mount_t; - -fs_user_mount_t *fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, bool mkfs); -mp_obj_t fatfs_umount(mp_obj_t bdev_or_path_in); - -MP_DECLARE_CONST_FUN_OBJ_KW(fsuser_mount_obj); -MP_DECLARE_CONST_FUN_OBJ_1(fsuser_umount_obj); -MP_DECLARE_CONST_FUN_OBJ_KW(fsuser_mkfs_obj); diff --git a/extmod/vfs.c b/extmod/vfs.c index 97c9077a2..1eb26acf1 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -31,10 +31,13 @@ #include "py/objstr.h" #include "py/mperrno.h" #include "extmod/vfs.h" -#include "extmod/vfs_fat.h" #if MICROPY_VFS +#if MICROPY_VFS_FAT +#include "extmod/vfs_fat.h" +#endif + // path is the path to lookup and *path_out holds the path within the VFS // object (starts with / if an absolute path). // Returns MP_VFS_ROOT for root dir (and then path_out is undefined) and diff --git a/extmod/vfs.h b/extmod/vfs.h index 92e53b305..4a1c225a0 100644 --- a/extmod/vfs.h +++ b/extmod/vfs.h @@ -35,6 +35,13 @@ #define MP_VFS_NONE ((mp_vfs_mount_t*)1) #define MP_VFS_ROOT ((mp_vfs_mount_t*)0) +// constants for block protocol ioctl +#define BP_IOCTL_INIT (1) +#define BP_IOCTL_DEINIT (2) +#define BP_IOCTL_SYNC (3) +#define BP_IOCTL_SEC_COUNT (4) +#define BP_IOCTL_SEC_SIZE (5) + typedef struct _mp_vfs_mount_t { const char *str; // mount point with leading / size_t len; diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index ecbbdb59a..b32bf7ad9 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -38,7 +38,6 @@ #include "py/mperrno.h" #include "lib/oofatfs/ff.h" #include "extmod/vfs_fat.h" -#include "extmod/fsusermount.h" #include "timeutils.h" #if _MAX_SS == _MIN_SS diff --git a/extmod/vfs_fat.h b/extmod/vfs_fat.h index bc5be0c67..fefae776c 100644 --- a/extmod/vfs_fat.h +++ b/extmod/vfs_fat.h @@ -25,8 +25,32 @@ */ #include "py/lexer.h" +#include "py/obj.h" +#include "lib/oofatfs/ff.h" +#include "extmod/vfs.h" -struct _fs_user_mount_t; +// these are the values for fs_user_mount_t.flags +#define FSUSER_NATIVE (0x0001) // readblocks[2]/writeblocks[2] contain native func +#define FSUSER_FREE_OBJ (0x0002) // fs_user_mount_t obj should be freed on umount +#define FSUSER_HAVE_IOCTL (0x0004) // new protocol with ioctl + +typedef struct _fs_user_mount_t { + mp_obj_base_t base; + const char *str; + uint16_t len; // length of str + uint16_t flags; + mp_obj_t readblocks[4]; + mp_obj_t writeblocks[4]; + // new protocol uses just ioctl, old uses sync (optional) and count + union { + mp_obj_t ioctl[4]; + struct { + mp_obj_t sync[2]; + mp_obj_t count[2]; + } old; + } u; + FATFS fatfs; +} fs_user_mount_t; extern const byte fresult_to_errno_table[20]; extern const mp_obj_type_t mp_fat_vfs_type; diff --git a/extmod/vfs_fat_diskio.c b/extmod/vfs_fat_diskio.c index e12c4597e..7efcc22f2 100644 --- a/extmod/vfs_fat_diskio.c +++ b/extmod/vfs_fat_diskio.c @@ -38,7 +38,7 @@ #include "py/runtime.h" #include "lib/oofatfs/ff.h" #include "lib/oofatfs/diskio.h" -#include "extmod/fsusermount.h" +#include "extmod/vfs_fat.h" #if _MAX_SS == _MIN_SS #define SECSIZE(fs) (_MIN_SS) diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index bb5903575..0f2a7a1aa 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -35,7 +35,6 @@ #include "py/stream.h" #include "py/mperrno.h" #include "lib/oofatfs/ff.h" -#include "extmod/fsusermount.h" #include "extmod/vfs_fat.h" #if MICROPY_VFS_FAT diff --git a/extmod/vfs_fat_misc.c b/extmod/vfs_fat_misc.c index ba513ef92..97d2675cd 100644 --- a/extmod/vfs_fat_misc.c +++ b/extmod/vfs_fat_misc.c @@ -32,7 +32,6 @@ #include "py/runtime.h" #include "lib/oofatfs/ff.h" #include "extmod/vfs_fat.h" -#include "extmod/fsusermount.h" #include "py/lexer.h" // TODO: actually, the core function should be ilistdir() diff --git a/stmhal/main.c b/stmhal/main.c index 9eab50061..3a0bd7a6b 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -39,7 +39,7 @@ #include "lib/utils/pyexec.h" #include "lib/oofatfs/ff.h" #include "extmod/vfs.h" -#include "extmod/fsusermount.h" +#include "extmod/vfs_fat.h" #include "systick.h" #include "pendsv.h" diff --git a/stmhal/modmachine.c b/stmhal/modmachine.c index aec8e29c5..b10bca819 100644 --- a/stmhal/modmachine.c +++ b/stmhal/modmachine.c @@ -37,7 +37,7 @@ #include "lib/utils/pyexec.h" #include "lib/oofatfs/ff.h" #include "extmod/vfs.h" -#include "extmod/fsusermount.h" +#include "extmod/vfs_fat.h" #include "gccollect.h" #include "irq.h" #include "rng.h" diff --git a/stmhal/sdcard.c b/stmhal/sdcard.c index 52ede492b..b1d67f62a 100644 --- a/stmhal/sdcard.c +++ b/stmhal/sdcard.c @@ -30,7 +30,6 @@ #include "py/runtime.h" #include "lib/oofatfs/ff.h" #include "extmod/vfs_fat.h" -#include "extmod/fsusermount.h" #include "mphalport.h" #include "sdcard.h" diff --git a/stmhal/storage.c b/stmhal/storage.c index 6130d6fb8..c1daad4c2 100644 --- a/stmhal/storage.c +++ b/stmhal/storage.c @@ -31,7 +31,6 @@ #include "py/runtime.h" #include "lib/oofatfs/ff.h" #include "extmod/vfs_fat.h" -#include "extmod/fsusermount.h" #include "systick.h" #include "led.h" -- cgit v1.2.3 From 196406e17a45dd87f12c2a1d7c97a67bb9dcb04f Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 29 Jan 2017 19:50:16 +1100 Subject: extmod/vfs_fat: Remove unused fatfs_builtin_open function. --- extmod/vfs_fat.h | 1 - extmod/vfs_fat_file.c | 8 -------- 2 files changed, 9 deletions(-) (limited to 'extmod') diff --git a/extmod/vfs_fat.h b/extmod/vfs_fat.h index fefae776c..0eb963d16 100644 --- a/extmod/vfs_fat.h +++ b/extmod/vfs_fat.h @@ -56,7 +56,6 @@ extern const byte fresult_to_errno_table[20]; extern const mp_obj_type_t mp_fat_vfs_type; mp_import_stat_t fat_vfs_import_stat(struct _fs_user_mount_t *vfs, const char *path); -mp_obj_t fatfs_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs); mp_obj_t fatfs_builtin_open_self(mp_obj_t self_in, mp_obj_t path, mp_obj_t mode); MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj); diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index 0f2a7a1aa..62da23f94 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -289,14 +289,6 @@ const mp_obj_type_t mp_type_textio = { .locals_dict = (mp_obj_dict_t*)&rawfile_locals_dict, }; -// Factory function for I/O stream classes -mp_obj_t fatfs_builtin_open(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { - // TODO: analyze buffering args and instantiate appropriate type - mp_arg_val_t arg_vals[FILE_OPEN_NUM_ARGS]; - mp_arg_parse_all(n_args, args, kwargs, FILE_OPEN_NUM_ARGS, file_open_args, arg_vals); - return file_open(NULL, &mp_type_textio, arg_vals); -} - // Factory function for I/O stream classes mp_obj_t fatfs_builtin_open_self(mp_obj_t self_in, mp_obj_t path, mp_obj_t mode) { // TODO: analyze buffering args and instantiate appropriate type -- cgit v1.2.3 From 0fb27888fc805e4b39f74153d8543fe7348eb886 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 29 Jan 2017 19:50:32 +1100 Subject: extmod/vfs_fat: Remove unused function fat_vfs_listdir. --- extmod/vfs_fat.h | 1 - extmod/vfs_fat_misc.c | 4 ---- 2 files changed, 5 deletions(-) (limited to 'extmod') diff --git a/extmod/vfs_fat.h b/extmod/vfs_fat.h index 0eb963d16..a5e3c604b 100644 --- a/extmod/vfs_fat.h +++ b/extmod/vfs_fat.h @@ -59,5 +59,4 @@ mp_import_stat_t fat_vfs_import_stat(struct _fs_user_mount_t *vfs, const char *p mp_obj_t fatfs_builtin_open_self(mp_obj_t self_in, mp_obj_t path, mp_obj_t mode); MP_DECLARE_CONST_FUN_OBJ_KW(mp_builtin_open_obj); -mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type); mp_obj_t fat_vfs_listdir2(struct _fs_user_mount_t *vfs, const char *path, bool is_str_type); diff --git a/extmod/vfs_fat_misc.c b/extmod/vfs_fat_misc.c index 97d2675cd..19db99c7f 100644 --- a/extmod/vfs_fat_misc.c +++ b/extmod/vfs_fat_misc.c @@ -36,10 +36,6 @@ // TODO: actually, the core function should be ilistdir() -mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type) { - return fat_vfs_listdir2(NULL, path, is_str_type); -} - mp_obj_t fat_vfs_listdir2(fs_user_mount_t *vfs, const char *path, bool is_str_type) { FRESULT res; FILINFO fno; -- cgit v1.2.3 From c30b308492ddd587e5a1f76c538956cad44735a2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 30 Jan 2017 22:26:54 +1100 Subject: extmod/vfs_reader: Fix use of NLR by popping context. --- extmod/vfs_reader.c | 1 + 1 file changed, 1 insertion(+) (limited to 'extmod') diff --git a/extmod/vfs_reader.c b/extmod/vfs_reader.c index 718bdeeb6..9509582d2 100644 --- a/extmod/vfs_reader.c +++ b/extmod/vfs_reader.c @@ -81,6 +81,7 @@ int mp_reader_new_file(mp_reader_t *reader, const char *filename) { rf->file = mp_vfs_open(1, &arg, (mp_map_t*)&mp_const_empty_map); int errcode; rf->len = mp_stream_rw(rf->file, rf->buf, sizeof(rf->buf), &errcode, MP_STREAM_RW_READ | MP_STREAM_RW_ONCE); + nlr_pop(); if (errcode != 0) { return errcode; } -- cgit v1.2.3 From 10dbf2383f14ad4cc9ddfd9e5b75492a219a7a6c Mon Sep 17 00:00:00 2001 From: Andrew Gatt Date: Mon, 30 Jan 2017 11:28:37 +0000 Subject: extmod/vfs_fat.c: Use explicit include path for timeutils.h. --- extmod/vfs_fat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'extmod') diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index b32bf7ad9..82dd312b8 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -38,7 +38,7 @@ #include "py/mperrno.h" #include "lib/oofatfs/ff.h" #include "extmod/vfs_fat.h" -#include "timeutils.h" +#include "lib/timeutils/timeutils.h" #if _MAX_SS == _MIN_SS #define SECSIZE(fs) (_MIN_SS) -- cgit v1.2.3 From d5e9ab6e61729f533dbed5c2b6b27307ce6c3b55 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 5 Feb 2017 14:20:17 +0300 Subject: extmod/machine_pulse: Make time_pulse_us() not throw exceptions. machine.time_pulse_us() is intended to provide very fine timing, including while working with signal bursts, where each transition is tracked in row. Throwing and handling an exception may take too much time and "signal loss". So instead, in case of a timeout, just return negative value. Cases of timeout while waiting for initial signal stabilization, and during actual timing, are recognized. The documentation is updated accordingly, and rewritten somewhat to clarify the function behavior. --- docs/library/machine.rst | 11 +++++++---- drivers/dht/dht.c | 4 ++-- extmod/machine_pulse.c | 6 ++---- tests/extmod/machine_pulse.py | 11 ++--------- tests/extmod/machine_pulse.py.exp | 4 ++-- 5 files changed, 15 insertions(+), 21 deletions(-) (limited to 'extmod') diff --git a/docs/library/machine.rst b/docs/library/machine.rst index 753f6b417..c6da71585 100644 --- a/docs/library/machine.rst +++ b/docs/library/machine.rst @@ -118,12 +118,15 @@ Miscellaneous functions microseconds. The `pulse_level` argument should be 0 to time a low pulse or 1 to time a high pulse. - The function first waits while the pin input is different to the `pulse_level` - parameter, then times the duration that the pin is equal to `pulse_level`. + If the current input value of the pin is different to `pulse_level`, + the function first (*) waits until the pin input becomes equal to `pulse_level`, + then (**) times the duration that the pin is equal to `pulse_level`. If the pin is already equal to `pulse_level` then timing starts straight away. - The function will raise an OSError with ETIMEDOUT if either of the waits is - longer than the given timeout value (which is in microseconds). + The function will return -2 if there was timeout waiting for condition marked + (*) above, and -1 if there was timeout during the main measurement, marked (**) + above. The timeout is the same for both cases and given by `timeout_us` (which + is in microseconds). .. _machine_constants: diff --git a/drivers/dht/dht.c b/drivers/dht/dht.c index 1f0cffc6f..6bdda44b4 100644 --- a/drivers/dht/dht.c +++ b/drivers/dht/dht.c @@ -65,7 +65,7 @@ STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) { // time pulse, should be 80us ticks = machine_time_pulse_us(pin, 1, 150); - if (ticks == (mp_uint_t)-1) { + if ((mp_int_t)ticks < 0) { goto timeout; } @@ -73,7 +73,7 @@ STATIC mp_obj_t dht_readinto(mp_obj_t pin_in, mp_obj_t buf_in) { uint8_t *buf = bufinfo.buf; for (int i = 0; i < 40; ++i) { ticks = machine_time_pulse_us(pin, 1, 100); - if (ticks == (mp_uint_t)-1) { + if ((mp_int_t)ticks < 0) { goto timeout; } buf[i / 8] = (buf[i / 8] << 1) | (ticks > 48); diff --git a/extmod/machine_pulse.c b/extmod/machine_pulse.c index b2a78d72e..5f837479d 100644 --- a/extmod/machine_pulse.c +++ b/extmod/machine_pulse.c @@ -34,7 +34,7 @@ mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t mp_uint_t start = mp_hal_ticks_us(); while (mp_hal_pin_read(pin) != pulse_level) { if ((mp_uint_t)(mp_hal_ticks_us() - start) >= timeout_us) { - return (mp_uint_t)-1; + return (mp_uint_t)-2; } } start = mp_hal_ticks_us(); @@ -57,9 +57,7 @@ STATIC mp_obj_t machine_time_pulse_us_(size_t n_args, const mp_obj_t *args) { timeout_us = mp_obj_get_int(args[2]); } mp_uint_t us = machine_time_pulse_us(pin, level, timeout_us); - if (us == (mp_uint_t)-1) { - mp_raise_OSError(MP_ETIMEDOUT); - } + // May return -1 or -2 in case of timeout return mp_obj_new_int(us); } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_time_pulse_us_obj, 2, 3, machine_time_pulse_us_); diff --git a/tests/extmod/machine_pulse.py b/tests/extmod/machine_pulse.py index b6e126435..6491b5409 100644 --- a/tests/extmod/machine_pulse.py +++ b/tests/extmod/machine_pulse.py @@ -43,12 +43,5 @@ t = machine.time_pulse_us(p, 0) print(type(t)) p = ConstPin(0) -try: - machine.time_pulse_us(p, 1, 10) -except OSError: - print("OSError") - -try: - machine.time_pulse_us(p, 0, 10) -except OSError: - print("OSError") +print(machine.time_pulse_us(p, 1, 10)) +print(machine.time_pulse_us(p, 0, 10)) diff --git a/tests/extmod/machine_pulse.py.exp b/tests/extmod/machine_pulse.py.exp index f9a474218..20d4c1043 100644 --- a/tests/extmod/machine_pulse.py.exp +++ b/tests/extmod/machine_pulse.py.exp @@ -5,5 +5,5 @@ value: 1 value: 0 value: 1 -OSError -OSError +-2 +-1 -- cgit v1.2.3 From dee47949cc8782c2d565de6d6b7e5c1339000061 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 6 Feb 2017 14:38:33 +1100 Subject: extmod/machine_spi: Remove EVENT_POLL_HOOK from soft-SPI transfer func. SPI needs to be fast, and calling the EVENT_POLL_HOOK every byte makes it unusable for ports that need to do non-trivial work in the EVENT_POLL_HOOK call. And individual SPI transfers should be short enough in time that EVENT_POLL_HOOK doesn't need to be called. If something like this proves to be needed in practice then we will need to introduce separate event hook macros, one for "slow" loops (eg select/poll) and one for "fast" loops (eg software I2C, SPI). --- extmod/machine_spi.c | 6 ------ 1 file changed, 6 deletions(-) (limited to 'extmod') diff --git a/extmod/machine_spi.c b/extmod/machine_spi.c index 6e7678498..a67d294ba 100644 --- a/extmod/machine_spi.c +++ b/extmod/machine_spi.c @@ -90,12 +90,6 @@ void mp_machine_soft_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint if (dest != NULL) { dest[i] = data_in; } - - // Some ports need a regular callback, but probably we don't need - // to do this every byte, or even at all. - #ifdef MICROPY_EVENT_POLL_HOOK - MICROPY_EVENT_POLL_HOOK; - #endif } } -- cgit v1.2.3