Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6d520a6

Browse files
committedFeb 1, 2025·
fixed comment in definition of write_frame, de-nested some of the matches when converting from embassy's frame to HypedCanFrame and vice versa
1 parent f4754dd commit 6d520a6

File tree

2 files changed

+26
-45
lines changed

2 files changed

+26
-45
lines changed
 

‎lib/io/hyped_can/hyped_can_derive/src/lib.rs

+25-42
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,20 @@ fn impl_hyped_can(ast: &syn::DeriveInput) -> TokenStream {
1919
fn read_frame(&mut self) -> Result<HypedEnvelope, CanError> {
2020
let result = self.can.lock(|can| can.borrow_mut().try_read());
2121
match result {
22-
Ok(envelope) => Ok(HypedEnvelope {
23-
frame: HypedCanFrame {
24-
can_id: {
25-
let id = envelope.frame.id();
26-
match id {
27-
Id::Standard(id) => id.as_raw() as u32, // 11-bit ID
28-
Id::Extended(id) => id.as_raw(), // 29-bit ID
29-
}
30-
},
31-
data: {
32-
let mut data = [0u8; 8];
33-
data.copy_from_slice(envelope.frame.data());
34-
data
35-
},
36-
},
37-
ts: envelope.ts,
38-
}),
22+
Ok(envelope) => {
23+
let can_id = match envelope.frame.id() {
24+
Id::Standard(id) => id.as_raw() as u32, // 11-bit ID
25+
Id::Extended(id) => id.as_raw(), // 29-bit ID
26+
};
27+
28+
let mut data = [0u8; 8];
29+
data.copy_from_slice(envelope.frame.data());
30+
31+
Ok(HypedEnvelope {
32+
frame: HypedCanFrame { can_id, data },
33+
ts: envelope.ts,
34+
})
35+
}
3936
Err(TryReadError::BusError(e)) => Err(match e {
4037
BusError::Stuff => CanError::Stuff,
4138
BusError::Form => CanError::Form,
@@ -53,25 +50,14 @@ fn impl_hyped_can(ast: &syn::DeriveInput) -> TokenStream {
5350
}
5451

5552
fn write_frame(&mut self, frame: &HypedCanFrame) -> Result<(), CanError> {
56-
let frame = Frame::new(
57-
match frame.can_id {
58-
id if id <= 0x7FF => frame::Header::new(
59-
Id::Standard(
60-
StandardId::new(id as u16).unwrap(),
61-
),
62-
frame.data.len() as u8,
63-
false,
64-
),
65-
id => frame::Header::new(
66-
Id::Extended(
67-
ExtendedId::new(id).unwrap(),
68-
),
69-
frame.data.len() as u8,
70-
false,
71-
),
72-
},
73-
&frame.data,
74-
);
53+
match frame.can_id {
54+
id if id <= 0x7FF => Id::Standard(StandardId::new(id as u16).unwrap()),
55+
id => Id::Extended(ExtendedId::new(id).unwrap()),
56+
};
57+
58+
let frame_header = frame::Header::new(id, frame.data.len() as u8, false);
59+
60+
let frame = Frame::new(frame_header, &frame.data);
7561

7662
match frame {
7763
Ok(frame) => {
@@ -82,19 +68,16 @@ fn impl_hyped_can(ast: &syn::DeriveInput) -> TokenStream {
8268
}
8369
}
8470
Err(e) => Err(match e {
85-
FrameCreateError::NotEnoughData => {
86-
CanError::NotEnoughData
87-
}
88-
FrameCreateError::InvalidDataLength => {
89-
CanError::InvalidDataLength
90-
}
71+
FrameCreateError::NotEnoughData => CanError::NotEnoughData,
72+
FrameCreateError::InvalidDataLength => CanError::InvalidDataLength,
9173
FrameCreateError::InvalidCanId => CanError::InvalidCanId,
9274
}),
9375
}
9476
}
9577
}
9678

9779

80+
9881
impl #impl_generics #name #ty_generics {
9982
pub fn new(can: &'static Mutex<NoopRawMutex, RefCell<Can<'static>>>) -> Self {
10083
Self { can }

‎lib/io/hyped_can/src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ pub trait HypedCan {
5151
///
5252
/// Returns [Err(CanError::Full)] if the frame can not be queued for transmission now.
5353
///
54-
/// If FIFO scheduling is enabled, any empty mailbox will be used.
55-
///
56-
/// Otherwise, the frame will only be accepted if there is no frame with the same priority already queued. This is done
54+
/// The frame will only be accepted if there is no frame with the same priority already queued. This is done
5755
/// to work around a hardware limitation that could lead to out-of-order delivery of frames with the same priority.
5856
fn write_frame(&mut self, frame: &HypedCanFrame) -> Result<(), CanError>;
5957
}

0 commit comments

Comments
 (0)
Please sign in to comment.