From 7c405250a77c08740524529757f8203b5b7e0a60 Mon Sep 17 00:00:00 2001 From: Timo Kröger Date: Fri, 6 Aug 2021 00:08:24 +0200 Subject: CAN support with bxcan crate --- examples/stm32f4/Cargo.toml | 1 + examples/stm32f4/src/bin/can.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 examples/stm32f4/src/bin/can.rs (limited to 'examples') diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml index c254573f8..95c8441d9 100644 --- a/examples/stm32f4/Cargo.toml +++ b/examples/stm32f4/Cargo.toml @@ -32,3 +32,4 @@ panic-probe = { version = "0.2.0", features= ["print-defmt"] } futures = { version = "0.3.8", default-features = false, features = ["async-await"] } rtt-target = { version = "0.3", features = ["cortex-m"] } heapless = { version = "0.7.1", default-features = false } +nb = { version = "1.0" } diff --git a/examples/stm32f4/src/bin/can.rs b/examples/stm32f4/src/bin/can.rs new file mode 100644 index 000000000..d35a81d1d --- /dev/null +++ b/examples/stm32f4/src/bin/can.rs @@ -0,0 +1,39 @@ +#![no_std] +#![no_main] +#![feature(trait_alias)] +#![feature(type_alias_impl_trait)] +#![allow(incomplete_features)] + +#[path = "../example_common.rs"] +mod example_common; + +use cortex_m_rt::entry; +use embassy_stm32::bxcan::{Can, Frame, StandardId}; +use embassy_stm32::dbgmcu::Dbgmcu; +use example_common::*; + +#[entry] +fn main() -> ! { + info!("Hello World!"); + + unsafe { + Dbgmcu::enable_all(); + } + + let p = embassy_stm32::init(Default::default()); + + let mut can = Can::new(p.CAN1, p.PA11, p.PA12); + + can.modify_config().set_loopback(true); + unwrap!(nb::block!(can.enable())); + + let mut i: u8 = 0; + loop { + let tx_frame = Frame::new_data(unwrap!(StandardId::new(i as _)), [i]); + unwrap!(nb::block!(can.transmit(&tx_frame))); + while !can.is_transmitter_idle() {} + let rx_frame = unwrap!(nb::block!(can.receive())); + info!("loopback frame {=u8}", unwrap!(rx_frame.data())[0]); + i += 1; + } +} -- cgit From 191a589820159a69fbc72f996c4be91093dc2b44 Mon Sep 17 00:00:00 2001 From: Timo Kröger Date: Fri, 6 Aug 2021 11:59:16 +0200 Subject: bxcan: namechange "bxcan_v1" -> "can_bxcan" --- examples/stm32f4/src/bin/can.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/stm32f4/src/bin/can.rs b/examples/stm32f4/src/bin/can.rs index d35a81d1d..04f3417c6 100644 --- a/examples/stm32f4/src/bin/can.rs +++ b/examples/stm32f4/src/bin/can.rs @@ -8,7 +8,7 @@ mod example_common; use cortex_m_rt::entry; -use embassy_stm32::bxcan::{Can, Frame, StandardId}; +use embassy_stm32::can::{Can, Frame, StandardId}; use embassy_stm32::dbgmcu::Dbgmcu; use example_common::*; -- cgit From dacf75d911058dd09d2a825a5b23d758f1f7a92d Mon Sep 17 00:00:00 2001 From: Timo Kröger Date: Mon, 9 Aug 2021 15:59:05 +0200 Subject: bxcan: Fix the flaky CAN example --- examples/stm32f4/src/bin/can.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/stm32f4/src/bin/can.rs b/examples/stm32f4/src/bin/can.rs index 04f3417c6..2bb24f045 100644 --- a/examples/stm32f4/src/bin/can.rs +++ b/examples/stm32f4/src/bin/can.rs @@ -8,8 +8,10 @@ mod example_common; use cortex_m_rt::entry; +use embassy_stm32::can::filter::Mask32; use embassy_stm32::can::{Can, Frame, StandardId}; use embassy_stm32::dbgmcu::Dbgmcu; +use embassy_stm32::gpio::{Input, Pull}; use example_common::*; #[entry] @@ -20,11 +22,22 @@ fn main() -> ! { Dbgmcu::enable_all(); } - let p = embassy_stm32::init(Default::default()); + let mut p = embassy_stm32::init(Default::default()); + + // The next two lines are a workaround for testing without transceiver. + // To synchronise to the bus the RX input needs to see a high level. + // Use `mem::forget()` to release the borrow on the pin but keep the + // pull-up resistor enabled. + let rx_pin = Input::new(&mut p.PA11, Pull::Up); + core::mem::forget(rx_pin); let mut can = Can::new(p.CAN1, p.PA11, p.PA12); - can.modify_config().set_loopback(true); + can.modify_config() + .set_bit_timing(0x001c0003) // http://www.bittiming.can-wiki.info/ + .set_loopback(true) // Receive own frames + .set_silent(true); + can.modify_filters().enable_bank(0, Mask32::accept_all()); unwrap!(nb::block!(can.enable())); let mut i: u8 = 0; -- cgit