Skip to content

Commit b69dc2b

Browse files
committed
Update BLE SoftDevice from SDK14 to SDK15
- Adjust memory for SoftDevice - Enable PRIO=5 for interrupt priority check - Change NRF_SD_BLE_API_VERSION to 6 - Add handle and buffer for advertising and scanning - Remove guard for phy update - Change scatter files and mbed_lib.json for PR ARMmbed#8607
1 parent 2fbc20f commit b69dc2b

File tree

13 files changed

+182
-78
lines changed

13 files changed

+182
-78
lines changed

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NORDIC_SOFTDEVICE/TARGET_NRF52/source/btle/btle.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ void btle_handler(const ble_evt_t *p_ble_evt)
334334

335335

336336
#if (NRF_SD_BLE_API_VERSION >= 5)
337-
#ifndef S140
338337
// Handle PHY upgrade request
339338
case BLE_GAP_EVT_PHY_UPDATE_REQUEST: {
340339
gap.on_phy_update_request(

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NORDIC_SOFTDEVICE/TARGET_NRF52/source/nRF5xGap.cpp

Lines changed: 162 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ nRF5xGap::nRF5xGap() :
171171
_connections_role()
172172
{
173173
m_connectionHandle = BLE_CONN_HANDLE_INVALID;
174+
#if (NRF_SD_BLE_API_VERSION >= 6)
175+
m_advHandle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
176+
#endif
174177
}
175178
/**************************************************************************/
176179
/*!
@@ -236,13 +239,37 @@ ble_error_t nRF5xGap::setAdvertisingData_(const GapAdvertisingData &advData, con
236239
// }
237240
//}
238241

242+
#if (NRF_SD_BLE_API_VERSION >= 6)
243+
/* sd_ble_gap_adv_data_set has been decprecated */
244+
if (m_advHandle != BLE_GAP_ADV_SET_HANDLE_NOT_SET) {
245+
/* This is for updating advdata*/
246+
ble_gap_adv_data_t adv_data = {0};
247+
adv_data.adv_data.p_data = const_cast<uint8_t*>(advData.getPayload());
248+
adv_data.adv_data.len = advData.getPayloadLen();
249+
adv_data.scan_rsp_data.p_data = const_cast<uint8_t*>(scanResponse.getPayload());
250+
adv_data.scan_rsp_data.len = scanResponse.getPayloadLen();
251+
252+
ASSERT_TRUE(ERROR_NONE ==
253+
sd_ble_gap_adv_stop(m_advHandle),
254+
BLE_ERROR_PARAM_OUT_OF_RANGE);
255+
256+
ASSERT_TRUE(ERROR_NONE ==
257+
sd_ble_gap_adv_set_configure(&m_advHandle, &adv_data, NULL),
258+
BLE_ERROR_PARAM_OUT_OF_RANGE);
259+
260+
ASSERT_TRUE(ERROR_NONE ==
261+
sd_ble_gap_adv_start(m_advHandle, NRF_CONNECTION_TAG),
262+
BLE_ERROR_PARAM_OUT_OF_RANGE);
263+
}
264+
#else
239265
/* Send advertising data! */
240266
ASSERT_TRUE(ERROR_NONE ==
241267
sd_ble_gap_adv_data_set(advData.getPayload(),
242268
advData.getPayloadLen(),
243269
scanResponse.getPayload(),
244270
scanResponse.getPayloadLen()),
245271
BLE_ERROR_PARAM_OUT_OF_RANGE);
272+
#endif
246273

247274
/* Make sure the GAP Service appearance value is aligned with the
248275
*appearance from GapAdvertisingData */
@@ -373,14 +400,56 @@ ble_error_t nRF5xGap::startAdvertising_(const GapAdvertisingParams &params)
373400

374401
/* Start Advertising */
375402

403+
#if (NRF_SD_BLE_API_VERSION >= 6)
376404

405+
/* FIXME: Must be chanaged if extended paramters added into GapAdvertisingParams */
406+
switch (params.getAdvertisingType()) {
407+
case GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED:
408+
adv_para.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_SCANNABLE_UNDIRECTED;
409+
break;
410+
411+
case GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED:
412+
adv_para.properties.type = BLE_GAP_ADV_TYPE_CONNECTABLE_NONSCANNABLE_DIRECTED;
413+
break;
414+
415+
case GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED:
416+
adv_para.properties.type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_SCANNABLE_UNDIRECTED;
417+
break;
418+
419+
case GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED:
420+
adv_para.properties.type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED;
421+
break;
422+
423+
default:
424+
return BLE_ERROR_PARAM_OUT_OF_RANGE;
425+
break;
426+
}
427+
428+
adv_para.interval = params.getIntervalInADVUnits(); // advertising interval (in units of 0.625 ms)
429+
adv_para.duration = params.getTimeout() * 100; // units have been changed from seconds to 10ms units.
430+
memset(adv_para.channel_mask, 0, sizeof(adv_para.channel_mask));
431+
adv_para.filter_policy = advertisingPolicyMode; // BLE_GAP_ADV_FP_ANY
432+
adv_para.primary_phy = BLE_GAP_PHY_1MBPS; /* Use _preferred_tx_phys if validated */
433+
adv_para.p_peer_addr = NULL;
434+
435+
m_adv_data.adv_data.p_data = const_cast<uint8_t*>(_advPayload.getPayload());
436+
m_adv_data.adv_data.len = _advPayload.getPayloadLen();
437+
m_adv_data.scan_rsp_data.p_data = const_cast<uint8_t*>(_scanResponse.getPayload());
438+
m_adv_data.scan_rsp_data.len = _scanResponse.getPayloadLen();
439+
#else
377440
adv_para.type = params.getAdvertisingType();
378441
adv_para.p_peer_addr = NULL; // Undirected advertisement
379442
adv_para.fp = advertisingPolicyMode;
380443
adv_para.interval = params.getIntervalInADVUnits(); // advertising interval (in units of 0.625 ms)
381444
adv_para.timeout = params.getTimeout();
445+
#endif
382446

383-
#if (NRF_SD_BLE_API_VERSION >= 5)
447+
448+
#if (NRF_SD_BLE_API_VERSION >= 6)
449+
if ((err = sd_ble_gap_adv_set_configure(&m_advHandle, &m_adv_data, &adv_para) == ERROR_NONE)) {
450+
err = sd_ble_gap_adv_start(m_advHandle, NRF_CONNECTION_TAG);
451+
}
452+
#elif (NRF_SD_BLE_API_VERSION == 5)
384453
err = sd_ble_gap_adv_start(&adv_para, NRF_CONNECTION_TAG);
385454
#else
386455
err = sd_ble_gap_adv_start(&adv_para);
@@ -427,15 +496,29 @@ ble_error_t nRF5xGap::startRadioScan_(const GapScanningParams &scanningParams)
427496
#else
428497
/* For NRF_SD_BLE_API_VERSION >= 3 nRF5xGap::setWhitelist setups the whitelist. */
429498

499+
#if (NRF_SD_BLE_API_VERSION >= 6)
500+
scanParams.filter_policy = scanningPolicyMode;
501+
#else
430502
scanParams.use_whitelist = scanningPolicyMode;
431503
scanParams.adv_dir_report = 0;
504+
#endif
432505
#endif
433506

507+
#if (NRF_SD_BLE_API_VERSION >= 6)
508+
scanParams.extended = 0;
509+
memset(scanParams.channel_mask, 0, sizeof(scanParams.channel_mask));
510+
scanParams.scan_phys = BLE_GAP_PHY_1MBPS; /* Use _preferred_rx_phys if validated */
511+
512+
scanParams.interval = scanningParams.getInterval(); /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
513+
scanParams.window = scanningParams.getWindow(); /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
514+
scanParams.timeout = scanningParams.getTimeout()*100; /**< Scan timeout between 0x0001 and 0xFFFF in 10 ms units, 0x0000 disables timeout. */
515+
#else
434516
scanParams.active = scanningParams.getActiveScanning(); /**< If 1, perform active scanning (scan requests). */
435517

436518
scanParams.interval = scanningParams.getInterval(); /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
437519
scanParams.window = scanningParams.getWindow(); /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */
438520
scanParams.timeout = scanningParams.getTimeout(); /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
521+
#endif
439522

440523
if (_privacy_enabled) {
441524
bool enable_resolution =
@@ -450,9 +533,20 @@ ble_error_t nRF5xGap::startRadioScan_(const GapScanningParams &scanningParams)
450533
}
451534
}
452535

536+
#if (NRF_SD_BLE_API_VERSION >= 6)
537+
m_scan_buffer.p_data = m_raw_scan_buffer;
538+
m_scan_buffer.len = sizeof(m_raw_scan_buffer);
539+
m_resume_scanning = true;
540+
//if (sd_ble_gap_scan_start(&scanParams, &m_scan_buffer) != NRF_SUCCESS) {
541+
uint32_t res = sd_ble_gap_scan_start(&scanParams, &m_scan_buffer);
542+
if (res != NRF_SUCCESS) {
543+
return BLE_ERROR_PARAM_OUT_OF_RANGE;
544+
}
545+
#else
453546
if (sd_ble_gap_scan_start(&scanParams) != NRF_SUCCESS) {
454547
return BLE_ERROR_PARAM_OUT_OF_RANGE;
455548
}
549+
#endif
456550

457551
return BLE_ERROR_NONE;
458552
}
@@ -485,7 +579,11 @@ ble_error_t nRF5xGap::stopScan_(void) {
485579
ble_error_t nRF5xGap::stopAdvertising_(void)
486580
{
487581
/* Stop Advertising */
582+
#if (NRF_SD_BLE_API_VERSION >= 6)
583+
ASSERT_TRUE(ERROR_NONE == sd_ble_gap_adv_stop(m_advHandle), BLE_ERROR_PARAM_OUT_OF_RANGE);
584+
#else
488585
ASSERT_TRUE(ERROR_NONE == sd_ble_gap_adv_stop(), BLE_ERROR_PARAM_OUT_OF_RANGE);
586+
#endif
489587

490588
state.advertising = 0;
491589

@@ -631,8 +729,11 @@ ble_error_t nRF5xGap::connect(
631729
}
632730
#else
633731
/* For NRF_SD_BLE_API_VERSION >= 3 nRF5xGap::setWhitelist setups the whitelist. */
634-
732+
#if (NRF_SD_BLE_API_VERSION >= 6)
733+
scanParams.filter_policy |= (whitelistAddressesSize) ? 1 : 0;
734+
#else
635735
scanParams.use_whitelist = (whitelistAddressesSize) ? 1 : 0;
736+
#endif
636737

637738
if (_privacy_enabled) {
638739
bool enable_resolution =
@@ -661,6 +762,10 @@ ble_error_t nRF5xGap::connect(
661762
scanParams.timeout = _scanningParams.getTimeout(); /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */
662763
}
663764

765+
#if NRF_SD_BLE_API_VERSION >= 6
766+
m_resume_scanning = false;
767+
#endif
768+
664769
#if NRF_SD_BLE_API_VERSION >= 5
665770
uint32_t rc = sd_ble_gap_connect(addr_ptr, &scanParams, &connParams, NRF_CONNECTION_TAG);
666771
#else
@@ -706,6 +811,9 @@ ble_error_t nRF5xGap::setPreferredPhys_(
706811
uint8_t preferred_rx_phys = rxPhys? rxPhys->value() : 0;
707812

708813
#ifdef S140
814+
#if (NRF_SD_BLE_API_VERSION) >= 6
815+
/* Set _preferred_tx_phys and _preferred_rx_phys here, used when start advertising or scanning */
816+
#else
709817
ble_opt_t opt = { 0 };
710818
opt.gap_opt.preferred_phys.tx_phys = preferred_tx_phys;
711819
opt.gap_opt.preferred_phys.rx_phys = preferred_rx_phys;
@@ -726,7 +834,7 @@ ble_error_t nRF5xGap::setPreferredPhys_(
726834
default:
727835
return BLE_ERROR_UNSPECIFIED;
728836
}
729-
837+
#endif
730838
#endif
731839

732840
_preferred_tx_phys = preferred_tx_phys;
@@ -741,7 +849,7 @@ ble_error_t nRF5xGap::setPhy_(
741849
const ble::phy_set_t* rxPhys,
742850
CodedSymbolPerBit_t codedSymbol
743851
) {
744-
#ifdef S140
852+
#if defined(S140) && ((NRF_SD_BLE_API_VERSION) < 6)
745853
return BLE_ERROR_NOT_IMPLEMENTED;
746854
#else
747855
// TODO handle coded symbol once supported by the softdevice.
@@ -851,6 +959,9 @@ ble_error_t nRF5xGap::reset_(void)
851959

852960
/* Clear derived class members */
853961
m_connectionHandle = BLE_CONN_HANDLE_INVALID;
962+
#if (NRF_SD_BLE_API_VERSION >= 6)
963+
m_advHandle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
964+
#endif
854965

855966
/* Set the whitelist policy filter modes to IGNORE_WHITELIST */
856967
advertisingPolicyMode = ADV_POLICY_IGNORE_WHITELIST;
@@ -1019,7 +1130,25 @@ ble_error_t nRF5xGap::getAppearance_(GapAdvertisingData::Appearance *appearanceP
10191130
ble_error_t nRF5xGap::setTxPower_(int8_t txPower)
10201131
{
10211132
unsigned rc;
1133+
#if (NRF_SD_BLE_API_VERSION >= 6)
1134+
/* FIXME: This has to change API for specified paramter */
1135+
uint16_t handle = 0;
1136+
rc = NRF_SUCCESS;
1137+
if ((handle = getConnectionHandle()) != BLE_CONN_HANDLE_INVALID) {
1138+
rc = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_CONN, handle, txPower);
1139+
}
1140+
if ((rc == NRF_SUCCESS) && (m_advHandle != BLE_GAP_ADV_SET_HANDLE_NOT_SET)) {
1141+
handle = (uint16_t)m_advHandle;
1142+
rc = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, handle, txPower);
1143+
}
1144+
if (rc == NRF_SUCCESS) {
1145+
rc = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_SCAN_INIT, 0 /* This is ingored for ROLE_SCAN_INIT*/, txPower);
1146+
}
1147+
1148+
if (rc != NRF_SUCCESS) {
1149+
#else
10221150
if ((rc = sd_ble_gap_tx_power_set(txPower)) != NRF_SUCCESS) {
1151+
#endif
10231152
switch (rc) {
10241153
case NRF_ERROR_BUSY:
10251154
return BLE_STACK_BUSY;
@@ -1564,6 +1693,34 @@ void nRF5xGap::on_advertising_packet(const ble_gap_evt_adv_report_t &evt) {
15641693
);
15651694
const uint8_t* peer_address = evt.peer_addr.addr;
15661695

1696+
#if (NRF_SD_BLE_API_VERSION >= 6)
1697+
GapAdvertisingParams::AdvertisingType_t type;
1698+
if (evt.type.connectable && !evt.type.directed) {
1699+
type = GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED;
1700+
} else if (evt.type.connectable && evt.type.directed) {
1701+
type = GapAdvertisingParams::ADV_CONNECTABLE_DIRECTED;
1702+
} else if (evt.type.scannable && !evt.type.directed) {
1703+
type = GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED;
1704+
} else if (!evt.type.connectable && !evt.type.directed) {
1705+
type = GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED;
1706+
} else {
1707+
// wrong condition
1708+
}
1709+
processAdvertisementReport(
1710+
peer_address,
1711+
evt.rssi,
1712+
(evt.type.scan_response? 1 : 0),
1713+
type,
1714+
evt.data.len,
1715+
evt.data.p_data,
1716+
peer_addr_type
1717+
);
1718+
1719+
/* If no action for connecting, must call sd_ble_gap_scan_start() aging for resuming scanning */
1720+
if (m_resume_scanning) {
1721+
sd_ble_gap_scan_start(NULL, &m_scan_buffer);
1722+
}
1723+
#else
15671724
processAdvertisementReport(
15681725
peer_address,
15691726
evt.rssi,
@@ -1573,6 +1730,7 @@ void nRF5xGap::on_advertising_packet(const ble_gap_evt_adv_report_t &evt) {
15731730
evt.data,
15741731
peer_addr_type
15751732
);
1733+
#endif
15761734
}
15771735

15781736
ble_error_t nRF5xGap::get_role(ble::connection_handle_t connection, Role_t& role) {
@@ -1656,7 +1814,6 @@ void nRF5xGap::on_phy_update(
16561814
);
16571815
}
16581816

1659-
#ifndef S140
16601817
void nRF5xGap::on_phy_update_request(
16611818
Handle_t connection,
16621819
const ble_gap_evt_phy_update_request_t& evt
@@ -1676,7 +1833,6 @@ void nRF5xGap::on_phy_update_request(
16761833

16771834
sd_ble_gap_phy_update(connection, &phys);
16781835
}
1679-
#endif
16801836

16811837

16821838

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NORDIC_SOFTDEVICE/TARGET_NRF52/source/nRF5xGap.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,18 @@ class nRF5xGap :
313313
void release_all_connections_role();
314314

315315
void on_phy_update(Handle_t connection, const ble_gap_evt_phy_update_t& evt);
316-
// FIXME: remove guard when S140 updated
317-
#ifndef S140
318316
void on_phy_update_request(Handle_t connection, const ble_gap_evt_phy_update_request_t& evt);
317+
318+
#if (NRF_SD_BLE_API_VERSION) >= 6
319+
uint8_t m_advHandle;
320+
/*
321+
Note: The advertising data must be kept alive in memory until advertising is terminated. Not doing so will lead to undefined behavior.
322+
Note: Updating advertising data while advertising can only be done by providing new advertising data buffers.
323+
*/
324+
ble_gap_adv_data_t m_adv_data;
325+
uint8_t m_raw_scan_buffer[BLE_GAP_SCAN_BUFFER_MIN];
326+
ble_data_t m_scan_buffer;
327+
bool m_resume_scanning; /* When the application receives a ble_gap_adv_report_t, it must now resume scanning by calling sd_ble_gap_scan_start() */
319328
#endif
320329

321330
uint16_t m_connectionHandle;

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52832/device/TOOLCHAIN_ARM_STD/nRF52832.sct

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,6 @@
1111

1212
#define Stack_Size MBED_BOOT_STACK_SIZE
1313

14-
/* If softdevice is present, set aside space for it */
15-
#if !defined(MBED_RAM_START)
16-
#if defined(SOFTDEVICE_PRESENT)
17-
#define MBED_RAM_START 0x20001D70
18-
#define MBED_RAM_SIZE 0xE290
19-
#else
20-
#define MBED_RAM_START 0x20000000
21-
#define MBED_RAM_SIZE 0x10000
22-
#endif
23-
#endif
24-
2514
#define MBED_RAM0_START MBED_RAM_START
2615
#define MBED_RAM0_SIZE 0xE0
2716
#define MBED_RAM1_START (MBED_RAM_START + MBED_RAM0_SIZE)

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52832/device/TOOLCHAIN_GCC_ARM/NRF52832.ld

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,6 @@
2525
#define MBED_APP_SIZE 0x80000
2626
#endif
2727

28-
/* If softdevice is present, set aside space for it */
29-
#if !defined(MBED_RAM_START)
30-
#if defined(SOFTDEVICE_PRESENT)
31-
#define MBED_RAM_START 0x20001D70
32-
#define MBED_RAM_SIZE 0xE290
33-
#else
34-
#define MBED_RAM_START 0x20000000
35-
#define MBED_RAM_SIZE 0x10000
36-
#endif
37-
#endif
38-
3928
#if !defined(MBED_BOOT_STACK_SIZE)
4029
#define MBED_BOOT_STACK_SIZE 0x800
4130
#endif

targets/TARGET_NORDIC/TARGET_NRF5x/TARGET_NRF52/TARGET_MCU_NRF52832/device/TOOLCHAIN_IAR/nRF52832.icf

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,6 @@ if (!isdefinedsymbol(MBED_APP_SIZE)) {
1111
define symbol MBED_APP_SIZE = 0x80000;
1212
}
1313

14-
/* If softdevice is present, set aside space for it */
15-
if (!isdefinedsymbol(MBED_RAM_START)) {
16-
if (isdefinedsymbol(SOFTDEVICE_PRESENT)) {
17-
define symbol MBED_RAM_START = 0x200031D0;
18-
define symbol MBED_RAM_SIZE = 0xCE30;
19-
} else {
20-
define symbol MBED_RAM_START = 0x20000000;
21-
define symbol MBED_RAM_SIZE = 0x10000;
22-
}
23-
}
24-
2514
if (!isdefinedsymbol(MBED_BOOT_STACK_SIZE)) {
2615
define symbol MBED_BOOT_STACK_SIZE = 0x400;
2716
}

0 commit comments

Comments
 (0)