BTC_TASK Stack overflow

This is my code

#include <BLEDevice.h> 
#include <BLEUtils.h> 
#include <BLEClient.h> 
#include <SD.h>

#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI();
 
// display変数 
#define LGFX_USE_V1 
#include <LovyanGFX.hpp> 
 
struct Touch_XiaoRound : public lgfx::v1::ITouch { 
  Touch_XiaoRound() { 
    _cfg.x_min = _cfg.y_min = 0; 
    _cfg.x_max = _cfg.y_max = 239; 
    _cfg.i2c_addr = 0x2e; 
  } 
  bool init() override { 
    if (isSPI()) { 
      return false; 
    } 
    if (_cfg.pin_int >= 0) { 
      lgfx::pinMode(_cfg.pin_int, lgfx::v1::pin_mode_t::input_pullup); 
    } 
    return lgfx::i2c::init(_cfg.i2c_port, _cfg.pin_sda, _cfg.pin_scl).has_value(); 
  } 
  void wakeup() override {} 
  void sleep() override {} 
 
  uint_fast8_t getTouchRaw(lgfx::v1::touch_point_t *tp, uint_fast8_t count) override { 
    tp[0].size = 0; 
    tp[0].id = 0; 
    if (_cfg.pin_int < 0) { 
      return 0; 
    } 
    if ((bool)lgfx::gpio_in(_cfg.pin_int)) { 
      ::delay(10); 
      if ((bool)lgfx::gpio_in(_cfg.pin_int)) { 
        return 0; 
      } 
    } 
    uint8_t buf[5]; 
    if (!lgfx::i2c::transactionRead(_cfg.i2c_port, _cfg.i2c_addr, buf, 5, _cfg.freq).has_value()) { 
      return 0; 
    } 
    if (buf[0] != 1) { 
      return 0; 
    } 
    tp[0].x = buf[2]; 
    tp[0].y = buf[4]; 
    tp[0].size = 1; 
    return 1; 
  } 
}; 
 
class XiaoRoundDisplay : public lgfx::LGFX_Device { 
  lgfx::Panel_GC9A01 _panel; 
  lgfx::Bus_SPI _bus; 
  lgfx::Light_PWM _light; 
  Touch_XiaoRound _touch; 
 
 public: 
  XiaoRoundDisplay() { 
    auto bus_cfg = _bus.config(); 
    bus_cfg.spi_host = SPI2_HOST; // for XIAO RP2040     
    bus_cfg.spi_mode = 0; 
    bus_cfg.freq_write = 40000000; 
    bus_cfg.freq_read  = 20000000; 
    bus_cfg.pin_sclk = D8; // for XIAO RP2040 
    bus_cfg.pin_mosi = D10; // for XIAO RP2040 
    bus_cfg.pin_miso = D9; // for XIAO RP2040 
    bus_cfg.pin_dc   = D3; // for XIAO RP2040 
    _bus.config(bus_cfg); 
    _panel.setBus(&_bus); 
 
    auto panel_cfg = _panel.config(); 
    panel_cfg.pin_cs = D1; // for XIAO RP2040 
    panel_cfg.pin_rst = -1; 
    panel_cfg.pin_busy = -1; 
    panel_cfg.memory_width = 240; 
    panel_cfg.memory_height = 240; 
    panel_cfg.panel_width = 240; 
    panel_cfg.panel_height = 240; 
    panel_cfg.offset_x = 0; 
    panel_cfg.offset_y = 0; 
    panel_cfg.offset_rotation = 0; 
    panel_cfg.dummy_read_pixel = 8; 
    panel_cfg.dummy_read_bits  = 1; 
    panel_cfg.readable = false; 
    panel_cfg.invert = true; 
    panel_cfg.rgb_order = false; 
    panel_cfg.dlen_16bit = false; 
    panel_cfg.bus_shared =  true; 
    _panel.config(panel_cfg); 
 
    auto light_cfg = _light.config(); 
    light_cfg.pin_bl = D6; // for XIAO RP2040 
    light_cfg.invert = false; 
    light_cfg.freq = 44100; 
    light_cfg.pwm_channel = 7; 
    _light.config(light_cfg); 
    _panel.setLight(&_light); 
 
    auto touch_cfg = _touch.config();  
    touch_cfg.pin_int = D7; // for XIAO RP2040 
    touch_cfg.i2c_port = 1; // for XIAO RP2040 
    touch_cfg.pin_sda  = D4; // for XIAO RP2040 
    touch_cfg.pin_scl  = D5; // for XIAO RP2040 
    touch_cfg.freq = 400000; 
 
    _touch.config(touch_cfg); 
    _panel.setTouch(&_touch); 
 
    setPanel(&_panel); 
  } 
} display; 
 
LGFX_Sprite record(&display); 
 
void write() { 
  display.startWrite(); 
  record.pushSprite(0,0); 
  display.endWrite(); 
  display.waitDisplay(); 
} 
 
// Manufacturer Dataのマッチングするデータ 
const uint8_t targetManufacturerData[] = {0xFF, 0xFF, 0x01, 0x02, 0x03, 0x04}; 
const size_t manufacturerDataLength = sizeof(targetManufacturerData); 
 
// BLEスキャンの時間 
const int scanTime = 5; // スキャン時間(秒) 
 
// 画像タイルの位置を記録する変数 
int lastTx = -1; 
int lastTy = -1; 
 
// BLEコールバッククラス 
class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks { 
  void onResult(BLEAdvertisedDevice advertisedDevice) { 
    if (advertisedDevice.haveManufacturerData()) { 
      std::string manufacturerData = advertisedDevice.getManufacturerData(); 
 
      if (manufacturerData.length() == manufacturerDataLength) { 
        bool match = true; 
        for (int i = 0; i < manufacturerDataLength; i++) { 
          if ((uint8_t)manufacturerData[i] != targetManufacturerData[i]) { 
            match = false; 
            break; 
          } 
        } 
 
        if (match) { 
          Serial.println("Target Manufacturer Data found!"); 
          uint8_t* payload = advertisedDevice.getPayload(); 
          int payloadLength = advertisedDevice.getPayloadLength(); 
 
          String Blat = ""; 
          String Blon = ""; 
 
          for (int i = 0; i < payloadLength; i++) { 
            if (i >= 13 && i <= 16) { 
              Blat += String(payload[i], HEX); 
            } 
            if (i >= 17 && i <= 20) { 
              Blon += String(payload[i], HEX); 
            } 
          } 
 
          unsigned long tlat = strtoul(Blat.c_str(), nullptr, 16); 
          unsigned long tlon = strtoul(Blon.c_str(), nullptr, 16); 
 
          float la = tlat / 1000000.0; 
          float ln = tlon / 1000000.0; 
          Serial.printf("lat: %.6f, lon: %.6f\n", la, ln); 
 
          int z = 16; 
          double L = 85.05112878; 
          double px = int(pow(2.0, z + 7.0) * ((ln / 180.0) + 1.0)); 
          double py = int(pow(2.0, z + 7.0) * (-1 * atanh(sin(PI * la / 180.0)) + atanh(sin(PI * L / 180.0))) / PI); 
          int tx = px / 256; 
          int ty = py / 256; 
          Serial.printf("tx: %d, ty: %d\n", tx, ty); 
 
          int x = int(px) % 256; 
          int y = int(py) % 256; 
          Serial.printf("x: %d, y: %d\n", x, y); 

          int sx = 120 - x;
          int sy = 120 - y;
 
          // 新しいタイルの位置が最後の位置と異なる場合のみ描画 
          if (tx != lastTx || ty != lastTy) { 
            lastTx = tx; 
            lastTy = ty; 
 
            String filename[9]; 
            filename[0] = String("/images/std/" + String(z) + "/" + String(tx - 1) + "/" + String(ty - 1) + ".png"); 
            filename[1] = String("/images/std/" + String(z) + "/" + String(tx) + "/" + String(ty - 1) + ".png"); 
            filename[2] = String("/images/std/" + String(z) + "/" + String(tx + 1) + "/" + String(ty - 1) + ".png"); 
            filename[3] = String("/images/std/" + String(z) + "/" + String(tx - 1) + "/" + String(ty) + ".png"); 
            filename[4] = String("/images/std/" + String(z) + "/" + String(tx) + "/" + String(ty) + ".png"); 
            filename[5] = String("/images/std/" + String(z) + "/" + String(tx + 1) + "/" + String(ty) + ".png"); 
            filename[6] = String("/images/std/" + String(z) + "/" + String(tx - 1) + "/" + String(ty + 1) + ".png"); 
            filename[7] = String("/images/std/" + String(z) + "/" + String(tx) + "/" + String(ty + 1) + ".png"); 
            filename[8] = String("/images/std/" + String(z) + "/" + String(tx + 1) + "/" + String(ty + 1) + ".png"); 
 
            for (int i = 0; i < 9; i++) { 
              Serial.printf("%s\n", filename[i].c_str()); 
            } 

            record.fillSprite(TFT_WHITE);
            if ((120 <= x && x <= 136) && (120 <= y && y <= 136)) {
              File file = SD.open(filename[4].c_str()); // 画像をSDカードから読み込む 
              if (file) { 
                record.drawPng(&file, sx, sy); // SDカードからPNG画像を描画 
                file.close(); // ファイルを閉じる 
                write(); // 画面に表示 
                Serial.println("Update success");
              } else { 
                Serial.println("File not found!"); 
              }
            } 
            if ((0 <= x && x <= 120) && (120 <= y && y <= 136)) {
              File file1 = SD.open(filename[4].c_str()); // 画像をSDカードから読み込む 
              File file2 = SD.open(filename[3].c_str());
              if (file1) { 
                record.drawPng(&file1, sx, sy); // SDカードからPNG画像を描画 
                record.drawPng(&file2, sx - 256, sy);
                file1.close(); // ファイルを閉じる
                file2.close(); 
                write(); // 画面に表示 
                Serial.println("Update success");
              } else { 
                Serial.println("File not found!"); 
              }
            }
            if ((120 <= x && x <= 136) && (0 <= y && y <= 120)) {
              File file1 = SD.open(filename[4].c_str()); // 画像をSDカードから読み込む 
              File file2 = SD.open(filename[1].c_str());
              if (file1) { 
                record.drawPng(&file1, sx, sy); // SDカードからPNG画像を描画 
                record.drawPng(&file2, sx, sy - 256);
                file1.close(); // ファイルを閉じる
                file2.close(); 
                write(); // 画面に表示 
                Serial.println("Update success");
              } else { 
                Serial.println("File not found!"); 
              }
            }
            if ((136 <= x && x <= 256) && (120 <= y && y <= 136)) {
              File file1 = SD.open(filename[4].c_str()); // 画像をSDカードから読み込む 
              File file2 = SD.open(filename[5].c_str());
              if (file1) { 
                record.drawPng(&file1, sx, sy); // SDカードからPNG画像を描画 
                record.drawPng(&file2, sx + 256, sy);
                file1.close(); // ファイルを閉じる
                file2.close(); 
                write(); // 画面に表示 
                Serial.println("Update success");
              } else { 
                Serial.println("File not found!"); 
              }
            }
            if ((120 <= x && x <= 136) && (136 <= y && y <= 256)) {
              File file1 = SD.open(filename[4].c_str()); // 画像をSDカードから読み込む 
              File file2 = SD.open(filename[7].c_str());
              if (file1) { 
                record.drawPng(&file1, sx, sy); // SDカードからPNG画像を描画 
                record.drawPng(&file2, sx, sy + 256);
                file1.close(); // ファイルを閉じる
                file2.close(); 
                write(); // 画面に表示 
                Serial.println("Update success");
              } else { 
                Serial.println("File not found!"); 
              }
            }
            if ((0 <= x && x <= 120) && (0 <= y && y <= 120)) {
              File file1 = SD.open(filename[4].c_str()); // 画像をSDカードから読み込む 
              File file2 = SD.open(filename[0].c_str());
              File file3 = SD.open(filename[1].c_str());
              File file4 = SD.open(filename[3].c_str());
              if (file1) { 
                record.drawPng(&file1, sx, sy); // SDカードからPNG画像を描画 
                record.drawPng(&file2, sx - 256, sy - 256);
                record.drawPng(&file3, sx, sy - 256);
                record.drawPng(&file4, sx - 256, sy);
                file1.close(); // ファイルを閉じる
                file2.close(); 
                file3.close();
                file4.close();
                write(); // 画面に表示 
                Serial.println("Update success");
              } else { 
                Serial.println("File not found!"); 
              }
            }
            if ((136 <= x && x <= 256) && (0 <= y && y <= 120)) {
              File file1 = SD.open(filename[4].c_str()); // 画像をSDカードから読み込む 
              File file2 = SD.open(filename[1].c_str());
              File file3 = SD.open(filename[2].c_str());
              File file4 = SD.open(filename[5].c_str());
              if (file1) { 
                record.drawPng(&file1, sx, sy); // SDカードからPNG画像を描画 
                record.drawPng(&file2, sx, sy - 256);
                record.drawPng(&file3, sx + 256, sy - 256);
                record.drawPng(&file4, sx + 256, sy);
                file1.close(); // ファイルを閉じる
                file2.close(); 
                file3.close();
                file4.close(); 
                write(); // 画面に表示 
                Serial.println("Update success");
              } else { 
                Serial.println("File not found!"); 
              }
            }
            if ((0 <= x && x <= 120) && (136 <= y && y <= 256)) {
              File file1 = SD.open(filename[4].c_str()); // 画像をSDカードから読み込む 
              File file2 = SD.open(filename[3].c_str());
              File file3 = SD.open(filename[6].c_str());
              File file4 = SD.open(filename[7].c_str());
              if (file1) { 
                record.drawPng(&file1, sx, sy); // SDカードからPNG画像を描画 
                record.drawPng(&file2, sx - 256, sy);
                record.drawPng(&file3, sx - 256, sy + 256);
                record.drawPng(&file4, sx, sy + 256);
                file1.close(); // ファイルを閉じる
                file2.close(); 
                file3.close();
                file4.close();
                write(); // 画面に表示 
                Serial.println("Update success");
              } else { 
                Serial.println("File not found!"); 
              }
            }
            if ((136 <= x && x <= 256) && (136 <= y && y <= 256)) {
              File file1 = SD.open(filename[4].c_str()); // 画像をSDカードから読み込む 
              File file2 = SD.open(filename[5].c_str());
              File file3 = SD.open(filename[7].c_str());
              File file4 = SD.open(filename[8].c_str());
              if (file1) { 
                record.drawPng(&file1, sx, sy); // SDカードからPNG画像を描画 
                record.drawPng(&file2, sx + 256, sy);
                record.drawPng(&file3, sx, sy + 256);
                record.drawPng(&file4, sx + 256, sy + 256);
                file1.close(); // ファイルを閉じる
                file2.close(); 
                file3.close();
                file4.close(); 
                write(); // 画面に表示 
                Serial.println("Update success");
              } else { 
                Serial.println("File not found!"); 
              }
            }
          } else {
            record.pushSprite(sx, sy);
            write();
            Serial.println("Tile shift");
            Serial.printf("sx: %d, sy: %d\n", sx, sy);
          }
        } 
      } 
    } 
  }; 
}; 
 
void setup() {
  Serial.begin(115200);
  tft.init();

  bool cardMounted = false;

  pinMode(3, OUTPUT);

  while (!cardMounted) {
    if (SD.begin(3)) {
      uint8_t cardType = SD.cardType();
      if (cardType != CARD_NONE) {
        cardMounted = true;
        Serial.println("SD Card Mounted Successfully");
      } else {
        Serial.println("No SD card attached");
      }
    } else {
      Serial.println("Card Mount Failed");
      Serial.println("Retrying to mount SD card...");
      delay(1000);
    }
  }

  // ディスプレイとBLEの初期化
  display.init();
  display.setRotation(0);
  record.createSprite(240, 240);

  BLEDevice::init("");
  BLEScan *pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setInterval(100);
  pBLEScan->setWindow(99);
  pBLEScan->setActiveScan(true);
  pBLEScan->start(scanTime, false);
}


 
void loop() {
  BLEScan *pBLEScan = BLEDevice::getScan();
  pBLEScan->start(scanTime, false);
  delay(10000); 
}

What causes this error?

ELF file SHA256: e2e048b7bca733b1

Rebooting...
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0xc (RTC_SW_CPU_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)
Saved PC:0x40376ee8
SPIWP:0xee
mode:DIO, clock div:1
load:0x3fce3808,len:0x44c
load:0x403c9700,len:0xbe4
load:0x403cc700,len:0x2a68
entry 0x403c98d4
SD Card Mounted Successfully
Target Manufacturer Data found!
lat: 35.563354, lon: 139.576233
tx: 58177, ty: 25833
x: 19, y: 9
/images/std/16/58176/25832.png
/images/std/16/58177/25832.png
/images/std/16/58178/25832.png
/images/std/16/58176/25833.png
/images/std/16/58177/25833.png
/images/std/16/58178/25833.png
/images/std/16/58176/25834.png
/images/std/16/58177/25834.png
/images/std/16/58178/25834.png
Guru Meditation Error: Core  0 panic'ed (Unhandled debug exception). 
Debug exception reason: Stack canary watchpoint triggered (BTC_TASK) 
Core  0 register dump:
PC      : 0x40381e37  PS      : 0x00060236  A0      : 0x80380a78  A1      : 0x3fcc74d0  
A2      : 0x3fc98a90  A3      : 0xb33fffff  A4      : 0x0000abab  A5      : 0x00060223  
A6      : 0x00060223  A7      : 0x0000cdcd  A8      : 0xb33fffff  A9      : 0xffffffff  
A10     : 0x00000000  A11     : 0x3fcecf80  A12     : 0x00000000  A13     : 0x00000001  
A14     : 0x02c98a90  A15     : 0x00ffffff  SAR     : 0x0000001d  EXCCAUSE: 0x00000001  
EXCVADDR: 0x00000000  LBEG    : 0x400570e8  LEND    : 0x400570f3  LCOUNT  : 0xffffffff  


Backtrace: 0x40381e34:0x3fcc74d0 0x40380a75:0x3fcc7510 0x4037f1f4:0x3fcc7540 0x4037f1ea:0x01000000 |<-CORRUPTED

Hi there,
Could be a couple things, But first Blush
What BSP are you using? What display are you trying to use with that TFT_eSPI.h
Put some Print initialization ahead or if and see if it gets that far. You’ll see closer where it’s not going.
HTH
GL :slight_smile: PJ :v:

Try a C3 and the BSP 2.11 for ESP’s and TFT_eSPI.h :+1:

UPDATE

I was able to compile and run it on a S3 with BSP 2.0.11
after it resets. It tries and mounts the SD card on the display.
after that it reports it was saucerful and does something image wise?

e[0;32mI (620) esp_image: segment 0: paddr=00010020 vaddr=3c0b00SD Card Mounted Successfully

HTH :v:

1 Like