esp_sys.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "../../include/defines.h"
  2. #ifdef DEBUG_ENABLE_SYSINFO
  3. #include "esp_sys.h"
  4. #include "esp_chip_info.h"
  5. #include <string>
  6. #include "esp_chip_info.h"
  7. void Restart() {
  8. esp_restart();
  9. }
  10. //source : https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/misc_system_api.html#_CPPv416esp_chip_model_t
  11. //https://github.com/espressif/esp-idf/blob/8464186e67e34b417621df6b6f1f289a6c60b859/components/esp_hw_support/include/esp_chip_info.h
  12. /*
  13. typedef enum {
  14. CHIP_ESP32 = 1, //!< ESP32
  15. CHIP_ESP32S2 = 2, //!< ESP32-S2
  16. CHIP_ESP32S3 = 9, //!< ESP32-S3
  17. CHIP_ESP32C3 = 5, //!< ESP32-C3
  18. CHIP_ESP32H4 = 6, //!< ESP32-H4
  19. CHIP_ESP32C2 = 12, //!< ESP32-C2
  20. CHIP_ESP32C6 = 13, //!< ESP32-C6
  21. CHIP_ESP32H2 = 16, //!< ESP32-H2
  22. CHIP_POSIX_LINUX = 999, //!< The code is running on POSIX/Linux simulator
  23. } esp_chip_model_t;
  24. */
  25. char* GetChipModel(){
  26. esp_chip_info_t chipInfo;
  27. esp_chip_info(&chipInfo);
  28. switch((int)chipInfo.model) {
  29. case 0 : return (char*)"ESP8266";
  30. case (int)esp_chip_model_t::CHIP_ESP32 : return (char*)"ESP32";
  31. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
  32. case (int)esp_chip_model_t::CHIP_ESP32S2 : return (char*)"ESP32-S2";
  33. case (int)esp_chip_model_t::CHIP_ESP32S3 : return (char*)"ESP32-S3";
  34. case (int)esp_chip_model_t::CHIP_ESP32C3 : return (char*)"ESP32-C3";
  35. case 6 : return (char*)"ESP32-H4";
  36. case 12 : return (char*)"ESP32-C2";
  37. case 13 : return (char*)"ESP32-C6";
  38. //case (int)esp_chip_model_t::CHIP_ESP32H4 : return (char*)"ESP32-H4";
  39. //case (int)esp_chip_model_t::CHIP_ESP32C2 : return (char*)"ESP32-C2";
  40. //case (int)esp_chip_model_t::CHIP_ESP32C6 : return (char*)"ESP32-C6";
  41. //case (int)esp_chip_model_t::CHIP_ESP32H2 : return (char*)"ESP32-H2";
  42. case 16 : return (char*)"ESP32-H2";
  43. //case (int)esp_chip_model_t::CHIP_POSIX_LINUX : return (char*)"CHIP_POSIX_LINUX";
  44. #endif
  45. }
  46. return (char*)"Chip Unknown";
  47. }
  48. uint8_t GetChipCoreCount() {
  49. esp_chip_info_t chipInfo;
  50. esp_chip_info(&chipInfo);
  51. return chipInfo.cores;
  52. }
  53. uint16_t GetChipRevision() {
  54. esp_chip_info_t chipInfo;
  55. esp_chip_info(&chipInfo);
  56. return chipInfo.revision;
  57. }
  58. uint32_t GetChipfeatures() {
  59. esp_chip_info_t chipInfo;
  60. esp_chip_info(&chipInfo);
  61. return chipInfo.features;
  62. }
  63. uint32_t GetFreeHeap() {
  64. return esp_get_free_heap_size();
  65. }
  66. uint32_t GetLeastHeapFreeSinceBoot() {
  67. return esp_get_minimum_free_heap_size();
  68. }
  69. std::string get_device_info()
  70. {
  71. esp_chip_info_t chip_info;
  72. esp_chip_info(&chip_info);
  73. std::string espInfoResultStr = "";
  74. char aMsgBuf[40];
  75. espInfoResultStr += "Device Info:";
  76. espInfoResultStr += "---------------\n";
  77. espInfoResultStr += "Chip Model: " + std::string(GetChipModel()) +"\n";
  78. sprintf(aMsgBuf,"Chip Revision: %d\n", chip_info.revision);
  79. espInfoResultStr += std::string(aMsgBuf);
  80. sprintf(aMsgBuf,"CPU Cores: %d\n", chip_info.cores);
  81. espInfoResultStr += std::string(aMsgBuf);
  82. sprintf(aMsgBuf,"Flash Memory: %dMB\n", spi_flash_get_chip_size()/(1024*1024));
  83. espInfoResultStr += std::string(aMsgBuf);
  84. if(chip_info.features & CHIP_FEATURE_WIFI_BGN)
  85. //espInfoResultStr += "Base MAC: " + std::string(getMac()) +"\n";
  86. espInfoResultStr += "ESP-IDF version: " + std::string(esp_get_idf_version()) +"\n";
  87. if((chip_info.features & CHIP_FEATURE_WIFI_BGN) || (chip_info.features & CHIP_FEATURE_BT) ||
  88. (chip_info.features & CHIP_FEATURE_BLE) || (chip_info.features & CHIP_FEATURE_EMB_FLASH))
  89. {
  90. espInfoResultStr += "Characteristics:\n";
  91. if(chip_info.features & CHIP_FEATURE_WIFI_BGN)
  92. espInfoResultStr += " WiFi 2.4GHz\n";
  93. if(chip_info.features & CHIP_FEATURE_BT)
  94. espInfoResultStr += " Bluetooth Classic\n";
  95. if(chip_info.features & CHIP_FEATURE_BLE)
  96. espInfoResultStr += " Bluetooth Low Energy\n";
  97. if(chip_info.features & CHIP_FEATURE_EMB_FLASH)
  98. espInfoResultStr += " Embedded Flash memory\n";
  99. else
  100. espInfoResultStr += " External Flash memory\n";
  101. }
  102. #ifdef USE_HIMEM_IF_AVAILABLE
  103. sprintf(aMsgBuf,"spiram size %u\n", esp_psram_get_size());
  104. espInfoResultStr += std::string(aMsgBuf);
  105. sprintf(aMsgBuf,"himem free %u\n", esp_himem_get_free_size());
  106. espInfoResultStr += std::string(aMsgBuf);
  107. sprintf(aMsgBuf,"himem phys %u\n", esp_himem_get_phys_size());
  108. espInfoResultStr += std::string(aMsgBuf);
  109. sprintf(aMsgBuf,"himem reserved %u\n", esp_himem_reserved_area_size());
  110. espInfoResultStr += std::string(aMsgBuf);
  111. #endif
  112. return espInfoResultStr;
  113. }
  114. size_t getFreeMemoryInternal(){ //Current Free Memory
  115. return heap_caps_get_free_size(MALLOC_CAP_8BIT) - heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
  116. }
  117. size_t getFreeMemorySPIRAM(){ //Current Free Memory
  118. return heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
  119. }
  120. size_t getLargestFreeBlockInternal(){ //Largest Free Block
  121. return heap_caps_get_largest_free_block(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  122. }
  123. size_t getLargestFreeBlockSPIRAM(){ //Largest Free Block
  124. return heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM);
  125. }
  126. size_t getMinEverFreeMemInternal(){ //Min. Ever Free Size
  127. return heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
  128. }
  129. size_t getMinEverFreeMemSPIRAM(){ //Min. Ever Free Size
  130. return heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM);
  131. }
  132. #ifdef USE_HIMEM_IF_AVAILABLE
  133. size_t getHimemTotSpace(){
  134. return esp_himem_get_phys_size();
  135. }
  136. size_t getHimemFreeSpace(){
  137. return esp_himem_get_free_size();
  138. }
  139. size_t getHimemReservedArea(){
  140. return esp_himem_reserved_area_size();
  141. }
  142. #endif
  143. #endif //DEBUG_ENABLE_SYSINFO