📄 intel_c3_2x16.c
字号:
#endif
*gpulCurAddr = FLASH_CLEAR_STATUS;
//
// Erase the block before unlocking the next block.
//
if(gbUnlocked)
{
//
// Erase the flash block
//
*gpulCurAddr = FLASH_BLOCK_ERASE;
*gpulCurAddr = FLASH_BLOCK_ERASE_RESUME;
//
// Read erase status.
//
*gpulCurAddr = FLASH_READ_STATUS;
//
// Wait for the Flash block command to succeed.
//
bSuccess = WaitForReady(gpulCurAddr, 10000, EXT_STATUS_WBUFFER_READY);
if(bSuccess == FALSE)
{
return FALSE;
}
*gpulCurAddr = FLASH_CLEAR_STATUS;
gbUnlocked = FALSE;
//
// Increment to the next block.
//
//gpulCurAddr += FLASH_BLOCK_SIZE>>2;
gpulCurAddr+= sBlockInfo[gdwCurEraseRegion].block_size>>2;
//
//go to the next bloxk
//
gdwCurEraseBlock = gdwCurEraseBlock+1;
if(gdwCurEraseBlock>=sBlockInfo[gdwCurEraseRegion].blocks)
{
gdwCurEraseBlock=0;
gdwCurEraseRegion=gdwCurEraseRegion+1;
//
//return error the current postiotn > the Flash MAX length
//
if(gdwCurEraseRegion>=gdwNumEraseRegion)
return FALSE;
}
}
#ifdef interflash_debug
EdbgOutputDebugString
(
"FinishEraseFlashC3_2x16 3 %x %x %x %x %x,Current=%x %x %x\r\n",
gdwCurEraseRegion,gdwCurEraseRegion,gdwCurEraseBlock,sBlockInfo[gdwCurEraseRegion].block_size,
sBlockInfo[gdwCurEraseRegion].blocks,gpulCurAddr,gdwStartAddr,gdwLength
);
#endif
//
// Erase the Flash
//
while((DWORD)gpulCurAddr < (gdwStartAddr + gdwLength))
{
#ifdef interflash_debug
EdbgOutputDebugString
(
"FinishEraseFlashC3_2x16 4 %x %x %x %x,Current=%x %x %x\r\n",
gdwCurEraseRegion,gdwCurEraseRegion,gdwCurEraseBlock,sBlockInfo[gdwCurEraseRegion].block_size,
gpulCurAddr
);
#endif
//
// Clear the lock bits if the flash block was locked.
//
*gpulCurAddr = FLASH_LOCK_BIT;
*gpulCurAddr = FLASH_LOCK_CLEAR;
*gpulCurAddr = FLASH_READ_STATUS;
//
// Wait for the Flash block command to succeed.
//
bSuccess = WaitForReady(gpulCurAddr, 10000, EXT_STATUS_WBUFFER_READY);
*gpulCurAddr = FLASH_CLEAR_STATUS;
if(!bSuccess)
{
break;
}
//
// Erase the flash block
//
*gpulCurAddr = FLASH_BLOCK_ERASE;
*gpulCurAddr = FLASH_BLOCK_ERASE_RESUME;
*gpulCurAddr = FLASH_READ_STATUS;
//
// Wait for the Flash block command to succeed.
//
bSuccess = WaitForReady(gpulCurAddr, 10000, EXT_STATUS_WBUFFER_READY);
*gpulCurAddr = FLASH_CLEAR_STATUS;
if(!bSuccess)
{
break;
}
//
// Occasionally write out a dot so they don't think
// the system is dead.
//
EdbgOutputDebugString(".");
//
// Increment to the next block.
//
//gpulCurAddr += FLASH_BLOCK_SIZE>>2;
gpulCurAddr+= sBlockInfo[gdwCurEraseRegion].block_size>>2;
//
//go to the next bloxk
//
gdwCurEraseBlock = gdwCurEraseBlock+1;
if(gdwCurEraseBlock>=sBlockInfo[gdwCurEraseRegion].blocks)
{
gdwCurEraseBlock=0;
gdwCurEraseRegion=gdwCurEraseRegion+1;
//
//return error the current postiotn > the Flash MAX length
//
if(gdwCurEraseRegion>gdwNumEraseRegion){
EdbgOutputDebugString("error erase regin \r\n" );
return FALSE;
}
}
}
return bSuccess;
}
extern volatile int bStop;
//****************************************************************************
// WriteFlashJ3_2x16
//****************************************************************************
// Writes to J3 Flash. Different from parameters than OEMWriteFlash.
//
// dwDest - Address in flash where the start of the downloaded image
// is to be written. Note that if this is not a flash
// block-aligned address, the platform code will need
// to take this into account and handle it as a special
// case.
//
// dwSource - Location of the Flash Cache.
//
// dwLength - Length of the image, in bytes, to be written to flash.
//
//
BOOL WriteFlashC3_2x16(DWORD dwDest, DWORD dwSource, DWORD dwLength)
{
ULONG ulCount;
//ULONG ulEndCount;
volatile PULONG pulBlockAddress;
volatile PULONG pulDest, pulSource;
//ULONG usWordsToWrite;
//ULONG ulStart;
BOOL bSuccess;
//ULONG ulAddDelay;
ULONG ulBufferSizeLen=0;
ulCount = 0;
pulDest = (PULONG)dwDest;
pulSource = (PULONG)dwSource;
//
//get the current region and block index
//
GotEraseFlashSectorIndex_C3_2X16(dwDest);
#ifdef interflash_debug
EdbgOutputDebugString
(
"WriteFlashC3_2x16 0: %x %x %x %x %x--%x,%x\r\n",
gdwNumEraseRegion,gdwCurEraseRegion,gdwCurEraseBlock,sBlockInfo[gdwCurEraseRegion].block_size,
sBlockInfo[gdwCurEraseRegion].blocks,dwDest,dwLength
);
#endif
while( ulCount < dwLength)
{
#if 0
//
// Calculate the block address
//
gdwCurEraseBlockMask = ~(sBlockInfo[gdwCurEraseRegion].block_size-1);
pulBlockAddress = (ULONG *)(((ULONG)&pulDest[ulCount>>2]) & gdwCurEraseBlockMask);
//
// Write the Flash to buffer command until the flash is ready.
//
ulStart = GetSystemTimeInMsec();
ulAddDelay = 1;
do
{
*pulBlockAddress = FLASH_WRITE_TO_BUFFER;
//
// Check to make sure that we are not in a loop.
//
ulCurrent = GetSystemTimeInMsec();
if(ulStart + 10000 <ulCurrent)
{
EdbgOutputDebugString("FLASH_WRITE_TO_BUFFER - Failed\n.");
EdbgOutputDebugString("pulBlockAddress = %x.\n",(ULONG)pulBlockAddress);
EdbgOutputDebugString("*pulBlockAddress = %x.\n",(ULONG)*pulBlockAddress);
*pulBlockAddress = FLASH_READ_STATUS;
EdbgOutputDebugString("*FLASH_READ_STATUS pulBlockAddress = %x.\n",(ULONG)*pulBlockAddress);
while(1);
return FALSE;
}
DelayInuSec(ulAddDelay);
ulAddDelay =(ulAddDelay<<1);
}
while((*pulBlockAddress & EXT_STATUS_WBUFFER_READY)!= EXT_STATUS_WBUFFER_READY);
//
// Program the Flash size to be written.
//
usWordsToWrite = (gdwFlashBufferSize - 1)>>2;
*pulBlockAddress = (usWordsToWrite<<16) | usWordsToWrite;
ulEndCount = ulCount + gdwFlashBufferSize;
ulBufferSizeLen = ulBufferSizeLen+gdwFlashBufferSize;
do
{
pulDest[ulCount>>2] = pulSource[ulCount>>2];
ulCount += sizeof(ULONG);
//
// Account for timing problem.
//
do
{
} while(!(*pulBlockAddress & EXT_STATUS_WBUFFER_READY) && (ulAddDelay-- != 0));
} while(ulCount < ulEndCount);
if(ulBufferSizeLen>sBlockInfo[gdwCurEraseRegion].block_size)
{
ulBufferSizeLen=0;
//
//go to the next bloxk
//
gdwCurEraseBlock = gdwCurEraseBlock+1;
if(gdwCurEraseBlock>=sBlockInfo[gdwCurEraseRegion].blocks)
{
gdwCurEraseBlock=0;
gdwCurEraseRegion=gdwCurEraseRegion+1;
//
//return error the current postiotn > the Flash MAX length
//
if(gdwCurEraseRegion>=gdwNumEraseRegion)
return FALSE;
}
}
//
// End of programming the block.
//
*pulBlockAddress = FLASH_BLOCK_PROGRAM_RESUME;
// DelayInuSec(500);
//
// Occasionally write out a dot so they don't think
// the system is dead.
//
if(((ULONG)&pulDest[ulCount>>2] & (sBlockInfo[gdwCurEraseRegion].block_size - 1)) == 0)
{
EdbgOutputDebugString(".");
}
//
// Wait for the Flash block command to succeed.
//
bSuccess = WaitForReady(pulBlockAddress, 6000, EXT_STATUS_WBUFFER_READY);
if(!bSuccess)
{
EdbgOutputDebugString("FLASH_BLOCK_PROGRAM_RESUME - Failed\n.");
EdbgOutputDebugString("pulBlockAddress = %x.\n",(ULONG)pulBlockAddress);
EdbgOutputDebugString("*pulBlockAddress = %x.\n",(ULONG)*pulBlockAddress);
*pulBlockAddress = FLASH_READ_STATUS;
EdbgOutputDebugString("*FLASH_READ_STATUS pulBlockAddress = %x.\n",(ULONG)*pulBlockAddress);
while(1);
break;
}
}
*pulBlockAddress = FLASH_READ_MODE;
return TRUE;
#else
ULONG i,j;
ULONG ulCountCmp;
ulCountCmp = 0;
//EdbgOutputDebugString("200\r\n");
for(i=0;( i<(sBlockInfo[gdwCurEraseRegion].block_size/2)) && (ulCount < dwLength); i++)
//for(i=0; i<4096 && ulCount < dwLength; i++)
{
pulDest[ulCount>>2] = 0x00400040; //#define FLASH_WRITE_WORD 0x0040
//for p30
for(j=0;j<100;j++){}
pulDest[ulCount>>2] = pulSource[ulCount>>2];
//for p30
for(j=0;j<100;j++){}
bSuccess = WaitForReady(&pulDest[ulCount>>2], 50000, EXT_STATUS_WBUFFER_READY);
if( !bSuccess )
{
EdbgOutputDebugString("\r\nfail at 0x%x 0x%x\r\n.",
(ULONG) (&pulDest[ulCount>>2]),
(ULONG) (&pulSource[ulCount>>2]));
while (1);
return bSuccess;
}
//if( ulCount % 1023 == 0)
// EdbgOutputDebugString("+");
ulCount += sizeof(ULONG);
}
#ifdef interflash_debug
EdbgOutputDebugString("200---%x %x\r\n",bSuccess,ulCount);
#endif
if( bSuccess )
{
pulDest[ulCountCmp>>2] = FLASH_READ_MODE;
for(i=0; (i<(sBlockInfo[gdwCurEraseRegion].block_size/2)) && (ulCountCmp < dwLength); i++)
//for(i=0; i<4096 && ulCountCmp < dwLength; i++)
{
// verify pargramed word
unsigned long t;
//pusDest[ulCountCmp>>1] = FLASH_READ_MODE;
t = pulDest[ulCountCmp>>2];
if( t != pulSource[ulCountCmp>>2] )
{
bSuccess = FALSE;
EdbgOutputDebugString("\r\nmismatch at 0x%x 0x%x 0x%x != 0x%x failed\r\n.",
(ULONG) (&pulDest[ulCountCmp>>2]),
(ULONG) (&pulSource[ulCountCmp>>2]),
t,
pulSource[ulCountCmp>>2] );
while(1);
return bSuccess;
}
//if( ulCountCmp % 1023 == 0)
// EdbgOutputDebugString("-");
ulCountCmp += sizeof(ULONG);
}
}
EdbgOutputDebugString(".");
//
//go to the next bloxk
//
gdwCurEraseBlock = gdwCurEraseBlock+1;
if(gdwCurEraseBlock>=sBlockInfo[gdwCurEraseRegion].blocks)
{
gdwCurEraseBlock=0;
gdwCurEraseRegion=gdwCurEraseRegion+1;
//
//return error the current postiotn > the Flash MAX length
//
if(gdwCurEraseRegion>=gdwNumEraseRegion)
return FALSE;
}
}
pulBlockAddress = (unsigned long*)PHYSICAL_ADDR_FLASH_INTEL;
*pulBlockAddress = FLASH_READ_MODE;
return TRUE;
#endif
}
//****************************************************************************
// WaitForReady
//****************************************************************************
// Wait until the flash is ready.
//
//
static int WaitForReady
(
volatile ULONG *pFlash,
ULONG ulTimeoutInMsec,
ULONG ulBitField
)
{
ULONG ulStart;
ULONG ulCurrent;
BOOL bSuccess = FALSE;
ulStart = GetSystemTimeInMsec();
do
{
//
// See if there are errors.
//
if((*pFlash & ulBitField)== ulBitField)
{
bSuccess = TRUE;
break;
}
//
// Get the current time.
//
ulCurrent = GetSystemTimeInMsec();
} while(ulCurrent<ulStart + ulTimeoutInMsec);
if(!bSuccess)
{
EdbgOutputDebugString("*pFlash = %x .\n",(ULONG)*pFlash);
}
return bSuccess;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -