1
+ cmake_minimum_required (VERSION 3.17 )
2
+
3
+ if (NOT DEFINED BOARD )
4
+ message (FATAL_ERROR "BOARD is not defined" )
5
+ endif ()
6
+
7
+ if (NOT DEFINED TOOLCHAIN )
8
+ set (TOOLCHAIN gcc )
9
+ endif ()
10
+
11
+ # include board specific
12
+ include (${CMAKE_CURRENT_LIST_DIR} /src/boards/${BOARD}/board.cmake )
13
+
14
+ # toolchain set up
15
+ if (MCU_VARIANT STREQUAL "nrf5340_application" )
16
+ set (CMAKE_SYSTEM_PROCESSOR cortex-m33 CACHE INTERNAL "System Processor" )
17
+ set (JLINK_DEVICE nrf5340_xxaa_app )
18
+ else ()
19
+ set (CMAKE_SYSTEM_PROCESSOR cortex-m4 CACHE INTERNAL "System Processor" )
20
+ set (JLINK_DEVICE ${MCU_VARIANT} _xxaa )
21
+ endif ()
22
+
23
+ set (CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_LIST_DIR} /cmake/toolchain/arm_${TOOLCHAIN}.cmake )
24
+
25
+ project (Adafruit_nRF52_Bootloader C ASM )
26
+
27
+ set (NRFX ${CMAKE_CURRENT_LIST_DIR} /lib/nrfx )
28
+ set (SDK11 ${CMAKE_CURRENT_LIST_DIR} /lib/sdk11/components )
29
+ set (SDK ${CMAKE_CURRENT_LIST_DIR} /lib/sdk/components )
30
+ set (SOFTDEVICE ${CMAKE_CURRENT_LIST_DIR} /lib/softdevice )
31
+ set (TUSB ${CMAKE_CURRENT_LIST_DIR} /lib/tinyusb/src )
32
+
33
+ #-------------------
34
+ # Bootloader
35
+ #-------------------
36
+ set (CMAKE_EXECUTABLE_SUFFIX .elf )
37
+ add_executable (bootloader )
38
+ #set_target_properties(bootloader PROPERTIES OUTPUT_NAME "${BOARD}_bootloader.elf")
39
+
40
+
41
+ # SD_VERSION can be overwritten by board.cmake
42
+ if (NOT DEFINED SD_VERSION )
43
+ set (SD_VERSION 6.1.1 )
44
+ endif ()
45
+
46
+ set (MBR_HEX ${SOFTDEVICE} /mbr/hex/mbr_nrf52_2.4.1_mbr.hex )
47
+
48
+ target_sources (bootloader PUBLIC
49
+ # src
50
+ src/dfu_ble_svc.c
51
+ src/dfu_init.c
52
+ src/flash_nrf5x.c
53
+ src/main.c
54
+ src/boards/boards.c
55
+ # nrfx
56
+ ${NRFX} /drivers/src/nrfx_power.c
57
+ ${NRFX} /drivers/src/nrfx_nvmc.c
58
+ ${NRFX} /mdk/system_${MCU_VARIANT}.c
59
+ # sdk 11
60
+ ${SDK11} /libraries/bootloader_dfu/bootloader.c
61
+ ${SDK11} /libraries/bootloader_dfu/bootloader_settings.c
62
+ ${SDK11} /libraries/bootloader_dfu/bootloader_util.c
63
+ ${SDK11} /libraries/bootloader_dfu/dfu_transport_serial.c
64
+ ${SDK11} /libraries/bootloader_dfu/dfu_transport_ble.c
65
+ ${SDK11} /libraries/bootloader_dfu/dfu_single_bank.c
66
+ ${SDK11} /ble/ble_services/ble_dfu/ble_dfu.c
67
+ ${SDK11} /ble/ble_services/ble_dis/ble_dis.c
68
+ ${SDK11} /drivers_nrf/pstorage/pstorage_raw.c
69
+ # latest sdk
70
+ ${SDK} /libraries/timer/app_timer.c
71
+ ${SDK} /libraries/scheduler/app_scheduler.c
72
+ ${SDK} /libraries/util/app_error.c
73
+ ${SDK} /libraries/util/app_util_platform.c
74
+ ${SDK} /libraries/crc16/crc16.c
75
+ ${SDK} /libraries/hci/hci_mem_pool.c
76
+ ${SDK} /libraries/hci/hci_slip.c
77
+ ${SDK} /libraries/hci/hci_transport.c
78
+ ${SDK} /libraries/util/nrf_assert.c
79
+ # ASM
80
+ ${NRFX} /mdk/gcc_startup_${MCU_VARIANT}.S
81
+ )
82
+ target_include_directories (bootloader PUBLIC
83
+ src
84
+ src/boards
85
+ src/boards/${BOARD}
86
+ src/cmsis/include
87
+ src/usb
88
+ ${TUSB}
89
+ # nrfx
90
+ ${NRFX}
91
+ ${NRFX} /mdk
92
+ ${NRFX} /hal
93
+ ${NRFX} /drivers/include
94
+ ${NRFX} /drivers/src
95
+ # sdk 11 for cdc/ble dfu
96
+ ${SDK11} /libraries/bootloader_dfu
97
+ ${SDK11} /libraries/bootloader_dfu/hci_transport
98
+ ${SDK11} /drivers_nrf/pstorage
99
+ ${SDK11} /ble/common
100
+ ${SDK11} /ble/ble_services/ble_dfu
101
+ ${SDK11} /ble/ble_services/ble_dis
102
+ # later sdk with updated drivers
103
+ ${SDK} /libraries/timer
104
+ ${SDK} /libraries/scheduler
105
+ ${SDK} /libraries/crc16
106
+ ${SDK} /libraries/util
107
+ ${SDK} /libraries/hci/config
108
+ ${SDK} /libraries/hci
109
+ ${SDK} /libraries/uart
110
+ ${SDK} /drivers_nrf/delay
111
+ # Softdevice
112
+ ${SOFTDEVICE} /mbr/headers
113
+ )
114
+
115
+ if (CMAKE_BUILD_TYPE STREQUAL "Debug" )
116
+ # TODO not work yet, also need to add segger rtt, DFU_APP_DATA_RESERVED=0, BOOTLOADER_REGION_START=0xED000
117
+ set (LD_FILE ${CMAKE_CURRENT_LIST_DIR} /linker/${MCU_VARIANT}_debug.ld )
118
+ message (FATAL_ERROR "Debug build not supported yet" )
119
+ else ()
120
+ set (LD_FILE ${CMAKE_CURRENT_LIST_DIR} /linker/${MCU_VARIANT}.ld )
121
+ endif ()
122
+
123
+ target_link_options (bootloader PUBLIC
124
+ "LINKER:--script=${LD_FILE} "
125
+ -L${NRFX}/mdk
126
+ --specs=nosys.specs
127
+ --specs=nano.specs
128
+ )
129
+ target_compile_options (bootloader PUBLIC
130
+ -fno-builtin
131
+ -fshort-enums
132
+ -fstack-usage
133
+ -fno-strict-aliasing
134
+ -Wall
135
+ -Wextra
136
+ -Werror
137
+ -Wfatal-errors
138
+ -Werror-implicit-function-declaration
139
+ -Wfloat-equal
140
+ -Wundef
141
+ -Wshadow
142
+ -Wwrite-strings
143
+ -Wsign-compare
144
+ -Wmissing-format-attribute
145
+ -Wno-endif-labels
146
+ -Wunreachable-code
147
+ # Suppress warning caused by SDK
148
+ -Wno-unused-parameter -Wno-expansion-to-defined
149
+ )
150
+ target_compile_definitions (bootloader PUBLIC
151
+ SOFTDEVICE_PRESENT
152
+ DFU_APP_DATA_RESERVED=7*4096
153
+ )
154
+
155
+ if (TRACE_ETM STREQUAL "1" )
156
+ # ENABLE_TRACE will cause system_nrf5x.c to set up ETM trace
157
+ target_compile_definitions (bootloader PUBLIC ENABLE_TRACE )
158
+ endif ()
159
+
160
+ if (MCU_VARIANT STREQUAL "nrf52" )
161
+ # UART transport
162
+ target_sources (bootloader PUBLIC
163
+ ${SDK} /libraries/uart/app_uart.c
164
+ ${SDK} /drivers_nrf/uart/nrf_drv_uart.c
165
+ ${SDK} /drivers_nrf/common/nrf_drv_common.c
166
+ )
167
+ target_include_directories (bootloader PUBLIC
168
+ ${SDK11} /libraries/util
169
+ ${SDK} /drivers_nrf/common
170
+ ${SDK} /drivers_nrf/uart
171
+ )
172
+ else ()
173
+ # USB transport
174
+ target_sources (bootloader PUBLIC
175
+ src/boards/${BOARD}/pinconfig.c
176
+ src/usb/msc_uf2.c
177
+ src/usb/usb.c
178
+ src/usb/usb_desc.c
179
+ src/usb/uf2/ghostfat.c
180
+ # TinyUSB
181
+ ${TUSB} /portable/nordic/nrf5x/dcd_nrf5x.c
182
+ ${TUSB} /common/tusb_fifo.c
183
+ ${TUSB} /device/usbd.c
184
+ ${TUSB} /device/usbd_control.c
185
+ ${TUSB} /class/cdc/cdc_device.c
186
+ ${TUSB} /class/msc/msc_device.c
187
+ ${TUSB} /tusb.c
188
+ )
189
+ endif ()
190
+
191
+ #----------------------------------------------------
192
+ # MCU Variant differences
193
+ # Supported are: nrf52 (nrf52832), nrf52833, nrf52840
194
+ #----------------------------------------------------
195
+ if (MCU_VARIANT STREQUAL "nrf52" )
196
+ set (SD_NAME s132 )
197
+ set (DFU_DEV_REV 0xADAF )
198
+ target_compile_definitions (bootloader PUBLIC
199
+ NRF52
200
+ NRF52832_XXAA
201
+ S132
202
+ )
203
+ target_include_directories (bootloader PUBLIC
204
+ ${SOFTDEVICE} /s132_nrf52_6.1.1/s132_nrf52_6.1.1_API/include
205
+ )
206
+
207
+ elseif (MCU_VARIANT STREQUAL "nrf52833" )
208
+ set (SD_NAME s140 )
209
+ set (DFU_DEV_REV 52833 )
210
+ target_compile_definitions (bootloader PUBLIC
211
+ NRF52833_XXAA
212
+ S140
213
+ )
214
+ target_include_directories (bootloader PUBLIC
215
+ ${SOFTDEVICE} /s140_nrf52_6.1.1/s140_nrf52_6.1.1_API/include
216
+ )
217
+
218
+ elseif (MCU_VARIANT STREQUAL "nrf52840" )
219
+ set (SD_NAME s140 )
220
+ set (DFU_DEV_REV 52840 )
221
+ target_compile_definitions (bootloader PUBLIC
222
+ NRF52840_XXAA
223
+ S140
224
+ )
225
+ target_include_directories (bootloader PUBLIC
226
+ ${SOFTDEVICE} /s140_nrf52_6.1.1/s140_nrf52_6.1.1_API/include
227
+ )
228
+
229
+ else ()
230
+ message (FATAL_ERROR "MCU_VARIANT ${MCU_VARIANT} is unknown" )
231
+ endif ()
232
+
233
+ set (SD_FILENAME ${SD_NAME} _nrf52_${SD_VERSION} )
234
+ set (SD_HEX ${SOFTDEVICE} /${SD_FILENAME}/${SD_FILENAME}_softdevice.hex )
235
+
236
+ #----------------------------------
237
+ # Get UF2 version from git
238
+ #----------------------------------
239
+ execute_process (COMMAND git describe --dirty --always --tags OUTPUT_VARIABLE GIT_VERSION )
240
+ string (STRIP ${GIT_VERSION} GIT_VERSION )
241
+
242
+ execute_process (COMMAND bash "-c" "git submodule status | cut -d\" \" -f3,4 | paste -s -d\" \" -"
243
+ OUTPUT_VARIABLE GIT_SUBMODULE_VERSIONS
244
+ )
245
+ string (STRIP ${GIT_SUBMODULE_VERSIONS} GIT_SUBMODULE_VERSIONS )
246
+ string (REPLACE ../ "" GIT_SUBMODULE_VERSIONS ${GIT_SUBMODULE_VERSIONS} )
247
+ string (REPLACE lib/ "" GIT_SUBMODULE_VERSIONS ${GIT_SUBMODULE_VERSIONS} )
248
+
249
+ string (REPLACE "-" ";" RELEASE_VERSION ${GIT_VERSION} )
250
+ list (GET RELEASE_VERSION 0 RELEASE_VERSION )
251
+ string (REPLACE "." ";" RELEASE_VERSION ${RELEASE_VERSION} )
252
+ list (GET RELEASE_VERSION 0 RELEASE_VERSION_MAJOR )
253
+ list (GET RELEASE_VERSION 1 RELEASE_VERSION_MINOR )
254
+ list (GET RELEASE_VERSION 2 RELEASE_VERSION_PATCH )
255
+ math (EXPR MK_BOOTLOADER_VERSION "(${RELEASE_VERSION_MAJOR} << 16) + (${RELEASE_VERSION_MINOR} << 8) + ${RELEASE_VERSION_PATCH} " )
256
+
257
+ cmake_print_variables (GIT_VERSION GIT_SUBMODULE_VERSIONS MK_BOOTLOADER_VERSION )
258
+
259
+ target_compile_definitions (bootloader PUBLIC
260
+ UF2_VERSION= "${GIT_VERSION} - ${GIT_SUBMODULE_VERSIONS} "
261
+ BLEDIS_FW_VERSION= "${GIT_VERSION} ${SD_NAME} ${SD_VERSION} "
262
+ MK_BOOTLOADER_VERSION=${MK_BOOTLOADER_VERSION}
263
+ )
264
+
265
+
266
+ #----------------------------------
267
+ # Post build
268
+ #----------------------------------
269
+
270
+ # run size after build
271
+ add_custom_command (TARGET bootloader POST_BUILD
272
+ COMMAND ${CMAKE_SIZE} $< TARGET_FILE:bootloader>
273
+ )
274
+
275
+ # Add bin/hex output
276
+ add_custom_command (TARGET bootloader POST_BUILD
277
+ COMMAND ${CMAKE_OBJCOPY} -Obinary $< TARGET_FILE:bootloader> $< TARGET_FILE_DIR:bootloader> /bootloader.bin
278
+ COMMAND ${CMAKE_OBJCOPY} -Oihex $< TARGET_FILE:bootloader> $< TARGET_FILE_DIR:bootloader> /bootloader.hex
279
+ VERBATIM )
280
+
281
+ #----------------------------------
282
+ # Flashing target
283
+ #----------------------------------
284
+
285
+ if (NOT DEFINED NRFJPROG )
286
+ set (NRFJPROG nrfjprog )
287
+ endif ()
288
+
289
+ add_custom_target (flash
290
+ DEPENDS bootloader
291
+ COMMAND ${NRFJPROG} --program $< TARGET_FILE:bootloader> --verify --sectoranduicrerase -f nrf52 --reset
292
+ )
293
+
294
+ add_custom_target (flash-sd
295
+ COMMAND ${NRFJPROG} --program ${SD_HEX} --verify --sectorerase -f nrf52 --reset
296
+ )
297
+
298
+ add_custom_target (flash-mbr
299
+ COMMAND ${NRFJPROG} --program ${MBR_HEX} --verify --sectorerase -f nrf52 --reset
300
+ )
301
+
302
+ add_custom_target (flash-erase
303
+ COMMAND ${NRFJPROG} -f nrf52 --eraseall
304
+ )
0 commit comments