-
Notifications
You must be signed in to change notification settings - Fork 65
Description
After successfully running the Accessory test program that is part of the library I started using the code in a larger program. It started crashing as soon as the DCC signal was present, which pointed my look closer into the ISR.
In the process I think I have discovered some problems with the ISR:
a) for ESP32, it should be compiled with the IRAM_ATTR attribute like this: void IRAM_ATTR ExternalInterruptHandler(void)
For more information see here: https://techtutorialsx.com/2017/09/30/esp32-arduino-external-interrupts/
and here: espressif/arduino-esp32#489
b) I also added the MUX line: portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED; and secured the access to the shard data in the ISR and the process routing:
portENTER_CRITICAL_ISR(&mux); DccRx.PacketCopy = DccRx.PacketBuf ; DccRx.DataReady = 1 ; portEXIT_CRITICAL_ISR(&mux);
Unfortunately, while all this is good, it was still crashing as soon as my program tried to access the file system (SPIFFS), eg. for loading config files or a serving a web page.
c) It turns out the real problem is the reattachment of interrupts within the IRS. As soon as I commented those lines out, the program stopped crashing, but unfortunately, it would no longer decode the DCC signals. So I found a workaround and defined a variable ISRWatch which holds the type of interrupt to look for, either falling, raising, or change. Then I replaced all attach interrupt functions by just changing the content of the ISRWatch variable and changed the attacheinterrupt call in the init function to CHANGE. Finally, I added a filter to the beginning of the ISR where I would check the type of interrupt and either return or proceed:
void IRAM_ATTR ExternalInterruptHandler(void)
{
switch (ISRWatch)
{
case CHANGE: break;
case RISING: if (digitalRead(DccProcState.ExtIntPinNum)) break;
case FALLING: if (digitalRead(DccProcState.ExtIntPinNum)) return; break;
}
In this quick fix I did not care about support for other processors etc. So maybe someone with good knowledge of the entire could make the proper adjustments so it works with all processors.