bootloader_memory_utils.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * SPDX-FileCopyrightText: 2010-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdlib.h>
  8. #include <stdint.h>
  9. #include <stdbool.h>
  10. #include "soc/soc.h"
  11. #include "soc/soc_caps.h"
  12. #include "sdkconfig.h"
  13. #include "esp_attr.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /** The content of this file is to be kept in sync with the common section of esp_memory_utils.h **/
  18. /**
  19. * @brief Check if the pointer is in iram
  20. *
  21. * @param p pointer
  22. *
  23. * @return true: is in iram; false: not in iram
  24. */
  25. __attribute__((always_inline))
  26. inline static bool esp_ptr_in_iram(const void *p) {
  27. #if CONFIG_IDF_TARGET_ESP32 && CONFIG_FREERTOS_UNICORE
  28. return ((intptr_t)p >= SOC_CACHE_APP_LOW && (intptr_t)p < SOC_IRAM_HIGH);
  29. #else
  30. return ((intptr_t)p >= SOC_IRAM_LOW && (intptr_t)p < SOC_IRAM_HIGH);
  31. #endif
  32. }
  33. /**
  34. * @brief Check if the pointer is in dram
  35. *
  36. * @param p pointer
  37. *
  38. * @return true: is in dram; false: not in dram
  39. */
  40. __attribute__((always_inline))
  41. inline static bool esp_ptr_in_dram(const void *p) {
  42. return ((intptr_t)p >= SOC_DRAM_LOW && (intptr_t)p < SOC_DRAM_HIGH);
  43. }
  44. /**
  45. * @brief Check if the pointer is in diram_dram
  46. *
  47. * @param p pointer
  48. *
  49. * @return true: is in diram_dram; false: not in diram_dram
  50. */
  51. __attribute__((always_inline))
  52. inline static bool esp_ptr_in_diram_dram(const void *p) {
  53. return ((intptr_t)p >= SOC_DIRAM_DRAM_LOW && (intptr_t)p < SOC_DIRAM_DRAM_HIGH);
  54. }
  55. /**
  56. * @brief Check if the pointer is in diram_iram
  57. *
  58. * @param p pointer
  59. *
  60. * @return true: is in diram_iram; false: not in diram_iram
  61. */
  62. __attribute__((always_inline))
  63. inline static bool esp_ptr_in_diram_iram(const void *p) {
  64. // TODO: IDF-5980 esp32c6 D/I RAM share the same address
  65. #if SOC_DIRAM_IRAM_LOW == SOC_DIRAM_DRAM_LOW
  66. return false;
  67. #else
  68. return ((intptr_t)p >= SOC_DIRAM_IRAM_LOW && (intptr_t)p < SOC_DIRAM_IRAM_HIGH);
  69. #endif
  70. }
  71. /**
  72. * @brief Check if the pointer is in rtc_iram_fast
  73. *
  74. * @param p pointer
  75. *
  76. * @return true: is in rtc_iram_fast; false: not in rtc_iram_fast
  77. */
  78. __attribute__((always_inline))
  79. inline static bool esp_ptr_in_rtc_iram_fast(const void *p) {
  80. #if SOC_RTC_FAST_MEM_SUPPORTED
  81. return ((intptr_t)p >= SOC_RTC_IRAM_LOW && (intptr_t)p < SOC_RTC_IRAM_HIGH);
  82. #else
  83. return false;
  84. #endif
  85. }
  86. /**
  87. * @brief Check if the pointer is in rtc_dram_fast
  88. *
  89. * @param p pointer
  90. *
  91. * @return true: is in rtc_dram_fast; false: not in rtc_dram_fast
  92. */
  93. __attribute__((always_inline))
  94. inline static bool esp_ptr_in_rtc_dram_fast(const void *p) {
  95. #if SOC_RTC_FAST_MEM_SUPPORTED
  96. return ((intptr_t)p >= SOC_RTC_DRAM_LOW && (intptr_t)p < SOC_RTC_DRAM_HIGH);
  97. #else
  98. return false;
  99. #endif
  100. }
  101. /**
  102. * @brief Check if the pointer is in rtc_slow
  103. *
  104. * @param p pointer
  105. *
  106. * @return true: is in rtc_slow; false: not in rtc_slow
  107. */
  108. __attribute__((always_inline))
  109. inline static bool esp_ptr_in_rtc_slow(const void *p) {
  110. #if SOC_RTC_SLOW_MEM_SUPPORTED
  111. return ((intptr_t)p >= SOC_RTC_DATA_LOW && (intptr_t)p < SOC_RTC_DATA_HIGH);
  112. #else
  113. return false;
  114. #endif
  115. }
  116. /* Convert a D/IRAM DRAM pointer to equivalent word address in IRAM
  117. - Address must be word aligned
  118. - Address must pass esp_ptr_in_diram_dram() test, or result will be invalid pointer
  119. */
  120. __attribute__((always_inline))
  121. inline static void * esp_ptr_diram_dram_to_iram(const void *p) {
  122. #if SOC_DIRAM_INVERTED
  123. return (void *) ( SOC_DIRAM_IRAM_LOW + (SOC_DIRAM_DRAM_HIGH - (intptr_t)p) - 4);
  124. #else
  125. return (void *) ( SOC_DIRAM_IRAM_LOW + ((intptr_t)p - SOC_DIRAM_DRAM_LOW) );
  126. #endif
  127. }
  128. /* Convert a D/IRAM IRAM pointer to equivalent word address in DRAM
  129. - Address must be word aligned
  130. - Address must pass esp_ptr_in_diram_iram() test, or result will be invalid pointer
  131. */
  132. __attribute__((always_inline))
  133. inline static void * esp_ptr_diram_iram_to_dram(const void *p) {
  134. #if SOC_DIRAM_INVERTED
  135. return (void *) ( SOC_DIRAM_DRAM_LOW + (SOC_DIRAM_IRAM_HIGH - (intptr_t)p) - 4);
  136. #else
  137. return (void *) ( SOC_DIRAM_DRAM_LOW + ((intptr_t)p - SOC_DIRAM_IRAM_LOW) );
  138. #endif
  139. }
  140. /** End of the common section that has to be in sync with esp_memory_utils.h **/
  141. /** Don't add new functions below **/
  142. #ifdef __cplusplus
  143. }
  144. #endif