HOWTO: Store Projects settings (like F_CPU, USB, Keyboard-layout)

Frank B

Senior Member
HOWTO: Store Project Settings

- Advanced users only -

I found a way to store project-settings.
Why ?
I often need different setting for projects, and often I forget to select the correct options in the IDE.

The following shows a way to store them on a per-project-basis.
For example: A Sketch needs - or works best - with F_CPU=144Mhz

Howto:
Copy the attached platform.txt to Arduino\hardware\teensy\avr\platform.txt (overwrite the existing, but make a backup before)

- It adds the following command to the commandline of GCC:"@{build.path}\sketch\defs.h"
- If the file does not exist, it writes an empty defs.h to prevent error-msgs during compilation

Create a file "defs.h" in your project-directory (where the .ino file is)
Example file: (overrides the F_CPU setting)
Code:
-UF_CPU
-DF_CPU=144000000

- The first line undefines the existing definition
- The second line defines a new F_CPU (144MHz in this case)

You're done.
Sketch-example:
Code:
void setup() {
 delay(1000);
 Serial.println(F_CPU);
}

void loop() {}
1. Create the mentioned defs.h file above in the same directory.
2. Compile the sketch with - for example 24MHz - and watch the output.

The file-extension ".h" is not the best - i tried an other one, but it did not work (the file was not copied to the build-directory)
(ideas?)


In the attached platform.txt is a line:
recipe.hooks.sketch.prebuild.1.pattern.windows=cmd /c if not exist "{build.path}\sketch\defs.h" "echo.>{build.path}\sketch\defs.h"
[edit]We need one more line for MAC:
recipe.hooks.sketch.prebuild.1.pattern.macosx=

Would be great if someone can test this and says "Yes, works!" :cool:



EDIT: Replaced platform.txt with a non-TYQT-version

EDIT: Find platform.txt for Arduino 1.8.5 in this post: https://forum.pjrc.com/threads/3853...yboard-layout)?p=172835&viewfull=1#post172835

EDIT: Find platform.txt for Arduino 1.8.7 in this post: https://forum.pjrc.com/threads/38533-HOWTO-Store-Projects-settings-(like-F_CPU-USB-Keyboard-layout)?p=187712&viewfull=1#post187712





 

Attachments

  • platform.txt
    4.9 KB · Views: 245
Last edited:
One was to do it is to use conditional compilation to use #error if the settings aren't right.

For example TeensyTransfer uses the following to make sure the speed is 120 Mhz or less:

Code:
#if F_CPU>120000000
#error LOWER CPU SPEED TO 120MHz!!! ...or remove this line and know that the printed Serial and MAC will not be correct :)
#endif
 
Other possibilites:

Want to use a special optimization-level ?:
Code:
-O2

In addition, it allows to publish projects with a set of options.
 
Last edited:
Hi Frank,

Looks interesting.

It is sort of along the lines I was asking about yesterday in the posting: https://forum.pjrc.com/threads/3839...DE-replacement?p=119829&viewfull=1#post119829

In particular if you look at the standard AVR platform.txt file, it has a section in the file:
Code:
# This can be overridden in boards.txt
build.extra_flags=

# These can be overridden in platform.local.txt
compiler.c.extra_flags=
compiler.c.elf.extra_flags=
compiler.S.extra_flags=
compiler.cpp.extra_flags=
compiler.ar.extra_flags=
compiler.objcopy.eep.extra_flags=
compiler.elf2hex.extra_flags=

That it looks like there is a setup to be able to use platform.local.txt, but I have not figured out where these files can be setup.

Then their receipes use these symbols, like:
Code:
## Compile c++ files
recipe.cpp.o.pattern="{compiler.path}{compiler.cpp.cmd}" {compiler.cpp.flags} -mmcu={build.mcu} -DF_CPU={build.f_cpu} -DARDUINO={runtime.ide.version} -DARDUINO_{build.board} -DARDUINO_ARCH_{build.arch} {compiler.cpp.extra_flags} {build.extra_flags} {includes} "{source_file}" -o "{object_file}"
So was asking if this worked this way, if it made sense to add these to the Teensy version of platform.txt.

Thanks Frank
 
So far I have not seen any good documentation on it yet, only some forum posts and the like up on Arduino forum... May have to pull out a non-teensy board and see what it does
 
@Paul:
And, finally, this is a way to prevent inclusion of unused serial-devices and their RAM-consuming buffers.
 
Frank: Quick update, I tried making a simple blink program, that allowed me to define the blink rate... tried putting in platform.local.txt into the sketches folder and it was not picked up.

I see back at the Arduino issues: https://github.com/arduino/Arduino/issues/3371
matthijskooijman said:
I added platform.local.txt but never got around to documenting it once the PR got merged.

The main usecase for platform.local.txt is to allow overriding values in a platform.txt file which is being tracked by e.g. git, so you prevent having modified files. I think it does not allow modifying platform.txt from another directory (e.g. I don't think you can put a sketchbook/hardware/arduino/avr/platform.local.txt file to override arduino_install_dir/hardware/arduino/avr/platform.txt).

You say you cannot override the compiler.c.flags? I think I had an override for that for a while (to add C++11 support), though I'm not entirely sure now. Where is your platform.local.txt?
So it looks like it is simply a way for you to add your own settings for all projects that use that platform, as to not edit the main github file...

So I think your approach is looking more promising.
 
LOL

6 Downloads, and nobody told me that the platform.txt was the TYQT version (does not work without it) ???

Post #1 updated.
 
Last edited:
Sorry, I meant to mention it. Also it was specific to I believe the 32 bit version of TyQt...

Yes - So of hard to decide what file name to use, I understand the idea of the .h as you can then see the file as part of your sketch.

Actually I wish there was a way that the options could be specified as part of the main .ino file. I believe I remembered some other IDEs in the past where you could have something like either special comments or #pragma like statements that could specify things like, which processor and maybe comm port... But my guess is that would be a lot more work...
 
Kurt,

.h : The builder does not copy other files to the buidl-directory. I tried an other extension, but this does not work.
But seeing the file in the Ardio-IDE is not bad..

Other IDEs: Yes.. Arduino is the only "IDE" I know, that does NOT store project settings. It would be so easy to add it. I have no idea why they don't add this feature.:mad:
 
Last edited:
Linux:
I guess, the line should be something like
Code:
recipe.hooks.sketch.prebuild.1.pattern.linux= bash -c "[ -f \"{build.path}/sketch/defs.h\" ] || touch \"{build.path}/sketch/defs.h\""
But it complains about a missing "]"
I'm not that linux expert ...
 
Ok, I found the problem. Linux works now. I have updated the platform.txt in post #1
MAC: still a TODO.
 
Last edited:
Good work Frank - Everyone should use TYQT - it would have worked for me :)

When you build a sketch there is a JSON file in that directory - shown on verbose output:
"C:\Users\xyzz\AppData\Local\Temp\buildfcbd65d9e8deac566b2daf204e8c1391.tmp\build.options.json"

Not sure where this comes from >> That file in my case has this empty entry: >> "customBuildProperties": "",
{
"builtInLibrariesFolders": "I:\\arduino-1.6.11\\libraries",
"customBuildProperties": "",
"fqbn": "teensy:avr:teensy31:usb=serial,speed=96opt,keys=en-us",
"hardwareFolders": "I:\\arduino-1.6.11\\hardware,C:\\Users\\Tim\\AppData\\Local\\Arduino15\\packages",
"otherLibrariesFolders": "C:\\tCode\\libraries",
"runtime.ide.version": "10611",
"sketchLocation": "C:\\tCode\\RTC_time\\RTC_millis\\RTC_millis.ino",
"toolsFolders": "I:\\arduino-1.6.11\\tools-builder,I:\\arduino-1.6.11\\hardware\\tools\\avr,C:\\Users\\Tim\\AppData\\Local\\Arduino15\\packages"
}

There is also this from preferences:: "C:\Users\xyzz\AppData\Local\Arduino15\preferences.txt"

I didn't catch a hook there?

But I do see it has a list of not just the '10 recent' sketches - but what looks like a pages long complete list of each sketch I touched. Would be nice if the IDE gave a way to display those.
 
Hm, yes, I think i don't understand... how would that be useful, without editing Arduino or Teensyduino-source ?
 
Tim, it works with TYQT, too. Just clik "integrate into Arduino" again, after overwriting the file "platform.txt"
 
Last edited:
Hm, yes, I think i don't understand... how would that be useful, without editing Arduino or Teensyduino-source ?

I just looked there and hoped maybe there was a way to use that I didn't know about - based on the name it seemed like it

Tim, it works with TYQT, too. Just clik "integrate into Arduino" again, after overwriting the file "platform.txt"

Of course I'd do that - and posting without that is right - I was just wondering how many are using TYQT of the 6 downloads - and trying to add something useful to get on the notify list for thread updates.
 
I just found something else that might make a good compile time editable #define value that is currently a constant - DELAY during init:

in Pins_teensy.c - the 400 in delay(400)::
Code:
void _init_Teensyduino_internal_(void)
{
[COLOR="#FF0000"]    // ...[/COLOR]
	// for background about this startup delay, please see these conversations
	// [URL="https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980"]https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980[/URL]
	// [URL="https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273"]https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273[/URL]
[COLOR="#FF0000"]    // Last two lines show this wait to call setup()[/COLOR]
	delay(400);
	usb_init();
}

On my Windows 10 machine (With Beta T_3.6 and a T_3.0) I just replaced this with 40 - and it works to get into setup() at 40 not 400.

USB is online printing to the newest TYQT in under 500ms [409-434 on a few resets] instead of ~800ms.

Also this delay() time seems to be ever present, not just when USB is in use.

Tried again at delay(4) or REMOVED and USB is online even faster at 400ms [395-413 on a few resets].
 
If i remember correctly, this delay was introduced for I2C Chips, mainly.
But, nevertheless, this sounds good.
 
Indeed this thread is :: - Advanced users only -

If you click the thread hyper links copied with the code they cover some of that. One issue at the time was T_3.2's not starting right - I didn't have a T_3.2 at this desk.

THIS WORKS :: Just typing this - Wondering if just reworking those last lines and doing USB_init like this for the same net time delay before setup() - but USB online 300ms faster::

Code:
	delay(40);
	usb_init();
	delay(360);
}

Made a pull request to let Paul consider this. The net USB online time with that is again 405 ms!

* :: this will need testing on other OS's against the Teensy family

[Hey KurtE - I did my very only pull request - using online web github again]

<edit> : temporary fail on github again - here is a PULL to Paul - not to myself :: https://github.com/PaulStoffregen/cores/pull/196
 
Last edited:
Looks like it does not work anymore- because of Arduino 1.8.5 ?
Any Ideas how to fi this ? I was away some time... Has anyone solved that yet?
 
fixed Arduino 1.8.5

Ok. Fixed.

Platform.txt for Arduino 1.8.5 as attached.
 

Attachments

  • platform.txt
    6.2 KB · Views: 215
Back
Top