aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-03-17 22:54:40 +0100
committerGitHub <[email protected]>2021-03-17 22:54:40 +0100
commitac74b922c174c3b49b396c7ade827d9909cb48a4 (patch)
tree1853cfdc7d97782edb4c31315e4e23ac8c7ba47d
parent278ce028247160328f22f3d52c25299280ec5d8c (diff)
parentcb8d53e498aa060f2ec37afb2ca88d1409e8cefd (diff)
Merge pull request #20 from xoviat/canbus
add can
-rw-r--r--embassy-stm32f4-examples/.cargo/config2
-rw-r--r--embassy-stm32f4-examples/Cargo.toml1
-rw-r--r--embassy-stm32f4-examples/src/bin/can.rs59
-rw-r--r--embassy-stm32f4/Cargo.toml4
-rw-r--r--embassy-stm32f4/src/can.rs103
-rw-r--r--embassy-stm32f4/src/lib.rs1
6 files changed, 168 insertions, 2 deletions
diff --git a/embassy-stm32f4-examples/.cargo/config b/embassy-stm32f4-examples/.cargo/config
index 3ccca879d..7c1d4dfb6 100644
--- a/embassy-stm32f4-examples/.cargo/config
+++ b/embassy-stm32f4-examples/.cargo/config
@@ -25,4 +25,4 @@ rustflags = [
25] 25]
26 26
27[build] 27[build]
28target = "thumbv7em-none-eabi" 28target = "thumbv7em-none-eabihf"
diff --git a/embassy-stm32f4-examples/Cargo.toml b/embassy-stm32f4-examples/Cargo.toml
index 94f24c75e..60ba44554 100644
--- a/embassy-stm32f4-examples/Cargo.toml
+++ b/embassy-stm32f4-examples/Cargo.toml
@@ -31,3 +31,4 @@ panic-probe = "0.1.0"
31stm32f4xx-hal = { version = "0.8.3", features = ["rt", "stm32f405"], git = "https://github.com/stm32-rs/stm32f4xx-hal.git"} 31stm32f4xx-hal = { version = "0.8.3", features = ["rt", "stm32f405"], git = "https://github.com/stm32-rs/stm32f4xx-hal.git"}
32futures = { version = "0.3.8", default-features = false, features = ["async-await"] } 32futures = { version = "0.3.8", default-features = false, features = ["async-await"] }
33rtt-target = { version = "0.3", features = ["cortex-m"] } 33rtt-target = { version = "0.3", features = ["cortex-m"] }
34bxcan = "0.5.0" \ No newline at end of file
diff --git a/embassy-stm32f4-examples/src/bin/can.rs b/embassy-stm32f4-examples/src/bin/can.rs
new file mode 100644
index 000000000..34ad12b59
--- /dev/null
+++ b/embassy-stm32f4-examples/src/bin/can.rs
@@ -0,0 +1,59 @@
1#![no_std]
2#![no_main]
3#![feature(trait_alias)]
4#![feature(type_alias_impl_trait)]
5
6#[path = "../example_common.rs"]
7mod example_common;
8use example_common::{panic, *};
9
10use bxcan::filter::Mask32;
11use cortex_m_rt::entry;
12use embassy::executor::{task, Executor};
13use embassy::traits::gpio::*;
14use embassy::util::Forever;
15use embassy_stm32f4::{can, exti, interrupt};
16use futures::pin_mut;
17use stm32f4xx_hal::prelude::*;
18use stm32f4xx_hal::{can::Can, stm32};
19
20static EXTI: Forever<exti::ExtiManager> = Forever::new();
21
22#[task]
23async fn run(dp: stm32::Peripherals, _cp: cortex_m::Peripherals) {
24 let gpioa = dp.GPIOA.split();
25
26 let rx = gpioa.pa11.into_alternate_af9();
27 let tx = gpioa.pa12.into_alternate_af9();
28 let mut can = bxcan::Can::new(Can::new(dp.CAN1, (tx, rx)));
29
30 // APB1 (PCLK1): 24MHz, Bit rate: 20kBit/s, Sample Point 87.5%
31 // Value was calculated with http://www.bittiming.can-wiki.info/
32 can.modify_config().set_bit_timing(0x001c_004a);
33 // Configure filters so that can frames can be received.
34 can.modify_filters().enable_bank(0, Mask32::accept_all());
35
36 let mut can = can::Can::new(can, interrupt::take!(CAN1_TX), interrupt::take!(CAN1_RX0));
37
38 let frame = can.receive().await;
39}
40
41static EXECUTOR: Forever<Executor> = Forever::new();
42
43#[entry]
44fn main() -> ! {
45 let dp = stm32::Peripherals::take().unwrap();
46 let cp = cortex_m::peripheral::Peripherals::take().unwrap();
47
48 dp.DBGMCU.cr.modify(|_, w| {
49 w.dbg_sleep().set_bit();
50 w.dbg_standby().set_bit();
51 w.dbg_stop().set_bit()
52 });
53 dp.RCC.ahb1enr.modify(|_, w| w.dma1en().enabled());
54
55 let executor = EXECUTOR.put(Executor::new());
56 executor.run(|spawner| {
57 unwrap!(spawner.spawn(run(dp, cp)));
58 });
59}
diff --git a/embassy-stm32f4/Cargo.toml b/embassy-stm32f4/Cargo.toml
index f9a931b7e..935b4a2b0 100644
--- a/embassy-stm32f4/Cargo.toml
+++ b/embassy-stm32f4/Cargo.toml
@@ -38,4 +38,6 @@ cortex-m-rt = "0.6.13"
38cortex-m = "0.7.1" 38cortex-m = "0.7.1"
39embedded-hal = { version = "0.2.4" } 39embedded-hal = { version = "0.2.4" }
40embedded-dma = { version = "0.1.2" } 40embedded-dma = { version = "0.1.2" }
41stm32f4xx-hal = { version = "0.8.3", features = ["rt"], git = "https://github.com/stm32-rs/stm32f4xx-hal.git"} 41stm32f4xx-hal = { version = "0.8.3", features = ["rt", "can"], git = "https://github.com/stm32-rs/stm32f4xx-hal.git"}
42bxcan = "0.5.0"
43nb = "*"
diff --git a/embassy-stm32f4/src/can.rs b/embassy-stm32f4/src/can.rs
new file mode 100644
index 000000000..a8a3ca164
--- /dev/null
+++ b/embassy-stm32f4/src/can.rs
@@ -0,0 +1,103 @@
1//! Async low power Serial.
2//!
3//! The peripheral is autmatically enabled and disabled as required to save power.
4//! Lowest power consumption can only be guaranteed if the send receive futures
5//! are dropped correctly (e.g. not using `mem::forget()`).
6
7use bxcan;
8use bxcan::Interrupts;
9use core::future::Future;
10use embassy::interrupt::Interrupt;
11use embassy::util::InterruptFuture;
12use embassy::util::Signal;
13use nb;
14use nb::block;
15
16use crate::hal::prelude::*;
17use crate::interrupt;
18
19/// Interface to the Serial peripheral
20pub struct Can<T: Instance> {
21 can: bxcan::Can<T>,
22 tx_int: T::TInterrupt,
23 rx_int: T::RInterrupt,
24}
25
26impl<T: Instance> Can<T> {
27 pub fn new(mut can: bxcan::Can<T>, tx_int: T::TInterrupt, rx_int: T::RInterrupt) -> Self {
28 // Sync to the bus and start normal operation.
29 can.enable_interrupts(
30 Interrupts::TRANSMIT_MAILBOX_EMPTY | Interrupts::FIFO0_MESSAGE_PENDING,
31 );
32 block!(can.enable()).unwrap();
33
34 Can {
35 can: can,
36 tx_int: tx_int,
37 rx_int: rx_int,
38 }
39 }
40
41 /// Sends can frame.
42 ///
43 /// This method async-blocks until the frame is transmitted.
44 pub fn transmit<'a>(&'a mut self, frame: &'a bxcan::Frame) -> impl Future<Output = ()> + 'a {
45 async move {
46 let fut = InterruptFuture::new(&mut self.tx_int);
47 self.can.transmit(frame);
48
49 fut.await;
50 }
51 }
52
53 /// Receive can frame.
54 ///
55 /// This method async-blocks until the frame is received.
56 pub fn receive<'a>(&'a mut self) -> impl Future<Output = (bxcan::Frame)> + 'a {
57 async move {
58 let mut frame: Option<bxcan::Frame> = None;
59
60 loop {
61 let fut = InterruptFuture::new(&mut self.rx_int);
62 frame = match self.can.receive() {
63 Ok(frame) => Some(frame),
64 Err(nb::Error::WouldBlock) => None,
65 Err(nb::Error::Other(_)) => None, // Ignore overrun errors.
66 };
67 if frame.is_some() {
68 break;
69 }
70 fut.await;
71 }
72
73 frame.unwrap()
74 }
75 }
76}
77
78mod private {
79 pub trait Sealed {}
80}
81
82pub trait Instance: bxcan::Instance + private::Sealed {
83 type TInterrupt: Interrupt;
84 type RInterrupt: Interrupt;
85}
86
87macro_rules! can {
88 ($($can:ident => ($tint:ident, $rint:ident),)+) => {
89 $(
90 impl private::Sealed for crate::hal::can::Can<crate::pac::$can> {}
91 impl Instance for crate::hal::can::Can<crate::pac::$can> {
92 type TInterrupt = interrupt::$tint;
93 type RInterrupt = interrupt::$rint;
94 }
95 )+
96 }
97}
98
99#[cfg(any(feature = "stm32f405",))]
100can! {
101 CAN1 => (CAN1_TX, CAN1_RX0),
102 CAN2 => (CAN2_TX, CAN2_RX0),
103}
diff --git a/embassy-stm32f4/src/lib.rs b/embassy-stm32f4/src/lib.rs
index e5e82cd5e..19375d962 100644
--- a/embassy-stm32f4/src/lib.rs
+++ b/embassy-stm32f4/src/lib.rs
@@ -313,6 +313,7 @@ pub use stm32f4xx_hal::stm32 as pac;
313// This mod MUST go first, so that the others see its macros. 313// This mod MUST go first, so that the others see its macros.
314pub(crate) mod fmt; 314pub(crate) mod fmt;
315 315
316pub mod can;
316pub mod exti; 317pub mod exti;
317pub mod interrupt; 318pub mod interrupt;
318pub mod qei; 319pub mod qei;