Go Down

Topic: Function fails with passed in variable, but works if set manually. (Read 1 time) previous topic - next topic

Erdin

Thanks for noticing that. I only checked the flash size and didn't check the ram size.
I tried a few more tests, and did some "avr-objdump -d" on the object file, but that didn't make things clear for me.

westfw

I have a hunch.  Try:
Code: [Select]
Serial.println((_FlashStringHelper*)  (  optCmd[i])  ) ;
I'd have to dig into docs to figure out if the extra parens are necessary, and think WAY to hard to figure out why it worked for the case with the constant 0 index, but I think the behavior is consistent with getting the cast vs array index order of operations wrong.

pYro_65

Code: [Select]
Serial.println((__FlashStringHelper*)flashArray[i]);

needs to be: ( your pointers to the strings are also in flash, get them first ):

Code: [Select]
Serial.println((__FlashStringHelper*) pgm_read_word( &flashArray[i] ) );

Also keep your vars const for fun ( pointer and var ):

Code: [Select]
const char * const flashArray[] PROGMEM = { string1, string2, string3 };

Finally if all else fails, replace PROGMEM to '__attribute__((section(".progmem.data.mysection")))'
volatile data can mess the sections up if mixed in the same file.

allenhuffman

westfw, no change on the parens. I really believe it's a compiler issue. It works fine, 100% of the time, with just the call, but place something else around it, and it stops working. When I got down to just doing a "delay(1);" and it broke, I pretty much gave up.

I developed my own routines for handling these items when I ported an old program that expected 21,000 bytes of RAM to work on the Arduino's 2K (just an impractical amount of array storage to be useful), but I really thought I could simplify things once I found that all the Print:: functions were already handling __FlashStringStorage* (so why do it twice).

If I find anything new, I will share it here.
Embedded Software Engineer
UNO | Leonardo | Due | Teensy | BASIC Stamp
http://www.subethasoftware.com

pYro_65


I really believe it's a compiler issue.

Nope,

Because your flasharray is an array of progmem pointers, not data. You must use pgm_read_word to retrieve the pointer.

Code: [Select]
Serial.println((__FlashStringHelper*)flashArray[i]);

This is reading the i'th value in sram, you placed the data in PROGMEM. I can see that it works on occasion for you ( and my test ), but it is still wrong.

Use this:
Code: [Select]
Serial.println((__FlashStringHelper*) pgm_read_word( &flashArray[i] ) );

Or change your flasharray to this ( no PROGMEM ):

Code: [Select]
const char * flashArray[] = { string1, string2, string3 };

Go Up