Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Vector XL] set hardcoded bitrate to 500k/2M #81

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 15,
tseg2Abr: 4,
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
22 changes: 20 additions & 2 deletions 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 @@ -103,7 +103,7 @@ pub fn xl_get_channel_mask(app_config: &ChannelConfig) -> Result<XLaccess> {
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();
let mut permission_mask = access_mask; // Request init access so we can change bitrate

let status = xl::xlOpenPort(
&mut port_handle,
Expand Down Expand Up @@ -160,6 +160,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
Loading