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