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