ChibiOS/HAL 9.0.0
Managed Flash Storage Driver

Managed Flash Storage Driver. More...

Detailed Description

Managed Flash Storage Driver.

This module implements a managed flash storage able to store a finite number of variable-size records. Records are retrieved by their index number.
The driver is automatically performs:

  • Wear leveling.
  • Auto repair after power loss.
  • Garbage collection in order to remove erased data.
Collaboration diagram for Managed Flash Storage Driver:

Configuration options

#define MFS_CFG_MAX_RECORDS   32
 Maximum number of indexed records in the managed storage.
#define MFS_CFG_MAX_REPAIR_ATTEMPTS   3
 Maximum number of repair attempts on partition mount.
#define MFS_CFG_WRITE_VERIFY   TRUE
 Verify written data.
#define MFS_CFG_STRONG_CHECKING   TRUE
 Enables a stronger and slower check procedure on mount.
#define MFS_CFG_BUFFER_SIZE   32
 Size of the buffer used for data copying.
#define MFS_CFG_MEMORY_ALIGNMENT   2
 Enforced memory alignment.
#define MFS_CFG_TRANSACTION_MAX   16
 Maximum number of objects writable in a single transaction.

Error codes handling macros

#define MFS_IS_ERROR(err)
#define MFS_IS_WARNING(err)

Alignment macros

#define MFS_ALIGN_MASK   ((uint32_t)MFS_CFG_MEMORY_ALIGNMENT - 1U)
#define MFS_IS_ALIGNED(v)
#define MFS_ALIGN_PREV(v)
#define MFS_ALIGN_NEXT(v)

Data Structures

union  mfs_bank_header_t
 Type of a bank header. More...
union  mfs_data_header_t
 Type of a data block header. More...
struct  mfs_record_descriptor_t
struct  MFSConfig
 Type of a MFS configuration structure. More...
struct  mfs_transaction_op_t
 Type of a buffered write/erase operation within a transaction. More...
union  mfs_nocache_buffer
 Type of an non-cacheable MFS buffer. More...
struct  MFSDriver
 Type of an MFS instance. More...

Macros

#define ALIGNED_REC_SIZE(n)
 Data record size aligned.
#define ALIGNED_DHDR_SIZE    ALIGNED_REC_SIZE(0)
 Data record header size aligned.
#define ALIGNED_SIZEOF(t)
 Aligned size of a type.
#define PAIR(a, b)
 Combines two values (0..3) in one (0..15).
#define RET_ON_ERROR(err)
 Error check helper.
#define MFS_BANK_MAGIC_1   0xEC705ADEU
#define MFS_BANK_MAGIC_2   0xF0339CC5U
#define MFS_HEADER_MAGIC_1   0x5FAE45F0U
#define MFS_HEADER_MAGIC_2   0xF045AE5FU

Typedefs

typedef uint32_t mfs_id_t
 Type of a record identifier.
typedef union mfs_nocache_buffer mfs_nocache_buffer_t
 Type of an non-cacheable MFS buffer.

Enumerations

enum  mfs_bank_t { MFS_BANK_0 = 0 , MFS_BANK_1 = 1 }
 Type of a flash bank. More...
enum  mfs_state_t {
  MFS_UNINIT = 0 , MFS_STOP = 1 , MFS_READY = 2 , MFS_TRANSACTION = 3 ,
  MFS_ERROR = 4
}
 Type of driver state machine states. More...
enum  mfs_error_t {
  MFS_NO_ERROR = 0 , MFS_WARN_REPAIR = 1 , MFS_WARN_GC = 2 , MFS_ERR_INV_STATE = -1 ,
  MFS_ERR_INV_SIZE = -2 , MFS_ERR_NOT_FOUND = -3 , MFS_ERR_OUT_OF_MEM = -4 , MFS_ERR_TRANSACTION_NUM = -5 ,
  MFS_ERR_TRANSACTION_SIZE = -6 , MFS_ERR_NOT_ERASED = -7 , MFS_ERR_FLASH_FAILURE = -8 , MFS_ERR_INTERNAL = -9
}
 Type of an MFS error code. More...
enum  mfs_bank_state_t { MFS_BANK_ERASED = 0 , MFS_BANK_OK = 1 , MFS_BANK_GARBAGE = 2 }
 Type of a bank state assessment. More...

Functions

uint16_t crc16 (uint16_t crc, const uint8_t *data, size_t n)
static void mfs_state_reset (MFSDriver *mfsp)
static flash_offset_t mfs_flash_get_bank_offset (MFSDriver *mfsp, mfs_bank_t bank)
static mfs_error_t mfs_flash_read (MFSDriver *mfsp, flash_offset_t offset, size_t n, uint8_t *rp)
 Flash read.
static mfs_error_t mfs_flash_write (MFSDriver *mfsp, flash_offset_t offset, size_t n, const uint8_t *wp)
 Flash write.
static mfs_error_t mfs_flash_copy (MFSDriver *mfsp, flash_offset_t doffset, flash_offset_t soffset, uint32_t n)
 Flash copy.
static mfs_error_t mfs_bank_erase (MFSDriver *mfsp, mfs_bank_t bank)
 Erases and verifies all sectors belonging to a bank.
static mfs_error_t mfs_bank_verify_erase (MFSDriver *mfsp, mfs_bank_t bank)
 Erases and verifies all sectors belonging to a bank.
static mfs_error_t mfs_bank_write_header (MFSDriver *mfsp, mfs_bank_t bank, uint32_t cnt)
 Writes the validation header in a bank.
static mfs_bank_state_t mfs_bank_check_header (MFSDriver *mfsp)
 Checks integrity of the header in the shared buffer.
static mfs_error_t mfs_bank_scan_records (MFSDriver *mfsp, mfs_bank_t bank, bool *wflagp)
 Scans blocks searching for records.
static mfs_error_t mfs_bank_get_state (MFSDriver *mfsp, mfs_bank_t bank, mfs_bank_state_t *statep, uint32_t *cntp)
 Determines the state of a bank.
static mfs_error_t mfs_garbage_collect (MFSDriver *mfsp)
 Enforces a garbage collection.
static mfs_error_t mfs_try_mount (MFSDriver *mfsp)
 Performs a flash partition mount attempt.
mfs_error_t mfs_mount (MFSDriver *mfsp)
 Configures and activates a MFS driver.
void mfsObjectInit (MFSDriver *mfsp, mfs_nocache_buffer_t *ncbuf)
 Initializes an instance.
mfs_error_t mfsStart (MFSDriver *mfsp, const MFSConfig *config)
 Configures and activates a MFS driver.
void mfsStop (MFSDriver *mfsp)
 Deactivates a MFS driver.
mfs_error_t mfsErase (MFSDriver *mfsp)
 Destroys the state of the managed storage by erasing the flash.
mfs_error_t mfsReadRecord (MFSDriver *mfsp, mfs_id_t id, size_t *np, uint8_t *buffer)
 Retrieves and reads a data record.
mfs_error_t mfsWriteRecord (MFSDriver *mfsp, mfs_id_t id, size_t n, const uint8_t *buffer)
 Creates or updates a data record.
mfs_error_t mfsEraseRecord (MFSDriver *mfsp, mfs_id_t id)
 Erases a data record.
mfs_error_t mfsPerformGarbageCollection (MFSDriver *mfsp)
 Enforces a garbage collection operation.
mfs_error_t mfsStartTransaction (MFSDriver *mfsp, size_t size)
 Puts the driver in transaction mode.
mfs_error_t mfsCommitTransaction (MFSDriver *mfsp)
 A transaction is committed and finalized atomically.
mfs_error_t mfsRollbackTransaction (MFSDriver *mfsp)
 A transaction is rolled back atomically.

Variables

static const uint16_t crc16_table [256]

Macro Definition Documentation

◆ ALIGNED_REC_SIZE

#define ALIGNED_REC_SIZE ( n)
Value:
uint32_t flash_offset_t
Type of a flash offset.
Definition hal_flash.h:112
#define MFS_ALIGN_NEXT(v)
Definition hal_mfs.h:423
Type of a data block header.
Definition hal_mfs.h:241

Data record size aligned.

Definition at line 50 of file hal_mfs.c.

Referenced by mfs_bank_scan_records(), mfs_garbage_collect(), mfs_try_mount(), mfsCommitTransaction(), mfsEraseRecord(), and mfsWriteRecord().

◆ ALIGNED_DHDR_SIZE

#define ALIGNED_DHDR_SIZE    ALIGNED_REC_SIZE(0)

Data record header size aligned.

Definition at line 56 of file hal_mfs.c.

Referenced by mfs_bank_scan_records(), mfsEraseRecord(), mfsStartTransaction(), and mfsWriteRecord().

◆ ALIGNED_SIZEOF

#define ALIGNED_SIZEOF ( t)
Value:
(((sizeof (t) - 1U) | MFS_ALIGN_MASK) + 1U)
#define MFS_ALIGN_MASK
Definition hal_mfs.h:420

Aligned size of a type.

Definition at line 62 of file hal_mfs.c.

Referenced by mfs_bank_scan_records(), mfs_garbage_collect(), and mfs_try_mount().

◆ PAIR

#define PAIR ( a,
b )
Value:
(((unsigned)(a) << 2U) | (unsigned)(b))

Combines two values (0..3) in one (0..15).

Definition at line 68 of file hal_mfs.c.

Referenced by mfs_try_mount().

◆ RET_ON_ERROR

#define RET_ON_ERROR ( err)
Value:
do { \
mfs_error_t e = (err); \
if (e != MFS_NO_ERROR) { \
return e; \
} \
} while (false)
mfs_error_t
Type of an MFS error code.
Definition hal_mfs.h:175
@ MFS_NO_ERROR
Definition hal_mfs.h:176

Error check helper.

Definition at line 73 of file hal_mfs.c.

Referenced by mfs_bank_get_state(), mfs_bank_scan_records(), mfs_flash_copy(), mfs_flash_write(), mfs_garbage_collect(), mfs_try_mount(), mfsCommitTransaction(), mfsErase(), mfsEraseRecord(), mfsReadRecord(), mfsStartTransaction(), and mfsWriteRecord().

◆ MFS_BANK_MAGIC_1

#define MFS_BANK_MAGIC_1   0xEC705ADEU

Definition at line 37 of file hal_mfs.h.

Referenced by mfs_bank_check_header(), and mfs_bank_write_header().

◆ MFS_BANK_MAGIC_2

#define MFS_BANK_MAGIC_2   0xF0339CC5U

Definition at line 38 of file hal_mfs.h.

Referenced by mfs_bank_check_header(), and mfs_bank_write_header().

◆ MFS_HEADER_MAGIC_1

#define MFS_HEADER_MAGIC_1   0x5FAE45F0U

◆ MFS_HEADER_MAGIC_2

#define MFS_HEADER_MAGIC_2   0xF045AE5FU

◆ MFS_CFG_MAX_RECORDS

#define MFS_CFG_MAX_RECORDS   32

Maximum number of indexed records in the managed storage.

Note
Record indexes go from 1 to MFS_CFG_MAX_RECORDS.

Definition at line 55 of file hal_mfs.h.

Referenced by mfs_bank_scan_records(), mfs_garbage_collect(), mfs_state_reset(), mfs_try_mount(), mfsEraseRecord(), mfsReadRecord(), and mfsWriteRecord().

◆ MFS_CFG_MAX_REPAIR_ATTEMPTS

#define MFS_CFG_MAX_REPAIR_ATTEMPTS   3

Maximum number of repair attempts on partition mount.

Definition at line 62 of file hal_mfs.h.

Referenced by mfs_mount().

◆ MFS_CFG_WRITE_VERIFY

#define MFS_CFG_WRITE_VERIFY   TRUE

Verify written data.

Definition at line 69 of file hal_mfs.h.

◆ MFS_CFG_STRONG_CHECKING

#define MFS_CFG_STRONG_CHECKING   TRUE

Enables a stronger and slower check procedure on mount.

Strong checking requires reading of the whole written data and this can be slow, normal checking only checks integrity of metadata, data errors would be detected on read.

Definition at line 79 of file hal_mfs.h.

◆ MFS_CFG_BUFFER_SIZE

#define MFS_CFG_BUFFER_SIZE   32

Size of the buffer used for data copying.

Note
The buffer size must be a power of two and not smaller than 16 bytes.
Larger buffers improve performance, buffers with size multiple of the flash program page size work better.

Definition at line 90 of file hal_mfs.h.

Referenced by mfs_bank_scan_records(), mfs_flash_copy(), and mfs_flash_write().

◆ MFS_CFG_MEMORY_ALIGNMENT

#define MFS_CFG_MEMORY_ALIGNMENT   2

Enforced memory alignment.

This value must be a power of two, it enforces a memory alignment for records in the flash array. This is required when alignment constraints exist, for example when using a DTR mode on OSPI devices.

Definition at line 101 of file hal_mfs.h.

◆ MFS_CFG_TRANSACTION_MAX

#define MFS_CFG_TRANSACTION_MAX   16

Maximum number of objects writable in a single transaction.

Definition at line 108 of file hal_mfs.h.

Referenced by mfsEraseRecord(), and mfsWriteRecord().

◆ MFS_IS_ERROR

#define MFS_IS_ERROR ( err)
Value:
((err) < MFS_NO_ERROR)

Definition at line 412 of file hal_mfs.h.

Referenced by mfs_mount().

◆ MFS_IS_WARNING

#define MFS_IS_WARNING ( err)
Value:
((err) > MFS_NO_ERROR)

Definition at line 413 of file hal_mfs.h.

◆ MFS_ALIGN_MASK

#define MFS_ALIGN_MASK   ((uint32_t)MFS_CFG_MEMORY_ALIGNMENT - 1U)

Definition at line 420 of file hal_mfs.h.

◆ MFS_IS_ALIGNED

#define MFS_IS_ALIGNED ( v)
Value:
(((uint32_t)(v) & MFS_ALIGN_MASK) == 0U)

Definition at line 421 of file hal_mfs.h.

◆ MFS_ALIGN_PREV

#define MFS_ALIGN_PREV ( v)
Value:
((uint32_t)(v) & ~MFS_ALIGN_MASK)

Definition at line 422 of file hal_mfs.h.

◆ MFS_ALIGN_NEXT

#define MFS_ALIGN_NEXT ( v)
Value:
(MFS_ALIGN_PREV(((uint32_t)(v) - 1U)) + \
#define MFS_ALIGN_PREV(v)
Definition hal_mfs.h:422
#define MFS_CFG_MEMORY_ALIGNMENT
Enforced memory alignment.
Definition hal_mfs.h:101

Definition at line 423 of file hal_mfs.h.

Referenced by mfsStartTransaction().

Typedef Documentation

◆ mfs_id_t

typedef uint32_t mfs_id_t

Type of a record identifier.

Definition at line 202 of file hal_mfs.h.

◆ mfs_nocache_buffer_t

Type of an non-cacheable MFS buffer.

Enumeration Type Documentation

◆ mfs_bank_t

enum mfs_bank_t

Type of a flash bank.

Enumerator
MFS_BANK_0 
MFS_BANK_1 

Definition at line 154 of file hal_mfs.h.

◆ mfs_state_t

Type of driver state machine states.

Enumerator
MFS_UNINIT 
MFS_STOP 
MFS_READY 
MFS_TRANSACTION 
MFS_ERROR 

Definition at line 162 of file hal_mfs.h.

◆ mfs_error_t

Type of an MFS error code.

Note
Errors are negative integers, informative warnings are positive integers.
Enumerator
MFS_NO_ERROR 
MFS_WARN_REPAIR 
MFS_WARN_GC 
MFS_ERR_INV_STATE 
MFS_ERR_INV_SIZE 
MFS_ERR_NOT_FOUND 
MFS_ERR_OUT_OF_MEM 
MFS_ERR_TRANSACTION_NUM 
MFS_ERR_TRANSACTION_SIZE 
MFS_ERR_NOT_ERASED 
MFS_ERR_FLASH_FAILURE 
MFS_ERR_INTERNAL 

Definition at line 175 of file hal_mfs.h.

◆ mfs_bank_state_t

Type of a bank state assessment.

Enumerator
MFS_BANK_ERASED 
MFS_BANK_OK 
MFS_BANK_GARBAGE 

Definition at line 193 of file hal_mfs.h.

Function Documentation

◆ crc16()

uint16_t crc16 ( uint16_t crc,
const uint8_t * data,
size_t n )

◆ mfs_state_reset()

◆ mfs_flash_get_bank_offset()

◆ mfs_flash_read()

mfs_error_t mfs_flash_read ( MFSDriver * mfsp,
flash_offset_t offset,
size_t n,
uint8_t * rp )
static

Flash read.

Parameters
[in]mfsppointer to the MFSDriver object
[in]offsetflash offset
[in]nnumber of bytes to be read
[out]rppointer to the data buffer
Returns
The operation status.
Function Class:
Not an API, this function is for internal use only.

Definition at line 172 of file hal_mfs.c.

References MFSDriver::config, FLASH_NO_ERROR, MFSConfig::flashp, flashRead, MFS_ERR_FLASH_FAILURE, MFS_ERROR, MFS_NO_ERROR, and MFSDriver::state.

Referenced by mfs_bank_get_state(), mfs_bank_scan_records(), mfs_flash_copy(), mfs_flash_write(), mfs_try_mount(), and mfsReadRecord().

◆ mfs_flash_write()

mfs_error_t mfs_flash_write ( MFSDriver * mfsp,
flash_offset_t offset,
size_t n,
const uint8_t * wp )
static

Flash write.

Note
If the option MFS_CFG_WRITE_VERIFY is enabled then the flash is also read back for verification.
Parameters
[in]mfsppointer to the MFSDriver object
[in]offsetflash offset
[in]nnumber of bytes to be written
[in]wppointer to the data buffer
Returns
The operation status.
Function Class:
Not an API, this function is for internal use only.

Definition at line 198 of file hal_mfs.c.

References MFSDriver::config, mfs_nocache_buffer::data8, FLASH_NO_ERROR, MFSConfig::flashp, flashProgram, MFS_CFG_BUFFER_SIZE, MFS_ERR_FLASH_FAILURE, MFS_ERROR, mfs_flash_read(), MFS_NO_ERROR, MFSDriver::ncbuf, RET_ON_ERROR, and MFSDriver::state.

Referenced by mfs_bank_write_header(), mfs_flash_copy(), mfsCommitTransaction(), mfsEraseRecord(), and mfsWriteRecord().

Here is the call graph for this function:

◆ mfs_flash_copy()

mfs_error_t mfs_flash_copy ( MFSDriver * mfsp,
flash_offset_t doffset,
flash_offset_t soffset,
uint32_t n )
static

Flash copy.

Note
If the option MFS_CFG_WRITE_VERIFY is enabled then the flash is also read back for verification.
Parameters
[in]mfsppointer to the MFSDriver object
[in]doffsetdestination flash offset
[in]soffsetsource flash offset
[in]nnumber of bytes to be copied
Returns
The operation status.
Function Class:
Not an API, this function is for internal use only.

Definition at line 243 of file hal_mfs.c.

References mfs_nocache_buffer::data8, MFS_CFG_BUFFER_SIZE, mfs_flash_read(), mfs_flash_write(), MFS_NO_ERROR, MFSDriver::ncbuf, and RET_ON_ERROR.

Referenced by mfs_garbage_collect().

Here is the call graph for this function:

◆ mfs_bank_erase()

mfs_error_t mfs_bank_erase ( MFSDriver * mfsp,
mfs_bank_t bank )
static

Erases and verifies all sectors belonging to a bank.

Parameters
[in]mfsppointer to the MFSDriver object
[in]bankbank to be erased
Returns
The operation status.
Function Class:
Not an API, this function is for internal use only.

Definition at line 279 of file hal_mfs.c.

References MFSConfig::bank0_sectors, MFSConfig::bank0_start, MFSConfig::bank1_sectors, MFSConfig::bank1_start, MFSDriver::config, FLASH_NO_ERROR, MFSConfig::flashp, flashStartEraseSector, flashVerifyErase, flashWaitErase(), MFS_BANK_0, MFS_ERR_FLASH_FAILURE, MFS_ERROR, MFS_NO_ERROR, and MFSDriver::state.

Referenced by mfs_garbage_collect(), mfs_try_mount(), and mfsErase().

Here is the call graph for this function:

◆ mfs_bank_verify_erase()

mfs_error_t mfs_bank_verify_erase ( MFSDriver * mfsp,
mfs_bank_t bank )
static

Erases and verifies all sectors belonging to a bank.

Parameters
[in]mfsppointer to the MFSDriver object
[in]bankbank to be verified
Returns
The operation status.
Function Class:
Not an API, this function is for internal use only.

Definition at line 325 of file hal_mfs.c.

References MFSConfig::bank0_sectors, MFSConfig::bank0_start, MFSConfig::bank1_sectors, MFSConfig::bank1_start, MFSDriver::config, FLASH_ERROR_VERIFY, FLASH_NO_ERROR, MFSConfig::flashp, flashVerifyErase, MFS_BANK_0, MFS_ERR_FLASH_FAILURE, MFS_ERR_NOT_ERASED, MFS_ERROR, MFS_NO_ERROR, and MFSDriver::state.

Referenced by mfs_bank_get_state().

◆ mfs_bank_write_header()

mfs_error_t mfs_bank_write_header ( MFSDriver * mfsp,
mfs_bank_t bank,
uint32_t cnt )
static

Writes the validation header in a bank.

Parameters
[in]mfsppointer to the MFSDriver object
[in]bankbank to be validated
[in]cntvalue for the flash usage counter
Returns
The operation status.
Function Class:
Not an API, this function is for internal use only.

Definition at line 365 of file hal_mfs.c.

References MFSConfig::bank0_start, MFSConfig::bank1_start, mfs_nocache_buffer::bhdr, MFSDriver::config, mfs_bank_header_t::counter, mfs_bank_header_t::crc, crc16(), MFSConfig::erased, mfs_bank_header_t::fields, flashGetSectorOffset(), MFSConfig::flashp, mfs_bank_header_t::hdr8, mfs_bank_header_t::magic1, mfs_bank_header_t::magic2, MFS_BANK_0, MFS_BANK_MAGIC_1, MFS_BANK_MAGIC_2, mfs_flash_write(), MFSDriver::ncbuf, and mfs_bank_header_t::reserved1.

Referenced by mfs_garbage_collect(), and mfs_try_mount().

Here is the call graph for this function:

◆ mfs_bank_check_header()

mfs_bank_state_t mfs_bank_check_header ( MFSDriver * mfsp)
static

Checks integrity of the header in the shared buffer.

Parameters
[in]mfsppointer to the MFSDriver object
Returns
The header state.
Function Class:
Not an API, this function is for internal use only.

Definition at line 399 of file hal_mfs.c.

References mfs_nocache_buffer::bhdr, MFSDriver::config, mfs_bank_header_t::counter, mfs_bank_header_t::crc, crc16(), MFSConfig::erased, mfs_bank_header_t::fields, mfs_bank_header_t::hdr32, mfs_bank_header_t::hdr8, mfs_bank_header_t::magic1, mfs_bank_header_t::magic2, MFS_BANK_ERASED, MFS_BANK_GARBAGE, MFS_BANK_MAGIC_1, MFS_BANK_MAGIC_2, MFS_BANK_OK, MFSDriver::ncbuf, and mfs_bank_header_t::reserved1.

Referenced by mfs_bank_get_state(), and mfs_try_mount().

Here is the call graph for this function:

◆ mfs_bank_scan_records()

mfs_error_t mfs_bank_scan_records ( MFSDriver * mfsp,
mfs_bank_t bank,
bool * wflagp )
static

Scans blocks searching for records.

Note
The block integrity is strongly checked.
Parameters
[in]mfsppointer to the MFSDriver object
[in]bankthe bank identifier
[out]wflagpwarning flag on anomalies
Returns
The operation status.
Function Class:
Not an API, this function is for internal use only.

Definition at line 439 of file hal_mfs.c.

References ALIGNED_DHDR_SIZE, ALIGNED_REC_SIZE, ALIGNED_SIZEOF, MFSConfig::bank_size, MFSDriver::config, mfs_data_header_t::crc, crc16(), mfs_nocache_buffer::data32, mfs_nocache_buffer::data8, MFSDriver::descriptors, mfs_nocache_buffer::dhdr, MFSConfig::erased, mfs_data_header_t::fields, mfs_data_header_t::id, mfs_data_header_t::magic1, mfs_data_header_t::magic2, MFS_CFG_BUFFER_SIZE, MFS_CFG_MAX_RECORDS, mfs_flash_get_bank_offset(), mfs_flash_read(), MFS_HEADER_MAGIC_1, MFS_HEADER_MAGIC_2, MFS_NO_ERROR, MFSDriver::ncbuf, MFSDriver::next_offset, mfs_record_descriptor_t::offset, RET_ON_ERROR, mfs_data_header_t::size, and mfs_record_descriptor_t::size.

Referenced by mfs_try_mount().

Here is the call graph for this function:

◆ mfs_bank_get_state()

mfs_error_t mfs_bank_get_state ( MFSDriver * mfsp,
mfs_bank_t bank,
mfs_bank_state_t * statep,
uint32_t * cntp )
static

Determines the state of a bank.

Note
This function does not test the bank integrity by scanning the data area, it just checks the header.
Parameters
[in]mfsppointer to the MFSDriver object
[in]bankbank to be checked
[out]statepbank state, it can be:
  • MFS_BANK_ERASED
  • MFS_BANK_GARBAGE
  • MFS_BANK_OK
[out]cntpbank counter
Returns
The operation status.
Function Class:
Not an API, this function is for internal use only.

Definition at line 548 of file hal_mfs.c.

References mfs_nocache_buffer::bhdr, mfs_bank_header_t::counter, mfs_nocache_buffer::data8, mfs_bank_header_t::fields, mfs_bank_check_header(), MFS_BANK_ERASED, MFS_BANK_GARBAGE, mfs_bank_verify_erase(), MFS_ERR_NOT_ERASED, mfs_flash_get_bank_offset(), mfs_flash_read(), MFS_NO_ERROR, MFSDriver::ncbuf, and RET_ON_ERROR.

Referenced by mfs_try_mount().

Here is the call graph for this function:

◆ mfs_garbage_collect()

mfs_error_t mfs_garbage_collect ( MFSDriver * mfsp)
static

Enforces a garbage collection.

Storage data is compacted into a single bank.

Parameters
[out]mfsppointer to the MFSDriver object
Returns
The operation status.
Function Class:
Not an API, this function is for internal use only.

Definition at line 586 of file hal_mfs.c.

References ALIGNED_REC_SIZE, ALIGNED_SIZEOF, MFSDriver::current_bank, MFSDriver::current_counter, MFSDriver::descriptors, MFS_BANK_0, MFS_BANK_1, mfs_bank_erase(), mfs_bank_write_header(), MFS_CFG_MAX_RECORDS, mfs_flash_copy(), mfs_flash_get_bank_offset(), MFS_NO_ERROR, MFSDriver::next_offset, mfs_record_descriptor_t::offset, RET_ON_ERROR, and mfs_record_descriptor_t::size.

Referenced by mfs_try_mount(), mfsEraseRecord(), mfsPerformGarbageCollection(), mfsRollbackTransaction(), mfsStartTransaction(), and mfsWriteRecord().

Here is the call graph for this function:

◆ mfs_try_mount()

◆ mfs_mount()

mfs_error_t mfs_mount ( MFSDriver * mfsp)

Configures and activates a MFS driver.

Parameters
[in]mfsppointer to the MFSDriver object
Returns
The operation status.
Return values
MFS_NO_ERRORif the operation has been successfully completed.
MFS_WARN_GCif the operation triggered a garbage collection.
MFS_ERR_FLASH_FAILUREif the flash memory is unusable because HW failures. Makes the driver enter the MFS_ERROR state.
MFS_ERR_INTERNALif an internal logic failure is detected.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 784 of file hal_mfs.c.

References MFS_CFG_MAX_REPAIR_ATTEMPTS, MFS_ERR_FLASH_FAILURE, MFS_ERR_INTERNAL, MFS_ERROR, MFS_IS_ERROR, MFS_READY, mfs_state_reset(), mfs_try_mount(), and MFSDriver::state.

Referenced by mfsErase(), and mfsStart().

Here is the call graph for this function:

◆ mfsObjectInit()

void mfsObjectInit ( MFSDriver * mfsp,
mfs_nocache_buffer_t * ncbuf )

Initializes an instance.

Parameters
[out]mfsppointer to the MFSDriver object
[in]ncbufpointer to an mfs_nocache_buffer_t buffer structure
Function Class:
Object or module nitializer function.

Definition at line 824 of file hal_mfs.c.

References MFSDriver::config, MFS_STOP, MFSDriver::ncbuf, osalDbgCheck, and MFSDriver::state.

◆ mfsStart()

mfs_error_t mfsStart ( MFSDriver * mfsp,
const MFSConfig * config )

Configures and activates a MFS driver.

Parameters
[in]mfsppointer to the MFSDriver object
[in]configpointer to the configuration
Returns
The operation status.
Return values
MFS_NO_ERRORif the operation has been completed.
MFS_WARN_GCif the operation triggered a garbage collection.
MFS_ERR_FLASH_FAILUREif the flash memory is unusable because HW failures. Makes the driver enter the MFS_ERROR state.
MFS_ERR_INTERNALif an internal logic failure is detected.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 850 of file hal_mfs.c.

References MFSDriver::config, MFS_ERROR, mfs_mount(), MFS_READY, MFS_STOP, osalDbgAssert, osalDbgCheck, and MFSDriver::state.

Here is the call graph for this function:

◆ mfsStop()

void mfsStop ( MFSDriver * mfsp)

Deactivates a MFS driver.

Parameters
[in]mfsppointer to the MFSDriver object
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 869 of file hal_mfs.c.

References MFSDriver::config, MFS_ERROR, MFS_READY, MFS_STOP, osalDbgAssert, osalDbgCheck, and MFSDriver::state.

◆ mfsErase()

mfs_error_t mfsErase ( MFSDriver * mfsp)

Destroys the state of the managed storage by erasing the flash.

Parameters
[in]mfsppointer to the MFSDriver object
Returns
The operation status.
Return values
MFS_ERR_INV_STATEif the driver is in not in MFS_READY state.
MFS_NO_ERRORif the operation has been successfully completed.
MFS_ERR_FLASH_FAILUREif the flash memory is unusable because HW failures. Makes the driver enter the MFS_ERROR state.
MFS_ERR_INTERNALif an internal logic failure is detected.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 895 of file hal_mfs.c.

References MFS_BANK_0, MFS_BANK_1, mfs_bank_erase(), MFS_ERR_INV_STATE, mfs_mount(), MFS_READY, osalDbgCheck, RET_ON_ERROR, and MFSDriver::state.

Here is the call graph for this function:

◆ mfsReadRecord()

mfs_error_t mfsReadRecord ( MFSDriver * mfsp,
mfs_id_t id,
size_t * np,
uint8_t * buffer )

Retrieves and reads a data record.

Parameters
[in]mfsppointer to the MFSDriver object
[in]idrecord numeric identifier, the valid range is between 1 and MFS_CFG_MAX_RECORDS
[in,out]npon input is the maximum buffer size, on return it is the size of the data copied into the buffer
[out]bufferpointer to a buffer for record data
Returns
The operation status.
Return values
MFS_NO_ERRORif the operation has been successfully completed.
MFS_ERR_INV_STATEif the driver is in not in MFS_READY state.
MFS_ERR_INV_SIZEif the passed buffer is not large enough to contain the record data.
MFS_ERR_NOT_FOUNDif the specified id does not exists.
MFS_ERR_FLASH_FAILUREif the flash memory is unusable because HW failures. Makes the driver enter the MFS_ERROR state.
MFS_ERR_INTERNALif an internal logic failure is detected.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 933 of file hal_mfs.c.

References mfs_data_header_t::crc, crc16(), mfs_nocache_buffer::data8, MFSDriver::descriptors, mfs_nocache_buffer::dhdr, mfs_data_header_t::fields, MFS_CFG_MAX_RECORDS, MFS_ERR_FLASH_FAILURE, MFS_ERR_INV_SIZE, MFS_ERR_INV_STATE, MFS_ERR_NOT_FOUND, MFS_ERROR, mfs_flash_read(), MFS_NO_ERROR, MFS_READY, MFS_TRANSACTION, MFSDriver::ncbuf, mfs_record_descriptor_t::offset, osalDbgCheck, RET_ON_ERROR, mfs_record_descriptor_t::size, and MFSDriver::state.

Here is the call graph for this function:

◆ mfsWriteRecord()

mfs_error_t mfsWriteRecord ( MFSDriver * mfsp,
mfs_id_t id,
size_t n,
const uint8_t * buffer )

Creates or updates a data record.

Parameters
[in]mfsppointer to the MFSDriver object
[in]idrecord numeric identifier, the valid range is between 1 and MFS_CFG_MAX_RECORDS
[in]nsize of data to be written, it cannot be zero
[in]bufferpointer to a buffer for record data
Returns
The operation status.
Return values
MFS_NO_ERRORif the operation has been successfully completed.
MFS_WARN_GCif the operation triggered a garbage collection.
MFS_ERR_INV_STATEif the driver is in not in MFS_READY state.
MFS_ERR_OUT_OF_MEMif there is not enough flash space for the operation.
MFS_ERR_TRANSACTION_NUMif the transaction operations buffer space has been exceeded.
MFS_ERR_TRANSACTION_SIZEif the transaction allocated space has been exceeded.
MFS_ERR_FLASH_FAILUREif the flash memory is unusable because HW failures. Makes the driver enter the MFS_ERROR state.
MFS_ERR_INTERNALif an internal logic failure is detected.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 1006 of file hal_mfs.c.

References ALIGNED_DHDR_SIZE, ALIGNED_REC_SIZE, MFSConfig::bank_size, MFSDriver::config, mfs_data_header_t::crc, crc16(), MFSDriver::current_bank, mfs_nocache_buffer::data8, MFSDriver::descriptors, mfs_nocache_buffer::dhdr, mfs_data_header_t::fields, mfs_data_header_t::id, mfs_transaction_op_t::id, mfs_data_header_t::magic1, mfs_data_header_t::magic2, MFS_CFG_MAX_RECORDS, MFS_CFG_TRANSACTION_MAX, MFS_ERR_INV_STATE, MFS_ERR_OUT_OF_MEM, MFS_ERR_TRANSACTION_NUM, MFS_ERR_TRANSACTION_SIZE, mfs_flash_get_bank_offset(), mfs_flash_write(), mfs_garbage_collect(), MFS_HEADER_MAGIC_1, MFS_HEADER_MAGIC_2, MFS_NO_ERROR, MFS_READY, MFS_TRANSACTION, MFS_WARN_GC, MFSDriver::ncbuf, MFSDriver::next_offset, mfs_record_descriptor_t::offset, mfs_transaction_op_t::offset, osalDbgCheck, RET_ON_ERROR, mfs_data_header_t::size, mfs_record_descriptor_t::size, mfs_transaction_op_t::size, MFSDriver::state, MFSDriver::tr_limit_offet, MFSDriver::tr_next_offset, MFSDriver::tr_nops, MFSDriver::tr_ops, and MFSDriver::used_space.

Here is the call graph for this function:

◆ mfsEraseRecord()

mfs_error_t mfsEraseRecord ( MFSDriver * mfsp,
mfs_id_t id )

Erases a data record.

Parameters
[in]mfsppointer to the MFSDriver object
[in]idrecord numeric identifier, the valid range is between 1 and MFS_CFG_MAX_RECORDS
Returns
The operation status.
Return values
MFS_NO_ERRORif the operation has been successfully completed.
MFS_WARN_GCif the operation triggered a garbage collection.
MFS_ERR_INV_STATEif the driver is in not in MFS_READY state.
MFS_ERR_OUT_OF_MEMif there is not enough flash space for the operation.
MFS_ERR_TRANSACTION_NUMif the transaction operations buffer space has been exceeded.
MFS_ERR_TRANSACTION_SIZEif the transaction allocated space has been exceeded.
MFS_ERR_FLASH_FAILUREif the flash memory is unusable because HW failures. Makes the driver enter the MFS_ERROR state.
MFS_ERR_INTERNALif an internal logic failure is detected.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 1155 of file hal_mfs.c.

References ALIGNED_DHDR_SIZE, ALIGNED_REC_SIZE, MFSConfig::bank_size, MFSDriver::config, mfs_data_header_t::crc, MFSDriver::current_bank, mfs_nocache_buffer::data8, MFSDriver::descriptors, mfs_nocache_buffer::dhdr, mfs_data_header_t::fields, mfs_data_header_t::id, mfs_transaction_op_t::id, mfs_data_header_t::magic1, mfs_data_header_t::magic2, MFS_CFG_MAX_RECORDS, MFS_CFG_TRANSACTION_MAX, MFS_ERR_INTERNAL, MFS_ERR_INV_STATE, MFS_ERR_NOT_FOUND, MFS_ERR_TRANSACTION_NUM, MFS_ERR_TRANSACTION_SIZE, mfs_flash_get_bank_offset(), mfs_flash_write(), mfs_garbage_collect(), MFS_HEADER_MAGIC_1, MFS_HEADER_MAGIC_2, MFS_NO_ERROR, MFS_READY, MFS_TRANSACTION, MFS_WARN_GC, MFSDriver::ncbuf, MFSDriver::next_offset, mfs_record_descriptor_t::offset, mfs_transaction_op_t::offset, osalDbgCheck, RET_ON_ERROR, mfs_data_header_t::size, mfs_record_descriptor_t::size, mfs_transaction_op_t::size, MFSDriver::state, MFSDriver::tr_limit_offet, MFSDriver::tr_next_offset, MFSDriver::tr_nops, MFSDriver::tr_ops, and MFSDriver::used_space.

Here is the call graph for this function:

◆ mfsPerformGarbageCollection()

mfs_error_t mfsPerformGarbageCollection ( MFSDriver * mfsp)

Enforces a garbage collection operation.

Garbage collection involves: integrity check, optionally repairs, obsolete data removal, data compaction and a flash bank swap.

Parameters
[in]mfsppointer to the MFSDriver object
Returns
The operation status.
Return values
MFS_NO_ERRORif the operation has been successfully completed.
MFS_ERR_INV_STATEif the driver is in not in MFS_READY state.
MFS_ERR_FLASH_FAILUREif the flash memory is unusable because HW failures. Makes the driver enter the MFS_ERROR state.
MFS_ERR_INTERNALif an internal logic failure is detected.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 1279 of file hal_mfs.c.

References MFS_ERR_INV_STATE, mfs_garbage_collect(), MFS_READY, osalDbgCheck, and MFSDriver::state.

Here is the call graph for this function:

◆ mfsStartTransaction()

mfs_error_t mfsStartTransaction ( MFSDriver * mfsp,
size_t size )

Puts the driver in transaction mode.

Note
The parameters n and size are used to make an estimation of the space required for the transaction to succeed. Note that the estimated size must include also the extra space required by alignment enforcement option. If the estimated size is wrong (by defect) what could happen is that there is a failure in the middle of a transaction and a roll-back would be required.
The conditions for starting a transaction are:
  • The driver must be started.
  • There must be enough compacted storage to accommodate the whole transaction. If the required space is available but it is not compacted then a garbage collect operation is performed.
Parameters
[in]mfsppointer to the MFSDriver object
[in]sizeestimated total size of written records in transaction, this includes, data, headers and alignment gaps
Returns
The operation status.
Return values
MFS_NO_ERRORif the operation has been successfully completed.
MFS_ERR_INV_STATEif the driver is in not in MFS_READY state.
MFS_ERR_FLASH_FAILUREif the flash memory is unusable because HW failures. Makes the driver enter the MFS_ERROR state.
MFS_ERR_INTERNALif an internal logic failure is detected.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 1320 of file hal_mfs.c.

References ALIGNED_DHDR_SIZE, MFSConfig::bank_size, MFSDriver::config, MFSDriver::current_bank, MFS_ALIGN_NEXT, MFS_ERR_INV_STATE, MFS_ERR_OUT_OF_MEM, mfs_flash_get_bank_offset(), mfs_garbage_collect(), MFS_NO_ERROR, MFS_READY, MFS_TRANSACTION, MFSDriver::next_offset, osalDbgCheck, RET_ON_ERROR, MFSDriver::state, MFSDriver::tr_limit_offet, MFSDriver::tr_next_offset, MFSDriver::tr_nops, and MFSDriver::used_space.

Here is the call graph for this function:

◆ mfsCommitTransaction()

mfs_error_t mfsCommitTransaction ( MFSDriver * mfsp)

A transaction is committed and finalized atomically.

Parameters
[in]mfsppointer to the MFSDriver object
Returns
The operation status.
Return values
MFS_NO_ERRORif the operation has been successfully completed.
MFS_ERR_INV_STATEif the driver is in not in MFS_TRANSACTION state.
MFS_ERR_FLASH_FAILUREif the flash memory is unusable because HW failures. Makes the driver enter the MFS_ERROR state.
MFS_ERR_INTERNALif an internal logic failure is detected.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 1376 of file hal_mfs.c.

References ALIGNED_REC_SIZE, mfs_nocache_buffer::data8, MFSDriver::descriptors, mfs_nocache_buffer::dhdr, mfs_data_header_t::fields, mfs_transaction_op_t::id, mfs_data_header_t::magic1, mfs_data_header_t::magic2, MFS_ERR_INV_STATE, mfs_flash_write(), MFS_HEADER_MAGIC_1, MFS_HEADER_MAGIC_2, MFS_NO_ERROR, MFS_READY, MFS_TRANSACTION, MFSDriver::ncbuf, MFSDriver::next_offset, mfs_record_descriptor_t::offset, mfs_transaction_op_t::offset, osalDbgCheck, RET_ON_ERROR, mfs_record_descriptor_t::size, mfs_transaction_op_t::size, MFSDriver::state, MFSDriver::tr_next_offset, MFSDriver::tr_nops, MFSDriver::tr_ops, and MFSDriver::used_space.

Here is the call graph for this function:

◆ mfsRollbackTransaction()

mfs_error_t mfsRollbackTransaction ( MFSDriver * mfsp)

A transaction is rolled back atomically.

This function performs a garbage collection in order to discard all written data that has not been finalized.

Parameters
[in]mfsppointer to the MFSDriver object
Returns
The operation status.
Return values
MFS_NO_ERRORif the operation has been successfully completed.
MFS_ERR_INV_STATEif the driver is in not in MFS_TRANSACTION state.
MFS_ERR_FLASH_FAILUREif the flash memory is unusable because HW failures. Makes the driver enter the MFS_ERROR state.
MFS_ERR_INTERNALif an internal logic failure is detected.
Function Class:
Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 1457 of file hal_mfs.c.

References MFS_ERR_INV_STATE, mfs_garbage_collect(), MFS_NO_ERROR, MFS_READY, MFS_TRANSACTION, osalDbgCheck, MFSDriver::state, and MFSDriver::tr_nops.

Here is the call graph for this function:

Variable Documentation

◆ crc16_table

const uint16_t crc16_table[256]
static

Definition at line 88 of file hal_mfs.c.

Referenced by crc16().