From 61effcaffd24185de7e11ff55affe3b8cb05b0de Mon Sep 17 00:00:00 2001 From: Gordon Williams Date: Fri, 22 Sep 2023 16:34:05 +0100 Subject: [PATCH] nRF52: bootloader asks for connection interval 7.5->30ms now, not just 15ms. Should improve DFU on iOS 16 which doesn't honour 15ms request --- ChangeLog | 1 + .../bootloader/ble_dfu/nrf_ble_dfu.c | 48 ++--- targets/nrf5x/bluetooth.c | 1 + targets/nrf5x_dfu/sdk12/sdk_config.h | 191 ++++++++++-------- targets/nrf5x_dfu/sdk15/sdk_config.h | 4 +- 5 files changed, 134 insertions(+), 111 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5c0de5a968..c787d1a1e9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,7 @@ Ensure eval("1;;;")==1 - eval("1;")==1 before, but not eval("1;;") Ensure AssignmentExpr returns the value of LHS, not the LHS. `(a=2)=3` now fails (as per spec) Ensure default args for arrow functions fail with error `(a,b=3)=>a+b` now fails (until default args get added) + nRF52: bootloader asks for connection interval 7.5->30ms now, not just 15ms. Should improve DFU on iOS 16 which doesn't honour 15ms request 2v19 : Fix Object.values/entries for numeric keys after 2v18 regression (fix #2375) nRF52: for SD>5 use static buffers for advertising and scan response data (#2367) diff --git a/targetlibs/nrf5x_12/components/libraries/bootloader/ble_dfu/nrf_ble_dfu.c b/targetlibs/nrf5x_12/components/libraries/bootloader/ble_dfu/nrf_ble_dfu.c index ef0c7ad96e..e3d3f675ce 100644 --- a/targetlibs/nrf5x_12/components/libraries/bootloader/ble_dfu/nrf_ble_dfu.c +++ b/targetlibs/nrf5x_12/components/libraries/bootloader/ble_dfu/nrf_ble_dfu.c @@ -1,30 +1,30 @@ /** * Copyright (c) 2016 - 2017, Nordic Semiconductor ASA - * + * * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: - * + * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. - * + * * 2. Redistributions in binary form, except as embedded into a Nordic * Semiconductor ASA integrated circuit in a product or a software update for * such product, must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. - * + * * 3. Neither the name of Nordic Semiconductor ASA nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. - * + * * 4. This software, with or without modification, must only be used with a * Nordic Semiconductor ASA integrated circuit. - * + * * 5. Any software provided in binary form under this license must not be reverse * engineered, decompiled, modified and/or disassembled. - * + * * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -35,7 +35,7 @@ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * + * */ #include "nrf_ble_dfu.h" @@ -143,7 +143,7 @@ static uint32_t conn_params_init(void) /**@brief Function for the Advertising functionality initialization. * * @details Encodes the required advertising data and passes it to the stack. - * The advertising data encoded here is specific for DFU. + * The advertising data encoded here is specific for DFU. * Setting advertising data can by done by calling @ref ble_advdata_set. */ static uint32_t advertising_init(uint8_t adv_flags) @@ -154,12 +154,12 @@ static uint32_t advertising_init(uint8_t adv_flags) uint16_t actual_device_name_length = max_device_name_length; uint8_t p_encoded_advdata[MAX_ADV_DATA_LENGTH]; - + // Encode flags. p_encoded_advdata[0] = 0x2; p_encoded_advdata[1] = BLE_GAP_AD_TYPE_FLAGS; p_encoded_advdata[2] = adv_flags; - + // Encode 'more available' uuid list. p_encoded_advdata[3] = 0x3; p_encoded_advdata[4] = BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE; @@ -172,7 +172,7 @@ static uint32_t advertising_init(uint8_t adv_flags) { return err_code; } - + // Set GAP device in advertising data. if (actual_device_name_length <= max_device_name_length) { @@ -524,7 +524,7 @@ static bool on_rw_authorize_req(ble_dfu_t * p_dfu, ble_evt_t * p_ble_evt) ble_gatts_rw_authorize_reply_params_t auth_reply = {0}; ble_gatts_evt_rw_authorize_request_t * p_authorize_request; ble_gatts_evt_write_t * p_ble_write_evt; - + p_authorize_request = &(p_ble_evt->evt.gatts_evt.params.authorize_request); p_ble_write_evt = &(p_ble_evt->evt.gatts_evt.params.authorize_request.request.write); @@ -544,7 +544,7 @@ static bool on_rw_authorize_req(ble_dfu_t * p_dfu, ble_evt_t * p_ble_evt) { // Send an error response to the peer indicating that the CCCD is improperly configured. auth_reply.params.write.gatt_status = BLE_GATT_STATUS_ATTERR_CPS_CCCD_CONFIG_ERROR; - + // Ignore response of auth reply (void)sd_ble_gatts_rw_authorize_reply(m_conn_handle, &auth_reply); return false; @@ -660,9 +660,9 @@ static void on_ble_evt(ble_evt_t * p_ble_evt) { if (on_rw_authorize_req(&m_dfu, p_ble_evt)) { - err_code = on_ctrl_pt_write(&m_dfu, + err_code = on_ctrl_pt_write(&m_dfu, &(p_ble_evt->evt.gatts_evt.params.authorize_request.request.write)); -#ifdef NRF_DFU_DEBUG_VERSION +#ifdef NRF_DFU_DEBUG_VERSION if (err_code != NRF_SUCCESS) { NRF_LOG_ERROR("Could not handle on_ctrl_pt_write. err_code: 0x%04x\r\n", err_code); @@ -684,19 +684,19 @@ static void on_ble_evt(ble_evt_t * p_ble_evt) err_code = sd_ble_gatts_sys_attr_set(p_ble_evt->evt.gap_evt.conn_handle, NULL, 0, 0); APP_ERROR_CHECK(err_code); break; - + case BLE_GATTS_EVT_WRITE: on_write(&m_dfu, p_ble_evt); break; #if (NRF_SD_BLE_API_VERSION == 3) case BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST: - err_code = sd_ble_gatts_exchange_mtu_reply(p_ble_evt->evt.gatts_evt.conn_handle, + err_code = sd_ble_gatts_exchange_mtu_reply(p_ble_evt->evt.gatts_evt.conn_handle, NRF_BLE_MAX_MTU_SIZE); APP_ERROR_CHECK(err_code); break; // BLE_GATTS_EVT_EXCHANGE_MTU_REQUEST #endif - + default: // No implementation needed. break; @@ -771,8 +771,8 @@ static uint32_t gap_params_init(void) strlen(DEVICE_NAME)); VERIFY_SUCCESS(err_code); - gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL; - gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL; + gap_conn_params.min_conn_interval = NRF_DFU_BLE_MIN_CONN_INTERVAL; + gap_conn_params.max_conn_interval = NRF_DFU_BLE_MAX_CONN_INTERVAL; gap_conn_params.slave_latency = SLAVE_LATENCY; gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT; @@ -809,8 +809,8 @@ static uint32_t ble_stack_init(bool init_softdevice) #if (NRF_SD_BLE_API_VERSION == 3) ble_enable_params.gatt_enable_params.att_mtu = NRF_BLE_MAX_MTU_SIZE; -#endif - +#endif + // Enable BLE stack. err_code = softdevice_enable(&ble_enable_params); return err_code; diff --git a/targets/nrf5x/bluetooth.c b/targets/nrf5x/bluetooth.c index 72dd64cdb7..2adeec919e 100644 --- a/targets/nrf5x/bluetooth.c +++ b/targets/nrf5x/bluetooth.c @@ -923,6 +923,7 @@ nrf_log_module_dynamic_data_t NRF_LOG_MODULE_DATA_DYNAMIC = { /// Function for handling an event from the Connection Parameters Module. static void on_conn_params_evt(ble_conn_params_evt_t * p_evt) { + // Either BLE_CONN_PARAMS_EVT_FAILED or BLE_CONN_PARAMS_EVT_SUCCEEDED - that's it } /// Sigh - NFC has lots of these, so we need to define it to build diff --git a/targets/nrf5x_dfu/sdk12/sdk_config.h b/targets/nrf5x_dfu/sdk12/sdk_config.h index 9c0848eb56..b8f49a373d 100644 --- a/targets/nrf5x_dfu/sdk12/sdk_config.h +++ b/targets/nrf5x_dfu/sdk12/sdk_config.h @@ -6,7 +6,7 @@ #ifdef USE_APP_CONFIG #include "app_config.h" #endif -// nRF_Drivers +// nRF_Drivers //========================================================== // CLOCK_ENABLED - nrf_drv_clock - CLOCK peripheral driver @@ -16,35 +16,35 @@ #endif #if CLOCK_ENABLED // CLOCK_CONFIG_XTAL_FREQ - HF XTAL Frequency - -// <0=> Default (64 MHz) + +// <0=> Default (64 MHz) #ifndef CLOCK_CONFIG_XTAL_FREQ #define CLOCK_CONFIG_XTAL_FREQ 0 #endif // CLOCK_CONFIG_LF_SRC - LF Clock Source - -// <0=> RC -// <1=> XTAL -// <2=> Synth + +// <0=> RC +// <1=> XTAL +// <2=> Synth #ifndef CLOCK_CONFIG_LF_SRC #define CLOCK_CONFIG_LF_SRC 1 #endif // CLOCK_CONFIG_IRQ_PRIORITY - Interrupt priority - + // Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice -// <0=> 0 (highest) -// <1=> 1 -// <2=> 2 -// <3=> 3 -// <4=> 4 -// <5=> 5 -// <6=> 6 -// <7=> 7 +// <0=> 0 (highest) +// <1=> 1 +// <2=> 2 +// <3=> 3 +// <4=> 4 +// <5=> 5 +// <6=> 6 +// <7=> 7 #ifndef CLOCK_CONFIG_IRQ_PRIORITY #define CLOCK_CONFIG_IRQ_PRIORITY 6 @@ -54,7 +54,7 @@ // // PERIPHERAL_RESOURCE_SHARING_ENABLED - nrf_drv_common - Peripheral drivers common module - + #ifndef PERIPHERAL_RESOURCE_SHARING_ENABLED #define PERIPHERAL_RESOURCE_SHARING_ENABLED 0 @@ -67,29 +67,29 @@ #endif #if RNG_ENABLED // RNG_CONFIG_ERROR_CORRECTION - Error correction - + #ifndef RNG_CONFIG_ERROR_CORRECTION #define RNG_CONFIG_ERROR_CORRECTION 0 #endif -// RNG_CONFIG_POOL_SIZE - Pool size +// RNG_CONFIG_POOL_SIZE - Pool size #ifndef RNG_CONFIG_POOL_SIZE #define RNG_CONFIG_POOL_SIZE 32 #endif // RNG_CONFIG_IRQ_PRIORITY - Interrupt priority - + // Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice -// <0=> 0 (highest) -// <1=> 1 -// <2=> 2 -// <3=> 3 -// <4=> 4 -// <5=> 5 -// <6=> 6 -// <7=> 7 +// <0=> 0 (highest) +// <1=> 1 +// <2=> 2 +// <3=> 3 +// <4=> 4 +// <5=> 5 +// <6=> 6 +// <7=> 7 #ifndef RNG_CONFIG_IRQ_PRIORITY #define RNG_CONFIG_IRQ_PRIORITY 6 @@ -98,14 +98,14 @@ #endif //RNG_ENABLED // -// +// //========================================================== -// nRF_Libraries +// nRF_Libraries //========================================================== // APP_FIFO_ENABLED - app_fifo - Software FIFO implementation - + #ifndef APP_FIFO_ENABLED #define APP_FIFO_ENABLED 1 @@ -118,14 +118,14 @@ #endif #if APP_SCHEDULER_ENABLED // APP_SCHEDULER_WITH_PAUSE - Enabling pause feature - + #ifndef APP_SCHEDULER_WITH_PAUSE #define APP_SCHEDULER_WITH_PAUSE 0 #endif // APP_SCHEDULER_WITH_PROFILER - Enabling scheduler profiling - + #ifndef APP_SCHEDULER_WITH_PROFILER #define APP_SCHEDULER_WITH_PROFILER 0 @@ -141,14 +141,14 @@ #endif #if APP_TIMER_ENABLED // APP_TIMER_WITH_PROFILER - Enable app_timer profiling - + #ifndef APP_TIMER_WITH_PROFILER #define APP_TIMER_WITH_PROFILER 0 #endif // APP_TIMER_KEEPS_RTC_ACTIVE - Enable RTC always on - + // If option is enabled RTC is kept running even if there is no active timers. // This option can be used when app_timer is used for timestamping. @@ -161,14 +161,14 @@ // // CRC32_ENABLED - crc32 - CRC32 calculation routines - + #ifndef CRC32_ENABLED #define CRC32_ENABLED 1 #endif // ECC_ENABLED - ecc - Elliptic Curve Cryptography Library - + #ifndef ECC_ENABLED #define ECC_ENABLED 0 @@ -180,7 +180,7 @@ #define FSTORAGE_ENABLED 1 #endif #if FSTORAGE_ENABLED -// FS_QUEUE_SIZE - Configures the size of the internal queue. +// FS_QUEUE_SIZE - Configures the size of the internal queue. // Increase this if there are many users, or if it is likely that many // operation will be queued at once without waiting for the previous operations // to complete. In general, increase the queue size if you frequently receive @@ -190,7 +190,7 @@ #define FS_QUEUE_SIZE 4 #endif -// FS_OP_MAX_RETRIES - Number attempts to execute an operation if the SoftDevice fails. +// FS_OP_MAX_RETRIES - Number attempts to execute an operation if the SoftDevice fails. // Increase this value if events return the @ref FS_ERR_OPERATION_TIMEOUT // error often. The SoftDevice may fail to schedule flash access due to high BLE activity. @@ -198,7 +198,7 @@ #define FS_OP_MAX_RETRIES 3 #endif -// FS_MAX_WRITE_SIZE_WORDS - Maximum number of words to be written to flash in a single operation. +// FS_MAX_WRITE_SIZE_WORDS - Maximum number of words to be written to flash in a single operation. // Tweaking this value can increase the chances of the SoftDevice being // able to fit flash operations in between radio activity. This value is bound by the // maximum number of words which the SoftDevice can write to flash in a single call to @@ -217,17 +217,17 @@ #define HCI_MEM_POOL_ENABLED 1 #endif #if HCI_MEM_POOL_ENABLED -// HCI_TX_BUF_SIZE - TX buffer size in bytes. +// HCI_TX_BUF_SIZE - TX buffer size in bytes. #ifndef HCI_TX_BUF_SIZE #define HCI_TX_BUF_SIZE 600 #endif -// HCI_RX_BUF_SIZE - RX buffer size in bytes. +// HCI_RX_BUF_SIZE - RX buffer size in bytes. #ifndef HCI_RX_BUF_SIZE #define HCI_RX_BUF_SIZE 600 #endif -// HCI_RX_BUF_QUEUE_SIZE - RX buffer queue size. +// HCI_RX_BUF_QUEUE_SIZE - RX buffer queue size. #ifndef HCI_RX_BUF_QUEUE_SIZE #define HCI_RX_BUF_QUEUE_SIZE 4 #endif @@ -235,10 +235,10 @@ #endif //HCI_MEM_POOL_ENABLED // -// +// // NRF_QUEUE_ENABLED - nrf_queue - Queue module - + #ifndef NRF_QUEUE_ENABLED #define NRF_QUEUE_ENABLED 1 @@ -246,7 +246,7 @@ //========================================================== -// nRF_Log +// nRF_Log //========================================================== // NRF_LOG_ENABLED - nrf_log - Logging @@ -262,48 +262,48 @@ #endif #if NRF_LOG_USES_COLORS // NRF_LOG_COLOR_DEFAULT - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White #ifndef NRF_LOG_COLOR_DEFAULT #define NRF_LOG_COLOR_DEFAULT 0 #endif // NRF_LOG_ERROR_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White #ifndef NRF_LOG_ERROR_COLOR #define NRF_LOG_ERROR_COLOR 0 #endif // NRF_LOG_WARNING_COLOR - ANSI escape code prefix. - -// <0=> Default -// <1=> Black -// <2=> Red -// <3=> Green -// <4=> Yellow -// <5=> Blue -// <6=> Magenta -// <7=> Cyan -// <8=> White + +// <0=> Default +// <1=> Black +// <2=> Red +// <3=> Green +// <4=> Yellow +// <5=> Blue +// <6=> Magenta +// <7=> Cyan +// <8=> White #ifndef NRF_LOG_WARNING_COLOR #define NRF_LOG_WARNING_COLOR 0 @@ -313,12 +313,12 @@ // // NRF_LOG_DEFAULT_LEVEL - Default Severity level - -// <0=> Off -// <1=> Error -// <2=> Warning -// <3=> Info -// <4=> Debug + +// <0=> Off +// <1=> Error +// <2=> Warning +// <3=> Info +// <4=> Debug #ifndef NRF_LOG_DEFAULT_LEVEL #define NRF_LOG_DEFAULT_LEVEL 3 @@ -332,7 +332,7 @@ #define NRF_LOG_DEFERRED 1 #endif #if NRF_LOG_DEFERRED -// NRF_LOG_DEFERRED_BUFSIZE - Size of the buffer for logs in words. +// NRF_LOG_DEFERRED_BUFSIZE - Size of the buffer for logs in words. // Must be power of 2 #ifndef NRF_LOG_DEFERRED_BUFSIZE @@ -343,7 +343,7 @@ // // NRF_LOG_USES_TIMESTAMP - Enable timestamping - + // Function for getting the timestamp is provided by the user @@ -354,8 +354,29 @@ #endif //NRF_LOG_ENABLED // -// +// + +//========================================================== + +// BLE DFU connection + //========================================================== +// NRF_DFU_BLE_MIN_CONN_INTERVAL - Minimum connection interval (units). +// Minimum GAP connection interval, in 1.25 ms units. + +#ifndef NRF_DFU_BLE_MIN_CONN_INTERVAL +#define NRF_DFU_BLE_MIN_CONN_INTERVAL 6 // 7.5ms +#endif + +// NRF_DFU_BLE_MAX_CONN_INTERVAL - Maximum connection interval (units). +// Maximum GAP connection interval, in 1.25 ms units. + +#ifndef NRF_DFU_BLE_MAX_CONN_INTERVAL +#define NRF_DFU_BLE_MAX_CONN_INTERVAL 24 // 30ms +#endif + +// + // <<< end of configuration section >>> #endif //SDK_CONFIG_H diff --git a/targets/nrf5x_dfu/sdk15/sdk_config.h b/targets/nrf5x_dfu/sdk15/sdk_config.h index 7e6ac2613a..1a6f04cd14 100644 --- a/targets/nrf5x_dfu/sdk15/sdk_config.h +++ b/targets/nrf5x_dfu/sdk15/sdk_config.h @@ -981,14 +981,14 @@ // Minimum GAP connection interval, in 1.25 ms units. #ifndef NRF_DFU_BLE_MIN_CONN_INTERVAL -#define NRF_DFU_BLE_MIN_CONN_INTERVAL 12 +#define NRF_DFU_BLE_MIN_CONN_INTERVAL 6 // 7.5ms #endif // NRF_DFU_BLE_MAX_CONN_INTERVAL - Maximum connection interval (units). // Maximum GAP connection interval, in 1.25 ms units. #ifndef NRF_DFU_BLE_MAX_CONN_INTERVAL -#define NRF_DFU_BLE_MAX_CONN_INTERVAL 12 +#define NRF_DFU_BLE_MAX_CONN_INTERVAL 24 // 30ms #endif // NRF_DFU_BLE_CONN_SUP_TIMEOUT_MS - Supervision timeout (ms).