Page 1 of 1

Issue in task creation and stack overflow

Posted: Fri Oct 29, 2021 9:56 am
by saigajul37

Code: Untitled.txt Select all


ESP32 wrover-b(8mb flash 8mb psram)
Ported code from esp-idf v3.3.3 to v4.3 stable
For FFT using esp-dsp lib
Following are the errors I am facing
1) ***ERROR*** A stack overflow in task procc_data has been detected.
2) Task creation failed due to memory allocation failure, but
I (13860) Start memory size : 4147695
I (13860) Consumed memory size : 16.000000
I (13860) Consumed memory size in percent : 0 %

Code: Untitled.c Select all



#define MAXSIZE (1024*5)
#define SAMPLE_FRQ 1344

#define N_SAMPLES 4096

#define FFT_SAMP (N_SAMPLES)

#define FFT_TEST_COMP_SAMPLES_LEN FFT_SAMP
#define FFT_TEST_OUT_MAG_SAMPLE_LEN (FFT_SAMP/2)


int16_t x_value[MAXSIZE] = {0};
int16_t y_value[MAXSIZE] = {0};
int16_t z_value[MAXSIZE] = {0};

__attribute__((aligned(16)))
float x_1[N_SAMPLES] ;
__attribute__((aligned(16)))
float y_1[N_SAMPLES] ;
__attribute__((aligned(16)))
float z_1[N_SAMPLES];

float xyz_abs [N_SAMPLES]={0};


void runn_fft_on_raw(float *in_buff, float *out_buff)
{
float y_cf[FFT_SAMP*2];

for (int i=0 ; i < FFT_SAMP ; i++) {

y_cf[i*2 + 0] = in_buff[i] *make_wind_sample(i) ;
y_cf[i*2 + 1] = 0.0;

}

dsps_fft2r_fc32(y_cf, FFT_SAMP);
dsps_bit_rev_fc32(y_cf, FFT_SAMP);
dsps_cplx2reC_fc32(y_cf, FFT_SAMP);


for (int i = 0 ; i < FFT_TEST_OUT_MAG_SAMPLE_LEN ; i++) {

out_buff[i] = ( sqrt(((y_cf[i * 2 + 0] * y_cf[i * 2 + 0] + y_cf[i * 2 + 1] * y_cf[i * 2 + 1] )))/(float)FFT_SAMP );
}


}

void procc_data (void *pvParameters) {

int16_t x_value[MAXSIZE] = {0};
int16_t y_value[MAXSIZE] = {0};
int16_t z_value[MAXSIZE] = {0};

while(1) {

//add accl data;
int16_t x_value[MAXSIZE] = add(x);
int16_t y_value[MAXSIZE] = add(y);
int16_t z_value[MAXSIZE] = add(z);
}

}


void procc_data (void *pvParameters)
{

esp_task_wdt_add(NULL);

float x_fft_data[FFT_TEST_OUT_MAG_SAMPLE_LEN] = {0.0};
float y_fft_data[FFT_TEST_OUT_MAG_SAMPLE_LEN] = {0.0};
float z_fft_data[FFT_TEST_OUT_MAG_SAMPLE_LEN] = {0.0};

while(1){


memset(x_value, 0, sizeof(x_value));
memset(y_value, 0, sizeof(y_value));
memset(z_value, 0, sizeof(z_value));

memset(x_fft_data, 0, sizeof(x_fft_data));
memset(y_fft_data, 0, sizeof(y_fft_data));
memset(z_fft_data, 0, sizeof(z_fft_data));

memset(x_1, 0, sizeof(x_1));
memset(y_1, 0, sizeof(y_1));
memset(z_1, 0, sizeof(z_1));


get_sample(x_value, y_value, z_value);
cov_sample_in_flt(x_1, y_1, z_1);

runn_fft_on_raw(x_1, x_fft_data);
runn_fft_on_raw(y_1, y_fft_data);
runn_fft_on_raw(z_1, z_fft_data);

// process data
// post process data to sever
}

}

void creatTask ()
{

isMaster = xTaskCreate(master_Task, "master_Task", 1024*8, NULL, configMAX_PRIORITIES, &master_t);
isLed = xTaskCreate(led_Task, "led_Task", 1024*2, NULL, 20, &led_t);
uint8_t istaskCreated = 0;

while ( iswebsever != pdPASS) { // wait till task is created properly


iswebsever = xTaskCreate(websever, "websever", 1024*8, NULL, 20, &websever_t, CORE_ONE);

if (istaskCreated > 3 && iswebsever != pdPASS ) {

esp_restart();
}

istaskCreated++;

msDelay(1000);
}
istaskCreated = 0;

while ( isadd_raw_data != pdPASS) { // wait till task is created properly

isadd_raw_data = xTaskCreatePinnedToCore(add_raw_data, "add_raw_data", 1024*2, NULL, 23, &add_raw_data_t, CORE_ONE);

if (istaskCreated > 3 && isadd_raw_data != pdPASS ) {

esp_restart();
}

istaskCreated++;

msDelay(1000);
}



istaskCreated = 0;

while ( isprocc_data != pdPASS) { // wait till task is created properly


isprocc_data = xTaskCreatePinnedToCore(procc_data, "procc_data", 1024*25, NULL, 22, &procc_data_t, CORE_ONE);

if (istaskCreated > 3 && isprocc_data != pdPASS ) {

esp_restart();
}

istaskCreated++;

msDelay(1000);
}

istaskCreated = 0;



}




void app_main(void)
{
esp_err_t err = ESP_FAIL;

err = nvs_flash_init();

if (err == ESP_ERR_NVS_NO_FREE_PAGES) {

ESP_ERROR_CHECK(nvs_flash_erase());
ESP_ERROR_CHECK( nvs_flash_init());
}

init_accel();

ret = dsps_fft2r_init_fc32( NULL, 4096 );

if (ret != ESP_OK)
{
printf( "Not possible to initialize FFT. Error = %i\n", ret);
return;
}

creatTask();

}

Re: Issue in task creation and stack overflow

Posted: Fri Oct 29, 2021 10:50 am
by chegewara
You are creating 6 local variables (arrays) with 5kB in size each allocated from stack. Task stack is 25kB. Do the math.

Re: Issue in task creation and stack overflow

Posted: Fri Oct 29, 2021 11:33 am
by saigajul37
Yes, I got that already, still thank you for highlighting and replying. please follow below points I have summed up so far.
1) so when I increase the stack size as required for the task, memory allocation to task fails even though I can see I have 4MB memory available.
2) If I allocate some of all those 6 variables global, I get a data segment overflow error by 13000 bytes(sometimes less) while compiling the code, thus compilation fails with exit code 1
3) If I partly allocate some variable in global and reset local, stack overflows while calculating FFT

Unless and until I do not set N_SAMPLE to 3072, FFT_SAMPLE to 1024, and MAXSIZE to 4096, the code doesn't work. :geek:

Re: Issue in task creation and stack overflow

Posted: Fri Oct 29, 2021 11:51 am
by chegewara
1. 4MB ram is a bit confusing, because you still have only <400kB internal RAM
2. in menuconfig you have option to set when malloc/calloc (and probably arrays too) should use PSRAM instead of internal ram (default is 16kB)
3. you can explicitly malloc from psram with:
https://docs.espressif.com/projects/esp ... t8uint32_t

Re: Issue in task creation and stack overflow

Posted: Fri Oct 29, 2021 11:57 am
by saigajul37
Its already done,
1)Mapping external PSRAM including the setting of malloc/calloc option
2)Mapping .bss memory segmet to external RAM

Re: Issue in task creation and stack overflow

Posted: Fri Oct 29, 2021 5:50 pm
by chegewara
1. please check this option in menuconfig SPIRAM_MALLOC_ALWAYSINTERNAL
2. again, its safest to use heap_caps_malloc(n, MALLOC_CAP_SPIRAM), because you control where exactly variable/array is placed