Skip to content

Commit c7cd9ba

Browse files
committed
feat: add mem utils, for checking memory range types
1 parent 73037e3 commit c7cd9ba

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ endif()
1616

1717
set(srcs
1818
src/flash.c
19+
src/mem_utils.c
1920
src/rom_wrappers.c
2021
)
2122

include/esp-stub-lib/mem_utils.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0 OR MIT
5+
*/
6+
7+
#pragma once
8+
9+
#include <stdbool.h>
10+
11+
/**
12+
* @brief Check if address is in external memory range.
13+
*
14+
* External flash memory mapped via the instruction bus.
15+
*/
16+
bool stub_lib_mem_is_irom(const void *p);
17+
18+
/**
19+
* @brief Check if address is in external memory range.
20+
*
21+
* External flash memory mapped via the data bus.
22+
*/
23+
bool stub_lib_mem_is_drom(const void *p);
24+
25+
/**
26+
* @brief Check if address is in internal memory range.
27+
*
28+
* Internal memory accessed via the instruction bus.
29+
*/
30+
bool stub_lib_mem_is_iram(const void *p);
31+
32+
/**
33+
* @brief Check if address is in internal memory range.
34+
*
35+
* Internal memory accessed via the data bus.
36+
*/
37+
bool stub_lib_mem_is_dram(const void *p);
38+
39+
bool stub_lib_mem_is_rtc_iram_fast(const void *p);
40+
bool stub_lib_mem_is_rtc_dram_fast(const void *p);
41+
bool stub_lib_mem_is_rtc_slow(const void *p);
42+
43+
bool stub_lib_mem_is_tcm(const void *p);

src/mem_utils.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0 OR MIT
5+
*/
6+
#include <stdbool.h>
7+
#include <stdint.h>
8+
9+
#include <mem_utils.h>
10+
11+
#include <soc/soc.h>
12+
13+
bool stub_lib_mem_is_irom(const void *p)
14+
{
15+
#if defined(SOC_IROM_LOW) && defined(SOC_IROM_HIGH)
16+
return (uintptr_t)p >= SOC_IROM_LOW && (uintptr_t)p < SOC_IROM_HIGH;
17+
#else
18+
return false;
19+
#endif
20+
}
21+
22+
bool stub_lib_mem_is_drom(const void *p)
23+
{
24+
#if defined(SOC_DROM_LOW) && defined(SOC_DROM_HIGH)
25+
return (uintptr_t)p >= SOC_DROM_LOW && (uintptr_t)p < SOC_DROM_HIGH;
26+
#else
27+
return false;
28+
#endif
29+
}
30+
31+
bool stub_lib_mem_is_iram(const void *p)
32+
{
33+
#if defined(SOC_IRAM_LOW) && defined(SOC_IRAM_HIGH)
34+
return (uintptr_t)p >= SOC_IRAM_LOW && (uintptr_t)p < SOC_IRAM_HIGH;
35+
#else
36+
return false;
37+
#endif
38+
}
39+
40+
bool stub_lib_mem_is_dram(const void *p)
41+
{
42+
#if defined(SOC_DRAM_LOW) && defined(SOC_DRAM_HIGH)
43+
return (uintptr_t)p >= SOC_DRAM_LOW && (uintptr_t)p < SOC_DRAM_HIGH;
44+
#else
45+
return false;
46+
#endif
47+
}
48+
49+
bool stub_lib_mem_is_rtc_iram_fast(const void *p)
50+
{
51+
#if defined(SOC_RTC_IRAM_LOW) && defined(SOC_RTC_IRAM_HIGH)
52+
return (uintptr_t)p >= SOC_RTC_IRAM_LOW && (uintptr_t)p < SOC_RTC_IRAM_HIGH;
53+
#else
54+
(void)p;
55+
return false;
56+
#endif
57+
}
58+
59+
bool stub_lib_mem_is_rtc_dram_fast(const void *p)
60+
{
61+
#if defined(SOC_RTC_DRAM_LOW) && defined(SOC_RTC_DRAM_HIGH)
62+
return (uintptr_t)p >= SOC_RTC_DRAM_LOW && (uintptr_t)p < SOC_RTC_DRAM_HIGH;
63+
#else
64+
(void)p;
65+
return false;
66+
#endif
67+
68+
}
69+
70+
bool stub_lib_mem_is_rtc_slow(const void *p)
71+
{
72+
#if defined(SOC_RTC_DATA_LOW) && defined(SOC_RTC_DATA_HIGH)
73+
return (uintptr_t)p >= SOC_RTC_DATA_LOW && (uintptr_t)p < SOC_RTC_DATA_HIGH;
74+
#else
75+
(void)p;
76+
return false;
77+
#endif
78+
}
79+
80+
bool stub_lib_mem_is_tcm(const void *p)
81+
{
82+
#if defined(SOC_TCM_LOW) && defined(SOC_TCM_HIGH)
83+
return ((uintptr_t)p >= SOC_TCM_LOW && (uintptr_t)p < SOC_TCM_HIGH);
84+
#else
85+
(void)p;
86+
return false;
87+
#endif
88+
}

0 commit comments

Comments
 (0)