/*
 * bp_brd.c
 */

#include <string.h>
#include "main.h"
#include "bp_brd.h"
#include "mcp23008_gpio_expander.h"
#include "eeprom_m24.h"
#include "max14830.h"

extern s_mcp23008 hgpio_exp_bp_28;
ahw_inst_id_t ahw_inst_id;
eeprom_id_info_s eprom_id;
extern m24c16_t hext_mem_eeprom_1;
extern m24c16_t hext_mem_eeprom_2;
extern s_mcp23008 hgpio_exp_bp_28;
//extern s_max14830_hdle spi_uart_max1430;

uint8_t* write_buff ;
uint8_t* read_buff;
int while_x = 0;                     /*!< If eeprom gets fail hardcore this
                                      value has 1 and check the read_buff and write_buff */
int eeprom_index;


/**
 * @eprom id and value assign function.
 */
uint8_t eeprom_value_assgn(eeprom_id_info_s *eprm_val,uint8_t *eprm_data)
{
    switch (eprm_val->eeprom_id)
    {
    case 1:
        eprm_val->epprom_hdle = hext_mem_eeprom_1;
        break;
    case 2:
        eprm_val->epprom_hdle = hext_mem_eeprom_2;
        break;
    }

    switch (eprm_val->eeprom_val)
    {
    case 1:
        *eprm_data = write_val_AA;
        break;
    case 2:
        *eprm_data = write_val_00;
        break;
    case 3:
        *eprm_data = write_val_55;
        break;
    case 4:
        *eprm_data = write_val_FF;
        break;
    default:
        eprm_data = 0;
        break;
    }

    return *eprm_data;
}

/*
 *  Backplane_board eeprom test function
 */
uint8_t bp_eeprom_test(eeprom_id_info_s *eprom_id)
{
    uint8_t sts = 1;
    uint8_t eprm_data = 0;
    uint16_t length_eeprom = 2048;
    write_buff = malloc(length_eeprom);
    read_buff = malloc(length_eeprom);
    eeprom_value_assgn(eprom_id, &eprm_data);
    memset(write_buff,eprm_data,length_eeprom);
    memset(read_buff,0x0,length_eeprom);
    sts = m24c16_i2c_read(&eprom_id->epprom_hdle,0,read_buff,length_eeprom);
    for(eeprom_index = 0; eeprom_index < length_eeprom; eeprom_index++)
    {
        if(eprm_data!=read_buff[eeprom_index])
        {
            sts++;
            break;
        }
    }
    while(while_x)
    {
        HAL_Delay(1000);
    }
    sts = 0;
    sts |= m24c16_i2c_write(&eprom_id->epprom_hdle,0,write_buff,length_eeprom);
    HAL_Delay(1000);
    sts |= m24c16_i2c_read(&eprom_id->epprom_hdle,0,read_buff,length_eeprom);

    for(eeprom_index = 0; eeprom_index<length_eeprom; eeprom_index++)
    {
        if(write_buff[eeprom_index]!=read_buff[eeprom_index])
        {
            sts++;
            break;
        }   }
    length_eeprom++;
    return sts;
}

/*
 * Backplane board gpio expander self test function
 */
uint8_t bp_brd_gpio_exp_self_test()
{
    uint8_t sts = 0;
    bp_brd_gpio_exp_test_init(GPIO_EXP_MCP23008_BP_U28);
    sts |= mcp23008_set_direction(&hgpio_exp_bp_28,SPI_RST|SPI_IRQ|PL72A1_RESET|GPS_ON|
            BP_PWER_EN|NRESET_IN,MCP23008_GPIO_OUTPUT);
    sts |= mcp23008_self_test(&hgpio_exp_bp_28);
    if(sts == 0)
    {
        printf("BackPlane GPIO_EXPANDER = U28 SUCCESS \n\n\r");
    }
    else
    {
        printf("\033[1;31mI2C COMMUNICATION FAIL\033[0m \n\n\r");
        printf("BackPlane GPIO_EXPANDER = U28 \033[1;31mFAIL \033[0m \n\n\r");
    }
    return sts;
}

#if 0
/*
 * SPI - UART test function
 */
uint8_t spi_uart_test_func()
{
    uint8_t sts = 0;
    spi_uart_init_func(UART_EXPANDER_MAX14830_OBC);
    sts = max14830_self_test(&spi_uart_max1430);
    if(sts == 0)
    {
        printf("SPI - UART self test SUCCESS \n\n\r");
    }
    else
    {
        printf("SPI - UART self test \033[1;31mFAIL \033[0m \n\n\r");
    }
    return sts;
}
#endif
