/*
 * 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(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;
    }
}

// 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
    // 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
    // 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

}

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)
{
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_7, 0xff);    // Power on
}
void PORTXXX_off(void)
{
    MAP_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_7, 0x00);    // Power off
}



void pwr_env_mon_on(void)
{
    ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_7, 0xFF);    // Turn on Load switch for ENV monitoring
}

void pwr_env_mon_off(void)
{
    // NOTE TODO check why ENV load switch is not turned off
    ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_7, 0x00);    // Turn off Load switch for ENV monitoring
}

void pwr_vbat_mon_on(void)
{

    ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_6, 0xFF);   // Turn on VBat monitoring
}
void pwr_vbat_mon_off(void)
{
    ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_6, 0x00);   // Turn off VBat monitoring
}

void pwr_iso_on(void)
{
    ROM_GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_2, 0xff);  // Iso-power Off
}

void pwr_iso_off(void)
{
    ROM_GPIOPinWrite(GPIO_PORTK_BASE, GPIO_PIN_2, 0x00);        // Iso-power Off
}


void pwr_ctd_on(void)
{
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0xff); // PD0
}

void pwr_ctd_off(void)
{
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0x00); // PD0
}

void pwr_aux_pwr1_on(void)
{
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1, 0xff); // PD1
}

void pwr_aux_pwr1_off(void)
{
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_1, 0x00); // PD1
}

void pwr_aux_pwr2_on(void)
{
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2, 0xff); // PD2
}

void pwr_aux_pwr2_off(void)
{
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2, 0x00); // PD2
}

void pwr_aux_pwr3_on(void)   // shared with uart2 on J14 COMM-PWR port on MPHOX
{
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3, 0xff); // PD3
}

void pwr_aux_pwr3_off(void)
{
    ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_3, 0x00); // PD3
}



void pwr_soln_on(void)   // solenoid, function name matches silk on schematic
{
    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;

}

void solenoid_hit(void)
{
    pwmSoln_stop(1);        //pwr_soln_on();
}

void solenoid_hold(unsigned int pwmVal)
{
    pwmSoln_set(pwmVal);
}

void solenoid_off(void)
{
    pwmSoln_stop(0);        // turn PWM module off and restore GPIO pin function as output lo

    // 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
}

void solenoid_on(void)
{
    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
}

void pwr_soln_off(void)
{
    ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_1, 0x00);
    ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_3, 0x00);
    pwmSoln.gpio = 0;
}

void pwr_pump_on(void)
{
    pump_on();
}

void pwr_pump_off(void)
{
    pump_off();
}

void pump_on(void)
{
#if BOARD_MFET >= 1 || BOARD_MPHOX == 1
    //ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0xff); // this is CTD!!
    pwr_aux_pwr1_on();
    pwmPump.gpio = 1;
#endif
#if BOARD_NANOFET >= 1
    //ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0xff);   // unused pin on nanoFET
#endif

}

void pump_off(void)
{
#if BOARD_MFET >= 1 || BOARD_MPHOX == 1
    //ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0x00);  // this is CTD!!
    pwr_aux_pwr1_off();
    pwmPump.gpio = 0;
#endif
#if BOARD_NANOFET >= 1
    //ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0x00);   // unused pin on nanoFET
#endif
}

void pressure_on(void)
{
#if BOARD_MFET >= 1 || BOARD_MPHOX == 1
    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
    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
    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

}

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
    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
    ledRedState = 0;
}

void led_red_on(void)
{
#if BOARD_MFET >= 1 || BOARD_MPHOX >= 1
    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 off
#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
    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
    ledGreenState = 0;
}

void led_green_on(void)
{
#if BOARD_MFET >= 1 || BOARD_MPHOX >= 1
    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 off
#endif
    ledGreenState = 1;
}

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();
    }
}




#ifdef OLDCODE
// Function Macros


#if BOARD_MFET >= 1 || BOARD_MPHOX == 1
#define pump_on()       ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0xff);
#define pump_off()      ROM_GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0x00);
#endif

#if BOARD_NANOFET >= 1
// There is no pump_on for NANOFET, wiggle unused pin
#define pump_on()       ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0xff);
#define pump_off()      ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0x00);
#endif

#if BOARD_NANOFET >= 1
// There is no pressure on off_on for NANOFET, wiggle unused pin
#define pressureOn()       ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0xff);
#define pressureOff()      ROM_GPIOPinWrite(GPIO_PORTE_BASE, GPIO_PIN_3, 0x00);
#endif


#if BOARD_MFET >= 1 || BOARD_MPHOX == 1
#define pressureOn()    ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_5, 0xff);
#define pressureOff()   ROM_GPIOPinWrite(GPIO_PORTM_BASE, GPIO_PIN_5, 0x00);
#endif
//#define console_on()  ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_0, 0xff);
// Thom new - make console into a routine
//#define console_off() ROM_GPIOPinWrite(GPIO_PORTL_BASE, GPIO_PIN_0, 0x00);
#endif
