bootloader_memory_utils.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. return ((intptr_t)p >= SOC_DIRAM_IRAM_LOW && (intptr_t)p < SOC_DIRAM_IRAM_HIGH);
  65. }
  66. /**
  67. * @brief Check if the pointer is in rtc_iram_fast
  68. *
  69. * @param p pointer
  70. *
  71. * @return true: is in rtc_iram_fast; false: not in rtc_iram_fast
  72. */
  73. __attribute__((always_inline))
  74. inline static bool esp_ptr_in_rtc_iram_fast(const void *p) {
  75. #if SOC_RTC_FAST_MEM_SUPPORTED
  76. return ((intptr_t)p >= SOC_RTC_IRAM_LOW && (intptr_t)p < SOC_RTC_IRAM_HIGH);
  77. #else
  78. return false;
  79. #endif
  80. }
  81. /**
  82. * @brief Check if the pointer is in rtc_dram_fast
  83. *
  84. * @param p pointer
  85. *
  86. * @return true: is in rtc_dram_fast; false: not in rtc_dram_fast
  87. */
  88. __attribute__((always_inline))
  89. inline static bool esp_ptr_in_rtc_dram_fast(const void *p) {
  90. #if SOC_RTC_FAST_MEM_SUPPORTED
  91. return ((intptr_t)p >= SOC_RTC_DRAM_LOW && (intptr_t)p < SOC_RTC_DRAM_HIGH);
  92. #else
  93. return false;
  94. #endif
  95. }
  96. /**
  97. * @brief Check if the pointer is in rtc_slow
  98. *
  99. * @param p pointer
  100. *
  101. * @return true: is in rtc_slow; false: not in rtc_slow
  102. */
  103. __attribute__((always_inline))
  104. inline static bool esp_ptr_in_rtc_slow(const void *p) {
  105. #if SOC_RTC_SLOW_MEM_SUPPORTED
  106. return ((intptr_t)p >= SOC_RTC_DATA_LOW && (intptr_t)p < SOC_RTC_DATA_HIGH);
  107. #else
  108. return false;
  109. #endif
  110. }
  111. /* Convert a D/IRAM DRAM pointer to equivalent word address in IRAM
  112. - Address must be word aligned
  113. - Address must pass esp_ptr_in_diram_dram() test, or result will be invalid pointer
  114. */
  115. __attribute__((always_inline))
  116. inline static void * esp_ptr_diram_dram_to_iram(const void *p) {
  117. #if SOC_DIRAM_INVERTED
  118. return (void *) ( SOC_DIRAM_IRAM_LOW + (SOC_DIRAM_DRAM_HIGH - (intptr_t)p) - 4);
  119. #else
  120. return (void *) ( SOC_DIRAM_IRAM_LOW + ((intptr_t)p - SOC_DIRAM_DRAM_LOW) );
  121. #endif
  122. }
  123. /* Convert a D/IRAM IRAM pointer to equivalent word address in DRAM
  124. - Address must be word aligned
  125. - Address must pass esp_ptr_in_diram_iram() test, or result will be invalid pointer
  126. */
  127. __attribute__((always_inline))
  128. inline static void * esp_ptr_diram_iram_to_dram(const void *p) {
  129. #if SOC_DIRAM_INVERTED
  130. return (void *) ( SOC_DIRAM_DRAM_LOW + (SOC_DIRAM_IRAM_HIGH - (intptr_t)p) - 4);
  131. #else
  132. return (void *) ( SOC_DIRAM_DRAM_LOW + ((intptr_t)p - SOC_DIRAM_IRAM_LOW) );
  133. #endif
  134. }
  135. /** End of the common section that has to be in sync with esp_memory_utils.h **/
  136. /** Don't add new functions below **/
  137. #ifdef __cplusplus
  138. }
  139. #endif