/**
 * @file exo_osal_mem_debug.c
 *
 * @brief This file contains function definitions for memory debugging.
 *
 * @copyright Copyright 2023 Antaris, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdint.h>
#include "exo_osal_mem_debug.h"
#include "exo_common.h"
#include "exo_osal_mem_cpy.h"
#include "exo_osal.h"
#include "obc_tc_tm.h"
#ifndef LINUX_TEMP_PORT
#include "exo_types.h"
#include "FreeRTOS.h"
#endif
#include "exo_ahw_al_ext_flash_common.h"

uint8_t mem_debug_data;
uint32_t obc_mem_debug_tm_enc_frame;
#ifndef LINUX_TEMP_PORT
uint8_t PLACE_IN_EXT_RAM_MEM sdram_test_array[OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH];
uint8_t PLACE_IN_EXT_FMC_FLASH_MEM fmc_nor_flash_test_array[OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH];
uint8_t PLACE_IN_EXT_QSPI_FLASH_MEM qspi_nor_flash_test_array[OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH];
#else
uint8_t sdram_test_array[OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH];
uint8_t fmc_nor_flash_test_array[OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH];
uint8_t qspi_nor_flash_test_array[OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH];
#endif

/**
 * @brief This API is used to test the requested memory by writing 
 * and reading using test different pattern
 */
static uint8_t test_func(uint8_t* addr)
{
    uint8_t sts=0;
    uint8_t write_buff[OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH],read_buff[OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH];
    os_memset((uint8_t*)write_buff,MEM_TEST_PATTERN_55,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
    os_memcpy_extmem(write_buff,addr,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH,NULL,1,1);
    os_memcpy_extmem(addr,read_buff,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH,NULL,1,1);
    for(uint8_t i=0;i<OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH;i++)
    {
        if(write_buff[i] != read_buff[i])
        {
            sts = 0;
            break;
        }
        sts = 1;
    }
    os_memset((uint8_t*)read_buff,0x00,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
    if(sts == 1)
    {
        os_memset((uint8_t*)write_buff,MEM_TEST_PATTERN_AA,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
        os_memcpy_extmem((uint8_t*)write_buff,addr,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH,NULL,1,1);
        os_memcpy_extmem(addr,(uint8_t*)read_buff,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH,NULL,1,1);
        for(uint8_t i=0;i<OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH;i++)
        {
            if(write_buff[i] != read_buff[i])
            {
                sts = 0;
                break;
            }
            sts = 1;
        }
    }
    return sts;
}

/**
 * @brief This API returns the encoded data frame
 */
uint8_t obc_mem_debug_tm_pack_data()
{
    return obc_mem_debug_tm_enc_frame ;

}

/**
 * @brief This API decode the test command and call the respective memory test function
 */
int8_t obc_mem_debug_tc_decode(void *mem_type_1)
{
    uint8_t sts;
    os_thread_handle_ptr temp = (os_thread_handle_ptr)mem_type_1;
    uint8_t* ptr = temp->app_entry_args;
    mem_debug_data = (*ptr >> 4 & 0x0f);
    DEBUG_CPRINT(("dataptr %d\n",*ptr));
    DEBUG_CPRINT(("mem_deug %d\n",mem_debug_data));
    switch(mem_debug_data)
    {
        case e_mem_debug_sdram_test:
            {
                DEBUG_CPRINT(("The memory type is : %d\n",mem_debug_data));
                sts = test_func(sdram_test_array);
                obc_mem_debug_tm_enc_frame = mem_debug_data << 4 & 0xf0;
                obc_mem_debug_tm_enc_frame = obc_mem_debug_tm_enc_frame | (sts<<3);
                obc_mem_debug_tm_send();
            }
            break;

        case e_mem_debug_fmc_nor_flash_test:
            {
                DEBUG_CPRINT(("The memory type is : %d\n",mem_debug_data));
                sts = test_func(fmc_nor_flash_test_array);
                obc_mem_debug_tm_enc_frame = mem_debug_data << 4 & 0xf0;
                obc_mem_debug_tm_enc_frame = obc_mem_debug_tm_enc_frame | (sts<<3);
                obc_mem_debug_tm_send();
            }
            break;

        case e_mem_debug_qspi_nor_flash_test:
            {
                DEBUG_CPRINT(("The memory type is : %d\n",mem_debug_data));
                sts = test_func(qspi_nor_flash_test_array);
                obc_mem_debug_tm_enc_frame = mem_debug_data << 4 & 0xf0;
                obc_mem_debug_tm_enc_frame = obc_mem_debug_tm_enc_frame | (sts<<3);
                obc_mem_debug_tm_send();
            }
            break;

        case e_iram_heap_availablity_test:
            {
             
#ifndef LINUX_TEMP_PORT
                obc_mem_debug_tm_enc_frame = xPortGetFreeHeapSize();
#else
                obc_mem_debug_tm_enc_frame = 1000;
#endif
                obc_mem_debug_tm_send();
            }
            break;

        default:
            DEBUG_CPRINT(("test:%d\n",mem_debug_data));
            break;

    }
    os_free(ptr);
    ptr = NULL;
    os_thread_delete(OBC_MEM_DEBUG_TC);
    return 1;
}


uint8_t mem_pattern_compare(uint8_t* ptr1, uint8_t pattern, int32_t len)
{
	uint8_t sts=0;
	len--;
	while(len>=0)
	{
		if(ptr1[len]!=pattern)
		{
			sts=1;
			break;
		}
		len--;
	}
	return sts;
}

uint8_t external_flash_test_fullchip_write(uint8_t mem_intf_id, uint8_t pattern)
{
	uint8_t sts=CPY_ERR;
	uint32_t buffer_len=1*1024*1024;  //1mb buffer
	uint8_t* end_addr=NULL;
	uint8_t* start_addr=NULL;
	switch(mem_intf_id)
	{
	case FMC_FLASH_MEM:
		end_addr=(uint8_t*)0x60000000+FMC_NOR_FLASH_MEM_SIZE;
		start_addr=(uint8_t*)0x60000000;
		break;
	case QSPI_FLASH_MEM:
		end_addr=(uint8_t*)0x90000000+(QSPI_NOR_FLASH_MEM_SIZE);
		start_addr=(uint8_t*)0x90000000;
		break;
	}
	uint8_t* src_addr = os_malloc_eram(buffer_len);
	memset(src_addr,pattern,buffer_len);
	while(start_addr<end_addr)
	{
		if(start_addr==(uint8_t*)0x93f00000)
		{
			buffer_len--;
		}
		sts=os_memcpy_extmem(start_addr,src_addr,buffer_len,NULL,1,1);
		if(start_addr==(uint8_t*)0x93f00000)
		{
			buffer_len++;
		}
		if(sts!=CPY_OK)
		{
			break;
		}
		else
		{
			start_addr+=buffer_len;
			os_delay(10);
		}
	}
	os_free(src_addr);
	return sts;
}

uint8_t external_flash_test_fullchip_read(uint8_t mem_intf_id, uint8_t pattern)
{
	uint8_t sts=CPY_ERR;
	uint32_t buffer_len=1*1024*1024;  //1mb buffer
	uint8_t* end_addr=NULL;
	uint8_t* start_addr=NULL;
	switch(mem_intf_id)
	{
	case FMC_FLASH_MEM:
		end_addr=(uint8_t*)0x60000000+FMC_NOR_FLASH_MEM_SIZE;
		start_addr=(uint8_t*)0x60000000;
		break;
	case QSPI_FLASH_MEM:
		end_addr=(uint8_t*)0x90000000+QSPI_NOR_FLASH_MEM_SIZE;
		start_addr=(uint8_t*)0x90000000;
		break;
	}
	uint8_t* rcv_ptr = os_malloc_eram(buffer_len);
	while(start_addr<end_addr)
	{
		sts=os_memcpy_extmem(rcv_ptr,start_addr,buffer_len,NULL,1,1);
		if(sts!=CPY_OK)
		{
			break;
		}
		else
		{
			for(int i = 0;i<buffer_len;i++)
			{
				if(rcv_ptr[i]==pattern)
				{
					sts=0;
				}
				else
				{
					sts=1;
					break;
				}
			}
			start_addr+=buffer_len;
		}
		if(sts!=CPY_OK)
		{
			break;
		}
		os_delay(10);
	}
	os_free(rcv_ptr);
	return sts;
}


uint8_t ext_mem_test_block_read(uint8_t* ext_mem_ptr, uint8_t read_type)
{
	uint8_t sts=CPY_ERR;
	uint8_t* read_buff = os_malloc(OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
	sts = os_memcpy_extmem(read_buff,ext_mem_ptr,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH,NULL,1,1);
	switch(read_type)
	{
	case EXT_MEM_READ_55:
		sts = mem_pattern_compare(read_buff,MEM_TEST_PATTERN_55,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
		break;
	case EXT_MEM_READ_AA:
		sts = mem_pattern_compare(read_buff,MEM_TEST_PATTERN_AA,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
		break;
	case EXT_MEM_READ_00:
		sts = mem_pattern_compare(read_buff,MEM_TEST_PATTERN_00,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
		break;
	case EXT_MEM_READ_FF:
		sts = mem_pattern_compare(read_buff,MEM_TEST_PATTERN_FF,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
		break;
	}
	os_free(read_buff);
	return sts;
}

uint8_t ext_eeprom_mem_test_read(uint8_t mem_id, uint8_t read_type)
{
        uint8_t sts=CPY_ERR;
        uint8_t* read_buff = os_malloc(OBC_MEMORY_EEPROM_TEST_BYTE_LENGTH);
        sts = ahw_al_ext_mem_read(mem_id,0,read_buff,OBC_MEMORY_EEPROM_TEST_BYTE_LENGTH);
        switch(read_type)
        {
        case EXT_MEM_READ_55:
                sts = mem_pattern_compare(read_buff,MEM_TEST_PATTERN_55,OBC_MEMORY_EEPROM_TEST_BYTE_LENGTH);
                break;
        case EXT_MEM_READ_AA:
                sts = mem_pattern_compare(read_buff,MEM_TEST_PATTERN_AA,OBC_MEMORY_EEPROM_TEST_BYTE_LENGTH);
                break;
        case EXT_MEM_READ_00:
                sts = mem_pattern_compare(read_buff,MEM_TEST_PATTERN_00,OBC_MEMORY_EEPROM_TEST_BYTE_LENGTH);
                break;
        case EXT_MEM_READ_FF:
                sts = mem_pattern_compare(read_buff,MEM_TEST_PATTERN_FF,OBC_MEMORY_EEPROM_TEST_BYTE_LENGTH);
                break;
        }
        os_free(read_buff);
        return sts;
}

uint8_t ext_mem_test_block_write(uint8_t* ext_mem_ptr, uint8_t write_type)
{
	uint8_t sts=CPY_ERR;
	uint8_t* write_buff = os_malloc(OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
	switch(write_type)
	{
	case EXT_MEM_WRITE_55:
		os_memset(write_buff,MEM_TEST_PATTERN_55,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
		break;
	case EXT_MEM_WRITE_AA:
		os_memset(write_buff,MEM_TEST_PATTERN_AA,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
		break;
	case EXT_MEM_WRITE_00:
		os_memset(write_buff,MEM_TEST_PATTERN_00,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
		break;
	case EXT_MEM_WRITE_FF:
		os_memset(write_buff,MEM_TEST_PATTERN_FF,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
		break;
	}
	sts = os_memcpy_extmem(ext_mem_ptr,write_buff,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH,NULL,1,1);
	os_free(write_buff);
	return sts;
}

uint8_t ext_eeprom_mem_test_write(uint8_t mem_id, uint8_t write_type)
{
        uint8_t sts=CPY_ERR;
        uint8_t* write_buff = os_malloc(OBC_MEMORY_EEPROM_TEST_BYTE_LENGTH);
        switch(write_type)
        {
        case EXT_MEM_WRITE_55:
                os_memset(write_buff,MEM_TEST_PATTERN_55,OBC_MEMORY_EEPROM_TEST_BYTE_LENGTH);
                break;
        case EXT_MEM_WRITE_AA:
                os_memset(write_buff,MEM_TEST_PATTERN_AA,OBC_MEMORY_EEPROM_TEST_BYTE_LENGTH);
                break;
        case EXT_MEM_WRITE_00:
                os_memset(write_buff,MEM_TEST_PATTERN_00,OBC_MEMORY_EEPROM_TEST_BYTE_LENGTH);
                break;
        case EXT_MEM_WRITE_FF:
                os_memset(write_buff,MEM_TEST_PATTERN_FF,OBC_MEMORY_EEPROM_TEST_BYTE_LENGTH);
                break;
        }
        sts = ahw_al_ext_mem_write(mem_id,0,write_buff,OBC_MEMORY_EEPROM_TEST_BYTE_LENGTH);
        os_free(write_buff);
        return sts;
}

uint8_t ext_sdram_validation(ext_mem_test_tc_tm_s* mem_test_info)
{
	uint8_t sts=CPY_ERR;
	switch(mem_test_info->test_type)
	{
	case EXT_MEM_WRITE_55:
	case EXT_MEM_WRITE_AA:
	case EXT_MEM_WRITE_00:
	case EXT_MEM_WRITE_FF:
		 sts = ext_mem_test_block_write(sdram_test_array,mem_test_info->test_type);
         break;
	case EXT_MEM_READ_55:
	case EXT_MEM_READ_AA:
	case EXT_MEM_READ_00:
	case EXT_MEM_READ_FF:
		 sts = ext_mem_test_block_read(sdram_test_array,mem_test_info->test_type);
		 break;
	case EXT_MEM_ERASE_WRITE_READ:
		sts=extrnl_memory_test(mem_test_info->mem_intf_type);
         break;
	}
	return sts;
}


uint8_t ext_eeprom_validation(ext_mem_test_tc_tm_s* mem_test_info)
{
     uint8_t sts=CPY_ERR;
     if(mem_test_info->block_or_fullchip == BLOCK_LVL_MEM_TEST)
        {
        switch(mem_test_info->test_type)
        {
            case EXT_MEM_WRITE_55:
            case EXT_MEM_WRITE_AA:
            case EXT_MEM_WRITE_00:
            case EXT_MEM_WRITE_FF:
                if(mem_test_info->mem_intf_type == EXT_EEPROM_MEM_1)
                {
                    sts = ext_eeprom_mem_test_write(EXT_EEPROM_MEM_1,mem_test_info->test_type);
                }
                else
                {
                    sts = ext_eeprom_mem_test_write(EXT_EEPROM_MEM_2,mem_test_info->test_type);
                }
                break;
            case EXT_MEM_READ_55:
            case EXT_MEM_READ_AA:
            case EXT_MEM_READ_00:
            case EXT_MEM_READ_FF:
                if(mem_test_info->mem_intf_type == EXT_EEPROM_MEM_1)
                {
                    sts = ext_eeprom_mem_test_read(EXT_EEPROM_MEM_1,mem_test_info->test_type);
                }
                else
                {
                    sts = ext_eeprom_mem_test_read(EXT_EEPROM_MEM_2,mem_test_info->test_type);
                }
                break;
            case EXT_MEM_ERASE_WRITE_READ:
                sts=extrnl_memory_test(mem_test_info->mem_intf_type);
         break;
        }
        }
     else
     {
         switch(mem_test_info->test_type)
         {
             case EXT_MEM_WRITE_55:
                 sts = ext_eeprom_mem_test_write(mem_test_info->mem_intf_type,MEM_TEST_PATTERN_55);
                 break;
             case EXT_MEM_WRITE_AA:
                 sts = ext_eeprom_mem_test_write(mem_test_info->mem_intf_type,MEM_TEST_PATTERN_AA);
                 break;
             case EXT_MEM_WRITE_00:
                 sts = ext_eeprom_mem_test_write(mem_test_info->mem_intf_type,MEM_TEST_PATTERN_00);
                 break;
             case EXT_MEM_WRITE_FF:
                 sts = ext_eeprom_mem_test_write(mem_test_info->mem_intf_type,MEM_TEST_PATTERN_FF);
                 break;
             case EXT_MEM_READ_55:
                 sts = ext_eeprom_mem_test_read(mem_test_info->mem_intf_type,MEM_TEST_PATTERN_55);
                 break;
             case EXT_MEM_READ_AA:
                 sts = ext_eeprom_mem_test_read(mem_test_info->mem_intf_type,MEM_TEST_PATTERN_AA);
                 break;
             case EXT_MEM_READ_00:
                 sts = ext_eeprom_mem_test_read(mem_test_info->mem_intf_type,MEM_TEST_PATTERN_00);
                 break;
             case EXT_MEM_READ_FF:
                 sts = ext_eeprom_mem_test_read(mem_test_info->mem_intf_type,MEM_TEST_PATTERN_FF);
                 break;

                case EXT_MEM_ERASE:
                        break;
                }
        }

        return sts;

}

uint8_t ext_flash_validation(ext_mem_test_tc_tm_s* flsh_mem_test_info)
{
	uint8_t sts=CPY_ERR;
	if(flsh_mem_test_info->block_or_fullchip == BLOCK_LVL_MEM_TEST)
	{
		switch(flsh_mem_test_info->test_type)
		{
		case EXT_MEM_WRITE_55:
		case EXT_MEM_WRITE_AA:
		case EXT_MEM_WRITE_00:
		case EXT_MEM_WRITE_FF:
			if(flsh_mem_test_info->mem_intf_type==QSPI_FLASH_MEM)
			{
			 sts = ext_mem_test_block_write(qspi_nor_flash_test_array,flsh_mem_test_info->test_type);
			}
			else
			{
			sts = ext_mem_test_block_write(fmc_nor_flash_test_array,flsh_mem_test_info->test_type);
			}
             break;
		case EXT_MEM_READ_55:
		case EXT_MEM_READ_AA:
		case EXT_MEM_READ_00:
		case EXT_MEM_READ_FF:
			if(flsh_mem_test_info->mem_intf_type==QSPI_FLASH_MEM)
			{
			 sts = ext_mem_test_block_read(qspi_nor_flash_test_array,flsh_mem_test_info->test_type);
			}
			else
			{
			sts = ext_mem_test_block_read(fmc_nor_flash_test_array,flsh_mem_test_info->test_type);
			}
             break;
		case EXT_MEM_ERASE_WRITE_READ:
			sts=extrnl_memory_test(flsh_mem_test_info->mem_intf_type);
	         break;
		}

	}
	else
	{
		switch(flsh_mem_test_info->test_type)
		{
		case EXT_MEM_WRITE_55:
			sts = external_flash_test_fullchip_write(flsh_mem_test_info->mem_intf_type,MEM_TEST_PATTERN_55);
			break;
		case EXT_MEM_WRITE_AA:
			sts = external_flash_test_fullchip_write(flsh_mem_test_info->mem_intf_type,MEM_TEST_PATTERN_AA);
			break;
		case EXT_MEM_WRITE_00:
			sts = external_flash_test_fullchip_write(flsh_mem_test_info->mem_intf_type,MEM_TEST_PATTERN_00);
			break;
		case EXT_MEM_WRITE_FF:
			sts = external_flash_test_fullchip_write(flsh_mem_test_info->mem_intf_type,MEM_TEST_PATTERN_FF);
             break;
		case EXT_MEM_READ_55:
			sts = external_flash_test_fullchip_read(flsh_mem_test_info->mem_intf_type,MEM_TEST_PATTERN_55);
			break;
		case EXT_MEM_READ_AA:
			sts = external_flash_test_fullchip_read(flsh_mem_test_info->mem_intf_type,MEM_TEST_PATTERN_AA);
			break;
		case EXT_MEM_READ_00:
			sts = external_flash_test_fullchip_read(flsh_mem_test_info->mem_intf_type,MEM_TEST_PATTERN_00);
			break;
		case EXT_MEM_READ_FF:
			sts = external_flash_test_fullchip_read(flsh_mem_test_info->mem_intf_type,MEM_TEST_PATTERN_FF);
			break;

		case EXT_MEM_ERASE:
			break;
		}
	}
	return sts;
}

uint8_t ext_mem_validation(ext_mem_test_tc_tm_s* mem_test_info)
{
	uint8_t sts=CPY_ERR;
    switch(mem_test_info->mem_intf_type)
    {
    case EXT_SDRAM_MEM:
    	sts = ext_sdram_validation(mem_test_info);
    	break;
    case EXT_EEPROM_MEM_1:
        sts = ext_eeprom_validation(mem_test_info);
        break;
    case EXT_EEPROM_MEM_2:
        sts = ext_eeprom_validation(mem_test_info);
        break;
    case QSPI_FLASH_MEM:
    case FMC_FLASH_MEM:
    	sts = ext_flash_validation(mem_test_info);
    	break;
    }
    return sts;
}

/**
 * @brief This API test the external memory.
 */
uint8_t extrnl_memory_test(uint8_t mem_type)
{
	uint8_t sts=0;
#ifndef LINUX_TEMP_PORT
	uint8_t intg_sts=0,rd_sts=0,wr_sts=0,ers_sts=0;
	uint8_t* write_buffer= os_malloc(OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
	uint8_t* read_buffer= os_malloc(OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);

	os_memset(write_buffer,MEM_TEST_PATTERN_AA,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
	/* Fill the buffer to write */
	switch(mem_type)
	{
	case EXT_SDRAM_MEM:
		wr_sts = os_memcpy_extmem(sdram_test_array,write_buffer,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH,NULL,1,1);
		rd_sts = os_memcpy_extmem(read_buffer,sdram_test_array,OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH,NULL,1,1);
		break;
	case QSPI_FLASH_MEM:
	ers_sts = ahw_al_ext_mem_erase(EXT_MEM_QSPI_FLASH,(uint32_t)qspi_nor_flash_test_array,(uint32_t)qspi_nor_flash_test_array+OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
	wr_sts = ahw_al_ext_mem_write(EXT_MEM_QSPI_FLASH, (uint32_t)qspi_nor_flash_test_array,write_buffer, OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
	rd_sts = ahw_al_ext_mem_read(EXT_MEM_QSPI_FLASH, (uint32_t)qspi_nor_flash_test_array,read_buffer, OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
		break;
	case FMC_FLASH_MEM:
	ers_sts = ahw_al_ext_mem_erase(EXT_MEM_MT28EW128_FMC,(uint32_t)fmc_nor_flash_test_array,(uint32_t)fmc_nor_flash_test_array+OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
	wr_sts = ahw_al_ext_mem_write(EXT_MEM_MT28EW128_FMC, (uint32_t)fmc_nor_flash_test_array,write_buffer, OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
	rd_sts = ahw_al_ext_mem_read(EXT_MEM_MT28EW128_FMC, (uint32_t)fmc_nor_flash_test_array,read_buffer, OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
		break;
	case EXT_EEPROM_MEM_1:
	ers_sts = ahw_al_ext_mem_erase(EXT_MEM_MT28EW128_FMC,(uint32_t)fmc_nor_flash_test_array,(uint32_t)fmc_nor_flash_test_array+OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
	wr_sts = ahw_al_ext_mem_write(EXT_MEM_MT28EW128_FMC, (uint32_t)fmc_nor_flash_test_array,write_buffer, OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
	rd_sts = ahw_al_ext_mem_read(EXT_MEM_MT28EW128_FMC, (uint32_t)fmc_nor_flash_test_array,read_buffer, OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
		break;
        case EXT_EEPROM_MEM_2:
	ers_sts = ahw_al_ext_mem_erase(EXT_MEM_MT28EW128_FMC,(uint32_t)fmc_nor_flash_test_array,(uint32_t)fmc_nor_flash_test_array+OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
	wr_sts = ahw_al_ext_mem_write(EXT_MEM_MT28EW128_FMC, (uint32_t)fmc_nor_flash_test_array,write_buffer, OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
	rd_sts = ahw_al_ext_mem_read(EXT_MEM_MT28EW128_FMC, (uint32_t)fmc_nor_flash_test_array,read_buffer, OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
		break;
	}
	intg_sts = mem_pattern_compare(read_buffer,MEM_TEST_PATTERN_AA, OBC_MEMORY_DEBUG_TEST_BYTE_LENGTH);
	os_free(read_buffer);
	os_free(write_buffer);
	sts = ((intg_sts) | (ers_sts)<<1 | (wr_sts)<<2 | (rd_sts)<<3);
#endif        
	return sts;
}
