c - Program memory space is being overwritten -
i writing firmware atmel mega645a controller using atmelstudio7. have bunch of messages display on 16x2 character display. in order preserve sram space, attempting store these messages in rom program memory. problem seems overwriting parts of program memory being used other code. here details:
i have following statements near beginning of main program:
#include "adht.h" #include "languages.h"
in adht.h have following:
#include <avr/pgmspace.h>
i believe nut of problem in header file languages.h, created. in languages.h have hundreds of lines of code this:
const char m1_1_1[] progmem = {32,32,32,32,69,110,116,101,114,105,110,103}; const char m1_2_1[] progmem = {32,32,83,116,97,110,100,98,121,32,77,111,100,101}; const char m2_1_1[] progmem = {32,32,70,87,32,82,101,118,105,115,105,111,110}; const char m2_2_1[] progmem = {32,32,32,32,40,110,117,109,98,101,114,41}; const char m3_1_1[] progmem = {32,78,79,46,32,79,70,32,84,72,69,82,65,80,89}; const char m3_2_1[] progmem = {32,32,32,32,83,69,83,83,73,79,78,83,58}; . . . etc. etc.
when compile ~660 lines this, atmelstudio reports following: program memory usage : 34892 bytes 53.2 % full data memory usage : 2919 bytes 71.3 % full eeprom memory usage : 170 bytes 8.3 % full
if comment out of "const char ...." lines, , rebuild, this:
program memory usage : 34896 bytes 53.2 % full data memory usage : 2919 bytes 71.3 % full eeprom memory usage : 170 bytes 8.3 % full
... looks same. both builds allow rest of program run no problem.
here running in trouble. further on in languages.h have following code:
pgm_p const string_table[] progmem = { m1_1_1 , m1_1_2 , m1_1_3 , m1_1_4 , m1_1_5 , m1_1_6}; /* , m1_2_1 , m1_2_2 , m1_2_3 , m1_2_4 , m1_2_5 , m1_2_6 , m2_1_1 , m2_1_2 , m2_1_3 , m2_1_4 , m2_1_5 , m2_1_6 , m2_2_1 , m2_2_2 , m2_2_3 , m2_2_4 , m2_2_5 , m2_2_6 , m3_1_1 , m3_1_2 , m3_1_3 , m3_1_4 , m3_1_5 , m3_1_6 , . . . etc. etc.
now note after first line of m1_1_1... have commented out "/" rest of "pointer assignments". rest assured have "/" @ end of assignments. if have 6 pointer assignments, code works ok. if move "/*" down couple lines couple more lines of code compiled, rest of program starts freak out if out of program memory. program fails in different ways depending on how of "pointer assignments" compiled. (note having problems typing "asterisk backslash" in forum). when compile 8 lines of code not commented out, compiler reports following:
program memory usage : 35530 bytes 54.2 % full data memory usage : 2919 bytes 71.3 % full eeprom memory usage : 170 bytes 8.3 % full
and when un-comment these lines, compiler reports following:
program memory usage : 44690 bytes 68.2 % full data memory usage : 2919 bytes 71.3 % full eeprom memory usage : 170 bytes 8.3 % full
so filling program memory, not getting close limit.
to complete, in main program copy messages program memory sram follows:
char line1[16]; char line2[16]; pgm_p p; memcpy_p(&p, &string_table[0], sizeof(pgm_p)); strcpy_p(line1, p); memcpy_p(&p, &string_table[1], sizeof(pgm_p)); strcpy_p(line2, p);
later on send line1 , line2 display.
so seems me problem in my
pgm_p const string_table[] progmem =
line(s). can please tell me how not overwriting rest of program?
update: in main program, if comment out following lines of code before compiling, program memory usage drops (to 54%), , rest of program works fine:
char line1[16]; char line2[16]; pgm_p p; memcpy_p(&p, &string_table[0], sizeof(pgm_p)); strcpy_p(line1, p); memcpy_p(&p, &string_table[1], sizeof(pgm_p)); strcpy_p(line2, p);
when un-comment code , re-compile, program memory usage goes 68%, , rest of program freaks out when run it. these lines of code overwriting program memory?
update #2: in lines of code above, if comment out these 2 lines:
strcpy_p(line1, p); strcpy_p(line2, p);
my program works. of course still not have access data stored in program memory though. please tell me going on. how data (an array of characters, or alternatively array of uint_8's) ram can display 16x2 screen?
in standard c, if have in header file:
const char m1_1_1[] = {32,32,32,32,69,110,116,101,114,105,110,103};
and 2 or more translation units include header file, undefined behaviour due having multiple definitions of same object. manifest high memory usage due compiler/linker using memory each definition, or else.
if can rearrange code definitions appear in .c
file, might solve problem. other units can access data extern const char m1_1_1[];
.
Comments
Post a Comment