From 777c72cbfb7bbb9b235cc7ec2da031cb93cc5bab Mon Sep 17 00:00:00 2001 From: Wyatt Marchand Date: Mon, 24 Aug 2026 19:30:05 -0700 Subject: [PATCH] Finally compiles --- .cargo/config.toml | 12 +++++++++ .gitignore | 4 +++ Cargo.toml | 19 +++++++++++++ build.rs | 3 +++ rust-toolchain.toml | 2 ++ sdkconfig.defaults | 16 +++++++++++ src/main.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 121 insertions(+) create mode 100644 .cargo/config.toml create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 build.rs create mode 100644 rust-toolchain.toml create mode 100644 sdkconfig.defaults create mode 100644 src/main.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..c7099c4 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,12 @@ +[build] +target = "xtensa-esp32s3-espidf" + +[target.xtensa-esp32s3-espidf] +linker = "ldproxy" + +[unstable] +build-std = ["std", "panic_abort"] + +[env] +MCU = "esp32s3" +ESP_IDF_VERSION = "v5.3.1" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..73a638b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/.vscode +/.embuild +/target +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..20276da --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "hello-eth" +version = "0.1.0" +edition = "2021" + +[[bin]] +name = "hello-eth" +harness = false + +[profile.release] +opt-level = "s" + +[dependencies] +esp-idf-svc = { version = "0.51", features = ["std"] } +anyhow = "1" +log = "0.4" + +[build-dependencies] +embuild = "0.33" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..112ec3f --- /dev/null +++ b/build.rs @@ -0,0 +1,3 @@ +fn main() { + embuild::espidf::sysenv::output(); +} diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..a2f5ab5 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "esp" diff --git a/sdkconfig.defaults b/sdkconfig.defaults new file mode 100644 index 0000000..a227535 --- /dev/null +++ b/sdkconfig.defaults @@ -0,0 +1,16 @@ +# Rust often needs a bit of an extra main task stack size compared to C (the default is 3K) +# You might have to increase this further if you allocate large stack variables in the main task +CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192 + +# Increase a bit these stack sizes as they are also a bit too small by default +CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=4096 +CONFIG_FREERTOS_IDLE_TASK_STACKSIZE=4096 + +# You might have to increase this further if you spawn your own Rust threads +# that allocate large stack variables; or better yet - use +# `std::thread::Builder::new().stack_size(XXX)` for spawning +CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT=4096 + +CONFIG_ETH_SPI_ETHERNET_W5500=y +CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000 +CONFIG_HTTPD_MAX_REQ_HDR_LEN=1024 diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..0140278 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,65 @@ +use std::thread; +use std::time::Duration; + +use esp_idf_svc::eth::{BlockingEth, EspEth, EthDriver, SpiEthChipset}; +use esp_idf_svc::eventloop::EspSystemEventLoop; +use esp_idf_svc::hal::peripherals::Peripherals; +use esp_idf_svc::hal::prelude::*; +use esp_idf_svc::hal::spi::{config::DriverConfig, SpiDriver}; +use esp_idf_svc::http::server::{Configuration as HttpServerConfig, EspHttpServer}; +use esp_idf_svc::http::Method; +use esp_idf_svc::io::Write; +use log::info; + +fn main() -> anyhow::Result<()> { + esp_idf_svc::sys::link_patches(); + esp_idf_svc::log::EspLogger::initialize_default(); + + let peripherals = Peripherals::take()?; + let sysloop = EspSystemEventLoop::take()?; + let pins = peripherals.pins; + + let spi_driver = SpiDriver::new( + peripherals.spi2, + pins.gpio13, // SCLK + pins.gpio11, // MOSI (sdo) + Some(pins.gpio12), // MISO (sdi) + &DriverConfig::new(), + )?; + + let eth_driver = EthDriver::new_spi( + spi_driver, + pins.gpio10, // INT + Some(pins.gpio14), // CS + Some(pins.gpio9), // RST + SpiEthChipset::W5500, + 20.MHz().into(), + Some(&[0x02, 0x00, 0x00, 0x12, 0x34, 0x56]), // locally-administered MAC + None, // phy addr — default + sysloop.clone(), + )?; + + let eth = EspEth::wrap(eth_driver)?; + let mut eth = BlockingEth::wrap(eth, sysloop)?; + + info!("Starting eth..."); + eth.start()?; + info!("Waiting for DHCP lease..."); + eth.wait_netif_up()?; + + let ip_info = eth.eth().netif().get_ip_info()?; + info!("Got IP: {:?}", ip_info); + + let mut server = EspHttpServer::new(&HttpServerConfig::default())?; + server.fn_handler("/", Method::Get, |req| { + let html = "

Hello World

"; + req.into_ok_response()?.write_all(html.as_bytes())?; + anyhow::Ok(()) + })?; + + info!("Server up. Browse to http://{}/", ip_info.ip); + + loop { + thread::sleep(Duration::from_secs(1)); + } +}