Uisng ifdef to differentiate between teensy 2.0, teensy 2.0++, and teensy 3.0

Status
Not open for further replies.

Dave X

Well-known member
What's the best way to test for the chip/device being compiled & maybe provide for alternate code for programs on the the teensy 3.0?

I saw some tests in https://github.com/triffid/Teacup_Firmware/blob/master/arduino.h

Here are some ARM-related #defines from a teensy3 compile:
Code:
f$ /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/arm-none-eabi/bin/arm-none-eabi-g++ -std=gnu++0x -felide-constructors -fno-exceptions -fno-rtti -Wall -g -Os -mcpu=cortex-m4 -mthumb -nostdlib -MMD -DF_CPU=96000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH -I. -I/Applications/Arduino.app/Contents/Resources/Java/hardware/teensy/cores/teensy3   -dM -E  main.cpp |egrep -i "ARM|mk|cortex"
#define ARM_DWT_CYCCNT *(volatile uint32_t *)0xE0001004
#define __ARMEL__ 1
#define __ARM_FEATURE_UNALIGNED 1
#define ARM_DEMCR *(volatile uint32_t *)0xE000EDFC
#define ARM_DWT_CTRL *(volatile uint32_t *)0xE0001000
#define __ARM_PCS 1
#define _EXPARM(name,proto) (* name) proto
#define IRQ_RTC_ALARM 28
#define __arm__ 1
#define ARM_DWT_CTRL_CYCCNTENA (1 << 0)
#define __ARM_ARCH_7EM__ 1
#define ARM_DEMCR_TRCENA (1 << 24)
#define __ARM_ARCH_EXT_IDIV__ 1
#define __ARM_EABI__ 1
#define _mk20dx128_h_ 
#define __ARM_FEATURE_DSP 1

If you've got verbose compiler output on in Arduino, compiling the code below for teensy 3.0 doesn't pick up on the _mk20dx128_h_ and shows the default warning.

Code:
/* LED Blink, Teensyduino Tutorial #1
   http://www.pjrc.com/teensy/tutorial.html  -- modified for compiler/processor ifdefs
 
   This example code is in the public domain.
*/


#if defined __AVR__
#  if defined (__AVR_AT90USB1286__) && defined (CORE_TEENSY) // teensy++ 2.0
#     define HARDWIRED_LED 6
#  endif
#  if defined (__AVR_ATmega32U4__) && defined (CORE_TEENSY) // teensy 2.0
#     define HARDWIRED_LED 11
#  endif
#ifndef HARDWIRED_LED
#     warning Assuming HARDWIRED_LED as LED_BUILTIN for most arduinos
#     define HARDWIRED_LED LED_BUILTIN   
#  endif
# endif

#if defined (__ARMEL__) 
#   if (defined (_mk20dx128_h_) || defined (__MK20DX128__)) && defined (CORE_TEENSY) // Teensy 3.0 
#      define HARDWIRED_LED 13
#   else
#      warning Assuming HARDWIRED_LED as 13 for an __ARMEL__
#      define HARDWIRED_LED 13
#   endif
#endif

#ifndef HARDWIRED_LED
#  error Unknown processor
#endif

#ifdef CORE_TEENSY
#warning CORE_TEENSY defined
#endif


const int ledPin = HARDWIRED_LED;   // Teensy has LED on 11, Teensy++ on 6

// the setup() method runs once, when the sketch starts

void setup() {
  // initialize the digital pin as an output.
  pinMode(ledPin, OUTPUT);
}

// the loop() method runs over and over again,
// as long as the board has power

void loop() {
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(1000);                  // wait for a second
  digitalWrite(ledPin, LOW);    // set the LED off
  delay(1000);                  // wait for a second
}
 
Last edited:
I don't know about T2 (++), but for my Teensy 3.0 I use:
Code:
#elif defined(__MK20DX128__)
 
That looks pretty good. I have only a couple minor points to add...


To detect Teensy 2.0, you might want to use this if you also intend to detect Arduino's boards using the same chip:

#if defined(__AVR_ATmega32U4__) && defined(CORE_TEENSY)


For the pin with the LED, you can use the pre-defined name "LED_BUILTIN". That works on all Teensy boards, and all Arduino boards since Arduino 1.0.
 
I don't know about T2 (++), but for my Teensy 3.0 I use:
Code:
#elif defined(__MK20DX128__)

That works in Arduino, but it doesn't seem to get defined using the Makefile. With verbose Arduino compliation output, I see that it is set. And the command line option seems to be set on my Mac in:

/Applications/Arduino.app/Contents/Resources/Java/hardware/teensy/boards.txt:teensy3.build.option3=-D__MK20DX128__
 
Last edited:
That looks pretty good. I have only a couple minor points to add...


To detect Teensy 2.0, you might want to use this if you also intend to detect Arduino's boards using the same chip:

#if defined(__AVR_ATmega32U4__) && defined(CORE_TEENSY)


For the pin with the LED, you can use the pre-defined name "LED_BUILTIN". That works on all Teensy boards, and all Arduino boards since Arduino 1.0.


CORE_TEENSY seems to be defined during Makefile compilations, but not during default Arduino compilations. I modified the above sketch to check for CORE_TEENSY.

Edit: if I #include "WProgram.h" in a sketch, then CORE_TEENSY is defined during Arduino compilations as part of core_id.h, but otherwise it is uncertain. Code that that uses Stream, Print, or includes it, but the blink sketch does not.
 
Last edited:
Paul,

Would it be good to add a -D__MK20DX128__ to in the CPPFLAGS in the Makefile to make it consistent with the Arduino compile?

Or maybe in mk20dx128.h add a #define __MK20DX128__ or an #ifndef __MK20DX128__ #warning or #error for processors where it doesn't apply?

It would be nice to have something dependable to condition on.
 
I'm adding this to the sample makefile, which will be in Teensyduino 1.14.

Code:
# options needed by many Arduino libraries to configure for Teensy 3.0
OPTIONS += -D__MK20DX128__ -DARDUIO=104
 
I'm adding this to the sample makefile, which will be in Teensyduino 1.14.

Code:
# options needed by many Arduino libraries to configure for Teensy 3.0
OPTIONS += -D__MK20DX128__ -DARDUIO=104



And for the Teensy 3.1 the compiler flag is
Code:
-D__MK20DX256__
so for teensy 3.0 and 3.1 specific code you could wrap it in:

Code:
#if defined(__MK20DX256__)|| defined(__MK20DX128__)

...

#endif
 
Status
Not open for further replies.
Back
Top