esp_attr.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 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. #ifdef CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
  21. // Forces data into IRAM instead of DRAM
  22. #define IRAM_DATA_ATTR __attribute__((section(".iram.data")))
  23. // Forces data into IRAM instead of DRAM and map it to coredump
  24. #define COREDUMP_IRAM_DATA_ATTR _SECTION_ATTR_IMPL(".iram.data.coredump", __COUNTER__)
  25. // Forces bss into IRAM instead of DRAM
  26. #define IRAM_BSS_ATTR __attribute__((section(".iram.bss")))
  27. #else
  28. #define COREDUMP_IRAM_DATA_ATTR
  29. #define IRAM_DATA_ATTR
  30. #define IRAM_BSS_ATTR
  31. #endif
  32. // Forces data to be 4 bytes aligned
  33. #define WORD_ALIGNED_ATTR __attribute__((aligned(4)))
  34. // Forces data to be placed to DMA-capable places
  35. #define DMA_ATTR WORD_ALIGNED_ATTR DRAM_ATTR
  36. // Forces a function to be inlined
  37. #define FORCE_INLINE_ATTR static inline __attribute__((always_inline))
  38. // Forces a string into DRAM instead of flash
  39. // Use as esp_rom_printf(DRAM_STR("Hello world!\n"));
  40. #define DRAM_STR(str) (__extension__({static const DRAM_ATTR char __c[] = (str); (const char *)&__c;}))
  41. // Forces code into RTC fast memory. See "docs/deep-sleep-stub.rst"
  42. #define RTC_IRAM_ATTR _SECTION_ATTR_IMPL(".rtc.text", __COUNTER__)
  43. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  44. // Forces bss variable into external memory. "
  45. #define EXT_RAM_ATTR _SECTION_ATTR_IMPL(".ext_ram.bss", __COUNTER__)
  46. #else
  47. #define EXT_RAM_ATTR
  48. #endif
  49. // Forces data into RTC slow memory. See "docs/deep-sleep-stub.rst"
  50. // Any variable marked with this attribute will keep its value
  51. // during a deep sleep / wake cycle.
  52. #define RTC_DATA_ATTR _SECTION_ATTR_IMPL(".rtc.data", __COUNTER__)
  53. // Forces read-only data into RTC memory. See "docs/deep-sleep-stub.rst"
  54. #define RTC_RODATA_ATTR _SECTION_ATTR_IMPL(".rtc.rodata", __COUNTER__)
  55. // Allows to place data into RTC_SLOW memory.
  56. #define RTC_SLOW_ATTR _SECTION_ATTR_IMPL(".rtc.force_slow", __COUNTER__)
  57. // Allows to place data into RTC_FAST memory.
  58. #define RTC_FAST_ATTR _SECTION_ATTR_IMPL(".rtc.force_fast", __COUNTER__)
  59. // Forces data into noinit section to avoid initialization after restart.
  60. #define __NOINIT_ATTR _SECTION_ATTR_IMPL(".noinit", __COUNTER__)
  61. #if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY
  62. // Forces data into external memory noinit section to avoid initialization after restart.
  63. #define EXT_RAM_NOINIT_ATTR _SECTION_ATTR_IMPL(".ext_ram_noinit", __COUNTER__)
  64. #else
  65. // Place in internal noinit section
  66. #define EXT_RAM_NOINIT_ATTR __NOINIT_ATTR
  67. #endif
  68. // Forces data into RTC slow memory of .noinit section.
  69. // Any variable marked with this attribute will keep its value
  70. // after restart or during a deep sleep / wake cycle.
  71. #define RTC_NOINIT_ATTR _SECTION_ATTR_IMPL(".rtc_noinit", __COUNTER__)
  72. // Forces code into DRAM instead of flash and map it to coredump
  73. #define COREDUMP_DRAM_ATTR _SECTION_ATTR_IMPL(".dram1.coredump", __COUNTER__)
  74. // Forces data into RTC memory and map it to coredump
  75. #define COREDUMP_RTC_DATA_ATTR _SECTION_ATTR_IMPL(".rtc.coredump", __COUNTER__)
  76. // Allows to place data into RTC_FAST memory and map it to coredump
  77. #define COREDUMP_RTC_FAST_ATTR _SECTION_ATTR_IMPL(".rtc.fast.coredump", __COUNTER__)
  78. // Forces to not inline function
  79. #define NOINLINE_ATTR __attribute__((noinline))
  80. // This allows using enum as flags in C++
  81. // Format: FLAG_ATTR(flag_enum_t)
  82. #ifdef __cplusplus
  83. // Inline is required here to avoid multiple definition error in linker
  84. #define FLAG_ATTR_IMPL(TYPE, INT_TYPE) \
  85. FORCE_INLINE_ATTR constexpr TYPE operator~ (TYPE a) { return (TYPE)~(INT_TYPE)a; } \
  86. FORCE_INLINE_ATTR constexpr TYPE operator| (TYPE a, TYPE b) { return (TYPE)((INT_TYPE)a | (INT_TYPE)b); } \
  87. FORCE_INLINE_ATTR constexpr TYPE operator& (TYPE a, TYPE b) { return (TYPE)((INT_TYPE)a & (INT_TYPE)b); } \
  88. FORCE_INLINE_ATTR constexpr TYPE operator^ (TYPE a, TYPE b) { return (TYPE)((INT_TYPE)a ^ (INT_TYPE)b); } \
  89. FORCE_INLINE_ATTR constexpr TYPE operator>> (TYPE a, int b) { return (TYPE)((INT_TYPE)a >> b); } \
  90. FORCE_INLINE_ATTR constexpr TYPE operator<< (TYPE a, int b) { return (TYPE)((INT_TYPE)a << b); } \
  91. FORCE_INLINE_ATTR TYPE& operator|=(TYPE& a, TYPE b) { a = a | b; return a; } \
  92. FORCE_INLINE_ATTR TYPE& operator&=(TYPE& a, TYPE b) { a = a & b; return a; } \
  93. FORCE_INLINE_ATTR TYPE& operator^=(TYPE& a, TYPE b) { a = a ^ b; return a; } \
  94. FORCE_INLINE_ATTR TYPE& operator>>=(TYPE& a, int b) { a >>= b; return a; } \
  95. FORCE_INLINE_ATTR TYPE& operator<<=(TYPE& a, int b) { a <<= b; return a; }
  96. #define FLAG_ATTR_U32(TYPE) FLAG_ATTR_IMPL(TYPE, uint32_t)
  97. #define FLAG_ATTR FLAG_ATTR_U32
  98. #else
  99. #define FLAG_ATTR(TYPE)
  100. #endif
  101. // Implementation for a unique custom section
  102. //
  103. // This prevents gcc producing "x causes a section type conflict with y"
  104. // errors if two variables in the same source file have different linkage (maybe const & non-const) but are placed in the same custom section
  105. //
  106. // Using unique sections also means --gc-sections can remove unused
  107. // data with a custom section type set
  108. #define _SECTION_ATTR_IMPL(SECTION, COUNTER) __attribute__((section(SECTION "." _COUNTER_STRINGIFY(COUNTER))))
  109. #define _COUNTER_STRINGIFY(COUNTER) #COUNTER
  110. /* Use IDF_DEPRECATED attribute to mark anything deprecated from use in
  111. ESP-IDF's own source code, but not deprecated for external users.
  112. */
  113. #ifdef IDF_CI_BUILD
  114. #define IDF_DEPRECATED(REASON) __attribute__((deprecated(REASON)))
  115. #else
  116. #define IDF_DEPRECATED(REASON)
  117. #endif
  118. #ifdef __cplusplus
  119. }
  120. #endif
  121. #endif /* __ESP_ATTR_H__ */