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

📄 fmd.cpp

📁 EP9315的wince下载程序。download.exe
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	        {
	            WRITE_COMMAND(ulBlockAddress, BLOCK_PROCEED_CMD);
	        }

	        // Wait for status...
	        WRITE_COMMAND(ulBlockAddress, READ_STATUS_CMD);
	        do
	        {
	            //WRITE_COMMAND(ulBlockAddress, READ_STATUS_CMD);
	        }
	        while (!CHECK_STATUS(ulBlockAddress, STATUS_READY_MASK));			
		WRITE_COMMAND(ulBlockAddress, CLEAR_STATUS_CMD);

		

		WRITE_COMMAND(ulBlockAddress, READ_ARRAY_CMD);
	        // TODO
	}
	else
	{
		for(j = g_FMDInfo.gdwCurEraseBlock; j< g_FlashRegion[g_FMDInfo.gdwCurEraseRegion].blocks; j++) 
		{

		        // Clear the status register.
		        WRITE_COMMAND(ulBlockAddress, CLEAR_STATUS_CMD);

		        // Set or Clear the block lock bit and confirm.
		        WRITE_COMMAND(ulBlockAddress, BLOCK_LOCK_CMD);
		        if (bLock)
		        {
		            WRITE_COMMAND(ulBlockAddress, BLOCK_SETLOCK_CMD);
		        }
		        else
		        {
		            WRITE_COMMAND(ulBlockAddress, BLOCK_PROCEED_CMD);
		        }

		        // Wait for status...
		        do
		        {
		            WRITE_COMMAND(ulBlockAddress, READ_STATUS_CMD);
		        }
		        while (!CHECK_STATUS(ulBlockAddress, STATUS_READY_MASK));
			WRITE_COMMAND(ulBlockAddress, CLEAR_STATUS_CMD);	

				

			ulBlockAddress = ulBlockAddress +g_FlashRegion[g_FMDInfo.gdwCurEraseRegion].block_size;
		}

	}

        ++blockID;
    }
	
    SetWriteProtect(TRUE);

    SetKMode(bLastMode);

#if flash_debug
RETAILMSG(1, (TEXT("SetBlockLock --over ID =%x num=%x).\r\n"),blockID,NumBlocks));
#endif	
    return(TRUE);
}


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Function:       FMD_ReadSector()

Description:    Reads the requested sector data and/or sector metadata from 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 this data
                resides in the respective Flash block (see note above).

                By default, the NOR Flash is configured in READ ARRAY MODE so there
                is no need to set any control lines to access the media.  The data
                can just be read directly from the media (like RAM).
                
Returns:        Boolean indicating success.
------------------------------------------------------------------------------*/
BOOL  FMD_ReadSector(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff, DWORD dwNumSectors)
{
    volatile SECTOR_ADDR  physicalSectorAddr = 0;
    volatile ULONG ulBlockAddress = 0;
    BLOCK_ID blockID = 0;
    DWORD i = 0;
    int j;
    BOOL bLastMode;

	#if flash_debug
	RETAILMSG(1, (TEXT("FMD_ReadSector: (startAdd =%x num=%x) sectot_addr=%x,sector_info %x.\r\n"),startSectorAddr,dwNumSectors,
									pSectorBuff,pSectorInfoBuff));
	#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 read 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 -----
        // Note we do this differently based on whether the caller wants us to read 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 read from 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);
		
        ulBlockAddress = (ULONG)(physicalSectorAddr - (physicalSectorAddr % g_FMDInfo.BlockSize));

	if(GetEraseFlashSectorIndex(ulBlockAddress-g_FMDInfo.ChipBaseAddress)==FALSE)
	{
		  RETAILMSG(1, (TEXT("ERASE Block error,not aligned\r\n")));
		return FALSE;
	}
	
	#if flash_debug
	RETAILMSG(1, (TEXT("blockID=%x,physicalSectorAddr=%x,g_bXIPMode=%x,ulBlockAddress=%x,g_FMDInfo.gdwCurEraseRegion=%x,gdwCurEraseBlock=%x.\r\n"),
						blockID,physicalSectorAddr,g_bXIPMode,ulBlockAddress,g_FMDInfo.gdwCurEraseRegion,g_FMDInfo.gdwCurEraseBlock));
	#endif

	if(g_FlashRegion[g_FMDInfo.gdwCurEraseRegion].block_size==g_FMDInfo.BlockSize)
	{  

	#if flash_debug
	RETAILMSG(1, (TEXT("1 \r\n")));
	#endif
	
	        WRITE_COMMAND(ulBlockAddress, READ_ARRAY_CMD);
	
	#if flash_debug
	RETAILMSG(1, (TEXT("2 \r\n")));
	#endif

	        //----- Read the necessary sector data -----
	        if(pSectorBuff)
	        {
	            memcpy(pSectorBuff, (CONST PVOID)physicalSectorAddr, SECTOR_SIZE);
	            pSectorBuff += SECTOR_SIZE;
	        }
			
	#if flash_debug
	RETAILMSG(1, (TEXT("3 \r\n")));
	#endif
	        //----- Read the necessary SectorInfo data (metadata) -----
	        if(!g_bXIPMode && pSectorInfoBuff)
	        {
	            // The metadata bytes are read directly into the SectorInfo structure...
	            memcpy(pSectorInfoBuff, (CONST PVOID)(physicalSectorAddr+SECTOR_SIZE), sizeof(SectorInfo));
	            pSectorInfoBuff += sizeof(SectorInfo);
	        }

	#if flash_debug
	RETAILMSG(1, (TEXT("4 \r\n")));
	#endif		
	
		#if flash_debug
		RETAILMSG(1, (TEXT("read sector %x\r\n"),physicalSectorAddr,ulBlockAddress));
		#endif
	}
	else
	{
	#if flash_debug
	RETAILMSG(1, (TEXT("5 \r\n")));
	#endif	
	
		for(j = g_FMDInfo.gdwCurEraseBlock; j< g_FlashRegion[g_FMDInfo.gdwCurEraseRegion].blocks; j++) 
		{
			WRITE_COMMAND(ulBlockAddress, READ_ARRAY_CMD);
			ulBlockAddress = ulBlockAddress +g_FlashRegion[g_FMDInfo.gdwCurEraseRegion].block_size;

			#if flash_debug
			RETAILMSG(1, (TEXT("6 \r\n")));
			#endif	
		}

			#if flash_debug
			RETAILMSG(1, (TEXT("7 \r\n")));
			#endif	
		//----- Read the necessary sector data -----
	        if(pSectorBuff)
	        {
	            memcpy(pSectorBuff, (CONST PVOID)physicalSectorAddr, SECTOR_SIZE);
	            pSectorBuff += SECTOR_SIZE;
	        }

			#if flash_debug
			RETAILMSG(1, (TEXT("8 \r\n")));
			#endif	
	        //----- Read the necessary SectorInfo data (metadata) -----
	        if(!g_bXIPMode && pSectorInfoBuff)
	        {
	            // The metadata bytes are read directly into the SectorInfo structure...
	            memcpy(pSectorInfoBuff, (CONST PVOID)(physicalSectorAddr+SECTOR_SIZE), sizeof(SectorInfo));
	            pSectorInfoBuff += sizeof(SectorInfo);
	        }

			#if flash_debug
			RETAILMSG(1, (TEXT("9 \r\n")));
			#endif		
	}
		
    }

    SetWriteProtect(TRUE);
    SetKMode(bLastMode);

#if flash_debug
	RETAILMSG(1, (TEXT("FMD_ReadSector: over.\r\n") ));
#endif

    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;
	
    DWORD uResult = 0;

#if flash_debug
RETAILMSG(1, (TEXT("FMD_WriteSector: startAdd =%x num=%x)  sector_add=%x sectorinfo_add=%x.\r\n"),startSectorAddr,dwNumSectors,
						pSectorBuff,pSectorInfoBuff));
#endif


#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;
            }

		uResult =1;
		
            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)&&(uResult))
        {
            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;

⌨️ 快捷键说明

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