2022-09-22 16:42:49 +02:00
|
|
|
use core::future::poll_fn;
|
2023-05-25 00:29:56 +02:00
|
|
|
use core::marker::PhantomData;
|
2021-11-10 14:47:16 +01:00
|
|
|
use core::task::Poll;
|
|
|
|
|
2023-07-28 13:23:22 +02:00
|
|
|
use embassy_hal_internal::{into_ref, PeripheralRef};
|
2022-08-22 21:46:09 +02:00
|
|
|
use embassy_sync::waitqueue::AtomicWaker;
|
2021-11-10 14:47:16 +01:00
|
|
|
|
2023-04-17 00:04:54 +02:00
|
|
|
use crate::dma::Transfer;
|
2022-06-12 22:15:44 +02:00
|
|
|
use crate::gpio::sealed::AFType;
|
|
|
|
use crate::gpio::Speed;
|
2023-06-08 16:08:40 +02:00
|
|
|
use crate::interrupt::typelevel::Interrupt;
|
2023-05-25 00:29:56 +02:00
|
|
|
use crate::{interrupt, Peripheral};
|
|
|
|
|
|
|
|
/// Interrupt handler.
|
|
|
|
pub struct InterruptHandler<T: Instance> {
|
|
|
|
_phantom: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
2023-06-08 16:08:40 +02:00
|
|
|
impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandler<T> {
|
2023-05-25 00:29:56 +02:00
|
|
|
unsafe fn on_interrupt() {
|
|
|
|
let ris = crate::pac::DCMI.ris().read();
|
|
|
|
if ris.err_ris() {
|
|
|
|
trace!("DCMI IRQ: Error.");
|
|
|
|
crate::pac::DCMI.ier().modify(|ier| ier.set_err_ie(false));
|
|
|
|
}
|
|
|
|
if ris.ovr_ris() {
|
|
|
|
trace!("DCMI IRQ: Overrun.");
|
|
|
|
crate::pac::DCMI.ier().modify(|ier| ier.set_ovr_ie(false));
|
|
|
|
}
|
|
|
|
if ris.frame_ris() {
|
|
|
|
trace!("DCMI IRQ: Frame captured.");
|
|
|
|
crate::pac::DCMI.ier().modify(|ier| ier.set_frame_ie(false));
|
|
|
|
}
|
|
|
|
STATE.waker.wake();
|
|
|
|
}
|
|
|
|
}
|
2022-02-10 02:34:59 +01:00
|
|
|
|
2021-11-10 14:47:16 +01:00
|
|
|
/// The level on the VSync pin when the data is not valid on the parallel interface.
|
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
|
|
|
pub enum VSyncDataInvalidLevel {
|
|
|
|
Low,
|
|
|
|
High,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The level on the VSync pin when the data is not valid on the parallel interface.
|
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
|
|
|
pub enum HSyncDataInvalidLevel {
|
|
|
|
Low,
|
|
|
|
High,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
|
|
|
pub enum PixelClockPolarity {
|
|
|
|
RisingEdge,
|
|
|
|
FallingEdge,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct State {
|
|
|
|
waker: AtomicWaker,
|
|
|
|
}
|
|
|
|
impl State {
|
|
|
|
const fn new() -> State {
|
|
|
|
State {
|
|
|
|
waker: AtomicWaker::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static STATE: State = State::new();
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub enum Error {
|
|
|
|
Overrun,
|
|
|
|
PeripheralError,
|
|
|
|
}
|
|
|
|
|
2022-02-10 02:34:59 +01:00
|
|
|
#[non_exhaustive]
|
|
|
|
pub struct Config {
|
|
|
|
pub vsync_level: VSyncDataInvalidLevel,
|
|
|
|
pub hsync_level: HSyncDataInvalidLevel,
|
|
|
|
pub pixclk_polarity: PixelClockPolarity,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
vsync_level: VSyncDataInvalidLevel::High,
|
|
|
|
hsync_level: HSyncDataInvalidLevel::Low,
|
|
|
|
pixclk_polarity: PixelClockPolarity::RisingEdge,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-10 21:38:03 +01:00
|
|
|
macro_rules! config_pins {
|
|
|
|
($($pin:ident),*) => {
|
2022-07-23 14:00:19 +02:00
|
|
|
into_ref!($($pin),*);
|
2023-06-19 03:07:26 +02:00
|
|
|
critical_section::with(|_| {
|
2022-02-10 21:38:03 +01:00
|
|
|
$(
|
|
|
|
$pin.set_as_af($pin.af_num(), AFType::Input);
|
|
|
|
$pin.set_speed(Speed::VeryHigh);
|
|
|
|
)*
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Dcmi<'d, T: Instance, Dma: FrameDma<T>> {
|
2022-07-23 14:00:19 +02:00
|
|
|
inner: PeripheralRef<'d, T>,
|
|
|
|
dma: PeripheralRef<'d, Dma>,
|
2021-11-10 14:47:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T, Dma> Dcmi<'d, T, Dma>
|
|
|
|
where
|
|
|
|
T: Instance,
|
2022-02-10 21:38:03 +01:00
|
|
|
Dma: FrameDma<T>,
|
2021-11-10 14:47:16 +01:00
|
|
|
{
|
2022-02-10 02:34:59 +01:00
|
|
|
pub fn new_8bit(
|
2022-07-23 14:00:19 +02:00
|
|
|
peri: impl Peripheral<P = T> + 'd,
|
|
|
|
dma: impl Peripheral<P = Dma> + 'd,
|
2023-06-08 16:08:40 +02:00
|
|
|
_irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
|
2022-07-23 14:00:19 +02:00
|
|
|
d0: impl Peripheral<P = impl D0Pin<T>> + 'd,
|
|
|
|
d1: impl Peripheral<P = impl D1Pin<T>> + 'd,
|
|
|
|
d2: impl Peripheral<P = impl D2Pin<T>> + 'd,
|
|
|
|
d3: impl Peripheral<P = impl D3Pin<T>> + 'd,
|
|
|
|
d4: impl Peripheral<P = impl D4Pin<T>> + 'd,
|
|
|
|
d5: impl Peripheral<P = impl D5Pin<T>> + 'd,
|
|
|
|
d6: impl Peripheral<P = impl D6Pin<T>> + 'd,
|
|
|
|
d7: impl Peripheral<P = impl D7Pin<T>> + 'd,
|
|
|
|
v_sync: impl Peripheral<P = impl VSyncPin<T>> + 'd,
|
|
|
|
h_sync: impl Peripheral<P = impl HSyncPin<T>> + 'd,
|
|
|
|
pixclk: impl Peripheral<P = impl PixClkPin<T>> + 'd,
|
2022-02-10 02:34:59 +01:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2023-05-25 00:29:56 +02:00
|
|
|
into_ref!(peri, dma);
|
2022-02-10 21:38:03 +01:00
|
|
|
config_pins!(d0, d1, d2, d3, d4, d5, d6, d7);
|
|
|
|
config_pins!(v_sync, h_sync, pixclk);
|
2022-02-10 02:34:59 +01:00
|
|
|
|
2023-05-25 00:29:56 +02:00
|
|
|
Self::new_inner(peri, dma, config, false, 0b00)
|
2022-02-10 02:34:59 +01:00
|
|
|
}
|
2022-02-10 21:38:03 +01:00
|
|
|
|
2022-02-10 02:34:59 +01:00
|
|
|
pub fn new_10bit(
|
2022-07-23 14:00:19 +02:00
|
|
|
peri: impl Peripheral<P = T> + 'd,
|
|
|
|
dma: impl Peripheral<P = Dma> + 'd,
|
2023-06-08 16:08:40 +02:00
|
|
|
_irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
|
2022-07-23 14:00:19 +02:00
|
|
|
d0: impl Peripheral<P = impl D0Pin<T>> + 'd,
|
|
|
|
d1: impl Peripheral<P = impl D1Pin<T>> + 'd,
|
|
|
|
d2: impl Peripheral<P = impl D2Pin<T>> + 'd,
|
|
|
|
d3: impl Peripheral<P = impl D3Pin<T>> + 'd,
|
|
|
|
d4: impl Peripheral<P = impl D4Pin<T>> + 'd,
|
|
|
|
d5: impl Peripheral<P = impl D5Pin<T>> + 'd,
|
|
|
|
d6: impl Peripheral<P = impl D6Pin<T>> + 'd,
|
|
|
|
d7: impl Peripheral<P = impl D7Pin<T>> + 'd,
|
|
|
|
d8: impl Peripheral<P = impl D8Pin<T>> + 'd,
|
|
|
|
d9: impl Peripheral<P = impl D9Pin<T>> + 'd,
|
|
|
|
v_sync: impl Peripheral<P = impl VSyncPin<T>> + 'd,
|
|
|
|
h_sync: impl Peripheral<P = impl HSyncPin<T>> + 'd,
|
|
|
|
pixclk: impl Peripheral<P = impl PixClkPin<T>> + 'd,
|
2022-02-10 02:34:59 +01:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2023-05-25 00:29:56 +02:00
|
|
|
into_ref!(peri, dma);
|
2022-02-10 21:38:03 +01:00
|
|
|
config_pins!(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9);
|
|
|
|
config_pins!(v_sync, h_sync, pixclk);
|
2022-02-10 02:34:59 +01:00
|
|
|
|
2023-05-25 00:29:56 +02:00
|
|
|
Self::new_inner(peri, dma, config, false, 0b01)
|
2022-02-10 02:34:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_12bit(
|
2022-07-23 14:00:19 +02:00
|
|
|
peri: impl Peripheral<P = T> + 'd,
|
|
|
|
dma: impl Peripheral<P = Dma> + 'd,
|
2023-06-08 16:08:40 +02:00
|
|
|
_irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
|
2022-07-23 14:00:19 +02:00
|
|
|
d0: impl Peripheral<P = impl D0Pin<T>> + 'd,
|
|
|
|
d1: impl Peripheral<P = impl D1Pin<T>> + 'd,
|
|
|
|
d2: impl Peripheral<P = impl D2Pin<T>> + 'd,
|
|
|
|
d3: impl Peripheral<P = impl D3Pin<T>> + 'd,
|
|
|
|
d4: impl Peripheral<P = impl D4Pin<T>> + 'd,
|
|
|
|
d5: impl Peripheral<P = impl D5Pin<T>> + 'd,
|
|
|
|
d6: impl Peripheral<P = impl D6Pin<T>> + 'd,
|
|
|
|
d7: impl Peripheral<P = impl D7Pin<T>> + 'd,
|
|
|
|
d8: impl Peripheral<P = impl D8Pin<T>> + 'd,
|
|
|
|
d9: impl Peripheral<P = impl D9Pin<T>> + 'd,
|
|
|
|
d10: impl Peripheral<P = impl D10Pin<T>> + 'd,
|
|
|
|
d11: impl Peripheral<P = impl D11Pin<T>> + 'd,
|
|
|
|
v_sync: impl Peripheral<P = impl VSyncPin<T>> + 'd,
|
|
|
|
h_sync: impl Peripheral<P = impl HSyncPin<T>> + 'd,
|
|
|
|
pixclk: impl Peripheral<P = impl PixClkPin<T>> + 'd,
|
2022-02-10 02:34:59 +01:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2023-05-25 00:29:56 +02:00
|
|
|
into_ref!(peri, dma);
|
2022-02-10 21:38:03 +01:00
|
|
|
config_pins!(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11);
|
|
|
|
config_pins!(v_sync, h_sync, pixclk);
|
2022-02-10 02:34:59 +01:00
|
|
|
|
2023-05-25 00:29:56 +02:00
|
|
|
Self::new_inner(peri, dma, config, false, 0b10)
|
2022-02-10 02:34:59 +01:00
|
|
|
}
|
2022-02-10 21:38:03 +01:00
|
|
|
|
2022-02-10 02:34:59 +01:00
|
|
|
pub fn new_14bit(
|
2022-07-23 14:00:19 +02:00
|
|
|
peri: impl Peripheral<P = T> + 'd,
|
|
|
|
dma: impl Peripheral<P = Dma> + 'd,
|
2023-06-08 16:08:40 +02:00
|
|
|
_irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
|
2022-07-23 14:00:19 +02:00
|
|
|
d0: impl Peripheral<P = impl D0Pin<T>> + 'd,
|
|
|
|
d1: impl Peripheral<P = impl D1Pin<T>> + 'd,
|
|
|
|
d2: impl Peripheral<P = impl D2Pin<T>> + 'd,
|
|
|
|
d3: impl Peripheral<P = impl D3Pin<T>> + 'd,
|
|
|
|
d4: impl Peripheral<P = impl D4Pin<T>> + 'd,
|
|
|
|
d5: impl Peripheral<P = impl D5Pin<T>> + 'd,
|
|
|
|
d6: impl Peripheral<P = impl D6Pin<T>> + 'd,
|
|
|
|
d7: impl Peripheral<P = impl D7Pin<T>> + 'd,
|
|
|
|
d8: impl Peripheral<P = impl D8Pin<T>> + 'd,
|
|
|
|
d9: impl Peripheral<P = impl D9Pin<T>> + 'd,
|
|
|
|
d10: impl Peripheral<P = impl D10Pin<T>> + 'd,
|
|
|
|
d11: impl Peripheral<P = impl D11Pin<T>> + 'd,
|
|
|
|
d12: impl Peripheral<P = impl D12Pin<T>> + 'd,
|
|
|
|
d13: impl Peripheral<P = impl D13Pin<T>> + 'd,
|
|
|
|
v_sync: impl Peripheral<P = impl VSyncPin<T>> + 'd,
|
|
|
|
h_sync: impl Peripheral<P = impl HSyncPin<T>> + 'd,
|
|
|
|
pixclk: impl Peripheral<P = impl PixClkPin<T>> + 'd,
|
2022-02-10 02:34:59 +01:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2023-05-25 00:29:56 +02:00
|
|
|
into_ref!(peri, dma);
|
2022-02-10 21:38:03 +01:00
|
|
|
config_pins!(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13);
|
|
|
|
config_pins!(v_sync, h_sync, pixclk);
|
2022-02-10 02:34:59 +01:00
|
|
|
|
2023-05-25 00:29:56 +02:00
|
|
|
Self::new_inner(peri, dma, config, false, 0b11)
|
2022-02-10 02:34:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_es_8bit(
|
2022-07-23 14:00:19 +02:00
|
|
|
peri: impl Peripheral<P = T> + 'd,
|
|
|
|
dma: impl Peripheral<P = Dma> + 'd,
|
2023-06-08 16:08:40 +02:00
|
|
|
_irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
|
2022-07-23 14:00:19 +02:00
|
|
|
d0: impl Peripheral<P = impl D0Pin<T>> + 'd,
|
|
|
|
d1: impl Peripheral<P = impl D1Pin<T>> + 'd,
|
|
|
|
d2: impl Peripheral<P = impl D2Pin<T>> + 'd,
|
|
|
|
d3: impl Peripheral<P = impl D3Pin<T>> + 'd,
|
|
|
|
d4: impl Peripheral<P = impl D4Pin<T>> + 'd,
|
|
|
|
d5: impl Peripheral<P = impl D5Pin<T>> + 'd,
|
|
|
|
d6: impl Peripheral<P = impl D6Pin<T>> + 'd,
|
|
|
|
d7: impl Peripheral<P = impl D7Pin<T>> + 'd,
|
|
|
|
pixclk: impl Peripheral<P = impl PixClkPin<T>> + 'd,
|
2022-02-10 02:34:59 +01:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2023-05-25 00:29:56 +02:00
|
|
|
into_ref!(peri, dma);
|
2022-02-10 21:38:03 +01:00
|
|
|
config_pins!(d0, d1, d2, d3, d4, d5, d6, d7);
|
|
|
|
config_pins!(pixclk);
|
2022-02-10 02:34:59 +01:00
|
|
|
|
2023-05-25 00:29:56 +02:00
|
|
|
Self::new_inner(peri, dma, config, true, 0b00)
|
2022-02-10 02:34:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_es_10bit(
|
2022-07-23 14:00:19 +02:00
|
|
|
peri: impl Peripheral<P = T> + 'd,
|
|
|
|
dma: impl Peripheral<P = Dma> + 'd,
|
2023-06-08 16:08:40 +02:00
|
|
|
_irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
|
2022-07-23 14:00:19 +02:00
|
|
|
d0: impl Peripheral<P = impl D0Pin<T>> + 'd,
|
|
|
|
d1: impl Peripheral<P = impl D1Pin<T>> + 'd,
|
|
|
|
d2: impl Peripheral<P = impl D2Pin<T>> + 'd,
|
|
|
|
d3: impl Peripheral<P = impl D3Pin<T>> + 'd,
|
|
|
|
d4: impl Peripheral<P = impl D4Pin<T>> + 'd,
|
|
|
|
d5: impl Peripheral<P = impl D5Pin<T>> + 'd,
|
|
|
|
d6: impl Peripheral<P = impl D6Pin<T>> + 'd,
|
|
|
|
d7: impl Peripheral<P = impl D7Pin<T>> + 'd,
|
|
|
|
d8: impl Peripheral<P = impl D8Pin<T>> + 'd,
|
|
|
|
d9: impl Peripheral<P = impl D9Pin<T>> + 'd,
|
|
|
|
pixclk: impl Peripheral<P = impl PixClkPin<T>> + 'd,
|
2022-02-10 02:34:59 +01:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2023-05-25 00:29:56 +02:00
|
|
|
into_ref!(peri, dma);
|
2022-02-10 21:38:03 +01:00
|
|
|
config_pins!(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9);
|
|
|
|
config_pins!(pixclk);
|
2022-02-10 02:34:59 +01:00
|
|
|
|
2023-05-25 00:29:56 +02:00
|
|
|
Self::new_inner(peri, dma, config, true, 0b01)
|
2022-02-10 02:34:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_es_12bit(
|
2022-07-23 14:00:19 +02:00
|
|
|
peri: impl Peripheral<P = T> + 'd,
|
|
|
|
dma: impl Peripheral<P = Dma> + 'd,
|
2023-06-08 16:08:40 +02:00
|
|
|
_irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
|
2022-07-23 14:00:19 +02:00
|
|
|
d0: impl Peripheral<P = impl D0Pin<T>> + 'd,
|
|
|
|
d1: impl Peripheral<P = impl D1Pin<T>> + 'd,
|
|
|
|
d2: impl Peripheral<P = impl D2Pin<T>> + 'd,
|
|
|
|
d3: impl Peripheral<P = impl D3Pin<T>> + 'd,
|
|
|
|
d4: impl Peripheral<P = impl D4Pin<T>> + 'd,
|
|
|
|
d5: impl Peripheral<P = impl D5Pin<T>> + 'd,
|
|
|
|
d6: impl Peripheral<P = impl D6Pin<T>> + 'd,
|
|
|
|
d7: impl Peripheral<P = impl D7Pin<T>> + 'd,
|
|
|
|
d8: impl Peripheral<P = impl D8Pin<T>> + 'd,
|
|
|
|
d9: impl Peripheral<P = impl D9Pin<T>> + 'd,
|
|
|
|
d10: impl Peripheral<P = impl D10Pin<T>> + 'd,
|
|
|
|
d11: impl Peripheral<P = impl D11Pin<T>> + 'd,
|
|
|
|
pixclk: impl Peripheral<P = impl PixClkPin<T>> + 'd,
|
2022-02-10 02:34:59 +01:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2023-05-25 00:29:56 +02:00
|
|
|
into_ref!(peri, dma);
|
2022-02-10 21:38:03 +01:00
|
|
|
config_pins!(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11);
|
|
|
|
config_pins!(pixclk);
|
2022-02-10 02:34:59 +01:00
|
|
|
|
2023-05-25 00:29:56 +02:00
|
|
|
Self::new_inner(peri, dma, config, true, 0b10)
|
2022-02-10 02:34:59 +01:00
|
|
|
}
|
2022-02-10 21:38:03 +01:00
|
|
|
|
2022-02-10 02:34:59 +01:00
|
|
|
pub fn new_es_14bit(
|
2022-07-23 14:00:19 +02:00
|
|
|
peri: impl Peripheral<P = T> + 'd,
|
|
|
|
dma: impl Peripheral<P = Dma> + 'd,
|
2023-06-08 16:08:40 +02:00
|
|
|
_irq: impl interrupt::typelevel::Binding<T::Interrupt, InterruptHandler<T>> + 'd,
|
2022-07-23 14:00:19 +02:00
|
|
|
d0: impl Peripheral<P = impl D0Pin<T>> + 'd,
|
|
|
|
d1: impl Peripheral<P = impl D1Pin<T>> + 'd,
|
|
|
|
d2: impl Peripheral<P = impl D2Pin<T>> + 'd,
|
|
|
|
d3: impl Peripheral<P = impl D3Pin<T>> + 'd,
|
|
|
|
d4: impl Peripheral<P = impl D4Pin<T>> + 'd,
|
|
|
|
d5: impl Peripheral<P = impl D5Pin<T>> + 'd,
|
|
|
|
d6: impl Peripheral<P = impl D6Pin<T>> + 'd,
|
|
|
|
d7: impl Peripheral<P = impl D7Pin<T>> + 'd,
|
|
|
|
d8: impl Peripheral<P = impl D8Pin<T>> + 'd,
|
|
|
|
d9: impl Peripheral<P = impl D9Pin<T>> + 'd,
|
|
|
|
d10: impl Peripheral<P = impl D10Pin<T>> + 'd,
|
|
|
|
d11: impl Peripheral<P = impl D11Pin<T>> + 'd,
|
|
|
|
d12: impl Peripheral<P = impl D12Pin<T>> + 'd,
|
|
|
|
d13: impl Peripheral<P = impl D13Pin<T>> + 'd,
|
|
|
|
pixclk: impl Peripheral<P = impl PixClkPin<T>> + 'd,
|
2022-02-10 02:34:59 +01:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2023-05-25 00:29:56 +02:00
|
|
|
into_ref!(peri, dma);
|
2022-02-10 21:38:03 +01:00
|
|
|
config_pins!(d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13);
|
|
|
|
config_pins!(pixclk);
|
2022-02-10 02:34:59 +01:00
|
|
|
|
2023-05-25 00:29:56 +02:00
|
|
|
Self::new_inner(peri, dma, config, true, 0b11)
|
2022-02-10 02:34:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn new_inner(
|
2022-07-23 14:00:19 +02:00
|
|
|
peri: PeripheralRef<'d, T>,
|
|
|
|
dma: PeripheralRef<'d, Dma>,
|
2022-02-10 02:34:59 +01:00
|
|
|
config: Config,
|
|
|
|
use_embedded_synchronization: bool,
|
|
|
|
edm: u8,
|
2021-11-10 14:47:16 +01:00
|
|
|
) -> Self {
|
|
|
|
T::reset();
|
|
|
|
T::enable();
|
|
|
|
|
2023-06-19 03:07:26 +02:00
|
|
|
peri.regs().cr().modify(|r| {
|
|
|
|
r.set_cm(true); // disable continuous mode (snapshot mode)
|
|
|
|
r.set_ess(use_embedded_synchronization);
|
|
|
|
r.set_pckpol(config.pixclk_polarity == PixelClockPolarity::RisingEdge);
|
|
|
|
r.set_vspol(config.vsync_level == VSyncDataInvalidLevel::High);
|
|
|
|
r.set_hspol(config.hsync_level == HSyncDataInvalidLevel::High);
|
|
|
|
r.set_fcrc(0x00); // capture every frame
|
|
|
|
r.set_edm(edm); // extended data mode
|
|
|
|
});
|
2021-11-10 14:47:16 +01:00
|
|
|
|
2023-06-01 02:22:46 +02:00
|
|
|
T::Interrupt::unpend();
|
|
|
|
unsafe { T::Interrupt::enable() };
|
2021-11-10 14:47:16 +01:00
|
|
|
|
2022-07-23 01:29:35 +02:00
|
|
|
Self { inner: peri, dma }
|
2021-11-10 14:47:16 +01:00
|
|
|
}
|
|
|
|
|
2023-06-19 03:07:26 +02:00
|
|
|
fn toggle(enable: bool) {
|
2021-11-10 14:47:16 +01:00
|
|
|
crate::pac::DCMI.cr().modify(|r| {
|
|
|
|
r.set_enable(enable);
|
|
|
|
r.set_capture(enable);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn enable_irqs() {
|
2023-06-19 03:07:26 +02:00
|
|
|
crate::pac::DCMI.ier().modify(|r| {
|
|
|
|
r.set_err_ie(true);
|
|
|
|
r.set_ovr_ie(true);
|
|
|
|
r.set_frame_ie(true);
|
|
|
|
});
|
2021-11-10 14:47:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn clear_interrupt_flags() {
|
2023-06-19 03:07:26 +02:00
|
|
|
crate::pac::DCMI.icr().write(|r| {
|
|
|
|
r.set_ovr_isc(true);
|
|
|
|
r.set_err_isc(true);
|
|
|
|
r.set_frame_isc(true);
|
|
|
|
})
|
2021-11-10 14:47:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This method starts the capture and finishes when both the dma transfer and DCMI finish the frame transfer.
|
|
|
|
/// The implication is that the input buffer size must be exactly the size of the captured frame.
|
2022-04-12 14:06:53 +02:00
|
|
|
///
|
|
|
|
/// Note that when `buffer.len() > 0xffff` the capture future requires some real-time guarantees to be upheld
|
|
|
|
/// (must be polled fast enough so the buffers get switched before data is overwritten).
|
|
|
|
/// It is therefore recommended that it is run on higher priority executor.
|
2021-11-10 14:47:16 +01:00
|
|
|
pub async fn capture(&mut self, buffer: &mut [u32]) -> Result<(), Error> {
|
2022-04-12 14:06:53 +02:00
|
|
|
if buffer.len() <= 0xffff {
|
|
|
|
return self.capture_small(buffer).await;
|
|
|
|
} else {
|
|
|
|
return self.capture_giant(buffer).await;
|
|
|
|
}
|
|
|
|
}
|
2023-06-19 03:07:26 +02:00
|
|
|
|
2022-04-12 14:06:53 +02:00
|
|
|
async fn capture_small(&mut self, buffer: &mut [u32]) -> Result<(), Error> {
|
2021-11-10 14:47:16 +01:00
|
|
|
let r = self.inner.regs();
|
2023-06-19 03:07:26 +02:00
|
|
|
let src = r.dr().as_ptr() as *mut u32;
|
2023-04-17 00:04:54 +02:00
|
|
|
let request = self.dma.request();
|
|
|
|
let dma_read = unsafe { Transfer::new_read(&mut self.dma, request, src, buffer, Default::default()) };
|
2021-11-10 14:47:16 +01:00
|
|
|
|
|
|
|
Self::clear_interrupt_flags();
|
|
|
|
Self::enable_irqs();
|
|
|
|
|
2023-06-19 03:07:26 +02:00
|
|
|
Self::toggle(true);
|
2021-11-10 14:47:16 +01:00
|
|
|
|
|
|
|
let result = poll_fn(|cx| {
|
|
|
|
STATE.waker.register(cx.waker());
|
|
|
|
|
2023-06-19 03:07:26 +02:00
|
|
|
let ris = crate::pac::DCMI.ris().read();
|
2021-11-10 14:47:16 +01:00
|
|
|
if ris.err_ris() {
|
2023-06-19 03:07:26 +02:00
|
|
|
crate::pac::DCMI.icr().write(|r| r.set_err_isc(true));
|
2021-11-10 14:47:16 +01:00
|
|
|
Poll::Ready(Err(Error::PeripheralError))
|
|
|
|
} else if ris.ovr_ris() {
|
2023-06-19 03:07:26 +02:00
|
|
|
crate::pac::DCMI.icr().write(|r| r.set_ovr_isc(true));
|
2021-11-10 14:47:16 +01:00
|
|
|
Poll::Ready(Err(Error::Overrun))
|
|
|
|
} else if ris.frame_ris() {
|
2023-06-19 03:07:26 +02:00
|
|
|
crate::pac::DCMI.icr().write(|r| r.set_frame_isc(true));
|
2021-11-10 14:47:16 +01:00
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-09-22 16:48:35 +02:00
|
|
|
let (_, result) = embassy_futures::join::join(dma_read, result).await;
|
2021-11-10 14:47:16 +01:00
|
|
|
|
2023-06-19 03:07:26 +02:00
|
|
|
Self::toggle(false);
|
2021-11-10 14:47:16 +01:00
|
|
|
|
|
|
|
result
|
|
|
|
}
|
2022-04-12 14:06:53 +02:00
|
|
|
|
2023-04-18 16:16:33 +02:00
|
|
|
#[cfg(not(dma))]
|
2023-04-17 00:04:54 +02:00
|
|
|
async fn capture_giant(&mut self, _buffer: &mut [u32]) -> Result<(), Error> {
|
2023-04-18 16:16:33 +02:00
|
|
|
panic!("capturing to buffers larger than 0xffff is only supported on DMA for now, not on BDMA or GPDMA.");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(dma)]
|
|
|
|
async fn capture_giant(&mut self, buffer: &mut [u32]) -> Result<(), Error> {
|
2022-04-12 14:06:53 +02:00
|
|
|
use crate::dma::TransferOptions;
|
|
|
|
|
|
|
|
let data_len = buffer.len();
|
|
|
|
let chunk_estimate = data_len / 0xffff;
|
|
|
|
|
|
|
|
let mut chunks = chunk_estimate + 1;
|
|
|
|
while data_len % chunks != 0 {
|
|
|
|
chunks += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let chunk_size = data_len / chunks;
|
|
|
|
|
|
|
|
let mut remaining_chunks = chunks - 2;
|
|
|
|
|
|
|
|
let mut m0ar = buffer.as_mut_ptr();
|
|
|
|
let mut m1ar = unsafe { buffer.as_mut_ptr().add(chunk_size) };
|
|
|
|
|
|
|
|
let channel = &mut self.dma;
|
|
|
|
let request = channel.request();
|
|
|
|
|
|
|
|
let r = self.inner.regs();
|
2023-06-19 03:07:26 +02:00
|
|
|
let src = r.dr().as_ptr() as *mut u32;
|
2022-04-12 14:06:53 +02:00
|
|
|
|
2023-04-18 16:16:33 +02:00
|
|
|
let mut transfer = unsafe {
|
|
|
|
crate::dma::DoubleBuffered::new_read(
|
|
|
|
&mut self.dma,
|
|
|
|
request,
|
|
|
|
src,
|
|
|
|
m0ar,
|
|
|
|
m1ar,
|
|
|
|
chunk_size,
|
|
|
|
TransferOptions::default(),
|
|
|
|
)
|
|
|
|
};
|
2022-04-12 14:06:53 +02:00
|
|
|
|
|
|
|
let mut last_chunk_set_for_transfer = false;
|
|
|
|
let mut buffer0_last_accessible = false;
|
|
|
|
let dma_result = poll_fn(|cx| {
|
2023-04-18 16:16:33 +02:00
|
|
|
transfer.set_waker(cx.waker());
|
2022-04-12 14:06:53 +02:00
|
|
|
|
2023-04-18 16:16:33 +02:00
|
|
|
let buffer0_currently_accessible = transfer.is_buffer0_accessible();
|
2022-04-12 14:06:53 +02:00
|
|
|
|
|
|
|
// check if the accessible buffer changed since last poll
|
|
|
|
if buffer0_last_accessible == buffer0_currently_accessible {
|
|
|
|
return Poll::Pending;
|
|
|
|
}
|
|
|
|
buffer0_last_accessible = !buffer0_last_accessible;
|
|
|
|
|
|
|
|
if remaining_chunks != 0 {
|
|
|
|
if remaining_chunks % 2 == 0 && buffer0_currently_accessible {
|
|
|
|
m0ar = unsafe { m0ar.add(2 * chunk_size) };
|
2023-04-18 16:16:33 +02:00
|
|
|
unsafe { transfer.set_buffer0(m0ar) }
|
2022-04-12 14:06:53 +02:00
|
|
|
remaining_chunks -= 1;
|
|
|
|
} else if !buffer0_currently_accessible {
|
|
|
|
m1ar = unsafe { m1ar.add(2 * chunk_size) };
|
2023-04-18 16:16:33 +02:00
|
|
|
unsafe { transfer.set_buffer1(m1ar) };
|
2022-04-12 14:06:53 +02:00
|
|
|
remaining_chunks -= 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if buffer0_currently_accessible {
|
2023-04-18 16:16:33 +02:00
|
|
|
unsafe { transfer.set_buffer0(buffer.as_mut_ptr()) }
|
2022-04-12 14:06:53 +02:00
|
|
|
} else {
|
2023-04-18 16:16:33 +02:00
|
|
|
unsafe { transfer.set_buffer1(buffer.as_mut_ptr()) }
|
2022-04-12 14:06:53 +02:00
|
|
|
}
|
|
|
|
if last_chunk_set_for_transfer {
|
2023-04-18 16:16:33 +02:00
|
|
|
transfer.request_stop();
|
2022-04-12 14:06:53 +02:00
|
|
|
return Poll::Ready(());
|
|
|
|
}
|
|
|
|
last_chunk_set_for_transfer = true;
|
|
|
|
}
|
|
|
|
Poll::Pending
|
|
|
|
});
|
|
|
|
|
|
|
|
Self::clear_interrupt_flags();
|
|
|
|
Self::enable_irqs();
|
|
|
|
|
|
|
|
let result = poll_fn(|cx| {
|
|
|
|
STATE.waker.register(cx.waker());
|
|
|
|
|
2023-06-19 03:07:26 +02:00
|
|
|
let ris = crate::pac::DCMI.ris().read();
|
2022-04-12 14:06:53 +02:00
|
|
|
if ris.err_ris() {
|
2023-06-19 03:07:26 +02:00
|
|
|
crate::pac::DCMI.icr().write(|r| r.set_err_isc(true));
|
2022-04-12 14:06:53 +02:00
|
|
|
Poll::Ready(Err(Error::PeripheralError))
|
|
|
|
} else if ris.ovr_ris() {
|
2023-06-19 03:07:26 +02:00
|
|
|
crate::pac::DCMI.icr().write(|r| r.set_ovr_isc(true));
|
2022-04-12 14:06:53 +02:00
|
|
|
Poll::Ready(Err(Error::Overrun))
|
|
|
|
} else if ris.frame_ris() {
|
2023-06-19 03:07:26 +02:00
|
|
|
crate::pac::DCMI.icr().write(|r| r.set_frame_isc(true));
|
2022-04-12 14:06:53 +02:00
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-06-19 03:07:26 +02:00
|
|
|
Self::toggle(true);
|
2022-04-12 14:06:53 +02:00
|
|
|
|
2022-09-22 16:48:35 +02:00
|
|
|
let (_, result) = embassy_futures::join::join(dma_result, result).await;
|
2022-04-12 14:06:53 +02:00
|
|
|
|
2023-06-19 03:07:26 +02:00
|
|
|
Self::toggle(false);
|
2022-04-12 14:06:53 +02:00
|
|
|
|
|
|
|
result
|
|
|
|
}
|
2021-11-10 14:47:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mod sealed {
|
2022-02-10 21:38:03 +01:00
|
|
|
pub trait Instance: crate::rcc::RccPeripheral {
|
2021-11-10 14:47:16 +01:00
|
|
|
fn regs(&self) -> crate::pac::dcmi::Dcmi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Instance: sealed::Instance + 'static {
|
2023-06-08 16:08:40 +02:00
|
|
|
type Interrupt: interrupt::typelevel::Interrupt;
|
2021-11-10 14:47:16 +01:00
|
|
|
}
|
|
|
|
|
2022-02-10 21:38:03 +01:00
|
|
|
pin_trait!(D0Pin, Instance);
|
|
|
|
pin_trait!(D1Pin, Instance);
|
|
|
|
pin_trait!(D2Pin, Instance);
|
|
|
|
pin_trait!(D3Pin, Instance);
|
|
|
|
pin_trait!(D4Pin, Instance);
|
|
|
|
pin_trait!(D5Pin, Instance);
|
|
|
|
pin_trait!(D6Pin, Instance);
|
|
|
|
pin_trait!(D7Pin, Instance);
|
|
|
|
pin_trait!(D8Pin, Instance);
|
|
|
|
pin_trait!(D9Pin, Instance);
|
|
|
|
pin_trait!(D10Pin, Instance);
|
|
|
|
pin_trait!(D11Pin, Instance);
|
|
|
|
pin_trait!(D12Pin, Instance);
|
|
|
|
pin_trait!(D13Pin, Instance);
|
|
|
|
pin_trait!(HSyncPin, Instance);
|
|
|
|
pin_trait!(VSyncPin, Instance);
|
|
|
|
pin_trait!(PixClkPin, Instance);
|
2021-11-10 14:47:16 +01:00
|
|
|
|
|
|
|
// allow unused as U5 sources do not contain interrupt nor dma data
|
|
|
|
#[allow(unused)]
|
|
|
|
macro_rules! impl_peripheral {
|
|
|
|
($inst:ident, $irq:ident) => {
|
|
|
|
impl sealed::Instance for crate::peripherals::$inst {
|
|
|
|
fn regs(&self) -> crate::pac::dcmi::Dcmi {
|
|
|
|
crate::pac::$inst
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Instance for crate::peripherals::$inst {
|
2023-06-08 16:08:40 +02:00
|
|
|
type Interrupt = crate::interrupt::typelevel::$irq;
|
2021-11-10 14:47:16 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-26 01:40:43 +01:00
|
|
|
foreach_interrupt! {
|
2021-11-10 14:47:16 +01:00
|
|
|
($inst:ident, dcmi, $block:ident, GLOBAL, $irq:ident) => {
|
|
|
|
impl_peripheral!($inst, $irq);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-10 21:38:03 +01:00
|
|
|
dma_trait!(FrameDma, Instance);
|