FLASH配置代码如下:
void InitFlash(void)
{
EALLOW;
//
// Set VREADST to the proper value for the flash banks to power up
// properly. This sets the bank power up delay.
//
Flash0CtrlRegs.FBAC.bit.VREADST = 0x14;
//
// At reset bank and pump are in sleep. A Flash access will power up the
// bank and pump automatically.
//
// After a Flash access, bank and pump go to low power mode (configurable
// in FBFALLBACK/FPAC1 registers) if there is no further access to flash.
//
// Power up Flash bank and pump. This also sets the fall back mode of
// flash and pump as active.
//
Flash0CtrlRegs.FPAC1.bit.PMPPWR = 0x1;
Flash0CtrlRegs.FBFALLBACK.bit.BNKPWR0 = 0x3;
//
// Disable Cache and prefetch mechanism before changing wait states
//
Flash0CtrlRegs.FRD_INTF_CTRL.bit.DATA_CACHE_EN = 0;
Flash0CtrlRegs.FRD_INTF_CTRL.bit.PREFETCH_EN = 0;
//
// Set waitstates according to frequency
//
// *CAUTION*
// Minimum waitstates required for the flash operating at a given CPU rate
// must be characterized by TI. Refer to the datasheet for the latest
// information.
//
#if CPU_FRQ_200MHZ
Flash0CtrlRegs.FRDCNTL.bit.RWAIT = 0x3;
#endif
#if CPU_FRQ_150MHZ
Flash0CtrlRegs.FRDCNTL.bit.RWAIT = 0x2;
#endif
#if CPU_FRQ_120MHZ
Flash0CtrlRegs.FRDCNTL.bit.RWAIT = 0x2;
#endif
//
// Enable Cache and prefetch mechanism to improve performance of code
// executed from Flash.
//
Flash0CtrlRegs.FRD_INTF_CTRL.bit.DATA_CACHE_EN = 1;
Flash0CtrlRegs.FRD_INTF_CTRL.bit.PREFETCH_EN = 1;
//
// At reset, ECC is enabled. If it is disabled by application software and
// if application again wants to enable ECC.
//
Flash0EccRegs.ECC_ENABLE.bit.ENABLE = 0xA;
EDIS;
//
// Force a pipeline flush to ensure that the write to the last register
// configured occurs before returning.
//
__asm(" RPT #7 || NOP");
}
//
// SeizeFlashPump - Wait until the flash pump is available. Then take control
// of it using the flash pump Semaphore.
//
void SeizeFlashPump(void)
{
EALLOW;
#ifdef CPU1
while (FlashPumpSemaphoreRegs.PUMPREQUEST.bit.PUMP_OWNERSHIP != 0x2)
{
FlashPumpSemaphoreRegs.PUMPREQUEST.all = IPC_PUMP_KEY | 0x2;
}
#elif defined(CPU2)
while (FlashPumpSemaphoreRegs.PUMPREQUEST.bit.PUMP_OWNERSHIP != 0x1)
{
FlashPumpSemaphoreRegs.PUMPREQUEST.all = IPC_PUMP_KEY | 0x1;
}
#endif
EDIS;
}
//
// Init_Flash_Sectors - Initialize flash API and active flash bank sectors
//
void Init_Flash_Sectors(void)
{
EALLOW;
Flash0EccRegs.ERR_THRESHOLD.bit.ERR_THRESHOLD = 0x1;
Flash0EccRegs.ECC_ENABLE.bit.ENABLE = 0x0;
Fapi_StatusType oReturnCheck;
//
// This function is required to initialize the Flash API based on System
// frequency before any other Flash API operation can be performed
//
oReturnCheck = Fapi_initializeAPI(F021_CPU0_BASE_ADDRESS, 200);
if(oReturnCheck != Fapi_Status_Success)
{
Example_Error(oReturnCheck);
}
//
// Fapi_setActiveFlashBank function sets the Flash bank and FMC for further
// Flash operations to be performed on the bank
//
oReturnCheck = Fapi_setActiveFlashBank(Fapi_FlashBank0);
if(oReturnCheck != Fapi_Status_Success)
{
Example_Error(oReturnCheck);
}
Flash0EccRegs.ECC_ENABLE.bit.ENABLE = 0xA;
EDIS;
}
erase和programme操作如下:
#pragma CODE_SECTION(StoreData2Flash, ramFuncSection);
int StoreData2Flash(Uint16 *start, Uint32 size, Uint16 sector)
{
//1. declare the variables used for programming flash
Fapi_StatusType oReturnCheck = 0;// Status structure
Fapi_FlashStatusWordType oFlashStatusWord;
volatile Fapi_FlashStatusType oFlashStatus;
uint32 u32Index = 0;
uint16 i = 0;
Uint32 lTemp;
Uint32 lSectorAddr;
uint32 *Buffer32 = (uint32 *)start;
EALLOW;
DINT; // disable global interrupt
//2. check if the sector storing data record is full. if yes then erase the sector
switch(sector)
{
case SECTORK: lSectorAddr = Bzero_SectorK_start; break;
case SECTORL: lSectorAddr = Bzero_SectorL_start; break;
case SECTORM: lSectorAddr = Bzero_SectorM_start; break;
case SECTORN: lSectorAddr = Bzero_SectorN_start; break;
default:
EINT;
return 1;
}
lTemp = lSectorAddr;
// search for the most recent data ration record in flash
while(*(unsigned long*)lTemp != 0xFFFFFFFF) // the value at lTemp is not 0xFFFFFFFF which means the location at lTemp is not programmed with some value
{
lTemp += size;
}
if(lSectorAddr + 0x2000 - lTemp < size) // if the flash is full, erase the sector
{
// Erase Sector
//
oReturnCheck = Fapi_issueAsyncCommandWithAddress(Fapi_EraseSector,(uint32 *)lSectorAddr);
// Wait until FSM is done with erase sector operation
while(Fapi_checkFsmForReady() != Fapi_Status_FsmReady){}
// Verify that SectorL is erased. The Erase step itself does a
// verify as it goes. This verify is a 2nd verification that can be done.
oReturnCheck = Fapi_doBlankCheck((uint32 *)lSectorAddr,Bzero_16KSector_u32length,&oFlashStatusWord);
if(oReturnCheck != Fapi_Status_Success)
{
EINT;
return 1;
}
lTemp = lSectorAddr;
}
//3. program the data record to flash
for(i=0, u32Index = lTemp;
(u32Index < (lTemp + size)) &&
(oReturnCheck == Fapi_Status_Success); i+= 8, u32Index+= 8)
{
oReturnCheck = Fapi_issueProgrammingCommand((uint32 *)u32Index,start+i,
8,
0,
0,
Fapi_AutoEccGeneration);
while(Fapi_checkFsmForReady() == Fapi_Status_FsmBusy);
if(oReturnCheck != Fapi_Status_Success)
{
//
// Check Flash API documentation for possible errors
//
EINT;
return 1;
}
//
// Read FMSTAT register contents to know the status of FSM after
// program command for any debug
//
oFlashStatus = Fapi_getFsmStatus();
//
// Verify the values programmed. The Program step itself does a verify
// as it goes. This verify is a 2nd verification that can be done.
//
oReturnCheck = Fapi_doVerify((uint32 *)u32Index,
4, Buffer32+(i/2),
&oFlashStatusWord);
if(oReturnCheck != Fapi_Status_Success)
{
//
// Check Flash API documentation for possible errors
//
EINT;
return 1;
}
}
EINT;
return 0;
}
此帖出自小平头技术问答
一周热门 更多>