mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-23 08:45:22 +03:00
Rework SPIFFS API to be more Arduino like
SD Style commands and Stream API
This commit is contained in:
@ -8,21 +8,17 @@ static u8_t spiffs_work_buf[LOG_PAGE_SIZE*2];
|
||||
static u8_t spiffs_fds[32*4];
|
||||
static u8_t spiffs_cache[(LOG_PAGE_SIZE+32)*4];
|
||||
|
||||
static s32_t api_spiffs_read(u32_t addr, u32_t size, u8_t *dst)
|
||||
{
|
||||
static s32_t api_spiffs_read(u32_t addr, u32_t size, u8_t *dst){
|
||||
flashmem_read(dst, addr, size);
|
||||
return SPIFFS_OK;
|
||||
}
|
||||
|
||||
static s32_t api_spiffs_write(u32_t addr, u32_t size, u8_t *src)
|
||||
{
|
||||
//debugf("api_spiffs_write");
|
||||
static s32_t api_spiffs_write(u32_t addr, u32_t size, u8_t *src){
|
||||
flashmem_write(src, addr, size);
|
||||
return SPIFFS_OK;
|
||||
}
|
||||
|
||||
static s32_t api_spiffs_erase(u32_t addr, u32_t size)
|
||||
{
|
||||
static s32_t api_spiffs_erase(u32_t addr, u32_t size){
|
||||
debugf("api_spiffs_erase");
|
||||
u32_t sect_first = flashmem_get_sector_of_address(addr);
|
||||
u32_t sect_last = sect_first;
|
||||
@ -45,48 +41,40 @@ extern uint32_t _SPIFFS_end;
|
||||
spiffs_config spiffs_get_storage_config()
|
||||
{
|
||||
spiffs_config cfg = {0};
|
||||
if ((u32_t)&_SPIFFS_start == 0) return cfg;
|
||||
cfg.phys_addr = (u32_t)&_SPIFFS_start;
|
||||
if (cfg.phys_addr == 0)
|
||||
return cfg;
|
||||
cfg.phys_addr += 0x3000;
|
||||
cfg.phys_addr &= 0xFFFFC000; // align to 4 sector.
|
||||
cfg.phys_size = (u32_t)((u32_t)&_SPIFFS_end - (u32_t)&_SPIFFS_start);
|
||||
/*cfg.phys_addr = INTERNAL_FLASH_SIZE - SPIFFS_WORK_SIZE + INTERNAL_FLASH_START_ADDRESS;
|
||||
cfg.phys_addr += 0x3000;
|
||||
cfg.phys_addr &= 0xFFFFC000; // align to 4 sector.
|
||||
cfg.phys_size = SPIFFS_WORK_SIZE;*/
|
||||
cfg.phys_erase_block = INTERNAL_FLASH_SECTOR_SIZE; // according to datasheet
|
||||
cfg.log_block_size = INTERNAL_FLASH_SECTOR_SIZE * 2; // Important to make large
|
||||
cfg.log_page_size = LOG_PAGE_SIZE; // as we said
|
||||
return cfg;
|
||||
}
|
||||
|
||||
bool spiffs_format_internal()
|
||||
{
|
||||
bool spiffs_format_internal(){
|
||||
spiffs_config cfg = spiffs_get_storage_config();
|
||||
if (cfg.phys_addr == 0)
|
||||
{
|
||||
SYSTEM_ERROR("Can't format file system, wrong address");
|
||||
return false;
|
||||
if (cfg.phys_addr == 0){
|
||||
SYSTEM_ERROR("Can't format file system, wrong address");
|
||||
return false;
|
||||
}
|
||||
|
||||
u32_t sect_first, sect_last;
|
||||
sect_first = cfg.phys_addr;
|
||||
sect_first = flashmem_get_sector_of_address((u32_t)&_SPIFFS_start);
|
||||
sect_first = flashmem_get_first_free_block_address();
|
||||
sect_last = flashmem_get_sector_of_address((u32_t)&_SPIFFS_end);
|
||||
debugf("sect_first: %x, sect_last: %x\n", sect_first, sect_last);
|
||||
while( sect_first <= sect_last )
|
||||
if(!flashmem_erase_sector( sect_first ++ ))
|
||||
return false;
|
||||
while( sect_first <= sect_last ){
|
||||
if(!flashmem_erase_sector( sect_first ++ ))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void spiffs_mount()
|
||||
{
|
||||
bool spiffs_mount(){
|
||||
spiffs_config cfg = spiffs_get_storage_config();
|
||||
if (cfg.phys_addr == 0)
|
||||
{
|
||||
SYSTEM_ERROR("Can't start file system, wrong address");
|
||||
return;
|
||||
if (cfg.phys_addr == 0){
|
||||
SYSTEM_ERROR("Can't start file system, wrong address");
|
||||
return false;
|
||||
}
|
||||
|
||||
debugf("fs.start:%x, size:%d Kb\n", cfg.phys_addr, cfg.phys_size / 1024);
|
||||
@ -94,17 +82,18 @@ void spiffs_mount()
|
||||
cfg.hal_read_f = api_spiffs_read;
|
||||
cfg.hal_write_f = api_spiffs_write;
|
||||
cfg.hal_erase_f = api_spiffs_erase;
|
||||
|
||||
|
||||
uint32_t dat;
|
||||
bool writeFirst = false;
|
||||
flashmem_read(&dat, cfg.phys_addr, 4);
|
||||
//debugf("%X", dat);
|
||||
|
||||
if (dat == UINT32_MAX)
|
||||
{
|
||||
debugf("First init file system");
|
||||
spiffs_format_internal();
|
||||
writeFirst = true;
|
||||
if (dat == UINT32_MAX){
|
||||
debugf("First init file system");
|
||||
if(!spiffs_format_internal()){
|
||||
SYSTEM_ERROR("Can't format file system");
|
||||
return false;
|
||||
}
|
||||
writeFirst = true;
|
||||
}
|
||||
|
||||
int res = SPIFFS_mount(&_filesystemStorageHandle,
|
||||
@ -116,66 +105,44 @@ void spiffs_mount()
|
||||
sizeof(spiffs_cache),
|
||||
NULL);
|
||||
debugf("mount res: %d\n", res);
|
||||
|
||||
if (writeFirst)
|
||||
{
|
||||
file_t fd = SPIFFS_open(&_filesystemStorageHandle, "initialize_fs_header.dat", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0);
|
||||
SPIFFS_write(&_filesystemStorageHandle, fd, (u8_t *)"1", 1);
|
||||
SPIFFS_fremove(&_filesystemStorageHandle, fd);
|
||||
SPIFFS_close(&_filesystemStorageHandle, fd);
|
||||
|
||||
if(res != 0) return false;
|
||||
|
||||
if (writeFirst){
|
||||
file_t fd = SPIFFS_open(&_filesystemStorageHandle, "initialize_fs_header.dat", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0);
|
||||
SPIFFS_write(&_filesystemStorageHandle, fd, (u8_t *)"1", 1);
|
||||
SPIFFS_fremove(&_filesystemStorageHandle, fd);
|
||||
SPIFFS_close(&_filesystemStorageHandle, fd);
|
||||
}
|
||||
|
||||
//dat=0;
|
||||
//flashmem_read(&dat, cfg.phys_addr, 4);
|
||||
//debugf("%X", dat);
|
||||
return true;
|
||||
}
|
||||
|
||||
void spiffs_unmount()
|
||||
{
|
||||
void spiffs_unmount(){
|
||||
SPIFFS_unmount(&_filesystemStorageHandle);
|
||||
}
|
||||
|
||||
// FS formatting function
|
||||
bool spiffs_format()
|
||||
{
|
||||
bool spiffs_format(){
|
||||
spiffs_unmount();
|
||||
spiffs_format_internal();
|
||||
if(!spiffs_format_internal()) return false;
|
||||
spiffs_mount();
|
||||
return true;
|
||||
}
|
||||
|
||||
//int spiffs_check( void )
|
||||
//{
|
||||
// ets_wdt_disable();
|
||||
// int res = (int)SPIFFS_check(&_filesystemStorageHandle);
|
||||
// ets_wdt_enable();
|
||||
// return res;
|
||||
//}
|
||||
|
||||
void test_spiffs()
|
||||
{
|
||||
void test_spiffs(){
|
||||
char buf[12] = {0};
|
||||
|
||||
// Surely, I've mounted spiffs before entering here
|
||||
|
||||
spiffs_file fd;
|
||||
spiffs_stat st = {0};
|
||||
SPIFFS_stat(&_filesystemStorageHandle, "my_file.txt", &st);
|
||||
if (st.size <= 0)
|
||||
{
|
||||
fd = SPIFFS_open(&_filesystemStorageHandle, "my_file.txt", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0);
|
||||
if (SPIFFS_write(&_filesystemStorageHandle, fd, (u8_t *)"Hello world", 11) < 0)
|
||||
debugf("errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
|
||||
SPIFFS_close(&_filesystemStorageHandle, fd);
|
||||
debugf("file created");
|
||||
}
|
||||
else
|
||||
debugf("file %s exist :)", st.name);
|
||||
|
||||
if (st.size <= 0){
|
||||
fd = SPIFFS_open(&_filesystemStorageHandle, "my_file.txt", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0);
|
||||
if (SPIFFS_write(&_filesystemStorageHandle, fd, (u8_t *)"Hello world", 11) < 0)
|
||||
debugf("errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
|
||||
SPIFFS_close(&_filesystemStorageHandle, fd);
|
||||
debugf("file created");
|
||||
} else debugf("file %s exist :)", st.name);
|
||||
|
||||
fd = SPIFFS_open(&_filesystemStorageHandle, "my_file.txt", SPIFFS_RDWR, 0);
|
||||
if (SPIFFS_read(&_filesystemStorageHandle, fd, (u8_t *)buf, 11) < 0) debugf("errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
|
||||
SPIFFS_close(&_filesystemStorageHandle, fd);
|
||||
|
||||
debugf("--> %s <--\n", buf);
|
||||
}
|
||||
|
@ -83,12 +83,10 @@ typedef enum {
|
||||
} spiffs_check_report;
|
||||
|
||||
/* file system check callback function */
|
||||
typedef void (*spiffs_check_callback)(spiffs_check_type type, spiffs_check_report report,
|
||||
u32_t arg1, u32_t arg2);
|
||||
typedef void (*spiffs_check_callback)(spiffs_check_type type, spiffs_check_report report, u32_t arg1, u32_t arg2);
|
||||
|
||||
#ifndef SPIFFS_DBG
|
||||
#define SPIFFS_DBG(...) \
|
||||
print(__VA_ARGS__)
|
||||
#define SPIFFS_DBG(...) printf(__VA_ARGS__)
|
||||
#endif
|
||||
#ifndef SPIFFS_GC_DBG
|
||||
#define SPIFFS_GC_DBG(...) printf(__VA_ARGS__)
|
||||
@ -456,7 +454,7 @@ u32_t SPIFFS_buffer_bytes_for_cache(spiffs *fs, u32_t num_pages);
|
||||
#endif
|
||||
|
||||
|
||||
void spiffs_mount();
|
||||
bool spiffs_mount();
|
||||
void spiffs_unmount();
|
||||
bool spiffs_format();
|
||||
spiffs_config spiffs_get_storage_config();
|
||||
|
@ -20,7 +20,7 @@ static spiffs_cache_page *spiffs_cache_page_get(spiffs *fs, spiffs_page_ix pix)
|
||||
if ((cache->cpage_use_map & (1<<i)) &&
|
||||
(cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) == 0 &&
|
||||
cp->pix == pix ) {
|
||||
SPIFFS_CACHE_DBG("CACHE_GET: have cache page %u for %04x\n", i, pix);
|
||||
SPIFFS_CACHE_DBG("CACHE_GET: have cache page %d for %04x\n", i, pix);
|
||||
cp->last_access = cache->last_access;
|
||||
return cp;
|
||||
}
|
||||
@ -46,9 +46,9 @@ static s32_t spiffs_cache_page_free(spiffs *fs, int ix, u8_t write_back) {
|
||||
cache->cpage_use_map &= ~(1 << ix);
|
||||
|
||||
if (cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) {
|
||||
SPIFFS_CACHE_DBG("CACHE_FREE: free cache page %u objid %04x\n", ix, cp->obj_id);
|
||||
SPIFFS_CACHE_DBG("CACHE_FREE: free cache page %d objid %04x\n", ix, cp->obj_id);
|
||||
} else {
|
||||
SPIFFS_CACHE_DBG("CACHE_FREE: free cache page %u pix %04x\n", ix, cp->pix);
|
||||
SPIFFS_CACHE_DBG("CACHE_FREE: free cache page %d pix %04x\n", ix, cp->pix);
|
||||
}
|
||||
}
|
||||
|
||||
@ -98,7 +98,7 @@ static spiffs_cache_page *spiffs_cache_page_allocate(spiffs *fs) {
|
||||
spiffs_cache_page *cp = spiffs_get_cache_page_hdr(fs, cache, i);
|
||||
cache->cpage_use_map |= (1<<i);
|
||||
cp->last_access = cache->last_access;
|
||||
SPIFFS_CACHE_DBG("CACHE_ALLO: allocated cache page %u\n", i);
|
||||
SPIFFS_CACHE_DBG("CACHE_ALLO: allocated cache page %d\n", i);
|
||||
return cp;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user