diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32f4/Cargo.toml | 1 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/can.rs | 52 |
2 files changed, 53 insertions, 0 deletions
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"] } | |||
| 32 | futures = { version = "0.3.8", default-features = false, features = ["async-await"] } | 32 | futures = { version = "0.3.8", default-features = false, features = ["async-await"] } |
| 33 | rtt-target = { version = "0.3", features = ["cortex-m"] } | 33 | rtt-target = { version = "0.3", features = ["cortex-m"] } |
| 34 | heapless = { version = "0.7.1", default-features = false } | 34 | heapless = { version = "0.7.1", default-features = false } |
| 35 | 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..2bb24f045 --- /dev/null +++ b/examples/stm32f4/src/bin/can.rs | |||
| @@ -0,0 +1,52 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(trait_alias)] | ||
| 4 | #![feature(type_alias_impl_trait)] | ||
| 5 | #![allow(incomplete_features)] | ||
| 6 | |||
| 7 | #[path = "../example_common.rs"] | ||
| 8 | mod example_common; | ||
| 9 | |||
| 10 | use cortex_m_rt::entry; | ||
| 11 | use embassy_stm32::can::filter::Mask32; | ||
| 12 | use embassy_stm32::can::{Can, Frame, StandardId}; | ||
| 13 | use embassy_stm32::dbgmcu::Dbgmcu; | ||
| 14 | use embassy_stm32::gpio::{Input, Pull}; | ||
| 15 | use example_common::*; | ||
| 16 | |||
| 17 | #[entry] | ||
| 18 | fn main() -> ! { | ||
| 19 | info!("Hello World!"); | ||
| 20 | |||
| 21 | unsafe { | ||
| 22 | Dbgmcu::enable_all(); | ||
| 23 | } | ||
| 24 | |||
| 25 | let mut p = embassy_stm32::init(Default::default()); | ||
| 26 | |||
| 27 | // The next two lines are a workaround for testing without transceiver. | ||
| 28 | // To synchronise to the bus the RX input needs to see a high level. | ||
| 29 | // Use `mem::forget()` to release the borrow on the pin but keep the | ||
| 30 | // pull-up resistor enabled. | ||
| 31 | let rx_pin = Input::new(&mut p.PA11, Pull::Up); | ||
| 32 | core::mem::forget(rx_pin); | ||
| 33 | |||
| 34 | let mut can = Can::new(p.CAN1, p.PA11, p.PA12); | ||
| 35 | |||
| 36 | can.modify_config() | ||
| 37 | .set_bit_timing(0x001c0003) // http://www.bittiming.can-wiki.info/ | ||
| 38 | .set_loopback(true) // Receive own frames | ||
| 39 | .set_silent(true); | ||
| 40 | can.modify_filters().enable_bank(0, Mask32::accept_all()); | ||
| 41 | unwrap!(nb::block!(can.enable())); | ||
| 42 | |||
| 43 | let mut i: u8 = 0; | ||
| 44 | loop { | ||
| 45 | let tx_frame = Frame::new_data(unwrap!(StandardId::new(i as _)), [i]); | ||
| 46 | unwrap!(nb::block!(can.transmit(&tx_frame))); | ||
| 47 | while !can.is_transmitter_idle() {} | ||
| 48 | let rx_frame = unwrap!(nb::block!(can.receive())); | ||
| 49 | info!("loopback frame {=u8}", unwrap!(rx_frame.data())[0]); | ||
| 50 | i += 1; | ||
| 51 | } | ||
| 52 | } | ||
