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

📄 jobinfo.cpp

📁 一个开放源代码的AVR单片机编程器
💻 CPP
📖 第 1 页 / 共 3 页
字号:
						readOSCCAL = true;
						try
						{
							OSCCAL_Parameter = convertHex( param + 2 );
						}
						catch( ErrorMsg * e )
						{
							delete e;
							throw new ErrorMsg( "Hex number format error for -O parameter!" );
						}

						break;


					case 5 : // Direct value specified.
						if( param[2] != '#' )
							throw new ErrorMsg( "Use one or two hex digits for -O and two for -O#!" );

						readOSCCAL = false;
						try
						{
							OSCCAL_Parameter = convertHex( param + 3 );
						}
						catch( ErrorMsg * e )
						{
							delete e;
							throw new ErrorMsg( "Hex number format error for -O# parameter!" );
						}

						break;

					default:
						throw new ErrorMsg( "Invalid use of -O or -O# parameter!" );
				}

				break;


			case 'p' : // Program data.
				if( strlen( param ) != 3 )
					throw new ErrorMsg( "Specify memory type, not just -p!" );

				switch( param[2] )
				{
					case 'f' : // Program Flash memory.
						programFlash = true;
						break;

					case 'e' : // Program EEPROM memory.
						programEEPROM = true;
						break;

					case 'b' : // Both.
						programFlash = true;
						programEEPROM = true;
						break;

					default:
						throw new ErrorMsg( "Unknown choice for -p, use -pf, -pe or -pb!" );
				}

				break;


			case 'q' : // Read all fuse bits.
				if( strlen( param ) != 2 )
					throw new ErrorMsg( "Parameter -q needs no extra arguments!" );

				readFuseBits = true;
				break;


			case 'r' : // Read data.
				if( strlen( param ) != 3 )
					throw new ErrorMsg( "Specify memory type, not just -r!" );

				switch( param[2] )
				{
					case 'f' : // Read Flash memory.
						readFlash = true;
						break;

					case 'e' : // Read EEPROM memory.
						readEEPROM = true;
						break;

					case 'b' : // Both.
						readFlash = true;
						readEEPROM = true;
						break;

					default:
						throw new ErrorMsg( "Unknown choice for -r, use -rf, -re or -rb!" );
				}

				break;


			case 's' : // Read signature byte.
				if( strlen( param ) != 2 )
					throw new ErrorMsg( "Parameter -s needs no extra arguments!" );

				readSignature = true;
				break;


			case 'S' : // Write OSCCAL byte to memory.
				if( strlen( param ) <= 2 )
					throw new ErrorMsg( "Cannot use -S without memory type!" );

				if( strlen( param ) <= 3 )
					throw new ErrorMsg( "Cannot use -S without byte address!" );

				switch( param[2] )
				{
					case 'f' : // Write to Flash address.
						try
						{
							OSCCAL_FlashAddress = convertHex( param + 3 );
						}
						catch( ErrorMsg * e )
						{
							delete e;
							throw new ErrorMsg( "Cannot convert hex number for -Sf parameter!" );
						}
						break;

					case 'e' : // Write to EEPROM address.
						try
						{
							OSCCAL_EEPROMAddress = convertHex( param + 3 );
						}
						catch( ErrorMsg * e )
						{
							delete e;
							throw new ErrorMsg( "Cannot convert hex number for -Se parameter!" );
						}
						break;

					default:
						throw new ErrorMsg( "Unknown choice for -S, use -Sf or -Se!" );
				}

				break;


			case 'v' : // Verify data.
				if( strlen( param ) != 3 )
					throw new ErrorMsg( "Specify memory type, not just -v!" );

				switch( param[2] )
				{
					case 'f' : // Verify Flash memory.
						verifyFlash = true;
						break;

					case 'e' : // Verify EEPROM memory.
						verifyEEPROM = true;
						break;

					case 'b' : // Both.
						verifyFlash = true;
						verifyEEPROM = true;
						break;

					default:
						throw new ErrorMsg( "Unknown choice for -v, use -vf, -ve or -vb!" );
				}

				break;


			case 'x' : // Fill unspecified memory.
				if( strlen( param ) != 4 )
					throw new ErrorMsg( "Use two hex digits for the -x parameter!" );

				try
				{
					memoryFillPattern = convertHex( param + 2 );
				}
				catch( ErrorMsg * e )
				{
					delete e;
					throw new ErrorMsg( "Hex number format error for -x parameter!" );
				}

				break;


			case 'y' : // Read lock bits.
				if( strlen( param ) != 2 )
					throw new ErrorMsg( "Parameter -y needs no extra arguments!" );

				readLockBits = true;
				break;


			case 'z' : // No progress indicator?
				if( strlen( param ) != 2 )
					throw new ErrorMsg( "Parameter -z needs no extra arguments!" );

				noProgressIndicator = true;
				break;


			default:
				throw new ErrorMsg( "Unknow parameter!" );
		}
	}
}


void JobInfo::help()
{
	cout
		<< "Command Line Switches:" << endl
		<< "        [-d device name] [-if infile] [-ie infile] [-of outfile] [-oe outfile]" << endl
		<< "        [-s] [-O index] [-O#value] [-Sf addr] [-Se addr] [-e] [-p f|e|b]" << endl
		<< "        [-r f|e|b] [-v f|e|b] [-l value] [-L value] [-y] [-f value] [-E value]" << endl
		<< "        [-F value] [-G value] [-q] [-x value] [-af start,stop] [-ae start,stop]" << endl
		<< "        [-c port] [-b h|s] [-g] [-z] [-h|?]" << endl
		<< endl
		<< "Parameters:" << endl
		<< "d       Device name. Must be applied when programming the device." << endl
		<< "if      Name of FLASH input file. Required for programming or verification" << endl
		<< "        of the FLASH memory. The file format is Intel Extended HEX." << endl
		<< "ie      Name of EEPROM input file. Required for programming or verification" << endl
		<< "        of the EEPROM memory. The file format is Intel Extended HEX." << endl
		<< "of      Name of FLASH output file. Required for readout of the FLASH memory." << endl
		<< "        The file format is Intel Extended HEX." << endl
		<< "oe      Name of EEPROM output file. Required for readout of the EEPROM" << endl
		<< "        memory. The file format is Intel Extended HEX." << endl
		<< "s       Read signature bytes." << endl;
	getch();
	cout
		<< "O       Read oscillator calibration byte. 'index' is optional." << endl
		<< "O#      User-defined oscillator calibration value." << endl
		<< "Sf      Write oscillator cal. byte to FLASH memory. 'addr' is byte address." << endl
		<< "Se      Write oscillator cal. byte to EEPROM memory. 'addr' is byte address." << endl
		<< "e       Erase device. If applied with another programming parameter, the" << endl
		<< "        device will be erased before any other programming takes place." << endl
		<< "p       Program device; FLASH (f), EEPROM (e) or both (b). Corresponding" << endl
		<< "        input files are required." << endl
		<< "r       Read out device; FLASH (f), EEPROM (e) or both (b). Corresponding" << endl
		<< "        output files are required" << endl
		<< "v       Verify device; FLASH (f), EEPROM (e) or both (b). Can be used with" << endl
		<< "        -p or alone. Corresponding input files are required." << endl
		<< "l       Set lock byte. 'value' is an 8-bit hex. value." << endl
		<< "L       Verify lock byte. 'value' is an 8-bit hex. value to verify against." << endl
		<< "y       Read back lock byte." << endl
		<< "f       Set fuse bytes. 'value' is a 16-bit hex. value describing the" << endl
		<< "        settings for the upper and lower fuse bytes." << endl
		<< "E       Set extended fuse byte. 'value' is an 8-bit hex. value describing the" << endl
		<< "        extend fuse settings." << endl
		<< "F       Verify fuse bytes. 'value' is a 16-bit hex. value to verify against." << endl;
	getch();
	cout
		<< "G       Verify extended fuse byte. 'value' is an 8-bit hex. value to" << endl
		<< "        verify against." << endl
		<< "q       Read back fuse bytes." << endl
		<< "x       Fill unspecified locations with a value (00-ff). The default is" << endl
		<< "        to not program locations not specified in the input files." << endl
		<< "af      FLASH address range. Specifies the address range of operations. The" << endl
		<< "        default is the entire FLASH. Byte addresses in hex." << endl
		<< "ae      EEPROM address range. Specifies the address range of operations." << endl
		<< "        The default is the entire EEPROM. Byte addresses in hex." << endl
		<< "c       Select communication port; 'COM1' to 'COM8'. If this parameter is" << endl
		<< "        omitted the program will scan the COM ports for a programmer." << endl
		<< "b       Get revisions; hardware revision (h) and software revision (s)." << endl
		<< "g       Silent operation." << endl
		<< "z       No progress indicator. E.g. if piping to a file for log purposes, use" << endl
		<< "        this option to avoid the characters used for the indicator." << endl
		<< "h|?     Help information (overrides all other settings)." << endl
		<< endl
		<< "Example:" << endl
		<< "        AVROSP -dATmega128 -ifmyapp.hex -pf" << endl;
}


long JobInfo::convertHex( char * txt )
{
	string t( txt );
	return Util.convertHex( t );
}


void JobInfo::doJob()
{
	long scanCOM;
	SerialPort * com;
	AVRProgrammer * prog;
	AVRDevice * avr;
	string programmerID;
	long sig0, sig1, sig2; // Signature bytes.

	/* Set correct silent and progress indicator status */
	if( silentMode )
	{
		Util.muteLog();
		Util.muteProgress(); // Silent also includes progress indicator.
	}

	if( noProgressIndicator )
		Util.muteProgress();

	/* Application header text */
	Util.log( "AVR Open-source Programmer " VERSIONSTRING " (C) 2004 Atmel Corp.\n\r\n\r" );

	/* Show help screen? */
	if( showHelp )
	{
		help();
		return;
	}

	Util.log( "Serial port timeout set to " TIMEOUTSTRING " sec.\r\n" );

	/* Need to scan for COM port? */
	if( comPort == -1 )
	{
		Util.log( "Scanning COM ports for supported programmer...\n\r" );

		for( scanCOM = 1; scanCOM <= 8; scanCOM++ )
		{
			Util.progress( "COM" + Util.convertLong( scanCOM ) + "...\r\n" );

			try
			{
				/* Try to communicate */
				com = NULL;
				com = new SerialPort( scanCOM, TIMEOUT );
				com->openChannel();
				programmerID = AVRProgrammer::readProgrammerID( com );

				/* Contact! Check ID... Add custom handler signatures here */
				if( programmerID == "AVRBOOT" || programmerID == "AVR ISP" )
				{
					break;
				}

				delete com;
				Util.progress( programmerID + " found - not supported!\r\n" );
			}
			catch( ErrorMsg * e )
			{
				/* No contact on COM port, skip to next */
				if( com != NULL ) delete com;
				delete e;
			}
		}

		/* Exit if no supported programmers found */
		if( scanCOM > 8 )
		{
			Util.log( "No supported programmers found!\r\n" );
			return;
		}

		comPort = scanCOM;

	} else // ... COM port is specified
	{
		/* Try to communicate, errors will propagate to caller */
		com = new SerialPort( comPort, TIMEOUT );
		com->openChannel();
		programmerID = AVRProgrammer::readProgrammerID( com );

		/* Contact! Check ID */
		if( programmerID != "AVRBOOT" && programmerID != "AVR ISP" )
			throw new ErrorMsg( "Programmer not supported!" );
	}

	Util.log( "Found " + programmerID + " on COM" + Util.convertLong( comPort ) + "!\r\n" );

	/* Create programmer interface object, add custom handlers here */
	if( programmerID == "AVRBOOT" )
	{
		prog = new AVRBootloader( com );
	}

	if( programmerID == "AVR ISP" )
	{
		prog = new AVRInSystemProg( com );
	}

	Util.log( "Entering programming mode...\r\n" );
	prog->enterProgrammingMode(); // Ignore return code.

	/* Do device independent operations */
	doDeviceIndependent( prog );

	/* Finished if no device name is specified */
	if( deviceName.size() == 0 )
	{
		Util.log( "Device name not specified!\r\n" );
		return;
	}

	/* Parse XML part description file */
	avr = new AVRDevice( deviceName );
	Util.log( "Parsing XML file for device parameters...\r\n" );
	Util.parsePath( searchpath );
	avr->readParametersFromAVRStudio( searchpath );

	/* Verify that the device signature matches the signature from the XML file */
	avr->getSignature( &sig0, &sig1, &sig2 );
	if( prog->checkSignature( sig0, sig1, sig2 ) )
		Util.log( "Signature matches device!\r\n" );

	/* Do device dependent operations */
	doDeviceDependent( prog, avr );

	/* Clean up */
	Util.log( "Leaving programming mode...\r\n" );
	prog->leaveProgrammingMode(); // Ignore return code.

	delete avr;
	delete prog;
	delete com;
}


void JobInfo::doDeviceIndependent( AVRProgrammer * prog )
{
	long sig0, sig1, sig2; // Signature bytes.
	long minor, major; // Minor and major programmer revision.

	/* Read signature? */
	if( readSignature )
	{
		Util.log( "Reading signature bytes: " );
		if( !prog->readSignature( &sig0, &sig1, &sig2 ) )
			throw new ErrorMsg( "Signature readout is not supported by this programmer!" );

⌨️ 快捷键说明

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