@@ -45,10 +45,9 @@ use core::{
45
45
/// Can be either a standard identifier (11bit, Range: 0..0x3FF) or a
46
46
/// extendended identifier (29bit , Range: 0..0x1FFFFFFF).
47
47
///
48
- /// The `Ord` trait is can be used to determine the frame’s priority this ID
49
- /// belongs to. This works because the EID and RTR flags are included in the
50
- /// underlying integer representaiton.
51
- /// Lower identifier values mean higher priority. Additionally standard frames
48
+ /// The `Ord` trait can be used to determine the frame’s priority this ID
49
+ /// belongs to.
50
+ /// Lower identifier values have a higher priority. Additionally standard frames
52
51
/// have a higher priority than extended frames and data frames have a higher
53
52
/// priority than remote frames.
54
53
#[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
@@ -67,15 +66,15 @@ impl Id {
67
66
68
67
/// Creates a new standard identifier (11bit, Range: 0..0x7FF)
69
68
///
70
- /// Ids outside the allowed range are silently truncated.
69
+ /// IDs outside the allowed range are silently truncated.
71
70
pub fn new_standard ( id : u32 ) -> Self {
72
71
assert ! ( id < 0x7FF ) ;
73
72
Self ( id << Self :: STANDARD_SHIFT )
74
73
}
75
74
76
75
/// Creates a new extendended identifier (29bit , Range: 0..0x1FFFFFFF).
77
76
///
78
- /// Ids outside the allowed range are silently truncated.
77
+ /// IDs outside the allowed range are silently truncated.
79
78
pub fn new_extended ( id : u32 ) -> Id {
80
79
assert ! ( id < 0x1FFF_FFFF ) ;
81
80
Self ( id << Self :: EXTENDED_SHIFT | Self :: IDE_MASK )
@@ -161,12 +160,12 @@ impl Frame {
161
160
frame
162
161
}
163
162
164
- /// Creates ane new frame with a standard identifier.
163
+ /// Creates a new frame with a standard identifier.
165
164
pub fn new_standard ( id : u32 , data : & [ u8 ] ) -> Self {
166
165
Self :: new ( Id :: new_standard ( id) , data)
167
166
}
168
167
169
- /// Creates ane new frame with an extended identifier.
168
+ /// Creates a new frame with an extended identifier.
170
169
pub fn new_extended ( id : u32 , data : & [ u8 ] ) -> Self {
171
170
Self :: new ( Id :: new_extended ( id) , data)
172
171
}
@@ -181,22 +180,22 @@ impl Frame {
181
180
self
182
181
}
183
182
184
- /// Returns true if this `Frame` is a extended frame
183
+ /// Returns true if this frame is an extended frame
185
184
pub fn is_extended ( & self ) -> bool {
186
185
self . id . is_extended ( )
187
186
}
188
187
189
- /// Returns true if this `Frame` is a standard frame
188
+ /// Returns true if this frame is a standard frame
190
189
pub fn is_standard ( & self ) -> bool {
191
190
self . id . is_standard ( )
192
191
}
193
192
194
- /// Returns true if this `Frame` is a remote frame
193
+ /// Returns true if this frame is a remote frame
195
194
pub fn is_remote_frame ( & self ) -> bool {
196
195
self . id . rtr ( )
197
196
}
198
197
199
- /// Returns true if this `Frame` is a data frame
198
+ /// Returns true if this frame is a data frame
200
199
pub fn is_data_frame ( & self ) -> bool {
201
200
!self . is_remote_frame ( )
202
201
}
@@ -297,8 +296,11 @@ impl traits::Pins for (PB6<Alternate<PushPull>>, PB5<Input<Floating>>) {
297
296
}
298
297
}
299
298
299
+ /// Number of supported filter banks.
300
300
#[ cfg( not( feature = "connectivity" ) ) ]
301
301
pub const NUM_FILTER_BANKS : usize = 14 ;
302
+
303
+ /// Number of supported filter banks.
302
304
#[ cfg( feature = "connectivity" ) ]
303
305
pub const NUM_FILTER_BANKS : usize = 28 ;
304
306
@@ -309,7 +311,7 @@ pub struct CanConfig<Instance> {
309
311
310
312
impl < Instance > CanConfig < Instance >
311
313
where
312
- Instance : traits:: Instance < Bus = APB1 > ,
314
+ Instance : traits:: Instance ,
313
315
{
314
316
/// Configures the bit timings.
315
317
///
@@ -322,13 +324,14 @@ where
322
324
} ) ;
323
325
}
324
326
325
- /// Enables or disables loopback mode: Internally connects the TX to RX.
327
+ /// Enables or disables loopback mode: Internally connects the TX and RX
328
+ /// signals together.
326
329
pub fn set_loopback ( & mut self , enabled : bool ) {
327
330
let can = unsafe { & * Instance :: REGISTERS } ;
328
331
can. btr . modify ( |_, w| w. lbkm ( ) . bit ( enabled) ) ;
329
332
}
330
333
331
- /// Enables or disables silent mode: Disconnects TX from the pin.
334
+ /// Enables or disables silent mode: Disconnects the TX signal from the pin.
332
335
pub fn set_silent ( & mut self , enabled : bool ) {
333
336
let can = unsafe { & * Instance :: REGISTERS } ;
334
337
can. btr . modify ( |_, w| w. silm ( ) . bit ( enabled) ) ;
@@ -417,7 +420,6 @@ where
417
420
let can = unsafe { & * Instance :: REGISTERS } ;
418
421
let msr = can. msr . read ( ) ;
419
422
if msr. slak ( ) . bit_is_set ( ) {
420
- // TODO: Make automatic bus-off management configurable.
421
423
can. mcr
422
424
. modify ( |_, w| w. abom ( ) . set_bit ( ) . sleep ( ) . clear_bit ( ) ) ;
423
425
Err ( nb:: Error :: WouldBlock )
@@ -486,7 +488,7 @@ impl Can<CAN1> {
486
488
/// `split_idx` can be in the range `0..NUM_FILTER_BANKS` and decides the number
487
489
/// of filters assigned to each peripheral. A value of `0` means all filter
488
490
/// banks are used for CAN2 while `NUM_FILTER_BANKS` reserves all filter banks
489
- /// for CAN2 .
491
+ /// for CAN1 .
490
492
#[ cfg( feature = "connectivity" ) ]
491
493
pub fn split_filters ( & mut self , split_idx : usize ) -> Option < ( Filters < CAN1 > , Filters < CAN2 > ) > {
492
494
// Set all filter banks to 32bit scale and mask mode.
@@ -570,7 +572,7 @@ impl Can<CAN1> {
570
572
}
571
573
}
572
574
573
- /// A masked filter configuration .
575
+ /// Filter with an optional mask .
574
576
pub struct Filter {
575
577
id : u32 ,
576
578
mask : u32 ,
@@ -636,9 +638,17 @@ impl Filter {
636
638
}
637
639
}
638
640
639
- fn as_16bit_filter ( reg : u32 ) -> u32 {
641
+ fn reg_to_16bit ( reg : u32 ) -> u32 {
640
642
( reg & Id :: STANDARD_MASK ) >> 16 | ( reg & Id :: IDE_MASK ) << 1 | ( reg & Id :: RTR_MASK ) << 3
641
643
}
644
+
645
+ fn id_to_16bit ( & self ) -> u32 {
646
+ Self :: reg_to_16bit ( self . id )
647
+ }
648
+
649
+ fn mask_to_16bit ( & self ) -> u32 {
650
+ Self :: reg_to_16bit ( self . mask )
651
+ }
642
652
}
643
653
644
654
/// Interface to the filter banks of a CAN peripheral.
@@ -663,6 +673,9 @@ where
663
673
}
664
674
665
675
/// Returns the number of available filters.
676
+ ///
677
+ /// This can number can be larger than the number of filter banks if
678
+ /// `Can::split_filters_advanced()` was used.
666
679
pub fn num_available ( & self ) -> usize {
667
680
let can = unsafe { & * CAN1 :: ptr ( ) } ;
668
681
@@ -714,8 +727,8 @@ where
714
727
self . check_config ( idx, false , false ) ?;
715
728
self . write_filter_bank (
716
729
idx,
717
- Filter :: as_16bit_filter ( filters[ 0 ] . mask ) << 16 | Filter :: as_16bit_filter ( filters[ 0 ] . id ) ,
718
- Filter :: as_16bit_filter ( filters[ 1 ] . mask ) << 16 | Filter :: as_16bit_filter ( filters[ 1 ] . id ) ,
730
+ filters[ 0 ] . mask_to_16bit ( ) << 16 | filters[ 0 ] . id_to_16bit ( ) ,
731
+ filters[ 1 ] . mask_to_16bit ( ) << 16 | filters[ 1 ] . id_to_16bit ( ) ,
719
732
) ;
720
733
Ok ( ( ) )
721
734
}
@@ -735,8 +748,8 @@ where
735
748
self . check_config ( idx, true , false ) ?;
736
749
self . write_filter_bank (
737
750
idx,
738
- Filter :: as_16bit_filter ( filters[ 1 ] . id ) << 16 | Filter :: as_16bit_filter ( filters[ 0 ] . id ) ,
739
- Filter :: as_16bit_filter ( filters[ 3 ] . id ) << 16 | Filter :: as_16bit_filter ( filters[ 2 ] . id ) ,
751
+ filters[ 1 ] . id_to_16bit ( ) << 16 | filters[ 0 ] . id_to_16bit ( ) ,
752
+ filters[ 3 ] . id_to_16bit ( ) << 16 | filters[ 2 ] . id_to_16bit ( ) ,
740
753
) ;
741
754
Ok ( ( ) )
742
755
}
@@ -772,7 +785,7 @@ where
772
785
self . count += 1 ;
773
786
}
774
787
775
- /// Disables all enabled filters .
788
+ /// Disables all enabled filter banks .
776
789
pub fn clear ( & mut self ) {
777
790
let can = unsafe { & * CAN1 :: ptr ( ) } ;
778
791
@@ -805,9 +818,9 @@ where
805
818
/// Puts a CAN frame in a free transmit mailbox for transmission on the bus.
806
819
///
807
820
/// Frames are transmitted to the bus based on their priority (identifier).
808
- /// Transmit order is preserved for frames with identical identifiers.
809
- /// If all transmit mailboxes are full a higher priority frame replaces the
810
- /// lowest priority frame which is returned as `Ok(Some(frame))`.
821
+ /// Transmit order is preserved for frames with of identifiers.
822
+ /// If all transmit mailboxes are full, a higher priority frame replaces the
823
+ /// lowest priority frame, which is returned as `Ok(Some(frame))`.
811
824
pub fn transmit ( & mut self , frame : & Frame ) -> nb:: Result < Option < Frame > , Infallible > {
812
825
let can = unsafe { & * Instance :: REGISTERS } ;
813
826
0 commit comments