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

📄 upgrade_lg.c

📁 M3355的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
*****************************************************************************/
int Write2MX1611(unsigned long StartAddr, LPWORD awData)
{
    DWORD pagenumber;
    int page, iRV, count;
    for (page = 0; page < 16;page++) /* 16 page once */
    {
        pagenumber = (StartAddr - F_STARTADDR) * 16 / 1024 + page;
        for (count = 0;count < 5;count++)
        {
            iRV = Page_Program_Op(pagenumber, &awData[64 * page]); /* 64 byte once */
            if (iRV == TRUE)
                break;
        }
        if (iRV == FALSE)
        {
            PRINTF("Program a page FAIL,try 5 times,pagenum=%d\n", pagenumber);
            return FALSE;
        }
        //  PRINTF("Program a page OK,pagenum=%d\n", pagenumber);
    }
    return TRUE;
}

#endif/*#if ( _FLASH_TYPE==MX1611 )*/

/*****************************************************************************
 Function	 : Erase_Flash()
 Description  : Erase_Flash
 Arguments	: None
 Return value : C_TRUE -> success
				C_FAIL -> fail
*****************************************************************************/
int Erase_Flash()
{
    int ret;

	UPPRINTF(">>>>> Erase_Flash\r\n");
//for test
//	return TRUE;
    ret = Erase_M29F800();
//    ret = Chip_Erase_MX29LV040();
//    ret = Chip_Erase_SST39SF040();
	UPPRINTF("<<<<<< Erase_Flash\r\n");
	return ret;

}

/*****************************************************************************
 Function	 : Erase_M29F800()
 Description  : Erase_M29F800
 Arguments	: None
 Return value : C_TRUE -> success
				C_FAIL -> fail
*****************************************************************************/
int Erase_M29F800()
{
    int iRV, i;

//    UPPRINTF("\n Erase M29F800 begin!!!!NUM_BLOCKS=%d\r\n",NUM_BLOCKS);
    UPPRINTF("\n Erase M29F800 begin!!!!\r\n");

    iRV = FlashChipErase();
    if (iRV < 0)
    {
        EPRINTF("%s\n", FlashErrorStr(iRV));
		UPPRINTF("--111111111111111\r\n");
    }
    else
    {
        EPRINTF("Block %d is protected!\n", iRV);
		UPPRINTF("--22222222222\r\n");
    }
    if (iRV != FLASH_SUCCESS)
    {
		UPPRINTF("--33333333333\r\n");
    }
    FlashPause(10);
    FlashReadReset();
	UPPRINTF("FlashReadReset return\r\n");
    if (iRV != FLASH_SUCCESS)
    {
		UPPRINTF("--6666666666\r\n");
        return FALSE;
    }
    else
    {
		UPPRINTF("--777777777\r\n");
        return TRUE;
    }
}


// Get 1ms timer tick
#if 0
unsigned long GetTimerTickup()
{
	extern unsigned long TimerTick;
    return TimerTick;
}
#endif

//////////////////////////////////////////////////////////////////////
BOOL WriteToFlash(BYTE* buffer, DWORD ROMSize)
{
	BYTE Data[2048];
	int  objnum = 0;
	int  readsize;

	int i, j, sectors;
	DWORD StartAddr;
	int progress = 0;
	int step;
	int iLastProgress = 0;

    StartAddr = F_STARTADDR;

    sectors = ROMSize / 2048 + 1;
    step = sectors / 4;

    for (i = 0; i < sectors; i++)
    {
/*    	if (i % step == 0)
    	{
    		progress = i / step;
    		if (progress >= 3) progress = 3;
    		ShowOSD_WriteFlash(progress);
    	}
*/
		readsize = UpReadData_ram(Data, 2048);
		if (readsize == 0)
		{
			UPPRINTF("Fatal Error: UpReadData return 0\r\n");
		}
		
#if (_FLASH_TYPE_ == FLASH_x8x16_16BIT_BUS)
		awData = (WORD*)(Data);
#else
		awData = (BYTE*)(Data);
#endif
    
    	if (Write2flash(StartAddr, awData) != TRUE)
    	{
    		for (j = 0; j < 10; j++)
    		{
    			if (Write2flash(StartAddr, awData) == TRUE) break;    				
    		}
    		if (j >= 0) return FALSE;
    	}
    	
		if (((i+1)*100/sectors) != iLastProgress)
		{
			iLastProgress = (i+1)*100/sectors;
			ShowProgress(iLastProgress);
		}

#if (_FLASH_TYPE_ == FLASH_x8x16_16BIT_BUS)
		StartAddr += 1024;
#else
		StartAddr += 2048;
#endif

    }
    return TRUE;

}


/*****************************************************************************
 Function	 : Write2flash()
 Description  : Write2flash
 Arguments	: None
 Return value : C_TRUE -> success
				C_FAIL -> fail
*****************************************************************************/
#if (_FLASH_TYPE_ == FLASH_x8x16_16BIT_BUS)
int Write2flash(unsigned long StartAddr, LPWORD awData)
#else
int Write2flash(unsigned long StartAddr, LPBYTE awData)
#endif
{
    int ret;

//for test
//	delay_u(20);
//	return TRUE;

   	ret = Write2M29F800(StartAddr,awData);
	return ret;
}

/*****************************************************************************
 Function	 : Write2M29F800()
 Description  : Write2M29F800
 Arguments	: None
 Return value : C_TRUE -> success
		C_FAIL -> fail
*****************************************************************************/
#if (_FLASH_TYPE_ == FLASH_x8x16_8BIT_BUS)||(_FLASH_TYPE_ == FLASH_x8_8BIT_BUS)
int Write2M29F800(unsigned long StartAddr, LPBYTE awData)
{
    int iRV, i, count;
    BYTE TempWord;


    for (count = 0;count < 5;count++)
    {
        //Dickma 1217, don't use DisInt here, sometime cause WatchDog timer out
        //DisInt();
        iRV = FlashProgram(StartAddr, 2048, awData);
        //EnInt();
        if (iRV == FLASH_SUCCESS)
            break;
        FlashPause(0x10); /*time overhead*/

    }
    if (iRV != FLASH_SUCCESS)
    {
        if (iRV < 0)
            EPRINTF("Program %s\n", FlashErrorStr(iRV));
        else
            EPRINTF("Block %d is protected!\n", iRV);
        return FALSE;
    }
    FlashReadReset();
    for (count = 0;count < 5;count++)
    {
        iRV = TRUE;
        for (i = 0;i < 2048;i++)
        {
            TempWord = FlashRead(StartAddr + i);
            if (awData[i] != TempWord)
            {
                iRV = FALSE;
                break;
            }
        }
        if (iRV == TRUE)
            return TRUE;
    }
    return FALSE;
}
#else
int Write2M29F800(unsigned long StartAddr, LPWORD awData)
{
    int iRV, i, count;
    WORD TempWord;

    for (count = 0;count < 5;count++)
    {
        //Dickma 1217, don't use DisInt here, sometime cause WatchDog timer out
        //DisInt();
        iRV = FlashProgram(StartAddr, 1024, awData);
        //EnInt();
        if (iRV == FLASH_SUCCESS)
            break;
        FlashPause(0x10); /*time overhead*/
        PRINTF("program flash address 0x%x %d times!\n", StartAddr, count + 2);
    }
    if (iRV != FLASH_SUCCESS)
    {
        if (iRV < 0)
            PRINTF("Program %s\n", FlashErrorStr(iRV));
        else
            PRINTF("Block %d is protected!\n", iRV);
        return FALSE;
    }
    FlashReadReset();
    for (count = 0;count < 5;count++)
    {
        iRV = TRUE;
        for (i = 0;i < 1024;i++)
        {
            TempWord = FlashRead(StartAddr + i);
            if (awData[i] != TempWord)
            {
                iRV = FALSE;
                break;
            }
        }
        if (iRV == TRUE)
            return TRUE;
    }
    return FALSE;
}
#endif


//peter 0307 for whadh
#if 0
int FlashAutoSelect_ID(void)
{
    BYTE temp_ret,temp_times,temp_read[3];

    for(temp_times=0;temp_times<3;temp_times++)
    {
        FlashWrite( 0xAAAL, 0xAA ); /* 1st Cycle */
        FlashWrite( 0x555L, 0x55 ); /* 2nd Cycle */
        FlashWrite( 0xAAAL, 0x90 ); /* 3rd Cycle */

        temp_read[temp_times]=(int) FlashRead( 0x0000L );
        FlashWrite( ANY_ADDR, 0xF0 );
    }
    if( (temp_read[0]==temp_read[1])&&(temp_read[1]==temp_read[2]) )
        temp_ret = temp_read[0]; //read 3 times to confirm FlashManufacturerID
    else
        temp_ret = 0x0;
    if(temp_ret==0xc2) // for Mxic or PM29f004
    {
        FlashManufacturerID=0xc2;

        FlashWrite( 0xAAAL, 0xAA ); /* 1st Cycle */
        FlashWrite( 0x555L, 0x55 ); /* 2nd Cycle */
        FlashWrite( 0xAAAL, 0x90 ); /* 3rd Cycle */

        temp_ret = (int) FlashRead( (0x0001L)*2 ); //jeff
        FlashDeviceID=temp_ret;
        FlashWrite( ANY_ADDR, 0xF0 );
    }
    else if(temp_ret==0x04) // for Fujisu
    {
        FlashManufacturerID=0x04;

        FlashWrite( 0x555L, 0xAA ); /* 1st Cycle */
        FlashWrite( 0x2AAL, 0x55 ); /* 2nd Cycle */
        FlashWrite( 0x555L, 0x90 ); /* 3rd Cycle */

        temp_ret = (int) FlashRead( 0x0001L );
        FlashDeviceID=temp_ret;
        FlashWrite( ANY_ADDR, 0xF0 );
    }
    else if(temp_ret==0x37) // for AMIC
    {
        FlashManufacturerID=0x37;

        FlashWrite( 0x555L, 0xAA ); /* 1st Cycle */
        FlashWrite( 0x2AAL, 0x55 ); /* 2nd Cycle */
        FlashWrite( 0x555L, 0x90 ); /* 3rd Cycle */

        temp_ret = (int) FlashRead( 0x0001L );
        FlashDeviceID=temp_ret;
        FlashWrite( ANY_ADDR, 0xF0 );
    }
    else if(temp_ret==0x01) // for AMD
    {
        FlashManufacturerID=0x01;

        FlashWrite( 0x555L, 0xAA ); /* 1st Cycle */
        FlashWrite( 0x2AAL, 0x55 ); /* 2nd Cycle */
        FlashWrite( 0x555L, 0x90 ); /* 3rd Cycle */

        temp_ret = (int) FlashRead( 0x0001L );
        FlashDeviceID=temp_ret;
        FlashWrite( ANY_ADDR, 0xF0 );
    }
    else //for sst39sf040 or w29c040
    {}
}
#endif

/*
void delay_pice_u( UINT time )
{
    UINT i;

    // It's about 46.296ns for 1 loop in M6311
    for ( i = 0; i < time; i++ )
        ;
}

void delay_u( int t )
{
    int i;

    for (i = 0; i < t; i++)
        delay_pice_u(21600);
}
*/

void clear_catch()
{
	asm volatile(".set noreorder
	li		$8, 0
	li		$9, 0			    	# current line number
	li      $10, 255				# max line number
	clear_catch_loop:
	.set mips3
	cache   1, 0($8)		    	# CacheOP instr:
					    			# index_writeback_invalidate
	cache   1, 0($8)                # do two times to invalidate
					    			## 2 sets of D-cache
	.set mips1
	addiu	$8, $8, 16		    	# next cache line
	blt     $9, $10, clear_catch_loop  # if current line no. < 256
	addiu	$9, $9, 1		    	# increment current line no.
	.set    reorder				    				    							
	"
	::);
	
}
//#endif	//

//
//debug for UPGRADE_FLASH_MODE
//
#if 0
int UPPRINTF(const char *fmt)
{
	char c;
	while(c = *fmt++)
	{
		upoutbyte(c);
	}
} 


/* Address for CSCI of M6311 */
#define CRDR            ((volatile unsigned int *)0xFF103000)
#define CTDR            ((volatile unsigned int *)0xFF103004)
#define CSCR            ((volatile unsigned int *)0xFF10300C)
#define CSSR            ((volatile unsigned int *)0xFF103010)
#define CBBR            ((volatile unsigned int *)0xFF103014)

/* bit fields in CSSR register */
#define TDRE            0x80
#define RDRF            0x40
#define ORER            0x20
#define FER             0x10
#define PER             0x08
#define TEND            0x04

int upoutbyte(char ch)
{
    unsigned int c=(unsigned int) ch;
    unsigned int    status = 0;

    /* send character */
    *(CTDR) = c;
    *(CSSR) = 0xff & (~TDRE);       /* clear TDRE bit, start transmitting */
    *(CSSR) = 0xff & (~TDRE);       /* clear TDRE bit, start transmitting */

    /* wait for transmission finished */
    while (!(status & TEND))
    {
        status = *(CSSR);
    }

    *(CSSR) = 0xff & (~TEND);       /* clear TEND bit */

    return 1;
}
#endif



⌨️ 快捷键说明

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