bootloader problems on mega128

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

Hello.

I have modified the code found in https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=56175 to create a demo bootloader for Mega128.

The fuse bits I use are as follows : BOOTSZ1 = 1 BOOTSZ0 = 0; corresponding to 0xFC00 for the Boot Loader Section.
BOOTRST = 0, so the code in the bootloader section will be executed first.

I have modified the Makefile as follows:

BOOTLOAD = 0x1F800
LDFLAGS += -Wl,--section-start=.bootloader=$(BOOTLOAD)

I use -O0 for compiler optimization.

I have put breakpoints in the boot_program_page(), it doesn't seem to enter the boot_program_page function (i am using a JTAGIce mk 2). Also, I have inspected the memory contents at address 0x300, and nothing is there. It seems that the function is never executed. Anybody know why?

Thank you for your time.

My code:

#include 
#include 
#include 
#include 
#include 
void boot_program_page() BOOTLOADER_SECTION;

uint8_t c[256] = "0123456789";

int main (void)
{

	if(c[4])
	{
	 DDRA = 5;
	}
   return (0);
}


void boot_program_page ()
{

	uint32_t page =0x300 ;
	uint8_t *buf =&c[0];
   uint16_t i;
    uint8_t sreg;

    // Disable interrupts.

    sreg = SREG;
    cli();
   
    eeprom_busy_wait ();

    boot_page_erase (page);
    boot_spm_busy_wait ();      // Wait until the memory is erased.

    for (i=0; i
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just announcing that you have modified something, is not very helpful.

What and Why?

I also note that you have used -O0. This will probably upset any timing or size critical code.

David.

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

david.prentice wrote:
Just announcing that you have modified something, is not very helpful.

What and Why?

I also note that you have used -O0. This will probably upset any timing or size critical code.

David.

I have modified boot_program_page to take no parameters, because I want it to execute before main. If you click the link in my post, you will see what I am talking about.

Needless to say, the problem is in my code, not the other post :)

I don't need any optimisations for the moment, that is why I use -O0

Regards

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

You are on the wrong track. Your current approach can not work, because after a reset the function boot_program_page is executed without any c startup code prior to it. Write the bootloader as separate application and locate the entire application in the bootloader area by relocating the .text segment.

https://www.avrfreaks.net/index.p...

Stefan Ernst

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

sternst wrote:
You are on the wrong track. Your current approach can not work, because after a reset the function boot_program_page is executed without any c startup code prior to it. Write the bootloader as separate application and locate the entire application in the bootloader area by relocating the .text segment.

https://www.avrfreaks.net/index.p...

This explains a lot. I have kept away from separating the bootloader and application into two different projects.

My last question is as follows: Is the address for the Bootloader start correct ?

From what I understand, it's the address from the datasheet corresponding to the BOOTSZ fuses *2.

In my case BOOTSZ1 = 1 and BOOTSZ2 = 0. So for Boot address 0xFC00 I should write :

LDFLAGS += -Wl,--section-start=.bootloader=0x1F800

in the Makefile?

Thank you once again.

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

Quote:

In my case BOOTSZ1 = 1 and BOOTSZ2 = 0.

Apart from the fact that I bet you meant BOOTSZ1=1 and BOOTSZ0=0 then, yes, the numbers look right

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

clawson wrote:
Quote:

In my case BOOTSZ1 = 1 and BOOTSZ2 = 0.

Apart from the fact that I bet you meant BOOTSZ1=1 and BOOTSZ0=0 then, yes, the numbers look right

:) yes.

Well then, I feel I must correct you on your post: https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=56175.

The default address for Mega128 is 1FC00, not 0x1E000 like you stated :

clawson wrote:
PS assuming you haven't changed the default state of the BOOTSZ fuses in your '128 then BOOTLOAD = 0x1E000 in your equivalent of the above

You should edit you post so other people won't run into the same trouble.

Thanks to all for your help.

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

You need to re-read your 128 datasheet:

Table 118 on page 288 wrote:
BOOTSZ1 bit=2 default=0
BOOTSZ0 bit=1 default=0

Table 112 on page 284 wrote:
BOOTSZ1=0 BOOTSZ0=0 4096words 32pages app=0000-EFFF boot=F000-FFFF end_app=EFFF boot_rst=F000

These addresses are in words (as all Atmel documenation) and 0xF000 in words is 0x1E000. So I won't be doing any editing today as what I wrote is the complete truth.
Quote:

The default address for Mega128 is 1FC00, not 0x1E000 like you stated

So where have you got this mis-information from? Not the datasheet, that's for sure.

Cliff

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

clawson wrote:
You need to re-read your 128 datasheet:
Table 118 on page 288 wrote:
BOOTSZ1 bit=2 default=0
BOOTSZ0 bit=1 default=0

Table 112 on page 284 wrote:
BOOTSZ1=0 BOOTSZ0=0 4096words 32pages app=0000-EFFF boot=F000-FFFF end_app=EFFF boot_rst=F000

These addresses are in words (as all Atmel documenation) and 0xF000 in words is 0x1E000. So I won't be doing any editing today as what I wrote is the complete truth.
Quote:

The default address for Mega128 is 1FC00, not 0x1E000 like you stated

So where have you got this mis-information from? Not the datasheet, that's for sure.

Cliff

yes, Cliff. You're right. I thought the unprogrammed(value = 1) was the default configuration.

My bad.

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

Well, I made two applications.

One is code that will be placed in the bootloader section(although it only self-programms a region in the flash - 0x300, and has no peripheral support yet).

The other is a simple test application that lights a led. I expect to see the code for the bootloader section in 0xFC00, and the code for the application somewhere near the start of the flash.

I have disabled "Erase Device" when reprogramming, to avoid erasing the bootloader section when flashing the test application. The JTAG Ice dialog throws an error:

WARNING: FLASH byte address 0x0098 is 0x10(should be 0x12).. FAILED!

Anybody know what's going on ??

Also, I have tried flashing the application first and the bootloader afterwards, and the same error pops up. Since the code in the bootloader section starts at 0xFC00, why is my programmer attempting to write at 0x0098?

Mihai

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

Update: It works, at it has been working all along.

JTAG Ice MK2 doesn't Update the contents of the Program Memory section in realtime. I had to do a flash memory dump and manually go to the specified address.

The writing to the flash problem has disappeared now, it was something transient probably.

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

The "normal" way to program boot+app is one of either:

1) combine app.hex+boot.hex and program composite.hex - simple way to combine is using a text editor and deleting the "end" record of app.hex

2) Just program boot.hex into the chip then use the bootloader delivery mechanism to transfer app.hex for it to be programmed into place.

WF (aka Cliff)

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

WrightFlyer wrote:
2) Just program boot.hex into the chip then use the bootloader delivery mechanism to transfer app.hex for it to be programmed into place.
You should know that there was a bug in AVR Studio 4.16 (? - early version?) where trying to program just a boot would fail; the device would not be programmed.

IIRC it was fixed and a service pack released. I know the latest version 4.17 works.

Stu

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!