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