Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve stlink error messages #82

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions probe-rs/src/probe/debug_probe.rs
Expand Up @@ -9,6 +9,7 @@ use log::debug;
use crate::memory::adi_v5_memory_interface::ADIMemoryInterface;
use crate::memory::MI;
use crate::probe::protocol::WireProtocol;
use crate::probe::stlink::constants::Status as StLinkStatus;
use std::error::Error;
use std::fmt;

Expand All @@ -33,6 +34,7 @@ pub enum DebugProbeError {
TargetPowerUpFailed,
Timeout,
AccessPortError(AccessPortError),
Custom(String),
}

impl Error for DebugProbeError {
Expand All @@ -57,6 +59,15 @@ impl From<AccessPortError> for DebugProbeError {
}
}

impl From<StLinkStatus> for DebugProbeError {
fn from(status: StLinkStatus) -> Self {
Self::Custom(format!(
"Unexpected STLink status {}: {:?}",
status as u8, status
))
}
}

#[derive(Debug, PartialEq)]
pub enum Port {
DebugPort,
Expand Down
10 changes: 10 additions & 0 deletions probe-rs/src/probe/stlink/constants.rs
@@ -1,3 +1,6 @@
use enum_primitive_derive::Primitive;
use num_traits::FromPrimitive;

pub mod commands {
// Common commands.
pub const GET_VERSION: u8 = 0xf1;
Expand Down Expand Up @@ -60,6 +63,7 @@ pub mod commands {
}

/// STLink status codes and messages.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Primitive)]
pub enum Status {
JtagOk = 0x80,
JtagUnknownError = 0x01,
Expand Down Expand Up @@ -92,6 +96,12 @@ pub enum Status {
JtagUnknownCmd = 0x42,
}

impl From<u8> for Status {
fn from(status_byte: u8) -> Status {
Status::from_u8(status_byte).unwrap_or(Status::JtagUnknownError)
}
}

/// Map from SWD frequency in Hertz to delay loop count.
pub enum SwdFrequencyToDelayCount {
Hz4600000 = 0,
Expand Down
37 changes: 22 additions & 15 deletions probe-rs/src/probe/stlink/mod.rs
Expand Up @@ -67,12 +67,12 @@ impl DebugProbe for STLink {
&mut buf,
TIMEOUT,
)?;
Self::check_status(&buf).and_then(|_| {
// After we checked the status with success,
// We store the current protocol.
self.protocol = protocol;
Ok(protocol)
})
Self::check_status(&buf)?;

// After we checked the status with success,
// We store the current protocol.
self.protocol = protocol;
Ok(protocol)
}

/// Leave debug mode.
Expand All @@ -93,7 +93,8 @@ impl DebugProbe for STLink {
&mut buf,
TIMEOUT,
)?;
Self::check_status(&buf)
Self::check_status(&buf)?;
Ok(())
}
}

Expand Down Expand Up @@ -326,7 +327,8 @@ impl STLink {
&mut buf,
TIMEOUT,
)?;
Self::check_status(&buf)
Self::check_status(&buf)?;
Ok(())
}

/// Sets the JTAG frequency.
Expand All @@ -345,7 +347,8 @@ impl STLink {
&mut buf,
TIMEOUT,
)?;
Self::check_status(&buf)
Self::check_status(&buf)?;
Ok(())
}

pub fn open_ap(&mut self, apsel: impl AccessPort) -> Result<(), DebugProbeError> {
Expand All @@ -364,7 +367,8 @@ impl STLink {
&mut buf,
TIMEOUT,
)?;
Self::check_status(&buf)
Self::check_status(&buf)?;
Ok(())
}
}

Expand All @@ -383,7 +387,8 @@ impl STLink {
&mut buf,
TIMEOUT,
)?;
Self::check_status(&buf)
Self::check_status(&buf)?;
Ok(())
}
}

Expand All @@ -402,18 +407,20 @@ impl STLink {
&mut buf,
TIMEOUT,
)?;
Self::check_status(&buf)
Self::check_status(&buf)?;
Ok(())
}

/// Validates the status given.
/// Returns an `Err(DebugProbeError::UnknownError)` if the status is not `Status::JtagOk`.
/// Returns Ok(()) otherwise.
/// This can be called on any status returned from the attached target.
fn check_status(status: &[u8]) -> Result<(), DebugProbeError> {
fn check_status(status: &[u8]) -> Result<(), Status> {
log::trace!("check_status({:?})", status);
if status[0] != Status::JtagOk as u8 {
let status = Status::from(status[0]);
if status != Status::JtagOk {
log::debug!("check_status failed: {:?}", status);
Err(DebugProbeError::UnknownError)
Err(status)
} else {
Ok(())
}
Expand Down