From dc3b83f9c83ac9316c0e623af1726fe975557ad3 Mon Sep 17 00:00:00 2001 From: Adam Greig Date: Fri, 18 Apr 2025 14:42:16 +0100 Subject: [PATCH] STM32: Change CAN data() to return slice with correct length --- embassy-stm32/src/can/fd/peripheral.rs | 2 +- embassy-stm32/src/can/frame.rs | 18 ++++++++---------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/embassy-stm32/src/can/fd/peripheral.rs b/embassy-stm32/src/can/fd/peripheral.rs index 39c4112ad..014f31c87 100644 --- a/embassy-stm32/src/can/fd/peripheral.rs +++ b/embassy-stm32/src/can/fd/peripheral.rs @@ -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; } diff --git a/embassy-stm32/src/can/frame.rs b/embassy-stm32/src/can/frame.rs index f621b8bd5..a1d773f2c 100644 --- a/embassy-stm32/src/can/frame.rs +++ b/embassy-stm32/src/can/frame.rs @@ -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 { 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() } }