-
Notifications
You must be signed in to change notification settings - Fork 7.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds noInterrupt() and interrupt() functionality #7226
Conversation
Tested on S3 and I'm getting 51 instead of 50. Tried various pins: |
Thanks. This seems to resolve the issue with the library here: However, I'm also seeing the extra count show up in the simple test sketch. Revised test sketch: #define OUT_PIN 14
#define IN_PIN 32
int count = 0;
void counter() {
count++;
}
void setup() {
Serial.begin(9600);
pinMode(OUT_PIN, OUTPUT);
digitalWrite(OUT_PIN, LOW);
pinMode(IN_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(IN_PIN), counter, RISING);
for (int i=0; i<50; i++) {
digitalWrite(OUT_PIN, HIGH);
digitalWrite(OUT_PIN, LOW);
}
Serial.print("count = "); Serial.println(count);
noInterrupts();
for (int i=0; i<50; i++) {
digitalWrite(OUT_PIN, HIGH);
digitalWrite(OUT_PIN, LOW);
}
interrupts();
Serial.print("count = "); Serial.println(count);
}
void loop() {
} Outputs:
|
I am seeing just same result as guys mentioned above, tested on ESP32. |
I have edited the sketch @caternuson posted to demonstrate, that my previous comment really happening.
Output is now:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my point of view it works as expected, there is no easy way to clear all coming interrupts before Interrupts()
is called. As suggested you must be careful when using noInterrupts()
, and its better to use detachInterrupt()
.
@@ -79,10 +79,9 @@ | |||
#define degrees(rad) ((rad)*RAD_TO_DEG) | |||
#define sq(x) ((x)*(x)) | |||
|
|||
#define sei() | |||
#define cli() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could have define sei and cli instead for backwards compatibility with older code. Any reason you didn't @SuGlider?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I know, SEI and CLI are ASM instructions used in ATMEL chips.
Arduino official API is interrupts()
and noInterrupts()
I see no reason for defining those ATMEL specific functions.
//interrupts is defined as:
#define interrupts() sei()
//and sei is defined as for the UNO/328p:
# define sei() __asm__ __volatile__ ("sei" ::)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking ESP8266 Arduino.h
I see that it also doesn't define sei()
nor cli()
.
My vote is for keeing it as defined in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
interrupts()
and noInterrupts()
are described in Arduino Language Reference so I vote for this too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, added back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
Having both variants should not hurt... |
@SuGlider will merge as soon as you re-add sei and cli :) |
Adds back sei()/cli() macros
Description of Change
Adds noInterrupt() and interrupt() to Arduino.
This must be used carefully because ESP32 Arduino runs under FreeRTOS and it is easy to get Watchdog Timeout error if it is no correctly used.
Tests scenarios
Tested with this sketch (as suggested in the issue)
Expected output:
wrong output (before this PR) says
count = 100
Related links
Fixes #7211