Runtime sketch size for Mega

Hi,

I would like to know the amount of used flash (sketch size) runtime for an Arduino Mega 2560. It is displayed on a webpage, and I might store it to a log file.

Now that my sketch is over the 64kbyte border, the size can no longer be calculated.

Compiler: flash: 68.062 bytes (26%). Maximum is 253.952 bytes. ram: 1.948 bytes (23%). Maximum is 8.192 byte

Runtime: flash: 2782 bytes :o That's wrong. ram: 2820 bytes (32.40%)

The address of _etext and _edata are 0x0000088C and 0x00000352. I read a note somewhere, that between 64kbyte and 128kbyte all the upper 16 bits are '1', but they are not.

    Serial.print(F("&_etext=0x"));
    Serial.print( (unsigned long) &_etext, HEX);
    Serial.print(F(", &_edata=0x"));
    Serial.print( (unsigned long) &_edata, HEX);
    Serial.println();

Shows: "&_etext=0x88C, &_edata=0x352".

What do I have to do to get the sketch size runtime ?

It is displayed on a webpage

I'm a firm believer in facts and data, but, WTF for? There is NOTHING you can do to change the size of a sketch at run time, so this "fact" seems completely pointless.

PaulS: I'm a firm believer in facts and data, but, WTF for? There is NOTHING you can do to change the size of a sketch at run time, so this "fact" seems completely pointless.

Think again, I believe Peter_n is testing a custom bootloader with hooks that can be called from a program to store data in the flash memory at runtime. So it would be nice to know where the program ends and where you can start writing flash data.

See here.

It's being truncated to 16 bits somewhere along the line (unsigned 16 bit integer max value is 64K ), it looks like... The numbers work out for that, right? Just knowing that may be enough to work around it, assuming it continues behaving like that..

I'm not using it for a special bootloader, just to show the data. On a webpage I show information (the time, my internet IP number, Arduino version, fuses, and so on), and I keep my eye on the ram usage as my sketch is growing. The sketch size did work untill now, since I'm beyond the 64kbyte limit.

I have added the code in my first post to show how I cast the address to an unsigned long. I don't understand what could be wrong with that.

I have added the code in my first post to show how I cast the address to an unsigned long. I don't understand what could be wrong with that.

Are you getting the size of the last code page, instead of the size of all code pages? Do you need to count how many code pages are in use? No clue, really, just tossing out ideas.

Yes, the size of a complete 64k segment is missing. The normal correction is -256, but above the 64kbyte limit the correction should be -512 (so I have read).

This prints the correct value:

Serial.print( (unsigned long) &_etext + (unsigned long) &_edata - 512UL + (1024UL * 64UL));

Compiler : 68.142 bytes Added 64k : 68142

How do I know that I'm above the 64kbyte limit? and what will happen at the 128kbyte limit ?

This sketch is able to fill every Arduino with code.
Uncomment the lines in the loop() function to create code for a certain code size.
It generates many numbers, but I have no clue to do with the numbers.

// Test sketch
// For forum, to find a way to calculate 
// runtime the sketch size for Arduino Mega

extern int _etext;
extern int _edata;
extern int __ctors_start;
extern int __ctors_end;
extern int __dtors_start;
extern int __dtors_end;
extern int __trampolines_start;
extern int __trampolines_end;
extern int __data_start;
extern int __data_end;
extern int __bss_start;
extern int __bss_end;
extern int __data_load_start;
extern int __data_load_end;
extern int __noinit_start;
extern int __noinit_end;
extern int __heap_start;
extern int __heap_end;
// extern int __eeprom_start;
extern int __eeprom_end;

volatile float vq;
#define Q  vq=Filler()
#define R  {Q;Q;Q;Q;Q;Q;Q;Q;}
#define S  {R;R;R;R;R;R;R;R;}
#define T  {S;S;S;S;S;S;S;S;}
#define U  {T;T;T;T;T;T;T;T;}
#define V  {U;U;U;U;U;U;U;U;}
#define W  {V;V;V;V;V;V;V;V;}
#define X  {W;W;W;W;W;W;W;W;}
#define Y  {X;X;X;X;X;X;X;X;}
#define Z  {Y;Y;Y;Y;Y;Y;Y;Y;}

void setup() {
  Serial.begin(9600);
  Serial.println(F("\nTest sketch"));
  Serial.print(F("_etext=0x"));
  Serial.println( (unsigned long) &_etext, HEX);
  Serial.print(F("_edata=0x"));
  Serial.println( (unsigned long) &_edata, HEX);
  Serial.print(F("__ctors_start=0x"));
  Serial.println( (unsigned long) &__ctors_start, HEX);
  Serial.print(F("__ctors_end=0x"));
  Serial.println( (unsigned long) &__ctors_end, HEX);
  Serial.print(F("__dtors_start=0x"));
  Serial.println( (unsigned long) &__dtors_start, HEX);
  Serial.print(F("__dtors_end=0x"));
  Serial.println( (unsigned long) &__dtors_end, HEX);
  Serial.print(F("__trampolines_start=0x"));
  Serial.println( (unsigned long) &__trampolines_start, HEX);
  Serial.print(F("__trampolines_end=0x"));
  Serial.println( (unsigned long) &__trampolines_end, HEX);
  Serial.print(F("__data_start=0x"));
  Serial.println( (unsigned long) &__data_start, HEX);
  Serial.print(F("__data_end=0x"));
  Serial.println( (unsigned long) &__data_end,HEX);
  Serial.print(F("__bss_start=0x"));
  Serial.println( (unsigned long) &__bss_start, HEX);
  Serial.print(F("__bss_end=0x"));
  Serial.println( (unsigned long) &__bss_end, HEX);
  Serial.print(F("__data_load_start=0x"));
  Serial.println( (unsigned long) &__data_load_start, HEX);
  Serial.print(F("__data_load_end=0x"));
  Serial.println( (unsigned long) &__data_load_end, HEX);
  Serial.print(F("__noinit_start=0x"));
  Serial.println( (unsigned long) &__noinit_start, HEX);
  Serial.print(F("__noinit_end=0x"));
  Serial.println( (unsigned long) &__noinit_end, HEX);
  Serial.print(F("__heap_start=0x"));
  Serial.println( (unsigned long) &__heap_start, HEX);
  Serial.print(F("__heap_end=0x"));
  Serial.println( (unsigned long) &__heap_end, HEX);
  // Serial.print(F("__eeprom_start=0x"));
  // Serial.println( (unsigned long) &__eeprom_start, HEX);
  Serial.print(F("__eeprom_end=0x"));
  Serial.println( (unsigned long) &__eeprom_end, HEX);
}

void loop() {
  // For Arduino 1.6.5:
  //    About 1210 byte per "S;"
  //    About 10kbyte per "T;"
  //    About 82kbyte per "U;"
  // Uncomment the appropriate line to fill the flash code.

  // Maximum for Arduino Uno
  // T;T; S;S;S;S;S;S;

  // Maximum for Arduino Leonardo
  // T;T; S;

  // Arduino Mega 2560, 0...32k
  // T;T;

  // Arduino Mega 2560, 32...64k
  // T;T;T;T;

  // Arduino Mega 2560, 64...128k
  // U;

  // Arduino Mega 2560, 128...256k
  // U;U;U;
}

float Filler( void)
{
  return( (float) random( 0, 100));
}