Process to send application code to bootloader

Go To Last Post
24 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

if i use bootloader code of avr109 self programming what are the option i have, in order to flash application code, i mean through which all means i can pass on the application code via uart communication?
 

Enoch Sam

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

Duplicate: https://www.avrfreaks.net/forum/p...

 

yup i posted by mistake in other fourm.... how to delete it?

 

Enoch Sam

Last Edited: Wed. Dec 20, 2017 - 12:05 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The protocol you use to packet up the bytes is completely up to you. Pick something that works for you that is easy to encode/decode and is foolproof. There have been a number of attempts at this AVR109, AVR910 (is it?), STK500, STK500v2 so either just implement one of those (in which case tools for the PC sending end already exist) or, like fboot for example, make up something completely new. (but then you need to write both encoder/sender and receiver/decoder)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

The protocol you use to packet up the bytes is completely up to you. Pick something that works for you that is easy to encode/decode and is foolproof. There have been a number of attempts at this AVR109, AVR910 (is it?), STK500, STK500v2 so either just implement one of those (in which case tools for the PC sending end already exist) or, like fboot for example, make up something completely new. (but then you need to write both encoder/sender and receiver/decoder)

 

okkk clawson...............one more clarification needed, do avr109 bootloader code directly accept intel .Hex file?

Enoch Sam

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your question makes no sense. AVR109 is simply the definition of a packet protocol. Your real question should be "does any software that passes data using the 109 protocol have the ability to read ihex". As it happens perhaps the most famous software that happens to implement AVR109, avrdude.exe, does have the ability to read ihex.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Your question makes no sense. AVR109 is simply the definition of a packet protocol. Your real question should be "does any software that passes data using the 109 protocol have the ability to read ihex". As it happens perhaps the most famous software that happens to implement AVR109, avrdude.exe, does have the ability to read ihex.
Actually, the question does make sense.

The answer is no, but the question made sense.

Using a bootloader involves throwing bytes at a device and getting some others back.

OP was asking whether the thrown bytes could be in Intel hex format.

 

@OP:

Bootloaders are generally not very smart.

Most of the smarts is on the PC side where there is more room.

Nomad could not have been running on an 2017 AVR.

International Theophysical Year seems to have been forgotten..
Anyone remember the song Jukebox Band?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

skeeve wrote:

OP was asking whether the thrown bytes could be in Intel hex format.

exactly this is wat i asked..... thanks for the answer....

Enoch Sam

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

If that was the question do NOT consider putting an ihex decoder into a boot loader. For one it can be more complex than you expect, for another a boot loader is the only bit of the software that MUST be 100% error free on day one and that means keeping it as simple as possible. The reason AVR109 is very popular is that it is a binary protocol and simple to implement. Do your ihex to bin conversion on the PC using standard (usually libbfd based) tools.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

Do your ihex to bin conversion on the PC using standard (usually libbfd based) tools.

okk clawson.... will try the conversion.... thank u....

Enoch Sam

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

can any one tell me the procedure to use terminal to write an application code to atmega328 while using avr109 bootloader,.... ?

Last Edited: Mon. Mar 19, 2018 - 11:08 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Err, you don’t use a terminal program. Avrdude.exe has been suggested as a suitable application.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ya Kartman....  but i also want to program it via terminal, so is there any way for this???

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Write a bootloader that accepts something like XMODEM.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
Write a bootloader that accepts something like XMODEM.
Yup, that's what I have done in the past - but just to note that Xmodem is not the "best" protocol to use as it does not pass the exact file size in bytes but just N lots of (usually) 128 byte packets. That might not matter if you just want to program everything from the last packet. But if you want it to be accurate to the very last byte then consider Ymodem instead.

 

In a protocol such as AVR109 the "size" is sent separately so the bootloader knows exactly when to stop - as such AVR109 might be a "better" option anyway. For the PC end of AVR109 just use avrdude.exe instead of a terminal program.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:

Write a bootloader that accepts something like XMODEM.

 

clawson wrote:
Yup, that's what I have done in the past

likewise

 

note that Xmodem is not the "best" protocol to use as it does not pass the exact file size in bytes but just N lots of (usually) 128 byte packets

The advantage of Xmodem is that it is (almost?) universally supported by terminal emulators.

 

Many support Xmodem-1K - where you get 1K-byte packets.

 

if you want it to be accurate to the very last byte then consider Ymodem instead

Indeed.

 

IIRC, Implementation is almost identical to Xmodem - it just uses a dummy first block to tell the file details.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Very early in my micro journey, the micros came with a "monitor" program, usually in rom that would let one load/save memory ( usually in motorola S9 or intel hex format) and examine and set memory from a terminal.

If one googles AVR monitor you will find such programs, like this one in assembler, https://community.atmel.com/proj...

You can also find examples in c in the forum listings by searching for bob gardner,  as many of his examples included a serial monitor in the code.

 

 

Jim

 

 

Mission: Improving the readiness of hams world wide : flinthillsradioinc.com

Interests: Ham Radio, Solar power, futures & currency trading - whats yours?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

but this avr 109 accepts serial commands... so y cant i just sent the hex file to be programmed using serial terminal like terra term...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Enoch sam wrote:
y cant i

Go on - is your time really that precious that you can't manage to type, "why" ?

 

just sent the hex file to be programmed

That question was answered in #9.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

just sent unknowingly in sms style

 just sent the hex file to be programmed

but this is not happening.... can any anyone send the procedure....

how the commands to be used in terraterm??

Last Edited: Wed. Apr 4, 2018 - 11:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Enoch sam wrote:
but this is not happening

Please explain this more fully.

 

  • What, exactly, are you doing?
  • What, exactly, are you expecting to happen when you do that?
  • What, exactly, is actually happening when you do that?
  • What investigation, testing, debugging, etc, have you done to find the cause of this discrepancy

 

can any anyone send the procedure

Isn't that precisely what this entire thread has been about?

 

Where, exactly, are you stuck?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  • What, exactly, are you doing?
  • What, exactly, are you expecting to happen when you do that?
  • What, exactly, is actually happening when you do that?
  • What investigation, testing, debugging, etc, have you done to find the cause of this discrepancy

i want to flash my atmega 328 using avr109 bootloader vis serial terminal.... there is a function called blockload and blockread... im able read the flash contents in serialterminal but im not able to write into flash using this blockload....

/*****************************************************************************
*
* Atmel Corporation
*
* File              : main.c
* Compiler          : IAR C 3.10C Kickstart, AVR-GCC/avr-libc(>= 1.2.5)
* Revision          : $Revision: 1.7 $
* Date              : $Date: Tuesday, June 07, 200 $
* Updated by        : $Author: raapeland $
*
* Support mail      : avr@atmel.com
*
* Target platform   : All AVRs with bootloader support
*
* AppNote           : AVR109 - Self-programming
*
* Description   : This Program allows an AVR with bootloader capabilities to 
*                 Read/write its own Flash/EEprom. To enter Programming mode   
*                 an input pin is checked. If this pin is pulled low, programming mode  
*                 is entered. If not, normal execution is done from $0000 
*                 "reset" vector in Application area.
*
* Preparations  : Use the preprocessor.xls file for obtaining a customized
*                 defines.h file and linker-file code-segment definition for
*                 the device you are compiling for.
****************************************************************************/
#include "defines.h"
#include "serial.h"
#include "flash.h"
#include <avr/interrupt.h>


/* Uncomment the following to save code space */
//#define REMOVE_AVRPROG_SUPPORT
//#define REMOVE_FUSE_AND_LOCK_BIT_SUPPORT
//#define REMOVE_BLOCK_SUPPORT
//#define REMOVE_EEPROM_BYTE_SUPPORT
//#define REMOVE_FLASH_BYTE_SUPPORT

/*
 * GCC doesn't optimize long int arithmetics very clever.  As the
 * address only needs to be larger than 16 bits for the ATmega128 and
 * above (where flash consumptions isn't much of an issue as the
 * entire boot loader will still fit even into the smallest possible
 * boot loader section), save space by using a 16-bit variable for the
 * smaller devices.
 */
#ifdef LARGE_MEMORY
#  define ADDR_T unsigned long
#else  /* !LARGE_MEMORY */
#  define ADDR_T unsigned int
#endif /* LARGE_MEMORY */

#ifndef REMOVE_BLOCK_SUPPORT
unsigned char BlockLoad(unsigned int size, unsigned char mem, ADDR_T *address);
void BlockRead(unsigned int size, unsigned char mem, ADDR_T *address);

/* BLOCKSIZE should be chosen so that the following holds: BLOCKSIZE*n = PAGESIZE,  where n=1,2,3... */
#define BLOCKSIZE PAGESIZE

#endif /* REMOVE_BLOCK_SUPPORT */

#ifdef __ICCAVR__
#  define C_TASK __C_task
#else /* ! __ICCAVR__ */
#  define C_TASK /**/
#endif /* __ICCAVR__ */
extern uint32_t count=0;
extern uint32_t tim=0;
void (*funcptr)( void ) = 0x0000; // Set up function pointer to RESET vector.
C_TASK void main(void)
{
	MCUCR = (1<<IVCE);
	MCUCR = (1<<IVSEL);
    ADDR_T address;
    unsigned int temp_int;
    unsigned char val;

    /* Initialization */    
	DDRD&=~(0x10);
    PROGPORT |= (1<<PROG_NO); // Enable pull-up on PROG_NO line on PROGPORT.
    initbootuart(); // Initialize UART.
	


    /* Branch to bootloader or application code? */
   // if( !(PROGPIN & (1<<PROG_NO)) ) // If PROGPIN is pulled low, enter programmingmode.
    {
		OCR1A = 0x3D08;
		TCCR1B |= (1 << WGM12);
		// Mode 4, CTC on OCR1A
		TIMSK1 |= (1 << OCIE1A);
		//Set interrupt on compare match
		TCCR1B |= (1 << CS12) | (1 << CS10);
		// set prescaler to 1024 and start the timer
		sei();
		// enable interrupts
        /* Main loop */
        for(;;)
        {
			//uint32_t count = 0;
            val=recchar(); // Wait for command character.
			//count++;
// if (count > MAX_TIME_COUNT)
 //funcptr();
            // Check autoincrement status.
            if(val=='a')
            {
				cli();
                sendchar('Y'); // Yes, we do autoincrement.
            }
            // Set address.
            else if(val=='A') // Set address...
            { // NOTE: Flash addresses are given in words, not bytes.                                            
                address=(recchar()<<8) | recchar(); // Read address high and low byte.
                sendchar('\r'); // Send OK back.
            }
            // Chip erase.
            else if(val=='e')
            {
                for(address = 0; address < APP_END;address += PAGESIZE)
                { // NOTE: Here we use address as a byte-address, not word-address, for convenience.
                    _WAIT_FOR_SPM();        
#ifdef __ICCAVR__
#pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
#endif
                    _PAGE_ERASE( address );
#ifdef __ICCAVR__
#pragma diag_default=Pe1053 // Back to default.
#endif
                }
          
                sendchar('\r'); // Send OK back.
            }
            
#ifndef REMOVE_BLOCK_SUPPORT
            // Check block load support.
            else if(val=='b')
		    {
    			sendchar('Y'); // Report block load supported.
    			sendchar((BLOCKSIZE>>8) & 0xFF); // MSB first.
    			sendchar(BLOCKSIZE&0xFF); // Report BLOCKSIZE (bytes).
    		}


            // Start block load.
    		else if(val=='B')
    		{
	    	    temp_int = (recchar()<<8) | recchar(); // Get block size.
		    	val = recchar(); // Get memtype.
			    sendchar( BlockLoad(temp_int,val,&address) ); // Block load.
		    }
		
		    
		    // Start block read.
    		else if(val=='g')
    		{
	    	    temp_int = (recchar()<<8) | recchar(); // Get block size.
    			val = recchar(); // Get memtype
	    		BlockRead(temp_int,val,&address); // Block read
    		}		
#endif /* REMOVE_BLOCK_SUPPORT */

#ifndef REMOVE_FLASH_BYTE_SUPPORT            
            // Read program memory.
            else if(val=='R')
            {        
                // Send high byte, then low byte of flash word.
                _WAIT_FOR_SPM();        
                _ENABLE_RWW_SECTION();
#ifdef __ICCAVR__
#pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
#endif
                sendchar( _LOAD_PROGRAM_MEMORY( (address << 1)+1 ) );
                sendchar( _LOAD_PROGRAM_MEMORY( (address << 1)+0 ) );
#ifdef __ICCAVR__
#pragma diag_default=Pe1053 // Back to default.
#endif

                address++; // Auto-advance to next Flash word.
            }
        

            // Write program memory, low byte.        
            else if(val=='c')
            { // NOTE: Always use this command before sending high byte.
                temp_int=recchar(); // Get low byte for later _FILL_TEMP_WORD.
                sendchar('\r'); // Send OK back.
            }
            
            
            // Write program memory, high byte.
            else if(val=='C')
            {
                temp_int |= (recchar()<<8); // Get and insert high byte.
                _WAIT_FOR_SPM();
#ifdef __ICCAVR__
#pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
#endif
                _FILL_TEMP_WORD( (address << 1), temp_int ); // Convert word-address to byte-address and fill.
#ifdef __ICCAVR__
#pragma diag_default=Pe1053 // Back to default.
#endif
                address++; // Auto-advance to next Flash word.
                sendchar('\r'); // Send OK back.
            }
        
        
            // Write page.       
            else if(val== 'm')
            {
                if( address >= (APP_END>>1) ) // Protect bootloader area.
                {
                    sendchar('?');
                } else
                {
                    _WAIT_FOR_SPM();        
#ifdef __ICCAVR__
#pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
#endif
                    _PAGE_WRITE( address << 1 ); // Convert word-address to byte-address and write.
#ifdef __ICCAVR__
#pragma diag_default=Pe1053 // Back to default.
#endif
                }

                sendchar('\r'); // Send OK back.
            }
#endif /* REMOVE_FLASH_BYTE_SUPPORT */

#ifndef REMOVE_EEPROM_BYTE_SUPPORT
            // Write EEPROM memory.
            else if (val == 'D')
            {
                _WAIT_FOR_SPM();        
                EEARL = address; // Setup EEPROM address.
                EEARH = (address >> 8);
                EEDR = recchar(); // Get byte.
                EECR |= (1<<EEMPE); // Write byte.
                EECR |= (1<<EEPE);
                while (EECR & (1<<EEPE)) // Wait for write operation to finish.
                    ;
                    
                address++; // Auto-advance to next EEPROM byte.
                sendchar('\r');// Send OK back.
            }

            
            // Read EEPROM memory.
            else if (val == 'd')
            {
                EEARL = address; // Setup EEPROM address.
                EEARH = (address >> 8);
                EECR |= (1<<EERE); // Read byte...
                sendchar(EEDR); // ...and send it back.
                address++; // Auto-advance to next EEPROM byte.
            }
#endif /* REMOVE_EEPROM_BYTE_SUPPORT */

#ifndef REMOVE_FUSE_AND_LOCK_BIT_SUPPORT
            // Write lockbits.
            else if(val=='l')
            {
                _WAIT_FOR_SPM();        
                _SET_LOCK_BITS( recchar() ); // Read and set lock bits.
                sendchar('\r'); // Send OK back.
            }
               

#if defined(_GET_LOCK_BITS)
            // Read lock bits.
            else if(val=='r')
            {
                _WAIT_FOR_SPM();        
                sendchar( _GET_LOCK_BITS() );
            }


            // Read fuse bits.
            else if(val=='F')
            {
                _WAIT_FOR_SPM();        
                sendchar( _GET_LOW_FUSES() );
            }


            // Read high fuse bits.
            else if(val=='N')
            {
                _WAIT_FOR_SPM();        
                sendchar( _GET_HIGH_FUSES() );
            }


            // Read extended fuse bits.
            else if(val=='Q')
            {
                _WAIT_FOR_SPM();        
                sendchar( _GET_EXTENDED_FUSES() );
            }
#endif /* defined(_GET_LOCK_BITS) */
#endif /* REMOVE_FUSE_AND_LOCK_BIT_SUPPORT */

#ifndef REMOVE_AVRPROG_SUPPORT        
            // Enter and leave programming mode.
            else if((val=='P')||(val=='L'))
            {
                sendchar('\r'); // Nothing special to do, just answer OK.
            }
            
            
            // Exit bootloader.
            else if(val=='E')
            {
                _WAIT_FOR_SPM();        
                _ENABLE_RWW_SECTION();
                sendchar('\r');
                funcptr(); // Jump to Reset vector 0x0000 in Application Section.
            }

    
            // Get programmer type.        
            else if (val=='p')
            {
                sendchar('S'); // Answer 'SERIAL'.
            }
            
            
            // Return supported device codes.
            else if(val=='t')
            {
#if PARTCODE+0 > 0
                sendchar( PARTCODE ); // Supports only this device, of course.
#endif /* PARTCODE */
                sendchar( 0 ); // Send list terminator.
            }
            
            
            // Set LED, clear LED and set device type.
            else if((val=='x')||(val=='y')||(val=='T'))
            {
                recchar(); // Ignore the command and it's parameter.
                sendchar('\r'); // Send OK back.
            }
#endif /* REMOVE_AVRPROG_SUPPORT */
       
            // Return programmer identifier.
            else if(val=='S')
            {
                sendchar('A'); // Return 'AVRBOOT'.
                sendchar('V'); // Software identifier (aka programmer signature) is always 7 characters.
                sendchar('R');
                sendchar('B');
                sendchar('O');
                sendchar('O');
                sendchar('T');
            }
        
            
            // Return software version.
            else if(val=='V')
            {
                sendchar('1');
                sendchar('5');
            }        


            // Return signature bytes.
            else if(val=='s')
            {							
                sendchar( SIGNATURE_BYTE_3 );
                sendchar( SIGNATURE_BYTE_2 );
                sendchar( SIGNATURE_BYTE_1 );
            }       


            // The last command to accept is ESC (synchronization).
            else if(val!=0x1b)                  // If not ESC, then it is unrecognized...
            {
                sendchar('?');
            }
        } // end: for(;;)
    }
/*   else
    {
        _WAIT_FOR_SPM();        
        _ENABLE_RWW_SECTION();
		 sendchar( 'a');
		 sendchar( 'p');
		 sendchar( 'p');
		 sendchar( 'n');
        funcptr(); // Jump to Reset vector 0x0000 in Application Section.
    }*/
} // end: main


#ifndef REMOVE_BLOCK_SUPPORT
unsigned char BlockLoad(unsigned int size, unsigned char mem, ADDR_T *address)
{
    unsigned char buffer[BLOCKSIZE];
    unsigned int data;
    ADDR_T tempaddress;
	
    // EEPROM memory type.
    if(mem=='E')
    {
        /* Fill buffer first, as EEPROM is too slow to copy with UART speed */
        for(tempaddress=0;tempaddress<size;tempaddress++)
            buffer[tempaddress] = recchar();
        
        /* Then program the EEPROM */
        _WAIT_FOR_SPM();
    	for( tempaddress=0; tempaddress < size; tempaddress++)
    	{
	        EEARL = *address; // Setup EEPROM address
            EEARH = ((*address) >> 8);
            EEDR = buffer[tempaddress]; // Get byte.
            EECR |= (1<<EEMPE); // Write byte.
            EECR |= (1<<EEPE);
            while (EECR & (1<<EEPE)) // Wait for write operation to finish.
                ;

  			(*address)++; // Select next EEPROM byte
        }

        return '\r'; // Report programming OK
    } 
    
    // Flash memory type.
	else if(mem=='F')
    { // NOTE: For flash programming, 'address' is given in words.
        (*address) <<= 1; // Convert address to bytes temporarily.
        tempaddress = (*address);  // Store address in page.
	
        do
		{
            data = recchar();
            data |= (recchar() << 8);
#ifdef __ICCAVR__
#pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
#endif
            _FILL_TEMP_WORD(*address,data);
#ifdef __ICCAVR__
#pragma diag_default=Pe1053 // Back to default.
#endif
            (*address)+=2; // Select next word in memory.
            size -= 2; // Reduce number of bytes to write by two.
        } while(size); // Loop until all bytes written.

#ifdef __ICCAVR__
#pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
#endif
	_PAGE_WRITE(tempaddress);
#ifdef __ICCAVR__
#pragma diag_default=Pe1053 // Back to default.
#endif
	_WAIT_FOR_SPM();
	_ENABLE_RWW_SECTION();

        (*address) >>= 1; // Convert address back to Flash words again.
        return '\r'; // Report programming OK
    }
    
    // Invalid memory type?
    else
    {
        return '?';
    }
}


void BlockRead(unsigned int size, unsigned char mem, ADDR_T *address)
{
    // EEPROM memory type.
    if (mem=='E') // Read EEPROM
    {
        do
        {
            EEARL = *address; // Setup EEPROM address
            EEARH = ((*address) >> 8);
            (*address)++; // Select next EEPROM byte
            EECR |= (1<<EERE); // Read EEPROM
            sendchar(EEDR); // Transmit EEPROM dat ato PC

            size--; // Decrease number of bytes to read
        } while (size); // Repeat until all block has been read
    }
    
    // Flash memory type.
	else if(mem=='F')
	{
        (*address) <<= 1; // Convert address to bytes temporarily.
	
        do
        {
#ifdef __ICCAVR__
#pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr.
#endif
            sendchar( _LOAD_PROGRAM_MEMORY(*address) );
            sendchar( _LOAD_PROGRAM_MEMORY((*address)+1) );
#ifdef __ICCAVR__
#pragma diag_default=Pe1053 // Back to default.
#endif
            (*address) += 2; // Select next word in memory.
            size -= 2; // Subtract two bytes from number of bytes to read
        } while (size); // Repeat until all block has been read

        (*address) >>= 1; // Convert address back to Flash words again.
    }
}
#endif /* REMOVE_BLOCK_SUPPORT */

ISR (TIMER1_COMPA_vect)
{
	tim++;
	if(count<=1 & tim==20)
	{
	tim=0;
	_WAIT_FOR_SPM();
	_ENABLE_RWW_SECTION();
	sendchar( 'a');
	sendchar( 'p');
	sendchar( 'p');
	sendchar( 'l');
	sendchar( 'n');
	funcptr();
	}

	
	// action to be done every 1 sec
}
/* end of file */

this is the entire code....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Enoch sam wrote:
i want to flash my atmega 328 using avr109 bootloader

So go to the ATmega328 Product Page:

 

https://www.microchip.com/wwwproducts/en/ATmega328

 

Go to the 'Documents' Tab, and find AVR109:

 

http://www.microchip.com//wwwAppNotes/AppNotes.aspx?appnote=en591230

 

AVR109 wrote:

This application note describes how an AVR with the Store Program Memory (SPM)

instruction can be configured for Self-programming. The sample application communicates

via the UART with a PC running the AVR Open Source Programmer (AVROSP)

from Application note AVR911.

 

But, if you don't want to use that application, the protocol is described on page 8 - so you can implement your own.

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AVR109 is a binary not a text (and certainly not an Intel hex!) protocol so of course you cannot just send .hex contents. You have to read and decode the hex, convert to binary, packetize that then send via 109.