Compiling Teensy Sketches with VisualCode (Win10)

@WMXZ BTW: did you try to implement that RTC setting for the linker? If you find a solution I'd be happy to include it in the makefile

this is working for me
Code:
FLAGS_LD    := -Wl,--gc-sections,--relax,--defsym=__rtc_localtime=$(shell powershell date +%s)

this assumes that all Windows 10 have powershell implemented

Edit1:
I have also a python solution that is somewhat more complicated

Code:
pyCmd = $(shell python -c '$1')
TS:=$(call pyCmd,import time; print("%d" % time.time()))
and
Code:
FLAGS_LD    := -Wl,--gc-sections,--relax,--defsym=__rtc_localtime=$(TS)
maybe this could also be combined into a single command

Edit2:
OK, this should also do it
Code:
FLAGS_LD    := -Wl,--gc-sections,--relax,--defsym=__rtc_localtime=$(shell python -c 'import time; print("%d" % time.time())')
 
Last edited:
As I found that it is convenient to have the rtc_localtime not to define compile time but upload time, I simply changed
Code:
$(TARGET_ELF): $(CORE_LIB) $(LIB_OBJ) $(USR_OBJ)
to
Code:
$(TARGET_ELF): $(CORE_LIB) $(LIB_OBJ) $(USR_OBJ) force
with 'force' added to .PHONY list and otherwise undefined target

this will force linking for every upload

Ideally one could use a program that edits symbols in the elf files, but, have not found one yet
 
Tried your powershell suggestion, didn't work for me. But that works:

FLAGS_LD := -Wl,--gc-sections,--relax,--defsym=__rtc_localtime=$(shell powershell [int](date -UFormat +%s))

Need to use UFormat to get a Unix format and transform to int.
 
Tried your powershell suggestion, didn't work for me. But that works:

FLAGS_LD := -Wl,--gc-sections,--relax,--defsym=__rtc_localtime=$(shell powershell [int](date -UFormat +%s))

Need to use UFormat to get a Unix format and transform to int.

Was playing with it only this morning (with close to zero knowledge on commandline scripting), so it may have worked only by chance
Anyhow, we have a solution and I learned something more.
 
It seems that it also depends on which date.exe system will find
here
C:/MinGW/msys/1.0/bin/date.exe
tells me that it does not like -UFormat

could you determine the path of your date.exe?
 
Thats interesting. I thougth date is just a shorthand for Get-Date which gets the date from .net. Looks like it chooses the linux version if it finds it. And that of course doesn't need the uformat...

Can you try this:
Code:
FLAGS_LD    := -Wl,--gc-sections,--relax,--defsym=__rtc_localtime=$(shell powershell [int](Get-Date -UFormat +%s))
 
would
Code:
            var src = makeEntry("FLAGS_LD    := ", "build.flags.ld", options);
            var dst = src.Replace("__rtc_localtime=0", "__rtc_localtime=$(shell powershell [int](Get-Date -UFormat +%s))");
            mf.Append(dst + "\n");
be a valid replacement for

Code:
             mf.Append(makeEntry("FLAGS_LD    := ", "build.flags.ld", options) + "\n");
or what would be better c# style?
 
The c# style is OK but why would you first set the _rtc_localtime to zero and then replace it afterwards? Would be more straight forward have the right flags set. You find the relevant code at the end of Board.cs.
 
The c# style is OK but why would you first set the _rtc_localtime to zero and then replace it afterwards? Would be more straight forward have the right flags set. You find the relevant code at the end of Board.cs.

Oh, I did not see that, I thought you were copying TD boards.txt
this way it is easier
 
It seemed there was a step missing - multiple references to ' the linker's timestamp ' - wasn't sure if that was inherent and I didn't see it.

It could be printed - but will just be a number … but it will match what the time code expects?

from ...\cores\teensy3\mk20dx128.c :: rtc_set((uint32_t)&__rtc_localtime); goes directly into the RTC pins_teensy.c::
Code:
void rtc_set(unsigned long t)
{
	RTC_SR = 0;
	RTC_TPR = 0;
	RTC_TSR = t;
	RTC_SR = RTC_SR_TCE;
}
 
It seemed there was a step missing - multiple references to ' the linker's timestamp ' - wasn't sure if that was inherent and I didn't see it.

It could be printed - but will just be a number … but it will match what the time code expects?

from ...\cores\teensy3\mk20dx128.c :: rtc_set((uint32_t)&__rtc_localtime); goes directly into the RTC pins_teensy.c::
Code:
void rtc_set(unsigned long t)
{
	RTC_SR = 0;
	RTC_TPR = 0;
	RTC_TSR = t;
	RTC_SR = RTC_SR_TCE;
}

I'm confused, what means 'multiple references' in this context?
All what is done to replace the "0" in "__rtc_localtime=0" with a adhoc estimated value (seconds since 1970)
The value may be wrong, but there should be no multiple references
could you please elaborate?
 
compatibility between Arduino sketch and VisualTeensy:

So I created a VisualTeensy project and copied code from existing (compiling) sketch.
Compiling obviously succeeded after some massaging
Now I tried to compile everything again from Arduino IDE
all code is in src/ so I needed an empty ino file to allow Arduino to open sketch.

Problem:
compiling sketch the precedencies for the include files are now screwed up.

Original sketch all files (also include files) are in same folder and take precedence over core library (in particular, I have my own audio_block_t type).

compiling the VisualTeensy adapted code from Arduino, Arduino adds Arduino.h, which includes usb_audio.h, which includes AudioStream.h, which defines audio_block_t,
Consequently when program comes to main in src folder and includes my own include files audio_block_t is already defined and compilation fails.
 
I'm confused, what means 'multiple references' in this context?
...

Paul's post referred 3-4 times to 'linker' timestamp in the linked post. At first I hoped it was something the linker passed - but it seems not. So an app has to get the UTC/time value exported to pass on the linker's command line.
 
So an app has to get the UTC/time value exported to pass on the linker's command line.

Yes, this is exactly what WMXZs solution is supposed to be doing. It compiles nicely and I assume it works but I didn't really try it yet. Is there a simple example where I could test the functionality without soldering a battery to the Teensy?
 
Problem:
compiling sketch the precedencies for the include files are now screwed up.

Would it be possible to get a minimal example showing the effect. I.e. a version of a sketch working with Arduino but not with the makefile and a tweaked version which is OK with the makefile but not with Arduino? I'm not sure if there will be a solution to this in all cases because, as you wrote, Arduino is doing a lot of things in the background to make things easier for beginners. But even if it couldn't be fixed, I'd be very interested in understanding the effects...
 
Yes, this is exactly what WMXZs solution is supposed to be doing. It compiles nicely and I assume it works but I didn't really try it yet. Is there a simple example where I could test the functionality without soldering a battery to the Teensy?

add -v to link script and search for rtc_localtime

default:
--defsym=__rtc_localtime=0
new:
--defsym=__rtc_localtime=1538289641, or similar
 
Sure, but I was thinking of a simple sketch that demonstrates that the time is set correctly. Never did anything with the RTC...
 
Would it be possible to get a minimal example showing the effect. I.e. a version of a sketch working with Arduino but not with the makefile and a tweaked version which is OK with the makefile but not with Arduino? I'm not sure if there will be a solution to this in all cases because, as you wrote, Arduino is doing a lot of things in the background to make things easier for beginners. But even if it couldn't be fixed, I'd be very interested in understanding the effects...

was a fake problem.
program needed to be compiled with USB-type Serial but Arduino was set to USB-type Serial+Midi+Audio and the compilation of usb_audio generated conflicting typedefs.


But thinking about it,
There should be an option that allows the creation of an Arduino library (that is without main.cpp, or main should go under examples). what do you think?
 
Yes, this is exactly what WMXZs solution is supposed to be doing. It compiles nicely and I assume it works but I didn't really try it yet. Is there a simple example where I could test the functionality without soldering a battery to the Teensy?

… Missed seeing solution provided ...

If you have a T_3.5 or 3.6 RTC runs with no battery - and will persist some second(s) of power off IIRC, and be lost after that.
 
was a fake problem.
Great :)

In principle there is nothing special with main.cpp, you can always delete it if you don't need it. But, next time you update the project with VisualTeensy it doesn't see main.cpp and copies a fresh one to the project... Can be anoying :)

I could either

  • make the generation of the main optional (Don't like that very much because I try to have the GUI in quick mode as simple as possible), or
  • only autogenerate a main.cpp during generation of the project and not during updates. If you programming a lib you'd have to delete main manually once.
  • have an option in the setttings tab

Edit:
Didn't see your last paragraph. An option for generating a lib would also be cool, it could then autogenerate the correct directory structure and place a library.property / library.json to the folder. Like this idea...
 
here is a test program for (T3.6, with xtal but no VBAT)
Code:
#include "Arduino.h"

extern void *__rtc_localtime; // Arduino build process sets this
uint32_t t1=(uint32_t)&__rtc_localtime;

void setup()
{
  pinMode(LED_BUILTIN,OUTPUT);
  while(!Serial);
  delay(1000);

  Serial.println(ARDUINO);
}

void loop()
{
  digitalWriteFast(LED_BUILTIN,!digitalReadFast(LED_BUILTIN));
  delay(1000);
  uint32_t tt = RTC_TSR;  
  Serial.print(t1);   Serial.print(" ");
  Serial.print(tt);   Serial.print(": ");
  Serial.print(tt/24L/3600L);   Serial.print(" ");
  tt %= (24L*3600L);      Serial.print(tt/3600L);       Serial.print(" ");
  tt %= 3600;             Serial.print(tt/60L);         Serial.print(" ");
  tt %= 60;               Serial.println(tt);
}

note: ARDUINO must be set to 10600 or greater (-DARDUINO=10600). you have only -DARDUINO

in case you try, make sure you disconnect teensy from power and plugin back with button pressed to force download.
 
Thanks, don't have a xtal for testing but this:

Code:
#include "Arduino.h"

extern void *__rtc_localtime;
uint32_t rtc_localtime = (uint32_t)&__rtc_localtime;

void setup()
{
  while (!Serial);

  Serial.printf("Time injected from the linker: %d",rtc_localtime);
}

void loop()
{
}

clearly shows that the __rtc_localtime is injected corretly. Works for me. Open question is, if Win7 also installes the powershell per default.

rtc.PNG
 
note: ARDUINO must be set to 10600 or greater (-DARDUINO=10600). you have only -DARDUINO
Didn't find a place yet where to read the information out of the current installation...
 
Back
Top