nrf_fstorage.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**
  2. * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_FSTORAGE_H__
  41. #define NRF_FSTORAGE_H__
  42. /**
  43. * @file
  44. *
  45. * @defgroup nrf_fstorage Flash storage (fstorage)
  46. * @ingroup app_common
  47. * @{
  48. *
  49. * @brief Flash abstraction library that provides basic read, write, and erase operations.
  50. *
  51. * @details The fstorage library can be implemented in different ways. Two implementations are provided:
  52. * - The @ref nrf_fstorage_sd implements flash access through the SoftDevice.
  53. * - The @ref nrf_fstorage_nvmc implements flash access through the non-volatile memory controller.
  54. *
  55. * You can select the implementation that should be used independently for each instance of fstorage.
  56. */
  57. #include <stdint.h>
  58. #include <stdbool.h>
  59. #include "sdk_errors.h"
  60. #include "nrf_section.h"
  61. #ifdef __cplusplus
  62. extern "C" {
  63. #endif
  64. /**@brief Macro for defining an fstorage instance.
  65. *
  66. * Users of fstorage must define an instance variable by using this macro.
  67. * Each instance is tied to an API implementation and contains information such
  68. * as the program and erase units for the target flash peripheral.
  69. * Instance variables are placed in the "fs_data" section of the binary.
  70. *
  71. * @param[in] inst A definition of an @ref nrf_fstorage_t variable.
  72. */
  73. #define NRF_FSTORAGE_DEF(inst) NRF_SECTION_ITEM_REGISTER(fs_data, inst)
  74. /**@brief Macro for retrieving an fstorage instance. */
  75. #define NRF_FSTORAGE_INSTANCE_GET(i) NRF_SECTION_ITEM_GET(fs_data, nrf_fstorage_t, (i))
  76. /**@brief Macro for retrieving the total number of fstorage instances. */
  77. #define NRF_FSTORAGE_INSTANCE_CNT NRF_SECTION_ITEM_COUNT(fs_data, nrf_fstorage_t)
  78. /**@brief Event IDs. */
  79. typedef enum
  80. {
  81. NRF_FSTORAGE_EVT_READ_RESULT,
  82. NRF_FSTORAGE_EVT_WRITE_RESULT, //!< Event for @ref nrf_fstorage_write.
  83. NRF_FSTORAGE_EVT_ERASE_RESULT //!< Event for @ref nrf_fstorage_erase.
  84. } nrf_fstorage_evt_id_t;
  85. /**@brief An fstorage event. */
  86. typedef struct
  87. {
  88. nrf_fstorage_evt_id_t id; //!< The event ID.
  89. ret_code_t result; //!< Result of the operation.
  90. uint32_t addr; //!< Address at which the operation was performed.
  91. void const * p_src; //!< Buffer written to flash.
  92. uint32_t len; //!< Length of the operation.
  93. void * p_param; //!< User-defined parameter passed to the event handler.
  94. } nrf_fstorage_evt_t;
  95. /**@brief Event handler function prototype.
  96. *
  97. * @param[in] p_evt The event.
  98. */
  99. typedef void (*nrf_fstorage_evt_handler_t)(nrf_fstorage_evt_t * p_evt);
  100. /**@brief Information about the implementation and the flash peripheral. */
  101. typedef struct
  102. {
  103. uint32_t erase_unit; //!< Size of a flash page (in bytes). A flash page is the smallest unit that can be erased.
  104. uint32_t program_unit; //!< Size of the smallest programmable unit (in bytes).
  105. bool rmap; //!< The device address space is memory mapped to the MCU address space.
  106. bool wmap; //!< The device address space is memory mapped to a writable MCU address space.
  107. } const nrf_fstorage_info_t;
  108. /* Necessary forward declaration. */
  109. struct nrf_fstorage_api_s;
  110. /**@brief An fstorage instance.
  111. *
  112. * @details Use the @ref NRF_FSTORAGE_DEF macro to define an fstorage instance.
  113. *
  114. * An instance is tied to an API implementation and contains information about the flash device,
  115. * such as the program and erase units as well and implementation-specific functionality.
  116. */
  117. typedef struct
  118. {
  119. /**@brief The API implementation used by this instance. */
  120. struct nrf_fstorage_api_s const * p_api;
  121. /**@brief Information about the implementation functionality and the flash peripheral. */
  122. nrf_fstorage_info_t * p_flash_info;
  123. /**@brief The event handler function.
  124. *
  125. * If set to NULL, no events will be sent.
  126. */
  127. nrf_fstorage_evt_handler_t evt_handler;
  128. /**@brief The beginning of the flash space on which this fstorage instance should operate.
  129. * All flash operations must be within the address specified in
  130. * this field and @ref end_addr.
  131. *
  132. * This field must be set manually.
  133. */
  134. uint32_t start_addr;
  135. /**@brief The last address (exclusive) of flash on which this fstorage instance should operate.
  136. * All flash operations must be within the address specified in
  137. * this field and @ref start_addr.
  138. *
  139. * This field must be set manually.
  140. */
  141. uint32_t end_addr;
  142. } nrf_fstorage_t;
  143. /**@brief Functions provided by the API implementation. */
  144. typedef struct nrf_fstorage_api_s
  145. {
  146. /**@brief Initialize the flash peripheral. */
  147. ret_code_t (*init)(nrf_fstorage_t * p_fs, void * p_param);
  148. /**@brief Uninitialize the flash peripheral. */
  149. ret_code_t (*uninit)(nrf_fstorage_t * p_fs, void * p_param);
  150. /**@brief Read data from flash. */
  151. ret_code_t (*read)(nrf_fstorage_t const * p_fs, uint32_t src, void * p_dest, uint32_t len);
  152. /**@brief Write bytes to flash. */
  153. ret_code_t (*write)(nrf_fstorage_t const * p_fs, uint32_t dest, void const * p_src, uint32_t len, void * p_param);
  154. /**@brief Erase flash pages. */
  155. ret_code_t (*erase)(nrf_fstorage_t const * p_fs, uint32_t addr, uint32_t len, void * p_param);
  156. /**@brief Map a device address to a readable address within the MCU address space. */
  157. uint8_t const * (*rmap)(nrf_fstorage_t const * p_fs, uint32_t addr);
  158. /**@brief Map a device address to a writable address within the MCU address space. */
  159. uint8_t * (*wmap)(nrf_fstorage_t const * p_fs, uint32_t addr);
  160. /**@brief Check if there are any pending flash operations. */
  161. bool (*is_busy)(nrf_fstorage_t const * p_fs);
  162. } const nrf_fstorage_api_t;
  163. /**@brief Function for initializing fstorage.
  164. *
  165. * @param[in] p_fs The fstorage instance to initialize.
  166. * @param[in] p_api The API implementation to use.
  167. * @param[in] p_param An optional parameter to pass to the implementation-specific API call.
  168. *
  169. * @retval NRF_SUCCESS If initialization was successful.
  170. * @retval NRF_ERROR_NULL If @p p_fs or @p p_api field in @p p_fs is NULL.
  171. * @retval NRF_ERROR_INTERNAL If another error occurred.
  172. */
  173. ret_code_t nrf_fstorage_init(nrf_fstorage_t * p_fs,
  174. nrf_fstorage_api_t * p_api,
  175. void * p_param);
  176. /**@brief Function for uninitializing an fstorage instance.
  177. *
  178. * @param[in] p_fs The fstorage instance to uninitialize.
  179. * @param[in] p_param An optional parameter to pass to the implementation-specific API call.
  180. *
  181. * @retval NRF_SUCCESS If uninitialization was successful.
  182. * @retval NRF_ERROR_NULL If @p p_fs is NULL.
  183. * @retval NRF_ERROR_INVALID_STATE If the module is not initialized.
  184. * @retval NRF_ERROR_INTERNAL If another error occurred.
  185. */
  186. ret_code_t nrf_fstorage_uninit(nrf_fstorage_t * p_fs, void * p_param);
  187. /**@brief Function for reading data from flash.
  188. *
  189. * Copy @p len bytes from @p addr to @p p_dest.
  190. *
  191. * @param[in] p_fs The fstorage instance.
  192. * @param[in] addr Address in flash where to read from.
  193. * @param[in] p_dest Buffer where the data should be copied.
  194. * @param[in] len Length of the data to be copied (in bytes).
  195. *
  196. * @retval NRF_SUCCESS If the operation was successful.
  197. * @retval NRF_ERROR_NULL If @p p_fs or @p p_dest is NULL.
  198. * @retval NRF_ERROR_INVALID_STATE If the module is not initialized.
  199. * @retval NRF_ERROR_INVALID_LENGTH If @p len is zero or otherwise invalid.
  200. * @retval NRF_ERROR_INVALID_ADDR If the address @p addr is outside the flash memory
  201. * boundaries specified in @p p_fs, or if it is unaligned.
  202. */
  203. ret_code_t nrf_fstorage_read(nrf_fstorage_t const * p_fs,
  204. uint32_t addr,
  205. void * p_dest,
  206. uint32_t len);
  207. /**@brief Function for writing data to flash.
  208. *
  209. * Write @p len bytes from @p p_src to @p dest.
  210. *
  211. * When using @ref nrf_fstorage_sd, the data is written by several calls to @ref sd_flash_write if
  212. * the length of the data exceeds @ref NRF_FSTORAGE_SD_MAX_WRITE_SIZE bytes.
  213. * Only one event is sent upon completion.
  214. *
  215. * @note The data to be written to flash must be kept in memory until the operation has
  216. * terminated and an event is received.
  217. *
  218. * @param[in] p_fs The fstorage instance.
  219. * @param[in] dest Address in flash memory where to write the data.
  220. * @param[in] p_src Data to be written.
  221. * @param[in] len Length of the data (in bytes).
  222. * @param[in] p_param User-defined parameter passed to the event handler (may be NULL).
  223. *
  224. * @retval NRF_SUCCESS If the operation was accepted.
  225. * @retval NRF_ERROR_NULL If @p p_fs or @p p_src is NULL.
  226. * @retval NRF_ERROR_INVALID_STATE If the module is not initialized.
  227. * @retval NRF_ERROR_INVALID_LENGTH If @p len is zero or not a multiple of the program unit,
  228. * or if it is otherwise invalid.
  229. * @retval NRF_ERROR_INVALID_ADDR If the address @p dest is outside the flash memory
  230. * boundaries specified in @p p_fs, or if it is unaligned.
  231. * @retval NRF_ERROR_NO_MEM If no memory is available to accept the operation.
  232. * When using the @ref nrf_fstorage_sd, this error
  233. * indicates that the internal queue of operations is full.
  234. */
  235. ret_code_t nrf_fstorage_write(nrf_fstorage_t const * p_fs,
  236. uint32_t dest,
  237. void const * p_src,
  238. uint32_t len,
  239. void * p_param);
  240. /**@brief Function for erasing flash pages.
  241. *
  242. * @details This function erases @p len pages starting from the page at address @p page_addr.
  243. * The erase operation must be initiated on a page boundary.
  244. *
  245. * @param[in] p_fs The fstorage instance.
  246. * @param[in] page_addr Address of the page to erase.
  247. * @param[in] len Number of pages to erase.
  248. * @param[in] p_param User-defined parameter passed to the event handler (may be NULL).
  249. *
  250. * @retval NRF_SUCCESS If the operation was accepted.
  251. * @retval NRF_ERROR_NULL If @p p_fs is NULL.
  252. * @retval NRF_ERROR_INVALID_STATE If the module is not initialized.
  253. * @retval NRF_ERROR_INVALID_LENGTH If @p len is zero.
  254. * @retval NRF_ERROR_INVALID_ADDR If the address @p page_addr is outside the flash memory
  255. * boundaries specified in @p p_fs, or if it is unaligned.
  256. * @retval NRF_ERROR_NO_MEM If no memory is available to accept the operation.
  257. * When using the @ref nrf_fstorage_sd, this error
  258. * indicates that the internal queue of operations is full.
  259. */
  260. ret_code_t nrf_fstorage_erase(nrf_fstorage_t const * p_fs,
  261. uint32_t page_addr,
  262. uint32_t len,
  263. void * p_param);
  264. /**@brief Map a flash address to a pointer in the MCU address space that can be dereferenced.
  265. *
  266. * @param p_fs The fstorage instance.
  267. * @param addr The address to map.
  268. *
  269. * @retval A pointer to the specified address,
  270. * or @c NULL if the address cannot be mapped or if @p p_fs is @c NULL.
  271. */
  272. uint8_t const * nrf_fstorage_rmap(nrf_fstorage_t const * p_fs, uint32_t addr);
  273. /**@brief Map a flash address to a pointer in the MCU address space that can be written to.
  274. *
  275. * @param p_fs The fstorage instance.
  276. * @param addr The address to map.
  277. *
  278. * @retval A pointer to the specified address,
  279. * or @c NULL if the address cannot be mapped or if @p p_fs is @c NULL.
  280. */
  281. uint8_t * nrf_fstorage_wmap(nrf_fstorage_t const * p_fs, uint32_t addr);
  282. /**@brief Function for querying the status of fstorage.
  283. *
  284. * @details An uninitialized instance of fstorage is treated as not busy.
  285. *
  286. * @param[in] p_fs The fstorage instance. Pass NULL to query all instances.
  287. *
  288. * @returns If @p p_fs is @c NULL, this function returns true if any fstorage instance is busy or false otherwise.
  289. * @returns If @p p_fs is not @c NULL, this function returns true if the fstorage instance is busy or false otherwise.
  290. */
  291. bool nrf_fstorage_is_busy(nrf_fstorage_t const * p_fs);
  292. /** @} */
  293. #ifdef __cplusplus
  294. }
  295. #endif
  296. #endif // NRF_FSTORAGE_H__