11cmake_minimum_required (VERSION 3.17)
2+ find_package (Python COMPONENTS Interpreter)
23
34if (NOT DEFINED BOARD)
45 message (FATAL_ERROR "BOARD is not defined" )
@@ -30,13 +31,14 @@ set(SDK ${CMAKE_CURRENT_LIST_DIR}/lib/sdk/components)
3031set (SOFTDEVICE ${CMAKE_CURRENT_LIST_DIR} /lib/softdevice)
3132set (TUSB ${CMAKE_CURRENT_LIST_DIR} /lib/tinyusb/src)
3233
34+ set (UF2CONV_PY ${CMAKE_CURRENT_LIST_DIR} /lib/uf2/utils/uf2conv.py)
35+ set (UF2_FAMILY_ID_BOOTLOADER 0xd663823c)
36+
3337#-------------------
3438# Bootloader
3539#-------------------
3640set (CMAKE_EXECUTABLE_SUFFIX .elf)
3741add_executable (bootloader)
38- #set_target_properties(bootloader PROPERTIES OUTPUT_NAME "${BOARD}_bootloader.elf")
39-
4042
4143# SD_VERSION can be overwritten by board.cmake
4244if (NOT DEFINED SD_VERSION)
@@ -51,6 +53,8 @@ target_sources(bootloader PUBLIC
5153 src/dfu_init.c
5254 src/flash_nrf5x.c
5355 src/main.c
56+ src/screen.c
57+ src/images.c
5458 src/boards/boards.c
5559 # nrfx
5660 ${NRFX} /drivers/src/nrfx_power.c
@@ -112,19 +116,37 @@ target_include_directories(bootloader PUBLIC
112116 ${SOFTDEVICE} /mbr/headers
113117 )
114118
119+ # Debug option
115120if (CMAKE_BUILD_TYPE STREQUAL "Debug" )
116121 # TODO not work yet, also need to add segger rtt, DFU_APP_DATA_RESERVED=0, BOOTLOADER_REGION_START=0xED000
117122 set (LD_FILE ${CMAKE_CURRENT_LIST_DIR} /linker/${MCU_VARIANT} _debug.ld)
118- message (FATAL_ERROR "Debug build not supported yet" )
123+
124+ target_sources (bootloader PUBLIC
125+ lib/SEGGER_RTT/RTT/SEGGER_RTT.c
126+ )
127+ target_include_directories (bootloader PUBLIC
128+ lib/SEGGER_RTT/RTT
129+ )
130+
131+ target_compile_definitions (bootloader PUBLIC
132+ CFG_DEBUG
133+ SEGGER_RTT_MODE_DEFAULT=SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL
134+ DFU_APP_DATA_RESERVED=0
135+ )
136+
137+ if (MCU_VARIANT STREQUAL "nrf52840" )
138+ target_compile_definitions (bootloader PUBLIC BOOTLOADER_REGION_START=0xEA000)
139+ else ()
140+ target_compile_definitions (bootloader PUBLIC BOOTLOADER_REGION_START=0x6D000)
141+ endif ()
119142else ()
120143 set (LD_FILE ${CMAKE_CURRENT_LIST_DIR} /linker/${MCU_VARIANT} .ld)
121144endif ()
122145
123146target_link_options (bootloader PUBLIC
124147 "LINKER:--script=${LD_FILE} "
125148 -L${NRFX} /mdk
126- --specs=nosys.specs
127- --specs=nano.specs
149+ --specs=nosys.specs --specs=nano.specs
128150 )
129151target_compile_options (bootloader PUBLIC
130152 -fno-builtin
@@ -149,7 +171,6 @@ target_compile_options(bootloader PUBLIC
149171 )
150172target_compile_definitions (bootloader PUBLIC
151173 SOFTDEVICE_PRESENT
152- DFU_APP_DATA_RESERVED=7*4096
153174 )
154175
155176if (TRACE_ETM STREQUAL "1" )
@@ -195,6 +216,7 @@ endif ()
195216if (MCU_VARIANT STREQUAL "nrf52" )
196217 set (SD_NAME s132)
197218 set (DFU_DEV_REV 0xADAF)
219+ set (DFU_APP_DATA_RESERVED 7*4096)
198220 target_compile_definitions (bootloader PUBLIC
199221 NRF52
200222 NRF52832_XXAA
@@ -207,6 +229,7 @@ if (MCU_VARIANT STREQUAL "nrf52")
207229elseif (MCU_VARIANT STREQUAL "nrf52833" )
208230 set (SD_NAME s140)
209231 set (DFU_DEV_REV 52833)
232+ set (DFU_APP_DATA_RESERVED 7*4096)
210233 target_compile_definitions (bootloader PUBLIC
211234 NRF52833_XXAA
212235 S140
@@ -218,6 +241,8 @@ elseif (MCU_VARIANT STREQUAL "nrf52833")
218241elseif (MCU_VARIANT STREQUAL "nrf52840" )
219242 set (SD_NAME s140)
220243 set (DFU_DEV_REV 52840)
244+ # App reserved 40KB (8+32) to match circuitpython for 840
245+ set (DFU_APP_DATA_RESERVED 10*4096)
221246 target_compile_definitions (bootloader PUBLIC
222247 NRF52840_XXAA
223248 S140
@@ -233,6 +258,10 @@ endif ()
233258set (SD_FILENAME ${SD_NAME} _nrf52_${SD_VERSION} )
234259set (SD_HEX ${SOFTDEVICE} /${SD_FILENAME} /${SD_FILENAME} _softdevice.hex)
235260
261+ if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug" )
262+ target_compile_definitions (bootloader PUBLIC DFU_APP_DATA_RESERVED=${DFU_APP_DATA_RESERVED} )
263+ endif ()
264+
236265#----------------------------------
237266# Get UF2 version from git
238267#----------------------------------
@@ -257,12 +286,12 @@ math(EXPR MK_BOOTLOADER_VERSION "(${RELEASE_VERSION_MAJOR} << 16) + (${RELEASE_V
257286cmake_print_variables(GIT_VERSION GIT_SUBMODULE_VERSIONS MK_BOOTLOADER_VERSION)
258287
259288target_compile_definitions (bootloader PUBLIC
289+ UF2_VERSION_BASE="${GIT_VERSION} "
260290 UF2_VERSION="${GIT_VERSION} - ${GIT_SUBMODULE_VERSIONS} "
261291 BLEDIS_FW_VERSION="${GIT_VERSION} ${SD_NAME} ${SD_VERSION} "
262292 MK_BOOTLOADER_VERSION=${MK_BOOTLOADER_VERSION}
263293 )
264294
265-
266295#----------------------------------
267296# Post build
268297#----------------------------------
@@ -276,6 +305,8 @@ add_custom_command(TARGET bootloader POST_BUILD
276305add_custom_command (TARGET bootloader POST_BUILD
277306 COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:bootloader> $<TARGET_FILE_DIR:bootloader>/bootloader.bin
278307 COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:bootloader> $<TARGET_FILE_DIR:bootloader>/bootloader.hex
308+ COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR} /tools/hexmerge.py --overlap=replace -o $<TARGET_FILE_DIR:bootloader>/bootloader_mbr.hex $<TARGET_FILE_DIR:bootloader>/bootloader.hex ${MBR_HEX}
309+ COMMAND ${Python_EXECUTABLE} ${UF2CONV_PY} -f ${UF2_FAMILY_ID_BOOTLOADER} -c -o $<TARGET_FILE_DIR:bootloader>/bootloader_mbr.uf2 $<TARGET_FILE_DIR:bootloader>/bootloader_mbr.hex
279310 VERBATIM )
280311
281312#----------------------------------
@@ -286,11 +317,16 @@ if (NOT DEFINED NRFJPROG)
286317 set (NRFJPROG nrfjprog)
287318endif ()
288319
289- add_custom_target (flash
320+ add_custom_target (flash-jlink
290321 DEPENDS bootloader
291322 COMMAND ${NRFJPROG} --program $<TARGET_FILE:bootloader> --verify --sectoranduicrerase -f nrf52 --reset
292323 )
293324
325+ add_custom_target (flash-uf2
326+ DEPENDS bootloader
327+ COMMAND ${Python_EXECUTABLE} ${UF2CONV_PY} -f ${UF2_FAMILY_ID_BOOTLOADER} --deploy $<TARGET_FILE_DIR:bootloader>/bootloader_mbr.uf2
328+ )
329+
294330add_custom_target (flash-sd
295331 COMMAND ${NRFJPROG} --program ${SD_HEX} --verify --sectorerase -f nrf52 --reset
296332 )
0 commit comments