2021-03-15 23:45:45 +01:00
|
|
|
//! Async low power Serial.
|
|
|
|
//!
|
|
|
|
//! The peripheral is autmatically enabled and disabled as required to save power.
|
|
|
|
//! Lowest power consumption can only be guaranteed if the send receive futures
|
|
|
|
//! are dropped correctly (e.g. not using `mem::forget()`).
|
|
|
|
|
|
|
|
use bxcan;
|
|
|
|
use bxcan::Interrupts;
|
|
|
|
use core::future::Future;
|
|
|
|
use embassy::interrupt::Interrupt;
|
|
|
|
use embassy::util::InterruptFuture;
|
|
|
|
use nb;
|
|
|
|
use nb::block;
|
|
|
|
|
|
|
|
use crate::interrupt;
|
|
|
|
|
|
|
|
/// Interface to the Serial peripheral
|
|
|
|
pub struct Can<T: Instance> {
|
|
|
|
can: bxcan::Can<T>,
|
|
|
|
tx_int: T::TInterrupt,
|
|
|
|
rx_int: T::RInterrupt,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Instance> Can<T> {
|
|
|
|
pub fn new(mut can: bxcan::Can<T>, tx_int: T::TInterrupt, rx_int: T::RInterrupt) -> Self {
|
|
|
|
// Sync to the bus and start normal operation.
|
|
|
|
can.enable_interrupts(
|
|
|
|
Interrupts::TRANSMIT_MAILBOX_EMPTY | Interrupts::FIFO0_MESSAGE_PENDING,
|
|
|
|
);
|
|
|
|
block!(can.enable()).unwrap();
|
|
|
|
|
|
|
|
Can {
|
|
|
|
can: can,
|
|
|
|
tx_int: tx_int,
|
|
|
|
rx_int: rx_int,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sends can frame.
|
|
|
|
///
|
|
|
|
/// This method async-blocks until the frame is transmitted.
|
|
|
|
pub fn transmit<'a>(&'a mut self, frame: &'a bxcan::Frame) -> impl Future<Output = ()> + 'a {
|
|
|
|
async move {
|
|
|
|
let fut = InterruptFuture::new(&mut self.tx_int);
|
2021-03-19 02:23:09 +01:00
|
|
|
// Infallible
|
|
|
|
self.can.transmit(frame).unwrap();
|
2021-03-15 23:45:45 +01:00
|
|
|
|
|
|
|
fut.await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Receive can frame.
|
|
|
|
///
|
|
|
|
/// This method async-blocks until the frame is received.
|
2021-03-18 03:21:48 +01:00
|
|
|
pub fn receive<'a>(&'a mut self) -> impl Future<Output = bxcan::Frame> + 'a {
|
2021-03-15 23:45:45 +01:00
|
|
|
async move {
|
2021-03-18 03:21:48 +01:00
|
|
|
let mut frame: Option<bxcan::Frame>;
|
2021-03-15 23:45:45 +01:00
|
|
|
|
|
|
|
loop {
|
|
|
|
let fut = InterruptFuture::new(&mut self.rx_int);
|
|
|
|
frame = match self.can.receive() {
|
|
|
|
Ok(frame) => Some(frame),
|
|
|
|
Err(nb::Error::WouldBlock) => None,
|
|
|
|
Err(nb::Error::Other(_)) => None, // Ignore overrun errors.
|
|
|
|
};
|
|
|
|
if frame.is_some() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fut.await;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame.unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod private {
|
|
|
|
pub trait Sealed {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Instance: bxcan::Instance + private::Sealed {
|
|
|
|
type TInterrupt: Interrupt;
|
|
|
|
type RInterrupt: Interrupt;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! can {
|
|
|
|
($($can:ident => ($tint:ident, $rint:ident),)+) => {
|
|
|
|
$(
|
2021-03-16 00:00:00 +01:00
|
|
|
impl private::Sealed for crate::hal::can::Can<crate::pac::$can> {}
|
|
|
|
impl Instance for crate::hal::can::Can<crate::pac::$can> {
|
2021-03-15 23:45:45 +01:00
|
|
|
type TInterrupt = interrupt::$tint;
|
|
|
|
type RInterrupt = interrupt::$rint;
|
|
|
|
}
|
|
|
|
)+
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-27 01:34:52 +01:00
|
|
|
#[cfg(any(
|
|
|
|
feature = "stm32f401",
|
|
|
|
feature = "stm32f405",
|
|
|
|
feature = "stm32f407",
|
|
|
|
feature = "stm32f410",
|
|
|
|
feature = "stm32f411",
|
|
|
|
feature = "stm32f412",
|
|
|
|
feature = "stm32f413",
|
|
|
|
feature = "stm32f415",
|
|
|
|
feature = "stm32f417",
|
|
|
|
feature = "stm32f423",
|
|
|
|
feature = "stm32f427",
|
|
|
|
feature = "stm32f429",
|
|
|
|
feature = "stm32f437",
|
|
|
|
feature = "stm32f439",
|
|
|
|
feature = "stm32f446",
|
|
|
|
feature = "stm32f469",
|
|
|
|
feature = "stm32f479",
|
|
|
|
))]
|
2021-03-15 23:45:45 +01:00
|
|
|
can! {
|
|
|
|
CAN1 => (CAN1_TX, CAN1_RX0),
|
|
|
|
CAN2 => (CAN2_TX, CAN2_RX0),
|
|
|
|
}
|