tobozo / ESP32-USB-Soft-Host

An Arduino wrapper to @sdima1357's usb_soft_host esp-idf example
GNU Affero General Public License v3.0
293 stars 44 forks source link

Crashes when using Bluetooth #14

Open kice opened 3 years ago

kice commented 3 years ago

Base on the example here, I added ESP32 BLE Keyboard library to it.

#include <BleKeyboard.h>
BleKeyboard bleKeyboard;

void setup()
{
  //...
  Serial.println("Starting BLE work!");
  bleKeyboard.begin();
  return;
}

I found that it crashes at whatever execute later, bleKeyboard.begin() or USH.init().

The ESP32 I am using is ESP32-DevkitC (ESP32­WROOM­32D).

ble.begin() before USH.init()

Starting BLE work!
USB Soft Host Test for Default Wroom
USB#0 (pins 16 17) is OK!
READ_BOTH_PINS = 0000
READ_BOTH_PINS = 0000
READ_BOTH_PINS = 0000
READ_BOTH_PINS = 0000
cpu freq = 240 MHz
TIME_MULT = 51 
ets Jun  8 2016 00:22:57

rst:0x8 (TG1WDT_SYS_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:0x3fff0030,len:1240
load:0x40078000,len:13012
load:0x40080400,len:3648
entry 0x400805f8
Starting BLE work!
USB Soft Host Test for Default Wroom
USB#0 (pins 16 17) is OK!
READ_BOTH_PINS = 0000
READ_BOTH_PINS = 0000
READ_BOTH_PINS = 0000
READ_BOTH_PINS = 0000
cpu freq = 240 MHz
TIME_MULT = 51 
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:0x3fff0030,len:1240
load:0x40078000,len:13012
load:0x40080400,len:3648
entry 0x400805f8

ble.begin() after USH.init()

USB Soft Host Test for Default Wroom
USB#0 (pins 16 17) is OK!
READ_BOTH_PINS = 0000
READ_BOTH_PINS = 0000
READ_BOTH_PINS = 0000
READ_BOTH_PINS = 0000
cpu freq = 240 MHz
TIME_MULT = 51 
D=   0 4800 bits in 1157.000000 uSec 4.148660 MHz  6 ticks in 1.446250 uS
D= 127 4800 bits in 3672.000000 uSec 1.307190 MHz  6 ticks in 4.590000 uS
D=  63 4800 bits in 2411.000000 uSec 1.990875 MHz  6 ticks in 3.013750 uS
D=  95 4800 bits in 3011.000000 uSec 1.594155 MHz  6 ticks in 3.763750 uS
D= 111 4800 bits in 3378.000000 uSec 1.420959 MHz  6 ticks in 4.222500 uS
D= 103 4800 bits in 3192.000000 uSec 1.503759 MHz  6 ticks in 3.990000 uS
D= 107 4800 bits in 3257.000000 uSec 1.473749 MHz  6 ticks in 4.071250 uS
D= 105 4800 bits in 3251.000000 uSec 1.476469 MHz  6 ticks in 4.063750 uS
D= 104 4800 bits in 3198.000000 uSec 1.500938 MHz  6 ticks in 3.997500 uS
D= 104 4800 bits in 3191.000000 uSec 1.504231 MHz  6 ticks in 3.988750 uS
TRANSMIT_TIME_DELAY = 106 time = 3.997500 error = -0.062501% 
USB#1 is disabled by user configuration
USB#2 is disabled by user configuration
USB#3 is disabled by user configuration
USB Soft Host init successfully
Starting BLE work!
Guru Meditation Error: Core  1 panic'ed (IllegalInstruction). Exception was unhandled.
Memory dump at 0x400d398c: 0000f01d 91004136 8230f1e6
Core  1 register dump:
PC      : 0x400d3992  PS      : 0x00060031  A0      : 0x80081216  A1      : 0x3ffbeea0  
A2      : 0x3ffc2d80  A3      : 0x3ffc3b70  A4      : 0x0000037c  A5      : 0x3ffc2b6c  
A6      : 0xfffffff7  A7      : 0x3ffc1464  A8      : 0x800813b1  A9      : 0x3ff00040  
A10     : 0x00000011  A11     : 0x00000011  A12     : 0x8008286c  A13     : 0x3ffcfa60  
A14     : 0x00000020  A15     : 0x13000054  SAR     : 0x00000020  EXCCAUSE: 0x00000000  
EXCVADDR: 0x00000000  LBEG    : 0x4008fbb8  LEND    : 0x4008fbc3  LCOUNT  : 0x00000000  

Backtrace:0x400d398f:0x3ffbeea00x40081213:0x3ffbeec0 0x40084af1:0x3ffbeee0 0x4009635b:0x3ffcfa40 0x400960ed:0x3ffcfa60 0x40098aa9:0x3ffcfaa0 0x40082251:0x3ffcfb20 0x400e188a:0x3ffcfb60 0x400e344e:0x3ffcfb90 0x400e3f79:0x3ffcfbb0 0x400e44dd:0x3ffcfbd0 0x400e2881:0x3ffcfc60 0x400e2f2a:0x3ffcfce0 0x400e32e3:0x3ffcfd50 0x400e1b03:0x3ffcfd70 0x400e1f85:0x3ffcfda0 0x40165dd7:0x3ffcfdc0 0x40165fbf:0x3ffcfe30 0x401660a4:0x3ffcfe60 0x400eb010:0x3ffcfe80 0x400db95a:0x3ffcfea0 0x400d74dd:0x3ffcfef0 0x400d453a:0x3ffcff20 0x400d1f4a:0x3ffcff60 0x400dc33f:0x3ffcffb0 

ELF file SHA256: 0000000000000000

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

rst:0x3 (SW_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:0x3fff0030,len:1240
load:0x40078000,len:13012
load:0x40080400,len:3648
entry 0x400805f8
USB Soft Host Test for Default Wroom
USB#0 (pins 16 17) is OK!
READ_BOTH_PINS = 0000
READ_BOTH_PINS = 0000
READ_BOTH_PINS = 0000
READ_BOTH_PINS = 0000
cpu freq = 240 MHz
TIME_MULT = 51 
D=   0 4800 bits in 1157.000000 uSec 4.148660 MHz  6 ticks in 1.446250 uS
D= 127 4800 bits in 3672.000000 uSec 1.307190 MHz  6 ticks in 4.590000 uS
D=  63 4800 bits in 2411.000000 uSec 1.990875 MHz  6 ticks in 3.013750 uS
D=  95 4800 bits in 3011.000000 uSec 1.594155 MHz  6 ticks in 3.763750 uS
D= 111 4800 bits in 3378.000000 uSec 1.420959 MHz  6 ticks in 4.222500 uS
D= 103 4800 bits in 3192.000000 uSec 1.503759 MHz  6 ticks in 3.990000 uS
D= 107 4800 bits in 3258.000000 uSec 1.473297 MHz  6 ticks in 4.072500 uS
D= 105 4800 bits in 3251.000000 uSec 1.476469 MHz  6 ticks in 4.063750 uS
D= 104 4800 bits in 3198.000000 uSec 1.500938 MHz  6 ticks in 3.997500 uS
D= 104 4800 bits in 3191.000000 uSec 1.504231 MHz  6 ticks in 3.988750 uS
TRANSMIT_TIME_DELAY = 106 time = 3.997500 error = -0.062501% 
USB#1 is disabled by user configuration
USB#2 is disabled by user configuration
USB#3 is disabled by user configuration
USB Soft Host init successfully
Starting BLE work!
Guru Meditation Error: Core  1 panic'ed (IllegalInstruction). Exception was unhandled.
Memory dump at 0x400d398c: 0000f01d 91004136 8230f1e6
Core  1 register dump:
PC      : 0x400d3992  PS      : 0x00060031  A0      : 0x80081216  A1      : 0x3ffbeea0  
A2      : 0x3ffc2d80  A3      : 0x3ffc3b70  A4      : 0x0000037c  A5      : 0x3ffc2b6c  
A6      : 0xfffffff7  A7      : 0x3ffc1464  A8      : 0x800813b1  A9      : 0x3ff00040  
A10     : 0x00000011  A11     : 0x00000011  A12     : 0x8008286c  A13     : 0x3ffcfa60  
A14     : 0x00000020  A15     : 0x13000054  SAR     : 0x00000020  EXCCAUSE: 0x00000000  
EXCVADDR: 0x00000000  LBEG    : 0x4008fbb8  LEND    : 0x4008fbc3  LCOUNT  : 0x00000000  

Backtrace:0x400d398f:0x3ffbeea00x40081213:0x3ffbeec0 0x40084af1:0x3ffbeee0 0x40096356:0x3ffcfa40 0x400960ed:0x3ffcfa60 0x40098aa9:0x3ffcfaa0 0x40082251:0x3ffcfb20 0x400e188a:0x3ffcfb60 0x400e344e:0x3ffcfb90 0x400e3f79:0x3ffcfbb0 0x400e44dd:0x3ffcfbd0 0x400e2881:0x3ffcfc60 0x400e2f2a:0x3ffcfce0 0x400e32e3:0x3ffcfd50 0x400e1b03:0x3ffcfd70 0x400e1f85:0x3ffcfda0 0x40165dd7:0x3ffcfdc0 0x40165fbf:0x3ffcfe30 0x401660a4:0x3ffcfe60 0x400eb010:0x3ffcfe80 0x400db95a:0x3ffcfea0 0x400d74dd:0x3ffcfef0 0x400d453a:0x3ffcff20 0x400d1f4a:0x3ffcff60 0x400dc33f:0x3ffcffb0 

ELF file SHA256: 0000000000000000

Rebooting...
tobozo commented 3 years ago

Hi, thanks for your feedback

Both BLE and USB Host uses group timers so it'll always crash.

It'll probably crash with WiFi too, best test case scenario was using SPI for SD and TFT in separate tasks with a semaphore.

This library is just a wrapper over a proof of concept anyway, for real USB project I use an ESP32-S2 instead, it doesn't have the dual core but tinyusb support is getting better every day.

kice commented 3 years ago

I knew that ESP32 has USB support, but without BLE is not what I want. I do have an ESP32-S3 on the way, but before that (but also third parties ESP32-S3 hit the market ) I would like to see if we could avoid the group timer crash.

As you can guess, I want to make a BT keyboard using normal wired USB keyboard. I assume if I use software group timer should be fine? But not sure what's the draw back for it since I don't have any experience with esp 32.

Or maybe use macros to hook into the BLE keyboard library if it is just software compatibility issue.

tobozo commented 3 years ago

have you tried to play with USB_SOFT_HOST::TimerPause() and USB_SOFT_HOST::TimerResume() ?

ugurbor commented 3 years ago

I am a complete noob here but can't we switch to the other Timer Group since there are two? If BLE uses timer_group_0, USH can use the timer_group_1, can't it? Like on here author mentions he had to switch to timer group 1 to prevent crashes: https://ojdip.net/2020/11/esp32-cube-timer/

tobozo commented 3 years ago

I'm noob too and can't understand much of the code in this library, especially group timers :-)

My assumptions were that Arduino comes with some config settings that can't be changed and now I'm in doubt.

timer group init and timer group isr both have the hardcoded TIMER_GROUP_0, if you find out using TIMER_GROUP_1 instead is working then I'll add a setter to the library.

Another important detail is the version of arduino-core (2.0.0 recommended), if for some reason you're with an older version, this library may produce a different (sluggish) behaviour.

#if defined ESP_IDF_VERSION_MAJOR && ESP_IDF_VERSION_MAJOR >= 4
  #define USE_NATIVE_GROUP_TIMERS
#else
  #warning "Using software group timers"
#endif
ugurbor commented 3 years ago

I have made it worked in a hacky way. I tried changing to TIMER_GROUP_1 but it was still crashing.

I have finally made it working by just replacing ESP_INTR_FLAG_IRAM with NULL (maybe it should be nullptr but anyway) on this line: https://github.com/tobozo/ESP32-USB-Soft-Host/blob/main/src/ESP32-USBSoftHost.hpp#L168

USH should be initialized before BleKeyboard and I only tested with #define USE_NIMBLE

Sad part was it worked with a cheap logitech office keyboard but not with my mechanical one. Probably because it's fullspeed for some reason. Same with my Logitech mouse, it doesn't work.

tobozo commented 1 year ago

bump

I found @martibn's annotated commit while browsing forks

I've added USH.setISRAllocFlag(int intr_alloc_flags) to the main class, it should be called before USH.init().

The compiler complains when I use USH.setISRAllocFlag(NULL) though, and I can't verify if USH.setISRAllocFlag(0) produces the expected behaviour since I don't have an easily reproductible example for that.

@ugurbor can you confirm setting 0 or NULL instead of ESP_INTR_FLAG_IRAM produces satisfying results when using BT?