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

Adding TWAI HIL test #1946

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions hil-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ name = "embassy_interrupt_executor"
harness = false
required-features = ["async", "embassy", "integrated-timers"]

[[test]]
name = "twai"
harness = false

[dependencies]
cfg-if = "1.0.0"
critical-section = "1.1.2"
Expand Down
86 changes: 86 additions & 0 deletions hil-test/tests/twai.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//! TWAI test
//!
//! Folowing pins are used:
//! TX GPIO2
//! RX GPIO3
//!
//! Connect TX (GPIO2) and RX (GPIO3) pins.

//% CHIPS: esp32c3 esp32c6 esp32s2 esp32s3

#![no_std]
#![no_main]

use defmt_rtt as _;
use embedded_hal_02::can::Frame;
use esp_backtrace as _;
use esp_hal::{
clock::ClockControl,
gpio::Io,
peripherals::{Peripherals, TWAI0},
prelude::*,
system::SystemControl,
twai::{self, filter::SingleStandardFilter, EspTwaiFrame, StandardId, TwaiMode},
Blocking,
};
use nb::block;

struct Context {
twai: twai::Twai<'static, TWAI0, Blocking>,
}

impl Context {
pub fn init() -> Self {
let peripherals = Peripherals::take();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);

let can_tx_pin = io.pins.gpio2;
let can_rx_pin = io.pins.gpio3;

const CAN_BAUDRATE: twai::BaudRate = twai::BaudRate::B1000K;

let mut config = twai::TwaiConfiguration::new(
peripherals.TWAI0,
can_tx_pin,
can_rx_pin,
&clocks,
CAN_BAUDRATE,
TwaiMode::SelfTest,
);

const FILTER: SingleStandardFilter =
SingleStandardFilter::new(b"00000000000", b"x", [b"xxxxxxxx", b"xxxxxxxx"]);
config.set_filter(FILTER);

let mut twai = config.start();
playfulFence marked this conversation as resolved.
Show resolved Hide resolved

Context { twai }
}
}

#[cfg(test)]
#[embedded_test::tests]
mod tests {
use defmt::assert_eq;

use super::*;

#[init]
fn init() -> Context {
Context::init()
}

#[test]
#[timeout(3)]
fn test_send_receive(mut ctx: Context) {
let frame = EspTwaiFrame::new_self_reception(StandardId::ZERO.into(), &[1, 2, 3]).unwrap();
block!(ctx.twai.transmit(&frame)).unwrap();

let frame = block!(ctx.twai.receive()).unwrap();

assert_eq!(frame.data(), &[1, 2, 3])
}
}