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

📄 flash.cpp

📁 coldfire5206芯片平台的自捡程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				{
					lTempLen1 = 0;
				}
				else
				{
					lTempLen1 = FLASH_NORMAL_BLOCK_SIZE + g_anFlashTailAddr[0] - nTempAddr;
				}
			}
			pTempStr1	= (UCHAR*)(s_acFlashBlockBuffer + nTempAddr - g_anFlashTailAddr[0]);
			for (i = 0; i < lTempLen1; i ++)
				*(pTempStr1 + i) = *(pTempStr2 + i);
			
			// unlock and erase all header blocks are unlocked, and earse all lastest blocks
			pStates = (UINT32*)(g_anFlashTailAddr[0]);
			*pStates = 0x00500050;	// clear status
			for( int i = 0; i < 8; i++ )
			{			
				// unlock
				pStates = ( UINT32*)(g_anFlashTailAddr[i]);
				*pStates = 0x00600060;
				*pStates = 0x00d000d0;
				*pStates = 0x00900090;
				// 注意使用g_anFlashTailAddr[0],而不是使用g_anFlashTailAddr[i]
				while (*((UINT32*)(g_anFlashTailAddr[0] + g_nSecondAddr))
					&& 0x00010001);
				*pStates = 0xffffffff;
				// erase 
				pStates = ( UINT32*)(g_anFlashTailAddr[i]);
				*pStates = 0x00500050;	// clear status
				pDestineStart = (UINT32*)(g_anFlashTailAddr[i]);
				*pDestineStart = 0x00200020;
				*pDestineStart = 0x00d000d0;
				*pStates = 0x00700070;
				while( (*pStates & SR7_OK) != SR7_OK );
				*pStates = 0x00500050;
				*pStates = 0xffffffff;
			}
			// write data
			pStates = (UINT32*)(g_anFlashTailAddr[0]);
			*pStates = 0x00500050;
			pSourceStart = (UINT32*)s_acFlashBlockBuffer;
			pDestineStart = (UINT32*)(g_anFlashTailAddr[0]);
			for (i = 0; i < FLASH_NORMAL_BLOCK_SIZE/4;i ++)
			{
				*pStates = 0x00400040;
				pDestineStart[i] = pSourceStart[i];
				*pStates = 0x00700070;
				while( (*pStates & SR7_OK) != SR7_OK );
				*pStates = 0x00500050;
			}
			*pStates = 0xffffffff;
			lTempLen = 0;
			continue;	// data's writing  is over
		}	
		else	//	---	 if (nTempAddr < FLASH_TAIL_POS)
		{
			// read current block, nTempAddr1 is start address of current block
			nTempAddr1 = nTempAddr / FLASH_NORMAL_BLOCK_SIZE * FLASH_NORMAL_BLOCK_SIZE;
			pTempStr = (UCHAR*)(nTempAddr1);
			for (int j = 0; j < FLASH_NORMAL_BLOCK_SIZE; j ++)
				*(s_acFlashBlockBuffer + j) = *(pTempStr + j);		
			// combine data
			lTempLen1 = lTempLen;
			if (lTempLen1 + nTempAddr > FLASH_NORMAL_BLOCK_SIZE + nTempAddr1)
			{
				lTempLen1 = FLASH_NORMAL_BLOCK_SIZE + nTempAddr1 - nTempAddr;
			}
			pTempStr1	= (UCHAR*)(s_acFlashBlockBuffer + nTempAddr - nTempAddr1);
			for (j = 0; j < lTempLen1; j ++)
				*(pTempStr1 + j) = *(pTempStr2 + j);
			// unlock 
			pStates = (UINT32*)(nTempAddr1);
			*pStates = 0x00500050;	// clear status
			// 注意unblock最后读状态需要使用 block的第二个地址,即使用nTempAddr1 + g_nSecondAddr
			pStates = ( UINT32*)(nTempAddr1);
			*pStates = 0x00600060;
			*pStates = 0x00d000d0;	
			*pStates = 0x00900090;
			while (*((UINT32*)(nTempAddr1 + g_nSecondAddr))	&& 0x00010001);
			*pStates = 0xffffffff;
			// erase 
			pStates = (UINT32*)(nTempAddr1);
			*pStates = 0x00500050;	// clear status
			pDestineStart = (UINT32*)(nTempAddr1);
			*pDestineStart = 0x00200020;
			*pDestineStart = 0x00d000d0;
			*pStates = 0x00700070;
			while( (*pStates & SR7_OK) != SR7_OK );
			*pStates = 0x00500050;
			*pStates = 0xffffffff;
			// write data
			pStates = (UINT32*)(nTempAddr1);
			*pStates = 0x00500050;
			pSourceStart = (UINT32*)s_acFlashBlockBuffer;
			pDestineStart = (UINT32*)(nTempAddr1);
			for (i = 0; i < FLASH_NORMAL_BLOCK_SIZE/4; i ++)
			{
				*pStates = 0x00400040;
				*(pDestineStart + i) = *(pSourceStart + i);
				*pStates = 0x00700070;
				while( (*pStates & SR7_OK) != SR7_OK );
				*pStates = 0x00500050;
			}
			lTempLen -= lTempLen1;
			nTempAddr += lTempLen1;
			pTempStr2 += lTempLen1;
			continue;	// continue
		}	// end 	 if (nTempAddr >= g_anFlashTailAddr[0])
	}	// end while(nTempLen > 0)

	*pStates = 0xffffffff;	// change to read mode

	CHIP_PROTECT;

	// check up by checksum
	UINT32 nCheckSum;
	UCHAR* pFlashInfo;

	nCheckSum = 0;
	nLen &= (~1);
	nTempAddr = nAddr;
	nTempAddr += FLASH_BASE_ADDRESS;
	pFlashInfo = (UCHAR* )(nTempAddr);
	for ( i = 0; i < nLen; i += 2)
	{
		if (*((UINT16*)(pInfo + i)) != *((UINT16*)(pFlashInfo + i)))
		{
			break;
		}
		nCheckSum += *((UINT16*)(pInfo + i));
	}
	if (i == nLen)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

void ChipErase()
{
	UINT32 nTempAddr;
	UINT32 *pStates;
	UINT32*		pDestineStart;

	CHIP_UNPROTECT;

	nTempAddr = 0;
	nTempAddr += FLASH_BASE_ADDRESS;
	while (1)
	{
		if (nTempAddr >= g_anFlashTailAddr[0])
		{
			// 将所有的尾部参数扇区作为一个扇区处理,只是需要分别擦除
			// 其他读写都作为一个正常扇区处理
			// unlock and erase all header blocks are unlocked, and earse all lastest blocks
			pStates = (UINT32*)(g_anFlashTailAddr[0]);
			*pStates = 0x00500050;	// clear status
			for( int i = 0; i < 8; i++ )
			{			
				// unlock
				pStates = ( UINT32*)(g_anFlashTailAddr[i]);
				*pStates = 0x00600060;
				*pStates = 0x00d000d0;
				*pStates = 0x00900090;
				// 注意使用g_anFlashTailAddr[0],而不是使用g_anFlashTailAddr[i]
				while (*((UINT32*)(g_anFlashTailAddr[0] + g_nSecondAddr))
					&& 0x00010001);
				*pStates = 0xffffffff;
				// erase 
				pStates = ( UINT32*)(g_anFlashTailAddr[i]);
				*pStates = 0x00500050;	// clear status
				pDestineStart = (UINT32*)(g_anFlashTailAddr[i]);
				*pDestineStart = 0x00200020;
				*pDestineStart = 0x00d000d0;
				*pStates = 0x00700070;
				while( (*pStates & SR7_OK) != SR7_OK );
				*pStates = 0x00500050;
				*pStates = 0xffffffff;
			}
			*pStates = 0xffffffff;
			nTempAddr += FLASH_NORMAL_BLOCK_SIZE;
			break;	// data's writing  is over
		}	
		else	//	---	 if (nTempAddr < FLASH_TAIL_POS)
		{
			// unlock 
			pStates = (UINT32*)(nTempAddr);
			*pStates = 0x00500050;	// clear status
			// 注意unblock最后读状态需要使用 block的第二个地址,即使用nTempAddr1 + g_nSecondAddr
			pStates = ( UINT32*)(nTempAddr);
			*pStates = 0x00600060;
			*pStates = 0x00d000d0;	
			*pStates = 0x00900090;
			while (*((UINT32*)(nTempAddr + g_nSecondAddr))	&& 0x00010001);
			*pStates = 0xffffffff;
			// erase 
			pStates = (UINT32*)(nTempAddr);
			*pStates = 0x00500050;	// clear status
			pDestineStart = (UINT32*)(nTempAddr);
			*pDestineStart = 0x00200020;
			*pDestineStart = 0x00d000d0;
			*pStates = 0x00700070;
			while( (*pStates & SR7_OK) != SR7_OK );
			*pStates = 0x00500050;
			*pStates = 0xffffffff;
			nTempAddr += FLASH_NORMAL_BLOCK_SIZE;
			continue;	// continue
		}	// end 	 if (nTempAddr >= g_anFlashTailAddr[0])
	}	// end while(nTempLen > 0)

	*pStates = 0xffffffff;	// change to read mode

	CHIP_PROTECT;

}

UINT16 TestFlashRom()
{
	UINT16 nRtn;
	UINT32* pTempData;
	UINT32*  pStates;
	UINT32   lFlashSize;
	UINT32 lTempData;

	if( JudgeLayOut()  == FALSE)
	{
		nRtn = 0xaa;
		return nRtn;
	}
	
	if (g_nFlashSizeType == FLASH_SIZE_4M)
	{
		lFlashSize = 0x400000;
	}
	else
	{
		lFlashSize = 0x200000;
	}

	ChipErase();
	CHIP_UNPROTECT;
	//clear state data
	pTempData = (UINT32*)(FLASH_BASE_ADDRESS);
	pStates = pTempData;
	*pStates = 0x00500050;
	for (int i = 0; i < lFlashSize; i += 4 )
	{
		*pStates = 0x00400040;
		*pTempData = 0xfffff7ff;
		//wait until writing ready
		while( (*pStates & SR7_OK) != SR7_OK );
		*pStates = 0xffffffff;
		lTempData = *pTempData;
		if ( lTempData != 0xfffff7ff )
		{
			nRtn = 1;
			return( nRtn );
		}
		pTempData++;
	}
	CHIP_PROTECT;	

	ChipErase();
	CHIP_UNPROTECT;
	//clear state data
	pTempData = (UINT32*)(FLASH_BASE_ADDRESS);
	pStates = pTempData;
	*pStates = 0x00500050;
	for (int i = 0; i < lFlashSize; i += 4 )
	{
		*pStates = 0x00400040;
		*pTempData = 0xAAAAAAAA;
		//wait until writing ready
		while( (*pStates & SR7_OK) != SR7_OK );
		*pStates = 0xffffffff;
		lTempData = *pTempData;
		if ( lTempData != 0xAAAAAAAA )
		{
			nRtn = 1;
			return( nRtn );
		}
		pTempData++;
	}
	CHIP_PROTECT;	

	*pStates = 0x00700070;
	//if writing succeed, return 0, else return 1
	if( ( *pStates & SR134_ERR))
	{
		*pStates = 0xffffffff;
		return 1;
	}
	else
	{
		*pStates = 0xffffffff;
		return 0;
	}

}

⌨️ 快捷键说明

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