/*
 * @file ps_brd.c
 */

#include "ps_brd.h"
#include "main.h"
#include "mcp9843.h"
#include "psm_ina230.h"
#include "adm1176.h"
#include "mcp23008_gpio_expander.h"

extern pcal6408a_dev hgpio_exp_ps_u13;
extern psm_ina230_dev_t ps_psm_hdl;
extern mcp9843_dev_t ps_temp_hdl;
extern s_adm1176_drv_cb hsc_info;
extern I2C_HandleTypeDef hi2c1;
power_info_s power_s = {0};

extern uint8_t reg_data[2];
extern uint8_t addr;
extern uint8_t sts;

/*
 *  PS_BOARD_1 enable function
 */
uint8_t ps_brd_test1 ()
{
    uint8_t sts = PCAL6408A_SCS;
    ps_brd_gpio_exp_test_init(GPIO_EXPANDER_PCAL6408A_PS);
    sts |= pcal6408a_set_dir(&hgpio_exp_ps_u13,ETH_VDD3V3_EN|PAYLOAD_VCC_ENABLE|
            PAYLOAD_SB_ENABLE|PAYLOAD_SSD_CORE_ENABLE|ETH_RESET,OUTPUT);
    sts |= pcal6408a_set_dir(&hgpio_exp_ps_u13,ETH_VDD3V3_PG,INPUT);
    if(sts == PCAL6408A_SCS)
    {
        printf("GPIO_EXPANDER = U13 SUCCESS \n\n\r");
    }
    else
    {
        printf("GPIO_EXPANDER = U13 \033[1;31mFAIL \033[0m \n\n\r");
    }

    sts |= pcal6408a_writepin(&hgpio_exp_ps_u13,ETH_VDD3V3_EN|ETH_VDD3V3_PG|PAYLOAD_VCC_ENABLE|
            PAYLOAD_SB_ENABLE|PAYLOAD_SSD_CORE_ENABLE|ETH_RESET,PIN_RESET);

    /*
     * Sequence for Boot PS -1
     */
    pcal6408a_writepin(&hgpio_exp_ps_u13,PAYLOAD_SSD_CORE_ENABLE,PIN_SET);     /*!< GP4 => 3.3v payload power supply */
    HAL_Delay(1);
    pcal6408a_writepin(&hgpio_exp_ps_u13,PAYLOAD_SB_ENABLE,PIN_SET);           /*!< GP3 => 5v pin enable after Q7 power supply go */
    HAL_Delay(1);
    pcal6408a_writepin(&hgpio_exp_ps_u13,PAYLOAD_VCC_ENABLE,PIN_SET);          /*!< GP2 */
    pcal6408a_writepin(&hgpio_exp_ps_u13,ETH_VDD3V3_EN,PIN_SET);               /*!< GP0 */
    HAL_Delay(10);
    pcal6408a_writepin(&hgpio_exp_ps_u13,ETH_RESET,PIN_SET);                   /*!< GP6 */
    return sts;
}

/*
 *  PS_BOARD get temperature function
 */
uint8_t ps_brd_get_tempreature()
{
    uint8_t sts = MCP9843_SCS;
    float temperature = 0;
    temp_sensor_init(TEMP_SENSOR_MCP9843_PS);
    HAL_Delay(1);
    sts |= mcp9843_gettemperature(&ps_temp_hdl, &temperature);
    power_s.pwr_info.temp = temperature;
    printf("PS_BRD Temperature  : %d\n\n\r",(int)power_s.pwr_info.temp);

    /*
     * GPIO EXPANDER U6
     */
    ps_temp_sensor_init(PSM_INA230_PS);
    HAL_Delay(1);
    power_s.pwr_info.vbus_vlg           = ina230_get_vbus(&ps_psm_hdl);
    power_s.pwr_info.vshunt_voltage     = ina230_get_vshunt(&ps_psm_hdl);
    power_s.power                       = ina230_get_power(&ps_psm_hdl);
    power_s.pwr_info.psm_current        = ina230_get_current(&ps_psm_hdl);
    printf("PS_BRD psm voltage  : %f\n\r",power_s.pwr_info.vbus_vlg);
    printf("PS_BRD psm vshunt   : %f\n\r",power_s.pwr_info.vshunt_voltage);
    printf("PS_BRD psm power    : %d\n\r",(int)power_s.power);
    printf("PS_BRD psm current  : %f\n\n\r",power_s.pwr_info.psm_current);

    /*
     * GPIO EXPANDER U4
     */
    ps_hsc_init(HSC_ADM1176_PS_1);
    HAL_Delay(1);
    power_s.pwr_info.hsc_crnt           = adm1176_read_current(&hsc_info);
    power_s.pwr_info.hsc_vlt            = adm1176_read_voltage(&hsc_info);
    printf("PS_BRD hsc voltage  : %f\n\r",power_s.pwr_info.hsc_vlt);
    printf("PS_BRD hsc current  : %f\n\n\r",power_s.pwr_info.hsc_crnt);

    return sts;
}

/*
 *  PS_BOARD power off function
 */
uint8_t ps_brd_power_off()
{
    uint8_t sts = PCAL6408A_SCS;
    ps_brd_gpio_exp_test_init(GPIO_EXPANDER_PCAL6408A_PS);
    HAL_Delay(1);
    sts |= pcal6408a_set_dir(&hgpio_exp_ps_u13,ETH_VDD3V3_EN|PAYLOAD_VCC_ENABLE|
            PAYLOAD_SB_ENABLE|PAYLOAD_SSD_CORE_ENABLE|ETH_RESET,OUTPUT);
    sts |= pcal6408a_set_dir(&hgpio_exp_ps_u13,ETH_VDD3V3_PG,INPUT);
    if(sts == PCAL6408A_SCS)
    {
        printf("GPIO_EXPANDER = U13 SUCCESS \n\n\r");
    }
    else
    {
        printf("GPIO_EXPANDER = U13 \033[1;31mFAIL \033[0m \n\n\r");
    }

    sts |= pcal6408a_writepin(&hgpio_exp_ps_u13,ETH_VDD3V3_EN|ETH_VDD3V3_PG|PAYLOAD_VCC_ENABLE|
            PAYLOAD_SB_ENABLE|PAYLOAD_SSD_CORE_ENABLE|ETH_RESET,PIN_RESET);
    pcal6408a_writepin(&hgpio_exp_ps_u13,PAYLOAD_SSD_CORE_ENABLE,PIN_RESET);     /*!< GP4 => 3.3v payload power supply */
    pcal6408a_writepin(&hgpio_exp_ps_u13,PAYLOAD_SB_ENABLE,PIN_RESET);           /*!< GP3 => 5v pin enable after Q7 power supply go */
    if(sts == PCAL6408A_SCS)
    {
        printf("PAYLOAD BOARD POWER SUPPLY OFF SUCCESS \n\n\r");
    }
    else
    {
        printf("PAYLOAD BOARD POWER SUPPLY OFF \033[1;31mFAIL \033[0m \n\n\r");
    }
    return sts;
}


/*
 *  PS_BOARD gate test function
 */
uint8_t ps_brd_gate_test(uint8_t* case_id, uint8_t on_off)
{
    if(case_id != NULL)
    {
        switch(*case_id)
        {
        case PS1_ON:
        {
            addr = ADM1176_PS_U4;
            reg_data[0] = CONTROL;
            reg_data[1] = on_off;
        }
        break;

        case PS1_OFF:
        {
            addr = ADM1176_PS_U4;
            reg_data[0] = CONTROL;
            reg_data[1] = on_off;
        }
        break;
        }
    }
    else
    {
        sts = MCP23008_NULL_PTR;
    }
    sts= HAL_I2C_Master_Transmit(&hi2c1,addr,reg_data,2,I2C_TEST_TIMEOUT);
    if(sts!=HAL_OK)
    {
        printf("\033[1;31mI2C COMMUNICATION FAIL\033[0m \n\n\r");
        printf("WRITE GATE SEQUENCE \033[1;31mFAIL \033[0m\n\n\r");
    }
    else
    {
        printf("WRITE GATE SEQUENCE SUCCESS \n\n\r");
    }
    return sts;
}
