esp_sys.cpp 5.9 KB

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