@@ -97,24 +97,24 @@ AdafruitBluefruit::AdafruitBluefruit(void)
97
97
/* ------------------------------------------------------------------*/
98
98
/* SoftDevice Default Configuration
99
99
* 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.
103
105
*/
104
106
/* ------------------------------------------------------------------*/
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);
108
108
_sd_cfg.uuid128_max = BLE_UUID_VS_COUNT_DEFAULT;
109
+ _sd_cfg.service_changed = 0 ;
110
+ _sd_cfg.mtu_max = BLEGATT_ATT_MTU_MAX;
109
111
110
112
#if SD_VER >= 500
111
113
_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;
114
114
#endif
115
115
116
- _prph_enabled = true ;
117
- _central_enabled = false ;
116
+ _prph_count = 0 ;
117
+ _central_count = 0 ;
118
118
119
119
_ble_event_sem = NULL ;
120
120
_soc_event_sem = NULL ;
@@ -211,10 +211,10 @@ uint8_t AdafruitBluefruit::getWriteCmdQueue(void)
211
211
}
212
212
213
213
214
- err_t AdafruitBluefruit::begin (bool prph_enable, bool central_enable )
214
+ err_t AdafruitBluefruit::begin (uint8_t prph_count, uint8_t central_count )
215
215
{
216
- _prph_enabled = prph_enable ;
217
- _central_enabled = central_enable ;
216
+ _prph_count = prph_count ;
217
+ _central_count = central_count ;
218
218
219
219
// Configure Clock
220
220
#if defined( USE_LFXO )
@@ -234,6 +234,48 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable)
234
234
235
235
VERIFY_STATUS ( sd_softdevice_enable (&clock_cfg, nrf_error_cb) );
236
236
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
+
237
279
/* ------------- Configure BLE params -------------*/
238
280
extern uint32_t __data_start__[]; // defined in linker
239
281
uint32_t ram_start = (uint32_t ) __data_start__;
@@ -244,9 +286,9 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable)
244
286
{
245
287
.common_enable_params = { .vs_uuid_count = _sd_cfg.uuid128_max },
246
288
.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 ),
250
292
},
251
293
.gatts_enable_params = {
252
294
.service_changed = 1 ,
@@ -257,6 +299,7 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable)
257
299
// Enable BLE stack
258
300
VERIFY_STATUS ( sd_ble_enable (¶ms, &ram_start) );
259
301
#else
302
+
260
303
ble_cfg_t blecfg;
261
304
262
305
// Vendor UUID count
@@ -266,9 +309,9 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable)
266
309
267
310
// Roles
268
311
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
272
315
VERIFY_STATUS ( sd_ble_cfg_set (BLE_GAP_CFG_ROLE_COUNT, &blecfg, ram_start) );
273
316
274
317
// Device Name
@@ -357,7 +400,7 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable)
357
400
/* ------------- DFU OTA as built-in service -------------*/
358
401
_dfu_svc.begin ();
359
402
360
- if (_central_enabled ) Central.begin (); // Init Central
403
+ if (_central_count ) Central.begin (); // Init Central
361
404
362
405
// Create RTOS Semaphore & Task for BLE Event
363
406
_ble_event_sem = xSemaphoreCreateBinary ();
@@ -610,8 +653,8 @@ void AdafruitBluefruit::printInfo(void)
610
653
611
654
// Max Connections
612
655
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 );
615
658
Serial.println ();
616
659
617
660
// Address
@@ -949,7 +992,7 @@ void AdafruitBluefruit::_ble_handler(ble_evt_t* evt)
949
992
}
950
993
951
994
// Central Event Handler
952
- if (_central_enabled )
995
+ if (_central_count )
953
996
{
954
997
// Skip if not central connection
955
998
if (evt_conn_hdl != _conn_hdl ||
0 commit comments