espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
12.95k stars 7.28k forks source link

Guru meditation error is caused by BLE writing into characteristic multiple times. ESP32-S3 #7254

Open guille-gv opened 1 year ago

guille-gv commented 1 year ago

Board

ESP32-S3-WROOM-1 module

Device Description

Custom board with ESP32-S3-WROOM-1 module

Hardware Configuration

I have a custom board, making use of:

  1. ESP32-S3-WROOM-1 module
  2. Inertial measurement unit connected to:
    • GPIO 47 for I2c SDA
    • GPIO 48 for I2c SCL
    • GPIO 21 to supply power to the sensor
  3. Led connected to GPIO 14.
  4. Button connected to GPIO 13.

Version

v2.0.4

IDE Name

Arduino IDE

Operating System

Windows 10

Flash frequency

QIO 80MHz

PSRAM enabled

yes

Upload speed

921600

Description

When trying to write into a BLE characteristic multiple times with the BLE_write example of ESP32 BLE Arduino, the module suddenly restarts.

Log console shows a guru meditation error.

Sketch

/*
    Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleWrite.cpp
    Ported to Arduino ESP32 by Evandro Copercini
*/

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

// BLE DEVICE Serial Number
const char BLEname[]= "MyESP32";                    // RECOMMEND: BLEname[]=SN=AccessPointName

class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic *pCharacteristic) {
      std::string value = pCharacteristic->getValue();

      if (value.length() > 0) {
        Serial.println("*********");
        Serial.print("New value: ");
        for (int i = 0; i < value.length(); i++)
          Serial.print(value[i]);

        Serial.println();
        Serial.println("*********");
      }
    }
};

void setup() {
  Serial.begin(115200);

  Serial.println("1- Download and install an BLE scanner app in your phone");
  Serial.println("2- Scan for BLE devices in the app");
  Serial.println("3- Connect to MyESP32");
  Serial.println("4- Go to CUSTOM CHARACTERISTIC in CUSTOM SERVICE and write something");
  Serial.println("5- See the magic =)");

  BLEDevice::init(BLEname);
  BLEServer *pServer = BLEDevice::createServer();

  BLEService *pService = pServer->createService(SERVICE_UUID);

  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setCallbacks(new MyCallbacks());

  pCharacteristic->setValue("Hello World");
  pService->start();

  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->start();
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
}

Debug Message

[ 70320][V][BLECharacteristic.cpp:465] handleGATTServerEvent(): << handleGATTServerEvent
[ 70321][V][BLEServer.cpp:280] handleGATTServerEvent(): << handleGATTServerEvent
[ 70329][D][BLEDevice.cpp:102] gattServerEventHandler(): gattServerEventHandler [esp_gatt_if: 3] ... ESP_GATTS_RESPONSE_EVT
[ 70339][V][BLEUtils.cpp:1530] dumpGattServerEvent(): GATT ServerEvent: ESP_GATTS_RESPONSE_EVT
[ 70347][V][BLEUtils.cpp:1666] dumpGattServerEvent(): [status: ESP_GATT_OK, handle: 0x2a]
[ 70355][V][BLEServer.cpp:144] handleGATTServerEvent(): >> handleGATTServerEvent: ESP_GATTS_RESPONSE_EVT
Guru Meditation Error: Core  0 panic'ed (Unhandled debug exception). 
Debug exception reason: Stack canary watchpoint triggered (BTC_TASK) 
Core  0 register dump:
PC      : 0x4037d600  PS      : 0x00060936  A0      : 0x403780f9  A1      : 0x3fcec1a0  
A2      : 0x3fc9e914  A3      : 0x3c0a0e40  A4      : 0x0000001e  A5      : 0x00000000  
A6      : 0x00000056  A7      : 0x00000054  A8      : 0x00000005  A9      : 0x3fc9e918  
A10     : 0x0000003e  A11     : 0x3fc9e8d0  A12     : 0x00000001  A13     : 0x00060523  
A14     : 0x00060520  A15     : 0x00000001  SAR     : 0x00000004  EXCCAUSE: 0x00000001  
EXCVADDR: 0x00000000  LBEG    : 0x400556d5  LEND    : 0x400556e5  LCOUNT  : 0xfffffffa  

Backtrace:0x4037d5fd:0x3fcec1a00x403780f6:0x3fcec280 0x42083c4e:0x3fcec2b0 0x420897c2:0x3fcec5c0 0x420897fe:0x3fcec650 0x4200e102:0x3fcec690 0x420050fd:0x3fcec6f0 0x42005a6d:0x3fcec9b0 0x42009b31:0x3fcec9d0 0x4200a561:0x3fceca30 0x42009512:0x3fceca50 0x42006a65:0x3fcecab0 0x4201e956:0x3fcecb10 0x4201eee9:0x3fcecb30 0x4203f0e5:0x3fcece30 0x420431e7:0x3fcece50 

ELF file SHA256: 0000000000000000

Rebooting...

Other Steps to Reproduce

I also tried in a M1 mac with the same outcome.

I have checked existing issues, online documentation and the Troubleshooting Guide

WeijenH commented 9 months ago

have the same issue here. could not understand why cause this issue.

WeijenH commented 9 months ago

Hi what do you mean caused by BLE writing into characteristic multiple times?

lbernstone commented 9 months ago

You are blowing out the stack somewhere (you haven't decoded the backtrace, so I can't know where). Try calling SET_LOOP_TASK_STACK_SIZE at the top of your program first. If that doesn't work, the place to set the stack size for BLE is at https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/ArduinoStackSize/ArduinoStackSize.ino#L20. It seems like there ought to be a way to set that programmatically rather than editing the source code, but I am not a BLE expert.