compiler.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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-2022 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. /**
  20. * @brief This is the section dedicated to IAR toolchain
  21. */
  22. #if defined(__ICCARM__) || defined(__IAR_SYSTEMS_ASM__)
  23. #ifndef __WEAK
  24. #define __WEAK __weak
  25. #endif
  26. #define QUOTE_(a) #a
  27. /**
  28. * @brief PACKED
  29. * Use the PACKED macro for variables that needs to be packed.
  30. * Usage: PACKED(struct) myStruct_s
  31. * PACKED(union) myStruct_s
  32. */
  33. #define PACKED(decl) __packed decl
  34. /**
  35. * @brief SECTION
  36. * Use the SECTION macro to assign data or code in a specific section.
  37. * Usage: SECTION(".my_section")
  38. */
  39. #define SECTION(name) _Pragma(QUOTE_(location = name))
  40. /**
  41. * @brief ALIGN_DEF
  42. * Use the ALIGN_DEF macro to specify the alignment of a variable.
  43. * Usage: ALIGN_DEF(4)
  44. */
  45. #define ALIGN_DEF(v) _Pragma(QUOTE_(data_alignment = v))
  46. /**
  47. * @brief NO_INIT
  48. * Use the NO_INIT macro to declare a not initialized variable.
  49. * Usage: NO_INIT(int my_no_init_var)
  50. * Usage: NO_INIT(uint16_t my_no_init_array[10])
  51. */
  52. #define NO_INIT(var) __no_init var
  53. /**
  54. * @brief This is the section dedicated to GNU toolchain
  55. */
  56. #else
  57. #ifdef __GNUC__
  58. #ifndef __WEAK
  59. #define __WEAK __attribute__((weak))
  60. #endif
  61. /**
  62. * @brief PACKED
  63. * Use the PACKED macro for variables that needs to be packed.
  64. * Usage: PACKED(struct) myStruct_s
  65. * PACKED(union) myStruct_s
  66. */
  67. #define PACKED(decl) decl __attribute__((packed))
  68. /**
  69. * @brief SECTION
  70. * Use the SECTION macro to assign data or code in a specific section.
  71. * Usage: SECTION(".my_section")
  72. */
  73. #define SECTION(name) __attribute__((section(name)))
  74. /**
  75. * @brief ALIGN_DEF
  76. * Use the ALIGN_DEF macro to specify the alignment of a variable.
  77. * Usage: ALIGN_DEF(4)
  78. */
  79. #define ALIGN_DEF(N) __attribute__((aligned(N)))
  80. /**
  81. * @brief NO_INIT
  82. * Use the NO_INIT macro to declare a not initialized variable.
  83. * Usage: NO_INIT(int my_no_init_var)
  84. * Usage: NO_INIT(uint16_t my_no_init_array[10])
  85. */
  86. #define NO_INIT(var) var __attribute__((section(".noinit")))
  87. /**
  88. * @brief This is the section dedicated to Keil toolchain
  89. */
  90. #else
  91. #ifdef __CC_ARM
  92. #ifndef __WEAK
  93. #define __WEAK __weak
  94. #endif
  95. /**
  96. * @brief PACKED
  97. * Use the PACKED macro for variables that needs to be packed.
  98. * Usage: PACKED(struct) myStruct_s
  99. * PACKED(union) myStruct_s
  100. */
  101. #define PACKED(decl) decl __attribute__((packed))
  102. /**
  103. * @brief SECTION
  104. * Use the SECTION macro to assign data or code in a specific section.
  105. * Usage: SECTION(".my_section")
  106. */
  107. #define SECTION(name) __attribute__((section(name)))
  108. /**
  109. * @brief ALIGN_DEF
  110. * Use the ALIGN_DEF macro to specify the alignment of a variable.
  111. * Usage: ALIGN_DEF(4)
  112. */
  113. #define ALIGN_DEF(N) __attribute__((aligned(N)))
  114. /**
  115. * @brief NO_INIT
  116. * Use the NO_INIT macro to declare a not initialized variable.
  117. * Usage: NO_INIT(int my_no_init_var)
  118. * Usage: NO_INIT(uint16_t my_no_init_array[10])
  119. */
  120. #define NO_INIT(var) var __attribute__((section("NoInit")))
  121. #else
  122. #error Neither ICCARM, CC ARM nor GNUC C detected. Define your macros.
  123. #endif
  124. #endif
  125. #endif
  126. #endif /* COMPILER_H__ */