M5Core2: "IRAM0 segment data does not fit"

I have an M5Stack Core2 (AWS) that I'm using for a controller to drive an amplifier.

Last fall I ran into a problem where the 4Relay (Module 13.2) failed. I proved that the hardware wasn't bad by loading in a simple script to exercise the relays. I've been troubleshooting it and ran into 2 issues.

If I compile the project on my Laptop: Arduino IDE 2.3.8, it compiles OK, but the 4Relay board doesn't work, the I2C bus doesn't recognize the Address 0x26 (nor any of the others!).

If I compile the project on a new Desktop, (I recently loaded the everything new, Arduino IDE 2.3.10) It will run the smaller version of the sketch where the 4Relay board works, but my project fails with the error:

"IRAM0 segment data does not fit" & "region 'iram0_0_seg' overflowed by 1324 bytes".

I DO NOT GET THAT error on the IDE 2.3.8.

I tried changing all of the instances of Serial.print("string"); to Serial.print(F("string")); but it made absolutely no difference. Obviously, I don't understand what the "IRAM0" is!

Why does this compile on the older version 2.3.8 (where the 4Relay board doesn't work), and won't compile on 2.3.10 where the 4Relay board & the I2C works?

Is there something in the M5Stack "Partition Scheme" that will allow it to work? I seem to remember playing with this setting sometime in the past...

Sir Michael

I suspect that this is not so much about the IDE version but about the version of the board package and possibly versions of the libraries that you use.

You can use the board manager and the library manager to compare the versions on both systems.

You can also enable verbose output during compilation in File → Preferences to see the versions of the board package (first few lines) and the libraries used by your application (near the end of the output).

It's further advisable to set the Warning level in File → Preferences to All if you haven't done so yet; it might reveal something.

Well, what I've found out so far is that it's not the version of the Compiler, but the version of the M5Stack board that's causing the IRAM0 problem.

I tried uninstalling and reinstalling the Module_4RELAY Library, didn't help.

I then uninstalled the M5Stack by M5Stack (was 2.1.3, didn't update!) and reinstalled it and it installed 3.3.7.

Now, BOTH of my installations will allow the Relays to work, but also BOTH give me the IRAM0 Error (1324 bytes). I'm assuming that the newer version it taking up more of the IRAM0, and so I'm supposed to reduce my own usage????

I've been digging into it and found that you can possibly change the Arduino IDE Tools/Flash Size, but it only lists "16MB (128Mb)".

I've tried to change all Serial.print("string"); to Serial.print(F("string)); but that made no difference.

I do have a global structure:

// Keep the fields that are used across functions in AmpState
struct AmpState {
  double volts;
  int fwdPower;
  int refPower;
  float swr;
  int ampTemp;
  int currentBand;
  byte hours;
  byte minutes;
  byte mode;
  bool act1_byp0;  // Active (1) or Bypass (0) state
  int fanOutput;   // Amplifier Fan Output speed as read from NANO.
  byte rigModel;   // 0 = None, 1 = K3, 2 = KX3, 3 = IC-705
};
// Create a global instance
AmpState ampState = { 0, 0, 0, 0, 0, 0, 0, 0, 0, false, 0, 0 };

but I don't think that removing will give me 1324 bytes!!!

What else is taking up the IRAM0?

Sir Michael

sounds like you need to change your partition scheme..
can you switch to Huge App, that might do it??

good luck.. ~q

No, I tried that, didn't help.

I may be narrowing it down. When I comment out the declarations:

#include "BluetoothSerial.h"
BluetoothSerial IC705BT;

and cobble things up to get it to compile, it gets rid of the IRAM0 error.

Not sure if BlueToothSerial.h is taking up all of the IRAM0.

Sir Michael

interrupt routines maybe ?

None!

If I remove the BluetoothSerial.h OR the SD.h, it fits. Somewhere along the line the esp32 started grabbing more IRAM0. When I updated to the latest M5Unified, it no longer fit.

Sir Michael

I'm having this same problem. I'm using M5Unified 0.2.19 and M5Stack 3.3.8, which my M5AtomS3 uses and working nicely (and 48% full). My M5Core2 (with 16M Flash) isn't getting past those same errors and I've tried all the partition schemes, even the 16MB Flash. I'm using Arduino IDE 2.3.10.

IRAM0 segment data does not fit

region `iram0_0_seg' overflowed by 552 bytes

I can take out full subsections of my code, even the Serial.print statements, and the same overflow stays the same or goes down to 528 bytes overflow. Also the different partition schemes don't change this error. Sure seems to be something strange going on with a library.

Any ideas?

thanks,

Normally it is taken by functions with IRAM_ATTR.

I would check linker files. Those are shipped with board support package. These files have descriptions of memory segments and .elf section names. May be it is good idea to compare these files to original linker files from ESP-IDF or from an older installation of Arduino Core

Search for memory.ld

These places can be found by searching for "IRAM_ATTR" in the code of SD and BluetoothSerial libraries

I’ll check linker files. It just seems strange that M5StomS3 works nicely, but Core2 doesn’t. Maybe those linker partitions are not up to date? Even the M5AtomLite compiles and loads, albeit witb 89%.

Thanks

Ok, looking at those memory.ld files .. that's definitely out of depth for user level changes. The esp32 version (which the core2 seems to be) is definitely smaller, which we know:

iram0_0_seg (RX) : org = 0x40080000, len = 0x20000 + 0x0

vs the S3

iram0_2_seg (RX) : org = 0x42000020, len = 0x800000-0x20

I think this is a question for the expressive/M5Stack folks.

As the OP posted, this is strange especially as everything non M5Unified is deprecated.

What's more strange, is the M5AtomLite can load, but the Core2 cannot.

thanks,

The more additional relevant information you didn't specify is which Arduino boards platform are you using:

You can determine this by checking the board selection Arduino IDE's Tools > Board menu (if the board selection is under Tools > Board > M5Stack or Tools > Board > esp32), or by examining the verbose compilation output (looking at the first lines of the output).

Additionally, which specific version of that platform do you have installed. You can determine this by looking in the Arduino IDE Boards Manager.

Thanks for reply.

I was trying the board: M5Stack 3.3.8 but also have tried M5AtomS3.

The esp32 tools are Arduino IDE 2.3.10.

Any insight on other things to try would be appreciated.

thanks,

FQBN: m5stack:esp32:m5stack_core2
Using board 'm5stack_core2' from platform in folder: /Users/scott/Library/Arduino15/packages/m5stack/hardware/esp32/3.3.8
Using core 'esp32' from platform in folder: /Users/scott/Library/Arduino15/packages/m5stack/hardware/esp32/3.3.8

That's 0_0 vs 0_2. Apparently, each chip variant has both.

esp32$ rg -B 1 -A 1 'iram0_._seg \('
tools/esp32-libs/3.3.10/ld/memory.ld
54-  /* IRAM for PRO cpu. Not sure if happy with this, this is MMU area... */
55:  iram0_0_seg (RX) : org = 0x40080000, len = 0x20000 + 0x0
56-  /* Even though the segment name is iram, it is actually mapped to flash
57-  */
58:  iram0_2_seg (RX) : org = 0x400D0020, len = 0x330000-0x20
59-  /*

tools/esp32s2-libs/3.3.10/ld/memory.ld
46-  /* IRAM for CPU.*/
47:  iram0_0_seg (RX) : org = (0x40020000 + 0x2000 + 0x2000), len = 0x3FFDE000 - (0x3FFB0000 + 0x2000 + 0x2000)
48-  /* Even though the segment name is iram, it is actually mapped to flash
49-  */
50:  iram0_2_seg (RX) : org = 0x40080020, len = 0x780000-0x20
51-  /*

tools/esp32c3-libs/3.3.10/ld/memory.ld
50-  /* IRAM for PRO CPU. */
51:  iram0_0_seg (RX) : org = (0x4037C000 + 0x4000), len = 0x403CE710 - (0x4037C000 - 0x3FC7C000) - (0x3FC7C000 + 0x4000)
52-  /* Flash mapped instruction data */
53:  iram0_2_seg (RX) : org = 0x42000020, len = 0x800000-0x20
54-  /**

tools/esp32s3-libs/3.3.10/ld/memory.ld
57-  /* IRAM for PRO CPU. */
58:  iram0_0_seg (RX) : org = (0x40370000 + 0x4000), len = (((0x403CB700 - (0x40378000 - 0x3FC88000)) - 0x3FC88000) + 0x8000 - 0x4000)
59-  /* Flash mapped instruction data */
60:  iram0_2_seg (RX) : org = 0x42000020, len = 0x800000-0x20
61-  /**

The repeated comments says that 0_2 is actually flash. But the OP error is with 0_0. The original ESP32's length is easy enough -- 0x20000 + 0x0 -- but the others are more cryptic

> (0x3FFDE000 - (0x3FFB0000 + 0x2000 + 0x2000)).toString(16)
"2a000"
> (0x403CE710 - (0x4037C000 - 0x3FC7C000) - (0x3FC7C000 + 0x4000)).toString(16)
"4e710"
> (((0x403CB700 - (0x40378000 - 0x3FC88000)) - 0x3FC88000) + 0x8000 - 0x4000).toString(16)
"57700"

Progressively bigger, although maybe not all in a single chunk.

I happen to have esp32 3.3.10 installed. Using the board ESP32 Dev Module, this fits exactly:

#include <esp_attr.h>

#define REPEAT_COUNT "8869"

void IRAM_ATTR filler_func() {
    __asm__ (
        ".rept " REPEAT_COUNT "\n\t"
        "nop\n\t" "nop\n\t" "nop\n\t" "nop\n\t"
        ".endr\n\t"
    );
}

void setup() {
    filler_func();
}

void loop() {}

Happens to be a Linux box, so readelf is handy

$ readelf -l /home/kenb4/.cache/arduino/sketches/86A48A2F572C707983213B7DF6D73E7F/sketch_aug19a.ino.elf

Elf file type is EXEC (Executable file)
Entry point 0x400819a8
There are 7 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x001020 0x3f400020 0x3f400020 0x10bd8 0x10bd8 RW  0x1000
  LOAD           0x000ff0 0x3ff81ff0 0x3ff81ff0 0x00000 0x00010 RW  0x1000
  LOAD           0x012b60 0x3ffbdb60 0x3ffbdb60 0x041c1 0x05640 RW  0x1000
  LOAD           0x017000 0x40080000 0x40080000 0x1ffff 0x20000 RWE 0x1000
  LOAD           0x037020 0x400d0020 0x400d0020 0x1dc08 0x1dc08 R E 0x1000
  LOAD           0x055200 0x50000200 0x50000200 0x00020 0x00020 RW  0x1000
  LOAD           0x000fe8 0x50001fe8 0x50001fe8 0x00000 0x00018 RW  0x1000

 Section to Segment mapping:
  Segment Sections...
   00     .flash.appdesc .flash.rodata 
   01     .rtc_fast_reserved 
   02     .dram0.data .noinit .dram0.bss 
   03     .iram0.vectors .iram0.text .iram0.text_end 
   04     .flash.text 
   05     .rtc.force_slow 
   06     .rtc_slow_reserved 

Reading from the bottom, segment 03 -- the one in the middle -- contains iram0. And on the middle LOAD line, the MemSiz is exactly 0x20000. Increasing the REPEAT_COUNT to "8870"

IRAM0 segment data does not fit.
region `iram0_0_seg' overflowed by 8 bytes

I guess NOP is 2 bytes? When it links successfully with "8869", the sketch size is

Sketch uses 338336 bytes (25%) of program storage space. Maximum is 1310720 bytes.
Global variables use 22076 bytes (6%) of dynamic memory, leaving 305604 bytes for local variables. Maximum is 327680 bytes.

So that reported size -- and therefore the partition scheme -- does not address this specific issue. BTW, the AI says that iram0_0_seg

is allocated for time-critical executable code, low-level handlers, and specific drivers that must run rapidly or function while flash cache operations are suspended

and contains

  • interrupt vectors
  • IRAM-safe functions
  • WiFi and Bluetooth stack code
  • RTOS and core drivers

I'll try getting more info like your post shows. My question is why my code loads on an M5AtomLite (not S3) - but not the Core2.

thanks.

With esp32 3.3.10, "8870" quads of NOP

  • ESP32 Dev Module
    region `iram0_0_seg' overflowed by 8 bytes
    
  • M5Atom -- that's the board you use; there is no Lite-specific one, right?
    region `iram0_0_seg' overflowed by 8 bytes
    
  • M5Core2
    region `iram0_0_seg' overflowed by 4720 bytes
    

So the M5Atom is equivalent to stock ESP32, while M5Core2 has a bunch more code, even in an empty sketch. The latter has more built-in hardware -- screen, audio, sensors. There could be a bug somewhere marking some of that additional code as IRAM_ATTR

Doing the math, the max filler sketch that fits has

> 8870 - 4720 / 8
8280

Confirmed that with "8280" the segment size with M5Core2 is exactly 0x20000; and for the record, at that size

  • M5Core2
    Sketch uses 346368 bytes (5%) of program storage space. Maximum is 6553600 bytes.
    Global variables use 22148 bytes (0%) of dynamic memory, leaving 4499836 bytes for local variables. Maximum is 4521984 bytes.
    
  • M5Atom
    Sketch uses 333592 bytes (10%) of program storage space. Maximum is 3145728 bytes.
    Global variables use 22076 bytes (6%) of dynamic memory, leaving 305604 bytes for local variables. Maximum is 327680 bytes.
    

the total sketch size difference is 12776 bytes, about 2.7 times the segment size difference.

Switching to esp32 board version 2.0.17, "8870" fits on M5Stack-Core2 with the segment size 0x1fda8; M5Stack-ATOM is 0x1e914. That difference, 5268 bytes, is actually larger, but the total size fits for both.

You might also try earlier v3 boards.

Here's the difference, with 3.3.10 and "7870" so it easily fits

$ nm -f sysv --defined-only -C --radix=d --print-size --size-sort --reverse-sort \
  /home/kenb4/.cache/arduino/sketches/86A48A2F572C707983213B7DF6D73E7F/sketch_aug19a.ino.elf \
  | awk -F '|' '$7 == ".iram0.text" { print $5, $1 }' > atom.txt
$ nm -f sysv --defined-only -C --radix=d --print-size --size-sort --reverse-sort \
  /home/kenb4/.cache/arduino/sketches/86A48A2F572C707983213B7DF6D73E7F/sketch_aug19a.ino.elf \
  | awk -F '|' '$7 == ".iram0.text" { print $5, $1 }' > core2.txt
$ diff -u atom.txt core2.txt
--- atom.txt
+++ core2.txt
@@ -1,4 +1,5 @@
 00062965 filler_func()       
+00002303 esp_psram_impl_enable
 00001263 tlsf_memalign_offs  
 00000973 tlsf_realloc        
 00000814 tlsf_malloc         
@@ -11,6 +12,8 @@
 00000628 bootloader_flash_execute_command_common
 00000603 wdt_hal_init        
 00000560 spi_flash_hal_configure_host_io_mode
+00000518 psram_spi_init      
+00000511 psram_cmd_recv_start$constprop$0
 00000510 esp_flash_erase_region
 00000497 bootloader_flash_gpio_config
 00000496 spi_flash_hal_common_command
@@ -27,6 +30,7 @@
 00000372 .handle_livelock_int
 00000347 mmu_hal_map_region  
 00000344 heap_caps_aligned_alloc_base
+00000337 cache_sram_mmu_set  
 00000334 wdt_hal_config_stage
 00000321 __sflush_r          
 00000312 xt_highint5         
@@ -58,6 +62,7 @@
 00000231 vTaskPriorityDisinheritAfterTimeout
 00000230 spi_flash_chip_winbond_erase_block
 00000226 spi_flash_chip_winbond_erase_sector
+00000225 esp_psram_extram_writeback_cache
 00000223 _strtoul_l$isra$0   
 00000222 spi_flash_hal_device_config
 00000219 prvReceiveGeneric   
@@ -96,6 +101,7 @@
 00000157 bootloader_flash_reset_chip
 00000156 _xt_medint3         
 00000156 _xt_medint2         
+00000156 rtc_vddsdio_get_config
 00000153 xQueueGenericCreateStatic
 00000153 __smakebuf_r        
 00000153 prvInitialiseNewTask$constprop$0
@@ -137,8 +143,10 @@
 00000117 esp_intr_enable     
 00000116 xTaskIncrementTickOtherCores
 00000113 bootloader_flash_cs_timing_config
+00000113 __atomic_compare_exchange_1
 00000112 spi_flash_chip_generic_get_caps
 00000112 esp_ipc_isr_release_other_cpu
+00000111 __atomic_s32c1i_compare_exchange_1
 00000110 tick_hook           
 00000110 __swhatbuf_r        
 00000107 esp_heap_adjust_alignment_to_hw
@@ -205,6 +213,7 @@
 00000076 esp_ptr_executable  
 00000076 esp_crosscore_int_send
 00000075 wdt_hal_enable      
+00000075 rtc_vddsdio_set_config
 00000075 memmove             
 00000074 vTaskPlaceOnEventList
 00000074 prvCheckForValidListAndQueue
@@ -519,6 +528,7 @@
 00000015 esp_coex_common_int_restore_wrapper
 00000015 esp_cache_suspend_ext_mem_cache
 00000015 esp_cache_resume_ext_mem_cache
+00000015 cache_register_writeback
 00000014 tlsf_size           
 00000014 spi_flash_encryption_hal_check
 00000014 __sfp_lock_release  
@@ -526,6 +536,7 @@
 00000014 periph_rcc_exit     
 00000014 _lock_acquire_recursive
 00000014 _lock_acquire       
+00000014 esp_psram_is_initialized
 00000014 __env_unlock        
 00000014 __env_lock          
 00000013 xTaskGetTickCount   
@@ -549,6 +560,7 @@
 00000011 flash_init_state    
 00000010 vPortYieldOtherCore 
 00000010 vPortFree           
+00000010 spi_flash_guard_get 
 00000010 release_buffer_malloc
 00000010 multi_heap_set_lock 
 00000010 _free_r             

Those sizes (with radix=d) are in decimal. M5Core2 has additional code in iram0_0_seg, about

  • 4000 bytes for PSRAM functions -- M5Atom only has SRAM
  • a few hundred for RTC
  • two __atomic functions, a little over 100 bytes each

With esp32 3.3.10, empty sketch for M5Core2, readelf shows

  LOAD           0x018000 0x40080000 0x40080000 0x0fd37 0x0fd38 RWE 0x1000

the iram0_0_seg is almost half-full to start: 0x0fd38 (it's also the only Read/Write/Executable segment). Adding

it grows just over 16K to 0x13df0. Separately, SD.h with an SD.begin() call grows about 6K to 0x11464