Hey all,
I'm trying to set up some read/ write functions for the SPIFFS vfs example that yall have been working on. Here
https://github.com/jwlusby/esp32_testing.
I've tried rolling back the arduino repo to the previous commit referenced in github, but when I try to compile, it throws some compilation errors about some SPI related library.
I've done some hacking around on my own and arrived here, with these functions being called from the main loop.
Code: Select all
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "Arduino.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "esp_spi_flash.h"
#include "esp_partition.h"
#include "spiffs.h"
#include "esp_spiffs.h"
#include "spiffs_vfs.h"
//0x10000 + (1024*512) + (1024*512) + (1024*512) to hex
#define NVS_PARTITION_START 0x9000
#define NVS_PARTITION_SIZE 0x6000
#define NVS_PHY_START 0xF000
#define NVS_PHY_SIZE 0x1000
#define FACTORY_PARTITION_START 0x10000
#define FACTORY_PARTITION_SIZE 1024*512
#define OTA1_PARTITION_START FACTORY_PARTITION_START + FACTORY_PARTITION_SIZE
#define OTA1_PARTITION_SIZE FACTORY_PARTITION_SIZE
#define OTA2_PARTITION_START OTA1_PARTITION_START + OTA1_PARTITION_SIZE
#define OTA2_PARTITION_SIZE FACTORY_PARTITION_SIZE
#define DATA_PARTITION_START OTA2_PARTITION_START + OTA2_PARTITION_SIZE
#define DATA_PARTITION_SIZE 1024*256
#define SECTOR_SIZE 4096
#define PAGE_SIZE 256
#define MEMORY_SIZE DATA_PARTITION_SIZE
#define MAX_CONCURRENT_FDS 4
static uint8_t spiffs_work_buf[PAGE_SIZE * 2];
static uint8_t spiffs_fds[32 * MAX_CONCURRENT_FDS];
static uint8_t spiffs_cache_buf[(PAGE_SIZE + 32) * 4];
static spiffs fs;
static spiffs_config cfg;
static uint32_t g_wbuf[SECTOR_SIZE / 4]; // is this necessary?
static uint32_t g_rbuf[SECTOR_SIZE / 4];
bool storage_initialized = false;
int read_pos;
int write_pos;
const char* filename = "/data/customer1";
void configure_spiffs(){
// configure the filesystem
cfg.phys_size = MEMORY_SIZE;
cfg.phys_addr = DATA_PARTITION_START;
cfg.phys_erase_block = SECTOR_SIZE;
cfg.log_block_size = SECTOR_SIZE;
cfg.log_page_size = PAGE_SIZE;
cfg.hal_read_f = esp32_spi_flash_read;
cfg.hal_write_f = esp32_spi_flash_write;
cfg.hal_erase_f = esp32_spi_flash_erase;
}
//Test file read/write using VFS component & spiffs (fopen/fread/fwrite)
void mount_format_spiffs()
{
char tag[] = "test_memory_vfs()";
int ret;
configure_spiffs();
// mount the filesystem
ESP_LOGI(tag, "Mounting SPIFFS");
ret = SPIFFS_mount(&fs, &cfg, spiffs_work_buf, spiffs_fds, sizeof(spiffs_fds), spiffs_cache_buf, sizeof(spiffs_cache_buf), NULL);
if (ret != SPIFFS_OK )
{
ESP_LOGI(tag, "Mounting SPIFFS failed with result=%i", ret);
ESP_LOGI(tag, "Unmounting SPIFFS...");
SPIFFS_unmount(&fs);
ESP_LOGI(tag, "Formatting SPIFFS...");
ret = SPIFFS_format(&fs);
ESP_LOGI(tag, " result=%i", ret);
ESP_LOGI(tag, "Remounting SPIFFS...");
ret = SPIFFS_mount(&fs, &cfg, spiffs_work_buf, spiffs_fds, sizeof(spiffs_fds), spiffs_cache_buf, sizeof(spiffs_cache_buf), NULL);
ESP_LOGI(tag, " result=%i", ret);
}
else if (ret == SPIFFS_OK)
{
ESP_LOGI(tag, "SPIFFS mounted");
}
spiffs_registerVFS("/data", &fs);
ESP_LOGI(tag, "vfs registered");
storage_initialized = true;
}
void test_file_read( const char* filename )
{
char tag[] = "read_spiffs";
int nbytes;
FILE* file = fopen( filename, "r" );
if(file <= NULL)
{
ESP_LOGE(tag, "Failed to open file!");
return;
}
else
{
// create pointer to empty buf size 64, is 64 arbitrary
char *buf;
buf = (char*)malloc(64);
memset(buf, 0, 64);
// ESP_LOGI(tag, "Attempting to read 6 bytes from file 0x%X into buffer at 0x%X.", (uint32_t)file, (uint32_t)buf);
nbytes = fread(buf,1,6,file);
ESP_LOGI(tag, "Read %i bytes from file 0x%X into buffer at 0x%X: '%s', ", nbytes, (uint32_t)file, (uint32_t)buf, buf);
// cleanup
fclose(file);
free(buf);
}
}
void test_file_write( const char* filename, void* data )
{
char tag[] = "test_file_write";
int ret;
FILE* file = fopen("data/customer1", "r");
// move file head to end of the file
// ret = fseek(file, write_pos, SEEK_END);
// ESP_LOGI(tag, "fseek=%i", ret);
if (file <= NULL)
{
ESP_LOGE(tag, "failed to open file '%s'. result=0x%X",filename, (uint32_t)file);
return;
}
else
{
ESP_LOGI(tag, "Opened file '%s'. file=0x%X", filename, (uint32_t)file);
int nbytes = fwrite( data, sizeof( struct reading ), 1, file);
write_pos = write_pos + nbytes;
if(nbytes==-1)
{
ESP_LOGE(tag, "Failed to write to file. result=%i", nbytes);
}
else
{
ESP_LOGI(tag, "Wrote %i bytes to file from buffer at 0x%X", nbytes, (uint32_t)data);
ret = fclose(file);
ESP_LOGI(tag, "fclose=%i", ret);
}
}
}
When I try to open the file, im getting "failed to open file." I'll keep truckin for a couple more days on this but any advice would be much appreciated!