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 
25 #ifndef __CMSIS_COMPILER_H
26 #define __CMSIS_COMPILER_H
27 
28 #include <stdint.h>
29 
30 /*
31  * ARM Compiler 4/5
32  */
33 #if   defined ( __CC_ARM )
34   #include "cmsis_armcc.h"
35 
36 
37 /*
38  * ARM Compiler 6 (armclang)
39  */
40 #elif defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
41   #include "cmsis_armclang.h"
42 
43 
44 /*
45  * GNU Compiler
46  */
47 #elif defined ( __GNUC__ )
48   #include "cmsis_gcc.h"
49 
50 
51 /*
52  * IAR Compiler
53  */
54 #elif defined ( __ICCARM__ )
55 
56 
57   #ifndef   __ASM
58     #define __ASM                                  __asm
59   #endif
60   #ifndef   __INLINE
61     #define __INLINE                               inline
62   #endif
63   #ifndef   __STATIC_INLINE
64     #define __STATIC_INLINE                        static inline
65   #endif
66 
67   #include <cmsis_iar.h>
68 
69   #ifdef   __RESTRICT
70     #if defined(__cplusplus)
71       #undef __RESTRICT
72       #define __RESTRICT
73     #endif
74   #endif
75 
76   /* CMSIS compiler control architecture macros */
77   #if (__CORE__ == __ARM6M__) || (__CORE__ == __ARM6SM__)
78     #ifndef __ARM_ARCH_6M__
79       #define __ARM_ARCH_6M__                      1
80     #endif
81   #elif (__CORE__ == __ARM7M__)
82     #ifndef __ARM_ARCH_7M__
83       #define __ARM_ARCH_7M__                      1
84     #endif
85   #elif (__CORE__ == __ARM7EM__)
86     #ifndef __ARM_ARCH_7EM__
87       #define __ARM_ARCH_7EM__                     1
88     #endif
89   #endif
90 
91   #ifndef   __NO_RETURN
92     #define __NO_RETURN                            __noreturn
93   #endif
94   #ifndef   __USED
95     #define __USED                                 __root
96   #endif
97   #ifndef   __WEAK
98     #define __WEAK                                 __weak
99   #endif
100   #ifndef   __PACKED
101     #define __PACKED                               __packed
102   #endif
103   #ifndef   __PACKED_STRUCT
104     #define __PACKED_STRUCT                        __packed struct
105   #endif
106   #ifndef   __PACKED_UNION
107     #define __PACKED_UNION                         __packed union
108   #endif
109   #ifndef   __UNALIGNED_UINT32        /* deprecated */
110     __packed struct T_UINT32 { uint32_t v; };
111     #define __UNALIGNED_UINT32(x)                  (((struct T_UINT32 *)(x))->v)
112   #endif
113   #ifndef   __UNALIGNED_UINT16_WRITE
114     __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
115     #define __UNALIGNED_UINT16_WRITE(addr, val)    (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
116   #endif
117   #ifndef   __UNALIGNED_UINT16_READ
118     __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
119     #define __UNALIGNED_UINT16_READ(addr)          (((const struct T_UINT16_READ *)(const void *)(addr))->v)
120   #endif
121   #ifndef   __UNALIGNED_UINT32_WRITE
122     __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
123     #define __UNALIGNED_UINT32_WRITE(addr, val)    (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
124   #endif
125   #ifndef   __UNALIGNED_UINT32_READ
126     __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
127     #define __UNALIGNED_UINT32_READ(addr)          (((const struct T_UINT32_READ *)(const void *)(addr))->v)
128   #endif
129   #ifndef   __ALIGNED
130     #warning No compiler specific solution for __ALIGNED. __ALIGNED is ignored.
131     #define __ALIGNED(x)
132   #endif
133   #ifndef   __RESTRICT
134     #warning No compiler specific solution for __RESTRICT. __RESTRICT is ignored.
135     #define __RESTRICT
136   #endif
137 
138   // Workaround for missing __CLZ intrinsic in
139   // various versions of the IAR compilers.
140   // __IAR_FEATURE_CLZ__ should be defined by
141   // the compiler that supports __CLZ internally.
142   #if (defined (__ARM_ARCH_6M__)) && (__ARM_ARCH_6M__ == 1) && (!defined (__IAR_FEATURE_CLZ__))
__CLZ(uint32_t data)143     __STATIC_INLINE uint32_t __CLZ(uint32_t data)
144     {
145       if (data == 0u) { return 32u; }
146 
147       uint32_t count = 0;
148       uint32_t mask = 0x80000000;
149 
150       while ((data & mask) == 0)
151       {
152         count += 1u;
153         mask = mask >> 1u;
154       }
155 
156       return (count);
157     }
158   #endif
159 
160 
161 /*
162  * TI ARM Compiler
163  */
164 #elif defined ( __TI_ARM__ )
165   #include <cmsis_ccs.h>
166 
167   #ifndef   __ASM
168     #define __ASM                                  __asm
169   #endif
170   #ifndef   __INLINE
171     #define __INLINE                               inline
172   #endif
173   #ifndef   __STATIC_INLINE
174     #define __STATIC_INLINE                        static inline
175   #endif
176   #ifndef   __NO_RETURN
177     #define __NO_RETURN                            __attribute__((noreturn))
178   #endif
179   #ifndef   __USED
180     #define __USED                                 __attribute__((used))
181   #endif
182   #ifndef   __WEAK
183     #define __WEAK                                 __attribute__((weak))
184   #endif
185   #ifndef   __PACKED
186     #define __PACKED                               __attribute__((packed))
187   #endif
188   #ifndef   __PACKED_STRUCT
189     #define __PACKED_STRUCT                        struct __attribute__((packed))
190   #endif
191   #ifndef   __PACKED_UNION
192     #define __PACKED_UNION                         union __attribute__((packed))
193   #endif
194   #ifndef   __UNALIGNED_UINT32        /* deprecated */
195     struct __attribute__((packed)) T_UINT32 { uint32_t v; };
196     #define __UNALIGNED_UINT32(x)                  (((struct T_UINT32 *)(x))->v)
197   #endif
198   #ifndef   __UNALIGNED_UINT16_WRITE
199     __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
200     #define __UNALIGNED_UINT16_WRITE(addr, val)    (void)((((struct T_UINT16_WRITE *)(void*)(addr))->v) = (val))
201   #endif
202   #ifndef   __UNALIGNED_UINT16_READ
203     __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
204     #define __UNALIGNED_UINT16_READ(addr)          (((const struct T_UINT16_READ *)(const void *)(addr))->v)
205   #endif
206   #ifndef   __UNALIGNED_UINT32_WRITE
207     __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
208     #define __UNALIGNED_UINT32_WRITE(addr, val)    (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
209   #endif
210   #ifndef   __UNALIGNED_UINT32_READ
211     __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
212     #define __UNALIGNED_UINT32_READ(addr)          (((const struct T_UINT32_READ *)(const void *)(addr))->v)
213   #endif
214   #ifndef   __ALIGNED
215     #define __ALIGNED(x)                           __attribute__((aligned(x)))
216   #endif
217   #ifndef   __RESTRICT
218     #warning No compiler specific solution for __RESTRICT. __RESTRICT is ignored.
219     #define __RESTRICT
220   #endif
221 
222 
223 /*
224  * TASKING Compiler
225  */
226 #elif defined ( __TASKING__ )
227   /*
228    * The CMSIS functions have been implemented as intrinsics in the compiler.
229    * Please use "carm -?i" to get an up to date list of all intrinsics,
230    * Including the CMSIS ones.
231    */
232 
233   #ifndef   __ASM
234     #define __ASM                                  __asm
235   #endif
236   #ifndef   __INLINE
237     #define __INLINE                               inline
238   #endif
239   #ifndef   __STATIC_INLINE
240     #define __STATIC_INLINE                        static inline
241   #endif
242   #ifndef   __NO_RETURN
243     #define __NO_RETURN                            __attribute__((noreturn))
244   #endif
245   #ifndef   __USED
246     #define __USED                                 __attribute__((used))
247   #endif
248   #ifndef   __WEAK
249     #define __WEAK                                 __attribute__((weak))
250   #endif
251   #ifndef   __PACKED
252     #define __PACKED                               __packed__
253   #endif
254   #ifndef   __PACKED_STRUCT
255     #define __PACKED_STRUCT                        struct __packed__
256   #endif
257   #ifndef   __PACKED_UNION
258     #define __PACKED_UNION                         union __packed__
259   #endif
260   #ifndef   __UNALIGNED_UINT32        /* deprecated */
261     struct __packed__ T_UINT32 { uint32_t v; };
262     #define __UNALIGNED_UINT32(x)                  (((struct T_UINT32 *)(x))->v)
263   #endif
264   #ifndef   __UNALIGNED_UINT16_WRITE
265     __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
266     #define __UNALIGNED_UINT16_WRITE(addr, val)    (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
267   #endif
268   #ifndef   __UNALIGNED_UINT16_READ
269     __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
270     #define __UNALIGNED_UINT16_READ(addr)          (((const struct T_UINT16_READ *)(const void *)(addr))->v)
271   #endif
272   #ifndef   __UNALIGNED_UINT32_WRITE
273     __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
274     #define __UNALIGNED_UINT32_WRITE(addr, val)    (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
275   #endif
276   #ifndef   __UNALIGNED_UINT32_READ
277     __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
278     #define __UNALIGNED_UINT32_READ(addr)          (((const struct T_UINT32_READ *)(const void *)(addr))->v)
279   #endif
280   #ifndef   __ALIGNED
281     #define __ALIGNED(x)              __align(x)
282   #endif
283   #ifndef   __RESTRICT
284     #warning No compiler specific solution for __RESTRICT. __RESTRICT is ignored.
285     #define __RESTRICT
286   #endif
287 
288 
289 /*
290  * COSMIC Compiler
291  */
292 #elif defined ( __CSMC__ )
293    #include <cmsis_csm.h>
294 
295  #ifndef   __ASM
296     #define __ASM                                  _asm
297   #endif
298   #ifndef   __INLINE
299     #define __INLINE                               inline
300   #endif
301   #ifndef   __STATIC_INLINE
302     #define __STATIC_INLINE                        static inline
303   #endif
304   #ifndef   __NO_RETURN
305     // NO RETURN is automatically detected hence no warning here
306     #define __NO_RETURN
307   #endif
308   #ifndef   __USED
309     #warning No compiler specific solution for __USED. __USED is ignored.
310     #define __USED
311   #endif
312   #ifndef   __WEAK
313     #define __WEAK                                 __weak
314   #endif
315   #ifndef   __PACKED
316     #define __PACKED                               @packed
317   #endif
318   #ifndef   __PACKED_STRUCT
319     #define __PACKED_STRUCT                        @packed struct
320   #endif
321   #ifndef   __PACKED_UNION
322     #define __PACKED_UNION                         @packed union
323   #endif
324   #ifndef   __UNALIGNED_UINT32        /* deprecated */
325     @packed struct T_UINT32 { uint32_t v; };
326     #define __UNALIGNED_UINT32(x)                  (((struct T_UINT32 *)(x))->v)
327   #endif
328   #ifndef   __UNALIGNED_UINT16_WRITE
329     __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
330     #define __UNALIGNED_UINT16_WRITE(addr, val)    (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
331   #endif
332   #ifndef   __UNALIGNED_UINT16_READ
333     __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
334     #define __UNALIGNED_UINT16_READ(addr)          (((const struct T_UINT16_READ *)(const void *)(addr))->v)
335   #endif
336   #ifndef   __UNALIGNED_UINT32_WRITE
337     __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
338     #define __UNALIGNED_UINT32_WRITE(addr, val)    (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
339   #endif
340   #ifndef   __UNALIGNED_UINT32_READ
341     __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
342     #define __UNALIGNED_UINT32_READ(addr)          (((const struct T_UINT32_READ *)(const void *)(addr))->v)
343   #endif
344   #ifndef   __ALIGNED
345     #warning No compiler specific solution for __ALIGNED. __ALIGNED is ignored.
346     #define __ALIGNED(x)
347   #endif
348   #ifndef   __RESTRICT
349     #warning No compiler specific solution for __RESTRICT. __RESTRICT is ignored.
350     #define __RESTRICT
351   #endif
352 
353 
354 #else
355   #error Unknown compiler.
356 #endif
357 
358 
359 #endif /* __CMSIS_COMPILER_H */
360 
361