section_vars.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* Copyright (c) 2016 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #ifndef SECTION_VARS_H__
  13. #define SECTION_VARS_H__
  14. /**
  15. * @defgroup section_vars Section variables
  16. * @ingroup app_common
  17. * @{
  18. *
  19. * @brief Section variables.
  20. */
  21. #if defined(__ICCARM__)
  22. // Enable IAR language extensions
  23. #pragma language=extended
  24. #endif
  25. // Macro to delay macro expansion.
  26. #define NRF_PRAGMA(x) _Pragma(#x)
  27. /**@brief Macro to register a named section.
  28. *
  29. * @param[in] section_name Name of the section to register.
  30. */
  31. #if defined(__CC_ARM)
  32. // Not required by this compiler.
  33. #define NRF_SECTION_VARS_REGISTER_SECTION(section_name)
  34. #elif defined(__GNUC__)
  35. // Not required by this compiler.
  36. #define NRF_SECTION_VARS_REGISTER_SECTION(section_name)
  37. #elif defined(__ICCARM__)
  38. #define NRF_SECTION_VARS_REGISTER_SECTION(section_name) NRF_PRAGMA(section = #section_name)
  39. #endif
  40. /*lint -save -e27 */
  41. /**@brief Macro to obtain the linker symbol for the beginning of a given section.
  42. *
  43. * @details The symbol that this macro resolves to is used to obtain a section start address.
  44. *
  45. * @param[in] section_name Name of the section.
  46. */
  47. #if defined(__CC_ARM)
  48. #define NRF_SECTION_VARS_START_SYMBOL(section_name) section_name ## $$Base
  49. #elif defined(__GNUC__)
  50. #define NRF_SECTION_VARS_START_SYMBOL(section_name) __start_ ## section_name
  51. #elif defined(__ICCARM__)
  52. #define NRF_SECTION_VARS_START_SYMBOL(section_name) __section_begin(#section_name)
  53. #endif
  54. /**@brief Macro to obtain the linker symbol for the end of a given section.
  55. *
  56. * @details The symbol that this macro resolves to is used to obtain a section end address.
  57. *
  58. * @param[in] section_name Name of the section.
  59. */
  60. #if defined(__CC_ARM)
  61. #define NRF_SECTION_VARS_END_SYMBOL(section_name) section_name ## $$Limit
  62. #elif defined(__GNUC__)
  63. #define NRF_SECTION_VARS_END_SYMBOL(section_name) __stop_ ## section_name
  64. #elif defined(__ICCARM__)
  65. #define NRF_SECTION_VARS_END_SYMBOL(section_name) __section_end(#section_name)
  66. #endif
  67. /*lint -restore */
  68. /**@brief Macro for retrieving the length of a given section, in bytes.
  69. *
  70. * @param[in] section_name Name of the section.
  71. */
  72. #if defined(__CC_ARM)
  73. #define NRF_SECTION_VARS_LENGTH(section_name) \
  74. ((uint32_t)&NRF_SECTION_VARS_END_SYMBOL(section_name) - (uint32_t)&NRF_SECTION_VARS_START_SYMBOL(section_name))
  75. #elif defined(__GNUC__)
  76. #define NRF_SECTION_VARS_LENGTH(section_name) \
  77. ((uint32_t)&NRF_SECTION_VARS_END_SYMBOL(section_name) - (uint32_t)&NRF_SECTION_VARS_START_SYMBOL(section_name))
  78. #elif defined(__ICCARM__)
  79. #define NRF_SECTION_VARS_LENGTH(section_name) \
  80. ((uint32_t)NRF_SECTION_VARS_END_SYMBOL(section_name) - (uint32_t)NRF_SECTION_VARS_START_SYMBOL(section_name))
  81. #endif
  82. /**@brief Macro to obtain the start address of a named section.
  83. *
  84. * param[in] section_name Name of the section.
  85. */
  86. #if defined(__CC_ARM)
  87. #define NRF_SECTION_VARS_START_ADDR(section_name) (uint32_t)&NRF_SECTION_VARS_START_SYMBOL(section_name)
  88. #elif defined(__GNUC__)
  89. #define NRF_SECTION_VARS_START_ADDR(section_name) (uint32_t)&NRF_SECTION_VARS_START_SYMBOL(section_name)
  90. #elif defined(__ICCARM__)
  91. #define NRF_SECTION_VARS_START_ADDR(section_name) (uint32_t)iar_ ## section_name ## _start
  92. #endif
  93. /*@brief Macro to obtain the end address of a named section.
  94. *
  95. * @param[in] section_name Name of the section.
  96. */
  97. #if defined(__CC_ARM)
  98. #define NRF_SECTION_VARS_END_ADDR(section_name) (uint32_t)&NRF_SECTION_VARS_END_SYMBOL(section_name)
  99. #elif defined(__GNUC__)
  100. #define NRF_SECTION_VARS_END_ADDR(section_name) (uint32_t)&NRF_SECTION_VARS_END_SYMBOL(section_name)
  101. #elif defined(__ICCARM__)
  102. #define NRF_SECTION_VARS_END_ADDR(section_name) (uint32_t)iar_ ## section_name ## _end
  103. #endif
  104. /**@brief Macro to extern a named section start and stop symbols.
  105. *
  106. * @note These declarations are required for GCC and Keil linkers (but not for IAR's).
  107. *
  108. * @param[in] type_name Name of the type stored in the section.
  109. * @param[in] section_name Name of the section.
  110. */
  111. #if defined(__CC_ARM)
  112. #define NRF_SECTION_VARS_REGISTER_SYMBOLS(type_name, section_name) \
  113. extern type_name * NRF_SECTION_VARS_START_SYMBOL(section_name); \
  114. extern void * NRF_SECTION_VARS_END_SYMBOL(section_name)
  115. #elif defined(__GNUC__)
  116. #define NRF_SECTION_VARS_REGISTER_SYMBOLS(type_name, section_name) \
  117. extern type_name * NRF_SECTION_VARS_START_SYMBOL(section_name); \
  118. extern void * NRF_SECTION_VARS_END_SYMBOL(section_name)
  119. #elif defined(__ICCARM__)
  120. // No symbol registration required for IAR.
  121. #define NRF_SECTION_VARS_REGISTER_SYMBOLS(type_name, section_name) \
  122. extern void * iar_ ## section_name ## _start = __section_begin(#section_name); \
  123. extern void * iar_ ## section_name ## _end = __section_end(#section_name)
  124. #endif
  125. /**@brief Macro to declare a variable to be placed in a named section.
  126. *
  127. * @details Declares a variable to be placed in a named section. This macro ensures that its symbol
  128. * is not stripped by the linker because of optimizations.
  129. *
  130. * @warning The order with which variables are placed in a section is implementation dependant.
  131. * Generally, variables are placed in a section depending on the order with which they
  132. * are found by the linker.
  133. *
  134. * @warning The symbols added in the named section must be word aligned to prevent padding.
  135. *
  136. * @param[in] section_name Name of the section.
  137. * @param[in] type_def Datatype of the variable to place in the given section.
  138. */
  139. #if defined(__CC_ARM)
  140. #define NRF_SECTION_VARS_ADD(section_name, type_def) \
  141. static type_def __attribute__ ((section(#section_name))) __attribute__((used))
  142. #elif defined(__GNUC__)
  143. #define NRF_SECTION_VARS_ADD(section_name, type_def) \
  144. static type_def __attribute__ ((section("."#section_name))) __attribute__((used))
  145. #elif defined(__ICCARM__)
  146. #define NRF_SECTION_VARS_ADD(section_name, type_def) \
  147. __root type_def @ #section_name
  148. #endif
  149. /**@brief Macro to get symbol from named section.
  150. *
  151. * @warning The stored symbol can only be resolved using this macro if the
  152. * type of the data is word aligned. The operation of acquiring
  153. * the stored symbol relies on sizeof of the stored type, no
  154. * padding can exist in the named section in between individual
  155. * stored items or this macro will fail.
  156. *
  157. * @param[in] i Index of item in section.
  158. * @param[in] type_name Type name of item in section.
  159. * @param[in] section_name Name of the section.
  160. */
  161. #if defined(__CC_ARM)
  162. #define NRF_SECTION_VARS_GET(i, type_name, section_name) \
  163. (type_name*)(NRF_SECTION_VARS_START_ADDR(section_name) + i * sizeof(type_name))
  164. #elif defined(__GNUC__)
  165. #define NRF_SECTION_VARS_GET(i, type_name, section_name) \
  166. (type_name*)(NRF_SECTION_VARS_START_ADDR(section_name) + i * sizeof(type_name))
  167. #elif defined(__ICCARM__)
  168. #define NRF_SECTION_VARS_GET(i, type_name, section_name) \
  169. (type_name*)(NRF_SECTION_VARS_START_ADDR(section_name) + i * sizeof(type_name))
  170. #endif
  171. /**@brief Macro to get number of items in named section.
  172. *
  173. * @param[in] type_name Type name of item in section.
  174. * @param[in] section_name Name of the section.
  175. */
  176. #define NRF_SECTION_VARS_COUNT(type_name, section_name) \
  177. NRF_SECTION_VARS_LENGTH(section_name) / sizeof(type_name)
  178. /** @} */
  179. #endif // SECTION_VARS_H__