spiffs_config.h
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2013-2016 Peter Andersson (pelleplutt1976<at>gmail.com)
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of
7  * this software and associated documentation files (the "Software"), to deal in
8  * the Software without restriction, including without limitation the rights to
9  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10  * the Software, and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #pragma once
25 
26 /*
27  * spiffs_config.h
28  *
29  * Created on: Jul 3, 2013
30  * Author: petera
31  */
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 #include "board.h"
38 
39 // ----------- 8< ------------
40 // Following includes are for the linux test build of spiffs
41 // These may/should/must be removed/altered/replaced in your target
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <stddef.h>
46 #include <unistd.h>
47 #include <stdint.h>
48 
49 // ----------- >8 ------------
50 
51 // compile time switches
52 
53 // Set generic spiffs debug output call.
54 #ifndef SPIFFS_DBG
55 #define SPIFFS_DBG(...) //printf(_f, ## __VA_ARGS__)
56 #endif
57 // Set spiffs debug output call for garbage collecting.
58 #ifndef SPIFFS_GC_DBG
59 #define SPIFFS_GC_DBG(...) //printf(_f, ## __VA_ARGS__)
60 #endif
61 // Set spiffs debug output call for caching.
62 #ifndef SPIFFS_CACHE_DBG
63 #define SPIFFS_CACHE_DBG(...) //printf(_f, ## __VA_ARGS__)
64 #endif
65 // Set spiffs debug output call for system consistency checks.
66 #ifndef SPIFFS_CHECK_DBG
67 #define SPIFFS_CHECK_DBG(...) //printf(_f, ## __VA_ARGS__)
68 #endif
69 // Set spiffs debug output call for all api invocations.
70 #ifndef SPIFFS_API_DBG
71 #define SPIFFS_API_DBG(...) //printf(_f, ## __VA_ARGS__)
72 #endif
73 
74 // Defines spiffs debug print formatters
75 // some general signed number
76 #ifndef _SPIPRIi
77 #define _SPIPRIi "%d"
78 #endif
79 // address
80 #ifndef _SPIPRIad
81 #define _SPIPRIad "%08x"
82 #endif
83 // block
84 #ifndef _SPIPRIbl
85 #define _SPIPRIbl "%04x"
86 #endif
87 // page
88 #ifndef _SPIPRIpg
89 #define _SPIPRIpg "%04x"
90 #endif
91 // span index
92 #ifndef _SPIPRIsp
93 #define _SPIPRIsp "%04x"
94 #endif
95 // file descriptor
96 #ifndef _SPIPRIfd
97 #define _SPIPRIfd "%d"
98 #endif
99 // file object id
100 #ifndef _SPIPRIid
101 #define _SPIPRIid "%04x"
102 #endif
103 // file flags
104 #ifndef _SPIPRIfl
105 #define _SPIPRIfl "%02x"
106 #endif
107 
108 // Enable/disable API functions to determine exact number of bytes
109 // for filedescriptor and cache buffers. Once decided for a configuration,
110 // this can be disabled to reduce flash.
111 #ifndef SPIFFS_BUFFER_HELP
112 #define SPIFFS_BUFFER_HELP 0
113 #endif
114 
115 // Enables/disable memory read caching of nucleus file system operations.
116 // If enabled, memory area must be provided for cache in SPIFFS_mount.
117 #ifndef SPIFFS_CACHE
118 #define SPIFFS_CACHE 1
119 #endif
120 #if SPIFFS_CACHE
121 // Enables memory write caching for file descriptors in hydrogen
122 #ifndef SPIFFS_CACHE_WR
123 #define SPIFFS_CACHE_WR 1
124 #endif
125 
126 // Enable/disable statistics on caching. Debug/test purpose only.
127 #ifndef SPIFFS_CACHE_STATS
128 #define SPIFFS_CACHE_STATS 1
129 #endif
130 #endif
131 
132 // Always check header of each accessed page to ensure consistent state.
133 // If enabled it will increase number of reads, will increase flash.
134 #ifndef SPIFFS_PAGE_CHECK
135 #define SPIFFS_PAGE_CHECK 1
136 #endif
137 
138 // Define maximum number of gc runs to perform to reach desired free pages.
139 #ifndef SPIFFS_GC_MAX_RUNS
140 #define SPIFFS_GC_MAX_RUNS 5
141 #endif
142 
143 // Enable/disable statistics on gc. Debug/test purpose only.
144 #ifndef SPIFFS_GC_STATS
145 #define SPIFFS_GC_STATS 1
146 #endif
147 
148 // Garbage collecting examines all pages in a block which and sums up
149 // to a block score. Deleted pages normally gives positive score and
150 // used pages normally gives a negative score (as these must be moved).
151 // To have a fair wear-leveling, the erase age is also included in score,
152 // whose factor normally is the most positive.
153 // The larger the score, the more likely it is that the block will
154 // picked for garbage collection.
155 
156 // Garbage collecting heuristics - weight used for deleted pages.
157 #ifndef SPIFFS_GC_HEUR_W_DELET
158 #define SPIFFS_GC_HEUR_W_DELET (5)
159 #endif
160 // Garbage collecting heuristics - weight used for used pages.
161 #ifndef SPIFFS_GC_HEUR_W_USED
162 #define SPIFFS_GC_HEUR_W_USED (-1)
163 #endif
164 // Garbage collecting heuristics - weight used for time between
165 // last erased and erase of this block.
166 #ifndef SPIFFS_GC_HEUR_W_ERASE_AGE
167 #define SPIFFS_GC_HEUR_W_ERASE_AGE (50)
168 #endif
169 
170 // Object name maximum length. Note that this length include the
171 // zero-termination character, meaning maximum string of characters
172 // can at most be SPIFFS_OBJ_NAME_LEN - 1.
173 #ifndef SPIFFS_OBJ_NAME_LEN
174 #define SPIFFS_OBJ_NAME_LEN (32)
175 #endif
176 
177 // Maximum length of the metadata associated with an object.
178 // Setting to non-zero value enables metadata-related API but also
179 // changes the on-disk format, so the change is not backward-compatible.
180 //
181 // Do note: the meta length must never exceed
182 // logical_page_size - (SPIFFS_OBJ_NAME_LEN + 64)
183 //
184 // This is derived from following:
185 // logical_page_size - (SPIFFS_OBJ_NAME_LEN + sizeof(spiffs_page_header) +
186 // spiffs_object_ix_header fields + at least some LUT entries)
187 #ifndef SPIFFS_OBJ_META_LEN
188 #define SPIFFS_OBJ_META_LEN (0)
189 #endif
190 
191 // Size of buffer allocated on stack used when copying data.
192 // Lower value generates more read/writes. No meaning having it bigger
193 // than logical page size.
194 #ifndef SPIFFS_COPY_BUFFER_STACK
195 #define SPIFFS_COPY_BUFFER_STACK (64)
196 #endif
197 
198 // Enable this to have an identifiable spiffs filesystem. This will look for
199 // a magic in all sectors to determine if this is a valid spiffs system or
200 // not on mount point. If not, SPIFFS_format must be called prior to mounting
201 // again.
202 #ifndef SPIFFS_USE_MAGIC
203 #define SPIFFS_USE_MAGIC (1)
204 #endif
205 
206 #if SPIFFS_USE_MAGIC
207 // Only valid when SPIFFS_USE_MAGIC is enabled. If SPIFFS_USE_MAGIC_LENGTH is
208 // enabled, the magic will also be dependent on the length of the filesystem.
209 // For example, a filesystem configured and formatted for 4 megabytes will not
210 // be accepted for mounting with a configuration defining the filesystem as 2
211 // megabytes.
212 #ifndef SPIFFS_USE_MAGIC_LENGTH
213 #define SPIFFS_USE_MAGIC_LENGTH (0)
214 #endif
215 #endif
216 
217 // SPIFFS_LOCK and SPIFFS_UNLOCK protects spiffs from reentrancy on api level
218 // These should be defined on a multithreaded system
219 
220 struct spiffs_t;
221 void spiffs_lock(struct spiffs_t *fs);
222 void spiffs_unlock(struct spiffs_t *fs);
223 
224 // define this to enter a mutex if you're running on a multithreaded system
225 #ifndef SPIFFS_LOCK
226 #define SPIFFS_LOCK(fs) spiffs_lock(fs)
227 #endif
228 // define this to exit a mutex if you're running on a multithreaded system
229 #ifndef SPIFFS_UNLOCK
230 #define SPIFFS_UNLOCK(fs) spiffs_unlock(fs)
231 #endif
232 
233 // Enable if only one spiffs instance with constant configuration will exist
234 // on the target. This will reduce calculations, flash and memory accesses.
235 // Parts of configuration must be defined below instead of at time of mount.
236 #ifndef SPIFFS_SINGLETON
237 #define SPIFFS_SINGLETON 0
238 #endif
239 
240 #if SPIFFS_SINGLETON
241 // Instead of giving parameters in config struct, singleton build must
242 // give parameters in defines below.
243 #ifndef SPIFFS_CFG_PHYS_SZ
244 #define SPIFFS_CFG_PHYS_SZ(ignore) (1024*1024*2)
245 #endif
246 #ifndef SPIFFS_CFG_PHYS_ERASE_SZ
247 #define SPIFFS_CFG_PHYS_ERASE_SZ(ignore) (65536)
248 #endif
249 #ifndef SPIFFS_CFG_PHYS_ADDR
250 #define SPIFFS_CFG_PHYS_ADDR(ignore) (0)
251 #endif
252 #ifndef SPIFFS_CFG_LOG_PAGE_SZ
253 #define SPIFFS_CFG_LOG_PAGE_SZ(ignore) (256)
254 #endif
255 #ifndef SPIFFS_CFG_LOG_BLOCK_SZ
256 #define SPIFFS_CFG_LOG_BLOCK_SZ(ignore) (65536)
257 #endif
258 #endif
259 
260 // Enable this if your target needs aligned data for index tables
261 #ifndef SPIFFS_ALIGNED_OBJECT_INDEX_TABLES
262 #define SPIFFS_ALIGNED_OBJECT_INDEX_TABLES 1
263 #endif
264 
265 // Enable this if you want the HAL callbacks to be called with the spiffs struct
266 #ifndef SPIFFS_HAL_CALLBACK_EXTRA
267 #define SPIFFS_HAL_CALLBACK_EXTRA 1
268 #endif
269 
270 // Enable this if you want to add an integer offset to all file handles
271 // (spiffs_file). This is useful if running multiple instances of spiffs on
272 // same target, in order to recognise to what spiffs instance a file handle
273 // belongs.
274 // NB: This adds config field fh_ix_offset in the configuration struct when
275 // mounting, which must be defined.
276 #ifndef SPIFFS_FILEHDL_OFFSET
277 #define SPIFFS_FILEHDL_OFFSET 0
278 #endif
279 
280 // Enable this to compile a read only version of spiffs.
281 // This will reduce binary size of spiffs. All code comprising modification
282 // of the file system will not be compiled. Some config will be ignored.
283 // HAL functions for erasing and writing to spi-flash may be null. Cache
284 // can be disabled for even further binary size reduction (and ram savings).
285 // Functions modifying the fs will return SPIFFS_ERR_RO_NOT_IMPL.
286 // If the file system cannot be mounted due to aborted erase operation and
287 // SPIFFS_USE_MAGIC is enabled, SPIFFS_ERR_RO_ABORTED_OPERATION will be
288 // returned.
289 // Might be useful for e.g. bootloaders and such.
290 #ifndef SPIFFS_READ_ONLY
291 #define SPIFFS_READ_ONLY 0
292 #endif
293 
294 // Set SPIFFS_TEST_VISUALISATION to non-zero to enable SPIFFS_vis function
295 // in the api. This function will visualize all filesystem using given printf
296 // function.
297 #ifndef SPIFFS_TEST_VISUALISATION
298 #define SPIFFS_TEST_VISUALISATION 0
299 #endif
300 #if SPIFFS_TEST_VISUALISATION
301 #ifndef spiffs_printf
302 #define spiffs_printf(...) DEBUG(__VA_ARGS__)
303 #endif
304 // spiffs_printf argument for a free page
305 #ifndef SPIFFS_TEST_VIS_FREE_STR
306 #define SPIFFS_TEST_VIS_FREE_STR "_"
307 #endif
308 // spiffs_printf argument for a deleted page
309 #ifndef SPIFFS_TEST_VIS_DELE_STR
310 #define SPIFFS_TEST_VIS_DELE_STR "/"
311 #endif
312 // spiffs_printf argument for an index page for given object id
313 #ifndef SPIFFS_TEST_VIS_INDX_STR
314 #define SPIFFS_TEST_VIS_INDX_STR(id) "i"
315 #endif
316 // spiffs_printf argument for a data page for given object id
317 #ifndef SPIFFS_TEST_VIS_DATA_STR
318 #define SPIFFS_TEST_VIS_DATA_STR(id) "d"
319 #endif
320 #endif
321 
322 // Types depending on configuration such as the amount of flash bytes
323 // given to spiffs file system in total (spiffs_file_system_size),
324 // the logical block size (log_block_size), and the logical page size
325 // (log_page_size)
326 
327 // Block index type. Make sure the size of this type can hold
328 // the highest number of all blocks - i.e. spiffs_file_system_size / log_block_size
329 typedef uint16_t spiffs_block_ix;
330 // Page index type. Make sure the size of this type can hold
331 // the highest page number of all pages - i.e. spiffs_file_system_size / log_page_size
332 typedef uint16_t spiffs_page_ix;
333 // Object id type - most significant bit is reserved for index flag. Make sure the
334 // size of this type can hold the highest object id on a full system,
335 // i.e. 2 + (spiffs_file_system_size / (2*log_page_size))*2
336 typedef uint16_t spiffs_obj_id;
337 // Object span index type. Make sure the size of this type can
338 // hold the largest possible span index on the system -
339 // i.e. (spiffs_file_system_size / log_page_size) - 1
340 typedef uint16_t spiffs_span_ix;
341 
342 typedef uint8_t u8_t;
343 typedef uint32_t u32_t;
344 typedef int32_t s32_t;
345 typedef uint16_t u16_t;
346 typedef int16_t s16_t;
347 
348 #ifdef __cplusplus
349 }
350 #endif
void spiffs_lock(struct spiffs_t *fs)
SPIFFS lock function.
void spiffs_unlock(struct spiffs_t *fs)
SPIFFS unlock function.