### rtt_init! Basic Syntax Example Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/initialization.md This example demonstrates the basic syntax for initializing RTT with customizable up and down channels, including buffer sizes, modes, names, and linker sections. ```rust let channels = rtt_init! { up: { 0: { size: 1024, mode: NoBlockSkip, name: "Terminal", section: ".segger_term_buf" } 1: { size: 512, name: "Debug" } 2: { } // Pre-allocated channel without buffer } down: { 0: { size: 64, name: "Input" } } section_cb: ".segger_rtt" }; ``` -------------------------------- ### Basic Usage Example Source: https://github.com/probe-rs/rtt-target/blob/master/panic-rtt-target/README.md Initialize RTT with `rtt_init_default!()` and then trigger a panic. Ensure RTT is initialized before panicking. ```rust #![no_std] use panic_rtt_target as _; use rtt_target::rtt_init_default; fn main() -> ! { // you can use `rtt_init_print` or you can call `set_print_channel` after initialization. rtt_init_default!(); panic!("Something has gone terribly wrong"); } ``` -------------------------------- ### Basic Panic Handler Example Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/panic-handler.md This example demonstrates the basic usage of `panic-rtt-target`. It initializes RTT printing and triggers a panic that will be logged over RTT. ```rust #![no_std] #![no_main] use panic_rtt_target as _; use rtt_target::{rtt_init_print, rprintln}; #[no_mangle] fn main() -> ! { rtt_init_print!(); rprintln!("Starting application"); let value = 5; if value != 10 { panic!("Value mismatch!"); // Will be logged over RTT } loop {} } ``` -------------------------------- ### Example: Reading data from DownChannel Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/down-channel.md Demonstrates a basic loop for continuously reading data from the DownChannel and processing it. Suitable for text-based input. ```rust let mut input = channels.down.0; let mut buf = [0u8; 256]; loop { let count = input.read(&mut buf); if count > 0 { // Process received data for byte in &buf[..count] { println!("Received: {:?}", *byte as char); } } } ``` -------------------------------- ### Log Output Format Example Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/logging-integration.md Log messages are formatted as LEVEL [target] message. This example shows typical output for different log levels. ```text INFO [myapp] Starting application WARN [myapp::system] Low battery ERROR [myapp::system] Hardware failure ``` -------------------------------- ### Example Usage of rtt_init_default! Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/initialization.md Demonstrates how to use the `rtt_init_default!` macro to initialize RTT channels and write to the output channel. Ensure `writeln!` is available and error handling is considered. ```rust #[no_mangle] fn main() -> ! { let channels = rtt_init_default!(); let mut output = channels.up.0; writeln!(output, "Hello, world!").ok(); loop {} } ``` -------------------------------- ### rtt_init! Multiple Channels Example Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/initialization.md Sets up multiple RTT channels for both up and down directions, each with defined buffer sizes and names. This is useful for separating different types of output or input. ```rust let channels = rtt_init! { up: { 0: { size: 2048, name: "Main" } 1: { size: 512, name: "Debug" } 2: { size: 256, name: "Trace" } } down: { 0: { size: 64 } } }; writeln!(channels.up.0, "Main output").ok(); writeln!(channels.up.1, "Debug output").ok(); ``` -------------------------------- ### rtt_init! Custom Linker Sections Example Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/initialization.md Demonstrates how to specify custom linker sections for both the RTT control block and individual channel buffers. This allows for precise memory placement. ```rust let channels = rtt_init! { up: { 0: { size: 1024, name: "Terminal", section: ".segger_term_buf" } } section_cb: ".segger_rtt" }; ``` -------------------------------- ### uWriter Usage Example Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/types.md Demonstrates how to obtain and use a uWriter instance for formatted output with the `uwriteln!` macro. Obtain the writer via `UpChannel::u()` and ensure it's dropped after each write operation. ```rust let mut output = channels.up.0; let mut writer = output.u(); uwriteln!(writer, "Value: {}", 42).ok(); ``` -------------------------------- ### rtt_init_log! macro Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/logging-integration.md Initializes RTT, sets up the print channel, and configures the log logger in one call. This macro simplifies the setup process by combining multiple initialization steps. ```APIDOC ## rtt_init_log! macro ### Description Initializes RTT, sets up the print channel, and configures the log logger in one call. ### Variants 1. **Default (Trace level, NoBlockSkip, 1024 bytes):** ```rust rtt_init_log!(); ``` 2. **Custom log level:** ```rust rtt_init_log!(log::LevelFilter::Info); ``` 3. **Custom level and mode:** ```rust rtt_init_log!(log::LevelFilter::Warn, ChannelMode::BlockIfFull); ``` 4. **Full customization (level, mode, size):** ```rust rtt_init_log!(log::LevelFilter::Debug, ChannelMode::BlockIfFull, 4096); ``` ### Parameters - **$level**: A `log::LevelFilter` path (default: `Trace`) - **$mode**: A `ChannelMode` path (default: `NoBlockSkip`) - **$size**: Buffer size in bytes (default: 1024) ### Behavior - Calls `rtt_init_print!` internally. - Calls `init_logger_with_level()` with the specified level. - All log output goes to the print channel (up channel 0). ### Example ```rust fn main() { rtt_init_log!(); log::info!("Starting"); log::debug!("Details: {}", value); log::error!("Something failed"); } fn main() { use log::LevelFilter; use rtt_target::ChannelMode::BlockIfFull; rtt_init_log!(LevelFilter::Warn, BlockIfFull, 2048); // Only warnings and errors are logged } ``` ``` -------------------------------- ### rtt_init! Single Channel Pair Example Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/initialization.md Initializes a single up and down channel pair with specified buffer sizes, a blocking mode for the up channel, and names for both. ```rust let channels = rtt_init! { up: { 0: { size: 1024, mode: ChannelMode::BlockIfFull, name: "Terminal" } } down: { 0: { size: 16, name: "Input" } } }; let mut output = channels.up.0; let mut input = channels.down.0; ``` -------------------------------- ### Initialize RTT with Defmt and Trigger Panic Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/panic-handler.md This example demonstrates how to initialize RTT with defmt support and trigger a panic. The panic message will be logged as a defmt::error!. ```rust use panic_rtt_target as _; use rtt_target::rtt_init_defmt; #[no_mangle] fn main() -> ! { rtt_init_defmt!(); panic!("Critical error"); // Outputs via defmt::error!() automatically } ``` -------------------------------- ### Simultaneous Log and Defmt Usage Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/logging-integration.md This Rust example demonstrates how to initialize and use both the 'log' and 'defmt' frameworks concurrently. It shows logging messages using both frameworks after initialization. ```rust fn main() { rtt_init_log!(log::LevelFilter::Info, ChannelMode::BlockIfFull, 2048); rtt_init_defmt!(ChannelMode::BlockIfFull, 1024); log::info!("Using log"); defmt::info!("Using defmt"); } ``` -------------------------------- ### rtt_init! FFI Pre-allocation Example Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/initialization.md Shows how to pre-allocate an RTT channel without a buffer, which is useful for Foreign Function Interface (FFI) integration where an external C function might initialize the buffer. ```rust let channels = rtt_init! { up: { 0: { size: 1024 } 1: { } // Pre-allocated for C code to initialize } }; ``` -------------------------------- ### Log and Defmt Crate Integration Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/INDEX.md Guides for integrating RTT Target with the `log` and `defmt` crates. Covers initialization functions, macros, output formats, and considerations for using both frameworks together. ```APIDOC ## Logging and Defmt Integration ### Log Integration: - **`init_logger()` function:** Initializes the RTT logger with default settings. - **`init_logger_with_level()` function:** Initializes the RTT logger with a specified log level. - **`rtt_init_log!` macro:** A macro to initialize RTT and the logger simultaneously. - **Logger output format:** Description of how logs are formatted for RTT transmission. - **Features and configuration:** Available features and configuration options for the logger integration. ### Defmt Integration: - **`set_defmt_channel()` function:** Configures the RTT channel for `defmt` output. - **`rtt_init_defmt!` macro:** Initializes RTT and sets up `defmt` logging. - **Defmt advantages:** Benefits of using `defmt` with RTT. - **Reentrancy handling:** How reentrancy is managed when using `defmt`. ### Combined Usage: - **Using both frameworks together:** Guidance on integrating both `log` and `defmt`. - **Critical section requirements:** Information on critical sections needed for safe integration. ``` -------------------------------- ### rtt_init_defmt! Macro Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/logging-integration.md Initializes RTT with a channel specifically for defmt output. This macro simplifies the setup process for using defmt with rtt-target. ```APIDOC ## Macro: rtt_init_defmt! ### Description Initializes RTT with a channel for defmt output. ### Variants 1. **Default (NoBlockSkip, 1024 bytes):** ```rust rtt_init_defmt!(); ``` 2. **Custom mode:** ```rust rtt_init_defmt!(ChannelMode::BlockIfFull); ``` 3. **Custom mode and size:** ```rust rtt_init_defmt!(ChannelMode::BlockIfFull, 4096); ``` ### Parameters - **$mode**: A `ChannelMode` path (default: `NoBlockSkip`) - **$size**: Buffer size in bytes (default: 1024) ### Behavior - Calls `rtt_init!` to create up channel 0 named "defmt". - Calls `set_defmt_channel()` with that channel. - Defmt uses critical sections for thread-safe access. ### Example ```rust fn main() { rtt_init_defmt!(); defmt::info!("Message: {}", value); defmt::debug!("Details"); defmt::error!("Error: {}", error_code); } ``` ``` -------------------------------- ### Example: Reading with retry logic Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/down-channel.md Shows how to handle cases where no data is immediately available by using a spin loop. This pattern is useful when polling for data. ```rust let mut input = channels.down.0; let mut buf = [0u8; 64]; loop { match input.read(&mut buf) { 0 => { // No data available, try again later core::hint::spin_loop(); } count => { // Process count bytes from buf for i in 0..count { process_byte(buf[i]); } } } } ``` -------------------------------- ### RTT Initialization Macros Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/INDEX.md Use these macros for initializing RTT. `rtt_init!` offers full control, while `rtt_init_default!`, `rtt_init_print!`, `rtt_init_log!`, and `rtt_init_defmt!` provide standard setups for printing, logging, or defmt. ```rust rtt_init! { up: {...} down: {...} } // Full control ``` ```rust rtt_init_default!() // Standard setup ``` ```rust rtt_init_print!() // Print output only ``` ```rust rtt_init_log!() // With logging ``` ```rust rtt_init_defmt!() // With defmt ``` -------------------------------- ### Panic Handler with Logging Framework Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/panic-handler.md This example shows how to integrate `panic-rtt-target` with the `log` crate. It initializes RTT logging and triggers a panic if a processing function returns an error. ```rust #![no_std] #![no_main] use panic_rtt_target as _; use rtt_target::rtt_init_log; use log::LevelFilter; #[no_mangle] fn main() -> ! { rtt_init_log!(LevelFilter::Info); log::info!("Application started"); if let Err(e) = process() { panic!("Processing failed: {}", e); } loop {} } fn process() -> Result<(), &'static str> { Err("Simulated error") } ``` -------------------------------- ### Panic Handler with Defmt Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/panic-handler.md This example demonstrates using `panic-rtt-target` with the `defmt` logging framework. It initializes defmt logging and triggers a panic during a simulated dangerous operation. ```rust #![no_std] #![no_main] use panic_rtt_target as _; use rtt_target::rtt_init_defmt; #[no_mangle] fn main() -> ! { rtt_init_defmt!(); defmt::info!("Starting"); let result = dangerous_operation(); defmt::info!("Result: {}", result); loop {} } fn dangerous_operation() -> u32 { panic!("Operation failed!"); } ``` -------------------------------- ### Configure RTT Channels with rtt_init! Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/configuration.md Use the `rtt_init!` macro to configure up and down channels for RTT communication. Parameters like size, mode, name, and section are optional, but channel numbers must be contiguous and start from 0. ```rust let channels = rtt_init! { up: { 0: { size: 1024, mode: NoBlockSkip, name: "Terminal", section: ".segger_buf" } 1: { size: 512 } 2: { } // Pre-allocated, no buffer } down: { 0: { size: 64, name: "Input" } } section_cb: ".segger_rtt" }; ``` -------------------------------- ### RTT Initialization Macros Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/INDEX.md Macros for initializing RTT (Real-Time Transfer) logging. Includes options for full customization or default setup, along with details on control blocks, memory layout, and debug variants. ```APIDOC ## RTT Initialization ### `rtt_init!` macro **Description:** Provides full customization for RTT initialization. ### `rtt_init_default!` macro **Description:** Sets up RTT with standard default configurations. ### Helper macros (internal) **Description:** Internal macros used for RTT initialization. ### RTT Control Block Details **Description:** Information about the RTT control block structure and its fields. ### Memory Layout **Description:** Details on how RTT data is laid out in memory. ### Debug Variants **Description:** Specific configurations or variants for debugging RTT. ### Example Configurations **Description:** Illustrative examples of RTT initialization configurations. ### Panics and Constraints **Description:** Information regarding potential panics and operational constraints during RTT initialization. ``` -------------------------------- ### rtt_init_default! Macro Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/initialization.md Initializes RTT with standard default channels for basic terminal I/O. Use this for minimal programs or examples requiring simple terminal interaction. ```rust #[macro_export] macro_rules! rtt_init_default { () => { $crate::rtt_init! { up: { 0: { size: 1024, name: "Terminal" } } down: { 0: { size: 16, name: "Terminal" } } }; }; } ``` -------------------------------- ### Simple Print-Based Debugging with rtt-target Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/usage-patterns.md Use for basic application logging with minimal setup. It utilizes a single up channel and handles critical sections automatically. ```rust #![no_std] #![no_main] use cortex_m_rt::entry; use rtt_target::{rtt_init_print, rprintln}; #[entry] fn main() -> ! { rtt_init_print!(); let mut counter = 0; loop { rprintln!("Counter: {}", counter); counter += 1; // Small delay for _ in 0..1000000 { core::hint::spin_loop(); } } } ``` -------------------------------- ### RTT Logging Functions Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/INDEX.md Functions for integrating with logging frameworks. `log::info!` and `defmt::println!` are for standard logging and defmt respectively (require setup). `init_logger_with_level` sets the log level, and `set_defmt_channel` configures defmt output. ```rust log::info!("Message") // Log crate (requires setup) ``` ```rust defmt::println!("Message") // Defmt (requires setup) ``` ```rust init_logger_with_level(LevelFilter::Info) // Set log level ``` ```rust set_defmt_channel(channel) // Set defmt output ``` -------------------------------- ### TerminalWriter Usage Example Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/types.md Shows how to obtain and use a TerminalWriter for writing to a specific virtual terminal. Obtain the writer via `TerminalChannel::write()` and use standard formatting macros like `writeln!`. The writer automatically updates the current terminal on drop. ```rust let mut terminal = output.into_terminal(); let mut writer = terminal.write(1); writeln!(writer, "To terminal 1").ok(); ``` -------------------------------- ### rtt_init! Macro Definition Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/initialization.md The `rtt_init!` macro handles the compile-time and runtime setup of the RTT control block and channel buffers. It must be called exactly once per program execution, typically early in the application entry point. ```rust #[macro_export] macro_rules! rtt_init { { $(up: { $($up:tt)* } )? $(down: { $($down:tt)* } )? $(section_cb: $section_cb:literal )? } => { ... } } ``` -------------------------------- ### Get Channel Mode Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/terminal-channel.md Gets the current blocking mode of the underlying RTT channel. ```rust pub fn mode(&self) -> ChannelMode ``` ```rust println!("Current mode: {:?}", terminal.mode()); ``` -------------------------------- ### Configuration Options Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/COMPLETION_SUMMARY.txt Documentation for channel and initialization configuration options. ```APIDOC ## Configuration Options ### Description Documentation for channel and initialization configuration options. ### Channel Parameters - Channel size - Channel mode - Channel name - Linker section (channel) - Control block section ### Initialization Parameters - Log level filter options - Feature flags (`log`, `defmt`, `log_racy_init`) ### Memory Layout - Memory layout and sizing details ### Examples (Code examples demonstrating how to configure these options would be provided here.) ``` -------------------------------- ### Initialization Macros Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/COMPLETION_SUMMARY.txt Documentation for RTT initialization macros, including `rtt_init!`, `rtt_init_default!`, `rtt_init_print!`, `rtt_init_log!`, and `rtt_init_defmt!`. ```APIDOC ## Initialization Macros ### Description Documentation for RTT initialization macros, including `rtt_init!`, `rtt_init_default!`, `rtt_init_print!`, `rtt_init_log!`, and `rtt_init_defmt!`. ### Macros - `rtt_init!`: Full customization. - `rtt_init_default!`: Standard setup. - `rtt_init_print!`: Print-only setup. - `rtt_init_log!`: With logging backend. - `rtt_init_defmt!`: With defmt backend. ### Parameters (Details for parameters of these macros would be listed here, e.g., `channel_mode`, `buffer_size`, `log_level`) ### Examples (Code examples demonstrating the usage of these initialization macros would be provided here.) ``` -------------------------------- ### Get UpChannel Mode Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/up-channel.md Retrieves the current blocking mode of the RTT channel. This indicates how the channel behaves when the buffer is full. ```rust let mode = output.mode(); match mode { ChannelMode::NoBlockSkip => println!("Skip mode"), _ => println!("Other mode"), } ``` -------------------------------- ### Initialize Panic Handler and Trigger Panic Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/panic-handler.md Initialize the RTT logging and trigger a panic. The panic-rtt-target crate automatically installs the panic handler when included as a dependency. ```rust #![no_std] #![no_main] use panic_rtt_target as _; // Install the panic handler use rtt_target::rtt_init_default; fn main() -> ! { rtt_init_default!(); panic!("Something failed!"); } ``` -------------------------------- ### Get Current RTT Channel Mode Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/types.md Shows how to retrieve the current operating mode of an RTT channel. This allows for conditional logic based on the channel's behavior. ```rust let mode = output.mode(); match mode { ChannelMode::NoBlockSkip => println!("Skip mode"), ChannelMode::NoBlockTrim => println!("Trim mode"), ChannelMode::BlockIfFull => println!("Blocking mode"), } ``` -------------------------------- ### Initialize Log Channel with rtt_init_log! Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/configuration.md Use `rtt_init_log!` to set up a channel for logging. This macro requires the `log` feature to be enabled and accepts parameters for log level, blocking mode, and buffer size. ```rust rtt_init_log!(level, mode, size); ``` ```rust rtt_init_log!(level, mode); ``` ```rust rtt_init_log!(level); ``` ```rust rtt_init_log!(); ``` -------------------------------- ### Configure and Use Virtual Terminals Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/configuration.md Shows how to create and write to virtual terminals. Terminals are identified by a number (0-15) and are created on first use. Output is written using a writer obtained from the terminal. ```rust let mut terminal = output.into_terminal(); let mut writer = terminal.write(0); // Virtual terminal 0 writeln!(writer, "Line 1").ok(); let mut writer = terminal.write(1); // Virtual terminal 1 writeln!(writer, "Line 2").ok(); ``` -------------------------------- ### Initialize RTT with a Print Channel Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/INDEX.md Use the `rtt_init_print!()` macro to set up RTT with a single print channel. Refer to the API reference for printing for more details. ```rust rtt_init_print!(); ``` -------------------------------- ### Initialize Print Channel with rtt_init_print! Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/configuration.md The `rtt_init_print!` macro initializes a channel for printing. Optional parameters include the blocking mode and buffer size. ```rust rtt_init_print!(mode, size); ``` ```rust rtt_init_print!(mode); ``` ```rust rtt_init_print!(); ``` -------------------------------- ### Basic RTT Printing with rtt-target Source: https://github.com/probe-rs/rtt-target/blob/master/README.md This snippet demonstrates the simplest way to initialize RTT printing and send messages using `rprintln!`. Ensure a platform-specific critical section is in place. ```rust use rtt_target::{rtt_init_print, rprintln}; fn main() { rtt_init_print!(); loop { rprintln!("Hello, world!"); } } ``` -------------------------------- ### Initialization Macros Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/INDEX.md Macros for initializing the RTT (Real-Time Transfer) target. These allow for different levels of control and output configurations. ```APIDOC ## Initialization Macros ### `rtt_init!` **Description**: Provides full control over RTT initialization, allowing specification of up and down buffer configurations. ### `rtt_init_default!` **Description**: Initializes RTT with default settings. ### `rtt_init_print!` **Description**: Initializes RTT specifically for print output. ### `rtt_init_log!` **Description**: Initializes RTT for use with logging functionalities. ### `rtt_init_defmt!` **Description**: Initializes RTT for use with the `defmt` logging framework. ``` -------------------------------- ### Initialize Defmt Channel with rtt_init_defmt! Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/configuration.md The `rtt_init_defmt!` macro initializes a channel for defmt logging. It requires the `defmt` feature to be enabled and allows configuration of blocking mode and buffer size. ```rust rtt_init_defmt!(mode, size); ``` ```rust rtt_init_defmt!(mode); ``` ```rust rtt_init_defmt!(); ``` -------------------------------- ### Initialize RTT Logger with Default Settings Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/logging-integration.md The `rtt_init_log!()` macro initializes RTT, sets up the print channel, and configures the log logger with default settings (Trace level, NoBlockSkip mode, 1024 bytes buffer). ```rust fn main() { rtt_init_log!(); log::info!("Starting"); log::debug!("Details: {}", value); log::error!("Something failed"); } ``` -------------------------------- ### Runtime Configuration Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/COMPLETION_SUMMARY.txt Information on configuring RTT Target at runtime. ```APIDOC ## Runtime Configuration Details the options available for configuring RTT Target dynamically at runtime, allowing adjustments to its behavior after the application has started. ``` -------------------------------- ### Initialize RTT with Defmt Channel (Custom Mode and Size) Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/logging-integration.md Initialize RTT for `defmt` with a custom `ChannelMode` and buffer size. ```rust rtt_init_defmt!(ChannelMode::BlockIfFull, 4096); ``` -------------------------------- ### rtt_init_print! Macro Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/printing.md Initializes the RTT peripheral with a single up channel and configures it as the default print channel. Supports custom modes and buffer sizes. ```APIDOC ## rtt_init_print! Macro Initializes RTT with a single up channel and sets it as the print channel. ### Variants: 1. **Default (NoBlockSkip mode, 1024 bytes):** ```rust rtt_init_print!(); ``` 2. **Custom mode:** ```rust rtt_init_print!(ChannelMode::BlockIfFull); ``` 3. **Custom mode and size:** ```rust rtt_init_print!(ChannelMode::BlockIfFull, 4096); ``` ### Parameters: - `$mode`: A path to a `ChannelMode` value (e.g., `NoBlockSkip`, `BlockIfFull`). Default: `NoBlockSkip`. - `$size`: Buffer size in bytes. Default: 1024. ### Returns: Nothing (the macro fully initializes RTT). ### Note: This macro calls `rtt_init!` internally and panics if called more than once. ### Example: ```rust fn main() { rtt_init_print!(); rprintln!("Hello, world!"); } fn main() { rtt_init_print!(ChannelMode::BlockIfFull, 2048); rprintln!("High-priority output"); } ``` ``` -------------------------------- ### Initialize RTT Logger with Custom Settings Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/logging-integration.md Use `rtt_init_log!` with custom parameters to specify log level, channel mode, and buffer size for RTT logging. ```rust fn main() { use log::LevelFilter; use rtt_target::ChannelMode::BlockIfFull; rtt_init_log!(LevelFilter::Warn, BlockIfFull, 2048); // Only warnings and errors are logged } ``` -------------------------------- ### Printing and Logging Functions Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/COMPLETION_SUMMARY.txt Documentation for printing and logging macros and functions, including `rprint!`, `rprintln!`, `rdbg!`, `set_print_channel()`, `with_terminal_channel()`, `init_logger()`, `init_logger_with_level()`, and `set_defmt_channel()`. ```APIDOC ## Printing and Logging Functions ### Description Documentation for printing and logging macros and functions, including `rprint!`, `rprintln!`, `rdbg!`, `set_print_channel()`, `with_terminal_channel()`, `init_logger()`, `init_logger_with_level()`, and `set_defmt_channel()`. ### Functions/Macros - `rprint!`: Print without newline. - `rprintln!`: Print with newline. - `rdbg!`: Debug print with location. - `set_print_channel()`: Manual channel setup. - `with_terminal_channel()`: Access print channel. - `init_logger()`: Log crate setup. - `init_logger_with_level()`: Log crate with level. - `set_defmt_channel()`: Defmt backend setup. ### Parameters (Details for parameters of these functions/macros would be listed here.) ### Examples (Code examples demonstrating the usage of these printing and logging functions would be provided here.) ``` -------------------------------- ### UpChannel Methods Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/COMPLETION_SUMMARY.txt Documentation for the UpChannel, which handles target-to-host communication. It includes 9 documented methods. ```APIDOC ## UpChannel Methods ### Description Documentation for the UpChannel, which handles target-to-host communication. It includes 9 documented methods. ### Methods - `read()` - `write()` - `peek()` - `flush()` - `is_empty()` - `is_full()` - `capacity()` - `len()` - `available_read()` ### Parameters (Details for each method's parameters would be listed here, e.g., for `read()`: `buf` (slice), `count` (usize)) ### Response (Details for each method's return types would be listed here, e.g., for `read()`: `Result`) ### Examples (Code examples demonstrating the usage of UpChannel methods would be provided here.) ``` -------------------------------- ### Create TerminalChannel from UpChannel Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/types.md Demonstrates how to create a TerminalChannel by converting an existing UpChannel. This is the primary method for initializing multi-terminal support. ```rust let terminal = output.into_terminal(); ``` -------------------------------- ### rtt_init_print! Macro Initialization Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/printing.md Initializes RTT with a single up channel and sets it as the print channel. Supports default settings, custom channel modes, and custom buffer sizes. Panics if called more than once. ```rust rtt_init_print!(); ``` ```rust rtt_init_print!(ChannelMode::BlockIfFull); ``` ```rust rtt_init_print!(ChannelMode::BlockIfFull, 4096); ``` ```rust fn main() { rtt_init_print!(); rprintln!("Hello, world!"); } fn main() { rtt_init_print!(ChannelMode::BlockIfFull, 2048); rprintln!("High-priority output"); } ``` -------------------------------- ### Implement `core::fmt::Write` for `UpChannel` Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/up-channel.md Allows using `write!` and `writeln!` macros with an `UpChannel` instance. Ensure external synchronization if used from multiple contexts simultaneously. ```rust impl fmt::Write for UpChannel ``` ```rust use core::fmt::Write; let mut output = channels.up.0; writeln!(output, "Value: {}", 42).ok(); ``` -------------------------------- ### Basic Print Output with RTT Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/OVERVIEW.md Initializes RTT printing and sends a simple string message. Use this for basic text output to the host. ```rust use rtt_target::rtt_init_print; use rtt_target::rprintln; fn main() { rtt_init_print!(); rprintln!("Hello from RTT!"); } ``` -------------------------------- ### Initialize RTT with Defmt Channel Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/logging-integration.md The `rtt_init_defmt!` macro simplifies RTT initialization for `defmt` output. It sets up channel 0 named 'defmt' and registers it. ```rust rtt_init_defmt!(); defmt::info!("Message: {}", value); defmt::debug!("Details"); defmt::error!("Error: {}", error_code); ``` -------------------------------- ### Initialize RTT Target with Defmt Configuration Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/configuration.md Configure the RTT target for defmt logging, specifying the channel mode and buffer size. ```rust rtt_init_defmt!(ChannelMode::BlockIfFull, 2048); ``` -------------------------------- ### Logging Integration Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/COMPLETION_SUMMARY.txt Details on integrating RTT Target with the `log` crate for structured logging. ```APIDOC ## Log Crate Integration RTT Target can be seamlessly integrated with the `log` crate, enabling structured and efficient logging from your embedded applications. ### Usage Configure RTT Target as a logging backend for the `log` crate to capture and transmit log messages via RTT. ``` -------------------------------- ### Defmt Compact Logging Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/COMPLETION_SUMMARY.txt Information on using Defmt for compact binary logging with RTT Target. ```APIDOC ## Defmt Compact Logging RTT Target supports Defmt, a highly efficient logging framework that uses compact binary encoding for log messages. This significantly reduces the overhead of logging. ### Features - **Compact Binary Format**: Minimizes data size for log messages. - **Efficient Transmission**: Ideal for bandwidth-constrained environments. ``` -------------------------------- ### Initialize RTT with Default Channels Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/configuration.md Initializes the RTT target with default channel configurations. This macro sets up both up and down channels with predefined sizes and names. ```rust rtt_init_default!(); // Equivalent to: // rtt_init! { // up: { 0: { size: 1024, name: "Terminal" } } // down: { 0: { size: 16, name: "Terminal" } } // } ``` -------------------------------- ### rtt_init! Macro Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/initialization.md Initializes RTT with fully customizable up and down channels. This macro must be called exactly once per program execution, typically early in the application entry point. ```APIDOC ## rtt_init! ### Description Initializes RTT with fully customizable up and down channels. This macro handles the compile-time and runtime setup of the RTT control block and channel buffers. ### Syntax ```rust let channels = rtt_init! { up: { ... } down: { ... } section_cb: ".segger_rtt" }; ``` ### Channel Definition Structure For each channel, you can specify: - `size`: Buffer size in bytes (required if channel is to be initialized). - `mode`: `ChannelMode` value (optional, default: `NoBlockSkip`). Options include `NoBlockSkip`, `NoBlockTrim`, `BlockIfFull`. - `name`: Channel name string (optional, default: no name). - `section`: Linker section for the buffer (optional, default: no section). ### Global Options - `section_cb`: Linker section for the RTT control block (optional, default: no section). ### Channel Numbering - Channel numbers must start at 0 and be contiguous. - Channels can be pre-allocated without buffers by omitting `size`. ### Return Type The macro generates a struct with up and down channels, accessible via fields like `channels.up.0`, `channels.down.0`, etc. ### Example: Single up/down channel pair ```rust let channels = rtt_init! { up: { 0: { size: 1024, mode: ChannelMode::BlockIfFull, name: "Terminal" } } down: { 0: { size: 16, name: "Input" } } }; let mut output = channels.up.0; let mut input = channels.down.0; ``` ### Example: Multiple channels ```rust let channels = rtt_init! { up: { 0: { size: 2048, name: "Main" } 1: { size: 512, name: "Debug" } 2: { size: 256, name: "Trace" } } down: { 0: { size: 64 } } }; writeln!(channels.up.0, "Main output").ok(); writeln!(channels.up.1, "Debug output").ok(); ``` ### Example: Custom linker sections ```rust let channels = rtt_init! { up: { 0: { size: 1024, name: "Terminal", section: ".segger_term_buf" } } section_cb: ".segger_rtt" }; ``` ### Example: FFI pre-allocation ```rust let channels = rtt_init! { up: { 0: { size: 1024 } 1: { } // Pre-allocated for C code to initialize } }; ``` ### Constraints - Must be called exactly once per program. - Channel numbers must start at 0 and be contiguous. - Panics if called multiple times. ### Threading - Uses a critical section for single initialization. - Safe to call from main or early initialization code only. ``` -------------------------------- ### RTT Target Core Exports Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/INDEX.md Provides essential macros and functions for initializing RTT and managing communication channels. ```APIDOC ## Macros - `rtt_init!`: Initializes RTT with custom configuration. - `rtt_init_default!`: Initializes RTT with default configuration. - `rprint!`: Prints formatted output to the RTT channel. - `rprintln!`: Prints formatted output with a newline to the RTT channel. - `rdbg!`: Debugging macro for printing values. - `debug_*` variants: Specific debug printing macros. ## Functions - `set_print_channel()`: Sets the default print channel for RTT. - `with_terminal_channel()`: Provides access to the terminal channel for writing. ## Types - `UpChannel`: Represents an RTT upload channel. - `DownChannel`: Represents an RTT download channel. - `TerminalChannel`: Represents a channel for terminal-like output. - `TerminalWriter`: A writer for terminal output. - `uWriter`: A generic writer for RTT channels. - `ChannelMode`: Enum defining channel blocking modes (NoBlockSkip, NoBlockTrim, BlockIfFull). ``` -------------------------------- ### Structured Logging with RTT Terminals Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/terminal-channel.md Demonstrates how to use different RTT terminals for structured logging. Each terminal is accessed via `terminal.write(TERMINAL_ID)`, and output is written using `writeln!`. Ensure the host-side RTT viewer supports virtual terminals. ```rust let mut terminal = output.into_terminal(); const TRACE_TERM: u8 = 0; const ERROR_TERM: u8 = 1; // Log traces { let mut w = terminal.write(TRACE_TERM); writeln!(w, "[TRACE] Function entered").ok(); } // Log errors { let mut w = terminal.write(ERROR_TERM); writeln!(w, "[ERROR] Device failure").ok(); } ``` -------------------------------- ### Initialization Variants Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/COMPLETION_SUMMARY.txt Overview of the multiple initialization variants available for RTT Target. ```APIDOC ## Multiple Initialization Variants RTT Target offers several ways to initialize the system, catering to different project requirements and constraints. ### Variants - **Macro-based Initialization**: Configure RTT Target using macros during compilation. - **Runtime Configuration**: Initialize and configure RTT Target at runtime. - **FFI Pre-allocation**: Utilize Foreign Function Interface (FFI) for pre-allocating buffer space. ``` -------------------------------- ### Interactive Console with RTT Host Input Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/usage-patterns.md Implement an interactive console by reading commands from the RTT viewer. Data is read from a down channel and processed, with responses written back to an up channel. ```rust use rtt_target::rtt_init; fn main() -> ! { let channels = rtt_init! { up: { 0: { size: 256 } } down: { 0: { size: 64 } } }; let mut input = channels.down.0; let mut output = channels.up.0; let mut buf = [0u8; 64]; loop { let count = input.read(&mut buf); if count > 0 { // Echo back and convert to uppercase for byte in &mut buf[..count] { byte.make_ascii_uppercase(); } let _ = output.write(&buf[..count]); } } } ``` -------------------------------- ### Cargo.toml Dependencies Source: https://github.com/probe-rs/rtt-target/blob/master/panic-rtt-target/README.md Add these dependencies to your Cargo.toml file to use the panic-rtt-target crate. ```toml [dependencies] rtt-target = "x.y.z" panic-rtt-target = "x.y.z" ``` -------------------------------- ### Initialize RTT with Defmt Channel (Custom Mode) Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/logging-integration.md Initialize RTT for `defmt` with a specific `ChannelMode`, such as `BlockIfFull`. ```rust rtt_init_defmt!(ChannelMode::BlockIfFull); ``` -------------------------------- ### Formatter Integration (Write, uWrite) Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/COMPLETION_SUMMARY.txt Details on integrating RTT Target with `Write` and `uWrite` formatters. ```APIDOC ## Formatter Integration (Write, uWrite) This section covers the integration of RTT Target with the `Write` and `uWrite` traits, enabling flexible and efficient formatting of output data. ``` -------------------------------- ### TerminalChannel Methods Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/COMPLETION_SUMMARY.txt Documentation for the TerminalChannel, which provides virtual terminal support (0-15). It includes 5 documented methods. ```APIDOC ## TerminalChannel Methods ### Description Documentation for the TerminalChannel, which provides virtual terminal support (0-15). It includes 5 documented methods. ### Methods - `new()` - `read()` - `write()` - `flush()` - `set_blocking_mode()` ### Parameters (Details for each method's parameters would be listed here, e.g., for `new()`: `channel_id` (u8), `buffer_size` (usize)) ### Response (Details for each method's return types would be listed here, e.g., for `new()`: `Result`) ### Examples (Code examples demonstrating the usage of TerminalChannel methods would be provided here.) ``` -------------------------------- ### Performance-Optimized Logging with NoBlockTrim Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/usage-patterns.md Maximize logging throughput by using NoBlockTrim mode. This allows for partial writes and prevents blocking, making it ideal for high-frequency data where occasional data loss is acceptable for performance gains. ```rust use rtt_target::{rtt_init, ChannelMode}; fn main() -> ! { let channels = rtt_init! { up: { 0: { size: 4096, mode: NoBlockTrim } } }; let mut output = channels.up.0; // Rapid logging for i in 0..10000 { let _ = write!(output, "{}", i); } // Some data may be lost if buffer overflows, but no blocking } ``` -------------------------------- ### rtt_init_default! Macro Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/initialization.md Initializes RTT with standard default channels. This macro sets up two channels: an up channel (for sending data from the target to the host) and a down channel (for sending data from the host to the target). It's designed for minimal programs and applications requiring basic terminal input/output. ```APIDOC ## rtt_init_default! Initializes RTT with standard default channels. **Default Configuration:** - Up channel 0: 1024-byte buffer named "Terminal", `NoBlockSkip` mode - Down channel 0: 16-byte buffer named "Terminal", `NoBlockSkip` mode **Returns:** Struct with `up: (UpChannel,)` and `down: (DownChannel,)` fields. **Example:** ```rust #[no_mangle] fn main() -> ! { let channels = rtt_init_default!(); let mut output = channels.up.0; writeln!(output, "Hello, world!").ok(); loop {} } ``` **Use Case:** Minimal programs, examples, and applications that only need basic terminal I/O. ``` -------------------------------- ### Enable Racy Logger Initialization in Cargo.toml Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/configuration.md Use the 'log_racy_init' feature for platforms lacking atomic pointer support, forcing the use of 'log::set_logger_racy()'. ```toml [dependencies] rtt-target = { version = "0.6", features = ["log", "log_racy_init"] } ``` -------------------------------- ### Usage Pattern: Reading binary data Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/down-channel.md Illustrates reading raw binary data from the DownChannel into a buffer. The number of bytes read determines the valid portion of the buffer. ```rust let mut input = channels.down.0; let mut buf = [0u8; 256]; let bytes_read = input.read(&mut buf); // buf[0..bytes_read] contains the received data ``` -------------------------------- ### Initialize RTT Target (Debug) Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/initialization.md Use `debug_rtt_init!` for RTT initialization that acts as a no-op in release builds. This is useful for enabling debug output only when needed. ```rust debug_rtt_init! { up: { 0: { size: 1024 } } }; rprintln!("Debug output only"); ``` -------------------------------- ### TerminalChannel::new Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/api-reference/terminal-channel.md Creates a new TerminalChannel instance, wrapping an existing UpChannel to enable virtual terminal functionality. This is typically accessed via the `into_terminal()` method. ```APIDOC ## TerminalChannel::new ### Description Creates a new `TerminalChannel` from an `UpChannel`. This is typically called via the `into_terminal()` method on `UpChannel` rather than directly. ### Method Associated Function ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **channel** (`UpChannel`) - Required - The up channel to wrap with virtual terminal support. ``` -------------------------------- ### FFI Pre-allocation Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/COMPLETION_SUMMARY.txt Details on using FFI for pre-allocating buffer space for RTT Target. ```APIDOC ## FFI Pre-allocation Explains how to use Foreign Function Interface (FFI) mechanisms to pre-allocate buffer space for RTT Target, allowing for more predictable memory usage and potentially improved performance. ``` -------------------------------- ### Configure RTT Channel Names Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/configuration.md Define names and buffer sizes for RTT channels. Names are stored as null-terminated C strings. ```rust up: { 0: { size: 1024, name: "Terminal" } 1: { size: 512, name: "Debug" } 2: { size: 256, name: "Trace" } } ``` -------------------------------- ### Structured Logging with RTT Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/OVERVIEW.md Initializes RTT logging with a specified log level filter. This enables structured logging output to the host, integrating with the `log` crate. ```rust use rtt_target::rtt_init_log; use log::LevelFilter; fn main() { rtt_init_log!(LevelFilter::Info); log::info!("Application started"); log::warn!("Low battery"); } ``` -------------------------------- ### Printing and Formatting Source: https://github.com/probe-rs/rtt-target/blob/master/_autodocs/COMPLETION_SUMMARY.txt Information on print macros and formatter integration for outputting data. ```APIDOC ## Print Macros and Formatting RTT Target provides print macros that support formatting, similar to standard C `printf` functions. It also integrates with `Write` and `uWrite` formatters for flexible output. ### Features - **Formatted Output**: Use familiar formatting specifiers for various data types. - **Formatter Integration**: Supports `Write` and `uWrite` traits for custom formatting logic. ```