• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  ******************************************************************************
3  *
4  * @file gr55xx_nvds.h
5  *
6  * @brief NVDS API
7  *
8  ******************************************************************************
9  * @attention
10   #####Copyright (c) 2019 GOODIX
11   All rights reserved.
12 
13     Redistribution and use in source and binary forms, with or without
14     modification, are permitted provided that the following conditions are met:
15   * Redistributions of source code must retain the above copyright
16     notice, this list of conditions and the following disclaimer.
17   * Redistributions in binary form must reproduce the above copyright
18     notice, this list of conditions and the following disclaimer in the
19     documentation and/or other materials provided with the distribution.
20   * Neither the name of GOODIX nor the names of its contributors may be used
21     to endorse or promote products derived from this software without
22     specific prior written permission.
23 
24   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27   ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34   POSSIBILITY OF SUCH DAMAGE.
35  *****************************************************************************************
36  */
37 
38 /**
39  * @addtogroup SYSTEM
40  * @{
41  */
42 /**
43  @addtogroup NVDS Non-Volatile Data Storage
44  @{
45  @brief Definitions and prototypes for the NVDS interface.
46 */
47 
48 #ifndef __GR55XX_NVDS_H__
49 #define __GR55XX_NVDS_H__
50 
51 #include <stdint.h>
52 #include <stdbool.h>
53 
54 /** @addtogroup NVDS_DEFINES Defines
55  * @{ */
56 #define NV_TAGCAT_APP       0x4000  /**< NVDS tag mask for user application. */
57 
58 /** @brief Get NVDS tag for user application.
59  * The values of Tag 0x0000 and 0xFFFF are invalid. idx should not be used
60  * as the parameter of NVDS APIs directly. The range of idx is 0x0001~0x3FFF.
61  */
ble_nv_tag_app(uint32_t idx)62 static inline uint32_t ble_nv_tag_app(uint32_t idx)
63 {
64     return (NV_TAGCAT_APP | ((idx) & 0x3FFF));
65 }
66 
67 #define NV_TAG_APP(idx)     ble_nv_tag_app(idx)
68 /** @} */
69 
70 /** @addtogroup NVDS_ENUMERATIONS Enumerations
71  * @{ */
72 /** @brief NVDS Returned Status. */
73 enum NVDS_STATUS {
74     NVDS_SUCCESS,                /**< NVDS succeeds. */
75     NVDS_FAIL,                   /**< NVDS failed. */
76     NVDS_TAG_NOT_EXISTED,        /**< NVDS tag does not exist. */
77     NVDS_SPACE_NOT_ENOUGH,       /**< NVDS space is not enough. */
78     NVDS_LENGTH_OUT_OF_RANGE,    /**< NVDS length out of range. */
79     NVDS_INVALID_PARA,           /**< NVDS invalid params. */
80     NVDS_INVALID_START_ADDR,     /**< NVDS invalid start address. */
81     NVDS_INVALID_SECTORS,        /**< NVDS invalid sector. */
82     NVDS_COMPACT_FAILED,         /**< NVDS failed to compact sectors. */
83     NVDS_STORAGE_ACCESS_FAILED,  /**< NVDS failed to access storage. */
84     NVDS_GC_COMPLETE,            /**< NVDS garbage collection complete. */
85     NVDS_NOT_INIT,               /**< NVDS not initialize. */
86     NVDS_POINTER_NULL            /**< NVDS or driver function repalce error: NULL. */
87 };
88 /** @} */
89 
90 /** @addtogroup NVDS_STRUCTURES Structures
91  * @{ */
92 /** @brief NVDS Item tag. */
93 typedef unsigned short NvdsTag_t;
94 /** @} */
95 
96 /** @addtogroup NVDS_FUNCTIONS Functions
97  * @{ */
98 /**
99  ****************************************************************************************
100  * @brief Initialize the sectors for NVDS.
101  *
102  * @note NVDS locates in the last sector of Flash.
103  *
104  * @param[in] start_addr: Start address of NVDS area. If the value equals zero,
105                           NVDS area will locate in the last sector(s) in flash
106                           memory. If the value does not equal zero, it must be
107                           sector-aligned.
108  * @param[in] sectors:    The number of sectors.
109  *
110  * @return ::NVDS_SUCCESS if successful.
111  * @return error code in ::NVDS_STATUS if not successful.
112  ****************************************************************************************
113  */
114 uint8_t nvds_init(uint32_t start_addr, uint8_t sectors);
115 
116 /**
117  ****************************************************************************************
118  * @brief Read data from NVDS.
119  *
120  * @param[in]     tag:   Valid NVDS item tag.
121  * @param[in,out] p_len: Pointer to the length of data.
122  * @param[out]    p_buf: Data is read into the buffer.
123  *
124  * @return ::NVDS_SUCCESS if successful.
125  * @return error code in ::NVDS_STATUS if not successful.
126  ****************************************************************************************
127  */
128 uint8_t nvds_get(NvdsTag_t tag, uint16_t *p_len, uint8_t *p_buf);
129 
130 /**
131  ****************************************************************************************
132  * @brief Write data to NVDS. If the tag does not exist, create one.
133  *
134  * @param[in] tag:   Valid NVDS item tag.
135  * @param[in] len:   Length of data to be written.
136  * @param[in] p_buf: Data to be written.
137  *
138  * @return ::NVDS_SUCCESS: if successful.
139  * @return error code in ::NVDS_STATUS if not successful.
140  ****************************************************************************************
141  */
142 uint8_t nvds_put(NvdsTag_t tag, uint16_t len, const uint8_t *p_buf);
143 
144 /**
145  ****************************************************************************************
146  * @brief Delete a tag in NVDS
147  *
148  * @param[in] tag: The tag to be deleted.
149  *
150  * @return NVDS_SUCCESS: If the deletion is successful.
151  * @return Error code in ::NVDS_STATUS if not successful.
152  ****************************************************************************************
153  */
154 uint8_t nvds_del(NvdsTag_t tag);
155 
156 /**
157  ****************************************************************************************
158  * @brief Get the length of a tag in NVDS
159  *
160  * @param[in] tag: The tag to get the length.
161  *
162  * @return length: if tag exists.
163  * @return 0: if tag does not exist.
164  ****************************************************************************************
165  */
166 uint16_t nvds_tag_length(NvdsTag_t tag);
167 
168 
169 /** @addtogroup LOCAL_FLASH_FUNCTIONS Local Flash Functions
170  * @{ */
171 /** @brief Flash operation API based on hal flash.*/
172 /**
173  ****************************************************************************************
174  * @brief Erase flash chip.
175  *
176  * @return true if successful.
177  * @return false if not successful.
178  ****************************************************************************************
179  */
180 bool local_hal_flash_erase_chip(void);
181 
182 /**
183  ****************************************************************************************
184  * @brief Erase flash region.
185  *
186  * @note All sectors that have address in range of [addr, addr+len]
187  *       will be erased. If addr is not sector aligned, preceding data
188  *       on the sector that addr belongs to will also be erased.
189  *       If (addr + size) is not sector aligned, the whole sector
190  *       will also be erased.
191  *
192  * @param[in] addr    start address in flash to write data to.
193  * @param[in] size    number of bytes to write.
194  *
195  * @return true if successful.
196  * @return false if not successful.
197  ****************************************************************************************
198  */
199 bool local_hal_flash_erase(const uint32_t addr, const uint32_t size);
200 
201 /**
202  *******************************************************************************
203  * @brief Write flash Memory.
204  *
205  * @param[in]       addr    start address in flash to write data to.
206  * @param[in,out]   buf     buffer of data to write.
207  * @param[in]       size    number of bytes to write.
208  *
209  * @return          number of bytes written
210  *******************************************************************************
211  */
212 uint32_t local_hal_flash_write(const uint32_t addr, const uint8_t *buf, const uint32_t size);
213 
214 /**
215  *******************************************************************************
216  * @brief Read flash Memory.
217  *
218  * @param[in]       addr    start address in flash to read data.
219  * @param[in,out]   buf     buffer to read data to.
220  * @param[in]       size    number of bytes to read.
221  *
222  * @return          number of bytes read
223  *******************************************************************************
224  */
225 uint32_t local_hal_flash_read(const uint32_t addr, uint8_t *buf, const uint32_t size);
226 
227 /**
228  *******************************************************************************
229  * @brief Get Flash information.
230  *
231  * @param[in,out] id Pointer to flash id.
232  * @param[in,out] size Pointer to flash size.
233  *
234  *******************************************************************************
235  */
236 void local_hal_flash_get_info(uint32_t *id, uint32_t *size);
237 
238 /**
239  *******************************************************************************
240  * @brief Get encrypted and decrypted status in write-read operations.
241  *
242  * @return true             Enable encrypted and decrypted.
243  * @return false            Disable encrypted and decrypted.
244  *******************************************************************************
245  */
246 bool local_hal_flash_get_security(void);
247 
248 /**
249  *******************************************************************************
250  * @brief Enable encrypted and decrypted in write-read operations.
251  *
252  * @param[in]       enable  control encrypted and decrypte.
253  *
254  *******************************************************************************
255  */
256 void local_hal_flash_set_security(bool enable);
257 
258 /**
259  *******************************************************************************
260  * @brief Write flash Memory reliably.
261  *
262  * @note It's possible that the data was not written into Flash Memory
263  *       successfully. This function reads the data from Flash Memory to check
264  *       the reliability of programming Flash Memory.
265  * @param[in]       addr    start address in flash to write data to.
266  * @param[in,out]   buf     buffer of data to write.
267  * @param[in]       size    number of bytes to write.
268  *
269  * @return          number of bytes written
270  *******************************************************************************
271  */
272 uint32_t local_hal_flash_write_r(const uint32_t addr, const uint8_t *buf, const uint32_t size);
273 
274 /** @} */
275 
276 /** @} */
277 
278 #endif
279 
280 /** @} */
281 /** @} */
282 
283