1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #ifndef __ESP_ATTR_H__
15 #define __ESP_ATTR_H__
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #include "sdkconfig.h"
22 
23 #define ROMFN_ATTR
24 
25 //Normally, the linker script will put all code and rodata in flash,
26 //and all variables in shared RAM. These macros can be used to redirect
27 //particular functions/variables to other memory regions.
28 
29 // Forces code into IRAM instead of flash
30 #define IRAM_ATTR _SECTION_ATTR_IMPL(".iram1", __COUNTER__)
31 
32 // Forces data into DRAM instead of flash
33 #define DRAM_ATTR _SECTION_ATTR_IMPL(".dram1", __COUNTER__)
34 
35 // Forces data to be 4 bytes aligned
36 #define WORD_ALIGNED_ATTR __attribute__((aligned(4)))
37 
38 // Forces data to be placed to DMA-capable places
39 #define DMA_ATTR WORD_ALIGNED_ATTR DRAM_ATTR
40 
41 // Forces a function to be inlined
42 #define FORCE_INLINE_ATTR static inline __attribute__((always_inline))
43 
44 // Forces a string into DRAM instead of flash
45 // Use as ets_printf(DRAM_STR("Hello world!\n"));
46 #define DRAM_STR(str) (__extension__({static const DRAM_ATTR char __c[] = (str); (const char *)&__c;}))
47 
48 // Forces code into RTC fast memory. See "docs/deep-sleep-stub.rst"
49 #define RTC_IRAM_ATTR _SECTION_ATTR_IMPL(".rtc.text", __COUNTER__)
50 
51 #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
52 // Forces bss variable into external memory. "
53 #define EXT_RAM_ATTR _SECTION_ATTR_IMPL(".ext_ram.bss", __COUNTER__)
54 #else
55 #define EXT_RAM_ATTR
56 #endif
57 
58 // Forces data into RTC slow memory. See "docs/deep-sleep-stub.rst"
59 // Any variable marked with this attribute will keep its value
60 // during a deep sleep / wake cycle.
61 #define RTC_DATA_ATTR _SECTION_ATTR_IMPL(".rtc.data", __COUNTER__)
62 
63 // Forces read-only data into RTC memory. See "docs/deep-sleep-stub.rst"
64 #define RTC_RODATA_ATTR _SECTION_ATTR_IMPL(".rtc.rodata", __COUNTER__)
65 
66 // Allows to place data into RTC_SLOW memory.
67 #define RTC_SLOW_ATTR _SECTION_ATTR_IMPL(".rtc.force_slow", __COUNTER__)
68 
69 // Allows to place data into RTC_FAST memory.
70 #define RTC_FAST_ATTR _SECTION_ATTR_IMPL(".rtc.force_fast", __COUNTER__)
71 
72 // Forces data into noinit section to avoid initialization after restart.
73 #define __NOINIT_ATTR _SECTION_ATTR_IMPL(".noinit", __COUNTER__)
74 
75 // Forces data into RTC slow memory of .noinit section.
76 // Any variable marked with this attribute will keep its value
77 // after restart or during a deep sleep / wake cycle.
78 #define RTC_NOINIT_ATTR  _SECTION_ATTR_IMPL(".rtc_noinit", __COUNTER__)
79 
80 // Forces to not inline function
81 #define NOINLINE_ATTR __attribute__((noinline))
82 
83 // This allows using enum as flags in C++
84 // Format: FLAG_ATTR(flag_enum_t)
85 #ifdef __cplusplus
86 
87 // Inline is required here to avoid multiple definition error in linker
88 #define FLAG_ATTR_IMPL(TYPE, INT_TYPE) \
89 FORCE_INLINE_ATTR constexpr TYPE operator~ (TYPE a) { return (TYPE)~(INT_TYPE)a; } \
90 FORCE_INLINE_ATTR constexpr TYPE operator| (TYPE a, TYPE b) { return (TYPE)((INT_TYPE)a | (INT_TYPE)b); } \
91 FORCE_INLINE_ATTR constexpr TYPE operator& (TYPE a, TYPE b) { return (TYPE)((INT_TYPE)a & (INT_TYPE)b); } \
92 FORCE_INLINE_ATTR constexpr TYPE operator^ (TYPE a, TYPE b) { return (TYPE)((INT_TYPE)a ^ (INT_TYPE)b); } \
93 FORCE_INLINE_ATTR constexpr TYPE operator>> (TYPE a, int b) { return (TYPE)((INT_TYPE)a >> b); } \
94 FORCE_INLINE_ATTR constexpr TYPE operator<< (TYPE a, int b) { return (TYPE)((INT_TYPE)a << b); } \
95 FORCE_INLINE_ATTR TYPE& operator|=(TYPE& a, TYPE b) { a = a | b; return a; } \
96 FORCE_INLINE_ATTR TYPE& operator&=(TYPE& a, TYPE b) { a = a & b; return a; } \
97 FORCE_INLINE_ATTR TYPE& operator^=(TYPE& a, TYPE b) { a = a ^ b; return a; } \
98 FORCE_INLINE_ATTR TYPE& operator>>=(TYPE& a, int b) { a >>= b; return a; } \
99 FORCE_INLINE_ATTR TYPE& operator<<=(TYPE& a, int b) { a <<= b; return a; }
100 
101 #define FLAG_ATTR_U32(TYPE) FLAG_ATTR_IMPL(TYPE, uint32_t)
102 #define FLAG_ATTR FLAG_ATTR_U32
103 
104 #else
105 #define FLAG_ATTR(TYPE)
106 #endif
107 
108 // Implementation for a unique custom section
109 //
110 // This prevents gcc producing "x causes a section type conflict with y"
111 // errors if two variables in the same source file have different linkage (maybe const & non-const) but are placed in the same custom section
112 //
113 // Using unique sections also means --gc-sections can remove unused
114 // data with a custom section type set
115 #define _SECTION_ATTR_IMPL(SECTION, COUNTER) __attribute__((section(SECTION "." _COUNTER_STRINGIFY(COUNTER))))
116 
117 #define _COUNTER_STRINGIFY(COUNTER) #COUNTER
118 
119 /* Use IDF_DEPRECATED attribute to mark anything deprecated from use in
120    ESP-IDF's own source code, but not deprecated for external users.
121 */
122 #ifdef IDF_CI_BUILD
123 #define IDF_DEPRECATED(REASON) __attribute__((deprecated(REASON)))
124 #else
125 #define IDF_DEPRECATED(REASON)
126 #endif
127 
128 #ifdef __cplusplus
129 }
130 #endif
131 #endif /* __ESP_ATTR_H__ */
132