Stack smashing protect failure on ESP32

Im having problems using a ESP32 as a data adquisition board.
Im using this ESP32-WROOM-32 on a ESP32 DEVKIT V1 from "doit" to send me an array of 16 characters consisting of the values from a MMA7361 accelerometer, im sensing X and Y axes from both and each one is a 4 digit number, that's why the 16 digits.
Thing is, the watchdog timer or something keeps messing up, rebooting the board after 2 messurements. I allready used Chat GPT to modify the code a bit, changing the timer clock and changing the variables so that they are local instead of global.
I leave the code up next:

#define XA_AXIS A0
#define YA_AXIS A3
#define XB_AXIS A6
#define YB_AXIS A7

hw_timer_t *My_timer = NULL;
int xa_sum = 0, ya_sum = 0, xb_sum = 0, yb_sum = 0;
int counter = 0;
int counter_local = 0;

void IRAM_ATTR onTimer() {
  int xa_sum_local = 0;
  int ya_sum_local = 0;
  int xb_sum_local = 0;
  int yb_sum_local = 0;

  xa_sum_local += analogRead(XA_AXIS);
  ya_sum_local += analogRead(YA_AXIS);
  xb_sum_local += analogRead(XB_AXIS);
  yb_sum_local += analogRead(YB_AXIS);
  counter_local++;

  if (counter_local == 5) {
    int xa_mean = xa_sum_local / 5;
    int ya_mean = ya_sum_local / 5;
    int xb_mean = xb_sum_local / 5;
    int yb_mean = yb_sum_local / 5;

    // Format the mean values with leading zeros and a fixed width of 4
    char buf[17]; // 16 digits plus a null terminator
    sprintf(buf, "%04d%04d%04d%04d\n", xa_mean, ya_mean, xb_mean, yb_mean);

    // Write the string to the serial port
    Serial.write(buf, 16);

    xa_sum_local = ya_sum_local = xb_sum_local = yb_sum_local = 0;
    counter_local = 0;
  }
}

void setup() {
  Serial.begin(115200);
  My_timer = timerBegin(0, 80, true);
  timerAttachInterrupt(My_timer, &onTimer, true);
  timerAlarmWrite(My_timer, 50000, true);
  timerAlarmEnable(My_timer);
}

void loop() {}

And here is what the serial terminal is returning, the tipical error im getting:

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:1184
load:0x40078000,len:13220
ho 0 tail 12 room 4
load:0x40080400,len:3028
entry 0x400805e4
0390034804050343 <------(this is the array i want to get)
Stack smashing protect failure!

abort() was called at PC 0x400da308 on core 1

Backtrace: 0x40083539:0x3ffbf19c |<-CORRUPTED

ELF file SHA256: 672e7622e5a84396

Rebooting...

I dont think is a timer issue, because when i modify the timer to gather data every 1 second, it still shows the same error.

Any help would be apreciatted, im kind of new to the ESP32 but im lead to belive that it's faster and more usefull to what im trying to do here, witch is to get the data from the accelerometers at about 20-50 times a second, the faster the better.

what gpio pin is A6 and A7 on a ESP32?


I thought i was using the ADC to be totaly honest, i used 0,3,6 and 7 just because they are next to one another.

not tested but

#define XA_AXIS A0
#define YA_AXIS A3
#define XB_AXIS A6
#define YB_AXIS A7

EventGroupHandle_t eg;
#define evtTriggered   ( 1 << 0 )
hw_timer_t *My_timer = NULL;
int xa_sum = 0, ya_sum = 0, xb_sum = 0, yb_sum = 0;
int counter = 0;
int counter_local = 0;

void IRAM_ATTR onTimer()
{
  BaseType_t xHigherPriorityTaskWoken;
  xEventGroupSetBitsFromISR(eg, evtTriggered, &xHigherPriorityTaskWoken);
}
////
void setup()
{
  //
  eg = xEventGroupCreate(); // get an event group handle
  //
  Serial.begin(115200);
  My_timer = timerBegin(0, 80, true);
  timerAttachInterrupt(My_timer, &onTimer, true);
  timerAlarmWrite(My_timer, 50000, true);
  timerAlarmEnable(My_timer);
  //
  xTaskCreatePinnedToCore( fTaskTriggered, "fTaskTriggered", 10000,  NULL, 3, NULL, 1 );
}
////
void fTaskTriggered (void * pvParameters)
{
  int xa_sum_local = 0;
  int ya_sum_local = 0;
  int xb_sum_local = 0;
  int yb_sum_local = 0;

  for (;;)
  {
    xEventGroupWaitBits (eg, evtTriggered, pdTRUE, pdTRUE, portMAX_DELAY );
    xa_sum_local += analogRead(XA_AXIS);
    ya_sum_local += analogRead(YA_AXIS);
    xb_sum_local += analogRead(XB_AXIS);
    yb_sum_local += analogRead(YB_AXIS);
    counter_local++;

    if (counter_local == 5) {
      int xa_mean = xa_sum_local / 5;
      int ya_mean = ya_sum_local / 5;
      int xb_mean = xb_sum_local / 5;
      int yb_mean = yb_sum_local / 5;

      // Format the mean values with leading zeros and a fixed width of 4
      char buf[17]; // 16 digits plus a null terminator
      sprintf(buf, "%04d%04d%04d%04d\n", xa_mean, ya_mean, xb_mean, yb_mean);

      // Write the string to the serial port
      Serial.write(buf, 16);

      xa_sum_local = ya_sum_local = xb_sum_local = yb_sum_local = 0;
      counter_local = 0;
    }
  }
} //void fTaskTriggered (void * pvParameters)
////
void loop() {}

Not tested.
is the stack smashing still happening, after using the proper gpio pins?

those are not the ESP32' s Arduino core ADC pins.

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/index.html

  • Strapping pin: GPIO0, GPIO2, GPIO5, GPIO12 (MTDI), and GPIO15 (MTDO) are strapping pins. For more infomation, please refer to ESP32 datasheet.
  • SPI0/1: GPIO6-11 and GPIO16-17 are usually connected to the SPI flash and PSRAM integrated on the module and therefore should not be used for other purposes.
  • JTAG: GPIO12-15 are usually used for inline debug.
  • GPI: GPIO34-39 can only be set as input mode and do not have software-enabled pullup or pulldown functions.
  • TXD & RXD are usually used for flashing and debugging.
  • ADC2: ADC2 pins cannot be used when Wi-Fi is used. So, if you are having trouble getting the value from an ADC2 GPIO while using Wi-Fi, you may consider using an ADC1 GPIO instead, which should solve your problem. For more details, please refer to Hardware Limitations of ADC Continuous Mode and Hardware Limitations of ADC Oneshot Mode.
  • Please do not use the interrupt of GPIO36 and GPIO39 when using ADC or Wi-Fi and Bluetooth with sleep mode enabled. Please refer to ESP32 ECO and Workarounds for Bugs > Section 3.11 for the detailed description of the issue.

Same Error again, im using serial trough USB altough i plan to use it via BT, but havent started that part yet, i wanted to clear the data adq. part first.
I cleared GPIO Pins 36 & 39, im using GPIO 32,33,34 & 35 now (Or A4,A5,A6 & A7 for that matter), checked the new pins for any hardware malfunctions and they are allright.
Im at a loss. Does it make any difference if i declare the pins as "#define XA_AXIS A6" as oposed to declaring it "#define XA_AXIS 34"?
Ty for all the help <3

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.