STM32: Change CAN data() to return slice with correct length

This commit is contained in:
Adam Greig 2025-04-18 14:42:16 +01:00
parent 97172c36b7
commit dc3b83f9c8
No known key found for this signature in database
GPG Key ID: 80E6DEA26ADACD09
2 changed files with 9 additions and 11 deletions

View File

@ -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,12 @@ impl Frame {
/// 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]
}
/// Get priority of frame
@ -260,7 +258,7 @@ impl embedded_can::Frame for Frame {
self.can_header.len as usize
}
fn data(&self) -> &[u8] {
&self.data.raw()
&self.data()
}
}
@ -405,12 +403,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 +446,7 @@ impl embedded_can::Frame for FdFrame {
self.can_header.len as usize
}
fn data(&self) -> &[u8] {
&self.data.raw()
&self.data()
}
}