ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
fds_internal_defs.h
Go to the documentation of this file.
1 
40 #ifndef FDS_INTERNAL_DEFS_H__
41 #define FDS_INTERNAL_DEFS_H__
42 #include "sdk_config.h"
43 #include <stdint.h>
44 #include <stdbool.h>
45 
46 #if defined (FDS_THREADS)
47  #include "nrf_soc.h"
48  #include "app_util_platform.h"
49 #endif
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
55 #define FDS_PAGE_TAG_SIZE (2) // Page tag size, in 4-byte words.
56 #define FDS_PAGE_TAG_WORD_0 (0) // Offset of the first word in the page tag from the page address.
57 #define FDS_PAGE_TAG_WORD_1 (1) // Offset of the second word in the page tag from the page address.
58 
59 // Page tag constants
60 #define FDS_PAGE_TAG_MAGIC (0xDEADC0DE)
61 #define FDS_PAGE_TAG_SWAP (0xF11E01FF)
62 #define FDS_PAGE_TAG_DATA (0xF11E01FE)
63 
64 #define FDS_ERASED_WORD (0xFFFFFFFF)
65 
66 #define FDS_OFFSET_TL (0) // Offset of TL from the record base address, in 4-byte words.
67 #define FDS_OFFSET_IC (1) // Offset of IC from the record base address, in 4-byte words.
68 #define FDS_OFFSET_ID (2) // Offset of ID from the record base address, in 4-byte words.
69 #define FDS_OFFSET_DATA (3) // Offset of the data from the record base address, in 4-byte words.
70 
71 #define FDS_HEADER_SIZE_TL (1) // Size of the TL part of the header, in 4-byte words.
72 #define FDS_HEADER_SIZE_IC (1) // Size of the IC part of the header, in 4-byte words.
73 #define FDS_HEADER_SIZE_ID (1) // Size of the record ID in the header, in 4-byte words.
74 #define FDS_HEADER_SIZE (3) // Size of the whole header, in 4-byte words.
75 
76 #define FDS_OP_EXECUTING (NRF_SUCCESS)
77 #define FDS_OP_COMPLETED (0x1D1D)
78 
79 #define NRF_FSTORAGE_NVMC 1
80 #define NRF_FSTORAGE_SD 2
81 
82 // The size of a physical page, in 4-byte words.
83 #if defined(NRF51)
84  #define FDS_PHY_PAGE_SIZE (256)
85 #else
86  #define FDS_PHY_PAGE_SIZE (1024)
87 #endif
88 
89 // The number of physical pages to be used. This value is configured indirectly.
90 #define FDS_PHY_PAGES ((FDS_VIRTUAL_PAGES * FDS_VIRTUAL_PAGE_SIZE) / FDS_PHY_PAGE_SIZE)
91 
92 // The number of physical pages at the end of the flash that are reserved by other modules.
93 #define FDS_PHY_PAGES_RESERVED ((FDS_VIRTUAL_PAGES_RESERVED * FDS_VIRTUAL_PAGE_SIZE) / FDS_PHY_PAGE_SIZE)
94 
95 // The size of a virtual page, in number of physical pages.
96 #define FDS_PHY_PAGES_IN_VPAGE (FDS_VIRTUAL_PAGE_SIZE / FDS_PHY_PAGE_SIZE)
97 
98 // The number of pages available to store data; which is the total minus one (the swap).
99 #define FDS_DATA_PAGES (FDS_VIRTUAL_PAGES - 1)
100 
101  // Just a shorter name for the size, in words, of a virtual page.
102 #define FDS_PAGE_SIZE (FDS_VIRTUAL_PAGE_SIZE)
103 
104 #if (FDS_VIRTUAL_PAGE_SIZE % FDS_PHY_PAGE_SIZE != 0)
105  #error "FDS_VIRTUAL_PAGE_SIZE must be a multiple of the size of a physical page."
106 #endif
107 
108 #if (FDS_VIRTUAL_PAGES < 2)
109  #error "FDS requires at least two virtual pages."
110 #endif
111 
112 
113 // Page types.
114 typedef enum
115 {
116  FDS_PAGE_DATA, // Page is ready for storage.
117  FDS_PAGE_SWAP, // Page is reserved for garbage collection.
118  FDS_PAGE_ERASED, // Page is erased.
119  FDS_PAGE_UNDEFINED, // Undefined page type.
121 
122 
123 typedef enum
124 {
125  FDS_HEADER_VALID, // Valid header.
126  FDS_HEADER_DIRTY, // Header is incomplete, or record has been deleted.
127  FDS_HEADER_CORRUPT // Header contains corrupt information, not related to CRC.
129 
130 
131 typedef struct
132 {
133  fds_page_type_t page_type; // The page type.
134  uint32_t const * p_addr; // The address of the page.
135  uint16_t write_offset; // The page write offset, in 4-byte words.
136  uint16_t words_reserved; // The amount of words reserved.
137  uint32_t volatile records_open; // The number of open records.
138  bool can_gc; // Indicates that there are some records that have been deleted.
139 } fds_page_t;
140 
141 
142 typedef struct
143 {
144  uint32_t const * p_addr;
145  uint16_t write_offset;
147 
148 
149 // FDS op-codes.
150 typedef enum
151 {
153  FDS_OP_INIT, // Initialize the module.
154  FDS_OP_WRITE, // Write a record to flash.
155  FDS_OP_UPDATE, // Update a record.
156  FDS_OP_DEL_RECORD, // Delete a record.
157  FDS_OP_DEL_FILE, // Delete a file.
158  FDS_OP_GC // Run garbage collection.
160 
161 
162 typedef enum
163 {
169 
170 
171 typedef enum
172 {
173  FDS_OP_WRITE_HEADER_BEGIN, // Write the record key and length.
174  FDS_OP_WRITE_HEADER_FINALIZE, // Write the file ID and CRC.
175  FDS_OP_WRITE_RECORD_ID, // Write the record ID.
176  FDS_OP_WRITE_DATA, // Write the record data.
178  FDS_OP_WRITE_FLAG_DIRTY, // Flag a record as dirty (as part of an update operation).
181 
182 
183 typedef enum
184 {
185  FDS_OP_DEL_RECORD_FLAG_DIRTY, // Flag a record as dirty.
186  FDS_OP_DEL_FILE_FLAG_DIRTY, // Flag multiple records as dirty.
189 
190 
191 #if defined(__CC_ARM)
192  #pragma push
193  #pragma anon_unions
194 #elif defined(__ICCARM__)
195  #pragma language=extended
196 #elif defined(__GNUC__)
197  // anonymous unions are enabled by default
198 #endif
199 
200 typedef struct
201 {
202  fds_op_code_t op_code; // The opcode for the operation.
203  union
204  {
205  struct
206  {
207  fds_init_step_t step; // The current step the operation is at.
208  } init;
209  struct
210  {
212  void const * p_data;
213  uint16_t page; // The page the flash space for this command was reserved.
214  fds_write_step_t step; // The current step the operation is at.
215  uint32_t record_to_delete; // The record to delete in case this is an update.
216  } write;
217  struct
218  {
220  uint16_t file_id;
221  uint16_t record_key;
222  uint32_t record_to_delete;
223  } del;
224  };
225 } fds_op_t;
226 
227 #if defined(__CC_ARM)
228  #pragma pop
229 #elif defined(__ICCARM__)
230  // leave anonymous unions enabled
231 #elif defined(__GNUC__)
232  // anonymous unions are enabled by default
233 #endif
234 
235 
236 enum
237 {
238  PAGE_ERASED = 0x1, // One or more erased pages found.
239  PAGE_DATA = 0x2, // One or more data pages found.
240  PAGE_SWAP_CLEAN = 0x4, // A clean (empty) swap page was found.
241  PAGE_SWAP_DIRTY = 0x8, // A dirty (non-empty) swap page was found.
242 };
243 
244 
245 typedef enum
246 {
247  // No erased pages or FDS pages found.
248  // This is a fatal error.
250 
251  // The filesystem can not be garbage collected.
252  // This is a fatal error.
254 
255  // Perform a fresh installation.
257 
258  // Tag an erased page as swap.
260 
261  // Tag all erased pages as data.
263 
264  // Tag all remaining erased pages as data.
266 
267  // The swap is dirty, likely because the device powered off during GC.
268  // Because there is also an erased page, assume that that page has been garbage collected.
269  // Hence, tag the swap as data (promote), an erased page as swap and remaining pages as data.
271 
272  // Tag the swap as data (promote), an erased page as swap and remaining pages as data.
274 
275  // The swap is dirty (written) and there are no erased pages. It is likely that the device
276  // powered off during GC. It is safe to discard (erase) the swap, since data that was
277  // swapped out still lies in one of the valid pages.
279 
280  // Do nothing.
282 
284 
285 
286 typedef enum
287 {
288  GC_BEGIN, // Begin GC.
289  GC_NEXT_PAGE, // GC a page.
290  GC_FIND_NEXT_RECORD, // Find a valid record to copy.
291  GC_COPY_RECORD, // Copy a valid record to swap.
292  GC_ERASE_PAGE, // Erase the page being garbage collected.
293  GC_DISCARD_SWAP, // Erase (discard) the swap page.
294  GC_PROMOTE_SWAP, // Tag the swap as valid.
295  GC_TAG_NEW_SWAP // Tag a freshly erased (GCed) page as swap.
297 
298 
299 // Holds garbage collection status and related data.
300 typedef struct
301 {
302  fds_gc_state_t state; // The current GC step.
303  uint16_t cur_page; // The current page being garbage collected.
304  uint32_t const * p_record_src; // The current record being copied to swap.
305  uint16_t run_count; // Total number of times GC was run.
306  bool do_gc_page[FDS_DATA_PAGES]; // Controls which pages to garbage collect.
307  bool resume; // Whether or not GC should be resumed.
308 } fds_gc_data_t;
309 
310 
311 // Macros to enable and disable application interrupts.
312 #if defined (FDS_THREADS)
313 
314  #define CRITICAL_SECTION_ENTER() CRITICAL_REGION_ENTER()
315  #define CRITICAL_SECTION_EXIT() CRITICAL_REGION_EXIT()
316 
317 #else
318 
319  #define CRITICAL_SECTION_ENTER()
320  #define CRITICAL_SECTION_EXIT()
321 
322 #endif
323 
324 
325 #ifdef __cplusplus
326 }
327 #endif
328 
329 #endif // FDS_INTERNAL_DEFS_H__
fds_init_opts_t
@ TAG_SWAP
@ DISCARD_SWAP
@ ALREADY_INSTALLED
@ NO_PAGES
@ TAG_DATA
@ FRESH_INSTALL
@ TAG_DATA_INST
@ PROMOTE_SWAP
@ NO_SWAP
@ PROMOTE_SWAP_INST
fds_gc_state_t
@ GC_PROMOTE_SWAP
@ GC_TAG_NEW_SWAP
@ GC_COPY_RECORD
@ GC_ERASE_PAGE
@ GC_NEXT_PAGE
@ GC_FIND_NEXT_RECORD
@ GC_BEGIN
@ GC_DISCARD_SWAP
fds_op_code_t
@ FDS_OP_DEL_FILE
@ FDS_OP_WRITE
@ FDS_OP_UPDATE
@ FDS_OP_NONE
@ FDS_OP_INIT
@ FDS_OP_GC
@ FDS_OP_DEL_RECORD
fds_header_status_t
@ FDS_HEADER_DIRTY
@ FDS_HEADER_VALID
@ FDS_HEADER_CORRUPT
@ PAGE_SWAP_CLEAN
@ PAGE_DATA
@ PAGE_SWAP_DIRTY
@ PAGE_ERASED
fds_init_step_t
@ FDS_OP_INIT_ERASE_SWAP
@ FDS_OP_INIT_PROMOTE_SWAP
@ FDS_OP_INIT_TAG_DATA
@ FDS_OP_INIT_TAG_SWAP
fds_delete_step_t
@ FDS_OP_DEL_RECORD_FLAG_DIRTY
@ FDS_OP_DEL_DONE
@ FDS_OP_DEL_FILE_FLAG_DIRTY
#define FDS_DATA_PAGES
fds_page_type_t
@ FDS_PAGE_ERASED
@ FDS_PAGE_SWAP
@ FDS_PAGE_UNDEFINED
@ FDS_PAGE_DATA
fds_write_step_t
@ FDS_OP_WRITE_FLAG_DIRTY
@ FDS_OP_WRITE_HEADER_FINALIZE
@ FDS_OP_WRITE_DONE
@ FDS_OP_WRITE_RECORD_ID
@ FDS_OP_WRITE_HEADER_BEGIN
@ FDS_OP_WRITE_DATA
@ FDS_OP_WRITE_FIND_RECORD
fds_gc_state_t state
uint32_t const * p_record_src
The record metadata as stored in flash.
Definition: fds.h:113
fds_op_code_t op_code
fds_delete_step_t step
uint16_t file_id
uint32_t record_to_delete
fds_header_t header
uint16_t page
void const * p_data
fds_init_step_t step
uint16_t record_key
fds_write_step_t step
uint16_t words_reserved
uint32_t volatile records_open
uint32_t const * p_addr
uint16_t write_offset
fds_page_type_t page_type
uint32_t const * p_addr