Skip to content

Commit 20e5a08

Browse files
committed
Update to Id changes of embedded-can
timokroeger/embedded-can#7
1 parent 38f168a commit 20e5a08

File tree

4 files changed

+50
-30
lines changed

4 files changed

+50
-30
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ nb = "0.1.2"
2121
cortex-m-rt = "0.6.8"
2222
stm32f1 = "0.11.0"
2323
as-slice = "0.1"
24-
embedded-can = "0.1.0"
24+
embedded-can = "0.2.0"
2525

2626
[dependencies.void]
2727
default-features = false

examples/can-loopback.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,33 @@ fn main() -> ! {
6060

6161
// 2x 11bit id + mask filter bank: Matches 0, 1, 2
6262
filters
63-
.add(&Filter::new(Id::Standard(0)).with_mask(!0b1))
63+
.add(&Filter::new(Id::new_standard(0).unwrap()).with_mask(!0b1))
6464
.unwrap();
6565
filters
66-
.add(&Filter::new(Id::Standard(0)).with_mask(!0b10))
66+
.add(&Filter::new(Id::new_standard(0).unwrap()).with_mask(!0b10))
6767
.unwrap();
6868

6969
// 2x 29bit id filter bank: Matches 4, 5
70-
filters.add(&Filter::new(Id::Standard(4))).unwrap();
71-
filters.add(&Filter::new(Id::Standard(5))).unwrap();
70+
filters
71+
.add(&Filter::new(Id::new_standard(4).unwrap()))
72+
.unwrap();
73+
filters
74+
.add(&Filter::new(Id::new_standard(5).unwrap()))
75+
.unwrap();
7276

7377
// 4x 11bit id filter bank: Matches 8, 9, 10, 11
74-
filters.add(&Filter::new(Id::Standard(8))).unwrap();
75-
filters.add(&Filter::new(Id::Standard(9))).unwrap();
76-
filters.add(&Filter::new(Id::Standard(10))).unwrap();
77-
filters.add(&Filter::new(Id::Standard(11))).unwrap();
78+
filters
79+
.add(&Filter::new(Id::new_standard(8).unwrap()))
80+
.unwrap();
81+
filters
82+
.add(&Filter::new(Id::new_standard(9).unwrap()))
83+
.unwrap();
84+
filters
85+
.add(&Filter::new(Id::new_standard(10).unwrap()))
86+
.unwrap();
87+
filters
88+
.add(&Filter::new(Id::new_standard(11).unwrap()))
89+
.unwrap();
7890

7991
// Get an embedded-hal comptible interface to the peripheral.
8092
// Depending on the imported traits this can be blocking or not.
@@ -86,15 +98,15 @@ fn main() -> ! {
8698

8799
// Some messages shall pass the filters.
88100
for &id in &[0, 1, 2, 4, 5, 8, 9, 10, 11] {
89-
let frame_tx = Frame::new(Id::Standard(id), &[id as u8]).unwrap();
101+
let frame_tx = Frame::new(Id::new_standard(id).unwrap(), &[id as u8]).unwrap();
90102
block!(can_hal.try_transmit(&frame_tx)).unwrap();
91103
let frame_rx = block!(can_hal.try_receive()).unwrap();
92104
assert_eq!(frame_tx, frame_rx);
93105
}
94106

95107
// Others must be filtered out.
96108
for &id in &[3, 6, 7, 12] {
97-
let frame_tx = Frame::new(Id::Standard(id), &[id as u8]).unwrap();
109+
let frame_tx = Frame::new(Id::new_standard(id).unwrap(), &[id as u8]).unwrap();
98110
block!(can_hal.try_transmit(&frame_tx)).unwrap();
99111
assert!(can_hal.try_receive().is_err());
100112
}

examples/can-rtfm.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,18 @@ const APP: () = {
9090
#[cfg(feature = "connectivity")]
9191
let (mut filters, _) = can.split_filters(0).unwrap();
9292
filters
93-
.add(&Filter::new(Id::Standard(0)).with_mask(0).allow_remote())
93+
.add(
94+
&Filter::new(Id::new_standard(0).unwrap())
95+
.with_mask(0)
96+
.allow_remote(),
97+
)
9498
.unwrap();
9599
filters
96-
.add(&Filter::new(Id::Extended(0)).with_mask(0).allow_remote())
100+
.add(
101+
&Filter::new(Id::new_extended(0).unwrap())
102+
.with_mask(0)
103+
.allow_remote(),
104+
)
97105
.unwrap();
98106

99107
let mut can_rx = can.take_rx(filters).unwrap();
@@ -123,25 +131,25 @@ const APP: () = {
123131
// Enqueue some messages. Higher ID means lower priority.
124132
tx_queue.lock(|tx_queue| {
125133
tx_queue
126-
.push(alloc_frame(Id::Standard(9), &[0, 1, 2, 4]))
134+
.push(alloc_frame(Id::new_standard(9).unwrap(), &[0, 1, 2, 4]))
127135
.unwrap();
128136
tx_queue
129-
.push(alloc_frame(Id::Standard(9), &[0, 1, 2, 4]))
137+
.push(alloc_frame(Id::new_standard(9).unwrap(), &[0, 1, 2, 4]))
130138
.unwrap();
131139
tx_queue
132-
.push(alloc_frame(Id::Standard(8), &[0, 1, 2, 4]))
140+
.push(alloc_frame(Id::new_standard(8).unwrap(), &[0, 1, 2, 4]))
133141
.unwrap();
134142

135143
// Extended frames have lower priority than standard frames.
136144
tx_queue
137-
.push(alloc_frame(Id::Extended(8), &[0, 1, 2, 4]))
145+
.push(alloc_frame(Id::new_extended(8).unwrap(), &[0, 1, 2, 4]))
138146
.unwrap();
139147
tx_queue
140-
.push(alloc_frame(Id::Extended(7), &[0, 1, 2, 4]))
148+
.push(alloc_frame(Id::new_extended(7).unwrap(), &[0, 1, 2, 4]))
141149
.unwrap();
142150

143151
tx_queue
144-
.push(alloc_frame(Id::Standard(7), &[0, 1, 2, 4]))
152+
.push(alloc_frame(Id::new_standard(7).unwrap(), &[0, 1, 2, 4]))
145153
.unwrap();
146154
});
147155

@@ -155,13 +163,13 @@ const APP: () = {
155163
if tx_count >= 3 {
156164
tx_queue.lock(|tx_queue| {
157165
tx_queue
158-
.push(alloc_frame(Id::Standard(3), &[0, 1, 2, 4]))
166+
.push(alloc_frame(Id::new_standard(3).unwrap(), &[0, 1, 2, 4]))
159167
.unwrap();
160168
tx_queue
161-
.push(alloc_frame(Id::Standard(2), &[0, 1, 2, 4]))
169+
.push(alloc_frame(Id::new_standard(2).unwrap(), &[0, 1, 2, 4]))
162170
.unwrap();
163171
tx_queue
164-
.push(alloc_frame(Id::Standard(1), &[0, 1, 2, 4]))
172+
.push(alloc_frame(Id::new_standard(1).unwrap(), &[0, 1, 2, 4]))
165173
.unwrap();
166174
});
167175
break;

src/can.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! | TX | PB6 | PB13 |
2020
//! | RX | PB5 | PB12 |
2121
22-
pub use embedded_can::Id;
22+
pub use embedded_can::{ExtendedId, Id, StandardId};
2323

2424
use crate::afio::MAPR;
2525
use crate::bb;
@@ -99,9 +99,9 @@ impl IdReg {
9999
/// Returns the identifier.
100100
fn to_id(self) -> Id {
101101
if self.is_extended() {
102-
Id::Extended(self.0 >> Self::EXTENDED_SHIFT)
102+
Id::new_extended(self.0 >> Self::EXTENDED_SHIFT).unwrap()
103103
} else {
104-
Id::Standard(self.0 >> Self::STANDARD_SHIFT)
104+
Id::new_standard((self.0 >> Self::STANDARD_SHIFT) as u16).unwrap()
105105
}
106106
}
107107

@@ -149,13 +149,13 @@ pub struct Frame {
149149
impl Frame {
150150
/// Creates a new frame.
151151
pub fn new(id: Id, data: &[u8]) -> Result<Frame, ()> {
152-
if !id.valid() || data.len() > 8 {
152+
if data.len() > 8 {
153153
return Err(());
154154
}
155155

156156
let id = match id {
157-
Id::Standard(id) => IdReg::new_standard(id),
158-
Id::Extended(id) => IdReg::new_extended(id),
157+
Id::Standard(id) => IdReg::new_standard(id.into()),
158+
Id::Extended(id) => IdReg::new_extended(id.into()),
159159
};
160160

161161
let mut frame = Self {
@@ -629,11 +629,11 @@ impl Filter {
629629
pub fn new(id: Id) -> Self {
630630
match id {
631631
Id::Standard(id) => Filter {
632-
id: id << IdReg::STANDARD_SHIFT,
632+
id: u32::from(id) << IdReg::STANDARD_SHIFT,
633633
mask: IdReg::STANDARD_MASK | IdReg::IDE_MASK | IdReg::RTR_MASK,
634634
},
635635
Id::Extended(id) => Filter {
636-
id: id << IdReg::EXTENDED_SHIFT | IdReg::IDE_MASK,
636+
id: u32::from(id) << IdReg::EXTENDED_SHIFT | IdReg::IDE_MASK,
637637
mask: IdReg::EXTENDED_MASK | IdReg::IDE_MASK | IdReg::RTR_MASK,
638638
},
639639
}

0 commit comments

Comments
 (0)