📄 i28fxxxj3.c
字号:
return( stat );
}
#endif /* X_16 */
/****************************************************************************
*
* TMPL_ProgramProtection
*
* Description:
*
* This procedure is called to program the protection register on
* the flash device at the specified location with the specified data
* value. See the flash device datasheet for specific details on this
* command.
*
* Parameters:
*
* IN location - the protection register location on the flash
* device to be programmed.
*
* IN value - the data item to be programmed.
*
* IN returnSR - flag to indicate whether the device status register
* value should be returned by this function.
*
* Returns:
*
* TMPL_Status - includes function return status defined by enum
* TMPL_CommandStat and optionally the flash device
* status register value.
*
* Assumptions:
*
* NONE
*
***************************************************************************/
TMPL_Status TMPL_ProgramProtection ( UINT32 location,
TMPL_FDATA value,
UINT8 returnSR )
{
UINT32 baseaddr;
UINT32 address;
TMPL_Status stat;
if ( location > TMPL_OTP_NUMWORDS )
{
stat.Result = StatBadOtp;
return( stat );
}
baseaddr = TMPL_BASE_FLASH_ADDRESS;
address = TMPL_OTP_BASE + TMPL_BASE_FLASH_ADDRESS;
address += ( location * sizeof(TMPL_FDATA) );
if ( returnSR )
{
TMPL_ClearStatus();
}
TMPL_WriteF( baseaddr, TMPL_OTP_PROGRAM );
TMPL_WriteF( address, value );
if ( !TMPL_WaitUntilReady( TMPL_PROGRAM_TIMEOUT ) )
{
stat.Result = StatTimeout;
}
else
{
stat.Result = StatCompleted;
}
if ( returnSR )
{
stat.SR = TMPL_ReadStatus();
}
/* return device to read array mode */
TMPL_WriteF( TMPL_BASE_FLASH_ADDRESS, TMPL_READ_ARRAY );
return( stat );
}
/****************************************************************************
*
* TMPL_ProgramSuspend
*
* Description:
*
* This procedure is called to issue the program suspend command to
* the flash device. See the flash device datasheet for specific details
* on this command.
*
* Parameters:
*
* IN blocknum - the block number on the device.
*
* IN returnSR - flag to indicate whether the device status register
* value should be returned by this function.
*
* Returns:
*
* TMPL_Status - includes function return status defined by enum
* TMPL_CommandStat and optionally the flash device
* status register value.
*
* Assumptions:
*
* When this function is called the device is currently in the program
* mode for the block identified.
*
***************************************************************************/
TMPL_Status TMPL_ProgramSuspend ( UINT16 blocknum,
UINT8 returnSR )
{
TMPL_Status stat;
UINT32 blockaddr;
stat = TMPL_GetBlockAddress( blocknum, &blockaddr );
if ( stat.Result != StatCompleted )
{
return( stat );
}
TMPL_WriteF( blockaddr, TMPL_BLOCK_SUSPEND);
if ( !TMPL_WaitUntilReady( TMPL_PROGRAM_TIMEOUT ) )
{
stat.Result = StatTimeout;
}
else
{
stat.Result = StatCompleted;
}
if ( returnSR )
{
stat.SR = TMPL_ReadStatus();
}
/* return device to read array mode */
TMPL_WriteF( TMPL_BASE_FLASH_ADDRESS, TMPL_READ_ARRAY );
return( stat );
}
/****************************************************************************
*
* TMPL_ReadBlockStatus
*
* Description:
*
* This procedure is called to read the status for the specified block
* from the flash device. See the flash device datasheet for specific
* details on this command.
*
* Parameters:
*
* IN blocknum - the block number on the device.
*
* OUT blockstat- the status of the block as: unlocked, locked,
* or locked down.
*
* Returns:
*
* TMPL_Status - includes function return status defined by enum
* TMPL_CommandStat.
*
* Assumptions:
*
* NONE
*
***************************************************************************/
TMPL_Status TMPL_ReadBlockStatus ( UINT16 blocknum,
TMPL_FDATA *blockstat )
{
UINT32 stataddress;
TMPL_Status stat;
UINT32 blockaddr;
stat = TMPL_GetBlockAddress( blocknum, &blockaddr );
if ( stat.Result != StatCompleted )
{
return( stat );
}
#if X_8
stataddress = ( blockaddr + 4 );
#endif
#if PX_16
stataddress = ( blockaddr + ( 2 * sizeof( TMPL_FDATA ) ) );
#endif
#if X_16
stataddress = ( blockaddr + ( 2 * sizeof( TMPL_FDATA ) ) );
#endif
#if X_32
stataddress = ( blockaddr + ( 2 * sizeof( TMPL_FDATA ) ) );
#endif
#if ILX_32
stataddress = ( blockaddr + ( 4 * sizeof( TMPL_FDATA ) ) );
#endif
TMPL_WriteF( TMPL_BASE_FLASH_ADDRESS, TMPL_READ_ID_CODES );
TMPL_ReadF( stataddress, blockstat );
/* return device to read array mode */
TMPL_WriteF( TMPL_BASE_FLASH_ADDRESS, TMPL_READ_ARRAY );
stat.Result = StatCompleted;
return( stat );
}
#if X_16
/****************************************************************************
*
* TMPL_ReadProtection
*
* Description:
*
* This procedure is called to read the protection register value on
* the flash device from the specified location. See the flash device
* datasheet for specific details on this command.
*
* Parameters:
*
* IN location - the protection register location on the flash
* device to be read from.
*
* OUT value - the data item read from the register.
*
* Returns:
*
* TMPL_Status - includes function return status defined by enum
* TMPL_CommandStat.
*
* Assumptions:
*
* NONE
*
***************************************************************************/
TMPL_Status TMPL_ReadProtection ( UINT32 location,
TMPL_FDATA_PTR value )
{
UINT32 baseaddr;
UINT32 address;
TMPL_Status stat;
if ( location > TMPL_OTP_NUMWORDS )
{
stat.Result = StatBadOtp;
return( stat );
}
baseaddr = TMPL_BASE_FLASH_ADDRESS;
address = TMPL_OTP_BASE + TMPL_BASE_FLASH_ADDRESS;
address += ( location * sizeof(TMPL_FDATA) );
TMPL_WriteF( baseaddr, TMPL_OTP_READ );
TMPL_ReadF( address, value );
/* return device to read array mode */
TMPL_WriteF( TMPL_BASE_FLASH_ADDRESS, TMPL_READ_ARRAY );
stat.Result = StatCompleted;
return( stat );
}
#endif /* X_16 */
/****************************************************************************
*
* TMPL_Resume
*
* Description:
*
* This procedure is called to issue the resume command to the flash
* device for the specified block. See the flash device datasheet for
* specific details on this command.
*
* Parameters:
*
* IN blocknum - the block number to resume.
*
* IN returnSR - flag to indicate whether the device status register
* value should be returned by this function.
*
* Returns:
*
* TMPL_Status - includes function return status defined by enum
* TMPL_CommandStat and optionally the flash device
* status register value.
*
* Assumptions:
*
* The block indicated was previously program suspended or erase
* suspended.
*
***************************************************************************/
TMPL_Status TMPL_Resume ( UINT16 blocknum,
UINT8 returnSR )
{
TMPL_Status stat;
UINT32 blockaddr;
// TMPL_FDATA status;
stat = TMPL_GetBlockAddress( blocknum, &blockaddr );
if ( stat.Result != StatCompleted )
{
return( stat );
}
if ( returnSR )
{
TMPL_ClearStatus();
}
TMPL_WriteF( blockaddr, TMPL_BLOCK_RESUME );
if ( !TMPL_WaitUntilReady( TMPL_PROGRAM_TIMEOUT ) )
{
stat.Result = StatTimeout;
}
else
{
stat.Result = StatCompleted;
}
if ( returnSR )
{
stat.SR = TMPL_ReadStatus();
}
return( stat );
}
/****************************************************************************
*
* TMPL_UnlockAllBlocks
*
* Description:
*
* This procedure is called to unlock all blocks on the flash device.
* See the flash device datasheet for specific details on this command.
*
* Parameters:
*
* IN returnSR - flag to indicate whether the device status register
* value should be returned by this function.
*
* Returns:
*
* TMPL_Status - includes function return status defined by enum
* TMPL_CommandStat and optionally the flash device
* status register value.
*
* Assumptions:
*
* The blocks were previously locked.
*
***************************************************************************/
TMPL_Status TMPL_UnlockAllBlocks ( UINT8 returnSR )
{
TMPL_Status stat;
if ( returnSR )
{
TMPL_ClearStatus();
}
TMPL_WriteF( TMPL_BASE_FLASH_ADDRESS, TMPL_CONFIG_SETUP );
TMPL_WriteF( TMPL_BASE_FLASH_ADDRESS, TMPL_LOCK_BIT_CLEAR );
if ( !TMPL_WaitUntilReady( TMPL_PROGRAM_TIMEOUT ) )
{
stat.Result = StatTimeout;
}
else
{
stat.Result = StatCompleted;
}
if ( returnSR )
{
stat.SR = TMPL_ReadStatus();
}
/* return device to read array mode */
TMPL_WriteF(TMPL_BASE_FLASH_ADDRESS, TMPL_READ_ARRAY );
return( stat );
}
/*******************************************************************************
Function: ReturnType TMPL_FlashEraseArray( uBlockType ublBlockNr )
Arguments: ublBlockNr is the number of the Block to be erased.
Return Value:
Flash_Success
Flash_BlockNrInvalid
Flash_OperationTimeOut
Flash_BlockProtected
Flash_BlockEraseFailed
Description:
This function can be used to erase the Block specified in ublBlockNr.
The function checks that the block nr is within the valid range before
issuing the erase command. Once the erase has completed the function
checks the Status Register for errors. Any errors are returned,
otherwise Flash_Success is returned.
Pseudo Code:
Step 1: Check that the block number exists
Step 2: Check the protection of the block
Step 3: Issue Erase Command
Step 4: Wait until Program/Erase Controller starts
Step 5: Wait until Program/Erase Controller completes
*******************************************************************************/
ReturnType TMPL_FlashEraseArray( UINT32 udAddrOff, UINT32 udNrOfElementsInArray) {
ReturnType rRetVal = Flash_Success; /* Return Value: Initially optimistic */
// rProtStatus; /* Protection Status of a block */
// UINT32 udCurBlockOffset; /* Holds Blockoffset[ublBlockNr] */
UINT32 udLastOff; /* Holds the last offset to be programmed */
UINT32 ublFirstBlock, /* The block where start to program */
ublLastBlock, /* The last block to be programmed */
ublCurBlock; /* Current block */
UINT8 returnSR;
UINT32 blAddress; /* Block Address */
/* Step 2: Check if the erase is within the Flash memory space */
if ( (udAddrOff >= TMPL_TOTAL_SIZE) || (udNrOfElementsInArray > (TMPL_TOTAL_SIZE - udAddrOff)) ) {
return Flash_AddressInvalid;
} /* EndIf */
udLastOff = udAddrOff + udNrOfElementsInArray - 1;
/* Step 3: Determine first and last block to program */
for (ublFirstBlock=0; ublFirstBlock < TMPL_TOTAL_NUMBLOCKS-1;ublFirstBlock++)
{
TMPL_GetBlockAddress ( ublFirstBlock+1, &blAddress );
if (udAddrOff < blAddress)
break;
}
for (ublLastBlock=ublFirstBlock; ublLastBlock < TMPL_TOTAL_NUMBLOCKS-1;ublLastBlock++)
{
TMPL_GetBlockAddress ( ublLastBlock+1, &blAddress );
if (udLastOff < blAddress)
break;
}
#if 0
/* Step 4: Check protection status for the blocks to be erased */
for (ublCurBlock = ublFirstBlock; ublCurBlock <= ublLastBlock; ublCurBlock++) {
if ( (rProtStatus = FlashCheckBlockProtection(ublCurBlock)) != Flash_BlockUnprotected ) {
rRetVal = Flash_BlockProtected;
if (ublCurBlock == ublFirstBlock) {
eiErrorInfo.udGeneralInfo[0] = udAddrOff;
return rRetVal;
} else {
eiErrorInfo.udGeneralInfo[0] = BlockOffset[ublCurBlock];
udLastOff = BlockOffset[ublCurBlock]-1;
} /* EndIf (ublCurBlock == ublFirstBlock) */
} /* EndIf rProtStatus */
} /* Next ublCurBlock */
#endif
for (ublCurBlock = ublFirstBlock; ublCurBlock <= ublLastBlock; ublCurBlock++) {
TMPL_EraseBlock ( ublCurBlock,returnSR );
}
return rRetVal;
} /* EndFunction FlashBlockErase */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -