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

📄 intel-j3.cpp

📁 Ep93XX TionProV2 BSP
💻 CPP
📖 第 1 页 / 共 3 页
字号:
        }
    }

    SetWriteProtect(TRUE);
    SetKMode(bLastMode);
    return(TRUE);
}


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function:       FMD_WriteSector()

Description:    Writes the requested sector data and/or sector metadata to the
                Flash media.

Notes:          Notice that although each byte of a NOR Flash block is individually
                addressable, the media is still logically broken up into sectors.
                Thus, for each sector request, we must determine where to put the
                data in each respective Flash block (see note above).

                By default, the NOR Flash is configured in READ ARRAY MODE we need
                to set some control lines to prepare for the WRITE operation.

Returns:        Boolean indicating success.
------------------------------------------------------------------------------*/
BOOL  FMD_WriteSector(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors)
{
    volatile SECTOR_ADDR physicalSectorAddr = 0;
    BLOCK_ID blockID = 0;
    DWORD i = 0;
    DWORD j = 0;
    DWORD k = 0;
    volatile ULONG ulBlockAddress = 0;
    BOOL bLastMode;
    DWORD dwBusWidth = g_bPairedFlash ? sizeof(ULONG) : sizeof(USHORT);
    LPBYTE pBuffer = pSectorBuff;
    USHORT usBufferSize = g_bPairedFlash ? (1 << g_FMDInfo.Geometry.WriteBuffSize) * 2 : (1 << g_FMDInfo.Geometry.WriteBuffSize);
    BOOL fRet = FALSE;

#ifdef READ_FROM_REGISTRY    
    BYTE cTempBuffer[SECTOR_SIZE];
#endif

    //----- 1. Check the input parameters -----
    //         NOTE: The FAL insures that the starting sector address is in the allowable range.
    if((dwNumSectors == 0) || ((pSectorBuff == NULL) && (pSectorInfoBuff == NULL)))
    {
        return(FALSE);
    }

    //----- 2. Process the write request(s)... -----

    bLastMode = SetKMode(TRUE);
    
    SetWriteProtect(FALSE);
    for(i = startSectorAddr; i < (startSectorAddr + dwNumSectors); i++)
    {
        //----- Determine the block this physical sector resides in -----
        blockID = (i / g_FMDInfo.SectorsPerBlock);

        //----- Compute the physical address for the requested sector -----
        // Note we do this differently based on whether the caller wants us to write the sector information structure as well.  Since we're
        // dealing with a NOR flash which is XIP-able, one might want to use this function to write an XIP region (i.e., no sector information
        // structures in flash).
        //
        if (!g_bXIPMode)
            physicalSectorAddr = g_FMDInfo.BaseAddress + i*(SECTOR_SIZE + sizeof(SectorInfo)) + (blockID * g_FMDInfo.UnusedBytesPerBlock);
        else
            physicalSectorAddr = g_FMDInfo.BaseAddress + (i*SECTOR_SIZE);

        // Compute the block address for this sector write.
        //
        ulBlockAddress = (ULONG)(physicalSectorAddr - (physicalSectorAddr % g_FMDInfo.BlockSize));
        
        //----- Write the necessary sector data -----       
        if(pSectorBuff)
        {
            
#ifdef READ_FROM_REGISTRY                
            // Check for unaligned pointer.  Only need to do this if the FMD is being used as 
            // a block driver (and therefore READ_FROM_REGISTRY is set)
            if ((DWORD)pSectorBuff & (dwBusWidth - 1))
            {
                RETAILMSG(1, (TEXT("FMD_WriteSector: Unaligned pointer - using internal buffer\r\n")));
                memcpy(cTempBuffer, pSectorBuff, SECTOR_SIZE);
                pBuffer = cTempBuffer;
            }
            else
#endif
            {
                pBuffer = pSectorBuff;
            }


            // from Intel: if the sector is not aligned modulo write buffer size - 
            // performance can be enhanced my doing a partial write to align the buffer 
            // for the rest of the writes to complete the sector.  If you are doing 
            // partial writes to align the buffer address, and a single word 
            // (or DWORD of x32) is being written, it is faster to use the flash's WORD  
            // write command instead of the buffered write.
            USHORT BytesNeededToAlign = usBufferSize - (USHORT)(physicalSectorAddr % (ULONG)usBufferSize);

            if (!DoBufferedWrite(ulBlockAddress, physicalSectorAddr, pBuffer, BytesNeededToAlign))
            {
                goto exit;
            }

            for (j = BytesNeededToAlign, k = 0 ; (j < SECTOR_SIZE) ; j += k)
            {
                // Since we're using the StrataFlash write buffer to accelerate the write operation, we need to determine how many words should be written
                // to the part(s).  The value will be the minimum of either the sector bytes remaining or the write buffer size itself (including the number
                // of flash parts/banks in the design).  Since we write the data in longword form, we assume 2 banks in our design.
                //
                USHORT NumWriteBytes = MIN((USHORT)(SECTOR_SIZE - j), usBufferSize);
                k = DoBufferedWrite(ulBlockAddress, physicalSectorAddr + j, pBuffer + j, NumWriteBytes);
                if (!k)
                    goto exit;
            }

            pSectorBuff += SECTOR_SIZE;
#if 0            
            for (j = 0, k = 0 ; (j < SECTOR_SIZE) ; j += k)
            {
                // Since we're using the StrataFlash write buffer to accelerate the write operation, we need to determine how many words should be written
                // to the part(s).  The value will be the minimum of either the sector bytes remaining or the write buffer size itself (including the number
                // of flash parts/banks in the design).  Since we write the data in longword form, we assume 2 banks in our design.
                //
                USHORT NumWriteBytes = MIN((USHORT)(SECTOR_SIZE - j), (USHORT)((1 << g_FMDInfo.Geometry.WriteBuffSize) * 2));

                // Select write buffer mode and wait for the buffer to become available.
                //
                WRITE_COMMAND(ulBlockAddress, BUFFER_WRITE_CMD);
                while (!CHECK_STATUS(ulBlockAddress, STATUS_READY_MASK))
                {
                    // Do nothing - read status command not required...
                }

                // TODO - write data in this section for non-paired design.

                // Let the flash know the size of the buffer we plan to send (note that this is a 0-based word count and we simulatenously
                // write it to both flash parts (upper and lower).
                //
                WRITE_FLASH(ulBlockAddress, ((((NumWriteBytes / sizeof(USHORT)) / 2) - 1) << 16 | (((NumWriteBytes / sizeof(USHORT)) / 2) - 1)));

                for(k = 0 ; k < NumWriteBytes ; k += sizeof(ULONG))
                {
                    WRITE_FLASH((physicalSectorAddr + j + k), *((PDWORD)(pSectorBuff + j + k)));
                }

                // Now program the buffer into flash...
                //
                WRITE_COMMAND(ulBlockAddress, BLOCK_PROCEED_CMD);
                do
                {
                    WRITE_COMMAND(ulBlockAddress, READ_STATUS_CMD);
                }
                while (!CHECK_STATUS(ulBlockAddress, STATUS_READY_MASK));
            }

            pSectorBuff += SECTOR_SIZE;
#endif
        }

        //----- Write the necessary sector info data (metadata) -----
        //      NOTE: Notice that the 6 metadata bytes are just stored after the sector data...
        if(!g_bXIPMode && pSectorInfoBuff)
        {
            if (!DoBufferedWrite(ulBlockAddress, physicalSectorAddr+SECTOR_SIZE, (PUCHAR)pSectorInfoBuff, sizeof(SectorInfo)))
                goto exit;
            pSectorInfoBuff += sizeof(SectorInfo);
            
#if 0            
            //----- Write the first reserved field -----
            WRITE_COMMAND(physicalSectorAddr+SECTOR_SIZE, BYTEWORD_PROGRAM_CMD);
            WRITE_FLASH(physicalSectorAddr+SECTOR_SIZE, pSectorInfoBuff->dwReserved1);
            do
            {
                WRITE_COMMAND(physicalSectorAddr+SECTOR_SIZE, READ_STATUS_CMD);
            }
            while (!CHECK_STATUS(physicalSectorAddr+SECTOR_SIZE, STATUS_READY_MASK));

            //----- Write the OEM reserved byte, bad block byte, and second reserved field -----
            WRITE_COMMAND(physicalSectorAddr+SECTOR_SIZE, BYTEWORD_PROGRAM_CMD);
            WRITE_FLASH(physicalSectorAddr+SECTOR_SIZE+sizeof(ULONG), (DWORD)((pSectorInfoBuff->wReserved2   << 16) |
                                                                             (pSectorInfoBuff->bBadBlock << 8)  | 
                                                                             (pSectorInfoBuff->bOEMReserved)));
            do
            {
                WRITE_COMMAND(physicalSectorAddr+SECTOR_SIZE, READ_STATUS_CMD);
            }
            while (!CHECK_STATUS(physicalSectorAddr+SECTOR_SIZE, STATUS_READY_MASK));

            pSectorInfoBuff += sizeof(SectorInfo);
#endif            
        }
    }

    fRet = TRUE;

exit:
    //----- 3. Put the Flash back into READ ARRAY mode -----
    WRITE_COMMAND(ulBlockAddress, READ_ARRAY_CMD);
    SetWriteProtect(TRUE);
    SetKMode(bLastMode);

    return fRet;
}
DWORD DoBufferedWrite(volatile ULONG ulBlockAddress,
                      volatile SECTOR_ADDR physicalSectorAddr,
                      PUCHAR pBuffer,
                      USHORT NumWriteBytes)
{
    DWORD k = 0;
    DWORD dwBusWidth = g_bPairedFlash ? sizeof(ULONG) : sizeof(USHORT);
    ULONG ulCount;

    // Let the flash know the size of the buffer we plan to send (note that this is a 0-based word count and we simulatenously
    // write it to both flash parts (upper and lower).
    DWORD dwWordCount = (NumWriteBytes / dwBusWidth) - 1;

    WRITE_COMMAND(ulBlockAddress, BUFFER_WRITE_CMD);
//    DelayInuSec(1000);
    ulCount = 0;
	while (!CHECK_STATUS(ulBlockAddress, STATUS_READY_MASK))
    {
        DelayInuSec(1);
        if ( ulCount++ == 1000 )
        {
            RETAILMSG(1, (TEXT("DoBufferedWrite: Timed out writing buffered data 0\r\n")));
            return 0;
        }
    }
    
//    RETAILMSG(1, (TEXT("DoBufferedWrite: dwWordCount = %d should 15 \r\n"), dwWordCount));
    if (g_bPairedFlash)
    {
        PULONG pLongBuffer = (PULONG)pBuffer;
        WRITE_ULONG (ulBlockAddress, (dwWordCount << 16) | dwWordCount);
        
        for(k = 0 ; k < NumWriteBytes ; k += dwBusWidth)
        {
            WRITE_ULONG (physicalSectorAddr + k, *pLongBuffer++);
            ulCount = 0;
        	while (!CHECK_STATUS(ulBlockAddress, STATUS_READY_MASK))
            {
                DelayInuSec(1);
                if ( ulCount++ == 1000 )
                {
                    RETAILMSG(1, (TEXT("DoBufferedWrite: Timed out writing buffered data 1\r\n")));
                    return 0;
                }
            }
        }
    }
    else 
    {
        PUSHORT pShortBuffer = (PUSHORT)pBuffer;
        WRITE_USHORT (ulBlockAddress, (USHORT)dwWordCount);
        
        for(k = 0 ; k < NumWriteBytes ; k += dwBusWidth)
        {
            WRITE_USHORT (physicalSectorAddr + k, *pShortBuffer++);        
            ulCount = 0;
        	while (!CHECK_STATUS(ulBlockAddress, STATUS_READY_MASK))
            {
                DelayInuSec(1);
                if ( ulCount++ == 1000 )
                {
                    RETAILMSG(1, (TEXT("DoBufferedWrite: Timed out writing buffered data 2\r\n")));
                    return 0;
                }
            }
        }
    }        

    // Now program the buffer into flash...
    //
    WRITE_COMMAND(ulBlockAddress, BLOCK_PROCEED_CMD);
//    DelayInuSec(100);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -