1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
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 // Forces code into RTC fast memory. See "docs/deep-sleep-stub.rst"
65 #define RTC_IRAM_ATTR _SECTION_ATTR_IMPL(".rtc.text", __COUNTER__)
66 
67 #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
68 // Forces bss variable into external memory. "
69 #define EXT_RAM_ATTR _SECTION_ATTR_IMPL(".ext_ram.bss", __COUNTER__)
70 #else
71 #define EXT_RAM_ATTR
72 #endif
73 
74 // Forces data into RTC slow memory. See "docs/deep-sleep-stub.rst"
75 // Any variable marked with this attribute will keep its value
76 // during a deep sleep / wake cycle.
77 #define RTC_DATA_ATTR _SECTION_ATTR_IMPL(".rtc.data", __COUNTER__)
78 
79 // Forces read-only data into RTC memory. See "docs/deep-sleep-stub.rst"
80 #define RTC_RODATA_ATTR _SECTION_ATTR_IMPL(".rtc.rodata", __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 // Allows to place data into RTC_FAST memory.
86 #define RTC_FAST_ATTR _SECTION_ATTR_IMPL(".rtc.force_fast", __COUNTER__)
87 
88 // Forces data into noinit section to avoid initialization after restart.
89 #define __NOINIT_ATTR _SECTION_ATTR_IMPL(".noinit", __COUNTER__)
90 
91 #if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY
92 // Forces data into external memory noinit section to avoid initialization after restart.
93 #define EXT_RAM_NOINIT_ATTR _SECTION_ATTR_IMPL(".ext_ram_noinit", __COUNTER__)
94 #else
95 // Place in internal noinit section
96 #define EXT_RAM_NOINIT_ATTR __NOINIT_ATTR
97 #endif
98 
99 // Forces data into RTC slow memory of .noinit section.
100 // Any variable marked with this attribute will keep its value
101 // after restart or during a deep sleep / wake cycle.
102 #define RTC_NOINIT_ATTR  _SECTION_ATTR_IMPL(".rtc_noinit", __COUNTER__)
103 
104 // Forces code into DRAM instead of flash and map it to coredump
105 // Use dram2 instead of dram1 to make sure this section will not be included
106 // by dram1 section in the linker script
107 #define COREDUMP_DRAM_ATTR _SECTION_ATTR_IMPL(".dram2.coredump", __COUNTER__)
108 
109 // Forces data into RTC memory and map it to coredump
110 #define COREDUMP_RTC_DATA_ATTR _SECTION_ATTR_IMPL(".rtc.coredump", __COUNTER__)
111 
112 // Allows to place data into RTC_FAST memory and map it to coredump
113 #define COREDUMP_RTC_FAST_ATTR _SECTION_ATTR_IMPL(".rtc.fast.coredump", __COUNTER__)
114 
115 // Forces to not inline function
116 #define NOINLINE_ATTR __attribute__((noinline))
117 
118 // This allows using enum as flags in C++
119 // Format: FLAG_ATTR(flag_enum_t)
120 #ifdef __cplusplus
121 
122 // Inline is required here to avoid multiple definition error in linker
123 #define FLAG_ATTR_IMPL(TYPE, INT_TYPE) \
124 FORCE_INLINE_ATTR constexpr TYPE operator~ (TYPE a) { return (TYPE)~(INT_TYPE)a; } \
125 FORCE_INLINE_ATTR constexpr TYPE operator| (TYPE a, TYPE b) { return (TYPE)((INT_TYPE)a | (INT_TYPE)b); } \
126 FORCE_INLINE_ATTR constexpr TYPE operator& (TYPE a, TYPE b) { return (TYPE)((INT_TYPE)a & (INT_TYPE)b); } \
127 FORCE_INLINE_ATTR constexpr TYPE operator^ (TYPE a, TYPE b) { return (TYPE)((INT_TYPE)a ^ (INT_TYPE)b); } \
128 FORCE_INLINE_ATTR constexpr TYPE operator>> (TYPE a, int b) { return (TYPE)((INT_TYPE)a >> b); } \
129 FORCE_INLINE_ATTR constexpr TYPE operator<< (TYPE a, int b) { return (TYPE)((INT_TYPE)a << b); } \
130 FORCE_INLINE_ATTR TYPE& operator|=(TYPE& a, TYPE b) { a = a | b; return a; } \
131 FORCE_INLINE_ATTR TYPE& operator&=(TYPE& a, TYPE b) { a = a & b; return a; } \
132 FORCE_INLINE_ATTR TYPE& operator^=(TYPE& a, TYPE b) { a = a ^ b; return a; } \
133 FORCE_INLINE_ATTR TYPE& operator>>=(TYPE& a, int b) { a >>= b; return a; } \
134 FORCE_INLINE_ATTR TYPE& operator<<=(TYPE& a, int b) { a <<= b; return a; }
135 
136 #define FLAG_ATTR_U32(TYPE) FLAG_ATTR_IMPL(TYPE, uint32_t)
137 #define FLAG_ATTR FLAG_ATTR_U32
138 
139 #else
140 #define FLAG_ATTR(TYPE)
141 #endif
142 
143 // Implementation for a unique custom section
144 //
145 // This prevents gcc producing "x causes a section type conflict with y"
146 // errors if two variables in the same source file have different linkage (maybe const & non-const) but are placed in the same custom section
147 //
148 // Using unique sections also means --gc-sections can remove unused
149 // data with a custom section type set
150 #define _SECTION_ATTR_IMPL(SECTION, COUNTER) __attribute__((section(SECTION "." _COUNTER_STRINGIFY(COUNTER))))
151 
152 #define _COUNTER_STRINGIFY(COUNTER) #COUNTER
153 
154 /* Use IDF_DEPRECATED attribute to mark anything deprecated from use in
155    ESP-IDF's own source code, but not deprecated for external users.
156 */
157 #ifdef IDF_CI_BUILD
158 #define IDF_DEPRECATED(REASON) __attribute__((deprecated(REASON)))
159 #else
160 #define IDF_DEPRECATED(REASON)
161 #endif
162 
163 #ifdef __cplusplus
164 }
165 #endif
166 #endif /* __ESP_ATTR_H__ */
167