SpiffsParticleRK
spiffs_config.h
1 /*
2  * spiffs_config.h
3  *
4  * Created on: Jul 3, 2013
5  * Author: petera
6  */
7 
8 #ifndef SPIFFS_CONFIG_H_
9 #define SPIFFS_CONFIG_H_
10 
11 // ----------- 8< ------------
12 // Following includes are for the linux test build of spiffs
13 // These may/should/must be removed/altered/replaced in your target
14 
15 //#include "params_test.h"
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stddef.h>
20 #include <unistd.h>
21 
22 #ifdef _SPIFFS_TEST
23 #include "testrunner.h"
24 #endif
25 
26 // ----------- >8 ------------
27 
28 typedef int8_t s8_t;
29 typedef uint8_t u8_t;
30 typedef int16_t s16_t;
31 typedef uint16_t u16_t;
32 typedef int32_t s32_t;
33 typedef uint32_t u32_t;
34 
35 
36 #if defined(__cplusplus)
37 extern "C" {
38 #endif
39 
40 void spiffsParticleInfoLog(const char *fmt, ...);
41 void spiffsParticleTraceLog(const char *fmt, ...);
42 
43 void spiffsParticleLock();
44 void spiffsParticleUnlock();
45 
46 #if defined(__cplusplus)
47 };
48 #endif
49 
50 // compile time switches
51 
52 // Set generic spiffs debug output call.
53 #ifndef SPIFFS_DBG
54 #define SPIFFS_DBG(_f, ...) spiffsParticleInfoLog(_f, ## __VA_ARGS__)
55 #endif
56 // Set spiffs debug output call for garbage collecting.
57 #ifndef SPIFFS_GC_DBG
58 #define SPIFFS_GC_DBG(_f, ...) spiffsParticleTraceLog(_f, ## __VA_ARGS__)
59 #endif
60 // Set spiffs debug output call for caching.
61 #ifndef SPIFFS_CACHE_DBG
62 #define SPIFFS_CACHE_DBG(_f, ...) // spiffsParticleTraceLog(_f, ## __VA_ARGS__)
63 #endif
64 // Set spiffs debug output call for system consistency checks.
65 #ifndef SPIFFS_CHECK_DBG
66 #define SPIFFS_CHECK_DBG(_f, ...) spiffsParticleTraceLog(_f, ## __VA_ARGS__)
67 #endif
68 // Set spiffs debug output call for all api invocations.
69 #ifndef SPIFFS_API_DBG
70 #define SPIFFS_API_DBG(_f, ...) spiffsParticleTraceLog(_f, ## __VA_ARGS__)
71 #endif
72 
73 
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 
110 // Enable/disable API functions to determine exact number of bytes
111 // for filedescriptor and cache buffers. Once decided for a configuration,
112 // this can be disabled to reduce flash.
113 #ifndef SPIFFS_BUFFER_HELP
114 #define SPIFFS_BUFFER_HELP 0
115 #endif
116 
117 // Enables/disable memory read caching of nucleus file system operations.
118 // If enabled, memory area must be provided for cache in SPIFFS_mount.
119 #ifndef SPIFFS_CACHE
120 #define SPIFFS_CACHE 1
121 #endif
122 #if SPIFFS_CACHE
123 // Enables memory write caching for file descriptors in hydrogen
124 #ifndef SPIFFS_CACHE_WR
125 #define SPIFFS_CACHE_WR 1
126 #endif
127 
128 // Enable/disable statistics on caching. Debug/test purpose only.
129 #ifndef SPIFFS_CACHE_STATS
130 #define SPIFFS_CACHE_STATS 1
131 #endif
132 #endif
133 
134 // Always check header of each accessed page to ensure consistent state.
135 // If enabled it will increase number of reads, will increase flash.
136 #ifndef SPIFFS_PAGE_CHECK
137 #define SPIFFS_PAGE_CHECK 1
138 #endif
139 
140 // Define maximum number of gc runs to perform to reach desired free pages.
141 #ifndef SPIFFS_GC_MAX_RUNS
142 #define SPIFFS_GC_MAX_RUNS 5
143 #endif
144 
145 // Enable/disable statistics on gc. Debug/test purpose only.
146 #ifndef SPIFFS_GC_STATS
147 #define SPIFFS_GC_STATS 1
148 #endif
149 
150 // Garbage collecting examines all pages in a block which and sums up
151 // to a block score. Deleted pages normally gives positive score and
152 // used pages normally gives a negative score (as these must be moved).
153 // To have a fair wear-leveling, the erase age is also included in score,
154 // whose factor normally is the most positive.
155 // The larger the score, the more likely it is that the block will
156 // picked for garbage collection.
157 
158 // Garbage collecting heuristics - weight used for deleted pages.
159 #ifndef SPIFFS_GC_HEUR_W_DELET
160 #define SPIFFS_GC_HEUR_W_DELET (5)
161 #endif
162 // Garbage collecting heuristics - weight used for used pages.
163 #ifndef SPIFFS_GC_HEUR_W_USED
164 #define SPIFFS_GC_HEUR_W_USED (-1)
165 #endif
166 // Garbage collecting heuristics - weight used for time between
167 // last erased and erase of this block.
168 #ifndef SPIFFS_GC_HEUR_W_ERASE_AGE
169 #define SPIFFS_GC_HEUR_W_ERASE_AGE (50)
170 #endif
171 
172 // Object name maximum length. Note that this length include the
173 // zero-termination character, meaning maximum string of characters
174 // can at most be SPIFFS_OBJ_NAME_LEN - 1.
175 #ifndef SPIFFS_OBJ_NAME_LEN
176 #define SPIFFS_OBJ_NAME_LEN (32)
177 #endif
178 
179 // Maximum length of the metadata associated with an object.
180 // Setting to non-zero value enables metadata-related API but also
181 // changes the on-disk format, so the change is not backward-compatible.
182 //
183 // Do note: the meta length must never exceed
184 // logical_page_size - (SPIFFS_OBJ_NAME_LEN + 64)
185 //
186 // This is derived from following:
187 // logical_page_size - (SPIFFS_OBJ_NAME_LEN + sizeof(spiffs_page_header) +
188 // spiffs_object_ix_header fields + at least some LUT entries)
189 #ifndef SPIFFS_OBJ_META_LEN
190 #define SPIFFS_OBJ_META_LEN (0)
191 #endif
192 
193 // Size of buffer allocated on stack used when copying data.
194 // Lower value generates more read/writes. No meaning having it bigger
195 // than logical page size.
196 #ifndef SPIFFS_COPY_BUFFER_STACK
197 #define SPIFFS_COPY_BUFFER_STACK (64)
198 #endif
199 
200 // Enable this to have an identifiable spiffs filesystem. This will look for
201 // a magic in all sectors to determine if this is a valid spiffs system or
202 // not on mount point. If not, SPIFFS_format must be called prior to mounting
203 // again.
204 #ifndef SPIFFS_USE_MAGIC
205 #define SPIFFS_USE_MAGIC (1)
206 #endif
207 
208 #if SPIFFS_USE_MAGIC
209 // Only valid when SPIFFS_USE_MAGIC is enabled. If SPIFFS_USE_MAGIC_LENGTH is
210 // enabled, the magic will also be dependent on the length of the filesystem.
211 // For example, a filesystem configured and formatted for 4 megabytes will not
212 // be accepted for mounting with a configuration defining the filesystem as 2
213 // megabytes.
214 #ifndef SPIFFS_USE_MAGIC_LENGTH
215 #define SPIFFS_USE_MAGIC_LENGTH (1)
216 #endif
217 #endif
218 
219 // SPIFFS_LOCK and SPIFFS_UNLOCK protects spiffs from reentrancy on api level
220 // These should be defined on a multithreaded system
221 
222 // define this to enter a mutex if you're running on a multithreaded system
223 #ifndef SPIFFS_LOCK
224 #define SPIFFS_LOCK(fs) spiffsParticleLock()
225 #endif
226 // define this to exit a mutex if you're running on a multithreaded system
227 #ifndef SPIFFS_UNLOCK
228 #define SPIFFS_UNLOCK(fs) spiffsParticleUnlock()
229 #endif
230 
231 // Enable if only one spiffs instance with constant configuration will exist
232 // on the target. This will reduce calculations, flash and memory accesses.
233 // Parts of configuration must be defined below instead of at time of mount.
234 #ifndef SPIFFS_SINGLETON
235 #define SPIFFS_SINGLETON 0
236 #endif
237 
238 #if SPIFFS_SINGLETON
239 // Instead of giving parameters in config struct, singleton build must
240 // give parameters in defines below.
241 #ifndef SPIFFS_CFG_PHYS_SZ
242 #define SPIFFS_CFG_PHYS_SZ(ignore) (1024*1024*2)
243 #endif
244 #ifndef SPIFFS_CFG_PHYS_ERASE_SZ
245 #define SPIFFS_CFG_PHYS_ERASE_SZ(ignore) (65536)
246 #endif
247 #ifndef SPIFFS_CFG_PHYS_ADDR
248 #define SPIFFS_CFG_PHYS_ADDR(ignore) (0)
249 #endif
250 #ifndef SPIFFS_CFG_LOG_PAGE_SZ
251 #define SPIFFS_CFG_LOG_PAGE_SZ(ignore) (256)
252 #endif
253 #ifndef SPIFFS_CFG_LOG_BLOCK_SZ
254 #define SPIFFS_CFG_LOG_BLOCK_SZ(ignore) (65536)
255 #endif
256 #endif
257 
258 // Enable this if your target needs aligned data for index tables
259 #ifndef SPIFFS_ALIGNED_OBJECT_INDEX_TABLES
260 #define SPIFFS_ALIGNED_OBJECT_INDEX_TABLES 0
261 #endif
262 
263 // Enable this if you want the HAL callbacks to be called with the spiffs struct
264 #ifndef SPIFFS_HAL_CALLBACK_EXTRA
265 #define SPIFFS_HAL_CALLBACK_EXTRA 1
266 #endif
267 
268 // Enable this if you want to add an integer offset to all file handles
269 // (spiffs_file). This is useful if running multiple instances of spiffs on
270 // same target, in order to recognise to what spiffs instance a file handle
271 // belongs.
272 // NB: This adds config field fh_ix_offset in the configuration struct when
273 // mounting, which must be defined.
274 #ifndef SPIFFS_FILEHDL_OFFSET
275 #define SPIFFS_FILEHDL_OFFSET 0
276 #endif
277 
278 // Enable this to compile a read only version of spiffs.
279 // This will reduce binary size of spiffs. All code comprising modification
280 // of the file system will not be compiled. Some config will be ignored.
281 // HAL functions for erasing and writing to spi-flash may be null. Cache
282 // can be disabled for even further binary size reduction (and ram savings).
283 // Functions modifying the fs will return SPIFFS_ERR_RO_NOT_IMPL.
284 // If the file system cannot be mounted due to aborted erase operation and
285 // SPIFFS_USE_MAGIC is enabled, SPIFFS_ERR_RO_ABORTED_OPERATION will be
286 // returned.
287 // Might be useful for e.g. bootloaders and such.
288 #ifndef SPIFFS_READ_ONLY
289 #define SPIFFS_READ_ONLY 0
290 #endif
291 
292 // Enable this to add a temporal file cache using the fd buffer.
293 // The effects of the cache is that SPIFFS_open will find the file faster in
294 // certain cases. It will make it a lot easier for spiffs to find files
295 // opened frequently, reducing number of readings from the spi flash for
296 // finding those files.
297 // This will grow each fd by 6 bytes. If your files are opened in patterns
298 // with a degree of temporal locality, the system is optimized.
299 // Examples can be letting spiffs serve web content, where one file is the css.
300 // The css is accessed for each html file that is opened, meaning it is
301 // accessed almost every second time a file is opened. Another example could be
302 // a log file that is often opened, written, and closed.
303 // The size of the cache is number of given file descriptors, as it piggybacks
304 // on the fd update mechanism. The cache lives in the closed file descriptors.
305 // When closed, the fd know the whereabouts of the file. Instead of forgetting
306 // this, the temporal cache will keep handling updates to that file even if the
307 // fd is closed. If the file is opened again, the location of the file is found
308 // directly. If all available descriptors become opened, all cache memory is
309 // lost.
310 #ifndef SPIFFS_TEMPORAL_FD_CACHE
311 #define SPIFFS_TEMPORAL_FD_CACHE 1
312 #endif
313 
314 // Temporal file cache hit score. Each time a file is opened, all cached files
315 // will lose one point. If the opened file is found in cache, that entry will
316 // gain SPIFFS_TEMPORAL_CACHE_HIT_SCORE points. One can experiment with this
317 // value for the specific access patterns of the application. However, it must
318 // be between 1 (no gain for hitting a cached entry often) and 255.
319 #ifndef SPIFFS_TEMPORAL_CACHE_HIT_SCORE
320 #define SPIFFS_TEMPORAL_CACHE_HIT_SCORE 4
321 #endif
322 
323 // Enable to be able to map object indices to memory.
324 // This allows for faster and more deterministic reading if cases of reading
325 // large files and when changing file offset by seeking around a lot.
326 // When mapping a file's index, the file system will be scanned for index pages
327 // and the info will be put in memory provided by user. When reading, the
328 // memory map can be looked up instead of searching for index pages on the
329 // medium. This way, user can trade memory against performance.
330 // Whole, parts of, or future parts not being written yet can be mapped. The
331 // memory array will be owned by spiffs and updated accordingly during garbage
332 // collecting or when modifying the indices. The latter is invoked by when the
333 // file is modified in some way. The index buffer is tied to the file
334 // descriptor.
335 #ifndef SPIFFS_IX_MAP
336 #define SPIFFS_IX_MAP 1
337 #endif
338 
339 // By default SPIFFS in some cases relies on the property of NOR flash that bits
340 // cannot be set from 0 to 1 by writing and that controllers will ignore such
341 // bit changes. This results in fewer reads as SPIFFS can in some cases perform
342 // blind writes, with all bits set to 1 and only those it needs reset set to 0.
343 // Most of the chips and controllers allow this behavior, so the default is to
344 // use this technique. If your controller is one of the rare ones that don't,
345 // turn this option on and SPIFFS will perform a read-modify-write instead.
346 #ifndef SPIFFS_NO_BLIND_WRITES
347 #define SPIFFS_NO_BLIND_WRITES 0
348 #endif
349 
350 // Set SPIFFS_TEST_VISUALISATION to non-zero to enable SPIFFS_vis function
351 // in the api. This function will visualize all filesystem using given printf
352 // function.
353 #ifndef SPIFFS_TEST_VISUALISATION
354 #define SPIFFS_TEST_VISUALISATION 0
355 #endif
356 #if SPIFFS_TEST_VISUALISATION
357 #ifndef spiffs_printf
358 #define spiffs_printf(...) printf(__VA_ARGS__)
359 #endif
360 // spiffs_printf argument for a free page
361 #ifndef SPIFFS_TEST_VIS_FREE_STR
362 #define SPIFFS_TEST_VIS_FREE_STR "_"
363 #endif
364 // spiffs_printf argument for a deleted page
365 #ifndef SPIFFS_TEST_VIS_DELE_STR
366 #define SPIFFS_TEST_VIS_DELE_STR "/"
367 #endif
368 // spiffs_printf argument for an index page for given object id
369 #ifndef SPIFFS_TEST_VIS_INDX_STR
370 #define SPIFFS_TEST_VIS_INDX_STR(id) "i"
371 #endif
372 // spiffs_printf argument for a data page for given object id
373 #ifndef SPIFFS_TEST_VIS_DATA_STR
374 #define SPIFFS_TEST_VIS_DATA_STR(id) "d"
375 #endif
376 #endif
377 
378 // Types depending on configuration such as the amount of flash bytes
379 // given to spiffs file system in total (spiffs_file_system_size),
380 // the logical block size (log_block_size), and the logical page size
381 // (log_page_size)
382 
383 // Block index type. Make sure the size of this type can hold
384 // the highest number of all blocks - i.e. spiffs_file_system_size / log_block_size
385 typedef u16_t spiffs_block_ix;
386 // Page index type. Make sure the size of this type can hold
387 // the highest page number of all pages - i.e. spiffs_file_system_size / log_page_size
388 typedef u16_t spiffs_page_ix;
389 // Object id type - most significant bit is reserved for index flag. Make sure the
390 // size of this type can hold the highest object id on a full system,
391 // i.e. 2 + (spiffs_file_system_size / (2*log_page_size))*2
392 typedef u16_t spiffs_obj_id;
393 // Object span index type. Make sure the size of this type can
394 // hold the largest possible span index on the system -
395 // i.e. (spiffs_file_system_size / log_page_size) - 1
396 typedef u16_t spiffs_span_ix;
397 
398 #endif /* SPIFFS_CONFIG_H_ */