/*
 * @file es_brd.c
 */

#include "main.h"
#include "mcp23008_gpio_expander.h"
#include "mcp9843.h"
#include "es_brd.h"

extern I2C_HandleTypeDef hi2c1;
extern s_mcp23008 hgpio_exp_es_16;
extern s_mcp23008 hgpio_exp_es_37;
extern s_mcp23008 hgpio_exp_es_38;
extern s_mcp23008 hgpio_exp_es_40;
extern mcp9843_dev_t es_temp1_hdl;
extern mcp9843_dev_t es_temp2_hdl;

temp_struct temp_sts;

/*
 *  ES_BOARD_1 enable function
 */
uint8_t es_brd_test1()
{
    uint8_t sts = MCP23008_SCS;
    es_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U16);
    es_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U37);
    es_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U38);
    es_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U40);
    sts |= mcp23008_set_direction(&hgpio_exp_es_16,E1_EDGE_TEM_SEN_INT|E1_PGOOD_1P8V_EDGE|
            E1_PGOOD_3P3V_EDGE,MCP23008_GPIO_INPUT);                                                    /*!< U16 => 1,4,5 */
    sts |= mcp23008_set_direction(&hgpio_exp_es_16,E1_EN_5V_EDGE|E1_EN_1P8V_EDGE|E1_EN_3P3V_EDGE|
            MUX_SSD_EN1|MUX_SSD_EN2,MCP23008_GPIO_OUTPUT);                                              /*!< U16 => 0,2,3,6,7 */
    if(sts == MCP23008_SCS)
    {
        printf("GPIO_EXPANDER = U16 SUCCESS \n\n\r");
    }
    else
    {
        printf("GPIO_EXPANDER = U16 \033[1;31mFAIL \033[0m \n\n\r");
    }
    sts |= mcp23008_set_direction(&hgpio_exp_es_37, CLK_EN_A_B, MCP23008_GPIO_OUTPUT);  /*!< U37 => 0 */
    sts |= mcp23008_write_pin(&hgpio_exp_es_16, E1_EN_5V_EDGE, MCP23008_GPIO_HIGH);     /*!< U16 => 0 */
    sts |= mcp23008_write_pin(&hgpio_exp_es_16, E1_EN_1P8V_EDGE |
            E1_EN_3P3V_EDGE, MCP23008_GPIO_HIGH);                                       /*!< U16 => 2,3 */
    sts |= mcp23008_write_pin(&hgpio_exp_es_16, MUX_SSD_EN1, MCP23008_GPIO_HIGH);       /*!< U16 => 6 */
    sts |= mcp23008_write_pin(&hgpio_exp_es_37, CLK_EN_A_B, MCP23008_GPIO_HIGH);        /*!< U37 => 0 */
    HAL_Delay(1000);
    return sts;
}

/*
 *  ES_BOARD_2 enable function
 */

uint8_t es_brd_test2 ()
{
    uint8_t sts = MCP23008_SCS;
    es_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U16);
    es_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U38);
    es_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U37);
    es_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U40);
    sts |= mcp23008_set_direction(&hgpio_exp_es_16,E1_EN_5V_EDGE|E1_EN_1P8V_EDGE|
            MUX_SSD_EN1|MUX_SSD_EN2|E1_EN_3P3V_EDGE,MCP23008_GPIO_OUTPUT);              /*!< U16 => 0,2,6,7,3 */
    sts |= mcp23008_set_direction(&hgpio_exp_es_16,E1_EDGE_TEM_SEN_INT|
            E1_PGOOD_1P8V_EDGE|E1_PGOOD_3P3V_EDGE,MCP23008_GPIO_INPUT);                 /*!< U16 => 1,4,5 */
    if(sts == MCP23008_SCS)
    {
        printf("GPIO_EXPANDER = U16 SUCCESS \n\n\r");
    }
    else
    {
        printf("GPIO_EXPANDER = U16 \033[1;31mFAIL \033[0m \n\n\r");
    }
    sts |= mcp23008_set_direction(&hgpio_exp_es_38,E2_EN_5V_EDGE|E2_EN_1P8V_EDGE|
            E2_EN_3P3V_EDGE_EN|USB_PD1_PD2|USB_SEL1_SEL2,MCP23008_GPIO_OUTPUT);         /*!< U38 => 0,2,3,6,7 */
    sts |= mcp23008_set_direction(&hgpio_exp_es_38,E2_PGOOD_1P8V_EDGE|
            E2_PGOOD_3P3V_EDGE,MCP23008_GPIO_INPUT);                                    /*!< U38 => 4,5 */
    if(sts == MCP23008_SCS)
    {
        printf("GPIO_EXPANDER = 38 SUCCESS \n\n\r");
    }
    else
    {
        printf("GPIO_EXPANDER = 38 \033[1;31mFAIL \033[0m \n\n\r");
    }
    /*
     * Sequence for Boot Edge -2
     * GPIO_EXPANDER_U16
     */
    sts |= mcp23008_write_pin(&hgpio_exp_es_16,E1_EN_5V_EDGE|E1_EN_1P8V_EDGE|
            E1_EN_3P3V_EDGE,MCP23008_GPIO_LOW);                                          /*!< GP0,GP2,GP3 */
    /*
     * GPIO_EXPANDER_U38
     */
    HAL_Delay(100);
    sts |= mcp23008_write_pin(&hgpio_exp_es_38,E2_EN_5V_EDGE|E2_EN_1P8V_EDGE|
            E2_EN_3P3V_EDGE_EN|USB_SEL1_SEL2,MCP23008_GPIO_HIGH);
    sts |= mcp23008_write_pin(&hgpio_exp_es_16,MUX_SSD_EN1|MUX_SSD_EN2,MCP23008_GPIO_HIGH);
    return sts;
}

/*
 *  Edge -1 ssd test function
 */
uint8_t edge1_ssd_test (uint8_t edge_brd_ssd_id)
{
    uint8_t sts = MCP23008_SCS;
    if(EDGE_1_SSD_1 == edge_brd_ssd_id)
    {
        /*
         * SSD BOOT
         */
        sts |= mcp23008_set_direction(&hgpio_exp_es_37,CLK_EN_A_B|SSD_RST_CON_ENB|SSD_WAKE_CON_ENB|
                CONN2_PCIE0_CLKREQ|CONN1_PCIE0_CLKREQ,MCP23008_GPIO_OUTPUT);
        if(sts == MCP23008_SCS)
        {
            printf("GPIO_EXPANDER = U37 SUCCESS \n\n\r");
        }
        else
        {
            printf("GPIO_EXPANDER = U37 \033[1;31mFAIL \033[0m \n\n\r");
        }
        // U40
        sts |= mcp23008_set_direction(&hgpio_exp_es_40,SSD_RST_ENB|SSD_WAKE_ENB|EI_SEL1_SEL2|
                E2_SEL1_SEL2|SSD0_SEL1_SEL2|SSD1_SEL1_SEL2|E1_E2_PD|SSD0_SSD1_PD,MCP23008_GPIO_OUTPUT);
        if(sts == MCP23008_SCS)
        {
            printf("GPIO_EXPANDER = U40 SUCCESS \n\n\r");
        }
        else
        {
            printf("GPIO_EXPANDER = U40 \033[1;31mFAIL \033[0m \n\n\r");
        }
        sts |= mcp23008_write_pin(&hgpio_exp_es_40,EI_SEL1_SEL2|SSD1_SEL1_SEL2,MCP23008_GPIO_LOW);
        sts |= mcp23008_write_pin(&hgpio_exp_es_40,SSD_RST_ENB|SSD_WAKE_ENB,MCP23008_GPIO_HIGH);
        sts |= mcp23008_write_pin(&hgpio_exp_es_37,SSD_RST_CON_ENB|SSD_WAKE_CON_ENB,MCP23008_GPIO_HIGH);
    }
    else if(EDGE_1_SSD_2 == edge_brd_ssd_id)
    {
        sts |= mcp23008_write_pin(&hgpio_exp_es_16,MUX_SSD_EN2,MCP23008_GPIO_HIGH);
        sts |= mcp23008_set_direction(&hgpio_exp_es_37,CLK_EN_A_B|SSD_RST_CON_ENB|SSD_WAKE_CON_ENB|
                CONN2_PCIE0_CLKREQ|CONN1_PCIE0_CLKREQ,MCP23008_GPIO_OUTPUT);
        sts |= mcp23008_write_pin(&hgpio_exp_es_37,CLK_EN_A_B,MCP23008_GPIO_HIGH);            /*!< GP0  */
        HAL_Delay(1000);
        if(sts == MCP23008_SCS)
        {
            printf("GPIO_EXPANDER = U37 SUCCESS \n\n\r");
        }
        else
        {
            printf("GPIO_EXPANDER = U37 \033[1;31mFAIL \033[0m \n\n\r");
        }
        // U40
        sts |= mcp23008_set_direction(&hgpio_exp_es_40,SSD_RST_ENB|SSD_WAKE_ENB|EI_SEL1_SEL2|
                E2_SEL1_SEL2|SSD0_SEL1_SEL2|SSD1_SEL1_SEL2|E1_E2_PD|SSD0_SSD1_PD,MCP23008_GPIO_OUTPUT);
        if(sts == MCP23008_SCS)
        {
            printf("GPIO_EXPANDER = U40 SUCCESS \n\n\r");
        }
        else
        {
            printf("GPIO_EXPANDER = U40 \033[1;31mFAIL \033[0m \n\n\r");
        }
        /*** U40 ***/
        sts |= mcp23008_write_pin(&hgpio_exp_es_40,SSD_RST_ENB|SSD_WAKE_ENB|
                EI_SEL1_SEL2,MCP23008_GPIO_HIGH);                                          /*!< GP0 */
        sts |= mcp23008_write_pin(&hgpio_exp_es_40,SSD1_SEL1_SEL2,MCP23008_GPIO_LOW);       /*!< GP5 */
        /*** u37 ***/
        sts |= mcp23008_write_pin(&hgpio_exp_es_37,SSD_RST_CON_ENB|
                SSD_WAKE_CON_ENB,MCP23008_GPIO_HIGH);                                      /*!< GP1,GP2 */
    }
    return sts;
}

/*
 *  Edge -2 ssd test function
 */
uint8_t edge2_ssd_test (uint8_t edge_brd_ssd_id)
{
    uint8_t sts = MCP23008_SCS;
    es_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U37);
    HAL_Delay(100);
    es_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U40);
    sts |= mcp23008_set_direction(&hgpio_exp_es_37,CLK_EN_A_B|SSD_RST_CON_ENB|SSD_WAKE_CON_ENB|E1_FORCE_RECOVERY_LT|
            E2_FORCE_RECOVERY_LT|CONN2_PCIE0_CLKREQ|CONN1_PCIE0_CLKREQ,MCP23008_GPIO_OUTPUT);
    sts |= mcp23008_set_direction(&hgpio_exp_es_37,E2_EDGE_TEM_SEN_INT,MCP23008_GPIO_INPUT);
    if(sts == MCP23008_SCS)
    {
        printf("GPIO_EXPANDER = U37 SUCCESS \n\n\r");
    }
    else
    {
        printf("GPIO_EXPANDER = U37 \033[1;31mFAIL \033[0m \n\n\r");
    }
    sts |= mcp23008_set_direction(&hgpio_exp_es_40,SSD_RST_ENB|SSD_WAKE_ENB|EI_SEL1_SEL2|
            E2_SEL1_SEL2|SSD0_SEL1_SEL2|SSD1_SEL1_SEL2|E1_E2_PD|SSD0_SSD1_PD,MCP23008_GPIO_OUTPUT);
    if(sts == MCP23008_SCS)
    {
        printf("GPIO_EXPANDER = U40 SUCCESS \n\n\r");
    }
    else
    {
        printf("GPIO_EXPANDER = U40 \033[1;31mFAIL \033[0m \n\n\r");
    }
    if(EDGE_2_SSD_1 == edge_brd_ssd_id)
    {
        /*
         * GPIO_EXPANDER_U37
         */
        HAL_Delay(100);
        sts |= mcp23008_write_pin(&hgpio_exp_es_37,CLK_EN_A_B,MCP23008_GPIO_LOW);                  /*!< GP0 */
        /*
         * GPIO_EXPANDER_U40
         */
        sts |= mcp23008_write_pin(&hgpio_exp_es_40,E2_SEL1_SEL2,MCP23008_GPIO_LOW);                /*!< GP3 */
        sts |= mcp23008_write_pin(&hgpio_exp_es_40,SSD0_SEL1_SEL2,MCP23008_GPIO_HIGH);             /*!< GP4 */
        sts |= mcp23008_write_pin(&hgpio_exp_es_40,SSD1_SEL1_SEL2|SSD_RST_ENB|
                SSD_WAKE_ENB,MCP23008_GPIO_LOW);                                                  /*!< GP5,GP0,GP1 */
        /*
         * GPIO_EXPANDER_U37
         */
        sts |= mcp23008_write_pin(&hgpio_exp_es_37,SSD_RST_CON_ENB|SSD_WAKE_CON_ENB,
                MCP23008_GPIO_LOW);                                                               /*!< GP1,GP2 */
    }
    else if(EDGE_2_SSD_2 == edge_brd_ssd_id)
    {
        /*
         * GPIO_EXPANDER_U40
         */
        HAL_Delay(500);
        sts |= mcp23008_write_pin(&hgpio_exp_es_37,CLK_EN_A_B,MCP23008_GPIO_LOW);                  /*!< GP0 */
        sts |= mcp23008_write_pin(&hgpio_exp_es_40,E2_SEL1_SEL2,MCP23008_GPIO_HIGH);               /*!< GP3 */
        sts |= mcp23008_write_pin(&hgpio_exp_es_40,SSD1_SEL1_SEL2,MCP23008_GPIO_HIGH);             /*!< GP5 */
        sts |= mcp23008_write_pin(&hgpio_exp_es_40,SSD_RST_ENB|SSD_WAKE_ENB,MCP23008_GPIO_LOW);    /*!< GP0,GP1 */
        sts |= mcp23008_write_pin(&hgpio_exp_es_37,SSD_RST_CON_ENB|SSD_WAKE_CON_ENB,
                MCP23008_GPIO_LOW);                                                               /*!< GP1,GP2 */
    }
    return sts;
}

/*
 * This get the temperature sensor value.
 */
uint8_t edge_brd_get_tempreature(temp_struct *temperature)
{
    uint8_t sts = MCP23008_SCS;
    if(temperature != NULL)
    {
        temp_sts.temp_1 = 0;
        temp_sensor_init(TEMP_SENSOR_MCP9843_EDGE_U13);
        sts |= mcp9843_gettemperature(&es_temp1_hdl, &temperature->temp_1);

        temp_sts.temp_2 = 0;
        temp_sensor_init(TEMP_SENSOR_MCP9843_EDGE_U34);
        sts |= mcp9843_gettemperature(&es_temp2_hdl, &temperature->temp_2);
    }
    else
    {
        sts = MCP9843_E_NULL_PTR;
    }
    return sts;
}

/*
 * This edge board power off function.
 */
uint8_t edge_brd_pwr_off(uint8_t* ahw_id)
{
    uint8_t sts = MCP23008_SCS;
    if(ahw_id != NULL)
    {
        switch(*ahw_id)
        {
        case ES_BRD_1_OFF:
        {
            es_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U16);
            sts |= mcp23008_set_direction(&hgpio_exp_es_16,E1_EDGE_TEM_SEN_INT|E1_PGOOD_1P8V_EDGE|
                    E1_PGOOD_3P3V_EDGE,MCP23008_GPIO_INPUT);                                                           /*!< 1,4,5,6,7 */
            sts |= mcp23008_set_direction(&hgpio_exp_es_16,E1_EN_5V_EDGE|E1_EN_1P8V_EDGE|
                    MUX_SSD_EN1|MUX_SSD_EN2,MCP23008_GPIO_OUTPUT);                                                     /*!< 0,2,3 */
            if(sts == MCP23008_SCS)
            {
                printf("GPIO_EXPANDER = U16 SUCCESS \n\n\r");
            }
            else
            {
                printf("GPIO_EXPANDER = U16 \033[1;31mFAIL \033[0m \n\n\r");
            }
            sts |= mcp23008_write_pin(&hgpio_exp_es_16,E1_EN_5V_EDGE|E1_EN_1P8V_EDGE|
                    E1_EN_3P3V_EDGE|MUX_SSD_EN1,MCP23008_GPIO_LOW);

            if(sts == MCP23008_SCS)
            {
                printf("EDGE 1 POWER OFF SUCCESS \n\n\r");
            }
            else
            {
                printf("EDGE 1 POWER OFF \033[1;31mFAIL \033[0m \n\n\r");
            }
        }
        break;

        case ES_BRD_2_OFF:
        {
            es_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U38);
            sts |= mcp23008_set_direction(&hgpio_exp_es_38,E2_EN_5V_EDGE|E2_EN_1P8V_EDGE|
                    E2_EN_3P3V_EDGE_EN|USB_PD1_PD2|USB_SEL1_SEL2,MCP23008_GPIO_OUTPUT);
            sts |= mcp23008_set_direction(&hgpio_exp_es_38,E2_PGOOD_1P8V_EDGE|
                    E2_PGOOD_3P3V_EDGE,MCP23008_GPIO_INPUT);
            if(sts == MCP23008_SCS)
            {
                printf("GPIO_EXPANDER = 38 SUCCESS \n\n\r");
            }
            else
            {
                printf("GPIO_EXPANDER = 38 \033[1;31mFAIL \033[0m \n\n\r");
            }
            sts |= mcp23008_write_pin(&hgpio_exp_es_38,E2_EN_5V_EDGE|E2_EN_1P8V_EDGE|
                    E2_EN_3P3V_EDGE_EN|USB_SEL1_SEL2,MCP23008_GPIO_LOW);
            if(sts == MCP23008_SCS)
            {
                printf("EDGE 2 POWER OFF SUCCESS \n\n\r");
            }
            else
            {
                printf("EDGE 2 POWER OFF \033[1;31mFAIL \033[0m \n\n\r");
            }
        }
        break;
        }
    }
    else
    {
        sts = MCP9843_E_NULL_PTR;
    }
    return sts;
}
