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 #include <stdio.h>
15 #include <stdint.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stddef.h>
19 #include <unistd.h>
20 #include <sdkconfig.h>
21 #include <esp_log.h>
22 #include <assert.h>
23 
24 // compile time switches
25 #define SPIFFS_TAG "SPIFFS"
26 
27 // Set generic spiffs debug output call.
28 #if CONFIG_SPIFFS_DBG
29 #define SPIFFS_DBG(...)             ESP_LOGD(SPIFFS_TAG, __VA_ARGS__)
30 #else
31 #define SPIFFS_DBG(...)
32 #endif
33 #if CONFIG_SPIFFS_API_DBG
34 #define SPIFFS_API_DBG(...)         ESP_LOGD(SPIFFS_TAG, __VA_ARGS__)
35 #else
36 #define SPIFFS_API_DBG(...)
37 #endif
38 #if CONFIG_SPIFFS_DBG
39 #define SPIFFS_GC_DBG(...)          ESP_LOGD(SPIFFS_TAG, __VA_ARGS__)
40 #else
41 #define SPIFFS_GC_DBG(...)
42 #endif
43 #if CONFIG_SPIFFS_CACHE_DBG
44 #define SPIFFS_CACHE_DBG(...)       ESP_LOGD(SPIFFS_TAG, __VA_ARGS__)
45 #else
46 #define SPIFFS_CACHE_DBG(...)
47 #endif
48 #if CONFIG_SPIFFS_CHECK_DBG
49 #define SPIFFS_CHECK_DBG(...)       ESP_LOGD(SPIFFS_TAG, __VA_ARGS__)
50 #else
51 #define SPIFFS_CHECK_DBG(...)
52 #endif
53 
54 // needed types
55 typedef int32_t s32_t;
56 typedef uint32_t u32_t;
57 typedef int16_t s16_t;
58 typedef uint16_t u16_t;
59 typedef int8_t s8_t;
60 typedef uint8_t u8_t;
61 
62 struct spiffs_t;
63 extern void spiffs_api_lock(struct spiffs_t *fs);
64 extern void spiffs_api_unlock(struct spiffs_t *fs);
65 
66 // Defines spiffs debug print formatters
67 // some general signed number
68 #define _SPIPRIi   "%d"
69 // address
70 #define _SPIPRIad  "%08x"
71 // block
72 #define _SPIPRIbl  "%04x"
73 // page
74 #define _SPIPRIpg  "%04x"
75 // span index
76 #define _SPIPRIsp  "%04x"
77 // file descriptor
78 #define _SPIPRIfd  "%d"
79 // file object id
80 #define _SPIPRIid  "%04x"
81 // file flags
82 #define _SPIPRIfl  "%02x"
83 
84 
85 // Enable/disable API functions to determine exact number of bytes
86 // for filedescriptor and cache buffers. Once decided for a configuration,
87 // this can be disabled to reduce flash.
88 #define SPIFFS_BUFFER_HELP              0
89 
90 // Enables/disable memory read caching of nucleus file system operations.
91 // If enabled, memory area must be provided for cache in SPIFFS_mount.
92 #ifdef CONFIG_SPIFFS_CACHE
93 #define SPIFFS_CACHE                (1)
94 #else
95 #define SPIFFS_CACHE                (0)
96 #endif
97 #if SPIFFS_CACHE
98 // Enables memory write caching for file descriptors in hydrogen
99 #ifdef CONFIG_SPIFFS_CACHE_WR
100 #define SPIFFS_CACHE_WR             (1)
101 #else
102 #define SPIFFS_CACHE_WR             (0)
103 #endif
104 
105 // Enable/disable statistics on caching. Debug/test purpose only.
106 #ifdef CONFIG_SPIFFS_CACHE_STATS
107 #define SPIFFS_CACHE_STATS          (1)
108 #else
109 #define SPIFFS_CACHE_STATS          (0)
110 #endif
111 #endif
112 
113 // Always check header of each accessed page to ensure consistent state.
114 // If enabled it will increase number of reads, will increase flash.
115 #ifdef CONFIG_SPIFFS_PAGE_CHECK
116 #define SPIFFS_PAGE_CHECK           (1)
117 #else
118 #define SPIFFS_PAGE_CHECK           (0)
119 #endif
120 
121 // Define maximum number of gc runs to perform to reach desired free pages.
122 #define SPIFFS_GC_MAX_RUNS              CONFIG_SPIFFS_GC_MAX_RUNS
123 
124 // Enable/disable statistics on gc. Debug/test purpose only.
125 #ifdef CONFIG_SPIFFS_GC_STATS
126 #define SPIFFS_GC_STATS             (1)
127 #else
128 #define SPIFFS_GC_STATS             (0)
129 #endif
130 
131 // Garbage collecting examines all pages in a block which and sums up
132 // to a block score. Deleted pages normally gives positive score and
133 // used pages normally gives a negative score (as these must be moved).
134 // To have a fair wear-leveling, the erase age is also included in score,
135 // whose factor normally is the most positive.
136 // The larger the score, the more likely it is that the block will
137 // picked for garbage collection.
138 
139 // Garbage collecting heuristics - weight used for deleted pages.
140 #define SPIFFS_GC_HEUR_W_DELET          (5)
141 // Garbage collecting heuristics - weight used for used pages.
142 #define SPIFFS_GC_HEUR_W_USED           (-1)
143 // Garbage collecting heuristics - weight used for time between
144 // last erased and erase of this block.
145 #define SPIFFS_GC_HEUR_W_ERASE_AGE      (50)
146 
147 // Object name maximum length. Note that this length include the
148 // zero-termination character, meaning maximum string of characters
149 // can at most be SPIFFS_OBJ_NAME_LEN - 1.
150 #define SPIFFS_OBJ_NAME_LEN             (CONFIG_SPIFFS_OBJ_NAME_LEN)
151 
152 // Maximum length of the metadata associated with an object.
153 // Setting to non-zero value enables metadata-related API but also
154 // changes the on-disk format, so the change is not backward-compatible.
155 //
156 // Do note: the meta length must never exceed
157 // logical_page_size - (SPIFFS_OBJ_NAME_LEN + SPIFFS_PAGE_EXTRA_SIZE)
158 //
159 // This is derived from following:
160 // logical_page_size - (SPIFFS_OBJ_NAME_LEN + sizeof(spiffs_page_header) +
161 // spiffs_object_ix_header fields + at least some LUT entries)
162 #define SPIFFS_OBJ_META_LEN             (CONFIG_SPIFFS_META_LENGTH)
163 #define SPIFFS_PAGE_EXTRA_SIZE          (64)
164 _Static_assert(SPIFFS_OBJ_META_LEN + SPIFFS_OBJ_NAME_LEN + SPIFFS_PAGE_EXTRA_SIZE
165         <= CONFIG_SPIFFS_PAGE_SIZE, "SPIFFS_OBJ_META_LEN or SPIFFS_OBJ_NAME_LEN too long");
166 
167 // Size of buffer allocated on stack used when copying data.
168 // Lower value generates more read/writes. No meaning having it bigger
169 // than logical page size.
170 #define SPIFFS_COPY_BUFFER_STACK        (256)
171 
172 // Enable this to have an identifiable spiffs filesystem. This will look for
173 // a magic in all sectors to determine if this is a valid spiffs system or
174 // not on mount point. If not, SPIFFS_format must be called prior to mounting
175 // again.
176 #ifdef CONFIG_SPIFFS_USE_MAGIC
177 #define SPIFFS_USE_MAGIC                (1)
178 #else
179 #define SPIFFS_USE_MAGIC                (0)
180 #endif
181 
182 #if SPIFFS_USE_MAGIC
183 // Only valid when SPIFFS_USE_MAGIC is enabled. If SPIFFS_USE_MAGIC_LENGTH is
184 // enabled, the magic will also be dependent on the length of the filesystem.
185 // For example, a filesystem configured and formatted for 4 megabytes will not
186 // be accepted for mounting with a configuration defining the filesystem as 2
187 // megabytes.
188 #ifdef CONFIG_SPIFFS_USE_MAGIC_LENGTH
189 #define SPIFFS_USE_MAGIC_LENGTH         (1)
190 #else
191 #define SPIFFS_USE_MAGIC_LENGTH         (0)
192 #endif
193 #endif
194 
195 // SPIFFS_LOCK and SPIFFS_UNLOCK protects spiffs from reentrancy on api level
196 // These should be defined on a multithreaded system
197 
198 // define this to enter a mutex if you're running on a multithreaded system
199 #define SPIFFS_LOCK(fs)   spiffs_api_lock(fs)
200 // define this to exit a mutex if you're running on a multithreaded system
201 #define SPIFFS_UNLOCK(fs) spiffs_api_unlock(fs)
202 
203 // Enable if only one spiffs instance with constant configuration will exist
204 // on the target. This will reduce calculations, flash and memory accesses.
205 // Parts of configuration must be defined below instead of at time of mount.
206 #define SPIFFS_SINGLETON 0
207 
208 // Enable this if your target needs aligned data for index tables
209 #define SPIFFS_ALIGNED_OBJECT_INDEX_TABLES      0
210 
211 // Enable this if you want the HAL callbacks to be called with the spiffs struct
212 #define SPIFFS_HAL_CALLBACK_EXTRA               1
213 
214 // Enable this if you want to add an integer offset to all file handles
215 // (spiffs_file). This is useful if running multiple instances of spiffs on
216 // same target, in order to recognise to what spiffs instance a file handle
217 // belongs.
218 // NB: This adds config field fh_ix_offset in the configuration struct when
219 // mounting, which must be defined.
220 #define SPIFFS_FILEHDL_OFFSET                   0
221 
222 // Enable this to compile a read only version of spiffs.
223 // This will reduce binary size of spiffs. All code comprising modification
224 // of the file system will not be compiled. Some config will be ignored.
225 // HAL functions for erasing and writing to spi-flash may be null. Cache
226 // can be disabled for even further binary size reduction (and ram savings).
227 // Functions modifying the fs will return SPIFFS_ERR_RO_NOT_IMPL.
228 // If the file system cannot be mounted due to aborted erase operation and
229 // SPIFFS_USE_MAGIC is enabled, SPIFFS_ERR_RO_ABORTED_OPERATION will be
230 // returned.
231 // Might be useful for e.g. bootloaders and such.
232 #define SPIFFS_READ_ONLY                        0
233 
234 // Enable this to add a temporal file cache using the fd buffer.
235 // The effects of the cache is that SPIFFS_open will find the file faster in
236 // certain cases. It will make it a lot easier for spiffs to find files
237 // opened frequently, reducing number of readings from the spi flash for
238 // finding those files.
239 // This will grow each fd by 6 bytes. If your files are opened in patterns
240 // with a degree of temporal locality, the system is optimized.
241 // Examples can be letting spiffs serve web content, where one file is the css.
242 // The css is accessed for each html file that is opened, meaning it is
243 // accessed almost every second time a file is opened. Another example could be
244 // a log file that is often opened, written, and closed.
245 // The size of the cache is number of given file descriptors, as it piggybacks
246 // on the fd update mechanism. The cache lives in the closed file descriptors.
247 // When closed, the fd know the whereabouts of the file. Instead of forgetting
248 // this, the temporal cache will keep handling updates to that file even if the
249 // fd is closed. If the file is opened again, the location of the file is found
250 // directly. If all available descriptors become opened, all cache memory is
251 // lost.
252 #define SPIFFS_TEMPORAL_FD_CACHE                1
253 
254 // Temporal file cache hit score. Each time a file is opened, all cached files
255 // will lose one point. If the opened file is found in cache, that entry will
256 // gain SPIFFS_TEMPORAL_CACHE_HIT_SCORE points. One can experiment with this
257 // value for the specific access patterns of the application. However, it must
258 // be between 1 (no gain for hitting a cached entry often) and 255.
259 #define SPIFFS_TEMPORAL_CACHE_HIT_SCORE         4
260 
261 // Enable to be able to map object indices to memory.
262 // This allows for faster and more deterministic reading if cases of reading
263 // large files and when changing file offset by seeking around a lot.
264 // When mapping a file's index, the file system will be scanned for index pages
265 // and the info will be put in memory provided by user. When reading, the
266 // memory map can be looked up instead of searching for index pages on the
267 // medium. This way, user can trade memory against performance.
268 // Whole, parts of, or future parts not being written yet can be mapped. The
269 // memory array will be owned by spiffs and updated accordingly during garbage
270 // collecting or when modifying the indices. The latter is invoked by when the
271 // file is modified in some way. The index buffer is tied to the file
272 // descriptor.
273 #define SPIFFS_IX_MAP                           1
274 
275 // Set SPIFFS_TEST_VISUALISATION to non-zero to enable SPIFFS_vis function
276 // in the api. This function will visualize all filesystem using given printf
277 // function.
278 #ifdef CONFIG_SPIFFS_TEST_VISUALISATION
279 #define SPIFFS_TEST_VISUALISATION               1
280 #else
281 #define SPIFFS_TEST_VISUALISATION               0
282 #endif
283 #if SPIFFS_TEST_VISUALISATION
284 #ifndef spiffs_printf
285 #define spiffs_printf(...)                ESP_LOGD(SPIFFS_TAG, __VA_ARGS__)
286 #endif
287 // spiffs_printf argument for a free page
288 #define SPIFFS_TEST_VIS_FREE_STR          "_"
289 // spiffs_printf argument for a deleted page
290 #define SPIFFS_TEST_VIS_DELE_STR          "/"
291 // spiffs_printf argument for an index page for given object id
292 #define SPIFFS_TEST_VIS_INDX_STR(id)      "i"
293 // spiffs_printf argument for a data page for given object id
294 #define SPIFFS_TEST_VIS_DATA_STR(id)      "d"
295 #endif
296 
297 // Types depending on configuration such as the amount of flash bytes
298 // given to spiffs file system in total (spiffs_file_system_size),
299 // the logical block size (log_block_size), and the logical page size
300 // (log_page_size)
301 
302 // Block index type. Make sure the size of this type can hold
303 // the highest number of all blocks - i.e. spiffs_file_system_size / log_block_size
304 typedef u16_t spiffs_block_ix;
305 // Page index type. Make sure the size of this type can hold
306 // the highest page number of all pages - i.e. spiffs_file_system_size / log_page_size
307 typedef u16_t spiffs_page_ix;
308 // Object id type - most significant bit is reserved for index flag. Make sure the
309 // size of this type can hold the highest object id on a full system,
310 // i.e. 2 + (spiffs_file_system_size / (2*log_page_size))*2
311 typedef u16_t spiffs_obj_id;
312 // Object span index type. Make sure the size of this type can
313 // hold the largest possible span index on the system -
314 // i.e. (spiffs_file_system_size / log_page_size) - 1
315 typedef u16_t spiffs_span_ix;
316 
317 #endif /* SPIFFS_CONFIG_H_ */
318