esp_attr.h 7.4 KB

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