spiffs_config.h 13 KB

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