ionpak-thermostat/firmware/src/tec.rs

129 lines
3.4 KiB
Rust
Raw Normal View History

2019-09-24 07:45:11 +08:00
use core::fmt;
use crate::board::pwm::{self, PwmChannel, PwmPeripheral};
use crate::board::gpio::{Gpio, GpioOutput, PP2, PP3, PK1, PQ4};
2019-11-11 05:27:20 +08:00
use embedded_hal::digital::v2::OutputPin;
#[derive(Clone, Copy, Debug)]
pub enum TecPin {
ISet,
MaxIPos,
MaxINeg,
MaxV,
}
2019-09-24 07:45:11 +08:00
impl TecPin {
pub const VALID_VALUES: &'static [TecPin] = &[
TecPin::ISet,
TecPin::MaxIPos,
TecPin::MaxINeg,
TecPin::MaxV,
];
}
impl fmt::Display for TecPin {
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self {
TecPin::ISet =>
"i_set".fmt(fmt),
TecPin::MaxIPos =>
"max_i_pos".fmt(fmt),
TecPin::MaxINeg =>
"max_i_neg".fmt(fmt),
TecPin::MaxV =>
"max_v".fmt(fmt),
}
}
}
2019-11-14 03:00:34 +08:00
fn setup_shdn<G>(gpio: G) -> GpioOutput<G>
2019-11-11 05:27:20 +08:00
where
G: Gpio,
GpioOutput<G>: OutputPin,
{
let mut pin = gpio.into_output();
2019-11-14 03:00:34 +08:00
// keep off until first use
let _ = pin.set_low();
pin
2019-11-11 05:27:20 +08:00
}
fn setup_freq<G>(gpio: G)
where
G: Gpio,
GpioOutput<G>: OutputPin,
{
let mut pin = gpio.into_output();
// Switching Frequency Select
// high: 1 MHz, low: 500 kHz
let _ = pin.set_high();
}
/// Thermo-Electric Cooling device controlled through four PWM
/// channels
2019-11-14 03:00:34 +08:00
pub struct Tec<MaxIPos: PwmChannel, MaxINeg: PwmChannel, ISet: PwmChannel, MaxV: PwmChannel, SHDN: OutputPin> {
max_i_pos: MaxIPos,
max_i_neg: MaxINeg,
i_set: ISet,
max_v: MaxV,
2019-11-14 03:00:34 +08:00
shdn: SHDN,
}
2019-11-14 03:13:57 +08:00
impl Tec<pwm::T2CCP0, pwm::T2CCP1, pwm::T3CCP0, pwm::T3CCP1, GpioOutput<PP2>> {
pub fn tec0() -> Self {
2019-09-24 07:45:51 +08:00
let (max_i_pos, max_i_neg) = tm4c129x::TIMER2::split();
let (i_set, max_v) = tm4c129x::TIMER3::split();
2019-11-14 03:13:57 +08:00
let shdn = setup_shdn(PP2);
setup_freq(PK1);
2019-11-14 03:00:34 +08:00
Tec { max_i_pos, max_i_neg, i_set, max_v, shdn }
}
}
2019-11-14 03:13:57 +08:00
impl Tec<pwm::T4CCP0, pwm::T4CCP1, pwm::T5CCP0, pwm::T5CCP1, GpioOutput<PP3>> {
pub fn tec1() -> Self {
2019-09-24 07:45:51 +08:00
let (max_i_pos, max_i_neg) = tm4c129x::TIMER4::split();
let (i_set, max_v) = tm4c129x::TIMER5::split();
2019-11-14 03:13:57 +08:00
let shdn = setup_shdn(PP3);
setup_freq(PQ4);
2019-11-14 03:00:34 +08:00
Tec { max_i_pos, max_i_neg, i_set, max_v, shdn }
}
}
2019-11-14 03:00:34 +08:00
impl<MaxIPos: PwmChannel, MaxINeg: PwmChannel, ISet: PwmChannel, MaxV: PwmChannel, SHDN: OutputPin> Tec<MaxIPos, MaxINeg, ISet, MaxV, SHDN> {
2019-11-14 03:12:25 +08:00
pub fn setup(mut self, iset_width: u16, max: u16) -> Self {
2019-10-03 07:33:21 +08:00
self.max_i_pos.set(max, max);
self.max_i_neg.set(max, max);
self.max_v.set(max, max);
2019-11-14 03:12:25 +08:00
self.i_set.set(iset_width, max);
2019-10-03 07:33:21 +08:00
self
}
2019-09-24 07:45:11 +08:00
pub fn get(&mut self, pin: TecPin) -> (u16, u16) {
match pin {
TecPin::MaxIPos =>
self.max_i_pos.get(),
TecPin::MaxINeg =>
self.max_i_neg.get(),
TecPin::ISet =>
self.i_set.get(),
TecPin::MaxV =>
self.max_v.get(),
}
}
pub fn set(&mut self, pin: TecPin, width: u16, total: u16) {
match pin {
TecPin::MaxIPos =>
self.max_i_pos.set(width, total),
TecPin::MaxINeg =>
self.max_i_neg.set(width, total),
2019-11-14 03:00:34 +08:00
TecPin::ISet => {
self.i_set.set(width, total);
// enable on first use
let _ = self.shdn.set_high();
}
TecPin::MaxV =>
self.max_v.set(width, total),
}
}
2019-09-23 22:50:43 +08:00
}