fds_internal_defs.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 FDS_INTERNAL_DEFS_H__
  13. #define FDS_INTERNAL_DEFS_H__
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include "fds_config.h"
  17. #if defined (FDS_THREADS)
  18. #include "nrf_soc.h"
  19. #include "app_util_platform.h"
  20. #endif
  21. #define FDS_PAGE_TAG_SIZE (2) // Page tag size, in 4-byte words.
  22. #define FDS_PAGE_TAG_WORD_0 (0) // Offset of the first word in the page tag from the page address.
  23. #define FDS_PAGE_TAG_WORD_1 (1) // Offset of the second word in the page tag from the page address.
  24. // Page tag constants
  25. #define FDS_PAGE_TAG_MAGIC (0xDEADC0DE)
  26. #define FDS_PAGE_TAG_SWAP (0xF11E01FF)
  27. #define FDS_PAGE_TAG_DATA (0xF11E01FE)
  28. #define FDS_ERASED_WORD (0xFFFFFFFF)
  29. #define FDS_OFFSET_TL (0) // Offset of TL from the record base address, in 4-byte words.
  30. #define FDS_OFFSET_IC (1) // Offset of IC from the record base address, in 4-byte words.
  31. #define FDS_OFFSET_ID (2) // Offset of ID from the record base address, in 4-byte words.
  32. #define FDS_OFFSET_DATA (3) // Offset of the data (chunks) from the record base address, in 4-byte words.
  33. #define FDS_HEADER_SIZE_TL (1) // Size of the TL part of the header, in 4-byte words.
  34. #define FDS_HEADER_SIZE_IC (1) // Size of the IC part of the header, in 4-byte words.
  35. #define FDS_HEADER_SIZE_ID (1) // Size of the record ID in the header, in 4-byte words.
  36. #define FDS_HEADER_SIZE (3) // Size of the whole header, in 4-byte words.
  37. #define FDS_OP_EXECUTING (FS_SUCCESS)
  38. #define FDS_OP_COMPLETED (0x1D1D)
  39. // The size of a physical page, in 4-byte words.
  40. #if defined(NRF51)
  41. #define FDS_PHY_PAGE_SIZE (256)
  42. #elif defined(NRF52)
  43. #define FDS_PHY_PAGE_SIZE (1024)
  44. #endif
  45. // The number of physical pages to be used. This value is configured indirectly.
  46. #define FDS_PHY_PAGES ((FDS_VIRTUAL_PAGES * FDS_VIRTUAL_PAGE_SIZE) / FDS_PHY_PAGE_SIZE)
  47. // The size of a virtual page, in number of physical pages.
  48. #define FDS_PHY_PAGES_IN_VPAGE (FDS_VIRTUAL_PAGE_SIZE / FDS_PHY_PAGE_SIZE)
  49. // The number of pages available to store data; which is the total minus one (the swap).
  50. #define FDS_MAX_PAGES (FDS_VIRTUAL_PAGES - 1)
  51. // Just a shorter name for the size, in words, of a virtual page.
  52. #define FDS_PAGE_SIZE (FDS_VIRTUAL_PAGE_SIZE)
  53. #if (FDS_VIRTUAL_PAGE_SIZE % FDS_PHY_PAGE_SIZE != 0)
  54. #error "FDS_VIRTUAL_PAGE_SIZE must be a multiple of the size of a physical page."
  55. #endif
  56. #if (FDS_VIRTUAL_PAGES < 2)
  57. #error "FDS requires at least two virtual pages."
  58. #endif
  59. // FDS internal status flags.
  60. typedef enum
  61. {
  62. FDS_FLAG_INITIALIZING = (1 << 0), // The module is initializing.
  63. FDS_FLAG_INITIALIZED = (1 << 1), // The module is initialized.
  64. FDS_FLAG_PROCESSING = (1 << 2), // The queue is being processed.
  65. FDS_FLAG_VERIFY_CRC = (1 << 3), // Verify CRC upon writing a record.
  66. } fds_flags_t;
  67. // Page types.
  68. typedef enum
  69. {
  70. FDS_PAGE_DATA, // Page is ready for storage.
  71. FDS_PAGE_SWAP, // Page is reserved for garbage collection.
  72. FDS_PAGE_ERASED, // Page is erased.
  73. FDS_PAGE_UNDEFINED, // Undefined page type.
  74. } fds_page_type_t;
  75. typedef struct
  76. {
  77. fds_page_type_t page_type; // The page type.
  78. uint32_t const * p_addr; // The address of the page.
  79. uint16_t write_offset; // The page write offset, in 4-byte words.
  80. uint16_t words_reserved; // The amount of words reserved by fds_write_reserve().
  81. uint16_t records_open; // The number of records opened using fds_open().
  82. bool can_gc; // Indicates that there are some records that have been deleted.
  83. } fds_page_t;
  84. typedef struct
  85. {
  86. uint32_t const * p_addr;
  87. uint16_t write_offset;
  88. } fds_swap_page_t;
  89. // FDS op-codes.
  90. typedef enum
  91. {
  92. FDS_OP_NONE,
  93. FDS_OP_INIT, // Initialize the module.
  94. FDS_OP_WRITE, // Write a record to flash.
  95. FDS_OP_UPDATE, // Update a record.
  96. FDS_OP_DEL_RECORD, // Delete a record.
  97. FDS_OP_DEL_FILE, // Delete a file.
  98. FDS_OP_GC // Run garbage collection.
  99. } fds_op_code_t;
  100. typedef enum
  101. {
  102. FDS_OP_INIT_TAG_SWAP,
  103. FDS_OP_INIT_TAG_DATA,
  104. FDS_OP_INIT_ERASE_SWAP,
  105. FDS_OP_INIT_PROMOTE_SWAP,
  106. } fds_init_step_t;
  107. typedef enum
  108. {
  109. FDS_OP_WRITE_HEADER_BEGIN, // Write the record key and length.
  110. FDS_OP_WRITE_HEADER_FINALIZE, // Write the file ID and CRC.
  111. FDS_OP_WRITE_RECORD_ID, // Write the record ID.
  112. FDS_OP_WRITE_CHUNKS, // Write the record data.
  113. FDS_OP_WRITE_FIND_RECORD,
  114. FDS_OP_WRITE_FLAG_DIRTY, // Flag a record as dirty (as part of an update operation).
  115. FDS_OP_WRITE_DONE,
  116. } fds_write_step_t;
  117. typedef enum
  118. {
  119. FDS_OP_DEL_RECORD_FLAG_DIRTY, // Flag a record as dirty.
  120. FDS_OP_DEL_FILE_FLAG_DIRTY, // Flag multiple records as dirty.
  121. FDS_OP_DEL_DONE,
  122. } fds_delete_step_t;
  123. #if defined(__CC_ARM)
  124. #pragma push
  125. #pragma anon_unions
  126. #elif defined(__ICCARM__)
  127. #pragma language=extended
  128. #elif defined(__GNUC__)
  129. // anonymous unions are enabled by default
  130. #endif
  131. typedef struct
  132. {
  133. fds_op_code_t op_code; // The opcode for the operation.
  134. union
  135. {
  136. struct
  137. {
  138. fds_init_step_t step; // The current step the operation is at.
  139. } init;
  140. struct
  141. {
  142. fds_header_t header;
  143. fds_write_step_t step; // The current step the operation is at.
  144. uint16_t page; // The page the flash space for this command was reserved.
  145. uint16_t chunk_offset; // Offset used for writing record chunks, in 4-byte words.
  146. uint8_t chunk_count; // Number of chunks to be written.
  147. uint32_t record_to_delete; // The record to delete in case this is an update.
  148. } write;
  149. struct
  150. {
  151. fds_delete_step_t step;
  152. uint16_t file_id;
  153. uint16_t record_key;
  154. uint32_t record_to_delete;
  155. } del;
  156. };
  157. } fds_op_t;
  158. #if defined(__CC_ARM)
  159. #pragma pop
  160. #elif defined(__ICCARM__)
  161. // leave anonymous unions enabled
  162. #elif defined(__GNUC__)
  163. // anonymous unions are enabled by default
  164. #endif
  165. typedef struct
  166. {
  167. fds_op_t op[FDS_OP_QUEUE_SIZE]; // Queued flash operations.
  168. uint32_t rp; // The index of the command being executed.
  169. uint32_t count; // Number of elements in the queue.
  170. } fds_op_queue_t;
  171. typedef struct
  172. {
  173. fds_record_chunk_t chunk[FDS_CHUNK_QUEUE_SIZE];
  174. uint32_t rp;
  175. uint32_t count;
  176. } fds_chunk_queue_t;
  177. enum
  178. {
  179. PAGE_ERASED = 0x1,
  180. PAGE_DATA = 0x2,
  181. SWAP_EMPTY = 0x4,
  182. SWAP_DIRTY = 0x8,
  183. };
  184. typedef enum
  185. {
  186. // This is a fatal error.
  187. NO_PAGES,
  188. // All pages are erased. Perform a fresh installation.
  189. FRESH_INSTALL = (PAGE_ERASED),
  190. // Swap is missing. Tag an erased page as swap.
  191. TAG_SWAP = (PAGE_ERASED | PAGE_DATA),
  192. // Swap is empty. Tag all erased pages as data.
  193. TAG_DATA = (PAGE_ERASED | SWAP_EMPTY),
  194. // Swap is empty. Tag all remaining erased pages as data.
  195. TAG_DATA_INST = (PAGE_ERASED | PAGE_DATA | SWAP_EMPTY),
  196. // The swap is dirty. This indicates that the device powered off during GC. However, since there
  197. // is also an erased page, it is possible to assume that that page had been entirely garbage
  198. // collected. Hence, tag the swap as data, one erased page as swap and any remaining pages as data.
  199. PROMOTE_SWAP = (PAGE_ERASED | SWAP_DIRTY),
  200. // Similar to the above. Tag the swap as data, one erased page as swap, and any remain
  201. // pages as data.
  202. PROMOTE_SWAP_INST = (PAGE_ERASED | PAGE_DATA | SWAP_DIRTY),
  203. // The swap is dirty (written) and there are no erased pages. This indicates that the device
  204. // was powered off during GC. It is safe to discard (erase) the swap, since data that was
  205. // swapped out lies in one of the valid pages.
  206. DISCARD_SWAP = (PAGE_DATA | SWAP_DIRTY),
  207. // Do nothing.
  208. ALREADY_INSTALLED = (PAGE_DATA | SWAP_EMPTY),
  209. } fds_init_opts_t;
  210. typedef enum
  211. {
  212. GC_BEGIN, // Begin GC.
  213. GC_NEXT_PAGE, // GC a page.
  214. GC_FIND_NEXT_RECORD, // Find a valid record to copy.
  215. GC_COPY_RECORD, // Copy a valid record to swap.
  216. GC_ERASE_PAGE, // Erase the page being garbage collected.
  217. GC_DISCARD_SWAP, // Erase (discard) the swap page.
  218. GC_PROMOTE_SWAP, // Tag the swap as valid.
  219. GC_TAG_NEW_SWAP // Tag a freshly erased (GCed) page as swap.
  220. } fds_gc_state_t;
  221. // Holds garbage collection status and related data.
  222. typedef struct
  223. {
  224. fds_gc_state_t state; // The current GC step.
  225. uint16_t cur_page; // The current page being garbage collected.
  226. uint32_t const * p_record_src; // The current record being copied to swap.
  227. uint16_t run_count; // Total number of times GC was run.
  228. bool do_gc_page[FDS_MAX_PAGES]; // Controls which pages to garbage collect.
  229. bool resume; // Whether or not GC should be resumed.
  230. } fds_gc_data_t;
  231. // Macros to enable and disable application interrupts.
  232. #if defined (FDS_THREADS)
  233. #define CRITICAL_SECTION_ENTER() CRITICAL_REGION_ENTER()
  234. #define CRITICAL_SECTION_EXIT() CRITICAL_REGION_EXIT()
  235. #else
  236. #define CRITICAL_SECTION_ENTER()
  237. #define CRITICAL_SECTION_EXIT()
  238. #endif
  239. #endif // FDS_INTERNAL_DEFS_H__