pub struct IntelHex { /* private fields */ }Expand description
Main structure for handling Intel HEX format data
Implementations§
Source§impl IntelHex
impl IntelHex
Sourcepub fn new() -> IntelHex
pub fn new() -> IntelHex
Creates a new empty IntelHex instance
Initializes a new IntelHex object with:
- An empty memory cells map (no data bytes stored)
- No execution start address
§Returns
Returns a new empty IntelHex instance
§Examples
use intelhex::IntelHex;
let intel_hex = IntelHex::new();
// Memory cells map is empty
assert!(intel_hex.memory_cells().is_empty());
// No execution start address
assert!(intel_hex.execution_start_address().is_none());Sourcepub fn memory_cells(&self) -> &BTreeMap<u32, u8>
pub fn memory_cells(&self) -> &BTreeMap<u32, u8>
Returns a reference to the memory cells map
The memory cells are stored in a BTreeMap where:
- Keys (u32) are memory addresses
- Values (u8) are the data bytes stored at those addresses
- Addresses are automatically sorted in ascending order
§Returns
Returns a reference to the BTreeMap containing all memory cells data
§Examples
use intelhex::IntelHex;
let hex_text = ":10010000214601360121470136007EFE09D2190140\n:00000001FF";
let intel_hex = IntelHex::from_intel_hex_text(hex_text).unwrap();
// Access memory cells
let memory = intel_hex.memory_cells();
// Check if an address contains data
if let Some(&value) = memory.get(&0x100) {
println!("Value at 0x100: {:02X}", value);
}
// Iterate through all memory cells in address order
for (&addr, &value) in memory.iter() {
println!("Address: {:08X}, Value: {:02X}", addr, value);
}Sourcepub fn execution_start_address(&self) -> Option<&ExecutionStartAddress>
pub fn execution_start_address(&self) -> Option<&ExecutionStartAddress>
Returns a reference to the execution start address if it exists
The execution start address can be either a segment address (CS:IP) or a linear address (EIP), as defined in the Intel HEX format.
§Returns
Some(&ExecutionStartAddress)- A reference to the execution start address if it was specifiedNone- If no execution start address was specified
§Examples
use intelhex::IntelHex;
use intelhex::ExecutionStartAddress;
let hex_text = ":0400000300100020C9\n:00000001FF"; // Contains a start segment address
let intel_hex = IntelHex::from_intel_hex_text(hex_text).unwrap();
if let Some(start_addr) = intel_hex.execution_start_address() {
match start_addr {
ExecutionStartAddress::StartSegmentAddress { cs, ip } => {
println!("CS:{:04X}, IP:{:04X}", cs, ip)
},
ExecutionStartAddress::StartLinearAddress { eip } => {
println!("EIP:{:08X}", eip)
},
}
}Sourcepub fn from_reader<R>(reader: R) -> Result<IntelHex, IntelHexError>where
R: Read,
pub fn from_reader<R>(reader: R) -> Result<IntelHex, IntelHexError>where
R: Read,
Creates an IntelHex instance from a reader
§Arguments
reader- Any type that implements theReadtrait
§Returns
Ok(IntelHex)- A new IntelHex instance containing the parsed dataErr(IntelHexError)- If there was an error reading or parsing the data
§Examples
use std::fs::File;
use intelhex::IntelHex;
let file = File::open("tests/fixtures/firmware.hex").unwrap();
let intel_hex = IntelHex::from_reader(file).unwrap();Sourcepub fn from_intel_hex_text(hex_text: &str) -> Result<IntelHex, IntelHexError>
pub fn from_intel_hex_text(hex_text: &str) -> Result<IntelHex, IntelHexError>
Creates an IntelHex instance from Intel HEX format text
§Arguments
hex_text- A string slice containing Intel HEX formatted data
§Returns
Ok(IntelHex)- A new IntelHex instance containing the parsed dataErr(IntelHexError)- If the input string is not a valid Intel HEX format
§Examples
use intelhex::IntelHex;
let hex_text = ":10010000214601360121470136007EFE09D2190140\n:00000001FF";
let result = IntelHex::from_intel_hex_text(hex_text).unwrap();Sourcepub fn to_writer<W>(&self, writer: W, data_byte_count: u8) -> Result<(), Error>where
W: Write,
pub fn to_writer<W>(&self, writer: W, data_byte_count: u8) -> Result<(), Error>where
W: Write,
Writes Intel HEX format data to a writer
§Arguments
writer- Any type that implements theWritetraitdata_byte_count- Number of data bytes per Data Record (1-255). A value of 16 is recommended for general use.
§Returns
Ok(())- If the data was successfully writtenErr(std::io::Error)- If there was an error writing the data
§Examples
use intelhex::IntelHex;
use std::fs::File;
let intel_hex = IntelHex::new();
let mut file = File::create("output.hex").unwrap();
intel_hex.to_writer(&mut file, 16).unwrap(); // 16 data bytes per data recordSourcepub fn to_intel_hex_text(&self) -> Result<String, Error>
pub fn to_intel_hex_text(&self) -> Result<String, Error>
Converts the IntelHex object to Intel HEX format text
§Returns
Ok(String)- A string containing the Intel HEX formatted dataErr(std::io::Error)- If there was an error writing the data
Sourcepub fn memory_blocks(&self) -> Vec<MemoryDataBlock>
pub fn memory_blocks(&self) -> Vec<MemoryDataBlock>
Converts memory data into a collection of continuous data blocks
§Returns
Returns a vector of MemoryDataBlock, each containing:
- address: Starting address of the data block
- data: Vector of continuous data bytes
Sourcepub fn add_memory_block(
&mut self,
block: &MemoryDataBlock,
) -> Result<(), IntelHexError>
pub fn add_memory_block( &mut self, block: &MemoryDataBlock, ) -> Result<(), IntelHexError>
Adds data from a MemoryDataBlock to the memory cells
§Arguments
block- The MemoryDataBlock containing address and data to add
§Returns
Ok(())- If the data was successfully addedErr(IntelHexError)- If there was an error adding the data
§Examples
use intelhex::{IntelHex, MemoryDataBlock};
let mut intel_hex = IntelHex::new();
let block = MemoryDataBlock {
address: 0x1000,
data: vec![0x12, 0x34, 0x56],
};
// Add the memory block
intel_hex.add_memory_block(&block).unwrap();
// Verify the data was added
let memory = intel_hex.memory_cells();
assert_eq!(memory.get(&0x1000), Some(&0x12));
assert_eq!(memory.get(&0x1001), Some(&0x34));
assert_eq!(memory.get(&0x1002), Some(&0x56));