ChibiOS/HAL 9.0.0
hal_mfs.h
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013,2014,
3 2015,2016,2017,2018,2019,2020,2021 Giovanni Di Sirio.
4
5 This file is part of ChibiOS.
6
7 ChibiOS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation version 3 of the License.
10
11 ChibiOS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20/**
21 * @file hal_mfs.h
22 * @brief Managed Flash Storage module header.
23 *
24 * @addtogroup HAL_MFS
25 * @{
26 */
27
28#ifndef HAL_MFS_H
29#define HAL_MFS_H
30
31#include "hal_flash.h"
32
33/*===========================================================================*/
34/* Driver constants. */
35/*===========================================================================*/
36
37#define MFS_BANK_MAGIC_1 0xEC705ADEU
38#define MFS_BANK_MAGIC_2 0xF0339CC5U
39#define MFS_HEADER_MAGIC_1 0x5FAE45F0U
40#define MFS_HEADER_MAGIC_2 0xF045AE5FU
41
42/*===========================================================================*/
43/* Driver pre-compile time settings. */
44/*===========================================================================*/
45
46/**
47 * @name Configuration options
48 * @{
49 */
50/**
51 * @brief Maximum number of indexed records in the managed storage.
52 * @note Record indexes go from 1 to @p MFS_CFG_MAX_RECORDS.
53 */
54#if !defined(MFS_CFG_MAX_RECORDS) || defined(__DOXYGEN__)
55#define MFS_CFG_MAX_RECORDS 32
56#endif
57
58/**
59 * @brief Maximum number of repair attempts on partition mount.
60 */
61#if !defined(MFS_CFG_MAX_REPAIR_ATTEMPTS) || defined(__DOXYGEN__)
62#define MFS_CFG_MAX_REPAIR_ATTEMPTS 3
63#endif
64
65/**
66 * @brief Verify written data.
67 */
68#if !defined(MFS_CFG_WRITE_VERIFY) || defined(__DOXYGEN__)
69#define MFS_CFG_WRITE_VERIFY TRUE
70#endif
71
72/**
73 * @brief Enables a stronger and slower check procedure on mount.
74 * @details Strong checking requires reading of the whole written data and
75 * this can be slow, normal checking only checks integrity of
76 * metadata, data errors would be detected on read.
77 */
78#if !defined(MFS_CFG_STRONG_CHECKING) || defined(__DOXYGEN__)
79#define MFS_CFG_STRONG_CHECKING TRUE
80#endif
81
82/**
83 * @brief Size of the buffer used for data copying.
84 * @note The buffer size must be a power of two and not smaller than
85 * 16 bytes.
86 * @note Larger buffers improve performance, buffers with size multiple
87 * of the flash program page size work better.
88 */
89#if !defined(MFS_CFG_BUFFER_SIZE) || defined(__DOXYGEN__)
90#define MFS_CFG_BUFFER_SIZE 32
91#endif
92
93/**
94 * @brief Enforced memory alignment.
95 * @details This value must be a power of two, it enforces a memory alignment
96 * for records in the flash array. This is required when alignment
97 * constraints exist, for example when using a DTR mode on OSPI
98 * devices.
99 */
100#if !defined(MFS_CFG_MEMORY_ALIGNMENT) || defined(__DOXYGEN__)
101#define MFS_CFG_MEMORY_ALIGNMENT 2
102#endif
103
104/**
105 * @brief Maximum number of objects writable in a single transaction.
106 */
107#if !defined(MFS_CFG_TRANSACTION_MAX) || defined(__DOXYGEN__)
108#define MFS_CFG_TRANSACTION_MAX 16
109#endif
110/** @} */
111
112/*===========================================================================*/
113/* Derived constants and error checks. */
114/*===========================================================================*/
115
116#if MFS_CFG_MAX_RECORDS < 0
117#error "invalid MFS_CFG_MAX_RECORDS value"
118#endif
119
120#if (MFS_CFG_MAX_REPAIR_ATTEMPTS < 1) || \
121 (MFS_CFG_MAX_REPAIR_ATTEMPTS > 10)
122#error "invalid MFS_MAX_REPAIR_ATTEMPTS value"
123#endif
124
125#if MFS_CFG_BUFFER_SIZE < 16
126#error "invalid MFS_CFG_BUFFER_SIZE value"
127#endif
128
129#if (MFS_CFG_BUFFER_SIZE & (MFS_CFG_BUFFER_SIZE - 1)) != 0
130#error "MFS_CFG_BUFFER_SIZE is not a power of two"
131#endif
132
133#if (MFS_CFG_MEMORY_ALIGNMENT < 1) || \
134 (MFS_CFG_MEMORY_ALIGNMENT > MFS_CFG_BUFFER_SIZE)
135#error "invalid MFS_CFG_MEMORY_ALIGNMENT value"
136#endif
137
138#if (MFS_CFG_MEMORY_ALIGNMENT & (MFS_CFG_MEMORY_ALIGNMENT - 1)) != 0
139#error "MFS_CFG_MEMORY_ALIGNMENT is not a power of two"
140#endif
141
142#if (MFS_CFG_TRANSACTION_MAX < 0) || \
143 (MFS_CFG_TRANSACTION_MAX > MFS_CFG_MAX_RECORDS)
144#error "invalid MFS_CFG_TRANSACTION_MAX value"
145#endif
146
147/*===========================================================================*/
148/* Driver data structures and types. */
149/*===========================================================================*/
150
151/**
152 * @brief Type of a flash bank.
153 */
154typedef enum {
157} mfs_bank_t;
158
159/**
160 * @brief Type of driver state machine states.
161 */
169
170/**
171 * @brief Type of an MFS error code.
172 * @note Errors are negative integers, informative warnings are positive
173 * integers.
174 */
189
190/**
191 * @brief Type of a bank state assessment.
192 */
198
199/**
200 * @brief Type of a record identifier.
201 */
202typedef uint32_t mfs_id_t;
203
204/**
205 * @brief Type of a bank header.
206 * @note The header resides in the first 16 bytes of a bank.
207 */
208typedef union {
209 struct {
210 /**
211 * @brief Bank magic 1.
212 */
213 uint32_t magic1;
214 /**
215 * @brief Bank magic 2.
216 */
217 uint32_t magic2;
218 /**
219 * @brief Usage counter of the bank.
220 * @details This value is increased each time a bank swap is performed. It
221 * indicates how much wearing the flash has already endured.
222 */
223 uint32_t counter;
224 /**
225 * @brief Reserved field.
226 */
227 uint16_t reserved1;
228 /**
229 * @brief Header CRC.
230 */
231 uint16_t crc;
232 } fields;
233 uint8_t hdr8[16];
234 uint32_t hdr32[4];
236
237/**
238 * @brief Type of a data block header.
239 * @details This structure is placed before each written data block.
240 */
241typedef union {
242 struct {
243 /**
244 * @brief Data header magic 1.
245 */
246 uint32_t magic1;
247 /**
248 * @brief Data header magic 2.
249 */
250 uint32_t magic2;
251 /**
252 * @brief Record identifier.
253 */
254 uint16_t id;
255 /**
256 * @brief Data CRC.
257 */
258 uint16_t crc;
259 /**
260 * @brief Data size.
261 * @note The next record is located at @p MFS_ALIGN_NEXT(size).
262 */
263 uint32_t size;
264 } fields;
265 uint8_t hdr8[16];
266 uint32_t hdr32[4];
268
269typedef struct {
270 /**
271 * @brief Offset of the record header.
272 */
274 /**
275 * @brief Record data size.
276 */
277 uint32_t size;
279
280/**
281 * @brief Type of a MFS configuration structure.
282 */
283typedef struct {
284 /**
285 * @brief Flash driver associated to this MFS instance.
286 */
288 /**
289 * @brief Erased value.
290 */
291 uint32_t erased;
292 /**
293 * @brief Banks size.
294 */
296 /**
297 * @brief Base sector index for bank 0.
298 */
300 /**
301 * @brief Number of sectors for bank 0.
302 * @note The total size of bank0 sectors must be greater or equal to
303 * @p bank_size.
304 */
306 /**
307 * @brief Base sector index for bank 1.
308 */
310 /**
311 * @brief Number of sectors for bank 1.
312 * @note The total size of bank1 sectors must be greater or equal to
313 * @p bank_size.
314 */
316} MFSConfig;
317
318/**
319 * @brief Type of a buffered write/erase operation within a transaction.
320 */
321typedef struct {
322 /**
323 * @brief Written header offset.
324 */
326 /**
327 * @brief Written data size.
328 */
329 size_t size;
330 /**
331 * @brief Record identifier.
332 */
335
336/**
337 * @brief Type of an non-cacheable MFS buffer.
338 */
346
347/**
348 * @brief Type of an MFS instance.
349 */
350typedef struct {
351 /**
352 * @brief Driver state.
353 */
355 /**
356 * @brief Current configuration data.
357 */
359 /**
360 * @brief Bank currently in use.
361 */
363 /**
364 * @brief Usage counter of the current bank.
365 */
367 /**
368 * @brief Pointer to the next free position in the current bank.
369 */
371 /**
372 * @brief Used space in the current bank without considering erased records.
373 */
375 /**
376 * @brief Offsets of the most recent instance of the records.
377 * @note Zero means that there is not a record with that id.
378 */
380#if (MFS_CFG_TRANSACTION_MAX > 0) || defined(__DOXYGEN__)
381 /**
382 * @brief Next write offset for current transaction.
383 */
385 /**
386 * @brief Maximum offset for the transaction.
387 */
389 /**
390 * @brief Number of buffered operations in current transaction.
391 */
392 uint32_t tr_nops;
393 /**
394 * @brief Buffered operations in current transaction.
395 */
397#endif
398 /**
399 * @brief Associated non-cacheable buffer.
400 */
402} MFSDriver;
403
404/*===========================================================================*/
405/* Driver macros. */
406/*===========================================================================*/
407
408/**
409 * @name Error codes handling macros
410 * @{
411 */
412#define MFS_IS_ERROR(err) ((err) < MFS_NO_ERROR)
413#define MFS_IS_WARNING(err) ((err) > MFS_NO_ERROR)
414/** @} */
415
416/**
417 * @name Alignment macros
418 * @{
419 */
420#define MFS_ALIGN_MASK ((uint32_t)MFS_CFG_MEMORY_ALIGNMENT - 1U)
421#define MFS_IS_ALIGNED(v) (((uint32_t)(v) & MFS_ALIGN_MASK) == 0U)
422#define MFS_ALIGN_PREV(v) ((uint32_t)(v) & ~MFS_ALIGN_MASK)
423#define MFS_ALIGN_NEXT(v) (MFS_ALIGN_PREV(((uint32_t)(v) - 1U)) + \
424 MFS_CFG_MEMORY_ALIGNMENT)
425/** @} */
426
427/*===========================================================================*/
428/* External declarations. */
429/*===========================================================================*/
430
431#ifdef __cplusplus
432extern "C" {
433#endif
434 void mfsObjectInit(MFSDriver *devp, mfs_nocache_buffer_t *ncbuf);
435 mfs_error_t mfsStart(MFSDriver *devp, const MFSConfig *config);
436 void mfsStop(MFSDriver *devp);
439 size_t *np, uint8_t *buffer);
441 size_t n, const uint8_t *buffer);
444#if MFS_CFG_TRANSACTION_MAX > 0
445 mfs_error_t mfsStartTransaction(MFSDriver *mfsp, size_t size);
448#endif /* MFS_CFG_TRANSACTION_MAX > 0 */
449#ifdef __cplusplus
450}
451#endif
452
453#endif /* HAL_MFS_H */
454
455/** @} */
456
uint32_t flash_sector_t
Type of a flash sector number.
Definition hal_flash.h:117
uint32_t flash_offset_t
Type of a flash offset.
Definition hal_flash.h:112
union mfs_nocache_buffer mfs_nocache_buffer_t
Type of an non-cacheable MFS buffer.
mfs_error_t mfsEraseRecord(MFSDriver *mfsp, mfs_id_t id)
Erases a data record.
Definition hal_mfs.c:1155
#define MFS_CFG_MAX_RECORDS
Maximum number of indexed records in the managed storage.
Definition hal_mfs.h:55
#define MFS_CFG_BUFFER_SIZE
Size of the buffer used for data copying.
Definition hal_mfs.h:90
mfs_error_t mfsReadRecord(MFSDriver *mfsp, mfs_id_t id, size_t *np, uint8_t *buffer)
Retrieves and reads a data record.
Definition hal_mfs.c:933
mfs_error_t
Type of an MFS error code.
Definition hal_mfs.h:175
mfs_state_t
Type of driver state machine states.
Definition hal_mfs.h:162
mfs_error_t mfsStart(MFSDriver *mfsp, const MFSConfig *config)
Configures and activates a MFS driver.
Definition hal_mfs.c:850
#define MFS_CFG_TRANSACTION_MAX
Maximum number of objects writable in a single transaction.
Definition hal_mfs.h:108
mfs_error_t mfsPerformGarbageCollection(MFSDriver *mfsp)
Enforces a garbage collection operation.
Definition hal_mfs.c:1279
void mfsObjectInit(MFSDriver *mfsp, mfs_nocache_buffer_t *ncbuf)
Initializes an instance.
Definition hal_mfs.c:824
mfs_error_t mfsWriteRecord(MFSDriver *mfsp, mfs_id_t id, size_t n, const uint8_t *buffer)
Creates or updates a data record.
Definition hal_mfs.c:1006
mfs_error_t mfsStartTransaction(MFSDriver *mfsp, size_t size)
Puts the driver in transaction mode.
Definition hal_mfs.c:1320
uint32_t mfs_id_t
Type of a record identifier.
Definition hal_mfs.h:202
mfs_error_t mfsRollbackTransaction(MFSDriver *mfsp)
A transaction is rolled back atomically.
Definition hal_mfs.c:1457
mfs_bank_t
Type of a flash bank.
Definition hal_mfs.h:154
mfs_error_t mfsCommitTransaction(MFSDriver *mfsp)
A transaction is committed and finalized atomically.
Definition hal_mfs.c:1376
void mfsStop(MFSDriver *mfsp)
Deactivates a MFS driver.
Definition hal_mfs.c:869
mfs_error_t mfsErase(MFSDriver *mfsp)
Destroys the state of the managed storage by erasing the flash.
Definition hal_mfs.c:895
mfs_bank_state_t
Type of a bank state assessment.
Definition hal_mfs.h:193
@ MFS_ERR_INV_SIZE
Definition hal_mfs.h:180
@ MFS_ERR_FLASH_FAILURE
Definition hal_mfs.h:186
@ MFS_ERR_NOT_ERASED
Definition hal_mfs.h:185
@ MFS_ERR_INTERNAL
Definition hal_mfs.h:187
@ MFS_ERR_TRANSACTION_SIZE
Definition hal_mfs.h:184
@ MFS_ERR_NOT_FOUND
Definition hal_mfs.h:181
@ MFS_WARN_GC
Definition hal_mfs.h:178
@ MFS_ERR_TRANSACTION_NUM
Definition hal_mfs.h:183
@ MFS_NO_ERROR
Definition hal_mfs.h:176
@ MFS_ERR_OUT_OF_MEM
Definition hal_mfs.h:182
@ MFS_WARN_REPAIR
Definition hal_mfs.h:177
@ MFS_ERR_INV_STATE
Definition hal_mfs.h:179
@ MFS_TRANSACTION
Definition hal_mfs.h:166
@ MFS_ERROR
Definition hal_mfs.h:167
@ MFS_UNINIT
Definition hal_mfs.h:163
@ MFS_STOP
Definition hal_mfs.h:164
@ MFS_READY
Definition hal_mfs.h:165
@ MFS_BANK_1
Definition hal_mfs.h:156
@ MFS_BANK_0
Definition hal_mfs.h:155
@ MFS_BANK_GARBAGE
Definition hal_mfs.h:196
@ MFS_BANK_ERASED
Definition hal_mfs.h:194
@ MFS_BANK_OK
Definition hal_mfs.h:195
Generic flash driver class header.
Base flash class.
Definition hal_flash.h:224
Type of a MFS configuration structure.
Definition hal_mfs.h:283
uint32_t erased
Erased value.
Definition hal_mfs.h:291
flash_sector_t bank1_start
Base sector index for bank 1.
Definition hal_mfs.h:309
flash_sector_t bank0_start
Base sector index for bank 0.
Definition hal_mfs.h:299
flash_sector_t bank0_sectors
Number of sectors for bank 0.
Definition hal_mfs.h:305
flash_offset_t bank_size
Banks size.
Definition hal_mfs.h:295
BaseFlash * flashp
Flash driver associated to this MFS instance.
Definition hal_mfs.h:287
flash_sector_t bank1_sectors
Number of sectors for bank 1.
Definition hal_mfs.h:315
Type of an MFS instance.
Definition hal_mfs.h:350
uint32_t current_counter
Usage counter of the current bank.
Definition hal_mfs.h:366
flash_offset_t tr_limit_offet
Maximum offset for the transaction.
Definition hal_mfs.h:388
flash_offset_t next_offset
Pointer to the next free position in the current bank.
Definition hal_mfs.h:370
mfs_transaction_op_t tr_ops[MFS_CFG_TRANSACTION_MAX]
Buffered operations in current transaction.
Definition hal_mfs.h:396
mfs_bank_t current_bank
Bank currently in use.
Definition hal_mfs.h:362
mfs_record_descriptor_t descriptors[MFS_CFG_MAX_RECORDS]
Offsets of the most recent instance of the records.
Definition hal_mfs.h:379
const MFSConfig * config
Current configuration data.
Definition hal_mfs.h:358
uint32_t tr_nops
Number of buffered operations in current transaction.
Definition hal_mfs.h:392
flash_offset_t tr_next_offset
Next write offset for current transaction.
Definition hal_mfs.h:384
mfs_nocache_buffer_t * ncbuf
Associated non-cacheable buffer.
Definition hal_mfs.h:401
flash_offset_t used_space
Used space in the current bank without considering erased records.
Definition hal_mfs.h:374
mfs_state_t state
Driver state.
Definition hal_mfs.h:354
flash_offset_t offset
Offset of the record header.
Definition hal_mfs.h:273
uint32_t size
Record data size.
Definition hal_mfs.h:277
Type of a buffered write/erase operation within a transaction.
Definition hal_mfs.h:321
flash_offset_t offset
Written header offset.
Definition hal_mfs.h:325
size_t size
Written data size.
Definition hal_mfs.h:329
mfs_id_t id
Record identifier.
Definition hal_mfs.h:333
Type of a bank header.
Definition hal_mfs.h:208
uint32_t hdr32[4]
Definition hal_mfs.h:234
uint16_t crc
Header CRC.
Definition hal_mfs.h:231
uint16_t reserved1
Reserved field.
Definition hal_mfs.h:227
uint32_t magic2
Bank magic 2.
Definition hal_mfs.h:217
uint32_t counter
Usage counter of the bank.
Definition hal_mfs.h:223
uint8_t hdr8[16]
Definition hal_mfs.h:233
uint32_t magic1
Bank magic 1.
Definition hal_mfs.h:213
Type of a data block header.
Definition hal_mfs.h:241
uint8_t hdr8[16]
Definition hal_mfs.h:265
uint16_t id
Record identifier.
Definition hal_mfs.h:254
uint32_t hdr32[4]
Definition hal_mfs.h:266
uint32_t magic1
Data header magic 1.
Definition hal_mfs.h:246
uint32_t magic2
Data header magic 2.
Definition hal_mfs.h:250
uint16_t crc
Data CRC.
Definition hal_mfs.h:258
uint32_t size
Data size.
Definition hal_mfs.h:263
Type of an non-cacheable MFS buffer.
Definition hal_mfs.h:339
mfs_bank_header_t bhdr
Definition hal_mfs.h:341
uint8_t data8[MFS_CFG_BUFFER_SIZE]
Definition hal_mfs.h:342
uint16_t data16[MFS_CFG_BUFFER_SIZE/sizeof(uint16_t)]
Definition hal_mfs.h:343
uint32_t data32[MFS_CFG_BUFFER_SIZE/sizeof(uint32_t)]
Definition hal_mfs.h:344
mfs_data_header_t dhdr
Definition hal_mfs.h:340