embassy/embassy-stm32/src/ipcc.rs

280 lines
8.6 KiB
Rust
Raw Normal View History

2023-06-17 19:00:33 +02:00
use core::future::poll_fn;
use core::sync::atomic::{compiler_fence, Ordering};
2023-06-17 19:00:33 +02:00
use core::task::Poll;
2023-05-27 22:05:07 +02:00
use self::sealed::Instance;
2023-06-17 19:00:33 +02:00
use crate::interrupt;
use crate::interrupt::typelevel::Interrupt;
2023-04-27 17:03:22 +02:00
use crate::peripherals::IPCC;
2023-04-27 17:22:41 +02:00
use crate::rcc::sealed::RccPeripheral;
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
/// Interrupt handler.
pub struct ReceiveInterruptHandler {}
impl interrupt::typelevel::Handler<interrupt::typelevel::IPCC_C1_RX> for ReceiveInterruptHandler {
unsafe fn on_interrupt() {
let regs = IPCC::regs();
let channels = [
IpccChannel::Channel1,
IpccChannel::Channel2,
IpccChannel::Channel3,
IpccChannel::Channel4,
IpccChannel::Channel5,
IpccChannel::Channel6,
];
// Status register gives channel occupied status. For rx, use cpu1.
2023-06-19 03:07:26 +02:00
let sr = regs.cpu(1).sr().read();
2023-06-17 19:00:33 +02:00
regs.cpu(0).mr().modify(|w| {
for channel in channels {
if sr.chf(channel as usize) {
// If bit is set to 1 then interrupt is disabled; we want to disable the interrupt
w.set_chom(channel as usize, true);
// There shouldn't be a race because the channel is masked only if the interrupt has fired
IPCC::state().rx_waker_for(channel).wake();
}
}
})
}
}
pub struct TransmitInterruptHandler {}
impl interrupt::typelevel::Handler<interrupt::typelevel::IPCC_C1_TX> for TransmitInterruptHandler {
unsafe fn on_interrupt() {
let regs = IPCC::regs();
let channels = [
IpccChannel::Channel1,
IpccChannel::Channel2,
IpccChannel::Channel3,
IpccChannel::Channel4,
IpccChannel::Channel5,
IpccChannel::Channel6,
];
// Status register gives channel occupied status. For tx, use cpu0.
2023-06-19 03:07:26 +02:00
let sr = regs.cpu(0).sr().read();
2023-06-17 19:00:33 +02:00
regs.cpu(0).mr().modify(|w| {
for channel in channels {
if !sr.chf(channel as usize) {
// If bit is set to 1 then interrupt is disabled; we want to disable the interrupt
w.set_chfm(channel as usize, true);
// There shouldn't be a race because the channel is masked only if the interrupt has fired
IPCC::state().tx_waker_for(channel).wake();
}
}
});
}
}
2023-04-27 17:03:22 +02:00
#[non_exhaustive]
#[derive(Clone, Copy, Default)]
pub struct Config {
// TODO: add IPCC peripheral configuration, if any, here
// reserved for future use
}
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub enum IpccChannel {
Channel1 = 0,
Channel2 = 1,
Channel3 = 2,
Channel4 = 3,
Channel5 = 4,
Channel6 = 5,
}
2023-05-27 22:05:07 +02:00
pub struct Ipcc;
2023-04-27 17:03:22 +02:00
2023-05-26 10:56:55 +02:00
impl Ipcc {
2023-05-27 22:05:07 +02:00
pub fn enable(_config: Config) {
IPCC::reset_and_enable();
2023-04-27 17:03:22 +02:00
IPCC::set_cpu2(true);
2023-06-19 03:07:26 +02:00
_configure_pwr();
2023-04-27 17:03:22 +02:00
let regs = IPCC::regs();
2023-06-19 03:07:26 +02:00
regs.cpu(0).cr().modify(|w| {
w.set_rxoie(true);
w.set_txfie(true);
});
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
// enable interrupts
crate::interrupt::typelevel::IPCC_C1_RX::unpend();
crate::interrupt::typelevel::IPCC_C1_TX::unpend();
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
unsafe { crate::interrupt::typelevel::IPCC_C1_RX::enable() };
unsafe { crate::interrupt::typelevel::IPCC_C1_TX::enable() };
2023-04-27 17:03:22 +02:00
}
2023-06-17 19:00:33 +02:00
/// Send data to an IPCC channel. The closure is called to write the data when appropriate.
pub async fn send(channel: IpccChannel, f: impl FnOnce()) {
2023-04-27 17:03:22 +02:00
let regs = IPCC::regs();
2023-06-17 19:00:33 +02:00
Self::flush(channel).await;
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
f();
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
compiler_fence(Ordering::SeqCst);
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
trace!("ipcc: ch {}: send data", channel as u8);
2023-06-19 03:07:26 +02:00
regs.cpu(0).scr().write(|w| w.set_chs(channel as usize, true));
2023-04-27 17:03:22 +02:00
}
2023-06-17 19:00:33 +02:00
/// Wait for the tx channel to become clear
pub async fn flush(channel: IpccChannel) {
2023-04-27 17:03:22 +02:00
let regs = IPCC::regs();
2023-06-17 19:00:33 +02:00
// This is a race, but is nice for debugging
2023-06-19 03:07:26 +02:00
if regs.cpu(0).sr().read().chf(channel as usize) {
2023-06-18 17:10:05 +02:00
trace!("ipcc: ch {}: wait for tx free", channel as u8);
2023-06-17 19:00:33 +02:00
}
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
poll_fn(|cx| {
IPCC::state().tx_waker_for(channel).register(cx.waker());
// If bit is set to 1 then interrupt is disabled; we want to enable the interrupt
2023-06-19 03:07:26 +02:00
regs.cpu(0).mr().modify(|w| w.set_chfm(channel as usize, false));
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
compiler_fence(Ordering::SeqCst);
2023-04-27 17:03:22 +02:00
2023-06-19 03:07:26 +02:00
if !regs.cpu(0).sr().read().chf(channel as usize) {
2023-06-17 19:00:33 +02:00
// If bit is set to 1 then interrupt is disabled; we want to disable the interrupt
2023-06-19 03:07:26 +02:00
regs.cpu(0).mr().modify(|w| w.set_chfm(channel as usize, true));
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
Poll::Ready(())
} else {
Poll::Pending
}
})
.await;
2023-04-27 17:03:22 +02:00
}
2023-06-17 19:00:33 +02:00
/// Receive data from an IPCC channel. The closure is called to read the data when appropriate.
pub async fn receive<R>(channel: IpccChannel, mut f: impl FnMut() -> Option<R>) -> R {
2023-04-27 17:03:22 +02:00
let regs = IPCC::regs();
2023-06-17 19:00:33 +02:00
loop {
// This is a race, but is nice for debugging
2023-06-19 03:07:26 +02:00
if !regs.cpu(1).sr().read().chf(channel as usize) {
2023-06-18 17:10:05 +02:00
trace!("ipcc: ch {}: wait for rx occupied", channel as u8);
2023-06-17 19:00:33 +02:00
}
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
poll_fn(|cx| {
IPCC::state().rx_waker_for(channel).register(cx.waker());
// If bit is set to 1 then interrupt is disabled; we want to enable the interrupt
2023-06-19 03:07:26 +02:00
regs.cpu(0).mr().modify(|w| w.set_chom(channel as usize, false));
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
compiler_fence(Ordering::SeqCst);
2023-04-27 17:03:22 +02:00
2023-06-19 03:07:26 +02:00
if regs.cpu(1).sr().read().chf(channel as usize) {
2023-06-17 19:00:33 +02:00
// If bit is set to 1 then interrupt is disabled; we want to disable the interrupt
2023-06-19 03:07:26 +02:00
regs.cpu(0).mr().modify(|w| w.set_chfm(channel as usize, true));
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
Poll::Ready(())
} else {
Poll::Pending
}
})
.await;
2023-04-27 17:03:22 +02:00
2023-06-18 17:10:05 +02:00
trace!("ipcc: ch {}: read data", channel as u8);
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
match f() {
Some(ret) => return ret,
None => {}
}
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
trace!("ipcc: ch {}: clear rx", channel as u8);
compiler_fence(Ordering::SeqCst);
// If the channel is clear and the read function returns none, fetch more data
2023-06-19 03:07:26 +02:00
regs.cpu(0).scr().write(|w| w.set_chc(channel as usize, true));
2023-06-17 19:00:33 +02:00
}
}
}
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
impl sealed::Instance for crate::peripherals::IPCC {
fn regs() -> crate::pac::ipcc::Ipcc {
crate::pac::IPCC
2023-04-27 17:03:22 +02:00
}
2023-06-17 19:00:33 +02:00
fn set_cpu2(enabled: bool) {
2023-06-19 03:07:26 +02:00
crate::pac::PWR.cr4().modify(|w| w.set_c2boot(enabled));
2023-06-17 19:00:33 +02:00
}
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
fn state() -> &'static self::sealed::State {
static STATE: self::sealed::State = self::sealed::State::new();
&STATE
2023-04-27 17:03:22 +02:00
}
2023-06-17 19:00:33 +02:00
}
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
pub(crate) mod sealed {
use embassy_sync::waitqueue::AtomicWaker;
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
use super::*;
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
pub struct State {
rx_wakers: [AtomicWaker; 6],
tx_wakers: [AtomicWaker; 6],
2023-04-27 17:03:22 +02:00
}
2023-05-15 11:25:02 +02:00
2023-06-17 19:00:33 +02:00
impl State {
pub const fn new() -> Self {
const WAKER: AtomicWaker = AtomicWaker::new();
2023-04-27 17:03:22 +02:00
2023-06-17 19:00:33 +02:00
Self {
rx_wakers: [WAKER; 6],
tx_wakers: [WAKER; 6],
}
}
2023-06-18 17:10:05 +02:00
pub const fn rx_waker_for(&self, channel: IpccChannel) -> &AtomicWaker {
2023-06-17 19:00:33 +02:00
match channel {
IpccChannel::Channel1 => &self.rx_wakers[0],
IpccChannel::Channel2 => &self.rx_wakers[1],
IpccChannel::Channel3 => &self.rx_wakers[2],
IpccChannel::Channel4 => &self.rx_wakers[3],
IpccChannel::Channel5 => &self.rx_wakers[4],
IpccChannel::Channel6 => &self.rx_wakers[5],
}
}
2023-06-18 17:10:05 +02:00
pub const fn tx_waker_for(&self, channel: IpccChannel) -> &AtomicWaker {
2023-06-17 19:00:33 +02:00
match channel {
IpccChannel::Channel1 => &self.tx_wakers[0],
IpccChannel::Channel2 => &self.tx_wakers[1],
IpccChannel::Channel3 => &self.tx_wakers[2],
IpccChannel::Channel4 => &self.tx_wakers[3],
IpccChannel::Channel5 => &self.tx_wakers[4],
IpccChannel::Channel6 => &self.tx_wakers[5],
}
}
2023-04-27 17:03:22 +02:00
}
2023-06-17 19:00:33 +02:00
pub trait Instance: crate::rcc::RccPeripheral {
fn regs() -> crate::pac::ipcc::Ipcc;
fn set_cpu2(enabled: bool);
fn state() -> &'static State;
2023-04-27 17:03:22 +02:00
}
}
2023-06-19 03:07:26 +02:00
fn _configure_pwr() {
2023-07-24 00:01:34 +02:00
// TODO: move the rest of this to rcc
2023-04-27 17:03:22 +02:00
let rcc = crate::pac::RCC;
// TODO: required
2023-04-27 17:03:22 +02:00
// set RF wake-up clock = LSE
rcc.csr().modify(|w| w.set_rfwkpsel(0b01));
// set LPTIM1 & LPTIM2 clock source
rcc.ccipr().modify(|w| {
w.set_lptim1sel(0b00); // PCLK
w.set_lptim2sel(0b00); // PCLK
});
2023-04-27 17:03:22 +02:00
}