-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlib.rs
332 lines (287 loc) · 8.87 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
pub mod prelude {
pub use embedded_can::{Can as _, Frame as _};
}
pub use embedded_can::{ExtendedId, Id, StandardId};
use std::{
ffi::{c_void, CString},
fmt,
mem::{self, MaybeUninit},
ptr,
};
use pcan_basic_sys::*;
use winapi::{
shared::minwindef::FALSE,
um::{synchapi, winbase::INFINITE, winnt::HANDLE},
};
#[derive(Debug)]
pub struct Error(String);
impl Error {
fn new(error_code: u32) -> Self {
unsafe {
let raw_error_msg = CString::from_vec_unchecked(Vec::with_capacity(256)).into_raw();
CAN_GetErrorText(error_code, 0, raw_error_msg);
Self(String::from_utf8_unchecked(
CString::from_raw(raw_error_msg).into_bytes(),
))
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::error::Error for Error {}
pub struct Interface {
channel: u16,
event_handle: HANDLE,
}
impl Interface {
pub fn init() -> Result<Self, Error> {
let pcan_channel = PCAN_USBBUS1 as u16;
// When running with 125kbps the STM32 bootloader sets the acknowledge bit early.
// Choose a nominal sample point of 75% to prevent form errors in the CRC delimiter.
// Value calculated using http://www.bittiming.can-wiki.info/ (NXP SJA1000)
const BAUDRATE_CONFIG: u16 = 0x033A;
let result = unsafe { CAN_Initialize(pcan_channel, BAUDRATE_CONFIG, 0, 0, 0) };
if result != PCAN_ERROR_OK {
return Err(Error::new(result));
}
let mut parameter_off = PCAN_PARAMETER_OFF;
unsafe {
CAN_SetValue(
pcan_channel,
PCAN_ALLOW_STATUS_FRAMES as u8,
&mut parameter_off as *mut _ as *mut c_void,
mem::size_of_val(¶meter_off) as u32,
);
}
let mut event_handle =
unsafe { synchapi::CreateEventA(ptr::null_mut(), FALSE, FALSE, ptr::null()) };
if event_handle.is_null() {
return Err(Error::new(result));
}
unsafe {
CAN_SetValue(
pcan_channel,
PCAN_RECEIVE_EVENT as u8,
&mut event_handle as *mut _ as *mut c_void,
mem::size_of_val(&event_handle) as u32,
);
};
let mut this = Self {
channel: pcan_channel,
event_handle,
};
// Drain all messages that were received since `init()` has been called.
loop {
if let Err(nb::Error::WouldBlock) = this.receive() {
break;
}
}
Ok(this)
}
}
impl Drop for Interface {
fn drop(&mut self) {
unsafe { CAN_Uninitialize(self.channel) };
}
}
#[derive(Debug)]
pub struct Frame(TPCANMsg);
impl embedded_can::Frame for Frame {
fn new(id: impl Into<Id>, data: &[u8]) -> Result<Frame, ()> {
if data.len() > 8 {
return Err(());
}
let (id, msg_type) = match id.into() {
Id::Standard(id) => (id.as_raw() as u32, PCAN_MESSAGE_STANDARD),
Id::Extended(id) => (id.as_raw(), PCAN_MESSAGE_EXTENDED),
};
let mut msg = TPCANMsg {
ID: id,
MSGTYPE: msg_type as u8,
LEN: data.len() as u8,
DATA: [0; 8],
};
msg.DATA[0..data.len()].copy_from_slice(data);
Ok(Frame(msg))
}
fn new_remote(id: impl Into<Id>, dlc: usize) -> Result<Frame, ()> {
if dlc >= 8 {
return Err(());
}
let mut frame = Frame::new(id, &[])?;
frame.0.MSGTYPE |= PCAN_MESSAGE_RTR as u8;
frame.0.LEN = dlc as u8;
Ok(frame)
}
fn is_extended(&self) -> bool {
self.0.MSGTYPE & PCAN_MESSAGE_EXTENDED as u8 != 0
}
fn is_remote_frame(&self) -> bool {
self.0.MSGTYPE & PCAN_MESSAGE_RTR as u8 != 0
}
fn id(&self) -> Id {
if self.is_extended() {
ExtendedId::new(self.0.ID).unwrap().into()
} else {
StandardId::new(self.0.ID as u16).unwrap().into()
}
}
fn dlc(&self) -> usize {
self.0.LEN as usize
}
fn data(&self) -> &[u8] {
&self.0.DATA[0..self.0.LEN as usize]
}
}
impl Interface {
fn transmit(&mut self, frame: &Frame) -> nb::Result<Option<Frame>, Error> {
let result = unsafe { CAN_Write(self.channel, &frame.0 as *const _ as *mut _) };
if result == PCAN_ERROR_OK {
Ok(None)
} else {
Err(nb::Error::Other(Error::new(result)))
}
}
fn receive(&mut self) -> nb::Result<Frame, Error> {
let mut msg = MaybeUninit::<TPCANMsg>::uninit();
let (result, msg) = unsafe {
(
CAN_Read(self.channel, msg.as_mut_ptr(), ptr::null_mut()),
msg.assume_init(),
)
};
match result {
PCAN_ERROR_QRCVEMPTY => Err(nb::Error::WouldBlock),
PCAN_ERROR_OK => Ok(Frame(msg)),
_ => Err(nb::Error::Other(Error::new(result))),
}
}
}
impl embedded_can::Can for Interface {
type Frame = Frame;
type Error = Error;
fn try_transmit(&mut self, frame: &Frame) -> nb::Result<Option<Frame>, Error> {
self.transmit(frame)
}
fn try_receive(&mut self) -> nb::Result<Frame, Error> {
self.receive()
}
}
impl embedded_can::blocking::Can for Interface {
type Frame = Frame;
type Error = Error;
fn try_write(&mut self, frame: &Frame) -> Result<(), Error> {
match self.transmit(frame) {
Ok(_) => Ok(()),
Err(nb::Error::Other(err)) => Err(err),
_ => panic!("The PCAN driver should never block!"),
}
}
fn try_read(&mut self) -> Result<Frame, Error> {
match self.receive() {
Err(nb::Error::WouldBlock) => {
unsafe { synchapi::WaitForSingleObject(self.event_handle, INFINITE) };
match self.receive() {
Ok(frame) => Ok(frame),
Err(nb::Error::Other(err)) => Err(err),
_ => panic!("Receive queue should not be empty!"),
}
}
Ok(frame) => Ok(frame),
Err(nb::Error::Other(err)) => Err(err),
}
}
}
pub struct Filter {
accept_all: bool,
is_extended: bool,
id: u32,
mask: u32,
}
impl Filter {
pub fn accept_all() -> Self {
// TODO: Fix
Self {
accept_all: true,
is_extended: true,
id: 0,
mask: 0,
}
}
pub fn new(id: Id) -> Self {
match id {
Id::Standard(id) => Self {
accept_all: false,
is_extended: false,
id: id.as_raw() as u32,
mask: 0x7FF,
},
Id::Extended(id) => Self {
accept_all: false,
is_extended: true,
id: id.as_raw(),
mask: 0x1FFF_FFFF,
},
}
}
pub fn with_mask(&mut self, mask: u32) -> &mut Self {
self.mask = mask;
self
}
}
impl Interface {
pub fn add_filter(&mut self, filter: &Filter) -> Result<(), Error> {
let mut filter_state = 0u32;
unsafe {
CAN_GetValue(
self.channel,
PCAN_MESSAGE_FILTER as u8,
&mut filter_state as *mut _ as *mut c_void,
mem::size_of_val(&filter_state) as u32,
);
}
if filter_state == PCAN_FILTER_CUSTOM {
return Err(Error("Cannot configure more than one filter".to_string()));
}
if filter.accept_all {
let mut filter_open = PCAN_FILTER_OPEN;
unsafe {
CAN_SetValue(
self.channel,
PCAN_MESSAGE_FILTER as u8,
&mut filter_open as *mut _ as *mut c_void,
mem::size_of_val(&filter_open) as u32,
);
};
} else {
let mut value = [filter.mask.to_le(), filter.id.to_le()];
unsafe {
CAN_SetValue(
self.channel,
if filter.is_extended {
PCAN_ACCEPTANCE_FILTER_29BIT
} else {
PCAN_ACCEPTANCE_FILTER_11BIT
} as u8,
&mut value as *mut _ as *mut c_void,
mem::size_of_val(&value) as u32,
);
};
}
Ok(())
}
pub fn clear_filters(&mut self) {
let mut filter_open = PCAN_FILTER_CLOSE;
unsafe {
CAN_SetValue(
self.channel,
PCAN_MESSAGE_FILTER as u8,
&mut filter_open as *mut _ as *mut c_void,
mem::size_of_val(&filter_open) as u32,
);
};
}
}