ESP32 crashes when try to execute code from IRAM

isaque
Posts: 1
Joined: Sat May 25, 2019 5:56 pm

ESP32 crashes when try to execute code from IRAM

Postby isaque » Sun May 26, 2019 5:26 pm

I am trying to find a way to execute code (for exemple, load a code from SD card to RAM) without re-flashing esp32. I know ESP32 can execute instructions from IRAM, but when I try to do it, esp32 crashes.

Code: Select all

int main()
{
    int c = 0;
    for (int i = 0; i < 1000; i ++)
    {
        for (int j = 0; j < 1000; j ++)
        {
            c++;
        }
    }
}
This is the code (the compiled binary) I am trying to execute from IRAM. I am compiling it with the following linker script:

Code: Select all

SECTIONS
{
	.literal : {*(.literal)}
	.text : {*(.text)}
}
I compile the code using

Code: Select all

xtensa-esp32-elf-gcc.exe -Xlinker -T link.ld -std=c99 -Os -nostdlib -c main.c -o out/main.o
xtensa-esp32-elf-objcopy -O binary out/main.o out/APP.bin
This is the output binary:

Code: Select all

0x36, 0x41, 0x00, 0x0C, 0x02, 0x1D, 0xF0
The code I am using on ESP32:

Code: Select all

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_attr.h"

#define SIZE 7
IRAM_ATTR uint32_t PROGRAM[SIZE] = {
0x36, 0x41, 0x00, 0x0C, 0x02, 0x1D, 0xF0
};

const void* PTR = (void*)PROGRAM;

void exec_bin()
{
  typedef void func(void);
  func* f = (func*)(PTR);
  printf("\nIT'S NOW ...\n");
  f(); // ESP32 CRASHES HERE <<---

}

void app_main()
{
  printf("\n\nGoint to exec code ...\n\n");
  vTaskDelay(1000 / portTICK_RATE_MS);

  exec_bin();

  printf("Done");
}
I put the compiled binary on IRAM using IRAM_ATTR. This code basically tryies to execute the compiled binary from IRAM, but when I call f() esp32 crashes.

Code: Select all

Guru Meditation Error: Core  0 panic'ed (Unhandled debug exception)
Debug exception reason: BREAK instr
What am I doing wrong?
* Sorry for my english. I speak portuguese. This is the first time I use this forum.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: ESP32 crashes when try to execute code from IRAM

Postby ESP_Angus » Sun May 26, 2019 11:56 pm

Hi isaque,

Your short binary code looks like it should probably run.

- If you capture both the full output of the panic handler (registers) and also print the address of f() before you call it, how does the PC address in the panic handler compare with the address of f()? Is it definitely crashing inside f()?

- This doesn't explain why it's crashing, but it looks like the optimizer has removed all the code in main.c - the disassembly looks like it will be "entry a1, 32; movi a2, 0; retw.n" - ie "return 0". You can run "xtensa-esp32-elf-objdump -S main.o" to compare.

- This also doesn't explain why it's crashing, but FWIW the program in main.o is not linked - main.o is unlinked object code. The "-c" option for gcc means "don't run the linker" so the linker options are being ignored. This probably doesn't matter in this case because the function doesn't link against any other objects, but it will start to matter if you write more complex code.

jcsbanks
Posts: 305
Joined: Tue Mar 28, 2017 8:03 pm

Re: ESP32 crashes when try to execute code from IRAM

Postby jcsbanks » Mon May 27, 2019 8:08 am

Your function pointer might be pointing to literal instead of text? Maybe not since the constant is less than 12 bits and the code is mostly optimised away, but that is all I had to do I think.

Who is online

Users browsing this forum: No registered users and 165 guests