Skip to content

Commit b733e21

Browse files
author
Felipe Torrezan
committed
example: custom build configurations
Adds an interactive example on how to create custom build configurations in CMake that make use of the IAR C/C++ Compiler optimization levels for higher speeds. Signed-off-by: Felipe Torrezan <[email protected]>
1 parent 544aa06 commit b733e21

6 files changed

Lines changed: 188 additions & 0 deletions

File tree

examples/custom/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
cmake_minimum_required(VERSION 4.1.2)
2+
3+
# TODO 1: Include CMake modules to the project
4+
5+
project(custom)
6+
7+
# Add multiple executables
8+
add_executable(sqrt_test)
9+
add_executable(sqrtf_test)
10+
11+
target_sources(sqrt_test PRIVATE
12+
sqrt.c
13+
)
14+
15+
target_sources(sqrtf_test PRIVATE
16+
sqrtf.c
17+
)
18+
19+
# Set multiple target properties at once
20+
set_target_properties(
21+
sqrt_test
22+
sqrtf_test
23+
PROPERTIES
24+
COMPILE_OPTIONS --cpu=cortex-m4
25+
LINK_OPTIONS --semihosting
26+
)
27+
28+
# Add simulated tests
29+
iar_cspysim(sqrt_test)
30+
iar_cspysim(sqrtf_test)

examples/custom/iar-cspysim.cmake

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
include_guard()
2+
3+
enable_testing()
4+
5+
# Facilitate adding C-SPY tests driven by CTest
6+
macro(iar_cspysim TARGET)
7+
find_program(CSPYBAT
8+
NAMES CSpyBat CSpyBat.exe
9+
HINTS /opt/iar/cxarm /opt/iarsystems/bxarm /iar/ewarm-9.70.1
10+
PATH_SUFFIXES common/bin
11+
REQUIRED
12+
)
13+
cmake_path(GET CSPYBAT PARENT_PATH COMMON_DIR)
14+
15+
find_library(LIBPROC
16+
NAMES libarmproc.so libarmPROC.so armproc.dll
17+
HINTS /opt/iar/cxarm /opt/iarsystems/bxarm /iar/ewarm-9.70.1
18+
PATH_SUFFIXES arm/bin
19+
REQUIRED
20+
)
21+
find_library(LIBSIM
22+
NAMES libarmsim2.so libarmSIM2.so armsim2.dll
23+
HINTS /opt/iar/cxarm /opt/iarsystems/bxarm /iar/ewarm-9.70.1
24+
PATH_SUFFIXES arm/bin
25+
REQUIRED
26+
)
27+
find_library(LIBSUPPORT
28+
NAMES libarmLibsupportUniversal.so armLibsupportUniversal.dll
29+
HINTS /opt/iar/cxarm /opt/iarsystems/bxarm /iar/ewarm-9.70.1
30+
PATH_SUFFIXES arm/bin
31+
REQUIRED
32+
)
33+
34+
add_test(
35+
NAME ${TARGET}
36+
COMMAND ${CSPYBAT} ${LIBPROC} ${LIBSIM}
37+
--plugin=${LIBSUPPORT}
38+
--debug_file=$<TARGET_FILE:${TARGET}>
39+
--macro=${CMAKE_CURRENT_SOURCE_DIR}/systick.mac
40+
--silent
41+
--backend
42+
--cpu=cortex-m4
43+
--semihosting )
44+
# More than zero ticks are expected for this test
45+
set_property(TEST ${TARGET} PROPERTY PASS_REGULAR_EXPRESSION "ticks: ")
46+
endmacro()

examples/custom/iar-custom.cmake

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
include_guard()
2+
3+
# Enable all CMake default build configurations
4+
list(APPEND CMAKE_CONFIGURATION_TYPES Debug)
5+
list(APPEND CMAKE_CONFIGURATION_TYPES Release)
6+
list(APPEND CMAKE_CONFIGURATION_TYPES RelWithDebInfo)
7+
list(APPEND CMAKE_CONFIGURATION_TYPES MinSizeRel)
8+
9+
# Add IAR C/C++ Compiler custom configuration for High Speed
10+
list(APPEND CMAKE_CONFIGURATION_TYPES HighSpeed)
11+
set(CMAKE_C_FLAGS_HIGHSPEED " -Ohs" CACHE INTERNAL "")
12+
set(CMAKE_CXX_FLAGS_HIGHSPEED " -Ohs" CACHE INTERNAL "")
13+
14+
# Add IAR C/C++ Compiler custom configuration for Maximum Speed
15+
list(APPEND CMAKE_CONFIGURATION_TYPES MaxSpeed)
16+
set(CMAKE_C_FLAGS_MAXSPEED " -Ohs --no_size_constraints" CACHE INTERNAL "")
17+
set(CMAKE_CXX_FLAGS_MAXSPEED " -Ohs --no_size_constraints" CACHE INTERNAL "")

examples/custom/sqrt.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**************************************************
2+
*
3+
* Copyright (c) 2025 IAR Systems AB.
4+
*
5+
* See LICENSE for detailed license information.
6+
*
7+
**************************************************/
8+
9+
#include <intrinsics.h>
10+
#include <math.h>
11+
#include <stdint.h>
12+
#include <stdio.h>
13+
14+
#define ITERATIONS 100000
15+
#define INTERRUPT 10000
16+
17+
const float f_input = 2.0f;
18+
float f_result;
19+
uint32_t ticks = 0;
20+
21+
void SysTick_Handler(void);
22+
void SysTick_Handler(void) {
23+
ticks++;
24+
}
25+
26+
void main()
27+
{
28+
__enable_interrupt();
29+
30+
for (uint32_t i = 0; i < ITERATIONS; i++)
31+
f_result = sqrtf(f_input);
32+
33+
__disable_interrupt();
34+
35+
printf("ticks: %u\n", ticks);
36+
printf("cycles: %u\n", ticks * INTERRUPT);
37+
printf("cycles/sqrtf(): %f\n", (float)(ticks * INTERRUPT / ITERATIONS ));
38+
}
39+

examples/custom/sqrtf.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**************************************************
2+
*
3+
* Copyright (c) 2025 IAR Systems AB.
4+
*
5+
* See LICENSE for detailed license information.
6+
*
7+
**************************************************/
8+
9+
#include <intrinsics.h>
10+
#include <math.h>
11+
#include <stdint.h>
12+
#include <stdio.h>
13+
14+
#define ITERATIONS 100000
15+
#define INTERRUPT 10000
16+
17+
const double d_input = 2.0;
18+
double d_result;
19+
uint32_t ticks = 0;
20+
21+
void SysTick_Handler(void);
22+
void SysTick_Handler(void) {
23+
ticks++;
24+
}
25+
26+
void main()
27+
{
28+
__enable_interrupt();
29+
30+
for (uint32_t i = 0; i < ITERATIONS; i++)
31+
d_result = sqrt(d_input);
32+
33+
__disable_interrupt();
34+
35+
printf("ticks: %u\n", ticks);
36+
printf("cycles: %u\n", ticks * INTERRUPT);
37+
printf("cycles/sqrtf(): %f\n", (float)(ticks * INTERRUPT / ITERATIONS ));
38+
}
39+

examples/custom/systick.mac

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**************************************************
2+
*
3+
* Copyright (c) 2025 IAR Systems AB.
4+
*
5+
* See LICENSE for detailed license information.
6+
*
7+
**************************************************/
8+
9+
__var _SysTick_Interrupt;
10+
11+
execUserSetup() {
12+
_SysTick_Interrupt = __orderInterrupt("SysTick", 0, 10000, 0, 0, 0, 100);
13+
}
14+
15+
execUserExit() {
16+
__cancelInterrupt( _SysTick_Interrupt );
17+
}

0 commit comments

Comments
 (0)