Teensyduino F() behavior

Status
Not open for further replies.

dlchambers

Well-known member
The Arduino F() macro places a string literal in PROGMEM (reducing the RAM footprint)

I'm using a Teensy 3.2, and need more RAM space, so I wrapped a number of Serial.print() strings in F().
But I saw no change in the mem sizes of Flash & RAM.

So I grepped for F() and found these defs:

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy\WString.h:
#define F(string_literal) ((const __FlashStringHelper *)(PSTR(string_literal)))

C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy3\WString.h
#define F(string_literal) ((const __FlashStringHelper *)(string_literal))

which seems to explain it - the T3 macro doesn't use PSTR(), which is the thing that places it in Flash... on a T3 the F() macro has no effect.

Can anyone explain:
a) Why the teensy and teensy3 macros re different?
b) The "correct" way to make it work on a Teensy3?

Thanks,
-Dave
 
I can't provide the level of detail you're looking for, but I remember when I left the AVR boards that F() does nothing, at least on the Due and Teensy 3.6. I did some cursory searching and it seemed that the ARM processors place strings in PROGMEM by default, so F() is redundant and does nothing. I would also appreciate some elaboration on this point.
 
F() is not needed on Teensyduino. String constants are in stored in the flash automatically (at least on Teensy 3.x)
 
The flat 32 bit addressing of the Teensy ARM's allows anything seen as or marked 'const' into Flash during the build - this includes inline strings. If there are data arrays or other than can be marked const they will push to Flash from RAM and are directly addressable at runtime. F() and PROGMEM are not needed/useful.
 
a) Why the teensy and teensy3 macros re different?

The older Teensy 2.0 boards are based on 8 bit AVR. The newer Teensy 3.x & LC use 32 bit ARM.

b) The "correct" way to make it work on a Teensy3?

If you want your code to be compatible with older boards and Arduino's boards, use F("mystring").

Otherwise, just use "mystring" in your code. On 32 bit ARM, string literals and anything defined with "const" automatically go into flash memory. No PROGMEM, F() or PSTR() needed.

However, PROGMEM, F() and PSTR() are still defined as do-nothing macros, so you use them if you want to make your code compile well on old boards.
 
Status
Not open for further replies.
Back
Top