Bootloader for ATmega4809

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

Hello.

I'm trying to make a bootloader for an ATmega4809.

I'm using this programmer

https://github.com/ElTangas/jtag2updi

And it works fine so far.

 

I'm using AtmelStudio 7.0.1931

 

I'm trying to flash two .elf files into the chip but according to my code, only one should be executed.

 

This is bootloader code

#include <atmel_start.h>
#include <string.h>
#include <stdio.h>
#include <avr/delay.h>
#include "src/console/console.h"

int main(void)
{
	/* Initializes MCU, drivers and middleware */
	sei();
	atmel_start_init();
	while(1)
	{
		printLine("bootloader\r\n");
	}
}

 

This is main application code

#include <atmel_start.h>
#include <string.h>
#include <stdio.h>
#include <avr/delay.h>
#include "src/console/console.h"

int main(void)
{
	/* Initializes MCU, drivers and middleware */
	sei();
	atmel_start_init();
	while(1)
	{
		printLine("main firmware\r\n");
	}
}

 

According to the datasheet. there are sections, BOOTEND, APPEND that are used only for flash memory protection.

I think I don't need that kind of protection for my tests now. So I can just flash both .elf files, one at the beginning and one just a few blocks further to see what the mcu is executing.

The BOOTEND and APPEND fuses has to setup in blocks of 256 bytes. So I chose to leave 8192 bytes for bootloader and write the main application there.

 

The flash address starts at 0x4000 + 0x2000 (8192 dec) = the main application should be at 0x6000.

 

I setup the AtmelStudio the address for the main application.

 

I'm flashing the "bootloader" firmware with

avrdude.exe -c jtag2updi -P com5 -p m4809 -e -U flash:w:bootloader.elf

I perform full chip erase before flashing the bootloader.elf.

 

 

and the "main application" firmware with

avrdude.exe -c jtag2updi -P com5 -p m4809 -D -U flash:w:main_app.elf

I think the -D flag prevents ISP programmer to erase anything...

 

 

When the main_app.elf is loaded, gibberish is printed on the terminal...

 

What am I doing wrong?

Last Edited: Sat. Jun 8, 2019 - 06:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

UART gibberish usually means a clock not running at the speed you thought it was.

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

giorgos3924 wrote:
I think the -D flag prevents ISP programmer to erase anything...

 

Don't worry about that, by default jtag2updi erases only the pages that are written.

 

edit: in fact, you should not use the -D option, because this might prevent page erase, thus incorrect data may be written (I don't remember if I implemented this or if -D is just ignored).

Last Edited: Sat. Jun 8, 2019 - 08:05 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
UART gibberish usually means a clock not running at the speed you thought it was.

Yes, usually means that, but the only thing I'm changing is the debug line, just to see what firmware is running.

 

El Tangas wrote:

giorgos3924 wrote:
I think the -D flag prevents ISP programmer to erase anything...

 

Don't worry about that, by default jtag2updi erases only the pages that are written.

 

edit: in fact, you should not use the -D option, because this might prevent page erase, thus incorrect data may be written (I don't remember if I implemented this or if -D is just ignored).

I'm gonna try it again.

Is everything else I'm trying to do correct?

 

EDIT: I just tried it. It's running the latest firmware that I flashed

I see "main firmware" printed out on the terminal.

 

I think the main firmware is written on the default address. it seems like the -Ttext value is ignored somehow.

 

Without changing anything in the source code. Just removing the option -Ttext I see that the MD5 checksum of the .elf files doesn't change.

I think it should be different.

 

EDIT2: it works.

I put the -Ttext value into the "miscellaneous" settings.

 

I got some segmentation fault errors at first. I deleted the -Ttext value..., it continued give me seg fault error.... So, I deleted the Debug folder and rebuild.

I flashed the bootloader with void settings into the Other Linker Flags. I setup the -Ttext=0x6000 value and I flashed the main firmware.

I also tried a jump.

asm ( "jmp 0x6000" );

and it worked.

 

I also saw that checksum changes even if the source code is the same.

Now I have some other questions about jumping to the address.

I'm not sure if just the command above is enough to safe start the application firmware.

Also, I saw somewhere on this forum the command about flashing fuses.

avrdude.exe -c jtag2updi -P com5 -p m4809 -U fuse5:w:YourProject.elf

is that correct? I'm asking this because when I read the mcu I see all fuses at 0x00, nothing changes

avrdude.exe -c jtag2updi -P com5 -p m4809 -v
Last Edited: Sat. Jun 8, 2019 - 09:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can't see the fuses with that command. Use this:

 

avrdude.exe -c jtag2updi -P com5 -p m4809 -t

you will enter avrdude terminal mode, then type this command:

 

dump fuses

and you will see the current values of the fuses.

 

edit: for example, I got this in a test (I marked the fuse values in red, they go from fuse 0 to fuse 8)

 

avrdude.exe: Device signature = 0x1e9422 (probably t1614)
avrdude> dump fuses
>>> dump fuses
0000  0a 00 02 bb 00 f6 01 00  00                       | ........       |

 

Last Edited: Sat. Jun 8, 2019 - 10:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Optiboot (https://github.com/Optiboot/optiboot) uses "-Wl,--section-start=.text=0x7e00" to specify the start address.  I'm not sure -T is sufficient (but I don't recall the distinction, if any.)

 

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

westfw wrote:

Optiboot (https://github.com/Optiboot/optiboot) uses "-Wl,--section-start=.text=0x7e00" to specify the start address.  I'm not sure -T is sufficient (but I don't recall the distinction, if any.)

 

Thanks for the reply! I don't remember if I tried that too. But it's good to know!

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

El Tangas wrote:

You can't see the fuses with that command. Use this:

 

avrdude.exe -c jtag2updi -P com5 -p m4809 -t

you will enter avrdude terminal mode, then type this command:

 

dump fuses

and you will see the current values of the fuses.

 

edit: for example, I got this in a test (I marked the fuse values in red, they go from fuse 0 to fuse 8)

 

avrdude.exe: Device signature = 0x1e9422 (probably t1614)
avrdude> dump fuses
>>> dump fuses
0000  0a 00 02 bb 00 f6 01 00  00                       | ........       |

 

Thanks for the info!

 

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

-Ttext= is just a short form of -section-start=.text=
.
Simply quicker/easier to type.