### Generated Main Entry Point (Blocking) Source: https://context7.com/esp-rs/esp-generate/llms.txt Generates a simple `no_std` application with basic hardware setup. It includes necessary crates for logging, timing, and backtraces. This example is suitable for applications that do not require asynchronous operations. ```rust # Generated src/bin/main.rs (without embassy) #![no_std] #![no_main] #![deny(clippy::mem_forget)] #![deny(clippy::large_stack_frames)] use esp_hal::{ clock::CpuClock, main, time::{Duration, Instant}, }; use log::info; // or defmt::info! if defmt selected use esp_backtrace as _; extern crate alloc; // if alloc option selected esp_bootloader_esp_idf::esp_app_desc!(); #[main] fn main() -> ! { esp_println::logger::init_logger_from_env(); let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max()); let _peripherals = esp_hal::init(config); // Heap allocator using reclaimed memory (if alloc selected) esp_alloc::heap_allocator!(#[esp_hal::ram(reclaimed)] size: 65536); loop { info!("Hello world!"); let delay_start = Instant::now(); while delay_start.elapsed() < Duration::from_millis(500) {} } } ``` -------------------------------- ### Generated Cargo.toml Example (Rust) Source: https://context7.com/esp-rs/esp-generate/llms.txt An example Cargo.toml file demonstrating dependencies and features for an ESP32-C6 project using embassy, wifi, and defmt. It includes configurations for the package, binary, and various crates like esp-hal, esp-rtos, and embassy-net. ```toml [package] name = "my-project" version = "0.1.0" edition = "2024" rust-version = "1.88" [[bin]] name = "my-project" path = "./src/bin/main.rs" [dependencies] esp-hal = { version = "~1.0", features = ["esp32c6", "unstable", "defmt"] } esp-rtos = { version = "0.2.0", features = [ "esp-radio", "esp-alloc", "embassy", "esp32c6", "defmt", ]} esp-bootloader-esp-idf = { version = "0.4.0", features = ["defmt", "esp32c6"] } defmt = "1.0.1" rtt-target = { version = "0.6.2", features = ["defmt"] } esp-alloc = { version = "0.9.0", features = ["defmt"] } embedded-io = { version = "0.7.1", features = ["defmt"] } embedded-io-async = { version = "0.7.0", features = ["defmt"] } embassy-net = { version = "0.7.1", features = ["tcp", "udp", "dhcpv4", "medium-ethernet", "defmt"] } smoltcp = { version = "0.12.0", default-features = false, features = [ "medium-ethernet", "multicast", "proto-dhcpv4", "proto-dns", "proto-ipv4", "socket-dns", "socket-raw", "socket-tcp", "socket-udp", "socket-icmp", "defmt" ] } esp-radio = { version = "0.17.0", features = [ "esp32c6", "wifi", "smoltcp", "esp-alloc", "unstable", "defmt" ] } embassy-executor = { version = "0.9.1", features = ["defmt"] } embassy-time = { version = "0.5.0", features = ["defmt"] } static_cell = "2.1.1" critical-section = "1.2.0" [profile.dev] opt-level = "s" [profile.release] codegen-units = 1 debug = 2 lto = 'fat' opt-level = 's' ``` -------------------------------- ### Generated Wi-Fi Application Source: https://context7.com/esp-rs/esp-generate/llms.txt Generates a project structure that includes Wi-Fi initialization and controller setup. This template is for applications that need to connect to a Wi-Fi network. It sets up the radio and Wi-Fi controller interfaces. ```rust // Generated src/bin/main.rs (with wifi option) #![no_std] #![no_main] use esp_hal::{clock::CpuClock, main, timer::timg::TimerGroup}; use log::info; use esp_backtrace as _; extern crate alloc; esp_bootloader_esp_idf::esp_app_desc!(); #[main] fn main() -> ! { esp_println::logger::init_logger_from_env(); let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max()); let peripherals = esp_hal::init(config); esp_alloc::heap_allocator!(#[esp_hal::ram(reclaimed)] size: 65536); let timg0 = TimerGroup::new(peripherals.TIMG0); let sw_interrupt = esp_hal::interrupt::software::SoftwareInterruptControl::new( peripherals.SW_INTERRUPT ); esp_rtos::start(timg0.timer0, sw_interrupt.software_interrupt0); let radio_init = esp_radio::init().expect("Failed to initialize Wi-Fi/BLE controller"); let (mut wifi_controller, interfaces) = esp_radio::wifi::new(&radio_init, peripherals.WIFI, Default::default()) .expect("Failed to initialize Wi-Fi controller"); // Use wifi_controller and interfaces to connect and communicate loop { info!("Wi-Fi initialized!"); // Your application logic here } } ``` -------------------------------- ### Generate Project with esp-generate CLI Source: https://github.com/esp-rs/esp-generate/blob/main/README.md Generates a new project using the esp-generate command-line interface. This example specifies the target chip, enables allocation and Wi-Fi options, and names the project 'your-project'. The `--headless` flag can be used to bypass the TUI. ```bash esp-generate --chip esp32 -o alloc -o wifi your-project ``` -------------------------------- ### Install esp-generate using Cargo Source: https://github.com/esp-rs/esp-generate/blob/main/README.md Installs the esp-generate tool globally using the Rust package manager, Cargo. Ensure you have Rust and Cargo installed. ```bash cargo install esp-generate --locked ``` -------------------------------- ### Bash Script for Tool Version Checking Source: https://context7.com/esp-rs/esp-generate/llms.txt A bash script demonstrating the tool version checks performed by esp-generate after project creation. It verifies the Rust toolchain version against MSRV and checks for required versions of `espflash`, `probe-rs`, and optionally `esp-config`. It also shows example output for successful checks and prompts for installation if tools are missing or outdated. ```bash # After generation, esp-generate checks: # - Rust toolchain version meets MSRV # - espflash >= 3.3.0 (for espflash-based projects) # - probe-rs >= 0.31.0 (for probe-rs-based projects) # - esp-config >= 0.5.0 (optional configuration tool) # Example output: Checking installed versions 🆗 Rust (nightly): 1.88.0 🆗 espflash: 3.3.0 🆗 probe-rs: 0.31.0 🆗 esp-config: 0.5.0 # If tools are missing or outdated, prompts for installation: 🛑 probe-rs is not installed or is below the required version. Do you want to run `cargo install probe-rs-tools --locked` now? [y/N] ``` -------------------------------- ### Rust API for Cargo.toml Parsing Source: https://context7.com/esp-rs/esp-generate/llms.txt Provides a Rust API for parsing and manipulating Cargo.toml files. It allows loading manifests, retrieving package version, minimum supported Rust version (MSRV), checking publication status, getting dependency versions, and iterating through dependency tables. It depends on the `toml_edit` crate. ```rust // Library API for Cargo.toml handling (esp_generate::cargo) use esp_metadata::Chip; pub struct CargoToml { pub manifest: toml_edit::DocumentMut, } impl CargoToml { // Load and parse a Cargo.toml string pub fn load(manifest: &str) -> Result; // Get the package version pub fn version(&self) -> &str; // Get the minimum supported Rust version pub fn msrv(&self) -> &str; // Check if the package is published to crates.io pub fn is_published(&self) -> bool; // Get a specific dependency's version pub fn dependency_version(&self, package_name: &str) -> String; // Visit all dependency tables (dependencies, dev-dependencies, build-dependencies) pub fn visit_dependencies( &self, handle: impl FnMut(&str, &'static str, &toml_edit::Table), ); } // Example usage: let toml = CargoToml::load(contents)?; let esp_hal_version = toml.dependency_version("esp-hal"); let msrv = toml.msrv(); // e.g., "1.88" ``` -------------------------------- ### Building and Flashing Generated Projects Source: https://context7.com/esp-rs/esp-generate/llms.txt Instructions on how to build and flash projects generated by esp-generate using standard Cargo commands. ```APIDOC ## Building and Flashing Generated Projects ### Description After generation, use standard Cargo commands with the configured runner. ### Build Command 1. Navigate to the generated project directory: ```bash cd my-project ``` 2. Build the project (release mode recommended): ```bash cargo build --release ``` ### Flashing Flashing instructions depend on the project configuration (e.g., using `espflash` or `probe-rs`). Refer to the specific tool's documentation for detailed flashing commands. ``` -------------------------------- ### Bash Commands for Building and Flashing Source: https://context7.com/esp-rs/esp-generate/llms.txt Provides standard bash commands for building and flashing projects generated by esp-generate. It includes navigating to the project directory, building the release version, and using the configured runner for flashing. ```bash # Navigate to generated project cd my-project # Build the project cargo build --release ``` -------------------------------- ### Programmatic Project Generation with esp-generate Library Source: https://context7.com/esp-rs/esp-generate/llms.txt Demonstrates how to use the esp-generate library in Rust for custom project generation workflows. It shows loading template configurations, flattening options, finding modules for a specific chip, creating and modifying an active configuration, and formatting requirement messages. ```rust use esp_generate::{ cargo::CargoToml, config::{ActiveConfiguration, flatten_options, find_option}, template::{GeneratorOption, GeneratorOptionItem, Template}, modules::{modules_for_chip, find_module}, append_list_as_sentence, }; use esp_metadata::Chip; // Load template configuration let template: Template = serde_yaml::from_str(template_yaml)?; // Get all available options for a chip let chip = Chip::Esp32c6; let flat_options = flatten_options(&template.options); let modules = modules_for_chip(chip); // Create active configuration let mut config = ActiveConfiguration { chip, selected: vec![], options: template.options.clone(), flat_options: flatten_options(&template.options), }; // Select options programmatically config.select("alloc"); config.select("embassy"); config.select("wifi"); // Check option availability if let Some((idx, opt)) = find_option("ble-trouble", &config.flat_options, chip) { if config.is_option_active(opt) { config.select("ble-trouble"); } } // Format requirement messages let missing = vec!["alloc", "unstable-hal"]; let message = append_list_as_sentence( "Option 'wifi' requires", "", &missing ); // Result: "Option 'wifi' requires `alloc` and `unstable-hal`." ``` -------------------------------- ### Generated Main Entry Point (Embassy Async) Source: https://context7.com/esp-rs/esp-generate/llms.txt Generates an Embassy-based application with asynchronous executor support. It sets up the necessary components for async operations, including the executor and timers. This is ideal for applications requiring concurrent tasks. ```rust // Generated src/bin/main.rs (with embassy option) #![no_std] #![no_main] #![deny(clippy::mem_forget)] #![deny(clippy::large_stack_frames)] use esp_hal::clock::CpuClock; use esp_hal::timer::timg::TimerGroup; use embassy_executor::Spawner; use embassy_time::{Duration, Timer}; use defmt::info; use esp_backtrace as _; extern crate alloc; esp_bootloader_esp_idf::esp_app_desc!(); #[esp_rtos::main] async fn main(spawner: Spawner) -> ! { rtt_target::rtt_init_defmt!(); // if probe-rs + defmt let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max()); let peripherals = esp_hal::init(config); esp_alloc::heap_allocator!(#[esp_hal::ram(reclaimed)] size: 65536); let timg0 = TimerGroup::new(peripherals.TIMG0); esp_rtos::start(timg0.timer0); info!("Embassy initialized!"); // TODO: Spawn async tasks let _ = spawner; loop { info!("Hello world!"); Timer::after(Duration::from_secs(1)).await; } } ``` -------------------------------- ### Module Selection API (Rust) Source: https://context7.com/esp-rs/esp-generate/llms.txt Demonstrates the Rust API for selecting ESP modules, which helps in managing reserved GPIO pins. It defines a `Module` struct with properties like name, chip, and reserved GPIOs, and provides functions to retrieve modules for a specific chip or find a module by name. ```rust // Library API for module management (esp_generate::modules) use esp_metadata::Chip; pub struct Module { pub name: &'static str, // e.g., "esp32s3-wroom-2" pub display_name: &'static str, // e.g., "ESP32-S3-WROOM-2 (octal flash/PSRAM)" pub chip: Chip, // Target chip pub reserved_gpios: &'static [u8], // GPIOs to avoid (e.g., [33, 34, 35, 36, 37]) pub octal_psram: bool, // Whether module uses octal PSRAM } // Available modules include: // ESP32: esp32-wroom-32e, esp32-wroom-32ue, esp32-wrover-e, esp32-mini-1 // ESP32-C3: esp32c3-wroom-02, esp32c3-wroom-02u, esp32c3-mini-1 // ESP32-C6: esp32c6-wroom-1, esp32c6-wroom-1u, esp32c6-mini-1 // ESP32-S3: esp32s3-wroom-1, esp32s3-wroom-1u, esp32s3-wroom-2, esp32s3-mini-1 // ESP32-S2: esp32s2-wroom, esp32s2-wrover, esp32s2-mini-1 // ESP32-C2: esp32c2-mini-1 // ESP32-H2: esp32h2-wroom-02, esp32h2-mini-1 // Get modules for a specific chip pub fn modules_for_chip(chip: Chip) -> Vec<&'static Module>; // Find a specific module by name pub fn find_module(name: &str) -> Option<&'static Module>; ``` -------------------------------- ### Rust API for Configuration Management Source: https://context7.com/esp-rs/esp-generate/llms.txt Provides a Rust API for managing configuration options, including dependencies, selection groups, and chip compatibility. It allows checking option status, selecting options, and retrieving relationship data for help text. Dependencies include `esp-metadata`. ```rust // Library API for configuration (esp_generate::config) use esp_metadata::Chip; pub struct ActiveConfiguration { pub chip: Chip, pub selected: Vec, // Indices of selected options pub options: Vec, pub flat_options: Vec, } impl ActiveConfiguration { // Check if an option is currently selected pub fn is_selected(&self, option: &str) -> bool; // Check if a selection group has any selected option pub fn is_group_selected(&self, group: &str) -> bool; // Select an option by name (handles mutual exclusion in groups) pub fn select(&mut self, option: &str); // Check if an option can be enabled (requirements met, not disabled) pub fn is_option_active(&self, option: &GeneratorOption) -> bool; // Check if an option can be disabled (not required by others) pub fn can_be_disabled(&self, option: &str) -> bool; // Get relationships for help text display pub fn collect_relationships(&self, option: &GeneratorOptionItem) -> Relationships; } pub struct Relationships<'a> { pub requires: Vec<&'a str>, // Options this one requires pub required_by: Vec<&'a str>, // Options that require this one pub disabled_by: Vec<&'a str>, // Options that disable this one } // Flatten nested options for easy iteration pub fn flatten_options(options: &[GeneratorOptionItem]) -> Vec; // Find an option by name, considering chip compatibility pub fn find_option<'c>( option: &str, options: &'c [GeneratorOption], chip: Chip, ) -> Option<(usize, &'c GeneratorOption)>; ``` -------------------------------- ### Build and Flash Projects with esp-generate Source: https://context7.com/esp-rs/esp-generate/llms.txt Commands to build, flash, and monitor embedded Rust projects generated with esp-generate. It covers both the default cargo run command and specific commands for espflash and probe-rs based projects. Assumes a configured .cargo/config.toml. ```bash cargo run --release espflash flash --monitor target/riscv32imac-unknown-none-elf/release/my-project probe-rs run --chip esp32c6 target/riscv32imac-unknown-none-elf/release/my-project ``` -------------------------------- ### Configuration Management API Source: https://context7.com/esp-rs/esp-generate/llms.txt Manages option dependencies and selection groups for project generation. Allows checking option status, selecting options, and retrieving relationship information. ```APIDOC ## Configuration Management API ### Description The configuration system manages option dependencies and selection groups. ### Methods - `is_selected(option: &str) -> bool`: Check if an option is currently selected. - `is_group_selected(group: &str) -> bool`: Check if a selection group has any selected option. - `select(option: &str)`: Select an option by name (handles mutual exclusion in groups). - `is_option_active(option: &GeneratorOption) -> bool`: Check if an option can be enabled (requirements met, not disabled). - `can_be_disabled(option: &str) -> bool`: Check if an option can be disabled (not required by others). - `collect_relationships(option: &GeneratorOptionItem) -> Relationships`: Get relationships for help text display. ### Helper Functions - `flatten_options(options: &[GeneratorOptionItem]) -> Vec`: Flatten nested options for easy iteration. - `find_option<'c>(option: &str, options: &'c [GeneratorOption], chip: Chip) -> Option<(usize, &'c GeneratorOption)>`: Find an option by name, considering chip compatibility. ### Data Structures - `ActiveConfiguration`: Represents the current state of selected configuration options. - `Relationships<'a>`: Holds information about option dependencies (requires, required_by, disabled_by). ``` -------------------------------- ### Generated Cargo.toml Configuration Source: https://context7.com/esp-rs/esp-generate/llms.txt The Cargo.toml file is automatically configured with the necessary dependencies based on the selected project options. This ensures that all required crates for the chosen features (like Embassy or Wi-Fi) are included. ```toml ``` -------------------------------- ### Tool Version Checking Source: https://context7.com/esp-rs/esp-generate/llms.txt Details the tool versions that the generator validates after project creation to ensure compatibility. ```APIDOC ## Tool Version Checking ### Description The generator validates required tool versions after project generation. ### Required Tools - Rust toolchain version (must meet MSRV) - `espflash` (>= 3.3.0 for espflash-based projects) - `probe-rs` (>= 0.31.0 for probe-rs-based projects) - `esp-config` (>= 0.5.0, optional) ### Example Output (Success) ```bash Checking installed versions 🆗 Rust (nightly): 1.88.0 🆗 espflash: 3.3.0 🆗 probe-rs: 0.31.0 🆗 esp-config: 0.5.0 ``` ### Example Output (Missing/Outdated Tool Prompt) ```bash 🛑 probe-rs is not installed or is below the required version. Do you want to run `cargo install probe-rs-tools --locked` now? [y/N] ``` ``` -------------------------------- ### Run Embedded Tests with esp-generate Source: https://context7.com/esp-rs/esp-generate/llms.txt Command to execute tests for an embedded Rust project generated by esp-generate, provided that the 'embedded-test' feature is enabled. This command builds and runs tests in release mode. ```bash cargo test --release ``` -------------------------------- ### Template Option Definitions Source: https://context7.com/esp-rs/esp-generate/llms.txt Describes the structure of YAML files used for defining generator options, including categories, individual options, requirements, and chip constraints. ```APIDOC ## Template Option Definitions ### Description Generator options are defined in YAML with requirements and chip constraints. ### Structure (`template.yaml`) - **`options`**: A list of configuration options. - **`!Category`**: Represents a group of related options. - `name`: Internal name of the category. - `display_name`: User-friendly name for the category. - `help`: Description of the category. - `options`: A list of options within this category. - `requires`: List of options that must be selected for this category to be available. - **`!Option`**: Represents an individual configuration option. - `name`: Internal name of the option. - `display_name`: User-friendly name for the option. - `help`: Description of the option. - `selection_group`: Name of the group this option belongs to (for mutual exclusion). - `requires`: List of options that must be selected for this option to be available. - `chips`: List of compatible chip targets for this option. ### Example Snippet ```yaml options: - !Category name: module display_name: Module/Board selection help: Select your ESP32 module to avoid reserved GPIOs options: - !Option name: PLACEHOLDER display_name: "" selection_group: module - !Option name: alloc display_name: Enable allocations via the esp-alloc crate help: esp-alloc comes with no stability guarantees - !Option name: wifi display_name: Enable Wi-Fi via the esp-radio crate requires: - alloc - unstable-hal chips: - esp32 - esp32c2 - esp32c3 - esp32c6 - esp32s2 - esp32s3 ``` ``` -------------------------------- ### YAML Structure for Template Options Source: https://context7.com/esp-rs/esp-generate/llms.txt Defines the structure for template options in YAML format, including categories, individual options, their display names, help text, selection groups, requirements, and chip constraints. This format is used to configure project generation. ```yaml # template.yaml structure options: - !Category name: module display_name: Module/Board selection help: Select your ESP32 module to avoid reserved GPIOs options: - !Option name: PLACEHOLDER # Dynamically populated at runtime display_name: "" selection_group: module - !Option name: alloc display_name: Enable allocations via the esp-alloc crate help: esp-alloc comes with no stability guarantees - !Option name: wifi display_name: Enable Wi-Fi via the esp-radio crate requires: - alloc - unstable-hal chips: - esp32 - esp32c2 - esp32c3 - esp32c6 - esp32s2 - esp32s3 - !Option name: ble-bleps display_name: Enable BLE via bleps selection_group: ble-lib # Mutually exclusive with ble-trouble requires: - alloc - unstable-hal - !Category name: flashing-probe-rs display_name: Flashing and debugging (probe-rs) requires: - probe-rs options: - !Option name: defmt display_name: Use defmt for logging selection_group: log-frontend ``` -------------------------------- ### Template Processing Directives (Rust) Source: https://context7.com/esp-rs/esp-generate/llms.txt Illustrates the use of conditional directives within template files for dynamic code generation in esp-generate. It covers conditional inclusion (`IF`/`ENDIF`), alternative conditions (`ELIF`/`ELSE`), file inclusion (`INCLUDEFILE`), file renaming (`INCLUDE_AS`), variable replacement (`REPLACE`), and comment stripping (`//+`). ```rust // Template directive syntax used in source files: // Conditional inclusion: //IF option("wifi") // This code is included only when wifi option is selected //ENDIF //IF option("defmt") use defmt::info; //ELIF option("log") use log::info; //ELSE // No logging //ENDIF // File-level directives: //INCLUDEFILE option("embassy") // Only include this file if embassy is selected //INCLUDE_AS src/bin/main.rs // Rename the output file // Variable replacement: //REPLACE project-name project-name // Replace placeholder with variable value name = "project-name" // Comment stripping (//+ prefix gets removed): //+use defmt::info; // becomes: use defmt::info; ``` -------------------------------- ### Cargo.toml Parsing API Source: https://context7.com/esp-rs/esp-generate/llms.txt Provides utilities for parsing and manipulating Cargo.toml manifests, including retrieving version information, MSRV, and dependency details. ```APIDOC ## Cargo.toml Parsing API ### Description The cargo module provides utilities for parsing and manipulating Cargo manifests. ### Methods - `load(manifest: &str) -> Result`: Load and parse a Cargo.toml string. - `version(&self) -> &str`: Get the package version. - `msrv(&self) -> &str`: Get the minimum supported Rust version. - `is_published(&self) -> bool`: Check if the package is published to crates.io. - `dependency_version(&self, package_name: &str) -> String`: Get a specific dependency's version. - `visit_dependencies(&self, handle: impl FnMut(&str, &'static str, &toml_edit::Table))`: Visit all dependency tables (dependencies, dev-dependencies, build-dependencies). ### Example Usage ```rust // Load and parse a Cargo.toml string let toml = CargoToml::load(contents)?; // Get the version of a specific dependency let esp_hal_version = toml.dependency_version("esp-hal"); // Get the minimum supported Rust version let msrv = toml.msrv(); // e.g., "1.88" ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.