1 /*
2  * Copyright (c) 2019 Bolt Innovation Management, LLC
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_FS_LITTLEFS_H_
8 #define ZEPHYR_INCLUDE_FS_LITTLEFS_H_
9 
10 #include <zephyr/types.h>
11 #include <zephyr/kernel.h>
12 #include <zephyr/storage/flash_map.h>
13 
14 #include <lfs.h>
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /** @brief Filesystem info structure for LittleFS mount */
21 struct fs_littlefs {
22 	/* Defaulted in driver, customizable before mount. */
23 	struct lfs_config cfg;
24 
25 	/* Must be cfg.cache_size */
26 	uint8_t *read_buffer;
27 
28 	/* Must be cfg.cache_size */
29 	uint8_t *prog_buffer;
30 
31 	/* Must be cfg.lookahead_size/4 elements, and
32 	 * cfg.lookahead_size must be a multiple of 8.
33 	 */
34 	uint32_t *lookahead_buffer[CONFIG_FS_LITTLEFS_LOOKAHEAD_SIZE / sizeof(uint32_t)];
35 
36 	/* These structures are filled automatically at mount. */
37 	struct lfs lfs;
38 	void *backend;
39 	struct k_mutex mutex;
40 };
41 
42 /** @brief Define a littlefs configuration with customized size
43  * characteristics.
44  *
45  * This defines static arrays required for caches, and initializes the
46  * littlefs configuration structure to use the provided values instead
47  * of the global Kconfig defaults.  A pointer to the named object must
48  * be stored in the @ref fs_mount_t.fs_data field of a @ref fs_mount_t object.
49  *
50  * To define an instance for the Kconfig defaults, use
51  * @ref FS_LITTLEFS_DECLARE_DEFAULT_CONFIG.
52  *
53  * To completely control file system configuration the application can
54  * directly define and initialize a @ref fs_littlefs
55  * object.  The application is responsible for ensuring the configured
56  * values are consistent with littlefs requirements.
57  *
58  * @note If you use a non-default configuration for cache size, you
59  * must also select @kconfig{CONFIG_FS_LITTLEFS_FC_HEAP_SIZE} to relax
60  * the size constraints on per-file cache allocations.
61  *
62  * @param name the name for the structure.  The defined object has
63  * file scope.
64  * @param alignment needed alignment for read/prog buffer for specific device
65  * @param read_sz see @kconfig{CONFIG_FS_LITTLEFS_READ_SIZE}
66  * @param prog_sz see @kconfig{CONFIG_FS_LITTLEFS_PROG_SIZE}
67  * @param cache_sz see @kconfig{CONFIG_FS_LITTLEFS_CACHE_SIZE}
68  * @param lookahead_sz see @kconfig{CONFIG_FS_LITTLEFS_LOOKAHEAD_SIZE}
69  */
70 #define FS_LITTLEFS_DECLARE_CUSTOM_CONFIG(name, alignment, read_sz, prog_sz, cache_sz,	  \
71 					  lookahead_sz)					  \
72 	static uint8_t __aligned(alignment) name ## _read_buffer[cache_sz];		  \
73 	static uint8_t __aligned(alignment) name ## _prog_buffer[cache_sz];		  \
74 	static uint32_t name ## _lookahead_buffer[(lookahead_sz) / sizeof(uint32_t)];	  \
75 	static struct fs_littlefs name = {						  \
76 		.cfg = {								  \
77 			.read_size = (read_sz),						  \
78 			.prog_size = (prog_sz),						  \
79 			.cache_size = (cache_sz),					  \
80 			.lookahead_size = (lookahead_sz),				  \
81 			.read_buffer = name ## _read_buffer,				  \
82 			.prog_buffer = name ## _prog_buffer,				  \
83 			.lookahead_buffer = name ## _lookahead_buffer,			  \
84 		},									  \
85 	}
86 
87 /** @brief Define a littlefs configuration with default characteristics.
88  *
89  * This defines static arrays and initializes the littlefs
90  * configuration structure to use the default size configuration
91  * provided by Kconfig.
92  *
93  * @param name the name for the structure.  The defined object has
94  * file scope.
95  */
96 #define FS_LITTLEFS_DECLARE_DEFAULT_CONFIG(name)			 \
97 	FS_LITTLEFS_DECLARE_CUSTOM_CONFIG(name,				 \
98 					  4,				 \
99 					  CONFIG_FS_LITTLEFS_READ_SIZE,	 \
100 					  CONFIG_FS_LITTLEFS_PROG_SIZE,	 \
101 					  CONFIG_FS_LITTLEFS_CACHE_SIZE, \
102 					  CONFIG_FS_LITTLEFS_LOOKAHEAD_SIZE)
103 
104 #ifdef __cplusplus
105 }
106 #endif
107 
108 #endif /* ZEPHYR_INCLUDE_FS_LITTLEFS_H_ */
109