#![no_main]
#![no_std]
use bxcan::{
filter::{ListEntry16, ListEntry32, Mask16},
ExtendedId, Fifo, Frame, StandardId,
};
use panic_halt as _;
use cortex_m_rt::entry;
use nb::block;
use stm32f1xx_hal::{can::Can, pac, prelude::*};
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let mut flash = dp.FLASH.constrain();
let rcc = dp.RCC.constrain();
rcc.cfgr.use_hse(8.MHz()).freeze(&mut flash.acr);
#[cfg(not(feature = "connectivity"))]
let can = Can::new(dp.CAN1, dp.USB);
#[cfg(feature = "connectivity")]
let can = Can::new(dp.CAN1);
let mut can = bxcan::Can::builder(can)
.set_bit_timing(0x001c_0000)
.set_loopback(true)
.set_silent(true)
.leave_disabled();
let mut filters = can.modify_filters();
assert!(filters.num_banks() > 3);
filters.enable_bank(
0,
Fifo::Fifo0,
[
Mask16::frames_with_std_id(StandardId::new(0).unwrap(), StandardId::new(1).unwrap()),
Mask16::frames_with_std_id(StandardId::new(0).unwrap(), StandardId::new(2).unwrap()),
],
);
filters.enable_bank(
1,
Fifo::Fifo0,
[
ListEntry32::data_frames_with_id(ExtendedId::new(4).unwrap()),
ListEntry32::data_frames_with_id(ExtendedId::new(5).unwrap()),
],
);
filters.enable_bank(
2,
Fifo::Fifo0,
[
ListEntry16::data_frames_with_id(StandardId::new(8).unwrap()),
ListEntry16::data_frames_with_id(StandardId::new(9).unwrap()),
ListEntry16::data_frames_with_id(StandardId::new(10).unwrap()),
ListEntry16::data_frames_with_id(StandardId::new(11).unwrap()),
],
);
drop(filters);
block!(can.enable_non_blocking()).ok();
for &id in &[0, 1, 2, 8, 9, 10, 11] {
let frame_tx = Frame::new_data(StandardId::new(id).unwrap(), [id as u8]);
block!(can.transmit(&frame_tx)).unwrap();
let frame_rx = block!(can.receive()).unwrap();
assert_eq!(frame_tx, frame_rx);
}
for &id in &[4, 5] {
let frame_tx = Frame::new_data(ExtendedId::new(id).unwrap(), [id as u8]);
block!(can.transmit(&frame_tx)).unwrap();
let frame_rx = block!(can.receive()).unwrap();
assert_eq!(frame_tx, frame_rx);
}
for &id in &[3, 6, 7, 12] {
let frame_tx = Frame::new_data(ExtendedId::new(id).unwrap(), [id as u8]);
block!(can.transmit(&frame_tx)).unwrap();
while !can.is_transmitter_idle() {}
assert!(can.receive().is_err());
}
let mut gpiob = dp.GPIOB.split();
let mut led = gpiob.pb9.into_push_pull_output(&mut gpiob.crh);
led.set_high();
loop {}
}