Skip to content

Commit 862a237

Browse files
committed
Update SPIFFS to 0.2-64-g15e5618
1 parent 088cf44 commit 862a237

File tree

7 files changed

+225
-112
lines changed

7 files changed

+225
-112
lines changed

spiffs/spiffs.h

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ extern "C" {
4747
#define SPIFFS_ERR_ERASE_FAIL -10027
4848
#define SPIFFS_ERR_MAGIC_NOT_POSSIBLE -10028
4949

50+
#define SPIFFS_ERR_NO_DELETED_BLOCKS -10029
51+
52+
#define SPIFFS_ERR_FILE_EXISTS -10030
5053

5154
#define SPIFFS_ERR_INTERNAL -10050
5255

@@ -62,12 +65,25 @@ typedef u16_t spiffs_mode;
6265
// object type
6366
typedef u8_t spiffs_obj_type;
6467

68+
#if SPIFFS_HAL_CALLBACK_EXTRA
69+
struct spiffs_t;
70+
71+
/* spi read call function type */
72+
typedef s32_t (*spiffs_read)(struct spiffs_t *fs, u32_t addr, u32_t size, u8_t *dst);
73+
/* spi write call function type */
74+
typedef s32_t (*spiffs_write)(struct spiffs_t *fs, u32_t addr, u32_t size, u8_t *src);
75+
/* spi erase call function type */
76+
typedef s32_t (*spiffs_erase)(struct spiffs_t *fs, u32_t addr, u32_t size);
77+
78+
#else // SPIFFS_HAL_CALLBACK_EXTRA
79+
6580
/* spi read call function type */
6681
typedef s32_t (*spiffs_read)(u32_t addr, u32_t size, u8_t *dst);
6782
/* spi write call function type */
6883
typedef s32_t (*spiffs_write)(u32_t addr, u32_t size, u8_t *src);
6984
/* spi erase call function type */
7085
typedef s32_t (*spiffs_erase)(u32_t addr, u32_t size);
86+
#endif // SPIFFS_HAL_CALLBACK_EXTRA
7187

7288
/* file system check callback report operation */
7389
typedef enum {
@@ -88,8 +104,13 @@ typedef enum {
88104
} spiffs_check_report;
89105

90106
/* file system check callback function */
107+
#if SPIFFS_HAL_CALLBACK_EXTRA
108+
typedef void (*spiffs_check_callback)(struct spiffs_t *fs, spiffs_check_type type, spiffs_check_report report,
109+
u32_t arg1, u32_t arg2);
110+
#else // SPIFFS_HAL_CALLBACK_EXTRA
91111
typedef void (*spiffs_check_callback)(spiffs_check_type type, spiffs_check_report report,
92112
u32_t arg1, u32_t arg2);
113+
#endif // SPIFFS_HAL_CALLBACK_EXTRA
93114

94115
#ifndef SPIFFS_DBG
95116
#define SPIFFS_DBG(...) \
@@ -119,6 +140,8 @@ typedef void (*spiffs_check_callback)(spiffs_check_type type, spiffs_check_repor
119140
#define SPIFFS_RDWR (SPIFFS_RDONLY | SPIFFS_WRONLY)
120141
/* Any writes to the filehandle will never be cached */
121142
#define SPIFFS_DIRECT (1<<5)
143+
/* If SPIFFS_CREAT and SPIFFS_EXCL are set, SPIFFS_open() shall fail if the file exists */
144+
#define SPIFFS_EXCL (1<<6)
122145

123146
#define SPIFFS_SEEK_SET (0)
124147
#define SPIFFS_SEEK_CUR (1)
@@ -166,7 +189,7 @@ typedef struct {
166189
#endif
167190
} spiffs_config;
168191

169-
typedef struct {
192+
typedef struct spiffs_t {
170193
// file system configuration
171194
spiffs_config cfg;
172195
// number of logical blocks
@@ -224,6 +247,8 @@ typedef struct {
224247

225248
// mounted flag
226249
u8_t mounted;
250+
// user data
251+
void *user_data;
227252
// config magic
228253
u32_t config_magic;
229254
} spiffs;
@@ -387,7 +412,7 @@ s32_t SPIFFS_fflush(spiffs *fs, spiffs_file fh);
387412
* @param fs the file system struct
388413
* @param fh the filehandle of the file to close
389414
*/
390-
void SPIFFS_close(spiffs *fs, spiffs_file fh);
415+
s32_t SPIFFS_close(spiffs *fs, spiffs_file fh);
391416

392417
/**
393418
* Renames a file
@@ -440,7 +465,6 @@ struct spiffs_dirent *SPIFFS_readdir(spiffs_DIR *d, struct spiffs_dirent *e);
440465
*/
441466
s32_t SPIFFS_check(spiffs *fs);
442467

443-
444468
/**
445469
* Returns number of total bytes available and number of used bytes.
446470
* This is an estimation, and depends on if there a many files with little
@@ -465,27 +489,60 @@ s32_t SPIFFS_info(spiffs *fs, u32_t *total, u32_t *used);
465489
* SPIFFS_format.
466490
* If SPIFFS_mount fails, SPIFFS_format can be called directly without calling
467491
* SPIFFS_unmount first.
492+
*
493+
* @param fs the file system struct
468494
*/
469495
s32_t SPIFFS_format(spiffs *fs);
470496

471497
/**
472498
* Returns nonzero if spiffs is mounted, or zero if unmounted.
499+
* @param fs the file system struct
473500
*/
474501
u8_t SPIFFS_mounted(spiffs *fs);
475502

476503
/**
477-
* Check if EOF reached.
478-
* @param fs the file system struct
479-
* @param fh the filehandle of the file to check
504+
* Tries to find a block where most or all pages are deleted, and erase that
505+
* block if found. Does not care for wear levelling. Will not move pages
506+
* around.
507+
* If parameter max_free_pages are set to 0, only blocks with only deleted
508+
* pages will be selected.
509+
*
510+
* NB: the garbage collector is automatically called when spiffs needs free
511+
* pages. The reason for this function is to give possibility to do background
512+
* tidying when user knows the system is idle.
513+
*
514+
* Use with care.
515+
*
516+
* Setting max_free_pages to anything larger than zero will eventually wear
517+
* flash more as a block containing free pages can be erased.
518+
*
519+
* Will set err_no to SPIFFS_OK if a block was found and erased,
520+
* SPIFFS_ERR_NO_DELETED_BLOCK if no matching block was found,
521+
* or other error.
522+
*
523+
* @param fs the file system struct
524+
* @param max_free_pages maximum number allowed free pages in block
480525
*/
481-
s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh);
526+
s32_t SPIFFS_gc_quick(spiffs *fs, u16_t max_free_pages);
482527

483528
/**
484-
* Get the current position of the data pointer.
529+
* Will try to make room for given amount of bytes in the filesystem by moving
530+
* pages and erasing blocks.
531+
* If it is physically impossible, err_no will be set to SPIFFS_ERR_FULL. If
532+
* there already is this amount (or more) of free space, SPIFFS_gc will
533+
* silently return. It is recommended to call SPIFFS_info before invoking
534+
* this method in order to determine what amount of bytes to give.
535+
*
536+
* NB: the garbage collector is automatically called when spiffs needs free
537+
* pages. The reason for this function is to give possibility to do background
538+
* tidying when user knows the system is idle.
539+
*
540+
* Use with care.
541+
*
485542
* @param fs the file system struct
486-
* @param fh the filehandle of the open file
543+
* @param size amount of bytes that should be freed
487544
*/
488-
s32_t SPIFFS_tell(spiffs *fs, spiffs_file fh);
545+
s32_t SPIFFS_gc(spiffs *fs, u32_t size);
489546

490547
#if SPIFFS_TEST_VISUALISATION
491548
/**
@@ -511,7 +568,7 @@ u32_t SPIFFS_buffer_bytes_for_cache(spiffs *fs, u32_t num_pages);
511568
#endif
512569
#endif
513570

514-
#if SPIFFS_CHACHE
571+
#if SPIFFS_CACHE
515572
#endif
516573
#if defined(__cplusplus)
517574
}

spiffs/spiffs_cache.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static s32_t spiffs_cache_page_free(spiffs *fs, int ix, u8_t write_back) {
3939
(cp->flags & SPIFFS_CACHE_FLAG_TYPE_WR) == 0 &&
4040
(cp->flags & SPIFFS_CACHE_FLAG_DIRTY)) {
4141
u8_t *mem = spiffs_get_cache_page(fs, cache, ix);
42-
res = fs->cfg.hal_write_f(SPIFFS_PAGE_TO_PADDR(fs, cp->pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), mem);
42+
res = SPIFFS_HAL_WRITE(fs, SPIFFS_PAGE_TO_PADDR(fs, cp->pix), SPIFFS_CFG_LOG_PAGE_SZ(fs), mem);
4343
}
4444

4545
cp->flags = 0;
@@ -137,10 +137,7 @@ s32_t spiffs_phys_rd(
137137
} else {
138138
if ((op & SPIFFS_OP_TYPE_MASK) == SPIFFS_OP_T_OBJ_LU2) {
139139
// for second layer lookup functions, we do not cache in order to prevent shredding
140-
return fs->cfg.hal_read_f(
141-
addr ,
142-
len,
143-
dst);
140+
return SPIFFS_HAL_READ(fs, addr, len, dst);
144141
}
145142
#if SPIFFS_CACHE_STATS
146143
fs->cache_misses++;
@@ -151,8 +148,7 @@ s32_t spiffs_phys_rd(
151148
cp->flags = SPIFFS_CACHE_FLAG_WRTHRU;
152149
cp->pix = SPIFFS_PADDR_TO_PAGE(fs, addr);
153150
}
154-
155-
s32_t res2 = fs->cfg.hal_read_f(
151+
s32_t res2 = SPIFFS_HAL_READ(fs,
156152
addr - SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr),
157153
SPIFFS_CFG_LOG_PAGE_SZ(fs),
158154
spiffs_get_cache_page(fs, cache, cp->ix));
@@ -186,7 +182,7 @@ s32_t spiffs_phys_wr(
186182
(op & SPIFFS_OP_TYPE_MASK) != SPIFFS_OP_T_OBJ_LU) {
187183
// page is being deleted, wipe from cache - unless it is a lookup page
188184
spiffs_cache_page_free(fs, cp->ix, 0);
189-
return fs->cfg.hal_write_f(addr, len, src);
185+
return SPIFFS_HAL_WRITE(fs, addr, len, src);
190186
}
191187

192188
u8_t *mem = spiffs_get_cache_page(fs, cache, cp->ix);
@@ -197,13 +193,13 @@ s32_t spiffs_phys_wr(
197193

198194
if (cp->flags & SPIFFS_CACHE_FLAG_WRTHRU) {
199195
// page is being updated, no write-cache, just pass thru
200-
return fs->cfg.hal_write_f(addr, len, src);
196+
return SPIFFS_HAL_WRITE(fs, addr, len, src);
201197
} else {
202198
return SPIFFS_OK;
203199
}
204200
} else {
205201
// no cache page, no write cache - just write thru
206-
return fs->cfg.hal_write_f(addr, len, src);
202+
return SPIFFS_HAL_WRITE(fs, addr, len, src);
207203
}
208204
}
209205

0 commit comments

Comments
 (0)