Skip to content

Commit 8d1732a

Browse files
committed
can: Add Id enum
With this addition the frame constructors are now fallible and return `Error` when the identifier is out of range.
1 parent e1b217d commit 8d1732a

File tree

3 files changed

+106
-88
lines changed

3 files changed

+106
-88
lines changed

examples/can-loopback.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use cortex_m_rt::entry;
1010
use embedded_hal::digital::v2::OutputPin;
1111
use nb::block;
1212
use stm32f1xx_hal::{
13-
can::{Can, Filter, Frame},
13+
can::{Can, Filter, Frame, Id},
1414
pac,
1515
prelude::*,
1616
};
@@ -59,21 +59,21 @@ fn main() -> ! {
5959

6060
// 2x 11bit id + mask filter bank: Matches 0, 1, 2
6161
filters
62-
.add(&Filter::new_standard(0).with_mask(!0b1))
62+
.add(&Filter::new(Id::Standard(0)).with_mask(!0b1))
6363
.unwrap();
6464
filters
65-
.add(&Filter::new_standard(0).with_mask(!0b10))
65+
.add(&Filter::new(Id::Standard(0)).with_mask(!0b10))
6666
.unwrap();
6767

6868
// 2x 29bit id filter bank: Matches 4, 5
69-
filters.add(&Filter::new_standard(4)).unwrap();
70-
filters.add(&Filter::new_standard(5)).unwrap();
69+
filters.add(&Filter::new(Id::Standard(4))).unwrap();
70+
filters.add(&Filter::new(Id::Standard(5))).unwrap();
7171

7272
// 4x 11bit id filter bank: Matches 8, 9, 10, 11
73-
filters.add(&Filter::new_standard(8)).unwrap();
74-
filters.add(&Filter::new_standard(9)).unwrap();
75-
filters.add(&Filter::new_standard(10)).unwrap();
76-
filters.add(&Filter::new_standard(11)).unwrap();
73+
filters.add(&Filter::new(Id::Standard(8))).unwrap();
74+
filters.add(&Filter::new(Id::Standard(9))).unwrap();
75+
filters.add(&Filter::new(Id::Standard(10))).unwrap();
76+
filters.add(&Filter::new(Id::Standard(11))).unwrap();
7777

7878
// Split the peripheral into transmitter and receiver parts.
7979
let mut rx = can.take_rx(filters).unwrap();
@@ -84,15 +84,15 @@ fn main() -> ! {
8484

8585
// Some messages shall pass the filters.
8686
for &id in &[0, 1, 2, 4, 5, 8, 9, 10, 11] {
87-
let frame_tx = Frame::new_standard(id, &[id as u8]);
87+
let frame_tx = Frame::new(Id::Standard(id), &[id as u8]).unwrap();
8888
block!(tx.transmit(&frame_tx)).unwrap();
8989
let frame_rx = block!(rx.receive()).unwrap();
9090
assert_eq!(frame_tx, frame_rx);
9191
}
9292

9393
// Others must be filtered out.
9494
for &id in &[3, 6, 7, 12] {
95-
let frame_tx = Frame::new_standard(id, &[id as u8]);
95+
let frame_tx = Frame::new(Id::Standard(id), &[id as u8]).unwrap();
9696
block!(tx.transmit(&frame_tx)).unwrap();
9797
assert!(rx.receive().is_err());
9898
}

examples/can-rtfm.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pool!(
3535

3636
fn alloc_frame(id: Id, data: &[u8]) -> Box<CanFramePool, Init> {
3737
let frame_box = CanFramePool::alloc().unwrap();
38-
frame_box.init(Frame::new(id, data))
38+
frame_box.init(Frame::new(id, data).unwrap())
3939
}
4040

4141
#[app(device = stm32f1xx_hal::pac, peripherals = true)]
@@ -90,10 +90,10 @@ const APP: () = {
9090
#[cfg(feature = "connectivity")]
9191
let (mut filters, _) = can.split_filters(0).unwrap();
9292
filters
93-
.add(&Filter::new_standard(0).with_mask(0).allow_remote())
93+
.add(&Filter::new(Id::Standard(0)).with_mask(0).allow_remote())
9494
.unwrap();
9595
filters
96-
.add(&Filter::new_extended(0).with_mask(0).allow_remote())
96+
.add(&Filter::new(Id::Extended(0)).with_mask(0).allow_remote())
9797
.unwrap();
9898

9999
let mut can_rx = can.take_rx(filters).unwrap();
@@ -123,25 +123,25 @@ const APP: () = {
123123
// Enqueue some messages. Higher ID means lower priority.
124124
tx_queue.lock(|tx_queue| {
125125
tx_queue
126-
.push(alloc_frame(Id::new_standard(9), &[0, 1, 2, 4]))
126+
.push(alloc_frame(Id::Standard(9), &[0, 1, 2, 4]))
127127
.unwrap();
128128
tx_queue
129-
.push(alloc_frame(Id::new_standard(9), &[0, 1, 2, 4]))
129+
.push(alloc_frame(Id::Standard(9), &[0, 1, 2, 4]))
130130
.unwrap();
131131
tx_queue
132-
.push(alloc_frame(Id::new_standard(8), &[0, 1, 2, 4]))
132+
.push(alloc_frame(Id::Standard(8), &[0, 1, 2, 4]))
133133
.unwrap();
134134

135135
// Extended frames have lower priority than standard frames.
136136
tx_queue
137-
.push(alloc_frame(Id::new_extended(8), &[0, 1, 2, 4]))
137+
.push(alloc_frame(Id::Extended(8), &[0, 1, 2, 4]))
138138
.unwrap();
139139
tx_queue
140-
.push(alloc_frame(Id::new_extended(7), &[0, 1, 2, 4]))
140+
.push(alloc_frame(Id::Extended(7), &[0, 1, 2, 4]))
141141
.unwrap();
142142

143143
tx_queue
144-
.push(alloc_frame(Id::new_standard(7), &[0, 1, 2, 4]))
144+
.push(alloc_frame(Id::Standard(7), &[0, 1, 2, 4]))
145145
.unwrap();
146146
});
147147

@@ -155,13 +155,13 @@ const APP: () = {
155155
if tx_count >= 3 {
156156
tx_queue.lock(|tx_queue| {
157157
tx_queue
158-
.push(alloc_frame(Id::new_standard(3), &[0, 1, 2, 4]))
158+
.push(alloc_frame(Id::Standard(3), &[0, 1, 2, 4]))
159159
.unwrap();
160160
tx_queue
161-
.push(alloc_frame(Id::new_standard(2), &[0, 1, 2, 4]))
161+
.push(alloc_frame(Id::Standard(2), &[0, 1, 2, 4]))
162162
.unwrap();
163163
tx_queue
164-
.push(alloc_frame(Id::new_standard(1), &[0, 1, 2, 4]))
164+
.push(alloc_frame(Id::Standard(1), &[0, 1, 2, 4]))
165165
.unwrap();
166166
});
167167
break;

0 commit comments

Comments
 (0)