Is there in intermediate file shows expanded macros?

I am using 2.3.6 with a Seeduino SAMD21 XIAO. I'm having issues with an interrupt.
I'm using the IDE "macro"? to setup the interrupt, see below.

I would like to see what the "attachinterrupt" macro expands to for troubleshooting and learning. I was hoping there was a C++ file with all the macros expanded before it went to the compiler.

/*
  This example shows how to wake up the SAMD21 from sleep with an external interrupt.
  This sketch will break the USB during normal operation.
  Double click the button contacts to enable bootloader mode for a new sketch.

  The circuit:
  - SEEED XIAO SAMD21.

*/

#include "Arduino.h"

#define LED_PIN       PIN_LED   // Yellow
#define ENCODERCLK    D2
#define ENCODERDATA   D3

volatile uint16_t PWMcount = 200;
volatile int16_t DataState = 0;
uint32_t startingMillis = millis();

void setup()
{
  Serial.begin(57600);
  pinMode( ENCODERCLK, INPUT_PULLUP );
  pinMode( ENCODERDATA, INPUT_PULLUP );
  attachInterrupt( digitalPinToInterrupt( EXTERNAL_INT_10) , encoderHandler, FALLING );
  
}

void loop()
{
  startingMillis = millis();
  Serial.print("PWMcount = ");
  while( (startingMillis + 5000) < millis()) {}
  Serial.println(PWMcount);
}


//This doesn't work
// It doesn't seem to want to run the second "if"
void encoderHandler()
{
// we assume the IDE adds in shutting off the interrupts while in this handler.
  
  if(ENCODERDATA  == 0){
    PWMcount = PWMcount - 1;
  }

  if(ENCODERDATA == 1 ){
    PWMcount = PWMcount + 1;
  }

}

I think an expanded version of the sketch is saved to TEMP. In the worst case, there will be a compiler option to output the expanded version. A search of the compiler documentation for “preprocessor” should reveal it.

You are not reading the pins, but testing the pin number.

Should be

  if (digitalRead(ENCODERDATA)  == 0){
    PWMcount = PWMcount - 1;
  }
1 Like

What makes you think "attachInterrupt" is a macro? It is simply a C++ function.
And is EXTERNAL_INT_10 really a pin number? I think it should be ENCODERCLK.

The mistake in the ISR itself has been already pointed out by @bobcousins.

1 Like

EXTERNAL_INT_10 is not a macro. Its an enum value:

The actual pin it refers to is defined in the file for each variant:

Hi @JohnRob. The compiler does have this capability:

https://gcc.gnu.org/onlinedocs/gcc-7.5.0/gcc/Overall-Options.html#index-S

-E
Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output.

Input files that don’t require preprocessing are ignored.

but it is not something that is produced by the "Seeed SAMD Boards" platform by default when you compile a sketch using Arduino IDE. So to obtain it you will need to use the compiler directly from the command line. I'll provide instructions you can follow to do that:

  1. Open the sketch for which you want to obtained the
  2. Select File > Preferences... (or Arduino IDE > Settings... for macOS users) from the Arduino IDE menus.
    The "Preferences" dialog will open.
  3. Check the box next to "Show verbose output during: ☐ compile" in the "Preferences" dialog.
  4. Click the "OK" button.
    The "Preferences" dialog will close.
  5. Select Sketch > Verify/Compile from the Arduino IDE menus.
  6. Wait for the compilation to finish.
  7. Scroll up through the contents of the black "Output" panel at the bottom of the Arduino IDE window until you see the section that is introduced by the text "Compiling sketch...". The lines between that and the "Compiling libraries..." line will be the commands Arduino IDE ran to compile the source files of your sketch. If your sketch only contains .ino files, this will have been done in a single command. For example:
    Compiling sketch...
    "C:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\Seeeduino\\tools\\arm-none-eabi-gcc\\7-2017q4/bin/arm-none-eabi-g++" -mcpu=cortex-m0plus -mthumb -c -g -Os -Wall -Wextra -Wno-expansion-to-defined -std=gnu++14 -ffunction-sections -fdata-sections -fno-threadsafe-statics -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -MMD "-D__SKETCH_NAME__=\"\"\"sketch_may31c.ino\"\"\"" -DF_CPU=48000000L -DARDUINO=10607 -DARDUINO_SEEED_XIAO_M0 -DARDUINO_ARCH_SAMD -DARDUINO_SAMD_ZERO -D__SAMD21__ -D__SAMD21G18A__ -DARM_MATH_CM0PLUS -DSEEED_XIAO_M0 -DUSB_VID=0x2886 -DUSB_PID=0x802F -DUSBCON -DUSB_CONFIG_POWER=100 "-DUSB_MANUFACTURER=\"Seeed\"" "-DUSB_PRODUCT=\"Seeed XIAO M0\"" "-IC:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\Seeeduino\\hardware\\samd\\1.8.5/libraries/Adafruit_TinyUSB_Arduino/src/arduino" -DARDUINO_SAMD_ZERO -D__SAMD21__ -D__SAMD21G18A__ -DARM_MATH_CM0PLUS -DSEEED_XIAO_M0 -DUSB_VID=0x2886 -DUSB_PID=0x802F -DUSBCON -DUSB_CONFIG_POWER=100 "-DUSB_MANUFACTURER=\"Seeed\"" "-DUSB_PRODUCT=\"Seeed XIAO M0\"" "-IC:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\Seeeduino\\hardware\\samd\\1.8.5/libraries/Adafruit_TinyUSB_Arduino/src/arduino" "-IC:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\Seeeduino\\tools\\CMSIS\\5.7.0/CMSIS/Core/Include/" "-IC:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\Seeeduino\\tools\\CMSIS\\5.7.0/CMSIS/DSP/Include/" "-IC:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\Seeeduino\\tools\\CMSIS-Atmel\\1.2.1/CMSIS-Atmel/CMSIS/Device/ATMEL/" "-IC:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\Seeeduino\\hardware\\samd\\1.8.5\\cores\\arduino" "-IC:\\Users\\per\\AppData\\Local\\Arduino15\\packages\\Seeeduino\\hardware\\samd\\1.8.5\\variants\\XIAO_m0" "C:\\Users\\per\\AppData\\Local\\arduino\\sketches\\E55F87043A38CF18FF9079BB9F134D9A\\sketch\\sketch_may31c.ino.cpp" -o "C:\\Users\\per\\AppData\\Local\\arduino\\sketches\\E55F87043A38CF18FF9079BB9F134D9A\\sketch\\sketch_may31c.ino.cpp.o"
    Compiling libraries...
    
  8. Select the full text of the command from the "Output" panel.
  9. Press the Ctrl+C keyboard shortcut (Command+C for macOS users).
    This will copy the selected text to the clipboard.
  10. Start any text editor and create a new text file.
  11. Press the Ctrl+V keyboard shortcut (Command+V for macOS users).
    This will paste the copied command into the text editor.
  12. Change the -c flag in the command to -E.
  13. Change the file path argument for the command's -o flag to the path of the file to which you want to write the preprocessed code. For example, the command from my output has this flag:
    -o "C:\\Users\\per\\AppData\\Local\\arduino\\sketches\\E55F87043A38CF18FF9079BB9F134D9A\\sketch\\sketch_may31c.ino.cpp.o"
    
    I want to write the preprocessed code to a file named preprocessed.cpp in the current working directory, so I change that to:
    -o "preprocessed.cpp"
    
  14. Press the Ctrl+A keyboard shortcut (Command+A for macOS users).
    The full text of the modified command will be selected.
  15. Press the Ctrl+C keyboard shortcut (Command+C for macOS users).
  16. Open a command line terminal.
    :warning: If you are using Windows, use cmd instead of PowerShell as I didn't find a way to make the command in the form as generated by Arduino IDE run correctly in PowerShell.
  17. Press the Ctrl+Shift+V (Command+V for macOS users) keyboard shortcut in the terminal window.
    The command copied from the text editor will be pasted to the command prompt.
  18. Press the Enter key.

The command should run successfully. You can now open the file at the path you specified via the -o flag argument in a text editor to view the sketch code in its form after passing through the C++ preprocessor.

1 Like

Thanks for responding,

I think attachInterrupt is a macro of some sort since it is not a C++ function and other examples go through the effort of setting the required registers in lines of code.
I would like to learn how to set the individual registers. I realize the way to do it is to dig into the manual and tough it out but when I went through this for the 8bit processors it took a long time and I can't imagine the 32bit devices will be easier. BTW I'm not a programmer, I write code only as part of my personal projects.

Duh on my part. Thanks

Thank you, Thank you , Thank you...
It worked exactly as you said !
I was taken aback at first as the command ran in what seemed like 0 time.
Now half to run through th 13,000 lines or so (there are a lot of empty lines so maybe only 10,000 :slight_smile:

John

You are welcome. I'm glad you were able to obtain the C++ preprocessed code.

Regards, Per

Why do you still think it is a macro and not a function? It is, you can find it in the WInterrupts.c file of your Seeeduino samd core.

I didn't know its exact form that's why I added "of some sort".

After a successful compile in the IDE, hovering over the name should display an info box like

You can then right-click and choose Go to Definition, opening the file where that function starts with

/*
 * \brief Specifies a named Interrupt Service Routine (ISR) to call when an interrupt occurs.
 *        Replaces any previous function that was attached to the interrupt.
 */
void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode)
{
	static int enabled = 0;
	uint32_t config;
	uint32_t pos;

	#if ARDUINO_SAMD_VARIANT_COMPLIANCE >= 10606
	EExt_Interrupts in = g_APinDescription[pin].ulExtInt;
	#else
	EExt_Interrupts in = digitalPinToInterrupt(pin);
	#endif
	if (in == NOT_AN_INTERRUPT) return;
1 Like