Skip to content

Commit 4abd5f1

Browse files
committed
increase max connections from 2 to 5
1 parent 89829ae commit 4abd5f1

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

ports/nrf/bluetooth/ble_drv.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333

3434
#include "ble.h"
3535

36-
#define MAX_TX_IN_PROGRESS 10
37-
3836
#ifndef BLE_GATT_ATT_MTU_DEFAULT
3937
#define BLE_GATT_ATT_MTU_DEFAULT GATT_MTU_SIZE_DEFAULT
4038
#endif

ports/nrf/boards/common.template.ld

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ MEMORY
1919

2020

2121
/* 0x2000000 - RAM:ORIGIN is reserved for Softdevice */
22-
/* SoftDevice 6.1.0 takes 0x7b78 bytes (30.86 kb) minimum with high ATT MTU. */
22+
/* SoftDevice 6.1.0 with 5 connections and various increases takes just under 64kiB.
2323
/* To measure the minimum required amount of memory for given configuration, set this number
2424
high enough to work and then check the mutation of the value done by sd_ble_enable. */
25-
RAM (xrw) : ORIGIN = 0x20000000 + 32K, LENGTH = 256K - 32K
25+
RAM (xrw) : ORIGIN = 0x20000000 + 64K, LENGTH = 256K - 64K
2626
}
2727

28-
/* produce a link error if there is not this amount of RAM for these sections */
29-
_minimum_stack_size = 40K;
28+
/* produce a link error if there is not this amount of RAM available */
3029
_minimum_heap_size = 0;
3130

3231
/* top end of the stack */
@@ -125,7 +124,7 @@ SECTIONS
125124
.stack :
126125
{
127126
. = ALIGN(4);
128-
. = . + _minimum_stack_size;
127+
. = . + ${CIRCUITPY_DEFAULT_STACK_SIZE};
129128
. = ALIGN(4);
130129
} >RAM
131130

ports/nrf/common-hal/_bleio/Adapter.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ STATIC uint32_t ble_stack_enable(void) {
108108

109109
ble_cfg_t ble_conf;
110110
ble_conf.conn_cfg.conn_cfg_tag = BLE_CONN_CFG_TAG_CUSTOM;
111+
// Each additional connection costs:
112+
// about 3700-4300 bytes when .hvn_tx_queue_size is 1
113+
// about 9000 bytes when .hvn_tx_queue_size is 10
111114
ble_conf.conn_cfg.params.gap_conn_cfg.conn_count = BLEIO_TOTAL_CONNECTION_COUNT;
112115
// Event length here can influence throughput so perhaps make multiple connection profiles
113116
// available.
@@ -118,17 +121,23 @@ STATIC uint32_t ble_stack_enable(void) {
118121
}
119122

120123
memset(&ble_conf, 0, sizeof(ble_conf));
124+
// adv_set_count must be == 1 for S140. Cannot be increased.
121125
ble_conf.gap_cfg.role_count_cfg.adv_set_count = 1;
122-
ble_conf.gap_cfg.role_count_cfg.periph_role_count = 2;
123-
ble_conf.gap_cfg.role_count_cfg.central_role_count = 1;
126+
// periph_role_count costs 1232 bytes for 2 to 3, then ~1840 for each further increment.
127+
ble_conf.gap_cfg.role_count_cfg.periph_role_count = 4;
128+
// central_role_count costs 648 bytes for 1 to 2, then ~1250 for each further increment.
129+
ble_conf.gap_cfg.role_count_cfg.central_role_count = 4;
124130
err_code = sd_ble_cfg_set(BLE_GAP_CFG_ROLE_COUNT, &ble_conf, app_ram_start);
125131
if (err_code != NRF_SUCCESS) {
126132
return err_code;
127133
}
128134

129135
memset(&ble_conf, 0, sizeof(ble_conf));
130136
ble_conf.conn_cfg.conn_cfg_tag = BLE_CONN_CFG_TAG_CUSTOM;
131-
ble_conf.conn_cfg.params.gatts_conn_cfg.hvn_tx_queue_size = MAX_TX_IN_PROGRESS;
137+
// Each increment to hvn_tx_queue_size costs 2064 bytes.
138+
// DevZone recommends not setting this directly, but instead changing gap_conn_cfg.event_length.
139+
// However, we are setting connection extension, so this seems to make sense.
140+
ble_conf.conn_cfg.params.gatts_conn_cfg.hvn_tx_queue_size = 9;
132141
err_code = sd_ble_cfg_set(BLE_CONN_CFG_GATTS, &ble_conf, app_ram_start);
133142
if (err_code != NRF_SUCCESS) {
134143
return err_code;
@@ -143,10 +152,11 @@ STATIC uint32_t ble_stack_enable(void) {
143152
return err_code;
144153
}
145154

146-
// Triple the GATT Server attribute size to accomodate both the CircuitPython built-in service
155+
// Increase the GATT Server attribute size to accomodate both the CircuitPython built-in service
147156
// and anything the user does.
148157
memset(&ble_conf, 0, sizeof(ble_conf));
149-
ble_conf.gatts_cfg.attr_tab_size.attr_tab_size = BLE_GATTS_ATTR_TAB_SIZE_DEFAULT * 3;
158+
// Each increment to the BLE_GATTS_ATTR_TAB_SIZE_DEFAULT multiplier costs 1408 bytes.
159+
ble_conf.gatts_cfg.attr_tab_size.attr_tab_size = BLE_GATTS_ATTR_TAB_SIZE_DEFAULT * 5;
150160
err_code = sd_ble_cfg_set(BLE_GATTS_CFG_ATTR_TAB_SIZE, &ble_conf, app_ram_start);
151161
if (err_code != NRF_SUCCESS) {
152162
return err_code;
@@ -155,7 +165,8 @@ STATIC uint32_t ble_stack_enable(void) {
155165
// Increase the number of vendor UUIDs supported. Apple uses a complete random number per
156166
// service and characteristic.
157167
memset(&ble_conf, 0, sizeof(ble_conf));
158-
ble_conf.common_cfg.vs_uuid_cfg.vs_uuid_count = 32; // Defaults to 10.
168+
// Each additional vs_uuid_count costs 16 bytes.
169+
ble_conf.common_cfg.vs_uuid_cfg.vs_uuid_count = 75; // Defaults to 10.
159170
err_code = sd_ble_cfg_set(BLE_COMMON_CFG_VS_UUID, &ble_conf, app_ram_start);
160171
if (err_code != NRF_SUCCESS) {
161172
return err_code;

ports/nrf/common-hal/_bleio/Adapter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include "shared-bindings/_bleio/Connection.h"
3636
#include "shared-bindings/_bleio/ScanResults.h"
3737

38-
#define BLEIO_TOTAL_CONNECTION_COUNT 2
38+
#define BLEIO_TOTAL_CONNECTION_COUNT 5
3939

4040
extern bleio_connection_internal_t bleio_connections[BLEIO_TOTAL_CONNECTION_COUNT];
4141

0 commit comments

Comments
 (0)