From cfd248ee050b334b5cd67f377ec5d829bd97b14f Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Tue, 6 Feb 2018 12:39:56 +1100 Subject: [PATCH 1/5] Enable ASSERTS's in nrf sdk to catch coding errors. These will now flow through to mbed standard error handling. --- .../targets/TARGET_NORDIC/TARGET_NRF51/source/btle/btle.cpp | 4 ++-- targets/targets.json | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF51/source/btle/btle.cpp b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF51/source/btle/btle.cpp index 0873336a44..25bbad56b0 100644 --- a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF51/source/btle/btle.cpp +++ b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF51/source/btle/btle.cpp @@ -299,10 +299,10 @@ static void btle_handler(ble_evt_t *p_ble_evt) gattServer.hwCallback(p_ble_evt); } -/*! @brief Callback when an error occurs inside the SoftDevice */ +/*! @brief Callback when an error occurs inside the SoftDevice or ASSERT in debug*/ void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) { - ASSERT_TRUE(false, (void) 0); + error("nrf failure at %s:%d", p_file_name, line_num); } /*! diff --git a/targets/targets.json b/targets/targets.json index 6285ffc7c6..507829213d 100755 --- a/targets/targets.json +++ b/targets/targets.json @@ -3450,7 +3450,8 @@ "CMSIS_VECTAB_VIRTUAL", "CMSIS_VECTAB_VIRTUAL_HEADER_FILE=\"cmsis_nvic.h\"", "NO_SYSTICK", - "MBED_TICKLESS" + "MBED_TICKLESS", + "DEBUG_NRF_USER" ], "MERGE_BOOTLOADER": false, "extra_labels": [ From f49070c876255745b734735345e43d9593b08e21 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Tue, 6 Feb 2018 12:40:38 +1100 Subject: [PATCH 2/5] Address known typo in nrf sdk 11 ref: https://devzone.nordicsemi.com/f/nordic-q-a/14000/nrf_drv_adc-c-doesn-t-compile-with--ddebug_nrf --- .../sdk/drivers_nrf/adc/nrf_drv_adc.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF51/TARGET_MCU_NRF51822_UNIFIED/sdk/drivers_nrf/adc/nrf_drv_adc.c b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF51/TARGET_MCU_NRF51822_UNIFIED/sdk/drivers_nrf/adc/nrf_drv_adc.c index 6bca7511be..3909cfcdb1 100644 --- a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF51/TARGET_MCU_NRF51822_UNIFIED/sdk/drivers_nrf/adc/nrf_drv_adc.c +++ b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF51/TARGET_MCU_NRF51822_UNIFIED/sdk/drivers_nrf/adc/nrf_drv_adc.c @@ -92,7 +92,7 @@ void nrf_drv_adc_uninit(void) void nrf_drv_adc_channel_enable(nrf_drv_adc_channel_t * const p_channel) { - ASSERT(mp_state == NRF_DRV_STATE_INITIALIZED); + ASSERT(m_cb.state == NRF_DRV_STATE_INITIALIZED); ASSERT(!is_address_from_stack(p_channel)); p_channel->p_next = NULL; @@ -114,7 +114,7 @@ void nrf_drv_adc_channel_enable(nrf_drv_adc_channel_t * const p_channel) void nrf_drv_adc_channel_disable(nrf_drv_adc_channel_t * const p_channel) { - ASSERT(mp_state == NRF_DRV_STATE_INITIALIZED); + ASSERT(m_cb.state == NRF_DRV_STATE_INITIALIZED); ASSERT(m_cb.p_head); nrf_drv_adc_channel_t * p_curr_channel = m_cb.p_head; @@ -137,7 +137,7 @@ void nrf_drv_adc_channel_disable(nrf_drv_adc_channel_t * const p_channel) void nrf_drv_adc_sample(void) { - ASSERT(mp_state != NRF_DRV_STATE_UNINITIALIZED); + ASSERT(m_cb.state != NRF_DRV_STATE_UNINITIALIZED); ASSERT(!nrf_adc_is_busy()); nrf_adc_start(); } @@ -145,7 +145,7 @@ void nrf_drv_adc_sample(void) ret_code_t nrf_drv_adc_sample_convert(nrf_drv_adc_channel_t const * const p_channel, nrf_adc_value_t * p_value) { - ASSERT(mp_state != NRF_DRV_STATE_UNINITIALIZED); + ASSERT(m_cb.state != NRF_DRV_STATE_UNINITIALIZED); if(m_cb.state == NRF_DRV_STATE_POWERED_ON) { return NRF_ERROR_BUSY; @@ -212,7 +212,7 @@ static bool adc_sample_process() ret_code_t nrf_drv_adc_buffer_convert(nrf_adc_value_t * buffer, uint16_t size) { - ASSERT(mp_state != NRF_DRV_STATE_UNINITIALIZED); + ASSERT(m_cb.state != NRF_DRV_STATE_UNINITIALIZED); if(m_cb.state == NRF_DRV_STATE_POWERED_ON) { return NRF_ERROR_BUSY; @@ -250,7 +250,7 @@ ret_code_t nrf_drv_adc_buffer_convert(nrf_adc_value_t * buffer, uint16_t size) bool nrf_drv_adc_is_busy(void) { - ASSERT(mp_state != NRF_DRV_STATE_UNINITIALIZED); + ASSERT(m_cb.state != NRF_DRV_STATE_UNINITIALIZED); return (m_cb.state == NRF_DRV_STATE_POWERED_ON) ? true : false; } From 244b7a5c699b02a831d7367a80a5edd0efb5fd2e Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Fri, 23 Feb 2018 14:39:26 +1100 Subject: [PATCH 3/5] Only enable DEBUG_NRF_USER when NDEBUG is not set --- .../TARGET_NRF5x/TARGET_SDK_11/libraries/util/nrf_assert.h | 4 ++++ .../TARGET_NRF5x/TARGET_SDK_14_2/libraries/util/nrf_assert.h | 4 ++++ targets/targets.json | 3 +-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_SDK_11/libraries/util/nrf_assert.h b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_SDK_11/libraries/util/nrf_assert.h index 8ef5a5d501..292c398017 100644 --- a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_SDK_11/libraries/util/nrf_assert.h +++ b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_SDK_11/libraries/util/nrf_assert.h @@ -52,6 +52,10 @@ extern "C" { #endif +#if !defined(NDEBUG) && !defined(DEBUG_NRF_USER) +#define DEBUG_NRF_USER +#endif + #if defined(DEBUG_NRF) || defined(DEBUG_NRF_USER) /** @brief Function for handling assertions. diff --git a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_SDK_14_2/libraries/util/nrf_assert.h b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_SDK_14_2/libraries/util/nrf_assert.h index 2ec647c87b..67c945aa1c 100644 --- a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_SDK_14_2/libraries/util/nrf_assert.h +++ b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_SDK_14_2/libraries/util/nrf_assert.h @@ -76,6 +76,10 @@ extern "C" { void assert_nrf_callback(uint16_t line_num, const uint8_t *file_name); //lint -restore +#if !defined(NDEBUG) && !defined(DEBUG_NRF_USER) +#define DEBUG_NRF_USER +#endif + #if (defined(DEBUG_NRF) || defined(DEBUG_NRF_USER)) #define NRF_ASSERT_PRESENT 1 #else diff --git a/targets/targets.json b/targets/targets.json index 507829213d..6285ffc7c6 100755 --- a/targets/targets.json +++ b/targets/targets.json @@ -3450,8 +3450,7 @@ "CMSIS_VECTAB_VIRTUAL", "CMSIS_VECTAB_VIRTUAL_HEADER_FILE=\"cmsis_nvic.h\"", "NO_SYSTICK", - "MBED_TICKLESS", - "DEBUG_NRF_USER" + "MBED_TICKLESS" ], "MERGE_BOOTLOADER": false, "extra_labels": [ From 5bb3ede8904d2488c60b1d7539dff2edcce87062 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Tue, 27 Mar 2018 12:37:42 +1100 Subject: [PATCH 4/5] Comment out the assert test that requires non-existent symbols from linker --- .../sdk/drivers_nrf/adc/nrf_drv_adc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF51/TARGET_MCU_NRF51822_UNIFIED/sdk/drivers_nrf/adc/nrf_drv_adc.c b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF51/TARGET_MCU_NRF51822_UNIFIED/sdk/drivers_nrf/adc/nrf_drv_adc.c index 3909cfcdb1..0312297e12 100644 --- a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF51/TARGET_MCU_NRF51822_UNIFIED/sdk/drivers_nrf/adc/nrf_drv_adc.c +++ b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF51/TARGET_MCU_NRF51822_UNIFIED/sdk/drivers_nrf/adc/nrf_drv_adc.c @@ -93,7 +93,8 @@ void nrf_drv_adc_uninit(void) void nrf_drv_adc_channel_enable(nrf_drv_adc_channel_t * const p_channel) { ASSERT(m_cb.state == NRF_DRV_STATE_INITIALIZED); - ASSERT(!is_address_from_stack(p_channel)); + // This assert has been removed as it requires non-existent symbols from linker + //ASSERT(!is_address_from_stack(p_channel)); p_channel->p_next = NULL; if (m_cb.p_head == NULL) From 67140a2706cade7ad4ed565d461bc474ec02deee Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 9 May 2018 14:17:40 +1000 Subject: [PATCH 5/5] Redirect NRF asserts to mbed error() in TARGET_NRF5x and SDK 14.2 Add related details to TARGET_NRF5x Readme's --- .../TARGET_NORDIC/TARGET_NRF52/source/btle/btle.cpp | 4 ++-- targets/TARGET_NORDIC/TARGET_NRF5x/README.md | 12 +++++++++++- .../TARGET_MCU_NRF52832/config/sdk_config.h | 6 ++++++ .../TARGET_MCU_NRF52840/config/sdk_config.h | 5 +++++ .../TARGET_SDK_14_2/libraries/util/nrf_assert.h | 4 ---- 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/btle/btle.cpp b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/btle/btle.cpp index 5d6def57ac..26beb9c725 100644 --- a/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/btle/btle.cpp +++ b/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF52/source/btle/btle.cpp @@ -449,10 +449,10 @@ static void btle_handler(ble_evt_t *p_ble_evt) gattServer.hwCallback(p_ble_evt); } -/*! @brief Callback when an error occurs inside the SoftDevice */ +/*! @brief Callback when an error occurs inside the SoftDevice or ASSERT in debug*/ void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name) { - ASSERT_TRUE(false, (void) 0); + error("nrf failure at %s:%d", p_file_name, line_num); } #if NRF_SD_BLE_API_VERSION >= 5 diff --git a/targets/TARGET_NORDIC/TARGET_NRF5x/README.md b/targets/TARGET_NORDIC/TARGET_NRF5x/README.md index 8c3475e479..e9b82a650d 100644 --- a/targets/TARGET_NORDIC/TARGET_NRF5x/README.md +++ b/targets/TARGET_NORDIC/TARGET_NRF5x/README.md @@ -1,4 +1,4 @@ -# Nordic NRF52 +# Nordic NRF5x ## Adding New Targets Based On Nordic NRF52832 And NRF52840 MCUs @@ -144,6 +144,16 @@ Because each DMA buffer must be at least 5 bytes deep, each buffer is automatica The RTC2 ISR is set at the lowest interrupt priority to ensure that UARTE interrupts take precedence. The last 2 of the 4 RTC channels are used for decoupling UARTE ISR context from Mbed IRQ events. This ensures that any user code will only delay other user callbacks and idle flushing and puts an upper bound on the interrupt handling time for the UARTE ISR. + +#### Asserts + +The nordic asserts have been redirected to mbed error handling when building in debug mode. +The SDK file `mbed-os/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_SDK_14_2/libraries/util/nrf_assert.h` was modified to enable the asserts when NDEBUG is not defined. + +The assert handler is defined in mbed-os/features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF5x/source/btle/btle.cpp : assert_nrf_callback() which forwards assert failures to thye mbed error() handler. + + + #### Limitations * The UARTE hardware only supports 8-bit, None/Even parity, and 1 stop bit. diff --git a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52832/config/sdk_config.h b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52832/config/sdk_config.h index 89a3a962ef..cf4a3c0805 100644 --- a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52832/config/sdk_config.h +++ b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52832/config/sdk_config.h @@ -48,6 +48,11 @@ #endif // Board Support +// Enable NRF Asserts when Mbed NDEBUG is not set +#if !defined(NDEBUG) && !defined(DEBUG_NRF_USER) +#define DEBUG_NRF_USER +#endif + //========================================================== // BSP_BTN_BLE_ENABLED - bsp_btn_ble - Button Control for BLE @@ -4287,6 +4292,7 @@ //========================================================== // NRF_LOG_ENABLED - Logging module for nRF5 SDK //========================================================== + #ifndef NRF_LOG_ENABLED #define NRF_LOG_ENABLED 0 #endif diff --git a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52840/config/sdk_config.h b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52840/config/sdk_config.h index 393d9be522..834e199156 100644 --- a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52840/config/sdk_config.h +++ b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52840/config/sdk_config.h @@ -48,6 +48,11 @@ #endif // Board Support +// Enable NRF Asserts when Mbed NDEBUG is not set +#if !defined(NDEBUG) && !defined(DEBUG_NRF_USER) +#define DEBUG_NRF_USER +#endif + //========================================================== // BSP_BTN_BLE_ENABLED - bsp_btn_ble - Button Control for BLE diff --git a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_SDK_14_2/libraries/util/nrf_assert.h b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_SDK_14_2/libraries/util/nrf_assert.h index 67c945aa1c..2ec647c87b 100644 --- a/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_SDK_14_2/libraries/util/nrf_assert.h +++ b/targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_SDK_14_2/libraries/util/nrf_assert.h @@ -76,10 +76,6 @@ extern "C" { void assert_nrf_callback(uint16_t line_num, const uint8_t *file_name); //lint -restore -#if !defined(NDEBUG) && !defined(DEBUG_NRF_USER) -#define DEBUG_NRF_USER -#endif - #if (defined(DEBUG_NRF) || defined(DEBUG_NRF_USER)) #define NRF_ASSERT_PRESENT 1 #else