Skip to content

Commit dbf0b18

Browse files
ferbarme-no-dev
andauthored
Bluetooth-classic: release BLE memory when BT classic only is requested (#8051)
* esp32-hal-bt.c free Bluetooth LE memory if CONFIG_BTDM_CONTROLLER_MODE_BR_EDR_ONLY is set BLE memory can be released if bluetooth-classic - only is requested * tStart( add error output * ble mem_release only for esp32 * disable BLE with BT_MODE define * BluetoothSerial add begin()+disableBLE; add memrelease * btStart with BT_MODE parameter * beautification * Update BluetoothSerial.cpp fix wrong merges --------- Co-authored-by: Me No Dev <me-no-dev@users.noreply.github.com>
1 parent 7d26b07 commit dbf0b18

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

Diff for: cores/esp32/esp32-hal-bt.c

+34-4
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,48 @@ bool btStarted(){
3838
return (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED);
3939
}
4040

41-
bool btStart(){
41+
bool btStart() {
42+
return btStartMode(BT_MODE);
43+
}
44+
45+
bool btStartMode(bt_mode mode){
46+
esp_bt_mode_t esp_bt_mode;
4247
esp_bt_controller_config_t cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
48+
#if CONFIG_IDF_TARGET_ESP32
49+
switch(mode) {
50+
case BT_MODE_BLE: esp_bt_mode=ESP_BT_MODE_BLE;
51+
break;
52+
case BT_MODE_CLASSIC_BT: esp_bt_mode=ESP_BT_MODE_CLASSIC_BT;
53+
break;
54+
case BT_MODE_BTDM: esp_bt_mode=ESP_BT_MODE_BTDM;
55+
break;
56+
default: esp_bt_mode=BT_MODE;
57+
break;
58+
}
59+
// esp_bt_controller_enable(MODE) This mode must be equal as the mode in “cfg” of esp_bt_controller_init().
60+
cfg.mode=esp_bt_mode;
61+
if(cfg.mode == ESP_BT_MODE_CLASSIC_BT) {
62+
esp_bt_controller_mem_release(ESP_BT_MODE_BLE);
63+
}
64+
#else
65+
// other esp variants dont support BT-classic / DM.
66+
esp_bt_mode=BT_MODE;
67+
#endif
68+
4369
if(esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_ENABLED){
4470
return true;
4571
}
72+
esp_err_t ret;
4673
if(esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE){
47-
esp_bt_controller_init(&cfg);
74+
if((ret = esp_bt_controller_init(&cfg)) != ESP_OK) {
75+
log_e("initialize controller failed: %s", esp_err_to_name(ret));
76+
return false;
77+
}
4878
while(esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE){}
4979
}
5080
if(esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED){
51-
if (esp_bt_controller_enable(BT_MODE)) {
52-
log_e("BT Enable failed");
81+
if((ret = esp_bt_controller_enable(esp_bt_mode)) != ESP_OK) {
82+
log_e("BT Enable mode=%d failed %s", BT_MODE, esp_err_to_name(ret));
5383
return false;
5484
}
5585
}

Diff for: cores/esp32/esp32-hal-bt.h

+3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
extern "C" {
2525
#endif
2626

27+
typedef enum {BT_MODE_DEFAULT, BT_MODE_BLE, BT_MODE_CLASSIC_BT, BT_MODE_BTDM } bt_mode;
28+
2729
bool btStarted();
2830
bool btStart();
31+
bool btStartMode(bt_mode mode);
2932
bool btStop();
3033

3134
#ifdef __cplusplus

Diff for: libraries/BluetoothSerial/src/BluetoothSerial.cpp

+21-11
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
626626
}
627627
}
628628

629-
static bool _init_bt(const char *deviceName)
629+
static bool _init_bt(const char *deviceName, bt_mode mode)
630630
{
631631
if(!_bt_event_group){
632632
_bt_event_group = xEventGroupCreate();
@@ -678,7 +678,7 @@ static bool _init_bt(const char *deviceName)
678678
}
679679
}
680680

681-
if (!btStarted() && !btStart()){
681+
if (!btStarted() && !btStartMode(mode)){
682682
log_e("initialize controller failed");
683683
return false;
684684
}
@@ -815,11 +815,10 @@ static bool waitForSDPRecord(int timeout) {
815815
return (xEventGroupWaitBits(_bt_event_group, BT_SDP_COMPLETED, pdFALSE, pdTRUE, xTicksToWait) & BT_SDP_COMPLETED) != 0;
816816
}
817817

818-
/*
818+
/**
819819
* Serial Bluetooth Arduino
820820
*
821-
* */
822-
821+
*/
823822
BluetoothSerial::BluetoothSerial()
824823
{
825824
local_name = "ESP32"; //default bluetooth name
@@ -831,15 +830,16 @@ BluetoothSerial::~BluetoothSerial(void)
831830
}
832831

833832
/**
834-
* @Param isMaster set to true if you want to connect to an other device
833+
* @param isMaster set to true if you want to connect to an other device
834+
* @param disableBLE if BLE is not used, its ram can be freed to get +10kB free ram
835835
*/
836-
bool BluetoothSerial::begin(String localName, bool isMaster)
836+
bool BluetoothSerial::begin(String localName, bool isMaster, bool disableBLE)
837837
{
838838
_isMaster = isMaster;
839839
if (localName.length()){
840840
local_name = localName;
841841
}
842-
return _init_bt(local_name.c_str());
842+
return _init_bt(local_name.c_str(), disableBLE ? BT_MODE_CLASSIC_BT : BT_MODE_BTDM);
843843
}
844844

845845
int BluetoothSerial::available(void)
@@ -910,6 +910,14 @@ void BluetoothSerial::end()
910910
_stop_bt();
911911
}
912912

913+
/**
914+
* free additional ~30kB ram, reset is required to enable BT again
915+
*/
916+
void BluetoothSerial::memrelease()
917+
{
918+
esp_bt_mem_release(ESP_BT_MODE_BTDM);
919+
}
920+
913921
#ifdef CONFIG_BT_SSP_ENABLED
914922
void BluetoothSerial::onConfirmRequest(ConfirmRequestCb cb)
915923
{
@@ -1026,11 +1034,13 @@ bool BluetoothSerial::connect(String remoteName)
10261034
}
10271035

10281036
/**
1029-
* @Param channel: specify channel or 0 for auto-detect
1030-
* @Param sec_mask:
1037+
* Connect to an other bluetooth device
1038+
*
1039+
* @param channel specify channel or 0 for auto-detect
1040+
* @param sec_mask
10311041
* ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE
10321042
* ESP_SPP_SEC_NONE
1033-
* @Param role:
1043+
* @param role
10341044
* ESP_SPP_ROLE_MASTER master can handle up to 7 connections to slaves
10351045
* ESP_SPP_ROLE_SLAVE can only have one connection to a master
10361046
*/

Diff for: libraries/BluetoothSerial/src/BluetoothSerial.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class BluetoothSerial: public Stream
4242
BluetoothSerial(void);
4343
~BluetoothSerial(void);
4444

45-
bool begin(String localName=String(), bool isMaster=false);
45+
bool begin(String localName=String(), bool isMaster=false, bool disableBLE=false);
4646
bool begin(unsigned long baud){//compatibility
4747
return begin();
4848
}
@@ -54,6 +54,7 @@ class BluetoothSerial: public Stream
5454
size_t write(const uint8_t *buffer, size_t size);
5555
void flush();
5656
void end(void);
57+
void memrelease();
5758
void setTimeout(int timeoutMS);
5859
void onData(BluetoothSerialDataCb cb);
5960
esp_err_t register_callback(esp_spp_cb_t * callback);

0 commit comments

Comments
 (0)