bootloader_memory_utils.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * SPDX-FileCopyrightText: 2010-2023 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 IRAM and DRAM are separate or using the same memory space
  20. *
  21. * @return true if the DRAM and IRAM are sharing the same memory space, false otherwise
  22. */
  23. __attribute__((always_inline))
  24. inline static bool esp_dram_match_iram(void) {
  25. return (SOC_DRAM_LOW == SOC_IRAM_LOW && SOC_DRAM_HIGH == SOC_IRAM_HIGH);
  26. }
  27. /**
  28. * @brief Check if the pointer is in iram
  29. *
  30. * @param p pointer
  31. *
  32. * @return true: is in iram; false: not in iram
  33. */
  34. __attribute__((always_inline))
  35. inline static bool esp_ptr_in_iram(const void *p) {
  36. #if CONFIG_IDF_TARGET_ESP32 && CONFIG_FREERTOS_UNICORE
  37. return ((intptr_t)p >= SOC_CACHE_APP_LOW && (intptr_t)p < SOC_IRAM_HIGH);
  38. #else
  39. return ((intptr_t)p >= SOC_IRAM_LOW && (intptr_t)p < SOC_IRAM_HIGH);
  40. #endif
  41. }
  42. /**
  43. * @brief Check if the pointer is in dram
  44. *
  45. * @param p pointer
  46. *
  47. * @return true: is in dram; false: not in dram
  48. */
  49. __attribute__((always_inline))
  50. inline static bool esp_ptr_in_dram(const void *p) {
  51. return ((intptr_t)p >= SOC_DRAM_LOW && (intptr_t)p < SOC_DRAM_HIGH);
  52. }
  53. /**
  54. * @brief Check if the pointer is in diram_dram
  55. *
  56. * @param p pointer
  57. *
  58. * @return true: is in diram_dram; false: not in diram_dram
  59. */
  60. __attribute__((always_inline))
  61. inline static bool esp_ptr_in_diram_dram(const void *p) {
  62. return ((intptr_t)p >= SOC_DIRAM_DRAM_LOW && (intptr_t)p < SOC_DIRAM_DRAM_HIGH);
  63. }
  64. /**
  65. * @brief Check if the pointer is in diram_iram
  66. *
  67. * @param p pointer
  68. *
  69. * @return true: is in diram_iram; false: not in diram_iram
  70. */
  71. __attribute__((always_inline))
  72. inline static bool esp_ptr_in_diram_iram(const void *p) {
  73. // TODO: IDF-5980 esp32c6 D/I RAM share the same address
  74. #if SOC_DIRAM_IRAM_LOW == SOC_DIRAM_DRAM_LOW
  75. return false;
  76. #else
  77. return ((intptr_t)p >= SOC_DIRAM_IRAM_LOW && (intptr_t)p < SOC_DIRAM_IRAM_HIGH);
  78. #endif
  79. }
  80. /**
  81. * @brief Check if the pointer is in rtc_iram_fast
  82. *
  83. * @param p pointer
  84. *
  85. * @return true: is in rtc_iram_fast; false: not in rtc_iram_fast
  86. */
  87. __attribute__((always_inline))
  88. inline static bool esp_ptr_in_rtc_iram_fast(const void *p) {
  89. #if SOC_RTC_FAST_MEM_SUPPORTED
  90. return ((intptr_t)p >= SOC_RTC_IRAM_LOW && (intptr_t)p < SOC_RTC_IRAM_HIGH);
  91. #else
  92. return false;
  93. #endif
  94. }
  95. /**
  96. * @brief Check if the pointer is in rtc_dram_fast
  97. *
  98. * @param p pointer
  99. *
  100. * @return true: is in rtc_dram_fast; false: not in rtc_dram_fast
  101. */
  102. __attribute__((always_inline))
  103. inline static bool esp_ptr_in_rtc_dram_fast(const void *p) {
  104. #if SOC_RTC_FAST_MEM_SUPPORTED
  105. return ((intptr_t)p >= SOC_RTC_DRAM_LOW && (intptr_t)p < SOC_RTC_DRAM_HIGH);
  106. #else
  107. return false;
  108. #endif
  109. }
  110. /**
  111. * @brief Check if the pointer is in rtc_slow
  112. *
  113. * @param p pointer
  114. *
  115. * @return true: is in rtc_slow; false: not in rtc_slow
  116. */
  117. __attribute__((always_inline))
  118. inline static bool esp_ptr_in_rtc_slow(const void *p) {
  119. #if SOC_RTC_SLOW_MEM_SUPPORTED
  120. return ((intptr_t)p >= SOC_RTC_DATA_LOW && (intptr_t)p < SOC_RTC_DATA_HIGH);
  121. #else
  122. return false;
  123. #endif
  124. }
  125. /* Convert a D/IRAM DRAM pointer to equivalent word address in IRAM
  126. - Address must be word aligned
  127. - Address must pass esp_ptr_in_diram_dram() test, or result will be invalid pointer
  128. */
  129. __attribute__((always_inline))
  130. inline static void * esp_ptr_diram_dram_to_iram(const void *p) {
  131. #if SOC_DIRAM_INVERTED
  132. return (void *) ( SOC_DIRAM_IRAM_LOW + (SOC_DIRAM_DRAM_HIGH - (intptr_t)p) - 4);
  133. #else
  134. return (void *) ( SOC_DIRAM_IRAM_LOW + ((intptr_t)p - SOC_DIRAM_DRAM_LOW) );
  135. #endif
  136. }
  137. /* Convert a D/IRAM IRAM pointer to equivalent word address in DRAM
  138. - Address must be word aligned
  139. - Address must pass esp_ptr_in_diram_iram() test, or result will be invalid pointer
  140. */
  141. __attribute__((always_inline))
  142. inline static void * esp_ptr_diram_iram_to_dram(const void *p) {
  143. #if SOC_DIRAM_INVERTED
  144. return (void *) ( SOC_DIRAM_DRAM_LOW + (SOC_DIRAM_IRAM_HIGH - (intptr_t)p) - 4);
  145. #else
  146. return (void *) ( SOC_DIRAM_DRAM_LOW + ((intptr_t)p - SOC_DIRAM_IRAM_LOW) );
  147. #endif
  148. }
  149. #if SOC_MEM_TCM_SUPPORTED
  150. /**
  151. * @brief Check if the pointer is in TCM
  152. *
  153. * @param p pointer
  154. *
  155. * @return true: is in TCM; false: not in TCM
  156. */
  157. __attribute__((always_inline))
  158. inline static bool esp_ptr_in_tcm(const void *p) {
  159. return ((intptr_t)p >= SOC_TCM_LOW && (intptr_t)p < SOC_TCM_HIGH);
  160. }
  161. #endif //#if SOC_MEM_TCM_SUPPORTED
  162. /** End of the common section that has to be in sync with esp_memory_utils.h **/
  163. /** Don't add new functions below **/
  164. #ifdef __cplusplus
  165. }
  166. #endif