Hello,
I've run into a problem recently when working on a new project involving an ESP32 and a max43421e(USB Host). There were relatively few problems regarding the hardware or the library(Host Shield Library 2.0 by Oleg Mazurov).
Problem:
I have an issue when I try to run everything in a FreeRTOS task instead of the setup
and 'loop' functions. *Please bare with me, I'm still very new to FreeRTOS. I placed all the routines and declarations inside the task. This is when the ESP began to crash over and over.
Clue:
I noticed that when I define "USB Usb;
" inside the task, it crashes. When I define "USB Usb;
" outside of the task, globally, it works just fine. At first, I thought it must be some kind of stack overflow, but even when I increase the task's stack to 100's of KB's, it still keeps crashing.
Replicating The Problem:
To replicate the problem, you don't need the max43421e(USB Host). The sketch will behave similarly with or without the shield. I've only tested this on my ESP32. I have not yet tested it on my ESP8266
I just want to know why this is happening. Is it just a dumb mistake on my part or could it be revealing a larger underlying problem? Any help or insight will be greatly appreciated!
Below is the sketch that causes my ESP to crash repeatedly and the Crash reports/error logs that the ESP Generates. The sketch is a modified version of Oleg's "USBHIDBootKbd" example:
[edit]: The library can be found here: GitHub - felis/USB_Host_Shield_2.0: Revision 2.0 of USB Host Library for Arduino.
Sketch:
#include <hidboot.h>
#include <usbhub.h>
#include <SPI.h>
/*
//////// Sketch Overview ////////
class KbdRptParser
{
methods to help report keyboard events. This is handed over to 'HIDBoot' in 'usb_xTask()'
}
usb_xTask()
{
Create USB Objects
Init Serial
Init USB
Run and report USB Tasks
}
setup()
{
Create USB Task
}
loop()
{
}
KbdRptParser Implemented methods:
...
*/
// define 'KbdRptParser' class. Implementations are lower in the sketch
class KbdRptParser : public KeyboardReportParser
{
void PrintKey(uint8_t mod, uint8_t key);
protected:
void OnControlKeysChanged(uint8_t before, uint8_t after);
void OnKeyDown(uint8_t mod, uint8_t key);
void OnKeyUp(uint8_t mod, uint8_t key);
void OnKeyPressed(uint8_t key);
};
// USB Usb; // << If 'Usb' is declared here, the sketch runs without crashing
void usb_xTask(void *__par__)
{
// setup:
USB Usb; // <<< This is the issue, If declared globally, it works fine. If declared within this task, crashes after "USB::Task"
HIDBoot<USB_HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);
KbdRptParser Prs;
Serial.begin(115200);
Serial.println("Start");
// Check if host is communicating. Keep executing regardless, program crash happens either way
if (Usb.Init() == -1)
/* normally exit program or implement a way to wait for USB Shield to begin communicating
For the sake of this example, report and keep executing: */
Serial.println("OSC did not start.");
else
Serial.println("OSC Started");
HidKeyboard.SetReportParser(0, &Prs); // Set parser for keyboard changes
uint8_t usbTask = 255; // value for keeping track of Task State Changes
Serial.println("Start Task");
Usb.Task(); // just an extra 'Task' call to trigger a program crash
Serial.println("End Task, Program didn't crash"); // << If 'Usb' was declared inside 'usb_xTask', the program will never reach this point
// loop:
for (;;)
{
Usb.Task(); // Run Usb Task. <<< This is were I believe the ESP crashes
// simply get the usb task state and report changes:
uint8_t ts = Usb.getUsbTaskState();
if (usbTask != ts)
{
usbTask = ts;
Serial.print("USB Task State: ");
Serial.println(usbTask);
}
delay(10); // allow other tasks to run
}
}
void setup() {
// create task:
xTaskCreate(
usb_xTask, // task name
"USB Task", // task description
2048, // stack size. I don't think this is the issue. I raised the number to hundreds of KBs and still doesn't seem to resolve the issue
NULL, // task parameters
1, // task priority
NULL); // task handler
}
void loop() {delay(10);}
void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
{
MODIFIERKEYS mod;
*((uint8_t *)&mod) = m;
Serial.print((mod.bmLeftCtrl == 1) ? "C" : " ");
Serial.print((mod.bmLeftShift == 1) ? "S" : " ");
Serial.print((mod.bmLeftAlt == 1) ? "A" : " ");
Serial.print((mod.bmLeftGUI == 1) ? "G" : " ");
Serial.print(" >");
PrintHex<uint8_t>(key, 0x80);
Serial.print("< ");
Serial.print((mod.bmRightCtrl == 1) ? "C" : " ");
Serial.print((mod.bmRightShift == 1) ? "S" : " ");
Serial.print((mod.bmRightAlt == 1) ? "A" : " ");
Serial.println((mod.bmRightGUI == 1) ? "G" : " ");
};
void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
{
Serial.print("DN ");
PrintKey(mod, key);
uint8_t c = OemToAscii(mod, key);
if (c)
OnKeyPressed(c);
}
void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after)
{
MODIFIERKEYS beforeMod;
*((uint8_t *)&beforeMod) = before;
MODIFIERKEYS afterMod;
*((uint8_t *)&afterMod) = after;
if (beforeMod.bmLeftCtrl != afterMod.bmLeftCtrl)
{
Serial.println("LeftCtrl changed");
}
if (beforeMod.bmLeftShift != afterMod.bmLeftShift)
{
Serial.println("LeftShift changed");
}
if (beforeMod.bmLeftAlt != afterMod.bmLeftAlt)
{
Serial.println("LeftAlt changed");
}
if (beforeMod.bmLeftGUI != afterMod.bmLeftGUI)
{
Serial.println("LeftGUI changed");
}
if (beforeMod.bmRightCtrl != afterMod.bmRightCtrl)
{
Serial.println("RightCtrl changed");
}
if (beforeMod.bmRightShift != afterMod.bmRightShift)
{
Serial.println("RightShift changed");
}
if (beforeMod.bmRightAlt != afterMod.bmRightAlt)
{
Serial.println("RightAlt changed");
}
if (beforeMod.bmRightGUI != afterMod.bmRightGUI)
{
Serial.println("RightGUI changed");
}
}
void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
{
Serial.print("UP ");
PrintKey(mod, key);
}
void KbdRptParser::OnKeyPressed(uint8_t key)
{
Serial.print("ASCII: ");
Serial.println((char)key);
};
ESP Crash Reports:
Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC : 0x400d2a7f PS : 0x00060e30 A0 : 0x800d1df8 A1 : 0x3ffb8930
A2 : 0x3ffb8990 A3 : 0x3ffb8a30 A4 : 0x00000000 A5 : 0x3ffb8a08
A6 : 0x3ffb8a48 A7 : 0x00000000 A8 : 0x800d2a84 A9 : 0x3ffb8900
A10 : 0x3ffa9bbc A11 : 0xcd8ae580 A12 : 0xcd8ae580 A13 : 0x00000000
A14 : 0x000000ff A15 : 0x3ffb8900 SAR : 0x0000001b EXCCAUSE: 0x0000001c
EXCVADDR: 0x0000000c LBEG : 0x400862c1 LEND : 0x400862d1 LCOUNT : 0xfffffffd
Backtrace: 0x400d2a7c:0x3ffb8930 0x400d1df5:0x3ffb8950