/*
 * init_defaults.c
 *
 *  Created on: May 14, 2025
 *      Author: tm
 */


#include "main.h"
#include "system.h"
#include "config.h"

#include "user_io.h"
#include "ads1248_iso.h"
#include "ads1248_noniso.h"
#include "i2c_fram.h"

#include "board_util.h"

#include "ctd_base.h"
#include "optode.h"

#include "sleep.h"
#include "sample.h"
#include "microsd.h"
#include "uartstdio.h"
#include "test.h"
#include "ymodem.h"

#include "apf.h"
#include "apf_init.h"
#include "apf_util.h"
#include "serial_cmd_fxn.h"
#include "init_defaults.h"

extern int gBootError;  //defd in main

//if a problem was found (non-vol eeprom or fram is erased or 'corrupted' by struct change,
//this function is called so that the operator (human or machine) can detect the problem and run defaults
void apf_min_defaults_to_run(void)
{
    sys_data.debug_flag = 0;
    sys_samp.state = IDLE;
    sys_data.app_cfg[APPCFG_EXEC_SWITCHERONI] = EXEC_DEPLOY;
    sys_data.app_cfg[APPCFG_MICROSD_ENABLE] = 1;
    sys_data.app_cfg[APPCFG_SAMPLING_DIAG] = 0;
    sys_data.app_cfg[APPCFG_MENU_LEVEL] = 0;


    sys_data.msc_op = MSC1_OP;
    sys_data.echo_flag = 0;
    sys_data.terminal_mode = M2M_MODE;
    sys_data.autoSleepSec = 30;             //typically 10 for M2M_MODE, give operator time to deal

    sys_data.app_cfg[APPCFG_SAMPLING_DIAG] = 0;
    sys_data.app_cfg[APPCFG_MENU_LEVEL] = 0;

    if(sys_data.baud_rate != 9600)
    {
        sys_data.baud_rate = 9600L;
        UARTStdioConfig(0, 9600, 16000000);       // 9600 bits per second
        uprintf("Baud rate is changed to default: 9600\r\n");
        wait_consoleTx();
    }
}

void fram_apf_var_defaults(void)
{
    apf_fram.initNumBeg = BYTE_SIGNATURE;              //byte signature

    apf_fram.sendlast_byteCnt = 0;        //
    apf_fram.isus_error_ctr = 0;          //
    apf_fram.isus_last_error = 0;         // Added 14 May
    apf_fram.isus_error_time = 0;         //, maybe 1
    apf_fram.isus_sample_time = 0;
    apf_fram.isus_reset_flag = TRUE;         // used for writing header to PROF file (can be eliminated)
    apf_fram.isus_lampOn_ctr = 0;
    apf_fram.isus_lampOn_mSec = 0;        // formerly odometer
    apf_fram.isus_sample_ctr = 0;         //

    apf_fram.dura_error_time = 0;         //
    apf_fram.dura_sample_time = 0;        //
    apf_fram.dura_error_ctr = 0;          //
    apf_fram.dura_last_error = 0;         // Added 14 May
    apf_fram.dura_reset_flag = TRUE;         //   // flag to mark initialization for outputting header info
    apf_fram.dura_sample_ctr = 0;         //

    apf_fram.sys_por_flag = 0;            //  NOT NEEDED  Power on reset ==
    apf_fram.sys_sensor_mode = 0;         //   ISUS_SENSOR_MODE or DURA_SENSOR_MODE  MODE,1 or MODE,2   (MODE,3 in sample databarf)
    apf_fram.sys_wdog_ctr = 0;             //  NOT NEEDED??

    apf_fram.profileCnt = 0;              // or 2   incremented by PROFILE command, zeroed by INIT command
    apf_fram.profileStartTime = read_hib_rtcc();

    apf_fram.sys_reset_ctr = 0;           //
    apf_fram.sys_reset_time = read_hib_rtcc();
    apf_fram.sys_data_mode = SYS_DATAMODE_VAL;      //deprecated.  used for takeIsusSample vs takeIsusPseudoSample in MSC1, not supported in MSC3

    apf_fram.sys_last_error = 0;            //
    apf_fram.sys_error_ctr = 0;             //
    apf_fram.sys_error_time = 0;            //
    apf_fram.sys_file_ctr = SYS_FILECTR_VAL;        //NOT Needed

    apf_fram.logIsus_fsize = 0;             //?   //fsize of PROFxxxx.log file
    apf_fram.logDura_fsize = 0;             //

    //--------------New Vars, add here ----
    apf_fram.isus_sampleCnt_perProfile = 0;     //
    apf_fram.dura_sampleCnt_perProfile = 0;     //

    apf_fram.errNum = 0;                      // used by FRAM soak test

    apf_fram.errorLog_errorCnt = 0;             // used to count the number of errors written to the error log

    apf_fram.microSD_errCnt = 0;              // new 9 May 2025
    apf_fram.Efpos = 0;                       // store error.log pointer.

    apf_fram.spare[0] = 0;
    apf_fram.spare[1] = 0;
    apf_fram.spare[2] = 0;
    apf_fram.spare[3] = 0;
    apf_fram.spare[4] = 0;
    apf_fram.spare[5] = 0;
    apf_fram.spare[6] = 0;
    apf_fram.spare[7] = 0;

    apf_fram.initNumEnd = BYTE_SIGNATURE;
}

// Called by "defaults" command, perhaps this should be in apf_init.c
void apf_defaults(void)
{

    gBootError = 0;

    // zero the array
    apf        = (struct apfBootInitGlobals){ };
    apf_eeprom = (struct apfCfgGlobals){ };
    apf_fram   = (struct apfVolatileGlobals){ };

    wdog_pet();
    eeprom_sys_data_defaults();
    apf_init_hmi_vars();
    eeprom_sys_data_store();        //store sys_data in EEPROM   eeprom_sys_data_store();  was storeSysDataVariables

    prn_nl();
    uprintf("Defaults assigned and stored in EEPROM for sys_data"); prn_nl();

    fram_sys_samp_defaults();            //
    fram_sys_samp_store();
    uprintf("Defaults assigned and stored to FRAM for sys_samp"); prn_nl();
    wdog_pet();

    fram_apf_var_defaults();            //byte signatures, zeros
    apf_init_apf_struct_defaults();    //apf_init.c, overwrites some of apf_fram and apf_eeprom

    apf_m2m_mode_set(0);                //defaults for m2m mode

    eeprom_apf_cfg_store();             //system.c
    fram_apf_vars_store();              //APFTODO, rename to fram_apfcfg_store();

    apf_deployment_init();              //added back 18 May 2025, defaults should end up looking like INIT was run also, error.log deleted

    wdog_pet();

    fram_sys_samp_store();

    fram_sampl_struct_defaults();            //sampl struct used for pH (DURA)
    fram_last_samp_store();

    //dbuf used for no3 (ISUS)
    fram_apf_dbuf_store(&prnBuf[0], apf_fram.sendlast_byteCnt);

    print_sys_data_structure();
    prn_nl(); prn_nl();   //uprintf("\r\n\r\n");

    print_sys_samp_data_structure();
    prn_nl(); prn_nl();

    wait_consoleTx();

    print_isus_header();
    prn_nl(); prn_nl();

    wait_consoleTx();

    print_dura_header();
    prn_nl();  prn_nl();

    wait_consoleTx();

    uprintf("Defaults assigned and stored in EEPROM and FRAM, m2m mode is active"); prn_nl(); prn_nl();

    LogErrorMsg("Default variable values were loaded into NV FRAM and EEPROM", (int)INIT_NVRAM_WITH_DEFAULTS, apf_eeprom.sys_message_enable);

    wait_consoleTx();

    UARTFlushRx();

    uprintf("apf_defaults is finished, wait for sleep *, then wakeup and"); prn_nl();
    uprintf("run readz command to load zcoefs and ecoefs into FRAM"); prn_nl();
}


void fram_sys_samp_defaults(void)
{
    sys_samp.initNumBeg = BYTE_SIGNATURE;       //0x55AA55AA
    sys_samp.state = IDLE;                  //430 IDLE, DEPLOYED, COMMAND,  NOT USER MODIFIABLE                        <<<<<------
    sys_samp.nextWakeUp = 0;            //434 Next intended RTC match (wake-up time) NOT USER MODIFIABLE           <<<<<------
    sys_samp.next_gdata_fptr = 0;       //438 The file position of the next gdata sample. NOT USER MODIFIABLE      <<<<<------
    sys_samp.current_sample = 0;        //442 The current data record to be stored. NOT USER MODIFIABLE            <<<<<------
    sys_samp.AD24_std = 0.0;                 //464 standard deviation for AD24                                          <<<<--------
    sys_samp.initNumEnd = BYTE_SIGNATURE;       //0x55AA55AA

    //fram_sys_samp_store();

}

// return 0 for failed verify
// return 1 if OK
// return -1 if fram is 'blank' ff's
int fram_sys_samp_verify(void)
{
    if((sys_samp.initNumBeg == BYTE_SIGNATURE) && (sys_samp.initNumEnd == BYTE_SIGNATURE))
    {
        return(1);
    }

    if((sys_samp.initNumBeg == 0xffffffff) || (sys_samp.initNumEnd == 0xffffffff))
    {
        return(-1);
    }

    return(0);
}

// return 0 for failed verify
// return 1 if OK
// return -1 if fram is 'blank' ff's
int fram_apf_vars_verify(void)
{
    if((apf_fram.initNumBeg == BYTE_SIGNATURE) && (apf_fram.initNumEnd == BYTE_SIGNATURE))
    {
        return(1);
    }

    if((apf_fram.initNumBeg == 0xffffffff) || (apf_fram.initNumEnd == 0xffffffff))
    {
        return(-1);
    }

    return(0);
}

// was init_sampl_struct
void fram_sampl_struct_defaults(void)
{
    sampl.initNumBeg = BYTE_SIGNATURE;      //0x55AA55AA
    sampl.sampNum = 0;          // 1
    sampl.timestamp = 0;        // 2
    sampl.fBiasNeg = 0.0;       // 3
    sampl.fBiasPos = 0.0;       // 4
    sampl.fIb = 0.0;            // 5 (Ib) SubstrateCurrent
    sampl.fIk = 0.0;            // 6 (Ik) CounterElectrodeCurrent
    sampl.fIk_std = 0.0;        // 7
    sampl.fVk = 0.0;            // 8 (Vk) CounterElectrodeVoltage
    sampl.fVk_std = 0.0;        // 9
    sampl.fVtherm = 0.0;        // 10
    sampl.fVtherm_std = 0.0;    // 11  Likely not needed
    sampl.fTempC = 0.0;         // 12
    sampl.fVrsi = 0.0;          // 13
    sampl.fVrsi_std = 0.0;      // 14
    sampl.pHint = 0.0;          // 15
    sampl.fVrse = 0.0;          // 16
    sampl.fVrse_std = 0.0;      // 17
    sampl.pHext = 0.0;          // 18
    sampl.fVbat_main = 0.0;     // 19
    sampl.fBrdTempC = 0.0;      // 20
    sampl.uiHumidity = 0;;      // 21
    sampl.fsysAmp = 0.0;        // 22  //new 8 Oct 2024
    sampl.spare1 = 0.0;         // 23
    sampl.spare2 = 0.0;         // 24
    sampl.initNumEnd = BYTE_SIGNATURE;
}


void ads1248_sampling_defaults(void)
{
    sys_data.Vtherm_trials = VTHERM_TRIALS;
    sys_data.Vtherm_sps = VTHERM_SPS;

    sys_data.Vrsi_trials = VRSI_TRIALS;
    sys_data.Vrsi_sps = VRSI_SPS;

    sys_data.Vrse_trials = VRSE_TRIALS;
    sys_data.Vrse_sps = VRSE_SPS;

    sys_data.Ik_trials = IK_TRIALS;
    sys_data.Ik_sps = IK_SPS;

    sys_data.Vk_trials = VK_TRIALS;
    sys_data.Vk_sps = VK_SPS;

    sys_data.Ib_trials = IB_TRIALS;
    sys_data.Ib_sps = IB_SPS;

    sys_data.Vb_trials = VB_TRIALS;
    sys_data.Vb_sps = VB_SPS;

    sys_data.Vbias_trials = VBIAS_TRIALS;
    sys_data.Vbias_sps = VBIAS_SPS;       // used for vbias bat
}


#ifdef NOCODE
void init_sys_samp_default(void)
{
    fram_sys_samp_defaults();   //was defd in system.c, initSysSampFRAMVariables
}
#endif


// return 0 for failed verify
// return 1 if OK
int eeprom_sys_data_verify(void)
{
    if((sys_data.initNumBeg == BYTE_SIGNATURE) && (sys_data.initNumEnd == BYTE_SIGNATURE))
    {
        return(1);
    }

    return(0);
}



// was init_sys_data_default
void eeprom_sys_data_defaults(void)
{
    int indx;

    sys_data.initNumBeg = 0x55AA55AA;              //new 13 May 2025
    sys_data.initNumEnd = 0x55AA55AA;

    strcpy(sys_data.firmware_version, VERSION);  //new 10Oct2022

    sys_data.deploy_time = read_hib_rtcc();

    sys_data.dura_vin_min_volt = 9.2;
    sys_data.isus_vin_min_volt = ISUS_SYS_V_MIN;    // defd in apf.h 11.2v, was 10.0v, changed after Josh Plant review of eol float batteries.
#if BOARD_MFET >= 1
    sys_data.dura_vin_min_volt = 5.5;
#endif
#if BOARD_MPHOX >= 1  || BOARD_MSC == 2
    sys_data.dura_vin_min_volt = 10.0;
#endif
#if BOARD_NANOFET >= 1
    sys_data.dura_vin_min_volt = 5.5;
#endif
#if BOARD_MSC == 3
    sys_data.isus_vin_min_volt = ISUS_SYS_V_MIN;    //defd in apf.h 10.0v
    sys_data.dura_vin_min_volt = DURA_SYS_V_MIN;        //defd in apf.h 8.0v
#endif
    sys_data.sampling_period = 0;
    sys_data.chksum = 0;           // was sample average
    sys_data.averaging_interval = 60;
    sys_data.sample_aligned = false;
    sys_data.GMT = false;                       //456 GMT or Local time
    sys_data.sample_aligned = true;            //460 Sample aligned to hour
    sys_data.pump1_ontime = 0;
    sys_data.TCOffset = 0.0;
    sys_data.k0ext = -1.3;
    sys_data.k0int = -0.4;
    sys_data.k2ext = -0.001081;
    sys_data.k2int = -0.001455;


    // steinhart-hart coefficients for thermistor (why are the 4 coeff, thought there should only be 3)
    sys_data.quadTherm_c0 = 340.9819863;
    sys_data.quadTherm_c1 = -9.10257E-05;  // -0.0000910257
    sys_data.quadTherm_c2 = -95.08806667;
    sys_data.quadTherm_c3 = 0.965370274;

    sys_data.shTherm_A = 0.002108508173;
    sys_data.shTherm_B = 0.00007979204727;
    sys_data.shTherm_C = 0.0000006535076315;

    for(indx=0; indx<MAX_NUMOF_K2_CAL_FIELDS; indx++)
    {
        sys_data.K2_Cal[indx] = 0.0000;
    }

    for(indx=0; indx<MAX_NUMOF_FP_CAL_FIELDS; indx++)
    {
        sys_data.fP_Cal[indx] = 0.0000;
    }

    sys_data.k0_HCL = 0.0;
    sys_data.k0_SW = 0.0;
    sys_data.k0std_SW = 0.0;

    strcpy(sys_data.service_date, " ");
    strcpy(sys_data.PT_Cal_date, " ");
    strcpy(sys_data.k0_Cal_date, " ");
    strcpy(sys_data.cal_filename, " ");

    sys_data.default_sal = 35.0;
    sys_samp.sensor_sal = 0.0;

    ads1248_sampling_defaults();

    sys_data.offset_Ik = 0.000800;             // 800uV or so, tbd measure a QA step.
    sys_data.offset_Ib = 0.000800;

    sys_data.output = NORMAL;
    sys_data.test_mode = 0;
    sys_data.fast_mode = 1;
    sys_data.solenoid_on2hold_msec = 100;   // hit
    sys_data.solenoid_hold_pwm = 40;        // hold
    sys_data.solenoid_holdtime = 10;        // seconds


    //state;                      //430 IDLE, DEPLOYED, COMMAND,  NOT USER MODIFIABLE                        <<<<<------
    //uint32_t nextWakeUp;            //434 Next intended RTC match (wake-up time) NOT USER MODIFIABLE           <<<<<------
    //unsigned long next_gdata_fptr;  //438 The file position of the next gdata sample. NOT USER MODIFIABLE      <<<<<------
    //unsigned int current_sample;    //442 The current data record to be stored. NOT USER MODIFIABLE            <<<<<------
    //float AD24_std;                 //464 standard deviation for AD24                                          <<<<--------
    sys_samp.state = IDLE;
    //sys_data.nextWakeUp = current_time + 30;
    sys_samp.nextWakeUp = read_hib_rtcc()+sys_data.sampling_period;
    sys_samp.next_gdata_fptr = 0;
    sys_samp.AD24_std = 0.0;
    sys_samp.current_sample = 0;
    sys_samp.start_pump_cntdown = 0;

    sys_data.deploy_time = 0;
    sys_data.start_pump_time = 0;         // Wakeup on sampling interval, but check the 'start deployment time' before actual pumping/sampling 16 Feb 2022

    sys_data.pump_seq_type = TYPE_PUMP_SINGLE;             // 0) legacy single pump, 1) BEAMS v1 dual pump seq, 2) BEAMS v2
    //uint32_t pump_seq_state;          // defd in FRAM volatile data struct
    sys_data.sampleCnt_duty_on = 0;         // duty cycle for extending experiment.   3 days on, 5 days off.   Number of 'sample periods' on
    sys_data.sampleCnt_duty_off = 0;        // in sample periods.    Accept input in hours, translate to sample periods (keep implementation simple for now).
    //uint32_t sampling_interval;         // = 1 min  (see sampling_period above)
    sys_data.pump1_cycles = 1;                   // = 10 cycles
    sys_data.pump2_cycles = 0;                   // = 5 cycles
    sys_data.pump1_ontime = 0;
    sys_data.pump2_ontime = 0;

    sys_data.msc_op = MSC1_OP;
    sys_data.echo_flag = 1;     // default echo,on is for MFET, NanoFET.   apf_init overrides and sets echo to off
    sys_data.terminal_mode = H2M_MODE;  // default for MFET, NanoFET.    apf_init sets terminal_mode = M2M_MODE
    sys_data.autoSleepSec = 30;                     // M2M mode, autosleep is overriden to 10 sec by apf_init


#if BOARD_MFET >= 1 || BOARD_MPHOX >= 1 || BOARD_MSC == 2 || BOARD_NANOFET >= 1
    if(sys_data.baud_rate != 115200)
    {
        uprintf("Baud rate is changed to default: 115200\r\n");
        wait_consoleTx();
        UARTStdioConfig(0, 115200, 16000000);       // 115200 bits per second
    }
    sys_data.baud_rate = 115200L;
#endif

#if BOARD_MSC == 3
    if(sys_data.msc_op == MSC1_OP)
    {
        if(sys_data.baud_rate != 9600)
        {
            uprintf("Baud rate is changed to default: 9600\r\n");
            wait_consoleTx();
            UARTStdioConfig(0, 9600, 16000000);       // 9600 bits per second
        }
        sys_data.baud_rate = 9600L;
    }
    if(sys_data.msc_op == MSC3_OP)
    {
        if(sys_data.baud_rate != 115200)
        {
            uprintf("Baud rate is changed to default: 115200L\r\n");
            wait_consoleTx();
            UARTStdioConfig(0, 115200, 16000000);       // 115200 bits per second
        }
        sys_data.baud_rate = 115200L;
    }
#endif

    for(indx=0; indx<MAX_NUMOF_CONFIG_FIELDS; indx++)
        sys_data.data_cfg[indx] = 0;


    sys_data.pHOffset = 0.0;
    sys_data.pHSlope = 1.0;
    sys_data.TCSlope = 1.0;
    sys_data.sensor_ctd_TC = 0.0;
    sys_data.sensor_optode_TC = 0.0;

    sys_data.spare[0] = 0.0;
    sys_data.spare[1] = 0.0;
    sys_data.spare[2] = 0.0;
    sys_data.spare[3] = 0.0;


    sys_data.diag = 0;                          // diagnostics, enables sample time measurement
    sys_data.debug_flag = 0;                    // diagnostic printf (dbg_printf() enabled)
    sys_data.GMT = false;                       // GMT or Local time
    sys_data.sample_aligned = false;            // Sample aligned to hour

    sys_data.pHOffset = 0.0;
    sys_data.pHSlope = 1.0;
    sys_data.fast_mode = 1;

    strcpy(sys_data.fileName, "noname.txt");
    strcpy(sys_data.user, "abc");
    strcpy(sys_data.board_SN, " ");
    strcpy(sys_data.package_fab_note, " ");
    strcpy(sys_data.mcap_SN, " ");
    strcpy(sys_data.sensor_SN, " ");
    strcpy(sys_data.isfet_SN, " ");
    strcpy(sys_data.ise_SN, " ");
    strcpy(sys_data.optode_SN, " ");
    strcpy(sys_data.ctd_SN, " ");
    strcpy(sys_data.pump_SN, " ");
    strcpy(sys_data.pump2_SN, " ");
    strcpy(sys_data.tiva_SN, " ");
    strcpy(sys_data.package_name, " ");   // instrument package name, ex. underway system, glider pH
    strcpy(sys_data.deploy_note, " ");
    strcpy(sys_data.cal_filename, " ");
    strcpy(sys_data.spec_SN, " ");
    strcpy(sys_data.lamp_SN, " ");
    strcpy(sys_data.probe_SN, " ");

    sys_data.sensor_ctd_TC = 1.0;
    sys_data.sensor_optode_TC = 1.0;
    for(indx=0; indx<MAX_NUMOF_K2_CAL_FIELDS; indx++)
    {
        sys_data.K2_Cal[indx] = 0.0;
    }
    for(indx=0; indx<MAX_NUMOF_FP_CAL_FIELDS; indx++)
    {
        sys_data.fP_Cal[indx] = 0.0;
    }

    sys_data.k0_HCL = 0.0;
    sys_data.k0_SW = 0.0;
    sys_data.k0std_SW = 0.0;

    sys_data.data_cfg[CFG_SAMPLE_NUM] = 1;          // A
    sys_data.data_cfg[CFG_TIMESTAMP] = 1;            // B

    sys_data.data_cfg[CFG_VIN_BAT_VOLT] = 1;        // C
    sys_data.data_cfg[CFG_BIAS_BAT_POS] = 1;        // D
    sys_data.data_cfg[CFG_BIAS_BAT_NEG] = 1;        // E

    sys_data.data_cfg[CFG_BRD_TEMP] = 1;            // F
    sys_data.data_cfg[CFG_BRD_HUMIDITY] = 1;        // G

    sys_data.data_cfg[CFG_V_THERMISTOR] = 1;        // H
    sys_data.data_cfg[CFG_V_THERMISTOR_STD] = 1;    //
    sys_data.data_cfg[CFG_CALC_TEMP] = 1;           // I

#if BOARD_MSC == 3

    sys_data.data_cfg[CFG_V_THERMISTOR] = 0;        // H        //No Vtherm on deep sea durafet 8 May 2025
    sys_data.data_cfg[CFG_V_THERMISTOR_STD] = 0;    //
    sys_data.data_cfg[CFG_VRSI] = 0;               // J
    sys_data.data_cfg[CFG_VRSI_STD] = 0;           //
    sys_data.data_cfg[CFG_CALC_PH_VRSI] = 0;          // K

    sys_data.data_cfg[CFG_VRSE] = 1;               // L
    sys_data.data_cfg[CFG_VRSE_STD] = 1;           //
    sys_data.data_cfg[CFG_CALC_PH_VRSE] = 1;          // m

    sys_data.data_cfg[CFG_VRSI_BIASED] = 0;        // N
    sys_data.data_cfg[CFG_VRSI_BIASED_STD] = 0;    // o
    sys_data.data_cfg[CFG_VRSE_BIASED] = 0;        // p
    sys_data.data_cfg[CFG_VRSE_BIASED_STD] = 0;    // q
#else
    sys_data.data_cfg[CFG_VRSI] = 1;               // J
    sys_data.data_cfg[CFG_VRSI_STD] = 1;           //
    sys_data.data_cfg[CFG_CALC_PH_VRSI] = 1;          // K

    sys_data.data_cfg[CFG_VRSE] = 1;               // L
    sys_data.data_cfg[CFG_VRSE_STD] = 1;           //
    sys_data.data_cfg[CFG_CALC_PH_VRSE] = 0;          // m

    sys_data.data_cfg[CFG_VRSI_BIASED] = 1;        // N
    sys_data.data_cfg[CFG_VRSI_BIASED_STD] = 0;    // o
    sys_data.data_cfg[CFG_VRSE_BIASED] = 0;        // p
    sys_data.data_cfg[CFG_VRSE_BIASED_STD] = 0;    // q
#endif



    sys_data.data_cfg[CFG_V_COUNTER_ELECT] = 1;     // R
    sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] = 1; //
    sys_data.data_cfg[CFG_I_COUNTER] = 1;           // T
    sys_data.data_cfg[CFG_I_SUBSTRATE] = 1;         // U

    sys_data.data_cfg[CFG_UART3_INST] = INST_OPTODE_NOTDEFD;              // v
    sys_data.data_cfg[CFG_UART4_INST] = INST_CTD_NOTDEFD;                 // w

    sys_data.data_cfg[CFG_SPARE1] = 0;
    sys_data.data_cfg[CFG_SPARE2] = 0;
    sys_data.data_cfg[CFG_SPARE3] = 0;
    sys_data.data_cfg[CFG_SPARE4] = 0;
    sys_data.data_cfg[CFG_SPARE5] = 0;
    sys_data.data_cfg[CFG_SPARE6] = 0;

    sys_data.app_cfg[APPCFG_SAMPLING_DIAG] = 0;
    sys_data.app_cfg[APPCFG_TEMPC_SH_ALG] = 0;
    sys_data.app_cfg[APPCFG_MICROSD_ENABLE] = 1;        // enable micro sd card
    sys_data.app_cfg[APPCFG_MENU_LEVEL] = 0;
    sys_data.app_cfg[APPCFG_DEPLOY_UARTECHO] = 1;       // 1 Echo ON
    sys_data.app_cfg[APPCFG_EXEC_SWITCHERONI] = EXEC_DEFAULT;       // 0

#if BOARD_MSC == 3
    sys_data.app_cfg[APPCFG_FLOATMODE] = 1;
#else
    sys_data.app_cfg[APPCFG_FLOATMODE] = 0;
#endif
    sys_data.app_cfg[APPCFG_SER_LINETERM_MODE] = 0;   //cr-lf mode
    sys_data.app_cfg[APPCFG_FILE_LINETERM_MODE] = 0;   //cr-lf mode
    sys_data.app_cfg[APPCFG_SPARE9] = 0;
    sys_data.app_cfg[APPCFG_SPARE10] = 0;
    sys_data.app_cfg[APPCFG_SPARE11] = 0;
    sys_data.app_cfg[APPCFG_SPARE12] = 0;
    sys_data.app_cfg[APPCFG_SPARE13] = 0;
    sys_data.app_cfg[APPCFG_SPARE14] = 0;
    sys_data.app_cfg[APPCFG_SPARE15] = 0;

// override NANOFET specifics...
#if BOARD_NANOFET >= 1
    sys_data.data_cfg[CFG_SAMPLE_NUM] = 1;          // A
    sys_data.data_cfg[CFG_TIMESTAMP] = 1;            // B

    sys_data.data_cfg[CFG_VIN_BAT_VOLT] = 0;        // c
    sys_data.data_cfg[CFG_BIAS_BAT_POS] = 1;        // D
    sys_data.data_cfg[CFG_BIAS_BAT_NEG] = 0;        // e

    sys_data.data_cfg[CFG_BRD_TEMP] = 1;            // F
    sys_data.data_cfg[CFG_BRD_HUMIDITY] = 1;        // G

    sys_data.data_cfg[CFG_V_THERMISTOR] = 1;        // H
    sys_data.data_cfg[CFG_V_THERMISTOR_STD] = 1;    //
    sys_data.data_cfg[CFG_CALC_TEMP] = 1;           // I

    sys_data.data_cfg[CFG_VRSI] = 0;               // j
    sys_data.data_cfg[CFG_VRSI_STD] = 0;           //
    sys_data.data_cfg[CFG_CALC_PH_VRSI] = 0;          // K

    sys_data.data_cfg[CFG_VRSE] = 1;               // L
    sys_data.data_cfg[CFG_VRSE_STD] = 1;           //
    sys_data.data_cfg[CFG_CALC_PH_VRSE] = 0;          // m


    sys_data.data_cfg[CFG_VRSI_BIASED] = 0;        // n
    sys_data.data_cfg[CFG_VRSI_BIASED_STD] = 0;    // o
    sys_data.data_cfg[CFG_VRSE_BIASED] = 0;        // p
    sys_data.data_cfg[CFG_VRSE_BIASED_STD] = 0;    // q

    sys_data.data_cfg[CFG_V_COUNTER_ELECT] = 1;     // R
    sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] = 1; //
    sys_data.data_cfg[CFG_I_COUNTER] = 1;           // T
    sys_data.data_cfg[CFG_I_SUBSTRATE] = 1;         // U

    sys_data.data_cfg[CFG_UART3_INST] = INST_OPTODE_NOTDEFD;              // v
    sys_data.data_cfg[CFG_UART4_INST] = INST_CTD_NOTDEFD;                 // w

    sys_data.data_cfg[CFG_SPARE1] = 0;
    sys_data.data_cfg[CFG_SPARE2] = 0;
    sys_data.data_cfg[CFG_SPARE3] = 0;
    sys_data.data_cfg[CFG_SPARE4] = 0;
    sys_data.data_cfg[CFG_SPARE5] = 0;
    sys_data.data_cfg[CFG_SPARE6] = 0;

    sys_data.app_cfg[APPCFG_SAMPLING_DIAG] = 0;
    sys_data.app_cfg[APPCFG_TEMPC_SH_ALG] = 0;
    sys_data.app_cfg[APPCFG_MICROSD_ENABLE] = 0;        // disable micro sd card
    sys_data.app_cfg[APPCFG_MENU_LEVEL] = 0;
    sys_data.app_cfg[APPCFG_DEPLOY_UARTECHO] = 1;       // 1 Echo ON
    sys_data.app_cfg[APPCFG_EXEC_SWITCHERONI] = EXEC_DEFAULT;      // For alternate 'application function' aka, Irene Hu enabling
    sys_data.app_cfg[APPCFG_FLOATMODE] = 0;            // was APPCFG_SPARE6  APF FLOAT
    sys_data.app_cfg[APPCFG_SER_LINETERM_MODE] = 0;        // was APPCFG_SPARE7
    sys_data.app_cfg[APPCFG_SPARE8] = 0;
    sys_data.app_cfg[APPCFG_SPARE9] = 0;
    sys_data.app_cfg[APPCFG_SPARE10] = 0;
    sys_data.app_cfg[APPCFG_SPARE11] = 0;
    sys_data.app_cfg[APPCFG_SPARE12] = 0;
    sys_data.app_cfg[APPCFG_SPARE13] = 0;
    sys_data.app_cfg[APPCFG_SPARE14] = 0;
    sys_data.app_cfg[APPCFG_SPARE15] = 0;
#endif

    //config_qa_data_init();   //init of qa removed 3 Nov 2022

}

// return 0 for failed verify
// return 1 if OK
int eeprom_apf_cfg_verify(void)
{
    if((apf_eeprom.initNumBeg == BYTE_SIGNATURE) && (apf_eeprom.initNumEnd == BYTE_SIGNATURE))
    {
        return(1);
    }

    return(0);
}



void eeprom_apf_cfg_defaults(void)
{

    apf_eeprom.initNumBeg = BYTE_SIGNATURE;

    apf_eeprom.isus_ref_samples = 0;        //2 msc1 menu

    apf_eeprom.lambda_beg = 0;              //2
    apf_eeprom.lambda_end = 0;              //2

    apf_eeprom.isus_salfit_mode = 0;        //2
    apf_eeprom.lambda_dat_beg = 0;          //2
    apf_eeprom.lambda_dat_end = 0;          //2
    apf_eeprom.isus_datpixel_beg = 0;       //2  SPECTRA
    apf_eeprom.isus_datpixel_end = 0;       //2

    apf_eeprom.dura_atod_readings = 0;      //2  //NOT NEEDED
    apf_eeprom.dura_atod_delay = 0;         //2  //NOT NEEDED
    apf_eeprom.dura_samps_per_cal = 0;      //2  //NOT NEEDED
    apf_eeprom.dura_led_enable = 0;         //2  //NOT NEEDED

    apf_eeprom.sys_tmout = 0;                 //2
    apf_eeprom.sys_tzoffset = 0;              //2
    apf_eeprom.sys_sleep_period = 0;          //2
    apf_eeprom.sys_pump_period = 0;           //2

    //apf_eeprom.logIsus[MAX_LOGNAME_SIZE];
    //apf_eeprom.logDura[MAX_LOGNAME_SIZE];

    apf_eeprom.spare[0] = 0;                    //2,3
    apf_eeprom.spare[1] = 0;
    apf_eeprom.spare[2] = 0;
    apf_eeprom.spare[3] = 0;
    apf_eeprom.spare[4] = 0;
    apf_eeprom.spare[5] = 0;
    apf_eeprom.spare[6] = 0;
    apf_eeprom.spare[7] = 0;

    apf_eeprom.initNumEnd = BYTE_SIGNATURE;

//-----------------------------------
    apf_eeprom.isus_warm_shut = TRUE;      //Shutter is no longer used, set shutter as open

    apf_fram.isus_last_error = 0;

    apf_eeprom.isus_spec_period = ISUS_PERIOD_VAL;
    apf_eeprom.isus_scans_per_dc = ISUS_SAMPSPERDC_VAL;
    apf_eeprom.isus_samps_per_clean = ISUS_SAMPSPERUV_VAL;

    //load ISUS FLOAT vals from the DS3234 SRAM
    apf_eeprom.no3_span = ISUS_NO3SPAN_VAL;
    apf_eeprom.no3_offset = ISUS_NO3OFFSET_VAL;

    apf_eeprom.isus_ref_limit = ISUS_REFLIMIT_VAL;

    apf_eeprom.isus_fit_concs = ISUS_FITCONCS_VAL;

    apf_eeprom.isus_pre_scans = ISUS_PRESCANS_VAL;
    apf_eeprom.isus_scan_num = ISUS_INTSCANS_VAL;
    apf_eeprom.isus_dark_num = ISUS_DCSCANS_VAL;
    apf_eeprom.isus_bsl_model = ISUS_BSLNMODL_VAL;
    apf_eeprom.isus_ref_samples = ISUS_REFSAMPS_VAL;
    apf_eeprom.isus_warm_lamp = ISUS_LAMPWARM_VAL;
    apf_eeprom.isus_clean_lamp = ISUS_CLEANUV_VAL;

    apf_eeprom.isus_pixel_beg = ISUS_PIXELBEG_VAL;
    apf_eeprom.isus_pixel_end = ISUS_PIXELEND_VAL;
    apf_eeprom.isus_fit_pixels = (apf_eeprom.isus_pixel_end - apf_eeprom.isus_pixel_beg) + 1;
    apf_eeprom.isus_tcomp_mode = ISUS_TCOMPMODE_VAL;
    apf_eeprom.isus_salfit_mode = NO3_SAL_FIX;       //ISUS_SALFITMODE_VAL;
    apf_eeprom.isus_datpixel_beg = ISUS_DATPIXELBEG_VAL;
    apf_eeprom.isus_datpixel_end = ISUS_DATPIXELEND_VAL;
    apf_eeprom.lambda_beg = 217;       //APFTODO make a define   NV not needed
    apf_eeprom.lambda_end = 240;
    apf_eeprom.lambda_dat_beg = 217;
    apf_eeprom.lambda_dat_end = 250;

    apf_eeprom.dura_atod_readings = DURA_ATODREADINGS_VAL;      //Var not used in MSC3
    apf_eeprom.dura_atod_delay = DURA_ATODDELAY_VAL;            //Var not used in MSC3
    apf_eeprom.dura_samps_per_cal = DURA_SAMPSPERCAL_VAL;       //Var not used in MSC3
    apf_eeprom.dura_led_enable = DURA_LEDENABLE_VAL;            //Var not used in MSC3


    apf_eeprom.sys_delay_hour = SYS_DELAYHR_VAL;
    apf_eeprom.sys_sync_hour = SYS_SYNCHR_VAL;
    apf_eeprom.sys_sync_min = SYS_SYNCMIN_VAL;
    apf_eeprom.sys_df_limit = SYS_DFLIMIT_VAL;
    apf_eeprom.sys_sf_limit = SYS_SFLIMIT_VAL;
    apf_eeprom.sys_ef_limit = SYS_EFLIMIT_VAL;
    apf_eeprom.sys_last_doy = SYS_LASTDOY_VAL;
    apf_eeprom.sys_serial_num = SYS_SERIALN_VAL;
    apf_eeprom.sys_apf_tmout = SYS_APFTMOUT_VAL;

    apf_eeprom.sys_tzoffset = SYS_TZOFFSET_VAL;

    apf_eeprom.sys_sleep_period = SYS_SLEEPPER_VAL;
    apf_eeprom.sys_pump_period = SYS_PUMPPER_VAL;

    // these were unsigned chars, now uint32_t
    apf_eeprom.sys_slog_delay = SYS_SYNCMODE_VAL;
    apf_eeprom.sys_deploy_mode = SYS_DEPLMODE_VAL;

    apf_eeprom.sys_time_type = SYS_TIMETYPE_VAL;
    apf_eeprom.sys_watchdog_enable = SYS_WDOGENABLE_VAL;
    apf_eeprom.sys_message_enable = SYS_MSGENABLE_VAL;
    apf_eeprom.sys_pump_enable = SYS_PUMPENABLE_VAL;
    apf_eeprom.sys_sensor_type = SYS_SENSORTYPE_VAL;


}

void ram_apf_var_defaults(void)
{
    /// JLD-TGM  I temporarily removed the structure fields that are
    /// JLD-TGM  specifically cleared in the functions: fram_apf_var_defaults() & eeprom_apf_cfg_defaults()
    // apf_fram.errNum = 0;                   //was errno, now errNum

    get_storage_stat(&apf.storage_size, &apf.storage_free);

    apf.isus_spec_pwr = FALSE;
    apf.isus_lamp_pwr = FALSE;
    apf.isus_lamp_deuterium = FALSE;
    apf.isus_lamp_tungsten = FALSE;
    apf.isus_ref_pwr = FALSE;

    apf.isus_sal = -1.0;            //Added by TM to globals 7 Oct 2024, from temp var isus_sal = FitData[SALINITY];  NV not needed
    apf.isus_no3 = -1.0;            // was uint32_t    from = FitData[NITRATE];  NV not needed
    apf.isus_hs = -1.0;             //  = FitData[BISULFIDE];   NV not needed
    apf.isus_err = -1.0;            // = FitData[FIT_ERROR];    NV not needed
    apf.dura_atod_pwr = FALSE;
    //apf_fram.dura_error_time = 0;
    //apf_fram.dura_sample_time = 0;
    //apf_fram.dura_error_ctr = 0;
    //apf_fram.dura_last_error = 0;
    //apf_fram.dura_reset_flag = TRUE;
    //apf_fram.dura_sample_ctr = 0;
    //apf_eeprom.dura_atod_readings = DURA_ATODREADINGS_VAL;      //Var not used in MSC3
    //apf_eeprom.dura_atod_delay = DURA_ATODDELAY_VAL;            //Var not used in MSC3
    //apf_eeprom.dura_samps_per_cal = DURA_SAMPSPERCAL_VAL;       //Var not used in MSC3
    //apf_eeprom.dura_led_enable = DURA_LEDENABLE_VAL;            //Var not used in MSC3
    apf.dura_pH = 0.0;              // NV not needed
    apf.pH = 0;                 //NV not needed?  redundant to dura_pH   APFTODO

    apf.gotSample = FALSE;      // flag for indicating sample taken

    apf.chkSum = 0;

    apf.sys_humidity = 0;

    apf.backupVoltage = -1.0;
    apf.VrsExt = -1.0;
    apf.StdDevVrs = -1.0;
    apf.Vk = -1.0;
    apf.StdDevVk = -1.0;
    apf.Ik = -1.0;
    apf.Ib = -1.0;
    apf.sys_temperature = -1.0;
    apf.sys_pressure = -1.0;
    apf.sys_current = -1.0;
    apf.sys_voltage = -1.0;
    apf.acquire_time = -1.0;

    apf.CTD_Time = 0;
    apf.acquire_time = -1.0;
    apf.CTD_Temp = -1.0;
    apf.CTD_Salinity = -1.0;
    apf.CTD_Depth = -1.0;

    apf.DataMean = -1.0;
    apf.DataStdDev = -1.0;
}


//void SetDefaultsIntoNVRAM( void )
void apf_init_apfArray_struct_defaults( void )
{
    // zero init first
    apfArray = (struct apfIsusArrays){ };

    sprintf(apfArray.CalDate,"00/00/00");
    apfArray.ZcoefsCnt = 0;       //Zcoefs;
    apfArray.EcoefsCnt = 0;       //Ecoefs;
    apfArray.ZerosCnt = 0;
    apfArray.ModuleNum = 0;       //Zeiss Module number
    apfArray.spare[0] = 0;
    apfArray.spare[1] = 1;
    apfArray.spare[2] = 2;
    apfArray.spare[3] = 3;
    apfArray.SwCalTemp = -11.0;
    for(uint32_t indx=0; indx<5; indx++)
    {
        apfArray.ZeissCoefs[indx] = 0.0;          //5
    }
    for(uint32_t indx=0; indx<256; indx++)
    {
        apfArray.LambdaData[indx] = 0.0;          //5
    }
    for(uint32_t indx=0; indx<256; indx++)
    {
        apfArray.ZeroData[indx] = 0.0;          //5
    }
    for(uint32_t indx=0; indx<10; indx++)
    {
        apfArray.TcompCoefs[indx] = 0.0;          //5
    }

    // read files to init struct vars apfArray, calls fram_apfArray_store
    init_zcoef_ecoef_apfArray_into_fram();

    prn_nl();
    uprintf("  ApfArray Default Values stored in NonVolatile FRAM");  prn_nl();

    LogErrorMsg("ApfArray Default Values stored in NonVolatile FRAM", (int)INIT_NVRAM_WITH_DEFAULTS, apf_eeprom.sys_message_enable);
}

#ifdef NOCODE
void config_qa_data_init(void)
{

    sys_qa.microSD = 0;            // microSD
    sys_qa.time = 0;               // time of QA
    sys_qa.test_time = 0;          // time it took to run QA
    sys_qa.eeprom = 0;
    sys_qa.sys_data_init = 0;      // was the data initialized
    sys_qa.fram = 0;
    sys_qa.adc12onchip = 0;
    sys_qa.adc24 = 0;              // how many of the ADC fields are out of expected Jensenian range


    sys_qa.Vrsi = 0.0;
    sys_qa.Vrsi_std = 0.0;
    sys_qa.Vrsi_err = 0.0;
    sys_qa.Vrse = 0.0;
    sys_qa.Vrse_std = 0.0;
    sys_qa.Vrse_err = 0.0;
    sys_qa.Vtherm = 0.0;
    sys_qa.Vtherm_std = 0.0;
    sys_qa.Vtherm_err = 0.0;

    sys_qa.Ik = 0.0;
    sys_qa.Ik_std = 0.0;
    sys_qa.Ik_err = 0.0;

    sys_qa.Vk = 0.0;
    sys_qa.Vk_std = 0.0;
    sys_qa.Vk_err = 0.0;

    sys_qa.Ib = 0.0;
    sys_qa.Ib_std = 0.0;
    sys_qa.Ib_err = 0.0;

    sys_qa.Vb = 0.0;
    sys_qa.Vb_std = 0.0;
    sys_qa.Vb_err = 0.0;

    sys_qa.Vbias_pos = 0.0;
    sys_qa.Vbias_neg = 0.0;

    sys_qa.aux_pwr1 = 0;
    sys_qa.aux_pwr2 = 0;
    sys_qa.aux_pwr3 = 0;

    sys_qa.ctd = 0;
    sys_qa.optode = 0;
    sys_qa.comm_pwr = 0;

    strcpy(sys_qa.tester, "Count Zero");
    strcpy(sys_qa.firmware_version, "Version Not Defined");

}
#endif

void eeprom_sys_qa_defaults(void)
{
    sys_qa.microSD = 0;            // microSD
    sys_qa.time = 0;               // time of QA
    sys_qa.test_time = 0;          // time it took to run QA
    sys_qa.eeprom = 0;
    sys_qa.sys_data_init = 0;      // was the data initialized
    sys_qa.fram = 0;
    sys_qa.adc12onchip = 0;
    sys_qa.adc24 = 0;              // how many of the ADC fields are out of expected Jensenian range


    sys_qa.Vrsi = 0.0;
    sys_qa.Vrsi_std = 0.0;
    sys_qa.Vrsi_err = 0.0;
    sys_qa.Vrse = 0.0;
    sys_qa.Vrse_std = 0.0;
    sys_qa.Vrse_err = 0.0;
    sys_qa.Vtherm = 0.0;
    sys_qa.Vtherm_std = 0.0;
    sys_qa.Vtherm_err = 0.0;

    sys_qa.Ik = 0.0;
    sys_qa.Ik_std = 0.0;
    sys_qa.Ik_err = 0.0;

    sys_qa.Vk = 0.0;
    sys_qa.Vk_std = 0.0;
    sys_qa.Vk_err = 0.0;

    sys_qa.Ib = 0.0;
    sys_qa.Ib_std = 0.0;
    sys_qa.Ib_err = 0.0;

    sys_qa.Vb = 0.0;
    sys_qa.Vb_std = 0.0;
    sys_qa.Vb_err = 0.0;

    sys_qa.Vbias_pos = 0.0;
    sys_qa.Vbias_neg = 0.0;

    sys_qa.aux_pwr1 = 0;
    sys_qa.aux_pwr2 = 0;
    sys_qa.aux_pwr3 = 0;

    sys_qa.ctd = 0;
    sys_qa.optode = 0;
    sys_qa.comm_pwr = 0;

    strcpy(sys_qa.tester, "Count Zero");
    strcpy(sys_qa.firmware_version, "Version Not Defined");
}

int eeprom_sys_qa_verify(void)
{
    return(1);
}



