From b8544f5c8c7c1e5e185232b54fd9c696ceb17fc7 Mon Sep 17 00:00:00 2001 From: JuergenLeber Date: Sun, 8 Jun 2025 21:00:39 +0200 Subject: [PATCH 1/3] feat: add SD S340 support for nRF52840 and nRF52833 --- .github/workflows/githubci.yml | 2 +- .gitignore | 4 +++ CMakeLists.txt | 35 +++++++++++++++------- Makefile | 16 +++++++--- README.md | 19 ++++++++++++ lib/softdevice/s340_nrf52_7.0.1/readme.txt | 1 + src/main.c | 23 ++++++++++++-- 7 files changed, 82 insertions(+), 18 deletions(-) create mode 100644 lib/softdevice/s340_nrf52_7.0.1/readme.txt diff --git a/.github/workflows/githubci.yml b/.github/workflows/githubci.yml index 68b4f7f2..55086fc1 100644 --- a/.github/workflows/githubci.yml +++ b/.github/workflows/githubci.yml @@ -21,7 +21,7 @@ jobs: id: set-matrix working-directory: src/boards run: | - MATRIX_JSON=$(ls -d */ | sed 's/\/$//' | jq -R -s -c 'split("\n")[:-1]') + MATRIX_JSON=$(ls -d */ | sed 's/\/$//' | grep -vE '_s340$' | jq -R -s -c 'split("\n")[:-1]') echo "matrix=$MATRIX_JSON" echo "matrix=$MATRIX_JSON" >> $GITHUB_OUTPUT diff --git a/.gitignore b/.gitignore index 1c244fca..e7b13c9f 100644 --- a/.gitignore +++ b/.gitignore @@ -65,3 +65,7 @@ TAGS .DS_Store Makefile.user + +# Exclude all SoftDevice S340 files +lib/softdevice/s340* + diff --git a/CMakeLists.txt b/CMakeLists.txt index 567a815a..6d21fa64 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -227,22 +227,37 @@ if (MCU_VARIANT STREQUAL "nrf52") S132 ) elseif (MCU_VARIANT STREQUAL "nrf52833") - set(SD_NAME s140) set(DFU_DEV_REV 52833) set(DFU_APP_DATA_RESERVED 7*4096) - target_compile_definitions(bootloader PUBLIC - NRF52833_XXAA - S140 - ) + if (SD_NAME STREQUAL "s340") + target_compile_definitions(bootloader PUBLIC + NRF52833_XXAA + S340 + ) + else () + set(SD_NAME s140) + target_compile_definitions(bootloader PUBLIC + NRF52833_XXAA + S140 + ) + endif () elseif (MCU_VARIANT STREQUAL "nrf52840") - set(SD_NAME s140) set(DFU_DEV_REV 52840) # App reserved 40KB (8+32) to match circuitpython for 840 set(DFU_APP_DATA_RESERVED 10*4096) - target_compile_definitions(bootloader PUBLIC - NRF52840_XXAA - S140 - ) + if (SD_NAME STREQUAL "s340") + target_compile_definitions(bootloader PUBLIC + NRF52840_XXAA + S340 + ) + else () + set(SD_NAME s140) + target_compile_definitions(bootloader PUBLIC + NRF52840_XXAA + S140 + ) + endif () + else () message(FATAL_ERROR "MCU_VARIANT ${MCU_VARIANT} is unknown") endif () diff --git a/Makefile b/Makefile index f3458267..22ad0072 100644 --- a/Makefile +++ b/Makefile @@ -109,14 +109,22 @@ ifeq ($(MCU_SUB_VARIANT),nrf52) CFLAGS += -DNRF52 -DNRF52832_XXAA -DS132 DFU_APP_DATA_RESERVED=7*4096 else ifeq ($(MCU_SUB_VARIANT),nrf52833) - SD_NAME = s140 + ifeq ($(SD_NAME),s340) + CFLAGS += -DNRF52833_XXAA -DS340 + else + SD_NAME = s140 + CFLAGS += -DNRF52833_XXAA -DS140 + endif DFU_DEV_REV = 52833 - CFLAGS += -DNRF52833_XXAA -DS140 DFU_APP_DATA_RESERVED=7*4096 else ifeq ($(MCU_SUB_VARIANT),nrf52840) - SD_NAME = s140 + ifeq ($(SD_NAME),s340) + CFLAGS += -DNRF52840_XXAA -DS340 + else + SD_NAME = s140 + CFLAGS += -DNRF52840_XXAA -DS140 + endif DFU_DEV_REV = 52840 - CFLAGS += -DNRF52840_XXAA -DS140 # App reserved 40KB (8+32) to match circuitpython for 840 DFU_APP_DATA_RESERVED=10*4096 else diff --git a/README.md b/README.md index b4e3cdb7..ce2ab06d 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,25 @@ Supported boards are: feather_nrf52840_express feather_nrf52840_express pca10056 Makefile:90: *** BOARD not defined. Stop ``` +#### Working with SoftDevice S340: +The SoftDevice S340 is closed-source, not publicly available and is only distributed by Garmin Canada Inc. + +In order to be able to download the required ANT+ capable SoftDevice, you need to register an 'ANT+ Adopter' account at [thisisant.com](https://www.thisisant.com/register/). After around one business day you will receive access to the resources there. Then do the following steps: +- Download the SoftDevice S340 v7.0.1 [(here)](https://www.thisisant.com/developer/components/nrf52832#tab_protocol_stacks_tab) and extract its contents +- Under `lib/softdevice` in this repository there is a folder called `s340_nrf52_7.0.1` + - Copy the API folder `ANT_s340_nrf52_7.0.1.API`, the license agreement `License_Agreement_ANT_Softdevice_rev3_3.pdf` and the hex file `ANT_s340_nrf52_7.0.1.hex` from the extracted contents to it. + - Rename the API folder to `s340_nrf52_7.0.1_API` + - Rename the hex file to `s340_nrf52_7.0.1_softdevice.hex` + - Modify `lib/softdevice/s340_nrf52_7.0.1_API/include/nrf_sdm.h` on line 191 and remove the two slashes at the beginning of `//#define...` to use the *evaluation key* for the ANT SoftDevice. + - **VERY IMPORTANT:** You MUST obtain a valid commercial license key BEFORE releasing a product to market that uses the ANT SoftDevice! + +To add or modify a board with an ANT+ capable SoftDevice S340 the `SD_VERSION` and `SD_NAME` parameters in the corresponding `board.mk` file have to be set: +``` +SD_VERSION = 7.0.1 +SD_NAME = s340 +``` +**Important:** When adding a new board you must add the suffix `_s340` to the folder name to exclude it from automatic builds. + ### Flash To flash the bootloader (without softdevice/mbr) using JLink: diff --git a/lib/softdevice/s340_nrf52_7.0.1/readme.txt b/lib/softdevice/s340_nrf52_7.0.1/readme.txt new file mode 100644 index 00000000..3cde0e65 --- /dev/null +++ b/lib/softdevice/s340_nrf52_7.0.1/readme.txt @@ -0,0 +1 @@ +Place SoftDevice S340 v7.0.1 files here \ No newline at end of file diff --git a/src/main.c b/src/main.c index 92ad0208..b48e1cbd 100644 --- a/src/main.c +++ b/src/main.c @@ -157,6 +157,14 @@ static void mbr_init_sd(void) { sd_mbr_command(&com); } +// Helper function to check if SoftDevice is already enabled. +// Especially SoftDevice S340 locks up when calling sd_softdevice_disable() and it's not enabled. +static bool is_sd_enabled(void) { + uint8_t sd_enabled = 0; + sd_softdevice_is_enabled(&sd_enabled); + return (sd_enabled == 1); +} + //--------------------------------------------------------------------+ // //--------------------------------------------------------------------+ @@ -206,13 +214,16 @@ int main(void) { if (!_sd_inited) mbr_init_sd(); // Make sure SD is disabled - sd_softdevice_disable(); + if (is_sd_enabled()) { + sd_softdevice_disable(); + } } // clear in case we kept DFU_DBL_RESET_APP there (*dbl_reset_mem) = 0; // start application + PRINTF("Starting app...\r\n"); bootloader_app_start(); } @@ -306,7 +317,9 @@ static void check_dfu_mode(void) { } if (_ota_dfu) { - sd_softdevice_disable(); + if (is_sd_enabled()) { + sd_softdevice_disable(); + } } else { usb_teardown(); } @@ -326,7 +339,11 @@ static uint32_t ble_stack_init(void) { .rc_temp_ctiv = 2, .accuracy = NRF_CLOCK_LF_ACCURACY_250_PPM }; - sd_softdevice_enable(&clock_cfg, app_error_fault_handler); + #ifndef ANT_LICENSE_KEY + sd_softdevice_enable(&clock_cfg, app_error_fault_handler); + #else + sd_softdevice_enable(&clock_cfg, app_error_fault_handler, ANT_LICENSE_KEY); + #endif sd_nvic_EnableIRQ(SD_EVT_IRQn); /*------------- Configure BLE params -------------*/ From de9a9b79f4e60b68bda892e359276ec9be841273 Mon Sep 17 00:00:00 2001 From: JuergenLeber Date: Tue, 10 Jun 2025 18:44:51 +0200 Subject: [PATCH 2/3] refactor: deduplicate code for disabling SD --- src/main.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main.c b/src/main.c index b48e1cbd..2884993b 100644 --- a/src/main.c +++ b/src/main.c @@ -157,12 +157,14 @@ static void mbr_init_sd(void) { sd_mbr_command(&com); } -// Helper function to check if SoftDevice is already enabled. +// Helper function to disable the SoftDevice with upfront check if it is enabled. // Especially SoftDevice S340 locks up when calling sd_softdevice_disable() and it's not enabled. -static bool is_sd_enabled(void) { +static void sd_disable(void) { uint8_t sd_enabled = 0; sd_softdevice_is_enabled(&sd_enabled); - return (sd_enabled == 1); + if (sd_enabled == 1) { + sd_softdevice_disable(); + } } //--------------------------------------------------------------------+ @@ -214,9 +216,7 @@ int main(void) { if (!_sd_inited) mbr_init_sd(); // Make sure SD is disabled - if (is_sd_enabled()) { - sd_softdevice_disable(); - } + sd_disable(); } // clear in case we kept DFU_DBL_RESET_APP there @@ -317,9 +317,7 @@ static void check_dfu_mode(void) { } if (_ota_dfu) { - if (is_sd_enabled()) { - sd_softdevice_disable(); - } + sd_disable(); } else { usb_teardown(); } From 31c3081668c0ffe0215c125b88d3cb5c2447050e Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 30 Jun 2025 17:32:49 +0700 Subject: [PATCH 3/3] update makefile/cmake for softdevice macro. Remove the _s340 filter in ci --- .github/workflows/githubci.yml | 2 +- CMakeLists.txt | 34 ++++++----------------- Makefile | 25 ++++++++--------- lib/softdevice/s340_nrf52_7.0.1/readme.md | 3 +- src/main.c | 15 +++++----- 5 files changed, 29 insertions(+), 50 deletions(-) diff --git a/.github/workflows/githubci.yml b/.github/workflows/githubci.yml index 55086fc1..68b4f7f2 100644 --- a/.github/workflows/githubci.yml +++ b/.github/workflows/githubci.yml @@ -21,7 +21,7 @@ jobs: id: set-matrix working-directory: src/boards run: | - MATRIX_JSON=$(ls -d */ | sed 's/\/$//' | grep -vE '_s340$' | jq -R -s -c 'split("\n")[:-1]') + MATRIX_JSON=$(ls -d */ | sed 's/\/$//' | jq -R -s -c 'split("\n")[:-1]') echo "matrix=$MATRIX_JSON" echo "matrix=$MATRIX_JSON" >> $GITHUB_OUTPUT diff --git a/CMakeLists.txt b/CMakeLists.txt index e66a274a..71331356 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -225,47 +225,29 @@ if (MCU_VARIANT STREQUAL "nrf52") set(SD_NAME s132) set(DFU_DEV_REV 0xADAF) set(DFU_APP_DATA_RESERVED 7*4096) - target_compile_definitions(bootloader PUBLIC - NRF52 - NRF52832_XXAA - S132 - ) + target_compile_definitions(bootloader PUBLIC NRF52 NRF52832_XXAA) elseif (MCU_VARIANT STREQUAL "nrf52833") set(DFU_DEV_REV 52833) set(DFU_APP_DATA_RESERVED 7*4096) - if (SD_NAME STREQUAL "s340") - target_compile_definitions(bootloader PUBLIC - NRF52833_XXAA - S340 - ) - else () + target_compile_definitions(bootloader PUBLIC NRF52833_XXAA) + if (NOT DEFINED SD_NAME) set(SD_NAME s140) - target_compile_definitions(bootloader PUBLIC - NRF52833_XXAA - S140 - ) endif () elseif (MCU_VARIANT STREQUAL "nrf52840") set(DFU_DEV_REV 52840) # App reserved 40KB (8+32) to match circuitpython for 840 set(DFU_APP_DATA_RESERVED 10*4096) - if (SD_NAME STREQUAL "s340") - target_compile_definitions(bootloader PUBLIC - NRF52840_XXAA - S340 - ) - else () + target_compile_definitions(bootloader PUBLIC NRF52840_XXAA) + if (NOT DEFINED SD_NAME) set(SD_NAME s140) - target_compile_definitions(bootloader PUBLIC - NRF52840_XXAA - S140 - ) endif () - else () message(FATAL_ERROR "MCU_VARIANT ${MCU_VARIANT} is unknown") endif () +string(TOUPPER ${SD_NAME} SD_NAME_UPPER) +target_compile_definitions(bootloader PUBLIC ${SD_NAME_UPPER}) + set(SD_FILENAME ${SD_NAME}_nrf52_${SD_VERSION}) set(SD_HEX ${SOFTDEVICE_DIR}/${SD_FILENAME}/${SD_FILENAME}_softdevice.hex) diff --git a/Makefile b/Makefile index 22ad0072..bea75cf9 100644 --- a/Makefile +++ b/Makefile @@ -104,33 +104,32 @@ BIN = _bin/$(BOARD) # MCU_SUB_VARIANT can be nrf52 (nrf52832), nrf52833, nrf52840 ifeq ($(MCU_SUB_VARIANT),nrf52) + CFLAGS += -DNRF52 -DNRF52832_XXAA SD_NAME = s132 DFU_DEV_REV = 0xADAF - CFLAGS += -DNRF52 -DNRF52832_XXAA -DS132 DFU_APP_DATA_RESERVED=7*4096 else ifeq ($(MCU_SUB_VARIANT),nrf52833) - ifeq ($(SD_NAME),s340) - CFLAGS += -DNRF52833_XXAA -DS340 - else - SD_NAME = s140 - CFLAGS += -DNRF52833_XXAA -DS140 - endif + CFLAGS += -DNRF52833_XXAA DFU_DEV_REV = 52833 DFU_APP_DATA_RESERVED=7*4096 + ifndef SD_NAME + SD_NAME = s140 + endif else ifeq ($(MCU_SUB_VARIANT),nrf52840) - ifeq ($(SD_NAME),s340) - CFLAGS += -DNRF52840_XXAA -DS340 - else - SD_NAME = s140 - CFLAGS += -DNRF52840_XXAA -DS140 - endif + CFLAGS += -DNRF52840_XXAA DFU_DEV_REV = 52840 # App reserved 40KB (8+32) to match circuitpython for 840 DFU_APP_DATA_RESERVED=10*4096 + ifndef SD_NAME + SD_NAME = s140 + endif else $(error Sub Variant $(MCU_SUB_VARIANT) is unknown) endif +SD_NAME_UPPER = $(subst s,S,${SD_NAME}) +CFLAGS += -D$(SD_NAME_UPPER) + #------------------------------------------------------------------------------ # SOURCE FILES #------------------------------------------------------------------------------ diff --git a/lib/softdevice/s340_nrf52_7.0.1/readme.md b/lib/softdevice/s340_nrf52_7.0.1/readme.md index 55b790be..c408bbda 100644 --- a/lib/softdevice/s340_nrf52_7.0.1/readme.md +++ b/lib/softdevice/s340_nrf52_7.0.1/readme.md @@ -15,5 +15,4 @@ To add or modify a board with an ANT+ capable SoftDevice S340 the `SD_VERSION` a ``` SD_VERSION = 7.0.1 SD_NAME = s340 -``` -**Important:** When adding a new board you must add the suffix `_s340` to the folder name to exclude it from automatic builds. \ No newline at end of file +``` \ No newline at end of file diff --git a/src/main.c b/src/main.c index cd9de877..a4c0a705 100644 --- a/src/main.c +++ b/src/main.c @@ -157,9 +157,8 @@ static void mbr_init_sd(void) { sd_mbr_command(&com); } -// Helper function to disable the SoftDevice with upfront check if it is enabled. -// Especially SoftDevice S340 locks up when calling sd_softdevice_disable() and it's not enabled. -static void sd_disable(void) { +// Disable the SoftDevice if it is enabled. +static void disable_softdevice(void) { uint8_t sd_enabled = 0; sd_softdevice_is_enabled(&sd_enabled); if (sd_enabled == 1) { @@ -216,7 +215,7 @@ int main(void) { if (!_sd_inited) mbr_init_sd(); // Make sure SD is disabled - sd_disable(); + disable_softdevice(); } // clear in case we kept DFU_DBL_RESET_APP there @@ -317,7 +316,7 @@ static void check_dfu_mode(void) { } if (_ota_dfu) { - sd_disable(); + disable_softdevice(); } else { usb_teardown(); } @@ -337,10 +336,10 @@ static uint32_t ble_stack_init(void) { .rc_temp_ctiv = 2, .accuracy = NRF_CLOCK_LF_ACCURACY_250_PPM }; - #ifndef ANT_LICENSE_KEY - sd_softdevice_enable(&clock_cfg, app_error_fault_handler); - #else + #ifdef ANT_LICENSE_KEY sd_softdevice_enable(&clock_cfg, app_error_fault_handler, ANT_LICENSE_KEY); + #else + sd_softdevice_enable(&clock_cfg, app_error_fault_handler); #endif sd_nvic_EnableIRQ(SD_EVT_IRQn);