esp_memory_utils.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #if CONFIG_SPIRAM
  15. #include "esp_private/esp_psram_extram.h"
  16. #endif
  17. bool esp_ptr_dma_ext_capable(const void *p)
  18. {
  19. #if !SOC_PSRAM_DMA_CAPABLE
  20. return false;
  21. #endif //!SOC_PSRAM_DMA_CAPABLE
  22. #if CONFIG_SPIRAM
  23. intptr_t vaddr_start = 0;
  24. intptr_t vaddr_end = 0;
  25. esp_psram_extram_get_mapped_range(&vaddr_start, &vaddr_end);
  26. return (intptr_t)p >= vaddr_start && (intptr_t)p < vaddr_end;
  27. #else
  28. return false;
  29. #endif //CONFIG_SPIRAM
  30. }
  31. bool esp_ptr_byte_accessible(const void *p)
  32. {
  33. intptr_t ip = (intptr_t) p;
  34. bool r;
  35. r = (ip >= SOC_BYTE_ACCESSIBLE_LOW && ip < SOC_BYTE_ACCESSIBLE_HIGH);
  36. #if CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  37. /* For ESP32 case, RTC fast memory is accessible to PRO cpu only and hence
  38. * for single core configuration (where it gets added to system heap) following
  39. * additional check is required */
  40. r |= (ip >= SOC_RTC_DRAM_LOW && ip < SOC_RTC_DRAM_HIGH);
  41. #endif
  42. #if CONFIG_SPIRAM
  43. intptr_t vaddr_start = 0;
  44. intptr_t vaddr_end = 0;
  45. esp_psram_extram_get_mapped_range(&vaddr_start, &vaddr_end);
  46. r |= (ip >= vaddr_start && ip < vaddr_end);
  47. #endif
  48. return r;
  49. }
  50. bool esp_ptr_external_ram(const void *p)
  51. {
  52. #if !SOC_SPIRAM_SUPPORTED
  53. return false;
  54. #endif //!SOC_SPIRAM_SUPPORTED
  55. #if CONFIG_SPIRAM
  56. intptr_t vaddr_start = 0;
  57. intptr_t vaddr_end = 0;
  58. esp_psram_extram_get_mapped_range(&vaddr_start, &vaddr_end);
  59. return (intptr_t)p >= vaddr_start && (intptr_t)p < vaddr_end;
  60. #else
  61. return false;
  62. #endif //CONFIG_SPIRAM
  63. }
  64. #if CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY
  65. bool esp_stack_ptr_in_extram(uint32_t sp)
  66. {
  67. intptr_t vaddr_start = 0;
  68. intptr_t vaddr_end = 0;
  69. esp_psram_extram_get_mapped_range(&vaddr_start, &vaddr_end);
  70. //Check if stack ptr is in between SOC_EXTRAM_DATA_LOW and SOC_EXTRAM_DATA_HIGH, and 16 byte aligned.
  71. return !(sp < vaddr_start + 0x10 || sp > vaddr_end - 0x10 || ((sp & 0xF) != 0));
  72. }
  73. #endif