NVS.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2017, 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  * To use the NVS driver, ensure that the correct driver library for your
37  * device is linked in and include this header file as follows:
38  * @code
39  * #include <ti/drivers/NVS.h>
40  * @endcode
41  *
42  * This module serves as the main interface for applications. Its purpose
43  * is to redirect the NVS APIs to specific driver implementations
44  * which are specified using a pointer to a #NVS_FxnTable.
45  *
46  * # Overview #
47  *
48  * The NVS module allows you to manage non-volatile memory. Using the
49  * NVS APIs, you can read and write data from and to persistent storage.
50  *
51  * Each NVS object manages a 'region' of non-volatile memory of a size
52  * specified in the NVS object's hardware attributes. A 'sector' refers to
53  * the smallest unit of non-volatile storage that can be erased at one time,
54  * and the 'sectorSize' is the size of this unit. Flash sector size is
55  * hardware specific and may be meaningless for some persistent storage
56  * systems. However, in the case of flash memory devices, the size
57  * of a managed region must be an integer multiple of the sector size.
58  *
59  * All NVS APIs are globally thread safe. Consequently, only one write, or
60  * erase, (or read in the case of SPI flash) operation is allowed to be
61  * performed at a time, even for distinct NVS regions. Threads initiating
62  * new NVS writes or erases will block until any current operation completes.
63  *
64  * # Usage #
65  *
66  * The NVS driver interface provides device independent APIs, data types,
67  * and macros. The following code example opens an NVS region instance,
68  * writes a string into it, then prints the string after reading it back
69  * into a local buffer, and also prints the string from its directly
70  * addressable location in flash memory.
71  *
72  * @code
73  * NVS_Handle rHandle;
74  * NVS_Attrs regionAttrs;
75  * NVS_Params nvsParams;
76  * uint_fast16_t status;
77  * char buf[32];
78  *
79  * // Initialize the NVS driver
80  * NVS_init();
81  *
82  * //
83  * // Open the NVS region specified by the 0th element in the NVS_config[] array
84  * // in Board.c
85  * //
86  * rHandle = NVS_open(0, NULL); // use default NVS_Params to open this flash region
87  *
88  * // confirm that the NVS region opened properly
89  * if (rHandle == NULL) {
90  * ...
91  * }
92  *
93  * // fetch the generic NVS region attributes
94  * NVS_getAttrs(rHandle, &regionAttrs);
95  *
96  * // erase the first sector of the NVS region
97  * status = NVS_erase(rHandle, 0, regionAttrs.sectorSize);
98  * if (status != NVS_STATUS_SUCCESS) {
99  * ...
100  * }
101  *
102  * // write "Hello" to the base address of region 0, verify after write
103  * status = NVS_write(rHandle, 0, "Hello", strlen("Hello")+1, NVS_POST_VERIFY);
104  * if (status != NVS_STATUS_SUCCESS) {
105  * ...
106  * }
107  *
108  * // copy "Hello" from region0 into local 'buf'
109  * status = NVS_read(rHandle, 0, buf, strlen("Hello")+1);
110  * if (status != NVS_STATUS_SUCCESS) {
111  * ...
112  * }
113  *
114  * // print string from fetched NVS storage
115  * System_printf("%s\n", buf);
116  *
117  * // print string using direct address reference if valid
118  * if (regionAttrs.regionBase != NVS_REGION_NOT_ADDRESSABLE) {
119  * System_printf("%s\n", regionAttrs.regionBase);
120  * }
121  *
122  * // close the region
123  * NVS_close(rHandle);
124  *
125  * @endcode
126  *
127  * Details for the example code above are described in the following
128  * subsections.
129  *
130  * ### NVS Driver Configuration #
131  *
132  * In order to use the NVS APIs, the application is required
133  * to provide device-specific NVS configuration in the Board.c file.
134  * The NVS driver interface defines a configuration data structure:
135  *
136  * @code
137  * typedef struct NVS_Config_ {
138  * NVS_FxnTable const *fxnTablePtr;
139  * void *object;
140  * void *hwAttrs;
141  * } NVS_Config;
142  * @endcode
143  *
144  * The application must declare an array of NVS_Config elements, named
145  * NVS_config[]. Each element of NVS_config[] is populated with
146  * pointers to a device specific NVS driver implementation's function
147  * table, driver object, and hardware attributes. The hardware attributes
148  * define properties such as the NVS region's base address and size,
149  * Each element in NVS_config[] corresponds to a NVS instance, and none
150  * of the elements should have NULL pointers.
151  *
152  * You will need to check the device-specific NVS driver implementation's
153  * header file for example configuration. Please also refer to the
154  * Board.c file of any of the provided examples to see the NVS configuration.
155  *
156  * ### Initializing the NVS Driver #
157  *
158  * NVS_init() must be called before any other NVS APIs. This function
159  * calls the device implementation's NVS initialization function, for each
160  * element of NVS_config[].
161  *
162  * ### Opening the NVS Driver #
163  *
164  * Opening a NVS requires four steps:
165  * 1. Optionally create and initialize a NVS_Params structure.
166  * 2. Fill in the desired parameters.
167  * 3. Call NVS_open(), passing the index of the NVS region in the NVS_config
168  * structure, and the address of the NVS_Params structure.
169  * 4. Check that the NVS handle returned by NVS_open() is non-NULL,
170  * and save it. The handle will be used to read and write to the
171  * NVS you just opened.
172  *
173  * Only one NVS index can be used at a time; calling NVS_open() a second
174  * time with the same index previously passed to NVS_open() will result in
175  * an error. You can, though, re-use the index if the instance is closed
176  * via NVS_close().
177  *
178  *****************************************************************************
179  */
180 
181 #ifndef ti_drivers_NVS__include
182 #define ti_drivers_NVS__include
183 
184 #include <stdint.h>
185 #include <stddef.h>
186 #include <stdbool.h>
187 
188 #if defined (__cplusplus)
189 extern "C" {
190 #endif
191 
209 #define NVS_CMD_RESERVED (32)
210 
223 #define NVS_STATUS_RESERVED (-32)
224 
240 #define NVS_STATUS_SUCCESS (0)
241 
249 #define NVS_STATUS_ERROR (-1)
250 
258 #define NVS_STATUS_UNDEFINEDCMD (-2)
259 
265 #define NVS_STATUS_TIMEOUT (-3)
266 
273 #define NVS_STATUS_INV_OFFSET (-4)
274 
281 #define NVS_STATUS_INV_ALIGNMENT (-5)
282 
290 #define NVS_STATUS_INV_SIZE (-6)
291 
299 #define NVS_STATUS_INV_WRITE (-7)
300 
311 /* Add NVS_CMD_<commands> here */
312 
333 #define NVS_WRITE_ERASE (0x1)
334 
343 #define NVS_WRITE_PRE_VERIFY (0x2)
344 
352 #define NVS_WRITE_POST_VERIFY (0x4)
353 
364 #define NVS_LOCK_WAIT_FOREVER (~(0U))
365 
369 #define NVS_LOCK_NO_WAIT (0U)
370 
385 #define NVS_REGION_NOT_ADDRESSABLE ((void *)(~(0U)))
386 
397 typedef struct NVS_Params {
398  void *custom;
399 } NVS_Params;
400 
418 typedef struct NVS_Attrs {
419  void *regionBase;
420  size_t regionSize;
421  size_t sectorSize;
422 } NVS_Attrs;
423 
427 typedef struct NVS_Config_ *NVS_Handle;
428 
433 typedef void (*NVS_CloseFxn) (NVS_Handle handle);
434 
439 typedef int_fast16_t (*NVS_ControlFxn) (NVS_Handle handle, uint_fast16_t cmd,
440  uintptr_t arg);
441 
446 typedef int_fast16_t (*NVS_EraseFxn) (NVS_Handle handle, size_t offset,
447  size_t size);
448 
453 typedef void (*NVS_GetAttrsFxn) (NVS_Handle handle, NVS_Attrs *attrs);
454 
459 typedef void (*NVS_InitFxn) (void);
460 
465 typedef NVS_Handle (*NVS_OpenFxn) (uint_least8_t index, NVS_Params *params);
466 
471 typedef int_fast16_t (*NVS_ReadFxn) (NVS_Handle handle, size_t offset,
472  void *buffer, size_t bufferSize);
473 
478 typedef int_fast16_t (*NVS_WriteFxn) (NVS_Handle handle, size_t offset,
479  void *buffer, size_t bufferSize,
480  uint_fast16_t flags);
481 
486 typedef int_fast16_t (*NVS_LockFxn) (NVS_Handle handle, uint32_t timeout);
487 
492 typedef void (*NVS_UnlockFxn) (NVS_Handle handle);
493 
499 typedef struct NVS_FxnTable {
502 
505 
508 
511 
514 
517 
520 
523 
526 
529 } NVS_FxnTable;
530 
542 typedef struct NVS_Config_ {
545 
547  void *object;
548 
550  void const *hwAttrs;
551 } NVS_Config;
552 
560 extern void NVS_close(NVS_Handle handle);
561 
581 extern int_fast16_t NVS_control(NVS_Handle handle, uint_fast16_t cmd, uintptr_t arg);
582 
606 extern int_fast16_t NVS_erase(NVS_Handle handle, size_t offset, size_t size);
607 
615 extern void NVS_getAttrs(NVS_Handle handle, NVS_Attrs *attrs);
616 
624 extern void NVS_init(void);
625 
650 extern int_fast16_t NVS_lock(NVS_Handle handle, uint32_t timeout);
651 
666 extern NVS_Handle NVS_open(uint_least8_t index, NVS_Params *params);
667 
677 extern void NVS_Params_init(NVS_Params *params);
678 
695 extern int_fast16_t NVS_read(NVS_Handle handle, size_t offset, void *buffer,
696  size_t bufferSize);
697 
708 extern void NVS_unlock(NVS_Handle handle);
709 
743 extern int_fast16_t NVS_write(NVS_Handle handle, size_t offset, void *buffer,
744  size_t bufferSize, uint_fast16_t flags);
745 
746 #if defined (__cplusplus)
747 }
748 #endif /* defined (__cplusplus) */
749 
751 #endif /* ti_drivers_NVS__include */
struct NVS_FxnTable NVS_FxnTable
The definition of an NVS function table that contains the required set of functions to control a spec...
void * object
Definition: NVS.h:547
struct NVS_Attrs NVS_Attrs
NVS attributes.
NVS attributes.
Definition: NVS.h:418
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:465
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:398
void(* NVS_GetAttrsFxn)(NVS_Handle handle, NVS_Attrs *attrs)
A function pointer to a driver specific implementation of NVS_getAttrs().
Definition: NVS.h:453
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:446
NVS_CloseFxn closeFxn
Definition: NVS.h:501
struct NVS_Params NVS_Params
NVS Parameters.
int_fast16_t NVS_write(NVS_Handle handle, size_t offset, void *buffer, size_t bufferSize, uint_fast16_t flags)
Write data to an NVS region.
struct NVS_Config_ * NVS_Handle
A handle that is returned from the NVS_open() call.
Definition: NVS.h:427
NVS_Handle NVS_open(uint_least8_t index, NVS_Params *params)
Get 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 &#39;size&#39; bytes of the region beginning at &#39;offset&#39; bytes from the base of the region referenced b...
void(* NVS_CloseFxn)(NVS_Handle handle)
A function pointer to a driver specific implementation of NVS_close().
Definition: NVS.h:433
NVS_GetAttrsFxn getAttrsFxn
Definition: NVS.h:510
void NVS_unlock(NVS_Handle handle)
Function to unlock an NVS region.
size_t sectorSize
Definition: NVS.h:421
size_t regionSize
Definition: NVS.h:420
struct NVS_Config_ NVS_Config
NVS Global configuration.
NVS_ReadFxn readFxn
Definition: NVS.h:522
void * regionBase
Definition: NVS.h:419
NVS_LockFxn lockFxn
Definition: NVS.h:516
void(* NVS_InitFxn)(void)
A function pointer to a driver specific implementation of NVS_init().
Definition: NVS.h:459
NVS_WriteFxn writeFxn
Definition: NVS.h:528
void NVS_init(void)
Function to initialize the NVS module.
NVS Parameters.
Definition: NVS.h:397
NVS_ControlFxn controlFxn
Definition: NVS.h:504
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:471
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:478
NVS_EraseFxn eraseFxn
Definition: NVS.h:507
NVS_InitFxn initFxn
Definition: NVS.h:513
NVS_FxnTable const * fxnTablePtr
Definition: NVS.h:544
void NVS_close(NVS_Handle handle)
Function to close an NVS handle.
NVS_OpenFxn openFxn
Definition: NVS.h:519
void(* NVS_UnlockFxn)(NVS_Handle handle)
A function pointer to a driver specific implementation of NVS_unlock().
Definition: NVS.h:492
int_fast16_t NVS_lock(NVS_Handle handle, uint32_t timeout)
Function to lock the NVS driver.
NVS Global configuration.
Definition: NVS.h:542
int_fast16_t NVS_read(NVS_Handle handle, size_t offset, void *buffer, size_t bufferSize)
Read data from an NVS region.
void NVS_getAttrs(NVS_Handle handle, NVS_Attrs *attrs)
Function to get the NVS attributes.
void const * hwAttrs
Definition: NVS.h:550
The definition of an NVS function table that contains the required set of functions to control a spec...
Definition: NVS.h:499
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:486
NVS_UnlockFxn unlockFxn
Definition: NVS.h:525
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:439
Copyright 2017, Texas Instruments Incorporated