Issue in task creation and stack overflow

saigajul37
Posts: 7
Joined: Tue Apr 27, 2021 12:07 pm
Location: pune

Issue in task creation and stack overflow

Postby saigajul37 » Fri Oct 29, 2021 9:56 am

  1. ESP32 wrover-b(8mb flash 8mb psram)
  2. Ported code from esp-idf v3.3.3 to v4.3 stable
  3. For FFT using esp-dsp lib
  4. Following are the errors I am facing
  5. 1) ***ERROR*** A stack overflow in task procc_data  has been detected.
  6. 2) Task creation failed due to memory allocation failure, but
  7. I (13860) Start memory size :   4147695
  8. I (13860) Consumed memory size :   16.000000
  9. I (13860) Consumed memory size in percent :   0 %
  1.  
  2. #define MAXSIZE (1024*5)
  3. #define SAMPLE_FRQ 1344
  4.  
  5. #define N_SAMPLES   4096
  6.  
  7. #define FFT_SAMP (N_SAMPLES)
  8.  
  9. #define FFT_TEST_COMP_SAMPLES_LEN           FFT_SAMP
  10. #define FFT_TEST_OUT_MAG_SAMPLE_LEN     (FFT_SAMP/2)
  11.  
  12.  
  13. int16_t x_value[MAXSIZE] = {0};
  14. int16_t y_value[MAXSIZE] = {0};
  15. int16_t z_value[MAXSIZE] = {0};
  16.  
  17. __attribute__((aligned(16)))
  18. float x_1[N_SAMPLES] ;
  19. __attribute__((aligned(16)))
  20. float y_1[N_SAMPLES] ;
  21. __attribute__((aligned(16)))
  22. float z_1[N_SAMPLES];
  23.  
  24. float xyz_abs [N_SAMPLES]={0};
  25.  
  26.  
  27. void runn_fft_on_raw(float *in_buff, float *out_buff)
  28. {
  29.     float y_cf[FFT_SAMP*2];
  30.    
  31.     for (int i=0 ; i < FFT_SAMP ; i++)    {
  32.  
  33.         y_cf[i*2 + 0] = in_buff[i] *make_wind_sample(i) ;
  34.         y_cf[i*2 + 1] = 0.0;
  35.        
  36.     }
  37.        
  38.     dsps_fft2r_fc32(y_cf, FFT_SAMP);
  39.     dsps_bit_rev_fc32(y_cf, FFT_SAMP);
  40.     dsps_cplx2reC_fc32(y_cf, FFT_SAMP);
  41.    
  42.  
  43.     for (int i = 0 ; i < FFT_TEST_OUT_MAG_SAMPLE_LEN ; i++) {
  44.        
  45.         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 );
  46.     }
  47.    
  48.  
  49. }      
  50.  
  51. void procc_data (void *pvParameters) {
  52.    
  53.     int16_t x_value[MAXSIZE] = {0};
  54.     int16_t y_value[MAXSIZE] = {0};
  55.     int16_t z_value[MAXSIZE] = {0};
  56.    
  57.     while(1) {
  58.        
  59.         //add accl data;
  60.         int16_t x_value[MAXSIZE] = add(x);
  61.         int16_t y_value[MAXSIZE] = add(y);
  62.         int16_t z_value[MAXSIZE] = add(z);
  63.     }
  64.    
  65. }
  66.  
  67.  
  68. void procc_data (void *pvParameters)
  69. {
  70.  
  71.     esp_task_wdt_add(NULL);
  72.  
  73.     float x_fft_data[FFT_TEST_OUT_MAG_SAMPLE_LEN] = {0.0};
  74.     float y_fft_data[FFT_TEST_OUT_MAG_SAMPLE_LEN] = {0.0};
  75.     float z_fft_data[FFT_TEST_OUT_MAG_SAMPLE_LEN] = {0.0}; 
  76.    
  77.     while(1){
  78.        
  79.        
  80.         memset(x_value, 0, sizeof(x_value));
  81.         memset(y_value, 0, sizeof(y_value));
  82.         memset(z_value, 0, sizeof(z_value));
  83.        
  84.         memset(x_fft_data, 0, sizeof(x_fft_data));
  85.         memset(y_fft_data, 0, sizeof(y_fft_data));
  86.         memset(z_fft_data, 0, sizeof(z_fft_data));
  87.        
  88.         memset(x_1, 0, sizeof(x_1));
  89.         memset(y_1, 0, sizeof(y_1));
  90.         memset(z_1, 0, sizeof(z_1));
  91.        
  92.  
  93.         get_sample(x_value, y_value, z_value);
  94.         cov_sample_in_flt(x_1, y_1, z_1);
  95.        
  96.         runn_fft_on_raw(x_1, x_fft_data);
  97.         runn_fft_on_raw(y_1, y_fft_data);
  98.         runn_fft_on_raw(z_1, z_fft_data);  
  99.        
  100.         // process data
  101.         // post process data to sever
  102.     }
  103.    
  104. }
  105.  
  106. void creatTask ()
  107. {
  108.    
  109.     isMaster = xTaskCreate(master_Task, "master_Task", 1024*8, NULL, configMAX_PRIORITIES, &master_t);
  110.     isLed = xTaskCreate(led_Task, "led_Task", 1024*2, NULL, 20, &led_t);
  111.     uint8_t istaskCreated = 0;
  112.  
  113.     while ( iswebsever != pdPASS)   {                           // wait till task is created properly
  114.  
  115.  
  116.         iswebsever = xTaskCreate(websever, "websever", 1024*8, NULL, 20, &websever_t, CORE_ONE);
  117.            
  118.         if (istaskCreated > 3 && iswebsever != pdPASS ) {
  119.  
  120.             esp_restart();
  121.         }
  122.  
  123.         istaskCreated++;
  124.  
  125.         msDelay(1000);
  126.     }
  127.     istaskCreated = 0;
  128.    
  129.         while ( isadd_raw_data != pdPASS)   {                       // wait till task is created properly
  130.        
  131.         isadd_raw_data = xTaskCreatePinnedToCore(add_raw_data, "add_raw_data", 1024*2, NULL, 23, &add_raw_data_t, CORE_ONE);
  132.  
  133.         if (istaskCreated > 3 && isadd_raw_data != pdPASS ) {
  134.  
  135.             esp_restart();
  136.         }
  137.  
  138.         istaskCreated++;
  139.  
  140.         msDelay(1000);
  141.     }
  142.    
  143.    
  144.    
  145.     istaskCreated = 0;
  146.    
  147.     while ( isprocc_data != pdPASS) {                       // wait till task is created properly
  148.  
  149.  
  150.         isprocc_data = xTaskCreatePinnedToCore(procc_data, "procc_data", 1024*25, NULL, 22, &procc_data_t, CORE_ONE);
  151.            
  152.         if (istaskCreated > 3 && isprocc_data != pdPASS ) {
  153.  
  154.             esp_restart();
  155.         }
  156.  
  157.         istaskCreated++;
  158.  
  159.         msDelay(1000);
  160.     }
  161.    
  162.     istaskCreated = 0;
  163.    
  164.    
  165.    
  166. }
  167.  
  168.    
  169.    
  170.  
  171. void app_main(void)
  172. {
  173.     esp_err_t err = ESP_FAIL;
  174.  
  175.     err = nvs_flash_init();
  176.  
  177.     if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
  178.  
  179.         ESP_ERROR_CHECK(nvs_flash_erase());
  180.         ESP_ERROR_CHECK( nvs_flash_init());
  181.     }
  182.    
  183.     init_accel();
  184.    
  185.     ret = dsps_fft2r_init_fc32( NULL, 4096 );
  186.    
  187.     if (ret  != ESP_OK)
  188.     {
  189.         printf( "Not possible to initialize FFT. Error = %i\n", ret);
  190.         return;
  191.     }
  192.    
  193.     creatTask();
  194.    
  195. }
Last edited by saigajul37 on Fri Oct 29, 2021 12:00 pm, edited 1 time in total.
Sainath Gajul

chegewara
Posts: 2375
Joined: Wed Jun 14, 2017 9:00 pm

Re: Issue in task creation and stack overflow

Postby chegewara » Fri Oct 29, 2021 10:50 am

You are creating 6 local variables (arrays) with 5kB in size each allocated from stack. Task stack is 25kB. Do the math.

saigajul37
Posts: 7
Joined: Tue Apr 27, 2021 12:07 pm
Location: pune

Re: Issue in task creation and stack overflow

Postby saigajul37 » Fri Oct 29, 2021 11:33 am

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:
Sainath Gajul

chegewara
Posts: 2375
Joined: Wed Jun 14, 2017 9:00 pm

Re: Issue in task creation and stack overflow

Postby chegewara » Fri Oct 29, 2021 11:51 am

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

saigajul37
Posts: 7
Joined: Tue Apr 27, 2021 12:07 pm
Location: pune

Re: Issue in task creation and stack overflow

Postby saigajul37 » Fri Oct 29, 2021 11:57 am

Its already done,
1)Mapping external PSRAM including the setting of malloc/calloc option
2)Mapping .bss memory segmet to external RAM
Sainath Gajul

chegewara
Posts: 2375
Joined: Wed Jun 14, 2017 9:00 pm

Re: Issue in task creation and stack overflow

Postby chegewara » Fri Oct 29, 2021 5:50 pm

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

Who is online

Users browsing this forum: No registered users and 376 guests