[SOLVED] ASM: page erase / page size

Hi
Im having a bit of a problem erasing a flash page of an atmega328p. Im using atmel studio 7 and since i dont have an uC at hand im using the simulator.
Pagesize in words is 64 = 128 bytes per page. The page address gets loaded into the Z (16 bits) register.

Page 1 should be addr 0, and since the page size is 64 words, page 2 should start at addr 0x40 and page 3 at addr 0x80 etc. Note that the addr in .ORG is a word addr ( 1 word = 2 bytes )

But i must miss something, because looking at the memory view (which is byte addr, not word addr).. if i want to erase flash page 1 at byte addr 0 (loading 0 into Z), it erases everything up to byte addr EDIT:0xff. So the string "page1" and "page2" gets erased and together thats 128 words and not 64. What is wrong ?

.include "m328pdef.inc"
.equ page1 = 0
.equ page2 = 0x0040 ;byte addr 0x80
.equ test = 0x007f
.equ page3 = 0x0080 ;byte addr 0x100


.org page1
jmp SMALLBOOTSTART
.db "page1"

.org page2
.db "page2"

.org test
.db "te"

.org page3
.db "page3"



.org SMALLBOOTSTART
loop:
.equ page_ = page1
.equ shift = 1
ldi zl, low(page_ << 1)
ldi zh, high(page_ << 1)

ldi r16, (1<<spmen) | (1<<pgers)
rcall doSpm
ldi r16, (1<<spmen) | (1<<RWWSRE) ;re-enable flash section after erase
rcall doSpm
rjmp loop

doSpm:
in r17, spmcsr
sbrc r17, 0 ;see if flash is rdy again
rjmp doSpm
out spmcsr, r16
spm
ret

spm takes byte address

Juraj:
spm takes byte address

Yes i know, thats why i shift the addr left by 1, but since i load 0 into it it doesnt matter. But also any other addr gives me wrong results.

0x7f is 128. one page is erased

Juraj:
0x7f is 128. one page is erased

I know, but here is what i mean.
.ORG uses a word addr, so if i write .org 0x01 it puts my code 1 word = 2 bytes away from 0x00. This can be seen in the memory view.

.org 0x00:
PIC

.org 0x01;
PIC

So if i write .org 0x40 it puts the part 64 words (1 page) away from addr 0. So from word addr 0 to 0x3f is page 1 and from 0x40 to 0x80 is page 2.

But if i want to erase flash page 1 that goes from byte addr 0 to 0x7f, it erases everything till byte addr 0xff, which is 128 words.

erased flash pic

so it erase until 7F or FF byte address? in post you have "it erases everything till byte addr 0x7f"

Juraj:
so it erase until 7F or FF byte address? in post you have "it erases everything till byte addr 0x7f"

Thats my mistake, i meant word addr.
I put a 2 byte string "te" at word addr 0x7f, which is byte addr 0xfe and ends at byte addr 0xff.
Everything from 0 to byte addr 0xff gets erased. Thats 256 bytes. But it should only go from 0 to byte addr 0x7f if i load Z with 0.

Any help would be appreciated.

Juraj:
spm takes byte address

Only lpm takes a byte address. spm uses a page:offset addressing. The page is stored in bits 14:7 and the word address within that page in bits 6:1 of the z-pointer. Bit 0 of the z-pointer should always be equal to 0.

Edit: I don't know about your setup, but spm instructions can only be executed from the bootsector. Take out the datasheet and read the chapter about self-programming the flash.

LightuC:
spm instructions can only be executed from the bootsector. Take out the datasheet and read the chapter about self-programming the flash.

The code is placed in the bootloader section, at the start i jump to this section and start executing code from there. The strings in the beginning are only there to see if the flash pages are erased correctly. I already read the datasheet a 1000 times, but maybe i cant see the forest for the trees and im missing something idk. But im pretty sure everything is correct, and only what atmel studio is showing me is wrong ? idk

I mean bits 14 to 7 are used for the page addr, thats 8 bits and there are 256 pages. Ok. So if i load 0 into Z it should erase the first flash page which is word addr 0 to 3f. What could go wrong loading 0 into Z.....nothing in my opinion.

There was a very similar issue reported on avrfreaks last fall:
https://www.avrfreaks.net/forum/page-erase-as7-simulator-doesnt-work-asm-bootloader-spm
re-reading it, I'm not sure that we every got to the bottom of everything, but there might be some hints there....

westfw:
There was a very similar issue reported on avrfreaks last fall:
https://www.avrfreaks.net/forum/page-erase-as7-simulator-doesnt-work-asm-bootloader-spm
re-reading it, I'm not sure that we every got to the bottom of everything, but there might be some hints there....

Sadly it doesnt help. The problem seems to be the same, but i cant extract any usefull information out of it. It doesnt matter what addr i load into Z, it always erases 128 words. Is the memory view wrong ? Am i misunderstanding the memory view ?

I updated post 4 (click) with better pictures to make clear what i mean.

Can someone maybe please test my code to report how it behaves.

bettssi:
Sadly it doesnt help. The problem seems to be the same, but i cant extract any usefull information out of it. It doesnt matter what addr i load into Z, it always erases 128 words. Is the memory view wrong ? Am i misunderstanding the memory view ?

I updated post 4 (click) with better pictures to make clear what i mean.

Can someone maybe please test my code to report how it behaves.

try it on a real AVR

Right now i dont have one.

Today i tested it with a real atmega328, and found out that the problem i described is a bug? in the atmel studio simulator. The code in post 1 works like it should on a real chip.

Is there a way to report this to microchip or whoever is responsible for atmel studio ? Email or so ?