/*
 * power.c
 *
 *  Created on: Apr 19, 2021
 *      Author: Thom Maughan (TM
 */



#include "system.h"
#include "pwm.h"
#include "board_util.h"
#include "user_io.h"

//extern int profileTarget;
//extern int profile2Target;

int profileTarget = 0;
int profile2Target = 0;

unsigned int ledGreenState = 0;
unsigned int ledRedState = 0;

struct watchDog wdog;

extern struct pwmDriver pwmSoln;
extern struct pwmDriver pwmPump;

extern uint32_t serDbgFlg;          // defd in serial.c

/*
 * Cause a HardFault on ARM by accessing an address that does not exist on 64pin part
 *
 * Call this function to test watch dog
 */
void crash_phfetsos(void)
{
    volatile uint32_t* x = (uint32_t*)0xFFFFFFFF;
    volatile uint32_t y = 0;
    y = *x;
    for(;;)
    {
        // Spinning on WFI can crash, as not all peripherals are set up correctly for sleep mode
        // TODO: configure needed peripherals to be awake in sleep using MAP_SysCtlPeripheralSleepEnable
        // See tm4c manual caution in section 5.2.6.2
        // __wfi();
    }
}



int wdog_init(void)
{
    wdog.tickStart = Timer_1ms();
    return wdog_resetMon();
}

int wdog_pet(void)
{
    uint32_t ticks;
    // grab the time to measure how often pet happens APFTODO

    ticks = Timer_1ms();
    wdog.tickTot = (ticks - wdog.tickStart);        // time since wdog_init (uptime)
    wdog.tickDelta = ticks - wdog.tickDelta;
    if(wdog.tickDelta > wdog.tickLongest)
    {
        wdog.tickLongest = wdog.tickDelta;          // keep track of worse case pet time.   Must be less than 1.0sec
        //uprintf("DEV: wdog.longest = %u, tot = %u", wdog.tickLongest, wdog.tickTot); prn_nl();  uprintf(">");  //DEV is for development tuning / profiling code, related by different than DBG:
    }
    wdog.tickDelta = ticks;
    wdog.tickTot = (ticks - wdog.tickStart);        // time since wdog_init (uptime)

#if BOARD_MSC == 3
    #if defined(PART_TM4C1290NCPDT)
        // Assert WD_PetDog to reset external wdt, Per Tiva 1290 rA schematic, STWD100NYWY3F
        //'Pet dog', per datasheet by toggling the WDI input (low-to-high on all versions and high-to-low on the STWD100xW, STWD100xX and STWD100xY only).
        // we have high to low transition part
        //led_red(1);
        //MAP_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_5, 0xFF);
        MAP_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_5, 0x00);            // hi->lo transition for STWD100NYWY3F
        _dsb();
        _isb();
        MAP_SysCtlDelay(MICROSECOND*10);

        // Release WD_PetDog
        //MAP_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_5, 0x00);
        MAP_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_5, 0xFF);
        _dsb();
        _isb();
        MAP_SysCtlDelay(MICROSECOND*10);
        //led_red(0);
    #elif defined(TIVA_MOD_REVB) // defined(PART_TM4C123GH6PM)
        // Assert WD_PetDog to reset external wdt
        MAP_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0xFF);
        _dsb();
        _isb();
        MAP_SysCtlDelay(MICROSECOND*10);

        // Release WD_PetDog
        MAP_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0x00);
        _dsb();
        _isb();
        MAP_SysCtlDelay(MICROSECOND*10);
    #else
        #error "Unknown part"
    #endif
#else
    #error "Unknown board"
#endif

    return 1;
}

int wdog_resetMon(void)
{
#if BOARD_MSC == 3
    #if defined(PART_TM4C1290NCPDT)
        // Assert /WD_ResetMon to async clear the flip-flop
        MAP_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_6, 0x00);
        _dsb();
        _isb();
        MAP_SysCtlDelay(MICROSECOND*10);

        // Release /WD_ResetMon
        MAP_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_6, 0xFF);
        _dsb();
        _isb();
        MAP_SysCtlDelay(MICROSECOND*10);
    #elif defined(PART_TM4C123GH6PM)

        // Assert /WD_ResetMon to async clear the flip-flop
        MAP_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0x00);
        _dsb();
        _isb();
        MAP_SysCtlDelay(MICROSECOND*10);

        // Release /WD_ResetMon
        MAP_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0xFF);
        _dsb();
        _isb();
        MAP_SysCtlDelay(MICROSECOND*10);
    #else
        #error "Unknown part"
    #endif
#else
    #error "Unknown board"
#endif

    return 1;
}

//prior to wdog_init, read the monitor
int wdog_readMon(void)
{
    // read watchdog monitor FF, done before init

    // if WD_Monitor is high, the ext watchdog tripped
    // if WD_Monitor is low, the ext watchdog has not tripped

    int32_t retVal;

#if BOARD_MSC == 3
    #if defined(PART_TM4C1290NCPDT)
        retVal = MAP_GPIOPinRead(GPIO_PORTM_BASE, GPIO_PIN_7);
    #elif defined(PART_TM4C123GH6PM)
        retVal = MAP_GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_7);
    #else
        #error "Unknown part"
    #endif
#else
    #error "Unknown board"
#endif

    //normalize bitfield
    retVal = (retVal) ? (1) : (0);

    return retVal;
}

void pwr_uSD_on(void)
{
    pwr_flash_on();
}

void pwr_uSD_off(void)
{
    pwr_flash_off();
}

void pwr_flash_on(void)
{
#if BOARD_MSC == 3
    #if defined(PART_TM4C1290NCPDT)
        MAP_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_2, 0xFF);
        delay_msec(3);
    #elif defined(PART_TM4C123GH6PM)
        MAP_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_5, 0xFF);
        delay_msec(3);
    #else
        #error "Unknown part"
    #endif
#else
    #error "Unknown board"
#endif
}

void pwr_flash_off(void)
{
#if BOARD_MSC == 3
    #if defined(PART_TM4C1290NCPDT)
        MAP_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_2, 0x00);
        delay_msec(3);
    #elif defined(PART_TM4C123GH6PM)
        MAP_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_5, 0x00);
        delay_msec(3);
    #else
        #error "Unknown part"
    #endif
#else
    #error "Unknown board"
#endif
}

int uSD_present(void)
{
#if BOARD_MFET >= 2 || BOARD_MPHOX >= 1 || BOARD_MSC == 2
    if(MAP_GPIOPinRead(GPIO_PORTL_BASE, GPIO_PIN_5) == 0)
    {
        return 1;       // uSD is present when GPIO pin 5 on PortL is ground on both MPHOX and MFET
    }
    else
    {
        return 0;
    }
#elif BOARD_MSC == 3
    #if defined(PART_TM4C1290NCPDT)
        if(MAP_GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_6) == 0)
        {
            return 1;       // uSD is present when GPIO pin 6 on PortA is ground on serlog_msc3_tiva1290_v1_x
        }
        else
        {
            return 0;
        }
    #elif defined(PART_TM4C123GH6PM)
        if(MAP_GPIOPinRead(GPIO_PORTA_BASE, GPIO_PIN_6) == 0)
        {
            return 1;       // uSD is present when GPIO pin 6 on PortA is ground on MSC3 Tiva RevA
        }
        else
        {
            return 0;
        }
    #else
        #error "Unknown part"
    #endif
#elif BOARD_NANOFET == 1
    return(0);
#else
    #error "Unknown board"
#endif
}

// console fxns are a routine (MFET_BOARD)  THOM
void console_off(void)
{
#if BOARD_SEAPHOX == 1
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_0, 0x00);        // LTC2801 PS
#endif

#if BOARD_MFET == 1
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_0, 0x00);        // LTC2801 PS
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_1, 0x00);        // LTC2801 MODE
#endif

#if BOARD_MFET == 2
    // MFET revB has MAX3222EETP+
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_0, 0xff);    // Set pin to turn console driver off // PL0, pin 108
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_1, 0x00);    // Clear to ensable shutdown  // PL0, pin 107
#endif

#if BOARD_MPHOX == 1 || BOARD_MSC == 2
    // MpHOx revA has ADM3312
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_0, 0xff);    // Set pin 108 PL0 to turn console driver off
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_1, 0xff);    // Clear pin 107 PL1 to disable shutdown (part is enabled)
#endif

#if BOARD_MSC == 3
    // THOM TODO APFTODO
    // MSC3 Tiva Module has MAX3222EETP+
    //uprintf("\r\nconsole disable is not ported on MSC3, board_util.c, line 82");  prn_nl();
    //MAP_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, 0xff);    // Set pin to turn console driver off // PL0, pin 108
#endif

}

void console_on(void)
{

#if BOARD_SEAPHOX == 1
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_0, 0xff);        // LTC2801 PS
#endif

#if BOARD_MFET == 1
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_0, 0xff);        // LTC2801 PS
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_1, 0xff);        // LTC2801 MODE
#endif


#if BOARD_MFET == 2
    // MFET revB has MAX3222EETP+
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_0, 0x00);    // Clear pin to turn console driver on // PL0, pin 108
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_1, 0xff);    // Set to disable shutdown (part is enabled) // PL0, pin 107
#endif

#if BOARD_MPHOX == 1  || BOARD_MSC == 2
    // MpHOx revA has ADM3312
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_0, 0x00);    // Set pin 108 PL0 to turn console driver off
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_1, 0x00);    // Clear pin 107 PL1 to disable shutdown (part is enabled)
#endif
#if BOARD_NANOFET == 1
    // No transceiver enable on nanofet
#endif

#if BOARD_MSC == 3
    // MSC3 Tiva Module has MAX3222EETP+
    if(serDbgFlg == 1) {prn_nl(); uprintf("DBG: console enable is not ported on MSC3, board_util.c, line 118"); prn_nl(); }
    //MAP_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, 0x00);    // 0 to turn console driver on
#endif
}

#if BOARD_MSC == 3
//void console_rts(uint32_t state)
// rts output to APF
void sys_rts(uint32_t state)
{
    if(state == 0)
    {
        MAP_GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_0, 0x00);
    }
    else
    {
        MAP_GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_0, 0xFF);
    }

}

//uint32_t console_cts(void)
// read system cts state, input from APF
uint32_t sys_cts(void)
{
    if(MAP_GPIOPinRead(GPIO_PORTH_BASE, GPIO_PIN_1) == 0)
    {
        return(0);
    }
    else
    {
        return(1);
    }

}
#endif



void spare_rs232_on(void)
{

}

void spare_rs232_off(void)
{

}


// PORTXXX is used for UART5 - empty stubs with old SEAPHOX gpio NOT AVAILABLE on MBARI boards
void PORTXXX_on(void)
{
#if BOARD_MFET >= 2 || BOARD_MPHOX >= 1 || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_7, 0xff);    // Power on
#endif
}
void PORTXXX_off(void)
{
#if BOARD_MFET >= 2 || BOARD_MPHOX >= 1 || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_7, 0x00);    // Power off
#endif
}



void pwr_env_mon_on(void)
{
#if BOARD_MFET >= 2 || BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_7, 0xFF);    // Turn on Load switch for ENV monitoring
#endif
#if BOARD_NANOFET >= 1
    MAP_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, 0xFF);    // Turn on Load switch for ENV monitoring
#endif
}

void pwr_env_mon_off(void)
{
    // NOTE TODO check why ENV load switch is not turned off
#if BOARD_MFET >= 2 || BOARD_MPHOX >= 1 || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_7, 0x00);    // Turn off Load switch for ENV monitoring
#endif
#if BOARD_NANOFET >= 1
    MAP_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, 0xFF);    // Turn on Load switch for ENV monitoring
#endif
}

void pwr_vbat_mon_on(void)
{
#if BOARD_MFET >= 2 || BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_6, 0xFF);   // Turn on VBat monitoring
#endif
}
void pwr_vbat_mon_off(void)
{
#if BOARD_MFET >= 2 || BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_6, 0x00);   // Turn off VBat monitoring
#endif
}

void pwr_iso_on(void)
{
#if BOARD_MFET >= 2 || BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_2, 0xff);  // Iso-power On
#endif
#if BOARD_MSC == 3
    //uprintf("\r\npwr_iso_on is not ported on MSC3, board_util.c, line 190\r\n");
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_3, 0xff);  // Iso-power On, schematic pH_pwrEnable
#endif
}

void pwr_iso_off(void)
{
#if BOARD_MFET >= 2 || BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_2, 0x00);        // Iso-power Off
#endif
#if BOARD_MSC == 3
    //uprintf("\r\npwr_iso_off is not ported on MSC3, board_util.c, line 201\r\n");
    //MAP_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_0, 0x00);  // Iso-power Off
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_3, 0x00);  // Iso-power Off
#endif
}


void pwr_ctd_on(void)
{
#if BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0xff); // PD0
#endif
}

void pwr_ctd_off(void)
{
#if BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0x00); // PD0
#endif
}

void pwr_aux_pwr1_on(void)
{
#if BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1, 0xff); // PD1
#endif
}

void pwr_aux_pwr1_off(void)
{
#if BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1, 0x00); // PD1
#endif
}

void pwr_aux_pwr2_on(void)
{
#if BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2, 0xff); // PD2
#endif
}

void pwr_aux_pwr2_off(void)
{
#if BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2, 0x00); // PD2
#endif
}

// PUMP4
void pwr_aux_pwr3_on(void)   // shared with uart2 on J14 COMM-PWR port on MPHOX
{
#if BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3, 0xff); // PD3
#endif
}

void pwr_aux_pwr3_off(void)
{
#if BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3, 0x00); // PD3
#endif
}



void pwr_soln_on(void)   // solenoid, function name matches silk on schematic
{
#if BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_1, 0xff);  // PG1 is L on schematic
    MAP_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_3, 0xff);  // PG3 is H on schematic (PG3 is PWM capable, PG1 is not
    pwmSoln.gpio = 1;
#endif

}

void solenoid_hit(void)
{
#if BOARD_MPHOX >= 1  || BOARD_MSC == 2
    pwmSoln_stop(1);        //pwr_soln_on();
#endif
}

void solenoid_hold(unsigned int pwmVal)
{
#if BOARD_MPHOX >= 1  || BOARD_MSC == 2
    pwmSoln_set(pwmVal);
#endif
}

void solenoid_off(void)
{
#if BOARD_MPHOX >= 1  || BOARD_MSC == 2
    pwmSoln_stop(0);        // turn PWM module off and restore GPIO pin function as output lo

    // TODO reconfigure PG3 as GPIO output
    //MAP_GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_3); // PG3, pin 52
    //MAP_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_3, 0x00);    // Clear

    //pwr_soln_off();     // not needed except for PG1, need to cleanup pwm code now that the onchip peripheral is working
#endif
}

void solenoid_on(void)
{
#if BOARD_MPHOX >= 1  || BOARD_MSC == 2
    pwmSoln_stop(1);    // turn PWM module off and restore GPIO pin function as output hi

    // reconfigure PG3 as GPIO output
    //MAP_GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_3); // PG3, pin 52
    //MAP_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_3, 0xFF);    // Clear

    //pwr_soln_on();     // not needed except for PG1, need to cleanup pwm code now that the onchip peripheral is working
#endif
}

void pwr_soln_off(void)
{
#if BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_1, 0x00);  // PG1
    MAP_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_3, 0x00);    // PG3
    pwmSoln.gpio = 0;
#endif
}



void pump_on(void)      // AUX1=PUMP1
{
#if BOARD_MPHOX == 1  || BOARD_MSC == 2
    //J12 AUX-PWR
    pwr_aux_pwr1_on();
    pwmPump.gpio = 1;
#endif
#if BOARD_MFET >= 1 || BOARD_NANOFET >= 1
    // no pump on MFET or NANOFET
#endif

#if BOARD_MSC == 3
    if(serDbgFlg == 1) {uprintf("pump_on is PCAL6416 on MSC3 baseboard, not supported yet board_util.c line 339"); prn_nl(); }
#endif

}

void pump_off(void)
{
//#if BOARD_MFET >= 1 || BOARD_MPHOX == 1
#if BOARD_MPHOX == 1  || BOARD_MSC == 2
    //J12 AUX-PWR
    pwr_aux_pwr1_off();
    pwmPump.gpio = 0;
#endif
#if BOARD_MFET >= 1 || BOARD_NANOFET >= 1
    // no pump on MFET or NANOFET
#endif

#if BOARD_MSC == 3
	if(serDbgFlg == 1) { uprintf("DBG: pump_off is PCAL6416 on MSC3 baseboard, not supported yet board_util.c line 357");  prn_nl(); }
#endif
}

void pump1_on(void)
{
    pump_on();
}

void pump1_off(void)
{
    pump_off();
}

void pump2_on(void)     // SOLN=PUMP2
{
    pwr_aux_pwr2_on();     // was pwr_soln_on();
}

void pump2_off(void)
{
    pwr_aux_pwr2_off();     // was pwr_soln_on();
}

void pump3_on(void)     // AUX3=PUMP3
{
    pwr_aux_pwr3_on();    //
}

void pump3_off(void)
{
    pwr_aux_pwr3_on();
}

void pump4_on(void)     // SOLN=PUMP4
{
    pwr_soln_on();    //
}

void pump4_off(void)
{
    pwr_soln_off();    //
}


void pressure_on(void)
{
#if BOARD_MFET >= 1 || BOARD_MPHOX == 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_5, 0xff);
#endif
#if BOARD_NANOFET >= 1
    //MAP_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0xff);   // unused pin on nanoFET
#endif
}

void pressure_off(void)
{
#if BOARD_MFET >= 1 || BOARD_MPHOX == 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_5, 0x00);
#endif
#if BOARD_NANOFET >= 1
    //MAP_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0x00);   // unused pin on nanoFET
#endif
}


// profileTarget is a global variable set programmatically
void profile_toggle(int target)
{
    if(target == profileTarget)
    {
        if(ledRedState == 0)
            led_red_on();
        else
            led_red_off();
    }
}


void profile_off(int target)
{
    if(target == profileTarget)
    {
        led_red_off();
    }
}


void profile_on(int target)
{
    if(target == profileTarget)
    {
        led_red_on();
    }
}

void led_init(void)
{
    ledRedState = 0;
    ledGreenState = 0;
#if BOARD_MFET >= 1 || BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_3); // Green LED (PE3, pin 12)
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTP_BASE, GPIO_PIN_2); // Red LED (PP2, pin 11)  (HMMM)

    MAP_GPIOPinWrite(GPIO_PORTP_BASE, GPIO_PIN_2, 0x00);    // Red LED off
    MAP_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0x00);    // LED GREEN off
#endif
#if BOARD_NANOFET >= 1
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_6);  // 14       PC6           PC6_GREEN_LED
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_7);  // 13       PC7           PC7_RED_LED

    MAP_GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_7, 0x00);    // LED RED off
    MAP_GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_6, 0x00);    // LED GREEN off
#endif

#if BOARD_MSC == 3
#ifdef PART_TM4C123GH6PM
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_3);  // PF3          PF3_GREEN_LED
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);  // PF2          PF2_RED_LED

    MAP_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0x00);    // LED GREEN off
    MAP_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0x00);    // LED RED off
#endif
#ifdef PART_TM4C1290NCPDT
    MAP_GPIOPinWrite(GPIO_PORTQ_BASE, GPIO_PIN_1, 0x00);    // LED GREEN off
    MAP_GPIOPinWrite(GPIO_PORTQ_BASE, GPIO_PIN_0, 0x00);    // LED RED off

    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTQ_BASE, GPIO_PIN_1);  // PK5 LED_GRN
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTQ_BASE, GPIO_PIN_0);  // PF4 RED_LED
#endif
#endif

#if BOARD_TESTFIT >= 1
    //PG7=RED, PG6=GRN
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_6);  // 87       PG6           PG6_RED_LED
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_7);  // 88       PG7           PG7_GRN_LED

    MAP_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_6, 0x00);    // LED RED off
    MAP_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_7, 0x00);    // LED GRN off

#endif

}

void led_red(int state)
{
    if(state == 0)
        led_red_off();
    else
        led_red_on();
}

void led_red_toggle(void)
{
    if(ledRedState == 0)
        led_red_on();
    else
        led_red_off();
}

void led_red_off(void)
{
#if BOARD_MFET >= 1 || BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTP_BASE, GPIO_PIN_2, 0x00);    // Red LED off
#endif
#if BOARD_NANOFET >= 1
    MAP_GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_7, 0x00);    // LED RED off
#endif
#if BOARD_MSC == 3
#ifdef PART_TM4C123GH6PM
    MAP_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0x00);    // PF2 LED RED off
#endif
#ifdef PART_TM4C1290NCPDT
    MAP_GPIOPinWrite(GPIO_PORTQ_BASE, GPIO_PIN_0, 0x00);    // PQ0 LED RED off
#endif
#endif
#if BOARD_TESTFIT >= 1
    MAP_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_6, 0x00);    // LED RED off
#endif
    ledRedState = 0;
}

void led_red_on(void)
{
#if BOARD_MFET >= 1 || BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTP_BASE, GPIO_PIN_2, 0xFF);    // Red LED on
#endif
#if BOARD_NANOFET >= 1
    MAP_GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_7, 0xFF);    // LED RED on
#endif
#if BOARD_MSC == 3
#ifdef PART_TM4C123GH6PM
    MAP_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0xFF);    // PF2 LED RED on
#endif
#ifdef PART_TM4C1290NCPDT
    MAP_GPIOPinWrite(GPIO_PORTQ_BASE, GPIO_PIN_0, 0xFF);    // PQ0 LED RED on
#endif
#endif
#if BOARD_TESTFIT >= 1
    MAP_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_6, 0xFF);    // LED RED on
#endif
    ledRedState = 1;
}

// PORTE.3

void led_green(int state)
{
    if(state == 0)
        led_green_off();
    else
        led_green_on();
}

void led_green_toggle(void)
{
    if(ledGreenState == 0)
        led_green_on();
    else
        led_green_off();
}

void led_green_off(void)
{
#if BOARD_MFET >= 1 || BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0x00);    // LED GREEN off
#endif
#if BOARD_NANOFET >= 1
    MAP_GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_6, 0x00);    // LED GREEN off
#endif
#if BOARD_MSC == 3
#ifdef PART_TM4C123GH6PM
    MAP_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0x00);    // PF3 LED GREEN off
#endif
#ifdef PART_TM4C1290NCPDT
    MAP_GPIOPinWrite(GPIO_PORTQ_BASE, GPIO_PIN_1, 0x00);    // PQ1 LED GREEN off
#endif
#endif
#if BOARD_TESTFIT >= 1
    MAP_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_7, 0x00);    // LED GRN off
#endif
    ledGreenState = 0;
}

void led_green_on(void)
{
#if BOARD_MFET >= 1 || BOARD_MPHOX >= 1  || BOARD_MSC == 2
    MAP_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0xFF);    // LED on
#endif
#if BOARD_NANOFET >= 1
    MAP_GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_6, 0xFF);    // LED GREEN on
#endif
#if BOARD_MSC == 3
#ifdef PART_TM4C123GH6PM
    MAP_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0xFF);    // PF3 LED GREEN on
#endif
#ifdef PART_TM4C1290NCPDT
    MAP_GPIOPinWrite(GPIO_PORTQ_BASE, GPIO_PIN_1, 0xFF);    // PQ1 LED GREEN on
#endif
#endif
#if BOARD_TESTFIT >= 1
    MAP_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_7, 0xFF);    // LED GRN on
#endif
    ledGreenState = 1;
}


void led_power_on(void)
{
    led_green_on();
}

void led_power_off(void)
{
    led_green_off();
}

void led_sampling_on(void)
{
    led_red_on();
}

void led_sampling_off(void)
{
    led_red_off();
}

void profile2_toggle(int target)
{
    if(target == profile2Target)
    {
        if(ledGreenState == 0)
            led_green_on();
        else
            led_green_off();
    }
}


void profile2_off(int target)
{
    if(target == profile2Target)
    {
        led_green_off();
    }
}

void profile2_on(int target)
{
    if(target == profile2Target)
    {
        led_green_on();
    }
}

