Skip to content

Commit 8fa96a0

Browse files
committed
change: add initial source/build structure
1 parent 2701ef8 commit 8fa96a0

File tree

111 files changed

+13198
-5
lines changed

Some content is hidden

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

111 files changed

+13198
-5
lines changed

.check_copyright_config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ DEFAULT:
1313
*/
1414
1515
espressif_copyright: '{years} Espressif Systems (Shanghai) CO LTD'
16+
17+
ignore:
18+
perform_check: false
19+
include:
20+
- src/*/ld/*.ld
21+
- example/ld/*.ld
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
name: Build Example
3+
4+
on:
5+
push:
6+
branches-ignore: [master]
7+
pull_request:
8+
branches: [master]
9+
workflow_dispatch: {}
10+
11+
jobs:
12+
build:
13+
name: Build
14+
runs-on: ubuntu-latest
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
espidf_target: [esp32, esp32s2, esp32s3, esp32c2, esp32c3, esp32c5, esp32c6, esp32c61, esp32h2, esp32p4]
19+
steps:
20+
- name: Checkout repository
21+
uses: actions/checkout@v4
22+
with:
23+
submodules: 'recursive'
24+
25+
- name: Build targets with ESP-IDF
26+
uses: espressif/esp-idf-ci-action@v1
27+
with:
28+
esp_idf_version: "latest"
29+
target: ${{ matrix.espidf_target }}
30+
path: 'example'
31+
command: |
32+
mkdir -p build && cd build
33+
cmake -DESP_TARGET=${{ matrix.espidf_target }} -GNinja ..
34+
ninja
35+
36+
- name: Upload build artifacts
37+
uses: actions/upload-artifact@v4
38+
with:
39+
name: stub_${{ matrix.espidf_target }}_artifacts
40+
path: |
41+
example/build/stub_${{ matrix.espidf_target }}.elf
42+
example/build/stub_${{ matrix.espidf_target }}.map
43+
example/build/stub_${{ matrix.espidf_target }}.asm
44+
if-no-files-found: error
45+
46+
build-esp8266:
47+
name: Build (esp8266)
48+
runs-on: ubuntu-latest
49+
steps:
50+
- name: Checkout repository
51+
uses: actions/checkout@v4
52+
with:
53+
submodules: 'recursive'
54+
55+
- name: Build ESP8266 with custom toolchain
56+
run: |
57+
cd example
58+
./build.sh esp8266
59+
60+
- name: Upload ESP8266 build artifacts
61+
uses: actions/upload-artifact@v4
62+
with:
63+
name: stub_esp8266_artifacts
64+
path: |
65+
example/build/esp8266/stub_esp8266.elf
66+
example/build/esp8266/stub_esp8266.map
67+
example/build/esp8266/stub_esp8266.asm
68+
if-no-files-found: error

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
src/build/
1+
example/build/

CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
set(srcs
3+
src/log.c
4+
src/flash.c
5+
)
6+
7+
add_library(esp-stub-lib STATIC ${srcs})
8+
9+
target_include_directories(esp-stub-lib
10+
PUBLIC include
11+
PRIVATE include/esp-stub-lib
12+
)
13+
14+
add_subdirectory(src/${ESP_TARGET})
15+
16+
target_link_libraries(esp-stub-lib PUBLIC ${ESP_TARGET})

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,31 @@
44

55
This project is experimental and not yet ready for production use.
66

7-
8-
# How To Use
9-
10-
TODO
7+
## Supported Targets
8+
9+
- ESP8266
10+
- ESP32
11+
- ESP32-S2
12+
- ESP32-S3
13+
- ESP32-C2
14+
- ESP32-C3
15+
- ESP32-C5
16+
- ESP32-C6
17+
- ESP32-C61
18+
- ESP32-H2
19+
- ESP32-P4
20+
21+
## How to use
22+
23+
The library provides a simple interface for creating stubs that can be loaded onto ESP chips.
24+
25+
A complete example project is provided in the [example](example/) directory. It demonstrates:
26+
- Basic stub implementation
27+
- Flash operations
28+
- Target-specific configurations
29+
- Build system integration
30+
31+
See the [example README](example/README.md) for build instructions.
1132

1233
## Contributing
1334

example/CMakeLists.txt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
cmake_minimum_required(VERSION 3.27)
2+
3+
include(${CMAKE_CURRENT_LIST_DIR}/cmake/util.cmake)
4+
5+
validate_esp_target(${ESP_TARGET})
6+
7+
include(${CMAKE_CURRENT_LIST_DIR}/cmake/build-flags.cmake)
8+
9+
# Setup toolchain and flags
10+
setup_toolchain(${ESP_TARGET} "${XTENSA_TARGETS}")
11+
12+
# Add compiler and linker flags to all targets
13+
add_compile_options(${TARGET_COMPILER_FLAGS})
14+
add_link_options(${COMMON_LINKER_FLAGS})
15+
16+
# Project setup
17+
set(APP_NAME stub_${ESP_TARGET})
18+
project(${APP_NAME}.elf LANGUAGES C CXX ASM)
19+
20+
# Source and linker configuration
21+
set(SRC_DIR .)
22+
set(LINKER_SCRIPTS
23+
${CMAKE_CURRENT_LIST_DIR}/ld/${ESP_TARGET}.ld
24+
${CMAKE_CURRENT_LIST_DIR}/ld/common.ld
25+
)
26+
file(GLOB SRC_FILES "${SRC_DIR}/*.c" "${SRC_DIR}/*.S")
27+
28+
# Target configuration
29+
add_executable(${PROJECT_NAME} ${SRC_FILES})
30+
target_include_directories(${PROJECT_NAME}
31+
PRIVATE include
32+
)
33+
foreach(script ${LINKER_SCRIPTS})
34+
target_link_options(${PROJECT_NAME} PRIVATE -T${script})
35+
endforeach()
36+
37+
if(${ESP_TARGET} STREQUAL "esp8266")
38+
target_link_options(${PROJECT_NAME} PRIVATE -Wl,--entry=stub_main_esp8266)
39+
endif()
40+
41+
target_compile_definitions(${PROJECT_NAME} PRIVATE asm=__asm__)
42+
43+
set(MAP_FILE ${CMAKE_CURRENT_BINARY_DIR}/${APP_NAME}.map)
44+
target_link_options(${PROJECT_NAME} PRIVATE -Wl,-Map=${MAP_FILE})
45+
set_directory_properties(PROPERTIES ADDITIONAL_CLEAN_FILES ${MAP_FILE})
46+
47+
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
48+
COMMAND ${CMAKE_SIZE} ${PROJECT_NAME}
49+
COMMAND ${CMAKE_OBJDUMP} -d $<TARGET_FILE:${APP_NAME}.elf> -M no-aliases > ${CMAKE_BINARY_DIR}/${APP_NAME}.asm
50+
COMMENT ""
51+
)
52+
53+
get_filename_component(PROJECT_ROOT ${CMAKE_CURRENT_LIST_DIR}/.. ABSOLUTE)
54+
add_subdirectory(${PROJECT_ROOT} esp-stub-lib)
55+
target_link_libraries(${PROJECT_NAME} PRIVATE esp-stub-lib)

example/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## Building the Example
2+
3+
### Using build script
4+
5+
The simplest way to build is using the provided build script:
6+
7+
```bash
8+
# Build for all supported targets
9+
./build.sh all
10+
11+
# Or specify a different target
12+
./build.sh esp32s2
13+
```
14+
15+
### Manual Build
16+
17+
1. Create and enter build directory:
18+
```bash
19+
cd example
20+
mkdir -p build && cd build
21+
```
22+
23+
2. Configure CMake with your ESP target:
24+
```bash
25+
cmake -DESP_TARGET=esp32 .. # Replace esp32 with your target (esp32s2, etc.)
26+
```
27+
28+
3. Build the project:
29+
```bash
30+
cmake --build .
31+
```
32+
33+
## Build Outputs
34+
35+
After a successful build, you'll find:
36+
- `build/stub_<target>.elf` - The compiled binary
37+
- `build/stub_<target>.map` - Memory map file
38+
- `build/stub_<target>.asm` - Disassembly output
39+
40+
## Notes
41+
42+
- Make sure you have exported the esp-idf environment with the correct toolchain for your target ESP chip. https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html
43+
- The example assumes the parent directory contains the ESP stub library

example/build.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
3+
TARGETS="esp8266 esp32 esp32s2 esp32s3 esp32c2 esp32c3 esp32c5 esp32c6 esp32c61 esp32h2 esp32p4"
4+
5+
ESP8266_LINUX_TOOLCHAIN_URL="https://dl.espressif.com/dl/xtensa-lx106-elf-gcc8_4_0-esp-2020r3-linux-amd64.tar.gz"
6+
ESP8266_MACOS_TOOLCHAIN_URL="https://dl.espressif.com/dl/xtensa-lx106-elf-gcc8_4_0-esp-2020r3-macos.tar.gz"
7+
8+
download_esp8266_toolchain() {
9+
mkdir -p toolchain
10+
cd toolchain
11+
12+
filename=$(basename "$ESP8266_TOOLCHAIN_URL")
13+
if [ ! -f "$filename" ]; then
14+
echo "Downloading ESP8266 toolchain... $ESP8266_TOOLCHAIN_URL"
15+
wget "$ESP8266_TOOLCHAIN_URL" --no-verbose
16+
tar -xzf "$filename"
17+
else
18+
echo "ESP8266 toolchain already exists. Skipping download."
19+
fi
20+
21+
cd ..
22+
}
23+
24+
build_target() {
25+
local target=$1
26+
echo "Building for $target..."
27+
mkdir -p build/$target
28+
cd build/$target
29+
30+
if [ "$target" = "esp8266" ]; then
31+
if [[ "$OSTYPE" == "windows"* ]]; then
32+
echo "ESP8266 build is only supported on unix-like systems. Skipping..."
33+
cd ../..
34+
return
35+
elif [[ "$OSTYPE" == "darwin"* ]]; then
36+
ESP8266_TOOLCHAIN_URL=$ESP8266_MACOS_TOOLCHAIN_URL
37+
else
38+
ESP8266_TOOLCHAIN_URL=$ESP8266_LINUX_TOOLCHAIN_URL
39+
fi
40+
41+
# Download and setup ESP8266 toolchain if needed
42+
if [ ! -d "../../toolchain/xtensa-lx106-elf" ]; then
43+
download_esp8266_toolchain
44+
fi
45+
export PATH="$PWD/toolchain/xtensa-lx106-elf/bin:$PATH"
46+
fi
47+
48+
cmake -DESP_TARGET=$target ../..
49+
make
50+
cd ../..
51+
}
52+
53+
if [ "$1" = "all" ]; then
54+
# Build all targets
55+
for target in $TARGETS; do
56+
build_target $target
57+
done
58+
elif [ "$1" = "clean" ]; then
59+
rm -rf build
60+
else
61+
# Build specific target
62+
if [[ ! " $TARGETS " =~ " $1 " ]]; then
63+
echo "Usage: $0 <target|all>"
64+
echo "Available targets: $TARGETS"
65+
exit 1
66+
fi
67+
build_target $1
68+
fi
69+
70+
# usage: ./build.sh all
71+
# usage: ./build.sh esp32
72+
# usage: ./build.sh clean

example/cmake/build-flags.cmake

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
set(COMMON_COMPILER_FLAGS
2+
-Wall
3+
-Werror
4+
-Os
5+
-nostdlib
6+
-fno-builtin
7+
-g
8+
-ffunction-sections
9+
-fdata-sections
10+
-std=gnu17
11+
)
12+
13+
set(XTENSA_COMPILER_FLAGS
14+
${COMMON_COMPILER_FLAGS}
15+
-mlongcalls
16+
-mtext-section-literals
17+
-flto
18+
)
19+
20+
set(ESP8266_COMPILER_FLAGS
21+
${COMMON_COMPILER_FLAGS}
22+
-mlongcalls
23+
-mtext-section-literals
24+
-DESP8266
25+
)
26+
27+
set(RISCV_COMPILER_FLAGS
28+
${COMMON_COMPILER_FLAGS}
29+
-flto
30+
)
31+
32+
set(COMMON_LINKER_FLAGS
33+
"-nostartfiles"
34+
"-nodefaultlibs"
35+
"-Wl,-static"
36+
"-Wl,--gc-sections"
37+
"-Wl,--start-group"
38+
"-lgcc"
39+
"-lc"
40+
"-Wl,--end-group"
41+
"-Wl,--undefined=s_esp_stub_desc"
42+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
set(CMAKE_SYSTEM_NAME Generic)
2+
set(CMAKE_SYSTEM_VERSION 1)
3+
set(CMAKE_SYSTEM_PROCESSOR XTENSA)
4+
5+
set(CMAKE_TOOLCHAIN_PREFIX xtensa-lx106-elf)
6+
set(CMAKE_C_COMPILER ${CMAKE_TOOLCHAIN_PREFIX}-gcc)
7+
set(CMAKE_CXX_COMPILER ${CMAKE_TOOLCHAIN_PREFIX}-g++)
8+
set(CMAKE_ASM_COMPILER ${CMAKE_TOOLCHAIN_PREFIX}-gcc)
9+
set(CMAKE_OBJCOPY ${CMAKE_TOOLCHAIN_PREFIX}-objcopy)
10+
set(CMAKE_OBJDUMP ${CMAKE_TOOLCHAIN_PREFIX}-objdump)
11+
set(CMAKE_READELF ${CMAKE_TOOLCHAIN_PREFIX}-readelf)
12+
set(CMAKE_SIZE ${CMAKE_TOOLCHAIN_PREFIX}-size)

0 commit comments

Comments
 (0)