NVS.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2019, 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 NVS.h
34  * @brief Non-Volatile Storage driver interface
35  *
36  * @anchor ti_drivers_NVS_Overview
37  * # Overview #
38  *
39  * The NVS module allows you to manage non-volatile memory. Using the
40  * NVS APIs, you can read and write data from and to persistent storage.
41  *
42  * Each NVS object is used to manage a region of non-volatile memory.
43  * The size of the region is specified in the device specific driver's
44  * hardware attributes.
45  * A sector is the smallest unit of non-volatile storage that can be erased
46  * at one time. The size of the sector, or sector size, is hardware specific
47  * and may be meaningless for some non-volatile storage hardware. For flash
48  * memory devices, the region must be aligned with the sector size. That is,
49  * the region must start on a sector boundary. Additionally, the overall size
50  * of the region must be an integer multiple of the sector size.
51  *
52  * <hr>
53  * @anchor ti_drivers_NVS_Usage
54  * # Usage
55  *
56  * This section provides a basic @ref ti_drivers_NVS_Synopsis
57  * "usage summary" and a set of @ref ti_drivers_NVS_Examples "examples"
58  * in the form of commented code fragments. Detailed descriptions of the
59  * APIs are provided in subsequent sections.
60 
61  * The NVS driver interface provides device independent APIs, data types,
62  * and macros. The following code example opens an NVS region instance,
63  * writes a string into it, then prints the string after reading it back
64  * into a local buffer, and also prints the string from its directly
65  * addressable location in flash memory.
66  *
67  * @anchor ti_drivers_NVS_Synopsis
68  * ## Synopsis
69  * @anchor ti_drivers_NVS_Synopsis_Code
70  * @code
71  * // Import NVS Driver definitions
72  * #include <ti/drivers/NVS.h>
73  *
74  * // One time init of NVS driver
75  * NVS_init();
76  *
77  * // Initialize optional NVS parameters
78  * NVS_Params_init(&nvsParams);
79  *
80  * // Open NVS driver instance
81  * nvsRegion = NVS_open(config_NVS0, &nvsParams);
82  *
83  * // write "Hello" to the base address of region 0, verify after write
84  * status = NVS_write(nvsRegion, 0, "Hello", strlen("Hello")+1, NVS_POST_VERIFY);
85  *
86  * // Close NVS region
87  * NVS_close(nvsRegion);
88  *
89  * @endcode
90  *
91  * <hr>
92  * @anchor ti_drivers_NVS_Examples
93  * # Examples
94  *
95  * @li @ref ti_drivers_NVS_Examples_open "Opening an NVS region instance"
96  * @li @ref ti_drivers_NVS_Examples_typical "Typical NVS region operations"
97  *
98  * @anchor ti_drivers_NVS_Examples_open
99  * ## Opening an NVS region instance
100  *
101  * @code
102  * NVS_Handle nvsRegion;
103  * NVS_Params nvsParams;
104  *
105  * NVS_Params_init(&nvsParams);
106  * nvsRegion = NVS_open(CONFIG_NVS0, &nvsParams);
107  * @endcode
108  *
109  * @anchor ti_drivers_NVS_Examples_typical
110  * ## Erasing, writing, reading an NVS region
111  *
112  * The following code example opens an NVS region instance, erases the first
113  * sector of that region, writes a string into it, then prints the string
114  * after reading it back into a local buffer. If the string is directly CPU
115  * addressable (i.e. not in SPI flash), the string is printed from
116  * its location in flash memory.
117  *
118  * @code
119  *
120  * // Import NVS Driver definitions
121  * #include <ti/drivers/NVS.h>
122  *
123  * NVS_Handle nvsRegion;
124  * NVS_Attrs regionAttrs;
125  *
126  * uint_fast16_t status;
127  * char buf[32];
128  *
129  * // initialize the NVS driver
130  * NVS_init();
131  *
132  * //
133  * // Open the NVS region specified by the 0th element in the NVS_config[]
134  * // array defined in ti_drivers_config.c.
135  * //
136  * // Use default NVS_Params to open this memory region, hence 'NULL'
137  * //
138  * nvsRegion = NVS_open(CONFIG_NVS0, NULL);
139  *
140  * // Confirm that the NVS region opened properly
141  * if (nvsRegion == NULL) {
142  * // Error handling code
143  * }
144  *
145  * // Fetch the generic NVS region attributes for nvsRegion
146  * NVS_getAttrs(nvsRegion, &regionAttrs);
147  *
148  * // Erase the first sector of nvsRegion
149  * status = NVS_erase(nvsRegion, 0, regionAttrs.sectorSize);
150  * if (status != NVS_STATUS_SUCCESS) {
151  * // Error handling code
152  * }
153  *
154  * // Write "Hello" to the base address of nvsRegion, verify after write
155  * status = NVS_write(nvsRegion, 0, "Hello", strlen("Hello")+1, NVS_POST_VERIFY);
156  * if (status != NVS_STATUS_SUCCESS) {
157  * // Error handling code
158  * }
159  *
160  * // Copy "Hello" from nvsRegion into local 'buf'
161  * status = NVS_read(nvsRegion, 0, buf, strlen("Hello")+1);
162  * if (status != NVS_STATUS_SUCCESS) {
163  * // Error handling code
164  * }
165  *
166  * // Print the string from fetched NVS storage
167  * System_printf("%s\n", buf);
168  *
169  * //
170  * // Print the string using direct flash address reference if valid
171  * //
172  * // When the NVS driver is managing SPI flash non volatile
173  * // storage, the regionBase attribute will be `NVS_REGION_NOT_ADDRESSABLE`
174  * //
175  * if (regionAttrs.regionBase != NVS_REGION_NOT_ADDRESSABLE) {
176  * System_printf("%s\n", regionAttrs.regionBase);
177  * }
178  *
179  * // close the region
180  * NVS_close(nvsRegion);
181  *
182  * @endcode
183  *
184  * <hr>
185  * @anchor ti_drivers_NVS_Configuration
186  * # Configuration
187  *
188  * Refer to the @ref driver_configuration "Driver's Configuration" section
189  * for driver configuration information.
190  * <hr>
191  *
192  * # Example discussion
193  *
194  * Details for the @ref ti_drivers_NVS_Examples_typical "Typical NVS region operations"
195  * example code above are described in the following subsections.
196  *
197  * ### NVS Driver Configuration #
198  *
199  * In order to use the NVS APIs, the application is required to provide
200  * device-specific NVS configuration in the ti_drivers_config.c file.
201  * The NVS driver interface defines a configuration data structure,
202  * #NVS_Config.
203  *
204  * The application must declare an array of #NVS_Config elements, named
205  * \p NVS_config[]. Each element of \p NVS_config[] is populated with
206  * pointers to a device specific NVS driver implementation's function
207  * table, driver object, and hardware attributes. The hardware attributes
208  * define properties such as the NVS region's base address and size,
209  * Each element in \p NVS_config[] corresponds to a NVS instance, and none
210  * of the elements should have NULL pointers.
211  *
212  * You will need to check the device-specific NVS driver implementation's
213  * header file for example configuration. Please also refer to the
214  * ti_drivers_config.c file of any of the examples to see the NVS configuration.
215  *
216  * ### Initializing the NVS Driver #
217  *
218  * NVS_init() must be called before any other NVS APIs. This function
219  * calls the device implementation's NVS initialization function, for each
220  * element of \p NVS_config[].
221  *
222  * ### Opening the NVS Driver #
223  *
224  * Opening a NVS requires four steps:
225  * 1. Optionally create and initialize a #NVS_Params structure.
226  * 2. Fill in the desired parameters.
227  * 3. Call NVS_open(), passing the index of the NVS region in the #NVS_Config
228  * structure, and the address of the #NVS_Params structure.
229  * 4. Check that the #NVS_Handle returned by NVS_open() is non-NULL,
230  * and save it. The handle will be used to read and write to the
231  * NVS you just opened.
232  *
233  * \note Each NVS index can only be opened exclusively. Calling NVS_open()
234  * multiple times with the same index will result in an error. The index can
235  * be re-used if NVS_close() is called first.
236  *
237  * <hr>
238  * # Thread Safety #
239  *
240  * All NVS APIs are globally thread safe. Consequently, only one write,
241  * erase (or read in the case of SPI flash) operation is allowed to be
242  * performed at a time, even for distinct NVS regions. Threads initiating
243  * new NVS writes or erases will block until any current operation completes.
244  *
245  * # Interrupt Latency During Flash Operations #
246  *
247  * When writing to or erasing internal flash, interrupts must be disabled
248  * to avoid executing code in flash while the flash is being reprogrammed.
249  * This constraint is met internally by the driver. User code does not need
250  * to safeguard against this.
251  *
252  * Care must be taken by the user to not perform flash write or erase
253  * operations during latency critical phases of an application. See the
254  * NVS_lock() and NVS_unlock() API descriptions for more information.
255  *
256  *****************************************************************************
257  */
258 
259 #ifndef ti_drivers_NVS__include
260 #define ti_drivers_NVS__include
261 
262 #include <stdbool.h>
263 #include <stddef.h>
264 #include <stdint.h>
265 
266 #if defined (__cplusplus)
267 extern "C" {
268 #endif
269 
287 #define NVS_CMD_RESERVED (32)
288 
301 #define NVS_STATUS_RESERVED (-32)
302 
318 #define NVS_STATUS_SUCCESS (0)
319 
327 #define NVS_STATUS_ERROR (-1)
328 
336 #define NVS_STATUS_UNDEFINEDCMD (-2)
337 
343 #define NVS_STATUS_TIMEOUT (-3)
344 
352 #define NVS_STATUS_INV_OFFSET (-4)
353 
360 #define NVS_STATUS_INV_ALIGNMENT (-5)
361 
369 #define NVS_STATUS_INV_SIZE (-6)
370 
378 #define NVS_STATUS_INV_WRITE (-7)
379 
390 /* Add NVS_CMD_<commands> here */
391 
412 #define NVS_WRITE_ERASE (0x1)
413 
422 #define NVS_WRITE_PRE_VERIFY (0x2)
423 
431 #define NVS_WRITE_POST_VERIFY (0x4)
432 
443 #define NVS_LOCK_WAIT_FOREVER (~(0U))
444 
448 #define NVS_LOCK_NO_WAIT (0U)
449 
464 #define NVS_REGION_NOT_ADDRESSABLE ((void *)(~(0U)))
465 
476 typedef struct
477 {
478  void *custom;
479 } NVS_Params;
480 
488 typedef struct
489 {
490  void *regionBase;
494  size_t regionSize;
495  size_t sectorSize;
497 } NVS_Attrs;
498 
502 typedef struct NVS_Config_ *NVS_Handle;
503 
508 typedef void (*NVS_CloseFxn) (NVS_Handle handle);
509 
514 typedef int_fast16_t (*NVS_ControlFxn) (NVS_Handle handle, uint_fast16_t cmd,
515  uintptr_t arg);
516 
521 typedef int_fast16_t (*NVS_EraseFxn) (NVS_Handle handle, size_t offset,
522  size_t size);
523 
528 typedef void (*NVS_GetAttrsFxn) (NVS_Handle handle, NVS_Attrs *attrs);
529 
534 typedef void (*NVS_InitFxn) (void);
535 
540 typedef NVS_Handle (*NVS_OpenFxn) (uint_least8_t index, NVS_Params *params);
541 
546 typedef int_fast16_t (*NVS_ReadFxn) (NVS_Handle handle, size_t offset,
547  void *buffer, size_t bufferSize);
548 
553 typedef int_fast16_t (*NVS_WriteFxn) (NVS_Handle handle, size_t offset,
554  void *buffer, size_t bufferSize,
555  uint_fast16_t flags);
556 
561 typedef int_fast16_t (*NVS_LockFxn) (NVS_Handle handle, uint32_t timeout);
562 
567 typedef void (*NVS_UnlockFxn) (NVS_Handle handle);
568 
574 typedef struct
575 {
578 
581 
584 
587 
590 
593 
596 
599 
602 
605 } NVS_FxnTable;
606 
618 typedef struct NVS_Config_
619 {
622 
624  void *object;
625 
627  void const *hwAttrs;
628 } NVS_Config;
629 
637 extern void NVS_close(NVS_Handle handle);
638 
658 extern int_fast16_t NVS_control(NVS_Handle handle, uint_fast16_t cmd, uintptr_t arg);
659 
690 extern int_fast16_t NVS_erase(NVS_Handle handle, size_t offset, size_t size);
691 
702 extern void NVS_getAttrs(NVS_Handle handle, NVS_Attrs *attrs);
703 
711 extern void NVS_init(void);
712 
741 extern int_fast16_t NVS_lock(NVS_Handle handle, uint32_t timeout);
742 
756 extern NVS_Handle NVS_open(uint_least8_t index, NVS_Params *params);
757 
764 extern void NVS_Params_init(NVS_Params *params);
765 
782 extern int_fast16_t NVS_read(NVS_Handle handle, size_t offset, void *buffer,
783  size_t bufferSize);
784 
793 extern void NVS_unlock(NVS_Handle handle);
794 
835 extern int_fast16_t NVS_write(NVS_Handle handle, size_t offset, void *buffer,
836  size_t bufferSize, uint_fast16_t flags);
837 
838 #if defined (__cplusplus)
839 }
840 #endif /* defined (__cplusplus) */
841 
843 #endif /* ti_drivers_NVS__include */
void * object
Definition: NVS.h:624
NVS attributes.
Definition: NVS.h:488
NVS_Handle(* NVS_OpenFxn)(uint_least8_t index, NVS_Params *params)
A function pointer to a driver specific implementation of NVS_open().
Definition: NVS.h:540
int_fast16_t NVS_control(NVS_Handle handle, uint_fast16_t cmd, uintptr_t arg)
Function performs implementation specific features on a given NVS_Handle.
void * custom
Definition: NVS.h:478
void(* NVS_GetAttrsFxn)(NVS_Handle handle, NVS_Attrs *attrs)
A function pointer to a driver specific implementation of NVS_getAttrs().
Definition: NVS.h:528
int_fast16_t(* NVS_EraseFxn)(NVS_Handle handle, size_t offset, size_t size)
A function pointer to a driver specific implementation of NVS_erase().
Definition: NVS.h:521
NVS_CloseFxn closeFxn
Definition: NVS.h:577
int_fast16_t NVS_write(NVS_Handle handle, size_t offset, void *buffer, size_t bufferSize, uint_fast16_t flags)
Write data to the NVS region associated with the NVS_Handle.
struct NVS_Config_ * NVS_Handle
A handle that is returned from the NVS_open() call.
Definition: NVS.h:502
NVS_Handle NVS_open(uint_least8_t index, NVS_Params *params)
Open an NVS region for reading and writing.
void NVS_Params_init(NVS_Params *params)
Function to initialize the NVS_Params struct to its defaults.
int_fast16_t NVS_erase(NVS_Handle handle, size_t offset, size_t size)
Erase size bytes of the region beginning at offset bytes from the base of the region referenced by th...
void(* NVS_CloseFxn)(NVS_Handle handle)
A function pointer to a driver specific implementation of NVS_close().
Definition: NVS.h:508
NVS_GetAttrsFxn getAttrsFxn
Definition: NVS.h:586
void NVS_unlock(NVS_Handle handle)
Function to unlock the NVS driver.
size_t sectorSize
Definition: NVS.h:495
size_t regionSize
Definition: NVS.h:494
struct NVS_Config_ NVS_Config
NVS Global configuration.
NVS_ReadFxn readFxn
Definition: NVS.h:598
void * regionBase
Definition: NVS.h:490
NVS_LockFxn lockFxn
Definition: NVS.h:592
void(* NVS_InitFxn)(void)
A function pointer to a driver specific implementation of NVS_init().
Definition: NVS.h:534
NVS_WriteFxn writeFxn
Definition: NVS.h:604
void NVS_init(void)
Function to initialize the NVS module.
NVS Parameters.
Definition: NVS.h:476
NVS_ControlFxn controlFxn
Definition: NVS.h:580
int_fast16_t(* NVS_ReadFxn)(NVS_Handle handle, size_t offset, void *buffer, size_t bufferSize)
A function pointer to a driver specific implementation of NVS_read().
Definition: NVS.h:546
int_fast16_t(* NVS_WriteFxn)(NVS_Handle handle, size_t offset, void *buffer, size_t bufferSize, uint_fast16_t flags)
A function pointer to a driver specific implementation of NVS_write().
Definition: NVS.h:553
NVS_EraseFxn eraseFxn
Definition: NVS.h:583
NVS_InitFxn initFxn
Definition: NVS.h:589
NVS_FxnTable const * fxnTablePtr
Definition: NVS.h:621
void NVS_close(NVS_Handle handle)
Function to close an NVS_Handle.
NVS_OpenFxn openFxn
Definition: NVS.h:595
void(* NVS_UnlockFxn)(NVS_Handle handle)
A function pointer to a driver specific implementation of NVS_unlock().
Definition: NVS.h:567
int_fast16_t NVS_lock(NVS_Handle handle, uint32_t timeout)
Function to lock the NVS driver.
NVS Global configuration.
Definition: NVS.h:618
int_fast16_t NVS_read(NVS_Handle handle, size_t offset, void *buffer, size_t bufferSize)
Read data from the NVS region associated with the NVS_Handle.
void NVS_getAttrs(NVS_Handle handle, NVS_Attrs *attrs)
Function to get the NVS attributes.
void const * hwAttrs
Definition: NVS.h:627
The definition of an NVS function table that contains the required set of functions to control a spec...
Definition: NVS.h:574
int_fast16_t(* NVS_LockFxn)(NVS_Handle handle, uint32_t timeout)
A function pointer to a driver specific implementation of NVS_lock().
Definition: NVS.h:561
NVS_UnlockFxn unlockFxn
Definition: NVS.h:601
int_fast16_t(* NVS_ControlFxn)(NVS_Handle handle, uint_fast16_t cmd, uintptr_t arg)
A function pointer to a driver specific implementation of NVS_control().
Definition: NVS.h:514
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale