/*
 * Init.c
 *
 *  Created on: Jan 23, 2014
 *      Author: rob
 *  Modified starting Mar 2018 by Thom Maughan (TM)
 *    Added MFET/MCAP board support
 */

#include "System.h"
#include "Init.h"
#include "SDCard.h"
#include "uartstdio.h"	// User local version with larger RX buffer
#include "sleep.h"
#include "i2c_fram.h"

extern int microSD_flg;



extern void AUX1_init(void);
extern void optode_init(void);
extern void microCat_init(void);
extern void condo_init(void);
extern void led_init(void);

extern void profile2_toggle(int target);
extern void ADS1248_gpio_init(void);

void init_TM4C123GH6PGE_144pin(void);
extern void init_TM4C123GH6PM_64pin(void);


//#define MFET_BOARD      1           // MFET board is the MiniFET board (small) with MCAP MiniFET CapAdapter per Yui 3 Apr 2018
//#define SEAPHOX_UNIQUE  0            // turn off (set to 0) SEAPHOX GPIO settings that are unique to SeaFET board

/**********************************************************************************
 * Init()
 * Initialize pins and ports upon reset or wake from hybernation. Set unused pins low.
 **********************************************************************************/
void init(void)
{
#if BOARD_MFET >= 1 || BOARD_SEAPHOX == 1 || BOARD_MPHOX == 1
    init_TM4C123GH6PGE_144pin();
#endif

#if BOARD_NANOFET >= 1

#define BOARD_DAG       1
    init_TM4C123GH6PM_64pin();
#endif
}




void init_TM4C123GH6PGE_144pin(void)
{
	// Enable lazy stacking for interrupt handlers.  This allows floating-point
	// instructions to be used within interrupt handlers, but at the expense of2
	// extra stack usage.
	ROM_FPULazyStackingEnable();

	//enable floats
	ROM_FPUEnable();

	//set the system clock...50MHZ
	ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);

	//TM4C123GH6PGE = 144     (105 GPIO)
	//PA0 - PA7       8
	//PB0 - PB5       6
	//PC0 - PC7       8
	//PD0 - PD7       8
	//PE0 - PE7       8
	//PF0 - PF7       8
	//PG0 - PG7       8
	//PH0 - PH7       8
	//PJ0 - PJ7       8
	//PK0 - PK7       8
	//PL0 - PL7       8
	//PM0 - PM7       8
	//PN0 - PN7       8
	//PP0 - PP2       3


	/*** Enable the GPIO ports used ***/
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOH);
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOJ);
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOK);
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOL);
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOM);
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOP);

	/*** Initialize the console ***/
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
	ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
	ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
	ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
	UARTStdioConfig(0, 115200, 16000000);		// 115200 bits per second

#if BOARD_MFET == 1
	// MFET revA has different logic level for PL0 enable rs232 transceiver
	// CONSOLE_ON pin (Console_PS)
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTL_BASE, GPIO_PIN_0);     // PL0, pin 108
	ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_0, 0xff);		// Set pin to turn console driver on

	// THOM new (LTC1280) set Console_Mode - this GPIO is not connected on SeaFET board
	// Console_Mode=1 has receivers active, Iq=15uA, Console_Mode=0, receivers disabled, Iq=1uA
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTL_BASE, GPIO_PIN_1);     // PL1, pin 107
    ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_1, 0xff);        // Set pin to turn mode on (enabled)
#endif
#if BOARD_MFET == 2
    // MFET revB has MAX3222EETP+  ENABLE# is active low, SHUTDOWN# is active low
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTL_BASE, GPIO_PIN_0); // PL0, pin 108             // CONSOLE ENABLE #
    ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_0, 0x00);    // Clear pin to turn console driver on

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTL_BASE, GPIO_PIN_1); // PL0, pin 107             // CONSOLE SHUTDOWN # (active low)
    ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_1, 0xff);    // Set to disable shutdown (part is enabled)
#endif
#if BOARD_MPHOX == 1
    // MPHOX revA has ADM3312    ENABLE# is active low, SHUTDOWN is active high

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTL_BASE, GPIO_PIN_0); // PL0, pin 108             // CONSOLE ENABLE active ;pe
    ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_0, 0x00);    // Set pin 108 to turn console driver on

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTL_BASE, GPIO_PIN_1); // PL0, pin 107             // CONSOLE SHUTDOWN  (active high)
    ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_1, 0x00);    // Clear pin 107 to disable shutdown (part is enabled)
#endif
	UARTEchoSet(true);	// Enable console echo



	//ROM_SysCtlDelay(MILLISECOND*100);
	ROM_SysCtlDelay(MILLISECOND*2);       // THOM, fast startup

	/*** Initialize isolated ADS1248 pins as GPIO to reduce power consumption ***/
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTH_BASE, GPIO_PIN_0);		// SSI3CLK
	ROM_GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_0, 0x00);

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTH_BASE, GPIO_PIN_1);		// SSI3Fss
	ROM_GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_1, 0x00);

	ROM_GPIOPinTypeGPIOInput(GPIO_PORTH_BASE, GPIO_PIN_2);		// SSI3Rx

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTH_BASE, GPIO_PIN_3);		// SSI3Tx
	ROM_GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_3, 0x00);

	// START pin (non-iso)  SEAPHOX ONLY
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_7);
	ROM_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_7, 0x00);		// Clear pin

    // START pin (iso)  THOM NEW MINI-BOARD
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_6);
    ROM_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_6, 0xFF);        // START should be high by default

	// DRDY pin
	ROM_GPIOPinTypeGPIOInput(GPIO_PORTN_BASE, GPIO_PIN_3);		// Input

	// RESET pin
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_5);
	//ROM_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_5, 0x00);		// Clear pin (this was the state with inverter???) THOM
    ROM_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_5, 0xFF);        // RESET high THOM

	// Isolated power
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTK_BASE, GPIO_PIN_2);
	ROM_GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_2, 0x00);  		// Iso-power Off

	// Optoisolator power
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_4);
	ROM_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_4, 0x00);  		// Optoisolator power Off


	/*** Initialize non-isolated ADS1248 on SSI1 ***/
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI1);  // Enable SSI1

	ROM_GPIOPinConfigure(GPIO_PF1_SSI1TX);
	ROM_GPIOPinConfigure(GPIO_PD2_SSI1RX);
	ROM_GPIOPinConfigure(GPIO_PF2_SSI1CLK);
	ROM_GPIOPinConfigure(GPIO_PF3_SSI1FSS);

	//THOM TODO: only call init if the microCat or optode or conductivity are being used.  18 Apr 2021
	// for now, just do as has been done for years.
	microCat_init();        // uart4, 9600 baud, no echo PC4 RX, PC5 TX, J8 on MPHOX, CTDpwr is PD0 (pin1 on proc)

#ifdef NOCODE
	// Set up MicroCAT CTD UART4  (J8 on MPHOX)
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART4);
	ROM_GPIOPinConfigure(GPIO_PC4_U4RX);
	ROM_GPIOPinConfigure(GPIO_PC5_U4TX);
	ROM_GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_4 | GPIO_PIN_5);

    // Use the internal 16MHz oscillator as the UART clock source.
    UARTClockSourceSet(UART4_BASE, UART_CLOCK_PIOSC);

    MicroCAT_UARTConfig(4, 9600, 16000000); // Configure UART  (9600,N,8,1)
 	MicroCAT_UARTEchoSet(0);	// Disable echo to MicroCAT
#endif

 	/*** Inititialize UART 3 9600 baud for Optode interface on J7 ***/
 	optode_init();

#ifdef MOVEDTOOPTODEC
 	/*** Inititialize Optode on UART 3 ***/
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_1);	// Optode power (PD1, pin 2)
	ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1, 0x00);	// Power off

	// set up UART to the optode
	ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART3);
	ROM_GPIOPinConfigure(GPIO_PC6_U3RX);
	ROM_GPIOPinConfigure(GPIO_PC7_U3TX);
	ROM_GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_6 | GPIO_PIN_7);

    // Use the internal 16MHz oscillator as the UART clock source.
    UARTClockSourceSet(UART3_BASE, UART_CLOCK_PIOSC);

#define OPTODE_BAUD     9600
    uart3_Config(3, OPTODE_BAUD, 16000000); // Configure Optode UART  (9600,N,8,1)
    uart3_echoSet(0);	// Disable echo to Optode
#endif

#if BOARD_MPHOX == 1
    /*** Initialize CTDpwr on MPHOX ***/
    // Moved to microCat_init()
#ifdef NOCODE
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_0); // CTDpwr off  PD0, (pin 1, on 144)
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0x00);    // CTDpwr=off   silk: J8
#endif

    /*** Initialize AUXpwr1 on MPHOX ***/
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_1); // AUXpwr1 off  PD1, (pin 2)
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1, 0x00);    // AUXpwr1=off   silk: J12 AUX - PWR1

    /*** Initialize AUXpwr2 on MPHOX ***/
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_2); // AUXpwr2 off  PD2, (pin 3)
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2, 0x00);    // AUXpwr2=off   silk: J13 AUX - PWR2

    /*** Initialize AUXPwr3 on MPHOX, used on J14 with uart2 ***/
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_3); // AUXpwr3 (MPHOX) (PD3, pin 4)
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3, 0x00);    // AUXpwr3=0     silk: J14  COMM - PWR
#endif

    /*** Initialize pressure sensor ***/
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTM_BASE, GPIO_PIN_5);
    ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_5, 0x00);    // Pressure sensor off

#if BOARD_MPHOX == 1
    /*** Inititialize AUX1 on UART 2 ***/
	//AUX1_init();        // defd in aux1.c initialize AUX1 / UART2  J14 on MPHOX, AUX1 needs hardware/software review, AUX1 init has errors
    condo_init();
#endif
#ifdef NOCODE
	/*** Inititialize AUX1 on UART 2 ***/
	MAP_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_4);	// AUX1 power  (Does not agree with mphox schematic, BUG)
	MAP_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_4, 0x00);	// Power off

	// COM2 PWRDOWN (PK4, pin 112) pin
	MAP_GPIOPinTypeGPIOOutput(GPIO_PORTK_BASE, GPIO_PIN_4);
	MAP_GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_4, 0x00);	// Shut off COM2 driver


	// void Init_AUX1(void) should be called -THOM todo
	// Set up AUX1 UART2 pins (J14 on MPHOX)
	MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART2);
	MAP_GPIOPinConfigure(GPIO_PG4_U2RX);			  // Set up the pins
	MAP_GPIOPinConfigure(GPIO_PG5_U2TX);
	MAP_GPIOPinTypeUART(GPIO_PORTG_BASE, GPIO_PIN_5 | GPIO_PIN_4);

    // Use the internal 16MHz oscillator as the UART clock source.
    UARTClockSourceSet(UART2_BASE, UART_CLOCK_PIOSC);

    // Initialize UART 2
    AUX1_UARTConfig(2, 9600, 16000000);
#endif


	// Set up AUX2 UART pins
	MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART5);
	MAP_GPIOPinConfigure(GPIO_PJ2_U5RX);			  // Set up the pins
	MAP_GPIOPinConfigure(GPIO_PJ3_U5TX);
	MAP_GPIOPinTypeUART(GPIO_PORTJ_BASE, GPIO_PIN_2 | GPIO_PIN_3);

    // Use the internal 16MHz oscillator as the UART clock source.
    UARTClockSourceSet(UART5_BASE, UART_CLOCK_PIOSC);

	// Initialize the UART
    AUX2_UARTConfig(5, 9600, 16000000);

    /*** Initialize Green LED  see led_green ***/
    led_init();
#ifdef OLDCODE6Apr2021
    /*** Initialize Green LED  see led_green ***/
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_3);	// Green LED (PE3, pin 12)
	ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0x00);	// LED off   // THOM 16 May 2019  was PORTD, changed to PORTE bug fix


	/*** Initialize Red LED ***/
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTP_BASE, GPIO_PIN_2);	// Red LED (PP2, pin 11)
	ROM_GPIOPinWrite(GPIO_PORTP_BASE, GPIO_PIN_2, 0x00);	// LED off   THOM 16 May 2019 Bug fix, was PORTD changed to PORTP
#endif

    /*** Initialize the DIC port ***/
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_6);	// PD6, pin 143
	ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_6, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_4);	// PE4, pin 139
	ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_4, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_5);	// PE5, pin 140
	ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_5, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_6);	// PE6, pin 133
	ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_6, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_7);	// PE7, pin 134
	ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_7, 0x00);	// Clear

	// Temporarily Setup unused ports as GPIO (change this if I2C, SSI2, or M0PWM used)
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTH_BASE, GPIO_PIN_4);	// PH4, pin 26
	ROM_GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_4, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTH_BASE, GPIO_PIN_5);	// PH5, pin 23
	ROM_GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_5, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTH_BASE, GPIO_PIN_6);	// PH6, pin 22
	ROM_GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_6, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTH_BASE, GPIO_PIN_7);	// PH7, pin 21
	ROM_GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_7, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_2);	// PB2, pin 99
	ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2, 0xFF);	// Set (R62 pull up)

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_3);	// PH3, pin 100
	ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0xFF);	// Set (R63 pull up)

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTP_BASE, GPIO_PIN_0);	// PP0, pin 131
	ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTP_BASE, GPIO_PIN_1);	// PP1, pin 132
	ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1, 0x00);	// Clear


	// Initialize unused pins
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_6);	// PA6, pin 45
	ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_7);	// PA7, pin 46
	ROM_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_0);	// PB0, pin 97
	ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_1);	// PB1, pin 98
	ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_4);	// PB4, pin 136
	ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_4, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_5);	// PD5, pin 142
	ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_5, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_7);	// PD7, pin 144
	ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_7, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_0);	// PG0, pin 55
	ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_0, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_1);	// PG1, pin 54
	ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_1, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_2);	// PG2, pin 53
	ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_2, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_3);	// PG3, pin 52
	ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_3, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_6);	// PG6, pin 48
	ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_6, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_7);	// PG7, pin 47
	ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_7, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTJ_BASE, GPIO_PIN_0);	// PJ0, pin 120
	ROM_GPIOPinWrite(GPIO_PORTJ_BASE, GPIO_PIN_0, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTJ_BASE, GPIO_PIN_1);	// PJ1, pin 121
	ROM_GPIOPinWrite(GPIO_PORTJ_BASE, GPIO_PIN_1, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTJ_BASE, GPIO_PIN_4);	// PJ4, pin 127
	ROM_GPIOPinWrite(GPIO_PORTJ_BASE, GPIO_PIN_4, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTJ_BASE, GPIO_PIN_5);	// PJ5, pin 128
	ROM_GPIOPinWrite(GPIO_PORTJ_BASE, GPIO_PIN_5, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTJ_BASE, GPIO_PIN_6);	// PJ6, pin 129
	ROM_GPIOPinWrite(GPIO_PORTJ_BASE, GPIO_PIN_6, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTJ_BASE, GPIO_PIN_7);	// PJ7, pin 130
	ROM_GPIOPinWrite(GPIO_PORTJ_BASE, GPIO_PIN_7, 0x00);	// Clear

#if BOARD_SEAPHOX == 1
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTK_BASE, GPIO_PIN_0);	// PK0, pin 16
	ROM_GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_0, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTK_BASE, GPIO_PIN_1);	// PK1, pin 17
	ROM_GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_1, 0x00);	// Clear
#endif

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTK_BASE, GPIO_PIN_5);	// PK5, pin 111
	ROM_GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_5, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTK_BASE, GPIO_PIN_6);	// PK6, pin 110
	ROM_GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_6, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTK_BASE, GPIO_PIN_7);	// PK7, pin 109
	ROM_GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_7, 0x00);	// Clear



#if BOARD_SEAPHOX == 1
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTL_BASE, GPIO_PIN_1);	// PL1, pin 107
	ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_1, 0x00);	// Clear
#endif

#if BOARD_SEAPHOX == 1 || BOARD_MFET == 1 // New 15 Jun 2020
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTL_BASE, GPIO_PIN_2); // PL2, pin 106
    ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_2, 0x00);    // Clear
#endif

#if BOARD_MFET == 2 || BOARD_MPHOX == 1
	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTL_BASE, GPIO_PIN_2);	// PL2, pin 106   5v On signal for optode power
	ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_2, 0x00);	// Clear
#endif

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTL_BASE, GPIO_PIN_3);	// PL3, pin 105
	ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_3, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTL_BASE, GPIO_PIN_4);	// PL4, pin 104
	ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_4, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTL_BASE, GPIO_PIN_5);	// PL5, pin 103
	ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_5, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTL_BASE, GPIO_PIN_6);	// PL6, pin 96
	ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_6, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTM_BASE, GPIO_PIN_0);	// PM0, pin 89
	ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_0, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTM_BASE, GPIO_PIN_1);	// PM1, pin 88
	ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_1, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTM_BASE, GPIO_PIN_2);	// PM2, pin 87
	ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_2, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTM_BASE, GPIO_PIN_3);	// PM3, pin 86
	ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_3, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTM_BASE, GPIO_PIN_6);	// PM6, pin 83  Venable - turns off vbat to adc
	ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_6, 0x00);	// Clear


	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTM_BASE, GPIO_PIN_7);	// PM7, pin 82  EnvVenable - turns on power to env sensor
	ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_7, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_0);	// PN0, pin 81
	ROM_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_1);	// PN1, pin 80
	ROM_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_1, 0x00);	// Clear

	ROM_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_2);	// PN2, pin 20
	ROM_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_2, 0x00);	// Clear


#if BOARD_MFET >= 1 || BOARD_MPHOX == 1        // THOM new Mini-Board - ADC setup
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
    ROM_SysCtlDelay(3);

    //Reset the ADC peripherals
    ROM_SysCtlPeripheralReset(SYSCTL_PERIPH_ADC0);
    //Wait "READY"
    while(!ROM_SysCtlPeripheralReady(SYSCTL_PERIPH_ADC0));

    // Select the external reference for greatest accuracy.
    //ROM_ADCReferenceSet(ADC0_BASE, ADC_REF_EXT_3V);

    //ROM_ADCClockConfigSet(ADC_CLOCK_RATE_HALF);     // 500K rather than default 1M
#endif

    // THOM debug
    profile2_toggle(1);

	/*** Configure SysTick for a 100Hz interrupt.  The FatFs driver and 10 ms timer share a 10 ms tick ***/
	ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / 100);
	ROM_SysTickEnable();
	ROM_SysTickIntEnable();

    //THOM DEBUG
   //uprintf("\nSysTick setup ROM_SysCtlClockGet() = %u\n", ROM_SysCtlClockGet());
   //uprintf("SysTick setup ROM_SysTickPeriodGet() = %u\n",ROM_SysTickPeriodGet());

#if BOARD_MFET == 2 || BOARD_MPHOX == 1
	// if i2c does not have a device, the fram_probe hangs (on MFET v1)
	fram_init();        // i2c0_init(TRUE);     // fast mode 400kpbs, defd in i2c_fram.c
#endif

    /*** Retrieve saved sys_data variables ***/
    retrieveSysDataVariables();         // defd in system.c, moved down in init until after i2c init (in fram)

    // THOM debug
    if(sys_data.state == IDLE)
        dbg_printf("Init.c: sys_data.state = IDLE\n");
    if(sys_data.state == START)
        dbg_printf("Init.c: sys_data.state = START\n");
    if(sys_data.state == COMMAND)
        dbg_printf("Init.c: sys_data.state = COMMAND\n");
    if(sys_data.state == DEPLOYED)
        dbg_printf("Init.c: sys_data.state = DEPLOYED\n");

    // default is 115200 and this is set above - if sys_data.baud_rate is specified, then re(set) here.
    if(sys_data.baud_rate == 9600L)
    {
        UARTStdioConfig(0, 9600, 16000000);       // 9600 bits per second
    }

	// Find out what woke us and decide what to do
	sleep_handler();

	// Initialize MicroSD card and FatFs
	if(microSD_flg == 1)
	{
	    Init_SDCard();

	}

}

#ifdef NOCODE

void init_TM4C123GH6PM_64pin(void)
{
    // Enable lazy stacking for interrupt handlers.  This allows floating-point
    // instructions to be used within interrupt handlers, but at the expense of2
    // extra stack usage.
    ROM_FPULazyStackingEnable();

    //enable floats
    ROM_FPUEnable();

    //set the system clock...50MHZ
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);

    ROM_SysCtlDelay(MILLISECOND*2);       // THOM, fast startup

    //TM4C123GH6PM = 64   (43 GPIO)
    //PA0 - PA7       8
    //PB0 - PB7       8
    //PC0 - PC7       8
    //PD0 - PD7       8
    //PE0 - PE5       6
    //PF0 - PF4       5

    /*** Enable the GPIO ports used ***/
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); //PA0 - PA7       8
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); //PB0 - PB7       8
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); //PC0 - PC7       8
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); //PD0 - PD7       8
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); //PE0 - PE5       6
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); //PF0 - PF4       5

    // THOM TODO  Init GPIO to low power state by default (check processor defaults, this might already be done in hardware)

#ifdef NOCODE_DEFS_FOR_PINS_DAG_NANOFET
    // + means checked, * means different between nanoFET and DAG, nanoFET is subset of DAG
                            NANOFET             //       DAG
    +1        PB6           PB6_ADC_START        // PB6_ADC_START
    +4*        PB7           NC                   // PB7_SHUTDOWN_ISO (not connected in DAG or nFET)
    +5        PF4           PF4_ADC_RESET        // PF4_ADC_RESET
    +6*        PE3           NC                   // PE3_AIN0_VOLTAGE
    +7*        PE2           NC                   // PE2_AIN1_HUMIDITY
    +8*        PE1           NC                   // PE1_AIN2_TMP_CS3
    +9*        PE0           NC                   // PE0_MUX_X6_A1
    +10*       PD7           NC                   // PD7_MUX_X6_A0
    +13       PC7           PC7_LED_RED_WDOG     // PC7_LED_RED_WDOG
    +14       PC6           PC6_GREEN_LED        // PC6_GREEN_LED
    +15*       PC5           PC5_NC               // PC5_SPARE_TX4
    +16*       PC4           PC4_NC               // PC4_SPARE_RX4

    +17       PA0           PA0_CONSOLE_RX       // PA0_CONSOLE_RX0
    +18       PA1           PA1_CONSOLE_TX       // PA1_CONSOLE_TX0
    +19       PA2           PA2_FLASH_CLK        // PA2_FLASH_CLK
    +20       PA3/SSI0FSS   PA3_FLASH_CS0        // PA3_FLASH_CS0
    +21       PA4           PA4_FLASH_MISO       // PA4_FLASH_MISO
    +22       PA5           PA5_FLASH_MOSI       // PA5_FLASH_MOSI
    +23*       PA6           PA6_NC               // PA6_FLASH_CS1
    +24       PA7           PA7_FLASH_HOLD       // PA7_FLASH_CS2   (TODO, FLASH_HOLD vs Chip select?)
    +28*       PF0           PF0_NC               // PF0_CONSOLE_PS
    +29*       PF1           PF1_NC               // PF1_CONSOLE_MODE
    +30*       PF2           PF2_NC               // PF2_DRIVER_MODE
    +31*       PF3           PF3_NC               // PF3_DRIVER_PS

    +43*       PD4           PD4_NC               // PD4_SPARE_RX6
    +44*       PD5           PD5_NC               // PD5_SPARE_TX6
    +45*       PB0/USB0ID    PB0_NC               // PB0_SPARE_RX1
    +46*       PB1/USB0VBUS  PB1_NC               // PB1_SPARE_TX1
    +47       PB2/I2C0SCL   I2C_SCL              // I2C_SCL
    +48       PB3/I2C0SDA   I2C_SDA              // I2C_SDA

    +53       PD6           PD6_DRDY             // PD6_DRDY
    +57       PB5           PB5_ENV_ENABLE       // PB5_NC
    +58       PB4           PB4_NC               // PB4_VENABLE_NC
    +59       PE4           PE4_NC              // PE4_SPARE_RX5
    +60       PE5           PE5_NC              // PE5_SPARE_TX5
    +61       PD0           PD0_SSI3_CLK         // PD0_SSI3_CLK
    +62       PD1           PD1_SSI3_SS_CS       // PD1_SSI3_SS_CS
    +63       PD2           PD2_SSI3_RX_MISO     // PD2_SSI3_RX_MISO
    +64       PD3           PD3_SSI3_TX_MOSI     // PD3_SSI3_TX_MOSI

#endif

    //1        PB6           PB6_ADC_START        // PB6_ADC_START
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_6); // 1        PB6           PB6_ADC_START
    ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_6, 0xFF);  // ADC_START=1 is inactive

#if BOARD_DAG == 1
    //+4*        PB7           NC                   // PB7_SHUTDOWN_ISO
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_7);
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_7, 0xFF);    // Init to 1 (NC on both DAG and nFET)
#endif

    //+5        PF4           PF4_ADC_RESET        // PF4_ADC_RESET
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_4); // 5        PF4           PF4_ADC_RESET
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, 0xFF);    // ADC_RESET to ADS1284 active low reset (init to 1)

#if BOARD_DAG == 1
    ROM_GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3);       //  6        PE3           NC                   // PE3_AIN0_VOLTAGE
    ROM_GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_2);       //  7        PE2           NC                   // PE2_AIN1_HUMIDITY
    ROM_GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_1);       //  8        PE1           NC                   // PE1_AIN2_TMP_CS3

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_0); //  9        PE0           NC                   // PE0_MUX_X6_A1
    ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_0, 0x00);    //

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_7); //  10       PD7           NC                   // PD7_MUX_X6_A0
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_7, 0x00);    //
#endif

    led_init();     // defd in system.c
#ifdef OLDCODE6Apr2021

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_7);  // 13       PC7           PC7_LED_RED_WDOG
    ROM_GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_7, 0x00);     // ??? TBD high or low as inactive

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_6);  // 14       PC6           PC6_GREEN_LED
    ROM_GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_6, 0x00);     // ??? TBD high or low as inactive
#endif

#if BOARD_DAG == 1
    /*** Initialize UART4 ***/
    //15       PC5           PC5_NC               //PC5_SPARE_TX4
    //16       PC4           PC4_NC               //PC4_SPARE_RX4
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART4);
    ROM_GPIOPinConfigure(GPIO_PC5_U4TX);
    ROM_GPIOPinConfigure(GPIO_PC4_U4RX);
    ROM_GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_4 | GPIO_PIN_5);
    UARTClockSourceSet(UART4_BASE, UART_CLOCK_PIOSC);
    MicroCAT_UARTConfig(4, 9600, 16000000);       // 115200 bits per second
    MicroCAT_UARTEchoSet(0);    // Disable echo to MicroCAT
#endif


    /*** Initialize UART0 for use as the console, printf, etc ***/
    //17       PA0/U0RX      PA0_CONSOLE_RX0
    //18       PA1/U0TX      PA1_CONSOLE_TX0
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
    ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
    ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
    UARTStdioConfig(0, 115200, 16000000);       // 115200 bits per second
    UARTEchoSet(true);  // Enable console echo

    //19 - 24 are SPI for FLASH/SDCARD, set in  init_SDcard()   TODO, init pins for low power in case SD card is not usedc

    //28 - 46 are NC for nanoFET (DAg uses pins)

#if BOARD_DAG == 1
    // TODO: DAg will require proper settings for these gpio pins
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0); // 28       PF0           PF0_NC               //PF0_CONSOLE_PS
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0x00);    // Clear

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1); // 29       PF1           PF1_NC               //PF1_CONSOLE_MODE
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0x00);    // Clear

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2); // 30       PF2           PF2_NC               //PF2_DRIVER_MODE
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0x00);    // Clear

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_3); // 31       PF3           PF3_NC               //PF3_DRIVER_PS
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0x00);    // Clear


    // THOM TODO: if not using UART6, then config for low power (move these outside BOARD_DAG)
    //ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_4); // 43       PD4           PD4_NC           //PD4_SPARE_RX6
    //ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_4, 0x00);    // Clear
    //ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_5); // 44       PD5           PD5_NC           //PD5_SPARE_TX6
    //ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_5, 0x00);    // Clear


    /*** Initialize UART6 ***/
    // 43       PD4           PD4_NC           //PD4_SPARE_RX6
    // 44       PD5           PD5_NC           //PD5_SPARE_TX6
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART6);
    ROM_GPIOPinConfigure(GPIO_PD4_U6RX);                    // 43       PD4           PD4_NC           //PD4_SPARE_RX6
    ROM_GPIOPinConfigure(GPIO_PD5_U6TX);                    // 44       PD5           PD5_NC           //PD5_SPARE_TX6
    ROM_GPIOPinTypeUART(GPIO_PORTD_BASE, GPIO_PIN_4 | GPIO_PIN_5);
    UARTClockSourceSet(UART6_BASE, UART_CLOCK_PIOSC);
    // init_UART6(115200, 16000000, 0);    // THOM TODO refactor uart code, pass in  baud, clock, echoFlg
        //MicroCAT_UARTConfig(4, 9600, 16000000);       // 115200 bits per second
        //MicroCAT_UARTEchoSet(0);    // Disable echo to MicroCAT

    // THOM TODO (same as above for UART6 GPIO when not used, set for lowest power)
    //ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_0); // 45       PB0/USB0ID    PB0_NC           //PB0_SPARE_RX1
    //ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0, 0x00);    // Clear
    //ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_1); // 46       PB1/USB0VBUS  PB1_NC           //PB1_SPARE_TX1
    //ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_1, 0x00);    // Clear

    /*** Initialize UART1 ***/
    // 45       PB0/USB0ID    PB0_NC           //PB0_SPARE_RX
    // 46       PB1/USB0VBUS  PB1_NC           //PB1_SPARE_TX1
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1);
    ROM_GPIOPinConfigure(GPIO_PB0_U1RX);                    // 45       PB0/USB0ID    PB0_NC           //PB0_SPARE_RX1
    ROM_GPIOPinConfigure(GPIO_PB1_U1TX);                    // 46       PB1/USB0VBUS  PB1_NC           //PB1_SPARE_TX1
    ROM_GPIOPinTypeUART(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    UARTClockSourceSet(UART1_BASE, UART_CLOCK_PIOSC);
    // init_UART1(115200, 16000000);    // THOM TODO refactor uart code, pass in  baud, clock, echoFlg


#endif

    // THOM TODO, figure out best way to manage I2C for low power, default is to set as GPIO high so to not pull down resistors for now
    // 47       PB2/I2C0SCL    I2C_SCL
    // 48       PB3/I2C0SDA    I2C_SDA
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_2); // 47       PB2/I2C0SCL    I2C_SCL
    ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_2, 0xFF);    // Set high (maybe should float?)
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_3); // 48       PB3/I2C0SDA    I2C_SDA
    ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_3, 0xFF);    // Set

    // FRAM init calls i2c_init - need to figure out peripheral init backplane policy (recommend calling i2c_init here)
    // void i2c0_init(bool bFast)

    // DRDY input pin for ADS1284
    ROM_GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_6);      // Input   53       PD6           PD6_DRDY


    // NC on both DAG and nanoFET, DAG has named the pins
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_5);     // 57       PB5           PB5_ENV_ENABLE (no connect on DAG)
    ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, 0x00);
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_4);     // 58       PB4           PB4_NC           //PB4_VENABLE_NC (no connect on DAG)
    ROM_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_4, 0x00);


#if BOARD_DAG == 1

    /*** Initialize UART1 ***/
    // 59       PE4           PE4_NC        // PE4_SPARE_RX5
    // 60       PE5           PE5_NC        // PE5_SPARE_TX5
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART5);
    ROM_GPIOPinConfigure(GPIO_PE4_U5RX);                    // 59       PE4           PE4_NC        // PE4_SPARE_RX5
    ROM_GPIOPinConfigure(GPIO_PE5_U5TX);                    // 60       PE5           PE5_NC        // PE5_SPARE_TX5
    ROM_GPIOPinTypeUART(GPIO_PORTE_BASE, GPIO_PIN_4 | GPIO_PIN_5);
    UARTClockSourceSet(UART5_BASE, UART_CLOCK_PIOSC);
    // init_UART5(115200, 16000000, 0);    // THOM TODO refactor uart code, pass in  baud, clock, echoFlg

#endif

    /*** Initialize ADS1248 pins as GPIO to reduce power consumption, SPI config happens in ADS1284_xxx.c ***/
    // THOM TODO on nanoFET and DAG - ADS1284 routines need tweaking
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_0);     // SSI3CLK  61       PD0           PD0_SSI3_CLK
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0x00);

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_1);     // SSI3Fss  62       PD1           PD1_SSI3_SS_CS
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1, 0x00);

    ROM_GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_2);      // SSI3Rx   63       PD2           PD2_SSI3_RX_MISO

    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_3);     // SSI3Tx   64       PD3           PD3_SSI3_TX_MOSI
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3, 0x00);


    //---------- Done with 'pin' init --------------------------


    /*** Configure SysTick for a 100Hz interrupt.  The FatFs driver and 10 ms timer share a 10 ms tick ***/
    ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / 100);
    ROM_SysTickEnable();
    ROM_SysTickIntEnable();


    //THOM DEBUG
   // uprintf("\nSysTick setup ROM_SysCtlClockGet() = %u\n", ROM_SysCtlClockGet());
   // uprintf("SysTick setup ROM_SysTickPeriodGet() = %u\n",ROM_SysTickPeriodGet());

    // if i2c does not have a device, the fram_probe hangs (on MFET v1)   THOM TODO enhance error handling
    fram_init();        // calls i2c0_init(TRUE);     // fast mode 400kpbs, defd in i2c_fram.c


    /*** Retrieve saved sys_data variables ***/
    retrieveSysDataVariables();         // defd in system.c, moved down in init until after i2c init (in fram)

    // THOM debug
    if(sys_data.state == IDLE)
        dbg_printf("Init.c: sys_data.state = IDLE\n");
    if(sys_data.state == START)
        dbg_printf("Init.c: sys_data.state = START\n");
    if(sys_data.state == COMMAND)
        dbg_printf("Init.c: sys_data.state = COMMAND\n");
    if(sys_data.state == DEPLOYED)
        dbg_printf("Init.c: sys_data.state = DEPLOYED\n");

    // default is 115200 and this is set above - if sys_data.baud_rate is specified, then re(set) here.
    if(sys_data.baud_rate == 9600L)
    {
        UARTStdioConfig(0, 9600, 16000000);       // 9600 bits per second
    }

    // Find out what woke us and decide what to do THOM TODO - new fresh nanoFET hangs in sleep handler (retrieveSysDataVariable are not setup???
#ifdef CODEHANGS_SLEEPHANDLER_SUSPECT32k
    sleep_handler();
#endif

    sys_data.state = IDLE;      // Drop through to main menu

    // 19       PA2           PA2_FLASH_CLK        // PA2_FLASH_CLK
    // 20       PA3/SSI0FSS   PA3_FLASH_CS0        // PA3_FLASH_CS0
    // 21       PA4           PA4_FLASH_MISO       // PA4_FLASH_MISO
    // 22       PA5           PA5_FLASH_MOSI       // PA5_FLASH_MOSI
    // 23*       PA6           PA6_NC              // PA6_FLASH_CS1
    // 24       PA7           PA7_FLASH_HOLD       // PA7_FLASH_CS2   (TODO, FLASH_HOLD vs Chip select?)

    // Initialize MicroSD card and FatFs
    //ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
    //ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);  //PA2 to PA5
    if(microSD_flg == 1)
    {
        Init_SDCard();

        sd_format();   //hangs in disk initialize
    }


}

#endif
