2021-09-21 18:32:54 -04:00
|
|
|
/*
|
2022-02-09 10:09:09 -05:00
|
|
|
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
2021-09-21 18:32:54 -04:00
|
|
|
*
|
2022-02-09 10:09:09 -05:00
|
|
|
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
2021-09-21 18:32:54 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "esp_vfs.h"
|
|
|
|
#include "esp_vfs_fat.h"
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
|
2021-09-21 18:32:54 -04:00
|
|
|
#if CONFIG_EXAMPLE_FATFS_MODE_READ_ONLY
|
|
|
|
#define EXAMPLE_FATFS_MODE_READ_ONLY true
|
|
|
|
#else
|
|
|
|
#define EXAMPLE_FATFS_MODE_READ_ONLY false
|
|
|
|
#endif
|
|
|
|
|
2022-02-09 10:09:09 -05:00
|
|
|
#if CONFIG_FATFS_LFN_NONE
|
|
|
|
#define EXAMPLE_FATFS_LONG_NAMES false
|
|
|
|
#else
|
|
|
|
#define EXAMPLE_FATFS_LONG_NAMES true
|
|
|
|
#endif
|
|
|
|
|
2021-09-21 18:32:54 -04:00
|
|
|
static const char *TAG = "example";
|
|
|
|
|
|
|
|
|
|
|
|
// Mount path for the partition
|
|
|
|
const char *base_path = "/spiflash";
|
|
|
|
|
2021-09-21 18:32:54 -04:00
|
|
|
// Handle of the wear levelling library instance
|
|
|
|
static wl_handle_t s_wl_handle = WL_INVALID_HANDLE;
|
|
|
|
|
2021-09-21 18:32:54 -04:00
|
|
|
void app_main(void)
|
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "Mounting FAT filesystem");
|
|
|
|
// To mount device we need name of device partition, define base_path
|
|
|
|
// and allow format partition in case if it is new one and was not formatted before
|
|
|
|
const esp_vfs_fat_mount_config_t mount_config = {
|
|
|
|
.max_files = 4,
|
|
|
|
.format_if_mount_failed = false,
|
|
|
|
.allocation_unit_size = CONFIG_WL_SECTOR_SIZE
|
|
|
|
};
|
2021-09-21 18:32:54 -04:00
|
|
|
esp_err_t err;
|
|
|
|
if (EXAMPLE_FATFS_MODE_READ_ONLY){
|
2022-03-04 14:15:49 -05:00
|
|
|
err = esp_vfs_fat_spiflash_mount_ro(base_path, "storage", &mount_config);
|
2021-09-21 18:32:54 -04:00
|
|
|
} else {
|
2022-03-04 14:15:49 -05:00
|
|
|
err = esp_vfs_fat_spiflash_mount_rw_wl(base_path, "storage", &mount_config, &s_wl_handle);
|
2021-09-21 18:32:54 -04:00
|
|
|
}
|
|
|
|
|
2021-09-21 18:32:54 -04:00
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
|
|
|
|
return;
|
|
|
|
}
|
2021-09-21 18:32:54 -04:00
|
|
|
|
|
|
|
char line[128];
|
2022-02-09 10:09:09 -05:00
|
|
|
char *device_filename;
|
|
|
|
if (EXAMPLE_FATFS_LONG_NAMES){
|
|
|
|
device_filename = "/spiflash/innerbutverylongname.txt";
|
|
|
|
} else {
|
|
|
|
device_filename = "/spiflash/inner.txt";
|
|
|
|
}
|
|
|
|
|
2021-09-21 18:32:54 -04:00
|
|
|
if (!EXAMPLE_FATFS_MODE_READ_ONLY){
|
|
|
|
// Open file for reading
|
|
|
|
ESP_LOGI(TAG, "Opening file");
|
2022-06-29 09:37:06 -04:00
|
|
|
FILE *f;
|
|
|
|
for(int i = 0; i < CONFIG_EXAMPLE_FATFS_WRITE_COUNT; i++){
|
|
|
|
f = fopen(device_filename, "wb");
|
|
|
|
if (f == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Failed to open file for writing");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fprintf(f, "This is written by the device");
|
|
|
|
fclose(f);
|
2021-09-21 18:32:54 -04:00
|
|
|
}
|
2022-06-29 09:37:06 -04:00
|
|
|
|
2021-09-21 18:32:54 -04:00
|
|
|
ESP_LOGI(TAG, "File written");
|
|
|
|
|
|
|
|
// Open file for reading
|
|
|
|
ESP_LOGI(TAG, "Reading file");
|
2022-02-09 10:09:09 -05:00
|
|
|
f = fopen(device_filename, "rb");
|
2021-09-21 18:32:54 -04:00
|
|
|
if (f == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Failed to open file for reading");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fgets(line, sizeof(line), f);
|
|
|
|
fclose(f);
|
|
|
|
// strip newline
|
|
|
|
char *pos = strchr(line, '\n');
|
|
|
|
if (pos) {
|
|
|
|
*pos = '\0';
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Read from file: '%s'", line);
|
|
|
|
|
|
|
|
}
|
|
|
|
FILE *f;
|
|
|
|
char *pos;
|
2021-09-21 18:32:54 -04:00
|
|
|
ESP_LOGI(TAG, "Reading file");
|
2022-02-09 10:09:09 -05:00
|
|
|
char *host_filename1;
|
|
|
|
char *host_filename2;
|
|
|
|
|
|
|
|
if (EXAMPLE_FATFS_LONG_NAMES){
|
|
|
|
host_filename1 = "/spiflash/sublongnames/testlongfilenames.txt";
|
|
|
|
host_filename2 = "/spiflash/hellolongname.txt";
|
|
|
|
} else{
|
|
|
|
host_filename1 = "/spiflash/sub/test.txt";
|
|
|
|
host_filename2 = "/spiflash/hello.txt";
|
|
|
|
}
|
|
|
|
|
2022-03-17 13:02:16 -04:00
|
|
|
struct stat info;
|
|
|
|
struct tm timeinfo;
|
|
|
|
char buffer[32];
|
|
|
|
|
|
|
|
if(stat(host_filename1, &info) < 0){
|
|
|
|
ESP_LOGE(TAG, "Failed to read file stats");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
localtime_r(&info.st_mtime, &timeinfo);
|
|
|
|
strftime(buffer, sizeof(buffer), "%Y-%m-%d", &timeinfo);
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "The file '%s' was modified at date: %s", host_filename1, buffer);
|
|
|
|
|
|
|
|
|
2021-09-21 18:32:54 -04:00
|
|
|
if (EXAMPLE_FATFS_MODE_READ_ONLY){
|
2022-02-09 10:09:09 -05:00
|
|
|
f = fopen(host_filename1, "rb");
|
2021-09-21 18:32:54 -04:00
|
|
|
} else {
|
2022-02-09 10:09:09 -05:00
|
|
|
f = fopen(host_filename2, "rb");
|
2021-09-21 18:32:54 -04:00
|
|
|
}
|
2021-09-21 18:32:54 -04:00
|
|
|
if (f == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Failed to open file for reading");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fgets(line, sizeof(line), f);
|
|
|
|
fclose(f);
|
|
|
|
// strip newline
|
2021-09-21 18:32:54 -04:00
|
|
|
pos = strchr(line, '\n');
|
2021-09-21 18:32:54 -04:00
|
|
|
if (pos) {
|
|
|
|
*pos = '\0';
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Read from file: '%s'", line);
|
|
|
|
|
|
|
|
// Unmount FATFS
|
|
|
|
ESP_LOGI(TAG, "Unmounting FAT filesystem");
|
2021-09-21 18:32:54 -04:00
|
|
|
if (EXAMPLE_FATFS_MODE_READ_ONLY){
|
2022-03-04 14:15:49 -05:00
|
|
|
ESP_ERROR_CHECK(esp_vfs_fat_spiflash_unmount_ro(base_path, "storage"));
|
2021-09-21 18:32:54 -04:00
|
|
|
} else {
|
2022-03-04 14:15:49 -05:00
|
|
|
ESP_ERROR_CHECK(esp_vfs_fat_spiflash_unmount_rw_wl(base_path, s_wl_handle));
|
2021-09-21 18:32:54 -04:00
|
|
|
}
|
2021-09-21 18:32:54 -04:00
|
|
|
ESP_LOGI(TAG, "Done");
|
|
|
|
}
|