How to use EEPROM in megaAVR 0 controllers?

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

I read the ATmega3209 datasheet and try to desperately understand how to use EEPROM. But it seems to be quite unclear and I can not find any application note or example code how to do it in practice. I understood that I can read EEPROM easily with lds beginning from address 0x1400, but writing seems to be more complex.

 

The datasheet says that I should read page buffer by writing to the memory. Does that mean for example:

.eseg

aa:    .db 0x12

 

.cseg

  sts   aa,r16

 

Then it does not change EEPROM but read page buffer to where? Should I allocate page buffer and give SRAM address in NVMCTRL_ADDR before executing sts? Should I then modify values in buffer in SRAM and after everything is OK I give ERWP command in NVMCTRL_CTRLA and it writes the EEPROM?

 

Can I use old method, put data byte in NVMCTRL_DATAL and address in NVMCTRL_ADDR and give ERWP-command? Possibility to write one byte in time is enough, I have to write one or two bytes only when user changes parameters (for example temperature setpoint) manually.

 

I use assembler and Atmel Studio 7.0 and have hobby level experience with older AVR controllers (like ATmega328 and ATXmega64). I have not assembled a circuit yet and can not experiemnt in practice.

 

Best regards,

Hannu

This topic has a solution.
Last Edited: Fri. Mar 27, 2020 - 02:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Some example code from the GCC C compiler's own libraries:

 

http://svn.savannah.gnu.org/viewvc/avr-libc/trunk/avr-libc/libc/misc/eewr_byte.S?revision=2486&view=markup

 

Take the "XMEGA" fork there.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Read/follow the datasheet, its not hard to follow. You now write to the memory mapped address, such as 0x1400 for eeprom, to write to the page buffer. There are differences in eeprom vs flash, and the datasheet will spell them out. The eeprom write is quite simple as the only bytes you have to worry about are the ones you want to change. You still have to consider when crossing a page (when doing more than 1 byte) as the last write to memory mapped address becomes the page buffer address.

 

Simple example in c++ -

 

EEVAR(uint8_t) ee_var1; //default is 0xFF

int main(){
    ee_var1=0x12;
    for(;;){}
}

 

and its asm output, which you can see is quite simple-

000001a4 <main>:
 1a4:    80 91 02 10     lds    r24, 0x1002     ;ee busy?
 1a8:    81 fd           sbrc    r24, 1
 1aa:    fc cf           rjmp    .-8

 

 1ac:    82 e1           ldi    r24, 0x12       ;write value to eeprom address (page buffer)
 1ae:    80 93 00 14     sts    0x1400, r24     ;0x801400 <ee_var1> (vma address)

 

 1b2:    8d e9           ldi    r24, 0x9D       ;spm ccp
 1b4:    84 bf           out    0x34, r24       ;as ctrla is protected

 1b6:    83 e0           ldi    r24, 0x03       ;command ERWP- erase and write page
 1b8:    80 93 00 10     sts    0x1000, r24     ;nvmctrl ctrla

 

 1bc:    ff cf           rjmp    .-2

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

curtvm wrote:
EEVAR(
Where is that defined ?

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

>Where is that defined ?

 

on line 330, where else would it be?

https://github.com/cv007/Avr0PlusPlus/blob/master/Nvmctrl.hpp

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

I was thinking more for OPs benefit. It's not something he's going to find in the support files of the assembler he's using.

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

>It's not something he's going to find in the support files of the assembler he's using.

 

Its also c++ code which does no good for someone using asm, which is why the asm listing was provided. The c++ code was shown to give context to the asm listing produced- 'I'm putting 0x12 in an eeprom location', and show the resulting asm code. There is no need to figure out what is going on behind the scenes in c++ as the asm is right there which is what was wanted.

 

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

Thank you clawson and curtvm for answers. My project has delayed but I think I understand now how to use NVM and access EEPROM.

 

Hannu

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

Time to mark the solution, then?

 

See Tip #5 in my signature, below.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry, this is new function for me. I marked.