Skip to content

Commit 7702511

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

File tree

111 files changed

+13277
-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

+13277
-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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
src/build/
1+
example/build/
2+
example/toolchain/

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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
rm "$filename"
18+
else
19+
echo "ESP8266 toolchain already exists. Skipping download."
20+
fi
21+
22+
cd ..
23+
}
24+
25+
build_target() {
26+
local target=$1
27+
echo "Building for $target..."
28+
mkdir -p build/$target
29+
cd build/$target
30+
31+
if [ "$target" = "esp8266" ]; then
32+
if [[ "$OSTYPE" == "windows"* ]]; then
33+
echo "ESP8266 build is only supported on unix-like systems. Skipping..."
34+
cd ../..
35+
return
36+
elif [[ "$OSTYPE" == "darwin"* ]]; then
37+
ESP8266_TOOLCHAIN_URL=$ESP8266_MACOS_TOOLCHAIN_URL
38+
else
39+
ESP8266_TOOLCHAIN_URL=$ESP8266_LINUX_TOOLCHAIN_URL
40+
fi
41+
42+
# Download and setup ESP8266 toolchain if needed
43+
if [ ! -d "../../toolchain/xtensa-lx106-elf" ]; then
44+
cd ../..
45+
download_esp8266_toolchain
46+
cd build/$target
47+
fi
48+
export PATH="$PWD/../../toolchain/xtensa-lx106-elf/bin:$PATH"
49+
fi
50+
51+
cmake -DESP_TARGET=$target ../..
52+
make
53+
cd ../..
54+
}
55+
56+
if [ "$1" = "all" ]; then
57+
# Build all targets
58+
for target in $TARGETS; do
59+
build_target $target
60+
done
61+
elif [ "$1" = "clean" ]; then
62+
rm -rf build
63+
else
64+
# Build specific target
65+
if [[ ! " $TARGETS " =~ " $1 " ]]; then
66+
echo "Usage: $0 <target|all|clean>"
67+
echo "Available targets: $TARGETS"
68+
exit 1
69+
fi
70+
build_target $1
71+
fi
72+
73+
# usage: ./build.sh all
74+
# usage: ./build.sh esp32
75+
# usage: ./build.sh clean

example/cmake/build-flags.cmake

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
set(COMMON_COMPILER_FLAGS
2+
-Wall
3+
-Werror
4+
-Wextra
5+
-Wshadow
6+
-Wundef
7+
-Wconversion
8+
-Os
9+
-nostdlib
10+
-fno-builtin
11+
-fno-common
12+
-g
13+
-ffunction-sections
14+
-fdata-sections
15+
-std=gnu17
16+
)
17+
18+
set(XTENSA_COMPILER_FLAGS
19+
${COMMON_COMPILER_FLAGS}
20+
-mlongcalls
21+
-mtext-section-literals
22+
-flto
23+
)
24+
25+
set(ESP8266_COMPILER_FLAGS
26+
${COMMON_COMPILER_FLAGS}
27+
-mlongcalls
28+
-mtext-section-literals
29+
-DESP8266
30+
)
31+
32+
set(RISCV_COMPILER_FLAGS
33+
${COMMON_COMPILER_FLAGS}
34+
-flto
35+
)
36+
37+
set(COMMON_LINKER_FLAGS
38+
"-nodefaultlibs"
39+
"-Wl,-static"
40+
"-Wl,--gc-sections"
41+
"-Wl,--start-group"
42+
"-lgcc"
43+
"-lc"
44+
"-Wl,--end-group"
45+
"-Wl,--undefined=s_esp_stub_desc"
46+
)
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)