ChibiOS/HAL  7.0.3
hal_mfs.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio.
3 
4  This file is part of ChibiOS.
5 
6  ChibiOS is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
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  */
154 typedef enum {
155  MFS_BANK_0 = 0,
156  MFS_BANK_1 = 1
157 } mfs_bank_t;
158 
159 /**
160  * @brief Type of driver state machine states.
161  */
162 typedef enum {
163  MFS_UNINIT = 0,
164  MFS_STOP = 1,
165  MFS_READY = 2,
166  MFS_TRANSACTION = 3,
167  MFS_ERROR = 4
168 } mfs_state_t;
169 
170 /**
171  * @brief Type of an MFS error code.
172  * @note Errors are negative integers, informative warnings are positive
173  * integers.
174  */
175 typedef enum {
176  MFS_NO_ERROR = 0,
177  MFS_WARN_REPAIR = 1,
178  MFS_WARN_GC = 2,
179  MFS_ERR_INV_STATE = -1,
180  MFS_ERR_INV_SIZE = -2,
181  MFS_ERR_NOT_FOUND = -3,
182  MFS_ERR_OUT_OF_MEM = -4,
183  MFS_ERR_TRANSACTION_NUM = -5,
184  MFS_ERR_TRANSACTION_SIZE = -6,
185  MFS_ERR_NOT_ERASED = -7,
186  MFS_ERR_FLASH_FAILURE = -8,
187  MFS_ERR_INTERNAL = -9
188 } mfs_error_t;
189 
190 /**
191  * @brief Type of a bank state assessment.
192  */
193 typedef enum {
194  MFS_BANK_ERASED = 0,
195  MFS_BANK_OK = 1,
196  MFS_BANK_GARBAGE = 2
198 
199 /**
200  * @brief Type of a record identifier.
201  */
202 typedef 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  */
208 typedef 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  */
241 typedef 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 
269 typedef struct {
270  /**
271  * @brief Offset of the record header.
272  */
273  flash_offset_t offset;
274  /**
275  * @brief Record data size.
276  */
277  uint32_t size;
278 } mfs_record_descriptor_t;
279 
280 /**
281  * @brief Type of a MFS configuration structure.
282  */
283 typedef 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  */
321 typedef 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 MFS instance.
338  */
339 typedef struct {
340  /**
341  * @brief Driver state.
342  */
344  /**
345  * @brief Current configuration data.
346  */
348  /**
349  * @brief Bank currently in use.
350  */
352  /**
353  * @brief Usage counter of the current bank.
354  */
355  uint32_t current_counter;
356  /**
357  * @brief Pointer to the next free position in the current bank.
358  */
360  /**
361  * @brief Used space in the current bank without considering erased records.
362  */
364  /**
365  * @brief Offsets of the most recent instance of the records.
366  * @note Zero means that there is not a record with that id.
367  */
368  mfs_record_descriptor_t descriptors[MFS_CFG_MAX_RECORDS];
369 #if (MFS_CFG_TRANSACTION_MAX > 0) || defined(__DOXYGEN__)
370  /**
371  * @brief Next write offset for current transaction.
372  */
374  /**
375  * @brief Maximum offset for the transaction.
376  */
378  /**
379  * @brief Number of buffered operations in current transaction.
380  */
381  uint32_t tr_nops;
382  /**
383  * @brief Buffered operations in current transaction.
384  */
386 #endif
387  /**
388  * @brief Transient buffer.
389  */
390  union {
391  mfs_data_header_t dhdr;
392  mfs_bank_header_t bhdr;
393  uint8_t data8[MFS_CFG_BUFFER_SIZE];
394  uint16_t data16[MFS_CFG_BUFFER_SIZE / sizeof (uint16_t)];
395  uint32_t data32[MFS_CFG_BUFFER_SIZE / sizeof (uint32_t)];
396  } buffer;
397 } MFSDriver;
398 
399 /*===========================================================================*/
400 /* Driver macros. */
401 /*===========================================================================*/
402 
403 /**
404  * @name Error codes handling macros
405  * @{
406  */
407 #define MFS_IS_ERROR(err) ((err) < MFS_NO_ERROR)
408 #define MFS_IS_WARNING(err) ((err) > MFS_NO_ERROR)
409 /** @} */
410 
411 /**
412  * @name Alignment macros
413  * @{
414  */
415 #define MFS_ALIGN_MASK ((uint32_t)MFS_CFG_MEMORY_ALIGNMENT - 1U)
416 #define MFS_IS_ALIGNED(v) (((uint32_t)(v) & MFS_ALIGN_MASK) == 0U)
417 #define MFS_ALIGN_PREV(v) ((uint32_t)(v) & ~MFS_ALIGN_MASK)
418 #define MFS_ALIGN_NEXT(v) (MFS_ALIGN_PREV(((uint32_t)(v) - 1U)) + \
419  MFS_CFG_MEMORY_ALIGNMENT)
420 /** @} */
421 
422 /*===========================================================================*/
423 /* External declarations. */
424 /*===========================================================================*/
425 
426 #ifdef __cplusplus
427 extern "C" {
428 #endif
429  void mfsObjectInit(MFSDriver *devp);
430  mfs_error_t mfsStart(MFSDriver *devp, const MFSConfig *config);
431  void mfsStop(MFSDriver *devp);
434  size_t *np, uint8_t *buffer);
436  size_t n, const uint8_t *buffer);
439 #if MFS_CFG_TRANSACTION_MAX > 0
440  mfs_error_t mfsStartTransaction(MFSDriver *mfsp, size_t size);
443 #endif /* MFS_CFG_TRANSACTION_MAX > 0 */
444 #ifdef __cplusplus
445 }
446 #endif
447 
448 #endif /* HAL_MFS_H */
449 
450 /** @} */
451 
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:932
uint16_t crc
Data CRC.
Definition: hal_mfs.h:258
void mfsObjectInit(MFSDriver *mfsp)
Initializes an instance.
Definition: hal_mfs.c:824
Type of a buffered write/erase operation within a transaction.
Definition: hal_mfs.h:321
uint32_t flash_sector_t
Type of a flash sector number.
Definition: hal_flash.h:88
uint32_t erased
Erased value.
Definition: hal_mfs.h:291
#define MFS_CFG_TRANSACTION_MAX
Maximum number of objects writable in a single transaction.
Definition: hal_mfs.h:108
mfs_state_t state
Driver state.
Definition: hal_mfs.h:343
mfs_id_t id
Record identifier.
Definition: hal_mfs.h:333
size_t size
Written data size.
Definition: hal_mfs.h:329
Type of a MFS configuration structure.
Definition: hal_mfs.h:283
uint32_t mfs_id_t
Type of a record identifier.
Definition: hal_mfs.h:202
void mfsStop(MFSDriver *mfsp)
Deactivates a MFS driver.
Definition: hal_mfs.c:868
flash_offset_t used_space
Used space in the current bank without considering erased records.
Definition: hal_mfs.h:363
Type of a data block header.
Definition: hal_mfs.h:241
mfs_error_t mfsRollbackTransaction(MFSDriver *mfsp)
A transaction is rolled back atomically.
Definition: hal_mfs.c:1457
uint32_t tr_nops
Number of buffered operations in current transaction.
Definition: hal_mfs.h:381
flash_offset_t tr_next_offset
Next write offset for current transaction.
Definition: hal_mfs.h:373
uint32_t size
Data size.
Definition: hal_mfs.h:263
mfs_error_t mfsPerformGarbageCollection(MFSDriver *mfsp)
Enforces a garbage collection operation.
Definition: hal_mfs.c:1278
mfs_bank_state_t
Type of a bank state assessment.
Definition: hal_mfs.h:193
uint16_t crc
Header CRC.
Definition: hal_mfs.h:231
mfs_error_t mfsStart(MFSDriver *mfsp, const MFSConfig *config)
Configures and activates a MFS driver.
Definition: hal_mfs.c:849
mfs_error_t mfsCommitTransaction(MFSDriver *mfsp)
A transaction is committed and finalized atomically.
Definition: hal_mfs.c:1376
uint32_t magic1
Bank magic 1.
Definition: hal_mfs.h:213
Base flash class.
Definition: hal_flash.h:187
flash_offset_t bank_size
Banks size.
Definition: hal_mfs.h:295
flash_offset_t offset
Written header offset.
Definition: hal_mfs.h:325
#define MFS_CFG_BUFFER_SIZE
Size of the buffer used for data copying.
Definition: hal_mfs.h:90
mfs_state_t
Type of driver state machine states.
Definition: hal_mfs.h:162
uint16_t reserved1
Reserved field.
Definition: hal_mfs.h:227
mfs_error_t mfsEraseRecord(MFSDriver *mfsp, mfs_id_t id)
Erases a data record.
Definition: hal_mfs.c:1154
flash_sector_t bank1_start
Base sector index for bank 1.
Definition: hal_mfs.h:309
flash_sector_t bank0_sectors
Number of sectors for bank 0.
Definition: hal_mfs.h:305
mfs_error_t mfsStartTransaction(MFSDriver *mfsp, size_t size)
Puts the driver in transaction mode.
Definition: hal_mfs.c:1320
flash_sector_t bank1_sectors
Number of sectors for bank 1.
Definition: hal_mfs.h:315
flash_offset_t next_offset
Pointer to the next free position in the current bank.
Definition: hal_mfs.h:359
mfs_error_t mfsErase(MFSDriver *mfsp)
Destroys the state of the managed storage by erasing the flash.
Definition: hal_mfs.c:894
Type of a bank header.
Definition: hal_mfs.h:208
uint32_t flash_offset_t
Type of a flash offset.
Definition: hal_flash.h:83
#define MFS_CFG_MAX_RECORDS
Maximum number of indexed records in the managed storage.
Definition: hal_mfs.h:55
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:1005
uint32_t counter
Usage counter of the bank.
Definition: hal_mfs.h:223
Type of an MFS instance.
Definition: hal_mfs.h:339
uint32_t magic1
Data header magic 1.
Definition: hal_mfs.h:246
mfs_error_t
Type of an MFS error code.
Definition: hal_mfs.h:175
mfs_bank_t current_bank
Bank currently in use.
Definition: hal_mfs.h:351
uint32_t current_counter
Usage counter of the current bank.
Definition: hal_mfs.h:355
Generic flash driver class header.
uint32_t magic2
Data header magic 2.
Definition: hal_mfs.h:250
flash_sector_t bank0_start
Base sector index for bank 0.
Definition: hal_mfs.h:299
BaseFlash * flashp
Flash driver associated to this MFS instance.
Definition: hal_mfs.h:287
mfs_bank_t
Type of a flash bank.
Definition: hal_mfs.h:154
flash_offset_t tr_limit_offet
Maximum offset for the transaction.
Definition: hal_mfs.h:377
uint32_t magic2
Bank magic 2.
Definition: hal_mfs.h:217
const MFSConfig * config
Current configuration data.
Definition: hal_mfs.h:347
uint16_t id
Record identifier.
Definition: hal_mfs.h:254