add can driver

This commit is contained in:
xoviat
2021-03-15 17:45:45 -05:00
parent 3fcecd076b
commit 93625c3f6a
5 changed files with 171 additions and 0 deletions

View File

@ -31,3 +31,4 @@ panic-probe = "0.1.0"
stm32f4xx-hal = { version = "0.8.3", features = ["rt", "stm32f405"], git = "https://github.com/stm32-rs/stm32f4xx-hal.git"}
futures = { version = "0.3.8", default-features = false, features = ["async-await"] }
rtt-target = { version = "0.3", features = ["cortex-m"] }
bxcan = "0.5.0"

View File

@ -0,0 +1,64 @@
#![no_std]
#![no_main]
#![feature(trait_alias)]
#![feature(type_alias_impl_trait)]
#[path = "../example_common.rs"]
mod example_common;
use example_common::{panic, *};
use cortex_m_rt::entry;
use embassy::executor::{task, Executor};
use embassy::traits::gpio::*;
use embassy::util::Forever;
use embassy_stm32f4::can;
use embassy_stm32f4::exti;
use embassy_stm32f4::interrupt;
use futures::pin_mut;
use stm32f4xx_hal::prelude::*;
use stm32f4xx_hal::stm32;
static EXTI: Forever<exti::ExtiManager> = Forever::new();
#[task]
async fn run(dp: stm32::Peripherals, _cp: cortex_m::Peripherals) {
let rx = gpioa.pa11.into_alternate_af9();
let tx = gpioa.pa12.into_alternate_af9();
let mut can = bxcan::Can::new(Can::new(dp.CAN1, (tx, rx)));
// APB1 (PCLK1): 24MHz, Bit rate: 20kBit/s, Sample Point 87.5%
// Value was calculated with http://www.bittiming.can-wiki.info/
can.modify_config().set_bit_timing(0x001c_004a);
// Configure filters so that can frames can be received.
can.modify_filters().enable_bank(0, Mask32::accept_all());
block!(can.enable()).unwrap();
let can = can::Can::new(
can,
interrupt::take!(CAN1_TX),
interrupt::take!(CAN1_RX0),
));
let frame = can.receive().await;
}
static EXECUTOR: Forever<Executor> = Forever::new();
#[entry]
fn main() -> ! {
let dp = stm32::Peripherals::take().unwrap();
let cp = cortex_m::peripheral::Peripherals::take().unwrap();
dp.DBGMCU.cr.modify(|_, w| {
w.dbg_sleep().set_bit();
w.dbg_standby().set_bit();
w.dbg_stop().set_bit()
});
dp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled());
let executor = EXECUTOR.put(Executor::new());
executor.run(|spawner| {
unwrap!(spawner.spawn(run(dp, cp)));
});
}