ESP32 Reset After Flash

divyat5
Posts: 3
Joined: Sun Jul 03, 2022 9:01 pm

ESP32 Reset After Flash

Postby divyat5 » Sun Jul 03, 2022 9:56 pm

Hi,
I am a newbie to ESP32. Can you please suggest a solution...

Hardware: ESP32 KitC V4 ESP32-WROOM-32
IDE: Platform IO for VSCode
Framework: ESP IDF
Board: az-delivery-devkit-v4

For my project, i want to run inference of neural network model on my esp32. The inference code consists the following variables:
  1. static constexpr signed char layer_2_weight[3][3][1][32] = <constant values>;
  2. static constexpr signed char layer_2_bias[32] = <constant values>;
  3. static constexpr float layer_3_threshold[32] = <constant values>;
  4. static constexpr unsigned int layer_5_weight[3][3][32][1] = <constant values>;
  5. static constexpr signed char layer_5_bias[32] = <constant values>;
  6. static constexpr signed char layer_6_threshold[32] = <constant values>;
  7. static constexpr unsigned int layer_9_weight[32][25] = <constant values>;
  8. static constexpr signed char layer_9_bias[32] = <constant values>;
  9. static constexpr signed char layer_10_threshold[32] = {<constant values>;
  10. static constexpr unsigned int layer_11_weight[10][1] = <constant values>;
  11. static constexpr signed char layer_11_bias[10] = <constant values>;
  12.  
  13. static double layer_2_output[26][26][32];
  14. static unsigned int layer_3_output[26][26][1];
  15. static unsigned int layer_4_output[13][13][1];
  16. static signed int layer_5_output[11][11][32];
  17. static unsigned int layer_6_output[11][11][1];
  18. static unsigned int layer_7_output[5][5][1];
  19. static signed int layer_9_output[32];
  20. static unsigned int layer_10_output[1];
  21. static signed int layer_11_output[10];
  22.  
  23. void predict_model(double const * const x, double * pred)
  24. {
  25. //for loop for layers.
  26. }
  27. ---- Code end -------
The size of all the static variables in this model is about 190kB.

For image buffers and uart buffers, 10kB is used is allocated using malloc().
I am transferring the image via UART and that aspect of the code runs perfectly when this predict_model function is not called.

Problem:
When predict_model is called, linker error comes that the data is overflown by certain amount of bytes.
  1. Linking .pio/build/az-delivery-devkit-v4/firmware.elf
  2. /home/divya/.platformio/packages/toolchain-xtensa-esp32@8.4.0+2021r2-patch3/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: .pio/build/az-delivery-devkit-v4/firmware.elf section `.dram0.bss' will not fit in region `dram0_0_seg'
  3. /home/divya/.platformio/packages/toolchain-xtensa-esp32@8.4.0+2021r2-patch3/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: DRAM segment data does not fit.
  4. /home/divya/.platformio/packages/toolchain-xtensa-esp32@8.4.0+2021r2-patch3/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: DRAM segment data does not fit.
  5. /home/divya/.platformio/packages/toolchain-xtensa-esp32@8.4.0+2021r2-patch3/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: region `dram0_0_seg' overflowed by 24848 bytes
  6. collect2: error: ld returned 1 exit status
  7. *** [.pio/build/az-delivery-devkit-v4/firmware.elf] Error 1
Therefore I increase dram0_0_seg (RW):
  1.   /* Shared data RAM, excluding memory reserved for ROM bss/data/stack.
  2.  
  3.      Enabling Bluetooth & Trace Memory features in menuconfig will decrease
  4.      the amount of RAM available.
  5.  
  6.      Note: Length of this section *should* be 0x50000, and this extra DRAM is available
  7.      in heap at runtime. However due to static ROM memory usage at this 176KB mark, the
  8.      additional static memory temporarily cannot be used.
  9.   */
  10.   dram0_0_seg (RW) :                 org = 0x3FFB0000 + CONFIG_BT_RESERVE_DRAM,
  11.                                   //  len = DRAM0_0_SEG_LEN
  12.                                      len = 0x50000 // changed value
Question 1 - Is this correct?

Question 2 - After these modifications, the ESP32 getting automatically resets after flashing with above changes on power on(hard reset).

Error code:
  1. Rebooting...
  2. ets Jun  8 2016 00:22:57
  3.  
  4. rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  5. configsip: 0, SPIWP:0xee
  6. clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
  7. mode:DIO, clock div:2
  8. load:0x3fff0030,len:7560
  9. load:0x40078000,len:14912
  10. load:0x40080400,len:3688
  11. entry 0x4008067c
  12. [0;32mI (27) boot: ESP-IDF 4.3.2 2nd stage bootloader␛[0m
  13. [0;32mI (27) boot: compile time 23:17:23[0m
  14. [0;32mI (27) boot: chip revision: 1[0m
  15. [0;32mI (30) boot_comm: chip revision: 1, min. bootloader chip revision: 0[0m
  16. [0;32mI (37) boot.esp32: SPI Speed      : 40MHz␛[0m
  17. [0;32mI (42) boot.esp32: SPI Mode       : DIO␛[0m
  18. [0;32mI (46) boot.esp32: SPI Flash Size : 4MB␛[0m
  19. [0;32mI (51) boot: Enabling RNG early entropy source...␛[0m
  20. [0;32mI (56) boot: Partition Table:␛[0m
  21. [0;32mI (60) boot: ## Label            Usage          Type ST Offset   Length␛[0m
  22. [0;32mI (67) boot:  0 nvs              WiFi data        01 02 00009000 00006000␛[0m
  23. [0;32mI (74) boot:  1 phy_init         RF data          01 01 0000f000 00001000␛[0m
  24. [0;32mI (82) boot:  2 factory          factory app      00 00 00010000 00100000␛[0m
  25. [0;32mI (89) boot: End of partition table␛[0m
  26. [0;32mI (93) boot_comm: chip revision: 1, min. application chip revision: 0[0m
  27. [0;32mI (101) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=09f30h ( 40752) map␛[0m
  28. [0;32mI (124) esp_image: segment 1: paddr=00019f58 vaddr=3ffb0000 size=02988h ( 10632) load␛[0m
  29. [0;32mI (128) esp_image: segment 2: paddr=0001c8e8 vaddr=40080000 size=03730h ( 14128) load␛[0m
  30. [0;32mI (136) esp_image: segment 3: paddr=00020020 vaddr=400d0020 size=16bb0h ( 93104) map␛[0m
  31. [0;32mI (172) esp_image: segment 4: paddr=00036bd8 vaddr=40083730 size=08aa4h ( 35492) load␛[0m
  32. [0;32mI (187) esp_image: segment 5: paddr=0003f684 vaddr=50000000 size=00010h (    16) load␛[0m
  33. [0;32mI (193) boot: Loaded app from partition at offset 0x10000␛[0m
  34. [0;32mI (193) boot: Disabling RNG early entropy source...␛[0m
  35. Re-enable cpu cache.
  36. Guru Meditation Error: Core  0 panic'ed (IntegerDivideByZero). Exception was unhandled.
  37.  
  38. Core  0 register dump:
  39. PC      : 0x4008afb1  PS      : 0x00060730  A0      : 0x8008b00c  A1      : 0x3ffe3c00  
  40. A2      : 0x00fdffb3  A3      : 0x00000000  A4      : 0x00000000  A5      : 0x00001000  
  41. A6      : 0x3ffe3ba2  A7      : 0x00000002  A8      : 0x00000000  A9      : 0x00000000  
  42. A10     : 0x20000000  A11     : 0x00000000  A12     : 0x00000014  A13     : 0x3ffe3bf4  
  43. A14     : 0x00000007  A15     : 0x00000006  SAR     : 0x00000010  EXCCAUSE: 0x00000006  
  44. EXCVADDR: 0x00000000  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0x00000000  
  45.  
  46. Backtrace:0x4008afae:0x3ffe3c00 0x4008b009:0x3ffe3c20 0x4008170b:0x3ffe3c40 0x40079508:0x3ffe3c80 |<-CORRUPTED
  47.  
  48.  
  49. ELF file SHA256: d0070a14dd27e9ef

ESP_Sprite
Posts: 9715
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 Reset After Flash

Postby ESP_Sprite » Mon Jul 04, 2022 2:23 am

You can't just modify the linker file and expect the ESP32 to magically develop RAM there. You likely still have free RAM left, but not in the BSS segment. Suggest to use dynamic allocation rather than static allocation for those variables.

divyat5
Posts: 3
Joined: Sun Jul 03, 2022 9:01 pm

Re: ESP32 Reset After Flash

Postby divyat5 » Mon Jul 04, 2022 8:30 pm

Thank you for your reply and your help!

What do you mean by dynamic allocation for those variables? If i use malloc():
1) I will get the memory, during runtime, but I still need the initialized data values of those variables.
2) According to my understanding when I have to use the constant values, the values require space in bss only.
3) Can you give an example of what you mean by dynamic allocation if it's not malloc() or any relevant link regarding this error.

ESP_Sprite
Posts: 9715
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 Reset After Flash

Postby ESP_Sprite » Tue Jul 05, 2022 7:43 am

Your constexpr stuff is fine (assuming constexpr in c++ does the same as const in c): that gets allocated in flash and won't get moved from there to RAM. It's the non-constexp things (layer_x_output) that get allocated in bss.

divyat5
Posts: 3
Joined: Sun Jul 03, 2022 9:01 pm

Re: ESP32 Reset After Flash

Postby divyat5 » Mon Jul 11, 2022 10:49 pm

Thank you for your suggestion! I have done what you have said and it makes sense to me now:
  1. double*** malloc_d_array(int x, int y, int z);
  2. signed int*** malloc_i_array(int x, int y, int z);
  3. unsigned int*** malloc_ui_array(int x, int y, int z);
  4.  
  5. void predict_model(double const * const x, double * pred) {
  6. double*** layer_2_output = malloc_d_array(26,26,32);
  7. }
  8.  
  9. double*** malloc_d_array(int x,int y,int z){
  10. // Allocate memory blocks of size
  11.   double *** a = new double **[x];
  12.     if (!a)
  13.       {printf ("--Memory allocation failed--");}
  14.  
  15.     for (int i = 0; i < x; i++) {
  16.     // Allocate memory blocks for rows of each 2D array
  17.         a[i] = new double*[y];
  18.         if (!a[i])
  19.         {printf ("--Memory allocation failed--");}
  20.  
  21.         for (int j = 0; j < y; j++) {
  22.  
  23.     // Allocate memory blocks for columns of each 2D array
  24.             a[i][j] = new double[z];
  25.             if (!a[i][j])
  26.             {printf ("--Memory allocation failed--");}
  27.             }
  28.     }
  29.     return a;
  30. }

And all the rest of the layers' outputs like this. But in the start of the predict model function,
I am getting different values for the layer_0_output every time I build and run. And I am not changing the values for x. :

  1. void predict_model(double const * const x, double * pred) {
  2.  
  3. double*** layer_2_output = malloc_d_array(26,26,32);
  4.  
  5. unsigned int*** layer_3_output = malloc_ui_array(26,26,1);
  6. // rest of the layers //
  7.  
  8.     auto layer_0_output = x;
  9.     printf(" Layer 0 Op: %d \n", layer_0_output);
  10.  //for loops
  11. }
  12.  



x is Image data consisting of 28x28 features and converting the values into double and the converted values are correct as I saw them by printing that memory block. I have used malloc() for saving the image data and malloc() for sizeof(double) to save the converted values.

And I am not able to understand why the values are changing and as it is happening in the first layer only, the outputs are changing.

Is there any reason why this is happening?

ESP_Sprite
Posts: 9715
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 Reset After Flash

Postby ESP_Sprite » Tue Jul 12, 2022 6:51 am

Malloc() does not initialize the allocate memory to zero, maybe that is the issue? You could use calloc() instead, that does make sure everything is zero'ed out.

Who is online

Users browsing this forum: No registered users and 57 guests