kopia lustrzana https://github.com/espressif/esp-idf
131 wiersze
3.7 KiB
C
131 wiersze
3.7 KiB
C
/* Wear levelling and FAT filesystem example.
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
This sample shows how to store files inside a FAT filesystem.
|
|
FAT filesystem is stored in a partition inside SPI flash, using the
|
|
flash wear levelling library.
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "esp_vfs.h"
|
|
#include "esp_vfs_fat.h"
|
|
#include "esp_system.h"
|
|
|
|
#define EXAMPLE_MAX_CHAR_SIZE 128
|
|
|
|
static const char *TAG = "example";
|
|
|
|
// Handle of the wear levelling library instance
|
|
static wl_handle_t s_wl_handle = WL_INVALID_HANDLE;
|
|
|
|
// Mount path for the partition
|
|
const char *base_path = "/spiflash";
|
|
|
|
static esp_err_t s_example_write_file(char *path, char *data)
|
|
{
|
|
ESP_LOGI(TAG, "Opening file");
|
|
FILE *f = fopen(path, "wb");
|
|
if (f == NULL) {
|
|
ESP_LOGE(TAG, "Failed to open file for writing");
|
|
return ESP_FAIL;
|
|
}
|
|
fprintf(f, data);
|
|
fclose(f);
|
|
ESP_LOGI(TAG, "File written");
|
|
|
|
return ESP_OK;
|
|
}
|
|
|
|
static esp_err_t s_example_read_file(char *path)
|
|
{
|
|
ESP_LOGI(TAG, "Reading file");
|
|
FILE *f = fopen(path, "rb");
|
|
if (f == NULL) {
|
|
ESP_LOGE(TAG, "Failed to open file for reading");
|
|
return ESP_FAIL;
|
|
}
|
|
char line[EXAMPLE_MAX_CHAR_SIZE];
|
|
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);
|
|
|
|
return ESP_OK;
|
|
}
|
|
|
|
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 = true,
|
|
.allocation_unit_size = CONFIG_WL_SECTOR_SIZE
|
|
};
|
|
esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(base_path, "storage", &mount_config, &s_wl_handle);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
|
|
return;
|
|
}
|
|
|
|
//Create file and write
|
|
char data[EXAMPLE_MAX_CHAR_SIZE];
|
|
snprintf(data, EXAMPLE_MAX_CHAR_SIZE, "%s %s\n", "hello world, from ESP-IDF", esp_get_idf_version());
|
|
err = s_example_write_file("/spiflash/hello.txt", data);
|
|
if (err != ESP_OK) {
|
|
return;
|
|
}
|
|
|
|
//Open file for reading
|
|
err = s_example_read_file("/spiflash/hello.txt");
|
|
if (err != ESP_OK) {
|
|
return;
|
|
}
|
|
|
|
// Format FATFS
|
|
err = esp_vfs_fat_spiflash_format_rw_wl(base_path, "storage");
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to format FATFS (%s)", esp_err_to_name(err));
|
|
return;
|
|
}
|
|
|
|
struct stat st;
|
|
if (stat("/spiflash/hello.txt", &st) == 0) {
|
|
ESP_LOGI(TAG, "file still exists");
|
|
return;
|
|
} else {
|
|
ESP_LOGI(TAG, "file doesnt exist, format done");
|
|
}
|
|
|
|
//Create file and write
|
|
memset(data, 0, EXAMPLE_MAX_CHAR_SIZE);
|
|
snprintf(data, EXAMPLE_MAX_CHAR_SIZE, "%s %s\n", "nihao shijie, from ESP-IDF", esp_get_idf_version());
|
|
err = s_example_write_file("/spiflash/nihao.txt", data);
|
|
if (err != ESP_OK) {
|
|
return;
|
|
}
|
|
|
|
//Open file for reading
|
|
err = s_example_read_file("/spiflash/nihao.txt");
|
|
if (err != ESP_OK) {
|
|
return;
|
|
}
|
|
|
|
// Unmount FATFS
|
|
ESP_LOGI(TAG, "Unmounting FAT filesystem");
|
|
ESP_ERROR_CHECK( esp_vfs_fat_spiflash_unmount_rw_wl(base_path, s_wl_handle));
|
|
|
|
ESP_LOGI(TAG, "Done");
|
|
}
|