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 /*
34  *  ======== NVS.c ========
35  */
36 #include <stdbool.h>
37 #include <stddef.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 
41 #include <ti/drivers/dpl/HwiP.h>
42 #include <ti/drivers/NVS.h>
43 
44 extern NVS_Config NVS_config[];
45 extern const uint8_t NVS_count;
46 
47 static bool isInitialized = false;
48 
49 /* Default NVS parameters structure */
50 const NVS_Params NVS_defaultParams = {
51     NULL    /* custom */
52 };
53 
54 /*
55  *  ======== NVS_close =======
56  */
NVS_close(NVS_Handle handle)57 void NVS_close(NVS_Handle handle)
58 {
59     handle->fxnTablePtr->closeFxn(handle);
60 }
61 
62 /*
63  *  ======== NVS_control ========
64  */
NVS_control(NVS_Handle handle,uint_fast16_t cmd,uintptr_t arg)65 int_fast16_t NVS_control(NVS_Handle handle, uint_fast16_t cmd, uintptr_t arg)
66 {
67     return (handle->fxnTablePtr->controlFxn(handle, cmd, arg));
68 }
69 
70 /*
71  *  ======== NVS_erase =======
72  */
NVS_erase(NVS_Handle handle,size_t offset,size_t size)73 int_fast16_t NVS_erase(NVS_Handle handle, size_t offset, size_t size)
74 {
75     return (handle->fxnTablePtr->eraseFxn(handle, offset, size));
76 }
77 
78 /*
79  *  ======== NVS_getAttrs =======
80  */
NVS_getAttrs(NVS_Handle handle,NVS_Attrs * attrs)81 void NVS_getAttrs(NVS_Handle handle, NVS_Attrs *attrs)
82 {
83     handle->fxnTablePtr->getAttrsFxn(handle, attrs);
84 }
85 
86 /*
87  *  ======== NVS_init =======
88  */
NVS_init(void)89 void NVS_init(void)
90 {
91     uint_least8_t i;
92 
93     /* Call each driver's init function */
94     for (i = 0; i < NVS_count; i++) {
95         NVS_config[i].fxnTablePtr->initFxn();
96     }
97 
98     isInitialized = true;
99 }
100 
101 /*
102  *  ======== NVS_lock =======
103  */
NVS_lock(NVS_Handle handle,uint32_t timeout)104 int_fast16_t NVS_lock(NVS_Handle handle, uint32_t timeout)
105 {
106     return (handle->fxnTablePtr->lockFxn(handle, timeout));
107 }
108 
109 /*
110  *  ======== NVS_open =======
111  */
NVS_open(uint_least8_t index,NVS_Params * params)112 NVS_Handle NVS_open(uint_least8_t index, NVS_Params *params)
113 {
114     NVS_Handle handle = NULL;
115 
116     /* do init if not done yet */
117     if (!isInitialized) {
118         NVS_init();
119     }
120 
121     if (index < NVS_count) {
122         if (params == NULL) {
123             /* No params passed in, so use the defaults */
124             params = (NVS_Params *)&NVS_defaultParams;
125         }
126         handle = NVS_config[index].fxnTablePtr->openFxn(index, params);
127     }
128 
129     return (handle);
130 }
131 
132 /*
133  *  ======== NVS_Params_init =======
134  */
NVS_Params_init(NVS_Params * params)135 void NVS_Params_init(NVS_Params *params)
136 {
137     *params = NVS_defaultParams;
138 }
139 
140 /*
141  *  ======== NVS_read =======
142  */
NVS_read(NVS_Handle handle,size_t offset,void * buffer,size_t bufferSize)143 int_fast16_t NVS_read(NVS_Handle handle, size_t offset, void *buffer,
144              size_t bufferSize)
145 {
146     return (handle->fxnTablePtr->readFxn(handle, offset, buffer, bufferSize));
147 }
148 
149 /*
150  *  ======== NVS_unlock =======
151  */
NVS_unlock(NVS_Handle handle)152 void NVS_unlock(NVS_Handle handle)
153 {
154     handle->fxnTablePtr->unlockFxn(handle);
155 }
156 
157 /*
158  *  ======== NVS_write =======
159  */
NVS_write(NVS_Handle handle,size_t offset,void * buffer,size_t bufferSize,uint_fast16_t flags)160 int_fast16_t NVS_write(NVS_Handle handle, size_t offset, void *buffer,
161               size_t bufferSize, uint_fast16_t flags)
162 {
163     return (handle->fxnTablePtr->writeFxn(handle, offset, buffer,
164                     bufferSize, flags));
165 }
166