-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCMakeLists.txt
161 lines (132 loc) · 4.67 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
cmake_minimum_required(VERSION 3.22)
# this is the board we are using, we need to set this to use the correct board header and bootloader
# the board file is: src/Hardware/Board/xerxes_rp2040.h
set(PICO_BOARD "xerxes_rp2040")
set(PICO_BOARD_HEADER_DIRS "${CMAKE_CURRENT_LIST_DIR}/src/Hardware/Board")
# this must be first to set the correct toolchain
include(pico_sdk_import.cmake)
# We also need PICO EXTRAS
include(pico_extras_import.cmake)
set(PROJECT_NAME sensor-rp2040)
project(${PROJECT_NAME} C CXX ASM)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_C_STANDARD 11)
set(PICO_CXX_ENABLE_EXCEPTIONS 1)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}/cmake")
set(DEVICE_TYPES "SCL3300" "SCL3300a" "SCL3400" "AnalogInput" "DiscreteAnalog"
"4DI4DO" "ABP" "hx711" "Encoder" "Cutter" "LightSound" "temperature" "lis2dw12" "LIS2" "lis2xy")
set(DEVICE_TYPE_HINT "Possible options for DEVICE_TYPE: ${DEVICE_TYPES}")
# parse version and build number from git
execute_process(
COMMAND git describe --long
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
add_compile_definitions(__VERSION="${VERSION}")
message("Version: ${VERSION}")
execute_process(
COMMAND git describe
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE TAG
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT DEFINED DEVICE_ADDRESS)
set(DEVICE_ADDRESS 0)
endif()
message("DEVICE_ADDRESS set to ${DEVICE_ADDRESS}")
add_compile_definitions(__DEVICE_ADDRESS=${DEVICE_ADDRESS})
# check if ${CLKDIV} is set and set it to 4 if not - necessary for some boards
if(NOT DEFINED CLKDIV)
set(CLKDIV 8)
endif()
add_compile_definitions(__CLKDIV=${CLKDIV})
if(NOT DEFINED LOG_LEVEL)
set(LOG_LEVEL 1)
endif()
add_compile_definitions(_LOG_LEVEL=${LOG_LEVEL})
if(${LOG_LEVEL} EQUAL 1)
message("LOG_LEVEL set to ${LOG_LEVEL} = ERROR")
elseif(${LOG_LEVEL} EQUAL 2)
message("LOG_LEVEL set to ${LOG_LEVEL} = WARNING")
elseif(${LOG_LEVEL} EQUAL 3)
message("LOG_LEVEL set to ${LOG_LEVEL} = INFO")
elseif(${LOG_LEVEL} EQUAL 4)
message("LOG_LEVEL set to ${LOG_LEVEL} = DEBUG")
elseif(${LOG_LEVEL} EQUAL 5)
message("LOG_LEVEL set to ${LOG_LEVEL} = TRACE")
endif()
# check if ${DEVICE_TYPE} is set and is one of the possible options
if(NOT DEFINED DEVICE_TYPE)
message(FATAL_ERROR "DEVICE_TYPE not set, use -DDEVICE_TYPE=...\n${DEVICE_TYPE_HINT}")
elseif(NOT ${DEVICE_TYPE} IN_LIST DEVICE_TYPES)
message(FATAL_ERROR "DEVICE_TYPE '${DEVICE_TYPE}' is incorrect, use -DDEVICE_TYPE=...\n${DEVICE_TYPE_HINT}")
endif()
# include the correct device config file based on the DEVICE_TYPE set
include(device-config) # this will set DEVICE_SOURCES and DEVICE_HEADER
if(NOT DEFINED DEVICE_HEADER)
message(FATAL_ERROR "DEVICE_HEADER not set")
endif()
message("DEVICE_HEADER set to ${DEVICE_HEADER}")
# Create the header file using configure_file
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/src/Sensors/SensorHeader.hpp.in
${CMAKE_CURRENT_SOURCE_DIR}/src/Sensors/SensorHeader.hpp
)
# print warning to clearly show which device type is used and which CLKDIV is set
message(WARNING "\nDEVICE_TYPE set to '${DEVICE_TYPE}'."
"\nCLKDIV set to ${CLKDIV}, increase this "
"value if device is not responding after reboot.")
pico_sdk_init()
# find xerxes-protocol library
set(xerxes-protocol_DIR "lib/xerxes-protocol-cpp")
find_package(xerxes-protocol REQUIRED)
add_library(xerxes-protocol STATIC ${xerxes-protocol_SOURCES})
include_directories(
"src"
${xerxes-protocol_INCLUDE_DIRS}
)
add_executable(
${PROJECT_NAME}
src/Communication/Callbacks.cpp
src/Hardware/ClockUtils.cpp
src/Hardware/Sleep.cpp
src/Hardware/InitUtils.cpp
src/Hardware/UserFlash.cpp
src/Communication/RS485.cpp
src/Core/Slave.cpp
src/Core/Register.cpp
src/Sensors/Peripheral.cpp
src/Sensors/Sensor.cpp
src/Utils/Gpio.cpp
${DEVICE_SOURCES} # this is automatically generated in cmake/device-config.cmake
src/main.cpp
)
target_link_libraries(
${PROJECT_NAME}
pico_stdlib
pico_multicore
hardware_rtc
hardware_gpio
hardware_adc
hardware_uart
hardware_pwm
hardware_sleep
hardware_flash
hardware_sync
hardware_spi
hardware_i2c
xerxes-protocol
)
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME ${DEVICE_TYPE}_0x${DEVICE_ADDRESS}_${CMAKE_BUILD_TYPE}_${TAG})
# enable usb output, disable uart output
pico_enable_stdio_usb(${PROJECT_NAME} 1)
pico_enable_stdio_uart(${PROJECT_NAME} 0)
# pico_enable_stdio_uart(${PROJECT_NAME} 1)
pico_add_extra_outputs(${PROJECT_NAME})
# copy .UF2 binary to the RP2040
install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/${DEVICE_TYPE}_0x${DEVICE_ADDRESS}_${CMAKE_BUILD_TYPE}_${TAG}.uf2
DESTINATION /media/$ENV{USER}/RPI-RP2/${DEVICE_TYPE}_0x${DEVICE_ADDRESS}_${CMAKE_BUILD_TYPE}_${TAG}.uf2
)