From f63de4e8799f16c1eb6626c7521054b5e4778ad1 Mon Sep 17 00:00:00 2001 From: Christoph Honal Date: Mon, 21 Mar 2022 14:05:49 +0100 Subject: [PATCH 1/3] Nimble: Reduce BLE power usage This configures Nimble to enable the HFCLOCK and other Bluetooth peripherals only when needed, but 1500 us in advance. This time is recommended by the Mynewt docs. --- src/CMakeLists.txt | 1 + .../npl/freertos/include/nimble/nimble_npl_os.h | 14 ++++++++++++-- .../porting/npl/freertos/src/npl_os_freertos.c | 2 ++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3b7503fdda..a00663fadf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -778,6 +778,7 @@ add_definitions(-DNRF52 -DNRF52832 -DNRF52832_XXAA -DNRF52_PAN_74 -DNRF52_PAN_64 add_definitions(-DFREERTOS) add_definitions(-D__STACK_SIZE=1024) add_definitions(-D__HEAP_SIZE=4096) +add_definitions(-DMYNEWT_VAL_BLE_LL_RFMGMT_ENABLE_TIME=1500) # Note: Only use this for debugging # Derive the low frequency clock from the main clock (SYNT) diff --git a/src/libs/mynewt-nimble/porting/npl/freertos/include/nimble/nimble_npl_os.h b/src/libs/mynewt-nimble/porting/npl/freertos/include/nimble/nimble_npl_os.h index 545be0f4bb..3a3de40d8b 100644 --- a/src/libs/mynewt-nimble/porting/npl/freertos/include/nimble/nimble_npl_os.h +++ b/src/libs/mynewt-nimble/porting/npl/freertos/include/nimble/nimble_npl_os.h @@ -37,6 +37,8 @@ extern "C" { #define BLE_NPL_TIME_FOREVER portMAX_DELAY +extern volatile int ble_npl_in_critical; + /* This should be compatible with TickType_t */ typedef uint32_t ble_npl_time_t; typedef int32_t ble_npl_stime_t; @@ -282,14 +284,22 @@ static inline uint32_t ble_npl_hw_enter_critical(void) { //vPortEnterCritical(); - return npl_freertos_hw_enter_critical(); + ++ble_npl_in_critical; + return npl_freertos_hw_enter_critical(); } static inline void ble_npl_hw_exit_critical(uint32_t ctx) { - npl_freertos_hw_exit_critical(ctx); + --ble_npl_in_critical; + npl_freertos_hw_exit_critical(ctx); +} +static inline bool +ble_npl_hw_is_in_critical(void) +{ + // Do the same as RIOT and keep track of the critical state manually + return (ble_npl_in_critical > 0); } #ifdef __cplusplus diff --git a/src/libs/mynewt-nimble/porting/npl/freertos/src/npl_os_freertos.c b/src/libs/mynewt-nimble/porting/npl/freertos/src/npl_os_freertos.c index 875521abdc..667a751ce7 100644 --- a/src/libs/mynewt-nimble/porting/npl/freertos/src/npl_os_freertos.c +++ b/src/libs/mynewt-nimble/porting/npl/freertos/src/npl_os_freertos.c @@ -22,6 +22,8 @@ #include #include "nimble/nimble_npl.h" +volatile int ble_npl_in_critical = 0; + static inline bool in_isr(void) { From 2ae1bf6cc8e25073289e6d27474324528a087f2c Mon Sep 17 00:00:00 2001 From: Christoph Honal Date: Mon, 21 Mar 2022 14:07:21 +0100 Subject: [PATCH 2/3] Nimble: Enable optional debug output via RTT This allows better debugging of the bluetooth stack. --- src/libs/mynewt-nimble/porting/nimble/include/logcfg/logcfg.h | 2 +- src/libs/mynewt-nimble/porting/nimble/include/modlog/modlog.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libs/mynewt-nimble/porting/nimble/include/logcfg/logcfg.h b/src/libs/mynewt-nimble/porting/nimble/include/logcfg/logcfg.h index 837cdeac1f..556635cab0 100644 --- a/src/libs/mynewt-nimble/porting/nimble/include/logcfg/logcfg.h +++ b/src/libs/mynewt-nimble/porting/nimble/include/logcfg/logcfg.h @@ -8,7 +8,7 @@ #include "modlog/modlog.h" #include "log_common/log_common.h" -#define BLE_HS_LOG_DEBUG(...) IGNORE(__VA_ARGS__) +#define BLE_HS_LOG_DEBUG(...) MODLOG_DEBUG(4, __VA_ARGS__) #define BLE_HS_LOG_INFO(...) MODLOG_INFO(4, __VA_ARGS__) #define BLE_HS_LOG_WARN(...) MODLOG_WARN(4, __VA_ARGS__) #define BLE_HS_LOG_ERROR(...) MODLOG_ERROR(4, __VA_ARGS__) diff --git a/src/libs/mynewt-nimble/porting/nimble/include/modlog/modlog.h b/src/libs/mynewt-nimble/porting/nimble/include/modlog/modlog.h index 0390461069..5b0ef45c22 100644 --- a/src/libs/mynewt-nimble/porting/nimble/include/modlog/modlog.h +++ b/src/libs/mynewt-nimble/porting/nimble/include/modlog/modlog.h @@ -20,7 +20,8 @@ #ifndef H_MODLOG_ #define H_MODLOG_ -#include +#include "SEGGER_RTT.h" +#define printf(...) SEGGER_RTT_printf(0, __VA_ARGS__) #include "log_common/log_common.h" #include "log/log.h" From 31c022cd13ab3700ea62e0b9118f80ae54a056be Mon Sep 17 00:00:00 2001 From: Christoph Honal Date: Fri, 25 Mar 2022 12:46:49 +0100 Subject: [PATCH 3/3] Nimble: Compatibility with LFRC calibration Nimble has to be aware of the low frequency clock calibration procedure, in order to not interfere with the usage of the HFCLK. For more info, see https://github.com/apache/mynewt-nimble/issues/1207 --- src/libs/mynewt-nimble/nimble/drivers/nrf52/src/ble_phy.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libs/mynewt-nimble/nimble/drivers/nrf52/src/ble_phy.c b/src/libs/mynewt-nimble/nimble/drivers/nrf52/src/ble_phy.c index 2f6ce08e72..aa25096b7e 100644 --- a/src/libs/mynewt-nimble/nimble/drivers/nrf52/src/ble_phy.c +++ b/src/libs/mynewt-nimble/nimble/drivers/nrf52/src/ble_phy.c @@ -37,6 +37,7 @@ #else #include "core_cm4.h" #endif +#include #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_CODED_PHY) #if !MYNEWT_VAL_CHOICE(MCU_TARGET, nRF52840) && !MYNEWT_VAL_CHOICE(MCU_TARGET, nRF52811) @@ -2101,7 +2102,7 @@ ble_phy_rfclk_enable(void) #if MYNEWT nrf52_clock_hfxo_request(); #else - NRF_CLOCK->TASKS_HFCLKSTART = 1; + nrf_drv_clock_hfclk_request(NULL); #endif } @@ -2111,6 +2112,6 @@ ble_phy_rfclk_disable(void) #if MYNEWT nrf52_clock_hfxo_release(); #else - NRF_CLOCK->TASKS_HFCLKSTOP = 1; + nrf_drv_clock_hfclk_release(); #endif }