So I`ve been using a simple state design pattern for a xiao esp32c3 chip, and upon uploading the code, my state machine works kinda fine, except the fact that in the first state I get spammed with the following:
Rebooting…
ESP-ROM:esp32c3-api1-20210207
Build:Feb 7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0xe (SPI_FAST_FLASH_BOOT)
Saved PC:0x40048b82
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd5820,len:0xf64
load:0x403cc710,len:0x73c
load:0x403ce710,len:0x2eac
entry 0x403cc710
Guru Meditation Error: Core 0 panic’ed (Store access fault). Exception was unhandled.
Core 0 register dump:
MEPC : 0x4038608c RA : 0x40386a0c SP : 0x3fcde250 GP : 0x3fc8c200
TP : 0x00000000 T0 : 0x4005890e T1 : 0x40386292 T2 : 0x00000000
S0/FP : 0x000007d0 S1 : 0x3fc8e000 A0 : 0x00000000 A1 : 0x00000000
A2 : 0x00000003 A3 : 0x00000008 A4 : 0x3fc8e000 A5 : 0x00000000
A6 : 0x00010000 A7 : 0x40384416 S2 : 0x3c034000 S3 : 0x00000000
S4 : 0x0000edd8 S5 : 0x3c030000 S6 : 0xffff0000 S7 : 0x4038133e
S8 : 0x00020020 S9 : 0x00010020 S10 : 0x00000005 S11 : 0x3fcde3c0
T3 : 0x3fc8fa38 T4 : 0xffff0000 T5 : 0x00000000 T6 : 0x00000000
MSTATUS : 0x00001881 MTVEC : 0x40380001 MCAUSE : 0x00000007 MTVAL : 0x00000152
MHARTID : 0x00000000
(stack memory displays here memory adresses… and then this)
ELF file SHA256: 8a3e19fac03e38d8
E (429) esp_core_dump_flash: Core dump flash config is corrupted! CRC=0x7bd5c66f instead of 0x0
E (437) esp_core_dump_elf: Elf write init failed!
E (442) esp_core_dump_common: Core dump write failed with error=-1
This error loops while in the first state, until i write in the serial monitor the input to switch states, which usually works. THe looping with the “Waiting for input” from the first state and the error is asynchronous.
This is my chip.cpp
#include "chip.h"
#include "Arduino.h"
#include <string>
chip::chip()
: reading_pin(D0), power_pin(D2), pwmFreq(5000), pwmResolution(8),
idleState(), pairedState(), genState(), currentState(&idleState){
delayMicroseconds(1000); // Stabilize serial
pinMode(reading_pin, INPUT);
ledcAttach(power_pin, pwmFreq, pwmResolution);
}
void chip::loop() {
this->performState();
this->handle(); // checks serial monitor for requests
//this->printchip();
}
void chip::performState(){
if (currentState == nullptr) {
Serial.println("Error: Current state is null.");
return;
}
currentState->perform(this);
}
void chip::handle() {
if (currentState == nullptr) {
Serial.println("Error: Current state is null.");
return;
}
currentState->handleRequest(this);
}
void chip::setState(State* newState) {
if (newState == nullptr) {
Serial.println("Error: Attempted to set state to null.");
return;
}
currentState = newState; // change states
}
/*
void chip::printchip(){
currentState->printState();
}*/
And my IdleState.cpp, which inherits from state.h
#include "idlestate.h"
#include "chip.h"
#include "Arduino.h"
void IdleState::perform(chip* esp) {
Serial.println("Waiting for HANDSHAKE input...");
delayMicroseconds(1000); // Let the user see the message
}
void IdleState::handleRequest(chip* esp) {
if (Serial.available() > 0) {
String input = Serial.readStringUntil('n');
if (input.equals("HANDSHAKE")){
// blink as sucessfull handshake
for (int i = 0; i < 3; i++) {
esp->ledWrite(255);
delay(100);
esp->ledWrite(0);
delay(100);
esp->ledWrite(255);
delay(100);
esp->ledWrite(0);
delay(100);
}
Serial.println("Comms established! Device paired.nWaiting for GEN input");
esp->setState(&esp->pairedState);
}
else{
Serial.println("Invalid input for current state of the chip");
return;
}
}
}
/*
void IdleState::printState(){
Serial.println("GAGAA");
}*/
So by commenting out functions i found the problem was in the chip’s loop function, and for the error not to happen i had to comment out this->performState() and this->handle(). Even if i commented their bodies, the error would still occur. I tried seeing if this issue was about accessing currentState’s functions,by creating the dummy function “printState()”, and it indeed keeps printing this error. My main.ino file is this:
#include "chip.h"
#include "Arduino.h"
chip babelChip;
void setup(){
Serial.begin(9600);// BAUD HERE
babelChip = chip();
}
void loop(){
babelChip.loop();
}
Keep in mind somehow the state machine works. I’ve tried everything for the last 2 days from debugging with other people to analyzing every line with chatgpt, which makes me think this is either a super niche error or a hardware issue. Any help is greatly appreciated.
Johan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.