pstorage.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /**@file
  13. *
  14. * @defgroup persistent_storage Persistent Storage Interface
  15. * @{
  16. * @ingroup app_common
  17. * @brief Abstracted flash interface.
  18. *
  19. * @details An abstracted interface is provided by the module to easily port the application and
  20. * SDK modules to an alternate option. This ensures that the SDK and application are moved
  21. * to alternate persistent storage instead of the one provided by default.
  22. */
  23. #ifndef PSTORAGE_H__
  24. #define PSTORAGE_H__
  25. #include "pstorage_platform.h"
  26. /**@defgroup ps_opcode Persistent Storage Access Operation Codes
  27. * @{
  28. * @brief Persistent Storage Access Operation Codes.
  29. *
  30. * @details Persistent Storage Access Operation Codes are used by Persistent storage operation
  31. * completion callback @ref pstorage_ntf_cb_t to identify the operation type requested by
  32. * the application.
  33. */
  34. #define PSTORAGE_STORE_OP_CODE 0x01 /**< Store Operation type. */
  35. #define PSTORAGE_LOAD_OP_CODE 0x02 /**< Load Operation type. */
  36. #define PSTORAGE_CLEAR_OP_CODE 0x03 /**< Clear Operation type. */
  37. #define PSTORAGE_UPDATE_OP_CODE 0x04 /**< Update Operation type. */
  38. /**@} */
  39. /**@defgroup pstorage_data_types Persistent Memory Interface Data Types
  40. * @{
  41. * @brief Data Types needed for interfacing with persistent memory.
  42. *
  43. * @details Data Types needed for interfacing with persistent memory.
  44. */
  45. /**@brief Persistent storage operation completion callback function type.
  46. *
  47. * @details The persistent storage operation completion callback is used by the interface to report
  48. * success or failure of a flash operation. Since data is not copied for a store operation,
  49. * a callback is an indication that the resident memory can now be reused or freed.
  50. *
  51. * @param[in] handle Identifies the module and block for the callback that is received.
  52. * @param[in] op_code Identifies the operation for the event that is notified.
  53. * @param[in] result Identifies the result of a flash access operation. NRF_SUCCESS implies
  54. * operation succeeded.
  55. *
  56. * @note Unmanaged (abnormal behaviour) error codes from the SoftDevice flash
  57. * access API are forwarded as is and are expected to be handled by the
  58. * application. For details refer to the implementation file and corresponding
  59. * SoftDevice flash API documentation.
  60. *
  61. * @param[in] p_data Identifies the application data pointer. For a store operation, this points
  62. * to the resident source of application memory that the application can now
  63. * free or reuse. When there is a clear operation, this is NULL since no
  64. * application pointer is needed for this operation.
  65. * @param[in] data_len Length data the application provided for the operation.
  66. */
  67. typedef void (*pstorage_ntf_cb_t)(pstorage_handle_t * p_handle,
  68. uint8_t op_code,
  69. uint32_t result,
  70. uint8_t * p_data,
  71. uint32_t data_len);
  72. /**@brief Struct containing module registration context. */
  73. typedef struct
  74. {
  75. pstorage_ntf_cb_t cb; /**< Persistent storage operation completion callback function @ref pstorage_ntf_cb_t. */
  76. pstorage_size_t block_size; /**< Desired block size for persistent memory storage. For example, if a module has a table with 10 entries, and each entry is 64 bytes in size,
  77. * it can request 10 blocks with a block size of 64 bytes. The module can also request one block that is 640 bytes depending
  78. * on how it would like to access or alter the memory in persistent memory.
  79. * The first option is preferred when it is a single entry that needs to be updated often and doesn't impact the other entries.
  80. * The second option is preferred when table entries are not changed individually but have a common point of loading and storing
  81. * data. */
  82. pstorage_size_t block_count; /** Number of blocks requested by the module; minimum values is 1. */
  83. } pstorage_module_param_t;
  84. /**@} */
  85. /**@defgroup pstorage_routines Persistent Storage Access Routines
  86. * @{
  87. * @brief Functions/Interface SDK modules used to persistently store data.
  88. *
  89. * @details Interface for the Application and SDK modules to load/store information persistently.
  90. * Note: While implementation of each of the persistent storage access functions
  91. * depends on the system and is specific to system/solution, the signature of the
  92. * interface routines should not be altered.
  93. */
  94. /**@brief Function for initializing the module.
  95. *
  96. * @details Function for initializing the module. This function is called once before any other APIs
  97. * of the module are used.
  98. *
  99. * @retval NRF_SUCCESS Operation success.
  100. */
  101. uint32_t pstorage_init(void);
  102. /**@brief Function for registering with persistent storage interface.
  103. *
  104. * @param[in] p_module_param Module registration parameter.
  105. * @param[out] p_block_id Block identifier to identify persistent memory blocks when
  106. * registration succeeds. Application is expected to use the block IDs
  107. * for subsequent operations on requested persistent memory. Maximum
  108. * registrations permitted is determined by the configuration of the
  109. * parameter PSTORAGE_NUM_OF_PAGES. If more than one memory block is
  110. * requested, the identifier provided here is the base identifier for the
  111. * first block and used to identify the subsequent block. The application
  112. * uses \@ref pstorage_block_identifier_get with this base identifier and
  113. * block number. Therefore if 10 blocks of size 64 are requested and the
  114. * application wishes to store memory in the 6th block, it shall use
  115. * \@ref pstorage_block_identifier_get with the base ID and provide a
  116. * block number of 5. This way the application is only expected to
  117. * remember the base block identifier.
  118. *
  119. * @retval NRF_SUCCESS Operation success.
  120. * @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
  121. * initialization.
  122. * @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
  123. * @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
  124. * @retval NRF_ERROR_NO_MEM Operation failure. Additional registrations can't be
  125. * supported.
  126. */
  127. uint32_t pstorage_register(pstorage_module_param_t * p_module_param,
  128. pstorage_handle_t * p_block_id);
  129. /**@brief Function for getting block ID with reference to base block identifier provided at the time
  130. * of registration.
  131. *
  132. * @details Function to get the block ID with reference to base block identifier provided at the
  133. * time of registration.
  134. * If more than one memory block was requested when registering, the identifier provided
  135. * here is the base identifier for the first block which is used to identify subsequent
  136. * blocks. The application shall use this routine to get the block identifier, providing
  137. * input as base identifier and block number. Therefore, if 10 blocks of size 64 are
  138. * requested and the application wishes to store memory in the 6th block, it shall use
  139. * \@ref pstorage_block_identifier_get with the base ID and provide a block number of 5.
  140. * This way the application is only expected to remember the base block identifier.
  141. *
  142. * @param[in] p_base_id Base block ID received at the time of registration.
  143. * @param[in] block_num Block Number, with first block numbered zero.
  144. * @param[out] p_block_id Block identifier for the block number requested when the API succeeds.
  145. *
  146. * @retval NRF_SUCCESS Operation success.
  147. * @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
  148. * initialization.
  149. * @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
  150. * @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
  151. */
  152. uint32_t pstorage_block_identifier_get(pstorage_handle_t * p_base_id,
  153. pstorage_size_t block_num,
  154. pstorage_handle_t * p_block_id);
  155. /**@brief Function for persistently storing data of length 'size' contained in the 'p_src' address
  156. * in the storage module at 'p_dest' address. Equivalent to Storage Write.
  157. *
  158. * @param[in] p_dest Destination address where data is to be stored persistently.
  159. * @param[in] p_src Source address containing data to be stored. API assumes this to be resident
  160. * memory and no intermediate copy of data is made by the API. Must be word
  161. * aligned.
  162. * @param[in] size Size of data to be stored expressed in bytes. Must be word aligned and size +
  163. * offset must be <= block size.
  164. * @param[in] offset Offset in bytes to be applied when writing to the block.
  165. * For example, if within a block of 100 bytes, the application wishes to
  166. * write 20 bytes at an offset of 12, then this field should be set to 12.
  167. * Must be word aligned.
  168. *
  169. * @retval NRF_SUCCESS Operation success.
  170. * @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
  171. * initialization.
  172. * @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
  173. * @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
  174. * @retval NRF_ERROR_INVALID_ADDR Operation failure. Parameter is not aligned.
  175. * @retval NRF_ERROR_NO_MEM Operation failure. No storage space available.
  176. *
  177. * @warning No copy of the data is made, meaning memory provided for the data source that is to
  178. * be written to flash cannot be freed or reused by the application until this procedure
  179. * is complete. The application is notified when the procedure is finished using the
  180. * notification callback registered by the application.
  181. */
  182. uint32_t pstorage_store(pstorage_handle_t * p_dest,
  183. uint8_t * p_src,
  184. pstorage_size_t size,
  185. pstorage_size_t offset);
  186. /**@brief Function for updating persistently stored data of length 'size' contained in the 'p_src'
  187. * address in the storage module at 'p_dest' address.
  188. *
  189. * @param[in] p_dest Destination address where data is to be updated.
  190. * @param[in] p_src Source address containing data to be stored. API assumes this to be resident
  191. * memory and no intermediate copy of data is made by the API.
  192. * @param[in] size Size of data to be stored expressed in bytes. Must be word aligned and size +
  193. * offset must be <= block size.
  194. * @param[in] offset Offset in bytes to be applied when writing to the block.
  195. * For example, if within a block of 100 bytes, the application wishes to
  196. * write 20 bytes at an offset of 12 bytes, then this field should be set to 12.
  197. * Must be word aligned.
  198. *
  199. * @retval NRF_SUCCESS Operation success.
  200. * @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
  201. * initialization.
  202. * @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
  203. * @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
  204. * @retval NRF_ERROR_INVALID_ADDR Operation failure. Parameter is not aligned.
  205. * @retval NRF_ERROR_NO_MEM Operation failure. No storage space available.
  206. *
  207. * @warning No copy of the data is made, meaning memory provided for the data source that is to
  208. * be written to flash cannot be freed or reused by the application until this procedure
  209. * is complete. The application is notified when the procedure is finished using the
  210. * notification callback registered by the application.
  211. */
  212. uint32_t pstorage_update(pstorage_handle_t * p_dest,
  213. uint8_t * p_src,
  214. pstorage_size_t size,
  215. pstorage_size_t offset);
  216. /**@brief Function for loading persistently stored data of length 'size' from 'p_src' address
  217. * to 'p_dest' address. Equivalent to Storage Read.
  218. *
  219. * @param[in] p_dest Destination address where persistently stored data is to be loaded.
  220. * @param[in] p_src Source where data is loaded from persistent memory.
  221. * @param[in] size Size of data to be loaded from persistent memory expressed in bytes.
  222. * Should be word aligned.
  223. * @param[in] offset Offset in bytes, to be applied when loading from the block.
  224. * For example, if within a block of 100 bytes, the application wishes to
  225. * load 20 bytes from offset of 12 bytes, then this field should be set to 12.
  226. * Should be word aligned.
  227. *
  228. * @retval NRF_SUCCESS Operation success.
  229. * @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
  230. * initialization.
  231. * @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
  232. * @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
  233. * @retval NRF_ERROR_INVALID_ADDR Operation failure. Parameter is not aligned.
  234. * @retval NRF_ERROR_NO_MEM Operation failure. No storage space available.
  235. */
  236. uint32_t pstorage_load(uint8_t * p_dest,
  237. pstorage_handle_t * p_src,
  238. pstorage_size_t size,
  239. pstorage_size_t offset);
  240. /**@brief Function for clearing data in persistent memory.
  241. *
  242. * @param[in] p_base_id Base block identifier in persistent memory that needs to be cleared;
  243. * equivalent to an Erase Operation.
  244. * @param[in] size Size of data to be cleared from persistent memory expressed in bytes.
  245. * This parameter is to provision for clearing of certain blocks
  246. * of memory, or all memory blocks in a registered module. If the total size
  247. * of the application module is used (blocks * block size) in combination with
  248. * the identifier for the first block in the module, all blocks in the
  249. * module will be erased. Must be multiple of block size.
  250. *
  251. * @retval NRF_SUCCESS Operation success.
  252. * @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
  253. * initialization.
  254. * @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
  255. * @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
  256. * @retval NRF_ERROR_INVALID_ADDR Operation failure. Parameter is not aligned.
  257. * @retval NRF_ERROR_NO_MEM Operation failure. No storage space available.
  258. *
  259. * @note Clear operations may take time. This API however, does not block until the clear
  260. * procedure is complete. The application is notified of procedure completion using
  261. * a notification callback registered by the application. The 'result' parameter of the
  262. * callback indicates if the procedure was successful or not.
  263. */
  264. uint32_t pstorage_clear(pstorage_handle_t * p_base_id, pstorage_size_t size);
  265. /**@brief Function for getting the number of pending operations with the module.
  266. *
  267. * @param[out] p_count Number of storage operations pending with the module. If 0, there are no
  268. * outstanding requests.
  269. *
  270. * @retval NRF_SUCCESS Operation success.
  271. * @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
  272. * initialization.
  273. * @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
  274. */
  275. uint32_t pstorage_access_status_get(uint32_t * p_count);
  276. #ifdef PSTORAGE_RAW_MODE_ENABLE
  277. /**@brief Function for registering with the persistent storage interface.
  278. *
  279. * @param[in] p_module_param Module registration parameter.
  280. * @param[out] p_block_id Block identifier used to identify persistent memory blocks upon
  281. * successful registration. The application is expected to use the block
  282. * IDs for subsequent operations on requested persistent memory. When
  283. * more than one memory block is requested, this identifier is the base
  284. * identifier for the first block and used to identify subsequent blocks.
  285. * The application shall use \@ref pstorage_block_identifier_get with
  286. * this base identifier and block number. Therefore if 10 blocks of size
  287. * 64 are requested and the application wishes to store memory in the 6th
  288. * block, it shall use \@ref pstorage_block_identifier_get with the base
  289. * ID and provide a block number of 5. Therefore, the application is only
  290. * expected to remember the base block identifier.
  291. *
  292. * @retval NRF_SUCCESS Operation success.
  293. * @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
  294. * initialization.
  295. * @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
  296. * @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
  297. * @retval NRF_ERROR_NO_MEM Operation failure. No storage space available.
  298. */
  299. uint32_t pstorage_raw_register(pstorage_module_param_t * p_module_param,
  300. pstorage_handle_t * p_block_id);
  301. /**@brief Function for persistently storing data of length 'size' contained in 'p_src' address in
  302. * storage module at 'p_dest' address. Equivalent to Storage Write.
  303. *
  304. * @param[in] p_dest Destination address where data is to be stored persistently.
  305. * @param[in] p_src Source address containing data to be stored. The API assumes this is resident
  306. * memory and no intermediate copy of data is made by the API. Must be word
  307. * aligned.
  308. * @param[in] size Size of data to be stored expressed in bytes. Must be word aligned.
  309. * @param[in] offset Offset in bytes to be applied when writing to the block.
  310. * For example, if within a block of 100 bytes, the application wishes to
  311. * write 20 bytes at an offset of 12 bytes, this field should be set to 12.
  312. * Must be word aligned.
  313. *
  314. * @retval NRF_SUCCESS Operation success.
  315. * @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
  316. * initialization.
  317. * @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
  318. * @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
  319. * @retval NRF_ERROR_INVALID_ADDR Operation failure. Parameter is not aligned.
  320. * @retval NRF_ERROR_NO_MEM Operation failure. No storage space available.
  321. *
  322. * @warning No copy of the data is made, meaning memory provided for data source that is to be
  323. * written to flash cannot be freed or reused by the application until this procedure
  324. * is complete. The application is notified when the procedure is finished using the
  325. * notification callback registered by the application.
  326. */
  327. uint32_t pstorage_raw_store(pstorage_handle_t * p_dest,
  328. uint8_t * p_src,
  329. pstorage_size_t size,
  330. pstorage_size_t offset);
  331. /**@brief Function for clearing data in persistent memory in raw mode.
  332. *
  333. * @param[in] p_dest Base block identifier in persistent memory that needs to be cleared.
  334. * Equivalent to an Erase Operation.
  335. * @param[in] size Size of data to be cleared from persistent memory expressed in bytes.
  336. * Not used.
  337. *
  338. * @retval NRF_SUCCESS Operation success.
  339. * @retval NRF_ERROR_INVALID_STATE Operation failure. API is called without module
  340. * initialization.
  341. * @retval NRF_ERROR_NULL Operation failure. NULL parameter has been passed.
  342. * @retval NRF_ERROR_INVALID_PARAM Operation failure. Invalid parameter has been passed.
  343. * @retval NRF_ERROR_NO_MEM Operation failure. No storage space available.
  344. *
  345. * @note Clear operations may take time. This API, however, does not block until the clear
  346. * procedure is complete. The application is notified of procedure completion using
  347. * a notification callback registered by the application. The 'result' parameter of the
  348. * callback indicates if the procedure was successful or not.
  349. */
  350. uint32_t pstorage_raw_clear(pstorage_handle_t * p_dest, pstorage_size_t size);
  351. #endif // PSTORAGE_RAW_MODE_ENABLE
  352. /**@} */
  353. /**@} */
  354. #endif // PSTORAGE_H__