Merge pull request #4137 from cschuhen/stm32-can-data-len

Stm32 can data len
This commit is contained in:
Dario Nieuwenhuis 2025-04-27 22:58:53 +00:00 committed by GitHub
commit 1d2f0add77
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 21 additions and 16 deletions

View File

@ -299,9 +299,9 @@ impl Registers {
mb.tdtr().write(|w| w.set_dlc(frame.header().len() as u8));
mb.tdlr()
.write(|w| w.0 = u32::from_ne_bytes(unwrap!(frame.data()[0..4].try_into())));
.write(|w| w.0 = u32::from_ne_bytes(unwrap!(frame.raw_data()[0..4].try_into())));
mb.tdhr()
.write(|w| w.0 = u32::from_ne_bytes(unwrap!(frame.data()[4..8].try_into())));
.write(|w| w.0 = u32::from_ne_bytes(unwrap!(frame.raw_data()[4..8].try_into())));
let id: IdReg = frame.id().into();
mb.tir().write(|w| {
w.0 = id.0;

View File

@ -75,7 +75,7 @@ impl Registers {
let mailbox = self.tx_buffer_element(bufidx);
mailbox.reset();
put_tx_header(mailbox, header);
put_tx_data(mailbox, &buffer[..header.len() as usize]);
put_tx_data(mailbox, buffer);
// Set <idx as Mailbox> as ready to transmit
self.regs.txbar().modify(|w| w.set_ar(bufidx, true));
@ -190,7 +190,7 @@ impl Registers {
DataLength::Fdcan(len) => len,
DataLength::Classic(len) => len,
};
if len as usize > ClassicData::MAX_DATA_LEN {
if len as usize > 8 {
return None;
}

View File

@ -104,15 +104,13 @@ pub trait CanHeader: Sized {
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ClassicData {
pub(crate) bytes: [u8; Self::MAX_DATA_LEN],
pub(crate) bytes: [u8; 8],
}
impl ClassicData {
pub(crate) const MAX_DATA_LEN: usize = 8;
/// Creates a data payload from a raw byte slice.
///
/// Returns `None` if `data` is more than 64 bytes (which is the maximum) or
/// cannot be represented with an FDCAN DLC.
/// Returns `FrameCreateError` if `data` is more than 8 bytes (which is the maximum).
pub fn new(data: &[u8]) -> Result<Self, FrameCreateError> {
if data.len() > 8 {
return Err(FrameCreateError::InvalidDataLength);
@ -211,12 +209,17 @@ impl Frame {
/// Get reference to data
pub fn data(&self) -> &[u8] {
&self.data.raw()
&self.data.raw()[..self.can_header.len as usize]
}
/// Get reference to underlying 8-byte raw data buffer, some bytes on the tail might be undefined.
pub fn raw_data(&self) -> &[u8] {
self.data.raw()
}
/// Get mutable reference to data
pub fn data_mut(&mut self) -> &mut [u8] {
self.data.raw_mut()
&mut self.data.raw_mut()[..self.can_header.len as usize]
}
/// Get priority of frame
@ -260,7 +263,7 @@ impl embedded_can::Frame for Frame {
self.can_header.len as usize
}
fn data(&self) -> &[u8] {
&self.data.raw()
&self.data()
}
}
@ -405,12 +408,12 @@ impl FdFrame {
/// Get reference to data
pub fn data(&self) -> &[u8] {
&self.data.raw()
&self.data.raw()[..self.can_header.len as usize]
}
/// Get mutable reference to data
pub fn data_mut(&mut self) -> &mut [u8] {
self.data.raw_mut()
&mut self.data.raw_mut()[..self.can_header.len as usize]
}
}
@ -448,7 +451,7 @@ impl embedded_can::Frame for FdFrame {
self.can_header.len as usize
}
fn data(&self) -> &[u8] {
&self.data.raw()
&self.data()
}
}

View File

@ -422,11 +422,13 @@ impl<'d, T: Instance> OpAmp<'d, T> {
T::regs().csr().modify(|w| match pair {
OpAmpDifferentialPair::P => {
defmt::info!("p calibration. offset: {}", mid);
#[cfg(feature = "defmt")]
defmt::debug!("opamp p calibration. offset: {}", mid);
w.set_trimoffsetp(mid);
}
OpAmpDifferentialPair::N => {
defmt::info!("n calibration. offset: {}", mid);
#[cfg(feature = "defmt")]
defmt::debug!("opamp n calibration. offset: {}", mid);
w.set_trimoffsetn(mid);
}
});