1 /**************************************************************************************************/
3 \defgroup compiler_conntrol_gr Compiler Control
4 \brief Compiler agnostic \#define symbols for generic C/C++ source code
6 The CMSIS-Core provides the header file <b>cmsis_compiler.h</b> with consistent \#define symbols for generate C or C++ source files that should be compiler agnostic.
7 Each CMSIS compliant compiler should support the functionality described in this section.
9 The header file <b>cmsis_compiler.h</b> is also included by each \ref device_h_pg so that these definitions are available.
15 \brief Set to 1 when generating code for Armv6-M architecture
17 The <b>\#define __ARM_ARCH_6M__</b> is set to 1 when generating code for the Armv6-M architecture. This architecture is for example used by the Cortex-M0, Cortex-M0+, and Cortex-M1 processor.
19 #define __ARM_ARCH_6M__
23 \brief Set to 1 when generating code for Armv7-M architecture
25 The <b>\#define __ARM_ARCH_7M__</b> is set to 1 when generating code for the Armv7-M architecture. This architecture is for example used by the Cortex-M3 processor.
27 #define __ARM_ARCH_7M__
31 \brief Set to 1 when generating code for Armv7-M architecture with FPU
33 The <b>\#define __ARM_ARCH_7EM__</b> is set to 1 when generating code for the Armv7-M architecture with floating point extension. This architecture is for example implemented by the Cortex-M4 processor with FPU.
35 #define __ARM_ARCH_7EM__
42 \def __ARM_ARCH_8M_BASE__
43 \brief Set to 1 when generating code for Armv8-M Baseline architecture
45 The <b>\#define __ARM_ARCH_8M_BASE__</b> is set to 1 when generating code for the Armv8-M architecture baseline variant.
46 This architecture is for example implemented by the Cortex-M23 processor.
48 #define __ARM_ARCH_8M_BASE__
51 \def __ARM_ARCH_8M_MAIN__
52 \brief Set to 1 when generating code for Armv8-M Mainline architecture
54 The <b>\#define __ARM_ARCH_8M_MAIN__</b> is set to 1 when generating code for the Armv8-M architecture mainline variant.
55 This architecture is for example implemented by the Cortex-M33 processor.
57 #define __ARM_ARCH_8M_MAIN__
60 \def __ARM_ARCH_8_1M_MAIN__
61 \brief Set to 1 when generating code for Armv8.1-M architecture
63 The <b>\#define __ARM_ARCH_8_1M_MAIN__</b> is set to 1 when generating code for the Armv8.1-M architecture.
64 This architecture is for example implemented by the Cortex-M55 processor.
66 #define __ARM_ARCH_8_1M_MAIN__
73 /**************************************************************************************************/
76 \brief Pass information from the compiler to the assembler.
78 The \b __ASM keyword can declare or define an embedded assembly function or incorporate inline assembly into a function
79 (shown in the code example below).
83 // Reverse bit order of value
85 __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __RBIT(uint32_t value)
89 __ASM volatile ("rbit %0, %1" : "=r" (result) : "r" (value) );
97 /**************************************************************************************************/
100 \brief Recommend that function should be inlined by the compiler.
102 Inline functions offer a trade-off between code size and performance. By default, the compiler decides during optimization whether to
103 inline code or not. The \b __INLINE attribute gives the compiler an hint to inline this function. Still, the compiler may decide not to inline
104 the function. As the function is global an callable function is also generated.
108 const uint32_t led_mask[] = {1U << 4, 1U << 5, 1U << 6, 1U << 7};
110 /*------------------------------------------------------------------------------
112 *------------------------------------------------------------------------------*/
113 __INLINE static void LED_On (uint32_t led) {
115 PTD->PCOR = led_mask[led];
122 /**************************************************************************************************/
125 \brief Define a static function that may be inlined by the compiler.
127 Defines a static function that may be inlined by the compiler. If the compiler generates inline code for
128 all calls to this functions, no additional function implementation is generated which may further optimize space.
132 \\ Get Interrupt Vector
133 __STATIC_INLINE uint32_t NVIC_GetVector(IRQn_Type IRQn)
135 uint32_t *vectors = (uint32_t *) ((uintptr_t) SCB->VTOR);
136 return vectors[(int32_t)IRQn + NVIC_USER_IRQ_OFFSET];
141 #define __STATIC_INLINE
143 /**************************************************************************************************/
145 \def __STATIC_FORCEINLINE
146 \brief Define a static function that should be always inlined by the compiler.
148 Defines a static function that should be always inlined by the compiler.
151 For compilers that do not allow to force function inlining, the macro maps to \ref __STATIC_INLINE.
155 \\ Get Interrupt Vector
156 __STATIC_FORCEINLINE uint32_t NVIC_GetVector(IRQn_Type IRQn)
158 uint32_t *vectors = (uint32_t *) ((uintptr_t) SCB->VTOR);
159 return vectors[(int32_t)IRQn + NVIC_USER_IRQ_OFFSET];
164 #define __STATIC_FORCEINLINE
166 /**************************************************************************************************/
169 \brief Inform the compiler that a function does not return.
171 Informs the compiler that the function does not return. The compiler can then perform optimizations by
172 removing code that is never reached.
176 // OS idle demon (running when no other thread is ready to run).
178 __NO_RETURN void os_idle_demon (void);
184 /**************************************************************************************************/
187 \brief restrict pointer qualifier to enable additional optimizations.
189 The __RESTRICT keyword corresponds to the \b restrict pointer qualifier that has been introduced in C99.
190 __RESTRICT is a hint to the compiler that enables additional optimizations. It specifies that for the lifetime
191 of the pointer, only the pointer itself or a value directly derived from it (such as pointer + 1) is used to access
192 the object. The compiler may therefore ignore potential pointer aliasing effects and perform additional optimizations.
195 For compilers that do not support the restrict keyword, __RESTRICT is defined as an empty macro and a warning is issued.
199 __STATIC_INLINE void ARM_MPU_OrderedMemcpy (volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)
202 for (i = 0U; i < len; ++i)
204 dst[i] = src[i]; // Since src is restrict, the compiler can assume that dst and src are not overlapping may load multiple values at a time
212 /**************************************************************************************************/
215 \brief Inform that a variable shall be retained in executable image.
217 Definitions tagged with \b __USED in the source code should be not removed by the linker when detected as unused.
221 /* Export following variables for debugging */
222 __USED uint32_t const CMSIS_RTOS_API_Version = osCMSIS;
223 __USED uint32_t const CMSIS_RTOS_RTX_Version = osCMSIS_RTX;
224 __USED uint32_t const os_clockrate = OS_TICK;
225 __USED uint32_t const os_timernum = 0;
231 /**************************************************************************************************/
234 \brief Export a function or variable weakly to allow overwrites.
236 Functions defined with \b __WEAK export their symbols weakly. A weakly defined function behaves like a normally defined
237 function unless a non-weakly defined function of the same name is linked into the same image. If both a non-weakly defined
238 function and a weakly defined function exist in the same image then all calls to the function resolve to call the non-weak
241 Functions declared with \b __WEAK and then defined without \b __WEAK behave as non-weak functions.
245 __WEAK void SystemInit(void)
248 SystemCoreClockSetup();
255 /**************************************************************************************************/
258 \brief Request smallest possible alignment.
260 Specifies that a type must have the smallest possible alignment.
266 uint32_t u32[2] __PACKED;
273 /**************************************************************************************************/
276 \brief Request smallest possible alignment for a structure.
278 Specifies that a structure must have the smallest possible alignment.
282 __PACKED_STRUCT foo {
290 #define __PACKED_STRUCT
292 /**************************************************************************************************/
294 \def __UNALIGNED_UINT16_READ
295 \brief Pointer for unaligned read of a uint16_t variable.
297 Defines a pointer to a uint16_t from an address that does not need to be aligned. This can then be used in read
298 operations. The compiler will generate the appropriate access (aligned or non-aligned) depending on the underlying Arm
299 processor core and compiler settings.
305 void test (uint8_t *ptr) {
306 val16 = __UNALIGNED_UINT16_READ(ptr);
311 #define __UNALIGNED_UINT16_READ
313 /**************************************************************************************************/
315 \def __UNALIGNED_UINT16_WRITE
316 \brief Pointer for unaligned write of a uint16_t variable.
318 Defines a pointer to a uint16_t from an address that does not need to be aligned. This can then be used in write
319 operations. The compiler will generate the appropriate access (aligned or non-aligned) depending on the underlying Arm
320 processor core and compiler settings.
326 void test (uint8_t *ptr) {
327 __UNALIGNED_UINT16_WRITE(ptr, val16);
332 #define __UNALIGNED_UINT16_WRITE
334 /**************************************************************************************************/
336 \def __UNALIGNED_UINT32_READ
337 \brief Pointer for unaligned read of a uint32_t variable.
339 Defines a pointer to a uint32_t from an address that does not need to be aligned. This can then be used in read
340 operations. The compiler will generate the appropriate access (aligned or non-aligned) depending on the underlying Arm
341 processor core and compiler settings.
347 void test (uint8_t *ptr) {
348 val32 = __UNALIGNED_UINT32_READ(ptr);
353 #define __UNALIGNED_UINT32_READ
355 /**************************************************************************************************/
357 \def __UNALIGNED_UINT32_WRITE
358 \brief Pointer for unaligned write of a uint32_t variable.
360 Defines a pointer to a uint32_t from an address that does not need to be aligned. This can then be used in write
361 operations. The compiler will generate the appropriate access (aligned or non-aligned) depending on the underlying Arm
362 processor core and compiler settings.
368 void test (uint8_t *ptr) {
369 __UNALIGNED_UINT32_WRITE(ptr, val32);
374 #define __UNALIGNED_UINT32_WRITE
376 /**************************************************************************************************/
379 \brief Minimum alignment for a variable.
381 Specifies a minimum alignment for a variable or structure field, measured in bytes.
385 uint32_t stack_space[0x100] __ALIGNED(8); // 8-byte alignment required
391 /**************************************************************************************************/
393 \def __COMPILER_BARRIER
394 \brief Barrier to prevent compiler from reordering instructions.
396 This barrier limits the compilers reordering optimizations. It prevents the compiler from swapping
397 instructions resulting from code before and after the barrier.
400 The assignments in the example are independent. Hence the compiler could choose a different order of
401 execution, e.g. for a better pipeline utilization. Using the barrier in between prevents this type
405 void test (uint8_t *ptr) {
413 #define __COMPILER_BARRIER
415 /**************************************************************************************************/
418 \brief Specifies a section name for a variable to simplify variable placement into a non-initialized memory
420 Specifies a section name (e.g, .bss.noinit or .noinit) for a variable that can be used by a linker-script
421 to position that variable into a non-initialized memory.
424 The EventBuffer in the example must not be copy- or zero-initialized. By adding
425 __NO_INIT at variable declaration/definition, and with appropriate linker-script, this variable is
426 positioned into a non-initialized memory.
429 static EventRecord_t EventBuffer[EVENT_RECORD_COUNT] __NO_INIT __ALIGNED(16);
435 /**************************************************************************************************/
438 \brief Creates a symbol as alias to another symbol.
441 The example declares the function Interrupt0_Handler. By default it is just an alias
442 pointing to Default_Handler. In combination with __WEAK modifier this allows giving
443 the function definition at a later point if required.
446 void Interrupt0_Handler (void) __WEAK __ALIAS("Default_Handler");
453 /**************************************************************************************************/
456 \brief Entry function into the user application or library startup.
458 Gives the function to be jumped into right after low level initialization, i.e. SystemInit. This
459 is compiler and library specific. CMSIS specifies common default for supported compilers.
461 \note This define is only intended to be used by the \ref startup_c_pg.
465 void Reset_Handler(void)
467 SystemInit(); /* CMSIS System Initialization */
468 __PROGRAM_START(); /* Enter PreMain (C library entry point) */
472 #define __PROGRAM_START
474 /**************************************************************************************************/
477 \brief Compiler/linker symbol specifying the location of the main stack (MSP).
479 The address of the specified symbol is used to initialize the main stack pointer (MSP) during low
480 level init. This is compiler/linker specific. CMSIS specifies common default for supported compilers.
482 \note This define is only intended to be used by the \ref startup_c_pg.
486 /**************************************************************************************************/
489 \brief Compiler/linker symbol specifying the limit of the main stack (MSP).
491 The address of the specified symbol is used to initialize the main stack pointer limit (MSPLIM on Armv8-M)
492 during low level init. This is compiler/linker specific. CMSIS specifies common default for supported
495 \note This define is only intended to be used by the \ref startup_c_pg.
499 void Reset_Handler(void)
501 __set_MSPLIM((uint32_t)(&__STACK_LIMIT));
507 #define __STACK_LIMIT
509 /**************************************************************************************************/
512 \brief Symbol name used for the (static) interrupt vector table.
514 The given name is used for defining the static (compiler time) interrupt vector table. The name
515 must comply with any compiler/linker conventions, e.g. if used for vector table relocation or debugger
516 awareness. CMSIS specifies common default for supported compilers.
518 \note This define is only intended to be used by the \ref startup_c_pg.
520 #define __VECTOR_TABLE
522 /**************************************************************************************************/
524 \def __VECTOR_TABLE_ATTRIBUTE
525 \brief Additional decl specs to be used when defining the (static) interrupt vector table.
527 The given decl specs are used for defining the static (compiler time) interrupt vector table, e.g.
528 to mark the table as used and force it into a specific linker section. CMSIS specifies common default
529 for supported compilers.
531 \note This define is only intended to be used by the \ref startup_c_pg.
533 #define __VECTOR_TABLE_ATTRIBUTE
535 /** @} */ /** end of compiler_conntrol_gr **/