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

📄 avrinsystemprog.cpp

📁 一个开放源代码的AVR单片机编程器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		data->setData( address+1, comm->getByte() ); // High byte.
		data->setData( address, comm->getByte() ); // Low byte.

		address += 2;

		if( (address % MEM_PROGRESS_GRANULARITY) == 0 )
			Util.progress( "#" ); // Advance progress indicator.

	} while( address < end );

	/* Need to read one even byte before finished? */
	if( address == end )
	{
		/* Read both, but use only low byte */
		comm->sendByte( 'R' );
		comm->flushTX();

		comm->getByte(); // Dont use high byte.
		data->setData( address-1, comm->getByte() ); // Low byte.
	}

	Util.progress( "\r\n" ); // Finish progress indicator.
	return true; // Indicate supported command.
}


bool AVRInSystemProg::writeEEPROM( HEXFile * data )
{
	long start, end; // Data address range.
	bool autoincrement; // Bootloader supports address autoincrement?
	long address;

	/* Get range from HEX file */
	start = data->getRangeStart();
	end = data->getRangeEnd();

	/* Check autoincrement support */
	comm->sendByte( 'a' );
	comm->flushTX();

	if( comm->getByte() == 'Y' )
		autoincrement = true;
	else
		autoincrement = false;

	/* Set initial address */
	setAddress( start );

	/* Send data */
	address = start;
	do
	{
		/* Need to set address again? */
		if( !autoincrement )
			setAddress( address );

		/* Send byte */
		comm->sendByte( 'D' );
		comm->sendByte( data->getData( address ) );
		comm->flushTX();

		if( comm->getByte() != '\r' )
			throw new ErrorMsg( "Writing byte to EEPROM failed! "
					"Programmer did not return CR after 'D'-command." );

		if( (address % MEM_PROGRESS_GRANULARITY) == 0 )
			Util.progress( "#" ); // Advance progress indicator.

		address++;
	} while( address <= end );

	Util.progress( "\r\n" ); // Finish progress indicator.
	return true; // Indicate supported command.
}


bool AVRInSystemProg::readEEPROM( HEXFile * data )
{
	long start, end; // Data address range.
	bool autoincrement; // Bootloader supports address autoincrement?
	long address;

	/* Get range from HEX file */
	start = data->getRangeStart();
	end = data->getRangeEnd();

	/* Check autoincrement support */
	comm->sendByte( 'a' );
	comm->flushTX();

	if( comm->getByte() == 'Y' )
		autoincrement = true;
	else
		autoincrement = false;

	/* Set initial address */
	setAddress( start );

	/* Send data */
	address = start;
	do
	{
		/* Need to set address again? */
		if( !autoincrement )
			setAddress( address );

		/* Get byte */
		comm->sendByte( 'd' );
		comm->flushTX();

		data->setData( address, comm->getByte() );		

		if( (address % MEM_PROGRESS_GRANULARITY) == 0 )
			Util.progress( "#" ); // Advance progress indicator.

		address++;
	} while( address <= end );

	Util.progress( "\r\n" ); // Finish progress indicator.
	return true; // Indicate supported command.
}


bool AVRInSystemProg::writeLockBits( long bits )
{
	/* Use AVRISP's 4-byte universal command */
	comm->sendByte( '.' );
	comm->sendByte( 0xac );
	comm->sendByte( 0xe0 );
	comm->sendByte( 0x00 ); // Dummy.
	comm->sendByte( bits );

	comm->flushTX();
	comm->getByte(); // Ignore return code from SPI communication.

	if( comm->getByte() != '\r' ) // Check return code from command.
		throw new ErrorMsg( "Writing lock bits failed! "
				"Programmer did not return CR after '.'-command." );

	return true; // Indicate supported command.
}


bool AVRInSystemProg::readLockBits( long * bits )
{
	/* Use AVRISP's 4-byte universal command */
	comm->sendByte( '.' );
	comm->sendByte( 0x58 );
	comm->sendByte( 0x00 );
	comm->sendByte( 0x00 ); // Dummy.
	comm->sendByte( 0x00 ); // Dummy.
	comm->flushTX();

	*bits = comm->getByte();

	if( comm->getByte() != '\r' ) // Check return code from command.
		throw new ErrorMsg( "Lock byte readout failed! "
				"Programmer did not return CR after '.'-command." );

	return true; // Indicate supported command.
}


bool AVRInSystemProg::writeFuseBits( long bits )
{
	/* Use AVRISP's 4-byte universal command */
	comm->sendByte( '.' );
	comm->sendByte( 0xac );
	comm->sendByte( 0xa0 );
	comm->sendByte( 0x00 ); // Dummy.
	comm->sendByte( bits & 0xff );
	comm->flushTX();

	comm->getByte(); // Ignore return code from SPI communication.

	if( comm->getByte() != '\r' ) // Check return code from command.
		throw new ErrorMsg( "Low fuse byte programming failed! "
				"Programmer did not return CR after '.'-command." );

	/* Use AVRISP's 4-byte universal command */
	comm->sendByte( '.' );
	comm->sendByte( 0xac );
	comm->sendByte( 0xa8 );
	comm->sendByte( 0x00 ); // Dummy.
	comm->sendByte( bits >> 8 );
	comm->flushTX();

	comm->getByte(); // Ignore return code from SPI communication.

	if( comm->getByte() != '\r' ) // Check return code from command.
		throw new ErrorMsg( "High fuse byte programming failed! "
				"Programmer did not return CR after '.'-command." );

	return true; // Indicate supported command.
}


bool AVRInSystemProg::readFuseBits( long * bits )
{
	long low, high;

	/* Use AVRISP's 4-byte universal command */
	comm->sendByte( '.' );
	comm->sendByte( 0x50 );
	comm->sendByte( 0x00 );
	comm->sendByte( 0x00 ); // Dummy.
	comm->sendByte( 0x00 ); // Dummy.
	comm->flushTX();

	low = comm->getByte();

	if( comm->getByte() != '\r' ) // Check return code from command.
		throw new ErrorMsg( "Low fuse byte readout failed! "
				"Programmer did not return CR after '.'-command." );

	/* Use AVRISP's 4-byte universal command */
	comm->sendByte( '.' );
	comm->sendByte( 0x58 );
	comm->sendByte( 0x08 );
	comm->sendByte( 0x00 ); // Dummy.
	comm->sendByte( 0x00 ); // Dummy.
	comm->flushTX();

	high = comm->getByte();

	if( comm->getByte() != '\r' ) // Check return code from command.
		throw new ErrorMsg( "Low fuse byte readout failed! "
				"Programmer did not return CR adter '.'-command." );

	/* Put low and high together */
	*bits = (high << 8) | low;

	return true; // Indicate supported command.
}


bool AVRInSystemProg::writeExtendedFuseBits( long bits )
{
	/* Use AVRISP's 4-byte universal command */
	comm->sendByte( '.' );
	comm->sendByte( 0xac );
	comm->sendByte( 0xa4 );
	comm->sendByte( 0x00 ); // Dummy.
	comm->sendByte( bits );
	comm->flushTX();

	comm->getByte(); // Ignore return code from SPI communication.

	if( comm->getByte() != '\r' ) // Check return code from command.
		throw new ErrorMsg( "Extended fuse byte programming failed! "
				"Programmer did not return CR after '.'-command." );

	return true; // Indicate supported command.
}


bool AVRInSystemProg::readExtendedFuseBits( long * bits )
{
	/* Use AVRISP's 4-byte universal command */
	comm->sendByte( '.' );
	comm->sendByte( 0x50 );
	comm->sendByte( 0x08 );
	comm->sendByte( 0x00 ); // Dummy.
	comm->sendByte( 0x00 ); // Dummy.
	comm->flushTX();

	*bits = comm->getByte();

	if( comm->getByte() != '\r' ) // Check return code from command.
		throw new ErrorMsg( "Extended fuse byte readout failed! "
				"Programmer did not return CR after '.'-command." );

	return true; // Indicate supported command.
}


bool AVRInSystemProg::programmerSoftwareVersion( long * major, long * minor )
{
	/* Send command 'V' to get software version */
	comm->sendByte( 'V' );
	comm->flushTX();

	/* Get data */
	*major = comm->getByte();
	*minor = comm->getByte();

	return true; // Indicate supported command.
}


bool AVRInSystemProg::programmerHardwareVersion( long * major, long * minor )
{
	/* Send command 'v' to get hardware version */
	comm->sendByte( 'v' );
	comm->flushTX();

	/* Get data */
	*major = comm->getByte();
	*minor = comm->getByte();

	return true; // Indicate supported command.
}


void AVRInSystemProg::setAddress( long address )
{
	/* Set current address */
	comm->sendByte( 'A' );
	comm->sendByte( address >> 8 ); // High byte of address.
	comm->sendByte( address & 0xff ); // Low byte.
	comm->flushTX();

	/* Should return CR */	
	if( comm->getByte() != '\r' )
		throw new ErrorMsg( "Setting address for programming operations failed! "
				"Programmer did not return CR after 'A'-command." );
}


void AVRInSystemProg::writeFlashLowByte( long value )
{
	comm->sendByte( 'c' );
	comm->sendByte( value );
	comm->flushTX();

	if( comm->getByte() != '\r' )
		throw new ErrorMsg( "Writing Flash low byte failed! "
				"Programmer did not return CR after 'c'-command." );
}


void AVRInSystemProg::writeFlashHighByte( long value )
{
	comm->sendByte( 'C' );
	comm->sendByte( value );
	comm->flushTX();

	if( comm->getByte() != '\r' )
		throw new ErrorMsg( "Writing Flash high byte failed! "
				"Programmer did not return CR after 'C'-command." );
}


void AVRInSystemProg::writeFlashPage()
{
	comm->sendByte( 'm' );
	comm->flushTX();

	if( comm->getByte() != '\r' )
		throw new ErrorMsg( "Writing Flash page failed! "
				"Programmer did not return CR after 'm'-command." );
}


/* end of file */

⌨️ 快捷键说明

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