esp_attr.h 5.1 KB

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