2016-06-27 15:06:06 +00:00
|
|
|
#include "espressif/esp_common.h"
|
|
|
|
#include "esp/uart.h"
|
|
|
|
#include "FreeRTOS.h"
|
|
|
|
#include "task.h"
|
|
|
|
#include "esp8266.h"
|
|
|
|
|
2016-07-15 12:22:03 +00:00
|
|
|
#include "fcntl.h"
|
|
|
|
#include "unistd.h"
|
|
|
|
|
2016-06-27 15:06:06 +00:00
|
|
|
#include "spiffs.h"
|
|
|
|
#include "esp_spiffs.h"
|
|
|
|
|
2016-08-08 15:08:42 +00:00
|
|
|
/**
|
|
|
|
* This example shows the default SPIFFS configuration when SPIFFS is
|
|
|
|
* configured in compile-time (SPIFFS_SINGLETON = 1).
|
|
|
|
*
|
|
|
|
* To configure SPIFFS in run-time uncomment SPIFFS_SINGLETON in the Makefile
|
|
|
|
* and replace the commented esp_spiffs_init in the code below.
|
|
|
|
*
|
|
|
|
*/
|
2016-06-27 15:06:06 +00:00
|
|
|
|
2016-07-15 12:22:03 +00:00
|
|
|
static void example_read_file_posix()
|
|
|
|
{
|
|
|
|
const int buf_size = 0xFF;
|
|
|
|
uint8_t buf[buf_size];
|
2016-06-27 15:06:06 +00:00
|
|
|
|
2016-07-15 12:22:03 +00:00
|
|
|
int fd = open("test.txt", O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
printf("Error opening file\n");
|
|
|
|
return;
|
|
|
|
}
|
2016-06-27 15:06:06 +00:00
|
|
|
|
2016-07-15 12:22:03 +00:00
|
|
|
int read_bytes = read(fd, buf, buf_size);
|
|
|
|
printf("Read %d bytes\n", read_bytes);
|
2016-06-27 15:06:06 +00:00
|
|
|
|
2016-07-15 12:22:03 +00:00
|
|
|
buf[read_bytes] = '\0'; // zero terminate string
|
|
|
|
printf("Data: %s\n", buf);
|
2016-06-27 15:06:06 +00:00
|
|
|
|
2016-07-15 12:22:03 +00:00
|
|
|
close(fd);
|
2016-06-27 15:06:06 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 12:22:03 +00:00
|
|
|
static void example_read_file_spiffs()
|
2016-06-27 15:06:06 +00:00
|
|
|
{
|
2016-07-15 12:22:03 +00:00
|
|
|
const int buf_size = 0xFF;
|
|
|
|
uint8_t buf[buf_size];
|
2016-06-27 15:06:06 +00:00
|
|
|
|
2016-07-15 12:22:03 +00:00
|
|
|
spiffs_file fd = SPIFFS_open(&fs, "other.txt", SPIFFS_RDONLY, 0);
|
|
|
|
if (fd < 0) {
|
|
|
|
printf("Error opening file\n");
|
|
|
|
return;
|
2016-06-27 15:06:06 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 12:22:03 +00:00
|
|
|
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);
|
2016-06-27 15:06:06 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 12:22:03 +00:00
|
|
|
static void example_write_file()
|
2016-06-27 15:06:06 +00:00
|
|
|
{
|
2016-07-15 12:22:03 +00:00
|
|
|
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;
|
2016-06-27 15:06:06 +00:00
|
|
|
}
|
2016-07-15 12:22:03 +00:00
|
|
|
|
|
|
|
int written = write(fd, buf, sizeof(buf));
|
|
|
|
printf("Written %d bytes\n", written);
|
|
|
|
|
|
|
|
close(fd);
|
2016-06-27 15:06:06 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 12:22:03 +00:00
|
|
|
static void example_fs_info()
|
2016-06-27 15:06:06 +00:00
|
|
|
{
|
|
|
|
uint32_t total, used;
|
|
|
|
SPIFFS_info(&fs, &total, &used);
|
2016-07-15 12:22:03 +00:00
|
|
|
printf("Total: %d bytes, used: %d bytes", total, used);
|
2016-06-27 15:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_task(void *pvParameters)
|
|
|
|
{
|
2016-08-08 15:08:42 +00:00
|
|
|
#if SPIFFS_SINGLETON == 1
|
2016-07-15 12:22:03 +00:00
|
|
|
esp_spiffs_init();
|
2016-08-08 15:08:42 +00:00
|
|
|
#else
|
|
|
|
// for run-time configuration when SPIFFS_SINGLETON = 0
|
|
|
|
esp_spiffs_init(0x200000, 0x10000);
|
|
|
|
#endif
|
|
|
|
|
2016-07-15 12:22:03 +00:00
|
|
|
if (esp_spiffs_mount() != SPIFFS_OK) {
|
|
|
|
printf("Error mount SPIFFS\n");
|
2016-06-27 15:06:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
2016-07-15 12:22:03 +00:00
|
|
|
vTaskDelay(2000 / portTICK_RATE_MS);
|
2016-06-27 15:06:06 +00:00
|
|
|
|
2016-07-15 12:22:03 +00:00
|
|
|
example_write_file();
|
2016-06-27 15:06:06 +00:00
|
|
|
|
2016-07-15 12:22:03 +00:00
|
|
|
example_read_file_posix();
|
2016-06-27 15:06:06 +00:00
|
|
|
|
2016-07-15 12:22:03 +00:00
|
|
|
example_read_file_spiffs();
|
2016-06-27 15:06:06 +00:00
|
|
|
|
2016-07-15 12:22:03 +00:00
|
|
|
example_fs_info();
|
2016-06-27 15:06:06 +00:00
|
|
|
|
2016-07-15 12:22:03 +00:00
|
|
|
printf("\n\n");
|
2016-06-27 15:06:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void user_init(void)
|
|
|
|
{
|
|
|
|
uart_set_baud(0, 115200);
|
|
|
|
|
2016-10-21 09:40:36 +00:00
|
|
|
xTaskCreate(test_task, "test_task", 1024, NULL, 2, NULL);
|
2016-06-27 15:06:06 +00:00
|
|
|
}
|