Skip to content

Commit

Permalink
Add support ESP8266 external virtual RAM (SRAM or PSRAM). Fixed compi…
Browse files Browse the repository at this point in the history
…lation error in FirebaseJson due to multiple sources of cJSON.
  • Loading branch information
mobizt committed Nov 23, 2021
1 parent 3205984 commit 3076fc6
Show file tree
Hide file tree
Showing 20 changed files with 1,237 additions and 2,541 deletions.
144 changes: 143 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Firebase Arduino Client Library for ESP8266 and ESP32


Google's Firebase Arduino Client Library for ESP8266 and ESP32 v2.6.7
Google's Firebase Arduino Client Library for ESP8266 and ESP32 v2.7.0


This library supports ESP8266 and ESP32 MCU from Espressif. The following are platforms in which the libraries are also available (RTDB only).
Expand Down Expand Up @@ -200,6 +200,148 @@ See [other authentication examples](/examples/Authentications) for more sign in




## IDE Configuaration for ESP8266 MMU - Adjust the Ratio of ICACHE to IRAM

### Arduino IDE

When you update the ESP8266 Arduino Core SDK to v3.0.0, the memory can be configurable from Arduino IDE board settings.

By default MMU **option 1** was selected, the free Heap can be low and may not suitable for the SSL client usage in this library.

To increase the Heap, choose the MMU **option 3**, 16KB cache + 48KB IRAM and 2nd Heap (shared).

![Arduino IDE config](/media/images/ArduinoIDE.png)

To use external Heap from 1 Mbit SRAM 23LC1024, choose the MMU **option 5**, 128K External 23LC1024.

![MMU VM 128K](/media/images/ESP8266_VM.png)

To use external Heap from PSRAM, choose the MMU **option 6**, 1M External 64 MBit PSRAM.

The connection between SRAM/PSRAM and ESP8266

```
23LC1024/ESP-PSRAM64 ESP8266
CS (Pin 1) GPIO15
SCK (Pin 6) GPIO14
MOSI (Pin 5) GPIO13
MISO (Pin 2) GPIO12
/HOLD (Pin 7 on 23LC1024 only) 3V3
Vcc (Pin 8) 3V3
Vcc (Pin 4) GND
```

More about MMU settings.
https://arduino-esp8266.readthedocs.io/en/latest/mmu.html



### PlatformIO IDE

By default the balanced ratio (32KB cache + 32KB IRAM) configuration is used.

To increase the heap, **PIO_FRAMEWORK_ARDUINO_MMU_CACHE16_IRAM48_SECHEAP_SHARED** build flag should be assigned in platformio.ini.

```ini
[env:d1_mini]
platform = espressif8266
build_flags = -D PIO_FRAMEWORK_ARDUINO_MMU_CACHE16_IRAM48_SECHEAP_SHARED
board = d1_mini
framework = arduino
monitor_speed = 115200
```

And to use external Heap from 1 Mbit SRAM 23LC1024 and 64 Mbit PSRAM, **PIO_FRAMEWORK_ARDUINO_MMU_EXTERNAL_128K** and **PIO_FRAMEWORK_ARDUINO_MMU_EXTERNAL_1024K** build flags should be assigned respectively.

The supportedd MMU build flags in PlatformIO.

- **PIO_FRAMEWORK_ARDUINO_MMU_CACHE16_IRAM48**

16KB cache + 48KB IRAM (IRAM)

- **PIO_FRAMEWORK_ARDUINO_MMU_CACHE16_IRAM48_SECHEAP_SHARED**

16KB cache + 48KB IRAM and 2nd Heap (shared)

- **PIO_FRAMEWORK_ARDUINO_MMU_CACHE16_IRAM32_SECHEAP_NOTSHARED**

16KB cache + 32KB IRAM + 16KB 2nd Heap (not shared)

- **PIO_FRAMEWORK_ARDUINO_MMU_EXTERNAL_128K**

128K External 23LC1024

- **PIO_FRAMEWORK_ARDUINO_MMU_EXTERNAL_1024K**

1M External 64 MBit PSRAM

- **PIO_FRAMEWORK_ARDUINO_MMU_CUSTOM**

Disables default configuration and expects user-specified flags


To use PSRAM/SRAM for internal memory allocation which you can config to use it via [**FirebaseFS.h**](src/FirebaseFS.h) with this macro.

```cpp
#define FIREBASE_USE_PSRAM
```


### Test code for MMU

```cpp

#include <Arduino.h>
#include <umm_malloc/umm_heap_select.h>

void setup()
{
Serial.begin(115200);
HeapSelectIram ephemeral;
Serial.printf("IRAM free: %6d bytes\r\n", ESP.getFreeHeap());
{
HeapSelectDram ephemeral;
Serial.printf("DRAM free: %6d bytes\r\n", ESP.getFreeHeap());
}

ESP.setExternalHeap();
Serial.printf("External free: %d\n", ESP.getFreeHeap());
ESP.resetHeap();
}

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

```


### Use PSRAM on ESP32


To enable PSRAM in ESP32 module with on-board PSRAM chip, in Arduino IDE

![Enable PSRAM in ESP32](/media/images/ESP32-PSRAM.png)


In PlatformIO in VSCode IDE, add the following build_flags in your project's platformio.ini file

```ini
build_flags = -DBOARD_HAS_PSRAM -mfix-esp32-psram-cache-issue
```

*When config the IDE or add the build flags to use PSRAM in the ESP32 dev boards that do not have on-board PSRAM chip, your device will be crashed (reset).


Since v2.6.0, this library supports PSRAM for internal memory allocation which you can config to use it via [**FirebaseFS.h**](src/FirebaseFS.h) with this macro.

```cpp
#define FIREBASE_USE_PSRAM
```


## IDE Configuaration for ESP8266 MMU - Adjust the Ratio of ICACHE to IRAM


Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Firebase Arduino Client Library for ESP8266 and ESP32",
"version": "2.6.7",
"version": "2.7.0",
"keywords": "communication, REST, esp32, esp8266, arduino",
"description": "The library supports Firebase products e.g. Realtime database, Cloud Firestore database, Firebase Storage and Google Cloud Storage, Cloud Functions for Firebase and Cloud Messaging.",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=Firebase Arduino Client Library for ESP8266 and ESP32

version=2.6.7
version=2.7.0

author=Mobizt

Expand Down
Binary file added media/images/ESP8266_VM.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 10 additions & 8 deletions src/Firebase.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* The Firebase class, Firebase.cpp v1.0.10
* The Firebase class, Firebase.cpp v1.0.11
*
* Created November 22, 2021
* Created November 23, 2021
*
* The MIT License (MIT)
* Copyright (c) 2021 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -188,14 +188,14 @@ void Firebase_ESP_Client::init(FirebaseConfig *config, FirebaseAuth *auth)
cfg->_int.fb_reconnect_wifi = WiFi.getAutoReconnect();

cfg->signer.lastReqMillis = 0;

//don't clear auth token if anonymous sign in or Email/Password sign up
if (!cfg->signer.anonymous && !cfg->signer.signup)
{
ut->clearS(cfg->_int.auth_token);
cfg->signer.tokens.expires = 0;
}

if (auth->user.email.length() > 0 && auth->user.password.length() > 0)
cfg->signer.idTokenCutomSet = false;

Expand Down Expand Up @@ -435,11 +435,13 @@ void FIREBASE_CLASS::init(FirebaseConfig *config, FirebaseAuth *auth)
cfg->_int.fb_reconnect_wifi = WiFi.getAutoReconnect();

cfg->signer.lastReqMillis = 0;
cfg->signer.tokens.expires = 0;

//don't clear auth token if anonymous sign in
if (!cfg->signer.anonymous)
//don't clear auth token if anonymous sign in or Email/Password sign up
if (!cfg->signer.anonymous && !cfg->signer.signup)
{
ut->clearS(cfg->_int.auth_token);
cfg->signer.tokens.expires = 0;
}

if (auth->user.email.length() > 0 && auth->user.password.length() > 0)
cfg->signer.idTokenCutomSet = false;
Expand Down Expand Up @@ -609,7 +611,7 @@ bool FIREBASE_CLASS::sdBegin(int8_t ss, int8_t sck, int8_t miso, int8_t mosi)

bool FIREBASE_CLASS::sdMMCBegin(const String &mountpoint, bool mode1bit, bool format_if_mount_failed)
{
#if defined (SD_FS) && defined(ESP32) && defined(CARD_TYPE_SD_MMC)
#if defined(SD_FS) && defined(ESP32) && defined(CARD_TYPE_SD_MMC)
if (Signer.getCfg())
{
Signer.getCfg()->_int.sd_config.sd_mmc_mountpoint = mountpoint;
Expand Down
4 changes: 2 additions & 2 deletions src/Firebase.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

/**
* The Firebase class, Firebase.h v1.0.10
* The Firebase class, Firebase.h v1.0.11
*
* Created November 22, 2021
* Created November 23, 2021
*
* The MIT License (MIT)
* Copyright (c) 2021 K. Suwatchai (Mobizt)
Expand Down
4 changes: 2 additions & 2 deletions src/FirebaseFS.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
//Comment to exclude Cloud Function for Firebase
#define ENABLE_FB_FUNCTIONS

/** Use PSRAM for supported ESP32 module */
#if defined(ESP32)
/** Use PSRAM for supported ESP32/ESP8266 module */
#if defined(ESP32) || defined(ESP8266)
#define FIREBASE_USE_PSRAM
#endif

Expand Down
9 changes: 5 additions & 4 deletions src/Firebase_ESP_Client.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#ifndef FIREBASE_CLIENT_VERSION
#define FIREBASE_CLIENT_VERSION "2.6.7"
#define FIREBASE_CLIENT_VERSION "2.7.0"
#endif

/**
* Google's Firebase ESP Client Main class, Firebase_ESP_Client.h v2.6.7
* Google's Firebase ESP Client Main class, Firebase_ESP_Client.h v2.7.0
*
* This library supports Espressif ESP8266 and ESP32 MCUs
*
* Created November 22, 2021
* Created November 23, 2021
*
* Updates:
* - Fixed token refreshment issue for anonymous sign in.
* - Fixed compilation error in FirebaseJson due to multiple sources of cJSON.
* - Add support ESP8266 external virtual RAM (SRAM or PSRAM).
*
*
* This work is a part of Firebase ESP Client library
Expand Down
2 changes: 1 addition & 1 deletion src/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Firebase Arduino Client Library for ESP8266 and ESP32


Google's Firebase Arduino Client Library for ESP8266 and ESP32 v2.6.7
Google's Firebase Arduino Client Library for ESP8266 and ESP32 v2.7.0


The default filessystem used in the library is flash and SD.
Expand Down
16 changes: 13 additions & 3 deletions src/Utils.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Google's Firebase Util class, Utils.h version 1.1.5
* Google's Firebase Util class, Utils.h version 1.1.6
*
* This library supports Espressif ESP8266 and ESP32
*
* Created November 20, 2021
* Created November 23, 2021
*
* This work is a part of Firebase ESP Client library
* Copyright (c) 2021 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -303,7 +303,17 @@ class UtilsClass

#else

if ((p = (void *)malloc(newLen)) == 0)
#if defined(ESP8266_USE_EXTERNAL_HEAP)
ESP.setExternalHeap();
#endif

bool nn = ((p = (void *)malloc(newLen)) > 0);

#if defined(ESP8266_USE_EXTERNAL_HEAP)
ESP.resetHeap();
#endif

if (!nn)
return NULL;

#endif
Expand Down
Loading

0 comments on commit 3076fc6

Please sign in to comment.