Check if Interrupts are enabled on 328

Hi all, Is there a way to check if the interrupts have been disabled on a 328 Chip? I need this for a library I’m working on. I thought about reading millis() a few times and checking if the value increases.

If you are running a normal sketch, they are enabled in the background by the code that gets added when Sketch:Verify/Compile is run (the checkmark on the toolbar.
main.cpp:

/*
  main.cpp - Main loop for Arduino sketches
  Copyright (c) 2005-2013 Arduino Team.  All right reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <Arduino.h>

// Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (* /*func*/ )()) { return 0; }

// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }

void setupUSB() __attribute__((weak));
void setupUSB() { }

int main(void)
{
	init();

	initVariant();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}

Can’t you just read the registers that control interrupts?

a7

The global interrupt enable flag is bit ‘SREG_I’ in the Status Register (SREG). The value 1 is enabled and the value 0 is disabled. Testing the bit would be something like:

if (SREG & (1 << SREG_I)) { // Interrupts enabled

@everyone Thanks for all the help.

Note that the usual “atomic” instruction block saves the status register, turns off interrupts, and then restores the status register, without ever actually checking whether interrupts were on or off to start with.

The Arduino Core does a check in the serial write function - if interrupts are disabled and the buffer is full, write will do some output anyway: ArduinoCore-avr/HardwareSerial.cpp at master · arduino/ArduinoCore-avr · GitHub