Filesystem library designed for flash devices

Dependents:   flash-fs-example Dragonfly_Filesystem_Example STM32F407VET6_SPIFlash Dragonfly_Filesystem_Example_mbed_5

Committer:
Leon Lindenfelser
Date:
Fri Jun 26 14:31:42 2020 -0500
Revision:
3:9adcf49bb77d
Parent:
1:ea3b983425c8
Update to latest from Multitech git repo

1. Add timeout when waiting for WIP
2. Check 'write in progress bit' after writing the status register
3. Add ability to write the status register
4. Read identification function actually reads identification
5. Hardcode memory size as parameter in constructor
6. Move wakeup() call after CS, WP, and HOLD setup so it wakes the flash if it was sleeping

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:bb4e812f7c97 1 /*
mfiore 0:bb4e812f7c97 2 * spiffs_nucleus.h
mfiore 0:bb4e812f7c97 3 *
mfiore 0:bb4e812f7c97 4 * Created on: Jun 15, 2013
mfiore 0:bb4e812f7c97 5 * Author: petera
mfiore 0:bb4e812f7c97 6 */
mfiore 0:bb4e812f7c97 7
mfiore 0:bb4e812f7c97 8 /* SPIFFS layout
mfiore 0:bb4e812f7c97 9 *
mfiore 0:bb4e812f7c97 10 * spiffs is designed for following spi flash characteristics:
mfiore 0:bb4e812f7c97 11 * - only big areas of data (blocks) can be erased
mfiore 0:bb4e812f7c97 12 * - erasing resets all bits in a block to ones
mfiore 0:bb4e812f7c97 13 * - writing pulls ones to zeroes
mfiore 0:bb4e812f7c97 14 * - zeroes cannot be pulled to ones, without erase
mfiore 0:bb4e812f7c97 15 * - wear leveling
mfiore 0:bb4e812f7c97 16 *
mfiore 0:bb4e812f7c97 17 * spiffs is also meant to be run on embedded, memory constraint devices.
mfiore 0:bb4e812f7c97 18 *
mfiore 0:bb4e812f7c97 19 * Entire area is divided in blocks. Entire area is also divided in pages.
mfiore 0:bb4e812f7c97 20 * Each block contains same number of pages. A page cannot be erased, but a
mfiore 0:bb4e812f7c97 21 * block can be erased.
mfiore 0:bb4e812f7c97 22 *
mfiore 0:bb4e812f7c97 23 * Entire area must be block_size * x
mfiore 0:bb4e812f7c97 24 * page_size must be block_size / (2^y) where y > 2
mfiore 0:bb4e812f7c97 25 *
mfiore 0:bb4e812f7c97 26 * ex: area = 1024*1024 bytes, block size = 65536 bytes, page size = 256 bytes
mfiore 0:bb4e812f7c97 27 *
mfiore 0:bb4e812f7c97 28 * BLOCK 0 PAGE 0 object lookup 1
mfiore 0:bb4e812f7c97 29 * PAGE 1 object lookup 2
mfiore 0:bb4e812f7c97 30 * ...
mfiore 0:bb4e812f7c97 31 * PAGE n-1 object lookup n
mfiore 0:bb4e812f7c97 32 * PAGE n object data 1
mfiore 0:bb4e812f7c97 33 * PAGE n+1 object data 2
mfiore 0:bb4e812f7c97 34 * ...
mfiore 0:bb4e812f7c97 35 * PAGE n+m-1 object data m
mfiore 0:bb4e812f7c97 36 *
mfiore 0:bb4e812f7c97 37 * BLOCK 1 PAGE n+m object lookup 1
mfiore 0:bb4e812f7c97 38 * PAGE n+m+1 object lookup 2
mfiore 0:bb4e812f7c97 39 * ...
mfiore 0:bb4e812f7c97 40 * PAGE 2n+m-1 object lookup n
mfiore 0:bb4e812f7c97 41 * PAGE 2n+m object data 1
mfiore 0:bb4e812f7c97 42 * PAGE 2n+m object data 2
mfiore 0:bb4e812f7c97 43 * ...
mfiore 0:bb4e812f7c97 44 * PAGE 2n+2m-1 object data m
mfiore 0:bb4e812f7c97 45 * ...
mfiore 0:bb4e812f7c97 46 *
mfiore 0:bb4e812f7c97 47 * n is number of object lookup pages, which is number of pages needed to index all pages
mfiore 0:bb4e812f7c97 48 * in a block by object id
mfiore 0:bb4e812f7c97 49 * : block_size / page_size * sizeof(obj_id) / page_size
mfiore 0:bb4e812f7c97 50 * m is number data pages, which is number of pages in block minus number of lookup pages
mfiore 0:bb4e812f7c97 51 * : block_size / page_size - block_size / page_size * sizeof(obj_id) / page_size
mfiore 0:bb4e812f7c97 52 * thus, n+m is total number of pages in a block
mfiore 0:bb4e812f7c97 53 * : block_size / page_size
mfiore 0:bb4e812f7c97 54 *
mfiore 0:bb4e812f7c97 55 * ex: n = 65536/256*2/256 = 2, m = 65536/256 - 2 = 254 => n+m = 65536/256 = 256
mfiore 0:bb4e812f7c97 56 *
mfiore 0:bb4e812f7c97 57 * Object lookup pages contain object id entries. Each entry represent the corresponding
mfiore 0:bb4e812f7c97 58 * data page.
mfiore 0:bb4e812f7c97 59 * Assuming a 16 bit object id, an object id being 0xffff represents a free page.
mfiore 0:bb4e812f7c97 60 * An object id being 0x0000 represents a deleted page.
mfiore 0:bb4e812f7c97 61 *
mfiore 0:bb4e812f7c97 62 * ex: page 0 : lookup : 0008 0001 0aaa ffff ffff ffff ffff ffff ..
mfiore 0:bb4e812f7c97 63 * page 1 : lookup : ffff ffff ffff ffff ffff ffff ffff ffff ..
mfiore 0:bb4e812f7c97 64 * page 2 : data : data for object id 0008
mfiore 0:bb4e812f7c97 65 * page 3 : data : data for object id 0001
mfiore 0:bb4e812f7c97 66 * page 4 : data : data for object id 0aaa
mfiore 0:bb4e812f7c97 67 * ...
mfiore 0:bb4e812f7c97 68 *
mfiore 0:bb4e812f7c97 69 *
mfiore 0:bb4e812f7c97 70 * Object data pages can be either object index pages or object content.
mfiore 0:bb4e812f7c97 71 * All object data pages contains a data page header, containing object id and span index.
mfiore 0:bb4e812f7c97 72 * The span index denotes the object page ordering amongst data pages with same object id.
mfiore 0:bb4e812f7c97 73 * This applies to both object index pages (when index spans more than one page of entries),
mfiore 0:bb4e812f7c97 74 * and object data pages.
mfiore 0:bb4e812f7c97 75 * An object index page contains page entries pointing to object content page. The entry index
mfiore 0:bb4e812f7c97 76 * in a object index page correlates to the span index in the actual object data page.
mfiore 0:bb4e812f7c97 77 * The first object index page (span index 0) is called object index header page, and also
mfiore 0:bb4e812f7c97 78 * contains object flags (directory/file), size, object name etc.
mfiore 0:bb4e812f7c97 79 *
mfiore 0:bb4e812f7c97 80 * ex:
mfiore 0:bb4e812f7c97 81 * BLOCK 1
mfiore 0:bb4e812f7c97 82 * PAGE 256: objectl lookup page 1
mfiore 0:bb4e812f7c97 83 * [*123] [ 123] [ 123] [ 123]
mfiore 0:bb4e812f7c97 84 * [ 123] [*123] [ 123] [ 123]
mfiore 0:bb4e812f7c97 85 * [free] [free] [free] [free] ...
mfiore 0:bb4e812f7c97 86 * PAGE 257: objectl lookup page 2
mfiore 0:bb4e812f7c97 87 * [free] [free] [free] [free] ...
mfiore 0:bb4e812f7c97 88 * PAGE 258: object index page (header)
mfiore 0:bb4e812f7c97 89 * obj.id:0123 span.ix:0000 flags:INDEX
mfiore 0:bb4e812f7c97 90 * size:1600 name:ex.txt type:file
mfiore 0:bb4e812f7c97 91 * [259] [260] [261] [262]
mfiore 0:bb4e812f7c97 92 * PAGE 259: object data page
mfiore 0:bb4e812f7c97 93 * obj.id:0123 span.ix:0000 flags:DATA
mfiore 0:bb4e812f7c97 94 * PAGE 260: object data page
mfiore 0:bb4e812f7c97 95 * obj.id:0123 span.ix:0001 flags:DATA
mfiore 0:bb4e812f7c97 96 * PAGE 261: object data page
mfiore 0:bb4e812f7c97 97 * obj.id:0123 span.ix:0002 flags:DATA
mfiore 0:bb4e812f7c97 98 * PAGE 262: object data page
mfiore 0:bb4e812f7c97 99 * obj.id:0123 span.ix:0003 flags:DATA
mfiore 0:bb4e812f7c97 100 * PAGE 263: object index page
mfiore 0:bb4e812f7c97 101 * obj.id:0123 span.ix:0001 flags:INDEX
mfiore 0:bb4e812f7c97 102 * [264] [265] [fre] [fre]
mfiore 0:bb4e812f7c97 103 * [fre] [fre] [fre] [fre]
mfiore 0:bb4e812f7c97 104 * PAGE 264: object data page
mfiore 0:bb4e812f7c97 105 * obj.id:0123 span.ix:0004 flags:DATA
mfiore 0:bb4e812f7c97 106 * PAGE 265: object data page
mfiore 0:bb4e812f7c97 107 * obj.id:0123 span.ix:0005 flags:DATA
mfiore 0:bb4e812f7c97 108 *
mfiore 0:bb4e812f7c97 109 */
mfiore 0:bb4e812f7c97 110 #ifndef SPIFFS_NUCLEUS_H_
mfiore 0:bb4e812f7c97 111 #define SPIFFS_NUCLEUS_H_
mfiore 0:bb4e812f7c97 112
mfiore 0:bb4e812f7c97 113 #define _SPIFFS_ERR_CHECK_FIRST (SPIFFS_ERR_INTERNAL - 1)
mfiore 0:bb4e812f7c97 114 #define SPIFFS_ERR_CHECK_OBJ_ID_MISM (SPIFFS_ERR_INTERNAL - 1)
mfiore 0:bb4e812f7c97 115 #define SPIFFS_ERR_CHECK_SPIX_MISM (SPIFFS_ERR_INTERNAL - 2)
mfiore 0:bb4e812f7c97 116 #define SPIFFS_ERR_CHECK_FLAGS_BAD (SPIFFS_ERR_INTERNAL - 3)
mfiore 0:bb4e812f7c97 117 #define _SPIFFS_ERR_CHECK_LAST (SPIFFS_ERR_INTERNAL - 4)
mfiore 0:bb4e812f7c97 118
mfiore 0:bb4e812f7c97 119 #define SPIFFS_VIS_COUNTINUE (SPIFFS_ERR_INTERNAL - 20)
mfiore 0:bb4e812f7c97 120 #define SPIFFS_VIS_COUNTINUE_RELOAD (SPIFFS_ERR_INTERNAL - 21)
mfiore 0:bb4e812f7c97 121 #define SPIFFS_VIS_END (SPIFFS_ERR_INTERNAL - 22)
mfiore 0:bb4e812f7c97 122
mfiore 0:bb4e812f7c97 123 #define SPIFFS_EV_IX_UPD 0
mfiore 0:bb4e812f7c97 124 #define SPIFFS_EV_IX_NEW 1
mfiore 0:bb4e812f7c97 125 #define SPIFFS_EV_IX_DEL 2
mfiore 0:bb4e812f7c97 126
mfiore 0:bb4e812f7c97 127 #define SPIFFS_OBJ_ID_IX_FLAG (1<<(8*sizeof(spiffs_obj_id)-1))
mfiore 0:bb4e812f7c97 128
mfiore 0:bb4e812f7c97 129 #define SPIFFS_UNDEFINED_LEN (-1)
mfiore 0:bb4e812f7c97 130
mfiore 0:bb4e812f7c97 131 #define SPIFFS_OBJ_ID_DELETED ((spiffs_obj_id)0)
mfiore 0:bb4e812f7c97 132 #define SPIFFS_OBJ_ID_FREE ((spiffs_obj_id)-1)
mfiore 0:bb4e812f7c97 133
mfiore 0:bb4e812f7c97 134 #if SPIFFS_SINGLETON == 0
mfiore 0:bb4e812f7c97 135 #define SPIFFS_CFG_LOG_PAGE_SZ(fs) \
mfiore 0:bb4e812f7c97 136 ((fs)->cfg.log_page_size)
mfiore 0:bb4e812f7c97 137 #define SPIFFS_CFG_LOG_BLOCK_SZ(fs) \
mfiore 0:bb4e812f7c97 138 ((fs)->cfg.log_block_size)
mfiore 0:bb4e812f7c97 139 #define SPIFFS_CFG_PHYS_SZ(fs) \
mfiore 0:bb4e812f7c97 140 ((fs)->cfg.phys_size)
mfiore 0:bb4e812f7c97 141 #define SPIFFS_CFG_PHYS_ERASE_SZ(fs) \
mfiore 0:bb4e812f7c97 142 ((fs)->cfg.phys_erase_block)
mfiore 0:bb4e812f7c97 143 #define SPIFFS_CFG_PHYS_ADDR(fs) \
mfiore 0:bb4e812f7c97 144 ((fs)->cfg.phys_addr)
mfiore 0:bb4e812f7c97 145 #endif
mfiore 0:bb4e812f7c97 146
mfiore 0:bb4e812f7c97 147 // total number of pages
mfiore 0:bb4e812f7c97 148 #define SPIFFS_MAX_PAGES(fs) \
mfiore 0:bb4e812f7c97 149 ( SPIFFS_CFG_PHYS_SZ(fs)/SPIFFS_CFG_LOG_PAGE_SZ(fs) )
mfiore 0:bb4e812f7c97 150 // total number of pages per block, including object lookup pages
mfiore 0:bb4e812f7c97 151 #define SPIFFS_PAGES_PER_BLOCK(fs) \
mfiore 0:bb4e812f7c97 152 ( SPIFFS_CFG_LOG_BLOCK_SZ(fs)/SPIFFS_CFG_LOG_PAGE_SZ(fs) )
mfiore 0:bb4e812f7c97 153 // number of object lookup pages per block
mfiore 0:bb4e812f7c97 154 #define SPIFFS_OBJ_LOOKUP_PAGES(fs) \
mfiore 0:bb4e812f7c97 155 (MAX(1, (SPIFFS_PAGES_PER_BLOCK(fs) * sizeof(spiffs_obj_id)) / SPIFFS_CFG_LOG_PAGE_SZ(fs)) )
mfiore 0:bb4e812f7c97 156 // checks if page index belongs to object lookup
mfiore 0:bb4e812f7c97 157 #define SPIFFS_IS_LOOKUP_PAGE(fs,pix) \
mfiore 0:bb4e812f7c97 158 (((pix) % SPIFFS_PAGES_PER_BLOCK(fs)) < SPIFFS_OBJ_LOOKUP_PAGES(fs))
mfiore 0:bb4e812f7c97 159 // number of object lookup entries in all object lookup pages
mfiore 0:bb4e812f7c97 160 #define SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs) \
mfiore 0:bb4e812f7c97 161 (SPIFFS_PAGES_PER_BLOCK(fs)-SPIFFS_OBJ_LOOKUP_PAGES(fs))
mfiore 0:bb4e812f7c97 162 // converts a block to physical address
mfiore 0:bb4e812f7c97 163 #define SPIFFS_BLOCK_TO_PADDR(fs, block) \
mfiore 0:bb4e812f7c97 164 ( SPIFFS_CFG_PHYS_ADDR(fs) + (block)* SPIFFS_CFG_LOG_BLOCK_SZ(fs) )
mfiore 0:bb4e812f7c97 165 // converts a object lookup entry to page index
mfiore 0:bb4e812f7c97 166 #define SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, block, entry) \
mfiore 0:bb4e812f7c97 167 ((block)*SPIFFS_PAGES_PER_BLOCK(fs) + (SPIFFS_OBJ_LOOKUP_PAGES(fs) + entry))
mfiore 0:bb4e812f7c97 168 // converts a object lookup entry to physical address of corresponding page
mfiore 0:bb4e812f7c97 169 #define SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, block, entry) \
mfiore 0:bb4e812f7c97 170 (SPIFFS_BLOCK_TO_PADDR(fs, block) + (SPIFFS_OBJ_LOOKUP_PAGES(fs) + entry) * SPIFFS_CFG_LOG_PAGE_SZ(fs) )
mfiore 0:bb4e812f7c97 171 // converts a page to physical address
mfiore 0:bb4e812f7c97 172 #define SPIFFS_PAGE_TO_PADDR(fs, page) \
mfiore 0:bb4e812f7c97 173 ( SPIFFS_CFG_PHYS_ADDR(fs) + (page) * SPIFFS_CFG_LOG_PAGE_SZ(fs) )
mfiore 0:bb4e812f7c97 174 // converts a physical address to page
mfiore 0:bb4e812f7c97 175 #define SPIFFS_PADDR_TO_PAGE(fs, addr) \
mfiore 0:bb4e812f7c97 176 ( ((addr) - SPIFFS_CFG_PHYS_ADDR(fs)) / SPIFFS_CFG_LOG_PAGE_SZ(fs) )
mfiore 0:bb4e812f7c97 177 // gives index in page for a physical address
mfiore 0:bb4e812f7c97 178 #define SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr) \
mfiore 0:bb4e812f7c97 179 ( ((addr) - SPIFFS_CFG_PHYS_ADDR(fs)) % SPIFFS_CFG_LOG_PAGE_SZ(fs) )
mfiore 0:bb4e812f7c97 180 // returns containing block for given page
mfiore 0:bb4e812f7c97 181 #define SPIFFS_BLOCK_FOR_PAGE(fs, page) \
mfiore 0:bb4e812f7c97 182 ( (page) / SPIFFS_PAGES_PER_BLOCK(fs) )
mfiore 0:bb4e812f7c97 183 // returns starting page for block
mfiore 0:bb4e812f7c97 184 #define SPIFFS_PAGE_FOR_BLOCK(fs, block) \
mfiore 0:bb4e812f7c97 185 ( (block) * SPIFFS_PAGES_PER_BLOCK(fs) )
mfiore 0:bb4e812f7c97 186 // converts page to entry in object lookup page
mfiore 0:bb4e812f7c97 187 #define SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, page) \
mfiore 0:bb4e812f7c97 188 ( (page) % SPIFFS_PAGES_PER_BLOCK(fs) - SPIFFS_OBJ_LOOKUP_PAGES(fs) )
mfiore 0:bb4e812f7c97 189 // returns data size in a data page
mfiore 0:bb4e812f7c97 190 #define SPIFFS_DATA_PAGE_SIZE(fs) \
mfiore 0:bb4e812f7c97 191 ( SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_header) )
mfiore 0:bb4e812f7c97 192 // returns physical address for block's erase count
mfiore 0:bb4e812f7c97 193 #define SPIFFS_ERASE_COUNT_PADDR(fs, bix) \
mfiore 0:bb4e812f7c97 194 ( SPIFFS_BLOCK_TO_PADDR(fs, bix) + SPIFFS_OBJ_LOOKUP_PAGES(fs) * SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_obj_id) )
mfiore 0:bb4e812f7c97 195
mfiore 0:bb4e812f7c97 196 // define helpers object
mfiore 0:bb4e812f7c97 197
mfiore 0:bb4e812f7c97 198 // entries in an object header page index
mfiore 0:bb4e812f7c97 199 #define SPIFFS_OBJ_HDR_IX_LEN(fs) \
mfiore 0:bb4e812f7c97 200 ((SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_object_ix_header))/sizeof(spiffs_page_ix))
mfiore 0:bb4e812f7c97 201 // entries in an object page index
mfiore 0:bb4e812f7c97 202 #define SPIFFS_OBJ_IX_LEN(fs) \
mfiore 0:bb4e812f7c97 203 ((SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_object_ix))/sizeof(spiffs_page_ix))
mfiore 0:bb4e812f7c97 204 // object index entry for given data span index
mfiore 0:bb4e812f7c97 205 #define SPIFFS_OBJ_IX_ENTRY(fs, spix) \
mfiore 0:bb4e812f7c97 206 ((spix) < SPIFFS_OBJ_HDR_IX_LEN(fs) ? (spix) : (((spix)-SPIFFS_OBJ_HDR_IX_LEN(fs))%SPIFFS_OBJ_IX_LEN(fs)))
mfiore 0:bb4e812f7c97 207 // object index span index number for given data span index or entry
mfiore 0:bb4e812f7c97 208 #define SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, spix) \
mfiore 0:bb4e812f7c97 209 ((spix) < SPIFFS_OBJ_HDR_IX_LEN(fs) ? 0 : (1+((spix)-SPIFFS_OBJ_HDR_IX_LEN(fs))/SPIFFS_OBJ_IX_LEN(fs)))
mfiore 0:bb4e812f7c97 210
mfiore 0:bb4e812f7c97 211
mfiore 0:bb4e812f7c97 212 #define SPIFFS_OP_T_OBJ_LU (0<<0)
mfiore 0:bb4e812f7c97 213 #define SPIFFS_OP_T_OBJ_LU2 (1<<0)
mfiore 0:bb4e812f7c97 214 #define SPIFFS_OP_T_OBJ_IX (2<<0)
mfiore 0:bb4e812f7c97 215 #define SPIFFS_OP_T_OBJ_DA (3<<0)
mfiore 0:bb4e812f7c97 216 #define SPIFFS_OP_C_DELE (0<<2)
mfiore 0:bb4e812f7c97 217 #define SPIFFS_OP_C_UPDT (1<<2)
mfiore 0:bb4e812f7c97 218 #define SPIFFS_OP_C_MOVS (2<<2)
mfiore 0:bb4e812f7c97 219 #define SPIFFS_OP_C_MOVD (3<<2)
mfiore 0:bb4e812f7c97 220 #define SPIFFS_OP_C_FLSH (4<<2)
mfiore 0:bb4e812f7c97 221 #define SPIFFS_OP_C_READ (5<<2)
mfiore 0:bb4e812f7c97 222 #define SPIFFS_OP_C_WRTHRU (6<<2)
mfiore 0:bb4e812f7c97 223
mfiore 0:bb4e812f7c97 224 #define SPIFFS_OP_TYPE_MASK (3<<0)
mfiore 0:bb4e812f7c97 225 #define SPIFFS_OP_COM_MASK (7<<2)
mfiore 0:bb4e812f7c97 226
mfiore 0:bb4e812f7c97 227
mfiore 0:bb4e812f7c97 228 // if 0, this page is written to, else clean
mfiore 0:bb4e812f7c97 229 #define SPIFFS_PH_FLAG_USED (1<<0)
mfiore 0:bb4e812f7c97 230 // if 0, writing is finalized, else under modification
mfiore 0:bb4e812f7c97 231 #define SPIFFS_PH_FLAG_FINAL (1<<1)
mfiore 0:bb4e812f7c97 232 // if 0, this is an index page, else a data page
mfiore 0:bb4e812f7c97 233 #define SPIFFS_PH_FLAG_INDEX (1<<2)
mfiore 0:bb4e812f7c97 234 // if 0, page is deleted, else valid
mfiore 0:bb4e812f7c97 235 #define SPIFFS_PH_FLAG_DELET (1<<7)
mfiore 0:bb4e812f7c97 236 // if 0, this index header is being deleted
mfiore 0:bb4e812f7c97 237 #define SPIFFS_PH_FLAG_IXDELE (1<<6)
mfiore 0:bb4e812f7c97 238
mfiore 0:bb4e812f7c97 239
mfiore 0:bb4e812f7c97 240 #define SPIFFS_CHECK_MOUNT(fs) \
mfiore 0:bb4e812f7c97 241 ((fs)->block_count > 0)
mfiore 0:bb4e812f7c97 242
mfiore 0:bb4e812f7c97 243 #define SPIFFS_CHECK_RES(res) \
mfiore 0:bb4e812f7c97 244 do { \
mfiore 0:bb4e812f7c97 245 if ((res) < SPIFFS_OK) return (res); \
mfiore 0:bb4e812f7c97 246 } while (0);
mfiore 0:bb4e812f7c97 247
mfiore 0:bb4e812f7c97 248 #define SPIFFS_API_CHECK_MOUNT(fs) \
mfiore 0:bb4e812f7c97 249 if (!SPIFFS_CHECK_MOUNT((fs))) { \
Leon Lindenfelser 3:9adcf49bb77d 250 (fs)->err_code = SPIFFS_ERR_NOT_MOUNTED; \
mfiore 0:bb4e812f7c97 251 return -1; \
mfiore 0:bb4e812f7c97 252 }
mfiore 0:bb4e812f7c97 253
mfiore 0:bb4e812f7c97 254 #define SPIFFS_API_CHECK_RES(fs, res) \
mfiore 0:bb4e812f7c97 255 if ((res) < SPIFFS_OK) { \
Leon Lindenfelser 3:9adcf49bb77d 256 (fs)->err_code = (res); \
mfiore 0:bb4e812f7c97 257 return -1; \
mfiore 0:bb4e812f7c97 258 }
mfiore 0:bb4e812f7c97 259
mfiore 0:bb4e812f7c97 260 #define SPIFFS_API_CHECK_RES_UNLOCK(fs, res) \
mfiore 0:bb4e812f7c97 261 if ((res) < SPIFFS_OK) { \
Leon Lindenfelser 3:9adcf49bb77d 262 (fs)->err_code = (res); \
mfiore 0:bb4e812f7c97 263 SPIFFS_UNLOCK(fs); \
mfiore 0:bb4e812f7c97 264 return -1; \
mfiore 0:bb4e812f7c97 265 }
mfiore 0:bb4e812f7c97 266
mfiore 0:bb4e812f7c97 267 #define SPIFFS_VALIDATE_OBJIX(ph, objid, spix) \
mfiore 0:bb4e812f7c97 268 if (((ph).flags & SPIFFS_PH_FLAG_USED) != 0) return SPIFFS_ERR_IS_FREE; \
mfiore 0:bb4e812f7c97 269 if (((ph).flags & SPIFFS_PH_FLAG_DELET) == 0) return SPIFFS_ERR_DELETED; \
mfiore 0:bb4e812f7c97 270 if (((ph).flags & SPIFFS_PH_FLAG_FINAL) != 0) return SPIFFS_ERR_NOT_FINALIZED; \
mfiore 0:bb4e812f7c97 271 if (((ph).flags & SPIFFS_PH_FLAG_INDEX) != 0) return SPIFFS_ERR_NOT_INDEX; \
mfiore 0:bb4e812f7c97 272 if (((objid) & SPIFFS_OBJ_ID_IX_FLAG) == 0) return SPIFFS_ERR_NOT_INDEX; \
mfiore 0:bb4e812f7c97 273 if ((ph).span_ix != (spix)) return SPIFFS_ERR_INDEX_SPAN_MISMATCH;
mfiore 0:bb4e812f7c97 274 //if ((spix) == 0 && ((ph).flags & SPIFFS_PH_FLAG_IXDELE) == 0) return SPIFFS_ERR_DELETED;
mfiore 0:bb4e812f7c97 275
mfiore 0:bb4e812f7c97 276 #define SPIFFS_VALIDATE_DATA(ph, objid, spix) \
mfiore 0:bb4e812f7c97 277 if (((ph).flags & SPIFFS_PH_FLAG_USED) != 0) return SPIFFS_ERR_IS_FREE; \
mfiore 0:bb4e812f7c97 278 if (((ph).flags & SPIFFS_PH_FLAG_DELET) == 0) return SPIFFS_ERR_DELETED; \
mfiore 0:bb4e812f7c97 279 if (((ph).flags & SPIFFS_PH_FLAG_FINAL) != 0) return SPIFFS_ERR_NOT_FINALIZED; \
mfiore 0:bb4e812f7c97 280 if (((ph).flags & SPIFFS_PH_FLAG_INDEX) == 0) return SPIFFS_ERR_IS_INDEX; \
mfiore 0:bb4e812f7c97 281 if ((objid) & SPIFFS_OBJ_ID_IX_FLAG) return SPIFFS_ERR_IS_INDEX; \
mfiore 0:bb4e812f7c97 282 if ((ph).span_ix != (spix)) return SPIFFS_ERR_DATA_SPAN_MISMATCH;
mfiore 0:bb4e812f7c97 283
mfiore 0:bb4e812f7c97 284
mfiore 0:bb4e812f7c97 285 // check id
mfiore 0:bb4e812f7c97 286 #define SPIFFS_VIS_CHECK_ID (1<<0)
mfiore 0:bb4e812f7c97 287 // report argument object id to visitor - else object lookup id is reported
mfiore 0:bb4e812f7c97 288 #define SPIFFS_VIS_CHECK_PH (1<<1)
mfiore 0:bb4e812f7c97 289 // stop searching at end of all look up pages
mfiore 0:bb4e812f7c97 290 #define SPIFFS_VIS_NO_WRAP (1<<2)
mfiore 0:bb4e812f7c97 291
mfiore 0:bb4e812f7c97 292 #if SPIFFS_CACHE
mfiore 0:bb4e812f7c97 293
mfiore 0:bb4e812f7c97 294 #define SPIFFS_CACHE_FLAG_DIRTY (1<<0)
mfiore 0:bb4e812f7c97 295 #define SPIFFS_CACHE_FLAG_WRTHRU (1<<1)
mfiore 0:bb4e812f7c97 296 #define SPIFFS_CACHE_FLAG_OBJLU (1<<2)
mfiore 0:bb4e812f7c97 297 #define SPIFFS_CACHE_FLAG_OBJIX (1<<3)
mfiore 0:bb4e812f7c97 298 #define SPIFFS_CACHE_FLAG_DATA (1<<4)
mfiore 0:bb4e812f7c97 299 #define SPIFFS_CACHE_FLAG_TYPE_WR (1<<7)
mfiore 0:bb4e812f7c97 300
mfiore 0:bb4e812f7c97 301 #define SPIFFS_CACHE_PAGE_SIZE(fs) \
mfiore 0:bb4e812f7c97 302 (sizeof(spiffs_cache_page) + SPIFFS_CFG_LOG_PAGE_SZ(fs))
mfiore 0:bb4e812f7c97 303
mfiore 0:bb4e812f7c97 304 #define spiffs_get_cache(fs) \
mfiore 0:bb4e812f7c97 305 ((spiffs_cache *)((fs)->cache))
mfiore 0:bb4e812f7c97 306
mfiore 0:bb4e812f7c97 307 #define spiffs_get_cache_page_hdr(fs, c, ix) \
mfiore 0:bb4e812f7c97 308 ((spiffs_cache_page *)(&((c)->cpages[(ix) * SPIFFS_CACHE_PAGE_SIZE(fs)])))
mfiore 0:bb4e812f7c97 309
mfiore 0:bb4e812f7c97 310 #define spiffs_get_cache_page(fs, c, ix) \
mfiore 0:bb4e812f7c97 311 ((u8_t *)(&((c)->cpages[(ix) * SPIFFS_CACHE_PAGE_SIZE(fs)])) + sizeof(spiffs_cache_page))
mfiore 0:bb4e812f7c97 312
mfiore 0:bb4e812f7c97 313 // cache page struct
mfiore 0:bb4e812f7c97 314 typedef struct {
mfiore 0:bb4e812f7c97 315 // cache flags
mfiore 0:bb4e812f7c97 316 u8_t flags;
mfiore 0:bb4e812f7c97 317 // cache page index
mfiore 0:bb4e812f7c97 318 u8_t ix;
mfiore 0:bb4e812f7c97 319 // last access of this cache page
mfiore 0:bb4e812f7c97 320 u32_t last_access;
mfiore 0:bb4e812f7c97 321 union {
mfiore 0:bb4e812f7c97 322 // type read cache
mfiore 0:bb4e812f7c97 323 struct {
mfiore 0:bb4e812f7c97 324 // read cache page index
mfiore 0:bb4e812f7c97 325 spiffs_page_ix pix;
mfiore 0:bb4e812f7c97 326 };
mfiore 0:bb4e812f7c97 327 #if SPIFFS_CACHE_WR
mfiore 0:bb4e812f7c97 328 // type write cache
mfiore 0:bb4e812f7c97 329 struct {
mfiore 0:bb4e812f7c97 330 // write cache
mfiore 0:bb4e812f7c97 331 spiffs_obj_id obj_id;
mfiore 0:bb4e812f7c97 332 // offset in cache page
mfiore 0:bb4e812f7c97 333 u32_t offset;
mfiore 0:bb4e812f7c97 334 // size of cache page
mfiore 0:bb4e812f7c97 335 u16_t size;
mfiore 0:bb4e812f7c97 336 };
mfiore 0:bb4e812f7c97 337 #endif
mfiore 0:bb4e812f7c97 338 };
mfiore 0:bb4e812f7c97 339 } spiffs_cache_page;
mfiore 0:bb4e812f7c97 340
mfiore 0:bb4e812f7c97 341 // cache struct
mfiore 0:bb4e812f7c97 342 typedef struct {
mfiore 0:bb4e812f7c97 343 u8_t cpage_count;
mfiore 0:bb4e812f7c97 344 u32_t last_access;
mfiore 0:bb4e812f7c97 345 u32_t cpage_use_map;
mfiore 0:bb4e812f7c97 346 u32_t cpage_use_mask;
mfiore 0:bb4e812f7c97 347 u8_t *cpages;
mfiore 0:bb4e812f7c97 348 } spiffs_cache;
mfiore 0:bb4e812f7c97 349
mfiore 0:bb4e812f7c97 350 #endif
mfiore 0:bb4e812f7c97 351
mfiore 0:bb4e812f7c97 352
mfiore 0:bb4e812f7c97 353 // spiffs nucleus file descriptor
mfiore 0:bb4e812f7c97 354 typedef struct {
mfiore 0:bb4e812f7c97 355 // the filesystem of this descriptor
mfiore 0:bb4e812f7c97 356 spiffs *fs;
mfiore 0:bb4e812f7c97 357 // number of file descriptor - if 0, the file descriptor is closed
mfiore 0:bb4e812f7c97 358 spiffs_file file_nbr;
mfiore 0:bb4e812f7c97 359 // object id - if SPIFFS_OBJ_ID_ERASED, the file was deleted
mfiore 0:bb4e812f7c97 360 spiffs_obj_id obj_id;
mfiore 0:bb4e812f7c97 361 // size of the file
mfiore 0:bb4e812f7c97 362 u32_t size;
mfiore 0:bb4e812f7c97 363 // cached object index header page index
mfiore 0:bb4e812f7c97 364 spiffs_page_ix objix_hdr_pix;
mfiore 0:bb4e812f7c97 365 // cached offset object index page index
mfiore 0:bb4e812f7c97 366 spiffs_page_ix cursor_objix_pix;
mfiore 0:bb4e812f7c97 367 // cached offset object index span index
mfiore 0:bb4e812f7c97 368 spiffs_span_ix cursor_objix_spix;
mfiore 0:bb4e812f7c97 369 // current absolute offset
mfiore 0:bb4e812f7c97 370 u32_t offset;
mfiore 0:bb4e812f7c97 371 // current file descriptor offset
mfiore 0:bb4e812f7c97 372 u32_t fdoffset;
mfiore 0:bb4e812f7c97 373 // fd flags
mfiore 0:bb4e812f7c97 374 spiffs_flags flags;
mfiore 0:bb4e812f7c97 375 #if SPIFFS_CACHE_WR
mfiore 0:bb4e812f7c97 376 spiffs_cache_page *cache_page;
mfiore 0:bb4e812f7c97 377 #endif
mfiore 0:bb4e812f7c97 378 } spiffs_fd;
mfiore 0:bb4e812f7c97 379
mfiore 0:bb4e812f7c97 380
mfiore 0:bb4e812f7c97 381 // object structs
mfiore 0:bb4e812f7c97 382
mfiore 0:bb4e812f7c97 383 // page header, part of each page except object lookup pages
Mike Fiore 1:ea3b983425c8 384 #ifdef __ICCARM__
Mike Fiore 1:ea3b983425c8 385 typedef __packed struct {
Mike Fiore 1:ea3b983425c8 386 #else
mfiore 0:bb4e812f7c97 387 typedef struct __attribute(( packed )) {
Mike Fiore 1:ea3b983425c8 388 #endif
mfiore 0:bb4e812f7c97 389 // object id
mfiore 0:bb4e812f7c97 390 spiffs_obj_id obj_id;
mfiore 0:bb4e812f7c97 391 // object span index
mfiore 0:bb4e812f7c97 392 spiffs_span_ix span_ix;
mfiore 0:bb4e812f7c97 393 // flags
mfiore 0:bb4e812f7c97 394 u8_t flags;
mfiore 0:bb4e812f7c97 395 } spiffs_page_header;
mfiore 0:bb4e812f7c97 396
mfiore 0:bb4e812f7c97 397 // object index header page header
Mike Fiore 1:ea3b983425c8 398 #ifdef __ICCARM__
Mike Fiore 1:ea3b983425c8 399 typedef __packed struct {
Mike Fiore 1:ea3b983425c8 400 #else
mfiore 0:bb4e812f7c97 401 typedef struct __attribute(( packed )) {
Mike Fiore 1:ea3b983425c8 402 #endif
mfiore 0:bb4e812f7c97 403 // common page header
mfiore 0:bb4e812f7c97 404 spiffs_page_header p_hdr;
mfiore 0:bb4e812f7c97 405 // alignment
mfiore 0:bb4e812f7c97 406 u8_t _align[4 - (sizeof(spiffs_page_header)&3)==0 ? 4 : (sizeof(spiffs_page_header)&3)];
mfiore 0:bb4e812f7c97 407 // size of object
mfiore 0:bb4e812f7c97 408 u32_t size;
mfiore 0:bb4e812f7c97 409 // type of object
mfiore 0:bb4e812f7c97 410 spiffs_obj_type type;
mfiore 0:bb4e812f7c97 411 // name of object
mfiore 0:bb4e812f7c97 412 u8_t name[SPIFFS_OBJ_NAME_LEN];
mfiore 0:bb4e812f7c97 413 } spiffs_page_object_ix_header;
mfiore 0:bb4e812f7c97 414
mfiore 0:bb4e812f7c97 415 // object index page header
Mike Fiore 1:ea3b983425c8 416 #ifdef __ICCARM__
Mike Fiore 1:ea3b983425c8 417 typedef __packed struct {
Mike Fiore 1:ea3b983425c8 418 #else
mfiore 0:bb4e812f7c97 419 typedef struct __attribute(( packed )) {
Mike Fiore 1:ea3b983425c8 420 #endif
mfiore 0:bb4e812f7c97 421 spiffs_page_header p_hdr;
mfiore 0:bb4e812f7c97 422 u8_t _align[4 - (sizeof(spiffs_page_header)&3)==0 ? 4 : (sizeof(spiffs_page_header)&3)];
mfiore 0:bb4e812f7c97 423 } spiffs_page_object_ix;
mfiore 0:bb4e812f7c97 424
mfiore 0:bb4e812f7c97 425 // callback func for object lookup visitor
mfiore 0:bb4e812f7c97 426 typedef s32_t (*spiffs_visitor_f)(spiffs *fs, spiffs_obj_id id, spiffs_block_ix bix, int ix_entry,
mfiore 0:bb4e812f7c97 427 u32_t user_data, void *user_p);
mfiore 0:bb4e812f7c97 428
mfiore 0:bb4e812f7c97 429
mfiore 0:bb4e812f7c97 430 #if SPIFFS_CACHE
mfiore 0:bb4e812f7c97 431 #define _spiffs_rd(fs, op, fh, addr, len, dst) \
mfiore 0:bb4e812f7c97 432 spiffs_phys_rd((fs), (op), (fh), (addr), (len), (dst))
mfiore 0:bb4e812f7c97 433 #define _spiffs_wr(fs, op, fh, addr, len, src) \
mfiore 0:bb4e812f7c97 434 spiffs_phys_wr((fs), (op), (fh), (addr), (len), (src))
mfiore 0:bb4e812f7c97 435 #else
mfiore 0:bb4e812f7c97 436 #define _spiffs_rd(fs, op, fh, addr, len, dst) \
mfiore 0:bb4e812f7c97 437 spiffs_phys_rd((fs), (addr), (len), (dst))
mfiore 0:bb4e812f7c97 438 #define _spiffs_wr(fs, op, fh, addr, len, src) \
mfiore 0:bb4e812f7c97 439 spiffs_phys_wr((fs), (addr), (len), (src))
mfiore 0:bb4e812f7c97 440 #endif
mfiore 0:bb4e812f7c97 441
mfiore 0:bb4e812f7c97 442 #ifndef MIN
mfiore 0:bb4e812f7c97 443 #define MIN(a,b) ((a) < (b) ? (a) : (b))
mfiore 0:bb4e812f7c97 444 #endif
mfiore 0:bb4e812f7c97 445 #ifndef MAX
mfiore 0:bb4e812f7c97 446 #define MAX(a,b) ((a) > (b) ? (a) : (b))
mfiore 0:bb4e812f7c97 447 #endif
mfiore 0:bb4e812f7c97 448
mfiore 0:bb4e812f7c97 449 // ---------------
mfiore 0:bb4e812f7c97 450
mfiore 0:bb4e812f7c97 451 s32_t spiffs_phys_rd(
mfiore 0:bb4e812f7c97 452 spiffs *fs,
mfiore 0:bb4e812f7c97 453 #if SPIFFS_CACHE
mfiore 0:bb4e812f7c97 454 u8_t op,
mfiore 0:bb4e812f7c97 455 spiffs_file fh,
mfiore 0:bb4e812f7c97 456 #endif
mfiore 0:bb4e812f7c97 457 u32_t addr,
mfiore 0:bb4e812f7c97 458 u32_t len,
mfiore 0:bb4e812f7c97 459 u8_t *dst);
mfiore 0:bb4e812f7c97 460
mfiore 0:bb4e812f7c97 461 s32_t spiffs_phys_wr(
mfiore 0:bb4e812f7c97 462 spiffs *fs,
mfiore 0:bb4e812f7c97 463 #if SPIFFS_CACHE
mfiore 0:bb4e812f7c97 464 u8_t op,
mfiore 0:bb4e812f7c97 465 spiffs_file fh,
mfiore 0:bb4e812f7c97 466 #endif
mfiore 0:bb4e812f7c97 467 u32_t addr,
mfiore 0:bb4e812f7c97 468 u32_t len,
mfiore 0:bb4e812f7c97 469 u8_t *src);
mfiore 0:bb4e812f7c97 470
mfiore 0:bb4e812f7c97 471 s32_t spiffs_phys_cpy(
mfiore 0:bb4e812f7c97 472 spiffs *fs,
mfiore 0:bb4e812f7c97 473 spiffs_file fh,
mfiore 0:bb4e812f7c97 474 u32_t dst,
mfiore 0:bb4e812f7c97 475 u32_t src,
mfiore 0:bb4e812f7c97 476 u32_t len);
mfiore 0:bb4e812f7c97 477
mfiore 0:bb4e812f7c97 478 s32_t spiffs_phys_count_free_blocks(
mfiore 0:bb4e812f7c97 479 spiffs *fs);
mfiore 0:bb4e812f7c97 480
mfiore 0:bb4e812f7c97 481 s32_t spiffs_obj_lu_find_entry_visitor(
mfiore 0:bb4e812f7c97 482 spiffs *fs,
mfiore 0:bb4e812f7c97 483 spiffs_block_ix starting_block,
mfiore 0:bb4e812f7c97 484 int starting_lu_entry,
mfiore 0:bb4e812f7c97 485 u8_t flags,
mfiore 0:bb4e812f7c97 486 spiffs_obj_id obj_id,
mfiore 0:bb4e812f7c97 487 spiffs_visitor_f v,
mfiore 0:bb4e812f7c97 488 u32_t user_data,
mfiore 0:bb4e812f7c97 489 void *user_p,
mfiore 0:bb4e812f7c97 490 spiffs_block_ix *block_ix,
mfiore 0:bb4e812f7c97 491 int *lu_entry);
mfiore 0:bb4e812f7c97 492
mfiore 0:bb4e812f7c97 493 // ---------------
mfiore 0:bb4e812f7c97 494
mfiore 0:bb4e812f7c97 495 s32_t spiffs_obj_lu_scan(
mfiore 0:bb4e812f7c97 496 spiffs *fs);
mfiore 0:bb4e812f7c97 497
mfiore 0:bb4e812f7c97 498 s32_t spiffs_obj_lu_find_free_obj_id(
mfiore 0:bb4e812f7c97 499 spiffs *fs,
mfiore 0:bb4e812f7c97 500 spiffs_obj_id *obj_id);
mfiore 0:bb4e812f7c97 501
mfiore 0:bb4e812f7c97 502 s32_t spiffs_obj_lu_find_free(
mfiore 0:bb4e812f7c97 503 spiffs *fs,
mfiore 0:bb4e812f7c97 504 spiffs_block_ix starting_block,
mfiore 0:bb4e812f7c97 505 int starting_lu_entry,
mfiore 0:bb4e812f7c97 506 spiffs_block_ix *block_ix,
mfiore 0:bb4e812f7c97 507 int *lu_entry);
mfiore 0:bb4e812f7c97 508
mfiore 0:bb4e812f7c97 509 s32_t spiffs_obj_lu_find_id(
mfiore 0:bb4e812f7c97 510 spiffs *fs,
mfiore 0:bb4e812f7c97 511 spiffs_block_ix starting_block,
mfiore 0:bb4e812f7c97 512 int starting_lu_entry,
mfiore 0:bb4e812f7c97 513 spiffs_obj_id obj_id,
mfiore 0:bb4e812f7c97 514 spiffs_block_ix *block_ix,
mfiore 0:bb4e812f7c97 515 int *lu_entry);
mfiore 0:bb4e812f7c97 516
mfiore 0:bb4e812f7c97 517 s32_t spiffs_obj_lu_find_id_and_span(
mfiore 0:bb4e812f7c97 518 spiffs *fs,
mfiore 0:bb4e812f7c97 519 spiffs_obj_id obj_id,
mfiore 0:bb4e812f7c97 520 spiffs_span_ix spix,
mfiore 0:bb4e812f7c97 521 spiffs_page_ix exclusion_pix,
mfiore 0:bb4e812f7c97 522 spiffs_page_ix *pix);
mfiore 0:bb4e812f7c97 523
mfiore 0:bb4e812f7c97 524 s32_t spiffs_obj_lu_find_id_and_span_by_phdr(
mfiore 0:bb4e812f7c97 525 spiffs *fs,
mfiore 0:bb4e812f7c97 526 spiffs_obj_id obj_id,
mfiore 0:bb4e812f7c97 527 spiffs_span_ix spix,
mfiore 0:bb4e812f7c97 528 spiffs_page_ix exclusion_pix,
mfiore 0:bb4e812f7c97 529 spiffs_page_ix *pix);
mfiore 0:bb4e812f7c97 530
mfiore 0:bb4e812f7c97 531 // ---------------
mfiore 0:bb4e812f7c97 532
mfiore 0:bb4e812f7c97 533 s32_t spiffs_page_allocate_data(
mfiore 0:bb4e812f7c97 534 spiffs *fs,
mfiore 0:bb4e812f7c97 535 spiffs_obj_id obj_id,
mfiore 0:bb4e812f7c97 536 spiffs_page_header *ph,
mfiore 0:bb4e812f7c97 537 u8_t *data,
mfiore 0:bb4e812f7c97 538 u32_t len,
mfiore 0:bb4e812f7c97 539 u32_t page_offs,
mfiore 0:bb4e812f7c97 540 u8_t finalize,
mfiore 0:bb4e812f7c97 541 spiffs_page_ix *pix);
mfiore 0:bb4e812f7c97 542
mfiore 0:bb4e812f7c97 543 s32_t spiffs_page_move(
mfiore 0:bb4e812f7c97 544 spiffs *fs,
mfiore 0:bb4e812f7c97 545 spiffs_file fh,
mfiore 0:bb4e812f7c97 546 u8_t *page_data,
mfiore 0:bb4e812f7c97 547 spiffs_obj_id obj_id,
mfiore 0:bb4e812f7c97 548 spiffs_page_header *page_hdr,
mfiore 0:bb4e812f7c97 549 spiffs_page_ix src_pix,
mfiore 0:bb4e812f7c97 550 spiffs_page_ix *dst_pix);
mfiore 0:bb4e812f7c97 551
mfiore 0:bb4e812f7c97 552 s32_t spiffs_page_delete(
mfiore 0:bb4e812f7c97 553 spiffs *fs,
mfiore 0:bb4e812f7c97 554 spiffs_page_ix pix);
mfiore 0:bb4e812f7c97 555
mfiore 0:bb4e812f7c97 556 // ---------------
mfiore 0:bb4e812f7c97 557
mfiore 0:bb4e812f7c97 558 s32_t spiffs_object_create(
mfiore 0:bb4e812f7c97 559 spiffs *fs,
mfiore 0:bb4e812f7c97 560 spiffs_obj_id obj_id,
mfiore 0:bb4e812f7c97 561 u8_t name[SPIFFS_OBJ_NAME_LEN],
mfiore 0:bb4e812f7c97 562 spiffs_obj_type type,
mfiore 0:bb4e812f7c97 563 spiffs_page_ix *objix_hdr_pix);
mfiore 0:bb4e812f7c97 564
mfiore 0:bb4e812f7c97 565 s32_t spiffs_object_update_index_hdr(
mfiore 0:bb4e812f7c97 566 spiffs *fs,
mfiore 0:bb4e812f7c97 567 spiffs_fd *fd,
mfiore 0:bb4e812f7c97 568 spiffs_obj_id obj_id,
mfiore 0:bb4e812f7c97 569 spiffs_page_ix objix_hdr_pix,
mfiore 0:bb4e812f7c97 570 u8_t *new_objix_hdr_data,
mfiore 0:bb4e812f7c97 571 u8_t name[SPIFFS_OBJ_NAME_LEN],
mfiore 0:bb4e812f7c97 572 u32_t size,
mfiore 0:bb4e812f7c97 573 spiffs_page_ix *new_pix);
mfiore 0:bb4e812f7c97 574
mfiore 0:bb4e812f7c97 575 void spiffs_cb_object_event(
mfiore 0:bb4e812f7c97 576 spiffs *fs,
mfiore 0:bb4e812f7c97 577 spiffs_fd *fd,
mfiore 0:bb4e812f7c97 578 int ev,
mfiore 0:bb4e812f7c97 579 spiffs_obj_id obj_id,
mfiore 0:bb4e812f7c97 580 spiffs_span_ix spix,
mfiore 0:bb4e812f7c97 581 spiffs_page_ix new_pix,
mfiore 0:bb4e812f7c97 582 u32_t new_size);
mfiore 0:bb4e812f7c97 583
mfiore 0:bb4e812f7c97 584 s32_t spiffs_object_open_by_id(
mfiore 0:bb4e812f7c97 585 spiffs *fs,
mfiore 0:bb4e812f7c97 586 spiffs_obj_id obj_id,
mfiore 0:bb4e812f7c97 587 spiffs_fd *f,
mfiore 0:bb4e812f7c97 588 spiffs_flags flags,
mfiore 0:bb4e812f7c97 589 spiffs_mode mode);
mfiore 0:bb4e812f7c97 590
mfiore 0:bb4e812f7c97 591 s32_t spiffs_object_open_by_page(
mfiore 0:bb4e812f7c97 592 spiffs *fs,
mfiore 0:bb4e812f7c97 593 spiffs_page_ix pix,
mfiore 0:bb4e812f7c97 594 spiffs_fd *f,
mfiore 0:bb4e812f7c97 595 spiffs_flags flags,
mfiore 0:bb4e812f7c97 596 spiffs_mode mode);
mfiore 0:bb4e812f7c97 597
mfiore 0:bb4e812f7c97 598 s32_t spiffs_object_append(
mfiore 0:bb4e812f7c97 599 spiffs_fd *fd,
mfiore 0:bb4e812f7c97 600 u32_t offset,
mfiore 0:bb4e812f7c97 601 u8_t *data,
mfiore 0:bb4e812f7c97 602 u32_t len);
mfiore 0:bb4e812f7c97 603
mfiore 0:bb4e812f7c97 604 s32_t spiffs_object_modify(
mfiore 0:bb4e812f7c97 605 spiffs_fd *fd,
mfiore 0:bb4e812f7c97 606 u32_t offset,
mfiore 0:bb4e812f7c97 607 u8_t *data,
mfiore 0:bb4e812f7c97 608 u32_t len);
mfiore 0:bb4e812f7c97 609
mfiore 0:bb4e812f7c97 610 s32_t spiffs_object_read(
mfiore 0:bb4e812f7c97 611 spiffs_fd *fd,
mfiore 0:bb4e812f7c97 612 u32_t offset,
mfiore 0:bb4e812f7c97 613 u32_t len,
mfiore 0:bb4e812f7c97 614 u8_t *dst);
mfiore 0:bb4e812f7c97 615
mfiore 0:bb4e812f7c97 616 s32_t spiffs_object_truncate(
mfiore 0:bb4e812f7c97 617 spiffs_fd *fd,
mfiore 0:bb4e812f7c97 618 u32_t new_len,
mfiore 0:bb4e812f7c97 619 u8_t remove_object);
mfiore 0:bb4e812f7c97 620
mfiore 0:bb4e812f7c97 621 s32_t spiffs_object_find_object_index_header_by_name(
mfiore 0:bb4e812f7c97 622 spiffs *fs,
mfiore 0:bb4e812f7c97 623 u8_t name[SPIFFS_OBJ_NAME_LEN],
mfiore 0:bb4e812f7c97 624 spiffs_page_ix *pix);
mfiore 0:bb4e812f7c97 625
mfiore 0:bb4e812f7c97 626 // ---------------
mfiore 0:bb4e812f7c97 627
mfiore 0:bb4e812f7c97 628 s32_t spiffs_gc_check(
mfiore 0:bb4e812f7c97 629 spiffs *fs,
mfiore 0:bb4e812f7c97 630 u32_t len);
mfiore 0:bb4e812f7c97 631
mfiore 0:bb4e812f7c97 632 s32_t spiffs_gc_erase_page_stats(
mfiore 0:bb4e812f7c97 633 spiffs *fs,
mfiore 0:bb4e812f7c97 634 spiffs_block_ix bix);
mfiore 0:bb4e812f7c97 635
mfiore 0:bb4e812f7c97 636 s32_t spiffs_gc_find_candidate(
mfiore 0:bb4e812f7c97 637 spiffs *fs,
mfiore 0:bb4e812f7c97 638 spiffs_block_ix **block_candidate,
mfiore 0:bb4e812f7c97 639 int *candidate_count);
mfiore 0:bb4e812f7c97 640
mfiore 0:bb4e812f7c97 641 s32_t spiffs_gc_clean(
mfiore 0:bb4e812f7c97 642 spiffs *fs,
mfiore 0:bb4e812f7c97 643 spiffs_block_ix bix);
mfiore 0:bb4e812f7c97 644
mfiore 0:bb4e812f7c97 645 s32_t spiffs_gc_quick(
mfiore 0:bb4e812f7c97 646 spiffs *fs);
mfiore 0:bb4e812f7c97 647
mfiore 0:bb4e812f7c97 648 // ---------------
mfiore 0:bb4e812f7c97 649
mfiore 0:bb4e812f7c97 650 s32_t spiffs_fd_find_new(
mfiore 0:bb4e812f7c97 651 spiffs *fs,
mfiore 0:bb4e812f7c97 652 spiffs_fd **fd);
mfiore 0:bb4e812f7c97 653
mfiore 0:bb4e812f7c97 654 s32_t spiffs_fd_return(
mfiore 0:bb4e812f7c97 655 spiffs *fs,
mfiore 0:bb4e812f7c97 656 spiffs_file f);
mfiore 0:bb4e812f7c97 657
mfiore 0:bb4e812f7c97 658 s32_t spiffs_fd_get(
mfiore 0:bb4e812f7c97 659 spiffs *fs,
mfiore 0:bb4e812f7c97 660 spiffs_file f,
mfiore 0:bb4e812f7c97 661 spiffs_fd **fd);
mfiore 0:bb4e812f7c97 662
mfiore 0:bb4e812f7c97 663 #if SPIFFS_CACHE
mfiore 0:bb4e812f7c97 664 void spiffs_cache_init(
mfiore 0:bb4e812f7c97 665 spiffs *fs);
mfiore 0:bb4e812f7c97 666
mfiore 0:bb4e812f7c97 667 void spiffs_cache_drop_page(
mfiore 0:bb4e812f7c97 668 spiffs *fs,
mfiore 0:bb4e812f7c97 669 spiffs_page_ix pix);
mfiore 0:bb4e812f7c97 670
mfiore 0:bb4e812f7c97 671 #if SPIFFS_CACHE_WR
mfiore 0:bb4e812f7c97 672 spiffs_cache_page *spiffs_cache_page_allocate_by_fd(
mfiore 0:bb4e812f7c97 673 spiffs *fs,
mfiore 0:bb4e812f7c97 674 spiffs_fd *fd);
mfiore 0:bb4e812f7c97 675
mfiore 0:bb4e812f7c97 676 void spiffs_cache_fd_release(
mfiore 0:bb4e812f7c97 677 spiffs *fs,
mfiore 0:bb4e812f7c97 678 spiffs_cache_page *cp);
mfiore 0:bb4e812f7c97 679
mfiore 0:bb4e812f7c97 680 spiffs_cache_page *spiffs_cache_page_get_by_fd(
mfiore 0:bb4e812f7c97 681 spiffs *fs,
mfiore 0:bb4e812f7c97 682 spiffs_fd *fd);
mfiore 0:bb4e812f7c97 683 #endif
mfiore 0:bb4e812f7c97 684 #endif
mfiore 0:bb4e812f7c97 685
mfiore 0:bb4e812f7c97 686 s32_t spiffs_lookup_consistency_check(
mfiore 0:bb4e812f7c97 687 spiffs *fs,
mfiore 0:bb4e812f7c97 688 u8_t check_all_objects);
mfiore 0:bb4e812f7c97 689
mfiore 0:bb4e812f7c97 690 s32_t spiffs_page_consistency_check(
mfiore 0:bb4e812f7c97 691 spiffs *fs);
mfiore 0:bb4e812f7c97 692
mfiore 0:bb4e812f7c97 693 s32_t spiffs_object_index_consistency_check(
mfiore 0:bb4e812f7c97 694 spiffs *fs);
mfiore 0:bb4e812f7c97 695
Mike Fiore 1:ea3b983425c8 696 #endif /* SPIFFS_NUCLEUS_H_ */