compiler.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*****************************************************************************
  2. * @file compiler.h
  3. * @author MDG
  4. * @brief This file contains the definitions which are compiler dependent.
  5. *****************************************************************************
  6. * @attention
  7. *
  8. * Copyright (c) 2018-2023 STMicroelectronics.
  9. * All rights reserved.
  10. *
  11. * This software is licensed under terms that can be found in the LICENSE file
  12. * in the root directory of this software component.
  13. * If no LICENSE file comes with this software, it is provided AS-IS.
  14. *
  15. *****************************************************************************
  16. */
  17. #ifndef COMPILER_H__
  18. #define COMPILER_H__
  19. #ifndef __PACKED_STRUCT
  20. #define __PACKED_STRUCT PACKED(struct)
  21. #endif
  22. #ifndef __PACKED_UNION
  23. #define __PACKED_UNION PACKED(union)
  24. #endif
  25. /**
  26. * @brief This is the section dedicated to IAR toolchain
  27. */
  28. #if defined(__ICCARM__) || defined(__IAR_SYSTEMS_ASM__)
  29. #ifndef __WEAK
  30. #define __WEAK __weak
  31. #endif
  32. #define QUOTE_(a) #a
  33. /**
  34. * @brief PACKED
  35. * Use the PACKED macro for variables that needs to be packed.
  36. * Usage: PACKED(struct) myStruct_s
  37. * PACKED(union) myStruct_s
  38. */
  39. #define PACKED(decl) __packed decl
  40. /**
  41. * @brief SECTION
  42. * Use the SECTION macro to assign data or code in a specific section.
  43. * Usage: SECTION(".my_section")
  44. */
  45. #define SECTION(name) _Pragma(QUOTE_(location = name))
  46. /**
  47. * @brief ALIGN_DEF
  48. * Use the ALIGN_DEF macro to specify the alignment of a variable.
  49. * Usage: ALIGN_DEF(4)
  50. */
  51. #define ALIGN_DEF(v) _Pragma(QUOTE_(data_alignment = v))
  52. /**
  53. * @brief NO_INIT
  54. * Use the NO_INIT macro to declare a not initialized variable.
  55. * Usage: NO_INIT(int my_no_init_var)
  56. * Usage: NO_INIT(uint16_t my_no_init_array[10])
  57. */
  58. #define NO_INIT(var) __no_init var
  59. /**
  60. * @brief This is the section dedicated to GNU toolchain
  61. */
  62. #else
  63. #ifdef __GNUC__
  64. #ifndef __WEAK
  65. #define __WEAK __attribute__((weak))
  66. #endif
  67. /**
  68. * @brief PACKED
  69. * Use the PACKED macro for variables that needs to be packed.
  70. * Usage: PACKED(struct) myStruct_s
  71. * PACKED(union) myStruct_s
  72. */
  73. #define PACKED(decl) decl __attribute__((packed))
  74. /**
  75. * @brief SECTION
  76. * Use the SECTION macro to assign data or code in a specific section.
  77. * Usage: SECTION(".my_section")
  78. */
  79. #define SECTION(name) __attribute__((section(name)))
  80. /**
  81. * @brief ALIGN_DEF
  82. * Use the ALIGN_DEF macro to specify the alignment of a variable.
  83. * Usage: ALIGN_DEF(4)
  84. */
  85. #define ALIGN_DEF(N) __attribute__((aligned(N)))
  86. /**
  87. * @brief NO_INIT
  88. * Use the NO_INIT macro to declare a not initialized variable.
  89. * Usage: NO_INIT(int my_no_init_var)
  90. * Usage: NO_INIT(uint16_t my_no_init_array[10])
  91. */
  92. #define NO_INIT(var) var __attribute__((section(".noinit")))
  93. /**
  94. * @brief This is the section dedicated to Keil toolchain
  95. */
  96. #else
  97. #ifdef __CC_ARM
  98. #ifndef __WEAK
  99. #define __WEAK __weak
  100. #endif
  101. /**
  102. * @brief PACKED
  103. * Use the PACKED macro for variables that needs to be packed.
  104. * Usage: PACKED(struct) myStruct_s
  105. * PACKED(union) myStruct_s
  106. */
  107. #define PACKED(decl) decl __attribute__((packed))
  108. /**
  109. * @brief SECTION
  110. * Use the SECTION macro to assign data or code in a specific section.
  111. * Usage: SECTION(".my_section")
  112. */
  113. #define SECTION(name) __attribute__((section(name)))
  114. /**
  115. * @brief ALIGN_DEF
  116. * Use the ALIGN_DEF macro to specify the alignment of a variable.
  117. * Usage: ALIGN_DEF(4)
  118. */
  119. #define ALIGN_DEF(N) __attribute__((aligned(N)))
  120. /**
  121. * @brief NO_INIT
  122. * Use the NO_INIT macro to declare a not initialized variable.
  123. * Usage: NO_INIT(int my_no_init_var)
  124. * Usage: NO_INIT(uint16_t my_no_init_array[10])
  125. */
  126. #define NO_INIT(var) var __attribute__((section("NoInit")))
  127. #else
  128. #error Neither ICCARM, CC ARM nor GNUC C detected. Define your macros.
  129. #endif
  130. #endif
  131. #endif
  132. #endif /* COMPILER_H__ */