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

📄 dvb_flash.c

📁 DVB软件,基于CT216软件的开发源程序.
💻 C
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************

        (C)Copyright Cheertek Inc. 2005,
           K000, all right reserved.

         Product: STB Firmware
         Note: Because flash and CI module use the same host bus,
               it must pause CI before access flash.

****************************************************************************/
#include <stdio.h>
#include <string.h>
#include "ct_type.h"
#include "ct_os.h"
#include "ct_flash.h"
#include "ct_drammap.h"
#include "ct_sys.h"
#include "dvb_sys.h"
#include "dvb_flash.h"
#include "dvb_msg.h"
#ifdef CI_ENABLE
	#include "dvb_ci.h"
#endif
#ifdef ATA_ENABLE
	#include "dvb_ata.h"
#endif



#define FLASH_TEST			0
/*******************************************************************************************/
#define FLASH_MSG(p)      printf p
//#define FLASH_DBG(p)      printf p
#define FLASH_DBG(p)
/*******************************************************************************************/

static ST_FLASH_INFO_T	stFlashInfo;
static CT_FLASH_Query	stFlashQuery;
static EN_DRV_RESULT	b8DVBFlashInit = DRVERR_NOTINITIALISED;
static u8				_bFlashBankNum  = 0;
static u8				_bFlashBootType = 1 ; /* <0 : bottom, 1 : top> */

//Jeans 20070201 boot from WICE & flash code writing
#include "ct_video.h"
static u8 bootWice = 0;

static bool8 flash_init(void)
{
	EN_DRV_RESULT	enResult;
	u8				i;
	
	/*
	** If the flash initialed but failure, do nothing and return false.
	** If the flash initialed and success, do nothing and return true.
	** If the flash is not initial yet, initial it.
	** If the host bus can't open, return FALSE but not mark the initial flag 
	** to initial again at next time.
	*/
	if (b8DVBFlashInit == DRVERR_NOTINITIALISED)
	{
		if(CT_SYS_Hostbus_Open(EN_CT_HOSTBUS_ACCESS_FLASH))
		{
/*#ifdef CI_ENABLE
			DVB_CI_Pause();
#endif*/
#ifdef ATA_ENABLE
			DVB_ATA_Pause();
#endif
			
			CT_Flash_Init();

			stFlashInfo.u16Manufacture = CT_Flash_GetManufactcode();
			stFlashInfo.u16Device      = CT_Flash_GetDeviceid(stFlashInfo.u16Manufacture);
			stFlashInfo.u32FlashSize   = 0;
			stFlashInfo.u8Num_Bank     = 0;
			
			FLASH_DBG(( "{flash_init} Manufactory Code=0x%X, DeviceID=0x%X\n", 
				stFlashInfo.u16Manufacture, stFlashInfo.u16Device));
			
			memset(&stFlashQuery, 0, sizeof(CT_FLASH_Query));
			enResult = CT_Flash_GetBlockInfo(&stFlashQuery);
			CT_SYS_Hostbus_Close();
			
			if(enResult != DRV_OK)
			{
				//Jeans 20070201 boot from WICE & flash code writing
				FLASH_MSG(("\nFlash unknown, default to WICE"));
				bootWice = 1;

				FLASH_DBG(( "{flash_init} CT_Flash_GetBlockInfo() failed\n"));
				b8DVBFlashInit = DRVERR_NOTOK;
				return FALSE;
			}
			
			if(stFlashQuery.erase_block[0].sector_size != FLASHSRV_BYTE_MODE_BANK_SIZE)
				_bFlashBootType = 0;

			for(i = 0; i < stFlashQuery.num_erase_blocks; i++)
				stFlashInfo.u32FlashSize += (stFlashQuery.erase_block[i].sector_size * stFlashQuery.erase_block[i].num_sectors);
		
			stFlashInfo.u8Num_Bank = stFlashInfo.u32FlashSize / FLASHSRV_BYTE_MODE_BANK_SIZE;
			_bFlashBankNum         = stFlashInfo.u8Num_Bank;
			
			b8DVBFlashInit = DRV_OK;
			return TRUE;
		}
		else
		{
			FLASH_DBG(( "{flash_init} CT_SYS_Hostbus_Open failed!\n"));
			return FALSE;
		}
	}
	else if(b8DVBFlashInit == DRVERR_NOTOK)
		return FALSE;

	return TRUE;
}

/*! \fn bool8 DVB_FlashCheckID(ST_FLASH_INFO_T *pstFlashInfo)
\brief check on board flash ID. this function only check flash id at first time, after that return local variable only.
\param pstFlashInfo (Output) refer to ST_FLASH_INFO_T structure.
\return TRUE - success
\return FALSE - flash not found
*/
bool8 DVB_FlashCheckID (ST_FLASH_INFO_T *pstFlashInfo)
{
	/* check parameter */
	if(!pstFlashInfo)			return FALSE;
	//Jeans 20070201 boot from WICE & flash code writing
	//if(flash_init() == FALSE)	return FALSE;
	if(flash_init() == FALSE)	//return FALSE;
	{
		if(bootWice)
		{
	    u8 i=0;
	    
	    CT_MPG_DisableAVInterrupt();
	    //Copy image from ROM_EMU to DRAM(MPEG area)
	    memcpy((u8*)MPEG_DRAM_BASE_ADDRESS, 0, 0x100000/*1024k*/);
	    
	    FLASH_MSG(("\n!!! please select flash jumpper !!!\n")) ;
	    DVB_TimerBlockingWait(5000);
	    
			//Init the flash
			b8DVBFlashInit = DRVERR_NOTINITIALISED;
			if(flash_init() == FALSE)	
			{
				FLASH_MSG(("\n flash_init() FALSE!"));
	        }
	    #if 1	//Swich to 0 no writing flash code
	        //Copy image from DRAM(MPEG area) to Flash
	        FLASH_MSG(("\nErase Flash Banks"));
			for(i=0; i<_bFlashBankNum; i++)
			{
				if(DVB_FlashErase(i) == FALSE)
				{
					FLASH_MSG(("\nErase bank:%d FALSE!",i));
				}
				else
				{
					FLASH_MSG(("."));
				}
			}
			FLASH_MSG(("\nWrite Flash ... "));
    	    if(DVB_FlashWrite(0, 0, (u8*)MPEG_DRAM_BASE_ADDRESS, 0x100000/*1024k*/)==FALSE)
    	    {
    	    	FLASH_MSG(("\nCopy code to Flash fail!"));
    	    }
			FLASH_MSG(("done!\n"));
			#endif
			CT_MPG_EnableAVInterrupt();
			//FLASH_MSG(("\n!!! reset !!!\n"));
			//while(1);
		}
		else	return FALSE;
	}

	memcpy(pstFlashInfo, &stFlashInfo, sizeof(ST_FLASH_INFO_T));
	return TRUE;
}

#ifdef P000_TEST
/*! \fn bool8 DVB_FlashIsWriteProtect (void)
\brief Check now is protect for write or not
\param none
\return TRUE - protect
\return FALSE - unprotect
*/
bool8 DVB_FlashIsWriteProtect (void)
{
	u8 u8Status = CT_Flash_StatusRead (FALSE);
	
	if ((u8Status & 0x1c) != 0)
	{
		// protect
		return TRUE;
	}	
	else
	{
		// unprotect
		return FALSE;
	}	
}	
#endif // end #ifdef P000_TEST

/*! \fn bool8 DVB_FlashErase(u8 u8Bank)
\brief Erase a flash bank
\param u8Bank (Input) the bank want to erase
\return TRUE - success
\return FALSE - erase fail
*/
bool8 DVB_FlashErase (u8 u8Bank)
{
	EN_DRV_RESULT	enResult = DRV_OK;
	u32				u32StartAddr, u32EndAddr, u32SecAddr;
	u8				i, j;

#ifdef P000_TEST
	if (DVB_FlashIsWriteProtect () == TRUE)
	{
		return FALSE;
	}
#endif // end #ifdef P000_TEST
	
	if(flash_init() == FALSE)	return FALSE;
	
	if(CT_SYS_Hostbus_Open(EN_CT_HOSTBUS_ACCESS_FLASH))
	{
/*#ifdef CI_ENABLE
		DVB_CI_Pause();
#endif*/
#ifdef ATA_ENABLE
		DVB_ATA_Pause();
#endif

		u32StartAddr = DVB_FlashGetBankAddress(u8Bank);
		
		/*
		** one bank size is 0x10000, and one bank equals one sector except boot bank.
		** at boot bank, there are more than one sectors with different size,
		** so calculate the sector start address to erase.
		**
		** if the block info doesn't exist, erase one sector directly.
		*/
		if(stFlashQuery.num_erase_blocks <= 0)
		{
			enResult = CT_Flash_SectorErase(u32StartAddr);
		}
		else
		{
			u32EndAddr   = u32StartAddr + FLASHSRV_BYTE_MODE_BANK_SIZE;
			u32SecAddr   = 0;

			for(i = 0; i < stFlashQuery.num_erase_blocks; i++)
			{
				for(j = 0; j < stFlashQuery.erase_block[i].num_sectors; j++)
				{
					if(u32SecAddr >= u32StartAddr)
					{
						enResult |= CT_Flash_SectorErase(u32SecAddr);
						if(enResult != DRV_OK)		break;
					}
					
					u32SecAddr += stFlashQuery.erase_block[i].sector_size;
					if(u32SecAddr >= u32EndAddr)	break;
				}
				
				if(enResult != DRV_OK)			break;
				if(u32SecAddr >= u32EndAddr)	break;
			}
		}
		
		CT_SYS_Hostbus_Close();
		if (enResult == DRV_OK)
		{
			return  TRUE;
		}
		else
		{
			return FALSE;
		}
		
	}
	else
	{
		FLASH_DBG(( "{DVB_FlashErase} CT_SYS_Hostbus_Open failed!\n"));
		return FALSE;
	}
}

/*! \fn bool8 DVB_FlashChipErase(void)
\brief Erase total data in flash
\return TRUE - success
\return FALSE - erase fail
*/
bool8 DVB_FlashChipErase (void)
{
	EN_DRV_RESULT	enResult = DRV_OK;

	if(flash_init() == FALSE)	return FALSE;
	
	if (CT_SYS_Hostbus_Open(EN_CT_HOSTBUS_ACCESS_FLASH))
	{
/*#ifdef CI_ENABLE
		DVB_CI_Pause();
#endif*/
#ifdef ATA_ENABLE
		DVB_ATA_Pause();
#endif
		
		enResult = CT_Flash_ChipErase();
		CT_SYS_Hostbus_Close();
		
		if (enResult == DRV_OK)
		{
			return TRUE;
		}
		else
		{
			FLASH_DBG(( "{DVB_FlashChipErase} CT_Flash_ChipErase failed\n"));
			return FALSE;
		}
	}
	else
	{
		FLASH_DBG(( "{DVB_FlashChipErase} CT_SYS_Hostbus_Open failed\n"));
		return FALSE;
	}
}

/*! \fn bool8 DVB_FlashWrite(u8 u8Bank, u32 u32Address, u8* pu8Buffer, u32 u32BufferSize)
\brief write data to specific bank and address
\param u8Bank (Input) the bank want to write
\param u32Address (Input) the bank address of start writing
\param pu8Buffer (Input) data pointer of write buffer
\param u32BufferSize (Input) bytes of write buffer
\return TRUE - success
\return FALSE - write fail
*/
bool8 DVB_FlashWrite (u8 u8Bank, u32 u32Address, u8* pu8Buffer, u32 u32BufferSize)
{
	return DVB_FlashWriteData(DVB_FlashGetBankAddress(u8Bank) + u32Address, pu8Buffer, u32BufferSize);
}	

/*! \fn bool8 DVB_FlashRead(u8 u8Bank, u32 u32Address, u8* pu8Buffer, u32 u32BufferSize)
\brief read data from specific bank and address
\param u8Bank (Input) the bank want to read
\param u32Address (Input) the bank address of start reading
\param pu8Buffer (Output) data pointer of read buffer
\param u32BufferSize (Output) bytes of read buffer
\return TRUE - success
\return FALSE - read fail
*/
bool8 DVB_FlashRead (u8 u8Bank, u32 u32Address, u8* pu8Buffer, u32 u32BufferSize)
{
	return DVB_FlashReadData(DVB_FlashGetBankAddress(u8Bank) + u32Address, pu8Buffer, u32BufferSize);
}

bool8 DVB_FlashWriteData(u32 u32Address, u8* pu8Buffer, u32 u32BufferSize)
{
	EN_DRV_RESULT	enResult = DRV_OK;

#ifdef P000_TEST
	if (DVB_FlashIsWriteProtect () == TRUE)
	{
		return enResult;
	}
#endif // end #ifdef P000_TEST

	if(!pu8Buffer)		return FALSE;
	if(!flash_init())	return FALSE;
	
	if(CT_SYS_Hostbus_Open(EN_CT_HOSTBUS_ACCESS_FLASH))
	{
/*#ifdef CI_ENABLE
		DVB_CI_Pause();
#endif*/
#ifdef ATA_ENABLE
		DVB_ATA_Pause();
#endif
		enResult = CT_Flash_Burn(u32Address, (u32)pu8Buffer, u32BufferSize);
		CT_SYS_Hostbus_Close();
		
		if(enResult == DRV_OK)
		{
			return TRUE;
		}
		else
		{
			FLASH_DBG(( "{DVB_FlashWriteData} CT_Flash_Burn failed! (Addr=0x%06lX)\n", u32Address));
			return FALSE;
		}
	}
	else
	{
		FLASH_DBG(( "{DVB_FlashWriteData} CT_SYS_Hostbus_Open failed! (Addr=0x%06lX)\n", u32Address));
		return FALSE;
	}
}	

bool8 DVB_FlashReadData( u32 u32Address, u8* pu8Buffer, u32 u32BufferSize)
{
	EN_DRV_RESULT	enResult = DRV_OK;

	if(!pu8Buffer)		return FALSE;

⌨️ 快捷键说明

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