EasyManuals Logo

Espressif ESP32-S2 User Manual

Espressif ESP32-S2
1695 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #1433 background imageLoading...
Page #1433 background image
Chapter 4. API Guides
How to place code in IRAM
Some code is automatically placed into the IRAM region using the linker script.
If some specific application code needs to be placed into IRAM, it can be done by using the Linker Script Generation
feature and adding a linker script fragment file to your component that targets entire source files or functions with the
noflash placement. See the Linker Script Generation docs for more information.
Alternatively, its possible to specify IRAM placement in the source code using the IRAM_ATTR macro:
#include "esp_attr.h"
void IRAM_ATTR gpio_isr_handler(void* arg)
{
// ...
}
There are some possible issues with placement in IRAM, that may cause problems with IRAM-safe interrupt handlers:
Strings or constants inside an IRAM_ATTR function may not be placed in RAM automatically. Its possible to
use DRAM_ATTR attributes to mark these, or using the linker script method will cause these to be automatically
placed correctly.
void IRAM_ATTR gpio_isr_handler(void* arg)
{
const static DRAM_ATTR uint8_t INDEX_DATA[] = { 45, 33, 12, 0 };
const static char *MSG = DRAM_STR("I am a string stored in RAM");
}
Note that knowing which data should be marked with DRAM_ATTR can be hard, the compiler will sometimes
recognize that a variable or expression is constant (even if it is not marked const) and optimize it into flash,
unless it is marked with DRAM_ATTR.
GCC optimizations that automatically generate jump tables or switch/case lookup tables place these tables in
flash. IDF by default builds all files with -fno-jump-tables -fno-tree-switch-conversion flags to avoid this.
Jump table optimizations can be re-enabled for individual source files that dont need to be placed in
IRAM. For instructions on how to add the -fjump-tables -ftree-switch-conversion options when compil-
ing individual source files, see Controlling Component Compilation
4.19.3 IROM (code executed from Flash)
If a function is not explicitly placed into IRAM (Instruction RAM) or RTC memory, it is placed into flash. The
mechanism by which Flash MMU is used to allow code execution from flash is described in ESP32-S2 Technical
Reference Manual > Memory Management and Protection Units (MMU, MPU) [PDF]. As IRAM is limited, most of
an applications binary code must be placed into IROM instead.
During Application Startup Flow, the bootloader (which runs from IRAM) configures the MMU flash cache to map
the apps instruction code region to the instruction space. Flash accessed via the MMU is cached using some internal
SRAM and accessing cached flash data is as fast as accessing other types of internal memory.
4.19.4 RTC fast memory
The same region of RTC fast memory can be accessed as both instruction and data memory. Code which has to run
after wake-up from deep sleep mode has to be placed into RTC memory. Please check detailed description in deep
sleep documentation.
Remaining RTC fast memory is added to the heap unless the option CON-
FIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP is disabled. This memory can be used interchangeably
with DRAM (Data RAM), but is slightly slower to access.
Espressif Systems 1422
Submit Document Feedback
Release v4.4

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Espressif ESP32-S2 and is the answer not in the manual?

Espressif ESP32-S2 Specifications

General IconGeneral
BrandEspressif
ModelESP32-S2
CategorySingle board computers
LanguageEnglish