/*
 * @file io_brd.c
 */

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

extern s_mcp23008 hgpio_exp_nic_u2;
extern s_mcp23008 hgpio_exp_nic_u4;
extern s_mcp23008 hgpio_exp_nic_u5;

extern mcp9843_dev_t io_temp_hdl;

/*
 *  IO_BOARD section -A enable function
 */
uint8_t io_board_test_sec_a()
{
    uint8_t sts = MCP23008_SCS ;
    nic_1_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U4);
    nic_1_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U5);

    /*
     * GEN-2 IO_BOARD GPIO_DIRECTION_CONFIGURATION
     */
    sts |= mcp23008_set_direction(&hgpio_exp_nic_u4,IO_A_ETH_DCDC1_ON|IO_A_ETH_DCDC2_ON|                 /*!< U4 => GP0,1,4,5,6,7 */
            PORT10_USB_SEL|IO_A2_ETH_RESET|IO_BOARD_ON|IO_FDTI_RESETN,MCP23008_GPIO_OUTPUT);
    sts |= mcp23008_set_direction(&hgpio_exp_nic_u4,IO_A_DVDDL_PG|IO_A_DVDDIO_PG,MCP23008_GPIO_INPUT);   /*!< U4 => GP2,3 */

    if(sts == MCP23008_SCS)
    {
        printf("GPIO_EXPANDER = U4 SUCCESS \n\n\r");
    }
    else
    {
        printf("GPIO_EXPANDER = U4 \033[1;31mFAIL \033[0m \n\n\r");
    }
    sts |= mcp23008_set_direction(&hgpio_exp_nic_u5,IO_B_ETH_DCDC1_ON|IO_B_ETH_DCDC2_ON|
            IO_B2_ETH_RESET|IO_MUX_PD_ENABLE,MCP23008_GPIO_OUTPUT);                                    /*!< U5 => GP0,1,5,6 */
    sts |= mcp23008_set_direction(&hgpio_exp_nic_u5,IO_B_DVDDL_PG|IO_B_DVDDIO_PG|IO_B1_ETH_RESET|
            IO_TEM_SEN_INT,MCP23008_GPIO_INPUT);                                                       /*!< U5 => GP2,3,4,7 */

    if(sts == MCP23008_SCS)
    {
        printf("GPIO_EXPANDER = U5 SUCCESS \n\n\r");
    }
    else
    {
        printf("GPIO_EXPANDER = U5 \033[1;31mFAIL \033[0m \n\n\r");
    }

    sts |= mcp23008_write_pin(&hgpio_exp_nic_u4,IO_BOARD_ON,MCP23008_GPIO_HIGH);           /*!< GP6 */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u5,IO_MUX_PD_ENABLE,MCP23008_GPIO_LOW);       /*!< U5=> GP6 */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u4,IO_FDTI_RESETN,MCP23008_GPIO_HIGH);        /*!< GP7 */

    nic_1_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U2);
    sts |= mcp23008_set_direction(&hgpio_exp_nic_u2,IO_NIC_PORT1_SEL|IO_NIC_PORT2_SEL|
            IO_NIC_PORT3_SEL|IO_NIC_PORT4_SEL|IO_NIC_PORT6_SEL|
            IO_NIC_PORT7_SEL|IO_NIC_PORT8_SEL|IO_NIC_PORT10_SEL,MCP23008_GPIO_OUTPUT);
    if(sts == MCP23008_SCS)
    {
        printf("GPIO_EXPANDER = U2 SUCCESS \n\n\r");
    }
    else
    {
        printf("GPIO_EXPANDER = U2 \033[1;31mFAIL \033[0m \n\n\r");
    }

    /*** Sequence for IO BOARD Section - A ***/
    /*** U2 ***/
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u2,IO_NIC_PORT1_SEL|IO_NIC_PORT2_SEL|
            IO_NIC_PORT3_SEL|IO_NIC_PORT4_SEL|IO_NIC_PORT6_SEL|  \
            IO_NIC_PORT7_SEL|IO_NIC_PORT8_SEL|IO_NIC_PORT10_SEL,MCP23008_GPIO_LOW);       /*!< U2 => GP0 - GP7 */
    /**
     * U5
     * This sequence disable the Section-B
     */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u5,IO_B_ETH_DCDC1_ON,MCP23008_GPIO_LOW);     /*!< U5 => GP0 */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u5,IO_B_ETH_DCDC2_ON,MCP23008_GPIO_LOW);     /*!< U5 => GP1 */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u5,IO_B1_ETH_RESET,MCP23008_GPIO_LOW);       /*!< U5 => GP4 */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u5,IO_B2_ETH_RESET,MCP23008_GPIO_LOW);       /*!< U5 => GP5 */


    /**
     * U4
     * This sequence enable the Section-A
     */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u4,IO_A_ETH_DCDC1_ON,MCP23008_GPIO_HIGH);    /*!< U4 => GP0 */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u4,IO_A_ETH_DCDC2_ON,MCP23008_GPIO_HIGH);    /*!< U4 => GP1 */
    HAL_Delay(10);

    sts |= mcp23008_write_pin(&hgpio_exp_nic_u4,PORT10_USB_SEL,MCP23008_GPIO_LOW);        /*!< U4 => GP4 */
    HAL_Delay(1000);
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u4,IO_A2_ETH_RESET,MCP23008_GPIO_LOW);       /*!< U4 => GP5 */
    HAL_Delay(100);
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u4,IO_A2_ETH_RESET,MCP23008_GPIO_HIGH);      /*!< U4 => GP5 */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u4,IO_BOARD_ON,MCP23008_GPIO_HIGH);          /*!< U4 => GP6 */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u5,IO_MUX_PD_ENABLE,MCP23008_GPIO_HIGH);     /*!< U5 => GP6 */

    return sts;
}

/*
 *  IO_BOARD section -B enable function
 */
uint8_t io_board_test_sec_b()
{
    uint8_t sts = MCP23008_SCS;

    nic_1_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U4);
    nic_1_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U5);

    sts |= mcp23008_set_direction(&hgpio_exp_nic_u4,IO_A_ETH_DCDC1_ON|IO_A_ETH_DCDC2_ON|                 /*!< U4 => 0,1,4,5,6,7 */
            PORT10_USB_SEL|IO_A2_ETH_RESET|IO_BOARD_ON|IO_FDTI_RESETN,MCP23008_GPIO_OUTPUT);
    sts |= mcp23008_set_direction(&hgpio_exp_nic_u4,IO_A_DVDDL_PG|IO_A_DVDDIO_PG,MCP23008_GPIO_INPUT);   /*!< U4 => 2,3 */

    if(sts == MCP23008_SCS)
    {
        printf("GPIO_EXPANDER = U4 SUCCESS \n\n\r");
    }
    else
    {
        printf("GPIO_EXPANDER = U4 \033[1;31mFAIL \033[0m \n\n\r");
    }
    sts |= mcp23008_set_direction(&hgpio_exp_nic_u5,IO_B_ETH_DCDC1_ON|IO_B_ETH_DCDC2_ON|
            IO_B2_ETH_RESET|IO_MUX_PD_ENABLE,MCP23008_GPIO_OUTPUT);                                   /*!< U5 => 0,1,5,6 */
    sts |= mcp23008_set_direction(&hgpio_exp_nic_u5,IO_B_DVDDL_PG|IO_B_DVDDIO_PG|IO_B1_ETH_RESET|
            IO_TEM_SEN_INT,MCP23008_GPIO_INPUT);                                                      /*!< U5 => 2,3,4,7 */

    if(sts == MCP23008_SCS)
    {
        printf("GPIO_EXPANDER = U5 SUCCESS \n\n\r");
    }

    else
    {
        printf("GPIO_EXPANDER = U5 \033[1;31mFAIL \033[0m \n\n\r");
    }
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u4,IO_BOARD_ON,MCP23008_GPIO_HIGH);           /*!< U4 => GP6 */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u5,IO_MUX_PD_ENABLE,MCP23008_GPIO_LOW);       /*!< U5 => GP6 */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u4,IO_FDTI_RESETN,MCP23008_GPIO_HIGH);        /*!< U4 => GP7 */

    nic_1_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U2);
    sts |= mcp23008_set_direction(&hgpio_exp_nic_u2,IO_NIC_PORT1_SEL|IO_NIC_PORT2_SEL|
            IO_NIC_PORT3_SEL|IO_NIC_PORT4_SEL|IO_NIC_PORT6_SEL|  \
            IO_NIC_PORT7_SEL|IO_NIC_PORT8_SEL|IO_NIC_PORT10_SEL,MCP23008_GPIO_OUTPUT);
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u2,IO_NIC_PORT1_SEL|IO_NIC_PORT2_SEL|IO_NIC_PORT3_SEL
            |IO_NIC_PORT4_SEL|IO_NIC_PORT6_SEL|  \
            IO_NIC_PORT7_SEL|IO_NIC_PORT8_SEL|IO_NIC_PORT10_SEL,MCP23008_GPIO_LOW);        /*!< U2 => GP0 - GP7 */

    if(sts == MCP23008_SCS)
    {
        printf("GPIO_EXPANDER = U2 SUCCESS \n\n\r");
    }
    else
    {
        printf("GPIO_EXPANDER = U2 \033[1;31mFAIL \033[0m \n\n\r");
    }

    /**
     * U4
     * This sequence disable the Section-A
     */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u4,IO_A_ETH_DCDC1_ON,MCP23008_GPIO_LOW);       /*!< GP0 */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u4,IO_A_ETH_DCDC2_ON,MCP23008_GPIO_LOW);       /*!< GP1 */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u4,PORT10_USB_SEL,MCP23008_GPIO_LOW);          /*!< GP4 */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u4,IO_A2_ETH_RESET,MCP23008_GPIO_LOW);         /*!< GP5 */

    /**
     * U5
     * This sequence enable the Section-B
     */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u5,IO_B_ETH_DCDC1_ON,MCP23008_GPIO_HIGH);      /*!< GP0 */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u5,IO_B_ETH_DCDC2_ON,MCP23008_GPIO_HIGH);      /*!< GP1 */
    HAL_Delay(10);

    sts |= mcp23008_write_pin(&hgpio_exp_nic_u5,IO_B1_ETH_RESET,MCP23008_GPIO_LOW);         /*!< GP4 */
    HAL_Delay(100);

    sts |= mcp23008_write_pin(&hgpio_exp_nic_u5,IO_B1_ETH_RESET,MCP23008_GPIO_HIGH);        /*!< GP4 */
    HAL_Delay(1000);

    sts |= mcp23008_write_pin(&hgpio_exp_nic_u5,IO_B2_ETH_RESET,MCP23008_GPIO_LOW);         /*!< GP5 */
    HAL_Delay(100);

    sts |= mcp23008_write_pin(&hgpio_exp_nic_u5,IO_B2_ETH_RESET,MCP23008_GPIO_HIGH);        /*!< GP5 */
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u5,IO_MUX_PD_ENABLE,MCP23008_GPIO_HIGH);       /*!< GP6 */
    /*** U2 ***/
    sts |= mcp23008_write_pin(&hgpio_exp_nic_u2,IO_NIC_PORT1_SEL|IO_NIC_PORT2_SEL|
            IO_NIC_PORT3_SEL|IO_NIC_PORT4_SEL|IO_NIC_PORT6_SEL|  \
            IO_NIC_PORT7_SEL|IO_NIC_PORT8_SEL|IO_NIC_PORT10_SEL,MCP23008_GPIO_HIGH);        /*!< GP0 - GP7 */

    return sts;
}

/*
 * IO-BOARD get temperature function.
 */
uint8_t io_brd_get_tempreature(float *temperature)
{
    uint8_t sts = MCP9843_ERR;
    if(temperature != NULL)
    {
        temp_sensor_init(TEMP_SENSOR_MCP9843_IO_U26);
        sts = mcp9843_gettemperature(&io_temp_hdl,temperature);
    }
    else
    {
        sts = MCP9843_E_NULL_PTR;
    }
    return sts;
}

/*
 * USB port deduct function
 */
uint8_t io_brd_usb_deduct_func(uint8_t usb_value)
{
    uint8_t sts = MCP23008_ERR;
    nic_1_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_U4);
    sts = mcp23008_set_direction(&hgpio_exp_nic_u4,PORT10_USB_SEL,MCP23008_GPIO_OUTPUT);
    if(usb_value == ENABLE)
    {
        /*
         * This pin enable the usb port
         */
        sts |= mcp23008_write_pin(&hgpio_exp_nic_u4,PORT10_USB_SEL,MCP23008_GPIO_HIGH);          /*!< GP4 */
        if(sts == MCP23008_SCS)
        {
            printf("IO_BRD USB ENABLE SUCCESS \n\n\r");
        }
        else
        {
            printf("IO_BRD USB ENABLE \033[1;31mFAIL \033[0m \n\n\r");
        }
    }
    else
    {
        sts |= mcp23008_write_pin(&hgpio_exp_nic_u4,PORT10_USB_SEL,MCP23008_GPIO_LOW);           /*!< GP4 */
        if(sts == MCP23008_SCS)
        {
            printf("IO_BRD USB DISABLE SUCCESS \n\n\r");
        }
        else
        {
            printf("IO_BRD USB DISABLE \033[1;31mFAIL \033[0m \n\n\r");
        }
    }
    return sts;
}
