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.
120
}
fds_page_type_t
;
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.
128
}
fds_header_status_t
;
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
;
146
}
fds_swap_page_t
;
147
148
149
// FDS op-codes.
150
typedef
enum
151
{
152
FDS_OP_NONE
,
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.
159
}
fds_op_code_t
;
160
161
162
typedef
enum
163
{
164
FDS_OP_INIT_TAG_SWAP
,
165
FDS_OP_INIT_TAG_DATA
,
166
FDS_OP_INIT_ERASE_SWAP
,
167
FDS_OP_INIT_PROMOTE_SWAP
,
168
}
fds_init_step_t
;
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.
177
FDS_OP_WRITE_FIND_RECORD
,
178
FDS_OP_WRITE_FLAG_DIRTY
,
// Flag a record as dirty (as part of an update operation).
179
FDS_OP_WRITE_DONE
,
180
}
fds_write_step_t
;
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.
187
FDS_OP_DEL_DONE
,
188
}
fds_delete_step_t
;
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
{
211
fds_header_t
header
;
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
{
219
fds_delete_step_t
step
;
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.
249
NO_PAGES
,
250
251
// The filesystem can not be garbage collected.
252
// This is a fatal error.
253
NO_SWAP
= (
PAGE_DATA
),
254
255
// Perform a fresh installation.
256
FRESH_INSTALL
= (
PAGE_ERASED
),
257
258
// Tag an erased page as swap.
259
TAG_SWAP
= (
PAGE_ERASED
|
PAGE_DATA
),
260
261
// Tag all erased pages as data.
262
TAG_DATA
= (
PAGE_ERASED
|
PAGE_SWAP_CLEAN
),
263
264
// Tag all remaining erased pages as data.
265
TAG_DATA_INST
= (
PAGE_ERASED
|
PAGE_DATA
|
PAGE_SWAP_CLEAN
),
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.
270
PROMOTE_SWAP
= (
PAGE_ERASED
|
PAGE_SWAP_DIRTY
),
271
272
// Tag the swap as data (promote), an erased page as swap and remaining pages as data.
273
PROMOTE_SWAP_INST
= (
PAGE_ERASED
|
PAGE_DATA
|
PAGE_SWAP_DIRTY
),
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.
278
DISCARD_SWAP
= (
PAGE_DATA
|
PAGE_SWAP_DIRTY
),
279
280
// Do nothing.
281
ALREADY_INSTALLED
= (
PAGE_DATA
|
PAGE_SWAP_CLEAN
),
282
283
}
fds_init_opts_t
;
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.
296
}
fds_gc_state_t
;
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
fds_init_opts_t
Definition:
fds_internal_defs.h:246
TAG_SWAP
@ TAG_SWAP
Definition:
fds_internal_defs.h:259
DISCARD_SWAP
@ DISCARD_SWAP
Definition:
fds_internal_defs.h:278
ALREADY_INSTALLED
@ ALREADY_INSTALLED
Definition:
fds_internal_defs.h:281
NO_PAGES
@ NO_PAGES
Definition:
fds_internal_defs.h:249
TAG_DATA
@ TAG_DATA
Definition:
fds_internal_defs.h:262
FRESH_INSTALL
@ FRESH_INSTALL
Definition:
fds_internal_defs.h:256
TAG_DATA_INST
@ TAG_DATA_INST
Definition:
fds_internal_defs.h:265
PROMOTE_SWAP
@ PROMOTE_SWAP
Definition:
fds_internal_defs.h:270
NO_SWAP
@ NO_SWAP
Definition:
fds_internal_defs.h:253
PROMOTE_SWAP_INST
@ PROMOTE_SWAP_INST
Definition:
fds_internal_defs.h:273
fds_gc_state_t
fds_gc_state_t
Definition:
fds_internal_defs.h:287
GC_PROMOTE_SWAP
@ GC_PROMOTE_SWAP
Definition:
fds_internal_defs.h:294
GC_TAG_NEW_SWAP
@ GC_TAG_NEW_SWAP
Definition:
fds_internal_defs.h:295
GC_COPY_RECORD
@ GC_COPY_RECORD
Definition:
fds_internal_defs.h:291
GC_ERASE_PAGE
@ GC_ERASE_PAGE
Definition:
fds_internal_defs.h:292
GC_NEXT_PAGE
@ GC_NEXT_PAGE
Definition:
fds_internal_defs.h:289
GC_FIND_NEXT_RECORD
@ GC_FIND_NEXT_RECORD
Definition:
fds_internal_defs.h:290
GC_BEGIN
@ GC_BEGIN
Definition:
fds_internal_defs.h:288
GC_DISCARD_SWAP
@ GC_DISCARD_SWAP
Definition:
fds_internal_defs.h:293
fds_op_code_t
fds_op_code_t
Definition:
fds_internal_defs.h:151
FDS_OP_DEL_FILE
@ FDS_OP_DEL_FILE
Definition:
fds_internal_defs.h:157
FDS_OP_WRITE
@ FDS_OP_WRITE
Definition:
fds_internal_defs.h:154
FDS_OP_UPDATE
@ FDS_OP_UPDATE
Definition:
fds_internal_defs.h:155
FDS_OP_NONE
@ FDS_OP_NONE
Definition:
fds_internal_defs.h:152
FDS_OP_INIT
@ FDS_OP_INIT
Definition:
fds_internal_defs.h:153
FDS_OP_GC
@ FDS_OP_GC
Definition:
fds_internal_defs.h:158
FDS_OP_DEL_RECORD
@ FDS_OP_DEL_RECORD
Definition:
fds_internal_defs.h:156
fds_header_status_t
fds_header_status_t
Definition:
fds_internal_defs.h:124
FDS_HEADER_DIRTY
@ FDS_HEADER_DIRTY
Definition:
fds_internal_defs.h:126
FDS_HEADER_VALID
@ FDS_HEADER_VALID
Definition:
fds_internal_defs.h:125
FDS_HEADER_CORRUPT
@ FDS_HEADER_CORRUPT
Definition:
fds_internal_defs.h:127
PAGE_SWAP_CLEAN
@ PAGE_SWAP_CLEAN
Definition:
fds_internal_defs.h:240
PAGE_DATA
@ PAGE_DATA
Definition:
fds_internal_defs.h:239
PAGE_SWAP_DIRTY
@ PAGE_SWAP_DIRTY
Definition:
fds_internal_defs.h:241
PAGE_ERASED
@ PAGE_ERASED
Definition:
fds_internal_defs.h:238
fds_init_step_t
fds_init_step_t
Definition:
fds_internal_defs.h:163
FDS_OP_INIT_ERASE_SWAP
@ FDS_OP_INIT_ERASE_SWAP
Definition:
fds_internal_defs.h:166
FDS_OP_INIT_PROMOTE_SWAP
@ FDS_OP_INIT_PROMOTE_SWAP
Definition:
fds_internal_defs.h:167
FDS_OP_INIT_TAG_DATA
@ FDS_OP_INIT_TAG_DATA
Definition:
fds_internal_defs.h:165
FDS_OP_INIT_TAG_SWAP
@ FDS_OP_INIT_TAG_SWAP
Definition:
fds_internal_defs.h:164
fds_delete_step_t
fds_delete_step_t
Definition:
fds_internal_defs.h:184
FDS_OP_DEL_RECORD_FLAG_DIRTY
@ FDS_OP_DEL_RECORD_FLAG_DIRTY
Definition:
fds_internal_defs.h:185
FDS_OP_DEL_DONE
@ FDS_OP_DEL_DONE
Definition:
fds_internal_defs.h:187
FDS_OP_DEL_FILE_FLAG_DIRTY
@ FDS_OP_DEL_FILE_FLAG_DIRTY
Definition:
fds_internal_defs.h:186
FDS_DATA_PAGES
#define FDS_DATA_PAGES
Definition:
fds_internal_defs.h:99
fds_page_type_t
fds_page_type_t
Definition:
fds_internal_defs.h:115
FDS_PAGE_ERASED
@ FDS_PAGE_ERASED
Definition:
fds_internal_defs.h:118
FDS_PAGE_SWAP
@ FDS_PAGE_SWAP
Definition:
fds_internal_defs.h:117
FDS_PAGE_UNDEFINED
@ FDS_PAGE_UNDEFINED
Definition:
fds_internal_defs.h:119
FDS_PAGE_DATA
@ FDS_PAGE_DATA
Definition:
fds_internal_defs.h:116
fds_write_step_t
fds_write_step_t
Definition:
fds_internal_defs.h:172
FDS_OP_WRITE_FLAG_DIRTY
@ FDS_OP_WRITE_FLAG_DIRTY
Definition:
fds_internal_defs.h:178
FDS_OP_WRITE_HEADER_FINALIZE
@ FDS_OP_WRITE_HEADER_FINALIZE
Definition:
fds_internal_defs.h:174
FDS_OP_WRITE_DONE
@ FDS_OP_WRITE_DONE
Definition:
fds_internal_defs.h:179
FDS_OP_WRITE_RECORD_ID
@ FDS_OP_WRITE_RECORD_ID
Definition:
fds_internal_defs.h:175
FDS_OP_WRITE_HEADER_BEGIN
@ FDS_OP_WRITE_HEADER_BEGIN
Definition:
fds_internal_defs.h:173
FDS_OP_WRITE_DATA
@ FDS_OP_WRITE_DATA
Definition:
fds_internal_defs.h:176
FDS_OP_WRITE_FIND_RECORD
@ FDS_OP_WRITE_FIND_RECORD
Definition:
fds_internal_defs.h:177
sdk_config.h
fds_gc_data_t
Definition:
fds_internal_defs.h:301
fds_gc_data_t::state
fds_gc_state_t state
Definition:
fds_internal_defs.h:302
fds_gc_data_t::resume
bool resume
Definition:
fds_internal_defs.h:307
fds_gc_data_t::run_count
uint16_t run_count
Definition:
fds_internal_defs.h:305
fds_gc_data_t::cur_page
uint16_t cur_page
Definition:
fds_internal_defs.h:303
fds_gc_data_t::p_record_src
uint32_t const * p_record_src
Definition:
fds_internal_defs.h:304
fds_header_t
The record metadata as stored in flash.
Definition:
fds.h:113
fds_op_t
Definition:
fds_internal_defs.h:201
fds_op_t::op_code
fds_op_code_t op_code
Definition:
fds_internal_defs.h:202
fds_op_t::step
fds_delete_step_t step
Definition:
fds_internal_defs.h:219
fds_op_t::file_id
uint16_t file_id
Definition:
fds_internal_defs.h:220
fds_op_t::record_to_delete
uint32_t record_to_delete
Definition:
fds_internal_defs.h:215
fds_op_t::header
fds_header_t header
Definition:
fds_internal_defs.h:211
fds_op_t::page
uint16_t page
Definition:
fds_internal_defs.h:213
fds_op_t::p_data
void const * p_data
Definition:
fds_internal_defs.h:212
fds_op_t::step
fds_init_step_t step
Definition:
fds_internal_defs.h:207
fds_op_t::record_key
uint16_t record_key
Definition:
fds_internal_defs.h:221
fds_op_t::step
fds_write_step_t step
Definition:
fds_internal_defs.h:214
fds_page_t
Definition:
fds_internal_defs.h:132
fds_page_t::can_gc
bool can_gc
Definition:
fds_internal_defs.h:138
fds_page_t::words_reserved
uint16_t words_reserved
Definition:
fds_internal_defs.h:136
fds_page_t::records_open
uint32_t volatile records_open
Definition:
fds_internal_defs.h:137
fds_page_t::p_addr
uint32_t const * p_addr
Definition:
fds_internal_defs.h:134
fds_page_t::write_offset
uint16_t write_offset
Definition:
fds_internal_defs.h:135
fds_page_t::page_type
fds_page_type_t page_type
Definition:
fds_internal_defs.h:133
fds_swap_page_t
Definition:
fds_internal_defs.h:143
fds_swap_page_t::p_addr
uint32_t const * p_addr
Definition:
fds_internal_defs.h:144
fds_swap_page_t::write_offset
uint16_t write_offset
Definition:
fds_internal_defs.h:145
src
nrf5_sdk15_platform
ruuvi.nrf_sdk15_3_overrides.c
fds_internal_defs.h
Generated by
1.9.1