NVS.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-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 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. The size is
52  * specified in the device specific driver's hardware attributes. A sector
53  * is the smallest unit of non-volatile storage that can be erased at
54  * one time. The size of the sector, or sector size, is hardware specific and
55  * may be meaningless for some non-volatile storage hardware. For flash
56  * memory devices, the region must be aligned with the sector size. That is,
57  * the region must start on a sector boundary. Additionally, the overall size
58  * of the region must be an integer multiple of the sector size.
59  *
60  * # Thread Safety #
61  *
62  * All NVS APIs are globally thread safe. Consequently, only one write,
63  * erase, or read in the case of SPI flash operation is allowed to be
64  * performed at a time, even for distinct NVS regions. Threads initiating
65  * new NVS writes or erases will block until any current operation completes.
66  *
67  * # Interrupt Latency During Flash Operations #
68  *
69  * When writing to or erasing internal flash, interrupts must be disabled
70  * to avoid executing code in flash while the flash is being reprogrammed.
71  * This constraint is met internally by the driver. User code does not need
72  * to safeguard against this.
73  *
74  * Care must be taken by the user to not perform flash write or erase
75  * operations during latency critical phases of an application. See the
76  * NVS_lock() and NVS_unlock() API descriptions for more information.
77  *
78  * # Usage #
79  *
80  * The NVS driver interface provides device independent APIs, data types,
81  * and macros. The following code example opens an NVS region instance,
82  * writes a string into it, then prints the string after reading it back
83  * into a local buffer, and also prints the string from its directly
84  * addressable location in flash memory.
85  *
86  * @code
87  * NVS_Handle rHandle;
88  * NVS_Attrs regionAttrs;
89  * NVS_Params nvsParams;
90  * uint_fast16_t status;
91  * char buf[32];
92  *
93  * // Initialize the NVS driver
94  * NVS_init();
95  *
96  * //
97  * // Open the NVS region specified by the 0 element in the NVS_config[]
98  * // array defined in Board.c.
99  * // Use default NVS_Params to open this memory region, hence NULL
100  * //
101  * rHandle = NVS_open(Board_NVSINTERNAL, NULL);
102  *
103  * // confirm that the NVS region opened properly
104  * if (rHandle == NULL) {
105  * // Error opening NVS driver
106  * while (1);
107  * }
108  *
109  * // fetch the generic NVS region attributes
110  * NVS_getAttrs(rHandle, &regionAttrs);
111  *
112  * // erase the first sector of the NVS region
113  * status = NVS_erase(rHandle, 0, regionAttrs.sectorSize);
114  * if (status != NVS_STATUS_SUCCESS) {
115  * // Error handling code
116  * }
117  *
118  * // write "Hello" to the base address of region 0, verify after write
119  * status = NVS_write(rHandle, 0, "Hello", strlen("Hello")+1, NVS_POST_VERIFY);
120  * if (status != NVS_STATUS_SUCCESS) {
121  * // Error handling code
122  * }
123  *
124  * // copy "Hello" from region0 into local 'buf'
125  * status = NVS_read(rHandle, 0, buf, strlen("Hello")+1);
126  * if (status != NVS_STATUS_SUCCESS) {
127  * // Error handling code
128  * }
129  *
130  * // print string from fetched NVS storage
131  * System_printf("%s\n", buf);
132  *
133  * // print string using direct address reference if valid
134  * if (regionAttrs.regionBase != NVS_REGION_NOT_ADDRESSABLE) {
135  * System_printf("%s\n", regionAttrs.regionBase);
136  * }
137  *
138  * // close the region
139  * NVS_close(rHandle);
140  *
141  * @endcode
142  *
143  * Details for the example code above are described in the following
144  * subsections.
145  *
146  * ### NVS Driver Configuration #
147  *
148  * In order to use the NVS APIs, the application is required
149  * to provide device-specific NVS configuration in the Board.c file.
150  * The NVS driver interface defines a configuration data structure,
151  * #NVS_Config.
152  *
153  * The application must declare an array of #NVS_Config elements, named
154  * \p NVS_config[]. Each element of \p NVS_config[] is populated with
155  * pointers to a device specific NVS driver implementation's function
156  * table, driver object, and hardware attributes. The hardware attributes
157  * define properties such as the NVS region's base address and size,
158  * Each element in \p NVS_config[] corresponds to a NVS instance, and none
159  * of the elements should have NULL pointers.
160  *
161  * You will need to check the device-specific NVS driver implementation's
162  * header file for example configuration. Please also refer to the
163  * Board.c file of any of the provided examples to see the NVS configuration.
164  *
165  * ### Initializing the NVS Driver #
166  *
167  * NVS_init() must be called before any other NVS APIs. This function
168  * calls the device implementation's NVS initialization function, for each
169  * element of \p NVS_config[].
170  *
171  * ### Opening the NVS Driver #
172  *
173  * Opening a NVS requires four steps:
174  * 1. Optionally create and initialize a #NVS_Params structure.
175  * 2. Fill in the desired parameters.
176  * 3. Call NVS_open(), passing the index of the NVS region in the #NVS_Config
177  * structure, and the address of the #NVS_Params structure.
178  * 4. Check that the #NVS_Handle returned by NVS_open() is non-NULL,
179  * and save it. The handle will be used to read and write to the
180  * NVS you just opened.
181  *
182  * \note Each NVS index can only be opened exclusively. Calling NVS_open()
183  * multiple times with the same index will result in an error. The index can
184  * be re-used if NVS_close() is called first.
185  *
186  *****************************************************************************
187  */
188 
189 #ifndef ti_drivers_NVS__include
190 #define ti_drivers_NVS__include
191 
192 #include <stdbool.h>
193 #include <stddef.h>
194 #include <stdint.h>
195 
196 #if defined (__cplusplus)
197 extern "C" {
198 #endif
199 
217 #define NVS_CMD_RESERVED (32)
218 
231 #define NVS_STATUS_RESERVED (-32)
232 
248 #define NVS_STATUS_SUCCESS (0)
249 
257 #define NVS_STATUS_ERROR (-1)
258 
266 #define NVS_STATUS_UNDEFINEDCMD (-2)
267 
273 #define NVS_STATUS_TIMEOUT (-3)
274 
282 #define NVS_STATUS_INV_OFFSET (-4)
283 
290 #define NVS_STATUS_INV_ALIGNMENT (-5)
291 
299 #define NVS_STATUS_INV_SIZE (-6)
300 
308 #define NVS_STATUS_INV_WRITE (-7)
309 
320 /* Add NVS_CMD_<commands> here */
321 
342 #define NVS_WRITE_ERASE (0x1)
343 
352 #define NVS_WRITE_PRE_VERIFY (0x2)
353 
361 #define NVS_WRITE_POST_VERIFY (0x4)
362 
373 #define NVS_LOCK_WAIT_FOREVER (~(0U))
374 
378 #define NVS_LOCK_NO_WAIT (0U)
379 
394 #define NVS_REGION_NOT_ADDRESSABLE ((void *)(~(0U)))
395 
406 typedef struct NVS_Params {
407  void *custom;
408 } NVS_Params;
409 
417 typedef struct NVS_Attrs {
418  void *regionBase;
422  size_t regionSize;
423  size_t sectorSize;
425 } NVS_Attrs;
426 
430 typedef struct NVS_Config_ *NVS_Handle;
431 
436 typedef void (*NVS_CloseFxn) (NVS_Handle handle);
437 
442 typedef int_fast16_t (*NVS_ControlFxn) (NVS_Handle handle, uint_fast16_t cmd,
443  uintptr_t arg);
444 
449 typedef int_fast16_t (*NVS_EraseFxn) (NVS_Handle handle, size_t offset,
450  size_t size);
451 
456 typedef void (*NVS_GetAttrsFxn) (NVS_Handle handle, NVS_Attrs *attrs);
457 
462 typedef void (*NVS_InitFxn) (void);
463 
468 typedef NVS_Handle (*NVS_OpenFxn) (uint_least8_t index, NVS_Params *params);
469 
474 typedef int_fast16_t (*NVS_ReadFxn) (NVS_Handle handle, size_t offset,
475  void *buffer, size_t bufferSize);
476 
481 typedef int_fast16_t (*NVS_WriteFxn) (NVS_Handle handle, size_t offset,
482  void *buffer, size_t bufferSize,
483  uint_fast16_t flags);
484 
489 typedef int_fast16_t (*NVS_LockFxn) (NVS_Handle handle, uint32_t timeout);
490 
495 typedef void (*NVS_UnlockFxn) (NVS_Handle handle);
496 
502 typedef struct NVS_FxnTable {
505 
508 
511 
514 
517 
520 
523 
526 
529 
532 } NVS_FxnTable;
533 
545 typedef struct NVS_Config_ {
548 
550  void *object;
551 
553  void const *hwAttrs;
554 } NVS_Config;
555 
563 extern void NVS_close(NVS_Handle handle);
564 
584 extern int_fast16_t NVS_control(NVS_Handle handle, uint_fast16_t cmd, uintptr_t arg);
585 
616 extern int_fast16_t NVS_erase(NVS_Handle handle, size_t offset, size_t size);
617 
628 extern void NVS_getAttrs(NVS_Handle handle, NVS_Attrs *attrs);
629 
637 extern void NVS_init(void);
638 
667 extern int_fast16_t NVS_lock(NVS_Handle handle, uint32_t timeout);
668 
682 extern NVS_Handle NVS_open(uint_least8_t index, NVS_Params *params);
683 
690 extern void NVS_Params_init(NVS_Params *params);
691 
708 extern int_fast16_t NVS_read(NVS_Handle handle, size_t offset, void *buffer,
709  size_t bufferSize);
710 
719 extern void NVS_unlock(NVS_Handle handle);
720 
761 extern int_fast16_t NVS_write(NVS_Handle handle, size_t offset, void *buffer,
762  size_t bufferSize, uint_fast16_t flags);
763 
764 #if defined (__cplusplus)
765 }
766 #endif /* defined (__cplusplus) */
767 
769 #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:550
struct NVS_Attrs NVS_Attrs
NVS attributes.
NVS attributes.
Definition: NVS.h:417
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:468
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:407
void(* NVS_GetAttrsFxn)(NVS_Handle handle, NVS_Attrs *attrs)
A function pointer to a driver specific implementation of NVS_getAttrs().
Definition: NVS.h:456
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:449
NVS_CloseFxn closeFxn
Definition: NVS.h:504
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 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:430
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:436
NVS_GetAttrsFxn getAttrsFxn
Definition: NVS.h:513
void NVS_unlock(NVS_Handle handle)
Function to unlock the NVS driver.
size_t sectorSize
Definition: NVS.h:423
size_t regionSize
Definition: NVS.h:422
struct NVS_Config_ NVS_Config
NVS Global configuration.
NVS_ReadFxn readFxn
Definition: NVS.h:525
void * regionBase
Definition: NVS.h:418
NVS_LockFxn lockFxn
Definition: NVS.h:519
void(* NVS_InitFxn)(void)
A function pointer to a driver specific implementation of NVS_init().
Definition: NVS.h:462
NVS_WriteFxn writeFxn
Definition: NVS.h:531
void NVS_init(void)
Function to initialize the NVS module.
NVS Parameters.
Definition: NVS.h:406
NVS_ControlFxn controlFxn
Definition: NVS.h:507
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:474
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:481
NVS_EraseFxn eraseFxn
Definition: NVS.h:510
NVS_InitFxn initFxn
Definition: NVS.h:516
NVS_FxnTable const * fxnTablePtr
Definition: NVS.h:547
void NVS_close(NVS_Handle handle)
Function to close an NVS_Handle.
NVS_OpenFxn openFxn
Definition: NVS.h:522
void(* NVS_UnlockFxn)(NVS_Handle handle)
A function pointer to a driver specific implementation of NVS_unlock().
Definition: NVS.h:495
int_fast16_t NVS_lock(NVS_Handle handle, uint32_t timeout)
Function to lock the NVS driver.
NVS Global configuration.
Definition: NVS.h:545
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:553
The definition of an NVS function table that contains the required set of functions to control a spec...
Definition: NVS.h:502
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:489
NVS_UnlockFxn unlockFxn
Definition: NVS.h:528
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:442
© Copyright 1995-2018, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale