Skip to content

File syn_param.h

FileList > src > syntropic > storage > syn_param.h

Go to the source code of this file

Persistent parameter store with wear leveling. More...

  • #include "../common/syn_defs.h"
  • #include "../port/syn_port_flash.h"
  • #include <stdbool.h>
  • #include <stddef.h>
  • #include <stdint.h>

Classes

Type Name
struct SYN_ParamSlotHeader
Slot header — stored at the beginning of each parameter slot.
struct SYN_ParamStore
Wear-leveled flash parameter store instance.

Public Functions

Type Name
SYN_Status syn_param_erase_all (SYN_ParamStore * store)
Erase all parameter data (factory reset).
SYN_Status syn_param_init (SYN_ParamStore * store, uint32_t flash_base, uint8_t sector_count, uint16_t data_size)
Initialize the parameter store.
SYN_Status syn_param_load (const SYN_ParamStore * store, void * data)
Load parameters from the latest valid slot.
SYN_Status syn_param_save (SYN_ParamStore * store, const void * data)
Save parameters to the next slot (wear-leveled).

Public Static Functions

Type Name
uint16_t syn_param_write_count (const SYN_ParamStore * store)
Get the current write count (approximate wear indicator).

Macros

Type Name
define SYN_PARAM_MAGIC 0xC0DEu
Magic number for parameter slot headers.

Detailed Description

Stores key-value parameters in flash with: * Wear leveling: Slot rotation across flash sectors * Integrity: CRC-16 on each slot * Atomic writes: Sequence numbers to identify the latest valid slot * No dynamic allocation: Fixed-size parameter block, caller-owned RAM

** **

The flash region is divided into sectors. Each sector contains N slots. Each slot stores the full parameter block + header (sequence, CRC). Writes go to the next free slot. When a sector is full, the next sector is erased and writing continues there.

Sector 0:  [slot0][slot1][slot2]...[slotN]
Sector 1:  [slot0][slot1]...
Sector 2:  ...

The highest sequence number with a valid CRC is the active slot.

** **

// Define your parameters as a struct
typedef struct {
    uint16_t brightness;
    int16_t  offset;
    uint8_t  mode;
} MyParams;

static SYN_ParamStore store;
static MyParams params;

syn_param_init(&store, FLASH_PARAM_START, 2, sizeof(MyParams));
syn_param_load(&store, &params);   // loads latest valid or defaults

params.brightness = 80;
syn_param_save(&store, &params);   // writes to next slot

Public Functions Documentation

function syn_param_erase_all

Erase all parameter data (factory reset).

SYN_Status syn_param_erase_all (
    SYN_ParamStore * store
) 

Parameters:

  • store Store instance.

Returns:

SYN_OK on success.


function syn_param_init

Initialize the parameter store.

SYN_Status syn_param_init (
    SYN_ParamStore * store,
    uint32_t flash_base,
    uint8_t sector_count,
    uint16_t data_size
) 

Scans flash to find the latest valid slot. Must be called before load or save.

Parameters:

  • store Store instance.
  • flash_base Start address of the flash region for params.
  • sector_count Number of flash sectors to use for wear leveling.
  • data_size Size of the parameter struct in bytes.

Returns:

SYN_OK if at least one valid slot was found, SYN_ERROR if flash is blank (use defaults).


function syn_param_load

Load parameters from the latest valid slot.

SYN_Status syn_param_load (
    const SYN_ParamStore * store,
    void * data
) 

Parameters:

  • store Initialized store.
  • data Buffer to receive the parameter data (must be data_size bytes).

Returns:

SYN_OK on success, SYN_ERROR if no valid data found.


function syn_param_save

Save parameters to the next slot (wear-leveled).

SYN_Status syn_param_save (
    SYN_ParamStore * store,
    const void * data
) 

Writes to the next free slot, rotating across sectors. When a sector fills up, the next sector is erased and writing continues there.

Parameters:

  • store Initialized store.
  • data Parameter data to save (must be data_size bytes).

Returns:

SYN_OK on success, SYN_ERROR on flash write failure.


Public Static Functions Documentation

function syn_param_write_count

Get the current write count (approximate wear indicator).

static inline uint16_t syn_param_write_count (
    const SYN_ParamStore * store
) 

Parameters:

  • store Store instance.

Returns:

The sequence number, which increments with each save.


Macro Definition Documentation

define SYN_PARAM_MAGIC

Magic number for parameter slot headers.

#define SYN_PARAM_MAGIC `0xC0DEu`



The documentation for this class was generated from the following file src/syntropic/storage/syn_param.h