]> begriffs open source - cmsis/blob - CMSIS/Core_A/Include/cmsis_armclang.h
rtos: rtx5: ARMCC5: move the variables in .bss.os section to ZI section
[cmsis] / CMSIS / Core_A / Include / cmsis_armclang.h
1 /**************************************************************************//**
2  * @file     cmsis_armclang.h
3  * @brief    CMSIS compiler specific macros, functions, instructions
4  * @version  V1.0.2
5  * @date     10. January 2018
6  ******************************************************************************/
7 /*
8  * Copyright (c) 2009-2018 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_ARMCLANG_H
26 #define __CMSIS_ARMCLANG_H
27
28 #pragma clang system_header   /* treat file as system include file */
29
30 #ifndef __ARM_COMPAT_H
31 #include <arm_compat.h>    /* Compatibility header for Arm Compiler 5 intrinsics */
32 #endif
33
34 /* CMSIS compiler specific defines */
35 #ifndef   __ASM
36   #define __ASM                                  __asm
37 #endif
38 #ifndef   __INLINE
39   #define __INLINE                               __inline
40 #endif
41 #ifndef   __FORCEINLINE
42   #define __FORCEINLINE                          __attribute__((always_inline))
43 #endif
44 #ifndef   __STATIC_INLINE
45   #define __STATIC_INLINE                        static __inline
46 #endif
47 #ifndef   __STATIC_FORCEINLINE
48   #define __STATIC_FORCEINLINE                   __attribute__((always_inline)) static __inline
49 #endif
50 #ifndef   __NO_RETURN
51   #define __NO_RETURN                            __attribute__((__noreturn__))
52 #endif
53 #ifndef   CMSIS_DEPRECATED
54   #define CMSIS_DEPRECATED                       __attribute__((deprecated))
55 #endif
56 #ifndef   __USED
57   #define __USED                                 __attribute__((used))
58 #endif
59 #ifndef   __WEAK
60   #define __WEAK                                 __attribute__((weak))
61 #endif
62 #ifndef   __PACKED
63   #define __PACKED                               __attribute__((packed, aligned(1)))
64 #endif
65 #ifndef   __PACKED_STRUCT
66   #define __PACKED_STRUCT                        struct __attribute__((packed, aligned(1)))
67 #endif
68 #ifndef   __UNALIGNED_UINT16_WRITE
69   #pragma clang diagnostic push
70   #pragma clang diagnostic ignored "-Wpacked"
71 /*lint -esym(9058, T_UINT16_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_WRITE */
72   __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
73   #pragma clang diagnostic pop
74   #define __UNALIGNED_UINT16_WRITE(addr, val)    (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
75 #endif
76 #ifndef   __UNALIGNED_UINT16_READ
77   #pragma clang diagnostic push
78   #pragma clang diagnostic ignored "-Wpacked"
79 /*lint -esym(9058, T_UINT16_READ)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_READ */
80   __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
81   #pragma clang diagnostic pop
82   #define __UNALIGNED_UINT16_READ(addr)          (((const struct T_UINT16_READ *)(const void *)(addr))->v)
83 #endif
84 #ifndef   __UNALIGNED_UINT32_WRITE
85   #pragma clang diagnostic push
86   #pragma clang diagnostic ignored "-Wpacked"
87 /*lint -esym(9058, T_UINT32_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT32_WRITE */
88   __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
89   #pragma clang diagnostic pop
90   #define __UNALIGNED_UINT32_WRITE(addr, val)    (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
91 #endif
92 #ifndef   __UNALIGNED_UINT32_READ
93   #pragma clang diagnostic push
94   #pragma clang diagnostic ignored "-Wpacked"
95   __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
96   #pragma clang diagnostic pop
97   #define __UNALIGNED_UINT32_READ(addr)          (((const struct T_UINT32_READ *)(const void *)(addr))->v)
98 #endif
99 #ifndef   __ALIGNED
100   #define __ALIGNED(x)                           __attribute__((aligned(x)))
101 #endif
102 #ifndef   __PACKED
103   #define __PACKED                               __attribute__((packed))
104 #endif
105 #ifndef   __SECTION_ZERO_INIT
106   #define __SECTION_ZERO_INIT(name)              __attribute__((section(name)))
107 #endif
108
109 /* ##########################  Core Instruction Access  ######################### */
110 /**
111   \brief   No Operation
112  */
113 #define __NOP                             __builtin_arm_nop
114
115 /**
116   \brief   Wait For Interrupt
117  */
118 #define __WFI                             __builtin_arm_wfi
119
120 /**
121   \brief   Wait For Event
122  */
123 #define __WFE                             __builtin_arm_wfe
124
125 /**
126   \brief   Send Event
127  */
128 #define __SEV                             __builtin_arm_sev
129
130 /**
131   \brief   Instruction Synchronization Barrier
132  */
133 #define __ISB() do {\
134                    __schedule_barrier();\
135                    __builtin_arm_isb(0xF);\
136                    __schedule_barrier();\
137                 } while (0U)
138
139 /**
140   \brief   Data Synchronization Barrier
141  */
142 #define __DSB() do {\
143                    __schedule_barrier();\
144                    __builtin_arm_dsb(0xF);\
145                    __schedule_barrier();\
146                 } while (0U)
147
148 /**
149   \brief   Data Memory Barrier
150  */
151 #define __DMB() do {\
152                    __schedule_barrier();\
153                    __builtin_arm_dmb(0xF);\
154                    __schedule_barrier();\
155                 } while (0U)
156
157 /**
158   \brief   Reverse byte order (32 bit)
159   \details Reverses the byte order in unsigned integer value. For example, 0x12345678 becomes 0x78563412.
160   \param [in]    value  Value to reverse
161   \return               Reversed value
162  */
163 #define __REV(value)   __builtin_bswap32(value)
164
165 /**
166   \brief   Reverse byte order (16 bit)
167   \details Reverses the byte order within each halfword of a word. For example, 0x12345678 becomes 0x34127856.
168   \param [in]    value  Value to reverse
169   \return               Reversed value
170  */
171 #define __REV16(value) __ROR(__REV(value), 16)
172
173
174 /**
175   \brief   Reverse byte order (16 bit)
176   \details Reverses the byte order in a 16-bit value and returns the signed 16-bit result. For example, 0x0080 becomes 0x8000.
177   \param [in]    value  Value to reverse
178   \return               Reversed value
179  */
180 #define __REVSH(value) (int16_t)__builtin_bswap16(value)
181
182
183 /**
184   \brief   Rotate Right in unsigned value (32 bit)
185   \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
186   \param [in]    op1  Value to rotate
187   \param [in]    op2  Number of Bits to rotate
188   \return               Rotated value
189  */
190 __STATIC_FORCEINLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
191 {
192   op2 %= 32U;
193   if (op2 == 0U)
194   {
195     return op1;
196   }
197   return (op1 >> op2) | (op1 << (32U - op2));
198 }
199
200
201 /**
202   \brief   Breakpoint
203   \param [in]    value  is ignored by the processor.
204                  If required, a debugger can use it to store additional information about the breakpoint.
205  */
206 #define __BKPT(value)   __ASM volatile ("bkpt "#value)
207
208 /**
209   \brief   Reverse bit order of value
210   \param [in]    value  Value to reverse
211   \return               Reversed value
212  */
213 #define __RBIT          __builtin_arm_rbit
214
215 /**
216   \brief   Count leading zeros
217   \param [in]  value  Value to count the leading zeros
218   \return             number of leading zeros in value
219  */
220 #define __CLZ           (uint8_t)__builtin_clz
221
222 /**
223   \brief   LDR Exclusive (8 bit)
224   \details Executes a exclusive LDR instruction for 8 bit value.
225   \param [in]    ptr  Pointer to data
226   \return             value of type uint8_t at (*ptr)
227  */
228 #define __LDREXB        (uint8_t)__builtin_arm_ldrex
229
230
231 /**
232   \brief   LDR Exclusive (16 bit)
233   \details Executes a exclusive LDR instruction for 16 bit values.
234   \param [in]    ptr  Pointer to data
235   \return        value of type uint16_t at (*ptr)
236  */
237 #define __LDREXH        (uint16_t)__builtin_arm_ldrex
238
239 /**
240   \brief   LDR Exclusive (32 bit)
241   \details Executes a exclusive LDR instruction for 32 bit values.
242   \param [in]    ptr  Pointer to data
243   \return        value of type uint32_t at (*ptr)
244  */
245 #define __LDREXW        (uint32_t)__builtin_arm_ldrex
246
247 /**
248   \brief   STR Exclusive (8 bit)
249   \details Executes a exclusive STR instruction for 8 bit values.
250   \param [in]  value  Value to store
251   \param [in]    ptr  Pointer to location
252   \return          0  Function succeeded
253   \return          1  Function failed
254  */
255 #define __STREXB        (uint32_t)__builtin_arm_strex
256
257 /**
258   \brief   STR Exclusive (16 bit)
259   \details Executes a exclusive STR instruction for 16 bit values.
260   \param [in]  value  Value to store
261   \param [in]    ptr  Pointer to location
262   \return          0  Function succeeded
263   \return          1  Function failed
264  */
265 #define __STREXH        (uint32_t)__builtin_arm_strex
266
267 /**
268   \brief   STR Exclusive (32 bit)
269   \details Executes a exclusive STR instruction for 32 bit values.
270   \param [in]  value  Value to store
271   \param [in]    ptr  Pointer to location
272   \return          0  Function succeeded
273   \return          1  Function failed
274  */
275 #define __STREXW        (uint32_t)__builtin_arm_strex
276
277 /**
278   \brief   Remove the exclusive lock
279   \details Removes the exclusive lock which is created by LDREX.
280  */
281 #define __CLREX             __builtin_arm_clrex
282
283 /**
284   \brief   Signed Saturate
285   \details Saturates a signed value.
286   \param [in]  value  Value to be saturated
287   \param [in]    sat  Bit position to saturate to (1..32)
288   \return             Saturated value
289  */
290 #define __SSAT             __builtin_arm_ssat
291
292 /**
293   \brief   Unsigned Saturate
294   \details Saturates an unsigned value.
295   \param [in]  value  Value to be saturated
296   \param [in]    sat  Bit position to saturate to (0..31)
297   \return             Saturated value
298  */
299 #define __USAT             __builtin_arm_usat
300
301
302 /* ###########################  Core Function Access  ########################### */
303
304 /**
305   \brief   Get FPSCR
306   \details Returns the current value of the Floating Point Status/Control register.
307   \return               Floating Point Status/Control register value
308  */
309 #define __get_FPSCR      __builtin_arm_get_fpscr
310
311 /**
312   \brief   Set FPSCR
313   \details Assigns the given value to the Floating Point Status/Control register.
314   \param [in]    fpscr  Floating Point Status/Control value to set
315  */
316 #define __set_FPSCR      __builtin_arm_set_fpscr
317
318 /** \brief  Get CPSR Register
319     \return               CPSR Register value
320  */
321 __STATIC_FORCEINLINE uint32_t __get_CPSR(void)
322 {
323   uint32_t result;
324   __ASM volatile("MRS %0, cpsr" : "=r" (result) );
325   return(result);
326 }
327
328 /** \brief  Set CPSR Register
329     \param [in]    cpsr  CPSR value to set
330  */
331 __STATIC_FORCEINLINE void __set_CPSR(uint32_t cpsr)
332 {
333 __ASM volatile ("MSR cpsr, %0" : : "r" (cpsr) : "memory");
334 }
335
336 /** \brief  Get Mode
337     \return                Processor Mode
338  */
339 __STATIC_FORCEINLINE uint32_t __get_mode(void)
340 {
341         return (__get_CPSR() & 0x1FU);
342 }
343
344 /** \brief  Set Mode
345     \param [in]    mode  Mode value to set
346  */
347 __STATIC_FORCEINLINE void __set_mode(uint32_t mode)
348 {
349   __ASM volatile("MSR  cpsr_c, %0" : : "r" (mode) : "memory");
350 }
351
352 /** \brief  Get Stack Pointer
353     \return Stack Pointer value
354  */
355 __STATIC_FORCEINLINE uint32_t __get_SP()
356 {
357   uint32_t result;
358   __ASM volatile("MOV  %0, sp" : "=r" (result) : : "memory");
359   return result;
360 }
361
362 /** \brief  Set Stack Pointer
363     \param [in]    stack  Stack Pointer value to set
364  */
365 __STATIC_FORCEINLINE void __set_SP(uint32_t stack)
366 {
367   __ASM volatile("MOV  sp, %0" : : "r" (stack) : "memory");
368 }
369
370 /** \brief  Get USR/SYS Stack Pointer
371     \return USR/SYS Stack Pointer value
372  */
373 __STATIC_FORCEINLINE uint32_t __get_SP_usr()
374 {
375   uint32_t cpsr;
376   uint32_t result;
377   __ASM volatile(
378     "MRS     %0, cpsr   \n"
379     "CPS     #0x1F      \n" // no effect in USR mode
380     "MOV     %1, sp     \n"
381     "MSR     cpsr_c, %2 \n" // no effect in USR mode
382     "ISB" :  "=r"(cpsr), "=r"(result) : "r"(cpsr) : "memory"
383    );
384   return result;
385 }
386
387 /** \brief  Set USR/SYS Stack Pointer
388     \param [in]    topOfProcStack  USR/SYS Stack Pointer value to set
389  */
390 __STATIC_FORCEINLINE void __set_SP_usr(uint32_t topOfProcStack)
391 {
392   uint32_t cpsr;
393   __ASM volatile(
394     "MRS     %0, cpsr   \n"
395     "CPS     #0x1F      \n" // no effect in USR mode
396     "MOV     sp, %1     \n"
397     "MSR     cpsr_c, %2 \n" // no effect in USR mode
398     "ISB" : "=r"(cpsr) : "r" (topOfProcStack), "r"(cpsr) : "memory"
399    );
400 }
401
402 /** \brief  Get FPEXC
403     \return               Floating Point Exception Control register value
404  */
405 __STATIC_FORCEINLINE uint32_t __get_FPEXC(void)
406 {
407 #if (__FPU_PRESENT == 1)
408   uint32_t result;
409   __ASM volatile("VMRS %0, fpexc" : "=r" (result) : : "memory");
410   return(result);
411 #else
412   return(0);
413 #endif
414 }
415
416 /** \brief  Set FPEXC
417     \param [in]    fpexc  Floating Point Exception Control value to set
418  */
419 __STATIC_FORCEINLINE void __set_FPEXC(uint32_t fpexc)
420 {
421 #if (__FPU_PRESENT == 1)
422   __ASM volatile ("VMSR fpexc, %0" : : "r" (fpexc) : "memory");
423 #endif
424 }
425
426 /*
427  * Include common core functions to access Coprocessor 15 registers
428  */
429
430 #define __get_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MRC p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : "=r" (Rt) : : "memory" )
431 #define __set_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MCR p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : : "r" (Rt) : "memory" )
432 #define __get_CP64(cp, op1, Rt, CRm)         __ASM volatile("MRRC p" # cp ", " # op1 ", %Q0, %R0, c" # CRm  : "=r" (Rt) : : "memory" )
433 #define __set_CP64(cp, op1, Rt, CRm)         __ASM volatile("MCRR p" # cp ", " # op1 ", %Q0, %R0, c" # CRm  : : "r" (Rt) : "memory" )
434
435 #include "cmsis_cp15.h"
436
437 /** \brief  Enable Floating Point Unit
438
439   Critical section, called from undef handler, so systick is disabled
440  */
441 __STATIC_INLINE void __FPU_Enable(void)
442 {
443   __ASM volatile(
444     //Permit access to VFP/NEON, registers by modifying CPACR
445     "        MRC     p15,0,R1,c1,c0,2  \n"
446     "        ORR     R1,R1,#0x00F00000 \n"
447     "        MCR     p15,0,R1,c1,c0,2  \n"
448
449     //Ensure that subsequent instructions occur in the context of VFP/NEON access permitted
450     "        ISB                       \n"
451
452     //Enable VFP/NEON
453     "        VMRS    R1,FPEXC          \n"
454     "        ORR     R1,R1,#0x40000000 \n"
455     "        VMSR    FPEXC,R1          \n"
456
457     //Initialise VFP/NEON registers to 0
458     "        MOV     R2,#0             \n"
459
460     //Initialise D16 registers to 0
461     "        VMOV    D0, R2,R2         \n"
462     "        VMOV    D1, R2,R2         \n"
463     "        VMOV    D2, R2,R2         \n"
464     "        VMOV    D3, R2,R2         \n"
465     "        VMOV    D4, R2,R2         \n"
466     "        VMOV    D5, R2,R2         \n"
467     "        VMOV    D6, R2,R2         \n"
468     "        VMOV    D7, R2,R2         \n"
469     "        VMOV    D8, R2,R2         \n"
470     "        VMOV    D9, R2,R2         \n"
471     "        VMOV    D10,R2,R2         \n"
472     "        VMOV    D11,R2,R2         \n"
473     "        VMOV    D12,R2,R2         \n"
474     "        VMOV    D13,R2,R2         \n"
475     "        VMOV    D14,R2,R2         \n"
476     "        VMOV    D15,R2,R2         \n"
477
478 #if __ARM_NEON == 1
479     //Initialise D32 registers to 0
480     "        VMOV    D16,R2,R2         \n"
481     "        VMOV    D17,R2,R2         \n"
482     "        VMOV    D18,R2,R2         \n"
483     "        VMOV    D19,R2,R2         \n"
484     "        VMOV    D20,R2,R2         \n"
485     "        VMOV    D21,R2,R2         \n"
486     "        VMOV    D22,R2,R2         \n"
487     "        VMOV    D23,R2,R2         \n"
488     "        VMOV    D24,R2,R2         \n"
489     "        VMOV    D25,R2,R2         \n"
490     "        VMOV    D26,R2,R2         \n"
491     "        VMOV    D27,R2,R2         \n"
492     "        VMOV    D28,R2,R2         \n"
493     "        VMOV    D29,R2,R2         \n"
494     "        VMOV    D30,R2,R2         \n"
495     "        VMOV    D31,R2,R2         \n"
496 #endif
497
498     //Initialise FPSCR to a known state
499     "        VMRS    R2,FPSCR          \n"
500     "        LDR     R3,=0x00086060    \n" //Mask off all bits that do not have to be preserved. Non-preserved bits can/should be zero.
501     "        AND     R2,R2,R3          \n"
502     "        VMSR    FPSCR,R2            "
503   );
504 }
505
506 #endif /* __CMSIS_ARMCLANG_H */