• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <stdint.h>
18 #include "sdkconfig.h"
19 
20 #define ROMFN_ATTR
21 
22 //Normally, the linker script will put all code and rodata in flash,
23 //and all variables in shared RAM. These macros can be used to redirect
24 //particular functions/variables to other memory regions.
25 
26 // Forces code into IRAM instead of flash
27 #define IRAM_ATTR _SECTION_ATTR_IMPL(".iram1", __COUNTER__)
28 
29 // Forces data into DRAM instead of flash
30 #define DRAM_ATTR _SECTION_ATTR_IMPL(".dram1", __COUNTER__)
31 
32 #ifdef CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
33 // Forces data into IRAM instead of DRAM
34 #define IRAM_DATA_ATTR __attribute__((section(".iram.data")))
35 
36 // Forces data into IRAM instead of DRAM and map it to coredump
37 #define COREDUMP_IRAM_DATA_ATTR _SECTION_ATTR_IMPL(".iram.data.coredump", __COUNTER__)
38 
39 // Forces bss into IRAM instead of DRAM
40 #define IRAM_BSS_ATTR __attribute__((section(".iram.bss")))
41 #else
42 #define COREDUMP_IRAM_DATA_ATTR
43 #define IRAM_DATA_ATTR
44 
45 #define IRAM_BSS_ATTR
46 #endif
47 
48 // Forces data to be 4 bytes aligned
49 #define WORD_ALIGNED_ATTR __attribute__((aligned(4)))
50 
51 // Forces data to be placed to DMA-capable places
52 #define DMA_ATTR WORD_ALIGNED_ATTR DRAM_ATTR
53 
54 // Forces a function to be inlined
55 #define FORCE_INLINE_ATTR static inline __attribute__((always_inline))
56 
57 // Forces a string into DRAM instead of flash
58 // Use as esp_rom_printf(DRAM_STR("Hello world!\n"));
59 #define DRAM_STR(str) (__extension__({static const DRAM_ATTR char __c[] = (str); (const char *)&__c;}))
60 
61 // Forces code into RTC fast memory. See "docs/deep-sleep-stub.rst"
62 #define RTC_IRAM_ATTR _SECTION_ATTR_IMPL(".rtc.text", __COUNTER__)
63 
64 #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
65 // Forces bss variable into external memory. "
66 #define EXT_RAM_ATTR _SECTION_ATTR_IMPL(".ext_ram.bss", __COUNTER__)
67 #else
68 #define EXT_RAM_ATTR
69 #endif
70 
71 // Forces data into RTC slow memory. See "docs/deep-sleep-stub.rst"
72 // Any variable marked with this attribute will keep its value
73 // during a deep sleep / wake cycle.
74 #define RTC_DATA_ATTR _SECTION_ATTR_IMPL(".rtc.data", __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 // Allows to place data into RTC_SLOW memory.
80 #define RTC_SLOW_ATTR _SECTION_ATTR_IMPL(".rtc.force_slow", __COUNTER__)
81 
82 // Allows to place data into RTC_FAST memory.
83 #define RTC_FAST_ATTR _SECTION_ATTR_IMPL(".rtc.force_fast", __COUNTER__)
84 
85 // Forces data into noinit section to avoid initialization after restart.
86 #define __NOINIT_ATTR _SECTION_ATTR_IMPL(".noinit", __COUNTER__)
87 
88 // Forces data into RTC slow memory of .noinit section.
89 // Any variable marked with this attribute will keep its value
90 // after restart or during a deep sleep / wake cycle.
91 #define RTC_NOINIT_ATTR  _SECTION_ATTR_IMPL(".rtc_noinit", __COUNTER__)
92 
93 // Forces code into DRAM instead of flash and map it to coredump
94 #define COREDUMP_DRAM_ATTR _SECTION_ATTR_IMPL(".dram1.coredump", __COUNTER__)
95 
96 // Forces data into RTC memory and map it to coredump
97 #define COREDUMP_RTC_DATA_ATTR _SECTION_ATTR_IMPL(".rtc.coredump", __COUNTER__)
98 
99 // Allows to place data into RTC_FAST memory and map it to coredump
100 #define COREDUMP_RTC_FAST_ATTR _SECTION_ATTR_IMPL(".rtc.fast.coredump", __COUNTER__)
101 
102 // Forces to not inline function
103 #define NOINLINE_ATTR __attribute__((noinline))
104 
105 // This allows using enum as flags in C++
106 // Format: FLAG_ATTR(flag_enum_t)
107 #ifdef __cplusplus
108 
109 // Inline is required here to avoid multiple definition error in linker
110 #define FLAG_ATTR_IMPL(TYPE, INT_TYPE) \
111 FORCE_INLINE_ATTR constexpr TYPE operator~ (TYPE a) { return (TYPE)~(INT_TYPE)a; } \
112 FORCE_INLINE_ATTR constexpr TYPE operator| (TYPE a, TYPE b) { return (TYPE)((INT_TYPE)a | (INT_TYPE)b); } \
113 FORCE_INLINE_ATTR constexpr TYPE operator& (TYPE a, TYPE b) { return (TYPE)((INT_TYPE)a & (INT_TYPE)b); } \
114 FORCE_INLINE_ATTR constexpr TYPE operator^ (TYPE a, TYPE b) { return (TYPE)((INT_TYPE)a ^ (INT_TYPE)b); } \
115 FORCE_INLINE_ATTR constexpr TYPE operator>> (TYPE a, int b) { return (TYPE)((INT_TYPE)a >> b); } \
116 FORCE_INLINE_ATTR constexpr TYPE operator<< (TYPE a, int b) { return (TYPE)((INT_TYPE)a << b); } \
117 FORCE_INLINE_ATTR TYPE& operator|=(TYPE& a, TYPE b) { a = a | b; return a; } \
118 FORCE_INLINE_ATTR TYPE& operator&=(TYPE& a, TYPE b) { a = a & b; return a; } \
119 FORCE_INLINE_ATTR TYPE& operator^=(TYPE& a, TYPE b) { a = a ^ b; return a; } \
120 FORCE_INLINE_ATTR TYPE& operator>>=(TYPE& a, int b) { a >>= b; return a; } \
121 FORCE_INLINE_ATTR TYPE& operator<<=(TYPE& a, int b) { a <<= b; return a; }
122 
123 #define FLAG_ATTR_U32(TYPE) FLAG_ATTR_IMPL(TYPE, uint32_t)
124 #define FLAG_ATTR FLAG_ATTR_U32
125 
126 #else
127 #define FLAG_ATTR(TYPE)
128 #endif
129 
130 // Implementation for a unique custom section
131 //
132 // This prevents gcc producing "x causes a section type conflict with y"
133 // errors if two variables in the same source file have different linkage (maybe const & non-const) but are placed in the same custom section
134 //
135 // Using unique sections also means --gc-sections can remove unused
136 // data with a custom section type set
137 #define _SECTION_ATTR_IMPL(SECTION, COUNTER) __attribute__((section(SECTION "." _COUNTER_STRINGIFY(COUNTER))))
138 
139 #define _COUNTER_STRINGIFY(COUNTER) #COUNTER
140 
141 /* Use IDF_DEPRECATED attribute to mark anything deprecated from use in
142    ESP-IDF's own source code, but not deprecated for external users.
143 */
144 #ifdef IDF_CI_BUILD
145 #define IDF_DEPRECATED(REASON) __attribute__((deprecated(REASON)))
146 #else
147 #define IDF_DEPRECATED(REASON)
148 #endif
149 
150 #endif /* __ESP_ATTR_H__ */
151