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!
You can add mcu selection to the Tools IDE menu for selected boards similar as for Arduino Nano in 1.x IDE:
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.
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?
Wow, this is interesting for me too, we done the similar for WinnerMicro W80x boards i
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:
Wow, that's awesome to hear! It's always exciting to connect with someone who has worked on similar projects. 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!
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
So if you have similar questions, don’t hesitate to contact.
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! "
(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
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.)
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:
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.
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:
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.
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.