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

📄 flashpgm.c

📁 liunx下通过jtag烧写flash的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
		tmp_ptr++;	    	    sst_write_word( addr, tmp );		    //prepare for next write	    size = size - 2;	    addr = addr + 2;	    //count the program percent	    if( size < percent * ( 10 - i ) ) {		printf( "finished programing %d %%\n ", i*10 );		i++;	    } 	}	printf( "\n" );		if( size == 0 )		return 0;		//write the last byte	ch = *( (unsigned char *)tmp_ptr );	tmp = sst_read_word( addr );	tmp = tmp & ch;		sst_write_word( addr, tmp );	return 0;}/*----------------------------------------------------------------	 * brief : 	save the flash data to the bin file * author:	guojian * param:	dst------point to the buffer 			addr-----the first read address(0x0~0x1fffff) 			size-----read size * retval:	0----------succeed 			-1---------error * modify history:	if you change the function please give the discription-----------------------------------------------------------------*/int flash_read( unsigned char *dst, unsigned int addr, unsigned int size ){	int break_point;	unsigned short  tmp;	unsigned short  *tmp_ptr;	unsigned char	ch;	int i=0;	int percent = size/10;		//write first byte if addr is odd	if( addr % 2 ) {	    tmp = sst_read_word( addr - 1 );	    *dst = tmp >> 8 ;	    dst++;	    size--;	    addr++;	}		//if read size is odd	if( size % 2 )	    break_point = 1;	else	    break_point = 0;		//very time we read 2 byte	tmp_ptr = (unsigned short *)dst;	while( size != break_point ) {		//read from flash		tmp = sst_read_word( addr );		*tmp_ptr = tmp;		tmp_ptr++;	    	    //prepare for next write	    size = size - 2;	    addr = addr + 2;	    //count the read percent	    if( size < percent * ( 10 - i ) ) {		printf( "finished reading %d %%\n ", i*10 );		i++;	    } 	}	printf( "\n" );		if( size == 0 )		return 0;		//read the last byte	tmp = sst_read_word( addr );	*( (unsigned char *)tmp_ptr ) = tmp & 0xff;	return 0;}/*----------------------------------------------------------------	 * brief : 	verify the program procedure * author:	guojian * param:	src------point to the verified data 			addr-----the first write address(0x0~0x1fffff) 			size-----verifed size * retval:	0----------succeed 			-1---------error * modify history:	if you change the function please give the discription-----------------------------------------------------------------*/int flash_verify( unsigned char *src, unsigned int addr, unsigned int size ){	int break_point;	unsigned short  tmp;	unsigned short  *tmp_ptr;	unsigned char	ch;	int i=0;	int percent = size/10;		//verify first byte if addr is odd	if( addr % 2 ) {	    tmp = sst_read_word( addr - 1 );	    ch  = *src;	    src++;	    	    //equal means program right	    if( (tmp >> 8) != ch )	        return -1;	    	    size--;	    addr++;	}		//if read size is odd	if( size % 2 )	    break_point = 1;	else	    break_point = 0;		//very time we verify 2 byte	tmp_ptr = (unsigned short *)src;	while( size != break_point ) {		//read from bin file		tmp = *tmp_ptr;		tmp_ptr++;	    	    if( sst_read_word( addr ) != tmp )	        return -1;		    //prepare for next write	    size = size - 2;	    addr = addr + 2;	    //count the verify percent	    if( size < percent * ( 10 - i ) ) {		printf( "finished verifing %d %%\n ", i*10 );		i++;	    } 	}	printf( "\n" );		if( size == 0 )		return 0;		//verify the last byte	ch = *( (unsigned char *)tmp_ptr );	tmp = sst_read_word( addr );	tmp &= 0xff;		if( tmp != ch )	    return -1;	return 0;}/*----------------------------------------------------------------	 * brief : 	erase sectors * author:	guojian * param:	none * retval:	none * modify history:		2003-12-3		Guo Jian 		first version-----------------------------------------------------------------*/void erase_sectors( void ){    unsigned int sector_addr, sector_count;    int          i;        printf( "please input the first erased sector num( 0~511 )\n" );    scanf( "%d", &sector_addr );        printf( "please input the erased sector size\n" );    scanf( "%d", &sector_count );        //health check    if( sector_addr > 512 || sector_addr < 0 ) {        printf( "bad sector number\n" );        return;    }        //change sector num to address    sector_addr = sector_addr * 4096;        //check overflow    if( ( sector_addr + sector_count * 4096 ) > FLASH_SIZE ) {        printf( "erase the space out of the flash\n" );        return;    }        //erase    for( i = 0; i < sector_count; i++ )        sst_erase_one_sector( sector_addr + i*4096 );        return;}/*----------------------------------------------------------------	 * brief : 	erase blocks * author:	guojian * param:	none * retval:	none * modify history:		2003-12-3		Guo Jian 		first version-----------------------------------------------------------------*/void erase_blocks( void ){    unsigned int block_addr, block_count;    int          i;        printf( "please input the first erased block num( 0~511 )\n" );    scanf( "%d", &block_addr );        printf( "please input the erased block count\n" );    scanf( "%d", &block_count );        //health check    if( block_addr > 512 || block_addr < 0 ) {        printf( "bad block number\n" );        return;    }        //change block num to address    block_addr = block_addr * 64 * 1024;        //check overflow    if( ( block_addr + block_count * 64 * 1024 ) > FLASH_SIZE ) {        printf( "erase the space out of the flash\n" );        return;    }        //erase    for( i = 0; i < block_count; i++ )        sst_erase_one_block( block_addr + i* 64 * 1024 );        return;}/*----------------------------------------------------------------	 * brief : 	main entry function * author:	guojian * param:	none * retval:	none * modify history:		2003-12-3		Guo Jian 		first version-----------------------------------------------------------------*/int main( int argc, char **argv ){	int fd;	unsigned char press_key;		// find a valid parallel port	if( ( lpt_address = test_port() ) == -1 ) {		printf( "can not find the parallel\n" );		exit( 1 );	}	printf( "LPT is at %04x\n", lpt_address );		//check the cpu id	reset_jtag();	test_logic_reset();	if( id_command() == -1 ) {		printf( "wrong cpu id\n" );		exit( 2 );	}		//check flash id	extest();		//select boundary scan register.	if( sst_check_id() ==  -1 ) {   	    printf( "SST39VF160 Chip Not Founded!!\n" );	    exit( 3 );	}else	    printf( "SST39VF160 Chip Founded!!\n" );		while( 1 ) {				//show the hint		show_menu();				//get the user input		while( 1 ) {			press_key = getchar();			if( press_key < 30 || press_key > 128 )				continue;			else				break;		}				switch( press_key ) {			case '1':				load_binfile();				break;			case '2':				sst_erase_chip();				break;			case '3':				erase_sectors();				break;			case '4':				erase_blocks();				break;			case '5':				save_binfile();				break;			case 'x':				exit( 0 );			default:				printf( "not support now\n" );				break;		}//switch( press_key )	}//while( 1 )            io_access_off( lpt_address );    exit( 0 );}

⌨️ 快捷键说明

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