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  * # Thread Safety #
60  *
61  * All NVS APIs are globally thread safe. Consequently, only one write,
62  * erase, or read in the case of SPI flash operation is allowed to be
63  * performed at a time, even for distinct NVS regions. Threads initiating
64  * new NVS writes or erases will block until any current operation completes.
65  *
66  * # Interrupt Latency During Flash Operations #
67  *
68  * When writing to or erasing internal flash, interrupts must be disabled
69  * to avoid executing code in flash while the flash is being reprogrammed.
70  * This constraint is met internally by the driver. User code does not need
71  * to safeguard against this.
72  *
73  * Care must be taken by the user to not perform flash write or erase
74  * operations during latency critical phases of an application. See the
75  * NVS_lock() and NVS_unlock() API descriptions for more information.
76  *
77  * # Usage #
78  *
79  * The NVS driver interface provides device independent APIs, data types,
80  * and macros. The following code example opens an NVS region instance,
81  * writes a string into it, then prints the string after reading it back
82  * into a local buffer, and also prints the string from its directly
83  * addressable location in flash memory.
84  *
85  * @code
86  * NVS_Handle rHandle;
87  * NVS_Attrs regionAttrs;
88  * NVS_Params nvsParams;
89  * uint_fast16_t status;
90  * char buf[32];
91  *
92  * // Initialize the NVS driver
93  * NVS_init();
94  *
95  * //
96  * // Open the NVS region specified by the 0th element in the NVS_config[] array
97  * // in Board.c
98  * //
99  * rHandle = NVS_open(0, NULL); // use default NVS_Params to open this flash region
100  *
101  * // confirm that the NVS region opened properly
102  * if (rHandle == NULL) {
103  * ...
104  * }
105  *
106  * // fetch the generic NVS region attributes
107  * NVS_getAttrs(rHandle, &regionAttrs);
108  *
109  * // erase the first sector of the NVS region
110  * status = NVS_erase(rHandle, 0, regionAttrs.sectorSize);
111  * if (status != NVS_STATUS_SUCCESS) {
112  * ...
113  * }
114  *
115  * // write "Hello" to the base address of region 0, verify after write
116  * status = NVS_write(rHandle, 0, "Hello", strlen("Hello")+1, NVS_POST_VERIFY);
117  * if (status != NVS_STATUS_SUCCESS) {
118  * ...
119  * }
120  *
121  * // copy "Hello" from region0 into local 'buf'
122  * status = NVS_read(rHandle, 0, buf, strlen("Hello")+1);
123  * if (status != NVS_STATUS_SUCCESS) {
124  * ...
125  * }
126  *
127  * // print string from fetched NVS storage
128  * System_printf("%s\n", buf);
129  *
130  * // print string using direct address reference if valid
131  * if (regionAttrs.regionBase != NVS_REGION_NOT_ADDRESSABLE) {
132  * System_printf("%s\n", regionAttrs.regionBase);
133  * }
134  *
135  * // close the region
136  * NVS_close(rHandle);
137  *
138  * @endcode
139  *
140  * Details for the example code above are described in the following
141  * subsections.
142  *
143  * ### NVS Driver Configuration #
144  *
145  * In order to use the NVS APIs, the application is required
146  * to provide device-specific NVS configuration in the Board.c file.
147  * The NVS driver interface defines a configuration data structure:
148  *
149  * @code
150  * typedef struct NVS_Config_ {
151  * NVS_FxnTable const *fxnTablePtr;
152  * void *object;
153  * void *hwAttrs;
154  * } NVS_Config;
155  * @endcode
156  *
157  * The application must declare an array of NVS_Config elements, named
158  * NVS_config[]. Each element of NVS_config[] is populated with
159  * pointers to a device specific NVS driver implementation's function
160  * table, driver object, and hardware attributes. The hardware attributes
161  * define properties such as the NVS region's base address and size,
162  * Each element in NVS_config[] corresponds to a NVS instance, and none
163  * of the elements should have NULL pointers.
164  *
165  * You will need to check the device-specific NVS driver implementation's
166  * header file for example configuration. Please also refer to the
167  * Board.c file of any of the provided examples to see the NVS configuration.
168  *
169  * ### Initializing the NVS Driver #
170  *
171  * NVS_init() must be called before any other NVS APIs. This function
172  * calls the device implementation's NVS initialization function, for each
173  * element of NVS_config[].
174  *
175  * ### Opening the NVS Driver #
176  *
177  * Opening a NVS requires four steps:
178  * 1. Optionally create and initialize a NVS_Params structure.
179  * 2. Fill in the desired parameters.
180  * 3. Call NVS_open(), passing the index of the NVS region in the NVS_config
181  * structure, and the address of the NVS_Params structure.
182  * 4. Check that the NVS handle returned by NVS_open() is non-NULL,
183  * and save it. The handle will be used to read and write to the
184  * NVS you just opened.
185  *
186  * Only one NVS index can be used at a time; calling NVS_open() a second
187  * time with the same index previously passed to NVS_open() will result in
188  * an error. You can, though, re-use the index if the instance is closed
189  * via NVS_close().
190  *
191  *****************************************************************************
192  */
193 
194 #ifndef ti_drivers_NVS__include
195 #define ti_drivers_NVS__include
196 
197 #include <stdbool.h>
198 #include <stddef.h>
199 #include <stdint.h>
200 
201 #if defined (__cplusplus)
202 extern "C" {
203 #endif
204 
222 #define NVS_CMD_RESERVED (32)
223 
236 #define NVS_STATUS_RESERVED (-32)
237 
253 #define NVS_STATUS_SUCCESS (0)
254 
262 #define NVS_STATUS_ERROR (-1)
263 
271 #define NVS_STATUS_UNDEFINEDCMD (-2)
272 
278 #define NVS_STATUS_TIMEOUT (-3)
279 
286 #define NVS_STATUS_INV_OFFSET (-4)
287 
294 #define NVS_STATUS_INV_ALIGNMENT (-5)
295 
303 #define NVS_STATUS_INV_SIZE (-6)
304 
312 #define NVS_STATUS_INV_WRITE (-7)
313 
324 /* Add NVS_CMD_<commands> here */
325 
346 #define NVS_WRITE_ERASE (0x1)
347 
356 #define NVS_WRITE_PRE_VERIFY (0x2)
357 
365 #define NVS_WRITE_POST_VERIFY (0x4)
366 
377 #define NVS_LOCK_WAIT_FOREVER (~(0U))
378 
382 #define NVS_LOCK_NO_WAIT (0U)
383 
398 #define NVS_REGION_NOT_ADDRESSABLE ((void *)(~(0U)))
399 
410 typedef struct NVS_Params {
411  void *custom;
412 } NVS_Params;
413 
431 typedef struct NVS_Attrs {
432  void *regionBase;
433  size_t regionSize;
434  size_t sectorSize;
435 } NVS_Attrs;
436 
440 typedef struct NVS_Config_ *NVS_Handle;
441 
446 typedef void (*NVS_CloseFxn) (NVS_Handle handle);
447 
452 typedef int_fast16_t (*NVS_ControlFxn) (NVS_Handle handle, uint_fast16_t cmd,
453  uintptr_t arg);
454 
459 typedef int_fast16_t (*NVS_EraseFxn) (NVS_Handle handle, size_t offset,
460  size_t size);
461 
466 typedef void (*NVS_GetAttrsFxn) (NVS_Handle handle, NVS_Attrs *attrs);
467 
472 typedef void (*NVS_InitFxn) (void);
473 
478 typedef NVS_Handle (*NVS_OpenFxn) (uint_least8_t index, NVS_Params *params);
479 
484 typedef int_fast16_t (*NVS_ReadFxn) (NVS_Handle handle, size_t offset,
485  void *buffer, size_t bufferSize);
486 
491 typedef int_fast16_t (*NVS_WriteFxn) (NVS_Handle handle, size_t offset,
492  void *buffer, size_t bufferSize,
493  uint_fast16_t flags);
494 
499 typedef int_fast16_t (*NVS_LockFxn) (NVS_Handle handle, uint32_t timeout);
500 
505 typedef void (*NVS_UnlockFxn) (NVS_Handle handle);
506 
512 typedef struct NVS_FxnTable {
515 
518 
521 
524 
527 
530 
533 
536 
539 
542 } NVS_FxnTable;
543 
555 typedef struct NVS_Config_ {
558 
560  void *object;
561 
563  void const *hwAttrs;
564 } NVS_Config;
565 
573 extern void NVS_close(NVS_Handle handle);
574 
594 extern int_fast16_t NVS_control(NVS_Handle handle, uint_fast16_t cmd, uintptr_t arg);
595 
626 extern int_fast16_t NVS_erase(NVS_Handle handle, size_t offset, size_t size);
627 
635 extern void NVS_getAttrs(NVS_Handle handle, NVS_Attrs *attrs);
636 
644 extern void NVS_init(void);
645 
674 extern int_fast16_t NVS_lock(NVS_Handle handle, uint32_t timeout);
675 
690 extern NVS_Handle NVS_open(uint_least8_t index, NVS_Params *params);
691 
701 extern void NVS_Params_init(NVS_Params *params);
702 
719 extern int_fast16_t NVS_read(NVS_Handle handle, size_t offset, void *buffer,
720  size_t bufferSize);
721 
730 extern void NVS_unlock(NVS_Handle handle);
731 
772 extern int_fast16_t NVS_write(NVS_Handle handle, size_t offset, void *buffer,
773  size_t bufferSize, uint_fast16_t flags);
774 
775 #if defined (__cplusplus)
776 }
777 #endif /* defined (__cplusplus) */
778 
780 #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:560
struct NVS_Attrs NVS_Attrs
NVS attributes.
NVS attributes.
Definition: NVS.h:431
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:478
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:411
void(* NVS_GetAttrsFxn)(NVS_Handle handle, NVS_Attrs *attrs)
A function pointer to a driver specific implementation of NVS_getAttrs().
Definition: NVS.h:466
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:459
NVS_CloseFxn closeFxn
Definition: NVS.h:514
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:440
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 &#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:446
NVS_GetAttrsFxn getAttrsFxn
Definition: NVS.h:523
void NVS_unlock(NVS_Handle handle)
Function to unlock the NVS driver.
size_t sectorSize
Definition: NVS.h:434
size_t regionSize
Definition: NVS.h:433
struct NVS_Config_ NVS_Config
NVS Global configuration.
NVS_ReadFxn readFxn
Definition: NVS.h:535
void * regionBase
Definition: NVS.h:432
NVS_LockFxn lockFxn
Definition: NVS.h:529
void(* NVS_InitFxn)(void)
A function pointer to a driver specific implementation of NVS_init().
Definition: NVS.h:472
NVS_WriteFxn writeFxn
Definition: NVS.h:541
void NVS_init(void)
Function to initialize the NVS module.
NVS Parameters.
Definition: NVS.h:410
NVS_ControlFxn controlFxn
Definition: NVS.h:517
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:484
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:491
NVS_EraseFxn eraseFxn
Definition: NVS.h:520
NVS_InitFxn initFxn
Definition: NVS.h:526
NVS_FxnTable const * fxnTablePtr
Definition: NVS.h:557
void NVS_close(NVS_Handle handle)
Function to close an NVS handle.
NVS_OpenFxn openFxn
Definition: NVS.h:532
void(* NVS_UnlockFxn)(NVS_Handle handle)
A function pointer to a driver specific implementation of NVS_unlock().
Definition: NVS.h:505
int_fast16_t NVS_lock(NVS_Handle handle, uint32_t timeout)
Function to lock the NVS driver.
NVS Global configuration.
Definition: NVS.h:555
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:563
The definition of an NVS function table that contains the required set of functions to control a spec...
Definition: NVS.h:512
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:499
NVS_UnlockFxn unlockFxn
Definition: NVS.h:538
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:452
Copyright 2017, Texas Instruments Incorporated