Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes ESP32 BT Memory Releasing #8551

Merged
merged 1 commit into from
Aug 23, 2023
Merged

Conversation

SuGlider
Copy link
Collaborator

@SuGlider SuGlider commented Aug 23, 2023

Description of Change

This is related to a change done in PR #8243 to fix Arduino with RainMaker + BLE Provisioning.
The PR #8243 works correctly for the C3 and S3 with no side effects.
But for the ESP32, it causes a problem with the linker that never allows it to release the BT memory whenever the sketch doesn't use BT/BLE.

Therefore, this PR just reverts the PR#8243 for the ESP32, making it work fine as before.

Tests scenarios

Tested with ESP32, ESP32-S3 and ESP32-C3 within the following sketches:

Doesn't use BT/BLE and prints the HEAP.

void setup() {
  Serial.begin(115200);
  Serial.printf("Total Heap Size: %d\n", ESP.getHeapSize());
  Serial.printf("Available Heap: %d\n", ESP.getFreeHeap());
  Serial.printf("Lowest level of heap since boot: %d\n", ESP.getMinFreeHeap());
  Serial.printf("Largest block of heap that can be allocated at once: %d\n", ESP.getMaxAllocHeap());
}

void loop() {}

The same BLE_server.ino example, but also prints HEAP information.

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.printf("Total Heap Size: %d\n", ESP.getHeapSize());
  Serial.printf("Available Heap: %d\n", ESP.getFreeHeap());
  Serial.printf("Lowest level of heap since boot: %d\n", ESP.getMinFreeHeap());
  Serial.printf("Largest block of heap that can be allocated at once: %d\n", ESP.getMaxAllocHeap());

  Serial.println("Starting BLE work!");

  BLEDevice::init("Long name works now");
  BLEServer *pServer = BLEDevice::createServer();
  BLEService *pService = pServer->createService(SERVICE_UUID);
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE
                                       );

  pCharacteristic->setValue("Hello World says Neil");
  pService->start();
  // BLEAdvertising *pAdvertising = pServer->getAdvertising();  // this still is working for backward compatibility
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pAdvertising->setScanResponse(true);
  pAdvertising->setMinPreferred(0x06);  // functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();
  Serial.println("Characteristic defined! Now you can read it in your phone!");
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
}

The sketch from the Issue #7903

The sketch example using WiFiProv.ino, but adjusted to use BLE provisioning.

https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFiProv/examples/WiFiProv/WiFiProv.ino

Related links

Closes #8482
Related to #7903 #8243 #8528

@SuGlider SuGlider self-assigned this Aug 23, 2023
@SuGlider SuGlider added this to the 2.0.12 milestone Aug 23, 2023
@VojtechBartoska VojtechBartoska added the Area: BT&Wifi BT & Wifi related issues label Aug 23, 2023
@VojtechBartoska
Copy link
Contributor

Thanks @SuGlider for the fix and very nice PR description.

@me-no-dev me-no-dev merged commit f1eacf7 into espressif:master Aug 23, 2023
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: BT&Wifi BT & Wifi related issues
Projects
Development

Successfully merging this pull request may close these issues.

Increase in HEAP usage on 2.0.11 (framework 4.4.5)
3 participants