-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
executable file
·135 lines (115 loc) · 5.36 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
cmake_minimum_required(VERSION 2.8)
#set(PROG_TYPE arduino)
set(PROG_TYPE stk500v1) ## Apparently ArduinoISP is stk500v1
set(USBPORT /dev/tty.usbmodemFD121)
#set(USBPORT /dev/tty.usbmodemFA131)
# extra arguments to avrdude: baud rate, chip type, -F flag, etc.
set(PROG_ARGS -b 19200 -P ${USBPORT})
# Variables regarding the AVR chip
set(MCU atmega328)
set(F_CPU 8000000)
set(BAUD 9600)
add_definitions(-DF_CPU=${F_CPU})
# Custom fuse for: make fuse_custom
# include the -U
set(CUSTOM_FUSE -U lfuse:w:0xe2:m)# -U hfuse:w:0xdf:m -U efuse:w:0xff:m)
# program names
set(AVRCPP avr-g++)
set(AVRC avr-gcc)
set(AVRSTRIP avr-strip)
set(OBJCOPY avr-objcopy)
set(OBJDUMP avr-objdump)
set(AVRSIZE avr-size)
set(AVRDUDE avrdude)
# Sets the compiler
# Needs to come before the project function
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_CXX_COMPILER ${AVRCPP})
set(CMAKE_C_COMPILER ${AVRC})
set(CMAKE_ASM_COMPILER ${AVRC})
project (main C CXX ASM)
# Important project paths
set(BASE_PATH "${${PROJECT_NAME}_SOURCE_DIR}")
set(INC_PATH "${BASE_PATH}/include")
set(SRC_PATH "${BASE_PATH}/src")
set(LIB_DIR_PATH "${BASE_PATH}/lib")
# Files to be compiled
file(GLOB SRC_FILES "${SRC_PATH}/*.cpp"
"${SRC_PATH}/*.cc"
"${SRC_PATH}/*.c"
"${SRC_PATH}/*.cxx"
"${SRC_PATH}/*.S"
"${SRC_PATH}/*.s"
"${SRC_PATH}/*.sx"
"${SRC_PATH}/*.asm")
set(LIB_SRC_FILES)
set(LIB_INC_PATH)
file(GLOB LIBRARIES "${LIB_DIR_PATH}/*")
foreach(subdir ${LIBRARIES})
file(GLOB lib_files "${subdir}/*.cpp"
"${subdir}/*.cc"
"${subdir}/*.c"
"${subdir}/*.cxx"
"${subdir}/*.S"
"${subdir}/*.s"
"${subdir}/*.sx"
"${subdir}/*.asm")
if(IS_DIRECTORY ${subdir})
list(APPEND LIB_INC_PATH "${subdir}")
endif()
list(APPEND LIB_SRC_FILES "${lib_files}")
endforeach()
# Compiler flags
set(CSTANDARD "-std=gnu99")
set(CDEBUG "-gstabs -g -ggdb")
set(CWARN "-Wall -Wstrict-prototypes -Wl,--gc-sections -Wl,--relax")
set(CTUNING "-funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -ffunction-sections -fdata-sections")
set(COPT "-Os -lm -lprintf_flt")
set(CMCU "-mmcu=${MCU}")
set(CDEFS "-DF_CPU=${F_CPU} -DBAUD=${BAUD}")
set(CFLAGS "${CMCU} ${CDEBUG} ${CDEFS} ${COPT} ${CWARN} ${CSTANDARD} ${CTUNING}")
set(CXXFLAGS "${CMCU} ${CDEBUG} ${CDEFS} ${COPT} ${CTUNING}")
set(CMAKE_C_FLAGS "${CFLAGS}")
set(CMAKE_CXX_FLAGS "${CXXFLAGS}")
set(CMAKE_ASM_FLAGS "${CFLAGS}")
# Project setup
include_directories(${INC_PATH} ${LIB_INC_PATH})
add_executable(${PROJECT_NAME} ${SRC_FILES} ${LIB_SRC_FILES})
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "${PROJECT_NAME}.elf")
# Compiling targets
add_custom_target(strip ALL ${AVRSTRIP} "${PROJECT_NAME}.elf" DEPENDS ${PROJECT_NAME})
add_custom_target(hex ALL ${OBJCOPY} -R .eeprom -O ihex "${PROJECT_NAME}.elf" "${PROJECT_NAME}.hex" DEPENDS strip)
add_custom_target(eeprom ${OBJCOPY} -j .eeprom --change-section-lma .eeprom=0 -O ihex "${PROJECT_NAME}.elf" "${PROJECT_NAME}.eeprom" DEPENDS strip)
add_custom_target(disassemble ${OBJDUMP} -S "${PROJECT_NAME}.elf" > "${PROJECT_NAME}.lst" DEPENDS strip)
# Flashing targets
add_custom_target(flash ${AVRDUDE} -c ${PROG_TYPE} -p ${MCU} ${PROG_ARGS} -U flash:w:${PROJECT_NAME}.hex DEPENDS hex)
add_custom_target(flash_eeprom ${AVRDUDE} -c ${PROG_TYPE} -p ${MCU} ${PROG_ARGS} -U eeprom:w:${PROJECT_NAME}.hex DEPENDS eeprom)
# Fuses (For ATMega328P-PU, Calculated using http://eleccelerator.com/fusecalc/fusecalc.php?chip=atmega328p)
add_custom_target(reset ${AVRDUDE} -c ${PROG_TYPE} -p ${MCU} ${PROG_ARGS} -e)
add_custom_target(fuses_custom ${AVRDUDE} -c ${PROG_TYPE} -p ${MCU} ${PROG_ARGS} ${CUSTOM_FUSE})
add_custom_target(fuses_1mhz ${AVRDUDE} -c ${PROG_TYPE} -p ${MCU} ${PROG_ARGS} -U lfuse:w:0x62:m)
add_custom_target(fuses_8mhz ${AVRDUDE} -c ${PROG_TYPE} -p ${MCU} ${PROG_ARGS} -U lfuse:w:0xE2:m)
add_custom_target(fuses_16mhz ${AVRDUDE} -c ${PROG_TYPE} -p ${MCU} ${PROG_ARGS} -U lfuse:w:0xFF:m)
add_custom_target(fuses_uno ${AVRDUDE} -c ${PROG_TYPE} -p ${MCU} ${PROG_ARGS} -U lfuse:w:0xFF:m -U hfuse:w:0xDE:m -U efuse:w:0x05:m)
add_custom_target(set_eeprom_save_fuse ${AVRDUDE} -c ${PROG_TYPE} -p ${MCU} ${PROG_ARGS} -U hfuse:w:0xD1:m)
add_custom_target(clear_eeprom_save_fuse ${AVRDUDE} -c ${PROG_TYPE} -p ${MCU} ${PROG_ARGS} -U hfuse:w:0xD9:m)
# Utilities targets
add_custom_target(avr_terminal ${AVRDUDE} -c ${PROG_TYPE} -p ${MCU} ${PROG_ARGS} -nt)
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${PROJECT_NAME}.hex;${PROJECT_NAME}.eeprom;${PROJECT_NAME}.lst")
# Show avr-size after hex built
add_custom_command(TARGET hex POST_BUILD
COMMAND ${AVRSIZE} -C --mcu=${MCU} "${PROJECT_NAME}.elf")
# Config logging
message("* ")
message("* Project Name:\t${PROJECT_NAME}")
message("* Project Source:\t${SRC_PATH}")
message("* Project Include:\t${INC_PATH}")
message("* Library Include:\t${LIB_INC_PATH}")
message("* ")
message("* Project Source Files:\t${SRC_FILES}")
message("* Library Source Files:\t${LIB_SRC_FILES}")
message("* ")
message("* C Flags:\t${CMAKE_C_FLAGS}")
message("* ")
message("* CXX Flags:\t${CMAKE_C_FLAGS}")
message("* ")