📄 fram.cpp
字号:
// Fram.cpp: implementation of the CFram class.
//
//////////////////////////////////////////////////////////////////////
#include "Fram.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CFram::CFram()
{
m_pMEMRegs = (volatile MEMreg*)VirtualAlloc(0, sizeof(MEMreg), MEM_RESERVE, PAGE_NOACCESS);
if (NULL == m_pMEMRegs)
{
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("Fail to Alloc Virtual region! m_pMEMRegs\r\n")));
}
else
{
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("Alloc Virtual region success! m_pMEMRegs\r\n")));
//映射物理地址
if (!VirtualCopy((PVOID)m_pMEMRegs, (PVOID)MEMCTRL_BASE, sizeof(MEMreg), PAGE_READWRITE | PAGE_NOCACHE))
{
VirtualFree((PVOID)m_pMEMRegs, 0, MEM_RELEASE);
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("Fail to map FRAM abstract address to virtual address! m_pMEMRegs\r\n")));
}
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("Map FRAM abstract address to virtual address successed! m_pMEMRegs\r\n")));
}
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T(":::rBANKCON4 = %x\trBWSCON = %x\r\n"),m_pMEMRegs->rBANKCON4, m_pMEMRegs->rBWSCON));
//m_pMEMRegs->rBANKCON4 &= 0x0;
m_pMEMRegs->rBANKCON4 |= (0x7ff<<4);
m_pMEMRegs->rBWSCON &= ~(0x3 << 16);
m_pMEMRegs->rBWSCON |= 0x00040000;
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("rBANKCON4 = %x\trBWSCON = %x\r\n"),m_pMEMRegs->rBANKCON4, m_pMEMRegs->rBWSCON));
//申请内存
m_pFram = (volatile UCHAR*)VirtualAlloc(0, FRAM_SIZE, MEM_RESERVE, PAGE_READWRITE);
if (NULL == m_pFram)
{
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("Fail to Alloc Virtual region!\r\n")));
}
else
{
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("Alloc Virtual region success!\r\n")));
//映射物理地址
if (!VirtualCopy((PVOID)m_pFram, (PVOID)(FRAM_START_ADD>>8), FRAM_SIZE, PAGE_READWRITE | PAGE_NOCACHE | PAGE_PHYSICAL))
{
VirtualFree((PVOID)m_pFram, 0, MEM_RELEASE);
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("Fail to map FRAM abstract address to virtual address!\r\n")));
}
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("Map FRAM abstract address to virtual address successed!\r\n")));
}
}
CFram::~CFram()
{
VirtualFree((PVOID)m_pFram, 0, MEM_RELEASE);
}
BOOL CFram::ReadByte(unsigned long offset,UCHAR& result)//, UCHAR &uchByte)
{
//检查地址有效性
if(offset > FRAM_SIZE)
{
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("The offset(0x%x) you want to access is out of the range!\r\n"),offset));
return FALSE;
}
result = *(m_pFram + offset);//There is a warning,ignore it
//Seep(1);
return TRUE;
}
BOOL CFram::WriteByte(unsigned long offset, UCHAR uchByte)
{
//检查地址有效性
if(offset > FRAM_SIZE)
{
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("The offset(0x%x) you want to access is out of the range!\r\n"),offset));
return FALSE;
}
*(m_pFram + offset) = uchByte;//There is a warning,ignore it!
Sleep(1);
return TRUE;
}
BOOL CFram::ReadBlock(unsigned long startOffset,unsigned long len,unsigned char* desData)
{
//参数有效性检查
if (len > FRAM_SIZE)
{
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("The offset(0x%x) you want to access is out of the range!\r\n"),(startOffset + len)));
return FALSE;
}
unsigned char temp;
for(unsigned long i = 0; i < len; i++)
{
temp = 0;
if (FALSE == ReadByte((startOffset + i),temp))
{
return FALSE;
}
*desData = temp;
desData++;
}
return TRUE;
}
BOOL CFram::Writeblock(unsigned long startOffset, unsigned long len, const unsigned char *srcData)
{
//参数有效性检查
if ((startOffset + len) > FRAM_SIZE)
{
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("The offset(0x%x) you want to access is out of the range!\r\n"),(startOffset + len)));
return FALSE;
}
for (unsigned long l = 0; l < len; l++)
{
if (FALSE == WriteByte((startOffset + l),*srcData))
{
return FALSE;
}
srcData++;
}
return TRUE;
}
BOOL CFram::LowLevelFormat()
{
unsigned char temp;
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("Format FRAM, keep the power on^^^^^^\n\r")));
for (unsigned long l = 0; l < FRAM_SIZE; l++)
{
if (FALSE == WriteByte(l,LOW_LEVEL_FORMAT_VALUE))
{
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("Low level formar error::: Write error add = 0x%x\r\n"),(FRAM_START_ADD + l)));
}
temp = 0;
if ((FALSE == ReadByte(l,temp)) ||
(LOW_LEVEL_FORMAT_VALUE != temp))
{
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("Low level formar error::: Read error add = 0x%x\r\n"),(FRAM_START_ADD + l)));
}
if (0 == (l%1024))
{
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("Format %dK>>>>>>>>\r\n"),(l/1024 + 1)));
}
}
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("Format FRAM, end^^^^^^\n\r")));
return TRUE;
}
BOOL CFram::PrintChip(unsigned long startOffset, unsigned long len)
{
//参数有效性检查
if ((startOffset + len) > FRAM_SIZE)
{
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("The offset(0x%x) you want to access is out of the range!\r\n"),(startOffset + len)));
return FALSE;
}
//打印表头
//RETAILMSG(FRAM_DEBUG_INF_OUT,(_T(" 1 2 3 4 5 6 7 8 9 a b c d e f\n\r")));
unsigned char temp = 0;
for (unsigned long l = 0; l < len; l++)
{
ReadByte((startOffset + l),temp);
if (0 == ((startOffset + l) % 16))
{
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("\r\n%x:%x "),(0x20000000 + startOffset + l),temp));
}
else
{
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("%x "),temp));
}
}
return TRUE;
}
BOOL CFram::ReadAll(LPCTSTR Filename)
{
//PBYTE status=NULL;
unsigned char* status = (unsigned char*)malloc(FileSize);
if (NULL == status)
{
free(status);
return 0;
}
DWORD actlen = FileSize;
HANDLE hFile = INVALID_HANDLE_VALUE;
TCHAR a[100]=_T("\\FriendlyARM\\");
DWORD Filelenth = wcslen(Filename);
DWORD i;
for(i=0;i<Filelenth;i++)
{
a[i+13]= *Filename;
Filename++;
}
hFile =CreateFile(a,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_ALWAYS,0,0);
if(hFile==INVALID_HANDLE_VALUE)
{
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("CreateFile error ")));
free(status);
return 0;
}
if(!ReadBlock(0,actlen,status))
{
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("ReadBlock error ")));
free(status);
return 0;
}
if(!WriteFile(hFile,status,FileSize,&actlen,NULL))
{
RETAILMSG(FRAM_DEBUG_INF_OUT,(_T("WriteFile error ")));
free(status);
return 0;
}
free(status);
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -