Electronic – AVR simple bootloader – how to call application code

atmegaavrbootloadercmicrocontroller

I am using the atmega328. I want to create a simple test application using a bootloader. I want to: Blink an led at one speed in the bootloader section of flash, and then blink at another speed in application section of flash. these are my fuse bits:

asdasdasd

From my knowledge, if I set the BOOTRST fuse I can set where the program boot(or rests from)
so, I have set the program to execute from the begging of the boot loader section, 0x3800 (for a page size of 2048). this is on page 282 of the datasheet ).

enter image description here

This is the current code:

#include <avr/io.h>
#include <avr/delay.h>


void boot_program_page ()
{
    DDRC = 0xFF; 
    PORTC = 0xFF;
    _delay_ms(1000); 
    PORTC= 0x00;  
    _delay_ms(1000);    
} 

void main(void)
{

    boot_program_page ();
    asm ( "jmp 0x0000" );// Jump to application code.   
    PORTC = 0xFF; 
    _delay_ms(10000); 
    PORTC= 0x00;  
    _delay_ms(10000);  
}

This is my understanding:
Because I have set the BOOTRST fuse at the beginning of the boot loader section, this code is burnt into the bootloader. As soon as the program sees the jump to application code (asm command) it burns the rest of the following code in the application memory. this is not what happens though, the boot_program_page() is just run indefinitely.

How can I fix this?

Thanks!!

Best Answer

You are misunderstanding the function of the assembler command.

If you program a "GOTO" in C, the jmp assembler command comes out. It is just what it says on the tin: Jump. It tells the compiler nothing about code location.

Nowhere in your code do you specify what code goes where, as such the compiler just decides for you that you want the code to go where it thinks is best. Which most likely is the start of non-vector non-boot space.

I think you'd be well off going to: http://www.avrfreaks.net/

And searching for "[TUT] Bootloader" and see if there's a tutorial that pops up that fits your general way of thinking. It'll take you at least a good half day, but you will learn most you need to know about it.


Edit:

I forgot to point out that your code also doesn't stop at a predictable point. These days often the compiler helps out in the assumption you mean to stop at the end of main(), but it's "good manners" to include a while(1){}; at the end, where you want the code to halt. That way you are in absolute control of what happens and you are absolutely sure your code doesn't just run on into empty flash.