nrf_section.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * Copyright (c) 2016 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_SECTION_H__
  41. #define NRF_SECTION_H__
  42. #include "nordic_common.h"
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup section_vars Section variables
  48. * @ingroup app_common
  49. * @{
  50. *
  51. * @brief Section variables.
  52. */
  53. //lint -save -e27 -esym(526,*)
  54. #if defined(__ICCARM__)
  55. // Enable IAR language extensions
  56. #pragma language=extended
  57. #endif
  58. /**@brief Macro for obtaining the address of the beginning of a section.
  59. *
  60. * param[in] section_name Name of the section.
  61. * @hideinitializer
  62. */
  63. #if defined(__CC_ARM)
  64. #define NRF_SECTION_START_ADDR(section_name) &CONCAT_2(section_name, $$Base)
  65. #elif defined(__GNUC__)
  66. #define NRF_SECTION_START_ADDR(section_name) &CONCAT_2(__start_, section_name)
  67. #elif defined(__ICCARM__)
  68. #define NRF_SECTION_START_ADDR(section_name) __section_begin(STRINGIFY(section_name))
  69. #endif
  70. /**@brief Macro for obtaining the address of the end of a section.
  71. *
  72. * @param[in] section_name Name of the section.
  73. * @hideinitializer
  74. */
  75. #if defined(__CC_ARM)
  76. #define NRF_SECTION_END_ADDR(section_name) &CONCAT_2(section_name, $$Limit)
  77. #elif defined(__GNUC__)
  78. #define NRF_SECTION_END_ADDR(section_name) &CONCAT_2(__stop_, section_name)
  79. #elif defined(__ICCARM__)
  80. #define NRF_SECTION_END_ADDR(section_name) __section_end(STRINGIFY(section_name))
  81. #endif
  82. /**@brief Macro for retrieving the length of a given section, in bytes.
  83. *
  84. * @param[in] section_name Name of the section.
  85. * @hideinitializer
  86. */
  87. #define NRF_SECTION_LENGTH(section_name) \
  88. ((size_t)NRF_SECTION_END_ADDR(section_name) - \
  89. (size_t)NRF_SECTION_START_ADDR(section_name))
  90. /**@brief Macro for creating a section.
  91. *
  92. * @param[in] section_name Name of the section.
  93. * @param[in] data_type Data type of the variables to be registered in the section.
  94. *
  95. * @warning Data type must be word aligned to prevent padding.
  96. * @hideinitializer
  97. */
  98. #if defined(__CC_ARM)
  99. #define NRF_SECTION_DEF(section_name, data_type) \
  100. extern data_type * CONCAT_2(section_name, $$Base); \
  101. extern void * CONCAT_2(section_name, $$Limit)
  102. #elif defined(__GNUC__)
  103. #define NRF_SECTION_DEF(section_name, data_type) \
  104. extern data_type * CONCAT_2(__start_, section_name); \
  105. extern void * CONCAT_2(__stop_, section_name)
  106. #elif defined(__ICCARM__)
  107. #define NRF_SECTION_DEF(section_name, data_type) \
  108. _Pragma(STRINGIFY(section = STRINGIFY(section_name)));
  109. #endif
  110. /**@brief Macro for declaring a variable and registering it in a section.
  111. *
  112. * @details Declares a variable and registers it in a named section. This macro ensures that the
  113. * variable is not stripped away when using optimizations.
  114. *
  115. * @note The order in which variables are placed in a section is dependent on the order in
  116. * which the linker script encounters the variables during linking.
  117. *
  118. * @param[in] section_name Name of the section.
  119. * @param[in] section_var Variable to register in the given section.
  120. * @hideinitializer
  121. */
  122. #if defined(__CC_ARM)
  123. #define NRF_SECTION_ITEM_REGISTER(section_name, section_var) \
  124. section_var __attribute__ ((section(STRINGIFY(section_name)))) __attribute__((used))
  125. #elif defined(__GNUC__)
  126. #define NRF_SECTION_ITEM_REGISTER(section_name, section_var) \
  127. section_var __attribute__ ((section("." STRINGIFY(section_name)))) __attribute__((used))
  128. #elif defined(__ICCARM__)
  129. #define NRF_SECTION_ITEM_REGISTER(section_name, section_var) \
  130. __root section_var @ STRINGIFY(section_name)
  131. #endif
  132. /**@brief Macro for retrieving a variable from a section.
  133. *
  134. * @warning The stored symbol can only be resolved using this macro if the
  135. * type of the data is word aligned. The operation of acquiring
  136. * the stored symbol relies on the size of the stored type. No
  137. * padding can exist in the named section in between individual
  138. * stored items or this macro will fail.
  139. *
  140. * @param[in] section_name Name of the section.
  141. * @param[in] data_type Data type of the variable.
  142. * @param[in] i Index of the variable in section.
  143. * @hideinitializer
  144. */
  145. #define NRF_SECTION_ITEM_GET(section_name, data_type, i) \
  146. ((data_type*)NRF_SECTION_START_ADDR(section_name) + (i))
  147. /**@brief Macro for getting the number of variables in a section.
  148. *
  149. * @param[in] section_name Name of the section.
  150. * @param[in] data_type Data type of the variables in the section.
  151. * @hideinitializer
  152. */
  153. #define NRF_SECTION_ITEM_COUNT(section_name, data_type) \
  154. NRF_SECTION_LENGTH(section_name) / sizeof(data_type)
  155. /** @} */
  156. //lint -restore
  157. #ifdef __cplusplus
  158. }
  159. #endif
  160. #endif // NRF_SECTION_H__