Skip to content

Commit 9924e54

Browse files
authored
Merge pull request #310 from adafruit/add-cmake
Add cmake build for nrf52840
2 parents 277a0c8 + 2de2fbf commit 9924e54

File tree

9 files changed

+433
-2
lines changed

9 files changed

+433
-2
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Checklist
2+
3+
*By completing this PR sufficiently, you help us to review this Pull Request quicker and also help improve the quality of Release Notes*
4+
5+
- [ ] Please provide specific title of the PR describing the change
6+
- [ ] Please provide related links (*eg. Issue which will be closed by this Pull Request*)
7+
- [ ] If you are adding an new boards, please make sure
8+
- [ ] Provide link to your allocated VID/PID if applicable
9+
- [ ] Add your board to [action ci](/.github/workflows) in correct workflow and alphabet order for release binary
10+
- [ ] `UF2_BOARD_ID` in your board.h follow correct format from [uf2 specs](https://github.com/microsoft/uf2#files-exposed-by-bootloaders)
11+
12+
*This checklist items that are not applicable to your PR can be deleted.*
13+
14+
-----------
15+
16+
## Description of Change
17+
18+
Please describe your proposed Pull Request and it's impact.

CMakeLists.txt

Lines changed: 304 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
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+
)

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ MBR_HEX = lib/softdevice/mbr/hex/mbr_nrf52_2.4.1_mbr.hex
2828

2929
# linker by MCU eg. nrf52840.ld
3030
ifeq ($(DEBUG), 1)
31-
LD_FILE = linker/$(MCU_SUB_VARIANT)_debug.ld
31+
LD_FILE = linker/$(MCU_SUB_VARIANT)_debug.ld
3232
else
33-
LD_FILE = linker/$(MCU_SUB_VARIANT).ld
33+
LD_FILE = linker/$(MCU_SUB_VARIANT).ld
3434
endif
3535

3636
GIT_VERSION := $(shell git describe --dirty --always --tags)
@@ -123,6 +123,7 @@ else ifeq ($(MCU_SUB_VARIANT),nrf52840)
123123
SD_NAME = s140
124124
DFU_DEV_REV = 52840
125125
CFLAGS += -DNRF52840_XXAA -DS140
126+
# App reserved 40KB to match circuitpython for 840
126127
DFU_APP_DATA_RESERVED=10*4096
127128
else
128129
$(error Sub Variant $(MCU_SUB_VARIANT) is unknown)
@@ -349,6 +350,7 @@ endif
349350
LDFLAGS += \
350351
$(CFLAGS) \
351352
-Wl,-L,linker -Wl,-T,$(LD_FILE) \
353+
-Wl,--print-memory-usage \
352354
-Wl,[email protected] -Wl,-cref -Wl,-gc-sections \
353355
-specs=nosys.specs -specs=nano.specs
354356

cmake/cpu/cortex-m33.cmake

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
if (TOOLCHAIN STREQUAL "gcc")
2+
set(TOOLCHAIN_COMMON_FLAGS
3+
-mthumb
4+
-mcpu=cortex-m33
5+
-mfloat-abi=hard
6+
-mfpu=fpv5-sp-d16
7+
)
8+
9+
set(FREERTOS_PORT GCC_ARM_CM33_NTZ_NONSECURE CACHE INTERNAL "")
10+
11+
elseif (TOOLCHAIN STREQUAL "iar")
12+
set(TOOLCHAIN_COMMON_FLAGS
13+
--cpu cortex-m33
14+
--fpu VFPv5-SP
15+
)
16+
17+
set(FREERTOS_PORT IAR_ARM_CM4F CACHE INTERNAL "")
18+
19+
endif ()

cmake/cpu/cortex-m4.cmake

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
if (TOOLCHAIN STREQUAL "gcc")
2+
set(TOOLCHAIN_COMMON_FLAGS
3+
-mthumb
4+
-mcpu=cortex-m4
5+
-mfloat-abi=hard
6+
-mfpu=fpv4-sp-d16
7+
)
8+
9+
if (NOT DEFINED FREERTOS_PORT)
10+
set(FREERTOS_PORT GCC_ARM_CM4F CACHE INTERNAL "")
11+
endif ()
12+
13+
elseif (TOOLCHAIN STREQUAL "iar")
14+
set(TOOLCHAIN_COMMON_FLAGS
15+
--cpu cortex-m4
16+
--fpu VFPv4
17+
)
18+
19+
if (NOT DEFINED FREERTOS_PORT)
20+
set(FREERTOS_PORT IAR_ARM_CM4F CACHE INTERNAL "")
21+
endif ()
22+
23+
endif ()

0 commit comments

Comments
 (0)