This commit is contained in:
2024-03-27 22:08:56 +01:00
parent 78db7eb9aa
commit bcad1e9481
62 changed files with 326622 additions and 0 deletions

17
rust/.cargo/config.toml Normal file
View File

@@ -0,0 +1,17 @@
[build]
target = "riscv32imc-esp-espidf"
[target.riscv32imc-esp-espidf]
linker = "ldproxy"
# runner = "espflash --monitor" # Select this runner for espflash v1.x.x
runner = "espflash flash --baud 921600 --monitor" # Select this runner for espflash v2.x.x
rustflags = [ "--cfg", "espidf_time64"] # Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110
[unstable]
build-std = ["std", "panic_abort"]
[env]
MCU="esp32c3"
# Note: this variable is not used by the pio builder (`cargo build --features pio`)
ESP_IDF_VERSION = "v5.1.1"

4
rust/.gitignore vendored Normal file
View File

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

35
rust/Cargo.toml Normal file
View File

@@ -0,0 +1,35 @@
[package]
name = "rust"
version = "0.1.0"
authors = ["Empire <empirephoenix@yahoo.de>"]
edition = "2021"
resolver = "2"
rust-version = "1.71"
[profile.release]
opt-level = "s"
[profile.dev]
debug = true # Symbols are nice and they don't increase the size on Flash
opt-level = "z"
[features]
default = ["std", "embassy", "esp-idf-svc/native"]
pio = ["esp-idf-svc/pio"]
std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"]
alloc = ["esp-idf-svc/alloc"]
nightly = ["esp-idf-svc/nightly"]
experimental = ["esp-idf-svc/experimental"]
embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"]
[dependencies]
log = { version = "0.4", default-features = false }
esp-idf-svc = { version = "0.48", default-features = false }
embedded-svc = { version = "0.27.0", features = ["experimental"] }
esp-idf-hal = "0.43.0"
esp-idf-sys = { version = "0.34.0", features = ["binstart", "native"] }
esp_idf_build = "0.1.3"
[build-dependencies]
embuild = "0.31.3"

3
rust/build.rs Normal file
View File

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

3
rust/rust-toolchain.toml Normal file
View File

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

10
rust/sdkconfig.defaults Normal file
View File

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

74
rust/src/main.rs Normal file
View File

@@ -0,0 +1,74 @@
use esp_idf_hal::gpio::{AnyInputPin, Gpio10, Gpio6, Gpio5, InputOutput, Level, PinDriver, Pull};
use esp_idf_svc::hal::{peripheral::Peripheral, peripherals::Peripherals};
use esp_idf_sys::{esp, gpio_hold_dis, gpio_hold_en, vTaskDelay, EspError};
use esp_idf_hal::delay::Delay;
use std::time::Duration;
use esp_idf_svc::eventloop::EspSystemEventLoop;
use esp_idf_svc::ipv4::IpInfo;
use esp_idf_svc::mqtt::client::QoS::AtLeastOnce;
use esp_idf_svc::mqtt::client::QoS::ExactlyOnce;
use esp_idf_svc::mqtt::client::{EspMqttClient, LwtConfiguration, MqttClientConfiguration};
use esp_idf_svc::nvs::EspDefaultNvsPartition;
use esp_idf_svc::wifi::config::{ScanConfig, ScanType};
use esp_idf_svc::wifi::EspWifi;
use esp_idf_hal::adc::{attenuation, AdcChannelDriver, AdcDriver};
fn main() {
// It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_svc::sys::link_patches();
// Bind the log crate to the ESP Logging facilities
esp_idf_svc::log::EspLogger::initialize_default();
log::info!("Hello, world!");
let mut peripherals = Peripherals::take().unwrap();
let mut in1 = PinDriver::input_output(peripherals.pins.gpio7).unwrap();
let mut in2 = PinDriver::input_output(peripherals.pins.gpio6).unwrap();
let sys_loop = EspSystemEventLoop::take().unwrap();
let nvs = EspDefaultNvsPartition::take().unwrap();
let mut wifi_driver = EspWifi::new(peripherals.modem, sys_loop, Some(nvs)).unwrap();
wifi_driver.start().unwrap();
wifi_driver.start_scan(
&ScanConfig {
scan_type: ScanType::Passive(Duration::from_secs(1)),
show_hidden: false,
..Default::default()
},
true,
).unwrap();
let sr = wifi_driver.get_scan_result().unwrap();
for r in sr.iter() {
println!("Found wifi {}", r.ssid);
}
let adc_config = esp_idf_hal::adc::config::Config {
resolution: esp_idf_hal::adc::config::Resolution::Resolution12Bit,
calibration: true,
};
let mut tank_driver = AdcDriver::new(peripherals.adc2, &adc_config).unwrap();
let mut tank_channel: AdcChannelDriver<'_, { attenuation::DB_11 }, Gpio5> =
AdcChannelDriver::new(peripherals.pins.gpio5).unwrap();
loop {
let mut delay = Delay::new_default();
in1.set_low().unwrap();
in2.set_high().unwrap();
for i in 0..50 {
delay.delay_ms(100);
let value_up = tank_driver.read(&mut tank_channel).unwrap();
println!("up {}", value_up);
}
let value_down = tank_driver.read(&mut tank_channel).unwrap();
println!("down {}", value_down);
in1.set_low().unwrap();
in2.set_low().unwrap();
delay.delay_ms(5000);
}
}