From 49064f41a2c731cbdfaf6605f32c9cfb7cc0dfd4 Mon Sep 17 00:00:00 2001 From: bhgv Date: Tue, 12 May 2020 13:38:53 +0300 Subject: support of FreeRTOS riscV-64 (k210 cpu). 2nd step --- libiot/freertos/adds.c | 5 +- libiot/mkfile | 3 + libiot/vfs/mkfile | 1 + libiot/vfs/spiffs.c | 74 +++++++++--- libiot/vfs/vfs.c | 322 +++++++++++++++++++++++++++++++++++++++++++++++++ libiot/vfs/vfs.h | 9 +- 6 files changed, 396 insertions(+), 18 deletions(-) create mode 100644 libiot/vfs/vfs.c (limited to 'libiot') diff --git a/libiot/freertos/adds.c b/libiot/freertos/adds.c index 3eede33..f2beaa6 100644 --- a/libiot/freertos/adds.c +++ b/libiot/freertos/adds.c @@ -59,6 +59,9 @@ #include + +void panic(const char*); + #define THREAD_LOCAL_STORAGE_POINTER_ID 0 // Reference to lua_thread, which is created in app_main @@ -134,7 +137,7 @@ void uxSetLuaState(lua_State* L) { if (!lua_rtos_tcb->lthread) { lua_rtos_tcb->lthread = calloc(1, sizeof(lthread_t)); if (lua_rtos_tcb->lthread == NULL) { - panic(); + panic("No thread TLS!"); } //assert(lua_rtos_tcb->lthread); } diff --git a/libiot/mkfile b/libiot/mkfile index 516eb9d..36ef8ed 100644 --- a/libiot/mkfile +++ b/libiot/mkfile @@ -24,3 +24,6 @@ CFLAGS=\ mount) { struct dirent *ment = mount_readdir((DIR *)dir); @@ -1004,6 +1005,7 @@ static struct dirent* vfs_spiffs_readdir(DIR* pdir) { return ment; } } +#endif //{} mtx_lock(&vfs_mtx); @@ -1220,7 +1222,10 @@ static void vfs_spiffs_free_resources() { mtx_destroy(&ll_mtx); } -int vfs_spiffs_mount(const char *target) { + + + + esp_vfs_t vfs = { .flags = ESP_VFS_FLAG_DEFAULT, .write = &vfs_spiffs_write, @@ -1240,15 +1245,44 @@ int vfs_spiffs_mount(const char *target) { .rmdir = &vfs_spiffs_rmdir, .fsync = &vfs_spiffs_fsync, .access = &vfs_spiffs_access, - .telldir = &vfs_spiffs_telldir, + .telldir = &vfs_spiffs_telldir, }; + + + +esp_vfs_t* vfs_spiffs_mount(const char *target) { +#if 0 //{} + esp_vfs_t vfs = { + .flags = ESP_VFS_FLAG_DEFAULT, + .write = &vfs_spiffs_write, + .open = &vfs_spiffs_open, + .fstat = &vfs_spiffs_fstat, + .close = &vfs_spiffs_close, + .read = &vfs_spiffs_read, + .lseek = &vfs_spiffs_lseek, + .stat = &vfs_spiffs_stat, + .link = NULL, + .unlink = &vfs_spiffs_unlink, + .rename = &vfs_spiffs_rename, + .mkdir = &vfs_spiffs_mkdir, + .opendir = &vfs_spiffs_opendir, + .readdir = &vfs_spiffs_readdir, + .closedir = &vfs_spiffs_closedir, + .rmdir = &vfs_spiffs_rmdir, + .fsync = &vfs_spiffs_fsync, + .access = &vfs_spiffs_access, + .telldir = &vfs_spiffs_telldir, + }; +#endif //{} + // Mount spiffs file system spiffs_config cfg; int res = 0; int retries = 0; // Find partition +#if 0 //{} const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, LUA_RTOS_SPIFFS_PART, NULL); if (!partition) { @@ -1259,11 +1293,17 @@ int vfs_spiffs_mount(const char *target) { cfg.phys_addr = partition->address; cfg.phys_size = partition->size; } +#endif //{} + + cfg.phys_addr = 0xA00000; + cfg.phys_size = 1024 * 1024; + cfg.phys_erase_block = w25qxx_FLASH_SECTOR_SIZE; //{} CONFIG_LUA_RTOS_SPIFFS_ERASE_SIZE; cfg.log_page_size = w25qxx_FLASH_PAGE_SIZE; //{} CONFIG_LUA_RTOS_SPIFFS_LOG_PAGE_SIZE; cfg.log_block_size = w25qxx_FLASH_SECTOR_SIZE; //{} CONFIG_LUA_RTOS_SPIFFS_LOG_BLOCK_SIZE; +#if 0 //{} if (partition) { syslog(LOG_INFO, "spiffs start address at 0x%x, size %d Kb", @@ -1272,6 +1312,7 @@ int vfs_spiffs_mount(const char *target) { syslog(LOG_INFO, "spiffs start address at 0x%x, size %d Kb", cfg.phys_addr, cfg.phys_size / 1024); } +#endif //{} cfg.hal_read_f = (spiffs_read) low_spiffs_read; cfg.hal_write_f = (spiffs_write) low_spiffs_write; @@ -1281,7 +1322,7 @@ int vfs_spiffs_mount(const char *target) { if (!my_spiffs_work_buf) { vfs_spiffs_free_resources(); syslog(LOG_ERR, "spiffs can't allocate memory for file system"); - return -1; + return NULL; } int fds_len = sizeof(spiffs_fd) * 5; @@ -1289,7 +1330,7 @@ int vfs_spiffs_mount(const char *target) { if (!my_spiffs_fds) { vfs_spiffs_free_resources(); syslog(LOG_ERR, "spiffs can't allocate memory for file system"); - return -1; + return NULL; } int cache_len = cfg.log_page_size * 5; @@ -1297,7 +1338,7 @@ int vfs_spiffs_mount(const char *target) { if (!my_spiffs_cache) { vfs_spiffs_free_resources(); syslog(LOG_ERR, "spiffs can't allocate memory for file system"); - return -1; + return NULL; } // Init mutex @@ -1318,13 +1359,13 @@ int vfs_spiffs_mount(const char *target) { if (res < 0) { vfs_spiffs_free_resources(); syslog(LOG_ERR, "spiffs format error"); - return -1; + return NULL; } } else { vfs_spiffs_free_resources(); syslog(LOG_ERR, "spiff can't mount file system (%s)", strerror(spiffs_result(fs.err_code))); - return -1; + return NULL; } } else { break; @@ -1342,28 +1383,33 @@ int vfs_spiffs_mount(const char *target) { vfs_spiffs_umount(target); syslog(LOG_ERR, "spiffs can't create root folder (%s)", strerror(spiffs_result(fs.err_code))); - return -1; + return NULL; } if (SPIFFS_close(&fs, fd) < 0) { vfs_spiffs_umount(target); syslog(LOG_ERR, "spiffs can't create root folder (%s)", strerror(spiffs_result(fs.err_code))); - return -1; + return NULL; } } lstinit(&files, 0, LIST_DEFAULT); - ESP_ERROR_CHECK(esp_vfs_register("/spiffs", &vfs, NULL)); +// ESP_ERROR_CHECK(esp_vfs_register("/spiffs", &vfs, NULL)); + +// esp_vfs_t* ovfs = malloc(sizeof(esp_vfs_t)); +// memcpy(ovfs, &vfs); syslog(LOG_INFO, "spiffs mounted on %s", target); - return 0; + return &vfs; //ovfs; } -int vfs_spiffs_umount(const char *target) { - esp_vfs_unregister("/spiffs"); +int vfs_spiffs_umount(/*esp_vfs_t* vfs*/ const char *target) { +// esp_vfs_unregister("/spiffs"); +// free(vfs); + SPIFFS_unmount(&fs); vfs_spiffs_free_resources(); @@ -1373,7 +1419,7 @@ int vfs_spiffs_umount(const char *target) { return 0; } -int vfs_spiffs_format(const char *target) { +int vfs_spiffs_format(/*esp_vfs_t** vfs*/ const char *target) { vfs_spiffs_umount(target); mtx_init(&ll_mtx, NULL, NULL, 0); diff --git a/libiot/vfs/vfs.c b/libiot/vfs/vfs.c new file mode 100644 index 0000000..2d40ee6 --- /dev/null +++ b/libiot/vfs/vfs.c @@ -0,0 +1,322 @@ +/* + * Copyright (C) 2015 - 2018, IBEROXARXA SERVICIOS INTEGRALES, S.L. + * Copyright (C) 2015 - 2018, Jaume Olivé Petrus (jolive@whitecatboard.org) + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * * The WHITECAT logotype cannot be changed, you can remove it, but you + * cannot change it in any way. The WHITECAT logotype is: + * + * /\ /\ + * / \_____/ \ + * /_____________\ + * W H I T E C A T + * + * * Redistributions in binary form must retain all copyright notices printed + * to any local or remote output device. This include any reference to + * Lua RTOS, whitecatboard.org, Lua, and other copyright notices that may + * appear in the future. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Lua RTOS common vfs functions + * + */ + +#include "vfs.h" + +#include +#include +#include +#include + +//#if CONFIG_LUA_RTOS_USE_SPIFFS +#include "../spiffs/spiffs.h" +//#endif + +#if CONFIG_LUA_RTOS_USE_LFS +#include +#endif + +#if CONFIG_LUA_RTOS_USE_FAT +#include +#endif + +#if CONFIG_LUA_RTOS_USE_RAM_FS +#include +#endif + +#if CONFIG_LUA_RTOS_USE_ROM_FS +#include +#endif + +#include + +//#include + +//extern const struct mount_pt mountps[]; + +#ifndef MAX +#define MAX(a,b) (((a)>(b))?(a):(b)) +#endif + +vfs_dir_t *vfs_allocate_dir(const char *vfs, const char *name) { + // Allocate directory + vfs_dir_t *dir = calloc(1, sizeof(vfs_dir_t)); + if (!dir) { + errno = ENOMEM; + return NULL; + } + +#if 0 //{} + // If the directory is the root folder, and the file system is the root + // file system, fill the mount pointer to read the mount points in the + // readdir function + if ((strcmp(name, "/") == 0) && (strcmp(mount_get_root()->fs, vfs) == 0)) { + dir->mount = mountps; + } else { + dir->mount = NULL; + } +#endif + + size_t size_dir = 0; + size_t size_info = 0; + +//#if CONFIG_LUA_RTOS_USE_SPIFFS + if (strcmp(vfs,"spiffs") == 0) { + size_dir = sizeof(spiffs_DIR); + size_info = sizeof(struct spiffs_dirent); + } +//#endif + +#if CONFIG_LUA_RTOS_USE_LFS + if (strcmp(vfs,"lfs") == 0) { + size_dir = sizeof(lfs_dir_t); + size_info = sizeof(struct lfs_info); + } +#endif + +#if CONFIG_LUA_RTOS_USE_RAM_FS + if (strcmp(vfs,"ramfs") == 0) { + size_dir = sizeof(ramfs_dir_t); + size_info = sizeof(ramfs_info_t); + } +#endif + +#if CONFIG_LUA_RTOS_USE_ROM_FS + if (strcmp(vfs,"romfs") == 0) { + size_dir = sizeof(romfs_dir_t); + size_info = sizeof(romfs_info_t); + } +#endif + +#if CONFIG_LUA_RTOS_USE_FAT + if (strcmp(vfs,"fat") == 0) { + size_dir = sizeof(FF_DIR); + size_info = sizeof(FILINFO); + } +#endif + + dir->fs_dir = (void *)calloc(1, size_dir); + if (!dir->fs_dir) { + vfs_free_dir(dir); + errno = ENOMEM; + return NULL; + } + + dir->fs_info = (void *)calloc(1, size_info); + if (!dir->fs_info) { + vfs_free_dir(dir); + errno = ENOMEM; + return NULL; + } + + dir->path = strdup(name); + if (!dir->path) { + vfs_free_dir(dir); + errno = ENOMEM; + return NULL; + } + + return dir; +} + +void vfs_free_dir(vfs_dir_t *dir) { + if (dir) { + if (dir->path) free(dir->path); + if (dir->fs_dir) free(dir->fs_dir); + if (dir->fs_info) free(dir->fs_info); + free(dir); + } +} + +vfs_fd_local_storage_t *vfs_create_fd_local_storage(int num) { + vfs_fd_local_storage_t *ptr; + + ptr = calloc(num, sizeof(vfs_fd_local_storage_t)); + if (!ptr) { + errno = ENOMEM; + return NULL; + } + + return ptr; +} + +void vfs_destroy_fd_local_storage(vfs_fd_local_storage_t *ptr) { + free(ptr); +} + +int vfs_generic_fcntl(vfs_fd_local_storage_t *local_storage, int fd, int cmd, va_list args) { + int result = 0; + + if (cmd == F_GETFL) { + if (local_storage) { + return local_storage[fd].flags; + } else { + return 0; + } + } else if (cmd == F_SETFL) { + if (local_storage) { + local_storage[fd].flags = va_arg(args, int); + } + } else { + result = -1; + errno = ENOSYS; + } + + return result; +} + +ssize_t vfs_generic_read(vfs_fd_local_storage_t *local_storage, vfs_has_bytes has_bytes, vfs_get_byte get, int fd, void * dst, size_t size) { + char *c = (char *)dst; + int bytes = 0; + + while (size) { + if (local_storage && (local_storage[fd].flags & O_NONBLOCK)) { + if (!has_bytes(fd, 0)) { + if (bytes > 0) { + return bytes; + } + + errno = EAGAIN; + return -1; + } + } else { + if (!has_bytes(fd, 0) && (bytes > 0)) { + return bytes; + } + } + + if (!get(fd, c)) { + errno = EIO; + return -1; + } + + c++; + size--; + bytes++; + } + + return bytes; +} + +ssize_t vfs_generic_write(vfs_fd_local_storage_t *local_storage, vfs_put_byte put, int fd, const void *data, size_t size) { + char *c = (char *)data; + int bytes = 0; + + while (size) { +#if CONFIG_NEWLIB_STDOUT_LINE_ENDING_LF + char n; + + if (*c=='\n') { + n = '\r'; + put(fd, &n); + } +#endif + + put(fd, c); + c++; + size--; + bytes++; + } + + return bytes; +} + +ssize_t vfs_generic_writev(vfs_fd_local_storage_t *local_storage, vfs_put_byte put, int fd, const struct iovec *iov, int iovcnt) { + int bytes = 0; + int len = 0; + char *c; + + while (iovcnt) { + c = (char *)iov->iov_base; + len = iov->iov_len; + + while (len) { + put(fd, c); + c++; + len--; + bytes++; + } + iov++; + iovcnt--; + } + + return bytes; +} + +int vfs_generic_select(vfs_fd_local_storage_t *local_storage, vfs_has_bytes has_bytes, vfs_free_bytes free_bytes, int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, struct timeval *timeout) { + int to = 0xffffffff; // Default timeout + + // Get the timeout + if (timeout) { + to = MAX((timeout->tv_sec * 1000 + (timeout->tv_usec + 500) / 1000),1); + } + + // Inspect all the file descriptors + int num = 0; // Number of available file descriptors + int fd; // Current inspected file descriptor + + for(fd = 0;fd <= maxfdp1;fd++) { + if (readset && FD_ISSET(fd, readset)) { + if (has_bytes(fd, to)) { + num++; + } else { + FD_CLR(fd, readset); + } + } + + if (writeset && FD_ISSET(fd, writeset)) { + if (free_bytes(fd) > 0) { + num++; + } else { + FD_CLR(fd, writeset); + } + } + + if (exceptset && FD_ISSET(fd, exceptset)) { + } + } + + return num; +} diff --git a/libiot/vfs/vfs.h b/libiot/vfs/vfs.h index eb743fd..4cea7c1 100644 --- a/libiot/vfs/vfs.h +++ b/libiot/vfs/vfs.h @@ -96,7 +96,7 @@ int vfs_fat_format(const char *target); int vfs_fat_fsstat(const char *target, u32_t *total, u32_t *used); */ -int vfs_spiffs_mount(const char *target); +esp_vfs_t* vfs_spiffs_mount(const char *target); int vfs_spiffs_umount(const char *target); int vfs_spiffs_format(const char *target); int vfs_spiffs_fsstat(const char *target, u32_t *total, u32_t *used); @@ -121,8 +121,11 @@ ssize_t vfs_generic_read(vfs_fd_local_storage_t *local_storage, vfs_has_bytes ha ssize_t vfs_generic_write(vfs_fd_local_storage_t *local_storage, vfs_put_byte put, int fd, const void *data, size_t size); ssize_t vfs_generic_writev(vfs_fd_local_storage_t *local_storage, vfs_put_byte put, int fd, const struct iovec *iov, int iovcnt); int vfs_generic_select(vfs_fd_local_storage_t *local_storage, vfs_has_bytes has_bytes, vfs_free_bytes free, int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, struct timeval *timeout); +*/ vfs_dir_t *vfs_allocate_dir(const char *vfs, const char *name); void vfs_free_dir(vfs_dir_t *dir); -vfs_fd_local_storage_t *vfs_create_fd_local_storage(int num); -*/ +//vfs_fd_local_storage_t *vfs_create_fd_local_storage(int num); + + +extern esp_vfs_t vfs; -- cgit v1.2.3