ESP32 reboots whenever a HID report is sent
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLEHIDDevice.h>
#include <HIDTypes.h>
#include <HIDKeyboardTypes.h>
// 定义触摸屏HID报告描述符
static const uint8_t touchHidReportDescriptor[] = {
0x05, 0x0D, // Usage Page (Digitizers)
0x09, 0x04, // Usage (Touch Screen)
0xA1, 0x01, // Collection (Application)
0x85, 0x01, // Report ID (1)
0x09, 0x22, // Usage (Finger)
0xA1, 0x02, // Collection (Logical)
0x09, 0x42, // Usage (Tip Switch)
0x15, 0x00, // Logical Minimum (0)
0x25, 0x01, // Logical Maximum (1)
0x75, 0x01, // Report Size (1)
0x95, 0x01, // Report Count (1)
0x81, 0x02, // Input (Data, Variable, Absolute)
0x95, 0x07, // Report Count (7)
0x81, 0x03, // Input (Constant)
0x09, 0x30, // Usage (X)
0x09, 0x31, // Usage (Y)
0x16, 0x00, 0x00, // Logical Minimum (0)
0x26, 0xFF, 0x7F, // Logical Maximum (32767)
0x75, 0x10, // Report Size (16)
0x95, 0x02, // Report Count (2)
0x81, 0x02, // Input (Data, Variable, Absolute)
0xC0, // End Collection
0xC0 // End Collection
};
BLEHIDDevice* hid;
BLECharacteristic* input;
bool deviceConnected = false;
class MyServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
Serial.println("设备已连接");
}
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
Serial.println("设备已断开连接");
BLEDevice::startAdvertising();
Serial.println("重新开始蓝牙广告");
}
};
void setup() {
Serial.begin(115200);
Serial.println("初始化蓝牙设备...");
// 初始化蓝牙
BLEDevice::init("ESP32 Touch");
// 创建蓝牙服务器
BLEServer* pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// 创建HID设备
hid = new BLEHIDDevice(pServer);
if (hid == nullptr) {
Serial.println("创建HID设备失败");
return;
}
// 设置HID报告描述符
hid->reportMap((uint8_t*)touchHidReportDescriptor, sizeof(touchHidReportDescriptor));
hid->startServices();
// 设置输入特性
input = hid->inputReport(1); // 报告ID 1
if (input == nullptr) {
Serial.println("创建输入特性失败");
return;
}
// 启用安全性(配对功能)
BLESecurity* pSecurity = new BLESecurity();
pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND);
// 开始蓝牙广告
BLEAdvertising* pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(hid->hidService()->getUUID());
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0);
BLEDevice::startAdvertising();
Serial.println("蓝牙设备已启动,等待连接...");
}
void loop() {
if (deviceConnected) {
delay(3000); // 延迟3秒
// 模拟发送触摸坐标 (500, 1000)
uint8_t touchData[] = {0x01, 0x01, 0xF4, 0x01, 0xE8, 0x03}; // Report ID, Tip Switch, X, Y
Serial.print("正在发送触摸数据: ");
for (int i = 0; i < sizeof(touchData); i++) {
Serial.printf("%02X ", touchData[i]);
}
Serial.println();
Serial.printf("触摸数据大小: %d\n", sizeof(touchData));
input->setValue(touchData, sizeof(touchData));
input->notify();
delay(100); // 延迟100毫秒
// 模拟触摸结束
uint8_t releaseData[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00};
Serial.print("正在发送释放数据: ");
for (int i = 0; i < sizeof(releaseData); i++) {
Serial.printf("%02X ", releaseData[i]);
}
Serial.println();
Serial.printf("释放数据大小: %d\n", sizeof(releaseData));
input->setValue(releaseData, sizeof(releaseData));
input->notify();
delay(3000); // 延迟3秒
}
}
初始化蓝牙设备...
蓝牙设备已启动,等待连接...
设备已连接
正在发送触摸数据: 01 01 F4 01 E8 03
触摸数据大小: 6
Guru Meditation Error: Core 0 panic'ed (Load access fault). Exception was unhandled.
Core 0 register dump:
MEPC : 0x42007572 RA : 0x42002170 SP : 0x3fca0940 GP : 0x3fc91600
TP : 0x3fc76f9c T0 : 0x00000000 T1 : 0x3fca056c T2 : 0x00400020
S0/FP : 0x3fc93000 S1 : 0x3fc95000 A0 : 0xb459aece A1 : 0x00000001
A2 : 0x00000000 A3 : 0x00000004 A4 : 0x00000000 A5 : 0x3fc95000
A6 : 0x00000007 A7 : 0x00000003 S2 : 0x3fcb4164 S3 : 0x00000000
S4 : 0x00000001 S5 : 0x00000000 S6 : 0x00000000 S7 : 0x00000000
S8 : 0x00000000 S9 : 0x00000000 S10 : 0x00000000 S11 : 0x00000000
T3 : 0x00000000 T4 : 0x2f3b3eec T5 : 0x448ea20c T6 : 0x00000000
MSTATUS : 0x00001881 MTVEC : 0x40380001 MCAUSE : 0x00000005 MTVAL : 0xb459af0e
MHARTID : 0x00000000
Stack memory:
3fca0940: 0x3fca09d0 0x3fcb4164 0x00000000 0x3fcb4244 0x3fca09d0 0x3fcb4164 0x3fcb4244 0x42008a70
3fca0960: 0x00000001 0x3fca099c 0x3fcb425c 0x00000000 0x00000000 0x00000000 0x05000006 0x3e0787b3
3fca0980: 0x3fca09d0 0x3fcb4164 0x00000006 0x420019ca 0x00000000 0x3fc93618 0x3c0c309c 0x3e0787b3
3fca09a0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca09c0: 0x00000000 0x3fc95000 0x3fc93000 0x4200032c 0x01f40101 0x000003e8 0x00000000 0x4200bac0
3fca09e0: 0x00000000 0x00000000 0x00000000 0x4200bb5c 0x00000000 0x00000000 0x00000000 0x4038c772
3fca0a00: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca0a20: 0x00000000 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xbaad5678 0x00000160 0xabba1234
3fca0a40: 0x00000154 0x3fca0910 0x0000d07a 0x3fc93b80 0x3fc93b80 0x3fca0a44 0x3fc93b78 0x00000018
3fca0a60: 0x53831c28 0xddddda63 0x3fca0a44 0x00000000 0x00000001 0x3fc9ea34 0x706f6f6c 0x6b736154
3fca0a80: 0x4ce81d00 0x00391404 0x00000000 0x3fca0a30 0x00000001 0x00000000 0x00000000 0x00000000
3fca0aa0: 0x00000000 0x3fc9a750 0x3fc9a7b8 0x3fc9a820 0x00000000 0x00000000 0x00000001 0x00000000
3fca0ac0: 0x00000000 0x00000000 0x420a6f0e 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca0ae0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca0b00: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca0b20: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca0b40: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca0b60: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca0b80: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x02000000 0xbaad5678 0x00000060
3fca0ba0: 0xabba1234 0x00000054 0x00000000 0x3fca0ba8 0x00000000 0x00000000 0x00000000 0x3fca0bc0
3fca0bc0: 0xffffffff 0x3fca0bc0 0x3fca0bc0 0x00000000 0x3fca0bd4 0xffffffff 0x3fca0bd4 0x3fca0bd4
3fca0be0: 0x00000001 0x00000001 0x00000000 0x5800ffff 0x00000000 0xb33fffff 0x00000000 0xbaad5678
3fca0c00: 0x00000160 0xabba1234 0x00000154 0x3fca0c60 0x3fca0c60 0x3fca0d60 0x3fca0d5f 0x00000000
3fca0c20: 0x3fca0c24 0xffffffff 0x3fca0c24 0x3fca0c24 0x00000000 0x3fca0c38 0xffffffff 0x3fca0c38
3fca0c40: 0x3fca0c38 0x00000000 0x00000100 0x00000001 0x2b00ffff 0x00000000 0xb33fffff 0x00000000
3fca0c60: 0xaa828077 0xbc07a5d9 0xb8b7d9d7 0xe2985e07 0x261ebe25 0xa0f6f573 0xb0887616 0x23d3e93f
3fca0c80: 0xd9be0c83 0x2f555d23 0x831c3c55 0x8bb9daf2 0x32cc94f7 0xc391e791 0x25e7d9a3 0xe5b469f2
3fca0ca0: 0x6c17acc9 0x3ea5363f 0x81a4fdbf 0x499ab929 0x1c557965 0x192a5624 0x739d12ba 0x8bcfb7f4
3fca0cc0: 0x38f2007b 0x93058e72 0x2e43f827 0x316b4bf0 0xc4b5b24e 0x9b710a27 0xf818bfcd 0xe7bf5d96
3fca0ce0: 0x28ee1b9a 0xfb7c51b1 0x8206bc4c 0x0d00c2fc 0x6cc99871 0xfe39e579 0x5351f4aa 0x17942c0c
3fca0d00: 0xc4ac7474 0xd2f9dded 0x4b4febdb 0x862fce7f 0x40bc7fad 0x2925b64e 0x2b87691a 0xbe530fc4
3fca0d20: 0xfac5df49 0xfa043fd8 0x75c2e4fd 0x6102a5bd 0xe78fa4f6 0x92f3d2a8 0xa846f442 0x946bbf07
LF file SHA256: 6d9582e218df8757
Rebooting...
ESP-ROM:esp32c3-api1-20210207
Build:Feb 7 2021
rst:0x3 (RTC_SW_SYS_RST),boot:0xd (SPI_FAST_FLASH_BOOT)
Saved PC:0x40048b82
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fcd5820,len:0x458
load:0x403cc710,len:0x814
load:0x403ce710,len:0x2880
entry 0x403cc710