Am I overflowing my AVR's flash memory with a program that's too big?
I have a project where an ATtiny2313V is controlling a 7x5 LED matrix to display scrolling text. To display the text, I built a font which is stored in the flash with the rest of the program.
The whole program, including the entire font, takes up 1106 bytes. But when I load it into the chip, it doesn't seem to run; instead it just lights up a couple of the LED and that's it.
However, when I remove most of the font, and compile with only the letters A to J, the program is 878 bytes in size, and runs just fine.
Is this because of some kind of overflow of the AVR flash memory?
The datasheet for the ATtiny2313V says it has 2KByte of flash! How can 1106 bytes be too much?
UPDATE: Just to be clear, the tool chain I'm using is AVR Studio (to compile the code) and then AVRDude to upload it to the micro-controller. As far as I know, AVR Studio uses a version of avr-gcc 开发者_JAVA百科to compile the code.
I'm not sure what tool chain you're using, but in avr-gcc you'll need to use the <avr/pgmspace.h>
header to store & access data in flash - it's not enough just to declare your data const
as it is still loaded into memory at runtime, and as such takes up space in both flash and ram (just as any other initialised variable).
Check out the User Manual and the Header Docs for more information. Useage is fairly simple, to declare a char array in flash, use the PROGMEM
macro:
char data[] PROGMEM = {0xc4, 0x77}; // etc
Then in order to access the data, you need to use the supplied macros
char d = pgm_read_byte(&(data[i]));
Edit: Also keep in mind that avrdude only reports the statically allocated portions of ram (.data and .bss) for globals and static variables etc. You need to leave room for the stack - how much exactly depends on your program (hint: recursion is bad).
I swear there's something magical about SO; I've been wracking my brains for weeks, trying to figure this out, and after asking the question here - I finally can see what's been staring me in the face!
Below is the memory usage for compiling with only the A-J letters in the font:
AVR Memory Usage
----------------
Device: attiny2313
Program: 872 bytes (42.6% Full)
(.text + .data + .bootloader)
Data: 82 bytes (64.1% Full)
(.data + .bss + .noinit)
And here it is again, with the letters A-Z:
AVR Memory Usage
----------------
Device: attiny2313
Program: 952 bytes (46.5% Full)
(.text + .data + .bootloader)
Data: 162 bytes (126.6% Full)
(.data + .bss + .noinit)
See the 126.6%
in the Data? Oops! I really did overflow!
Check that you're not overflowing your stack? That can produce crashes that are hard to detect. You can either set your stack size somewhere in the compiler/linker settings, or you can convert some local variables to global variables. An embedded processor usually doesn't have any checks for a stack overflow, it just crashes.
精彩评论