first commit.

This commit is contained in:
2024-01-15 00:14:47 +08:00
commit 8f45032f34
16 changed files with 392 additions and 0 deletions

17
.cargo/config.toml Normal file
View File

@ -0,0 +1,17 @@
[build]
target = "riscv32imc-esp-espidf"
[target.riscv32imc-esp-espidf]
linker = "ldproxy"
# runner = "espflash --monitor" # Select this runner for espflash v1.x.x
runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x
rustflags = ["--cfg", "espidf_time64", "-C", "default-linker-libraries"]
[unstable]
build-std = ["std", "panic_abort"]
[env]
MCU="esp32c3"
# Note: this variable is not used by the pio builder (`cargo build --features pio`)
ESP_IDF_VERSION = "v5.1.1"

66
.devcontainer/Dockerfile Normal file
View File

@ -0,0 +1,66 @@
ARG VARIANT=bookworm-slim
FROM debian:${VARIANT}
ENV DEBIAN_FRONTEND=noninteractive
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8
# Arguments
ARG CONTAINER_USER=esp
ARG CONTAINER_GROUP=esp
ARG ESP_BOARD=all
ARG GITHUB_TOKEN
# Install dependencies
RUN apt-get update \
&& apt-get install -y pkg-config curl gcc clang libudev-dev unzip xz-utils \
git wget flex bison gperf python3 python3-pip python3-venv cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0 \
&& apt-get clean -y && rm -rf /var/lib/apt/lists/* /tmp/library-scripts
# Set users
RUN adduser --disabled-password --gecos "" ${CONTAINER_USER}
USER ${CONTAINER_USER}
WORKDIR /home/${CONTAINER_USER}
# Install rustup
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- \
--default-toolchain none -y --profile minimal
# Update envs
ENV PATH=${PATH}:/home/${CONTAINER_USER}/.cargo/bin
# Install extra crates
RUN ARCH=$($HOME/.cargo/bin/rustup show | grep "Default host" | sed -e 's/.* //') && \
curl -L "https://github.com/esp-rs/espup/releases/latest/download/espup-${ARCH}" -o "${HOME}/.cargo/bin/espup" && \
chmod u+x "${HOME}/.cargo/bin/espup" && \
curl -L "https://github.com/esp-rs/espflash/releases/latest/download/cargo-espflash-${ARCH}.zip" -o "${HOME}/.cargo/bin/cargo-espflash.zip" && \
unzip "${HOME}/.cargo/bin/cargo-espflash.zip" -d "${HOME}/.cargo/bin/" && \
rm "${HOME}/.cargo/bin/cargo-espflash.zip" && \
chmod u+x "${HOME}/.cargo/bin/cargo-espflash" && \
curl -L "https://github.com/esp-rs/espflash/releases/latest/download/espflash-${ARCH}.zip" -o "${HOME}/.cargo/bin/espflash.zip" && \
unzip "${HOME}/.cargo/bin/espflash.zip" -d "${HOME}/.cargo/bin/" && \
rm "${HOME}/.cargo/bin/espflash.zip" && \
chmod u+x "${HOME}/.cargo/bin/espflash" && \
curl -L "https://github.com/esp-rs/embuild/releases/latest/download/ldproxy-${ARCH}.zip" -o "${HOME}/.cargo/bin/ldproxy.zip" && \
unzip "${HOME}/.cargo/bin/ldproxy.zip" -d "${HOME}/.cargo/bin/" && \
rm "${HOME}/.cargo/bin/ldproxy.zip" && \
chmod u+x "${HOME}/.cargo/bin/ldproxy" && \
curl -L "https://github.com/esp-rs/esp-web-flash-server/releases/latest/download/web-flash-${ARCH}.zip" -o "${HOME}/.cargo/bin/web-flash.zip" && \
unzip "${HOME}/.cargo/bin/web-flash.zip" -d "${HOME}/.cargo/bin/" && \
rm "${HOME}/.cargo/bin/web-flash.zip" && \
chmod u+x "${HOME}/.cargo/bin/web-flash"
# Install Xtensa Rust
RUN if [ -n "${GITHUB_TOKEN}" ]; then export GITHUB_TOKEN=${GITHUB_TOKEN}; fi \
&& ${HOME}/.cargo/bin/espup install\
--targets "${ESP_BOARD}" \
--log-level debug \
--export-file /home/${CONTAINER_USER}/export-esp.sh
# Set default toolchain
RUN rustup default nightly
# Activate ESP environment
RUN echo "source /home/${CONTAINER_USER}/export-esp.sh" >> ~/.bashrc
CMD [ "/bin/bash" ]

View File

@ -0,0 +1,44 @@
{
"name": "network_monitor_esp_rs",
// Select between image and build propieties to pull or build the image.
// "image": "docker.io/espressif/idf-rust:esp32c3_latest",
"build": {
"dockerfile": "Dockerfile",
"args": {
"CONTAINER_USER": "esp",
"CONTAINER_GROUP": "esp",
"ESP_BOARD": "esp32c3"
}
},
"settings": {
"editor.formatOnPaste": true,
"editor.formatOnSave": true,
"editor.formatOnSaveMode": "file",
"editor.formatOnType": true,
"lldb.executable": "/usr/bin/lldb",
"files.watcherExclude": {
"**/target/**": true
},
"rust-analyzer.checkOnSave.command": "clippy",
"rust-analyzer.checkOnSave.allTargets": false,
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer"
}
},
"extensions": [
"rust-lang.rust-analyzer",
"tamasfe.even-better-toml",
"serayuzgur.crates",
"mutantdino.resourcemonitor",
"yzhang.markdown-all-in-one",
"ms-vscode.cpptools",
"actboy168.tasks",
"Wokwi.wokwi-vscode"
],
"forwardPorts": [
3333,
8000
],
"workspaceMount": "source=${localWorkspaceFolder},target=/home/esp/network_monitor_esp_rs,type=bind,consistency=cached",
"workspaceFolder": "/home/esp/network_monitor_esp_rs"
}

1
.dockerignore Normal file
View File

@ -0,0 +1 @@
target

39
.github/workflows/rust_ci.yml vendored Normal file
View File

@ -0,0 +1,39 @@
name: Continuous Integration
on:
push:
paths-ignore:
- "**/README.md"
pull_request:
workflow_dispatch:
env:
CARGO_TERM_COLOR: always
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
rust-checks:
name: Rust Checks
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
action:
- command: build
args: --release
- command: fmt
args: --all -- --check --color always
- command: clippy
args: --all-targets --all-features --workspace -- -D warnings
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Enable caching
uses: Swatinem/rust-cache@v2
- name: Setup Rust
uses: dtolnay/rust-toolchain@v1
with:
toolchain: nightly
components: rust-src
- name: Run command
run: cargo ${{ matrix.action.command }} ${{ matrix.action.args }}

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/.vscode
/.embuild
/target
/Cargo.lock

31
Cargo.toml Normal file
View File

@ -0,0 +1,31 @@
[package]
name = "network-monitor-esp-rs"
version = "0.1.0"
authors = ["Ivan Li <ivanli2048@gmail.com>"]
edition = "2021"
resolver = "2"
rust-version = "1.71"
[profile.release]
opt-level = "s"
[profile.dev]
debug = true # Symbols are nice and they don't increase the size on Flash
opt-level = "z"
[features]
default = ["std", "embassy", "esp-idf-svc/native"]
pio = ["esp-idf-svc/pio"]
std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"]
alloc = ["esp-idf-svc/alloc"]
nightly = ["esp-idf-svc/nightly"]
experimental = ["esp-idf-svc/experimental"]
embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"]
[dependencies]
log = { version = "0.4", default-features = false }
esp-idf-svc = { version = "0.47.3", default-features = false }
[build-dependencies]
embuild = "0.31.3"

3
build.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
embuild::espidf::sysenv::output();
}

34
diagram.json Normal file
View File

@ -0,0 +1,34 @@
{
"version": 1,
"editor": "wokwi",
"author": "Ivan Li <ivanli2048@gmail.com>",
"parts": [
{
"type": "board-esp32-c3-devkitm-1",
"id": "esp",
"top": 0.59,
"left": 0.67,
"attrs": {
"flashSize": "16"
}
}
],
"connections": [
[
"esp:TX",
"$serialMonitor:RX",
"",
[]
],
[
"esp:RX",
"$serialMonitor:TX",
"",
[]
]
],
"serialMonitor": {
"display": "terminal",
"convertEol": true
}
}

77
docs/README.md Normal file
View File

@ -0,0 +1,77 @@
# network_monitor_esp_rs
## Dev Containers
This repository offers Dev Containers supports for:
- [VS Code Dev Containers](https://code.visualstudio.com/docs/remote/containers#_quick-start-open-an-existing-folder-in-a-container)
- [GitHub Codespaces](https://docs.github.com/en/codespaces/developing-in-codespaces/creating-a-codespace)
> **Note**
>
> In [order to use GitHub Codespaces](https://github.com/features/codespaces#faq)
> the project needs to be published in a GitHub repository and the user needs
> to be part of the Codespaces beta or have the project under an organization.
If using VS Code or GitHub Codespaces, you can pull the image instead of building it
from the Dockerfile by selecting the `image` property instead of `build` in
`.devcontainer/devcontainer.json`. Further customization of the Dev Container can
be achived, see [.devcontainer.json reference](https://code.visualstudio.com/docs/remote/devcontainerjson-reference).
When using Dev Containers, some tooling to facilitate building, flashing and
simulating in Wokwi is also added.
### Build
- Terminal approach:
```
scripts/build.sh [debug | release]
```
> If no argument is passed, `release` will be used as default
- UI approach:
The default build task is already set to build the project, and it can be used
in VS Code and GitHub Codespaces:
- From the [Command Palette](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette) (`Ctrl-Shift-P` or `Cmd-Shift-P`) run the `Tasks: Run Build Task` command.
- `Terminal`-> `Run Build Task` in the menu.
- With `Ctrl-Shift-B` or `Cmd-Shift-B`.
- From the [Command Palette](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette) (`Ctrl-Shift-P` or `Cmd-Shift-P`) run the `Tasks: Run Task` command and
select `Build`.
- From UI: Press `Build` on the left side of the Status Bar.
### Flash
> **Note**
>
> When using GitHub Codespaces, we need to make the ports
> public, [see instructions](https://docs.github.com/en/codespaces/developing-in-codespaces/forwarding-ports-in-your-codespace#sharing-a-port).
- Terminal approach:
- Using `flash.sh` script:
```
scripts/flash.sh [debug | release]
```
> If no argument is passed, `release` will be used as default
- UI approach:
- From the [Command Palette](https://code.visualstudio.com/docs/getstarted/userinterface#_command-palette) (`Ctrl-Shift-P` or `Cmd-Shift-P`) run the `Tasks: Run Task` command and
select `Build & Flash`.
- From UI: Press `Build & Flash` on the left side of the Status Bar.
- Any alternative flashing method from host machine.
### Wokwi Simulation
#### VS Code Dev Containers and GitHub Codespaces
The Dev Container includes the Wokwi Vs Code installed, hence you can simulate your built projects doing the following:
1. Press `F1`
2. Run `Wokwi: Start Simulator`
> **Note**
>
> We assume that the project is built in `debug` mode, if you want to simulate projects in release, please update the `elf` and `firmware` proprieties in `wokwi.toml`.
For more information and details on how to use the Wokwi extension, see [Getting Started] and [Debugging your code] Chapter of the Wokwi documentation.
[Getting Started]: https://docs.wokwi.com/vscode/getting-started
[Debugging your code]: https://docs.wokwi.com/vscode/debugging

3
rust-toolchain.toml Normal file
View File

@ -0,0 +1,3 @@
[toolchain]
channel = "nightly"
components = ["rust-src"]

18
scripts/build.sh Executable file
View File

@ -0,0 +1,18 @@
#!/bin/bash
which idf.py >/dev/null || {
source ~/export-esp.sh >/dev/null 2>&1
}
case "$1" in
"" | "release")
cargo build --release
;;
"debug")
cargo build
;;
*)
echo "Wrong argument. Only \"debug\"/\"release\" arguments are supported"
exit 1
;;
esac

21
scripts/flash.sh Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env bash
set -e
BUILD_MODE=""
case "$1" in
"" | "release")
bash scripts/build.sh
BUILD_MODE="release"
;;
"debug")
bash scripts/build.sh debug
BUILD_MODE="debug"
;;
*)
echo "Wrong argument. Only \"debug\"/\"release\" arguments are supported"
exit 1
;;
esac
web-flash --chip esp32c3 target/riscv32imc-esp-espidf/${BUILD_MODE}/network-monitor-esp-rs

10
sdkconfig.defaults Normal file
View File

@ -0,0 +1,10 @@
# Rust often needs a bit of an extra main task stack size compared to C (the default is 3K)
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000
# Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default).
# This allows to use 1 ms granuality for thread sleeps (10 ms by default).
#CONFIG_FREERTOS_HZ=1000
# Workaround for https://github.com/espressif/esp-idf/issues/7631
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n

19
src/main.rs Normal file
View File

@ -0,0 +1,19 @@
#![feature(thread_sleep_until)]
use std::{thread::sleep_until, time::Instant};
fn main() {
// It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_svc::sys::link_patches();
// Bind the log crate to the ESP Logging facilities
esp_idf_svc::log::EspLogger::initialize_default();
loop {
log::info!("Hello, world!");
// 睡眠一秒
sleep_until(Instant::now() + std::time::Duration::from_secs(1));
}
}

5
wokwi.toml Normal file
View File

@ -0,0 +1,5 @@
[wokwi]
version = 1
gdbServerPort = 3333
elf = "target/riscv32imc-esp-espidf/debug/network-monitor-esp-rs"
firmware = "target/riscv32imc-esp-espidf/debug/network-monitor-esp-rs"