esp_memory_utils.c 2.1 KB

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