#![no_std]
#![no_main]
use core::panic::PanicInfo;
use bxcan::{Frame, StandardId, filter::Mask32};
use cortex_m::delay::Delay;
use cortex_m_rt::entry; use defmt_rtt as _;
use hal::{
self,
can::Can,
clocks::{self, ApbPrescaler, Clocks, InputSrc, PllSrc, Pllp},
gpio::{OutputType, Pin, PinMode, Port},
pac,
};
use nb::block;
#[entry]
fn main() -> ! {
let cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();
let mut rcc = dp.RCC;
hal::debug_workaround();
let clock_cfg = Clocks {
input_src: InputSrc::Pll(PllSrc::Hse(8_000_000)),
pllm: 8,
plln: 180,
pllp: Pllp::Div2,
apb1_prescaler: ApbPrescaler::Div2,
..Default::default()
};
clock_cfg.setup().unwrap();
let mut can = {
let mut _rx = Pin::new(Port::B, 8, PinMode::Alt(9));
let mut _tx = Pin::new(Port::B, 9, PinMode::Alt(9));
_rx.output_type(OutputType::PushPull);
_tx.output_type(OutputType::PushPull);
let can = Can::new(dp.CAN1, &mut rcc);
bxcan::Can::builder(can)
.set_bit_timing(0x001b0002)
.enable()
};
defmt::info!("CAN enabled!");
let mut test: [u8; 8] = [0; 8];
let mut count: u8 = 0;
let id: u16 = 0x500;
test[1] = 1;
test[2] = 2;
test[3] = 3;
test[4] = 4;
test[5] = 5;
test[6] = 6;
test[7] = 7;
let test_frame = Frame::new_data(StandardId::new(id).unwrap(), test);
loop {
test[0] = count;
let test_frame = Frame::new_data(StandardId::new(id).unwrap(), test);
block!(can.transmit(&test_frame)).unwrap();
if count < 255 {
count += 1;
} else {
count = 0;
}
}
}
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
cortex_m::asm::udf()
}