ESP Arduino library version 1.0.6
Arduino IDE version 1.8.13
Problem description: I'm getting input from the serial monitor (through USB) into my application. Since the length of the input is variable, I use the function "Serial.readBytesUntil('\n', incomingByteArray, 20)" to capture the incoming characters. If the input is terminated by '\n' the program will parse the string immediately. If there is no terminating '\n', Serial waits (in my case the time-out is set to 500 ms) and then we move on to parsing the string.
Seems to work OK. But every now and then I get this Guru meditaion error:
Guru Meditation Error: Core 0 panic'ed (IllegalInstruction). Exception was unhandled.
Memory dump at 0x400e2564: 00000000 ffffaf00 ffffffff
When I check using the exception decoder, I always get this:
Decoding stack results
0x400e256b: timer_task at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/esp_timer.c line 325
0x400860ed: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143
According to this, a timer is involved in the crash. But I'm not using a timer in my code, except under the hood, in the Serial.readBytesUntil() function. Something wrong with the function?
When I enter 1 character terminated or not by '\n' I never get a crash.
When I enter multiple characters I get the crash, but only occasionally.
Here is a dump from my serial monitor:
It shows that the program is crashing several seconds after the last user interaction. It is supposed to do nothing in the main loop. And then it crashes, apparently out of the blue.ESP32 C-Meter v1.0
10:55:32.684 ->
10:55:32.684 -> incomingByteArray = c
10:55:32.684 -> Resetting PINs
10:55:32.684 ->
10:55:32.684 -> User Connected!
10:55:35.381 ->
10:55:35.381 -> incomingByteArray = ccc
10:55:41.578 -> incomingByteArray = dddddddGuru Meditation Error: Core 0 panic'ed (IllegalInstruction). Exception was unhandled.
10:56:15.299 -> Memory dump at 0x400e2564: 00000000 ffffaf00 ffffffff
10:56:15.299 -> Core 0 register dump:
10:56:15.299 -> PC : 0x400e256b PS : 0x00060b30 A0 : 0x800860f0 A1 : 0x3ffb78b0
10:56:15.346 -> A2 : 0x00000000 A3 : 0x00000000 A4 : 0x00060023 A5 : 0x3ffb796c
10:56:15.346 -> A6 : 0x00000000 A7 : 0x00000001 A8 : 0x800e256b A9 : 0x3ffb7870
10:56:15.346 -> A10 : 0x00000001 A11 : 0x00000000 A12 : 0x3ffaffe8 A13 : 0x00000000
10:56:15.346 -> A14 : 0x3ffaff9c A15 : 0x00060023 SAR : 0x00000000 EXCCAUSE: 0x00000000
10:56:15.346 -> EXCVADDR: 0x00000000 LBEG : 0x00000000 LEND : 0x00000000 LCOUNT : 0x00000000
10:56:15.346 ->
10:56:15.346 -> ELF file SHA256: 0000000000000000
10:56:15.346 ->
10:56:15.346 -> Backtrace: 0x400e256b:0x3ffb78b0 0x400860ed:0x3ffb78d0
10:56:15.394 ->
10:56:15.394 -> Rebooting...
10:56:15.394 -> ets Jun 8 2016 00:22:57
10:56:15.394 ->
10:56:15.394 -> rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
10:56:15.394 -> configsip: 0, SPIWP:0xee
10:56:15.394 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
10:56:15.394 -> mode:DIO, clock div:1
10:56:15.394 -> load:0x3fff0018,len:4
10:56:15.394 -> load:0x3fff001c,len:1044
10:56:15.394 -> load:0x40078000,len:10124
10:56:15.394 -> load:0x40080400,len:5856
10:56:15.394 -> entry 0x400806a8
10:56:15.750 -> ESP32 C-Meter v1.0
Code:
Code: Select all
#include "driver/gpio.h"
#include "driver/adc.h"
#define MAX_INPUTCHARS 256
unsigned long time0,time1,time2;
float c,null0;
byte kn,mk,i;
bool waiting;
char incomingByte;
char incomingByteArray[MAX_INPUTCHARS] = {0};
int measurement;
float capValue;
unsigned long startTime, currentTime, duration;
#define PIN_Discharge 32 // Discharge via 100 ohm resistor
#define PIN_PicoCharge 33 // Charge via 1k resistor
#define PIN_NanoCharge 25 // Charge via 10k resistor
#define PIN_MicroCharge 26 // Charge via 100k resistor
#define PIN_ADC ADC1_CHANNEL_6 // ADC1 channel 6 is GPIO34
#define TAU_VALUE 2580 // 63.21% of 4095
#define Micro_R 1000 // 1 k-ohm
#define Nano_R1 10000 // 10 k-ohm
#define Nano_R2 100000 // 100 k-ohm
#define Pico_R1 1000000 // 1 M-ohm
#define MaxDuration 10000000 // hard limit = 10 seconds
void setup() {
// put your setup code here, to run once:
mk = 0;
// make all pins High-Z
Serial.begin(115200); // Arduino IDE serial port for debugging @ 115.2 kBaud
while (!Serial) continue; // wait for serial port to connect. Needed for native USB
delay(200);
Serial.println("ESP32 C-Meter v1.0");
Command_Init(); // set up Command interpreter
}
void loop() {
Command_Parse(); // parse command string and execute command
}
unsigned int arrayIndex;
bool userConnected;
// set state and wait for user input
void Command_Init() {
userConnected = false;
Serial.setTimeout(500); // wait max 500 ms
}
// Parse the serial input auto-terminate after serial time-out
// If the command is valid, execute
// If the command is invalid, echo the bad command, empty the serial buffer and return to loop
void Command_Parse() {
if(Serial.available()){ // check the serial port
memset(incomingByteArray, NULL, sizeof(incomingByteArray)); // clear the local char buffer
Serial.readBytesUntil('\n', incomingByteArray, 20); // read up to 20 chars, wait max 500 ms
while(Serial.available() > 0){ // discard extra chars after '\n'
Serial.read();
}
// incomingByteArray contains the user command
Serial.print("\nincomingByteArray = "); // just checking ...
Serial.print(incomingByteArray);
}
else
return; // back to main loop
if(strcasecmp(incomingByteArray, "C") == 0){ // connect ?
userConnected = true; // update connection status
ResetAllPins(); // initialize all PINs to High-Z
Serial.println("\nUser Connected!"); // send response
return; // back to main loop
}
}
void ResetAllPins(){
Serial.println("\nResetting PINs");
pinMode(PIN_MicroCharge, INPUT);
pinMode(PIN_PicoCharge, INPUT);
pinMode(PIN_NanoCharge, INPUT);
pinMode(PIN_Discharge, INPUT);
pinMode(PIN_ADC, INPUT);
}
I just recompiled the program (after commenting out a section of code) and now the line "Serial.println("\nUser Connected!");" only produces "User Con" in the serial monitor. I the input a few times "cccc" or "dddd" and the program reponds as expected. 20 seconds later (!) the program crashes again.
I tried a few more things and now commented out the call to ResetAllPins().
To my big surprise the program is now behaving as expected and not crashing anymore.
What is wrong with this:
Code: Select all
void ResetAllPins(){
Serial.println("\nResetting PINs");
pinMode(PIN_MicroCharge, INPUT);
pinMode(PIN_PicoCharge, INPUT);
pinMode(PIN_NanoCharge, INPUT);
pinMode(PIN_Discharge, INPUT);
pinMode(PIN_ADC, INPUT);
}