Mega128 self-programming problem...

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

Hi,
What could be wrong with mega128 self-programming when
1) it works perfectly right in Avrstudio 4.07
2) same code programs ok Mega16 (page size is different, of course)
3) in real chip flash is only erased and not programmed

Is there anything special I need to know about Mega128 ?

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

I haven't checked, but it might not work if the mega103 compatibility fuse still is programmed (as it is by default).

Also, self programming the m128 is somewhat different from the m16; you must take into consideration the 64k boundary in the flash (you can read all about that in the m128 data sheet).

-Geir

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

thanks Geir, but mega103 fuse is not programmed and RAMPZ is set to zero, because I don't need now more than 64K.

there must be something else what is not documented in mega128 datasheet.

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

Are you making sure the Z register is being set to the correct page both prior to the page erase command and the page write ie the page address goes into the upper byte of the Z and the upper bit of the Z lower byte.
Mike

Keep it simple it will not bite as hard

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

Also there are no timing restrictions in studio so are you checking the bit make sure erase has finished?

Keep it simple it will not bite as hard

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

as mega128 has 256 byte page and Z must contain byte address (addr 0 is ignored by page load) then yes - prior to the page erase and page write there is same address... but only in the Z-hi, Z-lo is set to zero. Is this correct address?

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

and, yes of course I'm polling SPMCR.0 bit exactly like asm example code in mega datasheets.

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

yes, all above is ok - page writing algo is almost exactly copied from datasheet and I've been using it successfully for months with Mega16. Interrupts and eeprom are not used at all in bootloader.

there must be something else...

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

SPMCR is accessible with in/out instructions in Mega16 but not in Mega128.
Dave Raymond

admin's test signature
 

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

thanks Dave, but this is not the case - SPMCR is accessed as RAM
STS [0x0068],R23

admin's test signature
 

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

One last thing I can think of: you are sure that the SPM instruction is executed from the area defined by the boot size fuses?

If this doesn't help, you might consider posting the code.

-Geir

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

Yes, i'm sure - bootloader starts with org 0xFC00 and fuses are set for 1K word bootloader.

As I wrote in initial message page_erase works, only programming with new data does not. If I load something to the application area with avrstudio, then it erases it (readout is always filled with $FF until $FC00 where bootloader starts).
So it can't be the case - SPM executed from application section will not erase flash.

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

In the first posting you said the flash was erased now you say it is not!
Please clarify.
Mike

Keep it simple it will not bite as hard

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

flash is erased by self-prog but not programmed with new code.
I replied to geir's post from 9:00 that IF the SPM is executed from app area THEN it will not erase flash...

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

In my experience with Mega128 self-programming, I've found it necessary to wait for SPMEN to clear *after* executing SPM, as well as before...

i.e.

do_spm:
wait for SPMEN to clear
SPM
wait for SPMEN to clear
RET

This effectively makes all of my flash memory behave as NRWW - the only code that runs during a page write/erase is the wait loop. Not sure why this works, but taking the second wait out definitely causes page writes to fail, and IIRC it was *only* writes that failed, erases were OK - as Allan is seeing.

Regards
Chris

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

I had a similar problem. My "DoSPM" code checked SPMEN and re-enabled the RWW section. However, I found that it in order to make it work you MUST re-enable the RWW section after erasing and before loading
the flash page buffer but MUST NOT re-enable it after loading the buffer
and before programming it.

Rgds, Andy

admin's test signature
 

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

Thanks Chris, thats it! It works if I wait for SPMEN ready before & after SMP instruction.

With other RWW supported chips (like Mega16) it is not needed, so it must be mega128 microcode bug.