6161#endif
6262
6363#ifndef BLEIO_HVN_TX_QUEUE_SIZE
64- #define BLEIO_HVN_TX_QUEUE_SIZE 9
64+ #define BLEIO_HVN_TX_QUEUE_SIZE 5
6565#endif
6666
6767#ifndef BLEIO_CENTRAL_ROLE_COUNT
@@ -120,11 +120,11 @@ STATIC uint32_t ble_stack_enable(void) {
120120 // Start with no event handlers, etc.
121121 ble_drv_reset ();
122122
123- // Set everything up to have one persistent code editing connection and one user managed
124- // connection. In the future we could move .data and .bss to the other side of the stack and
123+ // In the future we might move .data and .bss to the other side of the stack and
125124 // dynamically adjust for different memory requirements of the SD based on boot.py
126- // configuration.
127- uint32_t app_ram_start = (uint32_t ) & _ram_start ;
125+ // configuration. But we still need to keep the SPIM3 buffer (if needed) in the first 64kB of RAM.
126+
127+ uint32_t sd_ram_end = SOFTDEVICE_RAM_START_ADDR + SOFTDEVICE_RAM_SIZE ;
128128
129129 ble_cfg_t ble_conf ;
130130 ble_conf .conn_cfg .conn_cfg_tag = BLE_CONN_CFG_TAG_CUSTOM ;
@@ -135,7 +135,7 @@ STATIC uint32_t ble_stack_enable(void) {
135135 // Event length here can influence throughput so perhaps make multiple connection profiles
136136 // available.
137137 ble_conf .conn_cfg .params .gap_conn_cfg .event_length = BLE_GAP_EVENT_LENGTH_DEFAULT ;
138- err_code = sd_ble_cfg_set (BLE_CONN_CFG_GAP , & ble_conf , app_ram_start );
138+ err_code = sd_ble_cfg_set (BLE_CONN_CFG_GAP , & ble_conf , sd_ram_end );
139139 if (err_code != NRF_SUCCESS ) {
140140 return err_code ;
141141 }
@@ -147,7 +147,7 @@ STATIC uint32_t ble_stack_enable(void) {
147147 ble_conf .gap_cfg .role_count_cfg .periph_role_count = BLEIO_PERIPH_ROLE_COUNT ;
148148 // central_role_count costs 648 bytes for 1 to 2, then ~1250 for each further increment.
149149 ble_conf .gap_cfg .role_count_cfg .central_role_count = BLEIO_CENTRAL_ROLE_COUNT ;
150- err_code = sd_ble_cfg_set (BLE_GAP_CFG_ROLE_COUNT , & ble_conf , app_ram_start );
150+ err_code = sd_ble_cfg_set (BLE_GAP_CFG_ROLE_COUNT , & ble_conf , sd_ram_end );
151151 if (err_code != NRF_SUCCESS ) {
152152 return err_code ;
153153 }
@@ -158,7 +158,7 @@ STATIC uint32_t ble_stack_enable(void) {
158158 // DevZone recommends not setting this directly, but instead changing gap_conn_cfg.event_length.
159159 // However, we are setting connection extension, so this seems to make sense.
160160 ble_conf .conn_cfg .params .gatts_conn_cfg .hvn_tx_queue_size = BLEIO_HVN_TX_QUEUE_SIZE ;
161- err_code = sd_ble_cfg_set (BLE_CONN_CFG_GATTS , & ble_conf , app_ram_start );
161+ err_code = sd_ble_cfg_set (BLE_CONN_CFG_GATTS , & ble_conf , sd_ram_end );
162162 if (err_code != NRF_SUCCESS ) {
163163 return err_code ;
164164 }
@@ -167,7 +167,7 @@ STATIC uint32_t ble_stack_enable(void) {
167167 memset (& ble_conf , 0 , sizeof (ble_conf ));
168168 ble_conf .conn_cfg .conn_cfg_tag = BLE_CONN_CFG_TAG_CUSTOM ;
169169 ble_conf .conn_cfg .params .gatt_conn_cfg .att_mtu = BLE_GATTS_VAR_ATTR_LEN_MAX ;
170- err_code = sd_ble_cfg_set (BLE_CONN_CFG_GATT , & ble_conf , app_ram_start );
170+ err_code = sd_ble_cfg_set (BLE_CONN_CFG_GATT , & ble_conf , sd_ram_end );
171171 if (err_code != NRF_SUCCESS ) {
172172 return err_code ;
173173 }
@@ -177,7 +177,7 @@ STATIC uint32_t ble_stack_enable(void) {
177177 memset (& ble_conf , 0 , sizeof (ble_conf ));
178178 // Each increment to the BLE_GATTS_ATTR_TAB_SIZE_DEFAULT multiplier costs 1408 bytes.
179179 ble_conf .gatts_cfg .attr_tab_size .attr_tab_size = BLEIO_ATTR_TAB_SIZE ;
180- err_code = sd_ble_cfg_set (BLE_GATTS_CFG_ATTR_TAB_SIZE , & ble_conf , app_ram_start );
180+ err_code = sd_ble_cfg_set (BLE_GATTS_CFG_ATTR_TAB_SIZE , & ble_conf , sd_ram_end );
181181 if (err_code != NRF_SUCCESS ) {
182182 return err_code ;
183183 }
@@ -187,13 +187,15 @@ STATIC uint32_t ble_stack_enable(void) {
187187 memset (& ble_conf , 0 , sizeof (ble_conf ));
188188 // Each additional vs_uuid_count costs 16 bytes.
189189 ble_conf .common_cfg .vs_uuid_cfg .vs_uuid_count = BLEIO_VS_UUID_COUNT ; // Defaults to 10.
190- err_code = sd_ble_cfg_set (BLE_COMMON_CFG_VS_UUID , & ble_conf , app_ram_start );
190+ err_code = sd_ble_cfg_set (BLE_COMMON_CFG_VS_UUID , & ble_conf , sd_ram_end );
191191 if (err_code != NRF_SUCCESS ) {
192192 return err_code ;
193193 }
194194
195- // This sets app_ram_start to the minimum value needed for the settings set above.
196- err_code = sd_ble_enable (& app_ram_start );
195+ // This sets sd_ram_end to the minimum value needed for the settings set above.
196+ // You can set a breakpoint just after this call and examine sd_ram_end to see
197+ // how much RAM the SD needs with the configuration above.
198+ err_code = sd_ble_enable (& sd_ram_end );
197199 if (err_code != NRF_SUCCESS ) {
198200 return err_code ;
199201 }
0 commit comments