Merge pull request #163 from sheinz/feature/spiffs
File system support. SPIFFS integration.
This commit is contained in:
commit
083aa0451a
22 changed files with 1709 additions and 9 deletions
11
examples/posix_fs/Makefile
Normal file
11
examples/posix_fs/Makefile
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
PROGRAM=posix_fs_example
|
||||
PROGRAM_EXTRA_SRC_FILES=./fs-test/fs_test.c
|
||||
|
||||
EXTRA_COMPONENTS = extras/spiffs
|
||||
FLASH_SIZE = 32
|
||||
|
||||
# spiffs configuration
|
||||
SPIFFS_BASE_ADDR = 0x200000
|
||||
SPIFFS_SIZE = 0x100000
|
||||
|
||||
include ../../common.mk
|
||||
10
examples/posix_fs/README.md
Normal file
10
examples/posix_fs/README.md
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# POSIX file access example
|
||||
|
||||
This example runs several file system tests on ESP8266.
|
||||
It uses fs-test library to perform file operations test. fs-test library uses
|
||||
only POSIX file functions so can be run on host system as well.
|
||||
|
||||
Currently included tests:
|
||||
* File system load test. Perform multiple file operations in random order.
|
||||
* File system speed test. Measures files read/write speed.
|
||||
|
||||
1
examples/posix_fs/fs-test
Submodule
1
examples/posix_fs/fs-test
Submodule
|
|
@ -0,0 +1 @@
|
|||
Subproject commit 2ad547adc5f725594b3c6752f036ff4401b221fc
|
||||
55
examples/posix_fs/posix_fs_example.c
Normal file
55
examples/posix_fs/posix_fs_example.c
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
#include "espressif/esp_common.h"
|
||||
#include "esp/uart.h"
|
||||
#include "esp/timer.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "esp8266.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#include "esp_spiffs.h"
|
||||
#include "spiffs.h"
|
||||
|
||||
#include "fs-test/fs_test.h"
|
||||
|
||||
static fs_time_t get_current_time()
|
||||
{
|
||||
return timer_get_count(FRC2) / 5000; // to get roughly 1ms resolution
|
||||
}
|
||||
|
||||
void test_task(void *pvParameters)
|
||||
{
|
||||
esp_spiffs_init();
|
||||
esp_spiffs_mount();
|
||||
SPIFFS_unmount(&fs); // FS must be unmounted before formating
|
||||
if (SPIFFS_format(&fs) == SPIFFS_OK) {
|
||||
printf("Format complete\n");
|
||||
} else {
|
||||
printf("Format failed\n");
|
||||
}
|
||||
esp_spiffs_mount();
|
||||
|
||||
while (1) {
|
||||
vTaskDelay(5000 / portTICK_RATE_MS);
|
||||
if (fs_load_test_run(100)) {
|
||||
printf("PASS\n");
|
||||
} else {
|
||||
printf("FAIL\n");
|
||||
}
|
||||
|
||||
vTaskDelay(5000 / portTICK_RATE_MS);
|
||||
float write_rate, read_rate;
|
||||
if (fs_speed_test_run(get_current_time, &write_rate, &read_rate)) {
|
||||
printf("Read speed: %.0f bytes/s\n", read_rate * 1000);
|
||||
printf("Write speed: %.0f bytes/s\n", write_rate * 1000);
|
||||
} else {
|
||||
printf("FAIL\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void user_init(void)
|
||||
{
|
||||
uart_set_baud(0, 115200);
|
||||
|
||||
xTaskCreate(test_task, (signed char *)"test_task", 1024, NULL, 2, NULL);
|
||||
}
|
||||
11
examples/spiffs/Makefile
Normal file
11
examples/spiffs/Makefile
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
PROGRAM=spiffs_example
|
||||
EXTRA_COMPONENTS = extras/spiffs
|
||||
FLASH_SIZE = 32
|
||||
|
||||
# spiffs configuration
|
||||
SPIFFS_BASE_ADDR = 0x200000
|
||||
SPIFFS_SIZE = 0x010000
|
||||
|
||||
include ../../common.mk
|
||||
|
||||
$(eval $(call make_spiffs_image,files))
|
||||
1
examples/spiffs/files/test.txt
Normal file
1
examples/spiffs/files/test.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
This file will go to SPIFFS image.
|
||||
104
examples/spiffs/spiffs_example.c
Normal file
104
examples/spiffs/spiffs_example.c
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
#include "espressif/esp_common.h"
|
||||
#include "esp/uart.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "esp8266.h"
|
||||
|
||||
#include "fcntl.h"
|
||||
#include "unistd.h"
|
||||
|
||||
#include "spiffs.h"
|
||||
#include "esp_spiffs.h"
|
||||
|
||||
|
||||
static void example_read_file_posix()
|
||||
{
|
||||
const int buf_size = 0xFF;
|
||||
uint8_t buf[buf_size];
|
||||
|
||||
int fd = open("test.txt", O_RDONLY);
|
||||
if (fd < 0) {
|
||||
printf("Error opening file\n");
|
||||
return;
|
||||
}
|
||||
|
||||
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 void example_read_file_spiffs()
|
||||
{
|
||||
const int buf_size = 0xFF;
|
||||
uint8_t buf[buf_size];
|
||||
|
||||
spiffs_file fd = SPIFFS_open(&fs, "other.txt", SPIFFS_RDONLY, 0);
|
||||
if (fd < 0) {
|
||||
printf("Error opening file\n");
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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("Total: %d bytes, used: %d bytes", total, used);
|
||||
}
|
||||
|
||||
void test_task(void *pvParameters)
|
||||
{
|
||||
esp_spiffs_init();
|
||||
if (esp_spiffs_mount() != SPIFFS_OK) {
|
||||
printf("Error mount SPIFFS\n");
|
||||
}
|
||||
|
||||
while (1) {
|
||||
vTaskDelay(2000 / portTICK_RATE_MS);
|
||||
|
||||
example_write_file();
|
||||
|
||||
example_read_file_posix();
|
||||
|
||||
example_read_file_spiffs();
|
||||
|
||||
example_fs_info();
|
||||
|
||||
printf("\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
void user_init(void)
|
||||
{
|
||||
uart_set_baud(0, 115200);
|
||||
|
||||
xTaskCreate(test_task, (signed char *)"test_task", 1024, NULL, 2, NULL);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue