invalid operation Xtensa

Gaston1980
Posts: 41
Joined: Sun Oct 06, 2024 10:26 am

invalid operation Xtensa

Postby Gaston1980 » Fri Jul 04, 2025 7:40 pm

Hi to all, I'm trying to generate a tone to create a wav file using esp-idf 5.4.1 and I tried to use this peace of code:

Code: Select all

const int buffer_size = 8000;
const int sample_rate = 8000;
    for(int i=0;i<buffer_size;i++)
    {
        buff_test[i]=(short int)(cos((2 * M_PI * 256*i)/sample_rate)*1000);
    }
and I also tried creating a lookup table with sin values and storing into a buffer, but in both cases I got this message:
Image
At first I thought it was a library error (float numbers) but why do I got this error with a lookup table? Why Xtensa? I already compile this tone using clang and works.
Thanks
Gastón
Attachments
Captura desde 2025-07-04 16-06-05.png
Captura desde 2025-07-04 16-06-05.png (16.63 KiB) Viewed 262 times

Gaston1980
Posts: 41
Joined: Sun Oct 06, 2024 10:26 am

Re: invalid operation Xtensa

Postby Gaston1980 » Fri Jul 04, 2025 8:29 pm

an update,
If I change the buffer_size value to less than 2000, I got it working. But why? I alreedy disable wdt timer from esp settings.
I'm using esp32 custom board with jtag esp-prog on es-idf 5.4.1.
Now if I use above 8000 of buffer_size I got this(see attachment). Don't know why I got this messages.
Attachments
Captura desde 2025-07-04 17-25-25.png
Captura desde 2025-07-04 17-25-25.png (13.85 KiB) Viewed 259 times

MicroController
Posts: 2672
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: invalid operation Xtensa

Postby MicroController » Fri Jul 04, 2025 10:08 pm

What "messages" are you talking about? I only see useless screenshots of some code snippets with 0 context.
If I change the buffer_size value to less than 2000, I got it working. But why?
Probably because you're corrupting some memory when you write 'too much' to the buffer.

Gaston1980
Posts: 41
Joined: Sun Oct 06, 2024 10:26 am

Re: invalid operation Xtensa

Postby Gaston1980 » Sat Jul 05, 2025 12:01 am

Microcontroller thanks for the reply, those codes is where the code is stopped. The debug halts there. I try to create a tone using the cos function and checking the build info I check that memory usage is correct. I also tried creating a lookup table for the cos function, but again the program halts in one of those two codes I uploaded. I attached the build info. and the tone coded is this:

Code: Select all

const int duration = 2;
uint16_t buff_test[buffer_size];
uint16_t sample_rate = 8000;
int buffer_size = sample_rate*duration;

    for(int i=0;i<buffer_size;i++)
    {
        buff_test[i]=(cos((2 * M_PI * 256*i)/sample_rate)*1000);
    }
How may I be able to create a tone of 2sec duration in the esp32? is there an example code to check? If memory usage is a problem, why a lookup table also have the same issue? I observe that the code is also stopped in those codes using a sin lookup table for 16000 values.
Thanks again for your help
Cheers
Attachments
Captura desde 2025-07-04 20-53-04.png
Captura desde 2025-07-04 20-53-04.png (36.62 KiB) Viewed 246 times

MicroController
Posts: 2672
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: invalid operation Xtensa

Postby MicroController » Sat Jul 05, 2025 12:12 am

Let the code run and check the log output over serial via idf.py monitor. This may give a better hint at what's going wrong.

Gaston1980
Posts: 41
Joined: Sun Oct 06, 2024 10:26 am

Re: invalid operation Xtensa

Postby Gaston1980 » Sat Jul 05, 2025 1:53 am

I tested the code in a different board and got this output from monitor:

Code: Select all

Guru Meditation Error: Core  0 panic'ed (InstrFetchProhibited). Exception was unhandled.

Core  0 register dump:
PC      : 0xfd5dfdfe  PS      : 0x00060031  A0      : 0x8008b012  A1      : 0x3ffb1fb0  
A2      : 0x00000003  A3      : 0x00060023  A4      : 0x00060021  A5      : 0x3ffd349c  
A6      : 0x00000000  A7      : 0x00000000  A8      : 0x800852b4  A9      : 0x3ffc5cb8  
A10     : 0x3ffb28b4  A11     : 0x3ffb28b8  A12     : 0x00000000  A13     : 0x4015dae8  
--- 0x4015dae8: queue_recv_wrapper at /home/gaston/esp/v5.4.1/esp-idf/components/esp_wifi/esp32/esp_adapter.c:319

A14     : 0x00000001  A15     : 0x00000001  SAR     : 0x00000000  EXCCAUSE: 0x00000014  
EXCVADDR: 0xfd5dfdfc  LBEG    : 0x400029ac  LEND    : 0x400029cb  LCOUNT  : 0x00000000  
--- 0x400029ac: __divdf3 in ROM
--- 0x400029cb: __divdf3 in ROM



Backtrace: 0x7d5dfdfb:0x3ffb1fb0 0x4008b00f:0x3ffb1fd0 0x4008ad55:0x3ffb1ff0 0x4008357a:0x3ffb2000 0x40002716:0x3ffc3d40 0x40149467:0x3ffc3d50 0x40148e45:0x3ffc3db0 0x400d8734:0x3ffc3de0 0x400d877b:0x3ffcbb00 0x4015dfeb:0x3ffcbb20 0x4008a879:0x3ffcbb50
--- 0x4008b00f: xPortSysTickHandler at /home/gaston/esp/v5.4.1/esp-idf/components/freertos/port_systick.c:199
--- 0x4008ad55: _frxt_timer_int at /home/gaston/esp/v5.4.1/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/portasm.S:344
--- 0x4008357a: _xt_lowint1 at /home/gaston/esp/v5.4.1/esp-idf/components/xtensa/xtensa_vectors.S:1240
--- 0x40002716: __subdf3 in ROM
--- 0x40149467: __ieee754_rem_pio2 at /builds/idf/crosstool-NG/.build/xtensa-esp-elf/src/newlib/newlib/libm/math/e_rem_pio2.c:158
--- 0x40148e45: cos at /builds/idf/crosstool-NG/.build/xtensa-esp-elf/src/newlib/newlib/libm/math/s_cos.c:71
--- 0x400d8734: construct_wav at /home/gaston/esp32_projects/Mems/main/main.c:197
--- 0x400d877b: app_main at /home/gaston/esp32_projects/Mems/main/main.c:209
--- 0x4015dfeb: main_task at /home/gaston/esp/v5.4.1/esp-idf/components/freertos/app_startup.c:208
--- 0x4008a879: vPortTaskWrapper at /home/gaston/esp/v5.4.1/esp-idf/components/freertos/FreeRTOS-Kernel/portable/xtensa/port.c:139





ELF file SHA256: 9de20521b

Rebooting...
ets Jul 29 2019 12:21:46

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:2
load:0x3fff0030,len:6276
load:0x40078000,len:15748
load:0x40080400,len:4
--- 0x40080400: _init at ??:?

ho 8 tail 4 room 4
load:0x40080404,len:3860
entry 0x4008063c

what this line means:

Code: Select all

--- 0x40148e45: cos at /builds/idf/crosstool-NG/.build/xtensa-esp-elf/src/newlib/newlib/libm/math/s_cos.c:71
could be some problems with the lib using COS function?
Could be that double operation consume resources and the Core0 is halted? If this is the case, how to create a tone of 2sec duration in the esp32?
Regards
Gastón

MicroController
Posts: 2672
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: invalid operation Xtensa

Postby MicroController » Sat Jul 05, 2025 7:36 am

This problem may be partially timing-related, as the actual error happens in the FreeRTOS tick interrupt, which may explain why it seems to work for smaller buffer sizes (=faster).
Still likely some memory corruption going on, maybe some stack-overwriting. Can't tell without seeing the code.

MicroController
Posts: 2672
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: invalid operation Xtensa

Postby MicroController » Sat Jul 05, 2025 11:10 am

This seems to work:

Code: Select all

#include <math.h>

static const float FREQUENCY_HZ = 256.0f;
static const unsigned SAMPLE_RATE_HZ = 8000;
static const unsigned SAMPLE_SCALE = 1000;

static const unsigned DURATION_MS = 2000;
static const unsigned NUM_SAMPLES = (SAMPLE_RATE_HZ * DURATION_MS)/1000;

static int16_t sample_buffer[NUM_SAMPLES];

// ~550ms on a C3 (no FPU!) @ 160MHz:
static void initSampleBuf() {
    const double step = (2*M_PI * FREQUENCY_HZ) / SAMPLE_RATE_HZ;
    /* Making extra sure to use the actual length of the buffer, not a variable
       which "holds the correct value at this point, I think": */
    const unsigned BUFFER_LEN = sizeof(sample_buffer)/sizeof(sample_buffer[0]);
    for(unsigned i = 0; i < BUFFER_LEN; ++i) {
        sample_buffer[i] = cos(i * step) * SAMPLE_SCALE;
    }
}

Gaston1980
Posts: 41
Joined: Sun Oct 06, 2024 10:26 am

Re: invalid operation Xtensa

Postby Gaston1980 » Sat Jul 05, 2025 2:39 pm

thanks Microcontroller for your time and help. That works!! Thanks again!!
Cheers
Gastón

eriksl
Posts: 199
Joined: Thu Dec 14, 2023 3:23 pm
Location: Netherlands

Re: invalid operation Xtensa

Postby eriksl » Mon Jul 21, 2025 11:03 am

Kudo's for the use of stdint types.

Please note that the esp's have a FPU for "float" type operands. If you change all occurrences of double to float and prevent implicit conversions to double, it may go way faster. You're using software fp emulation now.

Who is online

Users browsing this forum: Amazon [Bot], ChatGPT-User, Google [Bot], Semrush [Bot] and 6 guests