├── .gitignore ├── .gitmodules ├── LICENSE ├── Makefile ├── README.md ├── component.mk ├── components └── littleflash │ ├── component.mk │ ├── include │ └── littleflash.h │ └── littleflash.cpp ├── main ├── component.mk ├── littleflash.cpp ├── test_lfs_common.c ├── test_lfs_common.h └── unity.h └── partitions.csv /.gitignore: -------------------------------------------------------------------------------- 1 | .**.swp 2 | build 3 | sdkconfig 4 | sdkconfig.old 5 | 6 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "components/extflash"] 2 | path = components/extflash 3 | url = https://github.com/lllucius/esp32_extflash 4 | [submodule "components/littleflash/littlefs"] 5 | path = components/littleflash/littlefs 6 | url = https://github.com/geky/littlefs.git 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # This is a project Makefile. It is assumed the directory this Makefile resides in is a 3 | # project subdirectory. 4 | # 5 | 6 | PROJECT_NAME := littleflash 7 | 8 | include $(IDF_PATH)/make/project.mk 9 | 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LittleFS filesystem for the ESP32 2 | LittleFlash is a port of [LittleFS](https://github.com/geky/littlefs) to the ESP32, 3 | providing support for usage on internal and external flash. External support 4 | utilizes my [ExtFlash](https://github.com/lllucius/esp32_extflash) component. 5 | 6 | To clone this repo, use the following to automatically include the extflash 7 | submodule: 8 | ``` 9 | git clone --recurse-submodules https://github.com/lllucius/esp32_littleflash 10 | ``` 11 | 12 | To use, just add the "extflash" and "littleflash" components to your 13 | components directory and initialize it with something like: 14 | 15 | ``` 16 | #include "littleflash.h" 17 | 18 | void app_main() 19 | { 20 | esp_err_t err; 21 | 22 | #if !defined(CONFIG_LITTLEFS_PARTITION_LABEL) 23 | ExtFlash flash; 24 | 25 | ext_flash_config_t cfg = 26 | { 27 | .vspi = true, 28 | .sck_io_num = PIN_SPI_SCK, 29 | .miso_io_num = PIN_SPI_MISO, 30 | .mosi_io_num = PIN_SPI_MOSI, 31 | .ss_io_num = PIN_SPI_SS, 32 | .hd_io_num = PIN_SPI_HD, 33 | .wp_io_num = PIN_SPI_WP, 34 | .speed_mhz = 40, 35 | .dma_channel = 1, 36 | .queue_size = 2, 37 | .sector_size = 0, 38 | .capacity = 0 39 | }; 40 | 41 | err = flash.init(&cfg); 42 | if (err != ESP_OK) 43 | { 44 | ... 45 | } 46 | #endif 47 | 48 | const little_flash_config_t little_cfg = 49 | { 50 | #if !defined(CONFIG_LITTLEFS_PARTITION_LABEL) 51 | .flash = &extflash, 52 | .part_label = NULL, 53 | #else 54 | .flash = NULL, 55 | .part_label = CONFIG_LITTLEFS_PARTITION_LABEL, 56 | #endif 57 | .base_path = MOUNT_POINT, 58 | .open_files = openfiles, 59 | .auto_format = true, 60 | .lookahead = 32 61 | }; 62 | 63 | err = littleflash.init(&little_cfg); 64 | if (err != ESP_OK) 65 | { 66 | ... 67 | } 68 | ... 69 | } 70 | ``` 71 | 72 | The configuration options for LittleFlash are: 73 | 74 | ``` 75 | typedef struct 76 | { 77 | ExtFlash *flash; // initialized ExtFlash, or NULL for internal flash 78 | const char *part_label; // partition label if using internal flash 79 | const char *base_path; // mount point 80 | int open_files; // number of open files to support 81 | bool auto_format; // true=format if not valid 82 | lfs_size_t lookahead; // number of LFS lookahead blocks 83 | } little_flash_config_t; 84 | ``` 85 | 86 | More documentation to follow. 87 | 88 | -------------------------------------------------------------------------------- /component.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Component Makefile 3 | # 4 | 5 | COMPONENT_SRCDIRS = components/$(COMPONENT_NAME) 6 | COMPONENT_ADD_INCLUDEDIRS = components/$(COMPONENT_NAME)/include \ 7 | components/$(COMPONENT_NAME)/littlefs 8 | 9 | -------------------------------------------------------------------------------- /components/littleflash/component.mk: -------------------------------------------------------------------------------- 1 | # 2 | # Component Makefile 3 | # 4 | 5 | COMPONENT_SRCDIRS := . littlefs 6 | COMPONENT_ADD_INCLUDEDIRS := include littlefs 7 | 8 | -------------------------------------------------------------------------------- /components/littleflash/include/littleflash.h: -------------------------------------------------------------------------------- 1 | // Copyright 2017-2018 Leland Lucius 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #if !defined(_LITTLEFLASH_H_) 16 | #define _LITTLEFLASH_H_ 1 17 | 18 | #include 19 | 20 | #include "esp_err.h" 21 | #include "esp_vfs.h" 22 | #include "esp_partition.h" 23 | 24 | #include "extflash.h" 25 | 26 | extern "C" 27 | { 28 | #include "lfs.h" 29 | } 30 | 31 | typedef struct 32 | { 33 | ExtFlash *flash; // initialized ExtFlash, or NULL for internal flash 34 | const char *part_label; // partition label if using internal flash 35 | const char *base_path; // mount point 36 | int open_files; // number of open files to support 37 | bool auto_format; // true=format if not valid 38 | lfs_size_t lookahead; // number of LFS lookahead blocks 39 | } little_flash_config_t; 40 | 41 | class LittleFlash 42 | { 43 | public: 44 | LittleFlash(); 45 | virtual ~LittleFlash(); 46 | 47 | esp_err_t init(const little_flash_config_t *config); 48 | void term(); 49 | 50 | private: 51 | // 52 | // VFS interface 53 | // 54 | int get_free_fd(); 55 | 56 | static int map_lfs_error(int err); 57 | 58 | static ssize_t write_p(void *ctx, int fd, const void *data, size_t size); 59 | static off_t lseek_p(void *ctx, int fd, off_t size, int mode); 60 | static ssize_t read_p(void *ctx, int fd, void *dst, size_t size); 61 | static int open_p(void *ctx, const char *path, int flags, int mode); 62 | static int close_p(void *ctx, int fd); 63 | static int fstat_p(void *ctx, int fd, struct stat *st); 64 | static int stat_p(void *ctx, const char *path, struct stat *st); 65 | static int link_p(void *ctx, const char *n1, const char *n2); 66 | static int unlink_p(void *ctx, const char *path); 67 | static int rename_p(void *ctx, const char *src, const char *dst); 68 | static DIR *opendir_p(void *ctx, const char *name); 69 | static struct dirent *readdir_p(void *ctx, DIR *pdir); 70 | static int readdir_r_p(void *ctx, DIR *pdir, struct dirent *entry, struct dirent **out_dirent); 71 | static long telldir_p(void *ctx, DIR *pdir); 72 | static void seekdir_p(void *ctx, DIR *pdir, long offset); 73 | static int closedir_p(void *ctx, DIR *pdir); 74 | static int mkdir_p(void *ctx, const char *name, mode_t mode); 75 | static int rmdir_p(void *ctx, const char *name); 76 | static int fcntl_p(void *ctx, int fd, int cmd, va_list args); 77 | static int ioctl_p(void *ctx, int fd, int cmd, va_list args); 78 | static int fsync_p(void *ctx, int fd); 79 | 80 | // 81 | // LFS disk interface for external flash 82 | // 83 | static int external_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size); 84 | static int external_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size); 85 | static int external_erase(const struct lfs_config *c, lfs_block_t block); 86 | static int external_sync(const struct lfs_config *c); 87 | 88 | // 89 | // LFS disk interface for internal flash 90 | // 91 | static int internal_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size); 92 | static int internal_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size); 93 | static int internal_erase(const struct lfs_config *c, lfs_block_t block); 94 | static int internal_sync(const struct lfs_config *c); 95 | 96 | private: 97 | struct lfs_config lfs_cfg; 98 | 99 | little_flash_config_t cfg; 100 | const esp_partition_t *part; 101 | 102 | bool mounted; 103 | bool registered; 104 | 105 | size_t sector_sz; 106 | size_t block_cnt; 107 | 108 | _lock_t lock; 109 | lfs_t lfs; 110 | 111 | typedef struct vfs_fd 112 | { 113 | lfs_file *file; 114 | char *name; 115 | } vfs_fd_t; 116 | 117 | vfs_fd_t *fds; 118 | }; 119 | 120 | #endif 121 | -------------------------------------------------------------------------------- /components/littleflash/littleflash.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2017-2018 Leland Lucius 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include "esp_err.h" 24 | #include "esp_log.h" 25 | 26 | #include "littleflash.h" 27 | 28 | static const char *TAG = "littleflash"; 29 | 30 | LittleFlash::LittleFlash() 31 | { 32 | fds = NULL; 33 | mounted = false; 34 | registered = false; 35 | } 36 | 37 | LittleFlash::~LittleFlash() 38 | { 39 | term(); 40 | } 41 | 42 | esp_err_t LittleFlash::init(const little_flash_config_t *config) 43 | { 44 | ESP_LOGD(TAG, "%s", __func__); 45 | 46 | _lock_init(&lock); 47 | 48 | lfs_cfg = {}; 49 | 50 | cfg = *config; 51 | 52 | if (cfg.flash) 53 | { 54 | sector_sz = cfg.flash->sector_size(); 55 | block_cnt = cfg.flash->chip_size() / sector_sz; 56 | 57 | lfs_cfg.read = &external_read; 58 | lfs_cfg.prog = &external_prog; 59 | lfs_cfg.erase = &external_erase; 60 | lfs_cfg.sync = &external_sync; 61 | } 62 | else 63 | { 64 | part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, 65 | ESP_PARTITION_SUBTYPE_ANY, 66 | cfg.part_label); 67 | if (part == NULL) 68 | { 69 | ESP_LOGE(TAG, "Partition '%s' not found", cfg.part_label); 70 | return ESP_ERR_NOT_FOUND; 71 | } 72 | 73 | sector_sz = SPI_FLASH_SEC_SIZE; 74 | block_cnt = part->size / sector_sz; 75 | 76 | lfs_cfg.read = &internal_read; 77 | lfs_cfg.prog = &internal_prog; 78 | lfs_cfg.erase = &internal_erase; 79 | lfs_cfg.sync = &internal_sync; 80 | } 81 | 82 | lfs_cfg.context = (void *) this; 83 | lfs_cfg.read_size = sector_sz; 84 | lfs_cfg.prog_size = sector_sz; 85 | lfs_cfg.block_size = sector_sz; 86 | lfs_cfg.block_count = block_cnt; 87 | lfs_cfg.lookahead = cfg.lookahead; 88 | 89 | int err = lfs_mount(&lfs, &lfs_cfg); 90 | if (err < 0) 91 | { 92 | lfs_unmount(&lfs); 93 | if (!cfg.auto_format) 94 | { 95 | return ESP_FAIL; 96 | } 97 | 98 | lfs = {}; 99 | err = lfs_format(&lfs, &lfs_cfg); 100 | if (err < 0) 101 | { 102 | lfs_unmount(&lfs); 103 | return ESP_FAIL; 104 | } 105 | 106 | lfs = {}; 107 | err = lfs_mount(&lfs, &lfs_cfg); 108 | if (err < 0) 109 | { 110 | lfs_unmount(&lfs); 111 | return ESP_FAIL; 112 | } 113 | } 114 | mounted = true; 115 | 116 | fds = new vfs_fd_t[cfg.open_files]; 117 | if (fds == NULL) 118 | { 119 | return ESP_ERR_NO_MEM; 120 | } 121 | 122 | for (int i = 0; i < cfg.open_files; i++) 123 | { 124 | fds[i].file = NULL; 125 | fds[i].name = NULL; 126 | } 127 | 128 | esp_vfs_t vfs = {}; 129 | 130 | vfs.flags = ESP_VFS_FLAG_CONTEXT_PTR; 131 | vfs.write_p = &write_p; 132 | vfs.lseek_p = &lseek_p; 133 | vfs.read_p = &read_p; 134 | vfs.open_p = &open_p; 135 | vfs.close_p = &close_p; 136 | vfs.fstat_p = &fstat_p; 137 | vfs.stat_p = &stat_p; 138 | vfs.unlink_p = &unlink_p; 139 | vfs.rename_p = &rename_p; 140 | vfs.opendir_p = &opendir_p; 141 | vfs.readdir_p = &readdir_p; 142 | vfs.readdir_r_p = &readdir_r_p; 143 | vfs.telldir_p = &telldir_p; 144 | vfs.seekdir_p = &seekdir_p; 145 | vfs.closedir_p = &closedir_p; 146 | vfs.mkdir_p = &mkdir_p; 147 | vfs.rmdir_p = &rmdir_p; 148 | vfs.fsync_p = &fsync_p; 149 | 150 | esp_err_t esperr = esp_vfs_register(cfg.base_path, &vfs, this); 151 | if (esperr != ESP_OK) 152 | { 153 | return err; 154 | } 155 | 156 | registered = true; 157 | 158 | return ESP_OK; 159 | } 160 | 161 | void LittleFlash::term() 162 | { 163 | ESP_LOGD(TAG, "%s", __func__); 164 | 165 | if (registered) 166 | { 167 | for (int i = 0; i < cfg.open_files; i++) 168 | { 169 | if (fds[i].file) 170 | { 171 | close_p(this, i); 172 | fds[i].file = NULL; 173 | } 174 | } 175 | 176 | esp_vfs_unregister(cfg.base_path); 177 | registered = false; 178 | } 179 | 180 | if (fds) 181 | { 182 | delete [] fds; 183 | fds = NULL; 184 | } 185 | 186 | if (mounted) 187 | { 188 | lfs_unmount(&lfs); 189 | mounted = false; 190 | } 191 | 192 | _lock_close(&lock); 193 | } 194 | 195 | // ============================================================================ 196 | // ESP32 VFS implementation 197 | // ============================================================================ 198 | 199 | typedef struct 200 | { 201 | DIR dir; // must be first...ESP32 VFS expects it... 202 | struct dirent dirent; 203 | lfs_dir_t lfs_dir; 204 | long off; 205 | } vfs_lfs_dir_t; 206 | 207 | int LittleFlash::map_lfs_error(int err) 208 | { 209 | if (err == LFS_ERR_OK) 210 | { 211 | return 0; 212 | } 213 | 214 | switch (err) 215 | { 216 | case LFS_ERR_IO: 217 | errno = EIO; 218 | break; 219 | case LFS_ERR_CORRUPT: 220 | errno = EIO; 221 | break; 222 | case LFS_ERR_NOENT: 223 | errno = ENOENT; 224 | break; 225 | case LFS_ERR_EXIST: 226 | errno = EEXIST; 227 | break; 228 | case LFS_ERR_NOTDIR: 229 | errno = ENOTDIR; 230 | break; 231 | case LFS_ERR_ISDIR: 232 | errno = EISDIR; 233 | break; 234 | case LFS_ERR_NOTEMPTY: 235 | errno = ENOTEMPTY; 236 | break; 237 | case LFS_ERR_INVAL: 238 | errno = EINVAL; 239 | break; 240 | case LFS_ERR_NOSPC: 241 | errno = ENOSPC; 242 | break; 243 | case LFS_ERR_NOMEM: 244 | errno = ENOMEM; 245 | break; 246 | default: 247 | errno = EINVAL; 248 | break; 249 | } 250 | 251 | return -1; 252 | } 253 | 254 | int LittleFlash::get_free_fd() 255 | { 256 | for (int i = 0; i < cfg.open_files; i++) 257 | { 258 | if (fds[i].file == NULL) 259 | { 260 | return i; 261 | } 262 | } 263 | 264 | return -1; 265 | } 266 | 267 | ssize_t LittleFlash::write_p(void *ctx, int fd, const void *data, size_t size) 268 | { 269 | LittleFlash *that = (LittleFlash *) ctx; 270 | 271 | _lock_acquire(&that->lock); 272 | 273 | if (that->fds[fd].file == NULL) 274 | { 275 | _lock_release(&that->lock); 276 | errno = EBADF; 277 | return -1; 278 | } 279 | 280 | lfs_ssize_t written = lfs_file_write(&that->lfs, that->fds[fd].file, data, size); 281 | 282 | _lock_release(&that->lock); 283 | 284 | if (written < 0) 285 | { 286 | return map_lfs_error(written); 287 | } 288 | 289 | return written; 290 | } 291 | 292 | off_t LittleFlash::lseek_p(void *ctx, int fd, off_t size, int mode) 293 | { 294 | LittleFlash *that = (LittleFlash *) ctx; 295 | 296 | int lfs_mode = 0; 297 | if (mode == SEEK_SET) 298 | { 299 | lfs_mode = LFS_SEEK_SET; 300 | } 301 | else if (mode == SEEK_CUR) 302 | { 303 | lfs_mode = LFS_SEEK_CUR; 304 | } 305 | else if (mode == SEEK_END) 306 | { 307 | lfs_mode = LFS_SEEK_END; 308 | } 309 | else 310 | { 311 | errno = EINVAL; 312 | return -1; 313 | } 314 | 315 | _lock_acquire(&that->lock); 316 | 317 | if (that->fds[fd].file == NULL) 318 | { 319 | _lock_release(&that->lock); 320 | errno = EBADF; 321 | return -1; 322 | } 323 | 324 | lfs_soff_t pos = lfs_file_seek(&that->lfs, that->fds[fd].file, size, lfs_mode); 325 | 326 | if (pos >= 0) 327 | { 328 | pos = lfs_file_tell(&that->lfs, that->fds[fd].file); 329 | } 330 | 331 | _lock_release(&that->lock); 332 | 333 | if (pos < 0) 334 | { 335 | return map_lfs_error(pos); 336 | } 337 | 338 | return pos; 339 | } 340 | 341 | ssize_t LittleFlash::read_p(void *ctx, int fd, void *dst, size_t size) 342 | { 343 | LittleFlash *that = (LittleFlash *) ctx; 344 | 345 | _lock_acquire(&that->lock); 346 | 347 | if (that->fds[fd].file == NULL) 348 | { 349 | _lock_release(&that->lock); 350 | errno = EBADF; 351 | return -1; 352 | } 353 | 354 | lfs_ssize_t read = lfs_file_read(&that->lfs, that->fds[fd].file, dst, size); 355 | 356 | _lock_release(&that->lock); 357 | 358 | if (read < 0) 359 | { 360 | return map_lfs_error(read); 361 | } 362 | 363 | return read; 364 | } 365 | 366 | int LittleFlash::open_p(void *ctx, const char *path, int flags, int mode) 367 | { 368 | LittleFlash *that = (LittleFlash *) ctx; 369 | 370 | int lfs_flags = 0; 371 | if ((flags & O_ACCMODE) == O_RDONLY) 372 | { 373 | lfs_flags = LFS_O_RDONLY; 374 | } 375 | else if ((flags & O_ACCMODE) == O_WRONLY) 376 | { 377 | lfs_flags = LFS_O_WRONLY; 378 | } 379 | else if ((flags & O_ACCMODE) == O_RDWR) 380 | { 381 | lfs_flags = LFS_O_RDWR; 382 | } 383 | 384 | if (flags & O_CREAT) 385 | { 386 | lfs_flags |= LFS_O_CREAT; 387 | } 388 | 389 | if (flags & O_EXCL) 390 | { 391 | lfs_flags |= LFS_O_EXCL; 392 | } 393 | 394 | if (flags & O_TRUNC) 395 | { 396 | lfs_flags |= LFS_O_TRUNC; 397 | } 398 | 399 | if (flags & O_APPEND) 400 | { 401 | lfs_flags |= LFS_O_APPEND; 402 | } 403 | 404 | lfs_file *file = (lfs_file *) malloc(sizeof(lfs_file)); 405 | if (file == NULL) 406 | { 407 | errno = ENOMEM; 408 | return -1; 409 | } 410 | 411 | char *name = strdup(path); 412 | if (name == NULL) 413 | { 414 | free(file); 415 | errno = ENOMEM; 416 | return -1; 417 | } 418 | 419 | _lock_acquire(&that->lock); 420 | 421 | int fd = that->get_free_fd(); 422 | if (fd == -1) 423 | { 424 | _lock_release(&that->lock); 425 | free(name); 426 | free(file); 427 | errno = ENFILE; 428 | return -1; 429 | } 430 | 431 | int err = lfs_file_open(&that->lfs, file, path, lfs_flags); 432 | if (err < 0) 433 | { 434 | _lock_release(&that->lock); 435 | free(name); 436 | free(file); 437 | return map_lfs_error(err); 438 | } 439 | 440 | that->fds[fd].file = file; 441 | that->fds[fd].name = name; 442 | 443 | _lock_release(&that->lock); 444 | 445 | return fd; 446 | } 447 | 448 | int LittleFlash::close_p(void *ctx, int fd) 449 | { 450 | LittleFlash *that = (LittleFlash *) ctx; 451 | 452 | _lock_acquire(&that->lock); 453 | 454 | if (that->fds[fd].file == NULL) 455 | { 456 | _lock_release(&that->lock); 457 | errno = EBADF; 458 | return -1; 459 | } 460 | 461 | int err = lfs_file_close(&that->lfs, that->fds[fd].file); 462 | 463 | free(that->fds[fd].name); 464 | free(that->fds[fd].file); 465 | that->fds[fd] = {}; 466 | 467 | _lock_release(&that->lock); 468 | 469 | return map_lfs_error(err); 470 | } 471 | 472 | int LittleFlash::fstat_p(void *ctx, int fd, struct stat *st) 473 | { 474 | LittleFlash *that = (LittleFlash *) ctx; 475 | 476 | _lock_acquire(&that->lock); 477 | 478 | if (that->fds[fd].file == NULL) 479 | { 480 | _lock_release(&that->lock); 481 | errno = EBADF; 482 | return -1; 483 | } 484 | 485 | lfs_info lfs_info; 486 | int err = lfs_stat(&that->lfs, that->fds[fd].name, &lfs_info); 487 | 488 | _lock_release(&that->lock); 489 | 490 | if (err < 0) 491 | { 492 | return map_lfs_error(err); 493 | } 494 | 495 | *st = {}; 496 | st->st_size = lfs_info.size; 497 | if (lfs_info.type == LFS_TYPE_DIR) 498 | { 499 | st->st_mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO; 500 | } 501 | else 502 | { 503 | st->st_mode = S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO; 504 | } 505 | 506 | return 0; 507 | } 508 | 509 | int LittleFlash::stat_p(void *ctx, const char *path, struct stat *st) 510 | { 511 | LittleFlash *that = (LittleFlash *) ctx; 512 | 513 | _lock_acquire(&that->lock); 514 | 515 | struct lfs_info lfs_info; 516 | int err = lfs_stat(&that->lfs, path, &lfs_info); 517 | 518 | _lock_release(&that->lock); 519 | 520 | if (err < 0) 521 | { 522 | return map_lfs_error(err); 523 | } 524 | 525 | *st = {}; 526 | st->st_size = lfs_info.size; 527 | if (lfs_info.type == LFS_TYPE_DIR) 528 | { 529 | st->st_mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO; 530 | } 531 | else 532 | { 533 | st->st_mode = S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO; 534 | } 535 | 536 | return 0; 537 | } 538 | 539 | int LittleFlash::unlink_p(void *ctx, const char *path) 540 | { 541 | LittleFlash *that = (LittleFlash *) ctx; 542 | 543 | _lock_acquire(&that->lock); 544 | 545 | int err = lfs_remove(&that->lfs, path); 546 | 547 | _lock_release(&that->lock); 548 | 549 | return map_lfs_error(err); 550 | } 551 | 552 | int LittleFlash::rename_p(void *ctx, const char *src, const char *dst) 553 | { 554 | LittleFlash *that = (LittleFlash *) ctx; 555 | 556 | _lock_acquire(&that->lock); 557 | 558 | int err = lfs_rename(&that->lfs, src, dst); 559 | 560 | _lock_release(&that->lock); 561 | 562 | return map_lfs_error(err); 563 | } 564 | 565 | DIR *LittleFlash::opendir_p(void *ctx, const char *name) 566 | { 567 | LittleFlash *that = (LittleFlash *) ctx; 568 | 569 | vfs_lfs_dir_t *vfs_dir = (vfs_lfs_dir_t *) malloc(sizeof(vfs_lfs_dir_t)); 570 | if (vfs_dir == NULL) 571 | { 572 | errno = ENOMEM; 573 | return NULL; 574 | } 575 | *vfs_dir = {}; 576 | 577 | _lock_acquire(&that->lock); 578 | 579 | int err = lfs_dir_open(&that->lfs, &vfs_dir->lfs_dir, name); 580 | 581 | _lock_release(&that->lock); 582 | 583 | if (err != LFS_ERR_OK) 584 | { 585 | free(vfs_dir); 586 | vfs_dir = NULL; 587 | map_lfs_error(err); 588 | } 589 | 590 | return (DIR *) vfs_dir; 591 | } 592 | 593 | struct dirent *LittleFlash::readdir_p(void *ctx, DIR *pdir) 594 | { 595 | vfs_lfs_dir_t *vfs_dir = (vfs_lfs_dir_t *) pdir; 596 | if (vfs_dir == NULL) 597 | { 598 | errno = EBADF; 599 | return NULL; 600 | } 601 | 602 | struct dirent *out_dirent = NULL; 603 | 604 | int err = readdir_r_p(ctx, pdir, &vfs_dir->dirent, &out_dirent); 605 | if (err != 0) 606 | { 607 | errno = err; 608 | } 609 | 610 | return out_dirent; 611 | } 612 | 613 | int LittleFlash::readdir_r_p(void *ctx, DIR *pdir, struct dirent *entry, struct dirent **out_dirent) 614 | { 615 | LittleFlash *that = (LittleFlash *) ctx; 616 | 617 | vfs_lfs_dir_t *vfs_dir = (vfs_lfs_dir_t *) pdir; 618 | if (vfs_dir == NULL) 619 | { 620 | errno = EBADF; 621 | return errno; 622 | } 623 | 624 | _lock_acquire(&that->lock); 625 | 626 | struct lfs_info lfs_info; 627 | int err = lfs_dir_read(&that->lfs, &vfs_dir->lfs_dir, &lfs_info); 628 | 629 | _lock_release(&that->lock); 630 | 631 | if (err == 0) 632 | { 633 | *out_dirent = NULL; 634 | return 0; 635 | } 636 | 637 | if (err < 0) 638 | { 639 | map_lfs_error(err); 640 | return errno; 641 | } 642 | 643 | entry->d_ino = 0; 644 | if (lfs_info.type == LFS_TYPE_REG) 645 | { 646 | entry->d_type = DT_REG; 647 | } 648 | else if (lfs_info.type == LFS_TYPE_DIR) 649 | { 650 | entry->d_type = DT_DIR; 651 | } 652 | else 653 | { 654 | entry->d_type = DT_UNKNOWN; 655 | } 656 | size_t len = strlcpy(entry->d_name, lfs_info.name, sizeof(entry->d_name)); 657 | 658 | // This "shouldn't" happen, but the LFS name length can be customized and may 659 | // be longer than what's provided in "struct dirent" 660 | if (len >= sizeof(entry->d_name)) 661 | { 662 | errno = ENAMETOOLONG; 663 | return errno; 664 | } 665 | 666 | vfs_dir->off++; 667 | 668 | *out_dirent = entry; 669 | 670 | return 0; 671 | } 672 | 673 | long LittleFlash::telldir_p(void *ctx, DIR *pdir) 674 | { 675 | vfs_lfs_dir_t *vfs_dir = (vfs_lfs_dir_t *) pdir; 676 | if (vfs_dir == NULL) 677 | { 678 | errno = EBADF; 679 | return errno; 680 | } 681 | 682 | return vfs_dir->off; 683 | } 684 | 685 | void LittleFlash::seekdir_p(void *ctx, DIR *pdir, long offset) 686 | { 687 | LittleFlash *that = (LittleFlash *) ctx; 688 | 689 | vfs_lfs_dir_t *vfs_dir = (vfs_lfs_dir_t *) pdir; 690 | if (vfs_dir == NULL) 691 | { 692 | errno = EBADF; 693 | return; 694 | } 695 | 696 | _lock_acquire(&that->lock); 697 | 698 | // ESP32 VFS expects simple 0 to n counted directory offsets but lfs 699 | // doesn't so we need to "translate"... 700 | int err = lfs_dir_rewind(&that->lfs, &vfs_dir->lfs_dir); 701 | if (err >= 0) 702 | { 703 | for (vfs_dir->off = 0; vfs_dir->off < offset; ++vfs_dir->off) 704 | { 705 | struct lfs_info lfs_info; 706 | err = lfs_dir_read(&that->lfs, &vfs_dir->lfs_dir, &lfs_info); 707 | if (err < 0) 708 | { 709 | break; 710 | } 711 | } 712 | } 713 | 714 | _lock_release(&that->lock); 715 | 716 | if (err < 0) 717 | { 718 | map_lfs_error(err); 719 | return; 720 | } 721 | 722 | return; 723 | } 724 | 725 | int LittleFlash::closedir_p(void *ctx, DIR *pdir) 726 | { 727 | LittleFlash *that = (LittleFlash *) ctx; 728 | 729 | vfs_lfs_dir_t *vfs_dir = (vfs_lfs_dir_t *) pdir; 730 | if (vfs_dir == NULL) 731 | { 732 | errno = EBADF; 733 | return -1; 734 | } 735 | 736 | _lock_acquire(&that->lock); 737 | 738 | int err = lfs_dir_close(&that->lfs, &vfs_dir->lfs_dir); 739 | 740 | _lock_release(&that->lock); 741 | 742 | free(vfs_dir); 743 | 744 | return map_lfs_error(err); 745 | } 746 | 747 | int LittleFlash::mkdir_p(void *ctx, const char *name, mode_t mode) 748 | { 749 | LittleFlash *that = (LittleFlash *) ctx; 750 | 751 | _lock_acquire(&that->lock); 752 | 753 | int err = lfs_mkdir(&that->lfs, name); 754 | 755 | _lock_release(&that->lock); 756 | 757 | return map_lfs_error(err); 758 | } 759 | 760 | int LittleFlash::rmdir_p(void *ctx, const char *name) 761 | { 762 | LittleFlash *that = (LittleFlash *) ctx; 763 | 764 | _lock_acquire(&that->lock); 765 | 766 | int err = lfs_remove(&that->lfs, name); 767 | 768 | _lock_release(&that->lock); 769 | 770 | return map_lfs_error(err); 771 | } 772 | 773 | int LittleFlash::fsync_p(void *ctx, int fd) 774 | { 775 | LittleFlash *that = (LittleFlash *) ctx; 776 | 777 | _lock_acquire(&that->lock); 778 | 779 | if (that->fds[fd].file == NULL) 780 | { 781 | _lock_release(&that->lock); 782 | errno = EBADF; 783 | return -1; 784 | } 785 | 786 | int err = lfs_file_sync(&that->lfs, that->fds[fd].file); 787 | 788 | _lock_release(&that->lock); 789 | 790 | return map_lfs_error(err); 791 | } 792 | 793 | // ============================================================================ 794 | // LFS disk interface for external flash 795 | // ============================================================================ 796 | 797 | int LittleFlash::external_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) 798 | { 799 | ESP_LOGD(TAG, "%s - block=0x%08x off=0x%08x size=%d", __func__, block, off, size); 800 | 801 | LittleFlash *that = (LittleFlash *) c->context; 802 | 803 | esp_err_t err = that->cfg.flash->read((block * that->sector_sz) + off, buffer, size); 804 | 805 | return err == ESP_OK ? LFS_ERR_OK : LFS_ERR_IO; 806 | } 807 | 808 | int LittleFlash::external_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size) 809 | { 810 | ESP_LOGD(TAG, "%s - block=0x%08x off=0x%08x size=%d", __func__, block, off, size); 811 | 812 | LittleFlash *that = (LittleFlash *) c->context; 813 | 814 | esp_err_t err = that->cfg.flash->write((block * that->sector_sz) + off, buffer, size); 815 | 816 | return err == ESP_OK ? LFS_ERR_OK : LFS_ERR_IO; 817 | } 818 | 819 | int LittleFlash::external_erase(const struct lfs_config *c, lfs_block_t block) 820 | { 821 | ESP_LOGD(TAG, "%s - block=0x%08x", __func__, block); 822 | 823 | LittleFlash *that = (LittleFlash *) c->context; 824 | 825 | esp_err_t err = that->cfg.flash->erase_sector(block); 826 | 827 | return err == ESP_OK ? LFS_ERR_OK : LFS_ERR_IO; 828 | } 829 | 830 | int LittleFlash::external_sync(const struct lfs_config *c) 831 | { 832 | ESP_LOGD(TAG, "%s - c=%p", __func__, c); 833 | 834 | return LFS_ERR_OK; 835 | } 836 | 837 | // ============================================================================ 838 | // LFS disk interface for internal flash 839 | // ============================================================================ 840 | 841 | int LittleFlash::internal_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size) 842 | { 843 | ESP_LOGD(TAG, "%s - block=0x%08x off=0x%08x size=%d", __func__, block, off, size); 844 | 845 | LittleFlash *that = (LittleFlash *) c->context; 846 | 847 | esp_err_t err = esp_partition_read(that->part, (block * that->sector_sz) + off, buffer, size); 848 | 849 | return err == ESP_OK ? LFS_ERR_OK : LFS_ERR_IO; 850 | } 851 | 852 | int LittleFlash::internal_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size) 853 | { 854 | ESP_LOGD(TAG, "%s - block=0x%08x off=0x%08x size=%d", __func__, block, off, size); 855 | 856 | LittleFlash *that = (LittleFlash *) c->context; 857 | 858 | esp_err_t err = esp_partition_write(that->part, (block * that->sector_sz) + off, buffer, size); 859 | 860 | return err == ESP_OK ? LFS_ERR_OK : LFS_ERR_IO; 861 | } 862 | 863 | int LittleFlash::internal_erase(const struct lfs_config *c, lfs_block_t block) 864 | { 865 | ESP_LOGD(TAG, "%s - block=0x%08x", __func__, block); 866 | 867 | LittleFlash *that = (LittleFlash *) c->context; 868 | 869 | esp_err_t err = esp_partition_erase_range(that->part, block * that->sector_sz, that->sector_sz); 870 | 871 | return err == ESP_OK ? LFS_ERR_OK : LFS_ERR_IO; 872 | } 873 | 874 | int LittleFlash::internal_sync(const struct lfs_config *c) 875 | { 876 | ESP_LOGD(TAG, "%s", __func__); 877 | 878 | return LFS_ERR_OK; 879 | } 880 | 881 | -------------------------------------------------------------------------------- /main/component.mk: -------------------------------------------------------------------------------- 1 | # 2 | # "main" pseudo-component makefile. 3 | # 4 | # (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.) 5 | 6 | -------------------------------------------------------------------------------- /main/littleflash.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2017-2018 Leland Lucius 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | 18 | #include "esp_err.h" 19 | #include "freertos/FreeRTOS.h" 20 | #include "freertos/task.h" 21 | 22 | #include "extflash.h" 23 | #include "wb_w25q_dual.h" 24 | #include "wb_w25q_dio.h" 25 | #include "wb_w25q_quad.h" 26 | #include "wb_w25q_qio.h" 27 | #include "wb_w25q_qpi.h" 28 | 29 | #include "littleflash.h" 30 | 31 | extern "C" 32 | { 33 | #include "unity.h" 34 | #include "test_lfs_common.h" 35 | } 36 | 37 | #define PIN_SPI_MOSI GPIO_NUM_23 // PIN 5 - IO0 - DI 38 | #define PIN_SPI_MISO GPIO_NUM_19 // PIN 2 - IO1 - DO 39 | #define PIN_SPI_WP GPIO_NUM_22 // PIN 3 - IO2 - /WP 40 | #define PIN_SPI_HD GPIO_NUM_21 // PIN 7 - IO3 - /HOLD - /RESET 41 | #define PIN_SPI_SCK GPIO_NUM_18 // PIN 6 - CLK - CLK 42 | #define PIN_SPI_SS GPIO_NUM_5 // PIN 1 - /CS - /CS 43 | 44 | #define MOUNT_POINT "/littleflash" 45 | #define OPENFILES 4 46 | 47 | // Set to the partition name if using internal flash, 48 | // else comment to use externa flash 49 | //#define CONFIG_LITTLEFS_PARTITION_LABEL "littlefs" 50 | 51 | static ExtFlash extflash; 52 | static LittleFlash littleflash; 53 | 54 | static void test_extflash_setup() 55 | { 56 | #if !defined(CONFIG_LITTLEFS_PARTITION_LABEL) 57 | ext_flash_config_t ext_cfg = 58 | { 59 | .vspi = true, 60 | .sck_io_num = PIN_SPI_SCK, 61 | .miso_io_num = PIN_SPI_MISO, 62 | .mosi_io_num = PIN_SPI_MOSI, 63 | .ss_io_num = PIN_SPI_SS, 64 | .hd_io_num = PIN_SPI_HD, 65 | .wp_io_num = PIN_SPI_WP, 66 | .speed_mhz = 40, 67 | .dma_channel = 1, 68 | .queue_size = 4, 69 | .max_dma_size = 0, 70 | .sector_size = 0, 71 | .capacity = 0, 72 | }; 73 | 74 | TST(extflash.init(&ext_cfg) == ESP_OK, "ExtFlash initialization failed"); 75 | #endif 76 | } 77 | 78 | static void test_extflash_teardown() 79 | { 80 | #if !defined(CONFIG_LITTLEFS_PARTITION_LABEL) 81 | extflash.term(); 82 | #endif 83 | } 84 | 85 | static void test_littleflash_setup(int openfiles) 86 | { 87 | const little_flash_config_t little_cfg = 88 | { 89 | #if !defined(CONFIG_LITTLEFS_PARTITION_LABEL) 90 | .flash = &extflash, 91 | .part_label = NULL, 92 | #else 93 | .flash = NULL, 94 | .part_label = CONFIG_LITTLEFS_PARTITION_LABEL, 95 | #endif 96 | .base_path = MOUNT_POINT, 97 | .open_files = openfiles, 98 | .auto_format = true, 99 | .lookahead = 32 100 | }; 101 | 102 | TST(littleflash.init(&little_cfg) == ESP_OK, "LittleFlash initialization failed"); 103 | } 104 | 105 | static void test_littleflash_teardown() 106 | { 107 | littleflash.term(); 108 | } 109 | 110 | static void test_format() 111 | { 112 | #if !defined(CONFIG_LITTLEFS_PARTITION_LABEL) 113 | test_extflash_setup(); 114 | extflash.erase_sector(0); 115 | test_extflash_teardown(); 116 | #else 117 | const esp_partition_t *part = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, 118 | ESP_PARTITION_SUBTYPE_ANY, 119 | CONFIG_LITTLEFS_PARTITION_LABEL); 120 | TEST_ASSERT_NOT_NULL(part); 121 | TEST_ASSERT_EQUAL(esp_partition_erase_range(part, 0, SPI_FLASH_SEC_SIZE), ESP_OK); 122 | #endif 123 | } 124 | 125 | static void test_setup(int openfiles) 126 | { 127 | test_extflash_setup(); 128 | test_littleflash_setup(openfiles); 129 | } 130 | 131 | static void test_teardown() 132 | { 133 | test_littleflash_teardown(); 134 | test_extflash_teardown(); 135 | } 136 | 137 | // 138 | // Tests shamelessly copied from "esp-idf/components/fatfs/test" and modified 139 | // 140 | TEST_CASE(can_format, "can format chip", "[fatfs][wear_levelling]") 141 | { 142 | test_format(); 143 | test_setup(OPENFILES); 144 | test_teardown(); 145 | } 146 | 147 | TEST_CASE(can_create_write, "can create and write file", "[fatfs][wear_levelling]") 148 | { 149 | test_setup(OPENFILES); 150 | test_lfs_create_file_with_text(MOUNT_POINT "/hello.txt", lfs_test_hello_str); 151 | test_teardown(); 152 | } 153 | 154 | TEST_CASE(can_read, "can read file", "[fatfs][wear_levelling]") 155 | { 156 | test_setup(OPENFILES); 157 | test_lfs_create_file_with_text(MOUNT_POINT "/hello.txt", lfs_test_hello_str); 158 | test_lfs_read_file(MOUNT_POINT "/hello.txt"); 159 | test_teardown(); 160 | } 161 | 162 | TEST_CASE(can_open_max, "can open maximum number of files", "[fatfs][wear_levelling]") 163 | { 164 | int max_files = FOPEN_MAX - 3; /* account for stdin, stdout, stderr */ 165 | test_setup(max_files); 166 | test_lfs_open_max_files(MOUNT_POINT "/f", max_files); 167 | test_teardown(); 168 | } 169 | 170 | TEST_CASE(can_overwrite_append, "overwrite and append file", "[fatfs][wear_levelling]") 171 | { 172 | test_setup(OPENFILES); 173 | test_lfs_overwrite_append(MOUNT_POINT "/hello.txt"); 174 | test_teardown(); 175 | } 176 | 177 | TEST_CASE(can_lseek, "can lseek", "[fatfs][wear_levelling]") 178 | { 179 | test_setup(OPENFILES); 180 | test_lfs_lseek(MOUNT_POINT "/seek.txt"); 181 | test_teardown(); 182 | } 183 | 184 | TEST_CASE(can_stat, "stat returns correct values", "[fatfs][wear_levelling]") 185 | { 186 | test_setup(OPENFILES); 187 | test_lfs_stat(MOUNT_POINT "/stat.txt", MOUNT_POINT ""); 188 | test_teardown(); 189 | } 190 | 191 | TEST_CASE(can_unlink, "unlink removes a file", "[fatfs][wear_levelling]") 192 | { 193 | test_setup(OPENFILES); 194 | test_lfs_unlink(MOUNT_POINT "/unlink.txt"); 195 | test_teardown(); 196 | } 197 | 198 | TEST_CASE(can_rename, "rename moves a file", "[fatfs][wear_levelling]") 199 | { 200 | test_setup(OPENFILES); 201 | test_lfs_rename(MOUNT_POINT "/link"); 202 | test_teardown(); 203 | } 204 | 205 | TEST_CASE(can_create_remove, "can create and remove directories", "[fatfs][wear_levelling]") 206 | { 207 | test_setup(OPENFILES); 208 | test_lfs_mkdir_rmdir(MOUNT_POINT "/dir"); 209 | test_teardown(); 210 | } 211 | 212 | TEST_CASE(can_open_root, "can opendir root directory of FS", "[fatfs][wear_levelling]") 213 | { 214 | test_setup(OPENFILES); 215 | test_lfs_can_opendir(MOUNT_POINT ""); 216 | test_teardown(); 217 | } 218 | 219 | TEST_CASE(can_dir, "opendir, readdir, rewinddir, seekdir work as expected", "[fatfs][wear_levelling]") 220 | { 221 | test_setup(OPENFILES); 222 | test_lfs_opendir_readdir_rewinddir(MOUNT_POINT "/dir"); 223 | test_teardown(); 224 | } 225 | 226 | TEST_CASE(can_task, "multiple tasks can use same volume", "[fatfs][wear_levelling]") 227 | { 228 | test_setup(OPENFILES); 229 | test_lfs_concurrent(MOUNT_POINT "/f"); 230 | test_teardown(); 231 | } 232 | 233 | TEST_CASE(can_read_write, "write/read speed test", "[fatfs][wear_levelling]") 234 | { 235 | /* Erase partition before running the test to get consistent results */ 236 | test_format(); 237 | 238 | test_setup(OPENFILES); 239 | 240 | const size_t buf_size = 16 * 1024; 241 | uint32_t* buf = (uint32_t*) calloc(1, buf_size); 242 | for (size_t i = 0; i < buf_size / 4; ++i) { 243 | buf[i] = esp_random(); 244 | } 245 | const size_t file_size = 256 * 1024; 246 | const char* file = MOUNT_POINT "/256k.bin"; 247 | 248 | test_lfs_rw_speed(file, buf, 4 * 1024, file_size, true); 249 | test_lfs_rw_speed(file, buf, 8 * 1024, file_size, true); 250 | test_lfs_rw_speed(file, buf, 16 * 1024, file_size, true); 251 | 252 | test_lfs_rw_speed(file, buf, 4 * 1024, file_size, false); 253 | test_lfs_rw_speed(file, buf, 8 * 1024, file_size, false); 254 | test_lfs_rw_speed(file, buf, 16 * 1024, file_size, false); 255 | 256 | unlink(file); 257 | 258 | free(buf); 259 | test_teardown(); 260 | } 261 | 262 | extern "C" void app_main(void *) 263 | { 264 | can_format(); 265 | can_create_write(); 266 | can_read(); 267 | can_open_max(); 268 | can_overwrite_append(); 269 | can_lseek(); 270 | can_stat(); 271 | can_unlink(); 272 | can_rename(); 273 | can_create_remove(); 274 | can_open_root(); 275 | can_dir(); 276 | can_task(); 277 | can_read_write(); 278 | 279 | printf("All tests done...\n"); 280 | 281 | vTaskDelay(portMAX_DELAY); 282 | } 283 | -------------------------------------------------------------------------------- /main/test_lfs_common.c: -------------------------------------------------------------------------------- 1 | // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include "unity.h" 22 | #include "esp_log.h" 23 | #include "esp_system.h" 24 | #include "esp_vfs.h" 25 | #include "esp_vfs_fat.h" 26 | #include "freertos/FreeRTOS.h" 27 | #include "freertos/task.h" 28 | #include "ff.h" 29 | #include "test_lfs_common.h" 30 | 31 | const char* lfs_test_hello_str = "Hello, World!\n"; 32 | 33 | void test_lfs_create_file_with_text(const char* name, const char* text) 34 | { 35 | FILE* f = fopen(name, "wb"); 36 | TEST_ASSERT_NOT_NULL(f); 37 | TEST_ASSERT_TRUE(fputs(text, f) != EOF); 38 | TEST_ASSERT_EQUAL(0, fclose(f)); 39 | } 40 | 41 | void test_lfs_overwrite_append(const char* filename) 42 | { 43 | /* Create new file with 'aaaa' */ 44 | test_lfs_create_file_with_text(filename, "aaaa"); 45 | 46 | /* Append 'bbbb' to file */ 47 | FILE *f_a = fopen(filename, "a"); 48 | TEST_ASSERT_NOT_NULL(f_a); 49 | TEST_ASSERT_NOT_EQUAL(EOF, fputs("bbbb", f_a)); 50 | TEST_ASSERT_EQUAL(0, fclose(f_a)); 51 | 52 | /* Read back 8 bytes from file, verify it's 'aaaabbbb' */ 53 | char buf[10] = { 0 }; 54 | FILE *f_r = fopen(filename, "r"); 55 | TEST_ASSERT_NOT_NULL(f_r); 56 | TEST_ASSERT_EQUAL(8, fread(buf, 1, 8, f_r)); 57 | TEST_ASSERT_EQUAL_STRING_LEN("aaaabbbb", buf, 8); 58 | 59 | /* Be sure we're at end of file */ 60 | TEST_ASSERT_EQUAL(0, fread(buf, 1, 8, f_r)); 61 | 62 | TEST_ASSERT_EQUAL(0, fclose(f_r)); 63 | 64 | /* Overwrite file with 'cccc' */ 65 | test_lfs_create_file_with_text(filename, "cccc"); 66 | 67 | /* Verify file now only contains 'cccc' */ 68 | f_r = fopen(filename, "r"); 69 | TEST_ASSERT_NOT_NULL(f_r); 70 | bzero(buf, sizeof(buf)); 71 | TEST_ASSERT_EQUAL(4, fread(buf, 1, 8, f_r)); // trying to read 8 bytes, only expecting 4 72 | TEST_ASSERT_EQUAL_STRING_LEN("cccc", buf, 4); 73 | TEST_ASSERT_EQUAL(0, fclose(f_r)); 74 | } 75 | 76 | void test_lfs_read_file(const char* filename) 77 | { 78 | FILE* f = fopen(filename, "r"); 79 | TEST_ASSERT_NOT_NULL(f); 80 | char buf[32] = { 0 }; 81 | int cb = fread(buf, 1, sizeof(buf), f); 82 | TEST_ASSERT_EQUAL(strlen(lfs_test_hello_str), cb); 83 | TEST_ASSERT_EQUAL(0, strcmp(lfs_test_hello_str, buf)); 84 | TEST_ASSERT_EQUAL(0, fclose(f)); 85 | } 86 | 87 | void test_lfs_open_max_files(const char* filename_prefix, size_t files_count) 88 | { 89 | FILE** files = calloc(files_count, sizeof(FILE*)); 90 | for (size_t i = 0; i < files_count; ++i) { 91 | char name[32]; 92 | snprintf(name, sizeof(name), "%s_%d.txt", filename_prefix, i); 93 | files[i] = fopen(name, "w"); 94 | TEST_ASSERT_NOT_NULL(files[i]); 95 | } 96 | /* close everything and clean up */ 97 | for (size_t i = 0; i < files_count; ++i) { 98 | fclose(files[i]); 99 | } 100 | free(files); 101 | } 102 | 103 | void test_lfs_lseek(const char* filename) 104 | { 105 | FILE* f = fopen(filename, "wb+"); 106 | TEST_ASSERT_NOT_NULL(f); 107 | TEST_ASSERT_EQUAL(11, fprintf(f, "0123456789\n")); 108 | TEST_ASSERT_EQUAL(0, fseek(f, -2, SEEK_CUR)); 109 | TEST_ASSERT_EQUAL('9', fgetc(f)); 110 | TEST_ASSERT_EQUAL(0, fseek(f, 3, SEEK_SET)); 111 | TEST_ASSERT_EQUAL('3', fgetc(f)); 112 | TEST_ASSERT_EQUAL(0, fseek(f, -3, SEEK_END)); 113 | TEST_ASSERT_EQUAL('8', fgetc(f)); 114 | TEST_ASSERT_EQUAL(0, fseek(f, 3, SEEK_END)); 115 | TEST_ASSERT_EQUAL(14, ftell(f)); 116 | TEST_ASSERT_EQUAL(4, fprintf(f, "abc\n")); 117 | TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_END)); 118 | TEST_ASSERT_EQUAL(18, ftell(f)); 119 | TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_SET)); 120 | char buf[20]; 121 | TEST_ASSERT_EQUAL(18, fread(buf, 1, sizeof(buf), f)); 122 | const char ref_buf[] = "0123456789\n\0\0\0abc\n"; 123 | TEST_ASSERT_EQUAL_INT8_ARRAY(ref_buf, buf, sizeof(ref_buf) - 1); 124 | 125 | TEST_ASSERT_EQUAL(0, fclose(f)); 126 | } 127 | 128 | void test_lfs_stat(const char* filename, const char* root_dir) 129 | { 130 | test_lfs_create_file_with_text(filename, "foo\n"); 131 | 132 | struct stat st; 133 | TEST_ASSERT_EQUAL(0, stat(filename, &st)); 134 | TEST_ASSERT(st.st_mode & S_IFREG); 135 | TEST_ASSERT_FALSE(st.st_mode & S_IFDIR); 136 | 137 | memset(&st, 0, sizeof(st)); 138 | TEST_ASSERT_EQUAL(0, stat(root_dir, &st)); 139 | TEST_ASSERT(st.st_mode & S_IFDIR); 140 | TEST_ASSERT_FALSE(st.st_mode & S_IFREG); 141 | } 142 | 143 | void test_lfs_unlink(const char* filename) 144 | { 145 | test_lfs_create_file_with_text(filename, "unlink\n"); 146 | 147 | TEST_ASSERT_EQUAL(0, unlink(filename)); 148 | 149 | TEST_ASSERT_NULL(fopen(filename, "r")); 150 | } 151 | 152 | void test_lfs_rename(const char* filename_prefix) 153 | { 154 | char name_copy[64]; 155 | char name_dst[64]; 156 | char name_src[64]; 157 | snprintf(name_copy, sizeof(name_copy), "%s_cpy.txt", filename_prefix); 158 | snprintf(name_dst, sizeof(name_dst), "%s_dst.txt", filename_prefix); 159 | snprintf(name_src, sizeof(name_src), "%s_src.txt", filename_prefix); 160 | 161 | unlink(name_copy); 162 | unlink(name_dst); 163 | unlink(name_src); 164 | 165 | FILE* f = fopen(name_src, "w+"); 166 | TEST_ASSERT_NOT_NULL(f); 167 | char* str = "0123456789"; 168 | for (int i = 0; i < 4000; ++i) { 169 | TEST_ASSERT_NOT_EQUAL(EOF, fputs(str, f)); 170 | } 171 | TEST_ASSERT_EQUAL(0, fclose(f)); 172 | TEST_ASSERT_EQUAL(0, rename(name_src, name_dst)); 173 | TEST_ASSERT_NULL(fopen(name_src, "r")); 174 | FILE* fdst = fopen(name_dst, "r"); 175 | TEST_ASSERT_NOT_NULL(fdst); 176 | TEST_ASSERT_EQUAL(0, fseek(fdst, 0, SEEK_END)); 177 | TEST_ASSERT_EQUAL(40000, ftell(fdst)); 178 | TEST_ASSERT_EQUAL(0, fclose(fdst)); 179 | } 180 | 181 | void test_lfs_mkdir_rmdir(const char* filename_prefix) 182 | { 183 | char name_dir1[64]; 184 | char name_dir2[64]; 185 | char name_dir2_file[64]; 186 | snprintf(name_dir1, sizeof(name_dir1), "%s1", filename_prefix); 187 | snprintf(name_dir2, sizeof(name_dir2), "%s2", filename_prefix); 188 | snprintf(name_dir2_file, sizeof(name_dir2_file), "%s2/1.txt", filename_prefix); 189 | 190 | TEST_ASSERT_EQUAL(0, mkdir(name_dir1, 0755)); 191 | struct stat st; 192 | TEST_ASSERT_EQUAL(0, stat(name_dir1, &st)); 193 | TEST_ASSERT_TRUE(st.st_mode & S_IFDIR); 194 | TEST_ASSERT_FALSE(st.st_mode & S_IFREG); 195 | TEST_ASSERT_EQUAL(0, rmdir(name_dir1)); 196 | TEST_ASSERT_EQUAL(-1, stat(name_dir1, &st)); 197 | 198 | TEST_ASSERT_EQUAL(0, mkdir(name_dir2, 0755)); 199 | test_lfs_create_file_with_text(name_dir2_file, "foo\n"); 200 | TEST_ASSERT_EQUAL(0, stat(name_dir2, &st)); 201 | TEST_ASSERT_TRUE(st.st_mode & S_IFDIR); 202 | TEST_ASSERT_FALSE(st.st_mode & S_IFREG); 203 | TEST_ASSERT_EQUAL(0, stat(name_dir2_file, &st)); 204 | TEST_ASSERT_FALSE(st.st_mode & S_IFDIR); 205 | TEST_ASSERT_TRUE(st.st_mode & S_IFREG); 206 | TEST_ASSERT_EQUAL(-1, rmdir(name_dir2)); 207 | TEST_ASSERT_EQUAL(0, unlink(name_dir2_file)); 208 | TEST_ASSERT_EQUAL(0, rmdir(name_dir2)); 209 | } 210 | 211 | void test_lfs_can_opendir(const char* path) 212 | { 213 | char name_dir_file[64]; 214 | const char * file_name = "test_opd.txt"; 215 | snprintf(name_dir_file, sizeof(name_dir_file), "%s/%s", path, file_name); 216 | unlink(name_dir_file); 217 | test_lfs_create_file_with_text(name_dir_file, "test_opendir\n"); 218 | DIR* dir = opendir(path); 219 | TEST_ASSERT_NOT_NULL(dir); 220 | bool found = false; 221 | while (true) { 222 | struct dirent* de = readdir(dir); 223 | if (!de) { 224 | break; 225 | } 226 | if (strcasecmp(de->d_name, file_name) == 0) { 227 | found = true; 228 | break; 229 | } 230 | } 231 | TEST_ASSERT_TRUE(found); 232 | TEST_ASSERT_EQUAL(0, closedir(dir)); 233 | unlink(name_dir_file); 234 | } 235 | 236 | void test_lfs_opendir_readdir_rewinddir(const char* dir_prefix) 237 | { 238 | char name_dir_inner_file[64]; 239 | char name_dir_inner[64]; 240 | char name_dir_file3[64]; 241 | char name_dir_file2[64]; 242 | char name_dir_file1[64]; 243 | 244 | snprintf(name_dir_inner_file, sizeof(name_dir_inner_file), "%s/inner/3.txt", dir_prefix); 245 | snprintf(name_dir_inner, sizeof(name_dir_inner), "%s/inner", dir_prefix); 246 | snprintf(name_dir_file3, sizeof(name_dir_file2), "%s/boo.bin", dir_prefix); 247 | snprintf(name_dir_file2, sizeof(name_dir_file2), "%s/2.txt", dir_prefix); 248 | snprintf(name_dir_file1, sizeof(name_dir_file1), "%s/1.txt", dir_prefix); 249 | 250 | unlink(name_dir_inner_file); 251 | rmdir(name_dir_inner); 252 | unlink(name_dir_file1); 253 | unlink(name_dir_file2); 254 | unlink(name_dir_file3); 255 | rmdir(dir_prefix); 256 | 257 | TEST_ASSERT_EQUAL(0, mkdir(dir_prefix, 0755)); 258 | test_lfs_create_file_with_text(name_dir_file1, "1\n"); 259 | test_lfs_create_file_with_text(name_dir_file2, "2\n"); 260 | test_lfs_create_file_with_text(name_dir_file3, "\01\02\03"); 261 | TEST_ASSERT_EQUAL(0, mkdir(name_dir_inner, 0755)); 262 | test_lfs_create_file_with_text(name_dir_inner_file, "3\n"); 263 | 264 | DIR* dir = opendir(dir_prefix); 265 | TEST_ASSERT_NOT_NULL(dir); 266 | int count = 0; 267 | const char* names[6]; 268 | while(count < 6) { 269 | struct dirent* de = readdir(dir); 270 | if (!de) { 271 | break; 272 | } 273 | printf("found '%s'\n", de->d_name); 274 | if (strcasecmp(de->d_name, ".") == 0) { 275 | TEST_ASSERT_TRUE(de->d_type == DT_DIR); 276 | names[count] = "."; 277 | ++count; 278 | } else if (strcasecmp(de->d_name, "..") == 0) { 279 | TEST_ASSERT_TRUE(de->d_type == DT_DIR); 280 | names[count] = ".."; 281 | ++count; 282 | } else if (strcasecmp(de->d_name, "1.txt") == 0) { 283 | TEST_ASSERT_TRUE(de->d_type == DT_REG); 284 | names[count] = "1.txt"; 285 | ++count; 286 | } else if (strcasecmp(de->d_name, "2.txt") == 0) { 287 | TEST_ASSERT_TRUE(de->d_type == DT_REG); 288 | names[count] = "2.txt"; 289 | ++count; 290 | } else if (strcasecmp(de->d_name, "inner") == 0) { 291 | TEST_ASSERT_TRUE(de->d_type == DT_DIR); 292 | names[count] = "inner"; 293 | ++count; 294 | } else if (strcasecmp(de->d_name, "boo.bin") == 0) { 295 | TEST_ASSERT_TRUE(de->d_type == DT_REG); 296 | names[count] = "boo.bin"; 297 | ++count; 298 | } else { 299 | TEST_FAIL_MESSAGE("unexpected directory entry"); 300 | } 301 | } 302 | TEST_ASSERT_EQUAL(count, 6); 303 | 304 | rewinddir(dir); 305 | TEST_ASSERT_EQUAL(0, telldir(dir)); 306 | struct dirent* de = readdir(dir); 307 | TEST_ASSERT_NOT_NULL(de); 308 | TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[0])); 309 | TEST_ASSERT_EQUAL(1, telldir(dir)); 310 | seekdir(dir, 3); 311 | TEST_ASSERT_EQUAL(3, telldir(dir)); 312 | de = readdir(dir); 313 | TEST_ASSERT_NOT_NULL(de); 314 | TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[3])); 315 | TEST_ASSERT_EQUAL(4, telldir(dir)); 316 | seekdir(dir, 1); 317 | TEST_ASSERT_EQUAL(1, telldir(dir)); 318 | de = readdir(dir); 319 | TEST_ASSERT_NOT_NULL(de); 320 | TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[1])); 321 | TEST_ASSERT_EQUAL(2, telldir(dir)); 322 | seekdir(dir, 2); 323 | TEST_ASSERT_EQUAL(2, telldir(dir)); 324 | de = readdir(dir); 325 | TEST_ASSERT_NOT_NULL(de); 326 | TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[2])); 327 | TEST_ASSERT_EQUAL(3, telldir(dir)); 328 | 329 | TEST_ASSERT_EQUAL(0, closedir(dir)); 330 | } 331 | 332 | typedef struct { 333 | const char* filename; 334 | bool write; 335 | size_t word_count; 336 | int seed; 337 | SemaphoreHandle_t done; 338 | int result; 339 | } read_write_test_arg_t; 340 | 341 | #define READ_WRITE_TEST_ARG_INIT(name, seed_) \ 342 | { \ 343 | .filename = name, \ 344 | .seed = seed_, \ 345 | .word_count = 8192, \ 346 | .write = true, \ 347 | .done = xSemaphoreCreateBinary() \ 348 | } 349 | 350 | static void read_write_task(void* param) 351 | { 352 | read_write_test_arg_t* args = (read_write_test_arg_t*) param; 353 | FILE* f = fopen(args->filename, args->write ? "wb" : "rb"); 354 | if (f == NULL) { 355 | args->result = ESP_ERR_NOT_FOUND; 356 | goto done; 357 | } 358 | 359 | srand(args->seed); 360 | for (size_t i = 0; i < args->word_count; ++i) { 361 | uint32_t val = rand(); 362 | if (args->write) { 363 | int cnt = fwrite(&val, sizeof(val), 1, f); 364 | if (cnt != 1) { 365 | ets_printf("E(w): i=%d, cnt=%d val=%d\n\n", i, cnt, val); 366 | args->result = ESP_FAIL; 367 | goto close; 368 | } 369 | } else { 370 | uint32_t rval; 371 | int cnt = fread(&rval, sizeof(rval), 1, f); 372 | if (cnt != 1 || rval != val) { 373 | ets_printf("E(r): i=%d, cnt=%d rval=%d val=%d\n\n", i, cnt, rval, val); 374 | args->result = ESP_FAIL; 375 | goto close; 376 | } 377 | } 378 | } 379 | args->result = ESP_OK; 380 | 381 | close: 382 | fclose(f); 383 | 384 | done: 385 | xSemaphoreGive(args->done); 386 | vTaskDelay(1); 387 | vTaskDelete(NULL); 388 | } 389 | 390 | void test_lfs_concurrent(const char* filename_prefix) 391 | { 392 | char names[4][64]; 393 | for (size_t i = 0; i < 4; ++i) { 394 | snprintf(names[i], sizeof(names[i]), "%s%d", filename_prefix, i + 1); 395 | unlink(names[i]); 396 | } 397 | 398 | read_write_test_arg_t args1 = READ_WRITE_TEST_ARG_INIT(names[0], 1); 399 | read_write_test_arg_t args2 = READ_WRITE_TEST_ARG_INIT(names[1], 2); 400 | 401 | printf("writing f1 and f2\n"); 402 | 403 | const int cpuid_0 = 0; 404 | const int cpuid_1 = portNUM_PROCESSORS - 1; 405 | xTaskCreatePinnedToCore(&read_write_task, "rw1", 2048, &args1, 3, NULL, cpuid_0); 406 | xTaskCreatePinnedToCore(&read_write_task, "rw2", 2048, &args2, 3, NULL, cpuid_1); 407 | 408 | xSemaphoreTake(args1.done, portMAX_DELAY); 409 | printf("f1 done\n"); 410 | TEST_ASSERT_EQUAL(ESP_OK, args1.result); 411 | xSemaphoreTake(args2.done, portMAX_DELAY); 412 | printf("f2 done\n"); 413 | TEST_ASSERT_EQUAL(ESP_OK, args2.result); 414 | 415 | args1.write = false; 416 | args2.write = false; 417 | read_write_test_arg_t args3 = READ_WRITE_TEST_ARG_INIT(names[2], 3); 418 | read_write_test_arg_t args4 = READ_WRITE_TEST_ARG_INIT(names[3], 4); 419 | 420 | printf("reading f1 and f2, writing f3 and f4\n"); 421 | 422 | xTaskCreatePinnedToCore(&read_write_task, "rw3", 2048, &args3, 3, NULL, cpuid_1); 423 | xTaskCreatePinnedToCore(&read_write_task, "rw4", 2048, &args4, 3, NULL, cpuid_0); 424 | xTaskCreatePinnedToCore(&read_write_task, "rw1", 2048, &args1, 3, NULL, cpuid_0); 425 | xTaskCreatePinnedToCore(&read_write_task, "rw2", 2048, &args2, 3, NULL, cpuid_1); 426 | 427 | xSemaphoreTake(args1.done, portMAX_DELAY); 428 | printf("f1 done\n"); 429 | TEST_ASSERT_EQUAL(ESP_OK, args1.result); 430 | xSemaphoreTake(args2.done, portMAX_DELAY); 431 | printf("f2 done\n"); 432 | TEST_ASSERT_EQUAL(ESP_OK, args2.result); 433 | xSemaphoreTake(args3.done, portMAX_DELAY); 434 | printf("f3 done\n"); 435 | TEST_ASSERT_EQUAL(ESP_OK, args3.result); 436 | xSemaphoreTake(args4.done, portMAX_DELAY); 437 | printf("f4 done\n"); 438 | TEST_ASSERT_EQUAL(ESP_OK, args4.result); 439 | 440 | vSemaphoreDelete(args1.done); 441 | vSemaphoreDelete(args2.done); 442 | vSemaphoreDelete(args3.done); 443 | vSemaphoreDelete(args4.done); 444 | } 445 | 446 | 447 | void test_lfs_rw_speed(const char* filename, void* buf, size_t buf_size, size_t file_size, bool write) 448 | { 449 | const size_t buf_count = file_size / buf_size; 450 | 451 | FILE* f = fopen(filename, (write) ? "wb" : "rb"); 452 | TEST_ASSERT_NOT_NULL(f); 453 | 454 | struct timeval tv_start; 455 | gettimeofday(&tv_start, NULL); 456 | for (size_t n = 0; n < buf_count; ++n) { 457 | if (write) { 458 | TEST_ASSERT_EQUAL(1, fwrite(buf, buf_size, 1, f)); 459 | } else { 460 | if (fread(buf, buf_size, 1, f) != 1) { 461 | printf("reading at n=%d, eof=%d", n, feof(f)); 462 | TEST_FAIL(); 463 | } 464 | } 465 | } 466 | 467 | struct timeval tv_end; 468 | gettimeofday(&tv_end, NULL); 469 | 470 | TEST_ASSERT_EQUAL(0, fclose(f)); 471 | 472 | float t_s = tv_end.tv_sec - tv_start.tv_sec + 1e-6f * (tv_end.tv_usec - tv_start.tv_usec); 473 | printf("%s %d bytes (block size %d) in %.3fms (%.3f MB/s)\n", 474 | (write)?"Wrote":"Read", file_size, buf_size, t_s * 1e3, 475 | file_size / (1024.0f * 1024.0f * t_s)); 476 | } 477 | 478 | -------------------------------------------------------------------------------- /main/test_lfs_common.h: -------------------------------------------------------------------------------- 1 | // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // ============================================================================ 16 | // Copied from "/esp32/esp-idf/components/fatfs/test/test_fatfs_common.[ch]" 17 | // ============================================================================ 18 | 19 | #pragma once 20 | 21 | /** 22 | * @file test_lfs_common.h 23 | * @brief Common routines for FAT-on-SDMMC and FAT-on-WL tests 24 | */ 25 | 26 | #define HEAP_SIZE_CAPTURE(heap_size) \ 27 | heap_size = esp_get_free_heap_size(); 28 | 29 | #define HEAP_SIZE_CHECK(heap_size, tolerance) \ 30 | do {\ 31 | size_t final_heap_size = esp_get_free_heap_size(); \ 32 | if (final_heap_size < heap_size - tolerance) { \ 33 | printf("Initial heap size: %d, final: %d, diff=%d\n", heap_size, final_heap_size, heap_size - final_heap_size); \ 34 | } \ 35 | } while(0) 36 | 37 | 38 | extern const char* lfs_test_hello_str; 39 | 40 | void test_lfs_create_file_with_text(const char* name, const char* text); 41 | 42 | void test_lfs_overwrite_append(const char* filename); 43 | 44 | void test_lfs_read_file(const char* filename); 45 | 46 | void test_lfs_open_max_files(const char* filename_prefix, size_t files_count); 47 | 48 | void test_lfs_lseek(const char* filename); 49 | 50 | void test_lfs_stat(const char* filename, const char* root_dir); 51 | 52 | void test_lfs_unlink(const char* filename); 53 | 54 | void test_lfs_rename(const char* filename_prefix); 55 | 56 | void test_lfs_concurrent(const char* filename_prefix); 57 | 58 | void test_lfs_mkdir_rmdir(const char* filename_prefix); 59 | 60 | void test_lfs_can_opendir(const char* path); 61 | 62 | void test_lfs_opendir_readdir_rewinddir(const char* dir_prefix); 63 | 64 | void test_lfs_rw_speed(const char* filename, void* buf, size_t buf_size, size_t file_size, bool write); 65 | -------------------------------------------------------------------------------- /main/unity.h: -------------------------------------------------------------------------------- 1 | // Copyright 2017-2018 Leland Lucius 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #if !defined(_UNITY_H_) 16 | #define _UNITY_H_ 1 17 | 18 | // ============================================================================ 19 | // Simulate unity test macros 20 | // ============================================================================ 21 | 22 | #define TST(c,m) \ 23 | { \ 24 | if (!(c)) \ 25 | { \ 26 | printf("%s(%d) - \"%s\" - %s\n", __func__, __LINE__, #c, m); \ 27 | abort(); \ 28 | } \ 29 | } 30 | 31 | #define TEST_ASSERT_EQUAL_INT8_ARRAY(expected,actual,num_elements) \ 32 | { \ 33 | for (int test_i = 0; test_i < (num_elements); ++test_i) \ 34 | { \ 35 | int8_t test_e = ((expected))[test_i]; \ 36 | int8_t test_a = ((actual))[test_i]; \ 37 | if (test_e != test_a) \ 38 | { \ 39 | printf("%s(%d) - arrays \"%s\" and \"%s\" not equal at %d", \ 40 | __func__, __LINE__, #expected, #actual, test_e); \ 41 | abort(); \ 42 | } \ 43 | } \ 44 | } 45 | 46 | #define TEST_ASSERT(condition) \ 47 | TST((condition), "evaluated FALSE"); 48 | 49 | #define TEST_ASSERT_FALSE(condition) \ 50 | TST(!(condition), "expected FALSE got TRUE"); 51 | 52 | #define TEST_ASSERT_TRUE(condition) \ 53 | TST((condition), "expected TRUE got FALSE"); 54 | 55 | #define TEST_ASSERT_NULL(condition) \ 56 | TST((condition) == NULL, "expected NULL"); 57 | 58 | #define TEST_ASSERT_NOT_NULL(condition) \ 59 | TST((condition) != NULL, "expected non-NULL"); 60 | 61 | #define TEST_ASSERT_EQUAL(expected, actual) \ 62 | TST(((expected) == (actual)), "expected to be equal but wasn't"); 63 | 64 | #define TEST_ASSERT_NOT_EQUAL(expected, actual) \ 65 | TST(((expected) != (actual)), "expected to be not equal but was"); 66 | 67 | #define TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len) \ 68 | TST((strncmp((expected), (actual), (len)) == 0), "expected to be the same"); 69 | 70 | #define TEST_FAIL() \ 71 | TST((1), "forced failure"); 72 | 73 | #define TEST_FAIL_MESSAGE(m) \ 74 | TST((1), (m)); 75 | 76 | #define TEST_CASE(n, d, c) \ 77 | void test_ ## n(); \ 78 | void n() \ 79 | { \ 80 | printf("TEST: %s\n", d); \ 81 | test_ ## n(); \ 82 | } \ 83 | void test_ ## n() 84 | 85 | #endif 86 | -------------------------------------------------------------------------------- /partitions.csv: -------------------------------------------------------------------------------- 1 | # Name, Type, SubType, Offset, Size, Flags 2 | # Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild 3 | #reserved, data, data, 0x0000, 4K 4 | #bootldr, app, boot, 0x1000, 28K 5 | #parttbl, data, part, 0x8000, 4K 6 | nvs, data, nvs, 0x9000, 20K 7 | otadata, data, ota, 0xe000, 8K 8 | ota_0, app, ota_0, 0x10000, 1024K 9 | ota_1, app, ota_1, 0x110000, 1024K 10 | littlefs, data, ota, 0x210000, 1984K 11 | --------------------------------------------------------------------------------