embassy/examples/stm32f4/src/bin/can.rs

48 lines
1.4 KiB
Rust
Raw Normal View History

2021-08-06 00:08:24 +02:00
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt_rtt as _; // global logger
use panic_probe as _;
2021-08-06 00:08:24 +02:00
use cortex_m_rt::entry;
use defmt::*;
use embassy_stm32::can::bxcan::filter::Mask32;
use embassy_stm32::can::bxcan::{Frame, StandardId};
use embassy_stm32::can::Can;
2021-08-09 15:59:05 +02:00
use embassy_stm32::gpio::{Input, Pull};
2021-08-06 00:08:24 +02:00
#[entry]
fn main() -> ! {
info!("Hello World!");
2021-08-09 15:59:05 +02:00
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);
2021-08-06 00:08:24 +02:00
let mut can = Can::new(p.CAN1, p.PA11, p.PA12);
2021-11-15 18:00:26 +01:00
can.modify_filters().enable_bank(0, Mask32::accept_all());
2021-08-09 15:59:05 +02:00
can.modify_config()
.set_bit_timing(0x001c0003) // http://www.bittiming.can-wiki.info/
.set_loopback(true) // Receive own frames
2021-11-15 18:00:26 +01:00
.set_silent(true)
.enable();
2021-08-06 00:08:24 +02:00
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;
}
}