static void copy_file_task( void * pvParameters ) { FIL File; spiffs_DIR sf_dir; struct spiffs_dirent e; struct spiffs_dirent *pe = &e; s32_t err; void *temp = NULL; uint32_t remain; char *p_path_file = pvParameters; mn_screen_event_t fileLoad; choosedLinePosition = 0; choosedLine = 0; f_open(&File,p_path_file,FA_READ); spiffs_file *fd = &uspiffs[0].f; spiffs *fs = &uspiffs[0].gSPIFFS; SPIFFS_opendir(fs, "/", &sf_dir); do{ pe = SPIFFS_readdir(&sf_dir, pe); }while(strcmp((const char *)pe->name,"config.met") == 0); *fd = SPIFFS_open_by_dirent(fs, pe, SPIFFS_RDWR, 0); if(*fd >= SPIFFS_OK) { err = SPIFFS_fremove(fs, *fd); } *fd = SPIFFS_open(fs, p_path_file, SPIFFS_CREAT | SPIFFS_RDWR | SPIFFS_DIRECT, 0); temp = pvPortMalloc( FS_PAGE_SIZE ); while(!f_eof(&File)) { f_read(&File,temp,FS_PAGE_SIZE,(UINT *)&remain); err = SPIFFS_write(fs, *fd, (u8_t *)temp, remain); } vPortFree(temp); vPortFree(p_path_file); f_close(&File); SPIFFS_close(fs, *fd); fileLoad.event = FILE_LOAD_EVENT; xQueueSend( menu.qEvent, &fileLoad, 0 ); vTaskDelete( xHdlTaskFilecopy ); while(1) { } }
static void spiffs_mount_internal(spiffs_config *cfg) { if (cfg->phys_addr == 0) { SYSTEM_ERROR("Can't start file system, wrong address"); return; } debugf("fs.start: size:%d Kb, offset:0x%X\n", cfg->phys_size / 1024, cfg->phys_addr - INTERNAL_FLASH_START_ADDRESS); 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(cfg); writeFirst = true; } int res = SPIFFS_mount(&_filesystemStorageHandle, cfg, spiffs_work_buf, spiffs_fds, sizeof(spiffs_fds), spiffs_cache, 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); } //dat=0; //flashmem_read(&dat, cfg.phys_addr, 4); //debugf("%X", dat); }
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 false; } debugf("fs.start:%x, size:%d Kb\n", cfg.phys_addr, cfg.phys_size / 1024); 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); 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, &cfg, spiffs_work_buf, spiffs_fds, sizeof(spiffs_fds), spiffs_cache, sizeof(spiffs_cache), NULL); debugf("mount res: %d\n", res); 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); } return true; }
void fileDelete(file_t file) { SPIFFS_fremove(&_filesystemStorageHandle, file); }