Operácie

AVR Pamäťové priestory

Z SensorWiki

The memory space “seen” by AVRGCC runs from addresses:

0×00000000 up to 0×0007FFFF for Flash memory,
0×00800000 up to 0×0080FFFF for RAM, and
0×00810000 up to 0×0081FFFF for EEPROM.

This means if you need your variable to be stored at specific RAM address you need to add offset 0×00800000 to your desired RAM address like if you want the .data section to start at 0×1100, pass 0×801100 at the address to the linker.

Same is with EEPROM just offset is 0×00810000. For flash memory you don’t need to add offsets.


EEPROM

How to access AVR microcontroller EEPROM (Electronically Erasable Read-Only memory) memory in C?

This type of memory allows developers to store constants, parameters, values and are stored also after the power loss.

The AVR's internal EEPROM is accessed via special registers inside the AVR, which control the address to be written to (EEPROM uses byte addressing), the data to be written (or the data which has been read) as well as the flags to instruct the EEPROM controller to perform a write or a read.


To use this memory area just include eeprom.h header library from avr directory

#include "avr/eeprom.h"

Variable declaration (use an attribute EEMEM):

 #include "avr/io.h"
 #include "avr/eeprom.h"

  uint8_t EEMEM eeprombyte=0x10;              // Store a byte
 uint16_t EEMEM eepromword=0x5555;            // Store a word
  uint8_t EEMEM eepromstring[5]={"Test\0"};   // Store a string

int main(void)
{
  uint8_t RAMbyte;                            //Standard variable in RAM (byte)
 uint16_t RAMword;                            //Standard variable in RAM (word)
  uint8_t RAMstring[5];

//read byte from EEPROm and store to RAM

 RAMbyte = eeprom_read_byte(&eeprombyte);
 RAMword = eeprom_read_word(&eepromword);
 eeprom_read_block ((void *)&RAMstring, (const void *)&eepromstring,5);

return (0);
}

Don't forget that compiler creates separate .eep file which has to be written to chip separately!


Flash

Example of table, located in flash:

#include
uint8_t sinetable[] PROGMEM = { … };

level = pgm_read_byte_near(sinetable + phase);

Practically speaking you may ask where to define these sections? Returning to my example I defined my signal table this way:

//……………………………………………………..

//signals saved in flash memory

const uint8_t sinewave[] __attribute__ ((section (”.MySection1″)))= //256 values

{0×80,0×83,0×86,0×89,0×8c,0×8f,0×92…

//……………………………………………………..

you see I placed __attribute__ ((section (”.MySection1″))) instead of PROGMEM. This means that instead letting compiler decide whereto store this table I will deine this place by myself.

Now I need to define Mysection1. Lets say I want to put this table at flash address 0×1F00. Then I need to tel linker to put this section there by using command:

………………………………………………….

-Wl,--section-start=.MySection1=0x1F00

…………………………………………………

How to do this? If you work With WinAVR tool-set just open Makefile and find where is

……………………………………………………..

CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))

CFLAGS += $(CSTANDARD) located.

…………………………………………………….

At the end write additional line:

…………………………………………………….

CFLAGS += -Wl,-section-start=.MySection5=0×1F00