From 3690eab433d6ae337a703e52331374afb460139b Mon Sep 17 00:00:00 2001 From: Dong Heng Date: Thu, 27 Feb 2020 11:46:10 +0800 Subject: [PATCH] feat(fatfs): modify FATFS for ESP8266 --- .../esp8266/include/driver/sdspi_host.h | 6 ++-- components/fatfs/CMakeLists.txt | 3 +- components/fatfs/component.mk | 2 +- components/fatfs/test/test_fatfs_common.c | 2 +- components/fatfs/test/test_fatfs_rawflash.c | 29 +++++++++++---- components/fatfs/test/test_fatfs_sdmmc.c | 6 +++- components/newlib/component.mk | 2 +- components/newlib/newlib/include/stdio.h | 2 +- .../newlib/newlib/port/include/sys/unistd.h | 31 ++++++++++++++++ .../newlib/newlib/port/include/sys/utime.h | 35 +++++++++++++++++++ components/newlib/newlib/port/pread.c | 21 +++++++++++ components/newlib/newlib/port/pwrite.c | 21 +++++++++++ components/newlib/newlib/port/utime.c | 21 +++++++++++ .../partition_table_unit_test_app.csv | 2 +- 14 files changed, 166 insertions(+), 17 deletions(-) create mode 100644 components/newlib/newlib/port/include/sys/unistd.h create mode 100644 components/newlib/newlib/port/include/sys/utime.h create mode 100644 components/newlib/newlib/port/pread.c create mode 100644 components/newlib/newlib/port/pwrite.c create mode 100644 components/newlib/newlib/port/utime.c diff --git a/components/esp8266/include/driver/sdspi_host.h b/components/esp8266/include/driver/sdspi_host.h index 6eb24587..56400252 100644 --- a/components/esp8266/include/driver/sdspi_host.h +++ b/components/esp8266/include/driver/sdspi_host.h @@ -19,7 +19,7 @@ #include "esp_err.h" #include "sdmmc_types.h" #include "driver/gpio.h" -#include "driver/spi_master.h" +// #include "driver/spi_master.h" #ifdef __cplusplus extern "C" { @@ -32,7 +32,7 @@ extern "C" { * * 'slot' can be set to one of HSPI_HOST, VSPI_HOST. */ -#define SDSPI_HOST_DEFAULT() {\ +/*#define SDSPI_HOST_DEFAULT() {\ .flags = SDMMC_HOST_FLAG_SPI, \ .slot = HSPI_HOST, \ .max_freq_khz = SDMMC_FREQ_DEFAULT, \ @@ -47,7 +47,7 @@ extern "C" { .io_int_enable = &sdspi_host_io_int_enable, \ .io_int_wait = &sdspi_host_io_int_wait, \ .command_timeout_ms = 0, \ -} +}*/ /** * Extra configuration for SPI host diff --git a/components/fatfs/CMakeLists.txt b/components/fatfs/CMakeLists.txt index b5f3af8d..882f1fa4 100644 --- a/components/fatfs/CMakeLists.txt +++ b/components/fatfs/CMakeLists.txt @@ -1,6 +1,5 @@ set(srcs "diskio/diskio.c" "diskio/diskio_rawflash.c" - "diskio/diskio_sdmmc.c" "diskio/diskio_wl.c" "src/ff.c" "port/freertos/ffsystem.c" @@ -14,5 +13,5 @@ endif() idf_component_register(SRCS ${srcs} INCLUDE_DIRS diskio vfs src - REQUIRES wear_levelling sdmmc + REQUIRES wear_levelling ) diff --git a/components/fatfs/component.mk b/components/fatfs/component.mk index bf33270c..996a6522 100644 --- a/components/fatfs/component.mk +++ b/components/fatfs/component.mk @@ -1,3 +1,3 @@ COMPONENT_ADD_INCLUDEDIRS := diskio vfs src COMPONENT_SRCDIRS := diskio vfs port/freertos src -COMPONENT_OBJEXCLUDE := src/diskio.o src/ffsystem.o +COMPONENT_OBJEXCLUDE := src/diskio.o src/ffsystem.o diskio/diskio_sdmmc.o vfs/vfs_fat_sdmmc.o diff --git a/components/fatfs/test/test_fatfs_common.c b/components/fatfs/test/test_fatfs_common.c index 09e29382..41ae0d22 100644 --- a/components/fatfs/test/test_fatfs_common.c +++ b/components/fatfs/test/test_fatfs_common.c @@ -694,7 +694,7 @@ static void read_write_task(void* param) srand(args->seed); for (size_t i = 0; i < args->word_count; ++i) { - uint32_t val = rand(); + uint32_t val = i * 77; if (args->write) { int cnt = fwrite(&val, sizeof(val), 1, f); if (cnt != 1) { diff --git a/components/fatfs/test/test_fatfs_rawflash.c b/components/fatfs/test/test_fatfs_rawflash.c index ca949e1a..9f34d3c0 100644 --- a/components/fatfs/test/test_fatfs_rawflash.c +++ b/components/fatfs/test/test_fatfs_rawflash.c @@ -41,13 +41,30 @@ static void test_setup(size_t max_files) }; const esp_partition_t* part = get_test_data_partition(); - TEST_ASSERT(part->size == (fatfs_end - fatfs_start - 1)); + TEST_ASSERT(part->size >= (fatfs_end - fatfs_start - 1)); - spi_flash_mmap_handle_t mmap_handle; - const void* mmap_ptr; - TEST_ESP_OK(esp_partition_mmap(part, 0, part->size, SPI_FLASH_MMAP_DATA, &mmap_ptr, &mmap_handle)); - bool content_valid = memcmp(fatfs_start, mmap_ptr, part->size) == 0; - spi_flash_munmap(mmap_handle); + // spi_flash_mmap_handle_t mmap_handle; + // const void* mmap_ptr; + // TEST_ESP_OK(esp_partition_mmap(part, 0, part->size, SPI_FLASH_MMAP_DATA, &mmap_ptr, &mmap_handle)); + // bool content_valid = memcmp(fatfs_start, mmap_ptr, part->size) == 0; + // spi_flash_munmap(mmap_handle); + + size_t max_space_size = MIN(part->size, fatfs_end - fatfs_start - 1); + bool content_valid = true; + char *buf = malloc(4096); + TEST_ASSERT_NOT_NULL(buf); + + for (size_t i = 0; i < max_space_size; i += 4096) { + size_t bytes = MIN(4096, max_space_size - i); + + TEST_ESP_OK(esp_partition_read(part, i, buf, bytes)); + if (memcmp(fatfs_start + i, buf, bytes)) { + content_valid = false; + break; + } + } + + free(buf); if (!content_valid) { printf("Copying fatfs.img into test partition...\n"); diff --git a/components/fatfs/test/test_fatfs_sdmmc.c b/components/fatfs/test/test_fatfs_sdmmc.c index 09deb516..a484c7b8 100644 --- a/components/fatfs/test/test_fatfs_sdmmc.c +++ b/components/fatfs/test/test_fatfs_sdmmc.c @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#if 0 + #include #include #include @@ -297,4 +299,6 @@ TEST_CASE("(SD) opendir, readdir, rewinddir, seekdir work as expected using UTF- } #endif // CONFIG_FATFS_API_ENCODING_UTF_8 && CONFIG_FATFS_CODEPAGE == 936 -#endif \ No newline at end of file +#endif + +#endif diff --git a/components/newlib/component.mk b/components/newlib/component.mk index b432f4dd..036876d7 100644 --- a/components/newlib/component.mk +++ b/components/newlib/component.mk @@ -11,7 +11,7 @@ else ifdef CONFIG_NEWLIB_LIBRARY_LEVEL_FLOAT_NANO LIB_PATH := $(COMPONENT_PATH)/newlib/lib/libc_fnano.a $(COMPONENT_PATH)/newlib/lib/libm.a endif -COMPONENT_ADD_INCLUDEDIRS += newlib/port/include newlib/include +COMPONENT_ADD_INCLUDEDIRS := newlib/port/include newlib/include COMPONENT_SRCDIRS += newlib/port COMPONENT_ADD_LDFLAGS := $(LIB_PATH) -lnewlib COMPONENT_ADD_LINKER_DEPS := $(LIB_PATH) diff --git a/components/newlib/newlib/include/stdio.h b/components/newlib/newlib/include/stdio.h index e336ee6e..c064c7b1 100644 --- a/components/newlib/newlib/include/stdio.h +++ b/components/newlib/newlib/include/stdio.h @@ -110,7 +110,7 @@ typedef _fpos64_t fpos64_t; #ifdef __FOPEN_MAX__ #define FOPEN_MAX __FOPEN_MAX__ #else -#define FOPEN_MAX 20 +#define FOPEN_MAX 10 #endif #ifdef __FILENAME_MAX__ diff --git a/components/newlib/newlib/port/include/sys/unistd.h b/components/newlib/newlib/port/include/sys/unistd.h new file mode 100644 index 00000000..c3149486 --- /dev/null +++ b/components/newlib/newlib/port/include/sys/unistd.h @@ -0,0 +1,31 @@ +// Copyright 2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + +#ifndef _ESP_SYS_UNISTD_H +#define _ESP_SYS_UNISTD_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include_next + +int truncate(const char *, off_t __length); +int gethostname(char *__name, size_t __len); + +#ifdef __cplusplus +} +#endif +#endif /* _SYS_UNISTD_H */ diff --git a/components/newlib/newlib/port/include/sys/utime.h b/components/newlib/newlib/port/include/sys/utime.h new file mode 100644 index 00000000..3251d3ce --- /dev/null +++ b/components/newlib/newlib/port/include/sys/utime.h @@ -0,0 +1,35 @@ +// Copyright 2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef _UTIME_H_ +#define _UTIME_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct utimbuf { + time_t actime; // access time + time_t modtime; // modification time +}; + +int utime(const char *path, const struct utimbuf *times); + +#ifdef __cplusplus +}; +#endif + +#endif /* _UTIME_H_ */ diff --git a/components/newlib/newlib/port/pread.c b/components/newlib/newlib/port/pread.c new file mode 100644 index 00000000..d14bd725 --- /dev/null +++ b/components/newlib/newlib/port/pread.c @@ -0,0 +1,21 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include "esp_vfs.h" + +ssize_t pread(int fd, void *dst, size_t size, off_t offset) +{ + return esp_vfs_pread(fd, dst, size, offset); +} diff --git a/components/newlib/newlib/port/pwrite.c b/components/newlib/newlib/port/pwrite.c new file mode 100644 index 00000000..78fd0b85 --- /dev/null +++ b/components/newlib/newlib/port/pwrite.c @@ -0,0 +1,21 @@ +// Copyright 2019 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include "esp_vfs.h" + +ssize_t pwrite(int fd, const void *src, size_t size, off_t offset) +{ + return esp_vfs_pwrite(fd, src, size, offset); +} diff --git a/components/newlib/newlib/port/utime.c b/components/newlib/newlib/port/utime.c new file mode 100644 index 00000000..c838fd45 --- /dev/null +++ b/components/newlib/newlib/port/utime.c @@ -0,0 +1,21 @@ +// Copyright 2018 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include "esp_vfs.h" + +int utime(const char *path, const struct utimbuf *times) +{ + return esp_vfs_utime(path, times); +} diff --git a/tools/unit-test-app/partition_table_unit_test_app.csv b/tools/unit-test-app/partition_table_unit_test_app.csv index e597a011..0e7ba18e 100644 --- a/tools/unit-test-app/partition_table_unit_test_app.csv +++ b/tools/unit-test-app/partition_table_unit_test_app.csv @@ -9,6 +9,6 @@ factory, 0, 0, 0x10000, 0xF0000 ota_0, 0, ota_0, , 64K ota_1, 0, ota_1, , 64K # flash_test partition used for SPI flash tests, WL FAT tests, and SPIFFS tests -flash_test, data, fat, , 528K +flash_test, data, fat, , 896K # Note: still 1MB of a 4MB flash left free for some other purpose