1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-25 13:13:48 +01:00

Improve PSRAM support (#2884)

This commit is contained in:
Oxan van Leeuwen
2022-01-03 22:35:15 +01:00
committed by GitHub
parent dce3713f12
commit dbc2812022
11 changed files with 162 additions and 69 deletions

View File

@@ -8,6 +8,10 @@
#include "esphome/core/log.h"
#include "esphome/components/network/util.h"
#ifdef USE_ESP32
#include <esp_heap_caps.h>
#endif
namespace esphome {
namespace nextion {
static const char *const TAG = "nextion_upload";
@@ -158,12 +162,8 @@ void Nextion::upload_tft() {
if (!begin_status) {
this->is_updating_ = false;
ESP_LOGD(TAG, "connection failed");
#ifdef USE_ESP32
if (psramFound())
free(this->transfer_buffer_); // NOLINT
else
#endif
delete this->transfer_buffer_;
ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
allocator.deallocate(this->transfer_buffer_, this->transfer_buffer_size_);
return;
} else {
ESP_LOGD(TAG, "Connected");
@@ -252,7 +252,7 @@ void Nextion::upload_tft() {
// Nextion wants 4096 bytes at a time. Make chunk_size a multiple of 4096
#ifdef USE_ESP32
uint32_t chunk_size = 8192;
if (psramFound()) {
if (heap_caps_get_free_size(MALLOC_CAP_SPIRAM) > 0) {
chunk_size = this->content_length_;
} else {
if (ESP.getFreeHeap() > 40960) { // 32K to keep on hand
@@ -269,32 +269,18 @@ void Nextion::upload_tft() {
#endif
if (this->transfer_buffer_ == nullptr) {
#ifdef USE_ESP32
if (psramFound()) {
ESP_LOGD(TAG, "Allocating PSRAM buffer size %d, Free PSRAM size is %u", chunk_size, ESP.getFreePsram());
this->transfer_buffer_ = (uint8_t *) ps_malloc(chunk_size);
if (this->transfer_buffer_ == nullptr) {
ESP_LOGE(TAG, "Could not allocate buffer size %d!", chunk_size);
this->upload_end_();
}
} else {
#endif
// NOLINTNEXTLINE(readability-static-accessed-through-instance)
ESP_LOGD(TAG, "Allocating buffer size %d, Heap size is %u", chunk_size, ESP.getFreeHeap());
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
this->transfer_buffer_ = new (std::nothrow) uint8_t[chunk_size];
if (this->transfer_buffer_ == nullptr) { // Try a smaller size
ESP_LOGD(TAG, "Could not allocate buffer size: %d trying 4096 instead", chunk_size);
chunk_size = 4096;
ESP_LOGD(TAG, "Allocating %d buffer", chunk_size);
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
this->transfer_buffer_ = new uint8_t[chunk_size];
ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
// NOLINTNEXTLINE(readability-static-accessed-through-instance)
ESP_LOGD(TAG, "Allocating buffer size %d, Heap size is %u", chunk_size, ESP.getFreeHeap());
this->transfer_buffer_ = allocator.allocate(chunk_size);
if (this->transfer_buffer_ == nullptr) { // Try a smaller size
ESP_LOGD(TAG, "Could not allocate buffer size: %d trying 4096 instead", chunk_size);
chunk_size = 4096;
ESP_LOGD(TAG, "Allocating %d buffer", chunk_size);
this->transfer_buffer_ = allocator.allocate(chunk_size);
if (!this->transfer_buffer_)
this->upload_end_();
#ifdef USE_ESP32
}
#endif
if (!this->transfer_buffer_)
this->upload_end_();
}
this->transfer_buffer_size_ = chunk_size;