sandeepmistry / arduino-LoRa

An Arduino library for sending and receiving data using LoRa radios.
MIT License
1.65k stars 630 forks source link

ESP32: float value in ISR (Guru Meditation Error: Core 1 panic'ed (Coprocessor exception)) #459

Open sacculus opened 3 years ago

sacculus commented 3 years ago

Hi I'm writing sketch for esp32 board with event driven model when send/receive. I got exception when have tried print SNR & frequency error in onReceive callback like this:

Serial.printf("% 10d: LoRa: RSSI: %d, SNR: %0.2f, Frequency error: %d\n", millis(), LoRa.packetRssi(), LoRa.packetSnr(), LoRa.packetFrequencyError());

Exception:

`Guru Meditation Error: Core 1 panic'ed (Coprocessor exception) Core 1 register dump: PC : 0x400d0fee PS : 0x00060a31 A0 : 0x800d17c2 A1 : 0x3ffbea50
A2 : 0x0000153f A3 : 0x00000001 A4 : 0x3ffbffc4 A5 : 0x00000000
A6 : 0xffffffb7 A7 : 0x0000002f A8 : 0x800d0fee A9 : 0x3ffbeaf0
A10 : 0x41080000 A11 : 0x0000005b A12 : 0xc87536dc A13 : 0x3ffc0794
A14 : 0x3ffbeaac A15 : 0x00000000 SAR : 0x0000001d EXCCAUSE: 0x00000004
EXCVADDR: 0x00000000 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xfffffff4
Core 1 was running in ISR context: EPC1 : 0x400d0fee EPC2 : 0x00000000 EPC3 : 0x00000000 EPC4 : 0x400841af

ELF file SHA256: 0000000000000000

Backtrace: 0x400d0fee:0x3ffbea50 0x400d17bf:0x3ffbeb10 0x400d17bf:0x3ffbeb50 0x400811e6:0x3ffbeb70 0x40081231:0x3ffbeb90 0x40083f95:0x3ffbebb0 0x400d2804:0x3ffb1fb0 0x4008689d:0x3ffb1fd0

Rebooting... ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:1 load:0x3fff0018,len:4 load:0x3fff001c,len:1044 load:0x40078000,len:10124 load:0x40080400,len:5856 entry 0x400806a8 `

Decoded exception:

`PC: 0x400d0fee: onReceive(int) at /data/Projects/Arduino/sketch/SandeepMistry/lora_client_32/lora_client_32.ino line 145 EXCVADDR: 0x00000000

Decoding stack results 0x400d0fee: onReceive(int) at /data/Projects/Arduino/sketch/SandeepMistry/lora_client_32/lora_client_32.ino line 145 0x400d17bf: LoRaClass::handleDio0Rise() at /data/Projects/Arduino/libraries/LoRa/src/LoRa.cpp line 712 0x400d17bf: LoRaClass::handleDio0Rise() at /data/Projects/Arduino/libraries/LoRa/src/LoRa.cpp line 712 0x400811e6: LoRaClass::onDio0Rise() at /data/Projects/Arduino/libraries/LoRa/src/LoRa.cpp line 751 0x40081231: __onPinInterrupt at /home/mike/.arduino15/packages/esp32/hardware/esp32/1.0.6/cores/esp32/esp32-hal-gpio.c line 220 0x400d2804: loopTask(void*) at /home/mike/.arduino15/packages/esp32/hardware/esp32/1.0.6/cores/esp32/main.cpp line 24 0x4008689d: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143`

Googling led me to this issue. I replased float to double in library source for test and error has gone. What I've changed:

`diff -urN LoRa.ORIG/src/LoRa.cpp LoRa/src/LoRa.cpp --- LoRa.ORIG/src/LoRa.cpp 2020-12-05 17:38:41.000000000 +0200 +++ LoRa/src/LoRa.cpp 2021-04-20 22:51:42.099699743 +0300 @@ -266,7 +266,7 @@ return (readRegister(REG_PKT_RSSI_VALUE) - (_frequency < RF_MID_BAND_THRESHOLD ? RSSI_OFFSET_LF_PORT : RSSI_OFFSET_HF_PORT)); }

-float LoRaClass::packetSnr() +double LoRaClass::packetSnr() { return ((int8_t)readRegister(REG_PKT_SNR_VALUE)) * 0.25; } @@ -284,8 +284,8 @@ freqError -= 524288; // B1000'0000'0000'0000'0000 }

IoTThinks commented 3 years ago

I hit the same issue for esp32. When I put the packetSNR outside the ISR, the issue was gone.

Wonder how it affects Arduino boards?

Could you create a pull request for this?

sacculus commented 3 years ago

I hit the same issue for esp32. When I put the packetSNR outside the ISR, the issue was gone.

Not only packetSNR, packetFrequencyError leds this exception too.

IoTThinks commented 3 years ago

I remember packetSNR works fine in ISR for my esp32.

Only packetFrequencyError hits issue for me in ISR.

italocjs commented 3 years ago

Any bypass or fix for this beside using double? i'm having the exact same issue...

sacculus commented 3 years ago

Any bypass or fix for this beside using double? i'm having the exact same issue...

Hi Some info is there in this thread, take a look on this post.

italocjs commented 3 years ago

Any bypass or fix for this beside using double? i'm having the exact same issue...

Hi Some info is there in this thread, take a look on this post.

Thanks, but i ended working with int (old float * 100), my application needed to be as optimized as possible duo a massive amount of requests per second (the interrupt was being called by a CANBUS event)

huexpub commented 2 years ago

you must use xqueue with get isr interrupt