Skip to content

Commit

Permalink
[Vector XL] set hardcoded bitrate to 500k/2M
Browse files Browse the repository at this point in the history
  • Loading branch information
pd0wm committed Dec 6, 2024
1 parent 5f797b5 commit e562c73
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.rs text eol=lf
20 changes: 19 additions & 1 deletion src/vector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,26 @@ pub use error::Error;
use std::collections::VecDeque;

use crate::can::{AsyncCanAdapter, CanAdapter, Frame};
use crate::vector::types::{PortHandle, XLaccess, XLcanTxEvent};
use crate::vector::types::{PortHandle, XLaccess, XLcanFdConf, XLcanTxEvent};
use crate::vector::vxlapi::*;
use crate::Result;
use tracing::info;

const CONFIG_500K_2M_80: XLcanFdConf = XLcanFdConf {
arbitrationBitRate: 500_000,
sjwAbr: 1,
tseg1Abr: 25,
tseg2Abr: 6,
dataBitRate: 2_000_000,
sjwDbr: 1,
tseg1Dbr: 31,
tseg2Dbr: 8,
reserved: 0,
options: 0,
reserved1: [0, 0],
reserved2: 0,
};

#[derive(Clone)]
pub struct VectorCan {
port_handle: PortHandle,
Expand Down Expand Up @@ -42,6 +57,9 @@ impl VectorCan {
let channel_mask = xl_get_channel_mask(&config)?;
let port_handle = xl_open_port("automotive", channel_mask)?;

// Configure bitrate
xl_can_fd_set_configuration(&port_handle, channel_mask, &CONFIG_500K_2M_80)?;

xl_activate_channel(&port_handle, channel_mask)?;
info!("Connected to Vector Device. HW: {:?}", config.hw_type);

Expand Down
4 changes: 3 additions & 1 deletion src/vector/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use strum_macros::FromRepr;

use crate::vector::bindings as xl;
pub use crate::vector::bindings::{XLaccess, XLcanRxEvent, XLcanTxEvent, XLportHandle};
pub use crate::vector::bindings::{
XLaccess, XLcanFdConf, XLcanRxEvent, XLcanTxEvent, XLportHandle,
};

pub static DLC_TO_LEN: &[usize] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64];
pub static LEN_TO_DLC: &[u8] = &[
Expand Down
21 changes: 20 additions & 1 deletion src/vector/vxlapi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::vector::bindings as xl;
use crate::vector::error::Error;
use crate::vector::types::{
ChannelConfig, HwType, PortHandle, XLaccess, XLcanRxEvent, XLcanTxEvent,
ChannelConfig, HwType, PortHandle, XLaccess, XLcanFdConf, XLcanRxEvent, XLcanTxEvent,
};
use crate::Result;

Expand Down Expand Up @@ -104,6 +104,7 @@ pub fn xl_open_port(user_name: &str, access_mask: XLaccess) -> Result<PortHandle
unsafe {
let mut port_handle = std::mem::zeroed();
let mut permission_mask = std::mem::zeroed();
permission_mask = access_mask; // Request init access

let status = xl::xlOpenPort(
&mut port_handle,
Expand Down Expand Up @@ -160,6 +161,24 @@ pub fn xl_deactivate_channel(port_handle: &PortHandle, access_mask: XLaccess) ->
}
}

pub fn xl_can_fd_set_configuration(
port_handle: &PortHandle,
access_mask: XLaccess,
config: &XLcanFdConf,
) -> Result<()> {
unsafe {
let mut config = config.clone();
let status = xl::xlCanFdSetConfiguration(port_handle.port_handle, access_mask, &mut config);
match status as u32 {
xl::XL_SUCCESS => Ok(()),
_ => Err(
Error::DriverError(format!("xlCanFdSetConfiguration failed, err {}", status))
.into(),
),
}
}
}

pub fn xl_can_transmit_ex(
port_handle: &PortHandle,
access_mask: XLaccess,
Expand Down

0 comments on commit e562c73

Please sign in to comment.