
#include "exram.h"

#include "shell.h"
#include "delay.h"
#include "encrypt.h"
//----------------------------------------------------------

void TestExram(char * para1, char * para2);

//----------------------------------------------------------

void InitExramCtrl(void)
{
	const static u16 encrypt = 0xffff;
	AddToEncrypt(&encrypt);
#if (0)
	AddShellCmd("EXRAM", "test sdram", TestExram);
#endif

}




//----------------------------------------------------------

u32 GetExRamAddr(u32 vaddr)
{
	if (vaddr < EXRAM1_BYTE_SIZE)
	{
		return EXRAM1_BASE_ADDR + vaddr;
	}
	else if (vaddr < (EXRAM1_BYTE_SIZE+EXRAM2_BYTE_SIZE))
	{
		return EXRAM2_BASE_ADDR + vaddr - EXRAM1_BYTE_SIZE;
	}
	else
	{
		return EXRAM2_BASE_ADDR + EXRAM2_BYTE_SIZE;
	}
}

void TestExram(char * para1, char * para2)
{
	int p1, p2;

	p1 = p2 = 0;
	if (para1 != NULL && strcmp(para1, "") != 0)
	{
		p1 = atoi(para1);
	}
	if (para2 != NULL && strcmp(para2, "") != 0)
	{
		p2 = atoi(para2);
	}
	if (p1 == p2)
	{
	}
	//--------------------------------
#define	TDT		u32

	if (p1 >= 0)	// 测试单个存储
	{
		volatile TDT * paddr = NULL;
		TDT dat;
		if ((p1 <= (EXRAM1_BYTE_SIZE-sizeof(TDT))) ||
				((p1 > EXRAM1_BYTE_SIZE) && p1 < (EXRAM1_BYTE_SIZE+EXRAM2_BYTE_SIZE-sizeof(TDT)))	)
		{
			paddr = (TDT*)GetExRamAddr(p1);
		}
		else
		{
			printf("addr out of range\r\n");
			return;
		}
		dat = *paddr;
		*paddr = p2;
		printf("before set, addr=0x%lx, dat=0x%lx, set to %d\r\n", (u32)paddr, dat, p2);
		dat = *paddr;
		if (dat == p2)
		{
			printf("success, ");
		}
		else
		{
			printf("failed, ");
		}
		printf("after set, addr=0x%lx, dat=0x%lx\r\n", (u32)paddr, dat);
	}
	else if (p1 < 0 && p2 < 0)
	{
		volatile TDT * paddr = NULL;
		TDT dat;
		int i, count;
		int n;
		p1 *= -1;
		p1 -= 1;
		if (p1 > EXRAM_BYTE_SIZE)
		{
			p1 = EXRAM_BYTE_SIZE;
		}

		p2 *= -1;
	//	p2 -= 1;
		if (p2 > EXRAM_BYTE_SIZE)
		{
			p2 = EXRAM_BYTE_SIZE;
		}

		n = p1 + p2;

		p1 /= sizeof(TDT);
		p2 /= sizeof(TDT);

		count = 0;
		do
		{
			printf("\r\nat %d time, test sdram\r\n", count++);
			// 写入信息
			printf("write dat to sdram, addr (0x%lx, 0x%lx)\r\n", GetExRamAddr(p1*sizeof(TDT)),  GetExRamAddr(p2*sizeof(TDT)));
			for (i = p1; i < p2; i++)
			{
				dat = (TDT)(i+n+count);
		#if (0)

				if ((i&0xffff) == 0)
				{
					printf("write dat to sdram, addr=0x%lx\r\n", (u32)paddr);
					DelayMs(10);
				}
		#endif
				paddr = (TDT*)(GetExRamAddr(i*sizeof(TDT)));
				*paddr = dat;
			}

			// 校验信息
			printf("check dat from sdram, addr (0x%lx, 0x%lx)\r\n", GetExRamAddr(p1*sizeof(TDT)),  GetExRamAddr(p2*sizeof(TDT)));
			for (i = p1; i < p2; i++)
			{
				dat = (TDT)(i+n+count);
		#if (0)
				if ((i&0xffff) == 0)
				{
					printf("check dat from sdram, addr=0x%lx\r\n", (u32)paddr);
					DelayMs(10);
				}
		#endif
				paddr = (TDT*)(GetExRamAddr(i*sizeof(TDT)));
				if (*paddr != dat)
				{
					printf("addr=0x%lx check error\r\n", (u32)paddr);
					break;
				}
			}
			if (i == p2)
			{
				printf("sdram check ok\r\n");
			}
			else
			{

			}

			DelayMs(100);
			n *= count;

			if (IsConsoleCancel() != 0)		// 终端输入停止
			{
				break;
			}

		}while(1);
	}

}


