Defining Custom Macros for Microcontrollers in Arduino IDE

Hello Arduino community,

I'm working on a project that involves different microcontrollers (MCUs) within the Arduino ecosystem. I'm curious to know if there is a way to define custom macros specifically for different MCUs in the Arduino IDE.

For instance, when working with specific boards, the IDE sets predefined macros like __AVR_ATmega328P__ automatically. However, I would like to set custom macros based on the MCU itself, independent of the board.

Is there a method or workaround to achieve this in the Arduino IDE? I want to conditionally compile parts of my code based on the specific microcontroller being used.

Any insights or guidance would be greatly appreciated!

Thank you.

You can add mcu selection to the Tools IDE menu for selected boards similar as for Arduino Nano in 1.x IDE:
mcu_select
To do this you have to edit boards.txt file in the board support package as described in this discussion, for example:

Don’t be confused by the fact that the discussion is about Arduino-Cli - the packages of boards in Arduino-Cli and the usual IDE are the same.

1 Like

Thank you for your response, @b707.

Let me clarify my objective. I've successfully integrated VEGA ARIES Boards into the Arduino IDE, and there are a total of four board variants, each identified by a different build.board property used to set a compile-time macro.

#if defined(VEGA_ARIES_V2) || defined(VEGA_ARIES_V3) || defined(VEGA_ARIES_IOT) || defined(VEGA_ARIES_MICRO)
  // Code specific to any of the four VEGA ARIES boards
#endif

To streamline my code, I'm wondering if there's a way to define a single macro that encompasses all four boards. This would allow me to write code that applies universally to any of these boards without explicitly listing each one. Is there a way to achieve this in the Arduino IDE?

Thank you for your assistance.

Wow, this is interesting for me too, we done the similar for WinnerMicro W80x boards i :slight_smile:

Yes, of course.
For example, you can define any macro for your boards either in boards.txt or platforms.txt. For example, in our W80x package we defined a macro for a board family:

build.boards_family=HLK_w80x

and then add it in the building flags in platform.txt:

compiler.define.flags= -D__{build.board}__ -DARDUINO_{build.boards_family}

Now we can add conditional compilation for all our boards:

#if defined(DARDUINO_HLK_w80x)
1 Like

Wow, that's awesome to hear! It's always exciting to connect with someone who has worked on similar projects. :blush: How did your experience go with WinnerMicro W80x boards? Any tips or challenges you encountered along the way?

Thank you for your solution! It worked perfectly for me. I really appreciate your help!

1 Like

Quite interesting boards, a bit similar to bluepill STM32.

The most interesting thing was the experience of adapting third-party boards for IDE. Our team learned a lot during the year we work on this project :slight_smile:
So if you have similar questions, don’t hesitate to contact.

1 Like

Thanks for sharing! Adapting third-party boards sounds like quite a learning experience. If we have questions, we'll be sure to reach out. Appreciate the offer! :blush:"

Somewhere in your core files, do:

#if defined(VEGA_ARIES_V2) || defined(VEGA_ARIES_V3) || defined(VEGA_ARIES_IOT) || defined(VEGA_ARIES_MICRO)
#define VEGA_ARIES_SERIES
#endif

(assuming there isn't something already there that is similar.)

I apparently can't install the vega Arduino support on my old mac, but the way it works for AVR is that a bunch of preprocessor symbols are defined by the compiler itself (NOT by the Arduino IDE), based on the -mmcu=atmega328p or whatever in the compiler command line.
That includes the definition of AVR_ATMEGA328P, but it also includes a bunch of less specific symbols like

#define __AVR_MEGA__ 1
#define __AVR_ENHANCED__ 1
#define __AVR_ARCH__ 5
#define __AVR 1
#define AVR 1
#define __AVR__ 1
#define __AVR_DEVICE_NAME__ atmega328p
#define __WITH_AVRLIBC__ 1

You can find these with a command like:
echo | avr-gcc -mmcu=atmega328p -dM -E - | grep -i avr
(and some judicious filtering - there are a LOT of these compiler-defined symbols.)

2 Likes

Thank you for your response, @westfw .

Not currently implemented. I'll proceed to add the specified code snippet and test its functionality. Will keep you updated on the progress.

Vega Arduino support is compatible only with Windows and Linux OS, and unfortunately not supported on Mac

A common convention is to define a global ARDUINO_ARCH_<architecture ID> macro. For example:

Note the -DARDUINO_ARCH_{build.arch} flag in the compilation command pattern. The build.arch property is automatically defined by the Arduino boards platform framework:

https://arduino.github.io/arduino-cli/dev/platform-specification/#:~:text=The%20project%20name-,{build.arch},-%3A%20The%20MCU%20architecture

For your platform, adding this flag to the compilation patterns would cause an ARDUINO_ARCH_RISCV macro to be defined. The origin of the RISCV value is the packages[*].platforms[*].architecture field of the package index converted to all uppercase:

https://gitlab.com/riscv-vega/vega-arduino/-/blob/d798d39ea4e1b5f48621aca13722dc8748ca183b/package_vega_index.json#L246

		    "architecture": "riscv",

ARDUINO_ARCH_RISCV is less specific than the ones produced by the suggestions offered by @b707 and @westfw since this macro will also be defined when compiling for any other board from a platform of the riscv architecture that follows this common convention. That may or may not be desirable depending on the application.

1 Like

It looks like .../packages/vega/hardware/riscv/1.0.7/variants/standard/variant.h (which is included by all four of the existing boards) defines a few symbols that are common to all of them and would serve your purpose:

#define ARIES_v2_0
#define VEGA_PROCESSOR

Huh. AFAICT, the only one of the various symbols actually used anywhere in the Arduino core is VEGA_ARIES_IOT:

westfw@ww-gaming:/mnt/c/Users/westf/AppData/Local/Arduino15/packages/vega/hardware/riscv/1.0.7$ gid VEGA_ARIES_V2
boards.txt:90:aries_v2.build.board=VEGA_ARIES_V2

westfw@ww-gaming:/mnt/c/Users/westf/AppData/Local/Arduino15/packages/vega/hardware/riscv/1.0.7$ gid VEGA_ARIES_V3
boards.txt:135:aries_v3.build.board=VEGA_ARIES_V3

westfw@ww-gaming:/mnt/c/Users/westf/AppData/Local/Arduino15/packages/vega/hardware/riscv/1.0.7$ gid VEGA_ARIES_MICRO
boards.txt:177:aries_micro.build.board=VEGA_ARIES_MICRO

westfw@ww-gaming:/mnt/c/Users/westf/AppData/Local/Arduino15/packages/vega/hardware/riscv/1.0.7$ gid VEGA_ARIES_IOT
cores/arduino/pwm.h:74:#if defined(VEGA_ARIES_IOT)
cores/arduino/pwm.h:156:#endif // VEGA_ARIES_IOT
variants/standard/pins_arduino.h:11:#if defined(VEGA_ARIES_IOT)
cores/arduino/wiring_analog.cpp:18:#if defined(VEGA_ARIES_IOT)
cores/arduino/wiring_analog.cpp:233:#endif  // VEGA_ARIES_IOT
boards.txt:217:aries_iot.build.board=VEGA_ARIES_IOT

westfw@ww-gaming:/mnt/c/Users/westf/AppData/Local/Arduino15/packages/vega/hardware/riscv/1.0.7$ gid VEGA_PROCESSOR
variants/standard/variant.h:10:#define VEGA_PROCESSOR

westfw@ww-gaming:/mnt/c/Users/westf/AppData/Local/Arduino15/packages/vega/hardware/riscv/1.0.7$ gid ARIES_v2_0
variants/standard/variant.h:9:#define ARIES_v2_0
1 Like

Thank you for your response, @ptillisch.
I can really use this information whenever I want to write code that is specific to RISC-V; I can use this macro.

1 Like

Oh yeah! I completely forgot about these macros; I'll try using them in my code.

Yeah, you're correct! All the boards work on the same piece of code, except the ARIES IOT, which has a different ADS chip on it, and that's why it has different code. Actually, I wanted to use these macros outside the core file. Most libraries I get from the library manager are AVR, ARM, ESP specific, and I want to make those libraries compatible with VEGA ARIES boards, which are based on the RISC-V architecture, without changing them. That's why I need these macros.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.