-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
101 lines (88 loc) · 3.04 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
cmake_minimum_required(VERSION 3.16)
# AVR Fuses
set(E_FUSE 0xfd)
set(H_FUSE 0xd8)
set(L_FUSE 0xff)
set(LOCK_BIT 0xcf)
set(CMAKE_C_STANDARD 99)
# set target system
set(CMAKE_SYSTEM_NAME Generic)
# remove compiler tests against local libs
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
## AVR Chip Configuration
set(F_CPU 16000000UL)
set(MCU atmega2560)
set(BAUDRATE 115200)
set(AVRDUDE_PROG_TYPE "wiring")
# local configurable options
set(SERIAL_PORT "/dev/tty.usbserial-A10JTC54")
set(CGT_ROOT "/usr/local/bin")
# mmcu MUST be passed to bot the compiler and linker, this handle the linker
set(CMAKE_EXE_LINKER_FLAGS -mmcu=${MCU})
# Pass defines to compiler
add_definitions(
-DF_CPU=${F_CPU}
-DBAUD=${BAUD}
)
add_compile_options(
-mmcu=${MCU}
-std=gnu99
-Os
-Wall
-Wno-main
-Wundef
-pedantic
-Wstrict-prototypes
-Werror
-Wfatal-errors
-Wl,--relax,--gc-sections
-g
-gdwarf-2
-funsigned-char
-funsigned-bitfields
-fpack-struct
-fshort-enums
-ffunction-sections
-fdata-sections
-fno-split-wide-types
-fno-tree-scev-cprop
)
# find programs
find_program(AVR_CC NAMES avr-gcc PATHS ${CGT_ROOT} NO_DEFAULT_PATH)
find_program(AVR_CXX NAMES avr-g++ PATHS ${CGT_ROOT} NO_DEFAULT_PATH)
find_program(AVR_OBJCOPY NAMES avr-objcopy PATHS ${CGT_ROOT} NO_DEFAULT_PATH)
find_program(AVR_STRIP NAMES avr-strip PATHS ${CGT_ROOT} NO_DEFAULT_PATH)
find_program(AVRDUDE NAMES avrdude PATHS ${CGT_ROOT} NO_DEFAULT_PATH)
set(CMAKE_ASM_COMPILER ${AVR_CC})
set(CMAKE_C_COMPILER ${AVR_CC})
set(CMAKE_CXX_COMPILER ${AVR_CXX})
# global project name
set(PROJECT_NAME "avr")
project(
${PROJECT_NAME}
LANGUAGES CXX C
VERSION "1.0.0"
DESCRIPTION "Avr-gcc firmware toolchain demo"
)
# cmake build guard
if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
message(
FATAL_ERROR
"In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there."
)
endif()
message(STATUS "Building avr-gcc demo")
add_executable(${PROJECT_NAME} src/main.c src/usart.c)
include_directories(include)
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME ${PROJECT_NAME}.elf)
message(STATUS "Adding custom targets")
# Strip binary for upload
add_custom_target(strip ALL ${AVR_STRIP} ${PROJECT_NAME}.elf DEPENDS ${PROJECT_NAME})
# Transform binary into hex file, we ignore the eeprom segments in the step
add_custom_target(hex ALL ${AVR_OBJCOPY} -R .eeprom -O ihex ${PROJECT_NAME}.elf ${PROJECT_NAME}.hex DEPENDS strip)
# Upload the firmware with avrdude
add_custom_target(flash ${AVRDUDE} -c ${AVRDUDE_PROG_TYPE} -p${MCU} -P${SERIAL_PORT} -b${BAUDRATE} -D -Uflash:w:${PROJECT_NAME}.hex:i DEPENDS hex)
# Burn fuses
add_custom_target(fuses ${AVRDUDE} -c${AVRDUDE_PROG_TYPE} -p ${MCU} -P${SERIAL_PORT} -b${BAUDRATE} -Ulfuse:w:${L_FUSE}:m -Uhfuse:w:${H_FUSE}:m -Uefuse:w:${E_FUSE}:m -Ulock:w:${LOCK_BIT}:m)
message(STATUS "Flagging additional files for cleaning")
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${PROJECT_NAME}.hex;${PROJECT_NAME}.eeprom;${PROJECT_NAME}.lst")