psram.cpp 6.8 KB

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