1 /**************************************************************************//**
2  * @file     cmsis_gcc.h
3  * @brief    CMSIS compiler specific macros, functions, instructions
4  * @version  V1.3.1
5  * @date     05. May 2021
6  ******************************************************************************/
7 /*
8  * Copyright (c) 2009-2021 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_GCC_H
26 #define __CMSIS_GCC_H
27 
28 /* ignore some GCC warnings */
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wsign-conversion"
31 #pragma GCC diagnostic ignored "-Wconversion"
32 #pragma GCC diagnostic ignored "-Wunused-parameter"
33 
34 /* Fallback for __has_builtin */
35 #ifndef __has_builtin
36   #define __has_builtin(x) (0)
37 #endif
38 
39 /* CMSIS compiler specific defines */
40 #ifndef   __ASM
41   #define __ASM                                  __asm
42 #endif
43 #ifndef   __INLINE
44   #define __INLINE                               inline
45 #endif
46 #ifndef   __FORCEINLINE
47   #define __FORCEINLINE                          __attribute__((always_inline))
48 #endif
49 #ifndef   __STATIC_INLINE
50   #define __STATIC_INLINE                        static inline
51 #endif
52 #ifndef   __STATIC_FORCEINLINE
53   #define __STATIC_FORCEINLINE                   __attribute__((always_inline)) static inline
54 #endif
55 #ifndef   __NO_RETURN
56   #define __NO_RETURN                            __attribute__((__noreturn__))
57 #endif
58 #ifndef   CMSIS_DEPRECATED
59   #define CMSIS_DEPRECATED                       __attribute__((deprecated))
60 #endif
61 #ifndef   __USED
62   #define __USED                                 __attribute__((used))
63 #endif
64 #ifndef   __WEAK
65   #define __WEAK                                 __attribute__((weak))
66 #endif
67 #ifndef   __PACKED
68   #define __PACKED                               __attribute__((packed, aligned(1)))
69 #endif
70 #ifndef   __PACKED_STRUCT
71   #define __PACKED_STRUCT                        struct __attribute__((packed, aligned(1)))
72 #endif
73 #ifndef   __UNALIGNED_UINT16_WRITE
74   #pragma GCC diagnostic push
75   #pragma GCC diagnostic ignored "-Wpacked"
76 /*lint -esym(9058, T_UINT16_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_WRITE */
77   __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
78   #pragma GCC diagnostic pop
79   #define __UNALIGNED_UINT16_WRITE(addr, val)    (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
80 #endif
81 #ifndef   __UNALIGNED_UINT16_READ
82   #pragma GCC diagnostic push
83   #pragma GCC diagnostic ignored "-Wpacked"
84 /*lint -esym(9058, T_UINT16_READ)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_READ */
85   __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
86   #pragma GCC diagnostic pop
87   #define __UNALIGNED_UINT16_READ(addr)          (((const struct T_UINT16_READ *)(const void *)(addr))->v)
88 #endif
89 #ifndef   __UNALIGNED_UINT32_WRITE
90   #pragma GCC diagnostic push
91   #pragma GCC diagnostic ignored "-Wpacked"
92 /*lint -esym(9058, T_UINT32_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT32_WRITE */
93   __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
94   #pragma GCC diagnostic pop
95   #define __UNALIGNED_UINT32_WRITE(addr, val)    (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
96 #endif
97 #ifndef   __UNALIGNED_UINT32_READ
98   #pragma GCC diagnostic push
99   #pragma GCC diagnostic ignored "-Wpacked"
100   __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
101   #pragma GCC diagnostic pop
102   #define __UNALIGNED_UINT32_READ(addr)          (((const struct T_UINT32_READ *)(const void *)(addr))->v)
103 #endif
104 #ifndef   __ALIGNED
105   #define __ALIGNED(x)                           __attribute__((aligned(x)))
106 #endif
107 #ifndef   __COMPILER_BARRIER
108   #define __COMPILER_BARRIER()                   __ASM volatile("":::"memory")
109 #endif
110 
111 
__QSUB16(uint32_t op1,uint32_t op2)112 __STATIC_FORCEINLINE uint32_t __QSUB16(uint32_t op1, uint32_t op2)
113 {
114   uint32_t result;
115 
116   __ASM volatile ("qsub16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
117   return(result);
118 }
119 
120 
__QSUB8(uint32_t op1,uint32_t op2)121 __STATIC_FORCEINLINE uint32_t __QSUB8(uint32_t op1, uint32_t op2)
122 {
123   uint32_t result;
124 
125   __ASM volatile ("qsub8 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
126   return(result);
127 }
128 
129 
__QADD16(uint32_t op1,uint32_t op2)130 __STATIC_FORCEINLINE uint32_t __QADD16(uint32_t op1, uint32_t op2)
131 {
132   uint32_t result;
133 
134   __ASM volatile ("qadd16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
135   return(result);
136 }
137 
__QADD8(uint32_t op1,uint32_t op2)138 __STATIC_FORCEINLINE uint32_t __QADD8(uint32_t op1, uint32_t op2)
139 {
140   uint32_t result;
141 
142   __ASM volatile ("qadd8 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
143   return(result);
144 }
145 
__QADD(int32_t op1,int32_t op2)146 __STATIC_FORCEINLINE  int32_t __QADD( int32_t op1,  int32_t op2)
147 {
148   int32_t result;
149 
150   __ASM volatile ("qadd %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
151   return(result);
152 }
153 
__QSAX(uint32_t op1,uint32_t op2)154 __STATIC_FORCEINLINE uint32_t __QSAX(uint32_t op1, uint32_t op2)
155 {
156   uint32_t result;
157 
158   __ASM ("qsax %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
159   return(result);
160 }
161 
__SHSAX(uint32_t op1,uint32_t op2)162 __STATIC_FORCEINLINE uint32_t __SHSAX(uint32_t op1, uint32_t op2)
163 {
164   uint32_t result;
165 
166   __ASM ("shsax %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
167   return(result);
168 }
169 
__SMLALD(uint32_t op1,uint32_t op2,uint64_t acc)170 __STATIC_FORCEINLINE uint64_t __SMLALD (uint32_t op1, uint32_t op2, uint64_t acc)
171 {
172   union llreg_u{
173     uint32_t w32[2];
174     uint64_t w64;
175   } llr;
176   llr.w64 = acc;
177 
178 #ifndef __ARMEB__   /* Little endian */
179   __ASM volatile ("smlald %0, %1, %2, %3" : "=r" (llr.w32[0]), "=r" (llr.w32[1]): "r" (op1), "r" (op2) , "0" (llr.w32[0]), "1" (llr.w32[1]) );
180 #else               /* Big endian */
181   __ASM volatile ("smlald %0, %1, %2, %3" : "=r" (llr.w32[1]), "=r" (llr.w32[0]): "r" (op1), "r" (op2) , "0" (llr.w32[1]), "1" (llr.w32[0]) );
182 #endif
183 
184   return(llr.w64);
185 }
186 
__QSUB(int32_t op1,int32_t op2)187 __STATIC_FORCEINLINE  int32_t __QSUB( int32_t op1,  int32_t op2)
188 {
189   int32_t result;
190 
191   __ASM volatile ("qsub %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
192   return(result);
193 }
194 
__SXTB16(uint32_t op1)195 __STATIC_FORCEINLINE uint32_t __SXTB16(uint32_t op1)
196 {
197   uint32_t result;
198 
199   __ASM ("sxtb16 %0, %1" : "=r" (result) : "r" (op1));
200   return(result);
201 }
202 
203 
__SMUAD(uint32_t op1,uint32_t op2)204 __STATIC_FORCEINLINE uint32_t __SMUAD  (uint32_t op1, uint32_t op2)
205 {
206   uint32_t result;
207 
208   __ASM volatile ("smuad %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
209   return(result);
210 }
211 
212 
213 
214 #define __PKHBT(ARG1,ARG2,ARG3)          ( ((((uint32_t)(ARG1))          ) & 0x0000FFFFUL) |  \
215                                            ((((uint32_t)(ARG2)) << (ARG3)) & 0xFFFF0000UL)  )
216 
217 #define __PKHTB(ARG1,ARG2,ARG3)          ( ((((uint32_t)(ARG1))          ) & 0xFFFF0000UL) |  \
218                                            ((((uint32_t)(ARG2)) >> (ARG3)) & 0x0000FFFFUL)  )
219 
__SMLAD(uint32_t op1,uint32_t op2,uint32_t op3)220 __STATIC_FORCEINLINE uint32_t __SMLAD (uint32_t op1, uint32_t op2, uint32_t op3)
221 {
222   uint32_t result;
223 
224   __ASM volatile ("smlad %0, %1, %2, %3" : "=r" (result) : "r" (op1), "r" (op2), "r" (op3) );
225   return(result);
226 }
227 
__SMUADX(uint32_t op1,uint32_t op2)228 __STATIC_FORCEINLINE uint32_t __SMUADX (uint32_t op1, uint32_t op2)
229 {
230   uint32_t result;
231 
232   __ASM volatile ("smuadx %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
233   return(result);
234 }
235 
__SMLADX(uint32_t op1,uint32_t op2,uint32_t op3)236 __STATIC_FORCEINLINE uint32_t __SMLADX (uint32_t op1, uint32_t op2, uint32_t op3)
237 {
238   uint32_t result;
239 
240   __ASM volatile ("smladx %0, %1, %2, %3" : "=r" (result) : "r" (op1), "r" (op2), "r" (op3) );
241   return(result);
242 }
243 
__SMLALDX(uint32_t op1,uint32_t op2,uint64_t acc)244 __STATIC_FORCEINLINE uint64_t __SMLALDX (uint32_t op1, uint32_t op2, uint64_t acc)
245 {
246   union llreg_u{
247     uint32_t w32[2];
248     uint64_t w64;
249   } llr;
250   llr.w64 = acc;
251 
252 #ifndef __ARMEB__   /* Little endian */
253   __ASM volatile ("smlaldx %0, %1, %2, %3" : "=r" (llr.w32[0]), "=r" (llr.w32[1]): "r" (op1), "r" (op2) , "0" (llr.w32[0]), "1" (llr.w32[1]) );
254 #else               /* Big endian */
255   __ASM volatile ("smlaldx %0, %1, %2, %3" : "=r" (llr.w32[1]), "=r" (llr.w32[0]): "r" (op1), "r" (op2) , "0" (llr.w32[1]), "1" (llr.w32[0]) );
256 #endif
257 
258   return(llr.w64);
259 }
260 
__SMMLA(int32_t op1,int32_t op2,int32_t op3)261 __STATIC_FORCEINLINE int32_t __SMMLA (int32_t op1, int32_t op2, int32_t op3)
262 {
263  int32_t result;
264 
265  __ASM volatile ("smmla %0, %1, %2, %3" : "=r" (result): "r"  (op1), "r" (op2), "r" (op3) );
266  return(result);
267 }
268 
__SMUSD(uint32_t op1,uint32_t op2)269 __STATIC_FORCEINLINE uint32_t __SMUSD  (uint32_t op1, uint32_t op2)
270 {
271   uint32_t result;
272 
273   __ASM volatile ("smusd %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
274   return(result);
275 }
276 
__SMUSDX(uint32_t op1,uint32_t op2)277 __STATIC_FORCEINLINE uint32_t __SMUSDX (uint32_t op1, uint32_t op2)
278 {
279   uint32_t result;
280 
281   __ASM volatile ("smusdx %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
282   return(result);
283 }
284 
__QASX(uint32_t op1,uint32_t op2)285 __STATIC_FORCEINLINE uint32_t __QASX(uint32_t op1, uint32_t op2)
286 {
287   uint32_t result;
288 
289   __ASM ("qasx %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
290   return(result);
291 }
292 
__SHADD16(uint32_t op1,uint32_t op2)293 __STATIC_FORCEINLINE uint32_t __SHADD16(uint32_t op1, uint32_t op2)
294 {
295   uint32_t result;
296 
297   __ASM ("shadd16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
298   return(result);
299 }
300 
__SHSUB16(uint32_t op1,uint32_t op2)301 __STATIC_FORCEINLINE uint32_t __SHSUB16(uint32_t op1, uint32_t op2)
302 {
303   uint32_t result;
304 
305   __ASM ("shsub16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
306   return(result);
307 }
308 
__SHASX(uint32_t op1,uint32_t op2)309 __STATIC_FORCEINLINE uint32_t __SHASX(uint32_t op1, uint32_t op2)
310 {
311   uint32_t result;
312 
313   __ASM ("shasx %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
314   return(result);
315 }
316 
__SMLSDX(uint32_t op1,uint32_t op2,uint32_t op3)317 __STATIC_FORCEINLINE uint32_t __SMLSDX (uint32_t op1, uint32_t op2, uint32_t op3)
318 {
319   uint32_t result;
320 
321   __ASM volatile ("smlsdx %0, %1, %2, %3" : "=r" (result) : "r" (op1), "r" (op2), "r" (op3) );
322   return(result);
323 }
324 
325 
326 /* ##########################  Core Instruction Access  ######################### */
327 /**
328   \brief   No Operation
329  */
330 #define __NOP()                             __ASM volatile ("nop")
331 
332 /**
333   \brief   Wait For Interrupt
334  */
335 #define __WFI()                             __ASM volatile ("wfi":::"memory")
336 
337 /**
338   \brief   Wait For Event
339  */
340 #define __WFE()                             __ASM volatile ("wfe":::"memory")
341 
342 /**
343   \brief   Send Event
344  */
345 #define __SEV()                             __ASM volatile ("sev")
346 
347 /**
348   \brief   Instruction Synchronization Barrier
349   \details Instruction Synchronization Barrier flushes the pipeline in the processor,
350            so that all instructions following the ISB are fetched from cache or memory,
351            after the instruction has been completed.
352  */
__ISB(void)353 __STATIC_FORCEINLINE  void __ISB(void)
354 {
355   __ASM volatile ("isb 0xF":::"memory");
356 }
357 
358 
359 /**
360   \brief   Data Synchronization Barrier
361   \details Acts as a special kind of Data Memory Barrier.
362            It completes when all explicit memory accesses before this instruction complete.
363  */
__DSB(void)364 __STATIC_FORCEINLINE  void __DSB(void)
365 {
366   __ASM volatile ("dsb 0xF":::"memory");
367 }
368 
369 /**
370   \brief   Data Memory Barrier
371   \details Ensures the apparent order of the explicit memory operations before
372            and after the instruction, without ensuring their completion.
373  */
__DMB(void)374 __STATIC_FORCEINLINE  void __DMB(void)
375 {
376   __ASM volatile ("dmb 0xF":::"memory");
377 }
378 
379 /**
380   \brief   Reverse byte order (32 bit)
381   \details Reverses the byte order in unsigned integer value. For example, 0x12345678 becomes 0x78563412.
382   \param [in]    value  Value to reverse
383   \return               Reversed value
384  */
__REV(uint32_t value)385 __STATIC_FORCEINLINE  uint32_t __REV(uint32_t value)
386 {
387 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
388   return __builtin_bswap32(value);
389 #else
390   uint32_t result;
391 
392   __ASM ("rev %0, %1" : "=r" (result) : "r" (value) );
393   return result;
394 #endif
395 }
396 
397 /**
398   \brief   Reverse byte order (16 bit)
399   \details Reverses the byte order within each halfword of a word. For example, 0x12345678 becomes 0x34127856.
400   \param [in]    value  Value to reverse
401   \return               Reversed value
402  */
__REV16(uint32_t value)403 __STATIC_FORCEINLINE uint32_t __REV16(uint32_t value)
404 {
405   uint32_t result;
406   __ASM ("rev16 %0, %1" : "=r" (result) : "r" (value));
407   return result;
408 }
409 
410 /**
411   \brief   Reverse byte order (16 bit)
412   \details Reverses the byte order in a 16-bit value and returns the signed 16-bit result. For example, 0x0080 becomes 0x8000.
413   \param [in]    value  Value to reverse
414   \return               Reversed value
415  */
__REVSH(int16_t value)416 __STATIC_FORCEINLINE  int16_t __REVSH(int16_t value)
417 {
418 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
419   return (int16_t)__builtin_bswap16(value);
420 #else
421   int16_t result;
422 
423   __ASM ("revsh %0, %1" : "=r" (result) : "r" (value) );
424   return result;
425 #endif
426 }
427 
428 /**
429   \brief   Rotate Right in unsigned value (32 bit)
430   \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
431   \param [in]    op1  Value to rotate
432   \param [in]    op2  Number of Bits to rotate
433   \return               Rotated value
434  */
__ROR(uint32_t op1,uint32_t op2)435 __STATIC_FORCEINLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
436 {
437   op2 %= 32U;
438   if (op2 == 0U)
439   {
440     return op1;
441   }
442   return (op1 >> op2) | (op1 << (32U - op2));
443 }
444 
445 
446 /**
447   \brief   Breakpoint
448   \param [in]    value  is ignored by the processor.
449                  If required, a debugger can use it to store additional information about the breakpoint.
450  */
451 #define __BKPT(value)   __ASM volatile ("bkpt "#value)
452 
453 /**
454   \brief   Reverse bit order of value
455   \details Reverses the bit order of the given value.
456   \param [in]    value  Value to reverse
457   \return               Reversed value
458  */
__RBIT(uint32_t value)459 __STATIC_FORCEINLINE  uint32_t __RBIT(uint32_t value)
460 {
461   uint32_t result;
462 
463 #if ((defined (__ARM_ARCH_7M__      ) && (__ARM_ARCH_7M__      == 1)) || \
464      (defined (__ARM_ARCH_7EM__     ) && (__ARM_ARCH_7EM__     == 1)) || \
465      (defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1))    )
466    __ASM ("rbit %0, %1" : "=r" (result) : "r" (value) );
467 #else
468   int32_t s = (4U /*sizeof(v)*/ * 8U) - 1U; /* extra shift needed at end */
469 
470   result = value;                      /* r will be reversed bits of v; first get LSB of v */
471   for (value >>= 1U; value; value >>= 1U)
472   {
473     result <<= 1U;
474     result |= value & 1U;
475     s--;
476   }
477   result <<= s;                        /* shift when v's highest bits are zero */
478 #endif
479   return result;
480 }
481 
482 /**
483   \brief   Count leading zeros
484   \param [in]  value  Value to count the leading zeros
485   \return             number of leading zeros in value
486  */
__CLZ(uint32_t value)487 __STATIC_FORCEINLINE uint8_t __CLZ(uint32_t value)
488 {
489   /* Even though __builtin_clz produces a CLZ instruction on ARM, formally
490      __builtin_clz(0) is undefined behaviour, so handle this case specially.
491      This guarantees ARM-compatible results if happening to compile on a non-ARM
492      target, and ensures the compiler doesn't decide to activate any
493      optimisations using the logic "value was passed to __builtin_clz, so it
494      is non-zero".
495      ARM GCC 7.3 and possibly earlier will optimise this test away, leaving a
496      single CLZ instruction.
497    */
498   if (value == 0U)
499   {
500     return 32U;
501   }
502   return __builtin_clz(value);
503 }
504 
505 /**
506   \brief   LDR Exclusive (8 bit)
507   \details Executes a exclusive LDR instruction for 8 bit value.
508   \param [in]    ptr  Pointer to data
509   \return             value of type uint8_t at (*ptr)
510  */
__LDREXB(volatile uint8_t * addr)511 __STATIC_FORCEINLINE  uint8_t __LDREXB(volatile uint8_t *addr)
512 {
513     uint32_t result;
514 
515 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
516    __ASM volatile ("ldrexb %0, %1" : "=r" (result) : "Q" (*addr) );
517 #else
518     /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
519        accepted by assembler. So has to use following less efficient pattern.
520     */
521    __ASM volatile ("ldrexb %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
522 #endif
523    return ((uint8_t) result);    /* Add explicit type cast here */
524 }
525 
526 
527 /**
528   \brief   LDR Exclusive (16 bit)
529   \details Executes a exclusive LDR instruction for 16 bit values.
530   \param [in]    ptr  Pointer to data
531   \return        value of type uint16_t at (*ptr)
532  */
__LDREXH(volatile uint16_t * addr)533 __STATIC_FORCEINLINE  uint16_t __LDREXH(volatile uint16_t *addr)
534 {
535     uint32_t result;
536 
537 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
538    __ASM volatile ("ldrexh %0, %1" : "=r" (result) : "Q" (*addr) );
539 #else
540     /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
541        accepted by assembler. So has to use following less efficient pattern.
542     */
543    __ASM volatile ("ldrexh %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
544 #endif
545    return ((uint16_t) result);    /* Add explicit type cast here */
546 }
547 
548 
549 /**
550   \brief   LDR Exclusive (32 bit)
551   \details Executes a exclusive LDR instruction for 32 bit values.
552   \param [in]    ptr  Pointer to data
553   \return        value of type uint32_t at (*ptr)
554  */
__LDREXW(volatile uint32_t * addr)555 __STATIC_FORCEINLINE  uint32_t __LDREXW(volatile uint32_t *addr)
556 {
557     uint32_t result;
558 
559    __ASM volatile ("ldrex %0, %1" : "=r" (result) : "Q" (*addr) );
560    return(result);
561 }
562 
563 
564 /**
565   \brief   STR Exclusive (8 bit)
566   \details Executes a exclusive STR instruction for 8 bit values.
567   \param [in]  value  Value to store
568   \param [in]    ptr  Pointer to location
569   \return          0  Function succeeded
570   \return          1  Function failed
571  */
__STREXB(uint8_t value,volatile uint8_t * addr)572 __STATIC_FORCEINLINE  uint32_t __STREXB(uint8_t value, volatile uint8_t *addr)
573 {
574    uint32_t result;
575 
576    __ASM volatile ("strexb %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
577    return(result);
578 }
579 
580 
581 /**
582   \brief   STR Exclusive (16 bit)
583   \details Executes a exclusive STR instruction for 16 bit values.
584   \param [in]  value  Value to store
585   \param [in]    ptr  Pointer to location
586   \return          0  Function succeeded
587   \return          1  Function failed
588  */
__STREXH(uint16_t value,volatile uint16_t * addr)589 __STATIC_FORCEINLINE  uint32_t __STREXH(uint16_t value, volatile uint16_t *addr)
590 {
591    uint32_t result;
592 
593    __ASM volatile ("strexh %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
594    return(result);
595 }
596 
597 
598 /**
599   \brief   STR Exclusive (32 bit)
600   \details Executes a exclusive STR instruction for 32 bit values.
601   \param [in]  value  Value to store
602   \param [in]    ptr  Pointer to location
603   \return          0  Function succeeded
604   \return          1  Function failed
605  */
__STREXW(uint32_t value,volatile uint32_t * addr)606 __STATIC_FORCEINLINE  uint32_t __STREXW(uint32_t value, volatile uint32_t *addr)
607 {
608    uint32_t result;
609 
610    __ASM volatile ("strex %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" (value) );
611    return(result);
612 }
613 
614 
615 /**
616   \brief   Remove the exclusive lock
617   \details Removes the exclusive lock which is created by LDREX.
618  */
__CLREX(void)619 __STATIC_FORCEINLINE  void __CLREX(void)
620 {
621   __ASM volatile ("clrex" ::: "memory");
622 }
623 
624 /**
625   \brief   Signed Saturate
626   \details Saturates a signed value.
627   \param [in]  value  Value to be saturated
628   \param [in]    sat  Bit position to saturate to (1..32)
629   \return             Saturated value
630  */
631 #define __SSAT(ARG1, ARG2) \
632 __extension__ \
633 ({                          \
634   int32_t __RES, __ARG1 = (ARG1); \
635   __ASM volatile ("ssat %0, %1, %2" : "=r" (__RES) :  "I" (ARG2), "r" (__ARG1) : "cc" ); \
636   __RES; \
637  })
638 
639 
640 /**
641   \brief   Unsigned Saturate
642   \details Saturates an unsigned value.
643   \param [in]  value  Value to be saturated
644   \param [in]    sat  Bit position to saturate to (0..31)
645   \return             Saturated value
646  */
647 #define __USAT(ARG1, ARG2) \
648 __extension__ \
649 ({                          \
650   uint32_t __RES, __ARG1 = (ARG1); \
651   __ASM volatile ("usat %0, %1, %2" : "=r" (__RES) :  "I" (ARG2), "r" (__ARG1) : "cc" ); \
652   __RES; \
653  })
654 
655 /* ###########################  Core Function Access  ########################### */
656 
657 /**
658   \brief   Enable IRQ Interrupts
659   \details Enables IRQ interrupts by clearing the I-bit in the CPSR.
660            Can only be executed in Privileged modes.
661  */
__enable_irq(void)662 __STATIC_FORCEINLINE void __enable_irq(void)
663 {
664   __ASM volatile ("cpsie i" : : : "memory");
665 }
666 
667 /**
668   \brief   Disable IRQ Interrupts
669   \details Disables IRQ interrupts by setting the I-bit in the CPSR.
670   Can only be executed in Privileged modes.
671  */
__disable_irq(void)672 __STATIC_FORCEINLINE void __disable_irq(void)
673 {
674   __ASM volatile ("cpsid i" : : : "memory");
675 }
676 
677 /**
678   \brief   Enable FIQ
679   \details Enables FIQ interrupts by clearing the F-bit in the CPSR.
680            Can only be executed in Privileged modes.
681  */
__enable_fault_irq(void)682 __STATIC_FORCEINLINE void __enable_fault_irq(void)
683 {
684   __ASM volatile ("cpsie f" : : : "memory");
685 }
686 
687 /**
688   \brief   Disable FIQ
689   \details Disables FIQ interrupts by setting the F-bit in the CPSR.
690            Can only be executed in Privileged modes.
691  */
__disable_fault_irq(void)692 __STATIC_FORCEINLINE void __disable_fault_irq(void)
693 {
694   __ASM volatile ("cpsid f" : : : "memory");
695 }
696 
697 /**
698   \brief   Get FPSCR
699   \details Returns the current value of the Floating Point Status/Control register.
700   \return               Floating Point Status/Control register value
701  */
__get_FPSCR(void)702 __STATIC_FORCEINLINE  uint32_t __get_FPSCR(void)
703 {
704   #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
705        (defined (__FPU_USED   ) && (__FPU_USED    == 1U))     )
706   #if __has_builtin(__builtin_arm_get_fpscr)
707   // Re-enable using built-in when GCC has been fixed
708   // || (__GNUC__ > 7) || (__GNUC__ == 7 && __GNUC_MINOR__ >= 2)
709     /* see https://gcc.gnu.org/ml/gcc-patches/2017-04/msg00443.html */
710     return __builtin_arm_get_fpscr();
711   #else
712     uint32_t result;
713 
714     __ASM volatile ("VMRS %0, fpscr" : "=r" (result) );
715     return(result);
716   #endif
717   #else
718     return(0U);
719   #endif
720 }
721 
722 /**
723   \brief   Set FPSCR
724   \details Assigns the given value to the Floating Point Status/Control register.
725   \param [in]    fpscr  Floating Point Status/Control value to set
726  */
__set_FPSCR(uint32_t fpscr)727 __STATIC_FORCEINLINE void __set_FPSCR(uint32_t fpscr)
728 {
729   #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
730        (defined (__FPU_USED   ) && (__FPU_USED    == 1U))     )
731   #if __has_builtin(__builtin_arm_set_fpscr)
732   // Re-enable using built-in when GCC has been fixed
733   // || (__GNUC__ > 7) || (__GNUC__ == 7 && __GNUC_MINOR__ >= 2)
734     /* see https://gcc.gnu.org/ml/gcc-patches/2017-04/msg00443.html */
735     __builtin_arm_set_fpscr(fpscr);
736   #else
737     __ASM volatile ("VMSR fpscr, %0" : : "r" (fpscr) : "vfpcc", "memory");
738   #endif
739   #else
740     (void)fpscr;
741   #endif
742 }
743 
744 /** \brief  Get CPSR Register
745     \return               CPSR Register value
746  */
__get_CPSR(void)747 __STATIC_FORCEINLINE uint32_t __get_CPSR(void)
748 {
749   uint32_t result;
750   __ASM volatile("MRS %0, cpsr" : "=r" (result) );
751   return(result);
752 }
753 
754 /** \brief  Set CPSR Register
755     \param [in]    cpsr  CPSR value to set
756  */
__set_CPSR(uint32_t cpsr)757 __STATIC_FORCEINLINE void __set_CPSR(uint32_t cpsr)
758 {
759   __ASM volatile ("MSR cpsr, %0" : : "r" (cpsr) : "cc", "memory");
760 }
761 
762 /** \brief  Get Mode
763     \return                Processor Mode
764  */
__get_mode(void)765 __STATIC_FORCEINLINE uint32_t __get_mode(void)
766 {
767   return (__get_CPSR() & 0x1FU);
768 }
769 
770 /** \brief  Set Mode
771     \param [in]    mode  Mode value to set
772  */
__set_mode(uint32_t mode)773 __STATIC_FORCEINLINE void __set_mode(uint32_t mode)
774 {
775   __ASM volatile("MSR  cpsr_c, %0" : : "r" (mode) : "memory");
776 }
777 
778 /** \brief  Get Stack Pointer
779     \return Stack Pointer value
780  */
__get_SP(void)781 __STATIC_FORCEINLINE uint32_t __get_SP(void)
782 {
783   uint32_t result;
784   __ASM volatile("MOV  %0, sp" : "=r" (result) : : "memory");
785   return result;
786 }
787 
788 /** \brief  Set Stack Pointer
789     \param [in]    stack  Stack Pointer value to set
790  */
__set_SP(uint32_t stack)791 __STATIC_FORCEINLINE void __set_SP(uint32_t stack)
792 {
793   __ASM volatile("MOV  sp, %0" : : "r" (stack) : "memory");
794 }
795 
796 /** \brief  Get USR/SYS Stack Pointer
797     \return USR/SYS Stack Pointer value
798  */
__get_SP_usr(void)799 __STATIC_FORCEINLINE uint32_t __get_SP_usr(void)
800 {
801   uint32_t cpsr = __get_CPSR();
802   uint32_t result;
803   __ASM volatile(
804     "CPS     #0x1F  \n"
805     "MOV     %0, sp   " : "=r"(result) : : "memory"
806    );
807   __set_CPSR(cpsr);
808   __ISB();
809   return result;
810 }
811 
812 /** \brief  Set USR/SYS Stack Pointer
813     \param [in]    topOfProcStack  USR/SYS Stack Pointer value to set
814  */
__set_SP_usr(uint32_t topOfProcStack)815 __STATIC_FORCEINLINE void __set_SP_usr(uint32_t topOfProcStack)
816 {
817   uint32_t cpsr = __get_CPSR();
818   __ASM volatile(
819     "CPS     #0x1F  \n"
820     "MOV     sp, %0   " : : "r" (topOfProcStack) : "memory"
821    );
822   __set_CPSR(cpsr);
823   __ISB();
824 }
825 
826 /** \brief  Get FPEXC
827     \return               Floating Point Exception Control register value
828  */
__get_FPEXC(void)829 __STATIC_FORCEINLINE uint32_t __get_FPEXC(void)
830 {
831 #if (__FPU_PRESENT == 1)
832   uint32_t result;
833   __ASM volatile("VMRS %0, fpexc" : "=r" (result) : : "memory");
834   return(result);
835 #else
836   return(0);
837 #endif
838 }
839 
840 /** \brief  Set FPEXC
841     \param [in]    fpexc  Floating Point Exception Control value to set
842  */
__set_FPEXC(uint32_t fpexc)843 __STATIC_FORCEINLINE void __set_FPEXC(uint32_t fpexc)
844 {
845 #if (__FPU_PRESENT == 1)
846   __ASM volatile ("VMSR fpexc, %0" : : "r" (fpexc) : "memory");
847 #endif
848 }
849 
850 /*
851  * Include common core functions to access Coprocessor 15 registers
852  */
853 
854 #define __get_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MRC p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : "=r" (Rt) : : "memory" )
855 #define __set_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MCR p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : : "r" (Rt) : "memory" )
856 #define __get_CP64(cp, op1, Rt, CRm)         __ASM volatile("MRRC p" # cp ", " # op1 ", %Q0, %R0, c" # CRm  : "=r" (Rt) : : "memory" )
857 #define __set_CP64(cp, op1, Rt, CRm)         __ASM volatile("MCRR p" # cp ", " # op1 ", %Q0, %R0, c" # CRm  : : "r" (Rt) : "memory" )
858 
859 #include "cmsis_cp15.h"
860 
861 /** \brief  Enable Floating Point Unit
862 
863   Critical section, called from undef handler, so systick is disabled
864  */
__FPU_Enable(void)865 __STATIC_INLINE void __FPU_Enable(void)
866 {
867   __ASM volatile(
868     //Permit access to VFP/NEON, registers by modifying CPACR
869     "        MRC     p15,0,R1,c1,c0,2  \n"
870     "        ORR     R1,R1,#0x00F00000 \n"
871     "        MCR     p15,0,R1,c1,c0,2  \n"
872 
873     //Ensure that subsequent instructions occur in the context of VFP/NEON access permitted
874     "        ISB                       \n"
875 
876     //Enable VFP/NEON
877     "        VMRS    R1,FPEXC          \n"
878     "        ORR     R1,R1,#0x40000000 \n"
879     "        VMSR    FPEXC,R1          \n"
880 
881     //Initialise VFP/NEON registers to 0
882     "        MOV     R2,#0             \n"
883 
884     //Initialise D16 registers to 0
885     "        VMOV    D0, R2,R2         \n"
886     "        VMOV    D1, R2,R2         \n"
887     "        VMOV    D2, R2,R2         \n"
888     "        VMOV    D3, R2,R2         \n"
889     "        VMOV    D4, R2,R2         \n"
890     "        VMOV    D5, R2,R2         \n"
891     "        VMOV    D6, R2,R2         \n"
892     "        VMOV    D7, R2,R2         \n"
893     "        VMOV    D8, R2,R2         \n"
894     "        VMOV    D9, R2,R2         \n"
895     "        VMOV    D10,R2,R2         \n"
896     "        VMOV    D11,R2,R2         \n"
897     "        VMOV    D12,R2,R2         \n"
898     "        VMOV    D13,R2,R2         \n"
899     "        VMOV    D14,R2,R2         \n"
900     "        VMOV    D15,R2,R2         \n"
901 
902 #if (defined(__ARM_NEON) && (__ARM_NEON == 1))
903     //Initialise D32 registers to 0
904     "        VMOV    D16,R2,R2         \n"
905     "        VMOV    D17,R2,R2         \n"
906     "        VMOV    D18,R2,R2         \n"
907     "        VMOV    D19,R2,R2         \n"
908     "        VMOV    D20,R2,R2         \n"
909     "        VMOV    D21,R2,R2         \n"
910     "        VMOV    D22,R2,R2         \n"
911     "        VMOV    D23,R2,R2         \n"
912     "        VMOV    D24,R2,R2         \n"
913     "        VMOV    D25,R2,R2         \n"
914     "        VMOV    D26,R2,R2         \n"
915     "        VMOV    D27,R2,R2         \n"
916     "        VMOV    D28,R2,R2         \n"
917     "        VMOV    D29,R2,R2         \n"
918     "        VMOV    D30,R2,R2         \n"
919     "        VMOV    D31,R2,R2         \n"
920 #endif
921 
922     //Initialise FPSCR to a known state
923     "        VMRS    R1,FPSCR          \n"
924     "        LDR     R2,=0x00086060    \n" //Mask off all bits that do not have to be preserved. Non-preserved bits can/should be zero.
925     "        AND     R1,R1,R2          \n"
926     "        VMSR    FPSCR,R1            "
927     : : : "cc", "r1", "r2"
928   );
929 }
930 
931 #pragma GCC diagnostic pop
932 
933 #endif /* __CMSIS_GCC_H */
934