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 (Cortex-M0, Cortex-M1)
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 (Cortex-M3)
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 (Cortex-M4) 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 used 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
45 The <b>\#define __ARM_ARCH_8M_BASE__</b> is set to 1 when generating code for the Armv8-M architecture baseline variant.
47 #define __ARM_ARCH_8M_BASE__
50 \def __ARM_ARCH_8M_MAIN__
51 \brief Set to 1 when generating code for Armv8-M Mainline
53 The <b>\#define __ARM_ARCH_8M_MAIN__</b> is set to 1 when generating code for the Armv8-M architecture mainline variant.
55 #define __ARM_ARCH_8M_MAIN__
62 /**************************************************************************************************/
65 \brief Pass information from the compiler to the assembler.
67 The \b __ASM keyword can declare or define an embedded assembly function or incorporate inline assembly into a function
68 (shown in the code example below).
72 // Reverse bit order of value
74 __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __RBIT(uint32_t value)
78 __ASM volatile ("rbit %0, %1" : "=r" (result) : "r" (value) );
86 /**************************************************************************************************/
89 \brief Recommend that function should be inlined by the compiler.
91 Inline functions offer a trade-off between code size and performance. By default, the compiler decides during optimization whether to
92 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
93 the function. As the function is global an callable function is also generated.
97 const uint32_t led_mask[] = {1U << 4, 1U << 5, 1U << 6, 1U << 7};
99 /*------------------------------------------------------------------------------
101 *------------------------------------------------------------------------------*/
102 __INLINE static void LED_On (uint32_t led) {
104 PTD->PCOR = led_mask[led];
111 /**************************************************************************************************/
114 \brief Define a static function that may be inlined by the compiler.
116 Defines a static function that may be inlined by the compiler. If the compiler generates inline code for
117 all calls to this functions, no additional function implementation is generated which may further optimize space.
121 \\ Get Interrupt Vector
122 __STATIC_INLINE uint32_t NVIC_GetVector(IRQn_Type IRQn)
124 uint32_t *vectors = (uint32_t *) ((uintptr_t) SCB->VTOR);
125 return vectors[(int32_t)IRQn + NVIC_USER_IRQ_OFFSET];
130 #define __STATIC_INLINE
132 /**************************************************************************************************/
134 \def __STATIC_FORCEINLINE
135 \brief Define a static function that should be always inlined by the compiler.
137 Defines a static function that should be always inlined by the compiler.
140 For compilers that do not allow to force function inlining, the macro maps to \ref __STATIC_INLINE.
144 \\ Get Interrupt Vector
145 __STATIC_FORCEINLINE uint32_t NVIC_GetVector(IRQn_Type IRQn)
147 uint32_t *vectors = (uint32_t *) ((uintptr_t) SCB->VTOR);
148 return vectors[(int32_t)IRQn + NVIC_USER_IRQ_OFFSET];
153 #define __STATIC_FORCEINLINE
155 /**************************************************************************************************/
158 \brief Inform the compiler that a function does not return.
160 Informs the compiler that the function does not return. The compiler can then perform optimizations by
161 removing code that is never reached.
165 // OS idle demon (running when no other thread is ready to run).
167 __NO_RETURN void os_idle_demon (void);
173 /**************************************************************************************************/
176 \brief restrict pointer qualifier to enable additional optimizations.
178 The __RESTRICT keyword corresponds to the \b restrict pointer qualifier that has been introduced in C99.
179 __RESTRICT is a hint to the compiler that enables additional optimizations. It specifies that for the lifetime
180 of the pointer, only the pointer itself or a value directly derived from it (such as pointer + 1) is used to access
181 the object. The compiler may therefore ignore potential pointer aliasing effects and perform additional optimizations.
184 For compilers that do not support the restrict keyword, __RESTRICT is defined as an empty macro and a warning is issued.
188 __STATIC_INLINE void ARM_MPU_OrderedMemcpy (volatile uint32_t* dst, const uint32_t* __RESTRICT src, uint32_t len)
191 for (i = 0U; i < len; ++i)
193 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
201 /**************************************************************************************************/
204 \brief Inform that a variable shall be retained in executable image.
206 Definitions tagged with \b __USED in the source code should be not removed by the linker when detected as unused.
210 /* Export following variables for debugging */
211 __USED uint32_t const CMSIS_RTOS_API_Version = osCMSIS;
212 __USED uint32_t const CMSIS_RTOS_RTX_Version = osCMSIS_RTX;
213 __USED uint32_t const os_clockrate = OS_TICK;
214 __USED uint32_t const os_timernum = 0;
220 /**************************************************************************************************/
223 \brief Export a function or variable weakly to allow overwrites.
225 Functions defined with \b __WEAK export their symbols weakly. A weakly defined function behaves like a normally defined
226 function unless a non-weakly defined function of the same name is linked into the same image. If both a non-weakly defined
227 function and a weakly defined function exist in the same image then all calls to the function resolve to call the non-weak
230 Functions declared with \b __WEAK and then defined without \b __WEAK behave as non-weak functions.
234 __WEAK void SystemInit(void)
237 SystemCoreClockSetup();
244 /**************************************************************************************************/
247 \brief Request smallest possible alignment.
249 Specifies that a type must have the smallest possible alignment.
255 uint32_t u32[2] __PACKED;
262 /**************************************************************************************************/
265 \brief Request smallest possible alignment for a structure.
267 Specifies that a structure must have the smallest possible alignment.
271 __PACKED_STRUCT foo {
279 #define __PACKED_STRUCT
281 /**************************************************************************************************/
283 \def __UNALIGNED_UINT32
284 \brief Pointer for unaligned access of a uint32_t variable.
286 Do not use this macro.
287 It has been superseded by \ref __UNALIGNED_UINT32_READ, \ref __UNALIGNED_UINT32_WRITE and will be removed in the future.
289 Defines a pointer to a uint32_t from an address that does not need to be aligned. This can then be used in read/write
290 operations. The compiler will generate the appropriate access (aligned or non-aligned) depending on the underlying Arm
291 processor core and compiler settings.
297 void test (uint8_t *ptr) {
298 __UNALIGNED_UINT32(ptr) = val32;
303 #define __UNALIGNED_UINT32
305 /**************************************************************************************************/
307 \def __UNALIGNED_UINT16_READ
308 \brief Pointer for unaligned read of a uint16_t variable.
310 Defines a pointer to a uint16_t from an address that does not need to be aligned. This can then be used in read
311 operations. The compiler will generate the appropriate access (aligned or non-aligned) depending on the underlying Arm
312 processor core and compiler settings.
318 void test (uint8_t *ptr) {
319 val16 = __UNALIGNED_UINT16_READ(ptr);
324 #define __UNALIGNED_UINT16_READ
326 /**************************************************************************************************/
328 \def __UNALIGNED_UINT16_WRITE
329 \brief Pointer for unaligned write of a uint16_t variable.
331 Defines a pointer to a uint16_t from an address that does not need to be aligned. This can then be used in write
332 operations. The compiler will generate the appropriate access (aligned or non-aligned) depending on the underlying Arm
333 processor core and compiler settings.
339 void test (uint8_t *ptr) {
340 __UNALIGNED_UINT16_WRITE(ptr, val16);
345 #define __UNALIGNED_UINT16_WRITE
347 /**************************************************************************************************/
349 \def __UNALIGNED_UINT32_READ
350 \brief Pointer for unaligned read of a uint32_t variable.
352 Defines a pointer to a uint32_t from an address that does not need to be aligned. This can then be used in read
353 operations. The compiler will generate the appropriate access (aligned or non-aligned) depending on the underlying Arm
354 processor core and compiler settings.
360 void test (uint8_t *ptr) {
361 val32 = __UNALIGNED_UINT32_READ(ptr);
366 #define __UNALIGNED_UINT32_READ
368 /**************************************************************************************************/
370 \def __UNALIGNED_UINT32_WRITE
371 \brief Pointer for unaligned write of a uint32_t variable.
373 Defines a pointer to a uint32_t from an address that does not need to be aligned. This can then be used in write
374 operations. The compiler will generate the appropriate access (aligned or non-aligned) depending on the underlying Arm
375 processor core and compiler settings.
381 void test (uint8_t *ptr) {
382 __UNALIGNED_UINT32_WRITE(ptr, val32);
387 #define __UNALIGNED_UINT32_WRITE
389 /**************************************************************************************************/
392 \brief Minimum alignment for a variable.
394 Specifies a minimum alignment for a variable or structure field, measured in bytes.
398 uint32_t stack_space[0x100] __ALIGNED(8); // 8-byte alignment required
404 /**************************************************************************************************/
406 \def __COMPILER_BARRIER
407 \brief Barrier to prevent compiler from reordering instructions.
409 This barrier limits the compilers reordering optimizations. It prevents the compiler from swapping
410 instructions resulting from code before and after the barrier.
413 The assignments in the example are independent. Hence the compiler could choose a different order of
414 execution, e.g. for a better pipeline utilization. Using the barrier in between prevents this type
418 void test (uint8_t *ptr) {
426 #define __COMPILER_BARRIER
428 /**************************************************************************************************/
431 \brief Force symbol into uninitialized memory section
433 This puts a symbol (such as a variable) into an uninitialized memory section (e.g, .bss.noinit).
436 The EventBuffer in the example does not need to be copy- or zero-initialized. By adding
437 __NO_INIT this variable is allocated into an uninitialized memory section.
440 static EventRecord_t EventBuffer[EVENT_RECORD_COUNT] __NO_INIT __ALIGNED(16);
446 /**************************************************************************************************/
449 \brief Creates a symbol as alias to another symbol.
452 The example declares the function Interrupt0_Handler. By default it is just an alias
453 pointing to Default_Handler. In combination with __WEAK modifier this allows giving
454 the function definition at a later point if required.
457 void Interrupt0_Handler (void) __WEAK __ALIAS("Default_Handler");
464 /**************************************************************************************************/
467 \brief Entry function into the user application or library startup.
469 Gives the function to be jumped into right after low level initialization, i.e. SystemInit. This
470 is compiler and library specific. CMSIS specifies common default for supported compilers.
472 \note This define is only intended to be used by the \ref startup_c_pg.
476 void Reset_Handler(void)
478 SystemInit(); /* CMSIS System Initialization */
479 __PROGRAM_START(); /* Enter PreMain (C library entry point) */
483 #define __PROGRAM_START
485 /**************************************************************************************************/
488 \brief Compiler/linker symbol specifying the location of the main stack (MSP).
490 The address of the specified symbol is used to initialize the main stack pointer (MSP) during low
491 level init. This is compiler/linker specific. CMSIS specifies common default for supported compilers.
493 \note This define is only intended to be used by the \ref startup_c_pg.
497 /**************************************************************************************************/
500 \brief Compiler/linker symbol specifying the limit of the main stack (MSP).
502 The address of the specified symbol is used to initialize the main stack pointer limit (MSPLIM on Armv8-M)
503 during low level init. This is compiler/linker specific. CMSIS specifies common default for supported
506 \note This define is only intended to be used by the \ref startup_c_pg.
510 void Reset_Handler(void)
512 __set_MSPLIM((uint32_t)(&__STACK_LIMIT));
518 #define __STACK_LIMIT
520 /**************************************************************************************************/
523 \brief Symbol name used for the (static) interrupt vector table.
525 The given name is used for defining the static (compiler time) interrupt vector table. The name
526 must comply with any compiler/linker conventions, e.g. if used for vector table relocation or debugger
527 awareness. CMSIS specifies common default for supported compilers.
529 \note This define is only intended to be used by the \ref startup_c_pg.
531 #define __VECTOR_TABLE
533 /**************************************************************************************************/
535 \def __VECTOR_TABLE_ATTRIBUTE
536 \brief Additional decl specs to be used when defining the (static) interrupt vector table.
538 The given decl specs are used for defining the static (compiler time) interrupt vector table, e.g.
539 to mark the table as used and force it into a specific linker section. CMSIS specifies common default
540 for supported compilers.
542 \note This define is only intended to be used by the \ref startup_c_pg.
544 #define __VECTOR_TABLE_ATTRIBUTE
546 /** @} */ /** end of compiler_conntrol_gr **/