Good day everyone
I have a large code and I want to detect if there is nointerrupts();
or cli();
within the code, I mean within the whole functions and procedures in my code
because I think I'm losing some interrupts detection and I don't know why.
is there a simple way to know if interrupts are disabled in some code ?
Which arduino?
Interrupts are disabled automatically when in an ISR and when you disable them yourself so in general you will know whether they are disabled or not
I'm using atmega8a , minicore
I think you didn't get the idea, for example digitalWrite()
procedure has a cli()
within its code which I detected manually, but I need a method to detect if there is any other similar cli()
in the whole functions and procedures, I want to know if interrupts are disabled in any moment.
well Software Serial will also disable interrupts...
The thing is digitalWrite() or other functions re-establish interrupts before returning so your code won't be able to see at run time that they were disabled for some time within a function call.
Yes, but if you detect whether interrupts are disabled before you call digitalWrite() then afterwards the answer will be no in both cases and the same with several other functions so what good is that ?
At any point of the sketch you can execute the following code to now if the "Global Interrupt Enable Bit (I-bit of SREG, Fig-1)" is enabled by looking at the value of n. A true (1) will say that the interrupt is at enabled state.
bool n = bitRead(SREG, 7);
Figure-1:
it's not great:
- bitRead does not return a bool
- 7 is a magic number, using
SREG_I
would be preferred
So I would vote for:
bool globalInterruptEnabled = bitRead(SREG, SREG_I) == 1;
Owing to severe laziness, I have just loose a point.
Yes! The bitRead() function/macro returns HIGH or LOW and hence the data type should be byte/int and NOT bool.
The code bitRead(SREG, I) is not compiled; so, I have used the bit position which is now replaced by SREG_I in post #9.
void setup()
{
Serial.begin(9600);
cli();
byte n = bitRead(SREG, SREG_I);
Serial.println(n); //shows: 0 ; correct.
}
void loop() {}
//---------------------------------------------------------
Why is bool again?
I have asked myself to expand the above code into multi-lines to understand the meaning but have failed!
that's how the macro is defined
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
it does not return HIGH/LOW
the exact integral type will be the type derived from the expression (depends on the type of value)
given it's a shift right and a mask with 1, the answer is always either 0 or 1
why not. the question was "Detect if interrupts are disabled" so I though you were right to offer a boolean that says so
If 0 is defined as LOW and 1 as HIGH, then I have byte/int data type.
Is 0 is defined as false and 1 as true, then I have bool data type.
//---------------------------------------------------------------------------------------
Given:
value = (SREG) = 10000000
bit = (I) = 7
value = value >> 7 = 00000001
value = vale & 00000001 = 00000001 = 0x01 (HIGH) and NOT 1?
Personal preference:
If I take value = 0x01, then I would say that value is HIGH.
If I take value =1, then I would say that value is true.
LOW and HIGH are #define so would be int (0 is not defined as LOW, it’s the opposite, LOW is defined as 0)
In c++ true and false are not 1 and 0, they are their own type, a truth value. There exists an implicit promotion to stay compatible with C.
Not behind a PC, so can't check.
Use avr-objdump to generate a list file. Next search for cli.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.