1 / 26

Arduino Memory

Arduino Memory. Paul MacDougal Triembed.org July 11, 2016. Disclaimer. I will be talking about Arduino UNO with ATmega328P processor. ATTiny, Edison, Cortex M0, … will differ in important ways.

gguerrero
Download Presentation

Arduino Memory

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arduino Memory Paul MacDougal Triembed.org July 11, 2016

  2. Disclaimer • I will be talking about Arduino UNO with ATmega328P processor. ATTiny, Edison, Cortex M0, … will differ in important ways. • I will be talking about the 1.6.7 Arduino IDE. Details may differ in important ways if you are using a different IDE.

  3. ATmega328PMemory • 32 kBytes FLASH • 2 kBytes internal SRAM • 1kBytes EEPROM

  4. Harvard Architecture • ATmega chips are “Harvard Architecture” • This means they have one memory for instructions (the 32K of FLASH) and another memory for data (the 2K of SRAM) Image from Wikipedia

  5. Data address space • 2K SRAM plus 256B of interrupt vectors • Address 0-255 – interrupt vectors • Address 256 – 2303 – SRAM • Initialized global variables (.DATA) • Uninitialized global variables (.BSS) • Heap • Stack

  6. Program Address Space • 32K FLASH • Addresses 0 – 32767 • Initialized global variable values (.DATA) • Bootloader • Arduino support code • Libraries • Your code

  7. IDE info • When you compile and upload a program, the IDE will tell you how much program space (FLASH) is used. • And how much data space (SRAM) is used by global variables Sketch uses 3,694 bytes (11%) of program storage space. Maximum is 32,256 bytes. Global variables use 304 bytes (14%) of dynamic memory, leaving 1,744 bytes for local variables. Maximum is 2,048 bytes.

  8. The compiler knows all! • .Data: __data_start to __data_end • .BSS: __bss_start to __bss_end • Heap: __heap_start to __brkval • Stack: SP to RAMEND

  9. You can access these symbolsin your code • extern char *__data_start; • extern char *__data_end; • extern char *__bss_start; • extern char *__bss_end; • extern char *__heap_start; • extern char *__brkval; • SP • RAMEND

  10. +----------------+ __data_start = 256 + data + + variables + data_size = 618 + + +----------------+ __data_end-1 = 873 +----------------+ __bss_start = 874 + bss + + variables + bss_size = 658 + + +----------------+ __bss_end-1 = 1531 +----------------+ __heap_start = 1532 + + + heap + heap_size = 38 + + +----------------+ heap_end = 1569 available = 711 +----------------+ Current STACK = 2280 + + + stack + stack_size = 23 + + +----------------+ RAMEND = 2303 Global variables use 1,276 bytes (62%) of dynamic memory, leaving 772 bytes for local variables.

  11. What if you want more detail? • When you install the Arduino IDE, the Atmel AVR tools are placed in:...\hardware\tools\avr\bin • avr-nm.exe – dump symbols • avr-objdump.exe – dump more

  12. The IDE uses a temporary directory for compiling. E.g. C:\Users\paul\AppData\Local\Temp build25a60484b83419d86b0f9123905ed449.tmp There, you will find an elf file that contains the code to be uploaded to the Arduino. You can run nm on that file: avr-nm.exe –n *.ino.elf

  13. 00000000 a __tmp_reg__ 00000000 W __vector_default 00000000 T __vectors 00000001 a __zero_reg__ 0000003d a __SP_L__ 0000003e a __SP_H__ 0000003f a __SREG__ 00000068 T __trampolines_end 00000068 T __trampolines_start 00000068 t _ZZ5setupE3__c 0000007f T digital_pin_to_timer_PGM 00000093 T digital_pin_to_bit_mask_PGM 000000a7 T digital_pin_to_port_PGM 000000bb T port_to_input_PGM 000000c5 T port_to_output_PGM 000000cf T port_to_mode_PGM 000000da T __ctors_start 000000dc T __ctors_end 000000dc T __dtors_end 000000dc T __dtors_start 000000dc W __init . . . T – .text B – .bss D - .data N – debugging symbol 00800100 D __data_start . . . 0080036a B __bss_start 0080036a D __data_end . . . 00800519 b _ZL6buffer 00800527 b _ZL5error 00800528 b _ZL5state 00800529 B _ZN8SPIClass13interruptSaveE 0080052a B _ZN8SPIClass13interruptMaskE 0080052b B _ZN8SPIClass13interruptModeE 0080052c B ServoCount 0080052d b _ZL7Channel 0080052e b _ZL6servos 00800552 b timer0_fract 00800553 B timer0_millis 00800557 B timer0_overflow_count 0080055b B Serial 008005f8 B __brkval 008005fa B __flp 008005fc B __bss_end 008005fc N __heap_start 008005fc N _end 00810000 N __eeprom_end

  14. So, which are your biggest routines? 00000cc8 T _Z9drawGlyphhhhh 470 bytes 00000e9e T _Z17copyPageToDisplaysh 382 bytes 0000101c T _Z14drawBackgroundv 266 bytes 00001126 T setup 466 bytes 000012f8 T _Z10drawLatLonfhh 938 bytes 000016a2 T _Z13handleDisplayv 786 bytes 000019b4 T _Z10handle_GPSv 296 bytes 00001adc T _Z10handleTimev 422 bytes 00001c82 T _Z20handle_state_machin 658 bytes 00001f14 T _Z9handle_swv 222 bytes 00001ff2 T loop 350 bytes 00002146 t ...

  15. How can I move stuff to FLASH? • Use PROGMEM variable modifier #include <avr/pgmspace.h> const unsigned char PROGMEM glyphWidth[] = {\ 0, 5,10, 6,10,10,11,10,\10,10,10,10, 2, 9,13,11,\12,12,11,11}; uint8_t width = pgm_read_byte_near((int)(&glyphWidth[0]) + n); -------------------------------------------- #define pgm_read_byte_near(address_short) \ __LPM((uint16_t)(address_short))

  16. Not limited to bytes • pgm_read_word_near • pgm_read_dword_near • pgm_read_float_near • pgm_read_ptr_near

  17. PSTR() • # define PSTR(s) (__extension__({static const char __c[ ] PROGMEM = (s); &__c[0];}))

  18. memchr_P memcmp_P memccpy_P memcpy_P memmem_P memrchr_P strcat_P strchr_P strchrnul_P strcmp_P strcpy_P strcasecmp_P strcasestr_P strcspn_P strlcat_P strlcpy_P __strlen_P strnlen_P strncmp_P strncasecmp_P strncat_P strncpy_P strpbrk_P strrchr_P strsep_P strspn_P strstr_P strtok_P strtok_rP P versions

  19. Example #include <avr/pgmspace.h> unsigned char inputBuffer[20]; if (0 == strcmp_P(inputBuffer, PSTR("SUCCESS")) { }

  20. F() Serial.print(“This string is in SRAM”); Serial.print(F(“This string is in FLASH”));

  21. Resources • http://www.nongnu.org/avr-libc/user-manual/malloc.html • wikipedia • http://www.gammon.com.au/progmem

  22. Backup Slides

  23. F() WString.h class __FlashStringHelper; #define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal))) size_t Print::print(const __FlashStringHelper *ifsh) { PGM_P p = reinterpret_cast<PGM_P>(ifsh); size_t n = 0; while (1) { unsigned char c = pgm_read_byte(p++); if (c == 0) break; if (write(c)) n++; else break; } return n; }

  24. von Neumann Architecture • Von Neumann machines have one memory in which both instructions and data are stored Image from Wikipedia

  25. Modified Harvard Architecture • Intel and ARM processors are “modified” Harvard. They have caches that hold either data or instructions, but both are fed from a single memory source.

  26. A - The symbol's value is absolute, and will not be changed by further linking. • B,b - The symbol is in the uninitialized data section (known as BSS). • C - The symbol is common. Common symbols are uninitialized data. When linking, multiple common symbols may appear with the same name. If the symbol is defined anywhere, the common symbols are treated as undefined references. For more details on common symbols, see the discussion of –warn-common in Linker options. • D,d - The symbol is in the initialized data section. • G,g - The symbol is in an initialized data section for small objects. Some object file formats permit more efficient access to small data objects, such as a global int variable as opposed to a large global array. • I - For PE format files this indicates that the symbol is in a section specific to the implementation of DLLs. For ELF format files this indicates that the symbol is an indirect function. This is a GNU extension to the standard set of ELF symbol types. It indicates a symbol which if referenced by a relocation does not evaluate to its address, but instead must be invoked at runtime. The runtime execution will then return the value to be used in the relocation. • I - The symbol is an indirect reference to another symbol. • N - The symbol is a debugging symbol. • p - The symbols is in a stack unwind section. • R,r - The symbol is in a read only data section. • S,s - The symbol is in an uninitialized data section for small objects. • T,t - The symbol is in the text (code) section. • U - The symbol is undefined. • u - The symbol is a unique global symbol. This is a GNU extension to the standard set of ELF symbol bindings. For such a symbol the dynamic linker will make sure that in the entire process there is just one symbol with this name and type in use. • V,v - The symbol is a weak object. When a weak defined symbol is linked with a normal defined symbol, the normal defined symbol is used with no error. When a weak undefined symbol is linked and the symbol is not defined, the value of the weak symbol becomes zero with no error. On some systems, uppercase indicates that a default value has been specified. • W,w - The symbol is a weak symbol that has not been specifically tagged as a weak object symbol. When a weak defined symbol is linked with a normal defined symbol, the normal defined symbol is used with no error. When a weak undefined symbol is linked and the symbol is not defined, the value of the symbol is determined in a system-specific manner without error. On some systems, uppercase indicates that a default value has been specified.

More Related