#ifndef HART_FLASH_H
#define HART_FLASH_H

#include "../common.h"
#include "hart_settings.h"

//=========================== define ==========================================

#define HART_FLASH_ADDR (0x0007E000u)

//=========================== typedef =======================================
//==== burst settings struct
typedef struct
{
    uint8_t  mode;
    uint8_t  slot[8];
    uint16_t cmdNum;
    uint32_t updatePeriod;    // in 1/32 s
    uint32_t maxUpdatePeriod; // in 1/32 s
    uint8_t  triggerMode;
    float    triggerLevel;
    uint8_t  triggerClass;
    uint8_t  triggerUnit;
} burstSettings_t;

//==== event settings struct
typedef struct
{
    uint8_t  mode;
    uint8_t  status;
    uint8_t  statusMask;
    uint32_t pendingEventTime;
    uint32_t retryPeriod;
    uint32_t maxUpdatePeriod;
    uint32_t debouncePeriod;
    //uint8_t[25] eventMask;   // this is not used
} eventSettings_t;

//==== configuration changed counter struct
typedef struct
{
    uint16_t value;    // value of configuration changed counter
    uint8_t  flags[4]; // 4 flags for 4 masters (2wired+2wireless)
} cfgCounter_t;

//==== structure of non-volatile data saved in flash at HART_FLASH_ADDR
typedef struct
{
    cfgCounter_t     cfgCounter;                // configuration changed counter settings
    burstSettings_t  burst[HART_BURST_NUM];     // bursts settings
    eventSettings_t  event[HART_EVENT_NUM];     // events settings
    uint8_t          longTag[32];               // ISO latin characters
    uint8_t          shortTag[6];               // 8x  packed char (accorsding to HART specs)
    uint8_t          descriptor[12];            // 16x packed char (accorsding to HART specs)
    uint8_t          date[3];                   // {day, month, year-1990}
    uint8_t          message[24];               // 32x packed char (accorsding to HART specs)
    uint8_t          pollingAddr;               // used with CMD0, defaults to zero
    uint32_t         finalAssemblyNum;          // this is only 24 bit wide!!!!
} hart_flash_memImage_t;

//=========================== prototypes ======================================

void hart_flash_write(const void* address, const void* data, uint8_t dataLen, bool incCfgChangeCnt);

#endif
