Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/system/patch_sm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,35 @@ pub struct PatchSmSystem {
pub audio: audio::Audio,
pub adc1: adc::Adc<stm32::ADC1, adc::Disabled>,
pub adc2: adc::Adc<stm32::ADC2, adc::Disabled>,
pub c1: Option<dac::C1<stm32::DAC, dac::Disabled>>,
pub c2: Option<dac::C2<stm32::DAC, dac::Disabled>>,
pub dac: PatchSmDac,
pub sdram: &'static mut [f32],
pub flash: crate::flash::Flash,
pub internal_usb: Option<InternalUsbPins>,
pub delay: Delay,
}

pub struct PatchSmDac {
c1: Option<dac::C1<stm32::DAC, dac::Disabled>>,
c2: Option<dac::C2<stm32::DAC, dac::Disabled>>,
}

impl PatchSmDac {
fn new(c1: dac::C1<stm32::DAC, dac::Disabled>, c2: dac::C2<stm32::DAC, dac::Disabled>) -> Self {
Self {
c1: Some(c1),
c2: Some(c2),
}
}

pub fn take_c1(&mut self) -> Option<dac::C1<stm32::DAC, dac::Disabled>> {
self.c1.take()
}

pub fn take_c2(&mut self) -> Option<dac::C2<stm32::DAC, dac::Disabled>> {
self.c2.take()
}
}

pub struct MinimalPatchSmSystem {
pub gpio: crate::gpio::PatchSmGPIO,
pub flash: crate::flash::Flash,
Expand Down Expand Up @@ -449,8 +470,7 @@ impl PatchSmSystem {
audio,
adc1,
adc2,
c1: Some(c1),
c2: Some(c2),
dac: PatchSmDac::new(c1, c2),
sdram,
flash,
internal_usb: Some(internal_usb),
Expand Down
117 changes: 116 additions & 1 deletion src/system/seed.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use log::info;
use stm32h7xx_hal::{adc, delay::Delay, dma, prelude::*, rcc, stm32, time::Hertz};
use stm32h7xx_hal::{adc, dac, delay::Delay, dma, prelude::*, rcc, stm32, time::Hertz};

use crate::{audio::Audio, *};

Expand All @@ -10,12 +10,126 @@ pub struct SeedSystem {
pub audio: audio::Audio,
pub adc1: adc::Adc<stm32::ADC1, adc::Disabled>,
pub adc2: adc::Adc<stm32::ADC2, adc::Disabled>,
pub dac: SeedDac,
pub sdram: &'static mut [f32],
pub flash: crate::flash::Flash,
pub internal_usb: Option<InternalUsbPins>,
pub delay: Delay,
}

pub struct SeedDac {
c1: Option<dac::C1<stm32::DAC, dac::Disabled>>,
c2: Option<dac::C2<stm32::DAC, dac::Disabled>>,
state: SeedDacState,
}

enum SeedDacState {
Uninitialized {
dac: stm32::DAC,
dac12_rec: rcc::rec::Dac12,
},
Channel1Initialized,
Channel2Initialized,
BothInitialized,
}

impl SeedDac {
fn new(dac: stm32::DAC, dac12_rec: rcc::rec::Dac12) -> Self {
Self {
c1: None,
c2: None,
state: SeedDacState::Uninitialized { dac, dac12_rec },
}
}

pub fn both_initialized(&self) -> bool {
matches!(self.state, SeedDacState::BothInitialized)
}

pub fn init_channels(
&mut self,
c1_pin: hal::gpio::gpioa::PA4<hal::gpio::Analog>,
c2_pin: hal::gpio::gpioa::PA5<hal::gpio::Analog>,
) -> Option<()> {
match self.state {
SeedDacState::BothInitialized => return Some(()),
SeedDacState::Channel1Initialized | SeedDacState::Channel2Initialized => {
return None;
}
SeedDacState::Uninitialized { .. } => {}
}

let SeedDacState::Uninitialized { dac, dac12_rec } =
core::mem::replace(&mut self.state, SeedDacState::BothInitialized)
else {
return None;
};
let (c1, c2) = dac::dac(dac, (c1_pin, c2_pin), dac12_rec);

self.c1 = Some(c1);
self.c2 = Some(c2);
Some(())
}

fn init_c1(&mut self, c1_pin: hal::gpio::gpioa::PA4<hal::gpio::Analog>) -> Option<()> {
if self.c1.is_some() {
return Some(());
}

let SeedDacState::Uninitialized { dac, dac12_rec } =
core::mem::replace(&mut self.state, SeedDacState::Channel1Initialized)
else {
return None;
};
let c1 = dac::dac(dac, c1_pin, dac12_rec);
self.c1 = Some(c1);
Some(())
}

fn init_c2(&mut self, c2_pin: hal::gpio::gpioa::PA5<hal::gpio::Analog>) -> Option<()> {
if self.c2.is_some() {
return Some(());
}

let SeedDacState::Uninitialized { dac, dac12_rec } =
core::mem::replace(&mut self.state, SeedDacState::Channel2Initialized)
else {
return None;
};
let c2 = dac::dac(dac, c2_pin, dac12_rec);
self.c2 = Some(c2);
Some(())
}

pub fn take_c1_with_pin(
&mut self,
c1_pin: hal::gpio::gpioa::PA4<hal::gpio::Analog>,
) -> Option<dac::C1<stm32::DAC, dac::Disabled>> {
if self.c1.is_none() {
self.init_c1(c1_pin)?;
}
self.c1.take()
}

pub fn take_c2_with_pin(
&mut self,
c2_pin: hal::gpio::gpioa::PA5<hal::gpio::Analog>,
) -> Option<dac::C2<stm32::DAC, dac::Disabled>> {
if self.c2.is_none() {
self.init_c2(c2_pin)?;
}
self.c2.take()
}

pub fn take_c1(&mut self) -> Option<dac::C1<stm32::DAC, dac::Disabled>> {
self.c1.take()
}

pub fn take_c2(&mut self) -> Option<dac::C2<stm32::DAC, dac::Disabled>> {
self.c2.take()
}
}

pub struct MinimalSeedSystem {
pub gpio: crate::gpio::SeedGPIO,
pub flash: crate::flash::Flash,
Expand Down Expand Up @@ -495,6 +609,7 @@ impl SeedSystem {
audio,
adc1,
adc2,
dac: SeedDac::new(resources.dac, resources.dac12_rec),
sdram,
flash,
internal_usb: Some(internal_usb),
Expand Down
Loading