cmsis_compiler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /**************************************************************************//**
  2. * @file cmsis_compiler.h
  3. * @brief CMSIS compiler generic header file
  4. * @version V5.0.2
  5. * @date 13. February 2017
  6. ******************************************************************************/
  7. /*
  8. * Copyright (c) 2009-2017 ARM Limited. All rights reserved.
  9. *
  10. * SPDX-License-Identifier: Apache-2.0
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the License); you may
  13. * not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  20. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #ifndef __CMSIS_COMPILER_H
  25. #define __CMSIS_COMPILER_H
  26. #include <stdint.h>
  27. /*
  28. * ARM Compiler 4/5
  29. */
  30. #if defined ( __CC_ARM )
  31. #include "cmsis_armcc.h"
  32. /*
  33. * ARM Compiler 6 (armclang)
  34. */
  35. #elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
  36. #include "cmsis_armclang.h"
  37. /*
  38. * GNU Compiler
  39. */
  40. #elif defined ( __GNUC__ )
  41. #include "cmsis_gcc.h"
  42. /*
  43. * IAR Compiler
  44. */
  45. #elif defined ( __ICCARM__ )
  46. #ifndef __ASM
  47. #define __ASM __asm
  48. #endif
  49. #ifndef __INLINE
  50. #define __INLINE inline
  51. #endif
  52. #ifndef __STATIC_INLINE
  53. #define __STATIC_INLINE static inline
  54. #endif
  55. #include <cmsis_iar.h>
  56. /* CMSIS compiler control architecture macros */
  57. #if (__CORE__ == __ARM6M__) || (__CORE__ == __ARM6SM__)
  58. #ifndef __ARM_ARCH_6M__
  59. #define __ARM_ARCH_6M__ 1
  60. #endif
  61. #elif (__CORE__ == __ARM7M__)
  62. #ifndef __ARM_ARCH_7M__
  63. #define __ARM_ARCH_7M__ 1
  64. #endif
  65. #elif (__CORE__ == __ARM7EM__)
  66. #ifndef __ARM_ARCH_7EM__
  67. #define __ARM_ARCH_7EM__ 1
  68. #endif
  69. #endif
  70. #ifndef __NO_RETURN
  71. #define __NO_RETURN __noreturn
  72. #endif
  73. #ifndef __USED
  74. #define __USED __root
  75. #endif
  76. #ifndef __WEAK
  77. #define __WEAK __weak
  78. #endif
  79. #ifndef __PACKED
  80. #define __PACKED __packed
  81. #endif
  82. #ifndef __PACKED_STRUCT
  83. #define __PACKED_STRUCT __packed struct
  84. #endif
  85. #ifndef __PACKED_UNION
  86. #define __PACKED_UNION __packed union
  87. #endif
  88. #ifndef __UNALIGNED_UINT32 /* deprecated */
  89. __packed struct T_UINT32 { uint32_t v; };
  90. #define __UNALIGNED_UINT32(x) (((struct T_UINT32 *)(x))->v)
  91. #endif
  92. #ifndef __UNALIGNED_UINT16_WRITE
  93. __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
  94. #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
  95. #endif
  96. #ifndef __UNALIGNED_UINT16_READ
  97. __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
  98. #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v)
  99. #endif
  100. #ifndef __UNALIGNED_UINT32_WRITE
  101. __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
  102. #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
  103. #endif
  104. #ifndef __UNALIGNED_UINT32_READ
  105. __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
  106. #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
  107. #endif
  108. #ifndef __ALIGNED
  109. #warning No compiler specific solution for __ALIGNED. __ALIGNED is ignored.
  110. #define __ALIGNED(x)
  111. #endif
  112. // Workaround for missing __CLZ intrinsic in
  113. // various versions of the IAR compilers.
  114. // __IAR_FEATURE_CLZ__ should be defined by
  115. // the compiler that supports __CLZ internally.
  116. #if (defined (__ARM_ARCH_6M__)) && (__ARM_ARCH_6M__ == 1) && (!defined (__IAR_FEATURE_CLZ__))
  117. __STATIC_INLINE uint32_t __CLZ(uint32_t data)
  118. {
  119. if (data == 0u) { return 32u; }
  120. uint32_t count = 0;
  121. uint32_t mask = 0x80000000;
  122. while ((data & mask) == 0)
  123. {
  124. count += 1u;
  125. mask = mask >> 1u;
  126. }
  127. return (count);
  128. }
  129. #endif
  130. /*
  131. * TI ARM Compiler
  132. */
  133. #elif defined ( __TI_ARM__ )
  134. #include <cmsis_ccs.h>
  135. #ifndef __ASM
  136. #define __ASM __asm
  137. #endif
  138. #ifndef __INLINE
  139. #define __INLINE inline
  140. #endif
  141. #ifndef __STATIC_INLINE
  142. #define __STATIC_INLINE static inline
  143. #endif
  144. #ifndef __NO_RETURN
  145. #define __NO_RETURN __attribute__((noreturn))
  146. #endif
  147. #ifndef __USED
  148. #define __USED __attribute__((used))
  149. #endif
  150. #ifndef __WEAK
  151. #define __WEAK __attribute__((weak))
  152. #endif
  153. #ifndef __PACKED
  154. #define __PACKED __attribute__((packed))
  155. #endif
  156. #ifndef __PACKED_STRUCT
  157. #define __PACKED_STRUCT struct __attribute__((packed))
  158. #endif
  159. #ifndef __PACKED_UNION
  160. #define __PACKED_UNION union __attribute__((packed))
  161. #endif
  162. #ifndef __UNALIGNED_UINT32 /* deprecated */
  163. struct __attribute__((packed)) T_UINT32 { uint32_t v; };
  164. #define __UNALIGNED_UINT32(x) (((struct T_UINT32 *)(x))->v)
  165. #endif
  166. #ifndef __UNALIGNED_UINT16_WRITE
  167. __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
  168. #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void*)(addr))->v) = (val))
  169. #endif
  170. #ifndef __UNALIGNED_UINT16_READ
  171. __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
  172. #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v)
  173. #endif
  174. #ifndef __UNALIGNED_UINT32_WRITE
  175. __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
  176. #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
  177. #endif
  178. #ifndef __UNALIGNED_UINT32_READ
  179. __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
  180. #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
  181. #endif
  182. #ifndef __ALIGNED
  183. #define __ALIGNED(x) __attribute__((aligned(x)))
  184. #endif
  185. /*
  186. * TASKING Compiler
  187. */
  188. #elif defined ( __TASKING__ )
  189. /*
  190. * The CMSIS functions have been implemented as intrinsics in the compiler.
  191. * Please use "carm -?i" to get an up to date list of all intrinsics,
  192. * Including the CMSIS ones.
  193. */
  194. #ifndef __ASM
  195. #define __ASM __asm
  196. #endif
  197. #ifndef __INLINE
  198. #define __INLINE inline
  199. #endif
  200. #ifndef __STATIC_INLINE
  201. #define __STATIC_INLINE static inline
  202. #endif
  203. #ifndef __NO_RETURN
  204. #define __NO_RETURN __attribute__((noreturn))
  205. #endif
  206. #ifndef __USED
  207. #define __USED __attribute__((used))
  208. #endif
  209. #ifndef __WEAK
  210. #define __WEAK __attribute__((weak))
  211. #endif
  212. #ifndef __PACKED
  213. #define __PACKED __packed__
  214. #endif
  215. #ifndef __PACKED_STRUCT
  216. #define __PACKED_STRUCT struct __packed__
  217. #endif
  218. #ifndef __PACKED_UNION
  219. #define __PACKED_UNION union __packed__
  220. #endif
  221. #ifndef __UNALIGNED_UINT32 /* deprecated */
  222. struct __packed__ T_UINT32 { uint32_t v; };
  223. #define __UNALIGNED_UINT32(x) (((struct T_UINT32 *)(x))->v)
  224. #endif
  225. #ifndef __UNALIGNED_UINT16_WRITE
  226. __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
  227. #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
  228. #endif
  229. #ifndef __UNALIGNED_UINT16_READ
  230. __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
  231. #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v)
  232. #endif
  233. #ifndef __UNALIGNED_UINT32_WRITE
  234. __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
  235. #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
  236. #endif
  237. #ifndef __UNALIGNED_UINT32_READ
  238. __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
  239. #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
  240. #endif
  241. #ifndef __ALIGNED
  242. #define __ALIGNED(x) __align(x)
  243. #endif
  244. /*
  245. * COSMIC Compiler
  246. */
  247. #elif defined ( __CSMC__ )
  248. #include <cmsis_csm.h>
  249. #ifndef __ASM
  250. #define __ASM _asm
  251. #endif
  252. #ifndef __INLINE
  253. #define __INLINE inline
  254. #endif
  255. #ifndef __STATIC_INLINE
  256. #define __STATIC_INLINE static inline
  257. #endif
  258. #ifndef __NO_RETURN
  259. // NO RETURN is automatically detected hence no warning here
  260. #define __NO_RETURN
  261. #endif
  262. #ifndef __USED
  263. #warning No compiler specific solution for __USED. __USED is ignored.
  264. #define __USED
  265. #endif
  266. #ifndef __WEAK
  267. #define __WEAK __weak
  268. #endif
  269. #ifndef __PACKED
  270. #define __PACKED @packed
  271. #endif
  272. #ifndef __PACKED_STRUCT
  273. #define __PACKED_STRUCT @packed struct
  274. #endif
  275. #ifndef __PACKED_UNION
  276. #define __PACKED_UNION @packed union
  277. #endif
  278. #ifndef __UNALIGNED_UINT32 /* deprecated */
  279. @packed struct T_UINT32 { uint32_t v; };
  280. #define __UNALIGNED_UINT32(x) (((struct T_UINT32 *)(x))->v)
  281. #endif
  282. #ifndef __UNALIGNED_UINT16_WRITE
  283. __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
  284. #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
  285. #endif
  286. #ifndef __UNALIGNED_UINT16_READ
  287. __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
  288. #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v)
  289. #endif
  290. #ifndef __UNALIGNED_UINT32_WRITE
  291. __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
  292. #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
  293. #endif
  294. #ifndef __UNALIGNED_UINT32_READ
  295. __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
  296. #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
  297. #endif
  298. #ifndef __ALIGNED
  299. #warning No compiler specific solution for __ALIGNED. __ALIGNED is ignored.
  300. #define __ALIGNED(x)
  301. #endif
  302. #else
  303. #error Unknown compiler.
  304. #endif
  305. #endif /* __CMSIS_COMPILER_H */