Skip to content

Commit

Permalink
dp: peripheral: Add method to change the peripheral address
Browse files Browse the repository at this point in the history
For applications with dynamic peripheral addresses, add a method to
conveniently reset a peripheral to a new address without reconstructing
it explicitly.
  • Loading branch information
Rahix committed Nov 15, 2024
1 parent b1e23d4 commit 2fa00bc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/dp/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ impl<'a> ExtendedDiagnostics<'a> {
Self { buffer, length: 0 }
}

pub(crate) fn take_buffer(&mut self) -> &'a mut [u8] {
self.length = 0;
core::mem::take(&mut self.buffer)
}

pub(crate) fn fill(&mut self, buf: &[u8]) -> bool {
if self.buffer.len() == 0 {
// No buffer for ext. diagnostics so we ignore them entirely.
Expand Down
13 changes: 13 additions & 0 deletions src/dp/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,19 @@ impl<'a> Peripheral<'a> {
self
}

/// Completely reset this peripheral to a new address.
///
/// The process images are not changed by this operation. A new DP parameterization will take
/// place once the device responds at the new address.
pub fn reset_address(&mut self, new_address: crate::Address) {
let options = core::mem::take(&mut self.options);
let pi_i = core::mem::take(&mut self.pi_i);
let pi_q = core::mem::take(&mut self.pi_q);
let diag_buffer = self.ext_diag.take_buffer();

*self = Self::new(new_address, options, pi_i, pi_q).with_diag_buffer(diag_buffer);
}

/// Address of this peripheral.
#[inline(always)]
pub fn address(&self) -> u8 {
Expand Down

0 comments on commit 2fa00bc

Please sign in to comment.