NimBLE-Arduino icon indicating copy to clipboard operation
NimBLE-Arduino copied to clipboard

Enable PSRam for ESP32 (with Platform IO)

Open v11 opened this issue 3 years ago • 0 comments

Hello

I am working on a firmware. The firmware works fine as long as I don't include bluetooth. As soon as I use bluetooth, i get memory allocation crashes when trying to do HTTPS requests. Therefore i have split my firmware in different startup modes. The idea: I only want to initalize the services needed for the specific startup mode.

The following code is simplified. Just to give some context.

void setup() {

    // Attach Interrupts
    setInterrupt();

    // Init EPD
    initEPD();

    // SSID Data
    getSSIDData();

    switch (getStartupMode()) {
    case startUpWifi:

        // Connect to WiFi Network
        getWifi();

        // Get Config Data from Firebase
        getDeviceConfig(firebaseDatabaseHost);

        // Check Version
        checkVersion(updateServerHost, updateServerURL);

        // Render Bitmap
        renderBitmapFromHTTPS(firebaseStorageHost, firebaseStorageURL);

        // Save Logs to Firebase
        sendDeviceLogs(firebaseDatabaseHost);

        // Deep Sleep
        setDeepSleep(forever); 
        break;

    case startUpBluetooth:

        // Bluetooth Mode
        startUpMode = startUpBluetooth;

       // Start a Bluetooth Service to recieve data
       getBluetooth();

        break;
    
    default:
        break;
    }

And this code should init the Bluetooth Service. But only when startup mode is "startUpBluetooth".

void getBluetooth() {

    // This function establishes a Bluetooth Service
    // Use the iOS App nRF Connect to connect and write utf-8 data
    // Example JSON: {"ssid": "wifi","passphrase":"123456"}

    Serial.println("");
    Serial.println("get bluetooth");

    // // Init the Device
    NimBLEDevice::init("Inklay");

    // Create a Bluetooth Server
    BLEServer *pServer = NimBLEDevice::createServer();

    // Create a Service with the UUID
    BLEService *pService = pServer->createService(SERVICE_UUID);

    // Set up the Service as Read / Write
    BLECharacteristic *pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID);

    // Set the Callback (gets called when data is recieved)
    pCharacteristic->setCallbacks(new MyCallbacks());

    // Start the service
    pService->start();

    // Start advertising (visibility for ble-scanners)
    BLEAdvertising *pAdvertising = pServer->getAdvertising();
    pAdvertising->start();
}

In the above code you should see, that I try to not run Bluetooth and Wi-Fi at the same time to save memory. The getBluetooth() function is only called when the start up mode is "startUpBluetooth". But it seems that, also when getBluetooth is not get called, a lot of memory gets allocated. Only when i completely clear all the code in the getBluetooth() function, the memory gets not allocated.

With BLE:

Heap Size: 219 kb
Free Heap: 192 kb

Without BLE

Heap Size: 294 kb
Free Heap: 268 kb

Question 1 How is this possible? I am confused. Is it possble to free the memory when not using bluetooth?

Question 2 Since I am working with the ESP Wrover i was also checking if I could use the Psram. But i could not enable this. I tried to uncomment, but there is no effect.

/** @brief Un-comment to use external PSRAM for the NimBLE host */
#define CONFIG_BT_NIMBLE_MEM_ALLOC_MODE_EXTERNAL 1

Does anybody have a tip, to run Bluetooth on Psram?

Here is my platform.ini

[env:esp32dev]
framework = arduino
platform = [email protected]
; platform = [email protected] ; Working: Fast Updates and Bluetooth
board = esp32dev
board_build.f_cpu = 240000000L
board_build.partitions = min_spiffs.csv
board_build.f_flash = 80000000L
upload_speed = 115200
monitor_speed = 115200
build_type = debug
monitor_filters = esp32_exception_decoder
lib_deps = 
	vroland/epdiy@^1.0.0
	bblanchon/ArduinoJson@^6.19.4
	makuna/NeoPixelBus@^2.6.9
	h2zero/NimBLE-Arduino@^1.4.0
build_flags = 
	-DBOARD_HAS_PSRAM
	-mfix-esp32-psram-cache-issue
	-DCONFIG_EPD_BOARD_REVISION_V5
	-DCONFIG_EPD_DISPLAY_TYPE_ED097OC4
	-DCONFIG_ARDUINO_ISR_IRAM=1
	-DCONFIG_BT_NIMBLE_MEM_ALLOC_MODE_EXTERNAL=1

v11 avatar Jan 15 '23 21:01 v11