esp_memory_utils.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. return esp_psram_check_ptr_addr(p);
  24. #else
  25. return false;
  26. #endif //CONFIG_SPIRAM
  27. }
  28. bool esp_ptr_byte_accessible(const void *p)
  29. {
  30. intptr_t ip = (intptr_t) p;
  31. bool r;
  32. r = (ip >= SOC_BYTE_ACCESSIBLE_LOW && ip < SOC_BYTE_ACCESSIBLE_HIGH);
  33. #if CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP
  34. /* For ESP32 case, RTC fast memory is accessible to PRO cpu only and hence
  35. * for single core configuration (where it gets added to system heap) following
  36. * additional check is required */
  37. r |= (ip >= SOC_RTC_DRAM_LOW && ip < SOC_RTC_DRAM_HIGH);
  38. #endif
  39. #if CONFIG_SPIRAM
  40. r |= esp_psram_check_ptr_addr(p);
  41. #endif
  42. #if CONFIG_ESP32S3_DATA_CACHE_16KB
  43. /* For ESP32-S3, when the DCACHE size is set to 16 kB, the unused 48 kB is
  44. * added to the heap in 2 blocks of 32 kB (from 0x3FCF0000) and 16 kB
  45. * (from 0x3C000000 (SOC_DROM_LOW) - 0x3C004000).
  46. * Though this memory lies in the external memory vaddr, it is no different
  47. * from the internal RAM in terms of hardware attributes. It is a part of
  48. * the internal RAM when added to the heap and is byte-accessible .*/
  49. r |= (ip >= SOC_DROM_LOW && ip < (SOC_DROM_LOW + 0x4000));
  50. #endif
  51. return r;
  52. }
  53. bool esp_ptr_external_ram(const void *p)
  54. {
  55. #if !SOC_SPIRAM_SUPPORTED
  56. return false;
  57. #endif //!SOC_SPIRAM_SUPPORTED
  58. #if CONFIG_SPIRAM
  59. return esp_psram_check_ptr_addr(p);
  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. //Check if stack ptr is on PSRAM, and 16 byte aligned.
  68. return (esp_psram_check_ptr_addr((void *)sp) && ((sp & 0xF) == 0));
  69. }
  70. #endif