Skip to content

Commit 3ac15c4

Browse files
committed
BLE config refactor
- enhance the SRAM usage based on prph & central mode with respect to user config. - change Bluefruit.begin(bool, bool) to (uint8_t, uint8_t) as number of prph & central connections.
1 parent a3ff2e8 commit 3ac15c4

File tree

4 files changed

+73
-29
lines changed

4 files changed

+73
-29
lines changed

libraries/Bluefruit52Lib/examples/Central/central_scan/central_scan.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ void setup()
2121
Serial.println("Bluefruit52 Central Scan Example");
2222
Serial.println("--------------------------------\n");
2323

24-
// Enable both peripheral and central
25-
Bluefruit.begin(true, true);
24+
// Init Bluefruit with Number of Peripheral = 0, Central = 1
25+
Bluefruit.begin(0, 1);
26+
2627
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
2728
Bluefruit.setTxPower(4);
2829
Bluefruit.setName("Bluefruit52");

libraries/Bluefruit52Lib/examples/Central/central_scan_advanced/central_scan_advanced.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void setup()
2626
Serial.println("------------------------------------\n");
2727

2828
/* Enable both peripheral and central modes */
29-
Bluefruit.begin(true, true);
29+
Bluefruit.begin(0, 1);
3030
// Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
3131
Bluefruit.setTxPower(4);
3232

libraries/Bluefruit52Lib/src/bluefruit.cpp

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,24 +97,24 @@ AdafruitBluefruit::AdafruitBluefruit(void)
9797
/*------------------------------------------------------------------*/
9898
/* SoftDevice Default Configuration
9999
* Most config use Nordic default value, except the follows:
100-
* - ATTR Table Size : set to 0xB00 instead of BLE_GATTS_ATTR_TABLE_SIZE (0x580)
101-
* - HVN TX Queue Size: set to 3 instead of BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT (1)
102-
* to increase throughput for most user
100+
* - Max MTU : up to 247 for maximum throughput
101+
*
102+
* Attr Table Size, HVN queue size, Write Command queue size is
103+
* determined later in begin() depending on number of peripherals
104+
* and central connections for optimum SRAM usage.
103105
*/
104106
/*------------------------------------------------------------------*/
105-
_sd_cfg.attr_table_size = 0x800;
106-
_sd_cfg.mtu_max = BLEGATT_ATT_MTU_MAX;
107-
_sd_cfg.service_changed = 0;
107+
varclr(&_sd_cfg);
108108
_sd_cfg.uuid128_max = BLE_UUID_VS_COUNT_DEFAULT;
109+
_sd_cfg.service_changed = 0;
110+
_sd_cfg.mtu_max = BLEGATT_ATT_MTU_MAX;
109111

110112
#if SD_VER >= 500
111113
_sd_cfg.event_len = BLE_GAP_EVENT_LENGTH_DEFAULT;
112-
_sd_cfg.hvn_tx_qsize = 3;
113-
_sd_cfg.wr_cmd_qsize = BLE_GATTC_WRITE_CMD_TX_QUEUE_SIZE_DEFAULT;
114114
#endif
115115

116-
_prph_enabled = true;
117-
_central_enabled = false;
116+
_prph_count = 0;
117+
_central_count = 0;
118118

119119
_ble_event_sem = NULL;
120120
_soc_event_sem = NULL;
@@ -211,10 +211,10 @@ uint8_t AdafruitBluefruit::getWriteCmdQueue(void)
211211
}
212212

213213

214-
err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable)
214+
err_t AdafruitBluefruit::begin(uint8_t prph_count, uint8_t central_count)
215215
{
216-
_prph_enabled = prph_enable;
217-
_central_enabled = central_enable;
216+
_prph_count = prph_count;
217+
_central_count = central_count;
218218

219219
// Configure Clock
220220
#if defined( USE_LFXO )
@@ -234,6 +234,48 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable)
234234

235235
VERIFY_STATUS( sd_softdevice_enable(&clock_cfg, nrf_error_cb) );
236236

237+
/*------------------------------------------------------------------*/
238+
/* SoftDevice Default Configuration depending on the number of
239+
* prph and central connections for optimal SRAM usage.
240+
*
241+
* - If Peripheral mode is enabled
242+
* - ATTR Table Size = 0x800.
243+
* - HVN TX Queue Size = 3
244+
*
245+
* - If Central mode is enabled
246+
* - Write Command Queue Size = 3
247+
*
248+
* Otherwise value will have default as follows:
249+
* - ATTR Table Size = BLE_GATTS_ATTR_TAB_SIZE_DEFAULT (0x580)
250+
* - HVN TX Queue Size = 1
251+
* - Write Command Queue Size = 1
252+
*
253+
* Note: Value is left as it is if already configured by user.
254+
*/
255+
/*------------------------------------------------------------------*/
256+
if ( _prph_count )
257+
{
258+
// If not configured by user, set Attr Table Size large enough for
259+
// most peripheral applications
260+
if ( _sd_cfg.attr_table_size == 0 ) _sd_cfg.attr_table_size = 0x800;
261+
262+
// Increase HVN queue size with prph connections if not configured
263+
if ( _sd_cfg.hvn_tx_qsize == 0 ) _sd_cfg.hvn_tx_qsize = 3;
264+
}
265+
266+
if ( _central_count)
267+
{
268+
// Increase Write Command queue size with central connections if not configured
269+
if ( _sd_cfg.wr_cmd_qsize == 0) _sd_cfg.wr_cmd_qsize = 3;
270+
}
271+
272+
// Not configure, default value are used
273+
if ( _sd_cfg.attr_table_size == 0 ) _sd_cfg.attr_table_size = BLE_GATTS_ATTR_TAB_SIZE_DEFAULT;
274+
#if SD_VER >= 500
275+
if ( _sd_cfg.hvn_tx_qsize == 0 ) _sd_cfg.hvn_tx_qsize = BLE_GATTS_HVN_TX_QUEUE_SIZE_DEFAULT;
276+
if ( _sd_cfg.wr_cmd_qsize == 0 ) _sd_cfg.wr_cmd_qsize = BLE_GATTC_WRITE_CMD_TX_QUEUE_SIZE_DEFAULT;
277+
#endif
278+
237279
/*------------- Configure BLE params -------------*/
238280
extern uint32_t __data_start__[]; // defined in linker
239281
uint32_t ram_start = (uint32_t) __data_start__;
@@ -244,9 +286,9 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable)
244286
{
245287
.common_enable_params = { .vs_uuid_count = _sd_cfg.uuid128_max },
246288
.gap_enable_params = {
247-
.periph_conn_count = (uint8_t) (_prph_enabled ? 1 : 0),
248-
.central_conn_count = (uint8_t) (_central_enabled ? BLE_CENTRAL_MAX_CONN : 0),
249-
.central_sec_count = (uint8_t) (_central_enabled ? BLE_CENTRAL_MAX_SECURE_CONN : 0),
289+
.periph_conn_count = (uint8_t) (_prph_count ? 1 : 0),
290+
.central_conn_count = (uint8_t) (_central_count ? BLE_CENTRAL_MAX_CONN : 0),
291+
.central_sec_count = (uint8_t) (_central_count ? BLE_CENTRAL_MAX_SECURE_CONN : 0),
250292
},
251293
.gatts_enable_params = {
252294
.service_changed = 1,
@@ -257,6 +299,7 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable)
257299
// Enable BLE stack
258300
VERIFY_STATUS( sd_ble_enable(&params, &ram_start) );
259301
#else
302+
260303
ble_cfg_t blecfg;
261304

262305
// Vendor UUID count
@@ -266,9 +309,9 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable)
266309

267310
// Roles
268311
varclr(&blecfg);
269-
blecfg.gap_cfg.role_count_cfg.periph_role_count = (_prph_enabled ? 1 : 0);
270-
blecfg.gap_cfg.role_count_cfg.central_role_count = (_central_enabled ? BLE_CENTRAL_MAX_CONN : 0);
271-
blecfg.gap_cfg.role_count_cfg.central_sec_count = (_central_enabled ? BLE_CENTRAL_MAX_SECURE_CONN : 0);
312+
blecfg.gap_cfg.role_count_cfg.periph_role_count = _prph_count;
313+
blecfg.gap_cfg.role_count_cfg.central_role_count = _central_count; // ? BLE_CENTRAL_MAX_CONN : 0);
314+
blecfg.gap_cfg.role_count_cfg.central_sec_count = (_central_count ? 1 : 0); // should be enough
272315
VERIFY_STATUS( sd_ble_cfg_set(BLE_GAP_CFG_ROLE_COUNT, &blecfg, ram_start) );
273316

274317
// Device Name
@@ -357,7 +400,7 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable)
357400
/*------------- DFU OTA as built-in service -------------*/
358401
_dfu_svc.begin();
359402

360-
if (_central_enabled) Central.begin(); // Init Central
403+
if (_central_count) Central.begin(); // Init Central
361404

362405
// Create RTOS Semaphore & Task for BLE Event
363406
_ble_event_sem = xSemaphoreCreateBinary();
@@ -610,8 +653,8 @@ void AdafruitBluefruit::printInfo(void)
610653

611654
// Max Connections
612655
Serial.printf(title_fmt, "Max Connections");
613-
Serial.printf("Peripheral = %d, ", _prph_enabled ? 1 : 0);
614-
Serial.printf("Central = %d ", _central_enabled ? BLE_CENTRAL_MAX_CONN : 0);
656+
Serial.printf("Peripheral = %d, ", _prph_count ? 1 : 0);
657+
Serial.printf("Central = %d ", _central_count ? BLE_CENTRAL_MAX_CONN : 0);
615658
Serial.println();
616659

617660
// Address
@@ -949,7 +992,7 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
949992
}
950993

951994
// Central Event Handler
952-
if (_central_enabled)
995+
if (_central_count)
953996
{
954997
// Skip if not central connection
955998
if (evt_conn_hdl != _conn_hdl ||

libraries/Bluefruit52Lib/src/bluefruit.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class AdafruitBluefruit
134134
void configHvnTxQueue (uint8_t hvn_tx_qsize);
135135
void configWriteCmdQueue (uint8_t wr_cmd_qsize);
136136

137-
err_t begin(bool prph_enable = true, bool central_enable = false);
137+
err_t begin(uint8_t prph_count = 1, uint8_t central_count = 0);
138138

139139
/*------------- Config Retrieval -------------*/
140140
uint16_t getMaxMtu(void);
@@ -212,8 +212,8 @@ class AdafruitBluefruit
212212
uint8_t wr_cmd_qsize;
213213
}_sd_cfg;
214214

215-
bool _prph_enabled;
216-
bool _central_enabled;
215+
bool _prph_count;
216+
bool _central_count;
217217

218218
// Peripheral Preferred Connection Parameters (PPCP)
219219
uint16_t _ppcp_min_conn;

0 commit comments

Comments
 (0)