Compile-time conditional for Teensy series

Status
Not open for further replies.

CptDangerous

New member
Hi

My sincere apologies if this has been asked before but I can't find a reference in my searches...

As of Arduino IDE 1.5, according to https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5-3rd-party-Hardware-specification,
it is relatively easy to determine, at compile time, what board the IDE is compiling for.

I quote from boards.txt:

"...The uno.name property contains the name of the board shown in the Board menu of the Arduino IDE.

The uno.build.board property is used to set a compile-time variable ARDUINO_{build.board} to allow use of conditional code between #ifdef s. The Arduino IDE automatically generates a build.board value if not defined. In this case the variable defined at compile time will be ARDUINO_AVR_UNO."

The equivalent build.board values for the boards.txt in the teensy path are TEENSY31, TEENSYLC ... etc.

Is there an equivalent compile-time definition for the Teensy series?

I've tried #ifdef ARDUINO_TEENSY31 , #ifdef TEENSYDUINO_TEENSY31 & just #ifdef TEENSY31 without success. As in

#ifdef ARDUINO_TEENSY31
#warning "Teensy 3.2/3.1"
#endif

void setup() {
}

void loop() {
}

The warning is never emitted at compile time when the Tools->Board: setting in the IDE is set to Teensy 3.2/3.1 unlike:

#ifdef ARDUINO_AVR_NANO
#warning "Arduino Nano"
#endif

which does emit the warning at compile time when the Tools->Board: in IDE is set to Arduino Nano.

This facility would be very useful if it existed for the Teensy series. I know there are very clumsy definitions which require intimate knowledge of the chip used but the method described above would be much cleaner.

I'm sure there is a simple value which is generated by the IDE but I haven't found it yet...

Any help would be much appreciated.

TIA

CptDangerous
 
Is there an equivalent compile-time definition for the Teensy series?

I've tried #ifdef ARDUINO_TEENSY31 , #ifdef TEENSYDUINO_TEENSY31 & just #ifdef TEENSY31 without success.

I have the following in my main config file (Meissner_Config.h):

Code:
#if defined(__arm__) && defined(CORE_TEENSY)
#define PROCESSOR_TEENSY_3_X	1		// run on Paul Stoffregen's ARM Cortex M4 based teensy 3.0/teensy 3.1

#if defined(__MK20DX128__)
#define PROCESSOR_TEENSY_3_0	1

#elif defined(__MK20DX256__)
#define PROCESSOR_TEENSY_3_1	1
#define PROCESSOR_TEENSY_3_2	1

#elif defined(__MKL26Z64__)
#define PROCESSOR_TEENSY_LC	1

#elif defined(__MK64FX512__)
#define PROCESSOR_TEENSY_3_5	1

#elif defined(__MK66FX1M0__)
#define PROCESSOR_TEENSY_3_6	1

#else
#error "Unknown Teensy, fix Meissner_Config.h"
#endif
 
In addition to the ones Michael mentioned.

I also sometimes use: #ifdef KINETISK
This includes all of the Teensy 3.x boards.

#ifdef KINETISL
This includes the Teensy LC
 
In addition to the ones Michael mentioned.

I also sometimes use: #ifdef KINETISK
This includes all of the Teensy 3.x boards.

#ifdef KINETISL
This includes the Teensy LC

Thanks KurtE & Michael

KurtE's method is what I'm looking for as Michael's is what I described as 'clumsy' though I'm sure it works fine and once set up in a header file is no longer clumsy! :)

Thanks both. Now if there was a value which swept up the Teensy2 my cup would be running over!

Cheers

Cpt
 
Thanks KurtE & Michael

KurtE's method is what I'm looking for as Michael's is what I described as 'clumsy' though I'm sure it works fine and once set up in a header file is no longer clumsy! :)
Nothing says you have to use a header file. You can just cut+paste the #if/#elif/#endif lines into your sketch. Because I have multiple different sketches, it is more convenient to me to put everything in a single header file.

However, if you are going to use #if/etc. you do have to realize that the converter from .ino/.pde to C++ is fairly dumb. It is trying to make the language simpler than raw C++. One of the things it does is provide forward declarations of functions defined. To do this, it moves the #include files to the top, and then does the forward declarations. However, if you have an #include inside of #if, it may not do what you want on machines where the #if value is not true.
 
Here is a version looking at compile time #defines written for K66 beta - not finding old posting in quick search:

Code:
void CPUspecs() {
  Serial.println();
#if defined(__MK20DX128__)
  Serial.println( "CPU is T_LC");
#elif defined(__MK20DX256__)
  Serial.println( "CPU is T_3.1/3.2");
#elif defined(__MKL26Z64__)
  Serial.println( "CPU is T_3.0");
#elif defined(__MK64FX512__)
  Serial.println( "CPU is T_3.5");
#elif defined(__MK66FX1M0__)
  Serial.println( "CPU is T_3.6");
#endif
  Serial.print( "F_CPU =");   Serial.println( F_CPU );
  Serial.print( "F_PLL =");   Serial.println( F_PLL );
  Serial.print( "F_BUS =");   Serial.println( F_BUS );
  Serial.print( "F_MEM =");   Serial.println( F_MEM );
  Serial.print( "NVIC_NUM_INTERRUPTS =");   Serial.println( NVIC_NUM_INTERRUPTS );
  Serial.print( "DMA_NUM_CHANNELS =");   Serial.println( DMA_NUM_CHANNELS );
  Serial.print( "CORE_NUM_TOTAL_PINS =");   Serial.println( CORE_NUM_TOTAL_PINS );
  Serial.print( "CORE_NUM_DIGITAL =");   Serial.println( CORE_NUM_DIGITAL );
  Serial.print( "CORE_NUM_INTERRUPT =");   Serial.println( CORE_NUM_INTERRUPT );
  Serial.print( "CORE_NUM_ANALOG =");   Serial.println( CORE_NUM_ANALOG );
  Serial.print( "CORE_NUM_PWM =");   Serial.println( CORE_NUM_PWM );
  Serial.print( "ARDUINO =");   Serial.println( ARDUINO );
  Serial.print( "TEENSYDUINO =");   Serial.println( TEENSYDUINO );

#if 1 // defined(KINETISK)
  if ((RCM_SRS0 & RCM_SRS0_PIN))
    Serial.println( "\n RTC hardware Present!!!");
#endif
}
 
However, if you are going to use #if/etc. you do have to realize that the converter from .ino/.pde to C++ is fairly dumb. It is trying to make the language simpler than raw C++. One of the things it does is provide forward declarations of functions defined. To do this, it moves the #include files to the top, and then does the forward declarations. However, if you have an #include inside of #if, it may not do what you want on machines where the #if value is not true.

I would add:
I simple work-around to avoid Arduino interferences is to let the ino file completely empty and put all your code into a separate cpp file. Only draw-back is you have to program now in correct (complete) c++ .

With this you can enjoy the Teensyduino infrastructure but avoid Arduino meddling.
 
What do I use to just know that it's at least a 3 in PlatformIO? TEENSY30?

It depends on whether you count the Teensy LC as a Teensy 3.x board (which generally it is, unlike the Teensy 2/2++ boards). Then you would use the following:

Code:
#if defined(__arm__) && defined(CORE_TEENSY)
    //...
#endif

Now the other kicker is the Teensy 4.0 that is currently in beta test. Is it close enough to what the Teensy 3.x defined that you can just use the Teensy 3.x definitions. it depends on what PlatformIO really needs to use. Already, it looks like there will some changes in pinouts from the Teensy LC/3.0/3.1/3.2/3.5/3.6 layout.
 
So, I did some further reading and I was able to find how to do this within the constructs of PlatformIO. For each build target, there is a unique define for each. For the Teensy, this is enough: #if defined(TEENSY31)
 
Can you see the console command line used by platformio? That should show if it provides some unique defines separate from those relied on when using the Arduino IDE - like those shown in post#7 as the way to identify each Teensy by processor.
 
Can you see the console command line used by platformio? That should show if it provides some unique defines separate from those relied on when using the Arduino IDE - like those shown in post#7 as the way to identify each Teensy by processor.

just pass "--verbose" to the build_flags. Then you'll see what the specific define is for the board. Look at this

Code:
LIBRARY_PATH=/Users/bittond/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/5.4.1/armv7e-m/:/Users/bittond/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/5.4.1/../../../../arm-none-eabi/lib/armv7e-m/:/Users/bittond/.platformio/packages/toolchain-gccarmnoneeabi/bin/../arm-none-eabi/lib/armv7e-m/:/Users/bittond/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/5.4.1/:/Users/bittond/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/:/Users/bittond/.platformio/packages/toolchain-gccarmnoneeabi/bin/../lib/gcc/arm-none-eabi/5.4.1/../../../../arm-none-eabi/lib/:/Users/bittond/.platformio/packages/toolchain-gccarmnoneeabi/bin/../arm-none-eabi/lib/
COLLECT_GCC_OPTIONS='-o' '.pioenvs/teensy31/FrameworkArduino/yield.cpp.o' '-c' '-fno-exceptions' '-felide-constructors' '-fno-rtti' '-std=gnu++14' '-Wno-error=narrowing' '-v' '-Wall' '-ffunction-sections' '-fdata-sections' '-mthumb' '-mcpu=cortex-m4' '-nostdlib' '-fsingle-precision-constant' '-O2' '-D' 'PLATFORMIO=30606' '-D' '__MK20DX256__' '-D' 'TEENSY31' '-D' 'VERSION=1.0.12' '-D' 'Mav_Debug_Rssi' '-D' 'USB_SERIAL' '-D' 'ARDUINO=10805' '-D' 'TEENSYDUINO=145' '-D' 'F_CPU=72000000L' '-D' 'LAYOUT_US_ENGLISH' '-I' '/Users/bittond/.platformio/packages/framework-arduinoteensy/cores/teensy3'

You can see the defines at the end.
 
Status
Not open for further replies.
Back
Top