1 
2 /*
3  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 #ifndef __ESP_ATTR_H__
8 #define __ESP_ATTR_H__
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 #include "sdkconfig.h"
15 
16 #define ROMFN_ATTR
17 
18 //Normally, the linker script will put all code and rodata in flash,
19 //and all variables in shared RAM. These macros can be used to redirect
20 //particular functions/variables to other memory regions.
21 
22 // Forces code into IRAM instead of flash
23 #define IRAM_ATTR _SECTION_ATTR_IMPL(".iram1", __COUNTER__)
24 
25 // Forces data into DRAM instead of flash
26 #define DRAM_ATTR _SECTION_ATTR_IMPL(".dram1", __COUNTER__)
27 
28 // IRAM can only be accessed as an 8-bit memory on ESP32, when CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY is set
29 #define IRAM_8BIT_ACCESSIBLE (CONFIG_IDF_TARGET_ESP32 && CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY)
30 
31 // Make sure that IRAM is accessible as an 8-bit memory on ESP32.
32 // If that's not the case, coredump cannot dump data from IRAM.
33 #if IRAM_8BIT_ACCESSIBLE
34 // Forces data into IRAM instead of DRAM
35 #define IRAM_DATA_ATTR __attribute__((section(".iram.data")))
36 
37 // Forces data into IRAM instead of DRAM and map it to coredump
38 #define COREDUMP_IRAM_DATA_ATTR _SECTION_ATTR_IMPL(".iram2.coredump", __COUNTER__)
39 
40 // Forces bss into IRAM instead of DRAM
41 #define IRAM_BSS_ATTR __attribute__((section(".iram.bss")))
42 #else
43 
44 // IRAM is not accessible as an 8-bit memory, put IRAM coredump variables in DRAM
45 #define COREDUMP_IRAM_DATA_ATTR COREDUMP_DRAM_ATTR
46 #define IRAM_DATA_ATTR
47 
48 #define IRAM_BSS_ATTR
49 #endif
50 
51 // Forces data to be 4 bytes aligned
52 #define WORD_ALIGNED_ATTR __attribute__((aligned(4)))
53 
54 // Forces data to be placed to DMA-capable places
55 #define DMA_ATTR WORD_ALIGNED_ATTR DRAM_ATTR
56 
57 // Forces a function to be inlined
58 #define FORCE_INLINE_ATTR static inline __attribute__((always_inline))
59 
60 // Forces a string into DRAM instead of flash
61 // Use as esp_rom_printf(DRAM_STR("Hello world!\n"));
62 #define DRAM_STR(str) (__extension__({static const DRAM_ATTR char __c[] = (str); (const char *)&__c;}))
63 
64 #if CONFIG_SOC_RTC_FAST_MEM_SUPPORTED || CONFIG_SOC_RTC_SLOW_MEM_SUPPORTED
65 // Forces data into RTC memory. See "docs/deep-sleep-stub.rst"
66 // Any variable marked with this attribute will keep its value
67 // during a deep sleep / wake cycle.
68 #define RTC_DATA_ATTR _SECTION_ATTR_IMPL(".rtc.data", __COUNTER__)
69 
70 // Forces data into RTC memory of .noinit section.
71 // Any variable marked with this attribute will keep its value
72 // after restart or during a deep sleep / wake cycle.
73 #define RTC_NOINIT_ATTR  _SECTION_ATTR_IMPL(".rtc_noinit", __COUNTER__)
74 
75 // Forces read-only data into RTC memory. See "docs/deep-sleep-stub.rst"
76 #define RTC_RODATA_ATTR _SECTION_ATTR_IMPL(".rtc.rodata", __COUNTER__)
77 
78 // Forces data into RTC memory and map it to coredump
79 #define COREDUMP_RTC_DATA_ATTR _SECTION_ATTR_IMPL(".rtc.coredump", __COUNTER__)
80 
81 // Allows to place data into RTC_SLOW memory.
82 #define RTC_SLOW_ATTR _SECTION_ATTR_IMPL(".rtc.force_slow", __COUNTER__)
83 
84 // Forces code into RTC fast memory. See "docs/deep-sleep-stub.rst"
85 #define RTC_IRAM_ATTR _SECTION_ATTR_IMPL(".rtc.text", __COUNTER__)
86 
87 // Allows to place data into RTC_FAST memory.
88 #define RTC_FAST_ATTR _SECTION_ATTR_IMPL(".rtc.force_fast", __COUNTER__)
89 
90 // Allows to place data into RTC_FAST memory and map it to coredump
91 #define COREDUMP_RTC_FAST_ATTR _SECTION_ATTR_IMPL(".rtc.fast.coredump", __COUNTER__)
92 #else
93 #define RTC_DATA_ATTR
94 #define RTC_NOINIT_ATTR
95 #define RTC_RODATA_ATTR
96 #define COREDUMP_RTC_DATA_ATTR
97 #define RTC_SLOW_ATTR
98 #define RTC_IRAM_ATTR
99 #define RTC_FAST_ATTR
100 #define COREDUMP_RTC_FAST_ATTR
101 #endif
102 
103 #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
104 // Forces bss variable into external memory. "
105 #define EXT_RAM_BSS_ATTR _SECTION_ATTR_IMPL(".ext_ram.bss", __COUNTER__)
106 #else
107 #define EXT_RAM_BSS_ATTR
108 #endif
109 
110 /**
111  * Deprecated Macro for putting .bss on PSRAM
112  */
113 #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
114 // Forces bss variable into external memory. "
115 #define EXT_RAM_ATTR _SECTION_ATTR_IMPL(".ext_ram.bss", __COUNTER__) _Pragma ("GCC warning \"'EXT_RAM_ATTR' macro is deprecated, please use `EXT_RAM_BSS_ATTR`\"")
116 #else
117 #define EXT_RAM_ATTR _Pragma ("GCC warning \"'EXT_RAM_ATTR' macro is deprecated, please use `EXT_RAM_BSS_ATTR`\"")
118 #endif
119 
120 // Forces data into noinit section to avoid initialization after restart.
121 #define __NOINIT_ATTR _SECTION_ATTR_IMPL(".noinit", __COUNTER__)
122 
123 #if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY
124 // Forces data into external memory noinit section to avoid initialization after restart.
125 #define EXT_RAM_NOINIT_ATTR _SECTION_ATTR_IMPL(".ext_ram_noinit", __COUNTER__)
126 #else
127 // Place in internal noinit section
128 #define EXT_RAM_NOINIT_ATTR __NOINIT_ATTR
129 #endif
130 
131 // Forces code into DRAM instead of flash and map it to coredump
132 // Use dram2 instead of dram1 to make sure this section will not be included
133 // by dram1 section in the linker script
134 #define COREDUMP_DRAM_ATTR _SECTION_ATTR_IMPL(".dram2.coredump", __COUNTER__)
135 
136 // Forces to not inline function
137 #define NOINLINE_ATTR __attribute__((noinline))
138 
139 // This allows using enum as flags in C++
140 // Format: FLAG_ATTR(flag_enum_t)
141 #ifdef __cplusplus
142 
143 // Inline is required here to avoid multiple definition error in linker
144 #define FLAG_ATTR_IMPL(TYPE, INT_TYPE) \
145 FORCE_INLINE_ATTR constexpr TYPE operator~ (TYPE a) { return (TYPE)~(INT_TYPE)a; } \
146 FORCE_INLINE_ATTR constexpr TYPE operator| (TYPE a, TYPE b) { return (TYPE)((INT_TYPE)a | (INT_TYPE)b); } \
147 FORCE_INLINE_ATTR constexpr TYPE operator& (TYPE a, TYPE b) { return (TYPE)((INT_TYPE)a & (INT_TYPE)b); } \
148 FORCE_INLINE_ATTR constexpr TYPE operator^ (TYPE a, TYPE b) { return (TYPE)((INT_TYPE)a ^ (INT_TYPE)b); } \
149 FORCE_INLINE_ATTR constexpr TYPE operator>> (TYPE a, int b) { return (TYPE)((INT_TYPE)a >> b); } \
150 FORCE_INLINE_ATTR constexpr TYPE operator<< (TYPE a, int b) { return (TYPE)((INT_TYPE)a << b); } \
151 FORCE_INLINE_ATTR TYPE& operator|=(TYPE& a, TYPE b) { a = a | b; return a; } \
152 FORCE_INLINE_ATTR TYPE& operator&=(TYPE& a, TYPE b) { a = a & b; return a; } \
153 FORCE_INLINE_ATTR TYPE& operator^=(TYPE& a, TYPE b) { a = a ^ b; return a; } \
154 FORCE_INLINE_ATTR TYPE& operator>>=(TYPE& a, int b) { a = a >> b; return a; } \
155 FORCE_INLINE_ATTR TYPE& operator<<=(TYPE& a, int b) { a = a << b; return a; }
156 
157 #define FLAG_ATTR_U32(TYPE) FLAG_ATTR_IMPL(TYPE, uint32_t)
158 #define FLAG_ATTR FLAG_ATTR_U32
159 
160 #else
161 #define FLAG_ATTR(TYPE)
162 #endif
163 
164 // Implementation for a unique custom section
165 //
166 // This prevents gcc producing "x causes a section type conflict with y"
167 // errors if two variables in the same source file have different linkage (maybe const & non-const) but are placed in the same custom section
168 //
169 // Using unique sections also means --gc-sections can remove unused
170 // data with a custom section type set
171 #ifndef CONFIG_IDF_TARGET_LINUX
172 #define _SECTION_ATTR_IMPL(SECTION, COUNTER) __attribute__((section(SECTION "." _COUNTER_STRINGIFY(COUNTER))))
173 #define _COUNTER_STRINGIFY(COUNTER) #COUNTER
174 #else
175 // Custom section attributes are generally not used in the port files for Linux target, but may be found
176 // in the common header files. Don't declare custom sections in that case.
177 #define _SECTION_ATTR_IMPL(SECTION, COUNTER)
178 #endif
179 
180 /* Use IDF_DEPRECATED attribute to mark anything deprecated from use in
181    ESP-IDF's own source code, but not deprecated for external users.
182 */
183 #ifdef CONFIG_IDF_CI_BUILD
184 #define IDF_DEPRECATED(REASON) __attribute__((deprecated(REASON)))
185 #else
186 #define IDF_DEPRECATED(REASON)
187 #endif
188 
189 #ifdef __cplusplus
190 }
191 #endif
192 #endif /* __ESP_ATTR_H__ */
193