Mega 8 Bootloader

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

I am having trouble with bootloaders. The following program should reprogram the first 8 pages (512 bytes or 256 words) of flash. The first byte of each word should be the word address and the second should be the page number. It works in AVR Studio 4.09, but not on the actual hardware. The first page (32 words, 64 bytes) is all 0xFF, as if the page erase worked, but the page write didn't.

Can anyone help?

ORG 0x1F00
Bootloader:
clr r30 ; Z= 0x0000 for program counter
clr r31
clr r0 ; r1:r0 = data to write
clr r1

page_erase:
ldi r16,3
rcall dospm

fill_buffer:
ldi r16,1
out SPMCR,r16
spm
inc r0
adiw r31:r30,2
ldi r16,0x3F
and r16,r30
brne fill_buffer
sbiw r31:r30,0x20
sbiw r31:r30,0x20

ldi r16,5 ; page write
rcall dospm

adiw r31:r30,0x20
adiw r31:r30,0x20
inc r1
sbrs r1,3
rjmp page_erase

stop:
nop
rjmp stop

dospm:
out SPMCR,r16
spm
wait:
in r16,SPMCR
sbrc r16,SPMEN
rjmp wait
ret

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

I've noticed two things. I think your address for the bootloader namely $1F0 is incorrect. In Mega 8 the Smallbootstart is at $F80 and the Largebootstart is at $C0 so that your bootloader will miss the BLS or Boot Loader Section.
Another point, you are supposed to fill r1,r0 registers with data whose location is pointed to by X or Y register, so that you are not supposed to increment r1,r0 but, let's say, Y register (Y+). I hope I am right, regards, Johnny.

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

Sorry, I missed one zero for the address of the largebootlstart. It should be $C00

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

It might look silly to increment r1 and r0 like I did. This is just a test to write stuff to flash to test that part of the program.

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

I don't understand what you mean about the bootloader addresses.

Table 82 on page 217 of the datasheet says that the smallest boot size is 128 words and that the bootloader section starts at word address xF80. The IAR compiler uses byte addresses, so I multiplied by 2 to get x1F00.

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

I just checked with AVR Studio. It has the bootloader starting at word address xF80.

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

Hi,

Quote:
It works in AVR Studio 4.09, but not on the actual hardware.

Did you set the fuses on the MCU to allow writing to the flash?

Volkmar

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

I have lock bits =0xFF, no memory lock features. I also have the bootsize set for 128 words.

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

rneil wrote:
I have lock bits =0xFF, no memory lock features. I also have the bootsize set for 128 words.

I assume you have the BOOTRST fuse bit set to zero.

Don

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

I have fuses set to 0xDF94. The boot reset vector is not enabled. The AVR STudio box is not checked.

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

rneil wrote:
I have fuses set to 0xDF94. The boot reset vector is not enabled. The AVR STudio box is not checked.

If BOOTRST is not enabled, the ATMega8 is going to start executing at 0 on reset. Don't you want it to start at 0x1F00?

Don

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

I want it to start from the regular address 0. I have a simple C main routine to set outputs to avoid floating inputs, set the stack, and call the bootloader.

I know that with this test code the program can only run once. Ultimately the program will do someting usefull, then branch to the bootloader when an update is available, then load a substantive program.

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

It works!

You were on the right track with the boot sector. It must have executed multiple times and treated the test pattern as code. I added a rjmp stop before the bootloader label and it worked as expected. Now I know how to write to program memory.