ESPHome  2025.4.0
psram.cpp
Go to the documentation of this file.
1 
2 #ifdef USE_ESP32
3 #include "psram.h"
4 #ifdef USE_ESP_IDF
5 #include <esp_psram.h>
6 #endif // USE_ESP_IDF
7 
8 #include "esphome/core/log.h"
9 
10 #include <esp_heap_caps.h>
11 
12 namespace esphome {
13 namespace psram {
14 static const char *const TAG = "psram";
15 
16 void PsramComponent::dump_config() {
17  ESP_LOGCONFIG(TAG, "PSRAM:");
18 #ifdef USE_ESP_IDF
19  bool available = esp_psram_is_initialized();
20 
21  ESP_LOGCONFIG(TAG, " Available: %s", YESNO(available));
22  if (available) {
23  ESP_LOGCONFIG(TAG, " Size: %zu KB", esp_psram_get_size() / 1024);
24 #if CONFIG_SPIRAM_ECC_ENABLE
25  ESP_LOGCONFIG(TAG, " ECC enabled: YES");
26 #endif
27  }
28 #else
29  // Technically this can be false if the PSRAM is full, but heap_caps_get_total_size() isn't always available, and it's
30  // very unlikely for the PSRAM to be full.
31  bool available = heap_caps_get_free_size(MALLOC_CAP_SPIRAM) > 0;
32  ESP_LOGCONFIG(TAG, " Available: %s", YESNO(available));
33 
34  if (available) {
35  const size_t psram_total_size_bytes = heap_caps_get_total_size(MALLOC_CAP_SPIRAM);
36  const float psram_total_size_kb = psram_total_size_bytes / 1024.0f;
37 
38  if (abs(std::round(psram_total_size_kb) - psram_total_size_kb) < 0.05f) {
39  ESP_LOGCONFIG(TAG, " Size: %.0f KB", psram_total_size_kb);
40  } else {
41  ESP_LOGCONFIG(TAG, " Size: %zu bytes", psram_total_size_bytes);
42  }
43  }
44 #endif // USE_ESP_IDF
45 }
46 
47 } // namespace psram
48 } // namespace esphome
49 
50 #endif
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7