SPIFFSNVS.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, 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  * ## Using SPIFFS and SPIFFSNVS #
57  *
58  * To mount a SPIFFS file system some configuration parameters and RAM must be
59  * provided at runtime (all sizes in bytes):
60  * * physical block size (also known as sector size)
61  * * amount of memory allocated for SPIFFS
62  * * logical block size
63  * * logical page size
64  * * A RAM work buffer
65  * * A RAM file descriptor cache
66  * * A RAM read/write cache
67  *
68  * NVS drivers are aware of the physical block size and the amount of memory
69  * allocated (values are set in driver configuration; see board files). The
70  * logical block size must be an integer multiple of the physical block size
71  * (a.k.a. sector size):
72  * logicalBlockSize = n * physicalBlockSize
73  *
74  * @note It is recommended to set logical block size equal to the physical
75  * block size when starting with SPIFFS. This can be changed later when
76  * optimizing the file system for your application.
77  *
78  * The logical block size must also be an integer multiple of the logical page
79  * size: logicalBlockSize = i * logicalPageSize
80  *
81  * A statically allocated RAM work buffer must be provided. This buffer
82  * must be (2 * logicalPageSize) in length. A statically allocated RAM
83  * file descriptor cache must also be provided. File descriptors are 44 bytes
84  * for the default SPIFFS configuration. The cache must be large enough to
85  * store as many file descriptors as desired. Start with 4 file descriptors
86  * and modify when optimizing for your application. Finally, SPIFFS requires
87  * a read/write cache be provided. Start with a (2 * logicalPageSize) size
88  * cache; this can be increased or reduced later.
89  *
90  * As an example assume that we want to use SPIFFS with a NVS driver instance
91  * that has 128k of memory and the the physical block size is 4096 bytes. In
92  * this case the logical block size can be set to 8192 bytes (16 logical
93  * blocks). Now we can set the logical page size to 256 bytes (32 logical pages
94  * per logical block).
95  *
96  * @note SPIFFS always keeps 2 logical blocks free; in the example above there
97  * would only be 14 logical blocks available for storage (16k is unusable). The
98  * logical block size can be reduced to have more space for data. The logical
99  * block size and logical page size should be changed to optimize the file
100  * system for your application.
101  *
102  * Knowing the logical block and logical page sizes desired; SPIFFSNVS_config()
103  * must be used to initialize the #spiffs_config #spiffs structures.
104  * SPIFFSNVS_config() requires a #SPIFFSNVS_Data object be provided. This
105  * object is used by SPIFFSNVS when reading/writing to flash memory. It also
106  * stores references to OS objects used for thread safety. Each SPIFFS file
107  * system instance must have it's own #SPIFFSNVS_Data object and these objects
108  * must reside in persistent memory (not on a task stack or memory lost during
109  * low power modes). The #spiffs.user_data field is used to store a pointer to
110  * its respective #SPIFFSNVS_Data object. Users must not change #spiffs_config
111  * and #spiffs structures after SPIFFSNVS_config() has been called.
112  *
113  * @code
114  * #define SPIFFS_LOGICAL_BLOCK_SIZE (4096)
115  * #define SPIFFS_LOGICAL_PAGE_SIZE (128)
116  * #define SPIFFS_FILE_DESCRIPTOR_SIZE (44)
117  *
118  * static uint8_t spiffsWorkBuffer[SPIFFS_LOGICAL_PAGE_SIZE * 2];
119  * static uint8_t spiffsFileDescriptorCache[SPIFFS_FILE_DESCRIPTOR_SIZE * 4];
120  * static uint8_t spiffsReadWriteCache[SPIFFS_LOGICAL_PAGE_SIZE * 2];
121  *
122  * spiffs fs;
123  * spiffs_config fsConfig;
124  * SPIFFSNVS_Object spiffsnvs;
125  *
126  * status = SPIFFSNVS_config(&spiffsnvs, Board_NVSINTERNAL, &fs, &fsConfig,
127  * SPIFFS_LOGICAL_BLOCK_SIZE, SPIFFS_LOGICAL_PAGE_SIZE);
128  * if (status != SPIFFSNVS_STATUS_SUCCESS) {
129  * // Handle error condition...
130  * }
131  *
132  * status = SPIFFS_mount(&fs, &fsConfig, spiffsWorkBuffer,
133  * spiffsFileDescriptorCache, sizeof(spiffsFileDescriptorCache),
134  * spiffsReadWriteCache, sizeof(spiffsReadWriteCache), NULL);
135  * @endcode
136  *
137  * ## Logical block and page size restrictions on CC13XX and CC26XX devices #
138  *
139  * Flash memory on CC13XX and CC26XX devices is divided into rows of 128 or
140  * 256 bytes (refer to datasheet for exact size). These rows can support up to
141  * 83 write operations before suffering effects of row disturb in which data
142  * can be corrupted. Setting the logical page size very small or setting
143  * the logical block size too large can lead to many logical pages in a logical
144  * block. Normal use and updates to pages can cause more than 83 write
145  * operations on a logical block's index page (first page in the logical block).
146  * The following conditions must be followed to prevent exceeding the 83 write
147  * limit:
148  *
149  * * Select a logical page size that is equal to or an integer multiple of
150  * the device's physical row size (i.e. 128 or 256). Next select a logical
151  * block size to ensure the amount of logical pages in a logical block
152  * does not exceed 32:
153  * (logicalBlockSize / logicalPageSize <= 32).
154  *
155  * It is the user's responsibility to make sure the logical page size is equal
156  * to or an integer multiple of the physical row size. SPIFFSNVS_init() will
157  * verify and return #SPIFFSNVS_STATUS_INV_PAGE_SIZE if the amount of logical
158  * pages in a logical block is > 32.
159  *
160  *******************************************************************************
161  */
162 
163 #ifndef THIRD_PARTY_SPIFFS_SPIFFSNVS_H_
164 #define THIRD_PARTY_SPIFFS_SPIFFSNVS_H_
165 
166 #include <stdint.h>
167 
168 #include "spiffs_config.h"
169 #include "spiffs.h"
170 
171 #include <ti/drivers/NVS.h>
172 #include <ti/drivers/dpl/MutexP.h>
173 
174 #ifdef __cplusplus
175 extern "C" {
176 #endif
177 
189 #define SPIFFSNVS_STATUS_SUCCESS (0)
190 
197 #define SPIFFSNVS_STATUS_ERROR (-1)
198 
204 #define SPIFFSNVS_STATUS_INV_NVS_IDX (-2)
205 
214 #define SPIFFSNVS_STATUS_INV_BLOCK_SIZE (-3)
215 
224 #define SPIFFSNVS_STATUS_INV_PAGE_SIZE (-4)
225 
233 typedef struct SPIFFSNVS_Lock_ {
234  MutexP_Handle mutex;
235  uintptr_t keys[2];
236  volatile u32_t count;
238 
247 typedef struct SPIFFSNVS_Data_ {
251 
289 s32_t SPIFFSNVS_config(SPIFFSNVS_Data *spiffsnvsData,
290  u32_t nvsIndex, spiffs *fs, spiffs_config *fsConfig,
291  u32_t logicalBlockSize, u32_t logicalPageSize);
292 
293 #ifdef __cplusplus
294 }
295 #endif
296 
297 #endif /* THIRD_PARTY_SPIFFS_SPIFFSNVS_H_ */
uintptr_t keys[2]
Definition: SPIFFSNVS.h:235
struct SPIFFSNVS_Lock_ SPIFFSNVS_Lock
SPIFFSNVS Lock.
Definition: spiffs.h:200
SPIFFSNVS Lock.
Definition: SPIFFSNVS.h:233
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:234
struct SPIFFSNVS_Data_ SPIFFSNVS_Data
SPIFFSNVS data object.
volatile u32_t count
Definition: SPIFFSNVS.h:236
Non-Volatile Storage driver interface.
SPIFFSNVS data object.
Definition: SPIFFSNVS.h:247
NVS Global configuration.
Definition: NVS.h:618
SPIFFSNVS_Lock lock
Definition: SPIFFSNVS.h:249
NVS_Handle nvsHandle
Definition: SPIFFSNVS.h:248
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale