psram.cpp 7.9 KB

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