├── .gitignore ├── LICENSE ├── README.md ├── embassy_examples ├── basics │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── rust-toolchain.toml │ └── src │ │ └── main.rs ├── gpio │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── rust-toolchain.toml │ └── src │ │ └── main.rs ├── timer │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── rust-toolchain.toml │ └── src │ │ └── main.rs ├── uart-echo │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── rust-toolchain.toml │ └── src │ │ └── main.rs └── uart │ ├── .cargo │ └── config.toml │ ├── .github │ └── workflows │ │ └── rust_ci.yml │ ├── .gitignore │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── rust-toolchain.toml │ └── src │ └── main.rs ├── no_std_examples ├── .DS_Store ├── adc_analog_temp │ ├── .DS_Store │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── .vscode │ │ └── settings.json │ ├── Cargo.lock │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── rust-toolchain.toml │ └── src │ │ └── main.rs ├── gpio-interrupt │ ├── .DS_Store │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── .vscode │ │ └── settings.json │ ├── Cargo.lock │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── rust-toolchain.toml │ └── src │ │ └── main.rs ├── gpio │ ├── .DS_Store │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── .vscode │ │ └── settings.json │ ├── Cargo.lock │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── rust-toolchain.toml │ └── src │ │ └── main.rs ├── i2c │ ├── .DS_Store │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── .vscode │ │ └── settings.json │ ├── Cargo.lock │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── rust-toolchain.toml │ └── src │ │ └── main.rs ├── pwm buzzer │ ├── .DS_Store │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── .vscode │ │ └── settings.json │ ├── Cargo.lock │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── rust-toolchain.toml │ └── src │ │ └── main.rs ├── rmt │ ├── .DS_Store │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── .vscode │ │ └── settings.json │ ├── Cargo.lock │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── rust-toolchain.toml │ └── src │ │ └── main.rs ├── rng │ ├── .DS_Store │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── .vscode │ │ └── settings.json │ ├── Cargo.lock │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── rust-toolchain.toml │ └── src │ │ ├── main.rs │ │ └── nmea.rs ├── timer_polling │ ├── .DS_Store │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── .vscode │ │ └── settings.json │ ├── Cargo.lock │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── rust-toolchain.toml │ └── src │ │ └── main.rs ├── uart │ ├── .DS_Store │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── .vscode │ │ └── settings.json │ ├── Cargo.lock │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── rust-toolchain.toml │ └── src │ │ └── main.rs └── ultrasonic_timer │ ├── .DS_Store │ ├── .cargo │ └── config.toml │ ├── .gitignore │ ├── .vscode │ └── settings.json │ ├── Cargo.lock │ ├── Cargo.toml │ ├── LICENSE-APACHE │ ├── LICENSE-MIT │ ├── rust-toolchain.toml │ └── src │ └── main.rs ├── std_bt_examples ├── ble-advertise │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── build.rs │ ├── rust-toolchain.toml │ ├── sdkconfig.defaults │ └── src │ │ └── main.rs ├── ble-client │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── build.rs │ ├── rust-toolchain.toml │ ├── sdkconfig.defaults │ └── src │ │ └── main.rs ├── ble-secure-client │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── build.rs │ ├── rust-toolchain.toml │ ├── sdkconfig.defaults │ └── src │ │ └── main.rs ├── ble-secure-server │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── build.rs │ ├── rust-toolchain.toml │ ├── sdkconfig.defaults │ └── src │ │ └── main.rs ├── ble-server │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── build.rs │ ├── rust-toolchain.toml │ ├── sdkconfig.defaults │ └── src │ │ └── main.rs └── ble_scan │ ├── .cargo │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── build.rs │ ├── rust-toolchain.toml │ ├── sdkconfig.defaults │ └── src │ └── main.rs ├── std_examples ├── .DS_Store ├── adc │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── build.rs │ ├── rust-toolchain.toml │ ├── sdkconfig.defaults │ └── src │ │ └── main.rs ├── cli │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── build.rs │ ├── diagram.json │ ├── rust-toolchain.toml │ ├── sdkconfig.defaults │ ├── src │ │ └── main.rs │ └── wokwi.toml ├── gpio │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── build.rs │ ├── rust-toolchain.toml │ ├── sdkconfig.defaults │ └── src │ │ └── main.rs ├── gpio_interrupt │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── build.rs │ ├── rust-toolchain.toml │ ├── sdkconfig.defaults │ └── src │ │ └── main.rs ├── i2c │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── build.rs │ ├── rust-toolchain.toml │ ├── sdkconfig.defaults │ └── src │ │ └── main.rs ├── pwm │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── build.rs │ ├── rust-toolchain.toml │ ├── sdkconfig.defaults │ └── src │ │ └── main.rs ├── rtos_tasks │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── build.rs │ ├── rust-toolchain.toml │ ├── sdkconfig.defaults │ └── src │ │ └── main.rs ├── spi │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── build.rs │ ├── rust-toolchain.toml │ ├── sdkconfig.defaults │ └── src │ │ └── main.rs ├── timer │ ├── .cargo │ │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── build.rs │ ├── rust-toolchain.toml │ ├── sdkconfig.defaults │ └── src │ │ └── main.rs └── uart │ ├── .cargo │ └── config.toml │ ├── .gitignore │ ├── Cargo.toml │ ├── build.rs │ ├── rust-toolchain.toml │ ├── sdkconfig.defaults │ └── src │ └── main.rs └── std_net_examples ├── http_client ├── .cargo │ └── config.toml ├── .github │ └── workflows │ │ └── rust_ci.yml ├── .gitignore ├── Cargo.toml ├── build.rs ├── rust-toolchain.toml ├── sdkconfig.defaults └── src │ └── main.rs ├── http_server ├── .cargo │ └── config.toml ├── .github │ └── workflows │ │ └── rust_ci.yml ├── .gitignore ├── Cargo.toml ├── build.rs ├── rust-toolchain.toml ├── sdkconfig.defaults └── src │ └── main.rs ├── mqtt-sub ├── .cargo │ └── config.toml ├── .gitignore ├── Cargo.toml ├── build.rs ├── rust-toolchain.toml ├── sdkconfig.defaults └── src │ └── main.rs ├── ntp ├── .cargo │ └── config.toml ├── .gitignore ├── Cargo.toml ├── build.rs ├── rust-toolchain.toml ├── sdkconfig.defaults └── src │ └── main.rs ├── ping-cli ├── .cargo │ └── config.toml ├── .gitignore ├── Cargo.toml ├── build.rs ├── diagram.json ├── rust-toolchain.toml ├── sdkconfig.defaults ├── src │ └── main.rs └── wokwi.toml ├── ping ├── .cargo │ └── config.toml ├── .gitignore ├── Cargo.toml ├── build.rs ├── diagram.json ├── rust-toolchain.toml ├── sdkconfig.defaults ├── src │ └── main.rs └── wokwi.toml ├── wifi ├── .cargo │ └── config.toml ├── .gitignore ├── Cargo.toml ├── build.rs ├── rust-toolchain.toml ├── sdkconfig.defaults └── src │ └── main.rs └── wifi_revisit ├── .cargo └── config.toml ├── .gitignore ├── Cargo.toml ├── build.rs ├── diagram.json ├── rust-toolchain.toml ├── sdkconfig.defaults ├── src └── main.rs └── wokwi.toml /.gitignore: -------------------------------------------------------------------------------- 1 | # .gitignore file, derived from guthub examples: 2 | # https://github.com/github/gitignore 3 | 4 | 5 | ######################################################## 6 | ## - Rust.gitignore 7 | 8 | # Generated by Cargo 9 | # will have compiled files and executables 10 | debug/ 11 | target/ 12 | 13 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 14 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 15 | Cargo.lock 16 | 17 | # These are backup files generated by rustfmt 18 | **/*.rs.bk 19 | 20 | # MSVC Windows builds of rustc generate these, which store debugging information 21 | *.pdb 22 | 23 | ######################################################## 24 | ## Global/macOS.gitignore 25 | 26 | # General 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Icon must end with two \r 32 | Icon 33 | 34 | # Thumbnails 35 | ._* 36 | 37 | # Files that might appear in the root of a volume 38 | .DocumentRevisions-V100 39 | .fseventsd 40 | .Spotlight-V100 41 | .TemporaryItems 42 | .Trashes 43 | .VolumeIcon.icns 44 | .com.apple.timemachine.donotpresent 45 | 46 | # Directories potentially created on remote AFP share 47 | .AppleDB 48 | .AppleDesktop 49 | Network Trash Folder 50 | Temporary Items 51 | .apdisk 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rust Project Examples and Templates for the ESP32C3 2 | 3 | This repository contains example projects that can also be used as templates written in the Rust programming language targeting the ESP32C3 by Espressif. 4 | ESP32C3 is a single-core Wi-Fi and Bluetooth 5 (LE) microcontroller SoC, based on the open-source RISC-V architecture. 5 | 6 | Each of these these projects has an accompanying blog post explaining the code in more detail [here](https://apollolabsblog.hashnode.dev/). Each project also has an accompnaying Wokwi simulation. 7 | 8 | These templates were generated by `cargo-generate` using the `esp-template` repo for `no-std` applications. To used `cargo-generate` to generate your own template project, you can follow the instructions in the Rust ESP Book [here](https://esp-rs.github.io/book/writing-your-own-application/generate-project-from-template.html#esp-template). 9 | -------------------------------------------------------------------------------- /embassy_examples/basics/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.riscv32imc-unknown-none-elf] 2 | runner = "espflash flash --monitor" 3 | 4 | 5 | [build] 6 | rustflags = [ 7 | "-C", "link-arg=-Tlinkall.x", 8 | 9 | # Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.) 10 | # NOTE: May negatively impact performance of produced code 11 | "-C", "force-frame-pointers", 12 | 13 | # comment the cfgs below if you do _not_ wish to emulate atomics. 14 | # enable the atomic codegen option for RISCV 15 | # "-C", "target-feature=+a", 16 | # # tell the core library have atomics even though it's not specified in the target definition 17 | # "--cfg", "target_has_atomic_load_store", 18 | # "--cfg", 'target_has_atomic_load_store="8"', 19 | # "--cfg", 'target_has_atomic_load_store="16"', 20 | # "--cfg", 'target_has_atomic_load_store="32"', 21 | # "--cfg", 'target_has_atomic_load_store="ptr"', 22 | # # enable cas 23 | # "--cfg", "target_has_atomic", 24 | # "--cfg", 'target_has_atomic="8"', 25 | # "--cfg", 'target_has_atomic="16"', 26 | # "--cfg", 'target_has_atomic="32"', 27 | # "--cfg", 'target_has_atomic="ptr"', 28 | ] 29 | 30 | target = "riscv32imc-unknown-none-elf" 31 | 32 | [unstable] 33 | build-std = ["core"] 34 | -------------------------------------------------------------------------------- /embassy_examples/basics/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # These are backup files generated by rustfmt 7 | **/*.rs.bk 8 | 9 | # MSVC Windows builds of rustc generate these, which store debugging information 10 | *.pdb 11 | -------------------------------------------------------------------------------- /embassy_examples/basics/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "basics" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev "] 5 | edition = "2021" 6 | license = "MIT OR Apache-2.0" 7 | 8 | [dependencies] 9 | esp32c3-hal = { version = "0.13.0", features = ["async", "embassy", "embassy-time-timg0"] } 10 | esp-backtrace = { version = "0.9.0", features = [ 11 | "esp32c3", 12 | "panic-handler", 13 | "exception-handler", 14 | "print-uart", 15 | ] } 16 | esp-println = { version = "0.7.0", features = ["esp32c3"] } 17 | embassy-executor = { version = "0.3.0", features = ["nightly", "integrated-timers", "arch-riscv32", "executor-thread"] } 18 | embassy-time = { version = "0.1.5", features = ["nightly"] } 19 | embassy-sync = "0.5.0" 20 | embedded-hal-async = "1.0.0-rc.1" 21 | embedded-io-async = "0.6.1" 22 | portable-atomic = "1.5.1" -------------------------------------------------------------------------------- /embassy_examples/basics/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright [year] [fullname] 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /embassy_examples/basics/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | targets = ["riscv32imc-unknown-none-elf"] 5 | 6 | -------------------------------------------------------------------------------- /embassy_examples/basics/src/main.rs: -------------------------------------------------------------------------------- 1 | /* 2 | For a detailed explanation of this code check out the associated blog post: 3 | https://apollolabsblog.hashnode.dev/embassy-on-esp-getting-started 4 | 5 | GitHub Repo containing source code and other examples: 6 | https://github.com/apollolabsdev 7 | 8 | For notifications on similar examples and more, subscribe to newsletter here: 9 | https://www.theembeddedrustacean.com/subscribe 10 | */ 11 | 12 | #![no_std] 13 | #![no_main] 14 | #![feature(type_alias_impl_trait)] 15 | 16 | use embassy_executor::Spawner; 17 | use embassy_time::{Duration, Timer}; 18 | use esp32c3_hal::{clock::ClockControl, embassy, peripherals::Peripherals, prelude::*}; 19 | use esp_backtrace as _; 20 | use esp_println::print; 21 | 22 | #[embassy_executor::task] 23 | async fn one_second_task() { 24 | let mut count = 0; 25 | loop { 26 | esp_println::println!("Spawn Task Count: {}", count); 27 | count += 1; 28 | Timer::after(Duration::from_millis(1_000)).await; 29 | } 30 | } 31 | 32 | #[main] 33 | async fn main(spawner: Spawner) { 34 | let peripherals = Peripherals::take(); 35 | let system = peripherals.SYSTEM.split(); 36 | let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); 37 | 38 | embassy::init( 39 | &clocks, 40 | esp32c3_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks).timer0, 41 | ); 42 | 43 | spawner.spawn(one_second_task()).unwrap(); 44 | 45 | // This line is for Wokwi only so that the console output is formatted correctly 46 | print!("\x1b[20h"); 47 | 48 | let mut count = 0; 49 | loop { 50 | esp_println::println!("Main Task Count: {}", count); 51 | count += 1; 52 | Timer::after(Duration::from_millis(5_000)).await; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /embassy_examples/gpio/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.riscv32imc-unknown-none-elf] 2 | runner = "espflash flash --monitor" 3 | 4 | 5 | [build] 6 | rustflags = [ 7 | "-C", "link-arg=-Tlinkall.x", 8 | 9 | # Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.) 10 | # NOTE: May negatively impact performance of produced code 11 | "-C", "force-frame-pointers", 12 | 13 | # comment the cfgs below if you do _not_ wish to emulate atomics. 14 | # enable the atomic codegen option for RISCV 15 | # "-C", "target-feature=+a", 16 | # # tell the core library have atomics even though it's not specified in the target definition 17 | # "--cfg", "target_has_atomic_load_store", 18 | # "--cfg", 'target_has_atomic_load_store="8"', 19 | # "--cfg", 'target_has_atomic_load_store="16"', 20 | # "--cfg", 'target_has_atomic_load_store="32"', 21 | # "--cfg", 'target_has_atomic_load_store="ptr"', 22 | # # enable cas 23 | # "--cfg", "target_has_atomic", 24 | # "--cfg", 'target_has_atomic="8"', 25 | # "--cfg", 'target_has_atomic="16"', 26 | # "--cfg", 'target_has_atomic="32"', 27 | # "--cfg", 'target_has_atomic="ptr"', 28 | ] 29 | 30 | target = "riscv32imc-unknown-none-elf" 31 | 32 | [unstable] 33 | build-std = ["core"] 34 | -------------------------------------------------------------------------------- /embassy_examples/gpio/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # These are backup files generated by rustfmt 7 | **/*.rs.bk 8 | 9 | # MSVC Windows builds of rustc generate these, which store debugging information 10 | *.pdb 11 | -------------------------------------------------------------------------------- /embassy_examples/gpio/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "gpio" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev "] 5 | edition = "2021" 6 | license = "MIT OR Apache-2.0" 7 | 8 | [dependencies] 9 | esp32c3-hal = { version = "0.13.0", features = ["async", "embassy", "embassy-time-timg0"] } 10 | esp-backtrace = { version = "0.9.0", features = [ 11 | "esp32c3", 12 | "panic-handler", 13 | "exception-handler", 14 | "print-uart", 15 | ] } 16 | esp-println = { version = "0.7.0", features = ["esp32c3"] } 17 | embassy-executor = { version = "0.3.0", features = ["nightly", "integrated-timers", "arch-riscv32", "executor-thread"] } 18 | embassy-time = { version = "0.1.5", features = ["nightly"] } 19 | embassy-sync = "0.5.0" 20 | embedded-hal-async = "1.0.0-rc.1" 21 | embedded-io-async = "0.6.1" 22 | portable-atomic = "1.5.1" -------------------------------------------------------------------------------- /embassy_examples/gpio/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright [year] [fullname] 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /embassy_examples/gpio/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | targets = ["riscv32imc-unknown-none-elf"] 5 | 6 | -------------------------------------------------------------------------------- /embassy_examples/timer/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.riscv32imc-unknown-none-elf] 2 | runner = "espflash flash --monitor" 3 | 4 | 5 | [build] 6 | rustflags = [ 7 | "-C", "link-arg=-Tlinkall.x", 8 | 9 | # Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.) 10 | # NOTE: May negatively impact performance of produced code 11 | "-C", "force-frame-pointers", 12 | ] 13 | 14 | target = "riscv32imc-unknown-none-elf" 15 | 16 | [unstable] 17 | build-std = ["core"] 18 | -------------------------------------------------------------------------------- /embassy_examples/timer/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # These are backup files generated by rustfmt 7 | **/*.rs.bk 8 | 9 | # MSVC Windows builds of rustc generate these, which store debugging information 10 | *.pdb 11 | -------------------------------------------------------------------------------- /embassy_examples/timer/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "timer" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev "] 5 | edition = "2021" 6 | license = "MIT OR Apache-2.0" 7 | 8 | [dependencies] 9 | esp32c3-hal = { version = "0.14.0", features = ["async", "embassy", "embassy-executor-thread", "embassy-time-timg0"] } 10 | esp-backtrace = { version = "0.9.0", features = ["esp32c3", "panic-handler", "exception-handler", "print-uart"] } 11 | esp-println = { version = "0.7.1", features = ["esp32c3"] } 12 | embassy-executor = { version = "0.4.0", features = ["nightly"] } 13 | embassy-time = { version = "0.2.0" } 14 | embassy-sync = "0.5.0" 15 | embedded-hal-async = "=1.0.0-rc.2" 16 | embedded-io-async = "0.6.1" 17 | static_cell = { version = "2.0.0", features = ["nightly"] } -------------------------------------------------------------------------------- /embassy_examples/timer/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright [year] [fullname] 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /embassy_examples/timer/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | targets = ["riscv32imc-unknown-none-elf"] 5 | 6 | -------------------------------------------------------------------------------- /embassy_examples/uart-echo/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.riscv32imc-unknown-none-elf] 2 | runner = "espflash flash --monitor" 3 | 4 | 5 | [build] 6 | rustflags = [ 7 | "-C", "link-arg=-Tlinkall.x", 8 | 9 | # Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.) 10 | # NOTE: May negatively impact performance of produced code 11 | "-C", "force-frame-pointers", 12 | ] 13 | 14 | target = "riscv32imc-unknown-none-elf" 15 | 16 | [unstable] 17 | build-std = ["core"] 18 | -------------------------------------------------------------------------------- /embassy_examples/uart-echo/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # These are backup files generated by rustfmt 7 | **/*.rs.bk 8 | 9 | # MSVC Windows builds of rustc generate these, which store debugging information 10 | *.pdb 11 | -------------------------------------------------------------------------------- /embassy_examples/uart-echo/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "uart" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev "] 5 | edition = "2021" 6 | license = "MIT OR Apache-2.0" 7 | 8 | [dependencies] 9 | esp32c3-hal = { version = "0.14.0", features = ["async", "embassy", "embassy-executor-thread", "embassy-time-timg0"] } 10 | esp-backtrace = { version = "0.9.0", features = ["esp32c3", "panic-handler", "exception-handler", "print-uart"] } 11 | esp-println = { version = "0.7.1", features = ["esp32c3"] } 12 | embassy-executor = { version = "0.4.0", features = ["nightly"] } 13 | embassy-time = { version = "0.2.0" } 14 | embassy-sync = "0.5.0" 15 | embedded-hal-async = "=1.0.0-rc.2" 16 | embedded-io-async = "0.6.1" 17 | static_cell = { version = "2.0.0", features = ["nightly"] } -------------------------------------------------------------------------------- /embassy_examples/uart-echo/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright [year] [fullname] 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /embassy_examples/uart-echo/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | targets = ["riscv32imc-unknown-none-elf"] 5 | 6 | -------------------------------------------------------------------------------- /embassy_examples/uart/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.riscv32imc-unknown-none-elf] 2 | runner = "espflash flash --monitor" 3 | 4 | 5 | [build] 6 | rustflags = [ 7 | "-C", "link-arg=-Tlinkall.x", 8 | 9 | # Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.) 10 | # NOTE: May negatively impact performance of produced code 11 | "-C", "force-frame-pointers", 12 | 13 | # comment the cfgs below if you do _not_ wish to emulate atomics. 14 | # enable the atomic codegen option for RISCV 15 | # "-C", "target-feature=+a", 16 | # # tell the core library have atomics even though it's not specified in the target definition 17 | # "--cfg", "target_has_atomic_load_store", 18 | # "--cfg", 'target_has_atomic_load_store="8"', 19 | # "--cfg", 'target_has_atomic_load_store="16"', 20 | # "--cfg", 'target_has_atomic_load_store="32"', 21 | # "--cfg", 'target_has_atomic_load_store="ptr"', 22 | # # enable cas 23 | # "--cfg", "target_has_atomic", 24 | # "--cfg", 'target_has_atomic="8"', 25 | # "--cfg", 'target_has_atomic="16"', 26 | # "--cfg", 'target_has_atomic="32"', 27 | # "--cfg", 'target_has_atomic="ptr"', 28 | ] 29 | 30 | target = "riscv32imc-unknown-none-elf" 31 | 32 | [unstable] 33 | build-std = ["core"] 34 | -------------------------------------------------------------------------------- /embassy_examples/uart/.github/workflows/rust_ci.yml: -------------------------------------------------------------------------------- 1 | name: Continuous Integration 2 | 3 | on: 4 | push: 5 | paths-ignore: 6 | - "**/README.md" 7 | pull_request: 8 | workflow_dispatch: 9 | 10 | env: 11 | CARGO_TERM_COLOR: always 12 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 13 | 14 | jobs: 15 | rust-checks: 16 | name: Rust Checks 17 | runs-on: ubuntu-latest 18 | strategy: 19 | fail-fast: false 20 | matrix: 21 | action: 22 | - command: build 23 | args: --release 24 | - command: fmt 25 | args: --all -- --check --color always 26 | - command: clippy 27 | args: --all-features --workspace -- -D warnings 28 | steps: 29 | - name: Checkout repository 30 | uses: actions/checkout@v4 31 | - name: Enable caching 32 | uses: Swatinem/rust-cache@v2 33 | - name: Setup Rust 34 | uses: dtolnay/rust-toolchain@v1 35 | with: 36 | target: riscv32imc-unknown-none-elf 37 | toolchain: nightly 38 | components: rust-src, rustfmt, clippy 39 | - name: Run command 40 | run: cargo ${{ matrix.action.command }} ${{ matrix.action.args }} 41 | -------------------------------------------------------------------------------- /embassy_examples/uart/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # These are backup files generated by rustfmt 7 | **/*.rs.bk 8 | 9 | # MSVC Windows builds of rustc generate these, which store debugging information 10 | *.pdb 11 | -------------------------------------------------------------------------------- /embassy_examples/uart/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "uart" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev "] 5 | edition = "2021" 6 | license = "MIT OR Apache-2.0" 7 | 8 | [dependencies] 9 | esp32c3-hal = { version = "0.13.0", features = ["async", "embassy", "embassy-time-timg0"] } 10 | esp-backtrace = { version = "0.9.0", features = [ 11 | "esp32c3", 12 | "panic-handler", 13 | "exception-handler", 14 | "print-uart", 15 | ] } 16 | esp-println = { version = "0.7.0", features = ["esp32c3"] } 17 | embassy-executor = { version = "0.3.0", features = ["nightly", "integrated-timers", "arch-riscv32", "executor-thread"] } 18 | embassy-time = { version = "0.1.5", features = ["nightly"] } 19 | embassy-sync = "0.5.0" 20 | embedded-hal-async = "1.0.0-rc.1" 21 | embedded-io-async = "0.6.1" 22 | portable-atomic = "1.5.1" -------------------------------------------------------------------------------- /embassy_examples/uart/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright [year] [fullname] 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /embassy_examples/uart/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | targets = ["riscv32imc-unknown-none-elf"] 5 | 6 | -------------------------------------------------------------------------------- /embassy_examples/uart/src/main.rs: -------------------------------------------------------------------------------- 1 | /* 2 | For a detailed explanation of this code check out the associated blog post: 3 | https://apollolabsblog.hashnode.dev/embassy-on-esp-uart-transmitter 4 | 5 | GitHub Repo containing source code and other examples: 6 | https://github.com/apollolabsdev 7 | 8 | For notifications on similar examples and more, subscribe to newsletter here: 9 | https://www.theembeddedrustacean.com/subscribe 10 | */ 11 | 12 | #![no_std] 13 | #![no_main] 14 | #![feature(type_alias_impl_trait)] 15 | 16 | use embassy_executor::Spawner; 17 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; 18 | use embassy_sync::signal::Signal; 19 | use embedded_hal_async::digital::Wait; 20 | use esp32c3_hal::{ 21 | clock::ClockControl, 22 | embassy, interrupt, 23 | peripherals::{Interrupt, Peripherals, UART0}, 24 | prelude::*, 25 | Uart, UartTx, IO, 26 | }; 27 | use esp_backtrace as _; 28 | 29 | static MYSIGNAL: Signal = Signal::new(); 30 | 31 | #[embassy_executor::task] 32 | async fn uart_writer(mut tx: UartTx<'static, UART0>) { 33 | embedded_io_async::Write::write( 34 | &mut tx, 35 | b"UART Task Spawned. Waiting for Button Press...\r\n", 36 | ) 37 | .await 38 | .unwrap(); 39 | loop { 40 | let press_count = MYSIGNAL.wait().await; 41 | esp_println::println!("Button Pressed {} time(s)", press_count); 42 | } 43 | } 44 | 45 | #[main] 46 | async fn main(spawner: Spawner) { 47 | let peripherals = Peripherals::take(); 48 | let system = peripherals.SYSTEM.split(); 49 | let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); 50 | 51 | // Initilize Embassy Timers 52 | embassy::init( 53 | &clocks, 54 | esp32c3_hal::timer::TimerGroup::new(peripherals.TIMG0, &clocks).timer0, 55 | ); 56 | 57 | // Configure UART 58 | let uart0 = Uart::new(peripherals.UART0, &clocks); 59 | let (tx, _) = uart0.split(); 60 | 61 | // Configure GPIO 62 | let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); 63 | let mut button = io.pins.gpio2.into_pull_up_input(); 64 | 65 | // Enable Interrupts for GPIO 66 | interrupt::enable(Interrupt::GPIO, interrupt::Priority::Priority1).unwrap(); 67 | 68 | spawner.spawn(uart_writer(tx)).ok(); 69 | 70 | let mut press_count = 0; 71 | loop { 72 | // Detect and Count Button Presses 73 | button.wait_for_rising_edge().await.unwrap(); 74 | press_count += 1; 75 | // Signal Press Count 76 | MYSIGNAL.signal(press_count); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /no_std_examples/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theembeddedrustacean/learn-esp32c3-rs/41efd9d1bbdf8d2e071332af610bae0515407d07/no_std_examples/.DS_Store -------------------------------------------------------------------------------- /no_std_examples/adc_analog_temp/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theembeddedrustacean/learn-esp32c3-rs/41efd9d1bbdf8d2e071332af610bae0515407d07/no_std_examples/adc_analog_temp/.DS_Store -------------------------------------------------------------------------------- /no_std_examples/adc_analog_temp/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.riscv32imc-unknown-none-elf] 2 | runner = "espflash flash --monitor" 3 | 4 | [build] 5 | rustflags = [ 6 | "-C", "link-arg=-Tlinkall.x", 7 | # Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.) 8 | # NOTE: May negatively impact performance of produced code 9 | "-C", "force-frame-pointers", 10 | 11 | # comment the cfgs below if you do _not_ wish to emulate atomics. 12 | # enable the atomic codegen option for RISCV 13 | "-C", "target-feature=+a", 14 | # tell the core library have atomics even though it's not specified in the target definition 15 | "--cfg", "target_has_atomic_load_store", 16 | "--cfg", 'target_has_atomic_load_store="8"', 17 | "--cfg", 'target_has_atomic_load_store="16"', 18 | "--cfg", 'target_has_atomic_load_store="32"', 19 | "--cfg", 'target_has_atomic_load_store="ptr"', 20 | # enable cas 21 | "--cfg", "target_has_atomic", 22 | "--cfg", 'target_has_atomic="8"', 23 | "--cfg", 'target_has_atomic="16"', 24 | "--cfg", 'target_has_atomic="32"', 25 | "--cfg", 'target_has_atomic="ptr"', 26 | ] 27 | 28 | target = "riscv32imc-unknown-none-elf" 29 | 30 | [unstable] 31 | build-std = ["core"] 32 | -------------------------------------------------------------------------------- /no_std_examples/adc_analog_temp/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # These are backup files generated by rustfmt 7 | **/*.rs.bk 8 | 9 | # MSVC Windows builds of rustc generate these, which store debugging information 10 | *.pdb 11 | -------------------------------------------------------------------------------- /no_std_examples/adc_analog_temp/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "rust-analyzer.checkOnSave.allTargets": false, 3 | } 4 | -------------------------------------------------------------------------------- /no_std_examples/adc_analog_temp/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "timer_polling" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | license = "MIT OR Apache-2.0" 7 | 8 | [dependencies] 9 | esp32c3-hal = "0.8.0" 10 | esp-backtrace = { version = "0.6.0", features = [ 11 | "esp32c3", 12 | "panic-handler", 13 | "exception-handler", 14 | "print-uart", 15 | ] } 16 | esp-println = { version = "0.5.0", features = ["esp32c3"] } 17 | fugit = "0.3.6" 18 | libm = "0.2.6" -------------------------------------------------------------------------------- /no_std_examples/adc_analog_temp/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright [year] [fullname] 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /no_std_examples/adc_analog_temp/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | -------------------------------------------------------------------------------- /no_std_examples/adc_analog_temp/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | use esp32c3_hal::{ 5 | adc::{AdcConfig, Attenuation, ADC}, 6 | clock::ClockControl, 7 | peripherals::Peripherals, 8 | prelude::*, 9 | timer::TimerGroup, 10 | Rtc, IO, 11 | }; 12 | use esp_backtrace as _; 13 | use esp_println::println; 14 | use libm::log; 15 | 16 | #[entry] 17 | fn main() -> ! { 18 | // Take Peripherals, Initialize Clocks, and Create a Handle for Each 19 | let peripherals = Peripherals::take(); 20 | let mut system = peripherals.SYSTEM.split(); 21 | let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); 22 | 23 | // Instantiate and Create Handles for the RTC and TIMG watchdog timers 24 | let mut rtc = Rtc::new(peripherals.RTC_CNTL); 25 | let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks); 26 | let mut wdt0 = timer_group0.wdt; 27 | let timer_group1 = TimerGroup::new(peripherals.TIMG1, &clocks); 28 | let mut wdt1 = timer_group1.wdt; 29 | 30 | // Disable the RTC and TIMG watchdog timers 31 | rtc.swd.disable(); 32 | rtc.rwdt.disable(); 33 | wdt0.disable(); 34 | wdt1.disable(); 35 | 36 | // Instantiate and Create Handle for IO 37 | let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); 38 | 39 | // Create ADC Instance 40 | // Create handle for ADC configuration parameters 41 | let mut adc_config = AdcConfig::new(); 42 | // Configure ADC pin 43 | let mut adc_pin = 44 | adc_config.enable_pin(io.pins.gpio1.into_analog(), Attenuation::Attenuation0dB); 45 | // Promote ADC peripheral to HAL-level Struct 46 | let analog = peripherals.APB_SARADC.split(); 47 | // Create handle for ADC, configuring clock, and passing configuration handle 48 | let mut adc = ADC::adc( 49 | &mut system.peripheral_clock_control, 50 | analog.adc1, 51 | adc_config, 52 | ) 53 | .unwrap(); 54 | 55 | const B: f64 = 3950.0; // B value of the thermistor 56 | 57 | // Algorithm 58 | // 1) Get adc reading 59 | // 2) Convert to temperature 60 | // 3) Print to Console 61 | // 4) Go Back to step 1 62 | 63 | // Application 64 | loop { 65 | // Get ADC reading 66 | let sample: u16 = adc.read(&mut adc_pin).unwrap(); 67 | // For blocking read 68 | // let sample: u16 = nb::block!(adc.read(&mut adc_pin)).unwrap(); 69 | 70 | //Convert to temperature 71 | let temperature = 1. / (log(1. / (4096. / sample as f64 - 1.)) / B + 1.0 / 298.15) - 273.15; 72 | 73 | // Print the temperature output 74 | println!("Temperature {:02} Celcius\r", temperature); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /no_std_examples/gpio-interrupt/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theembeddedrustacean/learn-esp32c3-rs/41efd9d1bbdf8d2e071332af610bae0515407d07/no_std_examples/gpio-interrupt/.DS_Store -------------------------------------------------------------------------------- /no_std_examples/gpio-interrupt/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.riscv32imc-unknown-none-elf] 2 | runner = "espflash flash --monitor" 3 | 4 | [build] 5 | rustflags = [ 6 | "-C", "link-arg=-Tlinkall.x", 7 | # Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.) 8 | # NOTE: May negatively impact performance of produced code 9 | "-C", "force-frame-pointers", 10 | 11 | # comment the cfgs below if you do _not_ wish to emulate atomics. 12 | # enable the atomic codegen option for RISCV 13 | "-C", "target-feature=+a", 14 | # tell the core library have atomics even though it's not specified in the target definition 15 | "--cfg", "target_has_atomic_load_store", 16 | "--cfg", 'target_has_atomic_load_store="8"', 17 | "--cfg", 'target_has_atomic_load_store="16"', 18 | "--cfg", 'target_has_atomic_load_store="32"', 19 | "--cfg", 'target_has_atomic_load_store="ptr"', 20 | # enable cas 21 | "--cfg", "target_has_atomic", 22 | "--cfg", 'target_has_atomic="8"', 23 | "--cfg", 'target_has_atomic="16"', 24 | "--cfg", 'target_has_atomic="32"', 25 | "--cfg", 'target_has_atomic="ptr"', 26 | ] 27 | 28 | target = "riscv32imc-unknown-none-elf" 29 | 30 | [unstable] 31 | build-std = ["core"] 32 | -------------------------------------------------------------------------------- /no_std_examples/gpio-interrupt/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # These are backup files generated by rustfmt 7 | **/*.rs.bk 8 | 9 | # MSVC Windows builds of rustc generate these, which store debugging information 10 | *.pdb 11 | -------------------------------------------------------------------------------- /no_std_examples/gpio-interrupt/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "rust-analyzer.checkOnSave.allTargets": false, 3 | "cmake.configureOnOpen": false, 4 | } 5 | -------------------------------------------------------------------------------- /no_std_examples/gpio-interrupt/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "gpio" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | license = "MIT OR Apache-2.0" 7 | 8 | [dependencies] 9 | esp32c3-hal = "0.9.0" 10 | esp-backtrace = { version = "0.7.0", features = [ 11 | "esp32c3", 12 | "panic-handler", 13 | "print-uart", 14 | ] } 15 | esp-println = { version = "0.5.0", features = ["esp32c3"] } 16 | critical-section = "1.1.1" 17 | -------------------------------------------------------------------------------- /no_std_examples/gpio-interrupt/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright [year] [fullname] 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /no_std_examples/gpio-interrupt/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | -------------------------------------------------------------------------------- /no_std_examples/gpio/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theembeddedrustacean/learn-esp32c3-rs/41efd9d1bbdf8d2e071332af610bae0515407d07/no_std_examples/gpio/.DS_Store -------------------------------------------------------------------------------- /no_std_examples/gpio/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.riscv32imc-unknown-none-elf] 2 | runner = "espflash flash --monitor" 3 | 4 | [build] 5 | rustflags = [ 6 | "-C", "link-arg=-Tlinkall.x", 7 | # Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.) 8 | # NOTE: May negatively impact performance of produced code 9 | "-C", "force-frame-pointers", 10 | 11 | # comment the cfgs below if you do _not_ wish to emulate atomics. 12 | # enable the atomic codegen option for RISCV 13 | "-C", "target-feature=+a", 14 | # tell the core library have atomics even though it's not specified in the target definition 15 | "--cfg", "target_has_atomic_load_store", 16 | "--cfg", 'target_has_atomic_load_store="8"', 17 | "--cfg", 'target_has_atomic_load_store="16"', 18 | "--cfg", 'target_has_atomic_load_store="32"', 19 | "--cfg", 'target_has_atomic_load_store="ptr"', 20 | # enable cas 21 | "--cfg", "target_has_atomic", 22 | "--cfg", 'target_has_atomic="8"', 23 | "--cfg", 'target_has_atomic="16"', 24 | "--cfg", 'target_has_atomic="32"', 25 | "--cfg", 'target_has_atomic="ptr"', 26 | ] 27 | 28 | target = "riscv32imc-unknown-none-elf" 29 | 30 | [unstable] 31 | build-std = ["core"] 32 | -------------------------------------------------------------------------------- /no_std_examples/gpio/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # These are backup files generated by rustfmt 7 | **/*.rs.bk 8 | 9 | # MSVC Windows builds of rustc generate these, which store debugging information 10 | *.pdb 11 | -------------------------------------------------------------------------------- /no_std_examples/gpio/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "rust-analyzer.checkOnSave.allTargets": false, 3 | } 4 | -------------------------------------------------------------------------------- /no_std_examples/gpio/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "gpio" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | license = "MIT OR Apache-2.0" 7 | 8 | [dependencies] 9 | esp32c3-hal = "0.2.0" 10 | esp-backtrace = { version = "0.6.0", features = [ 11 | "esp32c3", 12 | "panic-handler", 13 | "print-uart", 14 | ] } 15 | riscv-rt = { version = "0.11.0", optional = true } 16 | 17 | [features] 18 | default = ["rt"] 19 | rt = ["riscv-rt"] -------------------------------------------------------------------------------- /no_std_examples/gpio/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright [year] [fullname] 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /no_std_examples/gpio/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | -------------------------------------------------------------------------------- /no_std_examples/gpio/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | use esp32c3_hal::{clock::ClockControl, pac::Peripherals, prelude::*, timer::TimerGroup, Rtc, IO}; 5 | use esp_backtrace as _; 6 | use riscv_rt::entry; 7 | 8 | #[entry] 9 | fn main() -> ! { 10 | // Take Peripherals, Initialize Clocks, and Create a Handle for Each 11 | let peripherals = Peripherals::take().unwrap(); 12 | let system = peripherals.SYSTEM.split(); 13 | let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); 14 | 15 | // Instantiate and Create Handles for the RTC and TIMG watchdog timers 16 | let mut rtc = Rtc::new(peripherals.RTC_CNTL); 17 | let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks); 18 | let mut wdt0 = timer_group0.wdt; 19 | let timer_group1 = TimerGroup::new(peripherals.TIMG1, &clocks); 20 | let mut wdt1 = timer_group1.wdt; 21 | 22 | // Disable the RTC and TIMG watchdog timers 23 | rtc.swd.disable(); 24 | rtc.rwdt.disable(); 25 | wdt0.disable(); 26 | wdt1.disable(); 27 | 28 | // Instantiate and Create Handle for IO 29 | let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); 30 | 31 | // Instantiate and Create Handle for LED output & Button Input 32 | let mut led = io.pins.gpio4.into_push_pull_output(); 33 | let button = io.pins.gpio0.into_pull_up_input(); 34 | 35 | // Create and initialize a delay variable to manage delay loop 36 | let mut del_var = 10_0000_u32; 37 | 38 | // Initialize LED to on or off 39 | led.set_low().unwrap(); 40 | 41 | // Application Loop 42 | loop { 43 | for _i in 1..del_var { 44 | // Check if button got pressed 45 | if button.is_low().unwrap() { 46 | // If button pressed decrease the delay value 47 | del_var = del_var - 2_5000_u32; 48 | // If updated delay value reaches zero then reset it back to starting value 49 | if del_var < 2_5000 { 50 | del_var = 10_0000_u32; 51 | } 52 | } 53 | } 54 | // Toggle LED 55 | led.toggle().unwrap(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /no_std_examples/i2c/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theembeddedrustacean/learn-esp32c3-rs/41efd9d1bbdf8d2e071332af610bae0515407d07/no_std_examples/i2c/.DS_Store -------------------------------------------------------------------------------- /no_std_examples/i2c/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.riscv32imc-unknown-none-elf] 2 | runner = "espflash flash --monitor" 3 | 4 | [build] 5 | rustflags = [ 6 | "-C", "link-arg=-Tlinkall.x", 7 | # Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.) 8 | # NOTE: May negatively impact performance of produced code 9 | "-C", "force-frame-pointers", 10 | 11 | # comment the cfgs below if you do _not_ wish to emulate atomics. 12 | # enable the atomic codegen option for RISCV 13 | # "-C", "target-feature=+a", 14 | # tell the core library have atomics even though it's not specified in the target definition 15 | # "--cfg", "target_has_atomic_load_store", 16 | # "--cfg", 'target_has_atomic_load_store="8"', 17 | # "--cfg", 'target_has_atomic_load_store="16"', 18 | # "--cfg", 'target_has_atomic_load_store="32"', 19 | # "--cfg", 'target_has_atomic_load_store="ptr"', 20 | # # enable cas 21 | # "--cfg", "target_has_atomic", 22 | # "--cfg", 'target_has_atomic="8"', 23 | # "--cfg", 'target_has_atomic="16"', 24 | # "--cfg", 'target_has_atomic="32"', 25 | # "--cfg", 'target_has_atomic="ptr"', 26 | ] 27 | 28 | target = "riscv32imc-unknown-none-elf" 29 | 30 | [unstable] 31 | build-std = ["core"] 32 | -------------------------------------------------------------------------------- /no_std_examples/i2c/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # These are backup files generated by rustfmt 7 | **/*.rs.bk 8 | 9 | # MSVC Windows builds of rustc generate these, which store debugging information 10 | *.pdb 11 | -------------------------------------------------------------------------------- /no_std_examples/i2c/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "rust-analyzer.checkOnSave.allTargets": false, 3 | } 4 | -------------------------------------------------------------------------------- /no_std_examples/i2c/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "i2c" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | license = "MIT OR Apache-2.0" 7 | 8 | [dependencies] 9 | esp32c3-hal = "0.15.0" 10 | esp-backtrace = { version = "0.10.0", features = [ 11 | "esp32c3", 12 | "panic-handler", 13 | "exception-handler", 14 | "print-uart", 15 | ] } 16 | esp-println = { version = "0.8.0", features = ["esp32c3"] } -------------------------------------------------------------------------------- /no_std_examples/i2c/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright [year] [fullname] 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /no_std_examples/i2c/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | -------------------------------------------------------------------------------- /no_std_examples/i2c/src/main.rs: -------------------------------------------------------------------------------- 1 | /* 2 | For a detailed explanation of this code check out the associated blog post: 3 | https://apollolabsblog.hashnode.dev/embassy-on-esp-i2c-scanner 4 | 5 | GitHub Repo containing source code and other examples: 6 | https://github.com/apollolabsdev 7 | 8 | For notifications on similar examples and more, subscribe to newsletter here: 9 | https://www.theembeddedrustacean.com/subscribe 10 | */ 11 | 12 | #![no_std] 13 | #![no_main] 14 | #![feature(type_alias_impl_trait)] 15 | 16 | use esp32c3_hal::{clock::ClockControl, i2c::I2C, peripherals::Peripherals, prelude::*, IO}; 17 | use esp_backtrace as _; 18 | use esp_println::println; 19 | 20 | #[entry] 21 | fn main() -> ! { 22 | let peripherals = Peripherals::take(); 23 | let system = peripherals.SYSTEM.split(); 24 | let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); 25 | 26 | // Obtain handle for GPIO 27 | let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); 28 | 29 | // Initialize and configure I2C0 30 | let mut i2c0 = I2C::new( 31 | peripherals.I2C0, 32 | io.pins.gpio3, 33 | io.pins.gpio2, 34 | 100u32.kHz(), 35 | &clocks, 36 | ); 37 | 38 | // This line is for Wokwi only so that the console output is formatted correctly 39 | // esp_println::print!("\x1b[20h"); 40 | 41 | // Start Scan at Address 1 going up to 127 42 | for addr in 1..=127 { 43 | println!("Scanning Address {}", addr as u8); 44 | 45 | // Scan Address 46 | let res = i2c0.read(addr as u8, &mut [0]); 47 | 48 | // Check and Print Result 49 | match res { 50 | Ok(_) => println!("Device Found at Address {}", addr as u8), 51 | Err(_) => println!("No Device Found"), 52 | } 53 | } 54 | 55 | // Loop Forever 56 | loop {} 57 | } 58 | -------------------------------------------------------------------------------- /no_std_examples/pwm buzzer/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theembeddedrustacean/learn-esp32c3-rs/41efd9d1bbdf8d2e071332af610bae0515407d07/no_std_examples/pwm buzzer/.DS_Store -------------------------------------------------------------------------------- /no_std_examples/pwm buzzer/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.riscv32imc-unknown-none-elf] 2 | runner = "espflash flash --monitor" 3 | 4 | [build] 5 | rustflags = [ 6 | "-C", "link-arg=-Tlinkall.x", 7 | # Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.) 8 | # NOTE: May negatively impact performance of produced code 9 | "-C", "force-frame-pointers", 10 | 11 | # comment the cfgs below if you do _not_ wish to emulate atomics. 12 | # enable the atomic codegen option for RISCV 13 | "-C", "target-feature=+a", 14 | # tell the core library have atomics even though it's not specified in the target definition 15 | "--cfg", "target_has_atomic_load_store", 16 | "--cfg", 'target_has_atomic_load_store="8"', 17 | "--cfg", 'target_has_atomic_load_store="16"', 18 | "--cfg", 'target_has_atomic_load_store="32"', 19 | "--cfg", 'target_has_atomic_load_store="ptr"', 20 | # enable cas 21 | "--cfg", "target_has_atomic", 22 | "--cfg", 'target_has_atomic="8"', 23 | "--cfg", 'target_has_atomic="16"', 24 | "--cfg", 'target_has_atomic="32"', 25 | "--cfg", 'target_has_atomic="ptr"', 26 | ] 27 | 28 | target = "riscv32imc-unknown-none-elf" 29 | 30 | [unstable] 31 | build-std = ["core"] 32 | -------------------------------------------------------------------------------- /no_std_examples/pwm buzzer/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # These are backup files generated by rustfmt 7 | **/*.rs.bk 8 | 9 | # MSVC Windows builds of rustc generate these, which store debugging information 10 | *.pdb 11 | -------------------------------------------------------------------------------- /no_std_examples/pwm buzzer/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "rust-analyzer.checkOnSave.allTargets": false, 3 | } 4 | -------------------------------------------------------------------------------- /no_std_examples/pwm buzzer/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "timer_polling" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | license = "MIT OR Apache-2.0" 7 | 8 | [dependencies] 9 | esp32c3-hal = "0.8.0" 10 | esp-backtrace = { version = "0.6.0", features = [ 11 | "esp32c3", 12 | "panic-handler", 13 | "exception-handler", 14 | "print-uart", 15 | ] } -------------------------------------------------------------------------------- /no_std_examples/pwm buzzer/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright [year] [fullname] 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /no_std_examples/pwm buzzer/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | -------------------------------------------------------------------------------- /no_std_examples/rmt/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theembeddedrustacean/learn-esp32c3-rs/41efd9d1bbdf8d2e071332af610bae0515407d07/no_std_examples/rmt/.DS_Store -------------------------------------------------------------------------------- /no_std_examples/rmt/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.riscv32imc-unknown-none-elf] 2 | runner = "espflash flash --monitor" 3 | 4 | [build] 5 | rustflags = [ 6 | "-C", "link-arg=-Tlinkall.x", 7 | # Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.) 8 | # NOTE: May negatively impact performance of produced code 9 | "-C", "force-frame-pointers", 10 | 11 | # comment the cfgs below if you do _not_ wish to emulate atomics. 12 | # enable the atomic codegen option for RISCV 13 | "-C", "target-feature=+a", 14 | # tell the core library have atomics even though it's not specified in the target definition 15 | "--cfg", "target_has_atomic_load_store", 16 | "--cfg", 'target_has_atomic_load_store="8"', 17 | "--cfg", 'target_has_atomic_load_store="16"', 18 | "--cfg", 'target_has_atomic_load_store="32"', 19 | "--cfg", 'target_has_atomic_load_store="ptr"', 20 | # enable cas 21 | "--cfg", "target_has_atomic", 22 | "--cfg", 'target_has_atomic="8"', 23 | "--cfg", 'target_has_atomic="16"', 24 | "--cfg", 'target_has_atomic="32"', 25 | "--cfg", 'target_has_atomic="ptr"', 26 | ] 27 | 28 | target = "riscv32imc-unknown-none-elf" 29 | 30 | [unstable] 31 | build-std = ["core"] 32 | -------------------------------------------------------------------------------- /no_std_examples/rmt/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # These are backup files generated by rustfmt 7 | **/*.rs.bk 8 | 9 | # MSVC Windows builds of rustc generate these, which store debugging information 10 | *.pdb 11 | -------------------------------------------------------------------------------- /no_std_examples/rmt/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "rust-analyzer.checkOnSave.allTargets": false, 3 | } 4 | -------------------------------------------------------------------------------- /no_std_examples/rmt/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rmt" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | license = "MIT OR Apache-2.0" 7 | 8 | [dependencies] 9 | esp32c3-hal = "0.9.0" 10 | esp-backtrace = { version = "0.7.0", features = [ 11 | "esp32c3", 12 | "panic-handler", 13 | "exception-handler", 14 | "print-uart", 15 | ] } 16 | esp-println = { version = "0.5.0", features = ["esp32c3"] } 17 | -------------------------------------------------------------------------------- /no_std_examples/rmt/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright [year] [fullname] 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /no_std_examples/rmt/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | -------------------------------------------------------------------------------- /no_std_examples/rng/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theembeddedrustacean/learn-esp32c3-rs/41efd9d1bbdf8d2e071332af610bae0515407d07/no_std_examples/rng/.DS_Store -------------------------------------------------------------------------------- /no_std_examples/rng/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.riscv32imc-unknown-none-elf] 2 | runner = "espflash flash --monitor" 3 | 4 | [build] 5 | rustflags = [ 6 | "-C", "link-arg=-Tlinkall.x", 7 | # Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.) 8 | # NOTE: May negatively impact performance of produced code 9 | "-C", "force-frame-pointers", 10 | 11 | # comment the cfgs below if you do _not_ wish to emulate atomics. 12 | # enable the atomic codegen option for RISCV 13 | "-C", "target-feature=+a", 14 | # tell the core library have atomics even though it's not specified in the target definition 15 | "--cfg", "target_has_atomic_load_store", 16 | "--cfg", 'target_has_atomic_load_store="8"', 17 | "--cfg", 'target_has_atomic_load_store="16"', 18 | "--cfg", 'target_has_atomic_load_store="32"', 19 | "--cfg", 'target_has_atomic_load_store="ptr"', 20 | # enable cas 21 | "--cfg", "target_has_atomic", 22 | "--cfg", 'target_has_atomic="8"', 23 | "--cfg", 'target_has_atomic="16"', 24 | "--cfg", 'target_has_atomic="32"', 25 | "--cfg", 'target_has_atomic="ptr"', 26 | ] 27 | 28 | target = "riscv32imc-unknown-none-elf" 29 | 30 | [unstable] 31 | build-std = ["core"] 32 | -------------------------------------------------------------------------------- /no_std_examples/rng/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # These are backup files generated by rustfmt 7 | **/*.rs.bk 8 | 9 | # MSVC Windows builds of rustc generate these, which store debugging information 10 | *.pdb 11 | -------------------------------------------------------------------------------- /no_std_examples/rng/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "rust-analyzer.checkOnSave.allTargets": false, 3 | } 4 | -------------------------------------------------------------------------------- /no_std_examples/rng/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rng" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | license = "MIT OR Apache-2.0" 7 | 8 | [dependencies] 9 | esp32c3-hal = "0.9.0" 10 | esp-backtrace = { version = "0.7.0", features = [ 11 | "esp32c3", 12 | "panic-handler", 13 | "exception-handler", 14 | "print-uart", 15 | ] } 16 | esp-println = { version = "0.5.0", features = ["esp32c3"] } 17 | nmea = { version = "0.5.0", default-features = false } 18 | -------------------------------------------------------------------------------- /no_std_examples/rng/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright [year] [fullname] 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /no_std_examples/rng/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | -------------------------------------------------------------------------------- /no_std_examples/rng/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | use esp32c3_hal::{ 5 | clock::ClockControl, esp_riscv_rt::entry, peripherals::Peripherals, prelude::*, 6 | timer::TimerGroup, Delay, Rng, Rtc, 7 | }; 8 | use esp_backtrace as _; 9 | use esp_println::{print, println}; 10 | 11 | static MOCK_SENTENCES: &'static str = include_str!("nmea.rs"); 12 | 13 | #[entry] 14 | fn main() -> ! { 15 | // Take Peripherals, Initialize Clocks, and Create a Handle for Each 16 | let peripherals = Peripherals::take(); 17 | let mut system = peripherals.SYSTEM.split(); 18 | let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); 19 | 20 | // Instantiate and Create Handles for the RTC and TIMG watchdog timers 21 | let mut rtc = Rtc::new(peripherals.RTC_CNTL); 22 | let timer_group0 = TimerGroup::new( 23 | peripherals.TIMG0, 24 | &clocks, 25 | &mut system.peripheral_clock_control, 26 | ); 27 | let mut wdt0 = timer_group0.wdt; 28 | let timer_group1 = TimerGroup::new( 29 | peripherals.TIMG1, 30 | &clocks, 31 | &mut system.peripheral_clock_control, 32 | ); 33 | let mut wdt1 = timer_group1.wdt; 34 | 35 | // Disable the RTC and TIMG watchdog timers 36 | rtc.swd.disable(); 37 | rtc.rwdt.disable(); 38 | wdt0.disable(); 39 | wdt1.disable(); 40 | 41 | let mut delay = Delay::new(&clocks); 42 | 43 | // Configure terminal: enable newline conversion 44 | // This is for no std Rust using Wokwi as the output gets shifted 45 | print!("\x1b[20h"); 46 | 47 | let mut rng = Rng::new(peripherals.RNG); 48 | 49 | // Application Loop 50 | loop { 51 | let num = rng.random() as u8; 52 | let sentence = MOCK_SENTENCES.lines().nth(num as usize).unwrap(); 53 | println!("{}", sentence); 54 | delay.delay_ms(2000_u32); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /no_std_examples/timer_polling/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theembeddedrustacean/learn-esp32c3-rs/41efd9d1bbdf8d2e071332af610bae0515407d07/no_std_examples/timer_polling/.DS_Store -------------------------------------------------------------------------------- /no_std_examples/timer_polling/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.riscv32imc-unknown-none-elf] 2 | runner = "espflash flash --monitor" 3 | 4 | [build] 5 | rustflags = [ 6 | "-C", "link-arg=-Tlinkall.x", 7 | # Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.) 8 | # NOTE: May negatively impact performance of produced code 9 | "-C", "force-frame-pointers", 10 | 11 | # comment the cfgs below if you do _not_ wish to emulate atomics. 12 | # enable the atomic codegen option for RISCV 13 | "-C", "target-feature=+a", 14 | # tell the core library have atomics even though it's not specified in the target definition 15 | "--cfg", "target_has_atomic_load_store", 16 | "--cfg", 'target_has_atomic_load_store="8"', 17 | "--cfg", 'target_has_atomic_load_store="16"', 18 | "--cfg", 'target_has_atomic_load_store="32"', 19 | "--cfg", 'target_has_atomic_load_store="ptr"', 20 | # enable cas 21 | "--cfg", "target_has_atomic", 22 | "--cfg", 'target_has_atomic="8"', 23 | "--cfg", 'target_has_atomic="16"', 24 | "--cfg", 'target_has_atomic="32"', 25 | "--cfg", 'target_has_atomic="ptr"', 26 | ] 27 | 28 | target = "riscv32imc-unknown-none-elf" 29 | 30 | [unstable] 31 | build-std = ["core"] 32 | -------------------------------------------------------------------------------- /no_std_examples/timer_polling/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # These are backup files generated by rustfmt 7 | **/*.rs.bk 8 | 9 | # MSVC Windows builds of rustc generate these, which store debugging information 10 | *.pdb 11 | -------------------------------------------------------------------------------- /no_std_examples/timer_polling/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "rust-analyzer.checkOnSave.allTargets": false, 3 | } 4 | -------------------------------------------------------------------------------- /no_std_examples/timer_polling/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "timer_polling" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | license = "MIT OR Apache-2.0" 7 | 8 | [dependencies] 9 | esp32c3-hal = "0.8.0" 10 | esp-backtrace = { version = "0.6.0", features = [ 11 | "esp32c3", 12 | "panic-handler", 13 | "exception-handler", 14 | "print-uart", 15 | ] } -------------------------------------------------------------------------------- /no_std_examples/timer_polling/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright [year] [fullname] 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /no_std_examples/timer_polling/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | -------------------------------------------------------------------------------- /no_std_examples/timer_polling/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | use esp32c3_hal::{ 5 | clock::ClockControl, peripherals::Peripherals, prelude::*, timer::TimerGroup, Rtc, IO, 6 | }; 7 | use esp_backtrace as _; 8 | 9 | #[entry] 10 | fn main() -> ! { 11 | // Take Peripherals, Initialize Clocks, and Create a Handle for Each 12 | let peripherals = Peripherals::take(); 13 | let system = peripherals.SYSTEM.split(); 14 | let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); 15 | 16 | // Instantiate and Create Handles for the RTC and TIMG watchdog timers 17 | let mut rtc = Rtc::new(peripherals.RTC_CNTL); 18 | let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks); 19 | let mut wdt0 = timer_group0.wdt; 20 | let timer_group1 = TimerGroup::new(peripherals.TIMG1, &clocks); 21 | let mut wdt1 = timer_group1.wdt; 22 | 23 | // Disable the RTC and TIMG watchdog timers 24 | rtc.swd.disable(); 25 | rtc.rwdt.disable(); 26 | wdt0.disable(); 27 | wdt1.disable(); 28 | 29 | // Instantiate and Create Handle for IO 30 | let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); 31 | 32 | // Instantiate and Create Handle for LED output & Button Input 33 | let mut led = io.pins.gpio4.into_push_pull_output(); 34 | let button = io.pins.gpio0.into_pull_up_input(); 35 | 36 | // Instantiate and Create Handle for Timer 37 | let mut timer0 = timer_group0.timer0; 38 | 39 | // Initialize LED to on or off 40 | led.set_low().unwrap(); 41 | 42 | // Create and initialize a delay variable to manage delay duration 43 | let mut del_var = 2000_u32.millis(); 44 | 45 | // Application Loop 46 | loop { 47 | // Start counter with with del_var duration 48 | timer0.start(del_var); 49 | // Enter loop and check for button press until counter reaches del_var 50 | while timer0.wait() != Ok(()) { 51 | if button.is_low().unwrap() { 52 | // If button pressed decrease the delay value by 500 ms 53 | del_var = del_var - 1000_u32.millis(); 54 | // If updated delay value drops below 500 ms then reset it back to starting value to 2 secs 55 | if del_var.to_millis() < 1000_u32 { 56 | del_var = 2000_u32.millis(); 57 | } 58 | // Exit delay loop since button was pressed 59 | break; 60 | } 61 | } 62 | // Toggle LED 63 | led.toggle().unwrap(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /no_std_examples/uart/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theembeddedrustacean/learn-esp32c3-rs/41efd9d1bbdf8d2e071332af610bae0515407d07/no_std_examples/uart/.DS_Store -------------------------------------------------------------------------------- /no_std_examples/uart/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.riscv32imc-unknown-none-elf] 2 | runner = "espflash flash --monitor" 3 | 4 | [build] 5 | rustflags = [ 6 | "-C", "link-arg=-Tlinkall.x", 7 | # Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.) 8 | # NOTE: May negatively impact performance of produced code 9 | "-C", "force-frame-pointers", 10 | 11 | # comment the cfgs below if you do _not_ wish to emulate atomics. 12 | # enable the atomic codegen option for RISCV 13 | "-C", "target-feature=+a", 14 | # tell the core library have atomics even though it's not specified in the target definition 15 | "--cfg", "target_has_atomic_load_store", 16 | "--cfg", 'target_has_atomic_load_store="8"', 17 | "--cfg", 'target_has_atomic_load_store="16"', 18 | "--cfg", 'target_has_atomic_load_store="32"', 19 | "--cfg", 'target_has_atomic_load_store="ptr"', 20 | # enable cas 21 | "--cfg", "target_has_atomic", 22 | "--cfg", 'target_has_atomic="8"', 23 | "--cfg", 'target_has_atomic="16"', 24 | "--cfg", 'target_has_atomic="32"', 25 | "--cfg", 'target_has_atomic="ptr"', 26 | ] 27 | 28 | target = "riscv32imc-unknown-none-elf" 29 | 30 | [unstable] 31 | build-std = ["core"] 32 | -------------------------------------------------------------------------------- /no_std_examples/uart/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # These are backup files generated by rustfmt 7 | **/*.rs.bk 8 | 9 | # MSVC Windows builds of rustc generate these, which store debugging information 10 | *.pdb 11 | -------------------------------------------------------------------------------- /no_std_examples/uart/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "rust-analyzer.checkOnSave.allTargets": false, 3 | } 4 | -------------------------------------------------------------------------------- /no_std_examples/uart/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "uart" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | license = "MIT OR Apache-2.0" 7 | 8 | [dependencies] 9 | esp32c3-hal = "0.8.0" 10 | esp-backtrace = { version = "0.6.0", features = [ 11 | "esp32c3", 12 | "panic-handler", 13 | "exception-handler", 14 | "print-uart", 15 | ] } 16 | debouncr = "0.2.2" 17 | -------------------------------------------------------------------------------- /no_std_examples/uart/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright [year] [fullname] 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /no_std_examples/uart/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | -------------------------------------------------------------------------------- /no_std_examples/uart/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | use core::fmt::Write; // allows use to use the WriteLn! macro for easy printing 5 | use debouncr::{debounce_16, Edge}; 6 | use esp32c3_hal::{ 7 | clock::ClockControl, peripherals::Peripherals, prelude::*, timer::TimerGroup, uart::Uart, Rtc, 8 | IO, 9 | }; 10 | use esp_backtrace as _; 11 | 12 | #[entry] 13 | fn main() -> ! { 14 | // Take Peripherals, Initialize Clocks, and Create a Handle for Each 15 | let peripherals = Peripherals::take(); 16 | let system = peripherals.SYSTEM.split(); 17 | let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); 18 | 19 | // Instantiate and Create Handles for the RTC and TIMG watchdog timers 20 | let mut rtc = Rtc::new(peripherals.RTC_CNTL); 21 | let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks); 22 | let mut wdt0 = timer_group0.wdt; 23 | let timer_group1 = TimerGroup::new(peripherals.TIMG1, &clocks); 24 | let mut wdt1 = timer_group1.wdt; 25 | 26 | // Disable the RTC and TIMG watchdog timers 27 | rtc.swd.disable(); 28 | rtc.rwdt.disable(); 29 | wdt0.disable(); 30 | wdt1.disable(); 31 | 32 | // Instantiate and Create Handle for IO 33 | let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); 34 | 35 | // Instantiate and Create Handle for LED output & Button Input 36 | let mut led = io.pins.gpio4.into_push_pull_output(); 37 | let button = io.pins.gpio0.into_pull_up_input(); 38 | 39 | // Initialize LED to on or off 40 | led.set_low().unwrap(); 41 | 42 | // Create UART instance with default config 43 | let mut uart0 = Uart::new(peripherals.UART0); 44 | 45 | // Initialize debouncer to false because button is active low 46 | let mut debouncer = debounce_16(false); 47 | 48 | // Create and initialize a delay variable to manage delay loop 49 | let mut del_var = 10_0000_u32; 50 | 51 | // Variable to keep track of how many button presses occured 52 | let mut value: u8 = 0; 53 | 54 | // Application Loop 55 | loop { 56 | // Enter Delay Loop 57 | for _i in 1..del_var { 58 | // Keep checking if button got pressed 59 | if debouncer.update(button.is_low().unwrap()) == Some(Edge::Falling) { 60 | // If button is pressed print to derial and decrease the delay value 61 | writeln!(uart0, "Button Press {:02}\r", value).unwrap(); 62 | // Increment value keeping track of button presses 63 | value = value.wrapping_add(1); 64 | // Decrement the amount of delay 65 | del_var = del_var - 2_5000_u32; 66 | // If updated delay value drops below threshold then reset it back to starting value 67 | if del_var < 2_5000_u32 { 68 | del_var = 10_0000_u32; 69 | } 70 | // Exit delay loop since button was pressed 71 | break; 72 | } 73 | } 74 | led.toggle().unwrap(); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /no_std_examples/ultrasonic_timer/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theembeddedrustacean/learn-esp32c3-rs/41efd9d1bbdf8d2e071332af610bae0515407d07/no_std_examples/ultrasonic_timer/.DS_Store -------------------------------------------------------------------------------- /no_std_examples/ultrasonic_timer/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.riscv32imc-unknown-none-elf] 2 | runner = "espflash flash --monitor" 3 | 4 | [build] 5 | rustflags = [ 6 | "-C", "link-arg=-Tlinkall.x", 7 | # Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.) 8 | # NOTE: May negatively impact performance of produced code 9 | "-C", "force-frame-pointers", 10 | 11 | # comment the cfgs below if you do _not_ wish to emulate atomics. 12 | # enable the atomic codegen option for RISCV 13 | "-C", "target-feature=+a", 14 | # tell the core library have atomics even though it's not specified in the target definition 15 | "--cfg", "target_has_atomic_load_store", 16 | "--cfg", 'target_has_atomic_load_store="8"', 17 | "--cfg", 'target_has_atomic_load_store="16"', 18 | "--cfg", 'target_has_atomic_load_store="32"', 19 | "--cfg", 'target_has_atomic_load_store="ptr"', 20 | # enable cas 21 | "--cfg", "target_has_atomic", 22 | "--cfg", 'target_has_atomic="8"', 23 | "--cfg", 'target_has_atomic="16"', 24 | "--cfg", 'target_has_atomic="32"', 25 | "--cfg", 'target_has_atomic="ptr"', 26 | ] 27 | 28 | target = "riscv32imc-unknown-none-elf" 29 | 30 | [unstable] 31 | build-std = ["core"] 32 | -------------------------------------------------------------------------------- /no_std_examples/ultrasonic_timer/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # These are backup files generated by rustfmt 7 | **/*.rs.bk 8 | 9 | # MSVC Windows builds of rustc generate these, which store debugging information 10 | *.pdb 11 | -------------------------------------------------------------------------------- /no_std_examples/ultrasonic_timer/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "rust-analyzer.checkOnSave.allTargets": false, 3 | } 4 | -------------------------------------------------------------------------------- /no_std_examples/ultrasonic_timer/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "timer_polling" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | license = "MIT OR Apache-2.0" 7 | 8 | [dependencies] 9 | esp32c3-hal = "0.8.0" 10 | esp-backtrace = { version = "0.6.0", features = [ 11 | "esp32c3", 12 | "panic-handler", 13 | "exception-handler", 14 | "print-uart", 15 | ] } 16 | esp-println = { version = "0.5.0", features = ["esp32c3"] } 17 | fugit = "0.3.6" -------------------------------------------------------------------------------- /no_std_examples/ultrasonic_timer/LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright [year] [fullname] 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /no_std_examples/ultrasonic_timer/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | -------------------------------------------------------------------------------- /no_std_examples/ultrasonic_timer/src/main.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | use esp32c3_hal::{ 5 | clock::ClockControl, peripherals::Peripherals, prelude::*, systimer::SystemTimer, 6 | timer::TimerGroup, Delay, Rtc, IO, 7 | }; 8 | use esp_backtrace as _; 9 | use esp_println::println; 10 | 11 | #[entry] 12 | fn main() -> ! { 13 | // Take Peripherals, Initialize Clocks, and Create a Handle for Each 14 | let peripherals = Peripherals::take(); 15 | let system = peripherals.SYSTEM.split(); 16 | let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); 17 | 18 | // Instantiate and Create Handles for the RTC and TIMG watchdog timers 19 | let mut rtc = Rtc::new(peripherals.RTC_CNTL); 20 | let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks); 21 | let mut wdt0 = timer_group0.wdt; 22 | let timer_group1 = TimerGroup::new(peripherals.TIMG1, &clocks); 23 | let mut wdt1 = timer_group1.wdt; 24 | 25 | // Disable the RTC and TIMG watchdog timers 26 | rtc.swd.disable(); 27 | rtc.rwdt.disable(); 28 | wdt0.disable(); 29 | wdt1.disable(); 30 | 31 | // Instantiate and Create Handle for IO 32 | let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); 33 | 34 | // Instantiate and Create Handle for trigger output & echo input 35 | let mut trig = io.pins.gpio1.into_push_pull_output(); 36 | let echo = io.pins.gpio0.into_floating_input(); 37 | //CHECK IF INTERNAL PULL UP IS OK 38 | 39 | let mut delay = Delay::new(&clocks); 40 | 41 | // Application Loop 42 | loop { 43 | // 1) Set pin ouput to low for 5 us to get clean low pulse 44 | trig.set_low().unwrap(); 45 | delay.delay_us(5_u32); 46 | 47 | // 2) Set pin output to high (trigger) for 10us 48 | trig.set_high().unwrap(); 49 | delay.delay_us(10_u32); 50 | trig.set_low().unwrap(); 51 | 52 | // Wait until pin goes high 53 | while !echo.is_high().unwrap() {} 54 | 55 | // Kick off timer measurement 56 | let echo_start = SystemTimer::now(); 57 | 58 | // Wait until pin goes low 59 | while !echo.is_low().unwrap() {} 60 | 61 | // Collect current timer count 62 | let echo_end = SystemTimer::now(); 63 | 64 | // Calculate the elapsed timer 65 | let echo_dur = echo_end.wrapping_sub(echo_start); 66 | 67 | // Calculate the distance in cms using formula in datasheet 68 | let distance_cm = echo_dur / 16 / 58; 69 | 70 | // Print the distance output 71 | println!("Distance {} cm\r", distance_cm); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /std_bt_examples/ble-advertise/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | MCU="esp32c3" 15 | # Note: this variable is not used by the pio builder (`cargo build --features pio`) 16 | ESP_IDF_VERSION = "v4.4.7" 17 | 18 | -------------------------------------------------------------------------------- /std_bt_examples/ble-advertise/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_bt_examples/ble-advertise/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "ble-advertise" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev "] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.71" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | default = ["std", "embassy", "esp-idf-svc/native"] 18 | 19 | pio = ["esp-idf-svc/pio"] 20 | std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"] 21 | alloc = ["esp-idf-svc/alloc"] 22 | nightly = ["esp-idf-svc/nightly"] 23 | experimental = ["esp-idf-svc/experimental"] 24 | embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"] 25 | 26 | [dependencies] 27 | log = { version = "0.4", default-features = false } 28 | esp-idf-svc = { version = "0.48", default-features = false } 29 | esp-idf-hal = { version = "0.43", default-features = false } 30 | esp-idf-sys = { version = "0.34", default-features = false } 31 | esp32-nimble = "0.6.0" 32 | 33 | [build-dependencies] 34 | embuild = "0.31.3" 35 | -------------------------------------------------------------------------------- /std_bt_examples/ble-advertise/build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | embuild::espidf::sysenv::output(); 3 | } 4 | -------------------------------------------------------------------------------- /std_bt_examples/ble-advertise/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_bt_examples/ble-advertise/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | CONFIG_BT_ENABLED=y 12 | CONFIG_BT_BLE_ENABLED=y 13 | CONFIG_BT_BLUEDROID_ENABLED=n 14 | CONFIG_BT_NIMBLE_ENABLED=y -------------------------------------------------------------------------------- /std_bt_examples/ble-advertise/src/main.rs: -------------------------------------------------------------------------------- 1 | use esp32_nimble::{ 2 | enums::{ConnMode, DiscMode}, 3 | BLEAdvertisementData, BLEDevice, 4 | }; 5 | use esp_idf_hal::delay::FreeRtos; 6 | use esp_idf_sys as _; 7 | 8 | fn main() { 9 | esp_idf_svc::sys::link_patches(); 10 | 11 | // Take ownership of device 12 | let ble_device = BLEDevice::take(); 13 | // Obtain handle for advertiser 14 | let ble_advertiser = ble_device.get_advertising(); 15 | 16 | // Specify Advertiser Name 17 | let mut ad_data = BLEAdvertisementData::new(); 18 | ad_data.name("ESP32-C3 Peripheral"); 19 | 20 | // Configure Advertiser with Specified Data 21 | ble_advertiser.lock().set_data(&mut ad_data).unwrap(); 22 | 23 | // Make Advertiser Non-Connectable 24 | // Set Discovery Mode to General 25 | // Deactivate Scan Responses 26 | ble_advertiser 27 | .lock() 28 | .advertisement_type(ConnMode::Non) 29 | .disc_mode(DiscMode::Gen) 30 | .scan_response(false); 31 | 32 | // Start Advertising 33 | ble_advertiser.lock().start().unwrap(); 34 | println!("Advertisement Started"); 35 | loop { 36 | // Keep Advertising 37 | // Add delay to prevent watchdog from triggering 38 | FreeRtos::delay_ms(10); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /std_bt_examples/ble-client/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | MCU="esp32c3" 15 | # Note: this variable is not used by the pio builder (`cargo build --features pio`) 16 | ESP_IDF_VERSION = "v4.4.7" 17 | 18 | -------------------------------------------------------------------------------- /std_bt_examples/ble-client/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_bt_examples/ble-client/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "ble-client" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev "] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.71" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | default = ["std", "embassy", "esp-idf-svc/native"] 18 | 19 | pio = ["esp-idf-svc/pio"] 20 | std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"] 21 | alloc = ["esp-idf-svc/alloc"] 22 | nightly = ["esp-idf-svc/nightly"] 23 | experimental = ["esp-idf-svc/experimental"] 24 | embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"] 25 | 26 | [dependencies] 27 | log = { version = "0.4", default-features = false } 28 | esp-idf-svc = { version = "0.48", default-features = false } 29 | esp-idf-hal = { version = "0.43", default-features = false } 30 | esp-idf-sys = { version = "0.34", default-features = false } 31 | esp32-nimble = "0.6.0" 32 | 33 | [build-dependencies] 34 | embuild = "0.31.3" 35 | -------------------------------------------------------------------------------- /std_bt_examples/ble-client/build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | embuild::espidf::sysenv::output(); 3 | } 4 | -------------------------------------------------------------------------------- /std_bt_examples/ble-client/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_bt_examples/ble-client/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | CONFIG_BT_ENABLED=y 12 | CONFIG_BT_BLE_ENABLED=y 13 | CONFIG_BT_BLUEDROID_ENABLED=n 14 | CONFIG_BT_NIMBLE_ENABLED=y -------------------------------------------------------------------------------- /std_bt_examples/ble-client/src/main.rs: -------------------------------------------------------------------------------- 1 | use esp32_nimble::{uuid128, BLEClient, BLEDevice}; 2 | use esp_idf_hal::delay::FreeRtos; 3 | use esp_idf_hal::task::block_on; 4 | use esp_idf_sys as _; 5 | 6 | const DEVICE_NAME: &str = "iPhone"; 7 | 8 | fn main() { 9 | esp_idf_sys::link_patches(); 10 | 11 | block_on(async { 12 | // Acquire BLE Device Handle 13 | let ble_device = BLEDevice::take(); 14 | // Acquire Scan Handle 15 | let ble_scan = ble_device.get_scan(); 16 | // Scan and Find Device/Server 17 | let device = ble_scan 18 | .active_scan(true) 19 | .interval(100) 20 | .window(99) 21 | .find_device(10000, |device| device.name() == DEVICE_NAME) 22 | .await 23 | .unwrap(); 24 | 25 | if let Some(device) = device { 26 | // Create Client Handle 27 | let mut client = BLEClient::new(); 28 | 29 | // Define Connect Behaviour 30 | client.on_connect(|client| { 31 | // Update Connect Parameters on Connect 32 | client.update_conn_params(120, 250, 0, 60).unwrap(); 33 | println!("Connected to {}", DEVICE_NAME); 34 | }); 35 | 36 | // Connect to advertising device using its address 37 | client.connect(device.addr()).await.unwrap(); 38 | 39 | // Seek and Create Handle for the BLE Remote Server Service corresponding to the UUID 40 | let service = client 41 | .get_service(uuid128!("9b574847-f706-436c-bed7-fc01eb0965c1")) 42 | .await 43 | .unwrap(); 44 | 45 | // Seek and Create Handle for the BLE Remote Server Characteristic corresponding to the UUID 46 | let uuid = uuid128!("681285a6-247f-48c6-80ad-68c3dce18585"); 47 | let characteristic = service.get_characteristic(uuid).await.unwrap(); 48 | 49 | loop { 50 | // Read & Print Characteristic Value 51 | let value = characteristic.read_value().await.unwrap(); 52 | println!("Read Value: {:?}", value); 53 | 54 | // Wait 1 second before reading again 55 | FreeRtos::delay_ms(1000); 56 | } 57 | } 58 | }); 59 | } 60 | -------------------------------------------------------------------------------- /std_bt_examples/ble-secure-client/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | MCU="esp32c3" 15 | # Note: this variable is not used by the pio builder (`cargo build --features pio`) 16 | ESP_IDF_VERSION = "v4.4.7" 17 | 18 | -------------------------------------------------------------------------------- /std_bt_examples/ble-secure-client/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_bt_examples/ble-secure-client/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "ble-secure-client" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev "] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.71" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | default = ["std", "embassy", "esp-idf-svc/native"] 18 | 19 | pio = ["esp-idf-svc/pio"] 20 | std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"] 21 | alloc = ["esp-idf-svc/alloc"] 22 | nightly = ["esp-idf-svc/nightly"] 23 | experimental = ["esp-idf-svc/experimental"] 24 | embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"] 25 | 26 | [dependencies] 27 | log = { version = "0.4", default-features = false } 28 | esp-idf-svc = { version = "0.48", default-features = false } 29 | esp-idf-hal = { version = "0.43", default-features = false } 30 | esp-idf-sys = { version = "0.34", default-features = false } 31 | esp32-nimble = "0.6.0" 32 | 33 | [build-dependencies] 34 | embuild = "0.31.3" 35 | -------------------------------------------------------------------------------- /std_bt_examples/ble-secure-client/build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | embuild::espidf::sysenv::output(); 3 | } 4 | -------------------------------------------------------------------------------- /std_bt_examples/ble-secure-client/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_bt_examples/ble-secure-client/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | CONFIG_BT_ENABLED=y 12 | CONFIG_BT_BLE_ENABLED=y 13 | CONFIG_BT_BLUEDROID_ENABLED=n 14 | CONFIG_BT_NIMBLE_ENABLED=y -------------------------------------------------------------------------------- /std_bt_examples/ble-secure-client/src/main.rs: -------------------------------------------------------------------------------- 1 | use esp32_nimble::{enums::*, uuid128, BLEClient, BLEDevice}; 2 | use esp_idf_hal::delay::FreeRtos; 3 | use esp_idf_hal::task::block_on; 4 | use esp_idf_sys as _; 5 | 6 | const DEVICE_NAME: &str = "iPhone"; 7 | 8 | fn main() { 9 | esp_idf_sys::link_patches(); 10 | 11 | block_on(async { 12 | // Acquire BLE Device Handle 13 | let ble_device = BLEDevice::take(); 14 | 15 | // Configure Device Security 16 | ble_device 17 | .security() 18 | .set_auth(AuthReq::all()) 19 | .set_io_cap(SecurityIOCap::KeyboardOnly); 20 | 21 | // Acquire Scan Handle 22 | let ble_scan = ble_device.get_scan(); 23 | 24 | // Scan and Find Device/Server 25 | let device = ble_scan 26 | .active_scan(true) 27 | .interval(100) 28 | .window(99) 29 | .find_device(10000, |device| device.name() == DEVICE_NAME) 30 | .await 31 | .unwrap(); 32 | 33 | if let Some(device) = device { 34 | // Create Client Handle 35 | let mut client = BLEClient::new(); 36 | 37 | // Define Connect Behaviour 38 | client.on_connect(|client| { 39 | // Update Connect Parameters on Connect 40 | client.update_conn_params(120, 250, 0, 60).unwrap(); 41 | println!("Connected to {}", DEVICE_NAME); 42 | }); 43 | 44 | // Connect to advertising device using its address 45 | client.connect(device.addr()).await.unwrap(); 46 | 47 | // Enter 123456 passkey on request 48 | client.on_passkey_request(|| 123456); 49 | 50 | // Secure Connection 51 | client.secure_connection().await.unwrap(); 52 | 53 | // Seek and Create Handle for the BLE Remote Server Service corresponding to the UUID 54 | let service = client 55 | .get_service(uuid128!("9b574847-f706-436c-bed7-fc01eb0965c1")) 56 | .await 57 | .unwrap(); 58 | 59 | // Seek and Create Handle for the BLE Remote Server Characteristic corresponding to the UUID 60 | let uuid = uuid128!("681285a6-247f-48c6-80ad-68c3dce18585"); 61 | let characteristic = service.get_characteristic(uuid).await.unwrap(); 62 | 63 | loop { 64 | // Read & Print Characteristic Value 65 | let value = characteristic.read_value().await.unwrap(); 66 | println!("Read Value: {:?}", value); 67 | 68 | // Wait 1 second before reading again 69 | FreeRtos::delay_ms(1000); 70 | } 71 | } 72 | }); 73 | } 74 | -------------------------------------------------------------------------------- /std_bt_examples/ble-secure-server/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | MCU="esp32c3" 15 | # Note: this variable is not used by the pio builder (`cargo build --features pio`) 16 | ESP_IDF_VERSION = "v4.4.7" 17 | 18 | -------------------------------------------------------------------------------- /std_bt_examples/ble-secure-server/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_bt_examples/ble-secure-server/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "ble-secure-server" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev "] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.71" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | default = ["std", "embassy", "esp-idf-svc/native"] 18 | 19 | pio = ["esp-idf-svc/pio"] 20 | std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"] 21 | alloc = ["esp-idf-svc/alloc"] 22 | nightly = ["esp-idf-svc/nightly"] 23 | experimental = ["esp-idf-svc/experimental"] 24 | embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"] 25 | 26 | [dependencies] 27 | log = { version = "0.4", default-features = false } 28 | esp-idf-svc = { version = "0.48", default-features = false } 29 | esp-idf-hal = { version = "0.43", default-features = false } 30 | esp-idf-sys = { version = "0.34", default-features = false } 31 | esp32-nimble = "0.6.0" 32 | 33 | [build-dependencies] 34 | embuild = "0.31.3" 35 | -------------------------------------------------------------------------------- /std_bt_examples/ble-secure-server/build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | embuild::espidf::sysenv::output(); 3 | } 4 | -------------------------------------------------------------------------------- /std_bt_examples/ble-secure-server/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_bt_examples/ble-secure-server/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | CONFIG_BT_ENABLED=y 12 | CONFIG_BT_BLE_ENABLED=y 13 | CONFIG_BT_BLUEDROID_ENABLED=n 14 | CONFIG_BT_NIMBLE_ENABLED=y -------------------------------------------------------------------------------- /std_bt_examples/ble-secure-server/src/main.rs: -------------------------------------------------------------------------------- 1 | use esp32_nimble::{enums::*, uuid128, BLEAdvertisementData, BLEDevice, NimbleProperties}; 2 | use esp_idf_hal::delay::FreeRtos; 3 | use esp_idf_sys as _; 4 | 5 | fn main() { 6 | esp_idf_sys::link_patches(); 7 | 8 | // Take ownership of device 9 | let ble_device = BLEDevice::take(); 10 | 11 | // Obtain handle for peripheral advertiser 12 | let ble_advertiser = ble_device.get_advertising(); 13 | 14 | // Configure Device Security 15 | ble_device 16 | .security() 17 | .set_auth(AuthReq::all()) 18 | .set_passkey(123456) 19 | .set_io_cap(SecurityIOCap::DisplayOnly) 20 | .resolve_rpa(); 21 | 22 | // Obtain handle for server 23 | let server = ble_device.get_server(); 24 | 25 | // Define server connect behaviour 26 | server.on_connect(|server, clntdesc| { 27 | // Print connected client data 28 | println!("{:?}", clntdesc); 29 | // Update connection parameters 30 | server 31 | .update_conn_params(clntdesc.conn_handle(), 24, 48, 0, 60) 32 | .unwrap(); 33 | }); 34 | 35 | // Define server disconnect behaviour 36 | server.on_disconnect(|_desc, _reason| { 37 | println!("Disconnected, back to advertising"); 38 | }); 39 | 40 | // Create a service with custom UUID 41 | let my_service = server.create_service(uuid128!("9b574847-f706-436c-bed7-fc01eb0965c1")); 42 | 43 | // Create a characteristic to associate with created service 44 | let my_service_characteristic = my_service.lock().create_characteristic( 45 | uuid128!("681285a6-247f-48c6-80ad-68c3dce18585"), 46 | NimbleProperties::READ | NimbleProperties::READ_ENC, 47 | ); 48 | 49 | // Modify characteristic value 50 | my_service_characteristic.lock().set_value(b"Start Value"); 51 | 52 | // Configure Advertiser Data 53 | ble_advertiser 54 | .lock() 55 | .set_data( 56 | BLEAdvertisementData::new() 57 | .name("ESP32 Server") 58 | .add_service_uuid(uuid128!("9b574847-f706-436c-bed7-fc01eb0965c1")), 59 | ) 60 | .unwrap(); 61 | 62 | // Start Advertising 63 | ble_advertiser.lock().start().unwrap(); 64 | 65 | // (Optional) Print dump of local GATT table 66 | // server.ble_gatts_show_local(); 67 | 68 | // Init a value to pass to characteristic 69 | let mut val = 0; 70 | 71 | loop { 72 | FreeRtos::delay_ms(1000); 73 | my_service_characteristic.lock().set_value(&[val]).notify(); 74 | val = val.wrapping_add(1); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /std_bt_examples/ble-server/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | MCU="esp32c3" 15 | # Note: this variable is not used by the pio builder (`cargo build --features pio`) 16 | ESP_IDF_VERSION = "v4.4.7" 17 | 18 | -------------------------------------------------------------------------------- /std_bt_examples/ble-server/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_bt_examples/ble-server/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "ble-server" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev "] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.71" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | default = ["std", "embassy", "esp-idf-svc/native"] 18 | 19 | pio = ["esp-idf-svc/pio"] 20 | std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"] 21 | alloc = ["esp-idf-svc/alloc"] 22 | nightly = ["esp-idf-svc/nightly"] 23 | experimental = ["esp-idf-svc/experimental"] 24 | embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"] 25 | 26 | [dependencies] 27 | log = { version = "0.4", default-features = false } 28 | esp-idf-svc = { version = "0.48", default-features = false } 29 | esp-idf-hal = { version = "0.43", default-features = false } 30 | esp-idf-sys = { version = "0.34", default-features = false } 31 | esp32-nimble = "0.6.0" 32 | 33 | [build-dependencies] 34 | embuild = "0.31.3" 35 | -------------------------------------------------------------------------------- /std_bt_examples/ble-server/build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | embuild::espidf::sysenv::output(); 3 | } 4 | -------------------------------------------------------------------------------- /std_bt_examples/ble-server/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_bt_examples/ble-server/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | CONFIG_BT_ENABLED=y 12 | CONFIG_BT_BLE_ENABLED=y 13 | CONFIG_BT_BLUEDROID_ENABLED=n 14 | CONFIG_BT_NIMBLE_ENABLED=y -------------------------------------------------------------------------------- /std_bt_examples/ble-server/src/main.rs: -------------------------------------------------------------------------------- 1 | use esp32_nimble::{uuid128, BLEAdvertisementData, BLEDevice, NimbleProperties}; 2 | use esp_idf_hal::delay::FreeRtos; 3 | use esp_idf_sys as _; 4 | 5 | fn main() { 6 | esp_idf_sys::link_patches(); 7 | 8 | // Take ownership of device 9 | let ble_device = BLEDevice::take(); 10 | 11 | // Obtain handle for peripheral advertiser 12 | let ble_advertiser = ble_device.get_advertising(); 13 | 14 | // Obtain handle for server 15 | let server = ble_device.get_server(); 16 | 17 | // Define server connect behaviour 18 | server.on_connect(|server, clntdesc| { 19 | // Print connected client data 20 | println!("{:?}", clntdesc); 21 | // Update connection parameters 22 | server 23 | .update_conn_params(clntdesc.conn_handle(), 24, 48, 0, 60) 24 | .unwrap(); 25 | }); 26 | 27 | // Define server disconnect behaviour 28 | server.on_disconnect(|_desc, _reason| { 29 | println!("Disconnected, back to advertising"); 30 | }); 31 | 32 | // Create a service with custom UUID 33 | let my_service = server.create_service(uuid128!("9b574847-f706-436c-bed7-fc01eb0965c1")); 34 | 35 | // Create a characteristic to associate with created service 36 | let my_service_characteristic = my_service.lock().create_characteristic( 37 | uuid128!("681285a6-247f-48c6-80ad-68c3dce18585"), 38 | NimbleProperties::READ | NimbleProperties::NOTIFY, 39 | ); 40 | 41 | // Modify characteristic value 42 | my_service_characteristic.lock().set_value(b"Start Value"); 43 | 44 | // Configure Advertiser Data 45 | ble_advertiser 46 | .lock() 47 | .set_data( 48 | BLEAdvertisementData::new() 49 | .name("ESP32 Server") 50 | .add_service_uuid(uuid128!("9b574847-f706-436c-bed7-fc01eb0965c1")), 51 | ) 52 | .unwrap(); 53 | 54 | // Start Advertising 55 | ble_advertiser.lock().start().unwrap(); 56 | 57 | // (Optional) Print dump of local GATT table 58 | // server.ble_gatts_show_local(); 59 | 60 | // Init a value to pass to characteristic 61 | let mut val = 0; 62 | 63 | loop { 64 | FreeRtos::delay_ms(1000); 65 | my_service_characteristic.lock().set_value(&[val]).notify(); 66 | val = val.wrapping_add(1); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /std_bt_examples/ble_scan/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | MCU="esp32c3" 15 | # Note: this variable is not used by the pio builder (`cargo build --features pio`) 16 | ESP_IDF_VERSION = "v4.4.7" 17 | 18 | -------------------------------------------------------------------------------- /std_bt_examples/ble_scan/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_bt_examples/ble_scan/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "another" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev "] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.71" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | default = ["std", "embassy", "esp-idf-svc/native"] 18 | 19 | pio = ["esp-idf-svc/pio"] 20 | std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"] 21 | alloc = ["esp-idf-svc/alloc"] 22 | nightly = ["esp-idf-svc/nightly"] 23 | experimental = ["esp-idf-svc/experimental"] 24 | embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"] 25 | 26 | [dependencies] 27 | log = { version = "0.4", default-features = false } 28 | esp-idf-svc = { version = "0.48", default-features = false } 29 | esp-idf-hal = { version = "0.43", default-features = false } 30 | esp-idf-sys = { version = "0.34", default-features = false } 31 | esp32-nimble = "0.6.0" 32 | 33 | [build-dependencies] 34 | embuild = "0.31.3" 35 | -------------------------------------------------------------------------------- /std_bt_examples/ble_scan/build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | embuild::espidf::sysenv::output(); 3 | } 4 | -------------------------------------------------------------------------------- /std_bt_examples/ble_scan/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_bt_examples/ble_scan/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | CONFIG_BT_ENABLED=y 12 | CONFIG_BT_BLE_ENABLED=y 13 | CONFIG_BT_BLUEDROID_ENABLED=n 14 | CONFIG_BT_NIMBLE_ENABLED=y 15 | -------------------------------------------------------------------------------- /std_bt_examples/ble_scan/src/main.rs: -------------------------------------------------------------------------------- 1 | use esp32_nimble::BLEDevice; 2 | use esp_idf_hal::task::block_on; 3 | use esp_idf_sys as _; 4 | 5 | fn main() { 6 | esp_idf_svc::sys::link_patches(); 7 | 8 | block_on(async { 9 | let ble_device = BLEDevice::take(); 10 | let ble_scan = ble_device.get_scan(); 11 | ble_scan 12 | .active_scan(true) 13 | .interval(100) 14 | .window(50) 15 | .on_result(|_scan, param| { 16 | println!( 17 | "Advertised Device Name: {:?}, Address: {:?} dB, RSSI: {:?}", 18 | param.name(), 19 | param.addr(), 20 | param.rssi() 21 | ); 22 | }); 23 | ble_scan.start(5000).await.unwrap(); 24 | println!("Scan finished"); 25 | }); 26 | } 27 | -------------------------------------------------------------------------------- /std_examples/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/theembeddedrustacean/learn-esp32c3-rs/41efd9d1bbdf8d2e071332af610bae0515407d07/std_examples/.DS_Store -------------------------------------------------------------------------------- /std_examples/adc/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | # Note: these variables are not used when using pio builder (`cargo build --features pio`) 15 | ESP_IDF_VERSION = "release/v4.4" 16 | 17 | -------------------------------------------------------------------------------- /std_examples/adc/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_examples/adc/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "gpio" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.66" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | 18 | default = ["std", "hal", "esp-idf-sys/native"] 19 | 20 | 21 | pio = ["esp-idf-sys/pio"] 22 | all = ["std", "nightly", "experimental", "embassy"] 23 | hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"] 24 | std = [ 25 | "alloc", 26 | "esp-idf-sys/std", 27 | "esp-idf-sys/binstart", 28 | "embedded-svc?/std", 29 | "esp-idf-hal?/std", 30 | "esp-idf-svc?/std", 31 | ] 32 | alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"] 33 | nightly = [ 34 | "embedded-svc?/nightly", 35 | "esp-idf-svc?/nightly", 36 | ] # Future: "esp-idf-hal?/nightly" 37 | experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"] 38 | embassy = [ 39 | "esp-idf-hal?/embassy-sync", 40 | "esp-idf-hal?/critical-section", 41 | # "esp-idf-hal?/edge-executor", 42 | "esp-idf-svc?/embassy-time-driver", 43 | "esp-idf-svc?/embassy-time-isr-queue", 44 | ] 45 | 46 | [dependencies] 47 | log = { version = "0.4.17", default-features = false } 48 | esp-idf-sys = { version = "0.33", default-features = false } 49 | esp-idf-hal = { version = "0.42", optional = true, default-features = false } 50 | esp-idf-svc = { version = "0.47.3", optional = true, default-features = false } 51 | embedded-svc = { version = "0.26.4", optional = true, default-features = false } 52 | libm = "0.2.7" 53 | anyhow = "1.0.75" 54 | 55 | [build-dependencies] 56 | embuild = "0.31.2" 57 | -------------------------------------------------------------------------------- /std_examples/adc/build.rs: -------------------------------------------------------------------------------- 1 | // Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641 2 | fn main() -> Result<(), Box> { 3 | embuild::build::CfgArgs::output_propagated("ESP_IDF")?; 4 | embuild::build::LinkArgs::output_propagated("ESP_IDF")?; 5 | Ok(()) 6 | } 7 | -------------------------------------------------------------------------------- /std_examples/adc/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_examples/adc/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=7000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_examples/adc/src/main.rs: -------------------------------------------------------------------------------- 1 | /* 2 | For a detailed explanation of this code check out the associated blog post: 3 | https://apollolabsblog.hashnode.dev/esp32-standard-library-embedded-rust-analog-temperature-sensing-using-the-adc 4 | 5 | GitHub Repo containing source code and other examples: 6 | https://github.com/apollolabsdev 7 | 8 | For notifications on similar examples and more, subscribe to newsletter here: 9 | https://subscribepage.io/apollolabsnewsletter 10 | */ 11 | 12 | use esp_idf_sys::{self as _}; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported 13 | 14 | use esp_idf_hal::adc::attenuation::adc_atten_t_ADC_ATTEN_DB_11; 15 | use esp_idf_hal::adc::config::Config; 16 | use esp_idf_hal::adc::*; 17 | use esp_idf_hal::gpio::Gpio4; 18 | use esp_idf_hal::peripherals::Peripherals; 19 | use libm::log; 20 | 21 | fn main() -> anyhow::Result<()> { 22 | let peripherals = Peripherals::take().unwrap(); 23 | 24 | // Configure ADC Driver 25 | let mut adc = AdcDriver::new(peripherals.adc1, &Config::new()).unwrap(); 26 | 27 | // Configure ADC Channel 28 | let mut adc_pin: esp_idf_hal::adc::AdcChannelDriver< 29 | '_, 30 | { adc_atten_t_ADC_ATTEN_DB_11 }, 31 | Gpio4, 32 | > = AdcChannelDriver::new(peripherals.pins.gpio4).unwrap(); 33 | 34 | const B: f64 = 3950.0; // B value of the thermistor 35 | const VMAX: f64 = 2500.0; // Full Range Voltage 36 | 37 | // Algorithm 38 | // 1) Get adc reading 39 | // 2) Convert to temperature 40 | // 3) Send over Serial 41 | // 4) Go Back to step 1 42 | 43 | loop { 44 | // Get ADC Reading 45 | let sample: u16 = adc.read(&mut adc_pin).unwrap(); 46 | 47 | //Convert to temperature 48 | let temperature = 1. / (log(1. / (VMAX / sample as f64 - 1.)) / B + 1.0 / 298.15) - 273.15; 49 | 50 | // Print the temperature output 51 | println!("Temperature {:02} Celcius\r", temperature); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /std_examples/cli/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = [ "--cfg", "espidf_time64"] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | MCU="esp32c3" 15 | # Note: this variable is not used by the pio builder (`cargo build --features pio`) 16 | ESP_IDF_VERSION = "v5.1.2" 17 | ESP_IDF_TOOLS_INSTALL_DIR = "global" 18 | 19 | -------------------------------------------------------------------------------- /std_examples/cli/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_examples/cli/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "cli" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev "] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.71" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | default = ["std", "embassy", "esp-idf-svc/native"] 18 | 19 | pio = ["esp-idf-svc/pio"] 20 | std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"] 21 | alloc = ["esp-idf-svc/alloc"] 22 | nightly = ["esp-idf-svc/nightly"] 23 | experimental = ["esp-idf-svc/experimental"] 24 | embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"] 25 | 26 | [dependencies] 27 | log = { version = "0.4", default-features = false } 28 | esp-idf-svc = { version = "0.48", default-features = false } 29 | esp-idf-hal = { version = "0.43", default-features = false } 30 | menu = "0.4.0" 31 | 32 | [build-dependencies] 33 | embuild = "0.31.3" 34 | -------------------------------------------------------------------------------- /std_examples/cli/build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | embuild::espidf::sysenv::output(); 3 | } 4 | -------------------------------------------------------------------------------- /std_examples/cli/diagram.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 1, 3 | "editor": "wokwi", 4 | "author": "apollolabsdev ", 5 | "parts": [ 6 | { 7 | "type": "board-esp32-c3-devkitm-1", 8 | "id": "esp", 9 | "top": 0.59, 10 | "left": 0.67, 11 | "attrs": { 12 | "flashSize": "16" 13 | } 14 | } 15 | ], 16 | "connections": [ 17 | [ 18 | "esp:TX", 19 | "$serialMonitor:RX", 20 | "", 21 | [] 22 | ], 23 | [ 24 | "esp:RX", 25 | "$serialMonitor:TX", 26 | "", 27 | [] 28 | ] 29 | ], 30 | "serialMonitor": { 31 | "display": "terminal", 32 | "convertEol": true 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /std_examples/cli/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_examples/cli/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_examples/cli/src/main.rs: -------------------------------------------------------------------------------- 1 | /* 2 | For a detailed explanation of this code check out the associated blog post: 3 | https://apollolabsblog.hashnode.dev/esp32-embedded-rust-simple-cli 4 | 5 | GitHub Repo containing source code and other examples: 6 | https://github.com/apollolabsdev 7 | 8 | For notifications on similar examples and more, subscribe to newsletter here: 9 | https://www.theembeddedrustacean.com/subscribe 10 | */ 11 | 12 | use esp_idf_hal::delay::BLOCK; 13 | use esp_idf_hal::gpio; 14 | use esp_idf_hal::peripherals::Peripherals; 15 | use esp_idf_hal::prelude::*; 16 | use esp_idf_hal::uart::*; 17 | use menu::*; 18 | use std::fmt::Write; 19 | 20 | // CLI Root Menu Struct Initialization 21 | const ROOT_MENU: Menu = Menu { 22 | label: "root", 23 | items: &[&Item { 24 | item_type: ItemType::Callback { 25 | function: hello_name, 26 | parameters: &[Parameter::Mandatory { 27 | parameter_name: "name", 28 | help: Some("Enter your name"), 29 | }], 30 | }, 31 | command: "hw", 32 | help: Some("This is an embedded CLI terminal. Check the summary for the list of supported commands"), 33 | }], 34 | entry: None, 35 | exit: None, 36 | }; 37 | 38 | fn main() { 39 | // Take Peripherals 40 | let peripherals = Peripherals::take().unwrap(); 41 | 42 | // Configure UART 43 | // Create handle for UART config struct 44 | let config = config::Config::default().baudrate(Hertz(115_200)); 45 | 46 | // Instantiate UART 47 | let mut uart = UartDriver::new( 48 | peripherals.uart0, 49 | peripherals.pins.gpio21, 50 | peripherals.pins.gpio20, 51 | Option::::None, 52 | Option::::None, 53 | &config, 54 | ) 55 | .unwrap(); 56 | 57 | // This line is for Wokwi only so that the console output is formatted correctly 58 | uart.write_str("\x1b[20h").unwrap(); 59 | 60 | // Create a buffer to store CLI input 61 | let mut clibuf = [0u8; 64]; 62 | // Instantiate CLI runner with root menu, buffer, and uart 63 | let mut r = Runner::new(ROOT_MENU, &mut clibuf, uart); 64 | 65 | loop { 66 | // Create single element buffer for UART characters 67 | let mut buf = [0_u8; 1]; 68 | // Read single byte from UART 69 | r.context.read(&mut buf, BLOCK).unwrap(); 70 | // Pass read byte to CLI runner for processing 71 | r.input_byte(buf[0]); 72 | } 73 | } 74 | 75 | // Callback function for hw commans 76 | fn hello_name<'a>( 77 | _menu: &Menu, 78 | item: &Item, 79 | args: &[&str], 80 | context: &mut UartDriver, 81 | ) { 82 | // Print to console passed "name" argument 83 | writeln!( 84 | context, 85 | "Hello, {}!", 86 | argument_finder(item, args, "name").unwrap().unwrap() 87 | ) 88 | .unwrap(); 89 | } 90 | -------------------------------------------------------------------------------- /std_examples/cli/wokwi.toml: -------------------------------------------------------------------------------- 1 | [wokwi] 2 | version = 1 3 | gdbServerPort = 3333 4 | elf = "target/riscv32imc-esp-espidf/debug/cli" 5 | firmware = "target/riscv32imc-esp-espidf/debug/cli" 6 | -------------------------------------------------------------------------------- /std_examples/gpio/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | # Note: these variables are not used when using pio builder (`cargo build --features pio`) 15 | ESP_IDF_VERSION = "release/v4.4" 16 | 17 | -------------------------------------------------------------------------------- /std_examples/gpio/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_examples/gpio/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "gpio" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.66" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | 18 | default = ["std", "hal", "esp-idf-sys/native"] 19 | 20 | 21 | pio = ["esp-idf-sys/pio"] 22 | all = ["std", "nightly", "experimental", "embassy"] 23 | hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"] 24 | std = [ 25 | "alloc", 26 | "esp-idf-sys/std", 27 | "esp-idf-sys/binstart", 28 | "embedded-svc?/std", 29 | "esp-idf-hal?/std", 30 | "esp-idf-svc?/std", 31 | ] 32 | alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"] 33 | nightly = [ 34 | "embedded-svc?/nightly", 35 | "esp-idf-svc?/nightly", 36 | ] # Future: "esp-idf-hal?/nightly" 37 | experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"] 38 | embassy = [ 39 | "esp-idf-hal?/embassy-sync", 40 | "esp-idf-hal?/critical-section", 41 | "esp-idf-hal?/edge-executor", 42 | "esp-idf-svc?/embassy-time-driver", 43 | "esp-idf-svc?/embassy-time-isr-queue", 44 | ] 45 | 46 | [dependencies] 47 | log = { version = "0.4.17", default-features = false } 48 | esp-idf-sys = { version = "0.33", default-features = false } 49 | esp-idf-hal = { version = "0.41", optional = true, default-features = false } 50 | esp-idf-svc = { version = "0.46", optional = true, default-features = false } 51 | embedded-svc = { version = "0.25", optional = true, default-features = false } 52 | 53 | [build-dependencies] 54 | embuild = "0.31.2" 55 | -------------------------------------------------------------------------------- /std_examples/gpio/build.rs: -------------------------------------------------------------------------------- 1 | // Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641 2 | fn main() -> Result<(), Box> { 3 | embuild::build::CfgArgs::output_propagated("ESP_IDF")?; 4 | embuild::build::LinkArgs::output_propagated("ESP_IDF")?; 5 | Ok(()) 6 | } 7 | -------------------------------------------------------------------------------- /std_examples/gpio/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_examples/gpio/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=7000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_examples/gpio_interrupt/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | # Note: these variables are not used when using pio builder (`cargo build --features pio`) 15 | ESP_IDF_VERSION = "release/v4.4" 16 | 17 | -------------------------------------------------------------------------------- /std_examples/gpio_interrupt/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_examples/gpio_interrupt/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "gpio" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.66" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | 18 | default = ["std", "hal", "esp-idf-sys/native"] 19 | 20 | 21 | pio = ["esp-idf-sys/pio"] 22 | all = ["std", "nightly", "experimental", "embassy"] 23 | hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"] 24 | std = [ 25 | "alloc", 26 | "esp-idf-sys/std", 27 | "esp-idf-sys/binstart", 28 | "embedded-svc?/std", 29 | "esp-idf-hal?/std", 30 | "esp-idf-svc?/std", 31 | ] 32 | alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"] 33 | nightly = [ 34 | "embedded-svc?/nightly", 35 | "esp-idf-svc?/nightly", 36 | ] # Future: "esp-idf-hal?/nightly" 37 | experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"] 38 | embassy = [ 39 | "esp-idf-hal?/embassy-sync", 40 | "esp-idf-hal?/critical-section", 41 | "esp-idf-hal?/edge-executor", 42 | "esp-idf-svc?/embassy-time-driver", 43 | "esp-idf-svc?/embassy-time-isr-queue", 44 | ] 45 | 46 | [dependencies] 47 | log = { version = "0.4.17", default-features = false } 48 | esp-idf-sys = { version = "0.33", default-features = false } 49 | esp-idf-hal = { version = "0.41", optional = true, default-features = false } 50 | esp-idf-svc = { version = "0.46", optional = true, default-features = false } 51 | embedded-svc = { version = "0.25", optional = true, default-features = false } 52 | anyhow = "1.0.75" 53 | 54 | [build-dependencies] 55 | embuild = "0.31.2" 56 | anyhow = "1.0.75" 57 | -------------------------------------------------------------------------------- /std_examples/gpio_interrupt/build.rs: -------------------------------------------------------------------------------- 1 | // Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641 2 | fn main() -> Result<(), Box> { 3 | embuild::build::CfgArgs::output_propagated("ESP_IDF")?; 4 | embuild::build::LinkArgs::output_propagated("ESP_IDF")?; 5 | Ok(()) 6 | } 7 | -------------------------------------------------------------------------------- /std_examples/gpio_interrupt/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_examples/gpio_interrupt/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=7000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_examples/gpio_interrupt/src/main.rs: -------------------------------------------------------------------------------- 1 | /* 2 | For a detailed explanation of this code check out the associated blog post: 3 | https://apollolabsblog.hashnode.dev/esp32-standard-library-embedded-rust-gpio-interrupts 4 | 5 | GitHub Repo containing source code and other examples: 6 | https://github.com/apollolabsdev 7 | 8 | For notifications on similar examples and more, subscribe to newsletter here: 9 | https://www.theembeddedrustacean.com/subscribe 10 | */ 11 | 12 | use std::sync::atomic::AtomicBool; 13 | use std::sync::atomic::Ordering; 14 | 15 | use esp_idf_hal::gpio::*; 16 | use esp_idf_hal::peripherals::Peripherals; 17 | use esp_idf_sys::{self as _}; 18 | 19 | static FLAG: AtomicBool = AtomicBool::new(false); 20 | 21 | fn gpio_int_callback() { 22 | // Assert FLAG indicating a press button happened 23 | FLAG.store(true, Ordering::Relaxed); 24 | } 25 | 26 | fn main() -> ! { 27 | // It is necessary to call this function once. Otherwise some patches to the runtime 28 | // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71 29 | esp_idf_sys::link_patches(); 30 | 31 | // Take Peripherals 32 | let dp = Peripherals::take().unwrap(); 33 | 34 | // Configure button pin as input 35 | let mut button = PinDriver::input(dp.pins.gpio0).unwrap(); 36 | // Configure button pin with internal pull up 37 | button.set_pull(Pull::Up).unwrap(); 38 | // Configure button pin to detect interrupts on a positive edge 39 | button.set_interrupt_type(InterruptType::PosEdge).unwrap(); 40 | // Attach the ISR to the button interrupt 41 | unsafe { button.subscribe(gpio_int_callback).unwrap() } 42 | // Enable interrupts 43 | button.enable_interrupt().unwrap(); 44 | 45 | // Set up a variable that keeps track of press button count 46 | let mut count = 0_u32; 47 | 48 | loop { 49 | // Check if global flag is asserted 50 | if FLAG.load(Ordering::Relaxed) { 51 | // Reset global flag 52 | FLAG.store(false, Ordering::Relaxed); 53 | // Update Press count and print 54 | count = count.wrapping_add(1); 55 | println!("Press Count {}", count); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /std_examples/i2c/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | # Note: these variables are not used when using pio builder (`cargo build --features pio`) 15 | ESP_IDF_VERSION = "release/v4.4" 16 | 17 | -------------------------------------------------------------------------------- /std_examples/i2c/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_examples/i2c/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "gpio" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.66" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | 18 | default = ["std", "hal", "esp-idf-sys/native"] 19 | 20 | 21 | pio = ["esp-idf-sys/pio"] 22 | all = ["std", "nightly", "experimental", "embassy"] 23 | hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"] 24 | std = [ 25 | "alloc", 26 | "esp-idf-sys/std", 27 | "esp-idf-sys/binstart", 28 | "embedded-svc?/std", 29 | "esp-idf-hal?/std", 30 | "esp-idf-svc?/std", 31 | ] 32 | alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"] 33 | nightly = [ 34 | "embedded-svc?/nightly", 35 | "esp-idf-svc?/nightly", 36 | ] # Future: "esp-idf-hal?/nightly" 37 | experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"] 38 | embassy = [ 39 | "esp-idf-hal?/embassy-sync", 40 | "esp-idf-hal?/critical-section", 41 | "esp-idf-hal?/edge-executor", 42 | "esp-idf-svc?/embassy-time-driver", 43 | "esp-idf-svc?/embassy-time-isr-queue", 44 | ] 45 | 46 | [dependencies] 47 | log = { version = "0.4.17", default-features = false } 48 | esp-idf-sys = { version = "0.33", default-features = false } 49 | esp-idf-hal = { version = "0.41", optional = true, default-features = false } 50 | esp-idf-svc = { version = "0.46", optional = true, default-features = false } 51 | embedded-svc = { version = "0.25", optional = true, default-features = false } 52 | nobcd = "0.2.0" 53 | 54 | [build-dependencies] 55 | embuild = "0.31.2" 56 | -------------------------------------------------------------------------------- /std_examples/i2c/build.rs: -------------------------------------------------------------------------------- 1 | // Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641 2 | fn main() -> Result<(), Box> { 3 | embuild::build::CfgArgs::output_propagated("ESP_IDF")?; 4 | embuild::build::LinkArgs::output_propagated("ESP_IDF")?; 5 | Ok(()) 6 | } 7 | -------------------------------------------------------------------------------- /std_examples/i2c/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_examples/i2c/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=7000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_examples/pwm/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | # Note: these variables are not used when using pio builder (`cargo build --features pio`) 15 | ESP_IDF_VERSION = "release/v4.4" 16 | 17 | -------------------------------------------------------------------------------- /std_examples/pwm/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_examples/pwm/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "gpio" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.66" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | 18 | default = ["std", "hal", "esp-idf-sys/native"] 19 | 20 | 21 | pio = ["esp-idf-sys/pio"] 22 | all = ["std", "nightly", "experimental", "embassy"] 23 | hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"] 24 | std = [ 25 | "alloc", 26 | "esp-idf-sys/std", 27 | "esp-idf-sys/binstart", 28 | "embedded-svc?/std", 29 | "esp-idf-hal?/std", 30 | "esp-idf-svc?/std", 31 | ] 32 | alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"] 33 | nightly = [ 34 | "embedded-svc?/nightly", 35 | "esp-idf-svc?/nightly", 36 | ] # Future: "esp-idf-hal?/nightly" 37 | experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"] 38 | embassy = [ 39 | "esp-idf-hal?/embassy-sync", 40 | "esp-idf-hal?/critical-section", 41 | "esp-idf-hal?/edge-executor", 42 | "esp-idf-svc?/embassy-time-driver", 43 | "esp-idf-svc?/embassy-time-isr-queue", 44 | ] 45 | 46 | [dependencies] 47 | log = { version = "0.4.17", default-features = false } 48 | esp-idf-sys = { version = "0.33", default-features = false } 49 | esp-idf-hal = { version = "0.41", optional = true, default-features = false } 50 | esp-idf-svc = { version = "0.46", optional = true, default-features = false } 51 | embedded-svc = { version = "0.25", optional = true, default-features = false } 52 | nobcd = "0.2.0" 53 | 54 | [build-dependencies] 55 | embuild = "0.31.2" 56 | -------------------------------------------------------------------------------- /std_examples/pwm/build.rs: -------------------------------------------------------------------------------- 1 | // Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641 2 | fn main() -> Result<(), Box> { 3 | embuild::build::CfgArgs::output_propagated("ESP_IDF")?; 4 | embuild::build::LinkArgs::output_propagated("ESP_IDF")?; 5 | Ok(()) 6 | } 7 | -------------------------------------------------------------------------------- /std_examples/pwm/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_examples/pwm/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=7000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_examples/pwm/src/main.rs: -------------------------------------------------------------------------------- 1 | /* 2 | For a detailed explanation of this code check out the associated blog post: 3 | https://apollolabsblog.hashnode.dev/esp32-standard-library-embedded-rust-pwm-servo-motor-sweep 4 | 5 | GitHub Repo containing source code and other examples: 6 | https://github.com/apollolabsdev 7 | 8 | For notifications on similar examples and more, subscribe to newsletter here: 9 | https://subscribepage.io/apollolabsnewsletter 10 | */ 11 | 12 | use esp_idf_hal::delay::FreeRtos; 13 | use esp_idf_hal::ledc::{config::TimerConfig, LedcDriver, LedcTimerDriver, Resolution}; 14 | use esp_idf_hal::peripherals::Peripherals; 15 | use esp_idf_hal::prelude::*; 16 | 17 | fn main() { 18 | esp_idf_sys::link_patches(); 19 | 20 | // Take Peripherals 21 | let peripherals = Peripherals::take().unwrap(); 22 | 23 | // Configure and Initialize LEDC Timer Driver 24 | let timer_driver = LedcTimerDriver::new( 25 | peripherals.ledc.timer0, 26 | &TimerConfig::default() 27 | .frequency(50.Hz()) 28 | .resolution(Resolution::Bits14), 29 | ) 30 | .unwrap(); 31 | 32 | // Configure and Initialize LEDC Driver 33 | let mut driver = LedcDriver::new( 34 | peripherals.ledc.channel0, 35 | timer_driver, 36 | peripherals.pins.gpio7, 37 | ) 38 | .unwrap(); 39 | 40 | // Get Max Duty and Calculate Upper and Lower Limits for Servo 41 | let max_duty = driver.get_max_duty(); 42 | println!("Max Duty {}", max_duty); 43 | let min_limit = max_duty * 25 / 1000; 44 | println!("Min Limit {}", min_limit); 45 | let max_limit = max_duty * 125 / 1000; 46 | println!("Max Limit {}", max_limit); 47 | 48 | // Define Starting Position 49 | driver 50 | .set_duty(map(0, 0, 180, min_limit, max_limit)) 51 | .unwrap(); 52 | // Give servo some time to update 53 | FreeRtos::delay_ms(500); 54 | 55 | loop { 56 | // Sweep from 0 degrees to 180 degrees 57 | for angle in 0..180 { 58 | // Print Current Angle for visual verification 59 | println!("Current Angle {} Degrees", angle); 60 | // Set the desired duty cycle 61 | driver 62 | .set_duty(map(angle, 0, 180, min_limit, max_limit)) 63 | .unwrap(); 64 | // Give servo some time to update 65 | FreeRtos::delay_ms(12); 66 | } 67 | 68 | // Sweep from 180 degrees to 0 degrees 69 | for angle in (0..180).rev() { 70 | // Print Current Angle for visual verification 71 | println!("Current Angle {} Degrees", angle); 72 | // Set the desired duty cycle 73 | driver 74 | .set_duty(map(angle, 0, 180, min_limit, max_limit)) 75 | .unwrap(); 76 | // Give servo some time to update 77 | FreeRtos::delay_ms(12); 78 | } 79 | } 80 | } 81 | 82 | // Function that maps one range to another 83 | fn map(x: u32, in_min: u32, in_max: u32, out_min: u32, out_max: u32) -> u32 { 84 | (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min 85 | } 86 | -------------------------------------------------------------------------------- /std_examples/rtos_tasks/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | # Note: these variables are not used when using pio builder (`cargo build --features pio`) 15 | ESP_IDF_VERSION = "release/v4.4" 16 | 17 | -------------------------------------------------------------------------------- /std_examples/rtos_tasks/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_examples/rtos_tasks/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "gpio" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.66" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | 18 | default = ["std", "hal", "esp-idf-sys/native"] 19 | 20 | 21 | pio = ["esp-idf-sys/pio"] 22 | all = ["std", "nightly", "experimental", "embassy"] 23 | hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"] 24 | std = [ 25 | "alloc", 26 | "esp-idf-sys/std", 27 | "esp-idf-sys/binstart", 28 | "embedded-svc?/std", 29 | "esp-idf-hal?/std", 30 | "esp-idf-svc?/std", 31 | ] 32 | alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"] 33 | nightly = [ 34 | "embedded-svc?/nightly", 35 | "esp-idf-svc?/nightly", 36 | ] # Future: "esp-idf-hal?/nightly" 37 | experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"] 38 | embassy = [ 39 | "esp-idf-hal?/embassy-sync", 40 | "esp-idf-hal?/critical-section", 41 | "esp-idf-hal?/edge-executor", 42 | "esp-idf-svc?/embassy-time-driver", 43 | "esp-idf-svc?/embassy-time-isr-queue", 44 | ] 45 | 46 | [dependencies] 47 | log = { version = "0.4.17", default-features = false } 48 | esp-idf-sys = { version = "0.33", default-features = false } 49 | esp-idf-hal = { version = "0.41", optional = true, default-features = false } 50 | esp-idf-svc = { version = "0.46", optional = true, default-features = false } 51 | embedded-svc = { version = "0.25", optional = true, default-features = false } 52 | anyhow = "=1.0.75" 53 | 54 | [build-dependencies] 55 | embuild = "0.31.2" 56 | anyhow = "=1.0.75" 57 | -------------------------------------------------------------------------------- /std_examples/rtos_tasks/build.rs: -------------------------------------------------------------------------------- 1 | // Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641 2 | fn main() -> Result<(), Box> { 3 | embuild::build::CfgArgs::output_propagated("ESP_IDF")?; 4 | embuild::build::LinkArgs::output_propagated("ESP_IDF")?; 5 | Ok(()) 6 | } 7 | -------------------------------------------------------------------------------- /std_examples/rtos_tasks/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_examples/rtos_tasks/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=7000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_examples/rtos_tasks/src/main.rs: -------------------------------------------------------------------------------- 1 | /* 2 | For a detailed explanation of this code check out the associated blog post: 3 | https://apollolabsblog.hashnode.dev/esp-embedded-rust-multithreading-with-freertos-bindings 4 | 5 | GitHub Repo containing source code and other examples: 6 | https://github.com/apollolabsdev 7 | 8 | For notifications on similar examples and more, subscribe to newsletter here: 9 | https://www.theembeddedrustacean.com/subscribe 10 | */ 11 | 12 | use esp_idf_hal::delay::FreeRtos; 13 | use esp_idf_sys::{self as _}; 14 | use esp_idf_sys::{esp, vTaskDelay, xPortGetTickRateHz, xTaskCreatePinnedToCore, xTaskDelayUntil}; 15 | use std::ffi::CString; 16 | 17 | unsafe extern "C" fn task1(_: *mut core::ffi::c_void) { 18 | loop { 19 | println!("Task 1 Entered"); 20 | FreeRtos::delay_ms(1000); 21 | } 22 | } 23 | 24 | unsafe extern "C" fn task2(_: *mut core::ffi::c_void) { 25 | loop { 26 | println!("Task 2 Entered"); 27 | FreeRtos::delay_ms(2000); 28 | } 29 | } 30 | 31 | fn main() -> anyhow::Result<()> { 32 | // It is necessary to call this function once. Otherwise some patches to the runtime 33 | // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71 34 | esp_idf_sys::link_patches(); 35 | 36 | unsafe { 37 | xTaskCreatePinnedToCore( 38 | Some(task1), 39 | CString::new("Task 1").unwrap().as_ptr(), 40 | 1000, 41 | std::ptr::null_mut(), 42 | 10, 43 | std::ptr::null_mut(), 44 | 1, 45 | ); 46 | } 47 | FreeRtos::delay_ms(1000); 48 | unsafe { 49 | xTaskCreatePinnedToCore( 50 | Some(task2), 51 | CString::new("Task 2").unwrap().as_ptr(), 52 | 1000, 53 | std::ptr::null_mut(), 54 | 9, 55 | std::ptr::null_mut(), 56 | 1, 57 | ); 58 | } 59 | loop { 60 | println!("Hello From Main"); 61 | FreeRtos::delay_ms(500); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /std_examples/spi/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | # Note: these variables are not used when using pio builder (`cargo build --features pio`) 15 | ESP_IDF_VERSION = "release/v4.4" 16 | 17 | -------------------------------------------------------------------------------- /std_examples/spi/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_examples/spi/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "gpio" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.66" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | 18 | default = ["std", "hal", "esp-idf-sys/native"] 19 | 20 | 21 | pio = ["esp-idf-sys/pio"] 22 | all = ["std", "nightly", "experimental", "embassy"] 23 | hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"] 24 | std = [ 25 | "alloc", 26 | "esp-idf-sys/std", 27 | "esp-idf-sys/binstart", 28 | "embedded-svc?/std", 29 | "esp-idf-hal?/std", 30 | "esp-idf-svc?/std", 31 | ] 32 | alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"] 33 | nightly = [ 34 | "embedded-svc?/nightly", 35 | "esp-idf-svc?/nightly", 36 | ] # Future: "esp-idf-hal?/nightly" 37 | experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"] 38 | embassy = [ 39 | "esp-idf-hal?/embassy-sync", 40 | "esp-idf-hal?/critical-section", 41 | "esp-idf-hal?/edge-executor", 42 | "esp-idf-svc?/embassy-time-driver", 43 | "esp-idf-svc?/embassy-time-isr-queue", 44 | ] 45 | 46 | [dependencies] 47 | log = { version = "0.4.17", default-features = false } 48 | esp-idf-sys = { version = "0.33", default-features = false } 49 | esp-idf-hal = { version = "0.41", optional = true, default-features = false } 50 | esp-idf-svc = { version = "0.46", optional = true, default-features = false } 51 | embedded-svc = { version = "0.25", optional = true, default-features = false } 52 | embedded-hal = "1.0.0-alpha.10" 53 | 54 | [build-dependencies] 55 | embuild = "0.31.2" 56 | -------------------------------------------------------------------------------- /std_examples/spi/build.rs: -------------------------------------------------------------------------------- 1 | // Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641 2 | fn main() -> Result<(), Box> { 3 | embuild::build::CfgArgs::output_propagated("ESP_IDF")?; 4 | embuild::build::LinkArgs::output_propagated("ESP_IDF")?; 5 | Ok(()) 6 | } 7 | -------------------------------------------------------------------------------- /std_examples/spi/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_examples/spi/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=7000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_examples/timer/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | # Note: these variables are not used when using pio builder (`cargo build --features pio`) 15 | ESP_IDF_VERSION = "release/v4.4" 16 | 17 | -------------------------------------------------------------------------------- /std_examples/timer/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_examples/timer/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "gpio" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.66" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | 18 | default = ["std", "hal", "esp-idf-sys/native"] 19 | 20 | 21 | pio = ["esp-idf-sys/pio"] 22 | all = ["std", "nightly", "experimental", "embassy"] 23 | hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"] 24 | std = [ 25 | "alloc", 26 | "esp-idf-sys/std", 27 | "esp-idf-sys/binstart", 28 | "embedded-svc?/std", 29 | "esp-idf-hal?/std", 30 | "esp-idf-svc?/std", 31 | ] 32 | alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"] 33 | nightly = [ 34 | "embedded-svc?/nightly", 35 | "esp-idf-svc?/nightly", 36 | ] # Future: "esp-idf-hal?/nightly" 37 | experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"] 38 | embassy = [ 39 | "esp-idf-hal?/embassy-sync", 40 | "esp-idf-hal?/critical-section", 41 | "esp-idf-hal?/edge-executor", 42 | "esp-idf-svc?/embassy-time-driver", 43 | "esp-idf-svc?/embassy-time-isr-queue", 44 | ] 45 | 46 | [dependencies] 47 | log = { version = "0.4.17", default-features = false } 48 | esp-idf-sys = { version = "0.33", default-features = false } 49 | esp-idf-hal = { version = "0.41", optional = true, default-features = false } 50 | esp-idf-svc = { version = "0.46", optional = true, default-features = false } 51 | embedded-svc = { version = "0.25", optional = true, default-features = false } 52 | nobcd = "0.2.0" 53 | 54 | [build-dependencies] 55 | embuild = "0.31.2" 56 | -------------------------------------------------------------------------------- /std_examples/timer/build.rs: -------------------------------------------------------------------------------- 1 | // Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641 2 | fn main() -> Result<(), Box> { 3 | embuild::build::CfgArgs::output_propagated("ESP_IDF")?; 4 | embuild::build::LinkArgs::output_propagated("ESP_IDF")?; 5 | Ok(()) 6 | } 7 | -------------------------------------------------------------------------------- /std_examples/timer/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_examples/timer/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=7000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_examples/uart/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | # Note: these variables are not used when using pio builder (`cargo build --features pio`) 15 | ESP_IDF_VERSION = "release/v4.4" 16 | 17 | -------------------------------------------------------------------------------- /std_examples/uart/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_examples/uart/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "gpio" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.66" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | 18 | default = ["std", "hal", "esp-idf-sys/native"] 19 | 20 | 21 | pio = ["esp-idf-sys/pio"] 22 | all = ["std", "nightly", "experimental", "embassy"] 23 | hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"] 24 | std = [ 25 | "alloc", 26 | "esp-idf-sys/std", 27 | "esp-idf-sys/binstart", 28 | "embedded-svc?/std", 29 | "esp-idf-hal?/std", 30 | "esp-idf-svc?/std", 31 | ] 32 | alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"] 33 | nightly = [ 34 | "embedded-svc?/nightly", 35 | "esp-idf-svc?/nightly", 36 | ] # Future: "esp-idf-hal?/nightly" 37 | experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"] 38 | embassy = [ 39 | "esp-idf-hal?/embassy-sync", 40 | "esp-idf-hal?/critical-section", 41 | "esp-idf-hal?/edge-executor", 42 | "esp-idf-svc?/embassy-time-driver", 43 | "esp-idf-svc?/embassy-time-isr-queue", 44 | ] 45 | 46 | [dependencies] 47 | log = { version = "0.4.17", default-features = false } 48 | esp-idf-sys = { version = "0.33", default-features = false } 49 | esp-idf-hal = { version = "0.41", optional = true, default-features = false } 50 | esp-idf-svc = { version = "0.46", optional = true, default-features = false } 51 | embedded-svc = { version = "0.25", optional = true, default-features = false } 52 | 53 | [build-dependencies] 54 | embuild = "0.31.2" 55 | -------------------------------------------------------------------------------- /std_examples/uart/build.rs: -------------------------------------------------------------------------------- 1 | // Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641 2 | fn main() -> Result<(), Box> { 3 | embuild::build::CfgArgs::output_propagated("ESP_IDF")?; 4 | embuild::build::LinkArgs::output_propagated("ESP_IDF")?; 5 | Ok(()) 6 | } 7 | -------------------------------------------------------------------------------- /std_examples/uart/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_examples/uart/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=7000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_examples/uart/src/main.rs: -------------------------------------------------------------------------------- 1 | use esp_idf_hal::delay::BLOCK; 2 | use esp_idf_hal::gpio; 3 | use esp_idf_hal::peripherals::Peripherals; 4 | use esp_idf_hal::prelude::*; 5 | use esp_idf_hal::uart::*; 6 | 7 | // Message to Send 8 | const MESSAGE: &str = "Hello"; 9 | // Key Value (Can be any value from 1 to 255) 10 | const KEY: u8 = 212; 11 | 12 | fn main() { 13 | esp_idf_sys::link_patches(); 14 | 15 | let peripherals = Peripherals::take().unwrap(); 16 | let tx = peripherals.pins.gpio5; 17 | let rx = peripherals.pins.gpio6; 18 | 19 | let config = config::Config::new().baudrate(Hertz(115_200)); 20 | let uart = UartDriver::new( 21 | peripherals.uart1, 22 | tx, 23 | rx, 24 | Option::::None, 25 | Option::::None, 26 | &config, 27 | ) 28 | .unwrap(); 29 | 30 | let mut rec = Vec::new(); 31 | 32 | // Print Message to be Sent 33 | println!("Sent Message in Text: {}", MESSAGE); 34 | println!("Sent Message in Values: {:?}", MESSAGE.as_bytes()); 35 | 36 | // Garble Message 37 | let gmsg: Vec = MESSAGE.as_bytes().iter().map(|m| m ^ KEY).collect(); 38 | 39 | // Print Garbled Message 40 | println!("Sent Garbled Message Values: {:?}", gmsg); 41 | 42 | // Send Garbled Message u8 Values One by One until Full Array is Sent 43 | for letter in gmsg.iter() { 44 | // Send Garbled Message Value 45 | uart.write(&[*letter]).unwrap(); 46 | 47 | // Recieve Garbled Message Value 48 | let mut buf = [0_u8; 1]; 49 | uart.read(&mut buf, BLOCK).unwrap(); 50 | 51 | // Buffer Recieved Message Value 52 | rec.extend_from_slice(&buf); 53 | } 54 | 55 | // Print Recieved Garbled Message Values 56 | println!("Recieved Garbled Message Values: {:?}", rec); 57 | 58 | // UnGarble Message 59 | let ugmsg: Vec = rec.iter().map(|m| m ^ KEY).collect(); 60 | println!("Ungarbled Message in Values: {:?}", ugmsg); 61 | 62 | // Print Recovered Message 63 | if let Ok(rmsg) = std::str::from_utf8(&ugmsg) { 64 | println!("Recieved Message in Text: {:?}", rmsg); 65 | }; 66 | 67 | loop {} 68 | } 69 | -------------------------------------------------------------------------------- /std_net_examples/http_client/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["--cfg", "espidf_time64", "-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | # Note: these variables are not used when using pio builder (`cargo build --features pio`) 15 | ESP_IDF_VERSION = "v5.1" 16 | 17 | -------------------------------------------------------------------------------- /std_net_examples/http_client/.github/workflows/rust_ci.yml: -------------------------------------------------------------------------------- 1 | name: Continuous Integration 2 | 3 | on: 4 | push: 5 | paths-ignore: 6 | - "**/README.md" 7 | pull_request: 8 | workflow_dispatch: 9 | 10 | env: 11 | CARGO_TERM_COLOR: always 12 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 13 | 14 | jobs: 15 | rust-checks: 16 | name: Rust Checks 17 | runs-on: ubuntu-latest 18 | strategy: 19 | fail-fast: false 20 | matrix: 21 | action: 22 | - command: build 23 | args: --release 24 | - command: fmt 25 | args: --all -- --check --color always 26 | - command: clippy 27 | args: --all-targets --all-features --workspace -- -D warnings 28 | steps: 29 | - name: Checkout repository 30 | uses: actions/checkout@v4 31 | - name: Enable caching 32 | uses: Swatinem/rust-cache@v2 33 | - name: Setup Rust 34 | uses: dtolnay/rust-toolchain@v1 35 | with: 36 | toolchain: nightly 37 | components: rust-src 38 | - name: Run command 39 | run: cargo ${{ matrix.action.command }} ${{ matrix.action.args }} 40 | -------------------------------------------------------------------------------- /std_net_examples/http_client/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_net_examples/http_client/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "http" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.66" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | 18 | default = ["all", "hal", "esp-idf-sys/native"] 19 | 20 | 21 | pio = ["esp-idf-sys/pio"] 22 | all = ["std", "nightly", "experimental", "embassy"] 23 | hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"] 24 | std = [ 25 | "alloc", 26 | "esp-idf-sys/std", 27 | "esp-idf-sys/binstart", 28 | "embedded-svc?/std", 29 | "esp-idf-hal?/std", 30 | "esp-idf-svc?/std", 31 | ] 32 | alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"] 33 | nightly = [ 34 | "embedded-svc?/nightly", 35 | "esp-idf-svc?/nightly", 36 | ] # Future: "esp-idf-hal?/nightly" 37 | experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"] 38 | embassy = [ 39 | "esp-idf-hal?/embassy-sync", 40 | "esp-idf-hal?/critical-section", 41 | "esp-idf-hal?/edge-executor", 42 | "esp-idf-svc?/embassy-time-driver", 43 | "esp-idf-svc?/embassy-time-isr-queue", 44 | ] 45 | 46 | [dependencies] 47 | log = { version = "0.4.17", default-features = false } 48 | esp-idf-sys = { version = "0.33", default-features = false } 49 | esp-idf-hal = { version = "0.41", optional = true, default-features = false } 50 | esp-idf-svc = { version = "0.46", optional = true, default-features = false } 51 | embedded-svc = { version = "0.25", optional = true, default-features = false } 52 | anyhow = "1.0.75" 53 | 54 | [build-dependencies] 55 | embuild = "0.31.2" 56 | anyhow = "1.0.75" 57 | -------------------------------------------------------------------------------- /std_net_examples/http_client/build.rs: -------------------------------------------------------------------------------- 1 | // Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641 2 | fn main() -> Result<(), Box> { 3 | embuild::build::CfgArgs::output_propagated("ESP_IDF")?; 4 | embuild::build::LinkArgs::output_propagated("ESP_IDF")?; 5 | Ok(()) 6 | } 7 | -------------------------------------------------------------------------------- /std_net_examples/http_client/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_net_examples/http_client/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=7000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_net_examples/http_server/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["--cfg", "espidf_time64", "-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | # Note: these variables are not used when using pio builder (`cargo build --features pio`) 15 | ESP_IDF_VERSION = "tag:v5.1" 16 | 17 | -------------------------------------------------------------------------------- /std_net_examples/http_server/.github/workflows/rust_ci.yml: -------------------------------------------------------------------------------- 1 | name: Continuous Integration 2 | 3 | on: 4 | push: 5 | paths-ignore: 6 | - "**/README.md" 7 | pull_request: 8 | workflow_dispatch: 9 | 10 | env: 11 | CARGO_TERM_COLOR: always 12 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 13 | 14 | jobs: 15 | rust-checks: 16 | name: Rust Checks 17 | runs-on: ubuntu-latest 18 | strategy: 19 | fail-fast: false 20 | matrix: 21 | action: 22 | - command: build 23 | args: --release 24 | - command: fmt 25 | args: --all -- --check --color always 26 | - command: clippy 27 | args: --all-targets --all-features --workspace -- -D warnings 28 | steps: 29 | - name: Checkout repository 30 | uses: actions/checkout@v4 31 | - name: Enable caching 32 | uses: Swatinem/rust-cache@v2 33 | - name: Setup Rust 34 | uses: dtolnay/rust-toolchain@v1 35 | with: 36 | toolchain: nightly 37 | components: rust-src 38 | - name: Run command 39 | run: cargo ${{ matrix.action.command }} ${{ matrix.action.args }} 40 | -------------------------------------------------------------------------------- /std_net_examples/http_server/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_net_examples/http_server/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "http" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.66" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | 18 | default = ["all", "hal", "esp-idf-sys/native"] 19 | 20 | 21 | pio = ["esp-idf-sys/pio"] 22 | all = ["std", "nightly", "experimental", "embassy"] 23 | hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"] 24 | std = [ 25 | "alloc", 26 | "esp-idf-sys/std", 27 | "esp-idf-sys/binstart", 28 | "embedded-svc?/std", 29 | "esp-idf-hal?/std", 30 | "esp-idf-svc?/std", 31 | ] 32 | alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"] 33 | nightly = [ 34 | "embedded-svc?/nightly", 35 | "esp-idf-svc?/nightly", 36 | ] # Future: "esp-idf-hal?/nightly" 37 | experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"] 38 | embassy = [ 39 | "esp-idf-hal?/embassy-sync", 40 | "esp-idf-hal?/critical-section", 41 | "esp-idf-hal?/edge-executor", 42 | "esp-idf-svc?/embassy-time-driver", 43 | "esp-idf-svc?/embassy-time-isr-queue", 44 | ] 45 | 46 | [dependencies] 47 | log = { version = "0.4.17", default-features = false } 48 | esp-idf-sys = { version = "0.33", default-features = false } 49 | esp-idf-hal = { version = "0.41", optional = true, default-features = false } 50 | esp-idf-svc = { version = "0.46", optional = true, default-features = false } 51 | embedded-svc = { version = "0.25", optional = true, default-features = false } 52 | anyhow = "1.0.75" 53 | 54 | [build-dependencies] 55 | embuild = "0.31.2" 56 | -------------------------------------------------------------------------------- /std_net_examples/http_server/build.rs: -------------------------------------------------------------------------------- 1 | // Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641 2 | fn main() -> Result<(), Box> { 3 | embuild::build::CfgArgs::output_propagated("ESP_IDF")?; 4 | embuild::build::LinkArgs::output_propagated("ESP_IDF")?; 5 | Ok(()) 6 | } 7 | -------------------------------------------------------------------------------- /std_net_examples/http_server/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_net_examples/http_server/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=7000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_net_examples/http_server/src/main.rs: -------------------------------------------------------------------------------- 1 | /* 2 | For a detailed explanation of this code check out the associated blog post: 3 | https://apollolabsblog.hashnode.dev/edge-iot-with-rust-on-esp-http-server 4 | 5 | GitHub Repo containing source code and other examples: 6 | https://github.com/apollolabsdev 7 | 8 | For notifications on similar examples and more, subscribe to newsletter here: 9 | https://www.theembeddedrustacean.com/subscribe 10 | */ 11 | 12 | use anyhow; 13 | use embedded_svc::http::Method; 14 | use embedded_svc::wifi::{AuthMethod, ClientConfiguration, Configuration}; 15 | use esp_idf_hal::peripherals::Peripherals; 16 | use esp_idf_svc::eventloop::EspSystemEventLoop; 17 | use esp_idf_svc::http::server::{Configuration as HttpServerConfig, EspHttpServer}; 18 | use esp_idf_svc::nvs::EspDefaultNvsPartition; 19 | use esp_idf_svc::wifi::{BlockingWifi, EspWifi}; 20 | use std::{thread::sleep, time::Duration}; 21 | 22 | fn main() -> anyhow::Result<()> { 23 | esp_idf_sys::link_patches(); 24 | 25 | // Configure Wifi 26 | let peripherals = Peripherals::take().unwrap(); 27 | let sysloop = EspSystemEventLoop::take()?; 28 | let nvs = EspDefaultNvsPartition::take()?; 29 | 30 | let mut wifi = BlockingWifi::wrap( 31 | EspWifi::new(peripherals.modem, sysloop.clone(), Some(nvs))?, 32 | sysloop, 33 | )?; 34 | 35 | wifi.set_configuration(&Configuration::Client(ClientConfiguration { 36 | ssid: "SSID".into(), 37 | bssid: None, 38 | auth_method: AuthMethod::None, 39 | password: "PASSWORD".into(), 40 | channel: None, 41 | }))?; 42 | 43 | // Start Wifi 44 | wifi.start()?; 45 | 46 | // Connect Wifi 47 | wifi.connect()?; 48 | 49 | // Wait until the network interface is up 50 | wifi.wait_netif_up()?; 51 | 52 | println!("Wifi Connected, Starting HTTP Server"); 53 | 54 | // HTTP Configuration 55 | // Create HTTP Server Connection Handle 56 | let mut httpserver = EspHttpServer::new(&HttpServerConfig::default())?; 57 | 58 | // Define Server Request Handler Behaviour on Get for Root URL 59 | httpserver.fn_handler("/", Method::Get, |request| { 60 | // Retrieve html String 61 | let html = index_html(); 62 | // Respond with OK status 63 | let mut response = request.into_ok_response()?; 64 | // Return Requested Object (Index Page) 65 | response.write(html.as_bytes())?; 66 | Ok(()) 67 | })?; 68 | 69 | // Loop to Avoid Program Termination 70 | loop { 71 | sleep(Duration::from_millis(1000)); 72 | } 73 | } 74 | 75 | fn index_html() -> String { 76 | format!( 77 | r#" 78 | 79 | 80 | 81 | 82 | esp-rs web server 83 | 84 | 85 | Hello World from ESP! 86 | 87 | 88 | "# 89 | ) 90 | } 91 | -------------------------------------------------------------------------------- /std_net_examples/mqtt-sub/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["--cfg", "espidf_time64", "-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | MCU="esp32c3" 15 | # Note: this variable is not used by the pio builder (`cargo build --features pio`) 16 | ESP_IDF_VERSION = "v5.1.1" 17 | 18 | -------------------------------------------------------------------------------- /std_net_examples/mqtt-sub/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_net_examples/mqtt-sub/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "mqtt_sub" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.66" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | 18 | default = ["all", "hal", "esp-idf-sys/native"] 19 | 20 | 21 | pio = ["esp-idf-sys/pio"] 22 | all = ["std", "nightly", "experimental", "embassy"] 23 | hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"] 24 | std = [ 25 | "alloc", 26 | "esp-idf-sys/std", 27 | "esp-idf-sys/binstart", 28 | "embedded-svc?/std", 29 | "esp-idf-hal?/std", 30 | "esp-idf-svc?/std", 31 | ] 32 | alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"] 33 | nightly = [ 34 | "embedded-svc?/nightly", 35 | "esp-idf-svc?/nightly", 36 | ] # Future: "esp-idf-hal?/nightly" 37 | experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"] 38 | embassy = [ 39 | "esp-idf-hal?/embassy-sync", 40 | "esp-idf-hal?/critical-section", 41 | "esp-idf-svc?/embassy-time-driver", 42 | "esp-idf-svc?/embassy-time-isr-queue", 43 | ] 44 | 45 | [dependencies] 46 | log = { version = "0.4.17", default-features = false } 47 | esp-idf-sys = { version = "0.33", default-features = false } 48 | esp-idf-hal = { version = "0.42", optional = true, default-features = false } 49 | esp-idf-svc = { version = "0.47.2", optional = true, default-features = false } 50 | embedded-svc = { version = "0.26.2", optional = true, default-features = false } 51 | anyhow = "1.0.75" 52 | 53 | [build-dependencies] 54 | embuild = "0.31.2" -------------------------------------------------------------------------------- /std_net_examples/mqtt-sub/build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | embuild::espidf::sysenv::output(); 3 | } 4 | -------------------------------------------------------------------------------- /std_net_examples/mqtt-sub/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_net_examples/mqtt-sub/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_net_examples/ntp/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["--cfg", "espidf_time64", "-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | MCU="esp32c3" 15 | # Note: this variable is not used by the pio builder (`cargo build --features pio`) 16 | ESP_IDF_VERSION = "v5.1.1" 17 | 18 | -------------------------------------------------------------------------------- /std_net_examples/ntp/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_net_examples/ntp/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "http" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.66" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | 18 | default = ["all", "hal", "esp-idf-sys/native"] 19 | 20 | 21 | pio = ["esp-idf-sys/pio"] 22 | all = ["std", "nightly", "experimental", "embassy"] 23 | hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"] 24 | std = [ 25 | "alloc", 26 | "esp-idf-sys/std", 27 | "esp-idf-sys/binstart", 28 | "embedded-svc?/std", 29 | "esp-idf-hal?/std", 30 | "esp-idf-svc?/std", 31 | ] 32 | alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"] 33 | nightly = [ 34 | "embedded-svc?/nightly", 35 | "esp-idf-svc?/nightly", 36 | ] # Future: "esp-idf-hal?/nightly" 37 | experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"] 38 | embassy = [ 39 | "esp-idf-hal?/embassy-sync", 40 | "esp-idf-hal?/critical-section", 41 | "esp-idf-hal?/edge-executor", 42 | "esp-idf-svc?/embassy-time-driver", 43 | "esp-idf-svc?/embassy-time-isr-queue", 44 | ] 45 | 46 | [dependencies] 47 | log = { version = "0.4.17", default-features = false } 48 | esp-idf-sys = { version = "0.33", default-features = false } 49 | esp-idf-hal = { version = "0.41", optional = true, default-features = false } 50 | esp-idf-svc = { version = "0.46", optional = true, default-features = false } 51 | embedded-svc = { version = "0.25", optional = true, default-features = false } 52 | anyhow = "1.0.75" 53 | chrono = "0.4.31" 54 | 55 | [build-dependencies] 56 | embuild = "0.31.2" 57 | anyhow = "1.0.75" 58 | -------------------------------------------------------------------------------- /std_net_examples/ntp/build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | embuild::espidf::sysenv::output(); 3 | } 4 | -------------------------------------------------------------------------------- /std_net_examples/ntp/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_net_examples/ntp/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_net_examples/ntp/src/main.rs: -------------------------------------------------------------------------------- 1 | use anyhow; 2 | use chrono::{DateTime, Utc}; 3 | use embedded_svc::wifi::{AuthMethod, ClientConfiguration, Configuration}; 4 | use esp_idf_hal::delay::FreeRtos; 5 | use esp_idf_hal::peripherals::Peripherals; 6 | use esp_idf_svc::eventloop::EspSystemEventLoop; 7 | use esp_idf_svc::nvs::EspDefaultNvsPartition; 8 | use esp_idf_svc::sntp::{EspSntp, SyncStatus}; 9 | use esp_idf_svc::wifi::{BlockingWifi, EspWifi}; 10 | use std::time::SystemTime; 11 | 12 | fn main() -> anyhow::Result<()> { 13 | // It is necessary to call this function once. Otherwise some patches to the runtime 14 | // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71 15 | esp_idf_sys::link_patches(); 16 | 17 | let peripherals = Peripherals::take().unwrap(); 18 | let sysloop = EspSystemEventLoop::take()?; 19 | let nvs = EspDefaultNvsPartition::take()?; 20 | 21 | let mut wifi = BlockingWifi::wrap( 22 | EspWifi::new(peripherals.modem, sysloop.clone(), Some(nvs))?, 23 | sysloop, 24 | )?; 25 | 26 | wifi.set_configuration(&Configuration::Client(ClientConfiguration { 27 | ssid: "Wokwi-GUEST".into(), 28 | bssid: None, 29 | auth_method: AuthMethod::None, 30 | password: "".into(), 31 | channel: None, 32 | }))?; 33 | 34 | // Start Wifi 35 | wifi.start()?; 36 | 37 | // Connect Wifi 38 | wifi.connect()?; 39 | 40 | // Wait until the network interface is up 41 | wifi.wait_netif_up()?; 42 | 43 | // Print Out Wifi Connection Configuration 44 | while !wifi.is_connected().unwrap() { 45 | // Get and print connection configuration 46 | let config = wifi.get_configuration().unwrap(); 47 | println!("Waiting for station {:?}", config); 48 | } 49 | 50 | println!("Wifi Connected"); 51 | 52 | // Create Handle and Configure SNTP 53 | let ntp = EspSntp::new_default().unwrap(); 54 | 55 | // Synchronize NTP 56 | println!("Synchronizing with NTP Server"); 57 | while ntp.get_sync_status() != SyncStatus::Completed {} 58 | println!("Time Sync Completed"); 59 | 60 | loop { 61 | // Obtain System Time 62 | let st_now = SystemTime::now(); 63 | // Convert to UTC Time 64 | let dt_now_utc: DateTime = st_now.clone().into(); 65 | // Format Time String 66 | let formatted = format!("{}", dt_now_utc.format("%d/%m/%Y %H:%M:%S")); 67 | // Print Time 68 | println!("{}", formatted); 69 | // Delay 70 | FreeRtos::delay_ms(1000); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /std_net_examples/ping-cli/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = [ "--cfg", "espidf_time64"] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | MCU="esp32c3" 15 | # Note: this variable is not used by the pio builder (`cargo build --features pio`) 16 | ESP_IDF_VERSION = "v5.1.2" 17 | ESP_IDF_TOOLS_INSTALL_DIR = "global" -------------------------------------------------------------------------------- /std_net_examples/ping-cli/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_net_examples/ping-cli/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "ping-cli" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev "] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.71" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | default = ["std", "embassy", "esp-idf-svc/native"] 18 | 19 | pio = ["esp-idf-svc/pio"] 20 | std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"] 21 | alloc = ["esp-idf-svc/alloc"] 22 | nightly = ["esp-idf-svc/nightly"] 23 | experimental = ["esp-idf-svc/experimental"] 24 | embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"] 25 | 26 | [dependencies] 27 | log = { version = "0.4", default-features = false } 28 | esp-idf-svc = { version = "0.48", default-features = false } 29 | esp-idf-hal = { version = "0.43", default-features = false } 30 | menu = "0.4.0" 31 | anyhow = "=1.0.80" 32 | 33 | [build-dependencies] 34 | embuild = "0.31.3" 35 | -------------------------------------------------------------------------------- /std_net_examples/ping-cli/build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | embuild::espidf::sysenv::output(); 3 | } 4 | -------------------------------------------------------------------------------- /std_net_examples/ping-cli/diagram.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 1, 3 | "editor": "wokwi", 4 | "author": "apollolabsdev ", 5 | "parts": [ 6 | { 7 | "type": "board-esp32-c3-devkitm-1", 8 | "id": "esp", 9 | "top": 0.59, 10 | "left": 0.67, 11 | "attrs": { 12 | "flashSize": "16" 13 | } 14 | } 15 | ], 16 | "connections": [ 17 | [ 18 | "esp:TX", 19 | "$serialMonitor:RX", 20 | "", 21 | [] 22 | ], 23 | [ 24 | "esp:RX", 25 | "$serialMonitor:TX", 26 | "", 27 | [] 28 | ] 29 | ], 30 | "serialMonitor": { 31 | "display": "terminal", 32 | "convertEol": true 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /std_net_examples/ping-cli/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_net_examples/ping-cli/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_net_examples/ping-cli/wokwi.toml: -------------------------------------------------------------------------------- 1 | [wokwi] 2 | version = 1 3 | gdbServerPort = 3333 4 | elf = "target/riscv32imc-esp-espidf/debug/ping-cli" 5 | firmware = "target/riscv32imc-esp-espidf/debug/ping-cli" 6 | -------------------------------------------------------------------------------- /std_net_examples/ping/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = [ "--cfg", "espidf_time64"] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | MCU="esp32c3" 15 | ESP_IDF_VERSION = "v5.1.2" 16 | ESP_IDF_TOOLS_INSTALL_DIR = "global" -------------------------------------------------------------------------------- /std_net_examples/ping/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_net_examples/ping/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "ping" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev "] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.71" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | default = ["std", "embassy", "esp-idf-svc/native"] 18 | 19 | pio = ["esp-idf-svc/pio"] 20 | std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"] 21 | alloc = ["esp-idf-svc/alloc"] 22 | nightly = ["esp-idf-svc/nightly"] 23 | experimental = ["esp-idf-svc/experimental"] 24 | embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"] 25 | 26 | [dependencies] 27 | log = { version = "0.4", default-features = false } 28 | esp-idf-svc = { version = "0.48", default-features = false } 29 | esp-idf-hal = { version = "0.43", default-features = false } 30 | menu = "0.4.0" 31 | anyhow = "1.0.79" 32 | 33 | [build-dependencies] 34 | embuild = "0.31.3" 35 | -------------------------------------------------------------------------------- /std_net_examples/ping/build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | embuild::espidf::sysenv::output(); 3 | } 4 | -------------------------------------------------------------------------------- /std_net_examples/ping/diagram.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 1, 3 | "editor": "wokwi", 4 | "author": "apollolabsdev ", 5 | "parts": [ 6 | { 7 | "type": "board-esp32-c3-devkitm-1", 8 | "id": "esp", 9 | "top": 0.59, 10 | "left": 0.67, 11 | "attrs": { 12 | "flashSize": "16" 13 | } 14 | } 15 | ], 16 | "connections": [ 17 | [ 18 | "esp:TX", 19 | "$serialMonitor:RX", 20 | "", 21 | [] 22 | ], 23 | [ 24 | "esp:RX", 25 | "$serialMonitor:TX", 26 | "", 27 | [] 28 | ] 29 | ], 30 | "serialMonitor": { 31 | "display": "terminal", 32 | "convertEol": true 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /std_net_examples/ping/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_net_examples/ping/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_net_examples/ping/src/main.rs: -------------------------------------------------------------------------------- 1 | /* 2 | For a detailed explanation of this code check out the associated blog post: 3 | https://apollolabsblog.hashnode.dev/esp32-embedded-rust-ping 4 | 5 | GitHub Repo containing source code and other examples: 6 | https://github.com/apollolabsdev 7 | 8 | For notifications on similar examples and more, subscribe to newsletter here: 9 | https://www.theembeddedrustacean.com/subscribe 10 | */ 11 | 12 | use esp_idf_hal::peripherals::Peripherals; 13 | use esp_idf_svc::eventloop::EspSystemEventLoop; 14 | use esp_idf_svc::ipv4::Ipv4Addr; 15 | use esp_idf_svc::nvs::EspDefaultNvsPartition; 16 | use esp_idf_svc::ping::{Configuration as PingConfiguration, EspPing}; 17 | use esp_idf_svc::wifi::{AuthMethod, BlockingWifi, ClientConfiguration, Configuration, EspWifi}; 18 | use std::str::FromStr; 19 | 20 | fn main() -> anyhow::Result<()> { 21 | // Take Peripherals 22 | let peripherals = Peripherals::take().unwrap(); 23 | let sysloop = EspSystemEventLoop::take()?; 24 | let nvs = EspDefaultNvsPartition::take()?; 25 | 26 | let mut wifi = BlockingWifi::wrap( 27 | EspWifi::new(peripherals.modem, sysloop.clone(), Some(nvs))?, 28 | sysloop, 29 | )?; 30 | 31 | wifi.set_configuration(&Configuration::Client(ClientConfiguration { 32 | ssid: "Wokwi-GUEST".try_into().unwrap(), 33 | bssid: None, 34 | auth_method: AuthMethod::None, 35 | password: "".try_into().unwrap(), 36 | channel: None, 37 | }))?; 38 | 39 | println!("Connecting to WiFi"); 40 | 41 | // Start Wifi 42 | wifi.start()?; 43 | 44 | // Connect Wifi 45 | wifi.connect()?; 46 | 47 | // Wait until the network interface is up 48 | wifi.wait_netif_up()?; 49 | 50 | // This line is for Wokwi only so that the console output is formatted correctly 51 | println!("\x1b[20h"); 52 | 53 | println!("Wifi Connected"); 54 | 55 | println!("Pinging Google DNS (8.8.8.8)"); 56 | 57 | let mut ping = EspPing::new(0_u32); 58 | 59 | let ping_res = ping.ping( 60 | Ipv4Addr::from_str("8.8.8.8").unwrap(), 61 | &PingConfiguration::default(), 62 | ); 63 | 64 | match ping_res { 65 | Ok(summary) => println!( 66 | "Transmitted: {}, Recieved: {} Time: {:?}", 67 | summary.transmitted, summary.received, summary.time 68 | ), 69 | Err(e) => println!("{:?}", e), 70 | } 71 | 72 | loop {} 73 | } 74 | -------------------------------------------------------------------------------- /std_net_examples/ping/wokwi.toml: -------------------------------------------------------------------------------- 1 | [wokwi] 2 | version = 1 3 | gdbServerPort = 3333 4 | elf = "target/riscv32imc-esp-espidf/debug/ping" 5 | firmware = "target/riscv32imc-esp-espidf/debug/ping" 6 | -------------------------------------------------------------------------------- /std_net_examples/wifi/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = ["-C", "default-linker-libraries"] 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | # Note: these variables are not used when using pio builder (`cargo build --features pio`) 15 | ESP_IDF_VERSION = "release/v4.4" 16 | 17 | -------------------------------------------------------------------------------- /std_net_examples/wifi/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_net_examples/wifi/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "gpio" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev <104051566+apollolabsdev@users.noreply.github.com>"] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.66" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | 18 | default = ["std", "hal", "esp-idf-sys/native"] 19 | 20 | 21 | pio = ["esp-idf-sys/pio"] 22 | all = ["std", "nightly", "experimental", "embassy"] 23 | hal = ["esp-idf-hal", "embedded-svc", "esp-idf-svc"] 24 | std = [ 25 | "alloc", 26 | "esp-idf-sys/std", 27 | "esp-idf-sys/binstart", 28 | "embedded-svc?/std", 29 | "esp-idf-hal?/std", 30 | "esp-idf-svc?/std", 31 | ] 32 | alloc = ["embedded-svc?/alloc", "esp-idf-hal?/alloc", "esp-idf-svc?/alloc"] 33 | nightly = [ 34 | "embedded-svc?/nightly", 35 | "esp-idf-svc?/nightly", 36 | ] # Future: "esp-idf-hal?/nightly" 37 | experimental = ["embedded-svc?/experimental", "esp-idf-svc?/experimental"] 38 | embassy = [ 39 | "esp-idf-hal?/embassy-sync", 40 | "esp-idf-hal?/critical-section", 41 | "esp-idf-hal?/edge-executor", 42 | "esp-idf-svc?/embassy-time-driver", 43 | "esp-idf-svc?/embassy-time-isr-queue", 44 | ] 45 | 46 | [dependencies] 47 | log = { version = "0.4.17", default-features = false } 48 | esp-idf-sys = { version = "0.33", default-features = false } 49 | esp-idf-hal = { version = "0.41", optional = true, default-features = false } 50 | esp-idf-svc = { version = "0.46", optional = true, default-features = false } 51 | embedded-svc = { version = "0.25", optional = true, default-features = false } 52 | anyhow = "=1.0.75" 53 | 54 | [build-dependencies] 55 | embuild = "0.31.2" 56 | -------------------------------------------------------------------------------- /std_net_examples/wifi/build.rs: -------------------------------------------------------------------------------- 1 | // Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641 2 | fn main() -> Result<(), Box> { 3 | embuild::build::CfgArgs::output_propagated("ESP_IDF")?; 4 | embuild::build::LinkArgs::output_propagated("ESP_IDF")?; 5 | Ok(()) 6 | } 7 | -------------------------------------------------------------------------------- /std_net_examples/wifi/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_net_examples/wifi/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=7000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_net_examples/wifi/src/main.rs: -------------------------------------------------------------------------------- 1 | /* 2 | For a detailed explanation of this code check out the associated blog post: 3 | https://apollolabsblog.hashnode.dev/edge-iot-with-rust-on-esp-connecting-wifi 4 | 5 | GitHub Repo containing source code and other examples: 6 | https://github.com/apollolabsdev 7 | 8 | For notifications on similar examples and more, subscribe to newsletter here: 9 | https://www.theembeddedrustacean.com/subscribe 10 | */ 11 | 12 | use anyhow::{self, Error}; 13 | use embedded_svc::wifi::{AuthMethod, ClientConfiguration, Configuration}; 14 | use esp_idf_hal::peripherals::Peripherals; 15 | use esp_idf_svc::eventloop::EspSystemEventLoop; 16 | use esp_idf_svc::nvs::EspDefaultNvsPartition; 17 | use esp_idf_svc::wifi::EspWifi; 18 | 19 | fn main() -> anyhow::Result<()> { 20 | esp_idf_sys::link_patches(); 21 | 22 | // Configure Wifi 23 | let peripherals = Peripherals::take().unwrap(); 24 | let sysloop = EspSystemEventLoop::take()?; 25 | let nvs = EspDefaultNvsPartition::take()?; 26 | 27 | let mut wifi = EspWifi::new(peripherals.modem, sysloop, Some(nvs))?; 28 | 29 | wifi.set_configuration(&Configuration::Client(ClientConfiguration { 30 | ssid: "Wokwi-GUEST".into(), 31 | password: "".into(), 32 | auth_method: AuthMethod::None, 33 | ..Default::default() 34 | }))?; 35 | 36 | // Start Wifi 37 | wifi.start()?; 38 | 39 | // Connect Wifi 40 | wifi.connect()?; 41 | 42 | // Confirm Wifi Connection 43 | while !wifi.is_connected().unwrap() { 44 | let config = wifi.get_configuration().unwrap(); 45 | println!("Waiting for station {:?}", config); 46 | } 47 | 48 | println!("Connected"); 49 | 50 | Ok(()) 51 | } 52 | -------------------------------------------------------------------------------- /std_net_examples/wifi_revisit/.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv32imc-esp-espidf" 3 | 4 | [target.riscv32imc-esp-espidf] 5 | linker = "ldproxy" 6 | # runner = "espflash --monitor" # Select this runner for espflash v1.x.x 7 | runner = "espflash flash --monitor" # Select this runner for espflash v2.x.x 8 | rustflags = [ "--cfg", "espidf_time64"] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110 9 | 10 | [unstable] 11 | build-std = ["std", "panic_abort"] 12 | 13 | [env] 14 | MCU="esp32c3" 15 | # Note: this variable is not used by the pio builder (`cargo build --features pio`) 16 | ESP_IDF_VERSION = "v5.1.2" 17 | ESP_IDF_TOOLS_INSTALL_DIR = "global" -------------------------------------------------------------------------------- /std_net_examples/wifi_revisit/.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | /.embuild 3 | /target 4 | /Cargo.lock 5 | -------------------------------------------------------------------------------- /std_net_examples/wifi_revisit/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "wifi-revisit" 3 | version = "0.1.0" 4 | authors = ["apollolabsdev "] 5 | edition = "2021" 6 | resolver = "2" 7 | rust-version = "1.71" 8 | 9 | [profile.release] 10 | opt-level = "s" 11 | 12 | [profile.dev] 13 | debug = true # Symbols are nice and they don't increase the size on Flash 14 | opt-level = "z" 15 | 16 | [features] 17 | default = ["std", "embassy", "esp-idf-svc/native"] 18 | 19 | pio = ["esp-idf-svc/pio"] 20 | std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"] 21 | alloc = ["esp-idf-svc/alloc"] 22 | nightly = ["esp-idf-svc/nightly"] 23 | experimental = ["esp-idf-svc/experimental"] 24 | embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"] 25 | 26 | [dependencies] 27 | log = { version = "0.4", default-features = false } 28 | esp-idf-svc = { version = "0.48", default-features = false } 29 | esp-idf-hal = { version = "0.43", default-features = false } 30 | menu = "0.4.0" 31 | anyhow = "=1.0.80" 32 | heapless ="0.8.0" 33 | 34 | [build-dependencies] 35 | embuild = "0.31.3" 36 | -------------------------------------------------------------------------------- /std_net_examples/wifi_revisit/build.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | embuild::espidf::sysenv::output(); 3 | } 4 | -------------------------------------------------------------------------------- /std_net_examples/wifi_revisit/diagram.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 1, 3 | "editor": "wokwi", 4 | "author": "apollolabsdev ", 5 | "parts": [ 6 | { 7 | "type": "board-esp32-c3-devkitm-1", 8 | "id": "esp", 9 | "top": 0.59, 10 | "left": 0.67, 11 | "attrs": { 12 | "flashSize": "16" 13 | } 14 | } 15 | ], 16 | "connections": [ 17 | [ 18 | "esp:TX", 19 | "$serialMonitor:RX", 20 | "", 21 | [] 22 | ], 23 | [ 24 | "esp:RX", 25 | "$serialMonitor:TX", 26 | "", 27 | [] 28 | ] 29 | ], 30 | "serialMonitor": { 31 | "display": "terminal", 32 | "convertEol": true 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /std_net_examples/wifi_revisit/rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | components = ["rust-src"] 4 | -------------------------------------------------------------------------------- /std_net_examples/wifi_revisit/sdkconfig.defaults: -------------------------------------------------------------------------------- 1 | # Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) 2 | CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000 3 | 4 | # Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default). 5 | # This allows to use 1 ms granuality for thread sleeps (10 ms by default). 6 | #CONFIG_FREERTOS_HZ=1000 7 | 8 | # Workaround for https://github.com/espressif/esp-idf/issues/7631 9 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n 10 | #CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n 11 | -------------------------------------------------------------------------------- /std_net_examples/wifi_revisit/wokwi.toml: -------------------------------------------------------------------------------- 1 | [wokwi] 2 | version = 1 3 | gdbServerPort = 3333 4 | elf = "target/riscv32imc-esp-espidf/debug/wifi-revisit" 5 | firmware = "target/riscv32imc-esp-espidf/debug/wifi-revisit" 6 | --------------------------------------------------------------------------------