psram.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "ClassLogFile.h"
  2. #include "../../include/defines.h"
  3. #include "psram.h"
  4. static const char* TAG = "PSRAM";
  5. using namespace std;
  6. void *shared_region = NULL;
  7. uint32_t allocatedBytesForSTBI = 0;
  8. /** Reserve a large block in the PSRAM which will be shared between the different steps.
  9. * Each step uses it differently but only wiuthin itself. */
  10. bool reserve_psram_shared_region(void) {
  11. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocating shared PSRAM region (" +
  12. std::to_string(TENSOR_ARENA_SIZE + MAX_MODEL_SIZE) + " bytes)...");
  13. shared_region = malloc_psram_heap("Shared PSRAM region", TENSOR_ARENA_SIZE + MAX_MODEL_SIZE,
  14. MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  15. if (shared_region == NULL) {
  16. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to allocating shared PSRAM region!");
  17. return false;
  18. }
  19. else {
  20. return true;
  21. }
  22. }
  23. /*******************************************************************
  24. * Memory used in Take Image (STBI)
  25. *******************************************************************/
  26. void psram_init_shared_memory_for_take_image_step(void) {
  27. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Init shared memory for step 'Take Image' (STBI buffers)");
  28. allocatedBytesForSTBI = 0;
  29. }
  30. void *psram_reserve_shared_stbi_memory(size_t size) {
  31. /* Only large buffers should be placed in the shared PSRAM
  32. * If we also place all smaller STBI buffers here, we get artefacts for some reasons. */
  33. if (size >= 100000) {
  34. if ((allocatedBytesForSTBI + size) > TENSOR_ARENA_SIZE + MAX_MODEL_SIZE) { // Check if it still fits in the shared region
  35. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Shared memory in PSRAM too small (STBI) to fit additional " +
  36. std::to_string(size) + "bytes! Available: " + std::to_string(TENSOR_ARENA_SIZE + MAX_MODEL_SIZE - allocatedBytesForSTBI) + " bytes!");
  37. return NULL;
  38. }
  39. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocating memory (" + std::to_string(size) + " bytes) for STBI (use shared memory in PSRAM)...");
  40. allocatedBytesForSTBI += size;
  41. return (uint8_t *)shared_region + allocatedBytesForSTBI - size;
  42. }
  43. else { // Normal PSRAM
  44. return malloc_psram_heap("STBI", size, MALLOC_CAP_SPIRAM);
  45. }
  46. }
  47. void *psram_reallocate_shared_stbi_memory(void *ptr, size_t newsize) {
  48. char buf[20];
  49. sprintf(buf, "%p", ptr);
  50. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "STBI requested realloc for " + std::string(buf) + " but this is currently unsupported!");
  51. return NULL;
  52. }
  53. void psram_free_shared_stbi_memory(void *p) {
  54. if ((p >= shared_region) && (p <= ((uint8_t *)shared_region + allocatedBytesForSTBI))) { // was allocated inside the shared memory
  55. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Part of shared memory used for STBI (PSRAM, part of shared memory) is free again");
  56. }
  57. else { // Normal PSRAM
  58. free_psram_heap("STBI", p);
  59. }
  60. }
  61. /*******************************************************************
  62. * Memory used in Aligning Step
  63. * During this step we only use the shared part of the PSRAM
  64. * for the tmpImage.
  65. *******************************************************************/
  66. void *psram_reserve_shared_tmp_image_memory(void) {
  67. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocating tmpImage (" + std::to_string(IMAGE_SIZE) + " bytes, use shared memory in PSRAM)...");
  68. return shared_region; // Use 1th part of the shared memory for the tmpImage (only user)
  69. }
  70. void psram_free_shared_temp_image_memory(void) {
  71. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Shared memory used for tmpImage (PSRAM, part of shared memory) is free again");
  72. }
  73. /*******************************************************************
  74. * Memory used in Digitalization Steps
  75. * During this step we only use the shared part of the PSRAM for the
  76. * Tensor Arena and one of the Models.
  77. * The shared memory is large enough for the largest model and the
  78. * Tensor Arena. Therefore we do not need to monitor the usage.
  79. *******************************************************************/
  80. void *psram_get_shared_tensor_arena_memory(void) {
  81. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocating Tensor Arena (" + std::to_string(TENSOR_ARENA_SIZE) + " bytes, use shared memory in PSRAM)...");
  82. return shared_region; // Use 1th part of the shared memory for Tensor
  83. }
  84. void *psram_get_shared_model_memory(void) {
  85. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocating Model memory (" + std::to_string(MAX_MODEL_SIZE) + " bytes, use shared memory in PSRAM)...");
  86. return (uint8_t *)shared_region + TENSOR_ARENA_SIZE; // Use 2nd part of the shared memory (after Tensor Arena) for the model
  87. }
  88. void psram_free_shared_tensor_arena_and_model_memory(void) {
  89. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Shared memory used for Tensor Arena and model (PSRAM, part of shared memory) is free again");
  90. }
  91. /*******************************************************************
  92. * General
  93. *******************************************************************/
  94. void *malloc_psram_heap(std::string name, size_t size, uint32_t caps) {
  95. void *ptr;
  96. ptr = heap_caps_malloc(size, caps);
  97. if (ptr != NULL) {
  98. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Allocated " + to_string(size) + " bytes in PSRAM for '" + name + "'");
  99. }
  100. else {
  101. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to allocate " + to_string(size) + " bytes in PSRAM for '" + name + "'!");
  102. }
  103. return ptr;
  104. }
  105. void *realloc_psram_heap(std::string name, void *ptr, size_t size, uint32_t caps) {
  106. ptr = heap_caps_realloc(ptr, size, caps);
  107. if (ptr != NULL) {
  108. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Reallocated " + to_string(size) + " bytes in PSRAM for '" + name + "'");
  109. }
  110. else {
  111. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to reallocate " + to_string(size) + " bytes in PSRAM for '" + name + "'!");
  112. }
  113. return ptr;
  114. }
  115. void *calloc_psram_heap(std::string name, size_t n, size_t size, uint32_t caps) {
  116. void *ptr;
  117. ptr = heap_caps_calloc(n, size, caps);
  118. if (ptr != NULL) {
  119. LogFile.WriteToFile(ESP_LOG_INFO, TAG, "Allocated " + to_string(size) + " bytes in PSRAM for '" + name + "'");
  120. }
  121. else {
  122. LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to allocate " + to_string(size) + " bytes in PSRAM for '" + name + "'!");
  123. }
  124. return ptr;
  125. }
  126. void free_psram_heap(std::string name, void *ptr) {
  127. LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "Freeing memory in PSRAM used for '" + name + "'...");
  128. heap_caps_free(ptr);
  129. }