/*
 * err_recovery.c
 *
 *  Created on: 12-Sep-2023
 *      Author: Invcuser52
 */


#include "err_recovery.h"
#include "exo_ahw_al_temp_sensor_common.h"
#include "exo_ahw_al_ext_flash_common.h"
#include "sensor_driver_intf.h"
#include "exo_ahw_al_gpio_exp_common.h"
#include "exo_io_al_fmc_nor_flash_common.h"
#include "exo_io_al_qspi_common.h"

#define MEMTEST_BIST_BUFFER_SIZE 12

uint8_t PLACE_IN_EXT_QSPI_FLASH_MEM extmem_qspi_mt25ql512_bist_buffer[MEMTEST_BIST_BUFFER_SIZE];
uint8_t PLACE_IN_EXT_FMC_FLASH_MEM  extmem_fmc_mt28ew_bist_buffer[MEMTEST_BIST_BUFFER_SIZE];




#if 0
uint8_t bist_test_extmem(ahw_inst_id_t inst_id, uint8_t *mem_addr)
{
	uint8_t u8ret;
	uint8_t test_write_buffer[MEMTEST_BIST_BUFFER_SIZE] = {0x00, 0xFF, 0x0F, 0xF0, 0xAA, 0x55, 0x00, 0xFF, 0x0F, 0xF0, 0xAA, 0x55};
	uint8_t test_read_buffer[MEMTEST_BIST_BUFFER_SIZE];

	u8ret = ahw_al_ext_mem_erase(inst_id, (uint32_t)mem_addr, (uint32_t)mem_addr + MEMTEST_BIST_BUFFER_SIZE);
    if(u8ret == HAL_SCS)
    {
    	u8ret = ahw_al_ext_mem_write(inst_id, (uint32_t)mem_addr, test_write_buffer, MEMTEST_BIST_BUFFER_SIZE);
    	ahw_al_ext_mem_read(inst_id, (uint32_t)mem_addr, test_read_buffer, MEMTEST_BIST_BUFFER_SIZE);
    }

    u8ret = memcmp(test_write_buffer, test_read_buffer, MEMTEST_BIST_BUFFER_SIZE);
	return u8ret;
}
#endif

#if 0
uint8_t ahw_rcvry_hdlr_extmem_qspi_m25l512(uint8_t rcvry_mode)
{
	uint8_t u8ret;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

		DEBUG_CPRINT(("mx25l512 recovery invoked\n"));
	}
	else
	{
		DEBUG_CPRINT(("mx25l512 self-test invoked\n"));
	}

  //  u8ret = bist_test_extmem(EXT_MEM_MX25L512_QSPI, extmem_qspi_mx25l512_bist_buffer);
	return u8ret;
}

uint8_t ahw_rcvry_hdlr_extmem_fmc_mt28ew(uint8_t rcvry_mode)
{
	uint8_t u8ret;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{
		DEBUG_CPRINT(("mt28ew recovery invoked\n"));
	}
	else
	{
		DEBUG_CPRINT(("mt28ew self-test invoked\n"));
	}

   // u8ret = bist_test_extmem(EXT_MEM_MT28EW128_FMC, extmem_fmc_mt28ew_bist_buffer);
	return u8ret;
}
#endif

/*
 * 			TEMPREATURE SENSOR RECOVERY FUNCTIONS
 */
uint8_t ahw_rcvry_hdlr_mcp9843_obc(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{
		temp_sns_reconfig(TEMP_SENSOR_MCP9843_OBC);
	}
	else
	{

	}

	u8ret = ahw_al_temp_sensor_self_test(TEMP_SENSOR_MCP9843_OBC);
	return u8ret;
}

uint8_t ahw_rcvry_hdlr_mcp9843_ps(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

		temp_sns_reconfig(TEMP_SENSOR_MCP9843_PS);
		DEBUG_CPRINT(("mcp9843 recovery invoked\n"));
	}
	else
	{
		DEBUG_CPRINT(("mcp9843 self-test invoked\n"));
	}

	u8ret = ahw_al_temp_sensor_self_test(TEMP_SENSOR_MCP9843_PS);

	return u8ret;
}

uint8_t ahw_rcvry_hdlr_mcp9843_edge(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

		temp_sns_reconfig(TEMP_SENSOR_MCP9843_EDGE);
		DEBUG_CPRINT(("mcp9843 recovery invoked\n"));
	}
	else
	{
		DEBUG_CPRINT(("mcp9843 self-test invoked\n"));
	}

	u8ret = ahw_al_temp_sensor_self_test(TEMP_SENSOR_MCP9843_EDGE);

	return u8ret;
}

uint8_t ahw_rcvry_hdlr_mcp9843_gps(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

		temp_sns_reconfig(TEMP_SENSOR_MCP9843_GPS);
		DEBUG_CPRINT(("mcp9843 recovery invoked\n"));
	}
	else
	{
		DEBUG_CPRINT(("mcp9843 self-test invoked\n"));
	}

	u8ret = ahw_al_temp_sensor_self_test(TEMP_SENSOR_MCP9843_GPS);

	return u8ret;
}

uint8_t ahw_rcvry_hdlr_mcp9843_SPE01(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{
		//temp_sns_reconfig(TEMP_SENSOR_MCP9843_SPE01);
	}
	else
	{

	}

	//u8ret = ahw_al_temp_sensor_self_test(TEMP_SENSOR_MCP9843_SPE01);
	return u8ret;
}

uint8_t ahw_rcvry_hdlr_mcp9843_SPE02(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{
		//temp_sns_reconfig(TEMP_SENSOR_MCP9843_SPE02);
	}
	else
	{

	}

	//u8ret = ahw_al_temp_sensor_self_test(TEMP_SENSOR_MCP9843_SPE02);
	return u8ret;
}

uint8_t ahw_rcvry_hdlr_mcp9843_SPW01(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{
		//temp_sns_reconfig(TEMP_SENSOR_MCP9843_SPW01);
	}
	else
	{

	}

	//u8ret = ahw_al_temp_sensor_self_test(TEMP_SENSOR_MCP9843_SPW01);
	return u8ret;
}

uint8_t ahw_rcvry_hdlr_mcp9843_SPW02(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{
		//temp_sns_reconfig(TEMP_SENSOR_MCP9843_SPW02);
	}
	else
	{

	}

	//u8ret = ahw_al_temp_sensor_self_test(TEMP_SENSOR_MCP9843_SPW02);
	return u8ret;
}

uint8_t ahw_rcvry_hdlr_mcp9843_SPB01(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{
		//temp_sns_reconfig(TEMP_SENSOR_MCP9843_SPB01);
	}
	else
	{

	}

	//u8ret = ahw_al_temp_sensor_self_test(TEMP_SENSOR_MCP9843_SPB01);
	return u8ret;
}

uint8_t ahw_rcvry_hdlr_ina230_obc(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

		psm_sns_reconfig(PSM_INA230_OBC);

	}
	else
	{

	}

	u8ret = ahw_al_psm_self_test(PSM_INA230_OBC);

	return u8ret;
}

uint8_t ahw_rcvry_hdlr_ina230_ps(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

		psm_sns_reconfig(PSM_INA230_PS);

	}
	else
	{

	}

	u8ret = ahw_al_psm_self_test(PSM_INA230_PS);

	return u8ret;
}

uint8_t ahw_gpio_expander_mcp23008_edge_1(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

		//temp_sns_reconfig(GPIO_EXPANDER_MCP23008_EDGE_1);

	}
	else
	{

	}

	//u8ret = ahw_al_gpio_exp_self_test(GPIO_EXPANDER_MCP23008_EDGE_1);

	return u8ret;
}

uint8_t ahw_gpio_expander_mcp23008_edge_2(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

		//temp_sns_reconfig(GPIO_EXPANDER_MCP23008_EDGE_2);

	}
	else
	{

	}

	//u8ret = ahw_al_gpio_exp_self_test(GPIO_EXPANDER_MCP23008_EDGE_2);

	return u8ret;
}


uint8_t ahw_gpio_expander_mcp23008_obc_1(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

		//temp_sns_reconfig(GPIO_EXPANDER_MCP23008_OBC_1);

	}
	else
	{

	}

	//u8ret = ahw_al_gpio_exp_self_test(GPIO_EXPANDER_MCP23008_OBC_1);

	return u8ret;
}


uint8_t ahw_gpio_expander_mcp23008_obc_2(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

//		temp_sns_reconfig(GPIO_EXPANDER_MCP23008_OBC_2);

	}
	else
	{

	}

	//u8ret = ahw_al_gpio_exp_self_test(GPIO_EXPANDER_MCP23008_OBC_2);

	return u8ret;
}


uint8_t ahw_gpio_expander_mcp23008_gps(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

	//	temp_sns_reconfig(GPIO_EXPANDER_MCP23008_GPS);

	}
	else
	{

	}

	//u8ret = ahw_al_gpio_exp_self_test(GPIO_EXPANDER_MCP23008_GPS);

	return u8ret;
}




uint8_t ahw_rcvry_hdlr_ds620_ps(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

		dt_sns_reconfig(DIGITAL_THERMOSTAT_DS620_PS);

	}
	else
	{

	}

	u8ret = ahw_al_dt_self_test(DIGITAL_THERMOSTAT_DS620_PS);

	return u8ret;
}

uint8_t ahw_rcvry_hdlr_ds620_edge(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

		dt_sns_reconfig(DIGITAL_THERMOSTAT_DS620_EDGE);

	}
	else
	{

	}

	u8ret = ahw_al_dt_self_test(DIGITAL_THERMOSTAT_DS620_EDGE);

	return u8ret;
}

uint8_t ahw_rcvry_hdlr_adm1176_edge(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

		hsc_sns_reconfig(HSC_ADM1176_EDGE);

	}
	else
	{

	}

	u8ret = ahw_al_hsc_self_test(HSC_ADM1176_EDGE);

	return u8ret;
}

uint8_t ahw_rcvry_hdlr_adm1176_ps_1(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

		hsc_sns_reconfig(HSC_ADM1176_PS_1);

	}
	else
	{

	}

	u8ret = ahw_al_hsc_self_test(HSC_ADM1176_PS_1);

	return u8ret;
}

uint8_t ahw_rcvry_hdlr_adm1176_ps_2(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

		hsc_sns_reconfig(HSC_ADM1176_PS_2);

	}
	else
	{

	}

	u8ret = ahw_al_hsc_self_test(HSC_ADM1176_PS_2);

	return u8ret;
}






uint8_t ahw_rcvry_hdlr_thruster_morpheus_obc(uint8_t rcvry_mode)
{
	uint8_t u8ret=1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

	//	temp_sns_reconfig(THRUSTER_MORPHEUS_OBC); // need to check

	}
	else
	{

	}

	//u8ret = ahw_al_hsc_self_test(IMU_BMX160_OBC_2);
	/*
	 *  no self test implementation
	 */

	return u8ret;
}

uint8_t eps_rcvry_hdlr_com_fail(uint8_t rcvry_mode)
{
	uint8_t u8ret = 1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

	}
	else
	{

	}

	return u8ret;
}


void err_recovery()
{

}


uint8_t ahw_rcvry_hdlr_norflash_fmc_mt28ew(uint8_t rcvry_mode)
{
	//uint8_t u8ret=1;
	hal_ret_sts ret_sts = HAL_MAX_ERR;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{
#ifndef LINUX_TEMP_PORT
		ret_sts = io_hal_fmc_nor_flash_reinit(IOAL_INST_FMC_NOR1);
#endif
		os_delay(200);
		if(ret_sts == HAL_SCS)
		{
#ifndef LINUX_TEMP_PORT
			ret_sts = ahw_al_ext_mem_reinit(EXT_MEM_MT28EW128_FMC);
#endif
		    os_delay(200);
		}
		else
		{

		}

	}
	else
	{

	}

	//u8ret = bist_test_extmem(EXT_MEM_MT28EW128_FMC, extmem_fmc_mt28ew_bist_buffer);
	return ret_sts;
}


uint8_t ahw_rcvry_hdlr_norflash_qspi_mt25ql(uint8_t rcvry_mode)
{
//	uint8_t u8ret=1;
	hal_ret_sts ret_sts = HAL_MAX_ERR;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{
		ret_sts = io_hal_qspi_reinit(IOAL_INST_QSPI1);
		os_delay(200);
	    if(ret_sts == HAL_SCS)
        {
#ifndef LINUX_TEMP_PORT
	    	ret_sts = ahw_al_ext_mem_reinit(EXT_MEM_MT25QL512_QSPI);
#endif
		    os_delay(200);
		}
		else
		{

		}

	}
	else
	{

	}

//	u8ret = bist_test_extmem(EXT_MEM_MT25QL512_QSPI, extmem_qspi_mt25ql512_bist_buffer);
	return ret_sts;
}


void test_func_extmem(void)
{
  //  bist_test_extmem(EXT_MEM_MX25L512_QSPI, extmem_qspi_mx25l512_bist_buffer);
}




uint8_t err_rcvry_hdlr_always_fail_cblk(uint8_t rcvry_mode)
{
	uint8_t u8ret = 1;

	if(rcvry_mode == RCVRY_AND_SELF_TEST)
	{

	}
	else
	{

	}

	return u8ret;
}

