Hi, I'm learning about AVR memories and am curious about how much memory I can use in my Arduino/AVR code without running into issues. Please correct me if ny understanding is wrong (my current work involves both Atmega328P (uno) and Atmega2560 (mega)).
From what I have learnt (skimming through the datasheet, websites)
AVR ATmega processors have 3 memories primarily:
Program flash (32KB for Atmega328P, 256KB for Atmega2560)
Data SRAM (2 KB for Atmega328P, 8 KB for Atmega2560)
EEPROM (1 KB for Atmega328P, 4 KB for Atmega2560)
All the code (/sketch) that we write in the Arduino IDE for example gets compiled and placed in the Flash memory (particularly in the Application Flash Section; there is another section for a Bootloader).
All the data (variables, constants, literals, arrays - both static and dynamically allocated using new) alo that we use in our code (/sketch) goes in the SRAM section?? Is that correct?
I'm a little unsure about point (3) because I tried running the following statement to see how much memory I could allocate with new before the compiler (in the Arduino IDE) complained
saying size of array is too large
#define LARGE_SIZE 32766
uint8_t *chunk = new uint8_t[LARGE_SIZE];
32766 is the max size the IDE allows (for both the boards Uno and Mega) and curiously it is also 2 less than 32768 (32KB) which is the closest power of 2^15. Does this mean that there are 15 address lines for the SRAM memory ? but that would not make sense because the sizes of the SRAM are 2 KB and 8 KB respectively which are way less than 32 KB. Can someone help make sense of this.
Practically speaking, I am working on a project which involves bulk transfer of data. Is there a size (like a general rule of thumb) that I must not exceed when reserving data in my sketches?
depends a little on the board, for the UNO i think that is correct, the Pro-mini and Nano bootloaders take up a bit more space.
The compiler doesn't check for dynamically allocated variable size, it just counts global static variable size and starts sending warnings when this reaches a limit (usually 75%) all local and dynamic variables are not counted or for the dynamically allocated ones just the size of the pointer is counted.
no think that the pointer points to the first point of the index, and the index is of type int. (16 bit signed)
No, the flash memory has that size, the bootloader is part of that flash memory.
The compiler just creates code for the AVR platform, it doesn't know about the actual processor you want to use. The IDE knows what processor you use and will warn you if your statically allocated global variables won't fit.
In the Arduino world you as the programmer take the responsibility of writing the code in a way that you don't run out of memory.
The AVR instruction set uses 16bit values to store pointers and the like. So the value you found is probably some kind of maximum the instruction set is able to handle, so the compiler can set a limit there.
Yes, but that depends on the actual processor used (as you found out yourself). Generally: keep it as small as ever possible, RAM is a rare resource in the AVR world.