2014-12-22 12:35:05 +01:00
|
|
|
|
#include "c_stdio.h"
|
|
|
|
|
#include "platform.h"
|
|
|
|
|
#include "spiffs.h"
|
2016-06-05 23:10:58 +02:00
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
|
spiffs fs;
|
|
|
|
|
|
2016-06-05 23:10:58 +02:00
|
|
|
|
#define LOG_PAGE_SIZE 256
|
|
|
|
|
#define LOG_BLOCK_SIZE (INTERNAL_FLASH_SECTOR_SIZE * 2)
|
|
|
|
|
#define LOG_BLOCK_SIZE_SMALL_FS (INTERNAL_FLASH_SECTOR_SIZE)
|
|
|
|
|
#define MIN_BLOCKS_FS 4
|
2014-12-22 12:35:05 +01:00
|
|
|
|
|
|
|
|
|
static u8_t spiffs_work_buf[LOG_PAGE_SIZE*2];
|
|
|
|
|
static u8_t spiffs_fds[32*4];
|
2015-07-31 08:29:29 +02:00
|
|
|
|
#if SPIFFS_CACHE
|
|
|
|
|
static u8_t spiffs_cache[(LOG_PAGE_SIZE+32)*2];
|
|
|
|
|
#endif
|
2014-12-22 12:35:05 +01:00
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
|
static s32_t my_spiffs_read(u32_t addr, u32_t size, u8_t *dst) {
|
2014-12-22 12:35:05 +01:00
|
|
|
|
platform_flash_read(dst, addr, size);
|
|
|
|
|
return SPIFFS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
|
static s32_t my_spiffs_write(u32_t addr, u32_t size, u8_t *src) {
|
2014-12-22 12:35:05 +01:00
|
|
|
|
platform_flash_write(src, addr, size);
|
|
|
|
|
return SPIFFS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
|
static s32_t my_spiffs_erase(u32_t addr, u32_t size) {
|
2014-12-22 12:35:05 +01:00
|
|
|
|
u32_t sect_first = platform_flash_get_sector_of_address(addr);
|
|
|
|
|
u32_t sect_last = sect_first;
|
|
|
|
|
while( sect_first <= sect_last )
|
|
|
|
|
if( platform_flash_erase_sector( sect_first ++ ) == PLATFORM_ERR )
|
|
|
|
|
return SPIFFS_ERR_INTERNAL;
|
|
|
|
|
return SPIFFS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void myspiffs_check_callback(spiffs_check_type type, spiffs_check_report report, u32_t arg1, u32_t arg2){
|
|
|
|
|
// if(SPIFFS_CHECK_PROGRESS == report) return;
|
|
|
|
|
// NODE_ERR("type: %d, report: %d, arg1: %d, arg2: %d\n", type, report, arg1, arg2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*******************
|
|
|
|
|
The W25Q32BV array is organized into 16,384 programmable pages of 256-bytes each. Up to 256 bytes can be programmed at a time.
|
|
|
|
|
Pages can be erased in groups of 16 (4KB sector erase), groups of 128 (32KB block erase), groups of 256 (64KB block erase) or
|
|
|
|
|
the entire chip (chip erase). The W25Q32BV has 1,024 erasable sectors and 64 erasable blocks respectively.
|
|
|
|
|
The small 4KB sectors allow for greater flexibility in applications that require data and parameter storage.
|
|
|
|
|
|
|
|
|
|
********************/
|
|
|
|
|
|
2016-06-05 23:10:58 +02:00
|
|
|
|
static bool myspiffs_set_location(spiffs_config *cfg, int align, int offset, int block_size) {
|
2015-10-09 04:54:51 +02:00
|
|
|
|
#ifdef SPIFFS_FIXED_LOCATION
|
2016-06-05 23:10:58 +02:00
|
|
|
|
cfg->phys_addr = (SPIFFS_FIXED_LOCATION + block_size - 1) & ~(block_size-1);
|
|
|
|
|
#else
|
|
|
|
|
cfg->phys_addr = ( u32_t )platform_flash_get_first_free_block_address( NULL ) + offset;
|
|
|
|
|
cfg->phys_addr = (cfg->phys_addr + align - 1) & ~(align - 1);
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef SPIFFS_SIZE_1M_BOUNDARY
|
|
|
|
|
cfg->phys_size = ((0x100000 - 16384 - ( ( u32_t )cfg->phys_addr )) & ~(block_size - 1)) & 0xfffff;
|
|
|
|
|
#else
|
|
|
|
|
cfg->phys_size = (INTERNAL_FLASH_SIZE - ( ( u32_t )cfg->phys_addr )) & ~(block_size - 1);
|
|
|
|
|
#endif
|
|
|
|
|
if ((int) cfg->phys_size < 0) {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
cfg->log_block_size = block_size;
|
|
|
|
|
|
|
|
|
|
return (cfg->phys_size / block_size) >= MIN_BLOCKS_FS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Returns TRUE if FS was found
|
|
|
|
|
* align must be a power of two
|
|
|
|
|
*/
|
|
|
|
|
static bool myspiffs_set_cfg(spiffs_config *cfg, int align, int offset, bool force_create) {
|
|
|
|
|
cfg->phys_erase_block = INTERNAL_FLASH_SECTOR_SIZE; // according to datasheet
|
|
|
|
|
cfg->log_page_size = LOG_PAGE_SIZE; // as we said
|
|
|
|
|
|
|
|
|
|
cfg->hal_read_f = my_spiffs_read;
|
|
|
|
|
cfg->hal_write_f = my_spiffs_write;
|
|
|
|
|
cfg->hal_erase_f = my_spiffs_erase;
|
|
|
|
|
|
|
|
|
|
if (!myspiffs_set_location(cfg, align, offset, LOG_BLOCK_SIZE)) {
|
|
|
|
|
if (!myspiffs_set_location(cfg, align, offset, LOG_BLOCK_SIZE_SMALL_FS)) {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NODE_DBG("fs.start:%x,max:%x\n",cfg->phys_addr,cfg->phys_size);
|
|
|
|
|
|
|
|
|
|
#ifdef SPIFFS_USE_MAGIC_LENGTH
|
|
|
|
|
if (force_create) {
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int size = SPIFFS_probe_fs(cfg);
|
|
|
|
|
|
|
|
|
|
if (size > 0 && size < cfg->phys_size) {
|
|
|
|
|
NODE_DBG("Overriding size:%x\n",size);
|
|
|
|
|
cfg->phys_size = size;
|
|
|
|
|
}
|
|
|
|
|
if (size > 0) {
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
return FALSE;
|
2015-10-09 04:54:51 +02:00
|
|
|
|
#else
|
2016-06-05 23:10:58 +02:00
|
|
|
|
return TRUE;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool myspiffs_find_cfg(spiffs_config *cfg, bool force_create) {
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (!force_create) {
|
|
|
|
|
#ifdef SPIFFS_FIXED_LOCATION
|
|
|
|
|
if (myspiffs_set_cfg(cfg, 0, 0, FALSE)) {
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
if (INTERNAL_FLASH_SIZE >= 700000) {
|
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
|
if (myspiffs_set_cfg(cfg, 0x10000, 0x10000 * i, FALSE)) {
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
|
if (myspiffs_set_cfg(cfg, LOG_BLOCK_SIZE, LOG_BLOCK_SIZE * i, FALSE)) {
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No existing file system -- set up for a format
|
|
|
|
|
if (INTERNAL_FLASH_SIZE >= 700000) {
|
|
|
|
|
myspiffs_set_cfg(cfg, 0x10000, 0x10000, TRUE);
|
|
|
|
|
#ifndef SPIFFS_MAX_FILESYSTEM_SIZE
|
|
|
|
|
if (cfg->phys_size < 400000) {
|
|
|
|
|
// Don't waste so much in alignment
|
|
|
|
|
myspiffs_set_cfg(cfg, LOG_BLOCK_SIZE, LOG_BLOCK_SIZE * 4, TRUE);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
} else {
|
|
|
|
|
myspiffs_set_cfg(cfg, LOG_BLOCK_SIZE, 0, TRUE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef SPIFFS_MAX_FILESYSTEM_SIZE
|
|
|
|
|
if (cfg->phys_size > SPIFFS_MAX_FILESYSTEM_SIZE) {
|
|
|
|
|
cfg->phys_size = (SPIFFS_MAX_FILESYSTEM_SIZE) & ~(cfg->log_block_size - 1);
|
|
|
|
|
}
|
2015-10-09 04:54:51 +02:00
|
|
|
|
#endif
|
2014-12-22 12:35:05 +01:00
|
|
|
|
|
2016-06-05 23:10:58 +02:00
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool myspiffs_mount_internal(bool force_mount) {
|
|
|
|
|
spiffs_config cfg;
|
|
|
|
|
if (!myspiffs_find_cfg(&cfg, force_mount) && !force_mount) {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs.err_code = 0;
|
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
|
int res = SPIFFS_mount(&fs,
|
|
|
|
|
&cfg,
|
|
|
|
|
spiffs_work_buf,
|
|
|
|
|
spiffs_fds,
|
|
|
|
|
sizeof(spiffs_fds),
|
2015-07-31 08:29:29 +02:00
|
|
|
|
#if SPIFFS_CACHE
|
2014-12-22 12:35:05 +01:00
|
|
|
|
spiffs_cache,
|
|
|
|
|
sizeof(spiffs_cache),
|
2015-07-31 08:29:29 +02:00
|
|
|
|
#else
|
|
|
|
|
0, 0,
|
|
|
|
|
#endif
|
2014-12-22 12:35:05 +01:00
|
|
|
|
// myspiffs_check_callback);
|
|
|
|
|
0);
|
2016-06-05 23:10:58 +02:00
|
|
|
|
NODE_DBG("mount res: %d, %d\n", res, fs.err_code);
|
|
|
|
|
return res == SPIFFS_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool myspiffs_mount() {
|
|
|
|
|
return myspiffs_mount_internal(FALSE);
|
2014-12-22 12:35:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-02 18:51:02 +02:00
|
|
|
|
void myspiffs_unmount() {
|
|
|
|
|
SPIFFS_unmount(&fs);
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
|
// FS formatting function
|
|
|
|
|
// Returns 1 if OK, 0 for error
|
2015-01-05 03:09:51 +01:00
|
|
|
|
int myspiffs_format( void )
|
2014-12-22 12:35:05 +01:00
|
|
|
|
{
|
|
|
|
|
SPIFFS_unmount(&fs);
|
2016-06-05 23:10:58 +02:00
|
|
|
|
myspiffs_mount_internal(TRUE);
|
|
|
|
|
SPIFFS_unmount(&fs);
|
|
|
|
|
|
|
|
|
|
NODE_DBG("Formatting: size 0x%x, addr 0x%x\n", fs.cfg.phys_size, fs.cfg.phys_addr);
|
|
|
|
|
|
|
|
|
|
if (SPIFFS_format(&fs) < 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return myspiffs_mount();
|
2014-12-22 12:35:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
|
int myspiffs_check( void )
|
2014-12-22 12:35:05 +01:00
|
|
|
|
{
|
|
|
|
|
// ets_wdt_disable();
|
|
|
|
|
// int res = (int)SPIFFS_check(&fs);
|
|
|
|
|
// ets_wdt_enable();
|
|
|
|
|
// return res;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
|
int myspiffs_open(const char *name, int flags){
|
2015-02-11 14:20:54 +01:00
|
|
|
|
return (int)SPIFFS_open(&fs, (char *)name, (spiffs_flags)flags, 0);
|
2014-12-22 12:35:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
|
int myspiffs_close( int fd ){
|
2016-06-05 23:10:58 +02:00
|
|
|
|
return SPIFFS_close(&fs, (spiffs_file)fd);
|
2014-12-22 12:35:05 +01:00
|
|
|
|
}
|
2015-01-05 03:09:51 +01:00
|
|
|
|
size_t myspiffs_write( int fd, const void* ptr, size_t len ){
|
2014-12-22 12:35:05 +01:00
|
|
|
|
#if 0
|
|
|
|
|
if(fd==c_stdout || fd==c_stderr){
|
|
|
|
|
uart0_tx_buffer((u8_t*)ptr, len);
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2015-01-07 08:57:17 +01:00
|
|
|
|
int res = SPIFFS_write(&fs, (spiffs_file)fd, (void *)ptr, len);
|
|
|
|
|
if (res < 0) {
|
|
|
|
|
NODE_DBG("write errno %i\n", SPIFFS_errno(&fs));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
2014-12-22 12:35:05 +01:00
|
|
|
|
}
|
2015-01-05 03:09:51 +01:00
|
|
|
|
size_t myspiffs_read( int fd, void* ptr, size_t len){
|
2015-01-07 08:57:17 +01:00
|
|
|
|
int res = SPIFFS_read(&fs, (spiffs_file)fd, ptr, len);
|
|
|
|
|
if (res < 0) {
|
|
|
|
|
NODE_DBG("read errno %i\n", SPIFFS_errno(&fs));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
2014-12-22 12:35:05 +01:00
|
|
|
|
}
|
2015-01-05 03:09:51 +01:00
|
|
|
|
int myspiffs_lseek( int fd, int off, int whence ){
|
2014-12-22 12:35:05 +01:00
|
|
|
|
return SPIFFS_lseek(&fs, (spiffs_file)fd, off, whence);
|
|
|
|
|
}
|
2015-01-05 03:09:51 +01:00
|
|
|
|
int myspiffs_eof( int fd ){
|
2014-12-22 12:35:05 +01:00
|
|
|
|
return SPIFFS_eof(&fs, (spiffs_file)fd);
|
|
|
|
|
}
|
2015-01-05 03:09:51 +01:00
|
|
|
|
int myspiffs_tell( int fd ){
|
2014-12-22 12:35:05 +01:00
|
|
|
|
return SPIFFS_tell(&fs, (spiffs_file)fd);
|
|
|
|
|
}
|
2015-01-05 03:09:51 +01:00
|
|
|
|
int myspiffs_getc( int fd ){
|
2015-01-08 09:03:21 +01:00
|
|
|
|
unsigned char c = 0xFF;
|
2015-01-07 08:57:17 +01:00
|
|
|
|
int res;
|
2014-12-22 12:35:05 +01:00
|
|
|
|
if(!myspiffs_eof(fd)){
|
2015-01-07 08:57:17 +01:00
|
|
|
|
res = SPIFFS_read(&fs, (spiffs_file)fd, &c, 1);
|
|
|
|
|
if (res != 1) {
|
|
|
|
|
NODE_DBG("getc errno %i\n", SPIFFS_errno(&fs));
|
|
|
|
|
return (int)EOF;
|
2015-01-08 09:03:21 +01:00
|
|
|
|
} else {
|
|
|
|
|
return (int)c;
|
2015-01-07 08:57:17 +01:00
|
|
|
|
}
|
2014-12-22 12:35:05 +01:00
|
|
|
|
}
|
2015-01-08 09:03:21 +01:00
|
|
|
|
return (int)EOF;
|
2014-12-22 12:35:05 +01:00
|
|
|
|
}
|
2015-01-05 03:09:51 +01:00
|
|
|
|
int myspiffs_ungetc( int c, int fd ){
|
2014-12-22 12:35:05 +01:00
|
|
|
|
return SPIFFS_lseek(&fs, (spiffs_file)fd, -1, SEEK_CUR);
|
|
|
|
|
}
|
2015-01-05 03:09:51 +01:00
|
|
|
|
int myspiffs_flush( int fd ){
|
2014-12-22 12:35:05 +01:00
|
|
|
|
return SPIFFS_fflush(&fs, (spiffs_file)fd);
|
|
|
|
|
}
|
2015-01-05 03:09:51 +01:00
|
|
|
|
int myspiffs_error( int fd ){
|
2014-12-22 12:35:05 +01:00
|
|
|
|
return SPIFFS_errno(&fs);
|
|
|
|
|
}
|
2015-01-05 03:09:51 +01:00
|
|
|
|
void myspiffs_clearerr( int fd ){
|
2015-03-09 18:12:36 +01:00
|
|
|
|
SPIFFS_clearerr(&fs);
|
2014-12-22 12:35:05 +01:00
|
|
|
|
}
|
2015-02-11 14:20:54 +01:00
|
|
|
|
int myspiffs_rename( const char *old, const char *newname ){
|
|
|
|
|
return SPIFFS_rename(&fs, (char *)old, (char *)newname);
|
|
|
|
|
}
|
2015-02-13 12:23:30 +01:00
|
|
|
|
size_t myspiffs_size( int fd ){
|
2016-06-05 23:10:58 +02:00
|
|
|
|
int32_t curpos = SPIFFS_tell(&fs, (spiffs_file) fd);
|
|
|
|
|
int32_t size = SPIFFS_lseek(&fs, (spiffs_file) fd, SPIFFS_SEEK_END, 0);
|
|
|
|
|
(void) SPIFFS_lseek(&fs, (spiffs_file) fd, SPIFFS_SEEK_SET, curpos);
|
|
|
|
|
return size;
|
2015-02-13 12:23:30 +01:00
|
|
|
|
}
|
2014-12-22 12:35:05 +01:00
|
|
|
|
#if 0
|
2015-01-05 03:09:51 +01:00
|
|
|
|
void test_spiffs() {
|
2014-12-22 12:35:05 +01:00
|
|
|
|
char buf[12];
|
|
|
|
|
|
|
|
|
|
// Surely, I've mounted spiffs before entering here
|
|
|
|
|
|
|
|
|
|
spiffs_file fd = SPIFFS_open(&fs, "my_file", SPIFFS_CREAT | SPIFFS_TRUNC | SPIFFS_RDWR, 0);
|
|
|
|
|
if (SPIFFS_write(&fs, fd, (u8_t *)"Hello world", 12) < 0) NODE_DBG("errno %i\n", SPIFFS_errno(&fs));
|
|
|
|
|
SPIFFS_close(&fs, fd);
|
|
|
|
|
|
|
|
|
|
fd = SPIFFS_open(&fs, "my_file", SPIFFS_RDWR, 0);
|
|
|
|
|
if (SPIFFS_read(&fs, fd, (u8_t *)buf, 12) < 0) NODE_DBG("errno %i\n", SPIFFS_errno(&fs));
|
|
|
|
|
SPIFFS_close(&fs, fd);
|
|
|
|
|
|
|
|
|
|
NODE_DBG("--> %s <--\n", buf);
|
|
|
|
|
}
|
|
|
|
|
#endif
|