/*
 * apf_init.c
 *
 *  Created on: Feb 26, 2024
 *      Author: tm, from lc cf2 code
 */



#include "apf.h"
#include "apf_init.h"
#include "serial.h"

extern struct apfGlobals apf;

void init_apf_global_vars(void)
{
    //InitVars();


    apf.errNum = 0;       //was errno
    apf.isus_spec_pwr = 0;
    apf.isus_lamp_pwr = 0;
    apf.isus_ref_pwr = 0;
    apf.dura_atod_pwr = 0;
    apf.sys_sensor_mode = 0;   //ISUS_SENSOR_MODE or DURA_SENSOR_MODE
    apf.sys_por_ctr = 0;
    apf.sys_reset_ctr = 0;
    apf.sys_reset_time = 0;
    apf.isus_error_time = 0;
    apf.dura_error_time = 0;
    apf.isus_sample_time = 0;
    apf.dura_sample_time = 0;
    apf.gotSample = 0;      // flag for indicating sample taken
    apf.acquire_time = 0;   // time to acquire sample
    apf.dura_error_ctr = 0;
    apf.dura_error_time = 0;
    apf.acquire_time = 0;


    //uchar
    apf.sys_slog_delay = 0;
    apf.sys_deploy_mode = 0;
    apf.sys_data_mode = 0;
    apf.sys_time_type = 0;
    apf.sys_watchdog_enable = 0;
    apf.sys_message_enable = 0;
    apf.sys_pump_enable = 0;
    apf.sys_sensor_type = 0;

    //ushorts
    apf.sys_delay_hour = 0;
    apf.sys_sync_hour = 0;
    apf.sys_sync_min = 0;
    apf.sys_df_limit = 0;
    apf.sys_sf_limit = 0;
    apf.sys_ef_limit = 0;
    apf.sys_last_doy = 0;
    apf.sys_serial_num = 0;
    apf.sys_tmout = 0;
    apf.sys_tzoffset = 0;
    apf.sys_last_error = 0;
    apf.sys_error_ctr = 0;
    apf.sys_error_time = 0;
    apf.sys_reset_ctr = 0;
    apf.sys_reset_time = 0;
    apf.sys_por_ctr = 0;
    apf.sys_file_ctr = 0;
    apf.sys_sleep_period = 0;
    apf.sys_pump_period = 0;

}


//#if BOARD_MSC3 >= 1

#ifdef APFTODO

/******************************************************************************/
/* MSC Initialization Routines                                                */
/******************************************************************************/

//#define ShowInitTimes 1

void InitMSC( void )
{
    short ret;

#ifdef ShowInitTimes
    ldiv_t sdiv;
    RTCTimer ElpTm;
#endif

    DS3234_Init();
    RegIO_Init();

    if( RegIO_RdWord() & SYS_POR_MON )
        sys_por_flag = FALSE;
    else
        sys_por_flag = TRUE;

    if( CFCardDetect() != TRUE ) {
        cprintf("\nNO FLASH CARD FOUND, EXITING!\n");
        BIOSResetToPicoDOS();           //return to PicoDOS Monitor
    }

    if( pdx_access("ISUS", _A_SUBDIR) != OK ) {
        ret = execstr("MKDIR C:\\ISUS");
        if( pdx_access("ISUS", _A_SUBDIR) != OK ) {
            printf("\nISUS MKDIR Failure, ret = %d!\n", ret);
            BIOSResetToPicoDOS();
        }
    }

    if( pdx_access("DURA", _A_SUBDIR) != OK ) {
        ret = execstr("MKDIR C:\\DURA");
        if( pdx_access("DURA", _A_SUBDIR) != OK ) {
            printf("\nDURA MKDIR Failure, ret = %d!\n", ret);
            BIOSResetToPicoDOS();
        }
    }

    printf("\n\n  MBARI Sensor Controller (ISUS/DURA) Initializing...\n");

    InitVars();

    IOPwrUp();

#ifdef ShowInitTimes
    RTCElapsedTimerSetup( &ElpTm );
#endif
    Zcoefs=LoadIsusZCoefs(ZeissCoefs);
#ifdef ShowInitTimes
    sdiv = ldiv(RTCElapsedTime( &ElpTm ), 1000000L);
    printf("ZcoefLoad = %ld.%.6ld\n", sdiv.quot, sdiv.rem);
#endif
    if(Zcoefs > 0) {
        InitLambdaArray(LambdaData, ZeissCoefs);
#ifdef ShowInitTimes
        sdiv = ldiv(RTCElapsedTime( &ElpTm ), 1000000L);
        printf("InitLambda = %ld.%.6ld\n", sdiv.quot, sdiv.rem);
#endif
        Ecoefs=LoadIsusECoefs(NULL, EcoefFile, ZeroData, TcompCoefs, &SwCalTemp, CalDate);
#ifdef ShowInitTimes
        sdiv = ldiv(RTCElapsedTime( &ElpTm ), 1000000L);
        printf("EcoefLoad = %ld.%.6ld\n", sdiv.quot, sdiv.rem);
#endif
        CopyEcoefs(EcoefFile, EcoefData);
#ifdef ShowInitTimes
        sdiv = ldiv(RTCElapsedTime( &ElpTm ), 1000000L);
        printf("CopyEcoefs = %ld.%.6ld\n", sdiv.quot, sdiv.rem);
#endif
    }
    else{
        Ecoefs=LoadIsusECoefs(LambdaData, EcoefFile, ZeroData, TcompCoefs, &SwCalTemp, CalDate);
        CopyEcoefs(EcoefFile, EcoefData);
    }

    //error log entry if no ZCOEF or ECOEF file

    DisplaySystemInfo();

    SyncNextSample();

    SCIRxSetBuffered( TRUE ); // enables IRQ4-

}


void InitVars( void )
{
    char ebuf[ERRBUFSIZE];
    uchar val_uchar;

    DS3234_RdSRAM(SYS_INITSRAM_PTR, sizeof(uchar), (uchar*)&val_uchar);

    if( val_uchar != SYS_INITSRAM_VAL ) {
        val_uchar = SYS_INITSRAM_VAL;
        DS3234_WrSRAM(SYS_INITSRAM_PTR, sizeof(uchar), (uchar*)&val_uchar);

        DS3234_RdSRAM(SYS_INITSRAM_PTR, sizeof(uchar), (uchar*)&val_uchar);

        if( val_uchar != SYS_INITSRAM_VAL ) {
            cprintf("\nNO DS3234 RTC FOUND, EXITING!\n");
            BIOSResetToPicoDOS();           //return to PicoDOS Monitor
        }
        else
            SetDefaultsIntoNVRAM();
    }

    GetVarsFromNVRAM();

    isus_reset_flag = dura_reset_flag = TRUE;          // flag to mark initialization for outputing header info

    sys_reset_time = DS3234_secs;   // instrument initialization time
    DS3234_WrSRAM(SYS_RESETTIME_PTR, sizeof(ulong), (ulong*)&sys_reset_time);

    ++sys_reset_ctr;
    DS3234_WrSRAM(SYS_RESETCTR_PTR, sizeof(ulong), (ulong*)&sys_reset_ctr);

    if( sys_por_flag == TRUE ) {
        ++sys_por_ctr;
        DS3234_WrSRAM(SYS_PORCTR_PTR, sizeof(ulong), (ulong*)&sys_por_ctr);
    }

    isus_fit_pixels = (isus_pixel_end - isus_pixel_beg) + 1;

    SpecData = calloc((size_t)(256*isus_scan_num), sizeof(ushort));
    if( SpecData == NULL ) {
        printf("CAN'T allocate ScanData Array memory\n");
        sprintf(ebuf, "CAN'T allocate ScanData Array memory");
        LogErrorMsg(ebuf, (int)MALLOC_FAILURE, sys_message_enable);
    }

    DarkData = calloc((size_t)(256*isus_dark_num), sizeof(ushort));
    if( DarkData == NULL ) {
        printf("CAN'T allocate DarkScanData Array memory\n");
        sprintf(ebuf, "CAN'T allocate DarkScanData Array memory");
        LogErrorMsg(ebuf, (int)MALLOC_FAILURE, sys_message_enable);
    }

    EcoefFile=dmatrix(1L,256L,1L,3L);
    if (EcoefFile != NULL )
        InitEcoefMatrix(EcoefFile);

    EcoefData=dmatrix(1L,256L,1L,3L);
    if (EcoefData != NULL )
        InitEcoefMatrix(EcoefData);

    dat_file=FNULL;
    err_file=FNULL;

}

void GetVarsFromNVRAM( void )
{
    GetSysVarsFromNVRAM();
    GetIsusVarsFromNVRAM();
    GetDuraVarsFromNVRAM();
}

void SetDefaultsIntoNVRAM( void )
{
    char ebuf[ERRBUFSIZE];

    SetSysDefaultsIntoNVRAM();
    SetIsusDefaultsIntoNVRAM();
    SetDuraDefaultsIntoNVRAM();

    sprintf(ebuf, "Default variable values were loaded into RTC NV SRAM");
    LogErrorMsg(ebuf, (int)INIT_NVRAM_WITH_DEFAULTS, sys_message_enable);

}

void GetSysVarsFromNVRAM( void )
{

    //load SYS UCHAR vals from the DS3234 SRAM
    DS3234_RdSRAM(SYS_SYNCMODE_PTR, sizeof(uchar), (uchar*)&sys_slog_delay);
    DS3234_RdSRAM(SYS_DEPLMODE_PTR, sizeof(uchar), (uchar*)&sys_deploy_mode);
    DS3234_RdSRAM(SYS_DATAMODE_PTR, sizeof(uchar), (uchar*)&sys_data_mode);
    DS3234_RdSRAM(SYS_TIMETYPE_PTR, sizeof(uchar), (uchar*)&sys_time_type);
    DS3234_RdSRAM(SYS_WDOGENABLE_PTR, sizeof(uchar), (uchar*)&sys_watchdog_enable);
    DS3234_RdSRAM(SYS_MSGENABLE_PTR, sizeof(uchar), (uchar*)&sys_message_enable);
    DS3234_RdSRAM(SYS_PUMPENABLE_PTR, sizeof(uchar), (uchar*)&sys_pump_enable);
    DS3234_RdSRAM(SYS_SENSORTYPE_PTR, sizeof(uchar), (uchar*)&sys_sensor_type);

    //load SYS USHORT vals from the DS3234 SRAM
    DS3234_RdSRAM(SYS_DELAYHR_PTR, sizeof(ushort), (ushort*)&sys_delay_hour);
    DS3234_RdSRAM(SYS_SYNCHR_PTR, sizeof(ushort), (ushort*)&sys_sync_hour);
    DS3234_RdSRAM(SYS_SYNCMIN_PTR, sizeof(ushort), (ushort*)&sys_sync_min);
    DS3234_RdSRAM(SYS_DFLIMIT_PTR, sizeof(ushort), (ushort*)&sys_df_limit);
    DS3234_RdSRAM(SYS_SFLIMIT_PTR, sizeof(ushort), (ushort*)&sys_sf_limit);
    DS3234_RdSRAM(SYS_EFLIMIT_PTR, sizeof(ushort), (ushort*)&sys_ef_limit);
    DS3234_RdSRAM(SYS_LASTDOY_PTR, sizeof(ushort), (ushort*)&sys_last_doy);
    DS3234_RdSRAM(SYS_SERIALN_PTR, sizeof(ushort), (ushort*)&sys_serial_num);
    DS3234_RdSRAM(SYS_APFTMOUT_PTR, sizeof(ushort), (ushort*)&sys_apf_tmout);

    //load SYS INT vals from the DS3234 SRAM
    DS3234_RdSRAM(SYS_TZOFFSET_PTR, sizeof(int), (int*)&sys_tzoffset);
    DS3234_RdSRAM(SYS_LASTERROR_PTR, sizeof(int), (int*)&sys_last_error);

    //load SYS ULONG vals from the DS3234 SRAM
    DS3234_RdSRAM(SYS_ERRORCTR_PTR, sizeof(ulong), (ulong*)&sys_error_ctr);
    DS3234_RdSRAM(SYS_LASTERRTIME_PTR, sizeof(ulong), (ulong*)&sys_error_time);
    DS3234_RdSRAM(SYS_RESETCTR_PTR, sizeof(ulong), (ulong*)&sys_reset_ctr);
    DS3234_RdSRAM(SYS_RESETTIME_PTR, sizeof(ulong), (ulong*)&sys_reset_time);
    DS3234_RdSRAM(SYS_PORCTR_PTR, sizeof(ulong), (ulong*)&sys_por_ctr);
    DS3234_RdSRAM(SYS_FILECTR_PTR, sizeof(ulong), (ulong*)&sys_file_ctr);
    DS3234_RdSRAM(SYS_SLEEPPER_PTR, sizeof(ulong), (ulong*)&sys_sleep_period);
    DS3234_RdSRAM(SYS_PUMPPER_PTR, sizeof(ulong), (ulong*)&sys_pump_period);

}

void SetSysDefaultsIntoNVRAM( void )
{
    uchar val_uchar;
    ushort val_ushort;
    int val_int;
    ulong val_ulong;

    //load SYS UCHAR vals into the DS3234 SRAM
    val_uchar = SYS_SYNCMODE_VAL;           DS3234_WrSRAM(SYS_SYNCMODE_PTR, sizeof(uchar), (uchar*)&val_uchar);
    val_uchar = SYS_DEPLMODE_VAL;           DS3234_WrSRAM(SYS_DEPLMODE_PTR, sizeof(uchar), (uchar*)&val_uchar);
    val_uchar = SYS_DATAMODE_VAL;           DS3234_WrSRAM(SYS_DATAMODE_PTR, sizeof(uchar), (uchar*)&val_uchar);
    val_uchar = SYS_TIMETYPE_VAL;           DS3234_WrSRAM(SYS_TIMETYPE_PTR, sizeof(uchar), (uchar*)&val_uchar);
    val_uchar = SYS_WDOGENABLE_VAL;         DS3234_WrSRAM(SYS_WDOGENABLE_PTR, sizeof(uchar), (uchar*)&val_uchar);
    val_uchar = SYS_MSGENABLE_VAL;          DS3234_WrSRAM(SYS_MSGENABLE_PTR, sizeof(uchar), (uchar*)&val_uchar);
    val_uchar = SYS_PUMPENABLE_VAL;         DS3234_WrSRAM(SYS_PUMPENABLE_PTR, sizeof(uchar), (uchar*)&val_uchar);
    val_uchar = SYS_SENSORTYPE_VAL;         DS3234_WrSRAM(SYS_SENSORTYPE_PTR, sizeof(uchar), (uchar*)&val_uchar);

    //load SYS USHORT vals into the DS3234 SRAM
    val_ushort = SYS_DELAYHR_VAL;           DS3234_WrSRAM(SYS_DELAYHR_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = SYS_SYNCHR_VAL;            DS3234_WrSRAM(SYS_SYNCHR_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = SYS_SYNCMIN_VAL;           DS3234_WrSRAM(SYS_SYNCMIN_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = SYS_DFLIMIT_VAL;           DS3234_WrSRAM(SYS_DFLIMIT_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = SYS_SFLIMIT_VAL;           DS3234_WrSRAM(SYS_SFLIMIT_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = SYS_EFLIMIT_VAL;           DS3234_WrSRAM(SYS_EFLIMIT_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = SYS_LASTDOY_VAL;           DS3234_WrSRAM(SYS_LASTDOY_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = SYS_SERIALN_VAL;           DS3234_WrSRAM(SYS_SERIALN_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = SYS_APFTMOUT_VAL;          DS3234_WrSRAM(SYS_APFTMOUT_PTR, sizeof(ushort), (ushort*)&val_ushort);

    //load SYS INT vals into the DS3234 SRAM
    val_int = SYS_TZOFFSET_VAL;             DS3234_WrSRAM(SYS_TZOFFSET_PTR, sizeof(int), (int*)&val_int);
    val_int = SYS_LASTERROR_VAL;            DS3234_WrSRAM(SYS_LASTERROR_PTR, sizeof(int), (int*)&val_int);

    //load SYS ULONG vals into the DS3234 SRAM
    val_ulong = SYS_ERRORCTR_VAL;           DS3234_WrSRAM(SYS_ERRORCTR_PTR, sizeof(ulong), (ulong*)&val_ulong);
    val_ulong = SYS_LASTERRTIME_VAL;        DS3234_WrSRAM(SYS_LASTERRTIME_PTR, sizeof(ulong), (ulong*)&val_ulong);
    val_ulong = SYS_RESETCTR_VAL;           DS3234_WrSRAM(SYS_RESETCTR_PTR, sizeof(ulong), (ulong*)&val_ulong);
    val_ulong = SYS_RESETTIME_VAL;          DS3234_WrSRAM(SYS_RESETTIME_PTR, sizeof(ulong), (ulong*)&val_ulong);
    val_ulong = SYS_PORCTR_VAL;             DS3234_WrSRAM(SYS_PORCTR_PTR, sizeof(ulong), (ulong*)&val_ulong);
    val_ulong = SYS_FILECTR_VAL;            DS3234_WrSRAM(SYS_FILECTR_PTR, sizeof(ulong), (ulong*)&val_ulong);
    val_ulong = SYS_SLEEPPER_VAL;           DS3234_WrSRAM(SYS_SLEEPPER_PTR, sizeof(ulong), (ulong*)&val_ulong);
    val_ulong = SYS_PUMPPER_VAL;            DS3234_WrSRAM(SYS_PUMPPER_PTR, sizeof(ulong), (ulong*)&val_ulong);

    printf("\n  System Default Values loaded into RTC SRAM!\n");

}

void GetIsusVarsFromNVRAM( void )
{

    //load ISUS UCHAR vals from the DS3234 SRAM
    DS3234_RdSRAM(ISUS_TCOMPMODE_PTR, sizeof(uchar), (uchar*)&isus_tcomp_mode);
    DS3234_RdSRAM(ISUS_SALFITMODE_PTR, sizeof(uchar), (uchar*)&isus_salfit_mode);
    DS3234_RdSRAM(ISUS_WARMSHUT_PTR, sizeof(uchar), (uchar*)&isus_warm_shut);

    //load ISUS USHORT vals from the DS3234 SRAM
    DS3234_RdSRAM(ISUS_INTSCANS_PTR, sizeof(ushort), (ushort*)&isus_scan_num);
    DS3234_RdSRAM(ISUS_DCSCANS_PTR, sizeof(ushort), (ushort*)&isus_dark_num);
    DS3234_RdSRAM(ISUS_PRESCANS_PTR, sizeof(ushort), (ushort*)&isus_pre_scans);
    DS3234_RdSRAM(ISUS_PIXELBEG_PTR, sizeof(ushort), (ushort*)&isus_pixel_beg);
    DS3234_RdSRAM(ISUS_PIXELEND_PTR, sizeof(ushort), (ushort*)&isus_pixel_end);
    DS3234_RdSRAM(ISUS_FITCONCS_PTR, sizeof(ushort), (ushort*)&isus_fit_concs);
    DS3234_RdSRAM(ISUS_BSLNMODL_PTR, sizeof(ushort), (ushort*)&isus_bsl_model);
    DS3234_RdSRAM(ISUS_REFLIMIT_PTR, sizeof(ushort), (ushort*)&isus_ref_limit);
    DS3234_RdSRAM(ISUS_REFSAMPS_PTR, sizeof(ushort), (ushort*)&isus_ref_samples);
    DS3234_RdSRAM(ISUS_DATPIXELBEG_PTR, sizeof(ushort), (ushort*)&isus_datpixel_beg);
    DS3234_RdSRAM(ISUS_DATPIXELEND_PTR, sizeof(ushort), (ushort*)&isus_datpixel_end);
    DS3234_RdSRAM(ISUS_LAMPWARM_PTR, sizeof(ushort), (ushort*)&isus_warm_lamp);
    DS3234_RdSRAM(ISUS_CLEANUV_PTR, sizeof(ushort), (ushort*)&isus_clean_lamp);

    //load ISUS INT vals from the DS3234 SRAM
    DS3234_RdSRAM(ISUS_LASTERROR_PTR, sizeof(int), (int*)&isus_last_error);

    //load ISUS ULONG vals from the DS3234 SRAM
    DS3234_RdSRAM(ISUS_ERRORCTR_PTR, sizeof(ulong), (ulong*)&isus_error_ctr);
    DS3234_RdSRAM(ISUS_LASTERRTIME_PTR, sizeof(ulong), (ulong*)&isus_error_time);
    DS3234_RdSRAM(ISUS_SAMPLECTR_PTR, sizeof(ulong), (ulong*)&isus_sample_ctr);
    DS3234_RdSRAM(ISUS_LASTSAMPTIME_PTR, sizeof(ulong), (ulong*)&isus_sample_time);
    DS3234_RdSRAM(ISUS_PERIOD_PTR, sizeof(ulong), (ulong*)&isus_spec_period);
    DS3234_RdSRAM(ISUS_SAMPSPERDC_PTR, sizeof(ulong), (ulong*)&isus_scans_per_dc);
    DS3234_RdSRAM(ISUS_SAMPSPERUV_PTR, sizeof(ulong), (ulong*)&isus_samps_per_clean);

    //load ISUS FLOAT vals from the DS3234 SRAM
    DS3234_RdSRAM(ISUS_NO3SPAN_PTR, sizeof(float), (float*)&no3_span);
    DS3234_RdSRAM(ISUS_NO3OFFSET_PTR, sizeof(float), (float*)&no3_offset);

}


void SetIsusDefaultsIntoNVRAM( void )
{
    uchar val_uchar;
    ushort val_ushort;
    int val_int;
    ulong val_ulong;
    float val_float;

    //load ISUS UCHAR vals into the DS3234 SRAM
    val_uchar = ISUS_TCOMPMODE_VAL;         DS3234_WrSRAM(ISUS_TCOMPMODE_PTR, sizeof(uchar), (uchar*)&val_uchar);
    val_uchar = ISUS_SALFITMODE_VAL;        DS3234_WrSRAM(ISUS_SALFITMODE_PTR, sizeof(uchar), (uchar*)&val_uchar);
    val_uchar = ISUS_WARMSHUT_VAL;          DS3234_WrSRAM(ISUS_WARMSHUT_PTR, sizeof(uchar), (uchar*)&val_uchar);

    //load ISUS USHORT vals into the DS3234 SRAM
    val_ushort = ISUS_INTSCANS_VAL;         DS3234_WrSRAM(ISUS_INTSCANS_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = ISUS_DCSCANS_VAL;          DS3234_WrSRAM(ISUS_DCSCANS_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = ISUS_PRESCANS_VAL;         DS3234_WrSRAM(ISUS_PRESCANS_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = ISUS_PIXELBEG_VAL;         DS3234_WrSRAM(ISUS_PIXELBEG_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = ISUS_PIXELEND_VAL;         DS3234_WrSRAM(ISUS_PIXELEND_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = ISUS_FITCONCS_VAL;         DS3234_WrSRAM(ISUS_FITCONCS_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = ISUS_BSLNMODL_VAL;         DS3234_WrSRAM(ISUS_BSLNMODL_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = ISUS_REFLIMIT_VAL;         DS3234_WrSRAM(ISUS_REFLIMIT_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = ISUS_REFSAMPS_VAL;         DS3234_WrSRAM(ISUS_REFSAMPS_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = ISUS_DATPIXELBEG_VAL;      DS3234_WrSRAM(ISUS_DATPIXELBEG_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = ISUS_DATPIXELEND_VAL;      DS3234_WrSRAM(ISUS_DATPIXELEND_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = ISUS_LAMPWARM_VAL;         DS3234_WrSRAM(ISUS_LAMPWARM_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = ISUS_CLEANUV_VAL;          DS3234_WrSRAM(ISUS_CLEANUV_PTR, sizeof(ushort), (ushort*)&val_ushort);

    //load ISUS INT vals into the DS3234 SRAM
    val_int = ISUS_LASTERROR_VAL;           DS3234_WrSRAM(ISUS_LASTERROR_PTR, sizeof(int), (int*)&val_int);

    //load ISUS ULONG vals into the DS3234 SRAM
    val_ulong = ISUS_ERRORCTR_VAL;          DS3234_WrSRAM(ISUS_ERRORCTR_PTR, sizeof(ulong), (ulong*)&val_ulong);
    val_ulong = ISUS_LASTERRTIME_VAL;       DS3234_WrSRAM(ISUS_LASTERRTIME_PTR, sizeof(ulong), (ulong*)&val_ulong);
    val_ulong = ISUS_SAMPLECTR_VAL;         DS3234_WrSRAM(ISUS_SAMPLECTR_PTR, sizeof(ulong), (ulong*)&val_ulong);
    val_ulong = ISUS_LASTSAMPTIME_VAL;      DS3234_WrSRAM(ISUS_LASTSAMPTIME_PTR, sizeof(ulong), (ulong*)&val_ulong);
    val_ulong = ISUS_PERIOD_VAL;            DS3234_WrSRAM(ISUS_PERIOD_PTR, sizeof(ulong), (ulong*)&val_ulong);
    val_ulong = ISUS_SAMPSPERDC_VAL;        DS3234_WrSRAM(ISUS_SAMPSPERDC_PTR, sizeof(ulong), (ulong*)&val_ulong);
    val_ulong = ISUS_SAMPSPERUV_VAL;        DS3234_WrSRAM(ISUS_SAMPSPERUV_PTR, sizeof(ulong), (ulong*)&val_ulong);

    //load ISUS FLOAT vals into the DS3234 SRAM
    val_float = ISUS_NO3SPAN_VAL;           DS3234_WrSRAM(ISUS_NO3SPAN_PTR, sizeof(float), (float*)&val_float);
    val_float = ISUS_NO3OFFSET_VAL;         DS3234_WrSRAM(ISUS_NO3OFFSET_PTR, sizeof(float), (float*)&val_float);

    printf("\n  ISUS Default Values loaded into RTC SRAM!\n");

}


void GetDuraVarsFromNVRAM( void )
{

    //load DURA UCHAR vals from the DS3234 SRAM
    DS3234_RdSRAM(DURA_LEDENABLE_PTR, sizeof(uchar), (uchar*)&dura_led_enable);

    //load DURA USHORT vals from the DS3234 SRAM
    DS3234_RdSRAM(DURA_ATODREADINGS_PTR, sizeof(ushort), (ushort*)&dura_atod_readings);
    DS3234_RdSRAM(DURA_ATODDELAY_PTR, sizeof(ushort), (ushort*)&dura_atod_delay);

    //load DURA INT vals from the DS3234 SRAM
    DS3234_RdSRAM(DURA_LASTERROR_PTR, sizeof(int), (int*)&dura_last_error);

    //load DURA ULONG vals from the DS3234 SRAM
    DS3234_RdSRAM(DURA_ERRORCTR_PTR, sizeof(ulong), (ulong*)&dura_error_ctr);
    DS3234_RdSRAM(DURA_LASTERRTIME_PTR, sizeof(ulong), (ulong*)&dura_error_time);
    DS3234_RdSRAM(DURA_SAMPLECTR_PTR, sizeof(ulong), (ulong*)&dura_sample_ctr);
    DS3234_RdSRAM(DURA_LASTSAMPTIME_PTR, sizeof(ulong), (ulong*)&dura_sample_time);
    DS3234_RdSRAM(DURA_SAMPSPERCAL_PTR, sizeof(ulong), (ulong*)&dura_samps_per_cal);

}

void SetDuraDefaultsIntoNVRAM( void )
{
    uchar val_uchar;
    ushort val_ushort;
    int val_int;
    ulong val_ulong;

    //load DURA UCHAR vals into the DS3234 SRAM
    val_uchar = DURA_LEDENABLE_VAL;         DS3234_WrSRAM(DURA_LEDENABLE_PTR, sizeof(uchar), (uchar*)&val_uchar);

    //load DURA USHORT vals into the DS3234 SRAM
    val_ushort = DURA_ATODREADINGS_VAL;     DS3234_WrSRAM(DURA_ATODREADINGS_PTR, sizeof(ushort), (ushort*)&val_ushort);
    val_ushort = DURA_ATODDELAY_VAL;        DS3234_WrSRAM(DURA_ATODDELAY_PTR, sizeof(ushort), (ushort*)&val_ushort);

    //load DURA INT vals into the DS3234 SRAM
    val_int = DURA_LASTERROR_VAL;           DS3234_WrSRAM(DURA_LASTERROR_PTR, sizeof(int), (int*)&val_int);

    //load DURA ULONG vals into the DS3234 SRAM
    val_ulong = DURA_ERRORCTR_VAL;          DS3234_WrSRAM(DURA_ERRORCTR_PTR, sizeof(ulong), (ulong*)&val_ulong);
    val_ulong = DURA_LASTERRTIME_VAL;       DS3234_WrSRAM(DURA_LASTERRTIME_PTR, sizeof(ulong), (ulong*)&val_ulong);
    val_ulong = DURA_SAMPLECTR_VAL;         DS3234_WrSRAM(DURA_SAMPLECTR_PTR, sizeof(ulong), (ulong*)&val_ulong);
    val_ulong = DURA_LASTSAMPTIME_VAL;      DS3234_WrSRAM(DURA_LASTSAMPTIME_PTR, sizeof(ulong), (ulong*)&val_ulong);
    val_ulong = DURA_SAMPSPERCAL_VAL;       DS3234_WrSRAM(DURA_SAMPSPERCAL_PTR, sizeof(ulong), (ulong*)&val_ulong);

    printf("\n  DuraFET Default Values loaded into RTC SRAM!\n");
}


int IOPwrUp( void )
{

    RegIO_WrDefaults();
    I2C_Init();
    PCAL6416_Init();
    INA219_Init(D_RANGE, D_GAIN, D_BUS_ADC, D_SHUNT_ADC, D_MODE, D_CAL_REG);
    DS1683_Init();

    sys_pump_pwr  = FALSE;
    isus_spec_pwr = isus_lamp_pwr = isus_ref_pwr = FALSE;
    dura_atod_pwr = FALSE;

    //if( sys_watchdog_enable == TRUE )
    //  EnableWatchDog();
    //else
    //  DisableWatchDog();

    TUInit( calloc, free );

    EIAAssertTXX( TRUE );   // Assert CF2 RTS output (CTS input to APF, etc)

    return( OK );

}

int IOPwrDown( void )
{

    if( dat_file != FNULL )
        fclose(dat_file);
    if( err_file != FNULL )
        fclose(err_file);

    PCAL6416_Reset();
    RegIO_WrWord( 0x0000 );

    TURelease();

    CheckRtcDelta();

    //if( sys_watchdog_enable == TRUE )
    //  DisableWatchDog();

    EIAAssertTXX( FALSE );  // Unassert CF2 RTS output (CTS input to APF, etc)

    return( OK );

}

void InitWatchDogKeepAlive( void )
{
//short pwmper;

    // Initialize TPU channel for 400Hz watchdog keep alive

//    pwmper = TPUGetTCR1() / 400;                   // 400Hz output w/4MHz TC1 clk rate
//    CHANPRIOR(WDOG_KEEPALIVE_CH, Disabled);        // disable processing TPU ch
//    *CIER &= ~(1 << WDOG_KEEPALIVE_CH );           // don't want interrupts enabled
//    FUNSEL(WDOG_KEEPALIVE_CH, PWM);                // channel function select
//    PRAM[WDOG_KEEPALIVE_CH][0] = OutputTCR1 | NoChangePAC | ForceLow;
//    PRAM[WDOG_KEEPALIVE_CH][2] = pwmper/2;         // hi period
//    PRAM[WDOG_KEEPALIVE_CH][3] = pwmper;           // period
//    HOSTSERVREQ(WDOG_KEEPALIVE_CH, 2);             // issue request
//    CHANPRIOR(WDOG_KEEPALIVE_CH, LowPrior);        // set channel priority
//   while(HOSTSERVSTAT(WDOG_KEEPALIVE_CH) & 3)     // await reply
//      ;

}

void EnableWatchDog( void )
{

//    InitWatchDogKeepAlive();             //Enable WatchDog KeepAlive signal

//    delay( TICKS_PER_SECOND/2 );

//    Set_Bit_WrReg1( WATCHDOG_ENABLE );   //set WatchDog latch bit

//    Set_Bit_WrReg1( WATCHDOG_GATE );     //clk the data into the latch
//    Clear_Bit_WrReg1( WATCHDOG_GATE );

//    Clear_Bit_WrReg1( WATCHDOG_ENABLE ); //clr WatchDog latch bit

}

void DisableWatchDog( void )
{

//    Clear_Bit_WrReg1( WATCHDOG_ENABLE ); //clr WatchDog latch bit

//    Set_Bit_WrReg1( WATCHDOG_GATE );     //clk the data into the latch
//    Clear_Bit_WrReg1( WATCHDOG_GATE );

//    TPUSetPin(WDOG_KEEPALIVE_CH, 0);     //Disable Watchdog KeepAlive signal

}

void SysShutDown( void )
{

    IOPwrDown();
    SCIRxFlush();
    SCIRxSetBuffered( false );

    LPStopCSE( FullStop );

}



#endif
