⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 doc_api.c

📁 在8051单片机应用系统中使用DiskOnChip_805
💻 C
📖 第 1 页 / 共 3 页
字号:
*           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 + -