?? doc_api.c
字號:
* flDataError — 數據錯誤
* 說 明:
*/
FLStatus DOC_ReadOnePage( dword pageNum, byte *buf, word len, byte modes )
{
dword address; /* The offset address of this page on DOC */
word lenRead, firstPage;
FLStatus status = flOK;
#ifdef EDC_MODE
byte syndrom[SYNDROM_BYTES];
byte synTmp;
word i;
#endif /* EDC_MODE */
if ( pageNum >= docInfo.noOfPages )
{
return( flOutOfMedia ); /* out of media */
}
address = pageNum << PAGE_BITS;
if( len > 0 )
{
mapWin(address); /* select flash device */
command(AREA_A); /* read from the first page */
setAddress(address);
if( waitForReady() != flOK )
{
return( flDOCNotReady );
}
lenRead = MIN(len,PAGE_SIZE);
modes = (lenRead != PAGE_SIZE) ? (modes & ~EDC_FLAG) : modes;
#ifdef EDC_MODE
if( modes & EDC_FLAG )
{
eccONread(); /* ECC ON Read */
}
#endif /* EDC_MODE */
if( !(docInfo.flags & BIG_PAGE) ) /* 2M - read to two pages separately */
{
firstPage = MIN(lenRead,CHIP_PAGE_SIZE);
readFlash(buf, firstPage); /* 1st page */
if( lenRead > CHIP_PAGE_SIZE ) /* 2nd page */
{
command(AREA_A);
setAddress(address + firstPage);
if( waitForReady() != flOK )
{
return(flDOCNotReady);
}
readFlash(buf + firstPage, (lenRead - firstPage));
}
}
else /* 512 byte page */
{
readFlash(buf, lenRead);
}
#ifdef EDC_MODE
if( modes & EDC_FLAG )
{
/* read syndrom to let it through the ECC unit */
readSyndrom((byte *)syndromEDC, SYNDROM_BYTES);
if( eccError() ) /* try to fix ECC error */
{
for(i=0;( i < SYNDROM_BYTES );i++)
{
if( syndromEDC[i] != 0xFF )
{
break;
}
}
if( i != SYNDROM_BYTES )
{
for(i=0;( i < SYNDROM_BYTES );i++) /* read syndrom regs */
{
syndrom[i] = docRead8bitReg(Nsyndrom+i);
}
SWAP(synTmp,syndrom[0],syndrom[4]); /* Swap 1 and 3 words */
SWAP(synTmp,syndrom[1],syndrom[5]);
if( flCheckAndFixEDC( (char *)buf, (char *)syndrom, 1) != NO_EDC_ERROR )
{
status = flDataError;
}
}
}
}
#endif /* EDC_MODE */
}
writeSignals(FLASH_IO | WP);
return( status );
} /* DOC_ReadOnePage */
/*-------------------------------------------------------------------
* readFlash - Low-level flash read.
*-------------------------------------------------------------------*/
static void readFlash( byte *buf, word len )
{
if( docInfo.flags & MDOC_ASIC ) /* load first data into pipeline */
{
docRead8bitReg(NReadPipeInit);
len--;
}
if( len>MDOC_ALIAS_RANGE )
{
memcpy(buf,((volatile byte *)docWin+Nio),MDOC_ALIAS_RANGE);
memcpy(buf+MDOC_ALIAS_RANGE,((volatile byte *)docWin+Nio),len-MDOC_ALIAS_RANGE);
}
else
{
memcpy(buf,((volatile byte *)docWin+Nio),len);
}
buf+=len;
if( docInfo.flags & MDOC_ASIC ) /* read last data from pipeline */
{
*buf = docRead8bitReg(NLastDataRead);
}
}
#ifdef ACCESS_DOC_EXTRA
/*-------------------------------------------------------------------
* readDocExtra - Read 'len' bytes from first page of block to
* 'sbuffer' to get unit signature. returns OK
* on success, error code otherwise.
*-------------------------------------------------------------------*/
FLStatus readDocExtra( dword blockNo, byte *sbuffer, word len )
{
word offset;
word tailSize;
dword address;
if( blockNo >= docInfo.noOfBlocks )
{
return( flOutOfMedia ); /* out of media */
}
len = MIN( len, SIGNATURE_LEN );
address = ( blockNo << docInfo.erasableBlockBits ) + DOC_SIGN_OFFSET;
offset = address & ((EXTRA_LEN * 2) - 1);
if( !(docInfo.flags & BIG_PAGE) ) /* 2M - read extra of second page */
{
if( offset < EXTRA_LEN ) /* First half of extra area */
{
address += CHIP_PAGE_SIZE; /* ... assigned to 2nd page */
}
else /* Second half of extra area */
{
address -= EXTRA_LEN; /* ... assigned to 1st page */
offset -= EXTRA_LEN;
}
tailSize = EXTRA_LEN;
}
else
{
tailSize = EXTRA_LEN * 2; /* 16 byte Extra Area Size */
}
mapWin(address); /* select flash device */
command(AREA_C);
setAddress(address);
if( waitForReady() != flOK )
{
return( flDOCNotReady );
}
readFlash(sbuffer,len);
writeSignals(FLASH_IO | WP);
if( (offset + len) == tailSize ) /* Serial Read Cycle Entry */
{
waitForReady();
}
return( flOK );
}
#endif /* ACCESS_DOC_EXTRA */
#ifdef EDC_MODE
/*-------------------------------------------------------------------
* readSyndrom - Low-level ECC syndrom read with checksum.
*-------------------------------------------------------------------*/
static void readSyndrom( byte *buf, word len )
{
word i;
if( docInfo.flags & MDOC_ASIC ) /* load first data into pipeline */
{
docRead8bitReg(NReadPipeInit);
len--;
}
for(i=0;( i < len );i++)
{
buf[i] = docRead8bitReg(((Nio)+(i & 0x01)));
}
if( docInfo.flags & MDOC_ASIC ) /* read last data from pipeline */
{
buf[len] = docRead8bitReg(NLastDataRead);
}
}
/*-------------------------------------------------------------------
* Purpose : enable ECC in read mode and reset it.
*-------------------------------------------------------------------*/
static void eccONread( void )
{
docWrite8bitReg(NECCconfig,ECC_RESET); /* reset ECC */
docWrite8bitReg(NECCconfig,ECC_EN); /* enable ECC in read mode */
}
/*-------------------------------------------------------------------
* Purpose : disable ECC.
*-------------------------------------------------------------------*/
void eccOFF( void )
{
docWrite8bitReg(NECCconfig,ECC_RESERVED);
}
/*-------------------------------------------------------------------
* Purpose : check for EDC error
*-------------------------------------------------------------------*/
static byte eccError( void )
{
int i;
byte ret;
for(i = 0;( i < 2 ); i++)
{
docRead8bitReg(ECCstatus);
}
ret = docRead8bitReg(ECCstatus);
return( (ret & ECC_ERROR) );
}
#endif /* EDC_MODE */
#ifdef EDC_MODE
/*-------------------------------------------------------------------
* Purpose : enable ECC in write mode and reset it.
*-------------------------------------------------------------------*/
static void eccONwrite( void )
{
docWrite8bitReg(NECCconfig,ECC_RESET); /* reset ECC */
docWrite8bitReg(NECCconfig ,(ECC_RW | ECC_EN)); /* enable ECC in write mode */
}
#endif /* EDC_MODE */
/*-------------------------------------------------------------------
* readStatus - Read the status of the selected flash device
*-------------------------------------------------------------------*/
static byte readStatus( void )
{
byte chipStatus;
command(READ_STATUS);
docRead8bitReg(NslowIO); /* read CDSN_slow_IO ignoring the data */
chipStatus = docRead8bitReg(Nio); /* finally read flash status */
writeSignals(FLASH_IO | WP);
return( chipStatus );
}
/*-------------------------------------------------------------------
* writeFlash - Low-level flash write.
*-------------------------------------------------------------------*/
static void writeFlash( byte *buf, word len )
{
if(len>MDOC_ALIAS_RANGE)
{
memcpy(((volatile byte *)docWin+Nio),buf,MDOC_ALIAS_RANGE);
memcpy(((volatile byte *)docWin+Nio),buf+MDOC_ALIAS_RANGE,len-MDOC_ALIAS_RANGE);
}
else
{
memcpy(((volatile byte *)docWin+Nio),buf,len);
}
if( docInfo.flags & MDOC_ASIC ) /* push last data through pipeline */
{
docWrite8bitReg(NWritePipeTerm,0);
}
}
/*----------------------------------------------------------------------*/
/* w r i t e C o m m a n d */
/* Issue write command. */
/* */
/* Parametes: */
/* cmd : Command to issue (according to area). */
/* addr : address to write to. */
/*----------------------------------------------------------------------*/
static void writeCommand( enum PointerOp cmd, dword addr )
{
if( docInfo.flags & FULL_PAGE )
{
command(RESET_FLASH); /* Clear page buffer */
waitForReady();
}
command((byte)cmd); /* move flash pointer to respective area of the page */
command(SERIAL_DATA_INPUT); /* start data loading for write */
setAddress(addr);
waitForReady();
}
/*----------------------------------------------------------------------*/
/* w r i t e E x e c u t e */
/* Execute write. */
/* */
/* Returns: */
/* FLStatus : 0 on success, otherwise failed. */
/*----------------------------------------------------------------------*/
static FLStatus writeExecute( void )
{
command(SETUP_WRITE); /* execute page program */
if( waitForReady() != flOK )
{
return( flDOCNotReady );
}
if( readStatus() & FAIL )
{
return( flWriteFault );
}
return( flOK );
}
/*
* 函數名稱: DOC_WriteOnePage
* 功能描述: 寫 DOC 的一頁
* 輸入參數: pageNum: 頁號(有效范圍:[0, docInfo.noOfPages-1])
* buf: 存放將寫入 DOC 的數據的緩沖區的首地址
* len: 數據長度(應不大于頁長度即 512 字節,若大于 512 則只有前 512 個字節被寫入 DOC)
* modes: 模式標志,當未定義 EDC_MODE 時可取 0;當定義 EDC_MODE 時,可取 EDC_FLAG 或 0
* 輸出參數: 無
* 返 回 值: flOK — 成功,無錯誤
* flOutOfMedia — 頁號超出 DOC 容量范圍
* flWriteFault — 寫 DOC 出錯
* 說 明:
*/
FLStatus DOC_WriteOnePage( dword pageNum, byte *buf, word len, byte modes )
{
dword address;
word lenWrite, toFirstPage, toSecondPage;
#ifdef DOC_VERIFY_WRITE
byte readback[PAGE_SIZE];
FLStatus status;
#endif /* DOC_VERIFY_WRITE */
#ifdef EDC_MODE
int i;
byte syndrom[SYNDROM_BYTES+2];
syndrom[SYNDROM_BYTES] = syndrom[SYNDROM_BYTES+1] = 0x55;
#endif /* EDC_MODE */
if ( pageNum >= docInfo.noOfPages )
{
return( flOutOfMedia ); /* out of media */
}
address = pageNum << PAGE_BITS;
if( len > 0 )
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -