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

📄 flash.c

📁 CCS2.0以上版本下的一个烧写Flash的源代码(flash_burner)!
💻 C
字号:
/***************************************************************************
 * This file define routines for operating SSTVF160 flash.
 *
 * Author:  Liu zengming
 * Date:    2001/9/16
 *
 * Adapted to accomodate AM29LV160DB on OMAP1510DC EVM
 * By Zhu, Yaozong
 * Date:    2002/7/28
 *
 * Adapted to accomodate Intel DT28F160 F3T95 device on OMAP1510DC EVM
 * By Zhu, Yaozong
 * Date:    2002/9/12
 *
 * Adapted to accomodate Intel E28F128 J3A150 device on OMAP1510DC EVM
 * By Zhu, Yaozong
 * Date:	2002/9/16
 * Adapted to accomodate Intel E28F320 J3A110 device on OMAP1510 EVM
 * By Zhu, Yaozong
 * Date:	2002/9/16
 **************************************************************************/

/*
	ChipWrite copy data from address source to address target, with length words(16-bit);
	Must make sure that [target, target+length) is a subset of the memory space mapped to
	the TARGET chip!
	
	Warning: Will erase all the old data on the blocks that [target, target + length) touches
	
 */
int ChipWrite(unsigned short* chipstart, unsigned short* source, volatile unsigned short* target, unsigned int length) {
	volatile unsigned short* ps, sdata;
	unsigned int i, j;


	printf("Burning flash: writing %d bytes from address 0x%x\n to address 0x%x\n", length*2, (unsigned int)source, (unsigned int)target);
	
	/* Make sure the parameters are valid */
	if((unsigned int)target % 16)
		return -1;
	if(!(target >= chipstart && 
			target < (chipstart + 8*1024*1024) && 
				(target + length) <= (chipstart + 8*1024*1024)))
		return -1;
	if(((unsigned int)chipstart)%2 || ((unsigned int)chipstart)%2 || ((unsigned int)chipstart)%2)
		return -1;
				
	i = ((unsigned int)target - (unsigned int)chipstart)/(128*1024);
	j = ((unsigned int)target - (unsigned int)chipstart + length*2 - 2)/(128*1024);			
	/* Block erasures */
	/* Erase the 128 main blocks */
	printf("Erase flash first:\n");

	//for(ps = chipstart + i*64*1024; ps < chipstart + (j+1)*64*1024; ps += 64*1024) {
	for(ps = chipstart + i*64*1024; i <= j; i++, ps += 64*1024) {
BLKErase:
		*ps = 0x20; // Block erase command -- block erase setup
		*ps	= 0xd0; // Block erase command -- block erase confirm
		while(!((sdata = *ps) & 128)); // Status register is read after block erase
									// bit 7: write state machine status -- 1: ready 0: busy
		if(sdata & (1<<5)) {// Status register bit 5: Erase status  1: Error in block erasure 0: Success
			*ps = 0x70; // Read Status
			sdata = *ps; // Bit 4: Program status 1: Error in program 0: Success
							// Bit 3: Vpp status 1: Vpp Low detect, operation abort 0: Vpp OK
							// Bit 1: Device protect status 1: Block erase or program attempted on a locked
							//		block, operation aborted 0: Unlocked
							// Bit 0: Reserved
			*ps = 0x50; // Clear status register command
						// Read array mode is entered after clear status command
			*ps = 0xff; // Read array command
			
			if(sdata&0x02) {
				printf("Block locked. Now unlock chip ...\n");
				*ps = 0x60;
				*ps = 0xd0;
				while(!((sdata = *ps) & 128));
				*ps = 0x70;
				sdata = *ps;
				if(!(sdata&(1<<5))) {
					printf("Chip unlocked!\n");
					goto BLKErase;
				}
			}
			
			for(;;);
		}
	}

	printf("Flash erasion finished.\n");
	
	i = (length + 15)/16;

	for(j = 0; j < length; j++, target++, source++) {
		if(j&&(!(j&0xffff))) {
			printf("Another 128KB burned, %dKB left...\n", (length - j)/512);
		}
		*target = 0x40; // Program setup command
		*target = *source;
		while(!((sdata = *target) & 128));
	
		if(sdata & (1<<4)) {// Status register bit 5: Erase/clear lock-bit status  1: Error 0: Success
			*target = 0x70; // Read Status
			sdata = *target; // Bit 4: Program/Set lock-bit status 1: Error 0: Success
							// Bit 3: Programming voltage status 1: Low programming voltage detected, operation aborted 0: voltage ok
							// Bit 2: Program suspend status -- 1: Program suspended 0: Program in progress/completed
							// Bit 1: Device protect status 1: Block lock-bit detected, operation aborted 0: Unlock
							// Bit 0: Reserved for future use
			*target = 0x50; // Clear status register command
						// Read array mode is entered after clear status command
			*target = 0xff;			
		}
		*target = 0xff;
		
	}
			
	*chipstart = 0xff; // Read array command;
	
	printf("Work done!\n");
	return 0;                                                                
	
}

#define SOURCE_ADDRESS 0x10400000

#define FLASH_ADDRESS 0x00800000
#define IMAGE_SIZE_IN_BYTE 512

int main(void)
{   
	int retval;
	unsigned short *ps, sdata;

	(*(int*)0xFFFECC0C) = 0x00000001;  /* zyz, Sep 12, 2002 Pull up the nFWP signal to enable flash erase/write */	
	ps = (unsigned short*)FLASH_ADDRESS; // The first chip 0x0c000000-0x0c200000
	sdata = *ps;

	*ps = 0x90;
	sdata = *(ps);
	sdata = *(ps + 1);
	sdata = *(ps + 2);
	*ps = 0xff; // Read array command
	*ps = 0x98; // Read Query command
	sdata = *(ps + 0x10); // Should be 0x0051 -- 'Q'
	if(sdata == 'Q') {
		sdata = *(ps + 0x11); // Should be 0x0052 -- 'R'
		if(sdata == 'R') {
			sdata = *(ps + 0x12); // Should be 0x0059 -- 'Y'
			if(sdata == 'Y') {
				printf("Flash chip OK! Going to burning...\n");
				goto ChipOK;
			}
		}
	}
	printf("Flash chip not OK! Now exit.\n");
	exit(1);
ChipOK:
	*ps = 0xff; // Read array command


	retval = ChipWrite((unsigned short*)(FLASH_ADDRESS), 
				(unsigned short*)(SOURCE_ADDRESS),
				 (unsigned short*)(FLASH_ADDRESS), (IMAGE_SIZE_IN_BYTE/2));

	return retval;
}

⌨️ 快捷键说明

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