SPIFFSNVS.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2023, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!*****************************************************************************
33  * @file SPIFFSNVS.h
34  * @brief Module to enable the SPI Flash File System (SPIFFS) on the
35  * TI NVS driver
36  *
37  * The SPIFFSNVS header file should be included in an application as follows:
38  * @code
39  * #include <third_party/spiffs/SPIFFSNVS.h>
40  * @endcode
41  *
42  * SPIFFSNVS is designed to provide interface functions required by the SPIFFS
43  * to read/write/erase NOR flash memory. These functions are implemented using
44  * the TI NVS driver. By leveraging the NVS driver, application code using
45  * SPIFFS is not tied to a physical memory type (like SPI flash). The NVS
46  * driver abstracts the physical memory interface so SPIFFS application code
47  * can run on any NVS driver instance. Changing between internal flash or
48  * external SPI flash can be done by having SPIFFS use a different NVS driver
49  * instance. SPIFFS will operate within the memory region which has been
50  * allocated for the NVS driver instance.
51  *
52  * It is highly recommended that you read SPIFFS design and usage documentation
53  * before using SPIFFS. Documentation available here:
54  * https://github.com/pellepl/spiffs
55  *
56  * @warning SPI Flash File Systems generated by applications built with one
57  * toolchain might not be compatible with applications built with another
58  * toolchain.
59  *
60  * ## Using SPIFFS and SPIFFSNVS ##
61  *
62  * To mount a SPIFFS file system some configuration parameters and RAM must be
63  * provided at runtime (all sizes in bytes):
64  * * physical block size (also known as sector size)
65  * * amount of memory allocated for SPIFFS
66  * * logical block size
67  * * logical page size
68  * * A RAM work buffer
69  * * A RAM file descriptor cache
70  * * A RAM read/write cache
71  *
72  * NVS drivers are aware of the physical block size and the amount of memory
73  * allocated (values are set in driver configuration; see board files). The
74  * logical block size must be an integer multiple of the physical block size
75  * (a.k.a. sector size):
76  * logicalBlockSize = n * physicalBlockSize
77  *
78  * @note It is recommended to set logical block size equal to the physical
79  * block size when starting with SPIFFS. This can be changed later when
80  * optimizing the file system for your application.
81  *
82  * The logical block size must also be an integer multiple of the logical page
83  * size: logicalBlockSize = i * logicalPageSize
84  *
85  * A statically allocated RAM work buffer must be provided. This buffer
86  * must be (2 * logicalPageSize) in length. A statically allocated RAM
87  * file descriptor cache must also be provided. File descriptors are 44 bytes
88  * for the default SPIFFS configuration. The cache must be large enough to
89  * store as many file descriptors as desired. Start with 4 file descriptors
90  * and modify when optimizing for your application. Finally, SPIFFS requires
91  * a read/write cache be provided. Start with a (2 * logicalPageSize) size
92  * cache; this can be increased or reduced later.
93  *
94  * As an example assume that we want to use SPIFFS with a NVS driver instance
95  * that has 128k of memory and the the physical block size is 4096 bytes. In
96  * this case the logical block size can be set to 8192 bytes (16 logical
97  * blocks). Now we can set the logical page size to 256 bytes (32 logical pages
98  * per logical block).
99  *
100  * @note SPIFFS always keeps 2 logical blocks free; in the example above there
101  * would only be 14 logical blocks available for storage (16k is unusable). The
102  * logical block size can be reduced to have more space for data. The logical
103  * block size and logical page size should be changed to optimize the file
104  * system for your application.
105  *
106  * Knowing the logical block and logical page sizes desired; SPIFFSNVS_config()
107  * must be used to initialize the #spiffs_config #spiffs structures.
108  * SPIFFSNVS_config() requires a #SPIFFSNVS_Data object be provided. This
109  * object is used by SPIFFSNVS when reading/writing to flash memory. It also
110  * stores references to OS objects used for thread safety. Each SPIFFS file
111  * system instance must have it's own #SPIFFSNVS_Data object and these objects
112  * must reside in persistent memory (not on a task stack or memory lost during
113  * low power modes). The #spiffs.user_data field is used to store a pointer to
114  * its respective #SPIFFSNVS_Data object. Users must not change #spiffs_config
115  * and #spiffs structures after SPIFFSNVS_config() has been called.
116  *
117  * @code
118  * #define SPIFFS_LOGICAL_BLOCK_SIZE (4096)
119  * #define SPIFFS_LOGICAL_PAGE_SIZE (128)
120  * #define SPIFFS_FILE_DESCRIPTOR_SIZE (44)
121  *
122  * static uint8_t spiffsWorkBuffer[SPIFFS_LOGICAL_PAGE_SIZE * 2];
123  * static uint8_t spiffsFileDescriptorCache[SPIFFS_FILE_DESCRIPTOR_SIZE * 4];
124  * static uint8_t spiffsReadWriteCache[SPIFFS_LOGICAL_PAGE_SIZE * 2];
125  *
126  * spiffs fs;
127  * spiffs_config fsConfig;
128  * SPIFFSNVS_Object spiffsnvs;
129  *
130  * status = SPIFFSNVS_config(&spiffsnvs, Board_NVSINTERNAL, &fs, &fsConfig,
131  * SPIFFS_LOGICAL_BLOCK_SIZE, SPIFFS_LOGICAL_PAGE_SIZE);
132  * if (status != SPIFFSNVS_STATUS_SUCCESS) {
133  * // Handle error condition...
134  * }
135  *
136  * status = SPIFFS_mount(&fs, &fsConfig, spiffsWorkBuffer,
137  * spiffsFileDescriptorCache, sizeof(spiffsFileDescriptorCache),
138  * spiffsReadWriteCache, sizeof(spiffsReadWriteCache), NULL);
139  * @endcode
140  *
141  * ## Logical block and page size restrictions on CC13XX and CC26XX devices ##
142  *
143  * Flash memory on CC13XX and CC26XX devices is divided into rows of 128 or
144  * 256 bytes (refer to datasheet for exact size). These rows can support up to
145  * 83 write operations before suffering effects of row disturb in which data
146  * can be corrupted. Setting the logical page size very small or setting
147  * the logical block size too large can lead to many logical pages in a logical
148  * block. Normal use and updates to pages can cause more than 83 write
149  * operations on a logical block's index page (first page in the logical block).
150  * The following conditions must be followed to prevent exceeding the 83 write
151  * limit:
152  *
153  * * Select a logical page size that is equal to or an integer multiple of
154  * the device's physical row size (i.e. 128 or 256). Next select a logical
155  * block size to ensure the amount of logical pages in a logical block
156  * does not exceed 32:
157  * (logicalBlockSize / logicalPageSize <= 32).
158  *
159  * It is the user's responsibility to make sure the logical page size is equal
160  * to or an integer multiple of the physical row size. SPIFFSNVS_init() will
161  * verify and return #SPIFFSNVS_STATUS_INV_PAGE_SIZE if the amount of logical
162  * pages in a logical block is > 32.
163  *
164  *******************************************************************************
165  */
166 
167 #ifndef THIRD_PARTY_SPIFFS_SPIFFSNVS_H_
168 #define THIRD_PARTY_SPIFFS_SPIFFSNVS_H_
169 
170 #include <stdint.h>
171 
172 #include "spiffs_config.h"
173 #include "spiffs.h"
174 
175 #include <ti/drivers/NVS.h>
176 #include <ti/drivers/dpl/MutexP.h>
177 
178 #ifdef __cplusplus
179 extern "C" {
180 #endif
181 
193 #define SPIFFSNVS_STATUS_SUCCESS (0)
194 
201 #define SPIFFSNVS_STATUS_ERROR (-1)
202 
208 #define SPIFFSNVS_STATUS_INV_NVS_IDX (-2)
209 
218 #define SPIFFSNVS_STATUS_INV_BLOCK_SIZE (-3)
219 
228 #define SPIFFSNVS_STATUS_INV_PAGE_SIZE (-4)
229 
237 typedef struct SPIFFSNVS_Lock_ {
239  uintptr_t keys[2];
240  volatile u32_t count;
242 
251 typedef struct SPIFFSNVS_Data_ {
255 
267 void SPIFFSNVS_close(SPIFFSNVS_Data *spiffsnvsData);
268 
306 s32_t SPIFFSNVS_config(SPIFFSNVS_Data *spiffsnvsData,
307  u32_t nvsIndex, spiffs *fs, spiffs_config *fsConfig,
308  u32_t logicalBlockSize, u32_t logicalPageSize);
309 
310 #ifdef __cplusplus
311 }
312 #endif
313 
314 #endif /* THIRD_PARTY_SPIFFS_SPIFFSNVS_H_ */
uintptr_t keys[2]
Definition: SPIFFSNVS.h:239
struct SPIFFSNVS_Lock_ SPIFFSNVS_Lock
SPIFFSNVS Lock.
Mutex module for the RTOS Porting Interface.
Definition: spiffs.h:200
void * MutexP_Handle
Opaque client reference to an instance of a MutexP.
Definition: MutexP.h:107
SPIFFSNVS Lock.
Definition: SPIFFSNVS.h:237
s32_t SPIFFSNVS_config(SPIFFSNVS_Data *spiffsnvsData, u32_t nvsIndex, spiffs *fs, spiffs_config *fsConfig, u32_t logicalBlockSize, u32_t logicalPageSize)
Initializes spiffs, spiffs_config and SPIFFSNVS_Data structures for SPIFFS to interface with the NVS ...
Definition: spiffs.h:231
MutexP_Handle mutex
Definition: SPIFFSNVS.h:238
struct SPIFFSNVS_Data_ SPIFFSNVS_Data
SPIFFSNVS data object.
volatile u32_t count
Definition: SPIFFSNVS.h:240
Non-Volatile Storage driver interface.
SPIFFSNVS data object.
Definition: SPIFFSNVS.h:251
NVS Global configuration.
Definition: NVS.h:621
void SPIFFSNVS_close(SPIFFSNVS_Data *spiffsnvsData)
Closes the NVS flash region used for the file system.
SPIFFSNVS_Lock lock
Definition: SPIFFSNVS.h:253
NVS_Handle nvsHandle
Definition: SPIFFSNVS.h:252
© Copyright 1995-2024, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale