From ffa2379818448c66751898d03fb77616d54faea8 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 26 May 2023 13:10:49 +0700 Subject: [PATCH 1/3] add cmake support, starting with feather nrf52840 and pca10056. Seems to work well --- CMakeLists.txt | 304 ++++++++++++++++++ cmake/cpu/cortex-m33.cmake | 19 ++ cmake/cpu/cortex-m4.cmake | 23 ++ cmake/toolchain/arm_gcc.cmake | 46 +++ cmake/toolchain/set_flags.cmake | 17 + .../feather_nrf52840_express/board.cmake | 1 + src/boards/pca10056/board.cmake | 1 + 7 files changed, 411 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 cmake/cpu/cortex-m33.cmake create mode 100644 cmake/cpu/cortex-m4.cmake create mode 100644 cmake/toolchain/arm_gcc.cmake create mode 100644 cmake/toolchain/set_flags.cmake create mode 100644 src/boards/feather_nrf52840_express/board.cmake create mode 100644 src/boards/pca10056/board.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 00000000..d5b2369f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,304 @@ +cmake_minimum_required(VERSION 3.17) + +if (NOT DEFINED BOARD) + message(FATAL_ERROR "BOARD is not defined") +endif () + +if (NOT DEFINED TOOLCHAIN) + set(TOOLCHAIN gcc) +endif () + +# include board specific +include(${CMAKE_CURRENT_LIST_DIR}/src/boards/${BOARD}/board.cmake) + +# toolchain set up +if (MCU_VARIANT STREQUAL "nrf5340_application") + set(CMAKE_SYSTEM_PROCESSOR cortex-m33 CACHE INTERNAL "System Processor") + set(JLINK_DEVICE nrf5340_xxaa_app) +else () + set(CMAKE_SYSTEM_PROCESSOR cortex-m4 CACHE INTERNAL "System Processor") + set(JLINK_DEVICE ${MCU_VARIANT}_xxaa) +endif () + +set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_LIST_DIR}/cmake/toolchain/arm_${TOOLCHAIN}.cmake) + +project(Adafruit_nRF52_Bootloader C ASM) + +set(NRFX ${CMAKE_CURRENT_LIST_DIR}/lib/nrfx) +set(SDK11 ${CMAKE_CURRENT_LIST_DIR}/lib/sdk11/components) +set(SDK ${CMAKE_CURRENT_LIST_DIR}/lib/sdk/components) +set(SOFTDEVICE ${CMAKE_CURRENT_LIST_DIR}/lib/softdevice) +set(TUSB ${CMAKE_CURRENT_LIST_DIR}/lib/tinyusb/src) + +#------------------- +# Bootloader +#------------------- +set(CMAKE_EXECUTABLE_SUFFIX .elf) +add_executable(bootloader) +#set_target_properties(bootloader PROPERTIES OUTPUT_NAME "${BOARD}_bootloader.elf") + + +# SD_VERSION can be overwritten by board.cmake +if(NOT DEFINED SD_VERSION) + set(SD_VERSION 6.1.1) +endif () + +set(MBR_HEX ${SOFTDEVICE}/mbr/hex/mbr_nrf52_2.4.1_mbr.hex) + +target_sources(bootloader PUBLIC + # src + src/dfu_ble_svc.c + src/dfu_init.c + src/flash_nrf5x.c + src/main.c + src/boards/boards.c + # nrfx + ${NRFX}/drivers/src/nrfx_power.c + ${NRFX}/drivers/src/nrfx_nvmc.c + ${NRFX}/mdk/system_${MCU_VARIANT}.c + # sdk 11 + ${SDK11}/libraries/bootloader_dfu/bootloader.c + ${SDK11}/libraries/bootloader_dfu/bootloader_settings.c + ${SDK11}/libraries/bootloader_dfu/bootloader_util.c + ${SDK11}/libraries/bootloader_dfu/dfu_transport_serial.c + ${SDK11}/libraries/bootloader_dfu/dfu_transport_ble.c + ${SDK11}/libraries/bootloader_dfu/dfu_single_bank.c + ${SDK11}/ble/ble_services/ble_dfu/ble_dfu.c + ${SDK11}/ble/ble_services/ble_dis/ble_dis.c + ${SDK11}/drivers_nrf/pstorage/pstorage_raw.c + # latest sdk + ${SDK}/libraries/timer/app_timer.c + ${SDK}/libraries/scheduler/app_scheduler.c + ${SDK}/libraries/util/app_error.c + ${SDK}/libraries/util/app_util_platform.c + ${SDK}/libraries/crc16/crc16.c + ${SDK}/libraries/hci/hci_mem_pool.c + ${SDK}/libraries/hci/hci_slip.c + ${SDK}/libraries/hci/hci_transport.c + ${SDK}/libraries/util/nrf_assert.c + # ASM + ${NRFX}/mdk/gcc_startup_${MCU_VARIANT}.S + ) +target_include_directories(bootloader PUBLIC + src + src/boards + src/boards/${BOARD} + src/cmsis/include + src/usb + ${TUSB} + # nrfx + ${NRFX} + ${NRFX}/mdk + ${NRFX}/hal + ${NRFX}/drivers/include + ${NRFX}/drivers/src + # sdk 11 for cdc/ble dfu + ${SDK11}/libraries/bootloader_dfu + ${SDK11}/libraries/bootloader_dfu/hci_transport + ${SDK11}/drivers_nrf/pstorage + ${SDK11}/ble/common + ${SDK11}/ble/ble_services/ble_dfu + ${SDK11}/ble/ble_services/ble_dis + # later sdk with updated drivers + ${SDK}/libraries/timer + ${SDK}/libraries/scheduler + ${SDK}/libraries/crc16 + ${SDK}/libraries/util + ${SDK}/libraries/hci/config + ${SDK}/libraries/hci + ${SDK}/libraries/uart + ${SDK}/drivers_nrf/delay + # Softdevice + ${SOFTDEVICE}/mbr/headers + ) + +if (CMAKE_BUILD_TYPE STREQUAL "Debug") + # TODO not work yet, also need to add segger rtt, DFU_APP_DATA_RESERVED=0, BOOTLOADER_REGION_START=0xED000 + set(LD_FILE ${CMAKE_CURRENT_LIST_DIR}/linker/${MCU_VARIANT}_debug.ld) + message(FATAL_ERROR "Debug build not supported yet") +else () + set(LD_FILE ${CMAKE_CURRENT_LIST_DIR}/linker/${MCU_VARIANT}.ld) +endif () + +target_link_options(bootloader PUBLIC + "LINKER:--script=${LD_FILE}" + -L${NRFX}/mdk + --specs=nosys.specs + --specs=nano.specs + ) +target_compile_options(bootloader PUBLIC + -fno-builtin + -fshort-enums + -fstack-usage + -fno-strict-aliasing + -Wall + -Wextra + -Werror + -Wfatal-errors + -Werror-implicit-function-declaration + -Wfloat-equal + -Wundef + -Wshadow + -Wwrite-strings + -Wsign-compare + -Wmissing-format-attribute + -Wno-endif-labels + -Wunreachable-code + # Suppress warning caused by SDK + -Wno-unused-parameter -Wno-expansion-to-defined + ) +target_compile_definitions(bootloader PUBLIC + SOFTDEVICE_PRESENT + DFU_APP_DATA_RESERVED=7*4096 + ) + +if (TRACE_ETM STREQUAL "1") + # ENABLE_TRACE will cause system_nrf5x.c to set up ETM trace + target_compile_definitions(bootloader PUBLIC ENABLE_TRACE) +endif () + +if (MCU_VARIANT STREQUAL "nrf52") + # UART transport + target_sources(bootloader PUBLIC + ${SDK}/libraries/uart/app_uart.c + ${SDK}/drivers_nrf/uart/nrf_drv_uart.c + ${SDK}/drivers_nrf/common/nrf_drv_common.c + ) + target_include_directories(bootloader PUBLIC + ${SDK11}/libraries/util + ${SDK}/drivers_nrf/common + ${SDK}/drivers_nrf/uart + ) +else () + # USB transport + target_sources(bootloader PUBLIC + src/boards/${BOARD}/pinconfig.c + src/usb/msc_uf2.c + src/usb/usb.c + src/usb/usb_desc.c + src/usb/uf2/ghostfat.c + # TinyUSB + ${TUSB}/portable/nordic/nrf5x/dcd_nrf5x.c + ${TUSB}/common/tusb_fifo.c + ${TUSB}/device/usbd.c + ${TUSB}/device/usbd_control.c + ${TUSB}/class/cdc/cdc_device.c + ${TUSB}/class/msc/msc_device.c + ${TUSB}/tusb.c + ) +endif () + +#---------------------------------------------------- +# MCU Variant differences +# Supported are: nrf52 (nrf52832), nrf52833, nrf52840 +#---------------------------------------------------- +if (MCU_VARIANT STREQUAL "nrf52") + set(SD_NAME s132) + set(DFU_DEV_REV 0xADAF) + target_compile_definitions(bootloader PUBLIC + NRF52 + NRF52832_XXAA + S132 + ) + target_include_directories(bootloader PUBLIC + ${SOFTDEVICE}/s132_nrf52_6.1.1/s132_nrf52_6.1.1_API/include + ) + +elseif (MCU_VARIANT STREQUAL "nrf52833") + set(SD_NAME s140) + set(DFU_DEV_REV 52833) + target_compile_definitions(bootloader PUBLIC + NRF52833_XXAA + S140 + ) + target_include_directories(bootloader PUBLIC + ${SOFTDEVICE}/s140_nrf52_6.1.1/s140_nrf52_6.1.1_API/include + ) + +elseif (MCU_VARIANT STREQUAL "nrf52840") + set(SD_NAME s140) + set(DFU_DEV_REV 52840) + target_compile_definitions(bootloader PUBLIC + NRF52840_XXAA + S140 + ) + target_include_directories(bootloader PUBLIC + ${SOFTDEVICE}/s140_nrf52_6.1.1/s140_nrf52_6.1.1_API/include + ) + +else () + message(FATAL_ERROR "MCU_VARIANT ${MCU_VARIANT} is unknown") +endif () + +set(SD_FILENAME ${SD_NAME}_nrf52_${SD_VERSION}) +set(SD_HEX ${SOFTDEVICE}/${SD_FILENAME}/${SD_FILENAME}_softdevice.hex) + +#---------------------------------- +# Get UF2 version from git +#---------------------------------- +execute_process(COMMAND git describe --dirty --always --tags OUTPUT_VARIABLE GIT_VERSION) +string(STRIP ${GIT_VERSION} GIT_VERSION) + +execute_process(COMMAND bash "-c" "git submodule status | cut -d\" \" -f3,4 | paste -s -d\" \" -" + OUTPUT_VARIABLE GIT_SUBMODULE_VERSIONS + ) +string(STRIP ${GIT_SUBMODULE_VERSIONS} GIT_SUBMODULE_VERSIONS) +string(REPLACE ../ "" GIT_SUBMODULE_VERSIONS ${GIT_SUBMODULE_VERSIONS}) +string(REPLACE lib/ "" GIT_SUBMODULE_VERSIONS ${GIT_SUBMODULE_VERSIONS}) + +string(REPLACE "-" ";" RELEASE_VERSION ${GIT_VERSION}) +list(GET RELEASE_VERSION 0 RELEASE_VERSION) +string(REPLACE "." ";" RELEASE_VERSION ${RELEASE_VERSION}) +list(GET RELEASE_VERSION 0 RELEASE_VERSION_MAJOR) +list(GET RELEASE_VERSION 1 RELEASE_VERSION_MINOR) +list(GET RELEASE_VERSION 2 RELEASE_VERSION_PATCH) +math(EXPR MK_BOOTLOADER_VERSION "(${RELEASE_VERSION_MAJOR} << 16) + (${RELEASE_VERSION_MINOR} << 8) + ${RELEASE_VERSION_PATCH}") + +cmake_print_variables(GIT_VERSION GIT_SUBMODULE_VERSIONS MK_BOOTLOADER_VERSION) + +target_compile_definitions(bootloader PUBLIC + UF2_VERSION="${GIT_VERSION} - ${GIT_SUBMODULE_VERSIONS}" + BLEDIS_FW_VERSION="${GIT_VERSION} ${SD_NAME} ${SD_VERSION}" + MK_BOOTLOADER_VERSION=${MK_BOOTLOADER_VERSION} + ) + + +#---------------------------------- +# Post build +#---------------------------------- + +# run size after build +add_custom_command(TARGET bootloader POST_BUILD + COMMAND ${CMAKE_SIZE} $ + ) + +# Add bin/hex output +add_custom_command(TARGET bootloader POST_BUILD + COMMAND ${CMAKE_OBJCOPY} -Obinary $ $/bootloader.bin + COMMAND ${CMAKE_OBJCOPY} -Oihex $ $/bootloader.hex + VERBATIM) + +#---------------------------------- +# Flashing target +#---------------------------------- + +if (NOT DEFINED NRFJPROG) + set(NRFJPROG nrfjprog) +endif() + +add_custom_target(flash + DEPENDS bootloader + COMMAND ${NRFJPROG} --program $ --verify --sectoranduicrerase -f nrf52 --reset + ) + +add_custom_target(flash-sd + COMMAND ${NRFJPROG} --program ${SD_HEX} --verify --sectorerase -f nrf52 --reset + ) + +add_custom_target(flash-mbr + COMMAND ${NRFJPROG} --program ${MBR_HEX} --verify --sectorerase -f nrf52 --reset + ) + +add_custom_target(flash-erase + COMMAND ${NRFJPROG} -f nrf52 --eraseall + ) \ No newline at end of file diff --git a/cmake/cpu/cortex-m33.cmake b/cmake/cpu/cortex-m33.cmake new file mode 100644 index 00000000..a028cd41 --- /dev/null +++ b/cmake/cpu/cortex-m33.cmake @@ -0,0 +1,19 @@ +if (TOOLCHAIN STREQUAL "gcc") + set(TOOLCHAIN_COMMON_FLAGS + -mthumb + -mcpu=cortex-m33 + -mfloat-abi=hard + -mfpu=fpv5-sp-d16 + ) + + set(FREERTOS_PORT GCC_ARM_CM33_NTZ_NONSECURE CACHE INTERNAL "") + +elseif (TOOLCHAIN STREQUAL "iar") + set(TOOLCHAIN_COMMON_FLAGS + --cpu cortex-m33 + --fpu VFPv5-SP + ) + + set(FREERTOS_PORT IAR_ARM_CM4F CACHE INTERNAL "") + +endif () diff --git a/cmake/cpu/cortex-m4.cmake b/cmake/cpu/cortex-m4.cmake new file mode 100644 index 00000000..79f97e24 --- /dev/null +++ b/cmake/cpu/cortex-m4.cmake @@ -0,0 +1,23 @@ +if (TOOLCHAIN STREQUAL "gcc") + set(TOOLCHAIN_COMMON_FLAGS + -mthumb + -mcpu=cortex-m4 + -mfloat-abi=hard + -mfpu=fpv4-sp-d16 + ) + + if (NOT DEFINED FREERTOS_PORT) + set(FREERTOS_PORT GCC_ARM_CM4F CACHE INTERNAL "") + endif () + +elseif (TOOLCHAIN STREQUAL "iar") + set(TOOLCHAIN_COMMON_FLAGS + --cpu cortex-m4 + --fpu VFPv4 + ) + + if (NOT DEFINED FREERTOS_PORT) + set(FREERTOS_PORT IAR_ARM_CM4F CACHE INTERNAL "") + endif () + +endif () diff --git a/cmake/toolchain/arm_gcc.cmake b/cmake/toolchain/arm_gcc.cmake new file mode 100644 index 00000000..5203d1e4 --- /dev/null +++ b/cmake/toolchain/arm_gcc.cmake @@ -0,0 +1,46 @@ +set(CMAKE_SYSTEM_NAME Generic) + +set(CMAKE_C_COMPILER "arm-none-eabi-gcc") +set(CMAKE_CXX_COMPILER "arm-none-eabi-g++") +set(CMAKE_ASM_COMPILER "arm-none-eabi-gcc") + +set(CMAKE_SIZE "arm-none-eabi-size" CACHE FILEPATH "") +set(CMAKE_OBJCOPY "arm-none-eabi-objcopy" CACHE FILEPATH "") +set(CMAKE_OBJDUMP "arm-none-eabi-objdump" CACHE FILEPATH "") + +set_property(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS FALSE) + +# Look for includes and libraries only in the target system prefix. +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) + +# pass TOOLCHAIN_CPU to +set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES CMAKE_SYSTEM_PROCESSOR) + +include(${CMAKE_CURRENT_LIST_DIR}/../cpu/${CMAKE_SYSTEM_PROCESSOR}.cmake) + +# enable all possible warnings for building examples +list(APPEND TOOLCHAIN_COMMON_FLAGS + -fdata-sections + -ffunction-sections + -fsingle-precision-constant + -fno-strict-aliasing + ) + +set(TOOLCHAIN_EXE_LINKER_FLAGS + -Wl,--print-memory-usage + -Wl,--gc-sections + #-Wl,--cref + ) + +include(${CMAKE_CURRENT_LIST_DIR}/set_flags.cmake) + +# try_compile is cmake test compiling its own example, +# pass -nostdlib to skip stdlib linking +get_property(IS_IN_TRY_COMPILE GLOBAL PROPERTY IN_TRY_COMPILE) +if (IS_IN_TRY_COMPILE) + set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -nostdlib") + set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -nostdlib") +endif () diff --git a/cmake/toolchain/set_flags.cmake b/cmake/toolchain/set_flags.cmake new file mode 100644 index 00000000..bbbd2112 --- /dev/null +++ b/cmake/toolchain/set_flags.cmake @@ -0,0 +1,17 @@ +include(CMakePrintHelpers) + +# join the toolchain flags into a single string +list(JOIN TOOLCHAIN_COMMON_FLAGS " " TOOLCHAIN_COMMON_FLAGS) + +foreach (LANG IN ITEMS C CXX ASM) + set(CMAKE_${LANG}_FLAGS_INIT ${TOOLCHAIN_COMMON_FLAGS}) + + #cmake_print_variables(CMAKE_${LANG}_FLAGS_INIT) + + # optimization flags for LOG, LOGGER ? + #set(CMAKE_${LANG}_FLAGS_RELEASE_INIT "-Os") + #set(CMAKE_${LANG}_FLAGS_DEBUG_INIT "-O0") +endforeach () + +# Linker +list(JOIN TOOLCHAIN_EXE_LINKER_FLAGS " " CMAKE_EXE_LINKER_FLAGS_INIT) diff --git a/src/boards/feather_nrf52840_express/board.cmake b/src/boards/feather_nrf52840_express/board.cmake new file mode 100644 index 00000000..2715f360 --- /dev/null +++ b/src/boards/feather_nrf52840_express/board.cmake @@ -0,0 +1 @@ +set(MCU_VARIANT nrf52840) diff --git a/src/boards/pca10056/board.cmake b/src/boards/pca10056/board.cmake new file mode 100644 index 00000000..2715f360 --- /dev/null +++ b/src/boards/pca10056/board.cmake @@ -0,0 +1 @@ +set(MCU_VARIANT nrf52840) From dbc7648649b183883226da7d8d83c5ba3678ecbd Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 20 Jul 2023 21:08:19 +0700 Subject: [PATCH 2/3] add linker --print-memory-usage --- Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 323a702c..fb1f521a 100644 --- a/Makefile +++ b/Makefile @@ -28,9 +28,9 @@ MBR_HEX = lib/softdevice/mbr/hex/mbr_nrf52_2.4.1_mbr.hex # linker by MCU eg. nrf52840.ld ifeq ($(DEBUG), 1) - LD_FILE = linker/$(MCU_SUB_VARIANT)_debug.ld + LD_FILE = linker/$(MCU_SUB_VARIANT)_debug.ld else - LD_FILE = linker/$(MCU_SUB_VARIANT).ld + LD_FILE = linker/$(MCU_SUB_VARIANT).ld endif GIT_VERSION := $(shell git describe --dirty --always --tags) @@ -123,6 +123,7 @@ else ifeq ($(MCU_SUB_VARIANT),nrf52840) SD_NAME = s140 DFU_DEV_REV = 52840 CFLAGS += -DNRF52840_XXAA -DS140 + # App reserved 40KB to match circuitpython for 840 DFU_APP_DATA_RESERVED=10*4096 else $(error Sub Variant $(MCU_SUB_VARIANT) is unknown) @@ -349,6 +350,7 @@ endif LDFLAGS += \ $(CFLAGS) \ -Wl,-L,linker -Wl,-T,$(LD_FILE) \ + -Wl,--print-memory-usage \ -Wl,-Map=$@.map -Wl,-cref -Wl,-gc-sections \ -specs=nosys.specs -specs=nano.specs From 2de2fbf82f8ab5e1e422da0c1f2a18ddaf8fc753 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 20 Jul 2023 22:06:28 +0700 Subject: [PATCH 3/3] add PR template --- .github/PULL_REQUEST_TEMPLATE.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 .github/PULL_REQUEST_TEMPLATE.md diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 00000000..6953d761 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,18 @@ +## Checklist + +*By completing this PR sufficiently, you help us to review this Pull Request quicker and also help improve the quality of Release Notes* + +- [ ] Please provide specific title of the PR describing the change +- [ ] Please provide related links (*eg. Issue which will be closed by this Pull Request*) +- [ ] If you are adding an new boards, please make sure + - [ ] Provide link to your allocated VID/PID if applicable + - [ ] Add your board to [action ci](/.github/workflows) in correct workflow and alphabet order for release binary + - [ ] `UF2_BOARD_ID` in your board.h follow correct format from [uf2 specs](https://github.com/microsoft/uf2#files-exposed-by-bootloaders) + +*This checklist items that are not applicable to your PR can be deleted.* + +----------- + +## Description of Change + +Please describe your proposed Pull Request and it's impact.