AVR simple bootloader - how to call application code?

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:

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 ).

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 ();
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!!

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.

• Imho and only a suggestion: your answer would be more valuable, should you give any hint what's the easiest way to achieve the desired result – Rustam Sep 23 '14 at 13:58
• I can re-write all of the tutorials of AVR Freaks here, to be sure, but I do have a day job. This is not a "oh I'll quickly post this single line of code" question, this is a fundamental principle in the controller that even professionals some times struggle with. Starting with a tutorial made by someone with experience is the only way to understand how it all works. – Asmyldof Sep 23 '14 at 14:28