1 | // Note: This file is copied and modified from fdcan crate by Richard Meadows |
2 | |
3 | /// Datalength is the message length generalised over |
4 | /// the Standard (Classic) and FDCAN message types |
5 | |
6 | #[derive (Clone, Copy, Debug, PartialEq)] |
7 | pub enum DataLength { |
8 | Classic(u8), |
9 | Fdcan(u8), |
10 | } |
11 | impl DataLength { |
12 | /// Creates a DataLength type |
13 | /// |
14 | /// Uses the byte length and Type of frame as input |
15 | pub fn new(len: u8, ff: FrameFormat) -> DataLength { |
16 | match ff { |
17 | FrameFormat::Classic => match len { |
18 | 0..=8 => DataLength::Classic(len), |
19 | _ => panic!("DataLength > 8" ), |
20 | }, |
21 | FrameFormat::Fdcan => match len { |
22 | 0..=64 => DataLength::Fdcan(len), |
23 | _ => panic!("DataLength > 64" ), |
24 | }, |
25 | } |
26 | } |
27 | /// Specialised function to create classic frames |
28 | pub fn new_classic(len: u8) -> DataLength { |
29 | Self::new(len, FrameFormat::Classic) |
30 | } |
31 | /// Specialised function to create FDCAN frames |
32 | pub fn new_fdcan(len: u8) -> DataLength { |
33 | Self::new(len, FrameFormat::Fdcan) |
34 | } |
35 | |
36 | /// returns the length in bytes |
37 | pub fn len(&self) -> u8 { |
38 | match self { |
39 | DataLength::Classic(l) | DataLength::Fdcan(l) => *l, |
40 | } |
41 | } |
42 | |
43 | pub(crate) fn dlc(&self) -> u8 { |
44 | match self { |
45 | DataLength::Classic(l) => *l, |
46 | // See RM0433 Rev 7 Table 475. DLC coding |
47 | DataLength::Fdcan(l) => match l { |
48 | 0..=8 => *l, |
49 | 9..=12 => 9, |
50 | 13..=16 => 10, |
51 | 17..=20 => 11, |
52 | 21..=24 => 12, |
53 | 25..=32 => 13, |
54 | 33..=48 => 14, |
55 | 49..=64 => 15, |
56 | _ => panic!("DataLength > 64" ), |
57 | }, |
58 | } |
59 | } |
60 | } |
61 | impl From<DataLength> for FrameFormat { |
62 | fn from(dl: DataLength) -> FrameFormat { |
63 | match dl { |
64 | DataLength::Classic(_) => FrameFormat::Classic, |
65 | DataLength::Fdcan(_) => FrameFormat::Fdcan, |
66 | } |
67 | } |
68 | } |
69 | |
70 | /// Wheter or not to generate an Tx Event |
71 | #[derive (Clone, Copy, Debug, PartialEq)] |
72 | pub enum Event { |
73 | /// Do not generate an Tx Event |
74 | NoEvent, |
75 | /// Generate an Tx Event with a specified ID |
76 | Event(u8), |
77 | } |
78 | |
79 | impl From<Event> for EventControl { |
80 | fn from(e: Event) -> Self { |
81 | match e { |
82 | Event::NoEvent => EventControl::DoNotStore, |
83 | Event::Event(_) => EventControl::Store, |
84 | } |
85 | } |
86 | } |
87 | |
88 | impl From<Option<u8>> for Event { |
89 | fn from(mm: Option<u8>) -> Self { |
90 | match mm { |
91 | None => Event::NoEvent, |
92 | Some(mm: u8) => Event::Event(mm), |
93 | } |
94 | } |
95 | } |
96 | |
97 | impl From<Event> for Option<u8> { |
98 | fn from(e: Event) -> Option<u8> { |
99 | match e { |
100 | Event::NoEvent => None, |
101 | Event::Event(mm: u8) => Some(mm), |
102 | } |
103 | } |
104 | } |
105 | |
106 | /// TODO |
107 | #[derive (Clone, Copy, Debug, PartialEq)] |
108 | pub enum ErrorStateIndicator { |
109 | /// TODO |
110 | ErrorActive = 0, |
111 | /// TODO |
112 | ErrorPassive = 1, |
113 | } |
114 | impl From<ErrorStateIndicator> for bool { |
115 | #[inline (always)] |
116 | fn from(e: ErrorStateIndicator) -> Self { |
117 | e as u8 != 0 |
118 | } |
119 | } |
120 | |
121 | /// Type of frame, standard (classic) or FdCAN |
122 | #[derive (Clone, Copy, Debug, PartialEq)] |
123 | pub enum FrameFormat { |
124 | Classic = 0, |
125 | Fdcan = 1, |
126 | } |
127 | impl From<FrameFormat> for bool { |
128 | #[inline (always)] |
129 | fn from(e: FrameFormat) -> Self { |
130 | e as u8 != 0 |
131 | } |
132 | } |
133 | |
134 | /// Type of Id, Standard or Extended |
135 | #[derive (Clone, Copy, Debug, PartialEq)] |
136 | pub enum IdType { |
137 | /// Standard ID |
138 | StandardId = 0, |
139 | /// Extended ID |
140 | ExtendedId = 1, |
141 | } |
142 | impl From<IdType> for bool { |
143 | #[inline (always)] |
144 | fn from(e: IdType) -> Self { |
145 | e as u8 != 0 |
146 | } |
147 | } |
148 | |
149 | /// Whether the frame contains data or requests data |
150 | #[derive (Clone, Copy, Debug, PartialEq)] |
151 | pub enum RemoteTransmissionRequest { |
152 | /// Frame contains data |
153 | TransmitDataFrame = 0, |
154 | /// frame does not contain data |
155 | TransmitRemoteFrame = 1, |
156 | } |
157 | impl From<RemoteTransmissionRequest> for bool { |
158 | #[inline (always)] |
159 | fn from(e: RemoteTransmissionRequest) -> Self { |
160 | e as u8 != 0 |
161 | } |
162 | } |
163 | |
164 | /// Whether BitRateSwitching should be or was enabled |
165 | #[derive (Clone, Copy, Debug, PartialEq)] |
166 | pub enum BitRateSwitching { |
167 | /// disable bit rate switching |
168 | WithoutBRS = 0, |
169 | /// enable bit rate switching |
170 | WithBRS = 1, |
171 | } |
172 | impl From<BitRateSwitching> for bool { |
173 | #[inline (always)] |
174 | fn from(e: BitRateSwitching) -> Self { |
175 | e as u8 != 0 |
176 | } |
177 | } |
178 | |
179 | /// Whether to store transmit Events |
180 | #[derive (Clone, Copy, Debug, PartialEq)] |
181 | pub enum EventControl { |
182 | /// do not store an tx event |
183 | DoNotStore, |
184 | /// store transmit events |
185 | Store, |
186 | } |
187 | impl From<EventControl> for bool { |
188 | #[inline (always)] |
189 | fn from(e: EventControl) -> Self { |
190 | e as u8 != 0 |
191 | } |
192 | } |
193 | |
194 | /// If an received message matched any filters |
195 | #[derive (Clone, Copy, Debug, PartialEq)] |
196 | pub enum FilterFrameMatch { |
197 | /// This did match filter <id> |
198 | DidMatch(u8), |
199 | /// This received frame did not match any specific filters |
200 | DidNotMatch, |
201 | } |
202 | |
203 | /// Type of filter to be used |
204 | #[derive (Clone, Copy, Debug, PartialEq)] |
205 | pub enum FilterType { |
206 | /// Filter uses the range between two id's |
207 | RangeFilter = 0b00, |
208 | /// The filter matches on two specific id's (or one ID checked twice) |
209 | DualIdFilter = 0b01, |
210 | /// Filter is using a bitmask |
211 | ClassicFilter = 0b10, |
212 | /// Filter is disabled |
213 | FilterDisabled = 0b11, |
214 | } |
215 | |
216 | #[derive (Clone, Copy, Debug, PartialEq)] |
217 | pub enum FilterElementConfig { |
218 | /// Filter is disabled |
219 | DisableFilterElement = 0b000, |
220 | /// Store a matching message in FIFO 0 |
221 | StoreInFifo0 = 0b001, |
222 | /// Store a matching message in FIFO 1 |
223 | StoreInFifo1 = 0b010, |
224 | /// Reject a matching message |
225 | Reject = 0b011, |
226 | /// Flag that a priority message has been received, *But do note store!*?? |
227 | SetPriority = 0b100, |
228 | /// Flag and store message in FIFO 0 |
229 | SetPriorityAndStoreInFifo0 = 0b101, |
230 | /// Flag and store message in FIFO 1 |
231 | SetPriorityAndStoreInFifo1 = 0b110, |
232 | //_Unused = 0b111, |
233 | } |
234 | |