From e45035db5c297190eee18cefa3b91b36b9f0e32b Mon Sep 17 00:00:00 2001 From: Oleg Korsak Date: Sat, 7 Jan 2017 22:03:51 +0200 Subject: extmod/modframebuf: optimize fill_rect subroutine call --- extmod/modframebuf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'extmod') diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index d6e686e07..93d4922c9 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -115,7 +115,7 @@ static inline uint32_t getpixel(const mp_obj_framebuf_t *fb, int x, int y) { } STATIC void fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) { - if (x + w <= 0 || y + h <= 0 || y >= fb->height || x >= fb->width) { + if (h < 1 || w < 1 || x + w <= 0 || y + h <= 0 || y >= fb->height || x >= fb->width) { // No operation needed. return; } -- cgit v1.2.3 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 From 181f7d145002731e5baec0f2c43ac57818447f8b Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 8 Feb 2017 11:14:23 +0300 Subject: extmod/machine_signal: Implement Signal .on() and .off() methods. Each method asserts and deasserts signal respectively. They are equivalent to .value(1) and .value(0) but conceptually simpler (and may help to avoid confusion with inverted signals, where "asserted" state means logical 0 output). --- extmod/machine_signal.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'extmod') diff --git a/extmod/machine_signal.c b/extmod/machine_signal.c index fb179c438..de6c3ff32 100644 --- a/extmod/machine_signal.c +++ b/extmod/machine_signal.c @@ -92,8 +92,22 @@ STATIC mp_obj_t signal_value(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(signal_value_obj, 1, 2, signal_value); +STATIC mp_obj_t signal_on(mp_obj_t self_in) { + mp_virtual_pin_write(self_in, 1); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(signal_on_obj, signal_on); + +STATIC mp_obj_t signal_off(mp_obj_t self_in) { + mp_virtual_pin_write(self_in, 0); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(signal_off_obj, signal_off); + STATIC const mp_rom_map_elem_t signal_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&signal_value_obj) }, + { MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&signal_on_obj) }, + { MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&signal_off_obj) }, }; STATIC MP_DEFINE_CONST_DICT(signal_locals_dict, signal_locals_dict_table); -- cgit v1.2.3 From ec7dc7f8d796b5b67772d1f40863d13fb5e19be2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 9 Feb 2017 12:03:12 +1100 Subject: extmod/vfs: Allow to mount a block device, not just a VFS object. If the mounted object doesn't have a "mount" method then assume it's a block device and try to detect the filesystem. Since we currently only support FAT filesystems, the behaviour is to just try and create a VfsFat object automatically, using the given block device. --- extmod/vfs.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'extmod') diff --git a/extmod/vfs.c b/extmod/vfs.c index 1eb26acf1..c1e32e052 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -132,11 +132,24 @@ mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args mp_uint_t mnt_len; const char *mnt_str = mp_obj_str_get_data(pos_args[1], &mnt_len); + // see if we need to auto-detect and create the filesystem + mp_obj_t vfs_obj = pos_args[0]; + mp_obj_t dest[2]; + mp_load_method_maybe(vfs_obj, MP_QSTR_mount, dest); + if (dest[0] == MP_OBJ_NULL) { + // Input object has no mount method, assume it's a block device and try to + // auto-detect the filesystem and create the corresponding VFS entity. + // (At the moment we only support FAT filesystems.) + #if MICROPY_VFS_FAT + vfs_obj = mp_fat_vfs_type.make_new(&mp_fat_vfs_type, 1, 0, &vfs_obj); + #endif + } + // create new object 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]; + vfs->obj = vfs_obj; vfs->next = NULL; // call the underlying object to do any mounting operation -- cgit v1.2.3 From 8f1c6d952ac695bc41afe8965aa79fbabcc6bcc1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 9 Feb 2017 15:51:34 +1100 Subject: extmod/vfs: Raise OSError(EEXIST) on attempt to mkdir a mount point. --- extmod/vfs.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'extmod') diff --git a/extmod/vfs.c b/extmod/vfs.c index c1e32e052..98d8711e4 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -275,6 +275,9 @@ 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; mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out); + if (vfs == MP_VFS_ROOT || (vfs != MP_VFS_NONE && !strcmp(mp_obj_str_get_str(path_out), "/"))) { + mp_raise_OSError(MP_EEXIST); + } 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); -- cgit v1.2.3 From 3625afa17362e8a2f873edeaa0856ae852c19beb Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 13 Feb 2017 12:25:43 +1100 Subject: extmod/vfs: Allow to stat the root directory. os.stat('/') now works and returns a mostly-empty tuple. Really all that is useful is the mode which tells that it's a directory. --- extmod/vfs.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'extmod') diff --git a/extmod/vfs.c b/extmod/vfs.c index 98d8711e4..c968345b8 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -311,6 +311,14 @@ 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; mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out); + if (vfs == MP_VFS_ROOT) { + mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL)); + t->items[0] = MP_OBJ_NEW_SMALL_INT(0x4000); // st_mode = stat.S_IFDIR + for (int i = 1; i <= 9; ++i) { + t->items[i] = MP_OBJ_NEW_SMALL_INT(0); // dev, nlink, uid, gid, size, atime, mtime, ctime + } + return MP_OBJ_FROM_PTR(t); + } 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); -- cgit v1.2.3 From a937750cebc104296f9342e559626ff354772fda Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 15 Feb 2017 19:20:46 +0300 Subject: extmod/modlwip: Add my copyright. Per: $ git log modlwip.c |grep ^Auth | sort | uniq -c 9 Author: Damien George 2 Author: Galen Hazelwood 43 Author: Paul Sokolovsky --- extmod/modlwip.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'extmod') diff --git a/extmod/modlwip.c b/extmod/modlwip.c index 0f2a6c64b..5b03d7c29 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -1,10 +1,11 @@ /* - * This file is part of the Micro Python project, http://micropython.org/ + * This file is part of the MicroPython project, http://micropython.org/ * * The MIT License (MIT) * * Copyright (c) 2013, 2014 Damien P. George * Copyright (c) 2015 Galen Hazelwood + * Copyright (c) 2015-2016 Paul Sokolovsky * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal -- cgit v1.2.3 From ae8d86758631e62466a55d179897d2111c3cb1c1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 9 Jan 2016 23:14:54 +0000 Subject: py: Add iter_buf to getiter type method. Allows to iterate over the following without allocating on the heap: - tuple - list - string, bytes - bytearray, array - dict (not dict.keys, dict.values, dict.items) - set, frozenset Allows to call the following without heap memory: - all, any, min, max, sum TODO: still need to allocate stack memory in bytecode for iter_buf. --- cc3200/mods/pybuart.c | 2 +- esp8266/machine_uart.c | 2 +- extmod/modbtree.c | 3 ++- extmod/vfs_fat_file.c | 4 ++-- py/emitnative.c | 1 + py/modbuiltins.c | 14 +++++++++----- py/obj.c | 5 +++++ py/obj.h | 15 +++++++++++++-- py/objarray.c | 14 +++++++++----- py/objdict.c | 24 +++++++++++++++--------- py/objenumerate.c | 6 +++--- py/objfilter.c | 4 ++-- py/objgenerator.c | 2 +- py/objgetitemiter.c | 7 ++++--- py/objlist.c | 14 ++++++++------ py/objmap.c | 4 ++-- py/objpolyiter.c | 2 +- py/objrange.c | 11 ++++++----- py/objreversed.c | 2 +- py/objset.c | 26 +++++++++++++++++--------- py/objstr.c | 17 ++++++++++------- py/objstringio.c | 4 ++-- py/objstrunicode.c | 7 ++++--- py/objtuple.c | 18 +++++++----------- py/objtuple.h | 2 +- py/objtype.c | 4 ++-- py/objzip.c | 4 ++-- py/runtime.c | 26 ++++++++++++++++++-------- py/runtime.h | 2 +- py/vm.c | 2 +- stmhal/pybstdio.c | 4 ++-- stmhal/uart.c | 2 +- stmhal/usb.c | 2 +- unix/file.c | 4 ++-- unix/modffi.c | 6 ++++-- unix/moduselect.c | 2 +- 36 files changed, 162 insertions(+), 106 deletions(-) (limited to 'extmod') diff --git a/cc3200/mods/pybuart.c b/cc3200/mods/pybuart.c index fe710be92..dceb842d5 100644 --- a/cc3200/mods/pybuart.c +++ b/cc3200/mods/pybuart.c @@ -664,7 +664,7 @@ const mp_obj_type_t pyb_uart_type = { .name = MP_QSTR_UART, .print = pyb_uart_print, .make_new = pyb_uart_make_new, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, .protocol = &uart_stream_p, .locals_dict = (mp_obj_t)&pyb_uart_locals_dict, diff --git a/esp8266/machine_uart.c b/esp8266/machine_uart.c index efdfafd1a..ef52e8c9a 100644 --- a/esp8266/machine_uart.c +++ b/esp8266/machine_uart.c @@ -285,7 +285,7 @@ const mp_obj_type_t pyb_uart_type = { .name = MP_QSTR_UART, .print = pyb_uart_print, .make_new = pyb_uart_make_new, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, .protocol = &uart_stream_p, .locals_dict = (mp_obj_dict_t*)&pyb_uart_locals_dict, diff --git a/extmod/modbtree.c b/extmod/modbtree.c index bb75845b1..27f700eea 100644 --- a/extmod/modbtree.c +++ b/extmod/modbtree.c @@ -184,7 +184,8 @@ STATIC mp_obj_t btree_items(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_items_obj, 1, 4, btree_items); -STATIC mp_obj_t btree_getiter(mp_obj_t self_in) { +STATIC mp_obj_t btree_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) { + (void)iter_buf; mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in); if (self->next_flags != 0) { // If we're called immediately after keys(), values(), or items(), diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index 62da23f94..6263492cb 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -264,7 +264,7 @@ const mp_obj_type_t mp_type_fileio = { .name = MP_QSTR_FileIO, .print = file_obj_print, .make_new = file_obj_make_new, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, .protocol = &fileio_stream_p, .locals_dict = (mp_obj_dict_t*)&rawfile_locals_dict, @@ -283,7 +283,7 @@ const mp_obj_type_t mp_type_textio = { .name = MP_QSTR_TextIOWrapper, .print = file_obj_print, .make_new = file_obj_make_new, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, .protocol = &textio_stream_p, .locals_dict = (mp_obj_dict_t*)&rawfile_locals_dict, diff --git a/py/emitnative.c b/py/emitnative.c index bdb590e77..e8e3754e1 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -1806,6 +1806,7 @@ STATIC void emit_native_get_iter(emit_t *emit) { vtype_kind_t vtype; emit_pre_pop_reg(emit, &vtype, REG_ARG_1); assert(vtype == VTYPE_PYOBJ); + assert(0); // TODO allocate memory for iter_buf emit_call(emit, MP_F_GETITER); emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET); } diff --git a/py/modbuiltins.c b/py/modbuiltins.c index a0c68930d..13312d229 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -117,7 +117,8 @@ STATIC mp_obj_t mp_builtin_abs(mp_obj_t o_in) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_abs_obj, mp_builtin_abs); STATIC mp_obj_t mp_builtin_all(mp_obj_t o_in) { - mp_obj_t iterable = mp_getiter(o_in); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iterable = mp_getiter(o_in, &iter_buf); mp_obj_t item; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { if (!mp_obj_is_true(item)) { @@ -129,7 +130,8 @@ STATIC mp_obj_t mp_builtin_all(mp_obj_t o_in) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_all_obj, mp_builtin_all); STATIC mp_obj_t mp_builtin_any(mp_obj_t o_in) { - mp_obj_t iterable = mp_getiter(o_in); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iterable = mp_getiter(o_in, &iter_buf); mp_obj_t item; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { if (mp_obj_is_true(item)) { @@ -258,7 +260,7 @@ STATIC mp_obj_t mp_builtin_hex(mp_obj_t o_in) { MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hex_obj, mp_builtin_hex); STATIC mp_obj_t mp_builtin_iter(mp_obj_t o_in) { - return mp_getiter(o_in); + return mp_getiter(o_in, NULL); } MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_iter_obj, mp_builtin_iter); @@ -270,7 +272,8 @@ STATIC mp_obj_t mp_builtin_min_max(size_t n_args, const mp_obj_t *args, mp_map_t mp_obj_t key_fn = key_elem == NULL ? MP_OBJ_NULL : key_elem->value; if (n_args == 1) { // given an iterable - mp_obj_t iterable = mp_getiter(args[0]); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iterable = mp_getiter(args[0], &iter_buf); mp_obj_t best_key = MP_OBJ_NULL; mp_obj_t best_obj = MP_OBJ_NULL; mp_obj_t item; @@ -495,7 +498,8 @@ STATIC mp_obj_t mp_builtin_sum(size_t n_args, const mp_obj_t *args) { case 1: value = MP_OBJ_NEW_SMALL_INT(0); break; default: value = args[1]; break; } - mp_obj_t iterable = mp_getiter(args[0]); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iterable = mp_getiter(args[0], &iter_buf); mp_obj_t item; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { value = mp_binary_op(MP_BINARY_OP_ADD, value, item); diff --git a/py/obj.c b/py/obj.c index 1d0c80ab9..4534ef1b2 100644 --- a/py/obj.c +++ b/py/obj.c @@ -481,6 +481,11 @@ mp_obj_t mp_identity(mp_obj_t self) { } MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity); +mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf) { + (void)iter_buf; + return self; +} + bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) { mp_obj_type_t *type = mp_obj_get_type(obj); if (type->buffer_p.get_buffer == NULL) { diff --git a/py/obj.h b/py/obj.h index 1169a9095..79a6d56fa 100644 --- a/py/obj.h +++ b/py/obj.h @@ -417,6 +417,11 @@ typedef enum { PRINT_EXC_SUBCLASS = 0x80, // Internal flag for printing exception subclasses } mp_print_kind_t; +typedef struct _mp_obj_iter_buf_t { + mp_obj_base_t base; + mp_obj_t buf[3]; +} mp_obj_iter_buf_t; + typedef void (*mp_print_fun_t)(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind); typedef mp_obj_t (*mp_make_new_fun_t)(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args); typedef mp_obj_t (*mp_call_fun_t)(mp_obj_t fun, size_t n_args, size_t n_kw, const mp_obj_t *args); @@ -424,6 +429,7 @@ typedef mp_obj_t (*mp_unary_op_fun_t)(mp_uint_t op, mp_obj_t); typedef mp_obj_t (*mp_binary_op_fun_t)(mp_uint_t op, mp_obj_t, mp_obj_t); typedef void (*mp_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest); typedef mp_obj_t (*mp_subscr_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value); +typedef mp_obj_t (*mp_getiter_fun_t)(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf); // Buffer protocol typedef struct _mp_buffer_info_t { @@ -486,7 +492,11 @@ struct _mp_obj_type_t { // value=MP_OBJ_NULL means delete, value=MP_OBJ_SENTINEL means load, else store // can return MP_OBJ_NULL if op not supported - mp_fun_1_t getiter; // corresponds to __iter__ special method + // corresponds to __iter__ special method + // can use given mp_obj_iter_buf_t to store iterator + // otherwise can return a pointer to an object on the heap + mp_getiter_fun_t getiter; + mp_fun_1_t iternext; // may return MP_OBJ_STOP_ITERATION as an optimisation instead of raising StopIteration() (with no args) mp_buffer_p_t buffer_p; @@ -637,7 +647,7 @@ mp_obj_t mp_obj_new_set(size_t n_args, mp_obj_t *items); mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step); mp_obj_t mp_obj_new_super(mp_obj_t type, mp_obj_t obj); mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self); -mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args); +mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args, mp_obj_iter_buf_t *iter_buf); mp_obj_t mp_obj_new_module(qstr module_name); mp_obj_t mp_obj_new_memoryview(byte typecode, size_t nitems, void *items); @@ -775,6 +785,7 @@ qstr mp_obj_code_get_name(const byte *code_info); mp_obj_t mp_identity(mp_obj_t self); MP_DECLARE_CONST_FUN_OBJ_1(mp_identity_obj); +mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf); // module typedef struct _mp_obj_module_t { diff --git a/py/objarray.c b/py/objarray.c index f23791857..c81aebb50 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -59,7 +59,7 @@ #define TYPECODE_MASK (~(size_t)0) #endif -STATIC mp_obj_t array_iterator_new(mp_obj_t array_in); +STATIC mp_obj_t array_iterator_new(mp_obj_t array_in, mp_obj_iter_buf_t *iter_buf); STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg); STATIC mp_obj_t array_extend(mp_obj_t self_in, mp_obj_t arg_in); STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags); @@ -141,7 +141,8 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) { mp_obj_array_t *array = array_new(typecode, len); - mp_obj_t iterable = mp_getiter(initializer); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iterable = mp_getiter(initializer, &iter_buf); mp_obj_t item; size_t i = 0; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { @@ -608,15 +609,18 @@ STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) { STATIC const mp_obj_type_t array_it_type = { { &mp_type_type }, .name = MP_QSTR_iterator, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = array_it_iternext, }; -STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) { +STATIC mp_obj_t array_iterator_new(mp_obj_t array_in, mp_obj_iter_buf_t *iter_buf) { + assert(sizeof(mp_obj_array_t) <= sizeof(mp_obj_iter_buf_t)); mp_obj_array_t *array = MP_OBJ_TO_PTR(array_in); - mp_obj_array_it_t *o = m_new0(mp_obj_array_it_t, 1); + mp_obj_array_it_t *o = (mp_obj_array_it_t*)iter_buf; o->base.type = &array_it_type; o->array = array; + o->offset = 0; + o->cur = 0; #if MICROPY_PY_BUILTINS_MEMORYVIEW if (array->base.type == &mp_type_memoryview) { o->offset = array->free; diff --git a/py/objdict.c b/py/objdict.c index 640df7b62..c52403f71 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -210,8 +210,9 @@ STATIC mp_obj_t dict_it_iternext(mp_obj_t self_in) { } } -STATIC mp_obj_t dict_getiter(mp_obj_t self_in) { - mp_obj_dict_it_t *o = m_new_obj(mp_obj_dict_it_t); +STATIC mp_obj_t dict_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) { + assert(sizeof(mp_obj_dict_it_t) <= sizeof(mp_obj_iter_buf_t)); + mp_obj_dict_it_t *o = (mp_obj_dict_it_t*)iter_buf; o->base.type = &mp_type_polymorph_iter; o->iternext = dict_it_iternext; o->dict = self_in; @@ -249,7 +250,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(dict_copy_obj, dict_copy); // this is a classmethod STATIC mp_obj_t dict_fromkeys(size_t n_args, const mp_obj_t *args) { - mp_obj_t iter = mp_getiter(args[1]); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iter = mp_getiter(args[1], &iter_buf); mp_obj_t value = mp_const_none; mp_obj_t next = MP_OBJ_NULL; @@ -375,10 +377,12 @@ STATIC mp_obj_t dict_update(size_t n_args, const mp_obj_t *args, mp_map_t *kwarg } } else { // update from a generic iterable of pairs - mp_obj_t iter = mp_getiter(args[1]); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iter = mp_getiter(args[1], &iter_buf); mp_obj_t next = MP_OBJ_NULL; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { - mp_obj_t inneriter = mp_getiter(next); + mp_obj_iter_buf_t inner_iter_buf; + mp_obj_t inneriter = mp_getiter(next, &inner_iter_buf); mp_obj_t key = mp_iternext(inneriter); mp_obj_t value = mp_iternext(inneriter); mp_obj_t stop = mp_iternext(inneriter); @@ -457,14 +461,15 @@ STATIC mp_obj_t dict_view_it_iternext(mp_obj_t self_in) { STATIC const mp_obj_type_t dict_view_it_type = { { &mp_type_type }, .name = MP_QSTR_iterator, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = dict_view_it_iternext, }; -STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in) { +STATIC mp_obj_t dict_view_getiter(mp_obj_t view_in, mp_obj_iter_buf_t *iter_buf) { + assert(sizeof(mp_obj_dict_view_it_t) <= sizeof(mp_obj_iter_buf_t)); mp_check_self(MP_OBJ_IS_TYPE(view_in, &dict_view_type)); mp_obj_dict_view_t *view = MP_OBJ_TO_PTR(view_in); - mp_obj_dict_view_it_t *o = m_new_obj(mp_obj_dict_view_it_t); + mp_obj_dict_view_it_t *o = (mp_obj_dict_view_it_t*)iter_buf; o->base.type = &dict_view_it_type; o->kind = view->kind; o->dict = view->dict; @@ -479,7 +484,8 @@ STATIC void dict_view_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ bool first = true; mp_print_str(print, mp_dict_view_names[self->kind]); mp_print_str(print, "(["); - mp_obj_t self_iter = dict_view_getiter(self_in); + mp_obj_iter_buf_t iter_buf; + mp_obj_t self_iter = dict_view_getiter(self_in, &iter_buf); mp_obj_t next = MP_OBJ_NULL; while ((next = dict_view_it_iternext(self_iter)) != MP_OBJ_STOP_ITERATION) { if (!first) { diff --git a/py/objenumerate.c b/py/objenumerate.c index 2b646ca45..faae6516c 100644 --- a/py/objenumerate.c +++ b/py/objenumerate.c @@ -56,13 +56,13 @@ STATIC mp_obj_t enumerate_make_new(const mp_obj_type_t *type, size_t n_args, siz // create enumerate object mp_obj_enumerate_t *o = m_new_obj(mp_obj_enumerate_t); o->base.type = type; - o->iter = mp_getiter(arg_vals.iterable.u_obj); + o->iter = mp_getiter(arg_vals.iterable.u_obj, NULL); o->cur = arg_vals.start.u_int; #else (void)n_kw; mp_obj_enumerate_t *o = m_new_obj(mp_obj_enumerate_t); o->base.type = type; - o->iter = mp_getiter(args[0]); + o->iter = mp_getiter(args[0], NULL); o->cur = n_args > 1 ? mp_obj_get_int(args[1]) : 0; #endif @@ -74,7 +74,7 @@ const mp_obj_type_t mp_type_enumerate = { .name = MP_QSTR_enumerate, .make_new = enumerate_make_new, .iternext = enumerate_iternext, - .getiter = mp_identity, + .getiter = mp_identity_getiter, }; STATIC mp_obj_t enumerate_iternext(mp_obj_t self_in) { diff --git a/py/objfilter.c b/py/objfilter.c index a5c85b2ce..a655b8a78 100644 --- a/py/objfilter.c +++ b/py/objfilter.c @@ -39,7 +39,7 @@ STATIC mp_obj_t filter_make_new(const mp_obj_type_t *type, size_t n_args, size_t mp_obj_filter_t *o = m_new_obj(mp_obj_filter_t); o->base.type = type; o->fun = args[0]; - o->iter = mp_getiter(args[1]); + o->iter = mp_getiter(args[1], NULL); return MP_OBJ_FROM_PTR(o); } @@ -65,7 +65,7 @@ const mp_obj_type_t mp_type_filter = { { &mp_type_type }, .name = MP_QSTR_filter, .make_new = filter_make_new, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = filter_iternext, }; diff --git a/py/objgenerator.c b/py/objgenerator.c index 0be9b8d59..654b18670 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -236,7 +236,7 @@ const mp_obj_type_t mp_type_gen_instance = { { &mp_type_type }, .name = MP_QSTR_generator, .print = gen_instance_print, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = gen_instance_iternext, .locals_dict = (mp_obj_dict_t*)&gen_instance_locals_dict, }; diff --git a/py/objgetitemiter.c b/py/objgetitemiter.c index 526180fdb..a3c754448 100644 --- a/py/objgetitemiter.c +++ b/py/objgetitemiter.c @@ -61,13 +61,14 @@ STATIC mp_obj_t it_iternext(mp_obj_t self_in) { STATIC const mp_obj_type_t it_type = { { &mp_type_type }, .name = MP_QSTR_iterator, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = it_iternext, }; // args are those returned from mp_load_method_maybe (ie either an attribute or a method) -mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args) { - mp_obj_getitem_iter_t *o = m_new_obj(mp_obj_getitem_iter_t); +mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args, mp_obj_iter_buf_t *iter_buf) { + assert(sizeof(mp_obj_getitem_iter_t) <= sizeof(mp_obj_iter_buf_t)); + mp_obj_getitem_iter_t *o = (mp_obj_getitem_iter_t*)iter_buf; o->base.type = &it_type; o->args[0] = args[0]; o->args[1] = args[1]; diff --git a/py/objlist.c b/py/objlist.c index 210140388..28da10991 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -33,7 +33,7 @@ #include "py/runtime.h" #include "py/stackctrl.h" -STATIC mp_obj_t mp_obj_new_list_iterator(mp_obj_t list, size_t cur); +STATIC mp_obj_t mp_obj_new_list_iterator(mp_obj_t list, size_t cur, mp_obj_iter_buf_t *iter_buf); STATIC mp_obj_list_t *list_new(size_t n); STATIC mp_obj_t list_extend(mp_obj_t self_in, mp_obj_t arg_in); STATIC mp_obj_t list_pop(size_t n_args, const mp_obj_t *args); @@ -60,7 +60,8 @@ STATIC void list_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t k } STATIC mp_obj_t list_extend_from_iter(mp_obj_t list, mp_obj_t iterable) { - mp_obj_t iter = mp_getiter(iterable); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iter = mp_getiter(iterable, &iter_buf); mp_obj_t item; while ((item = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { mp_obj_list_append(list, item); @@ -225,8 +226,8 @@ STATIC mp_obj_t list_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { } } -STATIC mp_obj_t list_getiter(mp_obj_t o_in) { - return mp_obj_new_list_iterator(o_in, 0); +STATIC mp_obj_t list_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) { + return mp_obj_new_list_iterator(o_in, 0, iter_buf); } mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg) { @@ -516,8 +517,9 @@ STATIC mp_obj_t list_it_iternext(mp_obj_t self_in) { } } -mp_obj_t mp_obj_new_list_iterator(mp_obj_t list, size_t cur) { - mp_obj_list_it_t *o = m_new_obj(mp_obj_list_it_t); +mp_obj_t mp_obj_new_list_iterator(mp_obj_t list, size_t cur, mp_obj_iter_buf_t *iter_buf) { + assert(sizeof(mp_obj_list_it_t) <= sizeof(mp_obj_iter_buf_t)); + mp_obj_list_it_t *o = (mp_obj_list_it_t*)iter_buf; o->base.type = &mp_type_polymorph_iter; o->iternext = list_it_iternext; o->list = list; diff --git a/py/objmap.c b/py/objmap.c index ed0291435..0b2189016 100644 --- a/py/objmap.c +++ b/py/objmap.c @@ -43,7 +43,7 @@ STATIC mp_obj_t map_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_ o->n_iters = n_args - 1; o->fun = args[0]; for (mp_uint_t i = 0; i < n_args - 1; i++) { - o->iters[i] = mp_getiter(args[i + 1]); + o->iters[i] = mp_getiter(args[i + 1], NULL); } return MP_OBJ_FROM_PTR(o); } @@ -68,6 +68,6 @@ const mp_obj_type_t mp_type_map = { { &mp_type_type }, .name = MP_QSTR_map, .make_new = map_make_new, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = map_iternext, }; diff --git a/py/objpolyiter.c b/py/objpolyiter.c index 9bba538c5..61bd1e0ac 100644 --- a/py/objpolyiter.c +++ b/py/objpolyiter.c @@ -49,6 +49,6 @@ STATIC mp_obj_t polymorph_it_iternext(mp_obj_t self_in) { const mp_obj_type_t mp_type_polymorph_iter = { { &mp_type_type }, .name = MP_QSTR_iterator, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = polymorph_it_iternext, }; diff --git a/py/objrange.c b/py/objrange.c index 79459316b..dd074a98a 100644 --- a/py/objrange.c +++ b/py/objrange.c @@ -55,12 +55,13 @@ STATIC mp_obj_t range_it_iternext(mp_obj_t o_in) { STATIC const mp_obj_type_t range_it_type = { { &mp_type_type }, .name = MP_QSTR_iterator, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = range_it_iternext, }; -STATIC mp_obj_t mp_obj_new_range_iterator(mp_int_t cur, mp_int_t stop, mp_int_t step) { - mp_obj_range_it_t *o = m_new_obj(mp_obj_range_it_t); +STATIC mp_obj_t mp_obj_new_range_iterator(mp_int_t cur, mp_int_t stop, mp_int_t step, mp_obj_iter_buf_t *iter_buf) { + assert(sizeof(mp_obj_range_it_t) <= sizeof(mp_obj_iter_buf_t)); + mp_obj_range_it_t *o = (mp_obj_range_it_t*)iter_buf; o->base.type = &range_it_type; o->cur = cur; o->stop = stop; @@ -161,9 +162,9 @@ STATIC mp_obj_t range_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { } } -STATIC mp_obj_t range_getiter(mp_obj_t o_in) { +STATIC mp_obj_t range_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) { mp_obj_range_t *o = MP_OBJ_TO_PTR(o_in); - return mp_obj_new_range_iterator(o->start, o->stop, o->step); + return mp_obj_new_range_iterator(o->start, o->stop, o->step, iter_buf); } diff --git a/py/objreversed.c b/py/objreversed.c index 4343c1978..fc85e72bf 100644 --- a/py/objreversed.c +++ b/py/objreversed.c @@ -74,7 +74,7 @@ const mp_obj_type_t mp_type_reversed = { { &mp_type_type }, .name = MP_QSTR_reversed, .make_new = reversed_make_new, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = reversed_iternext, }; diff --git a/py/objset.c b/py/objset.c index b9da44a44..99e1e8ca5 100644 --- a/py/objset.c +++ b/py/objset.c @@ -129,7 +129,8 @@ STATIC mp_obj_t set_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_ default: { // can only be 0 or 1 arg // 1 argument, an iterable from which we make a new set mp_obj_t set = mp_obj_new_set(0, NULL); - mp_obj_t iterable = mp_getiter(args[0]); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iterable = mp_getiter(args[0], &iter_buf); mp_obj_t item; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { mp_obj_set_store(set, item); @@ -156,8 +157,9 @@ STATIC mp_obj_t set_it_iternext(mp_obj_t self_in) { return MP_OBJ_STOP_ITERATION; } -STATIC mp_obj_t set_getiter(mp_obj_t set_in) { - mp_obj_set_it_t *o = m_new_obj(mp_obj_set_it_t); +STATIC mp_obj_t set_getiter(mp_obj_t set_in, mp_obj_iter_buf_t *iter_buf) { + assert(sizeof(mp_obj_set_it_t) <= sizeof(mp_obj_iter_buf_t)); + mp_obj_set_it_t *o = (mp_obj_set_it_t*)iter_buf; o->base.type = &mp_type_polymorph_iter; o->iternext = set_it_iternext; o->set = (mp_obj_set_t *)MP_OBJ_TO_PTR(set_in); @@ -233,7 +235,8 @@ STATIC mp_obj_t set_diff_int(size_t n_args, const mp_obj_t *args, bool update) { if (self == other) { set_clear(self); } else { - mp_obj_t iter = mp_getiter(other); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iter = mp_getiter(other, &iter_buf); mp_obj_t next; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { set_discard(self, next); @@ -270,7 +273,8 @@ STATIC mp_obj_t set_intersect_int(mp_obj_t self_in, mp_obj_t other, bool update) mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_set_t *out = MP_OBJ_TO_PTR(mp_obj_new_set(0, NULL)); - mp_obj_t iter = mp_getiter(other); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iter = mp_getiter(other, &iter_buf); mp_obj_t next; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) { @@ -302,7 +306,8 @@ STATIC mp_obj_t set_isdisjoint(mp_obj_t self_in, mp_obj_t other) { check_set_or_frozenset(self_in); mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in); - mp_obj_t iter = mp_getiter(other); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iter = mp_getiter(other, &iter_buf); mp_obj_t next; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { if (mp_set_lookup(&self->set, next, MP_MAP_LOOKUP)) { @@ -335,7 +340,8 @@ STATIC mp_obj_t set_issubset_internal(mp_obj_t self_in, mp_obj_t other_in, bool if (proper && self->set.used == other->set.used) { out = false; } else { - mp_obj_t iter = set_getiter(MP_OBJ_FROM_PTR(self)); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iter = set_getiter(MP_OBJ_FROM_PTR(self), &iter_buf); mp_obj_t next; while ((next = set_it_iternext(iter)) != MP_OBJ_STOP_ITERATION) { if (!mp_set_lookup(&other->set, next, MP_MAP_LOOKUP)) { @@ -408,7 +414,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_remove_obj, set_remove); STATIC mp_obj_t set_symmetric_difference_update(mp_obj_t self_in, mp_obj_t other_in) { check_set(self_in); mp_obj_set_t *self = MP_OBJ_TO_PTR(self_in); - mp_obj_t iter = mp_getiter(other_in); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iter = mp_getiter(other_in, &iter_buf); mp_obj_t next; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND); @@ -427,7 +434,8 @@ STATIC mp_obj_t set_symmetric_difference(mp_obj_t self_in, mp_obj_t other_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_2(set_symmetric_difference_obj, set_symmetric_difference); STATIC void set_update_int(mp_obj_set_t *self, mp_obj_t other_in) { - mp_obj_t iter = mp_getiter(other_in); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iter = mp_getiter(other_in, &iter_buf); mp_obj_t next; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { mp_set_lookup(&self->set, next, MP_MAP_LOOKUP_ADD_IF_NOT_FOUND); diff --git a/py/objstr.c b/py/objstr.c index 262b89ddd..c137afe67 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -38,7 +38,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, size_t n_args, const mp_obj_t *args, mp_obj_t dict); -STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str); +STATIC mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf); STATIC NORETURN void bad_implicit_conversion(mp_obj_t self_in); /******************************************************************************/ @@ -231,7 +231,8 @@ STATIC mp_obj_t bytes_make_new(const mp_obj_type_t *type_in, size_t n_args, size vstr_init(&vstr, len); } - mp_obj_t iterable = mp_getiter(args[0]); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iterable = mp_getiter(args[0], &iter_buf); mp_obj_t item; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { mp_int_t val = mp_obj_get_int(item); @@ -1942,7 +1943,7 @@ STATIC const mp_rom_map_elem_t str8_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(str8_locals_dict, str8_locals_dict_table); #if !MICROPY_PY_BUILTINS_STR_UNICODE -STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str); +STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf); const mp_obj_type_t mp_type_str = { { &mp_type_type }, @@ -2142,8 +2143,9 @@ STATIC mp_obj_t str_it_iternext(mp_obj_t self_in) { } } -STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str) { - mp_obj_str8_it_t *o = m_new_obj(mp_obj_str8_it_t); +STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf) { + assert(sizeof(mp_obj_str8_it_t) <= sizeof(mp_obj_iter_buf_t)); + mp_obj_str8_it_t *o = (mp_obj_str8_it_t*)iter_buf; o->base.type = &mp_type_polymorph_iter; o->iternext = str_it_iternext; o->str = str; @@ -2164,8 +2166,9 @@ STATIC mp_obj_t bytes_it_iternext(mp_obj_t self_in) { } } -mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str) { - mp_obj_str8_it_t *o = m_new_obj(mp_obj_str8_it_t); +mp_obj_t mp_obj_new_bytes_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf) { + assert(sizeof(mp_obj_str8_it_t) <= sizeof(mp_obj_iter_buf_t)); + mp_obj_str8_it_t *o = (mp_obj_str8_it_t*)iter_buf; o->base.type = &mp_type_polymorph_iter; o->iternext = bytes_it_iternext; o->str = str; diff --git a/py/objstringio.c b/py/objstringio.c index a77ffae24..61a30752e 100644 --- a/py/objstringio.c +++ b/py/objstringio.c @@ -215,7 +215,7 @@ const mp_obj_type_t mp_type_stringio = { .name = MP_QSTR_StringIO, .print = stringio_print, .make_new = stringio_make_new, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, .protocol = &stringio_stream_p, .locals_dict = (mp_obj_dict_t*)&stringio_locals_dict, @@ -227,7 +227,7 @@ const mp_obj_type_t mp_type_bytesio = { .name = MP_QSTR_BytesIO, .print = stringio_print, .make_new = stringio_make_new, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, .protocol = &bytesio_stream_p, .locals_dict = (mp_obj_dict_t*)&stringio_locals_dict, diff --git a/py/objstrunicode.c b/py/objstrunicode.c index 0091edd72..441ec293d 100644 --- a/py/objstrunicode.c +++ b/py/objstrunicode.c @@ -36,7 +36,7 @@ #if MICROPY_PY_BUILTINS_STR_UNICODE -STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str); +STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf); /******************************************************************************/ /* str */ @@ -301,8 +301,9 @@ STATIC mp_obj_t str_it_iternext(mp_obj_t self_in) { } } -STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str) { - mp_obj_str_it_t *o = m_new_obj(mp_obj_str_it_t); +STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str, mp_obj_iter_buf_t *iter_buf) { + assert(sizeof(mp_obj_str_it_t) <= sizeof(mp_obj_iter_buf_t)); + mp_obj_str_it_t *o = (mp_obj_str_it_t*)iter_buf; o->base.type = &mp_type_polymorph_iter; o->iternext = str_it_iternext; o->str = str; diff --git a/py/objtuple.c b/py/objtuple.c index 1935a6709..ad45696ca 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -32,8 +32,6 @@ #include "py/runtime0.h" #include "py/runtime.h" -STATIC mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, size_t cur); - /******************************************************************************/ /* tuple */ @@ -84,7 +82,8 @@ STATIC mp_obj_t mp_obj_tuple_make_new(const mp_obj_type_t *type_in, size_t n_arg size_t len = 0; mp_obj_t *items = m_new(mp_obj_t, alloc); - mp_obj_t iterable = mp_getiter(args[0]); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iterable = mp_getiter(args[0], &iter_buf); mp_obj_t item; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { if (len >= alloc) { @@ -195,10 +194,6 @@ mp_obj_t mp_obj_tuple_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { } } -mp_obj_t mp_obj_tuple_getiter(mp_obj_t o_in) { - return mp_obj_new_tuple_iterator(MP_OBJ_TO_PTR(o_in), 0); -} - STATIC mp_obj_t tuple_count(mp_obj_t self_in, mp_obj_t value) { mp_check_self(MP_OBJ_IS_TYPE(self_in, &mp_type_tuple)); mp_obj_tuple_t *self = MP_OBJ_TO_PTR(self_in); @@ -284,11 +279,12 @@ STATIC mp_obj_t tuple_it_iternext(mp_obj_t self_in) { } } -STATIC mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, size_t cur) { - mp_obj_tuple_it_t *o = m_new_obj(mp_obj_tuple_it_t); +mp_obj_t mp_obj_tuple_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) { + assert(sizeof(mp_obj_tuple_it_t) <= sizeof(mp_obj_iter_buf_t)); + mp_obj_tuple_it_t *o = (mp_obj_tuple_it_t*)iter_buf; o->base.type = &mp_type_polymorph_iter; o->iternext = tuple_it_iternext; - o->tuple = tuple; - o->cur = cur; + o->tuple = MP_OBJ_TO_PTR(o_in); + o->cur = 0; return MP_OBJ_FROM_PTR(o); } diff --git a/py/objtuple.h b/py/objtuple.h index 760135f86..555c3b3c2 100644 --- a/py/objtuple.h +++ b/py/objtuple.h @@ -44,7 +44,7 @@ void mp_obj_tuple_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t mp_obj_t mp_obj_tuple_unary_op(mp_uint_t op, mp_obj_t self_in); mp_obj_t mp_obj_tuple_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs); mp_obj_t mp_obj_tuple_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value); -mp_obj_t mp_obj_tuple_getiter(mp_obj_t o_in); +mp_obj_t mp_obj_tuple_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf); extern const mp_obj_type_t mp_type_attrtuple; diff --git a/py/objtype.c b/py/objtype.c index 60f630c3d..a4c424929 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -758,7 +758,7 @@ mp_obj_t mp_obj_instance_call(mp_obj_t self_in, size_t n_args, size_t n_kw, cons return mp_call_method_self_n_kw(member[0], member[1], n_args, n_kw, args); } -STATIC mp_obj_t instance_getiter(mp_obj_t self_in) { +STATIC mp_obj_t instance_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) { mp_obj_instance_t *self = MP_OBJ_TO_PTR(self_in); mp_obj_t member[2] = {MP_OBJ_NULL}; struct class_lookup_data lookup = { @@ -773,7 +773,7 @@ STATIC mp_obj_t instance_getiter(mp_obj_t self_in) { return MP_OBJ_NULL; } else if (member[0] == MP_OBJ_SENTINEL) { mp_obj_type_t *type = mp_obj_get_type(self->subobj[0]); - return type->getiter(self->subobj[0]); + return type->getiter(self->subobj[0], iter_buf); } else { return mp_call_method_n_kw(0, 0, member); } diff --git a/py/objzip.c b/py/objzip.c index 6edefc361..5d9ba48e7 100644 --- a/py/objzip.c +++ b/py/objzip.c @@ -43,7 +43,7 @@ STATIC mp_obj_t zip_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_ o->base.type = type; o->n_iters = n_args; for (mp_uint_t i = 0; i < n_args; i++) { - o->iters[i] = mp_getiter(args[i]); + o->iters[i] = mp_getiter(args[i], NULL); } return MP_OBJ_FROM_PTR(o); } @@ -71,6 +71,6 @@ const mp_obj_type_t mp_type_zip = { { &mp_type_type }, .name = MP_QSTR_zip, .make_new = zip_make_new, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = zip_iternext, }; diff --git a/py/runtime.c b/py/runtime.c index 3ac30f58e..b9d7b72dc 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -520,7 +520,8 @@ mp_obj_t mp_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs) { } if (type->getiter != NULL) { /* second attempt, walk the iterator */ - mp_obj_t iter = mp_getiter(rhs); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iter = mp_getiter(rhs, &iter_buf); mp_obj_t next; while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { if (mp_obj_equal(next, lhs)) { @@ -698,7 +699,8 @@ void mp_call_prepare_args_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_ args2_len += n_args; // extract the variable position args from the iterator - mp_obj_t iterable = mp_getiter(pos_seq); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iterable = mp_getiter(pos_seq, &iter_buf); mp_obj_t item; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { if (args2_len >= args2_alloc) { @@ -743,7 +745,8 @@ void mp_call_prepare_args_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_ // get the keys iterable mp_obj_t dest[3]; mp_load_method(kw_dict, MP_QSTR_keys, dest); - mp_obj_t iterable = mp_getiter(mp_call_method_n_kw(0, 0, dest)); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iterable = mp_getiter(mp_call_method_n_kw(0, 0, dest), &iter_buf); mp_obj_t key; while ((key = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { @@ -809,7 +812,8 @@ void mp_unpack_sequence(mp_obj_t seq_in, size_t num, mp_obj_t *items) { items[i] = seq_items[num - 1 - i]; } } else { - mp_obj_t iterable = mp_getiter(seq_in); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iterable = mp_getiter(seq_in, &iter_buf); for (seq_len = 0; seq_len < num; seq_len++) { mp_obj_t el = mp_iternext(iterable); @@ -873,7 +877,8 @@ void mp_unpack_ex(mp_obj_t seq_in, size_t num_in, mp_obj_t *items) { // items destination array, then the rest to a dynamically created list. Once the // iterable is exhausted, we take from this list for the right part of the items. // TODO Improve to waste less memory in the dynamically created list. - mp_obj_t iterable = mp_getiter(seq_in); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iterable = mp_getiter(seq_in, &iter_buf); mp_obj_t item; for (seq_len = 0; seq_len < num_left; seq_len++) { item = mp_iternext(iterable); @@ -1096,13 +1101,18 @@ void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) { } } -mp_obj_t mp_getiter(mp_obj_t o_in) { +mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) { assert(o_in); + // if caller did not provide a buffer then allocate one on the heap + if (iter_buf == NULL) { + iter_buf = m_new_obj(mp_obj_iter_buf_t); + } + // check for native getiter (corresponds to __iter__) mp_obj_type_t *type = mp_obj_get_type(o_in); if (type->getiter != NULL) { - mp_obj_t iter = type->getiter(o_in); + mp_obj_t iter = type->getiter(o_in, iter_buf); if (iter != MP_OBJ_NULL) { return iter; } @@ -1113,7 +1123,7 @@ mp_obj_t mp_getiter(mp_obj_t o_in) { mp_load_method_maybe(o_in, MP_QSTR___getitem__, dest); if (dest[0] != MP_OBJ_NULL) { // __getitem__ exists, create and return an iterator - return mp_obj_new_getitem_iter(dest); + return mp_obj_new_getitem_iter(dest, iter_buf); } // object not iterable diff --git a/py/runtime.h b/py/runtime.h index e25f2a483..954833b67 100644 --- a/py/runtime.h +++ b/py/runtime.h @@ -123,7 +123,7 @@ void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest); void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest); void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t val); -mp_obj_t mp_getiter(mp_obj_t o); +mp_obj_t mp_getiter(mp_obj_t o, mp_obj_iter_buf_t *iter_buf); mp_obj_t mp_iternext_allow_raise(mp_obj_t o); // may return MP_OBJ_STOP_ITERATION instead of raising StopIteration() mp_obj_t mp_iternext(mp_obj_t o); // will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration(...) mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val); diff --git a/py/vm.c b/py/vm.c index 97f344b08..917d41a11 100644 --- a/py/vm.c +++ b/py/vm.c @@ -723,7 +723,7 @@ unwind_jump:; ENTRY(MP_BC_GET_ITER): MARK_EXC_IP_SELECTIVE(); - SET_TOP(mp_getiter(TOP())); + SET_TOP(mp_getiter(TOP(), NULL)); DISPATCH(); ENTRY(MP_BC_FOR_ITER): { diff --git a/stmhal/pybstdio.c b/stmhal/pybstdio.c index dec4f227a..9b1bfff90 100644 --- a/stmhal/pybstdio.c +++ b/stmhal/pybstdio.c @@ -120,7 +120,7 @@ STATIC const mp_obj_type_t stdio_obj_type = { .name = MP_QSTR_FileIO, // TODO .make_new? .print = stdio_obj_print, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, .protocol = &stdio_obj_stream_p, .locals_dict = (mp_obj_t)&stdio_locals_dict, @@ -153,7 +153,7 @@ STATIC const mp_obj_type_t stdio_buffer_obj_type = { { &mp_type_type }, .name = MP_QSTR_FileIO, .print = stdio_obj_print, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, .protocol = &stdio_buffer_obj_stream_p, .locals_dict = (mp_obj_t)&stdio_locals_dict, diff --git a/stmhal/uart.c b/stmhal/uart.c index d53ca5b80..4fae3a80c 100644 --- a/stmhal/uart.c +++ b/stmhal/uart.c @@ -1037,7 +1037,7 @@ const mp_obj_type_t pyb_uart_type = { .name = MP_QSTR_UART, .print = pyb_uart_print, .make_new = pyb_uart_make_new, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, .protocol = &uart_stream_p, .locals_dict = (mp_obj_t)&pyb_uart_locals_dict, diff --git a/stmhal/usb.c b/stmhal/usb.c index c413ce4ba..f71c70665 100644 --- a/stmhal/usb.c +++ b/stmhal/usb.c @@ -518,7 +518,7 @@ const mp_obj_type_t pyb_usb_vcp_type = { .name = MP_QSTR_USB_VCP, .print = pyb_usb_vcp_print, .make_new = pyb_usb_vcp_make_new, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, .protocol = &pyb_usb_vcp_stream_p, .locals_dict = (mp_obj_t)&pyb_usb_vcp_locals_dict, diff --git a/unix/file.c b/unix/file.c index 96fe78c49..a60840c81 100644 --- a/unix/file.c +++ b/unix/file.c @@ -244,7 +244,7 @@ const mp_obj_type_t mp_type_fileio = { .name = MP_QSTR_FileIO, .print = fdfile_print, .make_new = fdfile_make_new, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, .protocol = &fileio_stream_p, .locals_dict = (mp_obj_dict_t*)&rawfile_locals_dict, @@ -263,7 +263,7 @@ const mp_obj_type_t mp_type_textio = { .name = MP_QSTR_TextIOWrapper, .print = fdfile_print, .make_new = fdfile_make_new, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = mp_stream_unbuffered_iter, .protocol = &textio_stream_p, .locals_dict = (mp_obj_dict_t*)&rawfile_locals_dict, diff --git a/unix/modffi.c b/unix/modffi.c index cae16c579..74194e2cc 100644 --- a/unix/modffi.c +++ b/unix/modffi.c @@ -194,7 +194,8 @@ STATIC mp_obj_t make_func(mp_obj_t rettype_in, void *func, mp_obj_t argtypes_in) o->rettype = *rettype; o->argtypes = argtypes; - mp_obj_t iterable = mp_getiter(argtypes_in); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iterable = mp_getiter(argtypes_in, &iter_buf); mp_obj_t item; int i = 0; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { @@ -251,7 +252,8 @@ STATIC mp_obj_t mod_ffi_callback(mp_obj_t rettype_in, mp_obj_t func_in, mp_obj_t o->rettype = *rettype; - mp_obj_t iterable = mp_getiter(paramtypes_in); + mp_obj_iter_buf_t iter_buf; + mp_obj_t iterable = mp_getiter(paramtypes_in, &iter_buf); mp_obj_t item; int i = 0; while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) { diff --git a/unix/moduselect.c b/unix/moduselect.c index f966efa41..76938329f 100644 --- a/unix/moduselect.c +++ b/unix/moduselect.c @@ -288,7 +288,7 @@ STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table); STATIC const mp_obj_type_t mp_type_poll = { { &mp_type_type }, .name = MP_QSTR_poll, - .getiter = mp_identity, + .getiter = mp_identity_getiter, .iternext = poll_iternext, .locals_dict = (void*)&poll_locals_dict, }; -- cgit v1.2.3 From 0982884655a2b8619f8334b5fdb9f2e1f59670b4 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 24 Feb 2017 10:04:23 -0500 Subject: extmod/modurandom: Use mp_raise_ValueError(). For the standard unix x86_64 build, this saves 11 bytes on object file level, but no difference in executable size due to (bloaty) code alignment. --- extmod/modurandom.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'extmod') diff --git a/extmod/modurandom.c b/extmod/modurandom.c index 12e56741e..4b63dace4 100644 --- a/extmod/modurandom.c +++ b/extmod/modurandom.c @@ -74,7 +74,7 @@ STATIC uint32_t yasmarang_randbelow(uint32_t n) { STATIC mp_obj_t mod_urandom_getrandbits(mp_obj_t num_in) { int n = mp_obj_get_int(num_in); if (n > 32 || n == 0) { - nlr_raise(mp_obj_new_exception(&mp_type_ValueError)); + mp_raise_ValueError(NULL); } uint32_t mask = ~0; // Beware of C undefined behavior when shifting by >= than bit size @@ -102,7 +102,7 @@ STATIC mp_obj_t mod_urandom_randrange(size_t n_args, const mp_obj_t *args) { if (start > 0) { return mp_obj_new_int(yasmarang_randbelow(start)); } else { - nlr_raise(mp_obj_new_exception(&mp_type_ValueError)); + goto error; } } else { mp_int_t stop = mp_obj_get_int(args[1]); @@ -111,7 +111,7 @@ STATIC mp_obj_t mod_urandom_randrange(size_t n_args, const mp_obj_t *args) { if (start < stop) { return mp_obj_new_int(start + yasmarang_randbelow(stop - start)); } else { - nlr_raise(mp_obj_new_exception(&mp_type_ValueError)); + goto error; } } else { // range(start, stop, step) @@ -122,15 +122,18 @@ STATIC mp_obj_t mod_urandom_randrange(size_t n_args, const mp_obj_t *args) { } else if (step < 0) { n = (stop - start + step + 1) / step; } else { - nlr_raise(mp_obj_new_exception(&mp_type_ValueError)); + goto error; } if (n > 0) { return mp_obj_new_int(start + step * yasmarang_randbelow(n)); } else { - nlr_raise(mp_obj_new_exception(&mp_type_ValueError)); + goto error; } } } + +error: + mp_raise_ValueError(NULL); } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_urandom_randrange_obj, 1, 3, mod_urandom_randrange); @@ -140,7 +143,7 @@ STATIC mp_obj_t mod_urandom_randint(mp_obj_t a_in, mp_obj_t b_in) { if (a <= b) { return mp_obj_new_int(a + yasmarang_randbelow(b - a + 1)); } else { - nlr_raise(mp_obj_new_exception(&mp_type_ValueError)); + mp_raise_ValueError(NULL); } } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_urandom_randint_obj, mod_urandom_randint); -- cgit v1.2.3 From 830ce74f324d3a384e383e840de4ee229c41ba36 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 7 Mar 2017 09:34:09 +0100 Subject: extmod/modutimeq: Make scheduling fair (round-robin). By adding back monotonically increasing field in addition to time field. As heapsort is not stable, without this, among entried added and readded at the same time instant, some might be always selected, and some might never be selected, leading to scheduling starvation. --- extmod/modutimeq.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'extmod') diff --git a/extmod/modutimeq.c b/extmod/modutimeq.c index f73b39103..1d7425bd7 100644 --- a/extmod/modutimeq.c +++ b/extmod/modutimeq.c @@ -4,7 +4,7 @@ * The MIT License (MIT) * * Copyright (c) 2014 Damien P. George - * Copyright (c) 2016 Paul Sokolovsky + * Copyright (c) 2016-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 @@ -43,6 +43,7 @@ struct qentry { mp_uint_t time; + mp_uint_t id; mp_obj_t callback; mp_obj_t args; }; @@ -54,6 +55,7 @@ typedef struct _mp_obj_utimeq_t { struct qentry items[]; } mp_obj_utimeq_t; +STATIC mp_uint_t utimeq_id; STATIC mp_obj_utimeq_t *get_heap(mp_obj_t heap_in) { return MP_OBJ_TO_PTR(heap_in); @@ -63,6 +65,11 @@ STATIC bool time_less_than(struct qentry *item, struct qentry *parent) { mp_uint_t item_tm = item->time; mp_uint_t parent_tm = parent->time; mp_uint_t res = parent_tm - item_tm; + if (res == 0) { + // TODO: This actually should use the same "ring" logic + // as for time, to avoid artifacts when id's overflow. + return item->id < parent->id; + } if ((mp_int_t)res < 0) { res += MODULO; } @@ -125,6 +132,7 @@ STATIC mp_obj_t mod_utimeq_heappush(size_t n_args, const mp_obj_t *args) { } mp_uint_t l = heap->len; heap->items[l].time = MP_OBJ_SMALL_INT_VALUE(args[1]); + heap->items[l].id = utimeq_id++; heap->items[l].callback = args[2]; heap->items[l].args = args[3]; heap_siftdown(heap, 0, heap->len); -- cgit v1.2.3 From 12d0731b91d8e58ba20ec28adf2d6c1aa995d74a Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 10 Mar 2017 19:09:42 +1100 Subject: extmod/vfs_fat: Remove obsolete and unused str/len members. --- cc3200/mptask.c | 2 -- extmod/vfs_fat.c | 2 -- extmod/vfs_fat.h | 2 -- stmhal/main.c | 4 ---- 4 files changed, 10 deletions(-) (limited to 'extmod') diff --git a/cc3200/mptask.c b/cc3200/mptask.c index 41264fbd0..3c49a5603 100644 --- a/cc3200/mptask.c +++ b/cc3200/mptask.c @@ -302,8 +302,6 @@ STATIC void mptask_init_sflash_filesystem (void) { // Initialise the local flash filesystem. // init the vfs object fs_user_mount_t *vfs_fat = sflash_vfs_fat; - vfs_fat->str = NULL; - vfs_fat->len = 0; vfs_fat->flags = 0; pyb_flash_init_vfs(vfs_fat); diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index 82dd312b8..8cd5a4674 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -55,8 +55,6 @@ STATIC mp_obj_t fat_vfs_make_new(const mp_obj_type_t *type, size_t n_args, size_ 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 diff --git a/extmod/vfs_fat.h b/extmod/vfs_fat.h index a5e3c604b..7eb865254 100644 --- a/extmod/vfs_fat.h +++ b/extmod/vfs_fat.h @@ -36,8 +36,6 @@ 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]; diff --git a/stmhal/main.c b/stmhal/main.c index 3c9906ad2..8d076a08b 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -167,8 +167,6 @@ static const char fresh_readme_txt[] = MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) { // init the vfs object fs_user_mount_t *vfs_fat = &fs_user_mount_flash; - vfs_fat->str = NULL; - vfs_fat->len = 0; vfs_fat->flags = 0; pyb_flash_init_vfs(vfs_fat); @@ -274,8 +272,6 @@ STATIC bool init_sdcard_fs(bool first_soft_reset) { if (vfs == NULL || vfs_fat == NULL) { break; } - vfs_fat->str = NULL; - vfs_fat->len = 0; vfs_fat->flags = FSUSER_FREE_OBJ; sdcard_init_vfs(vfs_fat, part_num); -- cgit v1.2.3 From 643876fb77d45540e5b82f450a7907f39ec95c6a Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 13 Mar 2017 21:23:31 +1100 Subject: extmod/vfs_fat: Allow to compile with MICROPY_VFS_FAT disabled. Some ports may want to compile with generic MICROPY_VFS support but without the VfsFat class. This patch allows such a thing. --- extmod/vfs_fat_diskio.c | 4 ++-- extmod/vfs_fat_file.c | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) (limited to 'extmod') diff --git a/extmod/vfs_fat_diskio.c b/extmod/vfs_fat_diskio.c index 7efcc22f2..24c00ffba 100644 --- a/extmod/vfs_fat_diskio.c +++ b/extmod/vfs_fat_diskio.c @@ -28,7 +28,7 @@ */ #include "py/mpconfig.h" -#if MICROPY_VFS +#if MICROPY_VFS && MICROPY_VFS_FAT #include #include @@ -277,4 +277,4 @@ DRESULT disk_ioctl ( } } -#endif // MICROPY_VFS +#endif // MICROPY_VFS && MICROPY_VFS_FAT diff --git a/extmod/vfs_fat_file.c b/extmod/vfs_fat_file.c index 6263492cb..edffa37c7 100644 --- a/extmod/vfs_fat_file.c +++ b/extmod/vfs_fat_file.c @@ -25,7 +25,7 @@ */ #include "py/mpconfig.h" -#if MICROPY_VFS +#if MICROPY_VFS && MICROPY_VFS_FAT #include #include @@ -37,10 +37,8 @@ #include "lib/oofatfs/ff.h" #include "extmod/vfs_fat.h" -#if MICROPY_VFS_FAT #define mp_type_fileio fatfs_type_fileio #define mp_type_textio fatfs_type_textio -#endif extern const mp_obj_type_t mp_type_fileio; extern const mp_obj_type_t mp_type_textio; @@ -300,4 +298,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 +#endif // MICROPY_VFS && MICROPY_VFS_FAT -- cgit v1.2.3 From 0a3ac07ec79acf580ccf3c6188f25cde2f77bbe5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 13 Mar 2017 21:37:21 +1100 Subject: extmod/vfs: Rewrite path lookup algo to support relative paths from root. For example, if the current directory is the root dir then this patch allows one to do uos.listdir('mnt'), where 'mnt' is a valid mount point. Previous to this patch such a thing would not work, on needed to do uos.listdir('/mnt') instead. --- extmod/vfs.c | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) (limited to 'extmod') diff --git a/extmod/vfs.c b/extmod/vfs.c index c968345b8..3ab8261dd 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -43,33 +43,38 @@ // 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 MP_VFS_ROOT; - } else if (MP_STATE_VM(vfs_cur) == MP_VFS_ROOT) { - // in root dir - if (path[0] == 0) { - return MP_VFS_ROOT; + if (*path == '/' || MP_STATE_VM(vfs_cur) == MP_VFS_ROOT) { + // an absolute path, or the current volume is root, so search root dir + bool is_abs = 0; + if (*path == '/') { + ++path; + is_abs = 1; } - } else if (*path != '/') { - // a relative path within a mounted device - *path_out = path; - return MP_STATE_VM(vfs_cur); - } - - 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; - return vfs; - } else if (path[vfs->len] == '\0') { - *path_out = "/"; - return vfs; + for (mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) { + size_t len = vfs->len - 1; + if (strncmp(path, vfs->str + 1, len) == 0) { + if (path[len] == '/') { + *path_out = path + len; + return vfs; + } else if (path[len] == '\0') { + *path_out = "/"; + return vfs; + } } } + if (*path == '\0') { + // path was "" or "/" so return virtual root + return MP_VFS_ROOT; + } + if (is_abs) { + // path began with / and was not found + return MP_VFS_NONE; + } } - // mount point not found - return MP_VFS_NONE; + // a relative path within a mounted device + *path_out = path; + return MP_STATE_VM(vfs_cur); } // Version of mp_vfs_lookup_path that takes and returns uPy string objects. -- cgit v1.2.3 From 1831034be13fef5344583c557ff089df31788251 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 14 Mar 2017 11:16:31 +1100 Subject: py: Allow lexer to raise exceptions during construction. This patch refactors the error handling in the lexer, to simplify it (ie reduce code size). A long time ago, when the lexer/parser/compiler were first written, the lexer and parser were designed so they didn't use exceptions (ie nlr) to report errors but rather returned an error code. Over time that has gradually changed, the parser in particular has more and more ways of raising exceptions. Also, the lexer never really handled all errors without raising, eg there were some memory errors which could raise an exception (and in these rare cases one would get a fatal nlr-not-handled fault). This patch accepts the fact that the lexer can raise exceptions in some cases and allows it to raise exceptions to handle all its errors, which are for the most part just out-of-memory errors during construction of the lexer. This makes the lexer a bit simpler, and also the persistent code stuff is simplified. What this means for users of the lexer is that calls to it must be wrapped in a nlr handler. But all uses of the lexer already have such an nlr handler for the parser (and compiler) so that doesn't put any extra burden on the callers. --- extmod/vfs_reader.c | 29 +++++++++-------------------- py/builtinevex.c | 3 --- py/builtinimport.c | 17 +++-------------- py/lexer.c | 31 +++++-------------------------- py/persistentcode.c | 9 ++------- py/reader.c | 28 +++++++++------------------- py/reader.h | 6 +++--- 7 files changed, 31 insertions(+), 92 deletions(-) (limited to 'extmod') diff --git a/extmod/vfs_reader.c b/extmod/vfs_reader.c index 9509582d2..891098aa1 100644 --- a/extmod/vfs_reader.c +++ b/extmod/vfs_reader.c @@ -27,7 +27,7 @@ #include #include -#include "py/nlr.h" +#include "py/runtime.h" #include "py/stream.h" #include "py/reader.h" #include "extmod/vfs.h" @@ -69,30 +69,19 @@ STATIC void mp_reader_vfs_close(void *data) { 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); - nlr_pop(); - if (errcode != 0) { - return errcode; - } - } else { - return MP_ENOENT; // assume error was "file not found" +void mp_reader_new_file(mp_reader_t *reader, const char *filename) { + mp_reader_vfs_t *rf = m_new_obj(mp_reader_vfs_t); + 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) { + mp_raise_OSError(errcode); } 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/builtinevex.c b/py/builtinevex.c index 636f86930..b0514ee99 100644 --- a/py/builtinevex.c +++ b/py/builtinevex.c @@ -136,9 +136,6 @@ STATIC mp_obj_t eval_exec_helper(size_t n_args, const mp_obj_t *args, mp_parse_i mp_lexer_t *lex; if (MICROPY_PY_BUILTINS_EXECFILE && parse_input_kind == MP_PARSE_SINGLE_INPUT) { lex = mp_lexer_new_from_file(str); - if (lex == NULL) { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "could not open file '%s'", str)); - } parse_input_kind = MP_PARSE_FILE_INPUT; } else { lex = mp_lexer_new_from_str_len(MP_QSTR__lt_string_gt_, str, str_len, 0); diff --git a/py/builtinimport.c b/py/builtinimport.c index 0a917c6f8..96846b963 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -131,18 +131,7 @@ STATIC mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *d } #if MICROPY_ENABLE_COMPILER -STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex, const char *fname) { - - if (lex == NULL) { - // we verified the file exists using stat, but lexer could still fail - if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) { - mp_raise_msg(&mp_type_ImportError, "module not found"); - } else { - nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, - "no module named '%s'", fname)); - } - } - +STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex) { #if MICROPY_PY___FILE__ qstr source_name = lex->source_name; mp_store_attr(module_obj, MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name)); @@ -207,7 +196,7 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) { // found the filename in the list of frozen files, then load and execute it. #if MICROPY_MODULE_FROZEN_STR if (frozen_type == MP_FROZEN_STR) { - do_load_from_lexer(module_obj, modref, file_str); + do_load_from_lexer(module_obj, modref); return; } #endif @@ -235,7 +224,7 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) { #if MICROPY_ENABLE_COMPILER { mp_lexer_t *lex = mp_lexer_new_from_file(file_str); - do_load_from_lexer(module_obj, lex, file_str); + do_load_from_lexer(module_obj, lex); return; } #endif diff --git a/py/lexer.c b/py/lexer.c index 9dcdd19eb..fadaee6f3 100644 --- a/py/lexer.c +++ b/py/lexer.c @@ -699,13 +699,7 @@ void mp_lexer_to_next(mp_lexer_t *lex) { } mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader) { - mp_lexer_t *lex = m_new_obj_maybe(mp_lexer_t); - - // check for memory allocation error - if (lex == NULL) { - reader.close(reader.data); - return NULL; - } + mp_lexer_t *lex = m_new_obj(mp_lexer_t); lex->source_name = src_name; lex->reader = reader; @@ -715,16 +709,9 @@ mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader) { lex->nested_bracket_level = 0; lex->alloc_indent_level = MICROPY_ALLOC_LEXER_INDENT_INIT; lex->num_indent_level = 1; - lex->indent_level = m_new_maybe(uint16_t, lex->alloc_indent_level); + lex->indent_level = m_new(uint16_t, lex->alloc_indent_level); vstr_init(&lex->vstr, 32); - // check for memory allocation error - // note: vstr_init above may fail on malloc, but so may mp_lexer_to_next below - if (lex->indent_level == NULL) { - mp_lexer_free(lex); - return NULL; - } - // store sentinel for first indentation level lex->indent_level[0] = 0; @@ -764,9 +751,7 @@ mp_lexer_t *mp_lexer_new(qstr src_name, mp_reader_t reader) { mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, size_t len, size_t free_len) { mp_reader_t reader; - if (!mp_reader_new_mem(&reader, (const byte*)str, len, free_len)) { - return NULL; - } + mp_reader_new_mem(&reader, (const byte*)str, len, free_len); return mp_lexer_new(src_name, reader); } @@ -774,10 +759,7 @@ mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, size_t len mp_lexer_t *mp_lexer_new_from_file(const char *filename) { mp_reader_t reader; - int ret = mp_reader_new_file(&reader, filename); - if (ret != 0) { - return NULL; - } + mp_reader_new_file(&reader, filename); return mp_lexer_new(qstr_from_str(filename), reader); } @@ -785,10 +767,7 @@ mp_lexer_t *mp_lexer_new_from_file(const char *filename) { mp_lexer_t *mp_lexer_new_from_fd(qstr filename, int fd, bool close_fd) { mp_reader_t reader; - int ret = mp_reader_new_file_from_fd(&reader, fd, close_fd); - if (ret != 0) { - return NULL; - } + mp_reader_new_file_from_fd(&reader, fd, close_fd); return mp_lexer_new(filename, reader); } diff --git a/py/persistentcode.c b/py/persistentcode.c index 5cb511709..2a9a5b7cc 100644 --- a/py/persistentcode.c +++ b/py/persistentcode.c @@ -225,18 +225,13 @@ mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader) { mp_raw_code_t *mp_raw_code_load_mem(const byte *buf, size_t len) { mp_reader_t reader; - if (!mp_reader_new_mem(&reader, buf, len, 0)) { - m_malloc_fail(BYTES_PER_WORD); // we need to raise a MemoryError - } + mp_reader_new_mem(&reader, buf, len, 0); return mp_raw_code_load(&reader); } mp_raw_code_t *mp_raw_code_load_file(const char *filename) { mp_reader_t reader; - int ret = mp_reader_new_file(&reader, filename); - if (ret != 0) { - mp_raise_OSError(ret); - } + mp_reader_new_file(&reader, filename); return mp_raw_code_load(&reader); } diff --git a/py/reader.c b/py/reader.c index d7de7aa6c..5df45c495 100644 --- a/py/reader.c +++ b/py/reader.c @@ -27,6 +27,7 @@ #include #include +#include "py/runtime.h" #include "py/mperrno.h" #include "py/reader.h" @@ -54,11 +55,8 @@ STATIC void mp_reader_mem_close(void *data) { m_del_obj(mp_reader_mem_t, reader); } -bool mp_reader_new_mem(mp_reader_t *reader, const byte *buf, size_t len, size_t free_len) { - mp_reader_mem_t *rm = m_new_obj_maybe(mp_reader_mem_t); - if (rm == NULL) { - return false; - } +void mp_reader_new_mem(mp_reader_t *reader, const byte *buf, size_t len, size_t free_len) { + mp_reader_mem_t *rm = m_new_obj(mp_reader_mem_t); rm->free_len = free_len; rm->beg = buf; rm->cur = buf; @@ -66,7 +64,6 @@ bool mp_reader_new_mem(mp_reader_t *reader, const byte *buf, size_t len, size_t reader->data = rm; reader->readbyte = mp_reader_mem_readbyte; reader->close = mp_reader_mem_close; - return true; } #if MICROPY_READER_POSIX @@ -110,14 +107,8 @@ STATIC void mp_reader_posix_close(void *data) { m_del_obj(mp_reader_posix_t, reader); } -int mp_reader_new_file_from_fd(mp_reader_t *reader, int fd, bool close_fd) { - mp_reader_posix_t *rp = m_new_obj_maybe(mp_reader_posix_t); - if (rp == NULL) { - if (close_fd) { - close(fd); - } - return MP_ENOMEM; - } +void mp_reader_new_file_from_fd(mp_reader_t *reader, int fd, bool close_fd) { + mp_reader_posix_t *rp = m_new_obj(mp_reader_posix_t); rp->close_fd = close_fd; rp->fd = fd; int n = read(rp->fd, rp->buf, sizeof(rp->buf)); @@ -125,22 +116,21 @@ int mp_reader_new_file_from_fd(mp_reader_t *reader, int fd, bool close_fd) { if (close_fd) { close(fd); } - return errno; + mp_raise_OSError(errno); } rp->len = n; rp->pos = 0; reader->data = rp; reader->readbyte = mp_reader_posix_readbyte; reader->close = mp_reader_posix_close; - return 0; // success } -int mp_reader_new_file(mp_reader_t *reader, const char *filename) { +void mp_reader_new_file(mp_reader_t *reader, const char *filename) { int fd = open(filename, O_RDONLY, 0644); if (fd < 0) { - return errno; + mp_raise_OSError(errno); } - return mp_reader_new_file_from_fd(reader, fd, true); + mp_reader_new_file_from_fd(reader, fd, true); } #endif diff --git a/py/reader.h b/py/reader.h index b02d96149..8511c72ce 100644 --- a/py/reader.h +++ b/py/reader.h @@ -39,8 +39,8 @@ typedef struct _mp_reader_t { void (*close)(void *data); } mp_reader_t; -bool mp_reader_new_mem(mp_reader_t *reader, const byte *buf, size_t len, size_t free_len); -int mp_reader_new_file(mp_reader_t *reader, const char *filename); -int mp_reader_new_file_from_fd(mp_reader_t *reader, int fd, bool close_fd); +void mp_reader_new_mem(mp_reader_t *reader, const byte *buf, size_t len, size_t free_len); +void mp_reader_new_file(mp_reader_t *reader, const char *filename); +void mp_reader_new_file_from_fd(mp_reader_t *reader, int fd, bool close_fd); #endif // MICROPY_INCLUDED_PY_READER_H -- cgit v1.2.3 From 231cfc84a7cae31f93208c334fc33b08278040eb Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Mon, 6 Feb 2017 10:59:44 +0000 Subject: extmod/modframebuf: Add support for monochrome horizontal format. MHLSB and MHMSB formats are added to the framebuf module, which have 8 adjacent horizontal pixels represented in a single byte. --- extmod/modframebuf.c | 48 ++++++++++-- tests/extmod/framebuf1.py | 171 ++++++++++++++++++++++-------------------- tests/extmod/framebuf1.py.exp | 46 ++++++++++++ 3 files changed, 179 insertions(+), 86 deletions(-) (limited to 'extmod') diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index 792a3a7fa..33985dd00 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -53,6 +53,41 @@ typedef struct _mp_framebuf_p_t { fill_rect_t fill_rect; } mp_framebuf_p_t; +// constants for formats +#define FRAMEBUF_MVLSB (0) +#define FRAMEBUF_RGB565 (1) +#define FRAMEBUF_GS4_HMSB (2) +#define FRAMEBUF_MHLSB (3) +#define FRAMEBUF_MHMSB (4) + +// Functions for MHLSB and MHMSB + +STATIC void mono_horiz_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t color) { + size_t index = (x + y * fb->stride) >> 3; + int offset = fb->format == FRAMEBUF_MHMSB ? x & 0x07 : 7 - (x & 0x07); + ((uint8_t*)fb->buf)[index] = (((uint8_t*)fb->buf)[index] & ~(0x01 << offset)) | ((color != 0) << offset); +} + +STATIC uint32_t mono_horiz_getpixel(const mp_obj_framebuf_t *fb, int x, int y) { + size_t index = (x + y * fb->stride) >> 3; + int offset = fb->format == FRAMEBUF_MHMSB ? x & 0x07 : 7 - (x & 0x07); + return (((uint8_t*)fb->buf)[index] >> (offset)) & 0x01; +} + +STATIC void mono_horiz_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, int h, uint32_t col) { + int reverse = fb->format == FRAMEBUF_MHMSB; + int advance = fb->stride >> 3; + while (w--) { + uint8_t *b = &((uint8_t*)fb->buf)[(x >> 3) + y * advance]; + int offset = reverse ? x & 7 : 7 - (x & 7); + for (int hh = h; hh; --hh) { + *b = (*b & ~(0x01 << offset)) | ((col != 0) << offset); + b += advance; + } + ++x; + } +} + // Functions for MVLSB format STATIC void mvlsb_setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t color) { @@ -148,15 +183,12 @@ STATIC void gs4_hmsb_fill_rect(const mp_obj_framebuf_t *fb, int x, int y, int w, } } -// constants for formats -#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}, + [FRAMEBUF_MHLSB] = {mono_horiz_setpixel, mono_horiz_getpixel, mono_horiz_fill_rect}, + [FRAMEBUF_MHMSB] = {mono_horiz_setpixel, mono_horiz_getpixel, mono_horiz_fill_rect}, }; static inline void setpixel(const mp_obj_framebuf_t *fb, int x, int y, uint32_t color) { @@ -207,6 +239,10 @@ STATIC mp_obj_t framebuf_make_new(const mp_obj_type_t *type, size_t n_args, size case FRAMEBUF_RGB565: case FRAMEBUF_GS4_HMSB: break; + case FRAMEBUF_MHLSB: + case FRAMEBUF_MHMSB: + o->stride = (o->stride + 7) & ~7; + break; default: nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "invalid format")); @@ -545,6 +581,8 @@ STATIC const mp_rom_map_elem_t framebuf_module_globals_table[] = { { 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) }, + { MP_ROM_QSTR(MP_QSTR_MHLSB), MP_OBJ_NEW_SMALL_INT(FRAMEBUF_MHLSB) }, + { MP_ROM_QSTR(MP_QSTR_MHMSB), MP_OBJ_NEW_SMALL_INT(FRAMEBUF_MHMSB) }, }; 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 c204e63aa..0a8e1ae55 100644 --- a/tests/extmod/framebuf1.py +++ b/tests/extmod/framebuf1.py @@ -7,87 +7,96 @@ except ImportError: w = 5 h = 16 -buf = bytearray(w * h // 8) -fbuf = framebuf.FrameBuffer(buf, w, h, framebuf.MVLSB) - -# access as buffer -print(memoryview(fbuf)[0]) - -# fill -fbuf.fill(1) -print(buf) -fbuf.fill(0) -print(buf) - -# put pixel -fbuf.pixel(0, 0, 1) -fbuf.pixel(4, 0, 1) -fbuf.pixel(0, 15, 1) -fbuf.pixel(4, 15, 1) -print(buf) - -# clear pixel -fbuf.pixel(4, 15, 0) -print(buf) - -# get pixel -print(fbuf.pixel(0, 0), fbuf.pixel(1, 1)) - -# hline -fbuf.fill(0) -fbuf.hline(0, 1, w, 1) -print('hline', buf) - -# vline -fbuf.fill(0) -fbuf.vline(1, 0, h, 1) -print('vline', buf) - -# rect -fbuf.fill(0) -fbuf.rect(1, 1, 3, 3, 1) -print('rect', buf) - -#fill rect -fbuf.fill(0) -fbuf.fill_rect(0, 0, 0, 3, 1) # zero width, no-operation -fbuf.fill_rect(1, 1, 3, 3, 1) -print('fill_rect', buf) - -# line -fbuf.fill(0) -fbuf.line(1, 1, 3, 3, 1) -print('line', buf) - -# line steep negative gradient -fbuf.fill(0) -fbuf.line(3, 3, 2, 1, 1) -print('line', buf) - -# scroll -fbuf.fill(0) -fbuf.pixel(2, 7, 1) -fbuf.scroll(0, 1) -print(buf) -fbuf.scroll(0, -2) -print(buf) -fbuf.scroll(1, 0) -print(buf) -fbuf.scroll(-1, 0) -print(buf) -fbuf.scroll(2, 2) -print(buf) - -# print text -fbuf.fill(0) -fbuf.text("hello", 0, 0, 1) -print(buf) -fbuf.text("hello", 0, 0, 0) # clear -print(buf) - -# char out of font range set to chr(127) -fbuf.text(str(chr(31)), 0, 0) -print(buf) +size = w * h // 8 +buf = bytearray(size) +maps = {framebuf.MVLSB : 'MVLSB', + framebuf.MHLSB : 'MHLSB', + framebuf.MHMSB : 'MHMSB'} + +for mapping in maps.keys(): + for x in range(size): + buf[x] = 0 + fbuf = framebuf.FrameBuffer(buf, w, h, mapping) + print(maps[mapping]) + # access as buffer + print(memoryview(fbuf)[0]) + + # fill + fbuf.fill(1) + print(buf) + fbuf.fill(0) + print(buf) + + # put pixel + fbuf.pixel(0, 0, 1) + fbuf.pixel(4, 0, 1) + fbuf.pixel(0, 15, 1) + fbuf.pixel(4, 15, 1) + print(buf) + + # clear pixel + fbuf.pixel(4, 15, 0) + print(buf) + + # get pixel + print(fbuf.pixel(0, 0), fbuf.pixel(1, 1)) + + # hline + fbuf.fill(0) + fbuf.hline(0, 1, w, 1) + print('hline', buf) + + # vline + fbuf.fill(0) + fbuf.vline(1, 0, h, 1) + print('vline', buf) + + # rect + fbuf.fill(0) + fbuf.rect(1, 1, 3, 3, 1) + print('rect', buf) + + #fill rect + fbuf.fill(0) + fbuf.fill_rect(0, 0, 0, 3, 1) # zero width, no-operation + fbuf.fill_rect(1, 1, 3, 3, 1) + print('fill_rect', buf) + + # line + fbuf.fill(0) + fbuf.line(1, 1, 3, 3, 1) + print('line', buf) + + # line steep negative gradient + fbuf.fill(0) + fbuf.line(3, 3, 2, 1, 1) + print('line', buf) + + # scroll + fbuf.fill(0) + fbuf.pixel(2, 7, 1) + fbuf.scroll(0, 1) + print(buf) + fbuf.scroll(0, -2) + print(buf) + fbuf.scroll(1, 0) + print(buf) + fbuf.scroll(-1, 0) + print(buf) + fbuf.scroll(2, 2) + print(buf) + + # print text + fbuf.fill(0) + fbuf.text("hello", 0, 0, 1) + print(buf) + fbuf.text("hello", 0, 0, 0) # clear + print(buf) + + # char out of font range set to chr(127) + fbuf.text(str(chr(31)), 0, 0) + print(buf) + print() # test invalid constructor, and stride argument try: diff --git a/tests/extmod/framebuf1.py.exp b/tests/extmod/framebuf1.py.exp index 83d775d3c..736ad7a45 100644 --- a/tests/extmod/framebuf1.py.exp +++ b/tests/extmod/framebuf1.py.exp @@ -1,3 +1,4 @@ +MVLSB 0 bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff') bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') @@ -18,4 +19,49 @@ bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01') bytearray(b'\x00\x7f\x7f\x04\x04\x00\x00\x00\x00\x00') bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') bytearray(b'\xaaU\xaaU\xaa\x00\x00\x00\x00\x00') + +MHLSB +0 +bytearray(b'\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x88\x00\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x88\x00\x00\x00\x00\x00\x00\x00\x00\x00') +1 0 +hline bytearray(b'\x00\xf8\x00\x00\x00\x00\x00\x00\x00\x00') +vline bytearray(b'@@@@@@@@@@') +rect bytearray(b'\x00pPp\x00\x00\x00\x00\x00\x00') +fill_rect bytearray(b'\x00ppp\x00\x00\x00\x00\x00\x00') +line bytearray(b'\x00@ \x10\x00\x00\x00\x00\x00\x00') +line bytearray(b'\x00 \x10\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00 \x00') +bytearray(b'\x00\x00\x00\x00\x00\x00 \x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00 \x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00') +bytearray(b'``x````\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'P\xa8P\xa8P\xa8P\xa8\x00\x00') + +MHMSB +0 +bytearray(b'\x1f\x1f\x1f\x1f\x1f\x1f\x1f\x1f\x1f\x1f') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x11\x00\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\x11\x00\x00\x00\x00\x00\x00\x00\x00\x00') +1 0 +hline bytearray(b'\x00\x1f\x00\x00\x00\x00\x00\x00\x00\x00') +vline bytearray(b'\x02\x02\x02\x02\x02\x02\x02\x02\x02\x02') +rect bytearray(b'\x00\x0e\n\x0e\x00\x00\x00\x00\x00\x00') +fill_rect bytearray(b'\x00\x0e\x0e\x0e\x00\x00\x00\x00\x00\x00') +line bytearray(b'\x00\x02\x04\x08\x00\x00\x00\x00\x00\x00') +line bytearray(b'\x00\x04\x04\x08\x00\x00\x00\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00') +bytearray(b'\x06\x06\x1e\x06\x06\x06\x06\x00\x00\x00') +bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +bytearray(b'\n\x15\n\x15\n\x15\n\x15\x00\x00') + ValueError -- cgit v1.2.3 From 2e3fc778094c0c31c3e15e196e1c7b3b838239fe Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 22 Mar 2017 12:49:21 +1100 Subject: extmod/utime_mphal: Don't exit/enter the GIL in generic sleep functions. GIL behaviour should be handled by the port. And ports probably want to define sleep_us so that it doesn't release the GIL, to improve timing accuracy. --- extmod/utime_mphal.c | 6 ------ 1 file changed, 6 deletions(-) (limited to 'extmod') diff --git a/extmod/utime_mphal.c b/extmod/utime_mphal.c index f447b3a68..e99ba46ce 100644 --- a/extmod/utime_mphal.c +++ b/extmod/utime_mphal.c @@ -37,13 +37,11 @@ #include "extmod/utime_mphal.h" STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) { - MP_THREAD_GIL_EXIT(); #if MICROPY_PY_BUILTINS_FLOAT mp_hal_delay_ms(1000 * mp_obj_get_float(seconds_o)); #else mp_hal_delay_ms(1000 * mp_obj_get_int(seconds_o)); #endif - MP_THREAD_GIL_ENTER(); return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_obj, time_sleep); @@ -51,9 +49,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_obj, time_sleep); STATIC mp_obj_t time_sleep_ms(mp_obj_t arg) { mp_int_t ms = mp_obj_get_int(arg); if (ms > 0) { - MP_THREAD_GIL_EXIT(); mp_hal_delay_ms(ms); - MP_THREAD_GIL_ENTER(); } return mp_const_none; } @@ -62,9 +58,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_utime_sleep_ms_obj, time_sleep_ms); STATIC mp_obj_t time_sleep_us(mp_obj_t arg) { mp_int_t us = mp_obj_get_int(arg); if (us > 0) { - MP_THREAD_GIL_EXIT(); mp_hal_delay_us(us); - MP_THREAD_GIL_ENTER(); } return mp_const_none; } -- cgit v1.2.3 From b568448306910d7526a5f1085eca8b33f36694b3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 26 Mar 2017 19:19:35 +1100 Subject: extmod/modlwip: Use mp_obj_str_get_str instead of mp_obj_str_get_data. --- extmod/modlwip.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'extmod') diff --git a/extmod/modlwip.c b/extmod/modlwip.c index 5b03d7c29..5709413de 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -1264,8 +1264,7 @@ STATIC void lwip_getaddrinfo_cb(const char *name, ip_addr_t *ipaddr, void *arg) // lwip.getaddrinfo STATIC mp_obj_t lwip_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) { - mp_uint_t hlen; - const char *host = mp_obj_str_get_data(host_in, &hlen); + const char *host = mp_obj_str_get_str(host_in); mp_int_t port = mp_obj_get_int(port_in); getaddrinfo_state_t state; -- cgit v1.2.3 From f7816188b701552036c9f3f2c6d1eb06462087d1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 29 Mar 2017 12:53:35 +1100 Subject: extmod/vfs_fat: Fix calculation of total blocks in statvfs. --- 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 8cd5a4674..dee4f9298 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -268,7 +268,7 @@ STATIC mp_obj_t fat_vfs_statvfs(mp_obj_t vfs_in, mp_obj_t path_in) { 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[2] = MP_OBJ_NEW_SMALL_INT((fatfs->n_fatent - 2)); // f_blocks t->items[3] = MP_OBJ_NEW_SMALL_INT(nclst); // f_bfree t->items[4] = t->items[3]; // f_bavail t->items[5] = MP_OBJ_NEW_SMALL_INT(0); // f_files -- cgit v1.2.3 From a8a3ab48da5a151a2796a0303e3741d3c4f24f96 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 26 Mar 2017 17:19:08 +1100 Subject: extmod/moduselect: Update to use size_t for array accessor. --- extmod/moduselect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'extmod') diff --git a/extmod/moduselect.c b/extmod/moduselect.c index 5b00f6bad..46dbe42e1 100644 --- a/extmod/moduselect.c +++ b/extmod/moduselect.c @@ -113,7 +113,7 @@ STATIC mp_uint_t poll_map_poll(mp_map_t *poll_map, mp_uint_t *rwx_num) { /// \function select(rlist, wlist, xlist[, timeout]) STATIC mp_obj_t select_select(uint n_args, const mp_obj_t *args) { // get array data from tuple/list arguments - mp_uint_t rwx_len[3]; + size_t rwx_len[3]; mp_obj_t *r_array, *w_array, *x_array; mp_obj_get_array(args[0], &rwx_len[0], &r_array); mp_obj_get_array(args[1], &rwx_len[1], &w_array); -- cgit v1.2.3 From 204ded848e114208b8ecef0b683df71b77af5b5a Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 25 Mar 2017 19:48:44 +1100 Subject: extmod: Update for changes to mp_obj_str_get_data. --- extmod/modbtree.c | 44 +++++++++++--------------------------------- extmod/modujson.c | 2 +- extmod/modure.c | 4 ++-- extmod/modussl_mbedtls.c | 4 ++-- extmod/modwebrepl.c | 2 +- extmod/vfs.c | 4 ++-- 6 files changed, 19 insertions(+), 41 deletions(-) (limited to 'extmod') diff --git a/extmod/modbtree.c b/extmod/modbtree.c index 27f700eea..127dd71a3 100644 --- a/extmod/modbtree.c +++ b/extmod/modbtree.c @@ -97,12 +97,8 @@ STATIC mp_obj_t btree_put(size_t n_args, const mp_obj_t *args) { (void)n_args; mp_obj_btree_t *self = MP_OBJ_TO_PTR(args[0]); DBT key, val; - // Different ports may have different type sizes - mp_uint_t v; - key.data = (void*)mp_obj_str_get_data(args[1], &v); - key.size = v; - val.data = (void*)mp_obj_str_get_data(args[2], &v); - val.size = v; + key.data = (void*)mp_obj_str_get_data(args[1], &key.size); + val.data = (void*)mp_obj_str_get_data(args[2], &val.size); return MP_OBJ_NEW_SMALL_INT(__bt_put(self->db, &key, &val, 0)); } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_put_obj, 3, 4, btree_put); @@ -110,10 +106,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(btree_put_obj, 3, 4, btree_put); STATIC mp_obj_t btree_get(size_t n_args, const mp_obj_t *args) { mp_obj_btree_t *self = MP_OBJ_TO_PTR(args[0]); DBT key, val; - // Different ports may have different type sizes - mp_uint_t v; - key.data = (void*)mp_obj_str_get_data(args[1], &v); - key.size = v; + key.data = (void*)mp_obj_str_get_data(args[1], &key.size); int res = __bt_get(self->db, &key, &val, 0); if (res == RET_SPECIAL) { if (n_args > 2) { @@ -132,10 +125,7 @@ STATIC mp_obj_t btree_seq(size_t n_args, const mp_obj_t *args) { int flags = MP_OBJ_SMALL_INT_VALUE(args[1]); DBT key, val; if (n_args > 2) { - // Different ports may have different type sizes - mp_uint_t v; - key.data = (void*)mp_obj_str_get_data(args[2], &v); - key.size = v; + key.data = (void*)mp_obj_str_get_data(args[2], &key.size); } int res = __bt_seq(self->db, &key, &val, flags); @@ -206,14 +196,11 @@ STATIC mp_obj_t btree_iternext(mp_obj_t self_in) { mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in); DBT key, val; int res; - // Different ports may have different type sizes - mp_uint_t v; bool desc = self->flags & FLAG_DESC; if (self->start_key != MP_OBJ_NULL) { int flags = R_FIRST; if (self->start_key != mp_const_none) { - key.data = (void*)mp_obj_str_get_data(self->start_key, &v); - key.size = v; + key.data = (void*)mp_obj_str_get_data(self->start_key, &key.size); flags = R_CURSOR; } else if (desc) { flags = R_LAST; @@ -231,8 +218,7 @@ STATIC mp_obj_t btree_iternext(mp_obj_t self_in) { if (self->end_key != mp_const_none) { DBT end_key; - end_key.data = (void*)mp_obj_str_get_data(self->end_key, &v); - end_key.size = v; + end_key.data = (void*)mp_obj_str_get_data(self->end_key, &end_key.size); BTREE *t = self->db->internal; int cmp = t->bt_cmp(&key, &end_key); if (desc) { @@ -264,13 +250,10 @@ STATIC mp_obj_t btree_iternext(mp_obj_t self_in) { STATIC mp_obj_t btree_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { mp_obj_btree_t *self = MP_OBJ_TO_PTR(self_in); - // Different ports may have different type sizes - mp_uint_t v; if (value == MP_OBJ_NULL) { // delete DBT key; - key.data = (void*)mp_obj_str_get_data(index, &v); - key.size = v; + key.data = (void*)mp_obj_str_get_data(index, &key.size); int res = __bt_delete(self->db, &key, 0); if (res == RET_SPECIAL) { nlr_raise(mp_obj_new_exception(&mp_type_KeyError)); @@ -280,8 +263,7 @@ STATIC mp_obj_t btree_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { } else if (value == MP_OBJ_SENTINEL) { // load DBT key, val; - key.data = (void*)mp_obj_str_get_data(index, &v); - key.size = v; + key.data = (void*)mp_obj_str_get_data(index, &key.size); int res = __bt_get(self->db, &key, &val, 0); if (res == RET_SPECIAL) { nlr_raise(mp_obj_new_exception(&mp_type_KeyError)); @@ -291,10 +273,8 @@ STATIC mp_obj_t btree_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) { } else { // store DBT key, val; - key.data = (void*)mp_obj_str_get_data(index, &v); - key.size = v; - val.data = (void*)mp_obj_str_get_data(value, &v); - val.size = v; + key.data = (void*)mp_obj_str_get_data(index, &key.size); + val.data = (void*)mp_obj_str_get_data(value, &val.size); int res = __bt_put(self->db, &key, &val, 0); CHECK_ERROR(res); return mp_const_none; @@ -305,10 +285,8 @@ STATIC mp_obj_t btree_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) mp_obj_btree_t *self = MP_OBJ_TO_PTR(lhs_in); switch (op) { case MP_BINARY_OP_IN: { - mp_uint_t v; DBT key, val; - key.data = (void*)mp_obj_str_get_data(rhs_in, &v); - key.size = v; + key.data = (void*)mp_obj_str_get_data(rhs_in, &key.size); int res = __bt_get(self->db, &key, &val, 0); CHECK_ERROR(res); return mp_obj_new_bool(res != RET_SPECIAL); diff --git a/extmod/modujson.c b/extmod/modujson.c index ca4e6df10..bb2d45274 100644 --- a/extmod/modujson.c +++ b/extmod/modujson.c @@ -274,7 +274,7 @@ STATIC mp_obj_t mod_ujson_load(mp_obj_t stream_obj) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_ujson_load_obj, mod_ujson_load); STATIC mp_obj_t mod_ujson_loads(mp_obj_t obj) { - mp_uint_t len; + size_t len; const char *buf = mp_obj_str_get_data(obj, &len); vstr_t vstr = {len, len, (char*)buf, true}; mp_obj_stringio_t sio = {{&mp_type_stringio}, &vstr, 0}; diff --git a/extmod/modure.c b/extmod/modure.c index b8c242429..7be091cc0 100644 --- a/extmod/modure.c +++ b/extmod/modure.c @@ -96,7 +96,7 @@ STATIC mp_obj_t ure_exec(bool is_anchored, uint n_args, const mp_obj_t *args) { (void)n_args; mp_obj_re_t *self = MP_OBJ_TO_PTR(args[0]); Subject subj; - mp_uint_t len; + size_t len; subj.begin = mp_obj_str_get_data(args[1], &len); subj.end = subj.begin + len; int caps_num = (self->re.sub + 1) * 2; @@ -128,7 +128,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_search_obj, 2, 4, re_search); STATIC mp_obj_t re_split(size_t n_args, const mp_obj_t *args) { mp_obj_re_t *self = MP_OBJ_TO_PTR(args[0]); Subject subj; - mp_uint_t len; + size_t len; subj.begin = mp_obj_str_get_data(args[1], &len); subj.end = subj.begin + len; int caps_num = (self->re.sub + 1) * 2; diff --git a/extmod/modussl_mbedtls.c b/extmod/modussl_mbedtls.c index a7b8a1440..40dd8c049 100644 --- a/extmod/modussl_mbedtls.c +++ b/extmod/modussl_mbedtls.c @@ -156,13 +156,13 @@ STATIC mp_obj_ssl_socket_t *socket_new(mp_obj_t sock, struct ssl_args *args) { mbedtls_ssl_set_bio(&o->ssl, &o->sock, _mbedtls_ssl_send, _mbedtls_ssl_recv, NULL); if (args->key.u_obj != MP_OBJ_NULL) { - mp_uint_t key_len; + size_t key_len; const byte *key = (const byte*)mp_obj_str_get_data(args->key.u_obj, &key_len); // len should include terminating null ret = mbedtls_pk_parse_key(&o->pkey, key, key_len + 1, NULL, 0); assert(ret == 0); - mp_uint_t cert_len; + size_t cert_len; const byte *cert = (const byte*)mp_obj_str_get_data(args->cert.u_obj, &cert_len); // len should include terminating null ret = mbedtls_x509_crt_parse(&o->cert, cert, cert_len + 1); diff --git a/extmod/modwebrepl.c b/extmod/modwebrepl.c index 8e0580966..ce3c7dcbd 100644 --- a/extmod/modwebrepl.c +++ b/extmod/modwebrepl.c @@ -308,7 +308,7 @@ STATIC mp_obj_t webrepl_close(mp_obj_t self_in) { STATIC MP_DEFINE_CONST_FUN_OBJ_1(webrepl_close_obj, webrepl_close); STATIC mp_obj_t webrepl_set_password(mp_obj_t passwd_in) { - mp_uint_t len; + size_t len; const char *passwd = mp_obj_str_get_data(passwd_in, &len); if (len > sizeof(webrepl_passwd) - 1) { mp_raise_ValueError(""); diff --git a/extmod/vfs.c b/extmod/vfs.c index 3ab8261dd..e389ab324 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -134,7 +134,7 @@ mp_obj_t mp_vfs_mount(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_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; + size_t mnt_len; const char *mnt_str = mp_obj_str_get_data(pos_args[1], &mnt_len); // see if we need to auto-detect and create the filesystem @@ -180,7 +180,7 @@ 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 mp_vfs_mount_t *vfs = NULL; - mp_uint_t mnt_len; + size_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); -- cgit v1.2.3 From e9d7c3ea0ef7b726b093b1abf9cece0c9146d635 Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Fri, 24 Feb 2017 14:14:08 +0100 Subject: modutimeq: Add peektime() function (provisional). Allows to get event time for a head item in the queue. The usecase if waiting for the next event *OR* I/O completion. I/O completion may happen before event triggers, and then wait should continue for the remaining event time (or I/O completion may schedule another earlier event altogether). The new function has a strongly provisional status - it may be converted to e.g. peek() function returning all of the event fields, not just time. --- extmod/modutimeq.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'extmod') diff --git a/extmod/modutimeq.c b/extmod/modutimeq.c index 1d7425bd7..a19b3fda9 100644 --- a/extmod/modutimeq.c +++ b/extmod/modutimeq.c @@ -166,6 +166,17 @@ STATIC mp_obj_t mod_utimeq_heappop(mp_obj_t heap_in, mp_obj_t list_ref) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_utimeq_heappop_obj, mod_utimeq_heappop); +STATIC mp_obj_t mod_utimeq_peektime(mp_obj_t heap_in) { + mp_obj_utimeq_t *heap = get_heap(heap_in); + if (heap->len == 0) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_IndexError, "empty heap")); + } + + struct qentry *item = &heap->items[0]; + return MP_OBJ_NEW_SMALL_INT(item->time); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_utimeq_peektime_obj, mod_utimeq_peektime); + #if DEBUG STATIC mp_obj_t mod_utimeq_dump(mp_obj_t heap_in) { mp_obj_utimeq_t *heap = get_heap(heap_in); @@ -190,6 +201,7 @@ STATIC mp_obj_t utimeq_unary_op(mp_uint_t op, mp_obj_t self_in) { STATIC const mp_rom_map_elem_t utimeq_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_push), MP_ROM_PTR(&mod_utimeq_heappush_obj) }, { MP_ROM_QSTR(MP_QSTR_pop), MP_ROM_PTR(&mod_utimeq_heappop_obj) }, + { MP_ROM_QSTR(MP_QSTR_peektime), MP_ROM_PTR(&mod_utimeq_peektime_obj) }, #if DEBUG { MP_ROM_QSTR(MP_QSTR_dump), MP_ROM_PTR(&mod_utimeq_dump_obj) }, #endif -- cgit v1.2.3 From b6c7e4b143d96ff9f84ccb22d83b1e15ab084250 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 31 Mar 2017 22:29:39 +1100 Subject: all: Use full path name when including mp-readline/timeutils/netutils. This follows the pattern of how all other headers are now included, and makes it explicit where the header file comes from. This patch also removes -I options from Makefile's that specify the mp-readline/timeutils/ netutils directories, which are no longer needed. --- cc3200/application.mk | 3 --- cc3200/ftp/ftp.c | 2 +- cc3200/mods/moduos.c | 2 +- cc3200/mods/modusocket.c | 2 +- cc3200/mods/modutime.c | 2 +- cc3200/mods/pybrtc.c | 2 +- cc3200/mptask.c | 2 +- esp8266/Makefile | 3 --- esp8266/fatfs_port.c | 2 +- esp8266/machine_rtc.c | 2 +- esp8266/modnetwork.c | 2 +- esp8266/modutime.c | 2 +- examples/embedding/Makefile.upylib | 1 - extmod/modlwip.c | 2 +- lib/mp-readline/readline.c | 2 +- lib/netutils/netutils.c | 2 +- lib/timeutils/timeutils.c | 2 +- minimal/Makefile | 1 - pic16bit/Makefile | 1 - pic16bit/main.c | 2 +- py/py.mk | 3 --- stmhal/Makefile | 3 --- stmhal/input.c | 2 +- stmhal/main.c | 2 +- stmhal/modnwcc3k.c | 2 +- stmhal/modnwwiznet5k.c | 2 +- stmhal/moduos.c | 2 +- stmhal/modusocket.c | 2 +- stmhal/modutime.c | 2 +- teensy/Makefile | 1 - teensy/main.c | 2 +- unix/Makefile | 1 - 32 files changed, 23 insertions(+), 40 deletions(-) (limited to 'extmod') diff --git a/cc3200/application.mk b/cc3200/application.mk index b15dbde7f..5d25424e1 100644 --- a/cc3200/application.mk +++ b/cc3200/application.mk @@ -18,9 +18,6 @@ APP_INC += -Iutil APP_INC += -Ibootmgr APP_INC += -I$(BUILD) APP_INC += -I$(BUILD)/genhdr -APP_INC += -I../lib/mp-readline -APP_INC += -I../lib/netutils -APP_INC += -I../lib/timeutils APP_INC += -I../stmhal APP_CPPDEFINES = -Dgcc -DTARGET_IS_CC3200 -DSL_FULL -DUSE_FREERTOS diff --git a/cc3200/ftp/ftp.c b/cc3200/ftp/ftp.c index 22035d6b3..1febe291f 100644 --- a/cc3200/ftp/ftp.c +++ b/cc3200/ftp/ftp.c @@ -29,6 +29,7 @@ #include "py/mpstate.h" #include "py/obj.h" +#include "lib/timeutils/timeutils.h" #include "lib/oofatfs/ff.h" #include "extmod/vfs.h" #include "extmod/vfs_fat.h" @@ -48,7 +49,6 @@ #include "fifo.h" #include "socketfifo.h" #include "updater.h" -#include "timeutils.h" #include "moduos.h" /****************************************************************************** diff --git a/cc3200/mods/moduos.c b/cc3200/mods/moduos.c index 5523c887f..add10ec6c 100644 --- a/cc3200/mods/moduos.c +++ b/cc3200/mods/moduos.c @@ -33,6 +33,7 @@ #include "py/objtuple.h" #include "py/objstr.h" #include "py/runtime.h" +#include "lib/timeutils/timeutils.h" #include "lib/oofatfs/ff.h" #include "lib/oofatfs/diskio.h" #include "genhdr/mpversion.h" @@ -43,7 +44,6 @@ #include "random.h" #include "mpexception.h" #include "version.h" -#include "timeutils.h" #include "pybsd.h" #include "pybuart.h" diff --git a/cc3200/mods/modusocket.c b/cc3200/mods/modusocket.c index 4e2da6737..ca28bf7ba 100644 --- a/cc3200/mods/modusocket.c +++ b/cc3200/mods/modusocket.c @@ -34,7 +34,7 @@ #include "py/objstr.h" #include "py/runtime.h" #include "py/stream.h" -#include "netutils.h" +#include "lib/netutils/netutils.h" #include "modnetwork.h" #include "modusocket.h" #include "mpexception.h" diff --git a/cc3200/mods/modutime.c b/cc3200/mods/modutime.c index 8e3c71402..48fde67e7 100644 --- a/cc3200/mods/modutime.c +++ b/cc3200/mods/modutime.c @@ -33,8 +33,8 @@ #include "py/obj.h" #include "py/smallint.h" #include "py/mphal.h" +#include "lib/timeutils/timeutils.h" #include "extmod/utime_mphal.h" -#include "timeutils.h" #include "inc/hw_types.h" #include "inc/hw_ints.h" #include "inc/hw_memmap.h" diff --git a/cc3200/mods/pybrtc.c b/cc3200/mods/pybrtc.c index 13d7a49cb..134bd440e 100644 --- a/cc3200/mods/pybrtc.c +++ b/cc3200/mods/pybrtc.c @@ -29,6 +29,7 @@ #include "py/obj.h" #include "py/runtime.h" #include "py/mperrno.h" +#include "lib/timeutils/timeutils.h" #include "inc/hw_types.h" #include "inc/hw_ints.h" #include "inc/hw_memmap.h" @@ -37,7 +38,6 @@ #include "pybrtc.h" #include "mpirq.h" #include "pybsleep.h" -#include "timeutils.h" #include "simplelink.h" #include "modnetwork.h" #include "modwlan.h" diff --git a/cc3200/mptask.c b/cc3200/mptask.c index 3c49a5603..d446711a2 100644 --- a/cc3200/mptask.c +++ b/cc3200/mptask.c @@ -33,6 +33,7 @@ #include "py/runtime.h" #include "py/gc.h" #include "py/mphal.h" +#include "lib/mp-readline/readline.h" #include "lib/oofatfs/ff.h" #include "lib/oofatfs/diskio.h" #include "extmod/vfs.h" @@ -51,7 +52,6 @@ #include "lib/utils/pyexec.h" #include "gccollect.h" #include "gchelper.h" -#include "readline.h" #include "mperror.h" #include "simplelink.h" #include "modnetwork.h" diff --git a/esp8266/Makefile b/esp8266/Makefile index 28dbf08a6..a3da9d398 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -25,9 +25,6 @@ ESP_SDK = $(shell $(CC) -print-sysroot)/usr INC += -I. INC += -I.. INC += -I../stmhal -INC += -I../lib/mp-readline -INC += -I../lib/netutils -INC += -I../lib/timeutils INC += -I$(BUILD) INC += -I$(ESP_SDK)/include diff --git a/esp8266/fatfs_port.c b/esp8266/fatfs_port.c index 20c3235d4..02384f605 100644 --- a/esp8266/fatfs_port.c +++ b/esp8266/fatfs_port.c @@ -25,8 +25,8 @@ */ #include "py/obj.h" +#include "lib/timeutils/timeutils.h" #include "lib/oofatfs/ff.h" -#include "timeutils.h" #include "modmachine.h" DWORD get_fattime(void) { diff --git a/esp8266/machine_rtc.c b/esp8266/machine_rtc.c index 8c1fe0779..019b705ba 100644 --- a/esp8266/machine_rtc.c +++ b/esp8266/machine_rtc.c @@ -30,7 +30,7 @@ #include "py/nlr.h" #include "py/obj.h" #include "py/runtime.h" -#include "timeutils.h" +#include "lib/timeutils/timeutils.h" #include "user_interface.h" #include "modmachine.h" diff --git a/esp8266/modnetwork.c b/esp8266/modnetwork.c index 762717b35..627655b36 100644 --- a/esp8266/modnetwork.c +++ b/esp8266/modnetwork.c @@ -32,7 +32,7 @@ #include "py/objlist.h" #include "py/runtime.h" #include "py/mphal.h" -#include "netutils.h" +#include "lib/netutils/netutils.h" #include "queue.h" #include "user_interface.h" #include "espconn.h" diff --git a/esp8266/modutime.c b/esp8266/modutime.c index 117720e6f..bdeb3bb45 100644 --- a/esp8266/modutime.c +++ b/esp8266/modutime.c @@ -34,8 +34,8 @@ #include "py/runtime.h" #include "py/mphal.h" #include "py/smallint.h" +#include "lib/timeutils/timeutils.h" #include "modmachine.h" -#include "timeutils.h" #include "user_interface.h" #include "extmod/utime_mphal.h" diff --git a/examples/embedding/Makefile.upylib b/examples/embedding/Makefile.upylib index 8b506e95e..873c0fd34 100644 --- a/examples/embedding/Makefile.upylib +++ b/examples/embedding/Makefile.upylib @@ -14,7 +14,6 @@ INC += -I. INC += -I.. INC += -I$(MPTOP) INC += -I$(MPTOP)/unix -#INC += -I../lib/timeutils INC += -I$(BUILD) # compiler settings diff --git a/extmod/modlwip.c b/extmod/modlwip.c index 5709413de..fffabb98a 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -36,7 +36,7 @@ #include "py/mperrno.h" #include "py/mphal.h" -#include "netutils.h" +#include "lib/netutils/netutils.h" #include "lwip/init.h" #include "lwip/timers.h" diff --git a/lib/mp-readline/readline.c b/lib/mp-readline/readline.c index cbb99cc94..4b9875136 100644 --- a/lib/mp-readline/readline.c +++ b/lib/mp-readline/readline.c @@ -31,7 +31,7 @@ #include "py/mpstate.h" #include "py/repl.h" #include "py/mphal.h" -#include "readline.h" +#include "lib/mp-readline/readline.h" #if 0 // print debugging info #define DEBUG_PRINT (1) diff --git a/lib/netutils/netutils.c b/lib/netutils/netutils.c index 3a4c600dc..a2ea31cf3 100644 --- a/lib/netutils/netutils.c +++ b/lib/netutils/netutils.c @@ -31,7 +31,7 @@ #include "py/obj.h" #include "py/nlr.h" -#include "netutils.h" +#include "lib/netutils/netutils.h" // Takes an array with a raw IPv4 address and returns something like '192.168.0.1'. mp_obj_t netutils_format_ipv4_addr(uint8_t *ip, netutils_endian_t endian) { diff --git a/lib/timeutils/timeutils.c b/lib/timeutils/timeutils.c index 19d3ddbdd..0af39a295 100644 --- a/lib/timeutils/timeutils.c +++ b/lib/timeutils/timeutils.c @@ -27,7 +27,7 @@ #include "py/obj.h" -#include "timeutils.h" +#include "lib/timeutils/timeutils.h" // LEAPOCH corresponds to 2000-03-01, which is a mod-400 year, immediately // after Feb 29. We calculate seconds as a signed integer relative to that. diff --git a/minimal/Makefile b/minimal/Makefile index 3b446beae..d61515797 100644 --- a/minimal/Makefile +++ b/minimal/Makefile @@ -14,7 +14,6 @@ endif INC += -I. INC += -I.. -INC += -I../lib/mp-readline INC += -I../stmhal INC += -I$(BUILD) diff --git a/pic16bit/Makefile b/pic16bit/Makefile index add9a8495..8c0c1c999 100644 --- a/pic16bit/Makefile +++ b/pic16bit/Makefile @@ -14,7 +14,6 @@ PART = 33FJ256GP506 INC += -I. INC += -I.. -INC += -I../lib/mp-readline INC += -I../stmhal INC += -I$(BUILD) INC += -I$(XC16)/include diff --git a/pic16bit/main.c b/pic16bit/main.c index f2a9debab..7de790069 100644 --- a/pic16bit/main.c +++ b/pic16bit/main.c @@ -35,7 +35,7 @@ #include "py/mphal.h" #include "py/mperrno.h" #include "lib/utils/pyexec.h" -#include "readline.h" +#include "lib/mp-readline/readline.h" #include "board.h" #include "modpyb.h" diff --git a/py/py.mk b/py/py.mk index 37bb5d023..5ff1fd6a6 100644 --- a/py/py.mk +++ b/py/py.mk @@ -16,9 +16,6 @@ endif # some code is performance bottleneck and compiled with other optimization options CSUPEROPT = -O3 -INC += -I../lib -INC += -I../lib/netutils - # this sets the config file for FatFs CFLAGS_MOD += -DFFCONF_H=\"lib/oofatfs/ffconf.h\" diff --git a/stmhal/Makefile b/stmhal/Makefile index 51aa07f3c..09643be94 100644 --- a/stmhal/Makefile +++ b/stmhal/Makefile @@ -47,9 +47,6 @@ INC += -I$(CMSIS_DIR)/ INC += -I$(HAL_DIR)/inc INC += -I$(USBDEV_DIR)/core/inc -I$(USBDEV_DIR)/class/inc #INC += -I$(USBHOST_DIR) -INC += -I../lib/mp-readline -INC += -I../lib/netutils -INC += -I../lib/timeutils CFLAGS_CORTEX_M = -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion CFLAGS_MCU_f4 = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 -DMCU_SERIES_F4 diff --git a/stmhal/input.c b/stmhal/input.c index 07e7ba35c..c78525cc9 100644 --- a/stmhal/input.c +++ b/stmhal/input.c @@ -26,7 +26,7 @@ #include "py/nlr.h" #include "py/obj.h" -#include "readline.h" +#include "lib/mp-readline/readline.h" STATIC mp_obj_t mp_builtin_input(uint n_args, const mp_obj_t *args) { if (n_args == 1) { diff --git a/stmhal/main.c b/stmhal/main.c index 8d076a08b..566c2db07 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -31,6 +31,7 @@ #include "py/stackctrl.h" #include "py/gc.h" #include "py/mphal.h" +#include "lib/mp-readline/readline.h" #include "lib/utils/pyexec.h" #include "lib/oofatfs/ff.h" #include "extmod/vfs.h" @@ -40,7 +41,6 @@ #include "pendsv.h" #include "pybthread.h" #include "gccollect.h" -#include "readline.h" #include "modmachine.h" #include "i2c.h" #include "spi.h" diff --git a/stmhal/modnwcc3k.c b/stmhal/modnwcc3k.c index ae403cacd..26c6cd48c 100644 --- a/stmhal/modnwcc3k.c +++ b/stmhal/modnwcc3k.c @@ -37,7 +37,7 @@ #include "py/runtime.h" #include "py/mperrno.h" #include "py/mphal.h" -#include "netutils.h" +#include "lib/netutils/netutils.h" #include "modnetwork.h" #include "pin.h" #include "genhdr/pins.h" diff --git a/stmhal/modnwwiznet5k.c b/stmhal/modnwwiznet5k.c index e3d5c6370..b32f16913 100644 --- a/stmhal/modnwwiznet5k.c +++ b/stmhal/modnwwiznet5k.c @@ -33,7 +33,7 @@ #include "py/runtime.h" #include "py/mperrno.h" #include "py/mphal.h" -#include "netutils.h" +#include "lib/netutils/netutils.h" #include "modnetwork.h" #include "pin.h" #include "genhdr/pins.h" diff --git a/stmhal/moduos.c b/stmhal/moduos.c index 0af682612..745c0d5d7 100644 --- a/stmhal/moduos.c +++ b/stmhal/moduos.c @@ -31,12 +31,12 @@ #include "py/runtime.h" #include "py/objtuple.h" #include "py/objstr.h" +#include "lib/timeutils/timeutils.h" #include "lib/oofatfs/ff.h" #include "lib/oofatfs/diskio.h" #include "extmod/vfs.h" #include "extmod/vfs_fat.h" #include "genhdr/mpversion.h" -#include "timeutils.h" #include "rng.h" #include "uart.h" #include "portmodules.h" diff --git a/stmhal/modusocket.c b/stmhal/modusocket.c index d066501d0..fd60c5ad4 100644 --- a/stmhal/modusocket.c +++ b/stmhal/modusocket.c @@ -32,7 +32,7 @@ #include "py/objlist.h" #include "py/runtime.h" #include "py/mperrno.h" -#include "netutils.h" +#include "lib/netutils/netutils.h" #include "modnetwork.h" #if MICROPY_PY_USOCKET diff --git a/stmhal/modutime.c b/stmhal/modutime.c index 2c7245080..97af35c49 100644 --- a/stmhal/modutime.c +++ b/stmhal/modutime.c @@ -31,9 +31,9 @@ #include "py/nlr.h" #include "py/smallint.h" #include "py/obj.h" +#include "lib/timeutils/timeutils.h" #include "extmod/utime_mphal.h" #include "systick.h" -#include "timeutils.h" #include "portmodules.h" #include "rtc.h" diff --git a/teensy/Makefile b/teensy/Makefile index e613b8f27..9f52cc3c7 100644 --- a/teensy/Makefile +++ b/teensy/Makefile @@ -32,7 +32,6 @@ CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mcpu=cortex-m4 -msoft-float -mfloat INC += -I. INC += -I.. INC += -I../stmhal -INC += -I../lib/mp-readline INC += -I$(BUILD) INC += -Icore diff --git a/teensy/main.c b/teensy/main.c index d62ae3bdb..41bbeb5d9 100644 --- a/teensy/main.c +++ b/teensy/main.c @@ -10,7 +10,7 @@ #include "py/mphal.h" #include "gccollect.h" #include "lib/utils/pyexec.h" -#include "readline.h" +#include "lib/mp-readline/readline.h" #include "lexermemzip.h" #include "Arduino.h" diff --git a/unix/Makefile b/unix/Makefile index f28a4bcae..546985306 100644 --- a/unix/Makefile +++ b/unix/Makefile @@ -17,7 +17,6 @@ include ../py/py.mk INC += -I. INC += -I.. -INC += -I../lib/timeutils INC += -I$(BUILD) # compiler settings -- cgit v1.2.3 From 468c6f9da147d6e752e437a32211e317a116b6df Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Sat, 1 Apr 2017 07:00:09 +0100 Subject: extmod/modframebuf: Make monochrome bitmap formats start with MONO_. MONO_xxx is much easier to read if you're not familiar with the code. MVLSB is deprecated but kept for backwards compatibility, for the time being. This patch also updates the associated docs and tests. --- docs/library/framebuf.rst | 25 ++++++++++++++++++++++++- extmod/modframebuf.c | 5 +++-- tests/extmod/framebuf1.py | 7 ++++--- tests/extmod/framebuf1.py.exp | 7 ++++--- 4 files changed, 35 insertions(+), 9 deletions(-) (limited to 'extmod') diff --git a/docs/library/framebuf.rst b/docs/library/framebuf.rst index 91fc362fd..61f0635f3 100644 --- a/docs/library/framebuf.rst +++ b/docs/library/framebuf.rst @@ -116,9 +116,32 @@ Other methods Constants --------- -.. data:: framebuf.MVLSB +.. data:: framebuf.MONO_VLSB Monochrome (1-bit) color format + This defines a mapping where the bits in a byte are vertically mapped with + bit 0 being nearest the top of the screen. Consequently each byte occupies + 8 vertical pixels. Subsequent bytes appear at successive horizontal + locations until the rightmost edge is reached. Further bytes are rendered + at locations starting at the leftmost edge, 8 pixels lower. + +.. data:: framebuf.MONO_HLSB + + Monochrome (1-bit) color format + This defines a mapping where the bits in a byte are horizontally mapped. + Each byte occupies 8 horizontal pixels with bit 0 being the leftmost. + Subsequent bytes appear at successive horizontal locations until the + rightmost edge is reached. Further bytes are rendered on the next row, one + pixel lower. + +.. data:: framebuf.MONO_HMSB + + Monochrome (1-bit) color format + This defines a mapping where the bits in a byte are horizontally mapped. + Each byte occupies 8 horizontal pixels with bit 7 being the leftmost. + Subsequent bytes appear at successive horizontal locations until the + rightmost edge is reached. Further bytes are rendered on the next row, one + pixel lower. .. data:: framebuf.RGB565 diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index 33985dd00..b8e84fe1c 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -579,10 +579,11 @@ STATIC const mp_rom_map_elem_t framebuf_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_FrameBuffer), MP_ROM_PTR(&mp_type_framebuf) }, { 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_MONO_VLSB), 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) }, - { MP_ROM_QSTR(MP_QSTR_MHLSB), MP_OBJ_NEW_SMALL_INT(FRAMEBUF_MHLSB) }, - { MP_ROM_QSTR(MP_QSTR_MHMSB), MP_OBJ_NEW_SMALL_INT(FRAMEBUF_MHMSB) }, + { MP_ROM_QSTR(MP_QSTR_MONO_HLSB), MP_OBJ_NEW_SMALL_INT(FRAMEBUF_MHLSB) }, + { MP_ROM_QSTR(MP_QSTR_MONO_HMSB), MP_OBJ_NEW_SMALL_INT(FRAMEBUF_MHMSB) }, }; 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 0a8e1ae55..990b0b120 100644 --- a/tests/extmod/framebuf1.py +++ b/tests/extmod/framebuf1.py @@ -9,9 +9,9 @@ w = 5 h = 16 size = w * h // 8 buf = bytearray(size) -maps = {framebuf.MVLSB : 'MVLSB', - framebuf.MHLSB : 'MHLSB', - framebuf.MHMSB : 'MHMSB'} +maps = {framebuf.MONO_VLSB : 'MONO_VLSB', + framebuf.MONO_HLSB : 'MONO_HLSB', + framebuf.MONO_HMSB : 'MONO_HMSB'} for mapping in maps.keys(): for x in range(size): @@ -107,3 +107,4 @@ except ValueError: # test legacy constructor fbuf = framebuf.FrameBuffer1(buf, w, h) fbuf = framebuf.FrameBuffer1(buf, w, h, w) +print(framebuf.MVLSB == framebuf.MONO_VLSB) diff --git a/tests/extmod/framebuf1.py.exp b/tests/extmod/framebuf1.py.exp index 736ad7a45..d954623de 100644 --- a/tests/extmod/framebuf1.py.exp +++ b/tests/extmod/framebuf1.py.exp @@ -1,4 +1,4 @@ -MVLSB +MONO_VLSB 0 bytearray(b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff') bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') @@ -20,7 +20,7 @@ bytearray(b'\x00\x7f\x7f\x04\x04\x00\x00\x00\x00\x00') bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') bytearray(b'\xaaU\xaaU\xaa\x00\x00\x00\x00\x00') -MHLSB +MONO_HLSB 0 bytearray(b'\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8\xf8') bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') @@ -42,7 +42,7 @@ bytearray(b'``x````\x00\x00\x00') bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') bytearray(b'P\xa8P\xa8P\xa8P\xa8\x00\x00') -MHMSB +MONO_HMSB 0 bytearray(b'\x1f\x1f\x1f\x1f\x1f\x1f\x1f\x1f\x1f\x1f') bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') @@ -65,3 +65,4 @@ bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') bytearray(b'\n\x15\n\x15\n\x15\n\x15\x00\x00') ValueError +True -- cgit v1.2.3 From 605ff91efdeb3cd7e4948c0398c839a9ae06044a Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 11 Apr 2017 00:12:20 +0300 Subject: extmod/machine_signal: Support all Pin's arguments to the constructor. This implements the orginal idea is that Signal is a subclass of Pin, and thus can accept all the same argument as Pin, and additionally, "inverted" param. On the practical side, it allows to avoid many enclosed parenses for a typical declararion, e.g. for Zephyr: Signal(Pin(("GPIO_0", 1))). Of course, passing a Pin to Signal constructor is still supported and is the most generic form (e.g. Unix port will only support such form, as it doesn't have "builtin" Pins), what's introduces here is just practical readability optimization. "value" kwarg is treated as applying to a Signal (i.e. accounts for possible inversion). --- esp8266/machine_pin.c | 4 +-- esp8266/mpconfigport.h | 1 + extmod/machine_signal.c | 73 +++++++++++++++++++++++++++++++++++++++++++------ extmod/virtpin.h | 3 ++ 4 files changed, 70 insertions(+), 11 deletions(-) (limited to 'extmod') diff --git a/esp8266/machine_pin.c b/esp8266/machine_pin.c index d4c6d3dea..1a263601b 100644 --- a/esp8266/machine_pin.c +++ b/esp8266/machine_pin.c @@ -287,7 +287,7 @@ STATIC mp_obj_t pyb_pin_obj_init_helper(pyb_pin_obj_t *self, mp_uint_t n_args, c } // constructor(id, ...) -STATIC mp_obj_t pyb_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { +mp_obj_t mp_pin_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, 1, MP_OBJ_FUN_ARGS_MAX, true); // get the wanted pin object @@ -436,7 +436,7 @@ const mp_obj_type_t pyb_pin_type = { { &mp_type_type }, .name = MP_QSTR_Pin, .print = pyb_pin_print, - .make_new = pyb_pin_make_new, + .make_new = mp_pin_make_new, .call = pyb_pin_call, .protocol = &pin_pin_p, .locals_dict = (mp_obj_t)&pyb_pin_locals_dict, diff --git a/esp8266/mpconfigport.h b/esp8266/mpconfigport.h index b25bb8bb4..d0b4a7b4b 100644 --- a/esp8266/mpconfigport.h +++ b/esp8266/mpconfigport.h @@ -73,6 +73,7 @@ #define MICROPY_PY_UZLIB (1) #define MICROPY_PY_LWIP (1) #define MICROPY_PY_MACHINE (1) +#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new #define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_PY_MACHINE_I2C (1) #define MICROPY_PY_MACHINE_SPI (1) diff --git a/extmod/machine_signal.c b/extmod/machine_signal.c index de6c3ff32..b10d90166 100644 --- a/extmod/machine_signal.c +++ b/extmod/machine_signal.c @@ -27,6 +27,8 @@ #include "py/mpconfig.h" #if MICROPY_PY_MACHINE +#include + #include "py/obj.h" #include "py/runtime.h" #include "extmod/virtpin.h" @@ -41,20 +43,73 @@ typedef struct _machine_signal_t { } 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_obj_t pin = args[0]; + bool inverted = false; + + #if defined(MICROPY_PY_MACHINE_PIN_MAKE_NEW) + mp_pin_p_t *pin_p = NULL; + + if (MP_OBJ_IS_OBJ(pin)) { + mp_obj_base_t *pin_base = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]); + pin_p = (mp_pin_p_t*)pin_base->type->protocol; + } + + if (pin_p == NULL) { + // If first argument isn't a Pin-like object, we filter out "inverted" + // from keyword arguments and pass them all to the exported Pin + // constructor to create one. + mp_obj_t pin_args[n_args + n_kw * 2]; + memcpy(pin_args, args, n_args * sizeof(mp_obj_t)); + const mp_obj_t *src = args + n_args; + mp_obj_t *dst = pin_args + n_args; + mp_obj_t *sig_value = NULL; + for (size_t cnt = n_kw; cnt; cnt--) { + if (*src == MP_OBJ_NEW_QSTR(MP_QSTR_inverted)) { + inverted = mp_obj_is_true(src[1]); + n_kw--; + } else { + *dst++ = *src; + *dst++ = src[1]; + } + if (*src == MP_OBJ_NEW_QSTR(MP_QSTR_value)) { + // Value is pertained to Signal, so we should invert + // it for Pin if needed, and we should do it only when + // inversion status is guaranteedly known. + sig_value = dst - 1; + } + src += 2; + } - mp_arg_val_t parsed_args[MP_ARRAY_SIZE(allowed_args)]; + if (inverted && sig_value != NULL) { + *sig_value = mp_obj_is_true(*sig_value) ? MP_OBJ_NEW_SMALL_INT(0) : MP_OBJ_NEW_SMALL_INT(1); + } - mp_arg_parse_all_kw_array(n_args, n_kw, args, MP_ARRAY_SIZE(allowed_args), allowed_args, parsed_args); + // Here we pass NULL as a type, hoping that mp_pin_make_new() + // will just ignore it as set a concrete type. If not, we'd need + // to expose port's "default" pin type too. + pin = MICROPY_PY_MACHINE_PIN_MAKE_NEW(NULL, n_args, n_kw, pin_args); + } + else + #endif + // Otherwise there should be 1 or 2 args + { + if (n_args == 1) { + if (n_kw == 0) { + } else if (n_kw == 1 && args[1] == MP_OBJ_NEW_QSTR(MP_QSTR_inverted)) { + inverted = mp_obj_is_true(args[1]); + } else { + goto error; + } + } else { + error: + mp_raise_TypeError(NULL); + } + } 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; + o->pin = pin; + o->inverted = inverted; return MP_OBJ_FROM_PTR(o); } diff --git a/extmod/virtpin.h b/extmod/virtpin.h index 3821f9dec..041010350 100644 --- a/extmod/virtpin.h +++ b/extmod/virtpin.h @@ -38,3 +38,6 @@ typedef struct _mp_pin_p_t { int mp_virtual_pin_read(mp_obj_t pin); void mp_virtual_pin_write(mp_obj_t pin, int value); + +// If a port exposes a Pin object, it's constructor should be like this +mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args); -- cgit v1.2.3 From 61616e84ce41e1b1a0fad62704488d79b87cfbe2 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 12 Apr 2017 13:00:40 +1000 Subject: extmod/machine_signal: Rename "inverted" arg to "invert", it's shorter. A shorter name takes less code size, less room in scripts and is faster to type at the REPL. Tests and HW-API examples are updated to reflect the change. --- examples/hwapi/hwconfig_esp8266_esp12.py | 2 +- extmod/machine_signal.c | 22 +++++++++++----------- tests/extmod/machine_signal.py | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) (limited to 'extmod') diff --git a/examples/hwapi/hwconfig_esp8266_esp12.py b/examples/hwapi/hwconfig_esp8266_esp12.py index fb6d5fe90..198a652d0 100644 --- a/examples/hwapi/hwconfig_esp8266_esp12.py +++ b/examples/hwapi/hwconfig_esp8266_esp12.py @@ -2,4 +2,4 @@ from machine import Pin, Signal # ESP12 module as used by many boards # Blue LED on pin 2, active low (inverted) -LED = Signal(Pin(2, Pin.OUT), inverted=True) +LED = Signal(Pin(2, Pin.OUT), invert=True) diff --git a/extmod/machine_signal.c b/extmod/machine_signal.c index b10d90166..d08931296 100644 --- a/extmod/machine_signal.c +++ b/extmod/machine_signal.c @@ -39,12 +39,12 @@ typedef struct _machine_signal_t { mp_obj_base_t base; mp_obj_t pin; - bool inverted; + bool invert; } 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) { mp_obj_t pin = args[0]; - bool inverted = false; + bool invert = false; #if defined(MICROPY_PY_MACHINE_PIN_MAKE_NEW) mp_pin_p_t *pin_p = NULL; @@ -55,7 +55,7 @@ STATIC mp_obj_t signal_make_new(const mp_obj_type_t *type, size_t n_args, size_t } if (pin_p == NULL) { - // If first argument isn't a Pin-like object, we filter out "inverted" + // If first argument isn't a Pin-like object, we filter out "invert" // from keyword arguments and pass them all to the exported Pin // constructor to create one. mp_obj_t pin_args[n_args + n_kw * 2]; @@ -64,8 +64,8 @@ STATIC mp_obj_t signal_make_new(const mp_obj_type_t *type, size_t n_args, size_t mp_obj_t *dst = pin_args + n_args; mp_obj_t *sig_value = NULL; for (size_t cnt = n_kw; cnt; cnt--) { - if (*src == MP_OBJ_NEW_QSTR(MP_QSTR_inverted)) { - inverted = mp_obj_is_true(src[1]); + if (*src == MP_OBJ_NEW_QSTR(MP_QSTR_invert)) { + invert = mp_obj_is_true(src[1]); n_kw--; } else { *dst++ = *src; @@ -80,7 +80,7 @@ STATIC mp_obj_t signal_make_new(const mp_obj_type_t *type, size_t n_args, size_t src += 2; } - if (inverted && sig_value != NULL) { + if (invert && sig_value != NULL) { *sig_value = mp_obj_is_true(*sig_value) ? MP_OBJ_NEW_SMALL_INT(0) : MP_OBJ_NEW_SMALL_INT(1); } @@ -95,8 +95,8 @@ STATIC mp_obj_t signal_make_new(const mp_obj_type_t *type, size_t n_args, size_t { if (n_args == 1) { if (n_kw == 0) { - } else if (n_kw == 1 && args[1] == MP_OBJ_NEW_QSTR(MP_QSTR_inverted)) { - inverted = mp_obj_is_true(args[1]); + } else if (n_kw == 1 && args[1] == MP_OBJ_NEW_QSTR(MP_QSTR_invert)) { + invert = mp_obj_is_true(args[1]); } else { goto error; } @@ -109,7 +109,7 @@ STATIC mp_obj_t signal_make_new(const mp_obj_type_t *type, size_t n_args, size_t machine_signal_t *o = m_new_obj(machine_signal_t); o->base.type = type; o->pin = pin; - o->inverted = inverted; + o->invert = invert; return MP_OBJ_FROM_PTR(o); } @@ -119,10 +119,10 @@ STATIC mp_uint_t signal_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg switch (request) { case MP_PIN_READ: { - return mp_virtual_pin_read(self->pin) ^ self->inverted; + return mp_virtual_pin_read(self->pin) ^ self->invert; } case MP_PIN_WRITE: { - mp_virtual_pin_write(self->pin, arg ^ self->inverted); + mp_virtual_pin_write(self->pin, arg ^ self->invert); return 0; } } diff --git a/tests/extmod/machine_signal.py b/tests/extmod/machine_signal.py index 401533f23..96b8f43c7 100644 --- a/tests/extmod/machine_signal.py +++ b/tests/extmod/machine_signal.py @@ -33,7 +33,7 @@ print(p.value(), s.value()) # test inverted, and using on/off methods p = Pin() -s = machine.Signal(p, inverted=True) +s = machine.Signal(p, invert=True) s.off() print(p.value(), s.value()) s.on() -- cgit v1.2.3 From 9e8f3163924c1f429f16f44a4a27b0cd33064719 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 21 Apr 2017 16:40:57 +0300 Subject: extmod/moductypes: Fix bigint handling for 32-bit ports. --- extmod/moductypes.c | 2 +- py/objint_mpz.c | 1 + tests/extmod/uctypes_32bit_intbig.py | 54 ++++++++++++++++++++++++++++++++ tests/extmod/uctypes_32bit_intbig.py.exp | 11 +++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/extmod/uctypes_32bit_intbig.py create mode 100644 tests/extmod/uctypes_32bit_intbig.py.exp (limited to 'extmod') diff --git a/extmod/moductypes.c b/extmod/moductypes.c index 6249a4940..d2d2e85de 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -360,7 +360,7 @@ STATIC void set_aligned(uint val_type, void *p, mp_int_t index, mp_obj_t val) { return; } #endif - mp_int_t v = mp_obj_get_int(val); + mp_int_t v = mp_obj_get_int_truncated(val); switch (val_type) { case UINT8: ((uint8_t*)p)[index] = (uint8_t)v; return; diff --git a/py/objint_mpz.c b/py/objint_mpz.c index 2353bd8d6..d818b6f40 100644 --- a/py/objint_mpz.c +++ b/py/objint_mpz.c @@ -116,6 +116,7 @@ mp_obj_t mp_obj_int_from_bytes_impl(bool big_endian, size_t len, const byte *buf void mp_obj_int_to_bytes_impl(mp_obj_t self_in, bool big_endian, size_t len, byte *buf) { assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int)); mp_obj_int_t *self = MP_OBJ_TO_PTR(self_in); + memset(buf, 0, len); mpz_as_bytes(&self->mpz, big_endian, len, buf); } diff --git a/tests/extmod/uctypes_32bit_intbig.py b/tests/extmod/uctypes_32bit_intbig.py new file mode 100644 index 000000000..a082dc370 --- /dev/null +++ b/tests/extmod/uctypes_32bit_intbig.py @@ -0,0 +1,54 @@ +# This test checks previously known problem values for 32-bit ports. +# It's less useful for 64-bit ports. +try: + import uctypes +except ImportError: + import sys + print("SKIP") + sys.exit() + +buf = b"12345678abcd" +struct = uctypes.struct( + uctypes.addressof(buf), + {"f32": uctypes.UINT32 | 0, "f64": uctypes.UINT64 | 4}, + uctypes.LITTLE_ENDIAN +) + +struct.f32 = 0x7fffffff +print(buf) + +struct.f32 = 0x80000000 +print(buf) + +struct.f32 = 0xff010203 +print(buf) + +struct.f64 = 0x80000000 +print(buf) + +struct.f64 = 0x80000000 * 2 +print(buf) + +print("=") + +buf = b"12345678abcd" +struct = uctypes.struct( + uctypes.addressof(buf), + {"f32": uctypes.UINT32 | 0, "f64": uctypes.UINT64 | 4}, + uctypes.BIG_ENDIAN +) + +struct.f32 = 0x7fffffff +print(buf) + +struct.f32 = 0x80000000 +print(buf) + +struct.f32 = 0xff010203 +print(buf) + +struct.f64 = 0x80000000 +print(buf) + +struct.f64 = 0x80000000 * 2 +print(buf) diff --git a/tests/extmod/uctypes_32bit_intbig.py.exp b/tests/extmod/uctypes_32bit_intbig.py.exp new file mode 100644 index 000000000..d1fc1fe35 --- /dev/null +++ b/tests/extmod/uctypes_32bit_intbig.py.exp @@ -0,0 +1,11 @@ +b'\xff\xff\xff\x7f5678abcd' +b'\x00\x00\x00\x805678abcd' +b'\x03\x02\x01\xff5678abcd' +b'\x03\x02\x01\xff\x00\x00\x00\x80\x00\x00\x00\x00' +b'\x03\x02\x01\xff\x00\x00\x00\x00\x01\x00\x00\x00' += +b'\x7f\xff\xff\xff5678abcd' +b'\x80\x00\x00\x005678abcd' +b'\xff\x01\x02\x035678abcd' +b'\xff\x01\x02\x03\x00\x00\x00\x00\x80\x00\x00\x00' +b'\xff\x01\x02\x03\x00\x00\x00\x01\x00\x00\x00\x00' -- cgit v1.2.3 From 8109cd5f23c530ef568c617c85193cfa9b836dbc Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Thu, 27 Apr 2017 15:00:23 +0300 Subject: extmod/crypto-algorithms/sha256: Remove non-standard memory.h header. --- extmod/crypto-algorithms/sha256.c | 1 - 1 file changed, 1 deletion(-) (limited to 'extmod') diff --git a/extmod/crypto-algorithms/sha256.c b/extmod/crypto-algorithms/sha256.c index 82e5d9c7b..276611cfd 100644 --- a/extmod/crypto-algorithms/sha256.c +++ b/extmod/crypto-algorithms/sha256.c @@ -14,7 +14,6 @@ /*************************** HEADER FILES ***************************/ #include -#include #include "sha256.h" /****************************** MACROS ******************************/ -- cgit v1.2.3 From b08286948abc43eb48bf5964066164a6fa2ae95a Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 29 Apr 2017 11:03:46 +0300 Subject: extmod/moduselect: Convert to MP_ROM_QSTR and friends. --- extmod/moduselect.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'extmod') diff --git a/extmod/moduselect.c b/extmod/moduselect.c index 46dbe42e1..299654181 100644 --- a/extmod/moduselect.c +++ b/extmod/moduselect.c @@ -269,18 +269,18 @@ STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) { } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_poll_obj, 1, 3, poll_poll); -STATIC const mp_map_elem_t poll_locals_dict_table[] = { - { MP_OBJ_NEW_QSTR(MP_QSTR_register), (mp_obj_t)&poll_register_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_unregister), (mp_obj_t)&poll_unregister_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_modify), (mp_obj_t)&poll_modify_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_poll), (mp_obj_t)&poll_poll_obj }, +STATIC const mp_rom_map_elem_t poll_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_register), MP_ROM_PTR(&poll_register_obj) }, + { MP_ROM_QSTR(MP_QSTR_unregister), MP_ROM_PTR(&poll_unregister_obj) }, + { MP_ROM_QSTR(MP_QSTR_modify), MP_ROM_PTR(&poll_modify_obj) }, + { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&poll_poll_obj) }, }; STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table); STATIC const mp_obj_type_t mp_type_poll = { { &mp_type_type }, .name = MP_QSTR_poll, - .locals_dict = (mp_obj_t)&poll_locals_dict, + .locals_dict = (void*)&poll_locals_dict, }; /// \function poll() @@ -292,14 +292,14 @@ STATIC mp_obj_t select_poll(void) { } MP_DEFINE_CONST_FUN_OBJ_0(mp_select_poll_obj, select_poll); -STATIC const mp_map_elem_t mp_module_select_globals_table[] = { - { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uselect) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_select), (mp_obj_t)&mp_select_select_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_poll), (mp_obj_t)&mp_select_poll_obj }, - { MP_OBJ_NEW_QSTR(MP_QSTR_POLLIN), MP_OBJ_NEW_SMALL_INT(MP_STREAM_POLL_RD) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_POLLOUT), MP_OBJ_NEW_SMALL_INT(MP_STREAM_POLL_WR) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_POLLERR), MP_OBJ_NEW_SMALL_INT(MP_STREAM_POLL_ERR) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_POLLHUP), MP_OBJ_NEW_SMALL_INT(MP_STREAM_POLL_HUP) }, +STATIC const mp_rom_map_elem_t mp_module_select_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uselect) }, + { MP_ROM_QSTR(MP_QSTR_select), MP_ROM_PTR(&mp_select_select_obj) }, + { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&mp_select_poll_obj) }, + { MP_ROM_QSTR(MP_QSTR_POLLIN), MP_OBJ_NEW_SMALL_INT(MP_STREAM_POLL_RD) }, + { MP_ROM_QSTR(MP_QSTR_POLLOUT), MP_OBJ_NEW_SMALL_INT(MP_STREAM_POLL_WR) }, + { MP_ROM_QSTR(MP_QSTR_POLLERR), MP_OBJ_NEW_SMALL_INT(MP_STREAM_POLL_ERR) }, + { MP_ROM_QSTR(MP_QSTR_POLLHUP), MP_OBJ_NEW_SMALL_INT(MP_STREAM_POLL_HUP) }, }; STATIC MP_DEFINE_CONST_DICT(mp_module_select_globals, mp_module_select_globals_table); -- cgit v1.2.3 From edc0dcb55c127ff17d8f7e72b9ba5a9f9b77574b Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 29 Apr 2017 13:05:20 +0300 Subject: extmod/moduselect: Refactor towards introduction of poll.ipoll(). This follows previous refactor made to unix/moduselect. --- extmod/moduselect.c | 56 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 23 deletions(-) (limited to 'extmod') diff --git a/extmod/moduselect.c b/extmod/moduselect.c index 299654181..7e4148a6d 100644 --- a/extmod/moduselect.c +++ b/extmod/moduselect.c @@ -182,6 +182,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_select_select_obj, 3, 4, select_select); typedef struct _mp_obj_poll_t { mp_obj_base_t base; mp_map_t poll_map; + int flags; } mp_obj_poll_t; /// \method register(obj[, eventmask]) @@ -219,9 +220,7 @@ STATIC mp_obj_t poll_modify(mp_obj_t self_in, mp_obj_t obj_in, mp_obj_t eventmas } MP_DEFINE_CONST_FUN_OBJ_3(poll_modify_obj, poll_modify); -/// \method poll([timeout]) -/// Timeout is in milliseconds. -STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) { +STATIC mp_uint_t poll_poll_internal(uint n_args, const mp_obj_t *args) { mp_obj_poll_t *self = args[0]; // work out timeout (its given already in ms) @@ -239,33 +238,44 @@ STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) { } } + self->flags = flags; + mp_uint_t start_tick = mp_hal_ticks_ms(); + mp_uint_t n_ready; for (;;) { // poll the objects - mp_uint_t n_ready = poll_map_poll(&self->poll_map, NULL); - + n_ready = poll_map_poll(&self->poll_map, NULL); if (n_ready > 0 || (timeout != -1 && mp_hal_ticks_ms() - start_tick >= timeout)) { - // one or more objects are ready, or we had a timeout - mp_obj_list_t *ret_list = mp_obj_new_list(n_ready, NULL); - n_ready = 0; - for (mp_uint_t i = 0; i < self->poll_map.alloc; ++i) { - if (!MP_MAP_SLOT_IS_FILLED(&self->poll_map, i)) { - continue; - } - poll_obj_t *poll_obj = (poll_obj_t*)self->poll_map.table[i].value; - if (poll_obj->flags_ret != 0) { - mp_obj_t tuple[2] = {poll_obj->obj, MP_OBJ_NEW_SMALL_INT(poll_obj->flags_ret)}; - ret_list->items[n_ready++] = mp_obj_new_tuple(2, tuple); - if (flags & FLAG_ONESHOT) { - // Don't poll next time, until new event flags will be set explicitly - poll_obj->flags = 0; - } - } - } - return ret_list; + break; } MICROPY_EVENT_POLL_HOOK } + + return n_ready; +} + +STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) { + mp_obj_poll_t *self = args[0]; + mp_uint_t n_ready = poll_poll_internal(n_args, args); + + // one or more objects are ready, or we had a timeout + mp_obj_list_t *ret_list = mp_obj_new_list(n_ready, NULL); + n_ready = 0; + for (mp_uint_t i = 0; i < self->poll_map.alloc; ++i) { + if (!MP_MAP_SLOT_IS_FILLED(&self->poll_map, i)) { + continue; + } + poll_obj_t *poll_obj = (poll_obj_t*)self->poll_map.table[i].value; + if (poll_obj->flags_ret != 0) { + mp_obj_t tuple[2] = {poll_obj->obj, MP_OBJ_NEW_SMALL_INT(poll_obj->flags_ret)}; + ret_list->items[n_ready++] = mp_obj_new_tuple(2, tuple); + if (self->flags & FLAG_ONESHOT) { + // Don't poll next time, until new event flags will be set explicitly + poll_obj->flags = 0; + } + } + } + return ret_list; } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_poll_obj, 1, 3, poll_poll); -- cgit v1.2.3 From de3a96ba1734654c3e8c88a8010e145927286767 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 29 Apr 2017 13:05:44 +0300 Subject: extmod/moduselect: Implement ipoll() method for alloc-free polling. Similar to the implementation added to unix port module previously. --- extmod/moduselect.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'extmod') diff --git a/extmod/moduselect.c b/extmod/moduselect.c index 7e4148a6d..88dd29a49 100644 --- a/extmod/moduselect.c +++ b/extmod/moduselect.c @@ -182,7 +182,11 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_select_select_obj, 3, 4, select_select); typedef struct _mp_obj_poll_t { mp_obj_base_t base; mp_map_t poll_map; + short iter_cnt; + short iter_idx; int flags; + // callee-owned tuple + mp_obj_t ret_tuple; } mp_obj_poll_t; /// \method register(obj[, eventmask]) @@ -279,17 +283,67 @@ STATIC mp_obj_t poll_poll(uint n_args, const mp_obj_t *args) { } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_poll_obj, 1, 3, poll_poll); +STATIC mp_obj_t poll_ipoll(size_t n_args, const mp_obj_t *args) { + mp_obj_poll_t *self = MP_OBJ_TO_PTR(args[0]); + + if (self->ret_tuple == MP_OBJ_NULL) { + self->ret_tuple = mp_obj_new_tuple(2, NULL); + } + + int n_ready = poll_poll_internal(n_args, args); + self->iter_cnt = n_ready; + self->iter_idx = 0; + + return args[0]; +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poll_ipoll_obj, 1, 3, poll_ipoll); + +STATIC mp_obj_t poll_iternext(mp_obj_t self_in) { + mp_obj_poll_t *self = MP_OBJ_TO_PTR(self_in); + + if (self->iter_cnt == 0) { + return MP_OBJ_STOP_ITERATION; + } + + self->iter_cnt--; + + for (mp_uint_t i = self->iter_idx; i < self->poll_map.alloc; ++i) { + self->iter_idx++; + if (!MP_MAP_SLOT_IS_FILLED(&self->poll_map, i)) { + continue; + } + poll_obj_t *poll_obj = (poll_obj_t*)self->poll_map.table[i].value; + if (poll_obj->flags_ret != 0) { + mp_obj_tuple_t *t = MP_OBJ_TO_PTR(self->ret_tuple); + t->items[0] = poll_obj->obj; + t->items[1] = MP_OBJ_NEW_SMALL_INT(poll_obj->flags_ret); + if (self->flags & FLAG_ONESHOT) { + // Don't poll next time, until new event flags will be set explicitly + poll_obj->flags = 0; + } + return MP_OBJ_FROM_PTR(t); + } + } + + assert(!"inconsistent number of poll active entries"); + self->iter_cnt = 0; + return MP_OBJ_STOP_ITERATION; +} + STATIC const mp_rom_map_elem_t poll_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_register), MP_ROM_PTR(&poll_register_obj) }, { MP_ROM_QSTR(MP_QSTR_unregister), MP_ROM_PTR(&poll_unregister_obj) }, { MP_ROM_QSTR(MP_QSTR_modify), MP_ROM_PTR(&poll_modify_obj) }, { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&poll_poll_obj) }, + { MP_ROM_QSTR(MP_QSTR_ipoll), MP_ROM_PTR(&poll_ipoll_obj) }, }; STATIC MP_DEFINE_CONST_DICT(poll_locals_dict, poll_locals_dict_table); STATIC const mp_obj_type_t mp_type_poll = { { &mp_type_type }, .name = MP_QSTR_poll, + .getiter = mp_identity_getiter, + .iternext = poll_iternext, .locals_dict = (void*)&poll_locals_dict, }; @@ -298,6 +352,8 @@ STATIC mp_obj_t select_poll(void) { mp_obj_poll_t *poll = m_new_obj(mp_obj_poll_t); poll->base.type = &mp_type_poll; mp_map_init(&poll->poll_map, 0); + poll->iter_cnt = 0; + poll->ret_tuple = MP_OBJ_NULL; return poll; } MP_DEFINE_CONST_FUN_OBJ_0(mp_select_poll_obj, select_poll); -- cgit v1.2.3 From 4c2402e41e869772d3c9fa9b197040c5712624f6 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 29 Apr 2017 18:56:39 +0300 Subject: extmod/modlwip: getaddrinfo: Allow to accept all 6 standard params. But warn if anything else but host/port is passed. --- extmod/modlwip.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'extmod') diff --git a/extmod/modlwip.c b/extmod/modlwip.c index fffabb98a..6a1dcaef5 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -1263,7 +1263,12 @@ STATIC void lwip_getaddrinfo_cb(const char *name, ip_addr_t *ipaddr, void *arg) } // lwip.getaddrinfo -STATIC mp_obj_t lwip_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) { +STATIC mp_obj_t lwip_getaddrinfo(size_t n_args, const mp_obj_t *args) { + if (n_args > 2) { + mp_warning("getaddrinfo constraints not supported"); + } + + mp_obj_t host_in = args[0], port_in = args[1]; const char *host = mp_obj_str_get_str(host_in); mp_int_t port = mp_obj_get_int(port_in); @@ -1299,7 +1304,7 @@ STATIC mp_obj_t lwip_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) { tuple->items[4] = netutils_format_inet_addr((uint8_t*)&state.ipaddr, port, NETUTILS_BIG); return mp_obj_new_list(1, (mp_obj_t*)&tuple); } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(lwip_getaddrinfo_obj, lwip_getaddrinfo); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(lwip_getaddrinfo_obj, 2, 6, lwip_getaddrinfo); // Debug functions -- cgit v1.2.3 From 5db55e63f3e7a5beda732c241693e37e3a7b099f Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 1 May 2017 18:20:09 +0300 Subject: extmod/modlwip: ioctl POLL: Fix handling of peer closed socket. Peer-closed socket is both readable and writable: read will return EOF, write - error. Without this poll will hang on such socket. Note that we don't return POLLHUP, based on argumentation in http://www.greenend.org.uk/rjk/tech/poll.html that it should apply to deeper disconnects, for example for networking, that would be link layer disconnect (e.g. WiFi went down). --- extmod/modlwip.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'extmod') diff --git a/extmod/modlwip.c b/extmod/modlwip.c index 6a1dcaef5..c72849cf9 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -1143,8 +1143,11 @@ STATIC mp_uint_t lwip_socket_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_ ret |= MP_STREAM_POLL_WR; } - if (flags & MP_STREAM_POLL_HUP && socket->state == STATE_PEER_CLOSED) { - ret |= MP_STREAM_POLL_HUP; + if (socket->state == STATE_PEER_CLOSED) { + // Peer-closed socket is both readable and writable: read will + // return EOF, write - error. Without this poll will hang on a + // socket which was closed by peer. + ret |= flags & (MP_STREAM_POLL_RD | MP_STREAM_POLL_WR); } } else { -- cgit v1.2.3 From c9a3a68a493426826841b4c0e22f8d688a12ad03 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 10 Mar 2017 17:13:29 +1100 Subject: extmod/vfs: Allow a VFS to be mounted at the root dir. This patch allows mounting of VFS objects right at the root directory, eg os.mount(vfs, '/'). It still allows VFS's to be mounted at a path within the root, eg os.mount(vfs, '/flash'), and such mount points will override any paths within a VFS that is mounted at the root. --- extmod/vfs.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 10 deletions(-) (limited to 'extmod') diff --git a/extmod/vfs.c b/extmod/vfs.c index e389ab324..84e9fe82d 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -27,6 +27,7 @@ #include #include +#include "py/runtime0.h" #include "py/runtime.h" #include "py/objstr.h" #include "py/mperrno.h" @@ -50,8 +51,16 @@ mp_vfs_mount_t *mp_vfs_lookup_path(const char *path, const char **path_out) { ++path; is_abs = 1; } + if (*path == '\0') { + // path is "" or "/" so return virtual root + return MP_VFS_ROOT; + } for (mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) { size_t len = vfs->len - 1; + if (len == 0) { + *path_out = path - is_abs; + return vfs; + } if (strncmp(path, vfs->str + 1, len) == 0) { if (path[len] == '/') { *path_out = path + len; @@ -62,10 +71,9 @@ mp_vfs_mount_t *mp_vfs_lookup_path(const char *path, const char **path_out) { } } } - if (*path == '\0') { - // path was "" or "/" so return virtual root - return MP_VFS_ROOT; - } + + // if we get here then there's nothing mounted on / + if (is_abs) { // path began with / and was not found return MP_VFS_NONE; @@ -162,13 +170,24 @@ 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 (mp_vfs_lookup_path(mp_obj_str_get_str(pos_args[1]), &path_out) != MP_VFS_NONE) { - mp_raise_OSError(MP_EPERM); + mp_vfs_mount_t *existing_mount = mp_vfs_lookup_path(mp_obj_str_get_str(pos_args[1]), &path_out); + if (existing_mount != MP_VFS_NONE && existing_mount != MP_VFS_ROOT) { + if (vfs->len != 1 && existing_mount->len == 1) { + // if root dir is mounted, still allow to mount something within a subdir of root + } else { + // mount point in use + mp_raise_OSError(MP_EPERM); + } } // insert the vfs into the mount table mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table); while (*vfsp != NULL) { + if ((*vfsp)->len == 1) { + // make sure anything mounted at the root stays at the end of the list + vfs->next = *vfsp; + break; + } vfsp = &(*vfsp)->next; } *vfsp = vfs; @@ -228,10 +247,21 @@ 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 != MP_VFS_ROOT) { + MP_STATE_VM(vfs_cur) = vfs; + if (vfs == MP_VFS_ROOT) { + // If we change to the root dir and a VFS is mounted at the root then + // we must change that VFS's current dir to the root dir so that any + // subsequent relative paths begin at the root of that VFS. + for (vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) { + if (vfs->len == 1) { + mp_obj_t root = mp_obj_new_str("/", 1, false); + mp_vfs_proxy_call(vfs, MP_QSTR_chdir, 1, &root); + break; + } + } + } else { 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); @@ -241,6 +271,10 @@ mp_obj_t mp_vfs_getcwd(void) { 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); + if (MP_STATE_VM(vfs_cur)->len == 1) { + // don't prepend "/" for vfs mounted at root + return cwd_o; + } 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); @@ -267,8 +301,15 @@ mp_obj_t mp_vfs_listdir(size_t n_args, const mp_obj_t *args) { // 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)); + if (vfs->len == 1) { + // vfs is mounted at root dir, delegate to it + mp_obj_t root = mp_obj_new_str("/", 1, false); + mp_obj_t dir_list2 = mp_vfs_proxy_call(vfs, MP_QSTR_listdir, 1, &root); + dir_list = mp_binary_op(MP_BINARY_OP_ADD, dir_list, dir_list2); + } else { + 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; } -- cgit v1.2.3 From 87283c1974188707b436eebf2a04d4555a26dcca Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 5 May 2017 23:31:51 +1000 Subject: extmod/vfs: Implement mp_vfs_ilistdir(). uos.ilistdir() is the core function, returning an iterator that yields 3-tuples. uos.listdir() is implemented in terms of ilistdir(). --- extmod/vfs.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++------------ extmod/vfs.h | 6 +++++ 2 files changed, 70 insertions(+), 15 deletions(-) (limited to 'extmod') diff --git a/extmod/vfs.c b/extmod/vfs.c index 84e9fe82d..8db7d5e44 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -286,7 +286,49 @@ mp_obj_t mp_vfs_getcwd(void) { } 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) { +typedef struct _mp_vfs_ilistdir_it_t { + mp_obj_base_t base; + mp_fun_1_t iternext; + union { + mp_vfs_mount_t *vfs; + mp_obj_t iter; + } cur; + bool is_str; + bool is_iter; +} mp_vfs_ilistdir_it_t; + +STATIC mp_obj_t mp_vfs_ilistdir_it_iternext(mp_obj_t self_in) { + mp_vfs_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in); + if (self->is_iter) { + // continue delegating to root dir + return mp_iternext(self->cur.iter); + } else if (self->cur.vfs == NULL) { + // finished iterating mount points and no root dir is mounted + return MP_OBJ_STOP_ITERATION; + } else { + // continue iterating mount points + mp_vfs_mount_t *vfs = self->cur.vfs; + self->cur.vfs = vfs->next; + if (vfs->len == 1) { + // vfs is mounted at root dir, delegate to it + mp_obj_t root = mp_obj_new_str("/", 1, false); + self->is_iter = true; + self->cur.iter = mp_vfs_proxy_call(vfs, MP_QSTR_ilistdir, 1, &root); + return mp_iternext(self->cur.iter); + } else { + // a mounted directory + mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL)); + t->items[0] = mp_obj_new_str_of_type( + self->is_str ? &mp_type_str : &mp_type_bytes, + (const byte*)vfs->str + 1, vfs->len - 1); + t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR); + t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // no inode number + return MP_OBJ_FROM_PTR(t); + } + } +} + +mp_obj_t mp_vfs_ilistdir(size_t n_args, const mp_obj_t *args) { mp_obj_t path_in; if (n_args == 1) { path_in = args[0]; @@ -299,22 +341,29 @@ mp_obj_t mp_vfs_listdir(size_t n_args, const mp_obj_t *args) { 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) { - if (vfs->len == 1) { - // vfs is mounted at root dir, delegate to it - mp_obj_t root = mp_obj_new_str("/", 1, false); - mp_obj_t dir_list2 = mp_vfs_proxy_call(vfs, MP_QSTR_listdir, 1, &root); - dir_list = mp_binary_op(MP_BINARY_OP_ADD, dir_list, dir_list2); - } else { - 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; + mp_vfs_ilistdir_it_t *iter = m_new_obj(mp_vfs_ilistdir_it_t); + iter->base.type = &mp_type_polymorph_iter; + iter->iternext = mp_vfs_ilistdir_it_iternext; + iter->cur.vfs = MP_STATE_VM(vfs_mount_table); + iter->is_str = mp_obj_get_type(path_in) == &mp_type_str; + iter->is_iter = false; + return MP_OBJ_FROM_PTR(iter); } - return mp_vfs_proxy_call(vfs, MP_QSTR_listdir, 1, &path_out); + return mp_vfs_proxy_call(vfs, MP_QSTR_ilistdir, 1, &path_out); +} +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_vfs_ilistdir_obj, 0, 1, mp_vfs_ilistdir); + +mp_obj_t mp_vfs_listdir(size_t n_args, const mp_obj_t *args) { + mp_obj_t iter = mp_vfs_ilistdir(n_args, args); + mp_obj_t dir_list = mp_obj_new_list(0, NULL); + mp_obj_t next; + while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) { + mp_obj_t *items; + mp_obj_get_array_fixed_n(next, 3, &items); + mp_obj_list_append(dir_list, items[0]); + } + return dir_list; } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_vfs_listdir_obj, 0, 1, mp_vfs_listdir); diff --git a/extmod/vfs.h b/extmod/vfs.h index 4a1c225a0..edaeb5349 100644 --- a/extmod/vfs.h +++ b/extmod/vfs.h @@ -35,6 +35,10 @@ #define MP_VFS_NONE ((mp_vfs_mount_t*)1) #define MP_VFS_ROOT ((mp_vfs_mount_t*)0) +// MicroPython's port-standardized versions of stat constants +#define MP_S_IFDIR (0x4000) +#define MP_S_IFREG (0x8000) + // constants for block protocol ioctl #define BP_IOCTL_INIT (1) #define BP_IOCTL_DEINIT (2) @@ -56,6 +60,7 @@ 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_ilistdir(size_t n_args, const mp_obj_t *args); 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); @@ -69,6 +74,7 @@ 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_ilistdir_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); -- cgit v1.2.3 From d4cd4831b01ff87b4a9f84bb88b165e3b156b3b4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 5 May 2017 23:32:44 +1000 Subject: extmod/vfs_fat: Replace listdir() with implementation of ilistdir(). VfsFat no longer has the listdir() method. Rather, if listdir() functionality is needed then one should use uos.listdir() which will call VfsFat.ilistdir(). --- extmod/vfs_fat.c | 8 +++--- extmod/vfs_fat.h | 2 +- extmod/vfs_fat_misc.c | 73 ++++++++++++++++++++++++++++++--------------------- 3 files changed, 48 insertions(+), 35 deletions(-) (limited to 'extmod') diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index dee4f9298..41c32c6b6 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -91,7 +91,7 @@ STATIC MP_DEFINE_CONST_STATICMETHOD_OBJ(fat_vfs_mkfs_obj, MP_ROM_PTR(&fat_vfs_mk 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) { +STATIC mp_obj_t fat_vfs_ilistdir_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; @@ -104,9 +104,9 @@ STATIC mp_obj_t fat_vfs_listdir_func(size_t n_args, const mp_obj_t *args) { path = ""; } - return fat_vfs_listdir2(self, path, is_str_type); + return fat_vfs_ilistdir2(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_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_ilistdir_obj, 1, 2, fat_vfs_ilistdir_func); 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); @@ -321,7 +321,7 @@ 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) }, { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&fat_vfs_open_obj) }, - { MP_ROM_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&fat_vfs_listdir_obj) }, + { MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&fat_vfs_ilistdir_obj) }, { MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&fat_vfs_mkdir_obj) }, { MP_ROM_QSTR(MP_QSTR_rmdir), MP_ROM_PTR(&fat_vfs_rmdir_obj) }, { MP_ROM_QSTR(MP_QSTR_chdir), MP_ROM_PTR(&fat_vfs_chdir_obj) }, diff --git a/extmod/vfs_fat.h b/extmod/vfs_fat.h index 7eb865254..6c7c05a9a 100644 --- a/extmod/vfs_fat.h +++ b/extmod/vfs_fat.h @@ -57,4 +57,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_listdir2(struct _fs_user_mount_t *vfs, const char *path, bool is_str_type); +mp_obj_t fat_vfs_ilistdir2(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 19db99c7f..5b906189f 100644 --- a/extmod/vfs_fat_misc.c +++ b/extmod/vfs_fat_misc.c @@ -34,51 +34,64 @@ #include "extmod/vfs_fat.h" #include "py/lexer.h" -// TODO: actually, the core function should be ilistdir() - -mp_obj_t fat_vfs_listdir2(fs_user_mount_t *vfs, const char *path, bool is_str_type) { - FRESULT res; - FILINFO fno; +typedef struct _mp_vfs_fat_ilistdir_it_t { + mp_obj_base_t base; + mp_fun_1_t iternext; + bool is_str; FF_DIR dir; +} mp_vfs_fat_ilistdir_it_t; - res = f_opendir(&vfs->fatfs, &dir, path); - if (res != FR_OK) { - mp_raise_OSError(fresult_to_errno_table[res]); - } - - mp_obj_t dir_list = mp_obj_new_list(0, NULL); +STATIC mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) { + mp_vfs_fat_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in); for (;;) { - res = f_readdir(&dir, &fno); /* Read a directory item */ - if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */ - if (fno.fname[0] == '.' && fno.fname[1] == 0) continue; /* Ignore . entry */ - if (fno.fname[0] == '.' && fno.fname[1] == '.' && fno.fname[2] == 0) continue; /* Ignore .. entry */ - + FILINFO fno; + FRESULT res = f_readdir(&self->dir, &fno); char *fn = fno.fname; + if (res != FR_OK || fn[0] == 0) { + // stop on error or end of dir + break; + } + if (fn[0] == '.' && (fn[1] == 0 || (fn[1] == '.' && fn[2] == 0))) { + // skip . and .. + continue; + } - /* + // make 3-tuple with info about this entry + mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL)); + if (self->is_str) { + t->items[0] = mp_obj_new_str(fn, strlen(fn), false); + } else { + t->items[0] = mp_obj_new_bytes((const byte*)fn, strlen(fn)); + } if (fno.fattrib & AM_DIR) { // dir + t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR); } else { // file + t->items[1] = MP_OBJ_NEW_SMALL_INT(MP_S_IFREG); } - */ + t->items[2] = MP_OBJ_NEW_SMALL_INT(0); // no inode number - // make a string object for this entry - mp_obj_t entry_o; - if (is_str_type) { - entry_o = mp_obj_new_str(fn, strlen(fn), false); - } else { - entry_o = mp_obj_new_bytes((const byte*)fn, strlen(fn)); - } - - // add the entry to the list - mp_obj_list_append(dir_list, entry_o); + return MP_OBJ_FROM_PTR(t); } - f_closedir(&dir); + // ignore error because we may be closing a second time + f_closedir(&self->dir); - return dir_list; + return MP_OBJ_STOP_ITERATION; +} + +mp_obj_t fat_vfs_ilistdir2(fs_user_mount_t *vfs, const char *path, bool is_str_type) { + mp_vfs_fat_ilistdir_it_t *iter = m_new_obj(mp_vfs_fat_ilistdir_it_t); + iter->base.type = &mp_type_polymorph_iter; + iter->iternext = mp_vfs_fat_ilistdir_it_iternext; + iter->is_str = is_str_type; + FRESULT res = f_opendir(&vfs->fatfs, &iter->dir, path); + if (res != FR_OK) { + mp_raise_OSError(fresult_to_errno_table[res]); + } + return MP_OBJ_FROM_PTR(iter); } mp_import_stat_t fat_vfs_import_stat(fs_user_mount_t *vfs, const char *path) { -- cgit v1.2.3 From d70f688f25a76e1e6a251a4ffc5144539c1a4e64 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 10 May 2017 12:30:34 +1000 Subject: extmod/vfs: Use MP_S_IFDIR, MP_S_IFREG consts instead of magic numbers. --- extmod/vfs.c | 2 +- extmod/vfs_fat.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'extmod') diff --git a/extmod/vfs.c b/extmod/vfs.c index 8db7d5e44..f158bd387 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -408,7 +408,7 @@ mp_obj_t mp_vfs_stat(mp_obj_t path_in) { mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out); if (vfs == MP_VFS_ROOT) { mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL)); - t->items[0] = MP_OBJ_NEW_SMALL_INT(0x4000); // st_mode = stat.S_IFDIR + t->items[0] = MP_OBJ_NEW_SMALL_INT(MP_S_IFDIR); // st_mode for (int i = 1; i <= 9; ++i) { t->items[i] = MP_OBJ_NEW_SMALL_INT(0); // dev, nlink, uid, gid, size, atime, mtime, ctime } diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index 41c32c6b6..0ec3fe6d2 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -225,9 +225,9 @@ STATIC mp_obj_t fat_vfs_stat(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)); mp_int_t mode = 0; if (fno.fattrib & AM_DIR) { - mode |= 0x4000; // stat.S_IFDIR + mode |= MP_S_IFDIR; } else { - mode |= 0x8000; // stat.S_IFREG + mode |= MP_S_IFREG; } mp_int_t seconds = timeutils_seconds_since_2000( 1980 + ((fno.fdate >> 9) & 0x7f), -- cgit v1.2.3 From f95e4e77823919dfe82b6711f1d25d2d8c5008fc Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 13 May 2017 18:58:46 +1000 Subject: extmod/vfs_fat_misc: Remove dot-dirs filter since FatFS already does it. --- extmod/vfs_fat_misc.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'extmod') diff --git a/extmod/vfs_fat_misc.c b/extmod/vfs_fat_misc.c index 5b906189f..7c16db7e5 100644 --- a/extmod/vfs_fat_misc.c +++ b/extmod/vfs_fat_misc.c @@ -52,10 +52,8 @@ STATIC mp_obj_t mp_vfs_fat_ilistdir_it_iternext(mp_obj_t self_in) { // stop on error or end of dir break; } - if (fn[0] == '.' && (fn[1] == 0 || (fn[1] == '.' && fn[2] == 0))) { - // skip . and .. - continue; - } + + // Note that FatFS already filters . and .., so we don't need to // make 3-tuple with info about this entry mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL)); -- cgit v1.2.3 From ca16c3821053e5bf2b87aeb10007f73f31dc1eac Mon Sep 17 00:00:00 2001 From: Ville Skyttä Date: Mon, 29 May 2017 10:08:14 +0300 Subject: various: Spelling fixes --- cc3200/README.md | 2 +- docs/library/btree.rst | 2 +- docs/library/machine.SD.rst | 2 +- docs/library/machine.UART.rst | 2 +- docs/library/uhashlib.rst | 4 ++-- docs/library/utime.rst | 4 ++-- docs/sphinx_selective_exclude/README.md | 2 +- docs/sphinx_selective_exclude/modindex_exclude.py | 2 +- esp8266/README.md | 2 +- esp8266/machine_rtc.c | 2 +- examples/conwaylife.py | 4 ++-- examples/embedding/Makefile.upylib | 2 +- examples/embedding/README.md | 2 +- extmod/modlwip.c | 4 ++-- extmod/modwebsocket.c | 2 +- lib/timeutils/timeutils.c | 2 +- lib/utils/stdout_helpers.c | 2 +- py/asmthumb.c | 2 +- py/builtinimport.c | 4 ++-- py/compile.c | 4 ++-- py/misc.h | 2 +- py/mkenv.mk | 2 +- py/mpconfig.h | 2 +- py/obj.c | 2 +- py/objstr.c | 4 ++-- py/py.mk | 2 +- py/ringbuf.h | 2 +- py/stream.c | 2 +- py/vm.c | 4 ++-- qemu-arm/README.md | 2 +- tests/basics/namedtuple1.py | 2 +- tests/basics/try_reraise2.py | 2 +- tests/pyb/can.py | 2 +- tests/thread/stress_aes.py | 2 +- tests/wipy/uart.py | 2 +- tools/insert-usb-ids.py | 2 +- tools/pyboard.py | 2 +- unix/Makefile | 2 +- unix/modsocket.c | 2 +- windows/windows_mphal.c | 2 +- zephyr/modutime.c | 2 +- 41 files changed, 49 insertions(+), 49 deletions(-) (limited to 'extmod') diff --git a/cc3200/README.md b/cc3200/README.md index 753fd450a..53cad3ba0 100644 --- a/cc3200/README.md +++ b/cc3200/README.md @@ -138,7 +138,7 @@ If `WIPY_IP`, `WIPY_USER` or `WIPY_PWD` are omitted the default values (the ones ## Regarding old revisions of the CC3200-LAUNCHXL First silicon (pre-release) revisions of the CC3200 had issues with the ram blocks, and MicroPython cannot run -there. Make sure to use a **v4.1 (or higer) LAUNCHXL board** when trying this port, otherwise it won't work. +there. Make sure to use a **v4.1 (or higher) LAUNCHXL board** when trying this port, otherwise it won't work. ### Note regarding FileZilla diff --git a/docs/library/btree.rst b/docs/library/btree.rst index aebcbc160..bd7890586 100644 --- a/docs/library/btree.rst +++ b/docs/library/btree.rst @@ -69,7 +69,7 @@ Functions Open a database from a random-access `stream` (like an open file). All other parameters are optional and keyword-only, and allow to tweak advanced - paramters of the database operation (most users will not need them): + parameters of the database operation (most users will not need them): * `flags` - Currently unused. * `cachesize` - Suggested maximum memory cache size in bytes. For a diff --git a/docs/library/machine.SD.rst b/docs/library/machine.SD.rst index 0eb024602..608e95831 100644 --- a/docs/library/machine.SD.rst +++ b/docs/library/machine.SD.rst @@ -34,7 +34,7 @@ Methods .. method:: SD.init(id=0, pins=('GP10', 'GP11', 'GP15')) - Enable the SD card. In order to initalize the card, give it a 3-tuple: + Enable the SD card. In order to initialize the card, give it a 3-tuple: ``(clk_pin, cmd_pin, dat0_pin)``. .. method:: SD.deinit() diff --git a/docs/library/machine.UART.rst b/docs/library/machine.UART.rst index f9c8efef7..64ff28e1a 100644 --- a/docs/library/machine.UART.rst +++ b/docs/library/machine.UART.rst @@ -16,7 +16,7 @@ UART objects can be created and initialised using:: uart = UART(1, 9600) # init with given baudrate uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters -Supported paramters differ on a board: +Supported parameters differ on a board: Pyboard: Bits can be 7, 8 or 9. Stop can be 1 or 2. With `parity=None`, only 8 and 9 bits are supported. With parity enabled, only 7 and 8 bits diff --git a/docs/library/uhashlib.rst b/docs/library/uhashlib.rst index cd0216dae..6b9a764ba 100644 --- a/docs/library/uhashlib.rst +++ b/docs/library/uhashlib.rst @@ -15,11 +15,11 @@ be implemented: * SHA1 - A previous generation algorithm. Not recommended for new usages, but SHA1 is a part of number of Internet standards and existing - applications, so boards targetting network connectivity and + applications, so boards targeting network connectivity and interoperatiability will try to provide this. * MD5 - A legacy algorithm, not considered cryptographically secure. Only - selected boards, targetting interoperatibility with legacy applications, + selected boards, targeting interoperatibility with legacy applications, will offer this. Constructors diff --git a/docs/library/utime.rst b/docs/library/utime.rst index 871f6c678..f3a067cde 100644 --- a/docs/library/utime.rst +++ b/docs/library/utime.rst @@ -146,8 +146,8 @@ Functions too distant inbetween, see below). The function returns **signed** value in the range [``-TICKS_PERIOD/2`` .. ``TICKS_PERIOD/2-1``] (that's a typical range definition for two's-complement signed binary integers). If the result is negative, it means that - ``ticks1`` occured earlier in time than ``ticks2``. Otherwise, it means that - ``ticks1`` occured after ``ticks2``. This holds ``only`` if ``ticks1`` and ``ticks2`` + ``ticks1`` occurred earlier in time than ``ticks2``. Otherwise, it means that + ``ticks1`` occurred after ``ticks2``. This holds ``only`` if ``ticks1`` and ``ticks2`` are apart from each other for no more than ``TICKS_PERIOD/2-1`` ticks. If that does not hold, incorrect result will be returned. Specifically, if two tick values are apart for ``TICKS_PERIOD/2-1`` ticks, that value will be returned by the function. diff --git a/docs/sphinx_selective_exclude/README.md b/docs/sphinx_selective_exclude/README.md index cc9725c21..dab140739 100644 --- a/docs/sphinx_selective_exclude/README.md +++ b/docs/sphinx_selective_exclude/README.md @@ -66,7 +66,7 @@ index for PDF, just the same as for HTML. search_auto_exclude ------------------- -Even if you exclude soem documents from toctree:: using only:: +Even if you exclude some documents from toctree:: using only:: directive, they will be indexed for full-text search, so user may find them and get confused. This plugin follows very simple idea that if you didn't include some documents in the toctree, then diff --git a/docs/sphinx_selective_exclude/modindex_exclude.py b/docs/sphinx_selective_exclude/modindex_exclude.py index 18b49cc80..bf8db795e 100644 --- a/docs/sphinx_selective_exclude/modindex_exclude.py +++ b/docs/sphinx_selective_exclude/modindex_exclude.py @@ -2,7 +2,7 @@ # This is a Sphinx documentation tool extension which allows to # exclude some Python modules from the generated indexes. Modules # are excluded both from "modindex" and "genindex" index tables -# (in the latter case, all members of a module are exlcuded). +# (in the latter case, all members of a module are excluded). # To control exclusion, set "modindex_exclude" variable in Sphinx # conf.py to the list of modules to exclude. Note: these should be # modules (as defined by py:module directive, not just raw filenames). diff --git a/esp8266/README.md b/esp8266/README.md index 897bb4737..d717d26fe 100644 --- a/esp8266/README.md +++ b/esp8266/README.md @@ -100,7 +100,7 @@ programming). __WiFi__ -Initally, the device configures itself as a WiFi access point (AP). +Initially, the device configures itself as a WiFi access point (AP). - ESSID: MicroPython-xxxxxx (x’s are replaced with part of the MAC address). - Password: micropythoN (note the upper-case N). - IP address of the board: 192.168.4.1. diff --git a/esp8266/machine_rtc.c b/esp8266/machine_rtc.c index 019b705ba..b17bcb261 100644 --- a/esp8266/machine_rtc.c +++ b/esp8266/machine_rtc.c @@ -93,7 +93,7 @@ void pyb_rtc_set_us_since_2000(uint64_t nowus) { int64_t delta = nowus - (((uint64_t)rtc_last_ticks * cal) >> 12); // As the calibration value jitters quite a bit, to make the - // clock at least somewhat practially usable, we need to store it + // clock at least somewhat practically usable, we need to store it system_rtc_mem_write(MEM_CAL_ADDR, &cal, sizeof(cal)); system_rtc_mem_write(MEM_DELTA_ADDR, &delta, sizeof(delta)); }; diff --git a/examples/conwaylife.py b/examples/conwaylife.py index f99796175..323f42e85 100644 --- a/examples/conwaylife.py +++ b/examples/conwaylife.py @@ -8,7 +8,7 @@ lcd.light(1) def conway_step(): for x in range(128): # loop over x coordinates for y in range(32): # loop over y coordinates - # count number of neigbours + # count number of neighbours num_neighbours = (lcd.get(x - 1, y - 1) + lcd.get(x, y - 1) + lcd.get(x + 1, y - 1) + @@ -25,7 +25,7 @@ def conway_step(): if self and not (2 <= num_neighbours <= 3): lcd.pixel(x, y, 0) # not enough, or too many neighbours: cell dies elif not self and num_neighbours == 3: - lcd.pixel(x, y, 1) # exactly 3 neigbours around an empty cell: cell is born + lcd.pixel(x, y, 1) # exactly 3 neighbours around an empty cell: cell is born # randomise the start def conway_rand(): diff --git a/examples/embedding/Makefile.upylib b/examples/embedding/Makefile.upylib index 873c0fd34..4663ad30a 100644 --- a/examples/embedding/Makefile.upylib +++ b/examples/embedding/Makefile.upylib @@ -170,7 +170,7 @@ SRC_QSTR_AUTO_DEPS += include $(MPTOP)/py/mkrules.mk # Value of configure's --host= option (required for cross-compilation). -# Deduce it from CROSS_COMPILE by default, but can be overriden. +# Deduce it from CROSS_COMPILE by default, but can be overridden. ifneq ($(CROSS_COMPILE),) CROSS_COMPILE_HOST = --host=$(patsubst %-,%,$(CROSS_COMPILE)) else diff --git a/examples/embedding/README.md b/examples/embedding/README.md index 989ce1fc8..804dfede6 100644 --- a/examples/embedding/README.md +++ b/examples/embedding/README.md @@ -18,7 +18,7 @@ Building the example is as simple as running: It's worth to trace what's happening behind the scenes though: 1. As a first step, a MicroPython library is built. This is handled by a -seperate makefile, Makefile.upylib. It is more or less complex, but the +separate makefile, Makefile.upylib. It is more or less complex, but the good news is that you won't need to change anything in it, just use it as is, the main Makefile shows how. What may require editing though is a MicroPython configuration file. MicroPython is highly configurable, so diff --git a/extmod/modlwip.c b/extmod/modlwip.c index c72849cf9..47669cb3a 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -373,7 +373,7 @@ STATIC err_t _lwip_tcp_recv(void *arg, struct tcp_pcb *tcpb, struct pbuf *p, err } /*******************************************************************************/ -// Functions for socket send/recieve operations. Socket send/recv and friends call +// Functions for socket send/receive operations. Socket send/recv and friends call // these to do the work. // Helper function for send/sendto to handle UDP packets. @@ -805,7 +805,7 @@ STATIC mp_obj_t lwip_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) { mp_raise_OSError(MP_EINPROGRESS); } } - // Register our recieve callback. + // Register our receive callback. tcp_recv(socket->pcb.tcp, _lwip_tcp_recv); socket->state = STATE_CONNECTING; err = tcp_connect(socket->pcb.tcp, &dest, port, _lwip_tcp_connected); diff --git a/extmod/modwebsocket.c b/extmod/modwebsocket.c index 8200ea708..9e17d6a6d 100644 --- a/extmod/modwebsocket.c +++ b/extmod/modwebsocket.c @@ -132,7 +132,7 @@ STATIC mp_uint_t websocket_read(mp_obj_t self_in, void *buf, mp_uint_t size, int self->buf_pos = 0; self->to_recv = to_recv; - self->msg_sz = sz; // May be overriden by FRAME_OPT + self->msg_sz = sz; // May be overridden by FRAME_OPT if (to_recv != 0) { self->state = FRAME_OPT; } else { diff --git a/lib/timeutils/timeutils.c b/lib/timeutils/timeutils.c index 0af39a295..06915f25a 100644 --- a/lib/timeutils/timeutils.c +++ b/lib/timeutils/timeutils.c @@ -165,7 +165,7 @@ mp_uint_t timeutils_mktime(mp_uint_t year, mp_int_t month, mp_int_t mday, // // tm_tomorrow = list(time.localtime()) // tm_tomorrow[2] += 1 # Adds 1 to mday - // tomorrow = time.mktime(tm_tommorrow) + // tomorrow = time.mktime(tm_tomorrow) // // And not have to worry about all the weird overflows. // diff --git a/lib/utils/stdout_helpers.c b/lib/utils/stdout_helpers.c index 5f7a17d32..3de119757 100644 --- a/lib/utils/stdout_helpers.c +++ b/lib/utils/stdout_helpers.c @@ -9,7 +9,7 @@ * implementation below can be used. */ -// Send "cooked" string of given length, where every occurance of +// Send "cooked" string of given length, where every occurrence of // LF character is replaced with CR LF. void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) { while (len--) { diff --git a/py/asmthumb.c b/py/asmthumb.c index 749c1e405..7e92e4de4 100644 --- a/py/asmthumb.c +++ b/py/asmthumb.c @@ -52,7 +52,7 @@ void asm_thumb_end_pass(asm_thumb_t *as) { #if defined(MCU_SERIES_F7) if (as->base.pass == MP_ASM_PASS_EMIT) { - // flush D-cache, so the code emited is stored in memory + // flush D-cache, so the code emitted is stored in memory SCB_CleanDCache_by_Addr((uint32_t*)as->base.code_base, as->base.code_size); // invalidate I-cache SCB_InvalidateICache(); diff --git a/py/builtinimport.c b/py/builtinimport.c index d01ebbe73..6994fc48f 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -271,7 +271,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { if (level != 0) { // What we want to do here is to take name of current module, // chop trailing components, and concatenate with passed-in - // module name, thus resolving relative import name into absolue. + // module name, thus resolving relative import name into absolute. // This even appears to be correct per // http://legacy.python.org/dev/peps/pep-0328/#relative-imports-and-name // "Relative imports use a module's __name__ attribute to determine that @@ -441,7 +441,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { #if MICROPY_CPYTHON_COMPAT // Store module as "__main__" in the dictionary of loaded modules (returned by sys.modules). mp_obj_dict_store(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_loaded_modules_dict)), MP_OBJ_NEW_QSTR(MP_QSTR___main__), module_obj); - // Store real name in "__main__" attribute. Choosen semi-randonly, to reuse existing qstr's. + // Store real name in "__main__" attribute. Chosen semi-randonly, to reuse existing qstr's. mp_obj_dict_store(MP_OBJ_FROM_PTR(o->globals), MP_OBJ_NEW_QSTR(MP_QSTR___main__), MP_OBJ_NEW_QSTR(mod_name)); #endif } diff --git a/py/compile.c b/py/compile.c index 8533e0528..3b6a264d6 100644 --- a/py/compile.c +++ b/py/compile.c @@ -939,7 +939,7 @@ STATIC void c_del_stmt(compiler_t *comp, mp_parse_node_t pn) { } } } else { - // some arbitrary statment that we can't delete (eg del 1) + // some arbitrary statement that we can't delete (eg del 1) goto cannot_delete; } @@ -1090,7 +1090,7 @@ STATIC void compile_import_name(compiler_t *comp, mp_parse_node_struct_t *pns) { STATIC void compile_import_from(compiler_t *comp, mp_parse_node_struct_t *pns) { mp_parse_node_t pn_import_source = pns->nodes[0]; - // extract the preceeding .'s (if any) for a relative import, to compute the import level + // extract the preceding .'s (if any) for a relative import, to compute the import level uint import_level = 0; do { mp_parse_node_t pn_rel; diff --git a/py/misc.h b/py/misc.h index 146b9a8e4..caa5945bf 100644 --- a/py/misc.h +++ b/py/misc.h @@ -197,7 +197,7 @@ int DEBUG_printf(const char *fmt, ...); extern mp_uint_t mp_verbose_flag; // This is useful for unicode handling. Some CPU archs has -// special instructions for efficient implentation of this +// special instructions for efficient implementation of this // function (e.g. CLZ on ARM). // NOTE: this function is unused at the moment #ifndef count_lead_ones diff --git a/py/mkenv.mk b/py/mkenv.mk index eb1e44fef..b167b2533 100644 --- a/py/mkenv.mk +++ b/py/mkenv.mk @@ -32,7 +32,7 @@ ifeq ($(BUILD_VERBOSE),0) $(info Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.) endif -# default settings; can be overriden in main Makefile +# default settings; can be overridden in main Makefile PY_SRC ?= $(TOP)/py BUILD ?= build diff --git a/py/mpconfig.h b/py/mpconfig.h index a61d431e5..78e346d73 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -32,7 +32,7 @@ // mpconfigport.h is a file containing configuration settings for a // particular port. mpconfigport.h is actually a default name for -// such config, and it can be overriden using MP_CONFIGFILE preprocessor +// such config, and it can be overridden using MP_CONFIGFILE preprocessor // define (you can do that by passing CFLAGS_EXTRA='-DMP_CONFIGFILE=""' // argument to make when using standard MicroPython makefiles). // This is useful to have more than one config per port, for example, diff --git a/py/obj.c b/py/obj.c index 98ffa930b..493945a22 100644 --- a/py/obj.c +++ b/py/obj.c @@ -401,7 +401,7 @@ mp_obj_t mp_obj_id(mp_obj_t o_in) { return MP_OBJ_NEW_SMALL_INT(id); } else { // If that didn't work, well, let's return long int, just as - // a (big) positve value, so it will never clash with the range + // a (big) positive value, so it will never clash with the range // of small int returned in previous case. return mp_obj_new_int_from_uint((mp_uint_t)id); } diff --git a/py/objstr.c b/py/objstr.c index 70de0a693..a1e223572 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -798,7 +798,7 @@ STATIC mp_obj_t str_uni_strip(int type, size_t n_args, const mp_obj_t *args) { } assert(last_good_char_pos >= first_good_char_pos); - //+1 to accomodate the last character + //+1 to accommodate the last character size_t stripped_len = last_good_char_pos - first_good_char_pos + 1; if (stripped_len == orig_str_len) { // If nothing was stripped, don't bother to dup original string @@ -1811,7 +1811,7 @@ STATIC mp_obj_t str_islower(mp_obj_t self_in) { } #if MICROPY_CPYTHON_COMPAT -// These methods are superfluous in the presense of str() and bytes() +// These methods are superfluous in the presence of str() and bytes() // constructors. // TODO: should accept kwargs too STATIC mp_obj_t bytes_decode(size_t n_args, const mp_obj_t *args) { diff --git a/py/py.mk b/py/py.mk index 5ff1fd6a6..70891677d 100644 --- a/py/py.mk +++ b/py/py.mk @@ -25,7 +25,7 @@ ifeq ($(MICROPY_SSL_AXTLS),1) CFLAGS_MOD += -DMICROPY_SSL_AXTLS=1 -I../lib/axtls/ssl -I../lib/axtls/crypto -I../lib/axtls/config LDFLAGS_MOD += -Lbuild -laxtls else ifeq ($(MICROPY_SSL_MBEDTLS),1) -# Can be overriden by ports which have "builtin" mbedTLS +# Can be overridden by ports which have "builtin" mbedTLS MICROPY_SSL_MBEDTLS_INCLUDE ?= ../lib/mbedtls/include CFLAGS_MOD += -DMICROPY_SSL_MBEDTLS=1 -I$(MICROPY_SSL_MBEDTLS_INCLUDE) LDFLAGS_MOD += -L../lib/mbedtls/library -lmbedx509 -lmbedtls -lmbedcrypto diff --git a/py/ringbuf.h b/py/ringbuf.h index 5662594f7..5e108afad 100644 --- a/py/ringbuf.h +++ b/py/ringbuf.h @@ -33,7 +33,7 @@ typedef struct _ringbuf_t { uint16_t iput; } ringbuf_t; -// Static initalization: +// Static initialization: // byte buf_array[N]; // ringbuf_t buf = {buf_array, sizeof(buf_array)}; diff --git a/py/stream.c b/py/stream.c index c915110e0..d3fc767bb 100644 --- a/py/stream.c +++ b/py/stream.c @@ -51,7 +51,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in); #define STREAM_CONTENT_TYPE(stream) (((stream)->is_text) ? &mp_type_str : &mp_type_bytes) // Returns error condition in *errcode, if non-zero, return value is number of bytes written -// before error condition occured. If *errcode == 0, returns total bytes written (which will +// before error condition occurred. If *errcode == 0, returns total bytes written (which will // be equal to input size). mp_uint_t mp_stream_rw(mp_obj_t stream, void *buf_, mp_uint_t size, int *errcode, byte flags) { byte *buf = buf_; diff --git a/py/vm.c b/py/vm.c index 5094e3e45..ad3d9e29c 100644 --- a/py/vm.c +++ b/py/vm.c @@ -947,7 +947,7 @@ unwind_jump:; DECODE_UINT; // unum & 0xff == n_positional // (unum >> 8) & 0xff == n_keyword - // We have folowing stack layout here: + // We have following stack layout here: // fun arg0 arg1 ... kw0 val0 kw1 val1 ... seq dict <- TOS sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 2; #if MICROPY_STACKLESS @@ -1018,7 +1018,7 @@ unwind_jump:; DECODE_UINT; // unum & 0xff == n_positional // (unum >> 8) & 0xff == n_keyword - // We have folowing stack layout here: + // We have following stack layout here: // fun self arg0 arg1 ... kw0 val0 kw1 val1 ... seq dict <- TOS sp -= (unum & 0xff) + ((unum >> 7) & 0x1fe) + 3; #if MICROPY_STACKLESS diff --git a/qemu-arm/README.md b/qemu-arm/README.md index 329ae4d92..0cf93c7d5 100644 --- a/qemu-arm/README.md +++ b/qemu-arm/README.md @@ -4,7 +4,7 @@ provided by QEMU (http://qemu.org). The purposes of this port are to enable: 1. Continuous integration - - run tests agains architecture-specific parts of code base + - run tests against architecture-specific parts of code base 2. Experimentation - simulation & prototyping of anything that has architecture-specific code diff --git a/tests/basics/namedtuple1.py b/tests/basics/namedtuple1.py index 132dcf96b..70372f7ca 100644 --- a/tests/basics/namedtuple1.py +++ b/tests/basics/namedtuple1.py @@ -76,7 +76,7 @@ T4 = namedtuple("TupTuple", ("foo", "bar")) t = T4(1, 2) print(t.foo, t.bar) -# Try single string with comma field seperator +# Try single string with comma field separator # Not implemented so far #T2 = namedtuple("TupComma", "foo,bar") #t = T2(1, 2) diff --git a/tests/basics/try_reraise2.py b/tests/basics/try_reraise2.py index d9434397c..5648d2467 100644 --- a/tests/basics/try_reraise2.py +++ b/tests/basics/try_reraise2.py @@ -1,4 +1,4 @@ -# Reraise not the latest occured exception +# Reraise not the latest occurred exception def f(): try: raise ValueError("val", 3) diff --git a/tests/pyb/can.py b/tests/pyb/can.py index 617eb7ccc..7f2d070ec 100644 --- a/tests/pyb/can.py +++ b/tests/pyb/can.py @@ -158,7 +158,7 @@ print(can.recv(1)) del can -# Testing asyncronous send +# Testing asynchronous send can = CAN(1, CAN.LOOPBACK) can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0)) diff --git a/tests/thread/stress_aes.py b/tests/thread/stress_aes.py index ecc963c92..df75e616c 100644 --- a/tests/thread/stress_aes.py +++ b/tests/thread/stress_aes.py @@ -8,7 +8,7 @@ # # The AES code comes first (code originates from a C version authored by D.P.George) # and then the test harness at the bottom. It can be tuned to be more/less -# agressive by changing the amount of data to encrypt, the number of loops and +# aggressive by changing the amount of data to encrypt, the number of loops and # the number of threads. # # MIT license; Copyright (c) 2016 Damien P. George on behalf of Pycom Ltd diff --git a/tests/wipy/uart.py b/tests/wipy/uart.py index a3a1c14e8..8e794015d 100644 --- a/tests/wipy/uart.py +++ b/tests/wipy/uart.py @@ -95,7 +95,7 @@ print(uart1.read() == None) print(uart1.write(b'123') == 3) print(uart0.read() == b'123') -# no pin assignemnt +# no pin assignment uart0 = UART(0, 1000000, pins=(None, None)) print(uart0.write(b'123456789') == 9) print(uart1.read() == None) diff --git a/tools/insert-usb-ids.py b/tools/insert-usb-ids.py index 420db34c5..cdccd3be9 100644 --- a/tools/insert-usb-ids.py +++ b/tools/insert-usb-ids.py @@ -1,4 +1,4 @@ -# Reads the USB VID and PID from the file specifed by sys.arg[1] and then +# Reads the USB VID and PID from the file specified by sys.argv[1] and then # inserts those values into the template file specified by sys.argv[2], # printing the result to stdout diff --git a/tools/pyboard.py b/tools/pyboard.py index 5eac030bd..921ffc52d 100755 --- a/tools/pyboard.py +++ b/tools/pyboard.py @@ -69,7 +69,7 @@ class TelnetToSerial: self.tn.write(bytes(password, 'ascii') + b"\r\n") if b'for more information.' in self.tn.read_until(b'Type "help()" for more information.', timeout=read_timeout): - # login succesful + # login successful from collections import deque self.fifo = deque() return diff --git a/unix/Makefile b/unix/Makefile index 837ddf2b7..006bce0ef 100644 --- a/unix/Makefile +++ b/unix/Makefile @@ -262,7 +262,7 @@ coverage_test: coverage gcov -o build-coverage/extmod ../extmod/*.c # Value of configure's --host= option (required for cross-compilation). -# Deduce it from CROSS_COMPILE by default, but can be overriden. +# Deduce it from CROSS_COMPILE by default, but can be overridden. ifneq ($(CROSS_COMPILE),) CROSS_COMPILE_HOST = --host=$(patsubst %-,%,$(CROSS_COMPILE)) else diff --git a/unix/modsocket.c b/unix/modsocket.c index 9ca04b88b..c7be6461e 100644 --- a/unix/modsocket.c +++ b/unix/modsocket.c @@ -58,7 +58,7 @@ from socket_more_funcs2 import * ------------------- I.e. this module should stay lean, and more functions (if needed) - should be add to seperate modules (C or Python level). + should be add to separate modules (C or Python level). */ #define MICROPY_SOCKET_EXTRA (0) diff --git a/windows/windows_mphal.c b/windows/windows_mphal.c index 1dd3105d8..a73140e54 100644 --- a/windows/windows_mphal.c +++ b/windows/windows_mphal.c @@ -72,7 +72,7 @@ void mp_hal_stdio_mode_orig(void) { // Previous versions of the mp_hal code would install a handler whenever Ctrl-C input is // allowed and remove the handler again when it is not. That is not necessary though (1), // and it might introduce problems (2) because console notifications are delivered to the -// application in a seperate thread. +// application in a separate thread. // (1) mp_hal_set_interrupt_char effectively enables/disables processing of Ctrl-C via the // ENABLE_PROCESSED_INPUT flag so in raw mode console_sighandler won't be called. // (2) if mp_hal_set_interrupt_char would remove the handler while Ctrl-C was issued earlier, diff --git a/zephyr/modutime.c b/zephyr/modutime.c index 378068bb3..0c268046a 100644 --- a/zephyr/modutime.c +++ b/zephyr/modutime.c @@ -36,7 +36,7 @@ #include "extmod/utime_mphal.h" STATIC mp_obj_t mod_time_time(void) { - /* The absense of FP support is deliberate. The Zephyr port uses + /* The absence of FP support is deliberate. The Zephyr port uses * single precision floats so the fraction component will start to * lose precision on devices with a long uptime. */ -- cgit v1.2.3 From a0dbbbebb8c0286e00ae06751b0173cbca4ec801 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 3 Jun 2017 22:32:07 +0300 Subject: extmod/modlwip: connect: For non-blocking mode, return EINPROGRESS. Instead of ETIMEDOUT. This is consistent with POSIX: http://pubs.opengroup.org/onlinepubs/7908799/xns/connect.html --- extmod/modlwip.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'extmod') diff --git a/extmod/modlwip.c b/extmod/modlwip.c index 47669cb3a..aa93eaa27 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -822,7 +822,7 @@ STATIC mp_obj_t lwip_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) { if (socket->state != STATE_CONNECTING) break; } if (socket->state == STATE_CONNECTING) { - mp_raise_OSError(MP_ETIMEDOUT); + mp_raise_OSError(MP_EINPROGRESS); } } else { while (socket->state == STATE_CONNECTING) { -- cgit v1.2.3 From 5da8de2b66d3f43107e1e745afa9bb6a4bf601eb Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 4 Jun 2017 12:30:41 +0300 Subject: extmod/modlwip: Fix error codes for duplicate calls to connect(). If socket is already connected, POSIX requires returning EISCONN. If connection was requested, but not yet complete (for non-blocking socket), error code is EALREADY. http://pubs.opengroup.org/onlinepubs/7908799/xns/connect.html --- extmod/modlwip.c | 4 ++-- py/mperrno.h | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'extmod') diff --git a/extmod/modlwip.c b/extmod/modlwip.c index aa93eaa27..d243985ad 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -800,9 +800,9 @@ STATIC mp_obj_t lwip_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) { case MOD_NETWORK_SOCK_STREAM: { if (socket->state != STATE_NEW) { if (socket->state == STATE_CONNECTED) { - mp_raise_OSError(MP_EALREADY); + mp_raise_OSError(MP_EISCONN); } else { - mp_raise_OSError(MP_EINPROGRESS); + mp_raise_OSError(MP_EALREADY); } } // Register our receive callback. diff --git a/py/mperrno.h b/py/mperrno.h index 4d092de45..6ea99ae22 100644 --- a/py/mperrno.h +++ b/py/mperrno.h @@ -73,6 +73,7 @@ #define MP_ECONNABORTED (103) // Software caused connection abort #define MP_ECONNRESET (104) // Connection reset by peer #define MP_ENOBUFS (105) // No buffer space available +#define MP_EISCONN (106) // Transport endpoint is already connected #define MP_ENOTCONN (107) // Transport endpoint is not connected #define MP_ETIMEDOUT (110) // Connection timed out #define MP_ECONNREFUSED (111) // Connection refused @@ -127,6 +128,7 @@ #define MP_ECONNABORTED ECONNABORTED #define MP_ECONNRESET ECONNRESET #define MP_ENOBUFS ENOBUFS +#define MP_EISCONN EISCONN #define MP_ENOTCONN ENOTCONN #define MP_ETIMEDOUT ETIMEDOUT #define MP_ECONNREFUSED ECONNREFUSED -- cgit v1.2.3 From 50de6d2fab9ad67c6df0d74ce4b8c3704d1de090 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sun, 4 Jun 2017 13:45:37 +0300 Subject: extmod/modlwip: accept: Fix error code for non-blocking mode. In non-blocking mode, if no pending connection available, should return EAGAIN, not ETIMEDOUT. --- extmod/modlwip.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'extmod') diff --git a/extmod/modlwip.c b/extmod/modlwip.c index d243985ad..01190d200 100644 --- a/extmod/modlwip.c +++ b/extmod/modlwip.c @@ -732,7 +732,9 @@ STATIC mp_obj_t lwip_socket_accept(mp_obj_t self_in) { // accept incoming connection if (socket->incoming.connection == NULL) { - if (socket->timeout != -1) { + if (socket->timeout == 0) { + mp_raise_OSError(MP_EAGAIN); + } else if (socket->timeout != -1) { for (mp_uint_t retries = socket->timeout / 100; retries--;) { mp_hal_delay_ms(100); if (socket->incoming.connection != NULL) break; -- cgit v1.2.3 From f6ef8e3f17222a397e02f93a8b0d283e0f6c9793 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 7 Jun 2017 15:17:45 +1000 Subject: extmod/vfs: Allow to statvfs the root directory. --- extmod/vfs.c | 26 ++++++++++++++++++++++++++ tests/extmod/vfs_basic.py | 5 +++++ tests/extmod/vfs_basic.py.exp | 4 ++++ 3 files changed, 35 insertions(+) (limited to 'extmod') diff --git a/extmod/vfs.c b/extmod/vfs.c index f158bd387..b75ec7516 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -421,6 +421,32 @@ 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; mp_vfs_mount_t *vfs = lookup_path(path_in, &path_out); + if (vfs == MP_VFS_ROOT) { + // statvfs called on the root directory, see if there's anything mounted there + for (vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) { + if (vfs->len == 1) { + break; + } + } + + // If there's nothing mounted at root then return a mostly-empty tuple + if (vfs == NULL) { + mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(10, NULL)); + + // fill in: bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flags + for (int i = 0; i <= 8; ++i) { + t->items[i] = MP_OBJ_NEW_SMALL_INT(0); + } + + // Put something sensible in f_namemax + t->items[9] = MP_OBJ_NEW_SMALL_INT(MICROPY_ALLOC_PATH_MAX); + + return MP_OBJ_FROM_PTR(t); + } + + // VFS mounted at root so delegate the call to it + path_out = MP_OBJ_NEW_QSTR(MP_QSTR__slash_); + } 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/tests/extmod/vfs_basic.py b/tests/extmod/vfs_basic.py index a3b2f3c29..fc016b8d5 100644 --- a/tests/extmod/vfs_basic.py +++ b/tests/extmod/vfs_basic.py @@ -57,6 +57,9 @@ for path in uos.listdir('/'): # stat root dir print(uos.stat('/')) +# statvfs root dir +print(uos.statvfs('/')) + # getcwd when in root dir print(uos.getcwd()) @@ -128,6 +131,8 @@ except OSError: # root dir uos.mount(Filesystem(3), '/') +print(uos.stat('/')) +print(uos.statvfs('/')) print(uos.listdir()) open('test') diff --git a/tests/extmod/vfs_basic.py.exp b/tests/extmod/vfs_basic.py.exp index 8a23aa8ae..f8ecd07ea 100644 --- a/tests/extmod/vfs_basic.py.exp +++ b/tests/extmod/vfs_basic.py.exp @@ -1,4 +1,5 @@ (16384, 0, 0, 0, 0, 0, 0, 0, 0, 0) +(0, 0, 0, 0, 0, 0, 0, 0, 0, 4096) / 1 mount False False ['test_mnt'] @@ -36,6 +37,9 @@ OSError 2 umount OSError 3 mount False False +(16384, 0, 0, 0, 0, 0, 0, 0, 0, 0) +3 statvfs / +(3,) 3 ilistdir / ['a3'] 3 open test r -- cgit v1.2.3 From 7ecfbb8267c050ba5bd5bdf7becfb055f53a4f80 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 7 Jun 2017 15:29:53 +1000 Subject: extmod/vfs: Allow "buffering" and "encoding" args to VFS's open(). These args are currently ignored but are parsed to make it easier to write portable scripts between CPython and MicroPython. --- extmod/vfs.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'extmod') diff --git a/extmod/vfs.c b/extmod/vfs.c index b75ec7516..3bdce80db 100644 --- a/extmod/vfs.c +++ b/extmod/vfs.c @@ -228,11 +228,14 @@ mp_obj_t mp_vfs_umount(mp_obj_t mnt_in) { } MP_DEFINE_CONST_FUN_OBJ_1(mp_vfs_umount_obj, mp_vfs_umount); +// Note: buffering and encoding args are currently ignored 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)} }, + { MP_QSTR_buffering, MP_ARG_INT, {.u_int = -1} }, + { MP_QSTR_encoding, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} }, }; // parse args -- cgit v1.2.3 From 0a7735f1a67e7c2d6f59205f8d57fca2449cec93 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 10 Jun 2017 20:31:07 +0300 Subject: extmod/modframebuf: Fix signed/unsigned comparison pendantic warning. Happened with 32-bit gcc 4.8.4. --- extmod/modframebuf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'extmod') diff --git a/extmod/modframebuf.c b/extmod/modframebuf.c index b8e84fe1c..a07392675 100644 --- a/extmod/modframebuf.c +++ b/extmod/modframebuf.c @@ -449,7 +449,7 @@ STATIC mp_obj_t framebuf_blit(size_t n_args, const mp_obj_t *args) { int cx1 = x1; for (int cx0 = x0; cx0 < x0end; ++cx0) { color = getpixel(source, cx1, y1); - if (color != key) { + if (color != (uint32_t)key) { setpixel(self, cx0, y0, color); } ++cx1; -- cgit v1.2.3