/*
 * test.c
 *
 *  Created on: Apr 23, 2021
 *      Author: Thom Maughan, tm@mbari.org
 */
#include "System.h"
#include "test.h"
//#include "Init.h"
#include "user_IO.h"
#include "SDCard.h"
#include "fatfs/src/ff.h"
#include "ADS1248_iso.h"
#include "ADS1248_noniso.h"
#include "Sleep.h"
#include "AUX1.h"
#include "AUX2.h"
#include "uartstdio.h"  // User local version with larger RX buffer
#include "board_util.h"
#include "Adc_onchip.h"






//Test Menu -- v3.5.5 MpHOx revB pre-release, DO NOT DEPLOY this version of firmware
//1 -- Battery and Board Sensors
void display_battery_boardSensors(void);
//2 -- ISFET, References and Thermistor volts
void display_ph_ref_therm_volts(void);
//3 -- Calc pH using Int Ref and Thermistor
void display_calcd_pH_intRef_therm(void);
//4 -- Calc pH using Ext Ref, Thermistor and Salinity
void display_calcd_pH_extRef_therm_sal(float Salinity);
//5 -- Communicate with Instrument
void communicate_with_instruments(void);
//6 -- Power Output Tests for AUXx and Pump
int power_output_tests(void);
//8 -- Test 24bit ADC and MUX channels
void test_24bit_adc_mux(void);
//D -- Data Header print
extern void print_data_header(void);
//M -- Meta Data print
extern void print_metadata_header(const time_t currentTime);
//Q -- QA and setup after board manufacture
int board_qa_and_init(int initFlg);


int test_sampling_mcap(void);

extern int optode_stateMachine(int optodeState);
extern void optode_serial_analyzer(void);
extern void optode_comm_test(void);



/*
 * Global System variables.
 */
extern uint32_t adc_data[];
extern char Optode_buff[];
extern float fBiasPos;  // used for read_bias_bat();
extern float fBiasNeg;

extern unsigned char prnBuf[];

void test_buffer_filewrite(void)
{
    uint32_t indx;
    uint32_t bufPtr;

    FIL fileObject;
    FRESULT fresult;
    UINT bw;


    for(indx=0; indx<1024; indx++)
    {
        prnBuf[indx] = 0;
    }

    bufPtr = 0;
    for(indx=0; indx<128; indx++)
    {
        bufPtr += sprintf(&prnBuf[bufPtr], "hello %03u %04u ", indx, bufPtr);
        if(bufPtr >= 1000)
            break;
    }

    // Open a file
    fresult = f_open(&fileObject, "testest.txt", FA_READ |FA_WRITE |FA_OPEN_ALWAYS);
    if(fresult != FR_OK)
    {
        uprintf("f_open error: %s\n", StringFromFresult(fresult));
        error_store("f_open error");
    }

    // Seek to the end, to append our file
    fresult = f_lseek(&fileObject, fileObject.fsize);
    if(fresult != FR_OK)
    {
        uprintf("f_lseek error: %s\n", StringFromFresult(fresult));
        error_store("f_lseek error");
    }


    // Write the buffer to the SD card
    fresult = f_write( &fileObject, prnBuf, strlen(prnBuf), (UINT*)&bw);
    if(fresult != FR_OK)
    {
        uprintf("f_write error: %s\n", StringFromFresult(fresult));
    }


    // Close the file
    fresult = f_close( &fileObject );
    if(fresult != FR_OK)
    {
        uprintf("f_close error: %s\n", StringFromFresult(fresult));
        error_store("f_close error");
    }

#ifdef OLDCODE
    fast_sample(0);

    // WHAT IS THIS DOING?  IS IT JUST DEBUG??  Thom 16 Apr 2021
    indx = 0;
    if(sys_data.data_cfg[CFG_SAMPLE_NUM] > 0)       { indx += sprintf(&sampBuf[indx], "#%07d\t", sys_data.current_sample);  } //0
    if(sys_data.data_cfg[CFG_TIMEDATE] > 0)         { indx += sprintf(&sampBuf[indx], "%s\t", timestamp);                    } //1
    if(sys_data.data_cfg[CFG_ENV_BAT_VOLT] > 0)     { indx += sprintf(&sampBuf[indx], "%5.2f\t", main_batt_volt);            } //2
    if(sys_data.data_cfg[CFG_ENV_BRD_TEMP] > 0)     { indx += sprintf(&sampBuf[indx], "%5.2f\t", con_temp);                  } //3
    if(sys_data.data_cfg[CFG_ENV_HUMIDITY] > 0)     { indx += sprintf(&sampBuf[indx], "%3u\t", uiHumidity);                  } //4
    if(sys_data.data_cfg[CFG_BIAS_BAT_VOLT] > 0)    { indx += sprintf(&sampBuf[indx], "%5.2f\t", fBias_batt_volt);           } //5
    if(sys_data.data_cfg[CFG_V_THERMISTOR] > 0)     { indx += sprintf(&sampBuf[indx], "%8.6f\t", Vtherm);                    } //6
    if(sys_data.data_cfg[CFG_V_THERMISTOR_STD] > 0) { indx += sprintf(&sampBuf[indx], "%8.6f\t", Vtherm_std);                } //7
    if(sys_data.data_cfg[CFG_V_INT_OFFSET] > 0)     { indx += sprintf(&sampBuf[indx], "%8.6f\t", IntRef_WithOffset);         } //8
    if(sys_data.data_cfg[CFG_V_INT_OFFSET_STD] > 0) { indx += sprintf(&sampBuf[indx], "%8.6f\t", IntRef_WithOffset_std);     } //9
    if(sys_data.data_cfg[CFG_V_EXT_OFFSET] > 0)     { indx += sprintf(&sampBuf[indx], "%8.6f\t", ExtRef_WithOffset);        } //10
    if(sys_data.data_cfg[CFG_V_EXT_OFFSET_STD] > 0) { indx += sprintf(&sampBuf[indx], "%8.6f\t", ExtRef_WithOffset_std);    } //11

    if(sys_data.data_cfg[CFG_V_INT] > 0)            { indx += sprintf(&sampBuf[indx], "%8.6f\t", Vint);                      } //12
    if(sys_data.data_cfg[CFG_V_INT_STD] > 0)        { indx += sprintf(&sampBuf[indx], "%8.6f\t", Vint_std);                  } //13
    if(sys_data.data_cfg[CFG_V_EXT] > 0)            { indx += sprintf(&sampBuf[indx], "%8.6f\t", Vext_ref);                  } //14
    if(sys_data.data_cfg[CFG_V_EXT_STD] > 0)        { indx += sprintf(&sampBuf[indx], "%8.6f\t", Vext_std);                  } //15
    if(sys_data.data_cfg[CFG_V_COUNTER_ELECT] > 0)  { indx += sprintf(&sampBuf[indx], "%8.6f\t", CounterElectrodeVoltage);   } //16
    if(sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] > 0)  { indx += sprintf(&sampBuf[indx], "%8.6f\t", CounterElectrodeVoltage_std);  } //17
    if(sys_data.data_cfg[CFG_I_COUNTER] > 0)        { indx += sprintf(&sampBuf[indx], "%8.6f\t", CounterElectrodeCurrent);   } //18
    if(sys_data.data_cfg[CFG_I_SUBSTRATE] > 0)      { indx += sprintf(&sampBuf[indx], "%8.6f\t", SubstrateCurrent);          } //19
    if(sys_data.data_cfg[CFG_CALC_TEMP] > 0)        { indx += sprintf(&sampBuf[indx], "%6.3f\t", TC);                        } //20
    if(sys_data.data_cfg[CFG_EST_PH_INT] > 0)       { indx += sprintf(&sampBuf[indx], "%7.6f\t", pHint);                     } //21
    if(sys_data.data_cfg[CFG_OPTODE] > 0)           { indx += sprintf(&sampBuf[indx], "%s\t", optode.buf);                  } //22
    sprintf(&sampBuf[indx], "\r\n");

    for(indx=0; indx<SIZEOF_SAMPBUF; indx++)
    {
        if(sampBuf[indx] == 0)
            sampBuf[indx] = 'x';      // DEBUG - make this a space
        if(sampBuf[indx] == 0x0a || sampBuf[indx] == 0x0d)
            break;
    }

    //uprintf("%s", sampBuf);      // debug
#endif
}

/*************************************************************************************************************************
 * Test menu.
 *************************************************************************************************************************/
void test_menu(void)
{
    char input;
    //float ADCResult, TC, fVbat, fOnChipTemp, fExtTemp, fPressure;
    //float ADCResult2, ADCResult3, ADCResult4, ADCResult5;
    //uint32_t  uiPressure, uiHumidity, uiWater;


    int response;
    //int breakflg;
    time_t timer;
    time_t current_time;



    //uint32_t indx = 0;

    // Test menu loop
    while(1)
    {
        uprintf("\n\n\nTest Menu -- %s\n", VERSION);

        uprintf("1 -- Battery and Board Sensors\n");  //uprintf("1 -- Internal Environment Sensors\n");  //uprintf("1 -- Display Battery, Internal Temp, Pressure\n");
        uprintf("2 -- ISFET, References and Thermistor volts\n");   // was Display pH Sensor Data
        uprintf("3 -- Calc pH using Int Ref and Thermistor\n");   //uprintf("3 -- Communicate with Instrument\n");
        uprintf("4 -- Calc pH using Ext Ref, Thermistor and Salinity\n");
        uprintf("5 -- Communicate with Instrument\n");
        uprintf("6 -- Power Output Tests for AUXx and Pump\n");
        //uprintf("7 -- spare\n");
        uprintf("8 -- Test 24bit ADC and MUX channels\n");
        uprintf("D -- Data Header print\n");
        uprintf("M -- Meta Data print\n");
        uprintf("Q -- QA and setup after board manufacture\n");
        uprintf("F -- test buffer based sdcard write\n");
        uprintf("\n");

        uprintf("9 -- Exit to Main Menu\n");
        uprintf("\nEnter Selection: ");

        UARTFlushRx();
        timer = 30000 + ROM_HibernateRTCGet();  //30 second

        while(1)
        {
            if( UARTRxBytesAvail() )
            {
                input = get_key();
                break;
            }

            if( timer <= ROM_HibernateRTCGet() ) sleep(IDLE, 0);
        }

        switch(input)
        {
            case 'f':
                test_buffer_filewrite();
                break;


            case '1':
                display_battery_boardSensors();  // defd in test.c
                break;


            case '2':
                //uprintf("2 -- ISFET, References and Thermistor volts\n");  //uprintf("2 -- Display pH Sensor Data\n");
                display_ph_ref_therm_volts();     // defd in test.c  // taken from 8->S sample all channels
                break;

            case '3':
                //Calculate pH with internal reference and temperature
                display_calcd_pH_intRef_therm();   // defd in test.c
                break;


            case '4':
                //Calculate pH with ext ref and temp, requires salinity
                //calc_pHext(float Eext, float TC, float salt);
                display_calcd_pH_extRef_therm_sal(35.0);     // defd in test.c   // pass in Salinity (future, this will be read from conductivity or ctd
                //calc_pHext(float Eext, float TC, float salt);
                break;

            case '5':
                communicate_with_instruments();  // defd in test.c   // peripheral instruments such as microcat, optode
                break;

            case '6':
                // exercise power outputs
                uprintf("Power Output Tests for AUXx and Pump\n\n");
                power_output_tests();  // defd in test.c
                break;

            case '8':
                test_24bit_adc_mux(); // defd in test.c
                break;

            case 'd':
            case 'D':
                print_data_header();     // defd in system.c
                break;

            case 'm':
            case 'M':
                current_time = ROM_HibernateRTCGet();
                print_metadata_header(current_time);
                break;

            case 'Q':
            case 'q':
                 //PP2  THOMMY
                //uprintf("QA for new board - will wipe out ALL config and setup data, do you wish to proceed?   ");

                response = yesOrNoMenuChoice("\n\nQA for new board, this will wipe out ALL config and setup data, do you wish to proceed? [N]? ", NO);
                if(response == YES)
                {
                    board_qa_and_init(1); // pass in 1 for init of Flash mem
                }

                break;

            default: //Exit
                uprintf("\nPress enter to bring up main menu\n");
                return;

        }
    }
}


//1 -- Battery and Board Sensors
void display_battery_boardSensors(void)
{
    unsigned int loopCnt;
    unsigned int uiWater;
    unsigned int uiPressure;
    unsigned int uiHumidity;
    float fVbat;
    float fOnChipTemp;
    float fExtTemp;
    float fPressure;
    float ADCResult;
    char  input;

#if BOARD_MFET >= 1 || BOARD_MPHOX >= 1

    //display_battery_boardSensors();
    //uprintf("1 -- Battery and Board Sensors\n");   // uprintf("1 -- Internal Environment Sensors\n");
    //uprintf("\nDEBUG NOTE: Tested on MpHOx, MFET v2\n\n");

    loopCnt = 0;
    pwr_env_mon_on();       // ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_7, 0xFF);    // Turn on Load switch for ENV monitoring
    pwr_vbat_mon_on();      // ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_6, 0xFF);   // Turn on VBat monitoring

    pwr_iso_on();  // was ROM_GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_2, 0xFF);        // Iso-power On
    openADS1248_iso();
    ROM_SysCtlDelay(MILLISECOND * 100);
    ADS1248_gpio_init();        // for reading bias bat

    uprintf("iso power on\n");

    adc_onchip_init();
    ROM_SysCtlDelay(MILLISECOND * 100);          // 10ms gives 5.06v (12.02v input), 20ms gives 7.88, 50 ms gives 11.07, 70 ms gives 11.65, 100 ms gives 11.93
    while(1)
    {
        //float ADCResult, TC, ADCvolts, fVbat, fOnChipTemp, fExtTemp;
        //uint32_t  TempValueC, uiPressure, uiHumidity, uiWater;

        read_all_adc();

        fVbat = (float)(adc_data[0]*100);
        //fVbat /= 18687.707;
        fVbat /= 18193.77;

        // onchip temperature (formulae from TI example code)
        //TempValueC = (uint32_t)(147.5 - ((75.0*3.3 *(float)adc_data[7])) / 4096.0);
        fOnChipTemp = (147.5 - ((75.0*3.3 *(float)adc_data[7])) / 4096.0);

        // LM60 temperature sensor - formula from datasheet / schematic (good job Scott Jensen)
        // Vout = (+6.25 mV/C * T) + 424mV   =>  T = ((ADCcnt * 0.80566) - 424) / 6.25
        fExtTemp = ((((float)adc_data[4])*0.80566) - 424) / 6.25;

        // Water sensor - convert to percent
        uiWater = (adc_data[3] * 100) / 2596;    // NOTE: Water voltage adc cnts is 2622 when pulled to 3.3v  (measured at 2.1092v on test point with agilent u1272a meter)
        if(uiWater > 100)
            uiWater = 100;

        // Humidity
        uiHumidity = (adc_data[2] * 100) / 3284;

        // Pressure
        uiPressure = adc_data[1];
        fPressure = (float)uiPressure;   //2020x = 14.70psi     (From scotts schematic0.33V = 0psia, 3.0V = 30psia)
        fPressure *= 0.0072772;

        //----------------

        // code taken from read_bias_bat();  (dont want to open and close, init and reinit

        ADS1248_gpio3(0);
        ROM_SysCtlDelay(MILLISECOND * 10);
        ADCResult = pollADS1248_iso(7, 6, 5, 1, 20);    // throw away first reading
        ROM_SysCtlDelay(MILLISECOND * 100);
        ADCResult = pollADS1248_iso(7, 6, 5, 1, 20);
        fBiasNeg = ADCResult*2.0;

        ADS1248_gpio3(1);
        ROM_SysCtlDelay(MILLISECOND * 10);
        ADCResult = pollADS1248_iso(7, 6, 5, 1, 20);    // throw away first reading
        ROM_SysCtlDelay(MILLISECOND * 100);
        ADCResult = pollADS1248_iso(7, 6, 5, 1, 20);
        fBiasPos = ADCResult*2.0;

        //----------------



        //uprintf("Bat = %6.2fV VBAT=%u VPRESS=%3u  VHUM=%3u  VWATER=%3u   VXTEMP=%3u   VBAT=%3u, VBAT=%u, VTEMP=%u  i=%u\r", fVbat, adc_data[0], adc_data[1], adc_data[2], adc_data[3], adc_data[4], adc_data[5], adc_data[6], TempValueC, indx++);
        //uprintf("Bat=%6.2fV  Pressure=%5.1f  Humidity=%3u  Water=%3u  BoardTemp=%5.1fC (%u)   ChipTemp=%5.1fC   bias+=%5.2fV   bias-=%5.2fV   i=%u\r", fVbat, fPressure, uiHumidity, uiWater, fExtTemp, adc_data[4], fOnChipTemp, fBiasPos, fBiasNeg, indx++);
        //uprintf("Bat=%6.2fV  Humidity=%3u  Water=%3u  BoardTemp=%5.1fC   chipTemp=%5.1fC   bias+=%5.2fV   bias-=%5.2fV   i=%u\r", fVbat, uiHumidity, uiWater, fExtTemp, fOnChipTemp, fBiasPos, fBiasNeg, loopCnt++);
        uprintf("Bat = %6.2fV   Humidity = %3u  Water = %3u   BoardTemp = %5.1fC   bias(+,-) = %5.2fV, %5.2fV   i=%u\r", fVbat, uiHumidity, uiWater, fExtTemp, fBiasPos, fBiasNeg, loopCnt++);


        ROM_SysCtlDelay(MILLISECOND * 500);

        if( UARTRxBytesAvail() )
        {
            input = get_key();
            break;    // break from while(1)
        }
    }

    closeADS1248_iso();

    pwr_vbat_mon_off();     // was ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_6, 0x00);   // Turn off VBat monitoring
    pwr_iso_off();      // ROM_GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_2, 0x00);        // Iso-power Off
    uprintf("iso power off, vbat monitoring off\n");
#endif

#if BOARD_NANOFET >= 1
    uprintf("\nTHOM TODO: Test menu item 1 needs to be implemented for nanoFET (need a board)\n\n");

#endif

}



//2 -- ISFET, References and Thermistor volts
void display_ph_ref_therm_volts(void)
{
    char input;
    float ADCResult;
    uint32_t indx = 0;
    //time_t timer;
    int sampleCnt;
    unsigned long sampTime;
    unsigned long nextTime;
    unsigned long sampDelay;

    // this code taken from void test_24bit_adc_mux(void), S stream all channels

#if BOARD_MFET >= 1 || BOARD_MPHOX == 1


    uprintf("\nOpen isolated ADS1248 and ADG1609 mux\n\n");
    ROM_SysCtlDelay(MILLISECOND * 100);
    //profile_on(1);      // THOM debug
    openADS1248_iso();      // 22msec
    //profile_off(1);

    ADS1248_gpio_init();    // defd in ADS1248_iso.c
#endif

#if BOARD_NANOFET >= 1
    uprintf("\nOpen nFET ADS1248 and ADG1609 mux\n\n");
    ROM_SysCtlDelay(MILLISECOND * 100);

    openADS1248_iso();      // 22msec  nanoFET is PD0 to PD3, MFET iso is PH0 to PH3


    ADS1248_gpio_init();    // defd in ADS1248_iso.c
#endif


    sampleCnt = 10;
    sampDelay = 200L;   // 500 is 5 seconds using 10ms tick


    sampDelay = 500L;

#if BOARD_MFET >= 1 || BOARD_MPHOX >= 1
//SampNum SampTime Vbatt_main TC_cont Humidity PresHousing Vtherm Vtherm_std VrsiBiased VrsiBiased_std VrseBiased VrseBiased_std Vrsi Vrsi_std Vrse Vrse_std Vk Vk_std Ik Ib TC_Dfet pHint_est Vbatt_iso or Vbatt_isop Vbatt_ison
// SampNum SampTime Vbatt   TC_brd Humid_brd Pres_brd Vtherm Vtherm_std Vrsi_O Vrsi_O_std Vrse_O Vrse_O_std Vrsi Vrsi_std Vrse Vrse_std Vk Vk_std Ik Ib TC_calc pHint pHext Vbias


    //uprintf("\n\n    Date Time,   Ext Ref Off, Int Ref Off, Ext Ref V, Int Ref V, CntrE Cur, CntrE Volt,  Subst Cur,  Subst Vol,  Thermistor\n");
               //    SampTime,   Vrse_B,  Vrse, Vrsi_B, Vrsi, Ik, Vk,  Ib,  Vb,  Vtherm
               //04/23/21 13:21:08,  -0.069202,  -0.799240,  0.674855,  -0.055217,  0.272939,  -0.500563,  -0.399630,  0.000679,  0.997511
               //04/23/21 13:21:13,  -0.069171,  -0.799247,  0.674859,  -0.055215,  0.272926,  -0.500557,  -0.399631,  0.000677,  0.997506
    uprintf("\n\n    SampTime,         Vrse_B,     Vrse,      Vrsi_B,      Vrsi,      Ik,         Vk,          Ib,        Vb,     Vtherm\n");

#endif

#if BOARD_NANOFET >= 1
    //uprintf("\n\n    Date Time,  Ext Ref V, CntrE Cur, CntrE Volt,  Subst Cur,  Subst Vol,  Thermistor\n");
    uprintf("\n\n    SampTime,   Vrse, Ik, Vk,  Ib,  Vb,  Vtherm   NEEDS WORK\n");
#endif

    indx = 0;
    Timer_10ms(TRUE);       // reset 10msec tick count
    sampleCnt = 1;
    sampTime = 0L;
    nextTime = 0L;
    while(1)
    {
        sampTime = Timer_10ms(FALSE);
        if(sampTime >= nextTime)
        {
            nextTime = sampTime + sampDelay;

            // Date Time,
            printCurrentTime();

            uprintf(", ");
#if BOARD_MFET >= 1 || BOARD_MPHOX >= 1
            //Ext Ref Off,
            ADS1248_gpio_ADG1609_mux(0);
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 5);       // was 20 Chan +1 -5, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f, ", ADCResult); // Ext Ref with Offset

            //Ext Ref V,
            ADS1248_gpio_ADG1609_mux(0);
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +1 -6, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f, ", ADCResult);       // Ext Ref Voltage

            //Int Ref Off,
            ADS1248_gpio_ADG1609_mux(3);
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 5);       // Chan +1 -5, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f, ", ADCResult); // Int Ref with Offset  // uprintf("Int Ref Offset = %.6f V,  Samp = %04u,  Time = %06lu \r\n", ADCResult, indx++, sampTime/100L);

            // Int Ref V,
            ADS1248_gpio_ADG1609_mux(3);
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +1 -6, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f, ", ADCResult);       // Int Ref Voltage
#endif
#if BOARD_NANOFET >= 1
            //Ext Ref V,
            ADS1248_gpio_ADG1609_mux(0);
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 5);       // Chan +1 -6, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f, ", ADCResult);       // Ext Ref Voltage
#endif

            //CntrE Cur,
            ADS1248_gpio_ADG1609_mux(1);
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f, ", ADCResult);       // Counter Electrode Current

            // CntrE Volt,
            ADS1248_gpio_ADG1609_mux(1);
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f, ", ADCResult);       // Counter Electrode Voltage

            // Subst Cur,
            ADS1248_gpio_ADG1609_mux(2);
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f, ", ADCResult);       // Substrate Current

            // Subst Vol,
            ADS1248_gpio_ADG1609_mux(2);
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(5, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f, ", ADCResult);       // Substrate Voltage

            // Thermistor
            ADS1248_gpio_ADG1609_mux(0);        // Not necessary, but consistent
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(0, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f", ADCResult);       // Substrate Voltage

            uprintf("\n");


        }

        if( UARTRxBytesAvail() )
        {
            input = get_key();
            break;    // break from while(1)
        }
    }// while(1)


}




void display_calcd_pH_intRef_therm(void)
{
    float Vtherm, TC, Vint, pHint;


    openADS1248_iso();          // 52 msec
    ADS1248_gpio_init();




    uprintf("First step is to calculate temperature in degrees Centrigrade using Durafet Thermistor\n");
    uprintf("   DuraFet Thermistor Steinhart-Hart coefficients:\n");
    uprintf("     c0 = 340.9819863\n");
    uprintf("     c1 = -9.10257E-05\n");
    uprintf("     c2 = -95.08806667\n");
    uprintf("     c3 = 0.965370274\n");
    uprintf("    Rtherm = 20000 / ((3.3 / V_therm) - 1)\n");
    uprintf("    TempC = c0 + c1*Rtherm + c2*(log10(Rtherm)) + c3*(pow(log10(Rtherm), 3))  Note: pow fxn is log10(Rtherm) cubed\n");
    uprintf("\n");
    uprintf("Next step is to compute pH using internal reference and temperature:\n");

    uprintf("   TempC = TempC + sys_data.TCOffset   TCOffset is user entered in config menu\n");
    uprintf("   TempK = TempC + 273.15\n");
    uprintf("   NernstSlope = (R_=8.31451 * TempK) / F_=96487 * LN10=2.302585\n");
    uprintf("   NernstSlope = TempK / 26,720.7\n");
    uprintf("   Eoint = k0int + dEoINT_dt=-0.0011012 * (TC-25)   k0int is typically -0.4 and is set in Config menu\n");
    uprintf("   Eoint = -0.4 + -0.0011012 * (TempC-25)\n");
    uprintf("   pHint = (Vint - Eoint) / NernstSlope\n");
    uprintf("\n\n");


    // Apply offset to TC; TCOffset is an user input,
    // or derived during pH calibration process.


    ROM_SysCtlDelay(MILLISECOND * 200); // Let electronics settle (was 1000)



    while(!UARTRxBytesAvail())
    {

        //get Vtherm voltage
        Vtherm = pollADS1248_iso(0, 6, 5, 1, 10);      // Chan 0-6, trials = 1, gain = user, sps = 5 Hz (changed to 20 6May2019 TM per YT)
        TC = DuraFET_temp(Vtherm,sys_data.TCOffset);                    // temperature centigrade

        //get Vint voltage
        //ADCResult2 = pollADS1248_iso(1, 6, sys_data.sample_average, sys_data.Vint_gain, sys_data.Vint_sps);
        //ADCResult2 = pollADS1248_iso(1, 6, 5, 1, 5); // normally 1,6,5,1,10
        ADS1248_gpio_ADG1609_mux(3);
        ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
        Vint = pollADS1248_iso(1, 5, 5, 1, 5);  // VintRef_withoffset

#ifdef FROMFASTSAMPLE
        ADS1248_gpio_ADG1609_mux(3);
        ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
        //IntRef_WithOffset = pollADS1248_iso(1, 5, sys_data.sample_average, 1, sys_data.Vint_sps);       // Chan +1 -5, trials = 1, gain = user, sps = 5 Hz
        IntRef_WithOffset = pollADS1248_iso(1, 5, sys_data.sample_average, sys_data.Vint_gain, sys_data.Vint_sps);
        IntRef_WithOffset_std = sys_data.AD24_std;
    }
#endif

        pHint = calc_pHint(Vint, TC);
        uprintf("pH= %2.6f,   Vtherm= %.6f V, Vint= %.6f V, std= %.6f, TC= %2.6f\n", pHint, Vtherm, Vint, sys_data.AD24_std, TC);

        ROM_SysCtlDelay(MILLISECOND*500);
    }
    //close the isolated ADC
    closeADS1248_iso();
}





void display_calcd_pH_extRef_therm_sal(float Salinity)
{
    float Vext;
    float TempC;
    float pHext;
    float Vtherm;
    //char input;


    openADS1248_iso();          // 52 msec
    ADS1248_gpio_init();

    ROM_SysCtlDelay(MILLISECOND * 200); // Let electronics settle (was 1000)
    while(!UARTRxBytesAvail())
    {
        Vtherm = pollADS1248_iso(0, 6, 4, 1, 5);      // Chan 0-6, trials=1, gain=1, sps = 5 Hz (changed to 20 6May2019 TM per YT)

        TempC = DuraFET_temp(Vtherm, sys_data.TCOffset);                    // temperature centigrade

        //

        ADS1248_gpio_ADG1609_mux(0);
        ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
#if BOARD_MFET >= 1 || BOARD_MPHOX >= 1
        Vext = pollADS1248_iso(1, 6, 10, 1, 20);  // 10x50msec= 500msec  trials=10, gain=1, sps=20  THOM note: suggest using 1,6,5,1,10 to allow adc to do better filtering
#endif
#if BOARD_NANOFET >= 1
            // 6 (GND) is not connected on nanoFET
        Vext = pollADS1248_iso(1, 5, 10, 1, 20);  // 10x50msec= 500msec  trials=10, gain=1, sps=20  THOM note: suggest using 1,6,5,1,10 to allow adc to do better filtering
#endif
        // what is the difference between Vext and Vextoff, offset gives reading of 7pH with 0 volts
        // was VextOff = pollADS1248_iso(4, 6, 4, 1, 5);        // trials = 4, gain=1, sps=5   let the sigma delta do the best it can do


        pHext = calc_pHext(Vext, TempC, Salinity);                       // Vext, TC, Sal

        uprintf("pHext= %2.6f,   Vtherm= %.6f V, Vext= %.6f V, std= %.6f, TC= %2.6f\n", pHext, Vtherm, Vext, sys_data.AD24_std, TempC);

        ROM_SysCtlDelay(MILLISECOND*100);
    }

    closeADS1248_iso();
}





void communicate_with_instruments(void)
{
    char input;
    int32_t charInput;
    int optodeState;
    int condoState;
    unsigned int tick1, tick2;

    while(1)
    {
        uprintf("\n\n\nMicroCAT and Optode Peripheral Communication Menu\n");
        uprintf("1 -- MicroCAT\n");
        uprintf("2 -- Optode\n");
        uprintf("4 -- AUX 1\n");
        uprintf("4 -- AUX 2\n");
        uprintf("5 -- Conductivity (on CTD port)\n");
        uprintf("o/O- DEBUG Test Optode Setup\n");
        uprintf("p -- DEBUG Optode serial analyzer\n");
        uprintf("c -- DEBUG Test conductivity\n");
        uprintf("9 -- Exit\n");
        uprintf("\nEnter peripheral device selection[9]: ");

        input = get_key();
        switch(input)
        {
            case '1':  // MicroCAT
                UARTEchoSet(false); // Disable console echo for MicroCAT
                uprintf("\n\n*Press CTRL-X to stop communicating with the MicroCAT.*\n\n");

                //set up the MicroCAT
                microCat_open();

                while(1)
                {
                    //if MicroCAT sent anything, print it out on UART1 (the desktop)
                    if( microCat_rxBytesAvail() )
                    {
                        uprintf( "%c", microCat_getc() );
                    }

                    //if user types anything, send it to the MicroCAT--exit on CTRL-X
                    if(UARTRxBytesAvail())
                    {
                        charInput = UARTgetc();

                        if(charInput == 24)  // ctrl-x
                        {
                            break;
                        }

                        microCat_putc(charInput);
                    }
                }


                //close up the MicroCAT
                microCat_close();
                UARTEchoSet(true);  // Enable console echo
                break;


            case '2': // Optode
                optode_comm_test();     // defd in optode.c
                // THOM TODO: move this to optode.c
                break;


            case '3': // formerly AUX 1 3 -- Conductivity

                uprintf("\n\n*Press CTRL-X to stop communicating with AUX 1.*\n\n");

                AUX1_init();

                UARTFlushTx(0);         //THOM todo, stdio routines need renaming, possibly goofed with optode in main.c and renamed a stdio pass thru
                UARTEchoSet(0);     // Disable echo. Set to 1 to enable
                Open_AUX1();

                while(1)
                {
                    //if AUX1 sent anything, print it out on UART1 (the desktop)
                    if(ROM_UARTCharsAvail(UART2_BASE))
                    {
                        uprintf("%c", ROM_UARTCharGet(UART2_BASE));
                    }

                    //if user types anything, send it to the AUX1--exit on CTRL-X
                    if(UARTRxBytesAvail())
                    {
                        charInput = UARTgetc();

                        if(charInput == 24)
                        {
                            break;
                        }

                        ROM_UARTCharPut(UART2_BASE, charInput);
                    }
                }

                Close_AUX1();
                UARTEchoSet(1);

                break;

            case '4': // AUX 2
                uprintf("\n\n*Press CTRL-X to stop communicating with AUX 2.*\n\n");
                UARTFlushTx(0);
                UARTEchoSet(0);     // Disable echo. Set to 1 to enable
                Open_AUX2();

                while(1)
                {
                    //if AUX1 sent anything, print it out on UART1 (the desktop)
                    if(ROM_UARTCharsAvail(UART5_BASE))
                    {
                        uprintf("%c", ROM_UARTCharGet(UART5_BASE));
                    }

                    //if user types anything, send it to the AUX1--exit on CTRL-X
                    if(UARTRxBytesAvail())
                    {
                        charInput = UARTgetc();

                        if(charInput == 24)
                        {
                            break;
                        }

                        ROM_UARTCharPut(UART5_BASE, charInput);
                    }
                }

                Close_AUX2();
                UARTEchoSet(1);
                break;

            case '5': // formerly AUX 1 3 -- Conductivity

                uprintf("\nNote: smart sensor mode, disable data descriptor text, set flow control to none \n\n");
                uprintf("\n\nCommand procedure to setup Aanderaa Conductivty 5860 for numeric output:\n\n");
                uprintf("set passkey(1)\n");
                uprintf("set mode(smart sensor terminal)\n");
                uprintf("set enable polled mode(yes)\n");
                uprintf("save\n");
                uprintf("\n");
                uprintf("set passkey(1)\n");
                uprintf("set enable text(no)\n");
                uprintf("save\n");
                uprintf("\n");
                uprintf("set passkey(1000)\n");       // response: #
                uprintf("set Flow Control(none)\n");  // response: #
                uprintf("save\n");
                uprintf("\n");

                uprintf("do sample\n");
                uprintf("\nThis should return a sample: \t5860\t29\t-0.003\t16.892\t0.009\t998.802\t1472.435\t\n\n");
                //uprintf("                               \t5860\t29\t-0.003\t16.892\t0.009\t998.802\t1472.435\t\n");
#ifdef GETALLOUTPUT
                get all
                Product Name    5860    29      Conductivity Sensor
                Product Number  5860    29      5860
                Serial Number   5860    29      29
                SW ID   5860    29      1940039
                SW Version      5860    29      5       5       1
                Enable Conductivity     5860    29      Yes
                Node Description        5860    29      Conductivity Sensor #29
                Owner   5860    29
                Mode    5860    29      Smart Sensor Terminal
                Enable Sleep    5860    29      Yes
                Enable Polled Mode      5860    29      Yes
                Enable Text     5860    29      No
                Enable Decimalformat    5860    29      Yes
                Enable Temperature      5860    29      Yes
                Enable Rawdata  5860    29      No
                Enable Derived Parameters       5860    29      Yes
                Interval[s]     5860    29      10.000
                Location        5860    29
                Geographic Position     5860    29      36.8044, -121.7869
                Vertical Position       5860    29
                Reference       5860    29
                Pressure        5860    29      0.000000E+00
#endif



                uprintf("\n\n*Press CTRL-X to stop communicating with the Conductivity instrument.*\n\n");

                //set up the condo
                condo_open();               // power on optode
                ROM_SysCtlDelay(500);       //500 is a guess

                while(1)
                {
                    //if condo sent anything, print it out on the console
                    if( uart2_rxBytesAvail() )      // condo
                    {
                        uprintf( "%c", uart2_getc() );
                    }

                    //if user types anything on console, send it to the condo -- exit on CTRL-X
                    if(UARTRxBytesAvail())
                    {
                        charInput = UARTgetc();

                        if(charInput == 13)     // If user hit carriage return, add a line feed
                        {
                            uart2_putc(charInput);  // CR
                            uart2_putc(10);     // LF
                        }
                        else if(charInput == 24)
                        {
                            break;
                        }
                        else
                        {
                            uart2_putc(charInput);
                        }
                    }
                }

                condo_close();  // Close up the Optode


                break;
            case 'O':
                uprintf("\nLoop Test Optode (statemachine code):  Power Optode with 5v, wait for %, send crlf, wait for !, Send 'do sample', Receive, trim and print Optode sample terminated by #, turn power off \n\n");

                while(1)
                {
                    //if user types anything, send it to the Optode--exit on CTRL-X
                    if(UARTRxBytesAvail())
                    {
                        charInput = UARTgetc();

                        //if(charInput == 24)  // ctrl-x
                            break;

                    }
                    tick1 = Timer_10ms(false);
                    optodeState = 1;
                    optodeState = optode_stateMachine(optodeState);

                    while(1)
                    {
                        optodeState = optode_stateMachine(optodeState);

                        if(optodeState == 0)
                        {
                            tick2 = Timer_10ms(false);
                            optode_print_buf();        // uprintf("\n%s\n", optode.buf);
                            uprintf("\n\nDebug Optode Read Time = %u ms \n", (tick2-tick1)*10);

                            break;
                        }
                    }
                    uprintf("\n\n");
                    ROM_SysCtlDelay(MILLISECOND*300);
                }
                break;

            case 'o':
                uprintf("\nTest Optode (statemachine code):  Power Optode with 5v, wait for %, send crlf, wait for !, Send 'do sample', Receive, trim and print Optode sample terminated by #, turn power off \n\n");
                //int optodeState;
                //unsigned int tick1, tick2;
                tick1 = Timer_10ms(false);
                optodeState = 1;
                optodeState = optode_stateMachine(optodeState);

                while(1)
                {
                    optodeState = optode_stateMachine(optodeState);

                    if(optodeState == 0)
                    {
                        tick2 = Timer_10ms(false);
                        optode_print_buf();        // uprintf("\n%s\n", optode.buf);
                        uprintf("\n\nDebug Optode Read Time = %u ms \n", (tick2-tick1)*10);

                        break;
                    }
                }
                uprintf("\n\n");
                break;

            case 'p':
                optode_serial_analyzer();

                break;

            case 'c':
                uprintf("\nTest Conductivity (statemachine code):  Power Condo with Vbat, wait for %, send crlf, wait for !, Send 'do sample', Receive, trim and print Optode sample terminated by #, turn power off \n\n");
                //int condoState;
                //unsigned int tick1, tick2;
                tick1 = Timer_10ms(false);
                condoState = 1;
                condoState = condo_stateMachine(condoState);

                while(1)
                {
                    condoState = condo_stateMachine(condoState);

                    if(condoState == 0)
                    {
                        tick2 = Timer_10ms(false);
                        condo_print_buf();        // uprintf("\n%s\n", condo.buf);
                        uprintf("\n\nCondo Read Time = %u ms \n", (tick2-tick1)*10);

                        break;
                    }
                }
                uprintf("\n\n");
                break;

            default: //Exit
                return;
        }
    }
}




int power_output_tests(void)
{
    time_t timer;
    char input;
    unsigned int indx;


    // IO Test menu loop
    while(1)
    {
        uprintf("\n\n\nPower Output Test Menu -- %s\n", VERSION);

        uprintf("A/a -- Pump ON/off\n");
        uprintf("B/b -- AUX1 ON/off\n");
        uprintf("C/c -- AUX2 ON/off\n");
        uprintf("D/d -- AUX3 ON/off\n");
        uprintf("E/e -- 5v for Optode ON/off\n");

        uprintf("G/g -- Green LED\n");
        uprintf("R/r -- Red LED\n");

        uprintf("S/s -- Solenoid ON/off\n");
        uprintf("P/p -- Solenoid PWM (hacked for 1KHz)\n");
        uprintf("\n");

        uprintf("9 -- Exit to Test Menu\n");
        uprintf("\nEnter Selection: ");

        UARTFlushRx();
        timer = 30000 + ROM_HibernateRTCGet();

        while(1)
        {
            if( UARTRxBytesAvail() )
            {
                input = get_key();
                break;
            }

            if( timer <= ROM_HibernateRTCGet() ) sleep(IDLE, 0);
        }

        switch(input)
        {
            case 'A':  //J8
                uprintf("A/a -- Pump ON\n");
                pump_on();
                break;
            case 'a':
                uprintf("A/a -- Pump off\n");
                pump_off();
                break;

            case 'B':  //J12
                uprintf("B/b -- AUX1 ON\n");
                AUX1_on();
                break;
            case 'b':
                uprintf("B/b -- AUX1 off\n");
                AUX1_off();
                break;

            case 'C':  // J13
                uprintf("C/c -- AUX2 ON\n");
                AUX2_on();
                break;
            case 'c':
                uprintf("C/c -- AUX2 off\n");
                AUX2_off();
                break;

            case 'D':  //J14
                uprintf("D/d -- AUX3 ON\n");
                //AUX3_on();
                MAP_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3, 0xff);    // Power on AUX3_on();
                break;
            case 'd':
                uprintf("D/d -- AUX3 off\n");
                //AUX3_off();
                MAP_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3, 0x00);    // Power on AUX3_off();
                break;

            case 'E':
                uprintf("E/e -- 5v for Optode ON\n");
                optode_open();
                break;
            case 'e':
                uprintf("E/e -- 5v for Optode off\n");
                optode_close();
                break;

            case 'G':
                uprintf("Green On\n");
                led_green(1);  //ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0xff);
                break;
            case 'g':
                uprintf("Green Off\n");
                led_green(0);   //ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0x00);
                break;
            case 'R':
                uprintf("Red On\n");
                led_red(1);     //ROM_GPIOPinWrite(GPIO_PORTP_BASE, GPIO_PIN_2, 0xff);
                break;
            case 'r':
                uprintf("Red Off\n");
                led_red(0);     //ROM_GPIOPinWrite(GPIO_PORTP_BASE, GPIO_PIN_2, 0x00);
                break;

            case 'S':
                uprintf("Solenoid On PG1,PG3");
                ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_1, 0xff);
                ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_3, 0xff);
                //ROM_SysCtlDelay(MILLISECOND * 9000);
                break;
            case 's':
                uprintf("Solenoid Off PG1,PG3");
                ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_1, 0x00);
                ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_3, 0x00);
                //ROM_SysCtlDelay(MILLISECOND * 2000);
                break;

            case 'P':
            case 'p':
                indx = 0;
                uprintf("PWM 1khz on PG1 (pin54) and PG3 (pin52) - work in progress\n\n");
                while(1)
                {
                    ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_1, 0xff);
                    ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_3, 0x00);
                    ROM_SysCtlDelay(MICROSECOND * 500);
                    ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_1, 0x00);
                    ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_3, 0xff);
                    ROM_SysCtlDelay(MICROSECOND * 500);

                    if(indx++ > 1000)
                    {
                        indx = 0;
                        if( UARTRxBytesAvail() )
                        {
                            input = get_key();
                            uprintf("done with PWM hack test\n");
                            ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_1, 0x00);
                            ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_3, 0x00);
                            break;
                        }
                    }

                }
                break;
            case '9':
                return;

            default:
                //uprintf("key = %c\n\n", input);
                return;

        } // end switch

    }  // end IO Test menu loop
}



void test_24bit_adc_mux(void)
{
    char input;
    float ADCResult;
    uint32_t indx = 0;
    time_t timer;
    int sampleCnt;
    unsigned long sampTime;
    unsigned long nextTime;
    unsigned long sampDelay;
    unsigned long tick1, tick2;


#if BOARD_MFET >= 1 || BOARD_MPHOX == 1
    uprintf("\niso power on, init ADG1609 mux\n\n");

    openADS1248_iso();      // 22msec

    ROM_SysCtlDelay(MILLISECOND * 100);
    ADS1248_gpio_init();    // defd in ADS1248_iso.c
#endif

#if BOARD_NANOFET >= 1
    uprintf("\nOpen nFET ADS1248 and ADG1609 mux\n\n");

    openADS1248_iso();      // 22msec  nanoFET is PD0 to PD3, MFET iso is PH0 to PH3
    ROM_SysCtlDelay(MILLISECOND * 100);

    ADS1248_gpio_init();    // defd in ADS1248_iso.c
#endif

    ROM_SysCtlDelay(MILLISECOND * 10);

    sampleCnt = 10;
    sampDelay = 200L;   // 500 is 5 seconds using 10ms tick

    // Test menu loop
    while(1)
    {
        uprintf("\n\n\nTest pH (24bit ADC Mux) Menu\n");
        uprintf("0 -- pH Voltage with Int Ref\n");
        uprintf("1 -- pH Voltage with Biased Int Ref (pH 7 is 0v)\n");
        uprintf("2 -- pH Voltage with Ext Ref\n");
        uprintf("3 -- pH Voltage with Biased Ext Ref (pH 7 is 0v)\n");
        uprintf("4 -- Ik, Counter Electrode Current\n");  // Counter A and Counter B are current measurement Counter Electrode
        uprintf("5 -- Vk, Counter Electrode Voltage\n");
        uprintf("6 -- Ib, Substrate Current\n");
        uprintf("7 -- Vb, Substrate Voltage\n");
        uprintf("8 -- Vtherm Thermistor Temperature\n");
        uprintf("S -- Sample all channels (0-8)\n");
        uprintf("X -- MCAP sample test (delta 0.0003), assumes Jensonian Analog Board\n");
        uprintf("9 -- Exit to Main Menu\n");
        uprintf("\nEnter Selection: ");


        UARTFlushRx();
        timer = 30000 + ROM_HibernateRTCGet();

        while(1)
        {
            if( UARTRxBytesAvail() )
            {
                input = get_key();
                break;
            }

            if( timer <= ROM_HibernateRTCGet() ) sleep(IDLE, 0);
        }

        switch(input)
        {
        case '0':
            ADS1248_gpio_ADG1609_mux(3);
            indx = 0;
            Timer_10ms(TRUE);       // reset 10msec tick count
            sampleCnt = 10;
            sampTime = 0L;
            nextTime = 0L;
            uprintf("0 -- Vrsi (pH Int Ref) Voltage, Mux = 11,  AIN1+ AIN6-, sampleCnt = %u for 5Hz and %u for 20Hz\n", sampleCnt, sampleCnt);
            while(1)
            {
                sampTime = Timer_10ms(FALSE);
                if(sampTime >= nextTime)
                {
                    // Step function and sample rate testing
                    nextTime = sampTime + sampDelay;
                    tick1 = Timer_10ms(FALSE);
                    ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +1 -6, trials = 1, gain = user, sps = 5 Hz
                    tick2 = Timer_10ms(FALSE) - tick1;
                    uprintf("Vrsi   = %.6f V, Std = %.6f,  Samp = %04u,  Time = %03lu ms 5Hz\r\n", ADCResult, sys_data.AD24_std, indx++, tick2/10L);

                    tick1 = Timer_10ms(FALSE);
                    ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 20);       // Chan +1 -6, trials = 1, gain = user, sps = 20 Hz
                    tick2 = Timer_10ms(FALSE) - tick1;
                    uprintf("Vrsi   = %.6f V, Std = %.6f,  Samp = %04u,  Time = %03lu ms 20Hz\n", ADCResult, sys_data.AD24_std, indx++, tick2/10L);
                }
                if( UARTRxBytesAvail() )
                {
                    input = get_key();
                    break;    // break from while(1)
                }
            }
            break;


            case '1':
                ADS1248_gpio_ADG1609_mux(3);
                indx = 0;
                Timer_10ms(TRUE);       // reset 10msec tick count
                sampleCnt = 10;
                sampTime = 0L;
                nextTime = 0L;
                uprintf("1 -- Vrsi_B (pH Int Ref with bias offset), Mux = 11,  AIN1+ AIN5, sampleCnt = %u for 5Hz and %u for 20Hz\n", sampleCnt, sampleCnt);
                while(1)
                {
                    sampTime = Timer_10ms(FALSE);
                    if(sampTime >= nextTime)
                    {
                        nextTime = sampTime + sampDelay;
                        tick1 = Timer_10ms(FALSE);
                        ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 5);       // Chan +1 -5, trials = 1, gain = user, sps = 5 Hz
                        tick2 = Timer_10ms(FALSE) - tick1;
                        //uprintf("Vrsi_B = %.6f V,  Samp = %04u,  Time = %03lu ms\n", ADCResult, indx++, sampTime/100L);
                        uprintf("Vrsi_B = %.6f V, Std = %.6f,  Samp = %04u,  Time = %03lu ms 5Hz\r\n", ADCResult, sys_data.AD24_std, indx++, tick2/10L);

                        tick1 = Timer_10ms(FALSE);
                        ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 20);       // Chan +1 -5, trials = 1, gain = user, sps = 20 Hz
                        tick2 = Timer_10ms(FALSE) - tick1;
                        uprintf("Vrsi_B = %.6f V, Std = %.6f,  Samp = %04u,  Time = %03lu ms 20Hz\n", ADCResult, sys_data.AD24_std, indx++, tick2/10L);


                    }
                    if( UARTRxBytesAvail() )
                    {
                        input = get_key();
                        break;    // break from while(1)
                    }
                }
                break;

            case '2':
                ADS1248_gpio_ADG1609_mux(0);
                indx = 0;
                Timer_10ms(TRUE);       // reset 10msec tick count
                sampleCnt = 10;
                sampTime = 0L;
                nextTime = 0L;
                uprintf("2 -- Vrse (pH Ext Ref) Voltage, Mux = 00,  AIN1+ AIN6-, sampleCnt = %u for 5Hz and %u for 20Hz\n", sampleCnt, sampleCnt);
                while(1)
                {
                    sampTime = Timer_10ms(FALSE);
                    if(sampTime >= nextTime)
                    {
                        nextTime = sampTime + sampDelay;
                        tick1 = Timer_10ms(FALSE);
                        ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +1 -6, trials = 1, gain = user, sps = 5 Hz
                        tick2 = Timer_10ms(FALSE) - tick1;
                        //uprintf("Ext Ref = %.6f V,  Samp = %04u,  Time = %06lu \r\n", ADCResult, indx++, sampTime/100L);
                        uprintf("Vrse   = %.6f V, Std = %.6f,  Samp = %04u,  Time = %03lu ms 5Hz\r\n", ADCResult, sys_data.AD24_std, indx++, tick2/10L);

                        tick1 = Timer_10ms(FALSE);
                        ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 20);       // Chan +1 -6, trials = 1, gain = user, sps = 5 Hz
                        tick2 = Timer_10ms(FALSE) - tick1;
                        uprintf("Vrse   = %.6f V, Std = %.6f,  Samp = %04u,  Time = %03lu ms 20Hz\n", ADCResult, sys_data.AD24_std, indx++, tick2/10L);


                    }
                    if( UARTRxBytesAvail() )
                    {
                        input = get_key();
                        break;    // break from while(1)
                    }
                }
                break;

            case '3':
                ADS1248_gpio_ADG1609_mux(0);
                indx = 0;
                Timer_10ms(TRUE);       // reset 10msec tick count
                sampleCnt = 10;
                sampTime = 0L;
                nextTime = 0L;
                //uprintf("0 -- Ext Ref with Offset,  Mux = 00,  AIN1+ AIN5-\n");
                uprintf("3 -- Vrse (pH Ext Ref with bias offset) Voltage, Mux = 00,  AIN1+ AIN6-, sampleCnt = %u for 5Hz and %u for 20Hz\n", sampleCnt, sampleCnt);

                while(1)
                {
                    sampTime = Timer_10ms(FALSE);
                    if(sampTime >= nextTime)
                    {
                        nextTime = sampTime + sampDelay;

                        tick1 = Timer_10ms(FALSE);
                        ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 5);
                        tick2 = Timer_10ms(FALSE) - tick1;
                        //uprintf("Ext Ref = %.6f V,  Samp = %04u,  Time = %06lu \r\n", ADCResult, indx++, sampTime/100L);
                        uprintf("Vrse_B = %.6f V, Std = %.6f,  Samp = %04u,  Time = %03lu ms 5Hz\r\n", ADCResult, sys_data.AD24_std, indx++, tick2/10L);

                        tick1 = Timer_10ms(FALSE);
                        ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 20);
                        tick2 = Timer_10ms(FALSE) - tick1;
                        uprintf("Vrse_B = %.6f V, Std = %.6f,  Samp = %04u,  Time = %03lu ms 20Hz\n", ADCResult, sys_data.AD24_std, indx++, tick2/10L);
                    }

                    if( UARTRxBytesAvail() )
                    {
                        input = get_key();
                        break;    // break from while(1)
                    }
                }
                break;

            case '4':
                ADS1248_gpio_ADG1609_mux(1);
                indx = 0;
                Timer_10ms(TRUE);       // reset 10msec tick count
                sampleCnt = 10;
                sampTime = 0L;
                nextTime = 0L;
                uprintf("4 -- Ik (Counter Electrode Current), Mux = 01,  AIN1+ AIN5-, sampleCnt = %u for 5Hz and %u for 20Hz\n", sampleCnt, sampleCnt);
                while(1)
                {
                    sampTime = Timer_10ms(FALSE);
                    if(sampTime >= nextTime)
                    {
                        nextTime = sampTime + sampDelay;

                        tick1 = Timer_10ms(FALSE);
                        ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
                        tick2 = Timer_10ms(FALSE) - tick1;
                        uprintf("Ik = %.6f V, Std = %.6f,  Samp = %04u,  Time = %03lu ms 5Hz\n", ADCResult, sys_data.AD24_std, indx++, tick2/10L);

                        tick1 = Timer_10ms(FALSE);
                        ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 20);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
                        tick2 = Timer_10ms(FALSE) - tick1;
                        uprintf("Ik = %.6f V, Std = %.6f,  Samp = %04u,  Time = %03lu ms 20Hz\n", ADCResult, sys_data.AD24_std, indx++, tick2/10L);
                        //uprintf("Counter Electrode Current = %.6f,  Samp = %04u,  Time = %06lu \r\n", ADCResult, indx++, sampTime/100L);
                    }
                    if( UARTRxBytesAvail() )
                    {
                        input = get_key();
                        break;    // break from while(1)
                    }
                }
                break;

            case '5':
                ADS1248_gpio_ADG1609_mux(1);
                indx = 0;
                Timer_10ms(TRUE);       // reset 10msec tick count
                sampleCnt = 10;
                sampTime = 0L;
                nextTime = 0L;
                uprintf("5 -- Vk (Counter Electrode Voltage), Mux = 01,  AIN1+ AIN6-, sampleCnt = %u for 5Hz and %u for 20Hz\n", sampleCnt, sampleCnt);
                while(1)
                {
                    sampTime = Timer_10ms(FALSE);
                    if(sampTime >= nextTime)
                    {
                        tick1 = Timer_10ms(FALSE);
                        ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
                        tick2 = Timer_10ms(FALSE) - tick1;
                        uprintf("Vk = %.6f V, Std = %.6f,  Samp = %04u,  Time = %03lu ms 5Hz\n", ADCResult, sys_data.AD24_std, indx++, tick2/10L);

                        tick1 = Timer_10ms(FALSE);
                        ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 20);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
                        tick2 = Timer_10ms(FALSE) - tick1;
                        uprintf("Vk = %.6f V, Std = %.6f,  Samp = %04u,  Time = %03lu ms 20Hz\n", ADCResult, sys_data.AD24_std, indx++, tick2/10L);


                        //nextTime = sampTime + sampDelay;
                        //ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +6 -1, trials = 1, gain = user, sps = 5 Hz
                        //uprintf("Counter Electrode Voltage = %.6f V,  Samp = %04u,  Time = %06lu \r\n", ADCResult, indx++, sampTime/100L);
                    }
                    if( UARTRxBytesAvail() )
                    {
                        input = get_key();
                        break;    // break from while(1)
                    }
                }
                break;

            case '6':

                ADS1248_gpio_ADG1609_mux(2);
                indx = 0;
                Timer_10ms(TRUE);       // reset 10msec tick count
                sampleCnt = 10;
                sampTime = 0L;
                nextTime = 0L;
                uprintf("6 -- Ib Substrate Current (voltage across 1M resistor), Mux = 10,  AIN1+ AIN6-, sampleCnt = %u for 5Hz and %u for 20Hz\n", sampleCnt, sampleCnt);
                while(1)
                {
                    sampTime = Timer_10ms(FALSE);
                    if(sampTime >= nextTime)
                    {
                        nextTime = sampTime + sampDelay;
//#define IB_RESISTOR  1000000
//SUBSTRATECURRENT_IB_SCALING is defd in system.h
                        tick1 = Timer_10ms(FALSE);
                        ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);
                        tick2 = Timer_10ms(FALSE) - tick1;
                        //uprintf("Ib= %.6f V, Vb Std = %.6f,  Samp = %04u,  Time = %03lu ms 5Hz\n", ADCResult/IB_RESISTOR, sys_data.AD24_std, indx++, tick2/10L);
                        uprintf("Ib= %.6f V, Vb Std = %.6f,  Samp = %04u,  Time = %03lu ms 5Hz\n", ADCResult/SUBSTRATECURRENT_IB_SCALING, sys_data.AD24_std/SUBSTRATECURRENT_IB_SCALING, indx++, tick2/10L);


                        tick1 = Timer_10ms(FALSE);
                        ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 20);
                        tick2 = Timer_10ms(FALSE) - tick1;
                        uprintf("Ib= %.6f V, Vb Std = %.6f,  Samp = %04u,  Time = %03lu ms 20Hz\n", ADCResult/SUBSTRATECURRENT_IB_SCALING, sys_data.AD24_std/SUBSTRATECURRENT_IB_SCALING, indx++, tick2/10L);


                        //ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
                        //uprintf("Ib = %.6f,  Samp = %04u,  Time = %06lu \r\n", ADCResult/1000000, indx++, sampTime/100L);
                    }
                    if( UARTRxBytesAvail() )
                    {
                        input = get_key();
                        break;    // break from while(1)
                    }
                }
                break;

            case '7':
                ADS1248_gpio_ADG1609_mux(2);
                indx = 0;
                Timer_10ms(TRUE);       // reset 10msec tick count
                sampleCnt = 10;
                sampTime = 0L;
                nextTime = 0L;
                //uprintf("7 -- Substrate Voltage, Mux = 10,  AIN5+ AIN6-\n");
                uprintf("7 -- Substrate Voltage, Mux = 10,  AIN5+ AIN6-, sampleCnt = %u for 5Hz and %u for 20Hz\n", sampleCnt, sampleCnt);

                while(1)
                {
                    sampTime = Timer_10ms(FALSE);
                    if(sampTime >= nextTime)
                    {
                        nextTime = sampTime + sampDelay;

                        tick1 = Timer_10ms(FALSE);
                        ADCResult = pollADS1248_iso(5, 6, sampleCnt, 1, 5);
                        tick2 = Timer_10ms(FALSE) - tick1;
                        uprintf("Vsubstrate = %.6f V, Vb Std = %.6f,  Samp = %04u,  Time = %03lu ms 5Hz\n", ADCResult, sys_data.AD24_std, indx++, tick2/10L);

                        tick1 = Timer_10ms(FALSE);
                        ADCResult = pollADS1248_iso(5, 6, sampleCnt, 1, 20);
                        tick2 = Timer_10ms(FALSE) - tick1;
                        uprintf("Vsubstrate = %.6f V, Vb Std = %.6f,  Samp = %04u,  Time = %03lu ms 20Hz\n", ADCResult, sys_data.AD24_std, indx++, tick2/10L);

                        //ADCResult = pollADS1248_iso(5, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
                        //sampTime = Timer_10ms(FALSE);
                        //uprintf("Substrate Voltage = %.6f V,  Samp = %04u,  Time = %06lu \r\n", ADCResult, indx++, sampTime/100L);
                    }
                    if( UARTRxBytesAvail() )
                    {
                        input = get_key();
                        break;    // break from while(1)
                    }
                }
                break;

            case '8':
                ADS1248_gpio_ADG1609_mux(0);        // Not necessary, but consistent
                indx = 0;
                Timer_10ms(TRUE);       // reset 10msec tick count
                sampleCnt = 10;
                sampTime = 0L;
                nextTime = 0L;
                uprintf("8 -- Thermistor ISFET Temperature, Mux = XX,  AIN0+ AIN6-, sampleCnt = %u for 5Hz and %u for 20Hz\n", sampleCnt, sampleCnt);
                while(1)
                {
                    sampTime = Timer_10ms(FALSE);
                    if(sampTime >= nextTime)
                    {
                        nextTime = sampTime + sampDelay;

                        tick1 = Timer_10ms(FALSE);
                        ADCResult = pollADS1248_iso(0, 6, sampleCnt, 1, 5);
                        tick2 = Timer_10ms(FALSE) - tick1;
                        uprintf("Vtherm = %.6f V, Vb Std = %.6f,  Samp = %04u,  Time = %03lu ms 5Hz\n", ADCResult, sys_data.AD24_std, indx++, tick2/10L);

                        tick1 = Timer_10ms(FALSE);
                        ADCResult = pollADS1248_iso(0, 6, sampleCnt, 1, 20);
                        tick2 = Timer_10ms(FALSE) - tick1;
                        uprintf("Vtherm = %.6f V, Vb Std = %.6f,  Samp = %04u,  Time = %03lu ms 20Hz\n", ADCResult, sys_data.AD24_std, indx++, tick2/10L);



                        //ADCResult = pollADS1248_iso(0, 6, sampleCnt, 1, 5);
                        //sampTime = Timer_10ms(FALSE);
                        //uprintf("Thermistor = %.6f,  Samp = %04u,  Time = %06lu \r\n", ADCResult, indx++, sampTime/100L);
                    }
                    if( UARTRxBytesAvail() )
                    {
                        input = get_key();
                        break;    // break from while(1)
                    }
                }
                break;
#ifdef NOCODE
            case 'a':
                uprintf("mux = 00\n");
                ADS1248_gpio_ADG1609_mux(0);
                break;
            case 'b':
                uprintf("mux = 01\n");
                ADS1248_gpio_ADG1609_mux(1);
                break;
            case 'c':
                uprintf("mux = 10\n");
                ADS1248_gpio_ADG1609_mux(2);
                break;
            case 'd':
                uprintf("mux = 11\n");
                ADS1248_gpio_ADG1609_mux(3);
                break;
            case 'e':
                ADS1248_gpio2(0);
                break;
            case 'E':
                ADS1248_gpio2(1);
                break;
#endif

            case 'S':
            case 's':

                //   Date Time,    Ext Ref Off, Int Ref Off, Ext Ref V, Int Ref V, CntrE Cur, CntrE Volt, Subst Cur, Subst Vol, Therm
                //04/20/18 19:00:24, -0.326934, -0.326969, 0.670350, 0.670312, 0.326919, -0.670372, -0.670333, -0.997410
#if BOARD_MFET >= 1 || BOARD_MPHOX >= 1
                //uprintf("\n\n    Date Time,   Ext Ref Off, Int Ref Off, Ext Ref V, Int Ref V, CntrE Cur, CntrE Volt,  Subst Cur,  Subst Vol,  Thermistor\n");

                           //    Date Time,         Vrsi,      Vrsi_B,    Vrse,       Vrse_B,      Ik,         Vk,         Ib,        Vb,     VTherm
                           //04/24/21 10:26:00,  -0.055155,  0.674927,  -0.799240,  -0.069124,  0.272970,  -0.500551,  -0.399626,  0.000691,  0.997603
                uprintf("\n\n    Date Time,         Vrsi,      Vrsi_B,    Vrse,       Vrse_B,      Ik,         Vk,         Ib,        Vb,     VTherm\n");

#endif

#if BOARD_NANOFET >= 1
                uprintf("\n\n    Date Time,         Vrse,        Ik,       Vk,           Ib,       Vb,       Vtherm\n");
#endif

                indx = 0;
                Timer_10ms(TRUE);       // reset 10msec tick count
                sampDelay = 200L;
                sampleCnt = 1;    // Standard Dev is not reported
                sampTime = 0L;
                nextTime = 0L;
                while(1)
                {
                    sampTime = Timer_10ms(FALSE);
                    if(sampTime >= nextTime)
                    {
                        nextTime = sampTime + sampDelay;

                        // Date Time,
                        printCurrentTime();

                        uprintf(", ");
#if BOARD_MFET >= 1 || BOARD_MPHOX >= 1
                        //Vrsi   Int Ref V,
                        ADS1248_gpio_ADG1609_mux(3);
                        ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
                        ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +1 -6, trials = 1, gain = user, sps = 5 Hz
                        uprintf(" %.6f, ", ADCResult);       // Int Ref Voltage

                        //Vrsi_B    Int Ref Off,
                        ADS1248_gpio_ADG1609_mux(3);
                        ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
                        ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 20);       // Chan +1 -5, trials = 1, gain = user, sps = 5 Hz
                        uprintf(" %.6f, ", ADCResult); // Int Ref with Offset  // uprintf("Int Ref Offset = %.6f V,  Samp = %04u,  Time = %06lu \r\n", ADCResult, indx++, sampTime/100L);

                        //Vrse   Ext Ref V,
                        ADS1248_gpio_ADG1609_mux(0);
                        ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
                        ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +1 -6, trials = 1, gain = user, sps = 5 Hz
                        uprintf(" %.6f, ", ADCResult);       // Ext Ref Voltage

                        //Vrse_B   Ext Ref Off,
                        ADS1248_gpio_ADG1609_mux(0);
                        ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
                        ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 20);       // Chan +1 -5, trials = 1, gain = user, sps = 5 Hz
                        uprintf(" %.6f, ", ADCResult); // Ext Ref with Offset
#endif
#if BOARD_NANOFET >= 1
                        //Ext Ref V,
                        ADS1248_gpio_ADG1609_mux(0);
                        ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
                        ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 5);       // Chan +1 -6, trials = 1, gain = user, sps = 5 Hz
                        uprintf(" %.6f, ", ADCResult);       // Ext Ref Voltage
#endif

                        //CntrE Cur,
                        ADS1248_gpio_ADG1609_mux(1);
                        ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
                        ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
                        uprintf(" %.6f, ", ADCResult);       // Counter Electrode Current

                        // CntrE Volt,
                        ADS1248_gpio_ADG1609_mux(1);
                        ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
                        ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
                        uprintf(" %.6f, ", ADCResult);       // Counter Electrode Voltage

                        // Subst Cur,
                        ADS1248_gpio_ADG1609_mux(2);
                        ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
                        ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
                        uprintf(" %.6f, ", ADCResult);       // Substrate Current

                        // Subst Vol,
                        ADS1248_gpio_ADG1609_mux(2);
                        ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
                        ADCResult = pollADS1248_iso(5, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
                        uprintf(" %.6f, ", ADCResult);       // Substrate Voltage

                        // Thermistor
                        ADS1248_gpio_ADG1609_mux(0);        // Not necessary, but consistent
                        ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
                        ADCResult = pollADS1248_iso(0, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
                        uprintf(" %.6f", ADCResult);       // Substrate Voltage

                        uprintf("\n");


                    }

                    if( UARTRxBytesAvail() )
                    {
                        input = get_key();
                        break;    // break from while(1)
                    }
                }
                break;


            case 'X':
            case 'x':

                //Date Time,   Ext Ref Off, Int Ref Off, Ext Ref V, Int Ref V, CntrE Cur, CntrE Volt,  Subst Cur,  Subst Vol,  Thermistor
            //09/19/19 12:04:16,  -0.067811,  0.132143,  -0.798702,  -0.598748,  0.291080,  -0.501614,  -0.399307,  0.000322,  1.000455
            //09/19/19 12:04:21,  -0.067814,  0.132146,  -0.798713,  -0.598748,  0.291105,  -0.501616,  -0.399309,  0.000326,  1.000459

                //uprintf("\n\n    Date Time,   Ext Ref Off, Int Ref Off, Ext Ref V, Int Ref V, CntrE Cur, CntrE Volt,  Subst Cur,  Subst Vol,  Thermistor\n");
                uprintf("\n\n    Date Time,         Vrsi,     Vrsi_B,     Vrse,      Vrse_B,       Ik,        Vk,         Ib,        Vb,       VTherm\n");

                indx = 0;
                Timer_10ms(TRUE);       // reset 10msec tick count
                sampDelay = 500L;
                sampleCnt = 10;
                sampTime = 0L;
                nextTime = 0L;
                while(1)
                {
                    sampTime = Timer_10ms(FALSE);
                    if(sampTime >= nextTime)
                    {
                        nextTime = sampTime + sampDelay;
                        //test_sampling_mcap(-0.067814,  0.132146,  -0.798713,  -0.598748,  0.290849,  -0.501616,  -0.399309,  0.000326,  1.000159);
                        test_sampling_mcap();

                    }


                    if( UARTRxBytesAvail() )
                    {
                        input = get_key();
                        break;    // break from while(1)
                    }
                }
                break;

#ifdef NOCODE
            case 'T':
                // reading pH voltage
                uprintf("\nReading 24bit voltage via ADG1609 mux and ADS1248\n");

                openADS1248_iso();
                ROM_SysCtlDelay(MILLISECOND * 100);
                ADS1248_gpio_init();    // defd in ADS1248_iso.c
                ROM_SysCtlDelay(MILLISECOND * 10);
                // Now read the pH value (diff across AIN1- and AIN5+)
                uprintf("\n\n1: ADG1609 mux=00 (0) S1A S1B, ADS1248 AIN1- to AIN5+\n");
                ADS1248_gpio_ADG1609_mux(0);

                indx = 0;
                while(1)
                {
                    // pos chan, neg chan, avg=2, gain=1, sps=5
                    // pH to pH ref
                    ADCResult = pollADS1248_iso(5, 1, 2, 1, 5);       // Chan 5-1, trials = 1, gain = user, sps = 5 Hz
                    uprintf("pH = %.6f V,  loop= %u \r", ADCResult, indx++);

                    if( UARTRxBytesAvail() )
                    {
                        input = get_key();
                        break;    // break from while(1)
                    }
                }

                uprintf("\n\n2: ADG1609 mux=00 (0) S1A S1B, ADS1248 AIN1- to AIN6+(gnd)\n");
                ADS1248_gpio_ADG1609_mux(0);

                indx = 0;
                while(1)
                {
                    // pos chan, neg chan, avg=2, gain=1, sps=5
                    // pH to pH ref
                    ADCResult = pollADS1248_iso(6, 1, 2, 1, 5);       // Chan 6-1, trials = 1, gain = user, sps = 5 Hz
                    uprintf("pH = %.6f V,  loop= %u \r", ADCResult, indx++);

                    if( UARTRxBytesAvail() )
                    {
                        input = get_key();
                        break;    // break from while(1)
                    }
                }

                uprintf("\n\n3: ADG1609 mux=11 (3) S4A S4B, ADS1248 AIN1- to AIN5+\n");
                ADS1248_gpio_ADG1609_mux(3);
                indx = 0;
                while(1)
                {
                    // pos chan, neg chan, avg=2, gain=1, sps=5
                    // pH to pH ref
                    ADCResult = pollADS1248_iso(5, 1, 2, 1, 5);       // Chan 5-1, trials = 1, gain = user, sps = 5 Hz
                    uprintf("pH = %.6f V,  loop= %u \r", ADCResult, indx++);

                    if( UARTRxBytesAvail() )
                    {
                        input = get_key();
                        break;    // break from while(1)
                    }
                }

                uprintf("\n\n4: ADG1609 mux=11 (3) S4A S4B, ADS1248 AIN1- to AIN6+(gnd)\n");
                ADS1248_gpio_ADG1609_mux(3);
                indx = 0;
                while(1)
                {
                    // pos chan, neg chan, avg=2, gain=1, sps=5
                    // pH to pH ref
                    ADCResult = pollADS1248_iso(6, 1, 2, 1, 5);       // Chan 6-1, trials = 1, gain = user, sps = 5 Hz
                    uprintf("pH = %.6f V,  loop= %u \r", ADCResult, indx++);

                    if( UARTRxBytesAvail() )
                    {
                        input = get_key();
                        break;    // break from while(1)
                    }
                }

                closeADS1248_iso();
                uprintf("iso power off\n");

                break;
#endif

            default:
                uprintf("\nPress enter to bring up menu\n");
                closeADS1248_iso();
                uprintf("iso power off\n");
                return;
        } // switch

    } // while(1)

    // Future: if break from while(1), then make sure to close isolated supply (return is from switch-default)
    closeADS1248_iso();
    uprintf("iso power off\n");
}





int board_qa_and_init(int initFlg)
{

    float ftemp;
    unsigned int temp;
    time_t usersTime;

    char buff[128];

    //set clock

    // set filename

    // set pH parms

    // set deployment parms

    // set glider config for data fields

    // test the adc

    sys_data.GMT = false;

    usersTime = getDateAndTime();

    if(usersTime)
    {
        ROM_HibernateRTCSet(usersTime); // Set time if user entered valid time
        uprintf("\nTime now: ");
        printCurrentTime();
        if(sys_data.GMT) uprintf(" GMT");
        else uprintf(" Local");

    }

    // initialize EEPROM sys_data
    if(initFlg == 1)
    {
        sys_data.low_batt_volt = 10.0;
        sys_data.sampling_period = 0;
        sys_data.sample_average = 5;
        sys_data.sample_average = 60;
        sys_data.pumpon_time = 0;
        sys_data.TCOffset = 0.0;
        sys_data.k0ext = -1.3;
        sys_data.k0int = -0.4;
        sys_data.k2ext = -0.001;
        sys_data.k2int = -0.001;

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

        sys_data.default_sal = 35.0;

        sys_data.Vint_gain = 1;
        sys_data.Vint_sps = 10;

        sys_data.gain1;                  // Future, parameterize sampling wiht gain0(Vint), thru gain3
        sys_data.sps1;
        sys_data.gain2;
        sys_data.sps2;
        sys_data.gain3;
        sys_data.sps3;

        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;

        //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_data.GMT = false;                       //456 GMT or Local time
        sys_data.sample_aligned = true;            //460 Sample aligned to hour

        sys_data.baud_rate = 115200L;
        int indx;
        for(indx=0; indx<MAX_NUMOF_CONFIG_FIELDS; indx++)
            sys_data.data_cfg[indx] = 0;
        sys_data.fast_mode = 1;        //511 added 10 Jun 2019

        strcpy(sys_data.fileName, "noname.txt");
        strcpy(sys_data.user, "abc");
        strcpy(sys_data.board_SN, " ");
        strcpy(sys_data.board_fab_note, " ");
        strcpy(sys_data.mcap_SN, " ");
        strcpy(sys_data.durafet_SN, " ");
        //strcpy(sys_data.capadap_SN, "abc");
        //strcpy(sys_data.ISE_SN, "abc");
        //strcpy(sys_data.sensor_name, "abc");
        //strcpy(sys_data.condo_SN, " ");     // was microcat, now conductivity
        strcpy(sys_data.optode_SN, " ");
        strcpy(sys_data.condo_SN, " ");
        strcpy(sys_data.pump_SN, " ");
        strcpy(sys_data.package_name, " ");   // instrument package name, ex. underway system, glider pH

        // Write the sys_data struct into EEPROM.  Last argument ensures # of bytes is multiple of 4
        if(storeSysDataVariables())                         //if(EEPROMProgram((uint32_t*) &sys_data, 0x400, (sizeof(sys_data) + 3) & ~3))
        {
            uprintf("\n\nError storing system data.\n");
        }

    }

    uprintf("\nEnter data file name (no spaces, 8.3 format) [%s]: ", sys_data.fileName);
    if( getUserInput(buff, 11) ) // was MAX_FILENAME-1) )  // storage size is 80, user input size is 100 - BUG!
    {
        strcpy(sys_data.fileName, buff);
        uprintf("\nFile name now: %s\n", sys_data.fileName);
    }

    uprintf("\n\nEnter user initials [%s]: ", sys_data.user);
    if( getUserInput(buff, MAX_USER_INITIALS-1) )       // was 10 user initials
    {
        strcpy(sys_data.user, buff);
        uprintf("\nUser initials now: %s\n", sys_data.user);
    }

    set_metadata();     // get user input and set metadata fields

    sys_data.sample_aligned = false;
    sys_data.sampling_period = 0;   // polled
    uprintf("\nSample period [%u] sec \n", sys_data.sampling_period);
    sys_data.sample_average = 10;

    // Sample average
    sys_data.fast_mode = 1;

    sys_data.pumpon_time = 0;
    uprintf("Pump on time [%u] sec\n", sys_data.pumpon_time);

    sys_data.low_batt_volt = 8.0;
    uprintf(buff, "Low battery_voltage [%.1f V]: ", sys_data.low_batt_volt);
    uprintf("%s", buff);

    sys_data.output = NORMAL;
    printf("\n\nOutput mode now: NORMAL");


    // A Sample Num
    // B MM/DD/YYYY HH:MM:SS
    // C Battery Volt
    // O Vext Ref
    // P Vext Ref Std
    // Q Vcounter Electrode
    // R Vcounter Electrode Std
    // S Counter Electrode Current
    // T Substrate Current
    // Z FAST

    sys_data.data_cfg[CFG_SAMPLE_NUM] = 1;          // A
    sys_data.data_cfg[CFG_TIMEDATE] = 0;            // B
    sys_data.data_cfg[CFG_ENV_BAT_VOLT] = 1;        // C
    sys_data.data_cfg[CFG_ENV_BRD_TEMP] = 0;        // d
    sys_data.data_cfg[CFG_ENV_HUMIDITY] = 0;        //e
    sys_data.data_cfg[CFG_BIAS_BAT_VOLT] = 1;        //f
    sys_data.data_cfg[CFG_V_THERMISTOR] = 1;        //g
    sys_data.data_cfg[CFG_V_THERMISTOR_STD] = 0;    //h
    sys_data.data_cfg[CFG_V_INT_OFFSET] = 1;        //i
    sys_data.data_cfg[CFG_V_INT_OFFSET_STD] = 0;    // j
    sys_data.data_cfg[CFG_V_EXT_OFFSET] = 0;        // k
    sys_data.data_cfg[CFG_V_EXT_OFFSET_STD] = 0;    //l
    sys_data.data_cfg[CFG_V_INT] = 1;               // m
    sys_data.data_cfg[CFG_V_INT_STD] = 1;           // n
    sys_data.data_cfg[CFG_V_EXT] = 1;               // O              14   //+ Vext
    sys_data.data_cfg[CFG_V_EXT_STD] = 1;           //   P        15   // + Vext_std
    sys_data.data_cfg[CFG_V_COUNTER_ELECT] = 1;     //  Q   16   //+ Vcounter_electrode
    sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] = 1; // R  //+ Vcounter_electrode_std
    sys_data.data_cfg[CFG_I_COUNTER] = 1;           //  S         18   // + Counter_current
    sys_data.data_cfg[CFG_I_SUBSTRATE] = 1;         // T        19   //+ Substrate_current
    sys_data.data_cfg[CFG_CALC_TEMP] = 1;           // u
    sys_data.data_cfg[CFG_EST_PH_INT] = 1;          // v
    sys_data.data_cfg[CFG_EST_PH_EXT] = 0;          // w
    sys_data.data_cfg[CFG_OPTODE] = 0;              // x
    sys_data.data_cfg[CFG_CONDO] = 0;              // y
    sys_data.fast_mode = 1;                         // Z




    //initSysDataVariables();

    // Write the sys_data struct into EEPROM.  Last argument ensures # of bytes is multiple of 4
    if(storeSysDataVariables())                         //if(EEPROMProgram((uint32_t*) &sys_data, 0x400, (sizeof(sys_data) + 3) & ~3))
    {
        uprintf("\n\nError storing system data.\n");
    }


    uprintf("\niso power on, adg1609 mux init'd\n\n");

    //profile_on(1);      // THOM debug
    openADS1248_iso();      // 22msec
    //profile_off(1);
    ROM_SysCtlDelay(MILLISECOND * 100);
    ADS1248_gpio_init();    // defd in ADS1248_iso.c
    ROM_SysCtlDelay(MILLISECOND * 10);


    uint32_t sampDelay;
    uint32_t sampTime;
    uint32_t nextTime;
    char input;

    //indx = 0;
    sampDelay = 500L;
    Timer_10ms(TRUE);       // reset 10msec tick count
    //sampleCnt = 10;
    sampTime = 0L;
    nextTime = 0L;

    uprintf("\nThe analog QA test assumes an analog sim, aka Jensenian v2, is connected to the MCAP, otherwise errors will be thrown\n");
    uprintf("\n\n    Date Time,         Vrsi,     Vrsi_B,     Vrse,      Vrse_B,       Ik,        Vk,         Ib,        Vb,       VTherm\n");

    while(1)
    {
        sampTime = Timer_10ms(FALSE);
        if(sampTime >= nextTime)
        {
            nextTime = sampTime + sampDelay;
            //test_sampling_mcap(-0.067814,  0.132146,  -0.798713,  -0.598748,  0.290849,  -0.501616,  -0.399309,  0.000326,  1.000159);
            test_sampling_mcap(); // checks for errors in voltage readings

        }


        if( UARTRxBytesAvail() )
        {
            input = get_key();
            break;    // break from while(1)
        }
    }




    closeADS1248_iso();
    uprintf("\niso power off\n");

    uprintf("\n\n\n\n\n***************** Settings **************\n");

    uprintf("Sampling period: \t%d seconds\n", sys_data.sampling_period);

    uprintf("Samples hour aligned\t");
    if(sys_data.sample_aligned) uprintf("Yes\n");
    else uprintf("No\n)");

    uprintf("Pump on time: \t%d seconds\n", sys_data.pumpon_time);
    uprintf("Low battery voltage: \t%.1f V\n", sys_data.low_batt_volt);

    if(sys_data.output == 1) uprintf("Output mode: \tNormal\n");
    else if(sys_data.output == 2) uprintf("Output mode: \tVerbose\n");
    uprintf("k0int @25 C = \t%1.6f V\n", sys_data.k0int);
    uprintf("k0ext @25 C = \t%1.6f V\n", sys_data.k0ext);

    uprintf("File name: \t%s\n", sys_data.fileName);

    uprintf("Current time: ");
    printCurrentTime();

    uprintf("\n*****************************************************\n");


    uprintf("\n\nquick test, initialization of EEPROM with metadata, and setup is complete\n\n\n");
#ifdef NOCODE
    char input;
    float ADCResult;
    uint32_t indx = 0;
    time_t timer;
    int sampleCnt;
    unsigned long sampTime;
    unsigned long nextTime;
    unsigned long sampDelay;

    sampleCnt = 10;


    sampDelay = 200L;
    //   Date Time,    Ext Ref Off, Int Ref Off, Ext Ref V, Int Ref V, CntrE Cur, CntrE Volt, Subst Cur, Subst Vol, Therm
    //04/20/18 19:00:24, -0.326934, -0.326969, 0.670350, 0.670312, 0.326919, -0.670372, -0.670333, -0.997410

    uprintf("\n\n    Date Time,   Ext Ref Off, Int Ref Off, Ext Ref V, Int Ref V, CntrE Cur, CntrE Volt,  Subst Cur,  Subst Vol,  Thermistor\n");

    indx = 0;
    Timer_10ms(TRUE);       // reset 10msec tick count
    sampleCnt = 1;
    sampTime = 0L;
    nextTime = 0L;
    while(1)
    {
        sampTime = Timer_10ms(FALSE);
        if(sampTime >= nextTime)
        {
            nextTime = sampTime + sampDelay;

            // Date Time,
            printCurrentTime();

            uprintf(", ");

            //Ext Ref Off,
            ADS1248_gpio_ADG1609_mux(0);
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 20);       // Chan +1 -5, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f, ", ADCResult); // Ext Ref with Offset

            //Int Ref Off,
            ADS1248_gpio_ADG1609_mux(3);
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 20);       // Chan +1 -5, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f, ", ADCResult); // Int Ref with Offset  // uprintf("Int Ref Offset = %.6f V,  Samp = %04u,  Time = %06lu \r\n", ADCResult, indx++, sampTime/100L);

            //Ext Ref V,
            ADS1248_gpio_ADG1609_mux(0);
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +1 -6, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f, ", ADCResult);       // Ext Ref Voltage

            // Int Ref V,
            ADS1248_gpio_ADG1609_mux(3);
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +1 -6, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f, ", ADCResult);       // Int Ref Voltage

            //CntrE Cur,
            ADS1248_gpio_ADG1609_mux(1);
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f, ", ADCResult);       // Counter Electrode Current

            // CntrE Volt,
            ADS1248_gpio_ADG1609_mux(1);
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f, ", ADCResult);       // Counter Electrode Voltage

            // Subst Cur,
            ADS1248_gpio_ADG1609_mux(2);
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f, ", ADCResult);       // Substrate Current

            // Subst Vol,
            ADS1248_gpio_ADG1609_mux(2);
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(5, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f, ", ADCResult);       // Substrate Voltage

            // Thermistor
            ADS1248_gpio_ADG1609_mux(0);        // Not necessary, but consistent
            ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
            ADCResult = pollADS1248_iso(0, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
            uprintf(" %.6f", ADCResult);       // Substrate Voltage

            uprintf("\n");


        }

        if( UARTRxBytesAvail() )
        {
            input = get_key();
            break;    // break from while(1)
        }
    }
#endif

}

















//------------------------ The Basement ------------------------------------


//int test_sampling_mcap(float extRefOff, float intRefOff, float extRefV, float intRefV, float cntrC, float cntrV, float substCur, float substV, float thermistor)
//int test_sampling_mcap(float intRefV, float intRefOff, float extRefV, float extRefOff, float cntrC, float cntrV, float substCur, float substV, float thermistor)
int test_sampling_mcap(void)        // changed to DEFINES in test.h from nois analysis

{
    float delta, ADCResult;
    int sampleCnt;
    int error;

    error = 0;
    sampleCnt = 1;

    //   Date Time,    Ext Ref Off, Int Ref Off, Ext Ref V, Int Ref V, CntrE Cur, CntrE Volt, Subst Cur, Subst Vol, Therm
    //04/20/18 19:00:24, -0.326934, -0.326969, 0.670350, 0.670312, 0.326919, -0.670372, -0.670333, -0.997410

    // Date Time,
    printCurrentTime();

    uprintf(", ");

    delta = 0.0200;

    // Vrsi   Int Ref V,
    ADS1248_gpio_ADG1609_mux(3);
    ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
    ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +1 -6, trials = 1, gain = user, sps = 5 Hz
    uprintf(" %.6f, ", ADCResult);       // Int Ref Voltage
    //if((ADCResult > (intRefV+delta))  || (ADCResult < (intRefV-delta)))
    if((ADCResult < VRSI_MIN)  || (ADCResult > VRSI_MAX))
    {
        uprintf(" Err ");
        error++;
    }

    //Vrsi_B   Int Ref Off,
    ADS1248_gpio_ADG1609_mux(3);
    ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
    ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 20);       // Chan +1 -5, trials = 1, gain = user, sps = 5 Hz
    uprintf(" %.6f, ", ADCResult); // Int Ref with Offset  // uprintf("Int Ref Offset = %.6f V,  Samp = %04u,  Time = %06lu \r\n", ADCResult, indx++, sampTime/100L);
    //if((ADCResult > (intRefOff+delta))  || (ADCResult < (intRefOff-delta)))
    if((ADCResult < VRSI_B_MIN)  || (ADCResult > VRSI_B_MAX))
    {
        uprintf(" Err ");
        error++;
    }


    //Ext Ref V,
    ADS1248_gpio_ADG1609_mux(0);
    ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
    ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +1 -6, trials = 1, gain = user, sps = 5 Hz
    uprintf(" %.6f, ", ADCResult);       // Ext Ref Voltage
    //if((ADCResult > (extRefV+delta))  || (ADCResult < (extRefV-delta)))
    if((ADCResult < VRSE_MIN)  || (ADCResult > VRSE_MAX))
    {
        uprintf(" Err ");
        error++;
    }

    //Vrse_B   Ext Ref Off,
    ADS1248_gpio_ADG1609_mux(0);
    ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
    ADCResult = pollADS1248_iso(1, 5, sampleCnt, 1, 20);       // Chan +1 -5, trials = 1, gain = user, sps = 5 Hz
    uprintf(" %.6f, ", ADCResult); // Ext Ref with Offset
    //if((ADCResult > (extRefOff+delta))  || (ADCResult < (extRefOff-delta)))
    if((ADCResult < VRSE_B_MIN)  || (ADCResult > VRSE_B_MAX))
    {
        uprintf(" Err ");
        error++;
    }


    //Ik   CntrE Cur,
    ADS1248_gpio_ADG1609_mux(1);
    ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
    ADCResult = pollADS1248_iso(1, 5, sampleCnt, 4, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
    uprintf(" %.6f, ", ADCResult);       // Counter Electrode Current
    if((ADCResult < IK_MIN)  || (ADCResult > IK_MAX))
    {
        uprintf(" Err ");
        error++;
    }


    // Vk  CntrE Volt,
    ADS1248_gpio_ADG1609_mux(1);
    ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
    ADCResult = pollADS1248_iso(1, 6, sampleCnt, 4, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
    uprintf(" %.6f, ", ADCResult);       // Counter Electrode Voltage
    if((ADCResult < VK_MIN)  || (ADCResult > VK_MAX))
    {
        uprintf(" Err ");
        error++;
    }

    // Ib   Subst Cur,
    ADS1248_gpio_ADG1609_mux(2);
    ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
    ADCResult = pollADS1248_iso(1, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
    uprintf(" %.6f, ", ADCResult);       // Substrate Current
    if((ADCResult < IB_MIN)  || (ADCResult > IB_MAX))
    {
        uprintf(" Err ");
        error++;
    }

    // VB  Subst Vol,
    ADS1248_gpio_ADG1609_mux(2);
    ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
    ADCResult = pollADS1248_iso(5, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
    uprintf(" %.6f, ", ADCResult);       // Substrate Voltage
    if((ADCResult < VB_MIN)  || (ADCResult > VB_MAX))
    {
        uprintf(" Err ");
        error++;
    }

    // Thermistor
    ADS1248_gpio_ADG1609_mux(0);        // Not necessary, but consistent
    ROM_SysCtlDelay(MILLISECOND * 10);      // 10msec settling for MUX analog
    ADCResult = pollADS1248_iso(0, 6, sampleCnt, 1, 5);       // Chan +5 -1, trials = 1, gain = user, sps = 5 Hz
    uprintf(" %.6f", ADCResult);       // Substrate Voltage
    if((ADCResult < VTHERM_MIN)  || (ADCResult > VTHERM_MAX))
    {
        uprintf(" Err ");
        error++;
    }

    if(error > 0)
    {
        uprintf("  ERROR count = %d  ", error);
    }
    uprintf("\n");

    return(error);
}
