diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index d08c7c8..d0e5551 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -7,7 +7,7 @@ ENV LC_ALL=C.UTF-8 ENV LANG=C.UTF-8 ARG CONTAINER_USER=esp ARG CONTAINER_GROUP=esp -ARG NIGHTLY_VERSION=nightly-2023-06-25 +ARG NIGHTLY_VERSION=nightly-2023-11-14 ARG ESP_BOARD=esp32c3 RUN apt-get update \ diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 83dab42..e04fd0a 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -6,7 +6,7 @@ "build": { "dockerfile": "Dockerfile", "args": { - "NIGHTLY_VERSION": "nightly-2023-06-25" + "NIGHTLY_VERSION": "nightly-2023-11-14" } }, "customizations": { diff --git a/.github/blinky.test.yaml b/.github/blinky.test.yaml new file mode 100644 index 0000000..dc0bbf1 --- /dev/null +++ b/.github/blinky.test.yaml @@ -0,0 +1,16 @@ +name: blinky test +version: 1 +author: Sergio Gasquez Arcos + +steps: + - wait-serial: "Hello world!" + - delay: 100ms + - expect-pin: + part-id: esp + pin: 7 + value: 0 + - delay: 550ms + - expect-pin: + part-id: esp + pin: 7 + value: 1 diff --git a/.github/button-interrupt.test.yaml b/.github/button-interrupt.test.yaml new file mode 100644 index 0000000..d96becd --- /dev/null +++ b/.github/button-interrupt.test.yaml @@ -0,0 +1,17 @@ +name: button-interrupt test +version: 1 +author: Sergio Gasquez Arcos + +steps: + - wait-serial: "Hello world!" + # Press once + - set-control: + part-id: btn1 + control: pressed + value: 0 + - delay: 100ms + - set-control: + part-id: btn1 + control: pressed + value: 1 + - wait-serial: "GPIO interrupt" diff --git a/.github/button.test.yaml b/.github/button.test.yaml new file mode 100644 index 0000000..1196e8b --- /dev/null +++ b/.github/button.test.yaml @@ -0,0 +1,26 @@ +name: button test +version: 1 +author: Sergio Gasquez Arcos + +steps: + - wait-serial: "Hello world!" + - delay: 100ms + # Press once + - set-control: + part-id: btn1 + control: pressed + value: 1 + - delay: 50ms + - expect-pin: + part-id: esp + pin: 7 + value: 0 + - set-control: + part-id: btn1 + control: pressed + value: 0 + - delay: 50ms + - expect-pin: + part-id: esp + pin: 7 + value: 1 diff --git a/.github/hello-world.test.yaml b/.github/hello-world.test.yaml new file mode 100644 index 0000000..74e48b5 --- /dev/null +++ b/.github/hello-world.test.yaml @@ -0,0 +1,6 @@ +name: hello-world test +version: 1 +author: Sergio Gasquez Arcos + +steps: + - wait-serial: "Hello world!" diff --git a/.github/http-client.test.yaml b/.github/http-client.test.yaml new file mode 100644 index 0000000..8ab0eca --- /dev/null +++ b/.github/http-client.test.yaml @@ -0,0 +1,9 @@ +name: http-client test +version: 1 +author: Sergio Gasquez Arcos + +steps: + - wait-serial: "Wait to get connected" + - wait-serial: "Wait to get an ip address" + - wait-serial: "Making HTTP request" + - wait-serial: "< Hello fellow Rustaceans! >" diff --git a/.github/panic.test.yaml b/.github/panic.test.yaml new file mode 100644 index 0000000..2558f78 --- /dev/null +++ b/.github/panic.test.yaml @@ -0,0 +1,6 @@ +name: panic test +version: 1 +author: Sergio Gasquez Arcos + +steps: + - wait-serial: "!! A panic occured in " diff --git a/.github/stack-overflow-detection.test.yaml b/.github/stack-overflow-detection.test.yaml new file mode 100644 index 0000000..26a8dd1 --- /dev/null +++ b/.github/stack-overflow-detection.test.yaml @@ -0,0 +1,7 @@ +name: stack-overflow-detection test +version: 1 +author: Sergio Gasquez Arcos + +steps: + - wait-serial: "Safe stack" + - wait-serial: "Possible Stack Overflow Detected" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3b65c7a..854d9d2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,21 +3,27 @@ name: CI on: push: paths-ignore: + - "book/" + - "book/**" - "**/README.md" + - ".github/workflows/wokwi_projects.yml" pull_request: paths-ignore: + - "book/" + - "book/**" - "**/README.md" + - ".github/workflows/wokwi_projects.yml" schedule: - cron: "50 7 * * *" env: CARGO_TERM_COLOR: always - SSID: ssid - PASSWORD: password + SSID: Wokwi-GUEST + PASSWORD: "" jobs: hello-world: - name: intro/hello-world + name: hello-world runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -31,14 +37,35 @@ jobs: - run: cargo build --release working-directory: intro/hello-world + - name: Wokwi CI check + if: github.actor == 'esp-rs' + uses: wokwi/wokwi-ci-action@v1 + with: + token: ${{ secrets.WOKWI_CLI_TOKEN }} + path: intro/hello-world + timeout: 30000 + scenario: ${{ github.workspace }}/.github/hello-world.test.yaml + fail_text: 'Error' + examples: - name: ${{ matrix.project }} + name: ${{ matrix.project.name }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: project: - ["intro/blinky", "intro/button", "intro/button-interrupt", "intro/hello-world", "intro/panic", "intro/http-client", "advanced/stack_overflow_detection"] + - name: "blinky" + path: "intro/blinky" + - name: "button" + path: "intro/button" + - name: "button-interrupt" + path: "intro/button-interrupt" + - name: "panic" + path: "intro/panic" + - name: "http-client" + path: "intro/http-client" + - name: "stack-overflow-detection" + path: "advanced/stack-overflow-detection" steps: - uses: actions/checkout@v4 @@ -49,4 +76,14 @@ jobs: components: rust-src - run: cargo build --release --examples - working-directory: ${{ matrix.project }} + working-directory: ${{ matrix.project.path }} + + - name: Wokwi CI check + if: matrix.project.name != 'stack-overflow-detection' && github.actor == 'esp-rs' + uses: wokwi/wokwi-ci-action@v1 + with: + token: ${{ secrets.WOKWI_CLI_TOKEN }} + path: ${{ matrix.project.path }} + timeout: 30000 + scenario: ${{ github.workspace }}/.github/${{ matrix.project.name }}.test.yaml + fail_text: 'Error' diff --git a/.github/workflows/wokwi_projects.yml b/.github/workflows/wokwi_projects.yml new file mode 100644 index 0000000..375562e --- /dev/null +++ b/.github/workflows/wokwi_projects.yml @@ -0,0 +1,91 @@ +--- +name: Wokwi.com CI + +on: + push: + paths: + - '.github/workflows/wokwi_projects.yml' + pull_request: + paths: + - '.github/workflows/wokwi_projects.yml' + schedule: + - cron: "50 7 * * *" + workflow_dispatch: + +jobs: + wokwi-check: + name: ${{ matrix.project.name }} + if: github.actor == 'esp-rs' + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + project: + # - name: "hello-world" + # id: "382725628217620481" + # path: "intro/hello-world" + - name: blinky + id: "382725482391094273" + path: "intro/blinky" + - name: button + id: "382725583123606529" + path: "intro/button" + - name: button-interrupt + id: "382723722184136705" + path: "intro/button-interrupt" + - name: panic + id: "382726300037178369" + path: "intro/panic" + + steps: + - name: Checkout the repository + uses: actions/checkout@v4 + + - name: Download project code + run: | + wget -q -O ${{ matrix.project.name }}.zip https://wokwi.com/api/projects/${{ matrix.project.id }}/zip + unzip ${{ matrix.project.name }}.zip -d ${{ matrix.project.name }} + mkdir -p ${{ matrix.project.name }}/src + rm -rf ${{ matrix.project.path }}/src/*.rs + cp ${{ matrix.project.name }}/*.rs ${{ matrix.project.path }}/src/ + rm -rf ${{ matrix.project.path }}/Cargo.toml + cp ${{ matrix.project.name }}/Cargo.toml ${{ matrix.project.path }}/Cargo.toml + + - name: Create wokwi.toml + run: echo -e "[wokwi]\nversion = 1\nfirmware = '${{ matrix.project.name }}/${{ matrix.project.name }}'\nelf = '${{ matrix.project.name }}/${{ matrix.project.name }}'" > ${{ matrix.project.path }}/wokwi.toml + + - name: Update ownership + run: | + sudo chown 1000:1000 -R ${{ matrix.project.path }} + + - name: Pull Docker image + run: docker image pull espressif/idf-rust:esp32c3_latest + + - name: Test code example in Docker image + uses: addnab/docker-run-action@v3 + with: + image: espressif/idf-rust:esp32c3_latest + options: -u esp -v ${{ github.workspace }}:/home/esp/workspace + run: | + cd /home/esp/workspace/${{ matrix.project.path }} + export SSID="Wokwi-GUEST" && export PASSWORD="" + /home/esp/.cargo/bin/cargo build --release --out-dir /home/esp/workspace/${{ matrix.project.path }}/${{ matrix.project.name }} -Z unstable-options + rm -rf /home/esp/workspace/${{ matrix.project.path }}/target + + - run: cat ${{ matrix.project.path }}/wokwi.toml + + - name: Wokwi CI check + uses: wokwi/wokwi-ci-action@v1 + with: + token: ${{ secrets.WOKWI_CLI_TOKEN }} + path: ${{ matrix.project.path }} + timeout: 30000 + scenario: ${{ github.workspace }}/.github/${{ matrix.project.name }}.test.yaml + fail_text: 'Error' + + - name: Upload source code + if: success() || failure() + uses: actions/upload-artifact@v3 + with: + name: ${{ matrix.project.name }}_${{ matrix.project.id }} + path: ${{ matrix.project.path }} diff --git a/advanced/stack_overflow_detection/.cargo/config.toml b/advanced/stack-overflow-detection/.cargo/config.toml similarity index 100% rename from advanced/stack_overflow_detection/.cargo/config.toml rename to advanced/stack-overflow-detection/.cargo/config.toml diff --git a/advanced/stack_overflow_detection/Cargo.lock b/advanced/stack-overflow-detection/Cargo.lock similarity index 99% rename from advanced/stack_overflow_detection/Cargo.lock rename to advanced/stack-overflow-detection/Cargo.lock index 03d6f17..5fb1b55 100644 --- a/advanced/stack_overflow_detection/Cargo.lock +++ b/advanced/stack-overflow-detection/Cargo.lock @@ -393,7 +393,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" [[package]] -name = "stack_overflow_protection" +name = "stack-overflow-detection" version = "0.1.0" dependencies = [ "critical-section", diff --git a/advanced/stack_overflow_detection/Cargo.toml b/advanced/stack-overflow-detection/Cargo.toml similarity index 91% rename from advanced/stack_overflow_detection/Cargo.toml rename to advanced/stack-overflow-detection/Cargo.toml index eb7431f..84f0496 100644 --- a/advanced/stack_overflow_detection/Cargo.toml +++ b/advanced/stack-overflow-detection/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "stack_overflow_protection" +name = "stack-overflow-detection" version = "0.1.0" edition = "2021" license = "MIT OR Apache-2.0" diff --git a/advanced/stack-overflow-detection/diagram.json b/advanced/stack-overflow-detection/diagram.json new file mode 100644 index 0000000..31a7a6f --- /dev/null +++ b/advanced/stack-overflow-detection/diagram.json @@ -0,0 +1,33 @@ +{ + "version": 1, + "author": "Sergio Gasquez Arcos", + "editor": "wokwi", + "parts": [ + { + "type": "board-esp32-c3-rust-1", + "id": "esp", + "top": -126.57, + "left": 46.35, + "attrs": { + "builder": "rust-nostd-esp" + } + } + ], + "connections": [ + [ + "esp:21", + "$serialMonitor:RX", + "", + [] + ], + [ + "esp:20", + "$serialMonitor:TX", + "", + [] + ] + ], + "serialMonitor": { + "display": "auto" + } +} diff --git a/advanced/stack_overflow_detection/examples/stack_overflow_protection.rs b/advanced/stack-overflow-detection/examples/stack-overflow-detection.rs similarity index 100% rename from advanced/stack_overflow_detection/examples/stack_overflow_protection.rs rename to advanced/stack-overflow-detection/examples/stack-overflow-detection.rs diff --git a/advanced/stack_overflow_detection/rust-toolchain.toml b/advanced/stack-overflow-detection/rust-toolchain.toml similarity index 72% rename from advanced/stack_overflow_detection/rust-toolchain.toml rename to advanced/stack-overflow-detection/rust-toolchain.toml index b716762..446d6fe 100644 --- a/advanced/stack_overflow_detection/rust-toolchain.toml +++ b/advanced/stack-overflow-detection/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] -channel = "nightly-2023-06-25" +channel = "nightly-2023-11-14" components = ["rust-src"] targets = ["riscv32imc-unknown-none-elf"] diff --git a/advanced/stack_overflow_detection/src/main.rs b/advanced/stack-overflow-detection/src/main.rs similarity index 100% rename from advanced/stack_overflow_detection/src/main.rs rename to advanced/stack-overflow-detection/src/main.rs diff --git a/advanced/stack-overflow-detection/wokwi.toml b/advanced/stack-overflow-detection/wokwi.toml new file mode 100644 index 0000000..a8384ac --- /dev/null +++ b/advanced/stack-overflow-detection/wokwi.toml @@ -0,0 +1,9 @@ +[wokwi] +version = 1 +# Exercise +# firmware = "target/riscv32imc-unknown-none-elf/release/stack_overflow_detection" +# elf = "target/riscv32imc-unknown-none-elf/release/stack_overflow_detection" + +# Solution +firmware = 'target/riscv32imc-unknown-none-elf/release/examples/stack-overflow-detection' +elf = 'target/riscv32imc-unknown-none-elf/release/examples/stack-overflow-detection' diff --git a/book/src/02_0_preparations.md b/book/src/02_0_preparations.md index 14e0d12..0515cf2 100644 --- a/book/src/02_0_preparations.md +++ b/book/src/02_0_preparations.md @@ -2,7 +2,7 @@ This chapter contains information about the course material, the required hardware, and an installation guide. -## Icons and Formatting we use +## Icons and Formatting We Use We use Icons to mark different kinds of information in the book: * ✅ Call for action. @@ -31,6 +31,20 @@ Anchor comments can be ignored, they are only used to introduce those parts of c > No additional debugger/probe hardware is required. -## Companion material +## Simulating Projects + +Certain projects can be simulated with [Wokwi][wokwi]. Look for indications in the book to identify projects available for simulation. Simulation can be accomplished through two methods: +- Using wokwi.com: Conduct the build process and code editing directly through the browser. +- Using [Wokwi VS Code extension][wokwi-vscode]: Leverage VS Code to edit projects and perform builds. Utilize the Wokwi VS Code extension to simulate the resulting binaries. + - This approach requires some [installation][wokwi-installation] + - This approach assumes that the project is built in debug mode + - This approach allows [debugging the project][wokwi-debug] + +[wokwi]: https://wokwi.com/ +[wokwi-vscode]: https://docs.wokwi.com/vscode/getting-started +[wokwi-installation]: https://docs.wokwi.com/vscode/getting-started#installation +[wokwi-debug]: https://docs.wokwi.com/vscode/debugging + +## Companion Material - [Official esp-rs book](https://esp-rs.github.io/book/introduction.html) diff --git a/book/src/02_2_software.md b/book/src/02_2_software.md index a19137b..1c540d6 100644 --- a/book/src/02_2_software.md +++ b/book/src/02_2_software.md @@ -8,12 +8,12 @@ Follow the steps below for a default installation of the ESP32-C3 platform tooli ✅ If you haven't got Rust on your computer, obtain it via -Furthermore, for ESP32-C3, a [*nightly* version](https://rust-lang.github.io/rustup/concepts/channels.html#working-with-nightly-rust) of the Rust toolchain is currently required, for this training we will use `nightly-2023-06-25` version. +Furthermore, for ESP32-C3, a [*nightly* version](https://rust-lang.github.io/rustup/concepts/channels.html#working-with-nightly-rust) of the Rust toolchain is currently required, for this training we will use `nightly-2023-11-14` version. ✅ Install *nightly* Rust and add support for the target architecture using the following command: ```console -rustup toolchain install nightly-2023-06-25 --component rust-src --target riscv32imc-unknown-none-elf +rustup toolchain install nightly-2023-11-14 --component rust-src --target riscv32imc-unknown-none-elf ``` 🔎 Rust is capable of cross-compiling to any supported target (see `rustup target list`). By default, only the native architecture of your system is installed. @@ -56,7 +56,7 @@ to compile the binaries for the Espressif target. Flashing binaries from contain - On the host: use the `cargo-espflash` sub-command to flash the program onto the embedded hardware - Use [`web-flash`](https://github.com/esp-rs/esp-web-flash-server) crate to flash the resulting binaries from the container. The container already includes `web-flash`. Here is how you would flash the build output of [`hello-world` project](./02_4_hello-world.md): ```console - web-flash --chip esp32c3 target/riscv32imc-unknown-none-elf/debug/hello-world + web-flash --chip esp32c3 target/riscv32imc-unknown-none-elf/release/hello-world ``` ✅ Install [`Docker`](https://docs.docker.com/get-docker/) for your operating system. diff --git a/book/src/02_4_hello_world.md b/book/src/02_4_hello_world.md index 91fdadc..b9a89f4 100644 --- a/book/src/02_4_hello_world.md +++ b/book/src/02_4_hello_world.md @@ -47,3 +47,12 @@ Hello world! [The Rust on ESP Book]: https://esp-rs.github.io/book/ [Generating Projects from Templates]: https://esp-rs.github.io/book/writing-your-own-application/generate-project/index.html [Understanding esp-template]: https://esp-rs.github.io/book/writing-your-own-application/generate-project/esp-template.html + +## Simulation + +This project is available for simulation through two methods: +- [Wokwi project](https://wokwi.com/projects/382725628217620481?build-cache=disable) +- Wokwi VS Code extension: + 1. Press F1, select `Wokwi: Select Config File`, and choose `intro/hello-world/wokwi.toml`. + 2. Build your project. + 3. Press F1 again and select `Wokwi: Start Simulator`. diff --git a/book/src/03_1_panic.md b/book/src/03_1_panic.md index d9d2751..56170f8 100644 --- a/book/src/03_1_panic.md +++ b/book/src/03_1_panic.md @@ -72,3 +72,15 @@ you might have noticed the difference in the size of the flashed binary. If you are reusing this project for other exercises, be sure to remove the line causing the explicit panic. [panic]: https://doc.rust-lang.org/book/ch09-01-unrecoverable-errors-with-panic.html + +## Simulation + +This project is available for simulation through two methods: +- Wokwi projects: + - Exercise: Currently not available + - [Solution](https://wokwi.com/projects/382726300037178369?build-cache=disable) +- Wokwi files are also present in the project folder to simulate it with Wokwi VS Code extension: + 1. Press F1, select `Wokwi: Select Config File` and choose `intro/panic/wokwi.toml` + - Edit the `wokwi.toml` file to select between exercise and solution simulation + 2. Build you project + 3. Press F1 again and select `Wokwi: Start Simulator` diff --git a/book/src/03_2_blinky.md b/book/src/03_2_blinky.md index 8286324..f41e237 100644 --- a/book/src/03_2_blinky.md +++ b/book/src/03_2_blinky.md @@ -44,3 +44,15 @@ We also see that the HAL offers a way to delay execution. [into-push-pull-output]: https://docs.rs/esp32c3-hal/latest/esp32c3_hal/gpio/struct.GpioPin.html#method.into_push_pull_output [toogle]: https://docs.rs/esp32c3-hal/latest/esp32c3_hal/gpio/struct.GpioPin.html#method.toggle [delay-ms]: https://docs.rs/esp32c3-hal/latest/esp32c3_hal/struct.Delay.html#method.delay_ms + +## Simulation + +This project is available for simulation through two methods: +- Wokwi projects: + - Exercise: Currently not available + - [Solution](https://wokwi.com/projects/382725482391094273?build-cache=disable) +- Wokwi files are also present in the project folder to simulate it with Wokwi VS Code extension: + 1. Press F1, select `Wokwi: Select Config File` and choose `intro/blinky/wokwi.toml` + - Edit the `wokwi.toml` file to select between exercise and solution simulation + 2. Build you project + 3. Press F1 again and select `Wokwi: Start Simulator` diff --git a/book/src/03_3_button.md b/book/src/03_3_button.md index be26093..030e75e 100644 --- a/book/src/03_3_button.md +++ b/book/src/03_3_button.md @@ -38,3 +38,15 @@ Similarly to turning a `GPIO` into an `output` we can turn it into an `input`. T [`BOOT` on `GPIO9`]: https://github.com/esp-rs/esp-rust-board#ios [into-pull-up-input]: https://docs.rs/esp32c3-hal/latest/esp32c3_hal/gpio/struct.GpioPin.html#method.into_pull_up_input [into-push-pull-output]: https://docs.rs/esp32c3-hal/latest/esp32c3_hal/gpio/struct.GpioPin.html#method.into_push_pull_output + +## Simulation + +This project is available for simulation through two methods: +- Wokwi projects: + - Exercise: Currently not available + - [Solution](https://wokwi.com/projects/382725583123606529?build-cache=disable) +- Wokwi files are also present in the project folder to simulate it with Wokwi VS Code extension: + 1. Press F1, select `Wokwi: Select Config File` and choose `intro/button/wokwi.toml` + - Edit the `wokwi.toml` file to select between exercise and solution simulation + 2. Build you project + 3. Press F1 again and select `Wokwi: Start Simulator` diff --git a/book/src/03_4_interrupt.md b/book/src/03_4_interrupt.md index e1368e6..b1075b1 100644 --- a/book/src/03_4_interrupt.md +++ b/book/src/03_4_interrupt.md @@ -63,3 +63,15 @@ Here, the name of the function must match the interrupt. [`critical-section`]: https://crates.io/crates/critical-section [possible interrupts]: https://docs.rs/esp32c3/0.5.1/esp32c3/enum.Interrupt.html [events]: https://docs.rs/esp32c3-hal/latest/esp32c3_hal/gpio/enum.Event.html#variants + +## Simulation + +This project is available for simulation through two methods: +- Wokwi projects: + - Exercise: Currently not available + - [Solution](https://wokwi.com/projects/382723722184136705?build-cache=disable) +- Wokwi files are also present in the project folder to simulate it with Wokwi VS Code extension: + 1. Press F1, select `Wokwi: Select Config File` and choose `intro/button-interrupt/wokwi.toml` + - Edit the `wokwi.toml` file to select between exercise and solution simulation + 2. Build you project + 3. Press F1 again and select `Wokwi: Start Simulator` diff --git a/book/src/03_5_http_client.md b/book/src/03_5_http_client.md index 7530d03..5f5b55a 100644 --- a/book/src/03_5_http_client.md +++ b/book/src/03_5_http_client.md @@ -93,3 +93,15 @@ To make an HTTP request, we first need to open a socket, and write to it the GET [timer]: https://docs.rs/esp32c3-hal/latest/esp32c3_hal/systimer/index.html [clock]: https://docs.rs/esp32c3-hal/latest/esp32c3_hal/clock/index.html + +## Simulation + +This project is available for simulation through two methods: +- Wokwi projects: + - Exercise: Currently not available + - Solution: Currently not available +- Wokwi files are also present in the project folder to simulate it with Wokwi VS Code extension: + 1. Press F1, select `Wokwi: Select Config File` and choose `intro/http-client/wokwi.toml` + - Edit the `wokwi.toml` file to select between exercise and solution simulation + 2. Build you project + 3. Press F1 again and select `Wokwi: Start Simulator` diff --git a/book/src/04_1_stack_overflow_protection.md b/book/src/04_1_stack_overflow_protection.md index 47ba6da..3a72182 100644 --- a/book/src/04_1_stack_overflow_protection.md +++ b/book/src/04_1_stack_overflow_protection.md @@ -1,4 +1,4 @@ -# Stack Overflow Protection +# Stack Overflow Detection Rust is well known for its memory safety. Whenever possible the compiler enforces memory safety at compile. @@ -31,9 +31,9 @@ We can also test for the current stack usage by temporarily increasing the safe ## Setup -✅ Go to `advanced/stack_overflow_protection` directory. +✅ Go to `advanced/stack-overflow-detection` directory. -✅ Open the prepared project skeleton in `advanced/stack_overflow_protection`. +✅ Open the prepared project skeleton in `advanced/stack-overflow-detection`. ✅ Open the docs for this project with the following command: @@ -52,10 +52,10 @@ If you change it to run from flash you won't see a crash but the application wil In this case it's easy to guess the cause of this behavior however in a real world application you probably won't know what exactly happened. -`advanced/stack_overflow_protection/examples/stack_overflow_protection.rs` contains the solution. You can run it with the following command: +`advanced/stack-overflow-detection/examples/stack-overflow-detection.rs` contains the solution. You can run it with the following command: ```shell -cargo run --example stack_overflow_protection +cargo run --example stack-overflow-detection ``` ## Exercise @@ -67,7 +67,7 @@ It should move the `DebugAssist` into a static variable. The resulting function should look like this ```rust,ignore -{{#include ../../advanced/stack_overflow_detection/examples/stack_overflow_protection.rs:debug_assists}} +{{#include ../../advanced/stack-overflow-detection/examples/stack-overflow-detection.rs:debug_assists}} ``` There is quite a lot going on here but most of this is setting up the interrupt. @@ -84,7 +84,7 @@ As you probably remember from the introduction to interrupts we can define the i The name of the function needs to match the name of the interrupt. ```rust,ignore -{{#include ../../advanced/stack_overflow_detection/examples/stack_overflow_protection.rs:interrupt}} +{{#include ../../advanced/stack-overflow-detection/examples/stack-overflow-detection.rs:interrupt}} ... ``` @@ -99,5 +99,5 @@ It is unfortunately not possible to generate a stack trace here since the stack The whole function should look like this ```rust,ignore -{{#include ../../advanced/stack_overflow_detection/examples/stack_overflow_protection.rs:handler}} +{{#include ../../advanced/stack-overflow-detection/examples/stack-overflow-detection.rs:handler}} ``` diff --git a/intro/blinky/diagram.json b/intro/blinky/diagram.json new file mode 100644 index 0000000..31a7a6f --- /dev/null +++ b/intro/blinky/diagram.json @@ -0,0 +1,33 @@ +{ + "version": 1, + "author": "Sergio Gasquez Arcos", + "editor": "wokwi", + "parts": [ + { + "type": "board-esp32-c3-rust-1", + "id": "esp", + "top": -126.57, + "left": 46.35, + "attrs": { + "builder": "rust-nostd-esp" + } + } + ], + "connections": [ + [ + "esp:21", + "$serialMonitor:RX", + "", + [] + ], + [ + "esp:20", + "$serialMonitor:TX", + "", + [] + ] + ], + "serialMonitor": { + "display": "auto" + } +} diff --git a/intro/blinky/rust-toolchain.toml b/intro/blinky/rust-toolchain.toml index b716762..446d6fe 100644 --- a/intro/blinky/rust-toolchain.toml +++ b/intro/blinky/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] -channel = "nightly-2023-06-25" +channel = "nightly-2023-11-14" components = ["rust-src"] targets = ["riscv32imc-unknown-none-elf"] diff --git a/intro/blinky/wokwi.toml b/intro/blinky/wokwi.toml new file mode 100644 index 0000000..265a082 --- /dev/null +++ b/intro/blinky/wokwi.toml @@ -0,0 +1,9 @@ +[wokwi] +version = 1 +# Exercise +# firmware = "target/riscv32imc-unknown-none-elf/release/blinky" +# elf = "target/riscv32imc-unknown-none-elf/release/blinky" + +# Solution +firmware = 'target/riscv32imc-unknown-none-elf/release/examples/blinky' +elf = 'target/riscv32imc-unknown-none-elf/release/examples/blinky' diff --git a/intro/button-interrupt/Cargo.lock b/intro/button-interrupt/Cargo.lock index c6ea54a..a199276 100644 --- a/intro/button-interrupt/Cargo.lock +++ b/intro/button-interrupt/Cargo.lock @@ -30,7 +30,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" [[package]] -name = "button_interrupt" +name = "button-interrupt" version = "0.1.0" dependencies = [ "critical-section", diff --git a/intro/button-interrupt/Cargo.toml b/intro/button-interrupt/Cargo.toml index 65da90e..8249e4a 100644 --- a/intro/button-interrupt/Cargo.toml +++ b/intro/button-interrupt/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "button_interrupt" +name = "button-interrupt" version = "0.1.0" authors = ["Sergio Gasquez "] edition = "2021" diff --git a/intro/button-interrupt/diagram.json b/intro/button-interrupt/diagram.json new file mode 100644 index 0000000..94e1cc2 --- /dev/null +++ b/intro/button-interrupt/diagram.json @@ -0,0 +1,62 @@ +{ + "version": 1, + "author": "Sergio Gasquez Arcos", + "editor": "wokwi", + "parts": [ + { + "type": "board-esp32-c3-rust-1", + "id": "esp", + "top": -99.32, + "left": 34.67, + "attrs": { + "builder": "rust-nostd-esp" + } + }, + { + "type": "wokwi-pushbutton", + "id": "btn1", + "top": 2.81, + "left": -49.66, + "rotate": 90, + "attrs": { + "color": "green", + "bounce": "0" + } + } + ], + "connections": [ + [ + "esp:21", + "$serialMonitor:RX", + "", + [] + ], + [ + "esp:20", + "$serialMonitor:TX", + "", + [] + ], + [ + "esp:9", + "btn1:1.r", + "green", + [ + "h0" + ] + ], + [ + "esp:GND", + "btn1:2.r", + "black", + [ + "h-97.82", + "v114.6", + "h26" + ] + ] + ], + "serialMonitor": { + "display": "auto" + } +} diff --git a/intro/button-interrupt/examples/button-interrupt.rs b/intro/button-interrupt/examples/button-interrupt.rs index 2769bc3..c6ba57b 100644 --- a/intro/button-interrupt/examples/button-interrupt.rs +++ b/intro/button-interrupt/examples/button-interrupt.rs @@ -7,14 +7,14 @@ use esp_backtrace as _; use esp_println::println; use hal::{ clock::ClockControl, - gpio::{Event, Gpio9, Input, PullDown, IO}, + gpio::{Event, Gpio9, Input, PullUp, IO}, interrupt, peripherals::{self, Peripherals}, prelude::*, riscv, Delay, }; -static BUTTON: Mutex>>>> = Mutex::new(RefCell::new(None)); +static BUTTON: Mutex>>>> = Mutex::new(RefCell::new(None)); #[entry] fn main() -> ! { @@ -29,7 +29,7 @@ fn main() -> ! { let mut led = io.pins.gpio7.into_push_pull_output(); // Set GPIO9 as an input - let mut button = io.pins.gpio9.into_pull_down_input(); + let mut button = io.pins.gpio9.into_pull_up_input(); button.listen(Event::FallingEdge); // ANCHOR: critical_section diff --git a/intro/button-interrupt/rust-toolchain.toml b/intro/button-interrupt/rust-toolchain.toml index b716762..446d6fe 100644 --- a/intro/button-interrupt/rust-toolchain.toml +++ b/intro/button-interrupt/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] -channel = "nightly-2023-06-25" +channel = "nightly-2023-11-14" components = ["rust-src"] targets = ["riscv32imc-unknown-none-elf"] diff --git a/intro/button-interrupt/src/main.rs b/intro/button-interrupt/src/main.rs index e076a91..0fd374b 100644 --- a/intro/button-interrupt/src/main.rs +++ b/intro/button-interrupt/src/main.rs @@ -7,14 +7,14 @@ use esp_backtrace as _; use esp_println::println; use hal::{ clock::ClockControl, - gpio::{Event, Gpio9, Input, PullDown, IO}, + gpio::{Event, Gpio9, Input, PullUp, IO}, interrupt, peripherals::{self, Peripherals}, prelude::*, riscv, Delay, }; -static BUTTON: Mutex>>>> = Mutex::new(RefCell::new(None)); +static BUTTON: Mutex>>>> = Mutex::new(RefCell::new(None)); #[entry] fn main() -> ! { @@ -29,7 +29,7 @@ fn main() -> ! { let mut led = io.pins.gpio7.into_push_pull_output(); // Set GPIO9 as an input - let mut button = io.pins.gpio9.into_pull_down_input(); + let mut button = io.pins.gpio9.into_pull_up_input(); let mut delay = Delay::new(&clocks); loop {} diff --git a/intro/button-interrupt/wokwi.toml b/intro/button-interrupt/wokwi.toml new file mode 100644 index 0000000..9424b99 --- /dev/null +++ b/intro/button-interrupt/wokwi.toml @@ -0,0 +1,9 @@ +[wokwi] +version = 1 +# Exercise +# firmware = "target/riscv32imc-unknown-none-elf/release/button_interrupt" +# elf = "target/riscv32imc-unknown-none-elf/release/button_interrupt" + +# Solution +firmware = 'target/riscv32imc-unknown-none-elf/release/examples/button-interrupt' +elf = 'target/riscv32imc-unknown-none-elf/release/examples/button-interrupt' diff --git a/intro/button/diagram.json b/intro/button/diagram.json new file mode 100644 index 0000000..94e1cc2 --- /dev/null +++ b/intro/button/diagram.json @@ -0,0 +1,62 @@ +{ + "version": 1, + "author": "Sergio Gasquez Arcos", + "editor": "wokwi", + "parts": [ + { + "type": "board-esp32-c3-rust-1", + "id": "esp", + "top": -99.32, + "left": 34.67, + "attrs": { + "builder": "rust-nostd-esp" + } + }, + { + "type": "wokwi-pushbutton", + "id": "btn1", + "top": 2.81, + "left": -49.66, + "rotate": 90, + "attrs": { + "color": "green", + "bounce": "0" + } + } + ], + "connections": [ + [ + "esp:21", + "$serialMonitor:RX", + "", + [] + ], + [ + "esp:20", + "$serialMonitor:TX", + "", + [] + ], + [ + "esp:9", + "btn1:1.r", + "green", + [ + "h0" + ] + ], + [ + "esp:GND", + "btn1:2.r", + "black", + [ + "h-97.82", + "v114.6", + "h26" + ] + ] + ], + "serialMonitor": { + "display": "auto" + } +} diff --git a/intro/button/rust-toolchain.toml b/intro/button/rust-toolchain.toml index b716762..446d6fe 100644 --- a/intro/button/rust-toolchain.toml +++ b/intro/button/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] -channel = "nightly-2023-06-25" +channel = "nightly-2023-11-14" components = ["rust-src"] targets = ["riscv32imc-unknown-none-elf"] diff --git a/intro/button/wokwi.toml b/intro/button/wokwi.toml new file mode 100644 index 0000000..644b316 --- /dev/null +++ b/intro/button/wokwi.toml @@ -0,0 +1,9 @@ +[wokwi] +version = 1 +# Exercise +# firmware = "target/riscv32imc-unknown-none-elf/release/button" +# elf = "target/riscv32imc-unknown-none-elf/release/button" + +# Solution +firmware = 'target/riscv32imc-unknown-none-elf/release/examples/button' +elf = 'target/riscv32imc-unknown-none-elf/release/examples/button' diff --git a/intro/hello-world/diagram.json b/intro/hello-world/diagram.json new file mode 100644 index 0000000..31a7a6f --- /dev/null +++ b/intro/hello-world/diagram.json @@ -0,0 +1,33 @@ +{ + "version": 1, + "author": "Sergio Gasquez Arcos", + "editor": "wokwi", + "parts": [ + { + "type": "board-esp32-c3-rust-1", + "id": "esp", + "top": -126.57, + "left": 46.35, + "attrs": { + "builder": "rust-nostd-esp" + } + } + ], + "connections": [ + [ + "esp:21", + "$serialMonitor:RX", + "", + [] + ], + [ + "esp:20", + "$serialMonitor:TX", + "", + [] + ] + ], + "serialMonitor": { + "display": "auto" + } +} diff --git a/intro/hello-world/rust-toolchain.toml b/intro/hello-world/rust-toolchain.toml index b716762..446d6fe 100644 --- a/intro/hello-world/rust-toolchain.toml +++ b/intro/hello-world/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] -channel = "nightly-2023-06-25" +channel = "nightly-2023-11-14" components = ["rust-src"] targets = ["riscv32imc-unknown-none-elf"] diff --git a/intro/hello-world/wokwi.toml b/intro/hello-world/wokwi.toml new file mode 100644 index 0000000..3db4014 --- /dev/null +++ b/intro/hello-world/wokwi.toml @@ -0,0 +1,4 @@ +[wokwi] +version = 1 +firmware = "target/riscv32imc-unknown-none-elf/release/hello_world" +elf = "target/riscv32imc-unknown-none-elf/release/hello_world" diff --git a/intro/http-client/diagram.json b/intro/http-client/diagram.json new file mode 100644 index 0000000..31a7a6f --- /dev/null +++ b/intro/http-client/diagram.json @@ -0,0 +1,33 @@ +{ + "version": 1, + "author": "Sergio Gasquez Arcos", + "editor": "wokwi", + "parts": [ + { + "type": "board-esp32-c3-rust-1", + "id": "esp", + "top": -126.57, + "left": 46.35, + "attrs": { + "builder": "rust-nostd-esp" + } + } + ], + "connections": [ + [ + "esp:21", + "$serialMonitor:RX", + "", + [] + ], + [ + "esp:20", + "$serialMonitor:TX", + "", + [] + ] + ], + "serialMonitor": { + "display": "auto" + } +} diff --git a/intro/http-client/examples/http-client.rs b/intro/http-client/examples/http-client.rs index adf4f06..dd71e66 100644 --- a/intro/http-client/examples/http-client.rs +++ b/intro/http-client/examples/http-client.rs @@ -6,7 +6,7 @@ use hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, systimer::S use embedded_io::*; use embedded_svc::{ ipv4::Interface, - wifi::{AccessPointInfo, ClientConfiguration, Configuration, Wifi}, + wifi::{AccessPointInfo, AuthMethod, ClientConfiguration, Configuration, Wifi}, }; use esp_backtrace as _; @@ -53,11 +53,21 @@ fn main() -> ! { create_network_interface(&init, wifi, WifiStaDevice, &mut socket_set_entries).unwrap(); let wifi_stack = WifiStack::new(iface, device, sockets, current_millis); // ANCHOR_END: wifi_config + + let mut auth_method = AuthMethod::WPA2Personal; + let mut channel = None; + if PASSWORD.is_empty() { + auth_method = AuthMethod::None; + channel = Some(6); + } + // ANCHOR: client_config_start let client_config = Configuration::Client(ClientConfiguration { // ANCHOR_END: client_config_start ssid: SSID.into(), password: PASSWORD.into(), + auth_method, + channel, ..Default::default() // ANCHOR: client_config_end }); diff --git a/intro/http-client/rust-toolchain.toml b/intro/http-client/rust-toolchain.toml index b716762..446d6fe 100644 --- a/intro/http-client/rust-toolchain.toml +++ b/intro/http-client/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] -channel = "nightly-2023-06-25" +channel = "nightly-2023-11-14" components = ["rust-src"] targets = ["riscv32imc-unknown-none-elf"] diff --git a/intro/http-client/wokwi.toml b/intro/http-client/wokwi.toml new file mode 100644 index 0000000..759aaae --- /dev/null +++ b/intro/http-client/wokwi.toml @@ -0,0 +1,9 @@ +[wokwi] +version = 1 +# Exercise +# firmware = "target/riscv32imc-unknown-none-elf/release/http_client" +# elf = "target/riscv32imc-unknown-none-elf/release/http_client" + +# Solution +firmware = 'target/riscv32imc-unknown-none-elf/release/examples/http-client' +elf = 'target/riscv32imc-unknown-none-elf/release/examples/http-client' diff --git a/intro/panic/diagram.json b/intro/panic/diagram.json new file mode 100644 index 0000000..31a7a6f --- /dev/null +++ b/intro/panic/diagram.json @@ -0,0 +1,33 @@ +{ + "version": 1, + "author": "Sergio Gasquez Arcos", + "editor": "wokwi", + "parts": [ + { + "type": "board-esp32-c3-rust-1", + "id": "esp", + "top": -126.57, + "left": 46.35, + "attrs": { + "builder": "rust-nostd-esp" + } + } + ], + "connections": [ + [ + "esp:21", + "$serialMonitor:RX", + "", + [] + ], + [ + "esp:20", + "$serialMonitor:TX", + "", + [] + ] + ], + "serialMonitor": { + "display": "auto" + } +} diff --git a/intro/panic/rust-toolchain.toml b/intro/panic/rust-toolchain.toml index b716762..446d6fe 100644 --- a/intro/panic/rust-toolchain.toml +++ b/intro/panic/rust-toolchain.toml @@ -1,4 +1,4 @@ [toolchain] -channel = "nightly-2023-06-25" +channel = "nightly-2023-11-14" components = ["rust-src"] targets = ["riscv32imc-unknown-none-elf"] diff --git a/intro/panic/wokwi.toml b/intro/panic/wokwi.toml new file mode 100644 index 0000000..e972006 --- /dev/null +++ b/intro/panic/wokwi.toml @@ -0,0 +1,9 @@ +[wokwi] +version = 1 +# Exercise +# firmware = "target/riscv32imc-unknown-none-elf/release/panic" +# elf = "target/riscv32imc-unknown-none-elf/release/panic" + +# Solution +firmware = 'target/riscv32imc-unknown-none-elf/release/examples/panic' +elf = 'target/riscv32imc-unknown-none-elf/release/examples/panic'