thermostat/src/channel.rs

62 lines
1.7 KiB
Rust
Raw Normal View History

use stm32f4xx_hal::hal::digital::v2::OutputPin;
use uom::si::{
f64::ElectricPotential,
electric_potential::volt,
};
2020-05-17 07:23:35 +08:00
use crate::{
ad5680,
ad7172,
2020-05-17 07:23:35 +08:00
channel_state::ChannelState,
pins::{ChannelPins, ChannelPinSet},
};
2020-05-13 05:16:57 +08:00
/// Marker type for the first channel
pub struct Channel0;
/// Marker type for the second channel
pub struct Channel1;
pub struct Channel<C: ChannelPins> {
pub state: ChannelState,
2020-05-17 01:36:00 +08:00
/// for `i_set`
2020-05-13 05:16:57 +08:00
pub dac: ad5680::Dac<C::DacSpi, C::DacSync>,
/// Measured vref of MAX driver chip
pub vref_meas: ElectricPotential,
2020-05-13 05:16:57 +08:00
pub shdn: C::Shdn,
2020-05-19 04:43:44 +08:00
pub vref_pin: C::VRefPin,
2020-05-17 05:59:31 +08:00
pub itec_pin: C::ItecPin,
2020-05-17 01:36:00 +08:00
/// feedback from `dac` output
2020-05-17 08:18:25 +08:00
pub dac_feedback_pin: C::DacFeedbackPin,
2020-05-19 03:38:13 +08:00
pub tec_u_meas_pin: C::TecUMeasPin,
2020-05-13 05:16:57 +08:00
}
impl<C: ChannelPins> Channel<C> {
pub fn new(pins: ChannelPinSet<C>, adc_calibration: ad7172::ChannelCalibration) -> Self {
let state = ChannelState::new(adc_calibration);
2020-05-13 05:16:57 +08:00
let mut dac = ad5680::Dac::new(pins.dac_spi, pins.dac_sync);
2020-05-19 06:15:39 +08:00
let _ = dac.set(0);
// sensible dummy preset taken from datasheet. calibrate_dac_value() should be used to override this value.
let vref_meas = ElectricPotential::new::<volt>(1.5);
2020-05-13 05:16:57 +08:00
Channel {
state,
dac, vref_meas,
2020-05-13 05:16:57 +08:00
shdn: pins.shdn,
2020-05-19 04:43:44 +08:00
vref_pin: pins.vref_pin,
2020-05-17 05:59:31 +08:00
itec_pin: pins.itec_pin,
2020-05-17 08:18:25 +08:00
dac_feedback_pin: pins.dac_feedback_pin,
2020-05-19 03:38:13 +08:00
tec_u_meas_pin: pins.tec_u_meas_pin,
2020-05-13 05:16:57 +08:00
}
}
// power up TEC
pub fn power_up(&mut self) {
let _ = self.shdn.set_high();
}
// power down TEC
pub fn power_down(&mut self) {
let _ = self.shdn.set_low();
}
2020-05-13 05:16:57 +08:00
}