Skip to content
Snippets Groups Projects
Commit 21b69784 authored by Per Lindgren's avatar Per Lindgren
Browse files

probe.rs connection, mem read, step, run, halt works

parent 75303861
Branches
No related tags found
No related merge requests found
......@@ -7,3 +7,10 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "ktest"
path = "src/lib.rs"
[[bin]]
name = "ktest"
path = "src/main.rs"
\ No newline at end of file
use std::fs::File;
use std::io::prelude::*; // provide io traits
use std::io::{Error, ErrorKind};
#[derive(Debug)]
pub struct KTEST {
pub version: i32,
pub args: Vec<String>,
pub objects: Vec<(String, Vec<u8>)>,
}
pub fn read_ktest(file_name: &str) -> std::io::Result<KTEST> {
let mut file: File = File::open(file_name)?;
let mut hdr = [0u8; 5];
file.read_exact(&mut hdr)?;
if &hdr != b"KTEST" {
return Err(Error::new(ErrorKind::Other, "not a KTEST file"));
}
let version = read_i32(&mut file)?;
println!("version : {}", version);
if version > 3 {
return Err(Error::new(ErrorKind::Other, "non support KTEST version"));
}
let num_args = read_i32(&mut file)?;
// info regarding the KTEST file
let mut args = vec![];
for _ in 0..num_args {
let arg = read_sized(&mut file)?;
let str = String::from_utf8(arg).unwrap();
args.push(str);
}
// metadata not used here
let _sym_argvs = read_i32(&mut file)?;
let _sym_argv_len = read_i32(&mut file)?;
// read the objects
let num_objects = read_i32(&mut file)?;
let mut objects = vec![];
for _ in 0..num_objects {
let name = read_string(&mut file)?;
let data = read_sized(&mut file)?;
objects.push((name, data))
}
Ok(KTEST {
version,
args,
objects,
})
}
fn read_i32(file: &mut File) -> std::io::Result<i32> {
let mut str = [0u8; 4];
file.read_exact(&mut str)?;
Ok(i32::from_be_bytes(str)) // big endian
}
fn read_string(file: &mut File) -> std::io::Result<String> {
let str = read_sized(file)?;
Ok(String::from_utf8(str).unwrap())
}
fn read_sized(file: &mut File) -> std::io::Result<Vec<u8>> {
let size = read_i32(file)?;
let mut buf = vec![0u8; size as usize];
file.read_exact(&mut buf)?;
Ok(buf)
}
use std::fs::File;
use std::io::prelude::*; // provide io traits
use std::io::{Error, ErrorKind};
use ktest::{read_ktest, KTEST};
fn main() -> std::io::Result<()> {
let ktest = read_ktest("test000001.ktest");
println!("{:?}", ktest);
Ok(())
}
#[derive(Debug)]
struct KTEST {
version: i32,
args: Vec<String>,
objects: Vec<(String, Vec<u8>)>,
}
fn read_ktest(file_name: &str) -> std::io::Result<KTEST> {
let mut file: File = File::open(file_name)?;
let mut hdr = [0u8; 5];
file.read_exact(&mut hdr)?;
if &hdr != b"KTEST" {
return Err(Error::new(ErrorKind::Other, "not a KTEST file"));
}
let version = read_i32(&mut file)?;
println!("version : {}", version);
if version > 3 {
return Err(Error::new(ErrorKind::Other, "non support KTEST version"));
}
let num_args = read_i32(&mut file)?;
// info regarding the KTEST file
let mut args = vec![];
for _ in 0..num_args {
let arg = read_sized(&mut file)?;
let str = String::from_utf8(arg).unwrap();
args.push(str);
}
// metadata not used here
let _sym_argvs = read_i32(&mut file)?;
let _sym_argv_len = read_i32(&mut file)?;
// read the objects
let num_objects = read_i32(&mut file)?;
let mut objects = vec![];
for _ in 0..num_objects {
let name = read_string(&mut file)?;
let data = read_sized(&mut file)?;
objects.push((name, data))
}
Ok(KTEST {
version,
args,
objects,
})
}
fn read_i32(file: &mut File) -> std::io::Result<i32> {
let mut str = [0u8; 4];
file.read_exact(&mut str)?;
Ok(i32::from_be_bytes(str)) // big endian
}
fn read_string(file: &mut File) -> std::io::Result<String> {
let str = read_sized(file)?;
Ok(String::from_utf8(str).unwrap())
}
fn read_sized(file: &mut File) -> std::io::Result<Vec<u8>> {
let size = read_i32(file)?;
let mut buf = vec![0u8; size as usize];
file.read_exact(&mut buf)?;
Ok(buf)
}
......@@ -8,4 +8,4 @@ edition = "2018"
[dependencies]
probe-rs = { path = "../../probe-rs/probe-rs", version = "0.3.0" }
ktest = { path = "../probe-rs/probe-rs", version = "0.3.0" }
\ No newline at end of file
ktest = { path = "../ktest", version = "0.1.0" }
\ No newline at end of file
use ktest::{read_ktest, KTEST};
use probe_rs::{
config::registry::{Registry, SelectionStrategy},
coresight::access_ports::AccessPortError,
flash::download::FileDownloadError,
probe::{stlink, DebugProbe, DebugProbeError, DebugProbeType, MasterProbe, WireProtocol},
session::Session,
target::info::{self, ChipInfo},
};
fn main() {
println!("Hello, world!");
let mut probe = open_probe();
println!("probe connected");
let strategy = SelectionStrategy::ChipInfo(ChipInfo::read_from_rom_table(&mut probe).unwrap());
println!("strategy {:?}", strategy);
let registry = Registry::from_builtin_families();
let target = registry.get_target(strategy).unwrap();
println!("target {:?}", target);
let mut session = Session::new(target, probe);
// session.probe.target_reset().unwrap();
let cpu_info = session
.target
.core
.reset_and_halt(&mut session.probe)
.unwrap();
println!("Core stopped at address 0x{:08x}", cpu_info.pc);
let mut data = [0u8; 4];
session
.target
.core
.read_block8(&mut session.probe, 0x0000_0000, &mut data)
.unwrap();
println!("stack {:?}, 0x{:08x}", data, u32::from_le_bytes(data));
let mut data = [0u8; 4];
session
.target
.core
.read_block8(&mut session.probe, 0x0000_0004, &mut data)
.unwrap();
println!("reset {:?}, 0x{:08x}", data, u32::from_le_bytes(data));
let cpu_info = session.target.core.step(&mut session.probe).unwrap();
println!("Core stopped at address 0x{:08x}", cpu_info.pc);
println!("run");
session.target.core.run(&mut session.probe).unwrap();
session
.target
.core
.wait_for_core_halted(&mut session.probe)
.unwrap();
let cpu_info = session.target.core.halt(&mut session.probe).unwrap();
println!("Core stopped at address 0x{:08x}", cpu_info.pc);
}
fn open_probe() -> MasterProbe {
let mut devs = stlink::tools::list_stlink_devices();
// just pick the first one
let device = devs.get(0).unwrap();
println!("device {:?}", device);
let mut link = stlink::STLink::new_from_probe_info(&device).unwrap();
link.attach(Some(WireProtocol::Swd)).unwrap();
MasterProbe::from_specific_probe(link)
}
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment