SPIFFS: Update example, README.md
Separate method to initialize SPIFFS memory buffers. REDME.md for spiffs component. Simplify spiffs example.
This commit is contained in:
parent
66610c56cb
commit
924860a78f
6 changed files with 277 additions and 171 deletions
|
@ -18,8 +18,9 @@ static fs_time_t get_current_time()
|
|||
|
||||
void test_task(void *pvParameters)
|
||||
{
|
||||
esp_spiffs_init();
|
||||
esp_spiffs_mount();
|
||||
esp_spiffs_unmount(); // FS must be unmounted before formating
|
||||
SPIFFS_unmount(&fs); // FS must be unmounted before formating
|
||||
if (SPIFFS_format(&fs) == SPIFFS_OK) {
|
||||
printf("Format complete\n");
|
||||
} else {
|
||||
|
|
|
@ -4,7 +4,7 @@ FLASH_SIZE = 32
|
|||
|
||||
# spiffs configuration
|
||||
SPIFFS_BASE_ADDR = 0x200000
|
||||
SPIFFS_SIZE = 0x100000
|
||||
SPIFFS_SIZE = 0x010000
|
||||
|
||||
include ../../common.mk
|
||||
|
||||
|
|
|
@ -4,171 +4,95 @@
|
|||
#include "task.h"
|
||||
#include "esp8266.h"
|
||||
|
||||
#include "fcntl.h"
|
||||
#include "unistd.h"
|
||||
|
||||
#include "spiffs.h"
|
||||
#include "esp_spiffs.h"
|
||||
|
||||
|
||||
#define TEST_FILE_NAME_LEN 16
|
||||
#define TEST_FILES 32
|
||||
#define TEST_FILE_MAX_SIZE 8192
|
||||
|
||||
typedef struct {
|
||||
char name[TEST_FILE_NAME_LEN];
|
||||
uint16_t size;
|
||||
uint8_t first_data_byte;
|
||||
} TestFile;
|
||||
|
||||
static TestFile test_files[TEST_FILES];
|
||||
|
||||
inline static void fill_test_data(uint8_t *src, uint16_t size, uint8_t first_byte)
|
||||
static void example_read_file_posix()
|
||||
{
|
||||
while (size--) {
|
||||
*src++ = first_byte++;
|
||||
}
|
||||
}
|
||||
const int buf_size = 0xFF;
|
||||
uint8_t buf[buf_size];
|
||||
|
||||
static bool write_test_files()
|
||||
{
|
||||
uint8_t *buf = (uint8_t*)malloc(TEST_FILE_MAX_SIZE);
|
||||
bool result = true;
|
||||
|
||||
for (uint8_t i = 0; i < TEST_FILES; i++) {
|
||||
sprintf(test_files[i].name, "file_%d.dat", i);
|
||||
spiffs_file f = SPIFFS_open(&fs, test_files[i].name,
|
||||
SPIFFS_CREAT|SPIFFS_RDWR|SPIFFS_TRUNC, 0);
|
||||
if (f < 0) {
|
||||
printf("Open file operation failed\n");
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
test_files[i].size = rand() % TEST_FILE_MAX_SIZE;
|
||||
test_files[i].first_data_byte = rand() % 256;
|
||||
fill_test_data(buf, test_files[i].size, test_files[i].first_data_byte);
|
||||
|
||||
printf("Writing file %s size=%d\n", test_files[i].name,
|
||||
test_files[i].size);
|
||||
int32_t written = SPIFFS_write(&fs, f, buf, test_files[i].size);
|
||||
if (written != test_files[i].size) {
|
||||
printf("Write file operation failed, written=%d\n", written);
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
SPIFFS_close(&fs, f);
|
||||
}
|
||||
free(buf);
|
||||
return result;
|
||||
}
|
||||
|
||||
inline static bool verify_test_data(uint8_t *data, uint16_t size,
|
||||
uint8_t first_byte)
|
||||
{
|
||||
while (size--) {
|
||||
if (*data++ != first_byte++) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool verify_test_files()
|
||||
{
|
||||
uint8_t *buf = (uint8_t*)malloc(TEST_FILE_MAX_SIZE);
|
||||
bool result = true;
|
||||
|
||||
for (uint8_t i = 0; i < TEST_FILES; i++) {
|
||||
printf("Verifying file %s\n", test_files[i].name);
|
||||
spiffs_file f = SPIFFS_open(&fs, test_files[i].name, SPIFFS_RDONLY, 0);
|
||||
if (f < 0) {
|
||||
printf("Open file operation failed\n");
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
|
||||
int32_t n = SPIFFS_read(&fs, f, buf, test_files[i].size);
|
||||
if (n != test_files[i].size) {
|
||||
printf("Read file operation failed\n");
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!verify_test_data(buf, test_files[i].size,
|
||||
test_files[i].first_data_byte)) {
|
||||
printf("Data verification failed\n");
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
|
||||
SPIFFS_close(&fs, f);
|
||||
int fd = open("test.txt", O_RDONLY);
|
||||
if (fd < 0) {
|
||||
printf("Error opening file\n");
|
||||
return;
|
||||
}
|
||||
|
||||
free(buf);
|
||||
return result;
|
||||
int read_bytes = read(fd, buf, buf_size);
|
||||
printf("Read %d bytes\n", read_bytes);
|
||||
|
||||
buf[read_bytes] = '\0'; // zero terminate string
|
||||
printf("Data: %s\n", buf);
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
static bool cleanup_test_files()
|
||||
static void example_read_file_spiffs()
|
||||
{
|
||||
bool result = true;
|
||||
const int buf_size = 0xFF;
|
||||
uint8_t buf[buf_size];
|
||||
|
||||
for (uint8_t i = 0; i < TEST_FILES; i++) {
|
||||
printf("Removing file %s\n", test_files[i].name);
|
||||
if (SPIFFS_remove(&fs, test_files[i].name) != SPIFFS_OK) {
|
||||
printf("Remove file operation failed\n");
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
spiffs_file fd = SPIFFS_open(&fs, "other.txt", SPIFFS_RDONLY, 0);
|
||||
if (fd < 0) {
|
||||
printf("Error opening file\n");
|
||||
return;
|
||||
}
|
||||
return result;
|
||||
|
||||
int read_bytes = SPIFFS_read(&fs, fd, buf, buf_size);
|
||||
printf("Read %d bytes\n", read_bytes);
|
||||
|
||||
buf[read_bytes] = '\0'; // zero terminate string
|
||||
printf("Data: %s\n", buf);
|
||||
|
||||
SPIFFS_close(&fs, fd);
|
||||
}
|
||||
|
||||
inline static void print_info()
|
||||
static void example_write_file()
|
||||
{
|
||||
uint8_t buf[] = "Example data, written by ESP8266";
|
||||
|
||||
int fd = open("other.txt", O_WRONLY|O_CREAT, 0);
|
||||
if (fd < 0) {
|
||||
printf("Error opening file\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int written = write(fd, buf, sizeof(buf));
|
||||
printf("Written %d bytes\n", written);
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
static void example_fs_info()
|
||||
{
|
||||
uint32_t total, used;
|
||||
|
||||
SPIFFS_info(&fs, &total, &used);
|
||||
|
||||
printf("FS total=%d bytes, used=%d bytes\n", total, used);
|
||||
printf("FS %d %% used\n", 100 * used/total);
|
||||
|
||||
// File system structure visualisation
|
||||
// SPIFFS_vis(&fs);
|
||||
printf("Total: %d bytes, used: %d bytes", total, used);
|
||||
}
|
||||
|
||||
void test_task(void *pvParameters)
|
||||
{
|
||||
bool result = true;
|
||||
|
||||
esp_spiffs_mount();
|
||||
esp_spiffs_unmount(); // FS must be unmounted before formating
|
||||
if (SPIFFS_format(&fs) == SPIFFS_OK) {
|
||||
printf("Format complete\n");
|
||||
} else {
|
||||
printf("Format failed\n");
|
||||
esp_spiffs_init();
|
||||
if (esp_spiffs_mount() != SPIFFS_OK) {
|
||||
printf("Error mount SPIFFS\n");
|
||||
}
|
||||
esp_spiffs_mount();
|
||||
|
||||
while (1) {
|
||||
vTaskDelay(5000 / portTICK_RATE_MS);
|
||||
vTaskDelay(2000 / portTICK_RATE_MS);
|
||||
|
||||
result = write_test_files();
|
||||
example_write_file();
|
||||
|
||||
if (result) {
|
||||
result = verify_test_files();
|
||||
}
|
||||
example_read_file_posix();
|
||||
|
||||
print_info();
|
||||
example_read_file_spiffs();
|
||||
|
||||
if (result) {
|
||||
result = cleanup_test_files();
|
||||
}
|
||||
example_fs_info();
|
||||
|
||||
if (result) {
|
||||
printf("Test passed!\n");
|
||||
} else {
|
||||
printf("Test failed!\n");
|
||||
while (1) {
|
||||
vTaskDelay(1);
|
||||
}
|
||||
}
|
||||
printf("\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue