ESP32 IllegalInstruction
Posted: Mon Feb 07, 2022 1:26 pm
I'm building a program that reads data from a RFID receiver through UART2 on ESP32 chip.
However there's a strange phenomenon.
main.cppRFID_Reader.cpp
RFID_Reader.h
Utils.h
What's more strange, if I uncomment any Serial.print() in function RFID_Read_Loop(), It can work perfect without any crash.
Print any string in the fuction can solve this.
I dont know how do this happen. Why print a string to Serial can have impact on the crash?
However there's a strange phenomenon.
Here PC is a pointer to an address out of the range.Guru Meditation Error: Core 1 panic'ed (IllegalInstruction). Exception was unhandled.
Core 1 register dump:
PC : 0x6b726f77 PS : 0x00060330 A0 : 0x800d0c89 A1 : 0x3ffb1f50
A2 : 0x3ffc0074 A3 : 0x3ffc0065 A4 : 0x80086330 A5 : 0x3ffb1e70
A6 : 0x00000000 A7 : 0x3ffb0060 A8 : 0x800d0dd3 A9 : 0x3ffb1f30
A10 : 0x3ffc0074 A11 : 0x00000000 A12 : 0x80086330 A13 : 0x3ffb1e50
A14 : 0x00000000 A15 : 0x3ffb0060 SAR : 0x00000018 EXCCAUSE: 0x00000000
EXCVADDR: 0x00000000 LBEG : 0x4000c28c LEND : 0x4000c296 LCOUNT : 0x00000000
ELF file SHA256: 0000000000000000
Backtrace: 0x6b726f77:0x3ffb1f50 0x400d0c86:0x3ffb1f70 0x400d0d5e:0x3ffb1f90 0x400d1fb9:0x3ffb1fb0 0x40086125:0x3ffb1fd0
Rebooting...
␙ 0�␄D)!1�9␌��)��!��Serial begins successfully.
main.cpp
- #include <Arduino.h>
- #include "lib/RFID_Reader.h"
- #include "lib/Utils.h"
- RFID_Reader RF_Reader;
- void RFID_Callback(RFID_Pkg *pkg)
- {
- }
- void setup()
- {
- Serial.begin(9600);
- Serial.println("Serial begins successfully.");
- RF_Reader.Init(RFID_Callback);
- }
- void loop()
- {
- RF_Reader.RFID_Read_Loop();
- }
- #include "RFID_Reader.h"
- #include <Arduino.h>
- RFID_Reader::RFID_Reader()
- {
- }
- RFID_Reader::~RFID_Reader()
- {
- }
- void RFID_Reader::Init(void (*pCallbackFunc)(RFID_Pkg *))
- {
- Reset();
- pCallback = pCallbackFunc;
- Serial2.begin(9600);
- }
- void RFID_Reader::RFID_Read_Loop()
- {
- char c = Serial2.read();
- if (m_State != STATE_WAITING_CMD_CAT || c != 0xff)
- {
- // Serial.print(" ");
- }
- switch (m_State)
- {
- case STATE_WAITING_CMD_CAT:
- if (c != 0xff)
- {
- m_Pkg.m_Cmd_Cat[0] = c;
- m_State = STATE_WAITING_PKG_LEN;
- }
- // Serial.println("STATE_WAITING_CMD_CAT");
- break;
- case STATE_WAITING_PKG_LEN:
- if (c != 0xff)
- {
- m_Pkg.m_Pkg_Len = c;
- m_Remain_Count = m_Pkg.m_Pkg_Len - 2;
- m_State = STATE_WAITING_CMD;
- // Serial.println("STATE_WAITING_CMD_CAT");
- }
- break;
- case STATE_WAITING_CMD:
- m_Pkg.m_Cmd[0] = c;
- m_Remain_Count--;
- m_State = STATE_WAITING_ADDRESS;
- // Serial.println("STATE_WAITING_CMD");
- break;
- case STATE_WAITING_ADDRESS:
- m_Pkg.m_Address[0] = c;
- m_Remain_Count--;
- m_State = STATE_WAITING_CMD_STATE;
- // Serial.println("STATE_WAITING_ADDRESS");
- break;
- case STATE_WAITING_CMD_STATE:
- m_Pkg.m_Cmd_State[0] = c;
- m_Remain_Count--;
- m_State = STATE_WAITING_DATA;
- // Serial.println("STATE_WAITING_CMD_STATE");
- break;
- case STATE_WAITING_DATA:
- if (m_Remain_Count > 1)
- {
- m_Pkg.m_Data[m_Pkg.m_Data_Count] = c;
- m_Remain_Count--;
- m_Pkg.m_Data_Count++;
- m_State = STATE_WAITING_DATA;
- }
- else
- {
- m_State = STATE_WAITING_VARY;
- }
- // Serial.println("STATE_WAITING_DATA");
- break;
- case STATE_WAITING_VARY:
- m_Pkg.m_Vary[0] = c;
- Finish();
- // Serial.println("STATE_WAITING_VARY");
- break;
- default:
- break;
- }
- }
- void RFID_Reader::Finish()
- {
- pCallback(&m_Pkg);
- Reset();
- }
- void RFID_Reader::Reset()
- {
- m_State = STATE_WAITING_CMD_CAT;
- m_Remain_Count = 0;
- m_Pkg.m_Pkg_Len = 0;
- m_Pkg.m_Data_Count = 0;
- }
- #ifndef RFID_READER_LIB
- #define RFID_READER_LIB
- struct RFID_Pkg
- {
- unsigned char m_Cmd_Cat[1];
- unsigned int m_Pkg_Len;
- unsigned char m_Cmd[1];
- unsigned char m_Address[1];
- unsigned char m_Cmd_State[1];
- unsigned char m_Data[512];
- unsigned char m_Vary[1];
- unsigned int m_Data_Count;
- };
- class RFID_Reader
- {
- private:
- enum RFID_Reader_State
- {
- STATE_WAITING_CMD_CAT,
- STATE_WAITING_PKG_LEN,
- STATE_WAITING_CMD,
- STATE_WAITING_ADDRESS,
- STATE_WAITING_CMD_STATE,
- STATE_WAITING_DATA,
- STATE_WAITING_VARY,
- } m_State;
- RFID_Pkg m_Pkg;
- unsigned int m_Remain_Count;
- public:
- RFID_Reader();
- ~RFID_Reader();
- void (*pCallback)(RFID_Pkg *);
- void Init(void (*pCallbackFunc)(RFID_Pkg *));
- void RFID_Read_Loop();
- void Reset();
- void Finish();
- };
- #endif
- #include <Arduino.h>
- #ifndef UTILS_LIB
- #define UTILS_LIB
- void ConvertNumber(unsigned long n, uint8_t base, char *s)
- {
- char buf[8 * sizeof(n) + 1]; // Assumes 8-bit chars plus zero byte.
- char *str = &buf[sizeof(buf) - 1];
- *str = '\0';
- // prevent crash if called with base == 1
- if (base < 2)
- {
- base = 10;
- }
- do
- {
- char c = n % base;
- n /= base;
- *--str = c < 10 ? c + '0' : c + 'A' - 10;
- } while (n);
- strcpy(s, str);
- }
- #endif
Print any string in the fuction can solve this.
I dont know how do this happen. Why print a string to Serial can have impact on the crash?