esp_memory_utils.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include "sdkconfig.h"
  10. #include "soc/soc.h"
  11. #include "soc/soc_caps.h"
  12. #include "esp_attr.h"
  13. #include "esp_memory_utils.h"
  14. //TODO: IDF-4855, replace PSRAM related address region into PSRAM private APIs
  15. bool esp_ptr_dma_ext_capable(const void *p)
  16. {
  17. #ifdef SOC_PSRAM_DMA_CAPABLE
  18. return (intptr_t)p >= SOC_DMA_EXT_LOW && (intptr_t)p < SOC_DMA_EXT_HIGH;
  19. #else
  20. return false;
  21. #endif
  22. }
  23. bool esp_ptr_byte_accessible(const void *p)
  24. {
  25. intptr_t ip = (intptr_t) p;
  26. bool r;
  27. r = (ip >= SOC_BYTE_ACCESSIBLE_LOW && ip < SOC_BYTE_ACCESSIBLE_HIGH);
  28. #if CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  29. /* For ESP32 case, RTC fast memory is accessible to PRO cpu only and hence
  30. * for single core configuration (where it gets added to system heap) following
  31. * additional check is required */
  32. r |= (ip >= SOC_RTC_DRAM_LOW && ip < SOC_RTC_DRAM_HIGH);
  33. #endif
  34. #if CONFIG_SPIRAM
  35. #if CONFIG_SPIRAM_SIZE != -1 // Fixed size, can be more accurate
  36. r |= (ip >= SOC_EXTRAM_DATA_LOW && ip < (SOC_EXTRAM_DATA_LOW + CONFIG_SPIRAM_SIZE));
  37. #else
  38. r |= (ip >= SOC_EXTRAM_DATA_LOW && ip < (SOC_EXTRAM_DATA_HIGH));
  39. #endif
  40. #endif
  41. return r;
  42. }
  43. bool esp_ptr_external_ram(const void *p) {
  44. #if SOC_SPIRAM_SUPPORTED
  45. return ((intptr_t)p >= SOC_EXTRAM_DATA_LOW && (intptr_t)p < SOC_EXTRAM_DATA_HIGH);
  46. #else
  47. return false; // SoC has no external RAM
  48. #endif
  49. }
  50. #if CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
  51. bool esp_stack_ptr_in_extram(uint32_t sp)
  52. {
  53. //Check if stack ptr is in between SOC_EXTRAM_DATA_LOW and SOC_EXTRAM_DATA_HIGH, and 16 byte aligned.
  54. return !(sp < SOC_EXTRAM_DATA_LOW + 0x10 || sp > SOC_EXTRAM_DATA_HIGH - 0x10 || ((sp & 0xF) != 0));
  55. }
  56. #endif