Detecting AVR type boards

Is it possible to detect AVR type boards VS non-AVR boards?

In my code I have as follows to include watchdog and interrupts:

#include <avr/wdt.h>
#include <avr/interrupt.h>

This is fine for the Uno, Nano or Mega 2560, however these libraries are not recognized when I compile for other boards such as the ESP8288 or ESP32. I would like to be able to add some conditional directives like this:

#ifdef AVR_BOARD
#include <avr/wdt.h>
#include <avr/interrupt.h>

and also;

#ifdef AVR_BOARD
(watchdog code)
(interrupt code)
(alternative code)

It would be a bit tedious to specify a list of Arduino boards with AVR_ATmega328P, AVR_ATmega2560, AVR_ATmega32U4 etc. Is there a higher level identifier to recognize the AVR prefix?

e.g. AVR

That was too obvious!
I didn't think to try two underscores either side though. With two it does seem to be working.
Thank you.

BTW, by the same toke, is it possible to determine whether a board supports EEPROM and HardwareSerial?

Each AVR has EEPROM (maybe a few have not) the E2END and EEMEM should be defined.
For HardwareSerial see the header file. There is at the end:

#if defined(UBRRH) || defined(UBRR0H)
  extern HardwareSerial Serial;
  #define HAVE_HWSERIAL0
#if defined(UBRR1H)
  extern HardwareSerial Serial1;
  #define HAVE_HWSERIAL1
#if defined(UBRR2H)
  extern HardwareSerial Serial2;
  #define HAVE_HWSERIAL2
#if defined(UBRR3H)
  extern HardwareSerial Serial3;
  #define HAVE_HWSERIAL3

It is based on USART register presence.

Thank you for that. It is much appreciated.

You can also identify the exact part with similar defines, ex:



How does one identify an ESP8266 or an ESP32?

I tried a number of options including:

The last one is taken form the boards.txt file, but none of them seem to work?

Looks like the ESP does not need the preceding double underscores so something like:

#if defined (ESP8266) || defined(ESP32)

works just fine.