Skip to content

Commit dd8dc00

Browse files
authored
Merge pull request #32 from espressif/dependency_inversion
Dependency inverted as common to target.
2 parents 4de6f9a + 709ef1e commit dd8dc00

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+282
-1234
lines changed

CMakeLists.txt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ if(NOT ESP_STUB_LIB)
77
endif()
88

99
if(NOT STUB_COMMAND)
10-
set(ESP_TARGET_LIB ${ESP_TARGET})
10+
set(ESP_BASE_LIB base)
1111
set(ESP_COMMON_LIB common)
12+
set(ESP_TARGET_LIB ${ESP_TARGET})
1213
else()
13-
set(ESP_TARGET_LIB ${ESP_TARGET}-${STUB_COMMAND})
14+
set(ESP_BASE_LIB base-${STUB_COMMAND})
1415
set(ESP_COMMON_LIB common-${STUB_COMMAND})
16+
set(ESP_TARGET_LIB ${ESP_TARGET}-${STUB_COMMAND})
1517
endif()
1618

1719
set(srcs
@@ -47,8 +49,20 @@ include_directories(include/esp-stub-lib)
4749
# STUB_COMPILE_DEFS is optional definitions coming from the parent CMakeLists.txt
4850
add_compile_definitions(${STUB_COMPILE_DEFS})
4951

52+
# Three-layer architecture:
53+
# - entry: Main stub code (src/)
54+
# - common: Generic implementations using base headers + target's soc.h + (weak functions)
55+
# - target: Target-specific code + soc.h + overrides (weak functions)
56+
# - base: Interface headers (target/*.h, private/*.h) - serves to common and target
57+
#
58+
# Dependency flow: entry → common → target
59+
# ↓ ↓
60+
# base base
61+
62+
add_subdirectory(src/target/base ${ESP_BASE_LIB})
5063
add_subdirectory(src/target/${ESP_TARGET} ${ESP_TARGET_LIB})
5164
add_subdirectory(src/target/common ${ESP_COMMON_LIB})
52-
target_link_libraries(${ESP_TARGET_LIB} PRIVATE ${ESP_COMMON_LIB})
5365

54-
target_link_libraries(${ESP_STUB_LIB} PUBLIC ${ESP_TARGET_LIB} ${ESP_COMMON_LIB})
66+
target_link_libraries(${ESP_TARGET_LIB} PUBLIC ${ESP_BASE_LIB})
67+
target_link_libraries(${ESP_COMMON_LIB} PUBLIC ${ESP_BASE_LIB} ${ESP_TARGET_LIB})
68+
target_link_libraries(${ESP_STUB_LIB} PUBLIC ${ESP_COMMON_LIB})

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This project is experimental and not yet ready for production use.
1616
- ESP32-C6
1717
- ESP32-C61
1818
- ESP32-H2
19+
- ESP32-H4
1920
- ESP32-P4
2021

2122
## How to use
@@ -30,6 +31,71 @@ A complete example project is provided in the [example](example/) directory. It
3031

3132
See the [example README](example/README.md) for build instructions.
3233

34+
## Project Structure
35+
36+
The library uses a three-layer architecture to eliminate circular dependencies and maximize code reuse:
37+
38+
```
39+
esp-stub-lib/
40+
├── include/esp-stub-lib/ # Public API - used by library clients
41+
│ ├── flash.h # (stub_lib_flash_init, stub_lib_flash_read, etc.)
42+
│ ├── log.h
43+
│ ├── mem_utils.h
44+
│ └── ...
45+
46+
├── src/ # Implementation layer
47+
│ ├── flash.c
48+
│ ├── mem_utils.c
49+
│ └── ...
50+
51+
└── src/target/ # Internal abstraction layers
52+
├── base/ # Interface layer (headers only)
53+
│ └── include/
54+
│ ├── target/ # Internal API between common/target layers
55+
│ └── private/ # Internal ROM/hardware details
56+
57+
├── common/ # Generic implementations
58+
│ ├── src/ # Weak functions using SOC_* macros
59+
│ │ ├── mem_utils.c # Default implementations
60+
│ │ ├── uart.c
61+
│ │ └── flash.c
62+
│ └── CMakeLists.txt
63+
64+
└── esp32*/ # Target-specific implementations
65+
├── include/
66+
│ └── soc/
67+
│ └── soc.h # SOC_* macro definitions
68+
├── src/
69+
│ ├── mem_utils.c # Strong overrides (optional)
70+
│ ├── uart.c
71+
│ └── ...
72+
└── CMakeLists.txt
73+
```
74+
75+
**Dependency Flow:**
76+
```
77+
Public API (include/esp-stub-lib/) ← Library clients use this
78+
79+
Implementation (src/)
80+
81+
Common (generic implementations with weak functions)
82+
83+
Target (overridden weak functions for target-specific implementations)
84+
85+
Base (interface headers only - serves both common and target)
86+
```
87+
88+
**Layer Purposes:**
89+
- **include/esp-stub-lib/**: Public API for library clients (e.g., `stub_lib_flash_init()`)
90+
- **src/**: Implementation layer that uses the target abstraction below
91+
- **base/**: Internal interface layer - shared by both common and target implementations
92+
- `target/`: Internal API between common/target layers (e.g., `stub_target_flash_init()`)
93+
- `private/`: Internal headers to be used from target implementations. (e.g., `esp_rom_spiflash_read()`)
94+
- **common/**: Provides reusable weak implementations that work across targets
95+
- **target/esp32***: Target-specific constants (`soc.h`) and strong function overrides when needed
96+
97+
> **Note**: Library clients should only include headers from `include/esp-stub-lib/`. The `src/target/base/` folder (both `target/` and `private/`) contains internal implementation details used by the library itself.
98+
3399
## Contributing
34100

35101
Please install the [pre-commit](https://pre-commit.com/) hooks to ensure that your commits are properly formatted:

src/target/base/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Base layer: Interface library with headers only (no implementation)
2+
add_library(${ESP_BASE_LIB} INTERFACE)
3+
4+
target_include_directories(${ESP_BASE_LIB}
5+
INTERFACE include
6+
)
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)