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



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

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

int profileTarget = 0;
int profile2Target = 0;

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

extern struct pwmDriver pwmSoln;
extern struct pwmDriver pwmPump;


int uSD_present(void)
{
#if BOARD_MFET >= 2 || BOARD_MPHOX >= 1 || BOARD_MSC == 2
    if(ROM_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;
    }
#endif
#if BOARD_NANOFET == 1
    return(0);
#endif
}

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

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

#if BOARD_MFET == 2
    // MFET revB has MAX3222EETP+
    ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_0, 0xff);    // Set pin to turn console driver off // PL0, pin 108
    ROM_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
    ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_0, 0xff);    // Set pin 108 PL0 to turn console driver off
    ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_1, 0xff);    // Clear pin 107 PL1 to disable shutdown (part is enabled)
#endif

}

void console_on(void)
{

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

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


#if BOARD_MFET == 2
    // MFET revB has MAX3222EETP+
    ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_0, 0x00);    // Clear pin to turn console driver on // PL0, pin 108
    ROM_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
    ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_0, 0x00);    // Set pin 108 PL0 to turn console driver off
    ROM_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
}

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
    ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_7, 0xFF);    // Turn on Load switch for ENV monitoring
#endif
#if BOARD_NANOFET >= 1
    ROM_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
    ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_7, 0x00);    // Turn off Load switch for ENV monitoring
#endif
#if BOARD_NANOFET >= 1
    ROM_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
    ROM_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
    ROM_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
    ROM_GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_2, 0xff);  // Iso-power Off
#endif
}

void pwr_iso_off(void)
{
#if BOARD_MFET >= 2 || BOARD_MPHOX >= 1  || BOARD_MSC == 2
    ROM_GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_2, 0x00);        // Iso-power Off
#endif
}


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

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

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

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

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

void pwr_aux_pwr2_off(void)
{
#if BOARD_MPHOX >= 1  || BOARD_MSC == 2
    ROM_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
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3, 0xff); // PD3
#endif
}

void pwr_aux_pwr3_off(void)
{
#if BOARD_MPHOX >= 1  || BOARD_MSC == 2
    ROM_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
    ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_1, 0xff);  // PG1 is L on schematic
    ROM_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
    //ROM_GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_3); // PG3, pin 52
    //ROM_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
    //ROM_GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_3); // PG3, pin 52
    //ROM_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
    ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_1, 0x00);  // PG1
    ROM_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

}

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
}

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
    ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_5, 0xff);
#endif
#if BOARD_NANOFET >= 1
    //ROM_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
    ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_5, 0x00);
#endif
#if BOARD_NANOFET >= 1
    //ROM_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
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_3); // Green LED (PE3, pin 12)
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTP_BASE, GPIO_PIN_2); // Red LED (PP2, pin 11)  (HMMM)

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

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

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

    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0x00);    // LED GREEN off
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0x00);    // LED RED off
#endif

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

    ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_6, 0x00);    // LED RED off
    ROM_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
    ROM_GPIOPinWrite(GPIO_PORTP_BASE, GPIO_PIN_2, 0x00);    // Red LED off
#endif
#if BOARD_NANOFET >= 1
    ROM_GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_7, 0x00);    // LED RED off
#endif
#if BOARD_MSC == 3
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0x00);    // PF2 LED RED off
#endif
#if BOARD_TESTFIT >= 1
    ROM_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
    ROM_GPIOPinWrite(GPIO_PORTP_BASE, GPIO_PIN_2, 0xFF);    // Red LED on
#endif
#if BOARD_NANOFET >= 1
    ROM_GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_7, 0xFF);    // LED RED on
#endif
#if BOARD_MSC == 3
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0xFF);    // PF2 LED RED on
#endif
#if BOARD_TESTFIT >= 1
    ROM_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
    ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0x00);    // LED GREEN off
#endif
#if BOARD_NANOFET >= 1
    ROM_GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_6, 0x00);    // LED GREEN off
#endif
#if BOARD_MSC == 3
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0x00);    // PF3 LED GREEN off
#endif
#if BOARD_TESTFIT >= 1
    ROM_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
    ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0xFF);    // LED on
#endif
#if BOARD_NANOFET >= 1
    ROM_GPIOPinWrite(GPIO_PORTC_BASE, GPIO_PIN_6, 0xFF);    // LED GREEN on
#endif
#if BOARD_MSC == 3
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_3, 0xFF);    // PF3 LED GREEN on
#endif
#if BOARD_TESTFIT >= 1
    ROM_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();
    }
}

