📄 device.c
字号:
/*************************************************************************
*** Intel Confidential
*** Copyright (C) Intel Corporation 2000
*** All Rights Reserved.
*** --------------------------------------------------------------------
***
*** Project Name: Flash Templates
***
*** Module Name: Device
***
*** File: Device.C - J3 device specific template source file
***
*** File Revision: $Revision: 6 $, $JustDate: 5/11/00 $, $Author: Kalowry $
***
*** Purpose:
***
*** The purpose of this file is to implement the J3 device specific
*** functionality for the J3 template package. This module
*** implements only those functions which have J3 device specific
*** implementations. The remaining functions which complete
*** the template application programmers interface are implemented
*** by the template.c source module.
***
*** $NoKeywords: $
*************************************************************************/
#include "template.h"
/****************************************************************************
*
* TMPL_EraseAllBlocks
*
* Description:
*
* This procedure is called to erase all data blocks on the flash
* device. See the flash device datasheet for specific details on
* the block erase 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:
*
* NONE
*
***************************************************************************/
TMPL_Status TMPL_EraseAllBlocks ( UINT8 returnSR )
{
TMPL_Status stat;
UINT16 block;
for ( block=0; block < TMPL_TOTAL_NUMBLOCKS; block++ )
{
stat = TMPL_EraseBlock( block, returnSR );
if ( stat.Result != StatCompleted )
{
return( stat );
}
}
return( stat );
}
/****************************************************************************
*
* TMPL_EraseSuspend
*
* Description:
*
* This procedure is called to issue the erase 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 erase
* mode for the block identified.
*
***************************************************************************/
TMPL_Status TMPL_EraseSuspend ( 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_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 );
}
#if X_16
/****************************************************************************
*
* TMPL_ExtendedQuery
*
* Description:
*
* This procedure is called to retrieve the extended query
* data from the flash device. See the flash device datasheet for
* specific details on this command.
*
* Parameters:
*
* OUT *extquery - pointer to extended query structure
*
* Returns:
*
* TMPL_Status - includes function return status defined by enum
* TMPL_CommandStat
*
* Assumptions:
*
* NONE
***************************************************************************/
TMPL_Status TMPL_ExtendedQuery ( struct TMPL_ExtQueryData *extquery )
{
TMPL_Status stat;
struct TMPL_QueryData query;
TMPL_FDATA item;
UINT32 addr;
UINT32 longitem;
UINT32 i;
UINT32 offset;
stat = TMPL_Query( &query );
if ( stat.Result != StatCompleted )
{
return( stat );
}
offset = query.ExtTablePtr;
TMPL_WriteF( TMPL_BASE_FLASH_ADDRESS, TMPL_READ_QUERY );
/* read extended query string */
for ( i=0; i < 3; i++ )
{
stat = TMPL_GetQueryAddress( offset, &addr );
if ( stat.Result != StatCompleted )
{
return( stat );
}
TMPL_ReadF( addr, &item );
item &= 0xff;
extquery->ExtQueryStr[i] = (char)item;
offset++;
}
extquery->ExtQueryStr[3] = '\0'; /* null terminate string */
/* read major version number */
stat = TMPL_GetQueryAddress( offset, &addr );
if ( stat.Result != StatCompleted )
{
return( stat );
}
TMPL_ReadF( addr, &item );
item &= 0xff;
extquery->MajorVersionNum = (UINT8)item;
offset++;
/* read minor version number */
stat = TMPL_GetQueryAddress( offset, &addr );
if ( stat.Result != StatCompleted )
{
return( stat );
}
TMPL_ReadF( addr, &item );
item &= 0xff;
extquery->MinorVersionNum = (UINT8)item;
offset++;
extquery->OptionalFeature = 0;
for ( i=0; i < 4; i++ )
{
stat = TMPL_GetQueryAddress( offset, &addr );
if ( stat.Result != StatCompleted )
{
return( stat );
}
TMPL_ReadF( addr, &item );
item &= 0xff;
longitem = (UINT32)((UINT32)item << (8*i));
extquery->OptionalFeature = (UINT32)( extquery->OptionalFeature | longitem );
offset++;
}
/* read after suspend functions */
stat = TMPL_GetQueryAddress( offset, &addr );
if ( stat.Result != StatCompleted )
{
return( stat );
}
TMPL_ReadF( addr, &item );
item &= 0xff;
extquery->AfterSuspendFunctions = (UINT8)item;
offset++;
/* read vendor id */
extquery->BlockLockStatus = 0;
for ( i=0; i < 2; i++ )
{
stat = TMPL_GetQueryAddress( offset, &addr );
if ( stat.Result != StatCompleted )
{
return( stat );
}
TMPL_ReadF( addr, &item );
item &= 0xff;
item = item << (8*i);
extquery->BlockLockStatus = (UINT16)( extquery->BlockLockStatus | item );
offset++;
}
/* read vcc optimum */
stat = TMPL_GetQueryAddress( offset, &addr );
if ( stat.Result != StatCompleted )
{
return( stat );
}
TMPL_ReadF( addr, &item );
item &= 0xff;
extquery->VccOptimum = (UINT8)item;
offset++;
/* read vpp optimum */
stat = TMPL_GetQueryAddress( offset, &addr );
if ( stat.Result != StatCompleted )
{
return( stat );
}
TMPL_ReadF( addr, &item );
item &= 0xff;
extquery->VppOptimum = (UINT8)item;
offset++;
stat.Result = StatCompleted;
/* return device to read array mode */
TMPL_WriteF( TMPL_BASE_FLASH_ADDRESS, TMPL_READ_ARRAY );
return( stat );
}
#endif /* X_16 */
/****************************************************************************
*
* TMPL_GetBlockAddress
*
* Description:
*
* This procedure is called to get the flash starting address for the
* specified block number.
*
* Parameters:
*
* IN blocknum - the block number on the device.
*
* OUT address - the starting flash address for the specified
* block.
*
* Returns:
*
* TMPL_Status - includes function return status defined by enum
* TMPL_CommandStat.
*
* Assumptions:
*
* NONE
*
***************************************************************************/
TMPL_Status TMPL_GetBlockAddress ( UINT16 blocknum,
UINT32 *address )
{
TMPL_Status stat;
if ( blocknum < TMPL_TOTAL_NUMBLOCKS )
{
*address = TMPL_BASE_FLASH_ADDRESS + ( blocknum * TMPL_BLOCK_NUMBYTES );
}
else
{
stat.Result = StatBadBlock;
return( stat );
}
stat.Result = StatCompleted;
return( stat );
}
/****************************************************************************
*
* TMPL_LockBlock
*
* Description:
*
* This procedure is called to lock the specified block on 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:
*
* NONE
*
***************************************************************************/
TMPL_Status TMPL_LockBlock ( UINT16 blocknum,
UINT8 returnSR )
{
TMPL_Status stat;
UINT32 blockaddr;
stat = TMPL_GetBlockAddress( blocknum, &blockaddr );
if ( stat.Result != StatCompleted )
{
return( stat );
}
if ( returnSR )
{
TMPL_ClearStatus();
}
TMPL_WriteF( blockaddr, TMPL_CONFIG_SETUP );
TMPL_WriteF( blockaddr, TMPL_LOCK_BIT_SET );
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_LockProtection
*
* Description:
*
* This procedure is called to program the protection register user lock
* bit 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
* command_stat and optionally the flash device status
* register value.
*
* Assumptions:
*
* NONE
*
***************************************************************************/
TMPL_Status TMPL_LockProtection ( UINT8 returnSR )
{
UINT32 baseadd;
UINT32 location;
UINT32 address;
TMPL_Status stat;
location = TMPL_PROTECTION_LOCK_LOCATION;
baseadd = TMPL_BASE_FLASH_ADDRESS;
address = TMPL_OTP_BASE + TMPL_BASE_FLASH_ADDRESS;
address += ( location * sizeof(TMPL_FDATA) );
if ( returnSR )
{
TMPL_ClearStatus();
}
TMPL_WriteF(baseadd, TMPL_OTP_PROGRAM );
TMPL_WriteF(address, TMPL_OTP_LOCK);
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_PageMode
*
* Description:
*
* This procedure is called to enable or disable page mode read to
* the device.
*
* Parameters:
*
* IN enable - flag "1" to indicate the page mode is anable and
* flag "0" to indicate the page mode is disable to
* the device.
*
* 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_PageMode ( UINT16 enable )
{
TMPL_Status stat;
UINT32 address; /* contains the address to load the
Read Configuration Register with */
if ( enable )
{
address = TMPL_PAGE_READ_MODE;
}
else
{
address = TMPL_STD_READ_MODE;
}
#if ( X_8 || X_16 )
address = address << 1; /* shift so address is on A16..A1 */
#else
address = address << 2; /* shift so address is on A16..A1 */
#endif
address += TMPL_BASE_FLASH_ADDRESS; /* offset by base flash mem */
/* write Read Configuration command */
TMPL_WriteF( TMPL_BASE_FLASH_ADDRESS, TMPL_CONFIG_SETUP );
TMPL_WriteF( address, TMPL_SET_READ_CONFIG );
stat.Result = StatCompleted;
return( stat );
}
#if X_16
/****************************************************************************
*
* TMPL_ProgramFlashBuffered
*
* Description:
*
* This procedure is called to program the flash device at the specified
* starting address contiguously with the specified buffer data. See
* the flash device datasheet for specific details on the write to buffer
* command.
*
* Parameters:
*
* IN address - the flash address to be programmed.
*
* IN buffer - the buffer containing data to be programmed.
*
* IN numbytes - the number of bytes of data contained in the buffer.
*
* 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_ProgramFlashBuffered ( UINT32 address,
UINT8_PTR buffer,
UINT32 numbytes,
UINT8 returnSR )
{
TMPL_Status stat;
TMPL_FDATA_PTR fptr;
TMPL_FDATA writedata;
UINT16 numitems;
UINT32 cmndaddress;
UINT32 numwritebytes;
UINT32 byteswritten;
cmndaddress = address;
if ( ( address + numbytes ) > TMPL_TOTAL_SIZE )
{
stat.Result = StatBadAddress;
return( stat );
}
if ( cmndaddress & 0x01 )
{
cmndaddress --;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -