fstorage.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* Copyright (c) 2015 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. #ifndef FSTORAGE_H__
  13. #define FSTORAGE_H__
  14. /**
  15. * @defgroup fstorage fstorage
  16. * @ingroup app_common
  17. * @{
  18. *
  19. * @brief Module which provides functionality to store data to flash and erase flash pages.
  20. */
  21. #include <stdint.h>
  22. #include "section_vars.h"
  23. /**@brief fstorage return values. */
  24. typedef enum
  25. {
  26. FS_SUCCESS,
  27. FS_ERR_NOT_INITIALIZED, //!< Error. The module is not initialized.
  28. FS_ERR_INVALID_CFG, //!< Error. Invalid fstorage configuration.
  29. FS_ERR_NULL_ARG, //!< Error. Argument is NULL.
  30. FS_ERR_INVALID_ARG, //!< Error. Argument contains invalid data.
  31. FS_ERR_INVALID_ADDR, //!< Error. Address out of bounds.
  32. FS_ERR_UNALIGNED_ADDR, //!< Error. Unaligned address.
  33. FS_ERR_QUEUE_FULL, //!< Error. Queue is full.
  34. FS_ERR_OPERATION_TIMEOUT, //!< Error. The operation has timed out.
  35. FS_ERR_INTERNAL, //!< Error. Internal error.
  36. } fs_ret_t;
  37. /**@brief fstorage event IDs. */
  38. typedef enum
  39. {
  40. FS_EVT_STORE, //!< Event for @ref fs_store.
  41. FS_EVT_ERASE //!< Event for @ref fs_erase.
  42. } fs_evt_id_t;
  43. #if defined(__CC_ARM)
  44. #pragma push
  45. #pragma anon_unions
  46. #elif defined(__ICCARM__)
  47. #pragma language=extended
  48. #elif defined(__GNUC__)
  49. /* anonymous unions are enabled by default */
  50. #endif
  51. /**@brief An fstorage event. */
  52. typedef struct
  53. {
  54. fs_evt_id_t id; //!< The event ID.
  55. union
  56. {
  57. struct
  58. {
  59. uint32_t const * p_data; //!< Pointer to the data stored in flash.
  60. uint16_t length_words; //!< Length of the data, in 4-byte words.
  61. } store;
  62. struct
  63. {
  64. uint16_t first_page; //!< First page erased.
  65. uint16_t last_page; //!< Last page erased.
  66. } erase;
  67. };
  68. } fs_evt_t;
  69. #if defined(__CC_ARM)
  70. #pragma pop
  71. #elif defined(__ICCARM__)
  72. /* leave anonymous unions enabled */
  73. #elif defined(__GNUC__)
  74. /* anonymous unions are enabled by default */
  75. #endif
  76. /**@brief fstorage event handler function prototype.
  77. *
  78. * @param[in] evt The event.
  79. * @param[in] result The result of the operation.
  80. */
  81. typedef void (*fs_cb_t)(fs_evt_t const * const evt, fs_ret_t result);
  82. /**@brief fstorage application-specific configuration.
  83. *
  84. * @details Specifies the callback to invoke when an operation completes, the number of flash pages
  85. * requested by the application and the priority with which these are to be assigned, with
  86. * respect to other applications. Additionally, the configuration specifies the boundaries
  87. * of the flash space assigned to an application. The configuration must be provided as an
  88. * argument when invoking @ref fs_store and @ref fs_erase.
  89. *
  90. * @note The fields @p p_start_addr and @p p_end_address are set by @ref fs_init, based on the
  91. * value of the field @p priority.
  92. */
  93. typedef struct
  94. {
  95. /**@brief The beginning of the flash space assigned to the application which registered this
  96. * configuration. This field is set by @ref fs_init.
  97. */
  98. uint32_t const * p_start_addr;
  99. /**@brief The end of the flash space assigned to the application which registered this
  100. * configuration. This field is set by @ref fs_init.
  101. */
  102. uint32_t const * p_end_addr;
  103. fs_cb_t const callback; //!< Callback to run when a flash operation has completed.
  104. uint8_t const num_pages; //!< The number of flash pages requested.
  105. /**@brief The priority with which fstorage should assign flash pages to this application,
  106. * with respect to other applications. Applications with higher priority will be
  107. * assigned flash pages with a higher memory address. The highest priority is
  108. * reserved. Must be unique among configurations.
  109. */
  110. uint8_t const priority;
  111. } fs_config_t;
  112. /**@brief Macro for registering with an fstorage configuration.
  113. * Applications which use fstorage must register with the module using this macro.
  114. * Registering involves defining a variable which holds the configuration of fstorage
  115. * specific to the application which invokes the macro.
  116. *
  117. * @details This macro places the configuration variable in a section named "fs_data" that
  118. * fstorage uses during initialization and regular operation.
  119. *
  120. * @param[in] cfg_var A @e definition of a @ref fs_config_t variable.
  121. */
  122. #define FS_REGISTER_CFG(cfg_var) NRF_SECTION_VARS_ADD(fs_data, cfg_var)
  123. /**@brief Function for initializing the module.
  124. *
  125. * @details This functions assigns pages in flash according to all registered configurations.
  126. *
  127. * @retval FS_SUCCESS If the module was successfully initialized.
  128. */
  129. fs_ret_t fs_init(void);
  130. /**@brief Function for storing data in flash.
  131. *
  132. * @details Copies @p length_words words from @p p_src to the location pointed by @p p_dest.
  133. * If the length of the data exceeds @ref FS_MAX_WRITE_SIZE_WORDS, the data will be
  134. * written down in several chunks, as necessary. Only one event will be sent to the
  135. * application upon completion. Both the source and the destination of the data must be
  136. * word aligned. This function is asynchronous, completion is reported via an event sent
  137. * the the callback function specified in the supplied configuration.
  138. *
  139. * @warning The data to be written to flash has to be kept in memory until the operation has
  140. * terminated, i.e., an event is received.
  141. *
  142. * @param[in] p_config fstorage configuration registered by the application.
  143. * @param[in] p_dest The address in flash memory where to store the data.
  144. * @param[in] p_src Pointer to the data to store in flash.
  145. * @param[in] length_words Length of the data to store, in words.
  146. *
  147. * @retval FS_SUCCESS If the operation was queued successfully.
  148. * @retval FS_ERR_NOT_INITIALIZED If the module is not initialized.
  149. * @retval FS_ERR_INVALID_CFG If @p p_config is NULL or contains invalid data.
  150. * @retval FS_ERR_NULL_ARG If @p p_dest or @p p_src are NULL.
  151. * @retval FS_ERR_INVALID_ARG If @p length_words is zero.
  152. * @retval FS_ERR_INVALID_ADDR If @p p_dest or @p p_src are outside of the flash memory
  153. * boundaries specified in @p p_config.
  154. * @retval FS_ERR_UNALIGNED_ADDR If @p p_dest or @p p_src are not aligned to a word boundary.
  155. * @retval FS_ERR_QUEUE_FULL If the internal operation queue is full.
  156. */
  157. fs_ret_t fs_store(fs_config_t const * const p_config,
  158. uint32_t const * const p_dest,
  159. uint32_t const * const p_src,
  160. uint16_t length_words);
  161. /**@brief Function for erasing flash pages.
  162. *
  163. * @details Starting from the page at @p p_page_addr, erases @p num_pages flash pages.
  164. * @p p_page_addr must be aligned to a page boundary. All pages to be erased must be
  165. * within the bounds specified in the supplied fstorage configuration.
  166. * This function is asynchronous. Completion is reported via an event.
  167. *
  168. * @param[in] p_config fstorage configuration registered by the application.
  169. * @param[in] p_page_addr Address of the page to erase. Must be aligned to a page boundary.
  170. * @param[in] num_pages Number of pages to erase. May not be zero.
  171. *
  172. * @retval FS_SUCCESS If the operation was queued successfully.
  173. * @retval FS_ERR_NOT_INITIALIZED If the module is not initialized.
  174. * @retval FS_ERR_INVALID_CFG If @p p_config is NULL or contains invalid data.
  175. * @retval FS_ERR_NULL_ARG If @p p_page_addr is NULL.
  176. * @retval FS_ERR_INVALID_ARG If @p num_pages is zero.
  177. * @retval FS_ERR_INVALID_ADDR If the operation would go beyond the flash memory boundaries
  178. * specified in @p p_config.
  179. * @retval FS_ERR_UNALIGNED_ADDR If @p p_page_addr is not aligned to a page boundary.
  180. * @retval FS_ERR_QUEUE_FULL If the internal operation queue is full.
  181. */
  182. fs_ret_t fs_erase(fs_config_t const * const p_config,
  183. uint32_t const * const p_page_addr,
  184. uint16_t num_pages);
  185. /**@brief Function for retrieving the number of queued flash operations.
  186. *
  187. * @param[out] p_op_count The number of queued flash operations.
  188. *
  189. * @retval FS_SUCCESS If the number of queued operations was retrieved successfully.
  190. * @retval FS_ERR_NULL_ARG If @p p_op_count is NULL.
  191. */
  192. fs_ret_t fs_queued_op_count_get(uint32_t * const p_op_count);
  193. /**@brief Function for handling system events from the SoftDevice.
  194. *
  195. * @details If any of the modules used by the application rely on fstorage, the application should
  196. * dispatch system events to fstorage using this function.
  197. *
  198. * @param[in] sys_evt System event from the SoftDevice.
  199. */
  200. void fs_sys_event_handler(uint32_t sys_evt);
  201. /** @} */
  202. #endif // FSTORAGE_H__