Initial commit

Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
This commit is contained in:
Thomas Klaehn
2025-02-08 08:48:24 +01:00
commit 18e9f86c36
28 changed files with 669 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
[build]
target = "riscv32imc-esp-espidf"
[target.riscv32imc-esp-espidf]
linker = "ldproxy"
runner = "espflash flash --monitor"
# Future - necessary for the experimental "native build" of esp-idf-sys with ESP32C3
# See also https://github.com/ivmarkov/embuild/issues/16
rustflags = ["--cfg", "espidf_time64"]
[unstable]
build-std = ["panic_abort", "std"]
[env]
# Enables the esp-idf-sys "native" build feature (`cargo build --features native`) to build against ESP-IDF (v5.3.2)
ESP_IDF_VERSION = { value = "tag:v5.3.2" }
# These configurations will pick up your custom "sdkconfig.release", "sdkconfig.debug" or "sdkconfig.defaults[.*]" files
# that you might put in the root of the project
# The easiest way to generate a full "sdkconfig[.release|debug]" configuration (as opposed to manually enabling only the necessary flags via "sdkconfig.defaults[.*]"
# is by running "cargo pio espidf menuconfig" (that is, if using the pio builder)
#ESP_IDF_SDKCONFIG = { value = "./sdkconfig.release", relative = true }
#ESP_IDF_SDKCONFIG = { value = "./sdkconfig.debug", relative = true }
ESP_IDF_SDKCONFIG_DEFAULTS = { value = "./sdkconfig.defaults", relative = true }
# ESP-IDF will be installed in ~/.espressif so it can be reused across the different examples.
# See also https://github.com/esp-rs/esp-idf-sys/blob/master/BUILD-OPTIONS.md#esp_idf_tools_install_dir-esp_idf_tools_install_dir
ESP_IDF_TOOLS_INSTALL_DIR = { value = "global" }

2
lib/rgb-led/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/target
Cargo.lock

15
lib/rgb-led/Cargo.toml Normal file
View File

@@ -0,0 +1,15 @@
[package]
name = "rgb-led"
version = "0.1.0"
edition = "2021"
authors = ["Sergio Gasquez <sergio.gasquez@gmail.com>"]
[dependencies]
anyhow = "=1.0.95"
esp-idf-svc = "=0.50.1"
esp-idf-hal ={ version = "=0.45.0", features = ["rmt-legacy"] }
log = "=0.4.22"
rgb = "0.8.29"
[build-dependencies]
embuild = "=0.33.0"

3
lib/rgb-led/build.rs Normal file
View File

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

View File

@@ -0,0 +1,27 @@
use anyhow::Result;
use esp_idf_svc::hal::{delay::FreeRtos, peripherals::Peripherals};
use log::info;
use rgb_led::WS2812RMT;
fn main() -> Result<()> {
esp_idf_svc::sys::link_patches();
esp_idf_svc::log::EspLogger::initialize_default();
let peripherals = Peripherals::take().unwrap();
// Onboard RGB LED pin
// Rust ESP Board gpio2, ESP32-C3-DevKitC-02 gpio8
let led = peripherals.pins.gpio2;
let channel = peripherals.rmt.channel0;
let mut ws2812 = WS2812RMT::new(led, channel)?;
loop {
info!("Red!");
ws2812.set_pixel(rgb::RGB8::new(255, 0, 0))?;
FreeRtos::delay_ms(1000);
info!("Green!");
ws2812.set_pixel(rgb::RGB8::new(0, 255, 0))?;
FreeRtos::delay_ms(1000);
info!("Blue!");
ws2812.set_pixel(rgb::RGB8::new(0, 0, 255))?;
FreeRtos::delay_ms(1000);
}
}

View File

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

48
lib/rgb-led/src/lib.rs Normal file
View File

@@ -0,0 +1,48 @@
use anyhow::Result;
use core::time::Duration;
use esp_idf_hal::{
gpio::OutputPin,
peripheral::Peripheral,
rmt::{config::TransmitConfig, FixedLengthSignal, PinState, Pulse, RmtChannel, TxRmtDriver},
};
pub use rgb::RGB8;
pub struct WS2812RMT<'a> {
tx_rtm_driver: TxRmtDriver<'a>,
}
impl<'d> WS2812RMT<'d> {
// Rust ESP Board gpio2, ESP32-C3-DevKitC-02 gpio8
pub fn new(
led: impl Peripheral<P = impl OutputPin> + 'd,
channel: impl Peripheral<P = impl RmtChannel> + 'd,
) -> Result<Self> {
let config = TransmitConfig::new().clock_divider(2);
let tx = TxRmtDriver::new(channel, led, &config)?;
Ok(Self { tx_rtm_driver: tx })
}
pub fn set_pixel(&mut self, rgb: RGB8) -> Result<()> {
let color: u32 = ((rgb.g as u32) << 16) | ((rgb.r as u32) << 8) | rgb.b as u32;
let ticks_hz = self.tx_rtm_driver.counter_clock()?;
let t0h = Pulse::new_with_duration(ticks_hz, PinState::High, &ns(350))?;
let t0l = Pulse::new_with_duration(ticks_hz, PinState::Low, &ns(800))?;
let t1h = Pulse::new_with_duration(ticks_hz, PinState::High, &ns(700))?;
let t1l = Pulse::new_with_duration(ticks_hz, PinState::Low, &ns(600))?;
let mut signal = FixedLengthSignal::<24>::new();
for i in (0..24).rev() {
let p = 2_u32.pow(i);
let bit = p & color != 0;
let (high_pulse, low_pulse) = if bit { (t1h, t1l) } else { (t0h, t0l) };
signal.set(23 - i as usize, &(high_pulse, low_pulse))?;
}
self.tx_rtm_driver.start_blocking(&signal)?;
Ok(())
}
}
fn ns(nanos: u64) -> Duration {
Duration::from_nanos(nanos)
}

View File

@@ -0,0 +1,28 @@
[build]
target = "riscv32imc-esp-espidf"
[target.riscv32imc-esp-espidf]
linker = "ldproxy"
runner = "espflash flash --monitor"
# Future - necessary for the experimental "native build" of esp-idf-sys with ESP32C3
# See also https://github.com/ivmarkov/embuild/issues/16
rustflags = ["--cfg", "espidf_time64"]
[unstable]
build-std = ["panic_abort", "std"]
[env]
# Enables the esp-idf-sys "native" build feature (`cargo build --features native`) to build against ESP-IDF (v5.3.2)
ESP_IDF_VERSION = { value = "tag:v5.3.2" }
# These configurations will pick up your custom "sdkconfig.release", "sdkconfig.debug" or "sdkconfig.defaults[.*]" files
# that you might put in the root of the project
# The easiest way to generate a full "sdkconfig[.release|debug]" configuration (as opposed to manually enabling only the necessary flags via "sdkconfig.defaults[.*]"
# is by running "cargo pio espidf menuconfig" (that is, if using the pio builder)
#ESP_IDF_SDKCONFIG = { value = "./sdkconfig.release", relative = true }
#ESP_IDF_SDKCONFIG = { value = "./sdkconfig.debug", relative = true }
ESP_IDF_SDKCONFIG_DEFAULTS = { value = "./sdkconfig.defaults", relative = true }
# ESP-IDF will be installed in ~/.espressif so it can be reused across the different examples.
# See also https://github.com/esp-rs/esp-idf-sys/blob/master/BUILD-OPTIONS.md#esp_idf_tools_install_dir-esp_idf_tools_install_dir
ESP_IDF_TOOLS_INSTALL_DIR = { value = "global" }

2
lib/wifi/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/target
Cargo.lock

16
lib/wifi/Cargo.toml Normal file
View File

@@ -0,0 +1,16 @@
[package]
name = "wifi"
version = "0.1.0"
edition = "2021"
authors = ["Sergio Gasquez <sergio.gasquez@gmail.com>"]
[dependencies]
anyhow = "=1.0.95"
esp-idf-svc = "=0.50.1"
log = "=0.4.22"
[build-dependencies]
embuild = "=0.33.0"
[dev-dependencies]
toml-cfg = "=0.1.3"

3
lib/wifi/build.rs Normal file
View File

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

View File

@@ -0,0 +1,3 @@
[wifi]
wifi_ssid = "FBI Surveillance Van"
wifi_psk = "hunter2"

40
lib/wifi/examples/wifi.rs Normal file
View File

@@ -0,0 +1,40 @@
use anyhow::{bail, Result};
use esp_idf_svc::{eventloop::EspSystemEventLoop, hal::prelude::Peripherals};
use wifi::wifi;
/// This configuration is picked up at compile time by `build.rs` from the
/// file `cfg.toml`.
#[toml_cfg::toml_config]
pub struct Config {
#[default("")]
wifi_ssid: &'static str,
#[default("")]
wifi_psk: &'static str,
}
fn main() -> Result<()> {
esp_idf_svc::sys::link_patches();
esp_idf_svc::log::EspLogger::initialize_default();
let peripherals = Peripherals::take().unwrap();
let sysloop = EspSystemEventLoop::take()?;
let app_config = CONFIG;
// Connect to the Wi-Fi network
let _wifi = match wifi(
app_config.wifi_ssid,
app_config.wifi_psk,
peripherals.modem,
sysloop,
) {
Ok(inner) => {
println!("Connected to Wi-Fi network!");
inner
}
Err(err) => {
// Red!
bail!("Could not connect to Wi-Fi network: {:?}", err)
}
};
Ok(())
}

View File

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

View File

@@ -0,0 +1,2 @@
CONFIG_ESP_MAIN_TASK_STACK_SIZE=20000
CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=4096

78
lib/wifi/src/lib.rs Normal file
View File

@@ -0,0 +1,78 @@
use anyhow::{bail, Result};
use esp_idf_svc::{
eventloop::EspSystemEventLoop,
hal::peripheral,
wifi::{AuthMethod, BlockingWifi, ClientConfiguration, Configuration, EspWifi},
};
use log::info;
pub fn wifi(
ssid: &str,
pass: &str,
modem: impl peripheral::Peripheral<P = esp_idf_svc::hal::modem::Modem> + 'static,
sysloop: EspSystemEventLoop,
) -> Result<Box<EspWifi<'static>>> {
let mut auth_method = AuthMethod::WPA2Personal;
if ssid.is_empty() {
bail!("Missing WiFi name")
}
if pass.is_empty() {
auth_method = AuthMethod::None;
info!("Wifi password is empty");
}
let mut esp_wifi = EspWifi::new(modem, sysloop.clone(), None)?;
let mut wifi = BlockingWifi::wrap(&mut esp_wifi, sysloop)?;
wifi.set_configuration(&Configuration::Client(ClientConfiguration::default()))?;
info!("Starting wifi...");
wifi.start()?;
info!("Scanning...");
let ap_infos = wifi.scan()?;
let ours = ap_infos.into_iter().find(|a| a.ssid == ssid);
let channel = if let Some(ours) = ours {
info!(
"Found configured access point {} on channel {}",
ssid, ours.channel
);
Some(ours.channel)
} else {
info!(
"Configured access point {} not found during scanning, will go with unknown channel",
ssid
);
None
};
wifi.set_configuration(&Configuration::Client(ClientConfiguration {
ssid: ssid
.try_into()
.expect("Could not parse the given SSID into WiFi config"),
password: pass
.try_into()
.expect("Could not parse the given password into WiFi config"),
channel,
auth_method,
..Default::default()
}))?;
info!("Connecting wifi...");
wifi.connect()?;
info!("Waiting for DHCP lease...");
wifi.wait_netif_up()?;
let ip_info = wifi.wifi().sta_netif().get_ip_info()?;
info!("Wifi DHCP info: {:?}", ip_info);
Ok(Box::new(esp_wifi))
}