BOARD preprocessor directives from IDE

I don’t know whether this is the correct forum to ask about this but I’ve been searching the Internet and cannot find any reference to preprocessor directives that reflect the type of board connected to the IDE. This functionality would be very handy for myself and others.

So for example when I have an Arduino Uno connected (and selected in the IDE) it would be great to have conditional code like:

#ifdef BOARD_UNO
… do this
#else
… do this
#endif

So my questions are:

Does this functionality exists currently?
How does one go about requesting this type of functionality be added?

Thanks

If you switch on Preferences>Verbose and then re-compile, you will see the compile commands that Arduino is using.

Each compile command will contain a number of params, the ones that start -D are preprocessor constants that you can use. The selected board is one of them, the arduino version is another.

This is all that I see. Where is the selected board?

/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=101

Here is something I use, original was from fat16lib's original DigitalPin library

// 168 and 328 Arduinos
#if defined(__AVR_ATmega168__) ||defined(__AVR_ATmega168P__) ||defined(__AVR_ATmega328P__)
  #define CHIPSET ATmega_168_168P_328P 

// Mega 1280 & 2560
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  #define CHIPSET ATmega_1280_2560 

// Sanguino
#elif defined(__AVR_ATmega644__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega1284P__)
  #define CHIPSET ATmega_644_644P_1284P 

#elif defined(__AVR_ATmega32U4__)

  // Teensy 2.0
  #ifdef CORE_TEENSY 
    #define CHIPSET ATmega_32U4_A 

  // Teensy
  #else
    #define CHIPSET ATmega_32U4_B 
  #endif

  // Teensy++ 1.0 & 2.0
  #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
    #define CHIPSET AT90USB_646_1286 
#endif

Just replace the CHIPSET defines with your platform specific stuff.

Here is a link to a thread on detecting the Due. http://arduino.cc/forum/index.php/topic,128520.0.html

doh! Oh yeah, sorry I forgot the mcu setting isn’t a -D

mcu=atmega328p

I agree with craiglindley, this would be very useful.

Other people have talked about #defines like:

BOARD_ARDUINO_MEGA BOARD_ARDUINO_DUE

Do they exist, or are there any plans to add them? Can pYro_65's approach be used to uniquely identify a board product?

Jim

Hello, i really agree with first message: Switching things by source would be very usefull (at least for me :-). i work with different types of Arduino at a time and its hard to change Board/Serial/Programmer all times, when it must be compiled and upload.

So i tried this to as a first step:

/source/

define ARDUINO_AVR_NANO

..

Then i selected a Mega1280 Board and hope to see somthing like this

-DARDUINO_AVR_NANO

from the precompiler output , but he ignored this an gave me a...

C:\Program Files\Arduino\hardware\tools\avr/bin/avr-g++ -c -g -Os -w -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -mmcu=atmega2560 -DF_CPU=16000000L -DARDUINO=10603 -DARDUINO_AVR_MEGA2560 -DARDUINO_ARCH_AVR -IC:\Program Files\Arduino\hardware\arduino\avr\cores\arduino -IC:\Program Files\Arduino\hardware\arduino\avr\variants\mega -IZ:\Kingston-DataTravelerG3-01\Projekt\Arduino\libraries\PCF8574 -IC:\Program Files\Arduino\hardware\arduino\avr\libraries\Wire -IC:\Program Files\Arduino\libraries\LiquidCrystal\src

so, this does not change anything :(.

Is there a way to do this all on a "IDE-legal"-way ? Or do i have to do it in some way by changing scripts (are there scripts ?) ? I think about write a wrapper for avr-gcc and place a script there to parse code and switch things - but there will be a small problem with IDE-updates :-/.

i dont know where to start now. internet searching does not help..

-- (sorry about many many errors in writing - keep some of them and use them in your own postings ! I place them in publicdomain :slightly_smiling_face: )

jgmdavies: I agree with craiglindley, this would be very useful.

Other people have talked about #defines like:

BOARD_ARDUINO_MEGA BOARD_ARDUINO_DUE

Do they exist, or are there any plans to add them? Can pYro_65's approach be used to uniquely identify a board product?

Jim

Not a board, but the chip used on it. Some boards have the same chip.

The newer IDE defines all the boards like this: ARDUINO_SAMD_ZERO ARDUINO_AVR_LEONARDO ARDUINO_AVR_UNO ARDUINO_SAM_DUE ARDUINO_ESP8266_ESP01

There is also defines for boards that use a specific architecture. ARDUINO_ARCH_SAM ARDUINO_ARCH_SAMD ARDUINO_ARCH_AVR ARDUINO_ARCH_ESP8266

Hello ,

Are there any updates for the same question asked by craiglindley? I have the same issue.

Thank you.

Are there any updates for the same question asked by craiglindley?

Yeah; things have changed a lot since 2012.

This still applies, though:

If you switch on Preferences>Verbose and then re-compile, you will see the compile commands that Arduino is using.

"/Applications/arduino/Arduino-1.8.5.app/Contents/Java/portable/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-g++" -mcpu=cortex-m0plus -mthumb -E -g -Os -w -std=gnu++11 -ffunction-sections -fdata-sections -fno-threadsafe-statics -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -MMD -DF_CPU=48000000L [color=red]-DARDUINO=10805 [color=teal][color=limegreen]-DARDUINO_SAMD_MKRZERO[/color][/color][/color] [color=red]-DARDUINO_ARCH_SAMD[/color]  -D__SAMD21G18A__ -DUSB_VID=0x2341 -DUSB_PID=0x804f -DUSBCON '-DUSB_MANUFACTURER="Arduino LLC"' '-DUSB_PRODUCT="Arduino MKRZero"' ...