Description
Board
ESP32-DevKitC
Device Description
DevKitC
Hardware Configuration
For the demo code below, connect a switch or square wave to pin 19.
Version
v2.0.2
IDE Name
PlatformIO
Operating System
macOS
Flash frequency
40MHz
PSRAM enabled
no
Upload speed
115200
Description
The pinMode
call, in addition to changing pullups/whether a pin is driving the output or not/etc, appears to disable pin interrupts as a side effect. In the ESP-IDF version of the code, intr_type
is explicitly set to GPIO_INTR_DISABLE
. In the older register code, the field is accidentally overwritten by the write to GPIO.pin[x].val
when it's really just trying to set GPIO.pin[x].pad_driver
.
This is a problem because while pinMode
is IRAM_ATTR
and safe to call from interrupt handlers, attachInterrupt
is not (and it's also rather heavyweight), so it's not possible to re-enable the interrupt if pinMode
is being used in an ISR.
pinMode
should just preserve whatever previous state of interrupts existed; if the user has an interrupt configured on a pin and wants to change the pin mode, they probably know what they are doing. If they want to disable the interrupt they can call detachInterrupt
directly.
Sketch
#include "Arduino.h"
// This sketch should print the number of times pin 19 changes.
uint32_t interrupt_count = 0;
void IRAM_ATTR pin_interrupt(void *arg) {
interrupt_count++;
pinMode(19, INPUT);
}
void setup() {
pinMode(19, INPUT);
attachInterruptArg(19, pin_interrupt, 0, CHANGE);
}
void loop() {
ESP_LOGD("loop", "interrupt count %d", interrupt_count);
delay(500);
}
Debug Message
n/a
Other Steps to Reproduce
No response
I have checked existing issues, online documentation and the Troubleshooting Guide
- I confirm I have checked existing issues, online documentation and Troubleshooting guide.
Metadata
Metadata
Assignees
Type
Projects
Status