#[macro_use]
pub mod register_generation;
pub(crate) mod generic_ap;
pub(crate) mod memory_ap;
use crate::architecture::arm::dp::DebugPortError;
use crate::probe::DebugProbeError;
pub use generic_ap::{ApClass, ApType, GenericAp, IDR};
pub use memory_ap::{
AddressIncrement, BaseaddrFormat, DataSize, MemoryAp, BASE, BASE2, CFG, CSW, DRW, TAR, TAR2,
};
use super::{
communication_interface::RegisterParseError, ApAddress, ArmError, DapAccess, DpAddress,
Register,
};
#[derive(Debug, thiserror::Error)]
pub enum AccessPortError {
#[error("Failed to read register {name} at address 0x{address:08x}")]
RegisterRead {
address: u8,
name: &'static str,
#[source]
source: Box<dyn std::error::Error + Send + Sync>,
},
#[error("Failed to write register {name} at address 0x{address:08x}")]
RegisterWrite {
address: u8,
name: &'static str,
#[source]
source: Box<dyn std::error::Error + Send + Sync>,
},
#[error("Error while communicating with debug port")]
DebugPort(#[from] DebugPortError),
#[error("Failed to flush batched writes")]
Flush(#[from] DebugProbeError),
#[error("Error parsing a register")]
RegisterParse(#[from] RegisterParseError),
}
impl AccessPortError {
pub fn register_read_error<R: Register, E: std::error::Error + Send + Sync + 'static>(
source: E,
) -> Self {
AccessPortError::RegisterRead {
address: R::ADDRESS,
name: R::NAME,
source: Box::new(source),
}
}
pub fn register_write_error<R: Register, E: std::error::Error + Send + Sync + 'static>(
source: E,
) -> Self {
AccessPortError::RegisterWrite {
address: R::ADDRESS,
name: R::NAME,
source: Box::new(source),
}
}
}
pub trait ApRegister<PORT: AccessPort>: Register + Sized {}
pub trait AccessPort: Clone {
fn ap_address(&self) -> ApAddress;
}
pub trait ApAccess {
fn read_ap_register<PORT, R>(&mut self, port: PORT) -> Result<R, ArmError>
where
PORT: AccessPort,
R: ApRegister<PORT>;
fn read_ap_register_repeated<PORT, R>(
&mut self,
port: impl Into<PORT> + Clone,
register: R,
values: &mut [u32],
) -> Result<(), ArmError>
where
PORT: AccessPort,
R: ApRegister<PORT>;
fn write_ap_register<PORT, R>(
&mut self,
port: impl Into<PORT>,
register: R,
) -> Result<(), ArmError>
where
PORT: AccessPort,
R: ApRegister<PORT>;
fn write_ap_register_repeated<PORT, R>(
&mut self,
port: impl Into<PORT> + Clone,
register: R,
values: &[u32],
) -> Result<(), ArmError>
where
PORT: AccessPort,
R: ApRegister<PORT>;
}
impl<T: DapAccess> ApAccess for T {
#[tracing::instrument(skip(self, port), fields(ap = port.ap_address().ap, register = R::NAME, value))]
fn read_ap_register<PORT, R>(&mut self, port: PORT) -> Result<R, ArmError>
where
PORT: AccessPort,
R: ApRegister<PORT>,
{
let raw_value = self.read_raw_ap_register(port.ap_address(), R::ADDRESS)?;
tracing::Span::current().record("value", raw_value);
tracing::debug!("Register read succesful");
Ok(raw_value.try_into()?)
}
fn write_ap_register<PORT, R>(
&mut self,
port: impl Into<PORT>,
register: R,
) -> Result<(), ArmError>
where
PORT: AccessPort,
R: ApRegister<PORT>,
{
tracing::debug!("Writing register {}, value={:x?}", R::NAME, register);
self.write_raw_ap_register(port.into().ap_address(), R::ADDRESS, register.into())
}
fn write_ap_register_repeated<PORT, R>(
&mut self,
port: impl Into<PORT>,
_register: R,
values: &[u32],
) -> Result<(), ArmError>
where
PORT: AccessPort,
R: ApRegister<PORT>,
{
tracing::debug!(
"Writing register {}, block with len={} words",
R::NAME,
values.len(),
);
self.write_raw_ap_register_repeated(port.into().ap_address(), R::ADDRESS, values)
}
fn read_ap_register_repeated<PORT, R>(
&mut self,
port: impl Into<PORT>,
_register: R,
values: &mut [u32],
) -> Result<(), ArmError>
where
PORT: AccessPort,
R: ApRegister<PORT>,
{
tracing::debug!(
"Reading register {}, block with len={} words",
R::NAME,
values.len(),
);
self.read_raw_ap_register_repeated(port.into().ap_address(), R::ADDRESS, values)
}
}
pub fn access_port_is_valid<AP>(debug_port: &mut AP, access_port: GenericAp) -> bool
where
AP: ApAccess,
{
let idr_result: Result<IDR, _> = debug_port.read_ap_register(access_port);
match idr_result {
Ok(idr) => u32::from(idr) != 0,
Err(e) => {
tracing::debug!(
"Error reading IDR register from AP {}: {}",
access_port.ap_address().ap,
e
);
false
}
}
}
#[tracing::instrument(skip(debug_port))]
pub(crate) fn valid_access_ports<AP>(debug_port: &mut AP, dp: DpAddress) -> Vec<GenericAp>
where
AP: ApAccess,
{
(0..=255)
.map(|ap| GenericAp::new(ApAddress { dp, ap }))
.take_while(|port| access_port_is_valid(debug_port, *port))
.collect::<Vec<GenericAp>>()
}
pub fn get_ap_by_idr<AP, P>(debug_port: &mut AP, dp: DpAddress, f: P) -> Option<GenericAp>
where
AP: ApAccess,
P: Fn(IDR) -> bool,
{
(0..=255)
.map(|ap| GenericAp::new(ApAddress { dp, ap }))
.find(|ap| {
if let Ok(idr) = debug_port.read_ap_register(*ap) {
f(idr)
} else {
false
}
})
}