Writing to code flash in IAR C

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

I've been battling for a day now, trying to get this working. IAR C provides a few intrinsic functions (which don't appear to be documented anywhere - I found them via Atmel's application note AVR109) to access the program memory.

To try and get something to work, I've created a pretty minimal program to write one page of data into the program memory. Here it is:

#include
#include

#define _WAIT_FOR_SPM() while( SPMCSR & (1<<SPMEN) );

void main(void)

{
unsigned long int FlashAddress;

__disable_interrupt();
for (FlashAddress = 0x3000; FlashAddress < 0x307f; ++FlashAddress) {
_WAIT_FOR_SPM();
__AddrToZ24WordToR1R0ByteToSPMCR_SPM( (void __farflash *) (FlashAddress << 1), 0x1234, 0x01 );
}
FlashAddress = 0x307f;
_WAIT_FOR_SPM();
__AddrToZ24WordToR1R0ByteToSPMCR_SPM( (void __farflash *) (FlashAddress << 1), 0x1234, 0x01 );
_WAIT_FOR_SPM();
__AddrToZ24ByteToSPMCR_SPM((void __farflash*)(FlashAddress << 1), 0x03);
_WAIT_FOR_SPM();
__AddrToZ24ByteToSPMCR_SPM((void __farflash*)(FlashAddress << 1), (0x05));
_WAIT_FOR_SPM();
while(1) {}
}

It does precisely nothing. Is there anyone familiar with writing to program flash that can see what's wrong? I have the lock bits unprogrammed, and SPM and ELPM writes are permitted anywhere. Target is an ATMega128, and I'm using AVR C 3.20A. And I've tried it using an ICE50, a JTAGICE, and a JTAGICE Mk II; all indicate that the program memory isn't changing. I tried the IAR C-SPY simulator, but it doesn't appear to support program memory writes (SPMEN never clears).

Regards,

Colin

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

Quote:
and SPM and ELPM writes are permitted anywhere.

Who have told you this?
"SPM instruction is disabled when executed from the application section."
(C), Mr.Datasheet.

Cats never lie. At least, they do this rarely.

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

micklecat wrote:
Quote:
and SPM and ELPM writes are permitted anywhere.

Who have told you this?
"SPM instruction is disabled when executed from the application section."
(C), Mr.Datasheet.

Thanks! I wasn't locating the 'SPM' code in the bootloader code area. :oops:

It doesn't work perfectly yet, but it's definitley doing more than it was before. :D

Regards,

Colin

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

Be aware that some built in functions are actualy macros that can increase your code level. You are better off writing your own functions. The _EEPUT and _EEGET are examples of this. Also in the _EEPUT macro that I have ( version 1.5c), the interrupts are not disabled while the write operation is being started.

Keep it simple it will not bite as hard

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

sutton wrote:
Be aware that some built in functions are actualy macros that can increase your code level. You are better off writing your own functions. The _EEPUT and _EEGET are examples of this. Also in the _EEPUT macro that I have ( version 1.5c), the interrupts are not disabled while the write operation is being started.

The functions that basically just arrange correct sequencing of the SPM instruction appear to be pretty efficient (the one's shown in the code listing in this posting are intrinsic functions; there are higher level macros that access them). I'm using them as the core of a flash access module, and I'm assuming that I need to take care of EVERYTHING else!

Regards,

Colin