Compare commits

..

3 Commits

Author SHA1 Message Date
Astro 3c94342448 replace tec_u_meas_adc with pins_adc 2020-05-28 02:06:32 +02:00
Astro 5418488a2f replace channel[01].adc with pins_adc 2020-05-28 02:01:55 +02:00
Astro ec6aa03dc3 set pwm values as ratio 0..1 2020-05-27 20:38:57 +02:00
5 changed files with 39 additions and 50 deletions

View File

@ -52,10 +52,10 @@ The scope of this setting is per TCP session.
| `report mode` | Show current report mode |
| `report mode <off/on>` | Set report mode |
| `pwm` | Show current PWM settings |
| `pwm <0/1> max_i_pos <width>` | Set PWM duty cycle for **max_i_pos** to *width* |
| `pwm <0/1> max_i_neg <width>` | Set PWM duty cycle for **max_i_neg** to *width* |
| `pwm <0/1> max_v <width>` | Set PWM duty cycle for **max_v** to *width* |
| `pwm <0/1> <width>` | Disengage PID, set **i_set** DAC to *width* |
| `pwm <0/1> max_i_pos <ratio>` | Set PWM duty cycle for **max_i_pos** to *ratio* |
| `pwm <0/1> max_i_neg <ratio>` | Set PWM duty cycle for **max_i_neg** to *ratio* |
| `pwm <0/1> max_v <ratio>` | Set PWM duty cycle for **max_v** to *ratio* |
| `pwm <0/1> <volts>` | Disengage PID, set **i_set** DAC to *volts* |
| `pwm <0/1> pid` | Set PWM to be controlled by PID |
| `pid` | Show PID configuration |
| `pid <0/1> target <value>` | Set the PID controller target |

View File

@ -19,8 +19,6 @@ pub struct Channel<C: ChannelPins> {
/// 1 / Volts
pub dac_factor: f64,
pub shdn: C::Shdn,
/// stm32f4 integrated adc
pub adc: C::Adc,
pub vref_pin: C::VRefPin,
pub itec_pin: C::ItecPin,
/// feedback from `dac` output
@ -42,7 +40,6 @@ impl<C: ChannelPins> Channel<C> {
state,
dac, dac_factor,
shdn: pins.shdn,
adc: pins.adc,
vref_pin: pins.vref_pin,
itec_pin: pins.itec_pin,
dac_feedback_pin: pins.dac_feedback_pin,

View File

@ -16,7 +16,8 @@ pub struct Channels {
channel0: Channel<Channel0>,
channel1: Channel<Channel1>,
pub adc: ad7172::Adc<pins::AdcSpi, pins::AdcNss>,
tec_u_meas_adc: pins::TecUMeasAdc,
/// stm32f4 integrated adc
pins_adc: pins::PinsAdc,
pub pwm: pins::PwmPins,
}
@ -24,7 +25,7 @@ impl Channels {
pub fn new(pins: pins::Pins) -> Self {
let channel0 = Channel::new(pins.channel0);
let channel1 = Channel::new(pins.channel1);
let tec_u_meas_adc = pins.tec_u_meas_adc;
let pins_adc = pins.pins_adc;
let pwm = pins.pwm;
let mut adc = ad7172::Adc::new(pins.adc_spi, pins.adc_nss).unwrap();
@ -47,7 +48,7 @@ impl Channels {
adc.setup_channel(1, ad7172::Input::Ain2, ad7172::Input::Ain3).unwrap();
adc.start_continuous_conversion().unwrap();
Channels { channel0, channel1, adc, tec_u_meas_adc, pwm }
Channels { channel0, channel1, adc, pins_adc, pwm }
}
pub fn channel_state<I: Into<usize>>(&mut self, channel: I) -> &mut ChannelState {
@ -106,19 +107,21 @@ impl Channels {
pub fn read_dac_feedback(&mut self, channel: usize) -> Volts {
match channel {
0 => {
let sample = self.channel0.adc.convert(
let sample = self.pins_adc.convert(
&self.channel0.dac_feedback_pin,
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
);
let mv = self.channel0.adc.sample_to_millivolts(sample);
let mv = self.pins_adc.sample_to_millivolts(sample);
info!("dac0_fb: {}/{:03X}", mv, sample);
Volts(mv as f64 / 1000.0)
}
1 => {
let sample = self.channel1.adc.convert(
let sample = self.pins_adc.convert(
&self.channel1.dac_feedback_pin,
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
);
let mv = self.channel1.adc.sample_to_millivolts(sample);
let mv = self.pins_adc.sample_to_millivolts(sample);
info!("dac1_fb: {}/{:03X}", mv, sample);
Volts(mv as f64 / 1000.0)
}
_ => unreachable!(),
@ -140,19 +143,19 @@ impl Channels {
pub fn read_itec(&mut self, channel: usize) -> Volts {
match channel {
0 => {
let sample = self.channel0.adc.convert(
let sample = self.pins_adc.convert(
&self.channel0.itec_pin,
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
);
let mv = self.channel0.adc.sample_to_millivolts(sample);
let mv = self.pins_adc.sample_to_millivolts(sample);
Volts(mv as f64 / 1000.0)
}
1 => {
let sample = self.channel1.adc.convert(
let sample = self.pins_adc.convert(
&self.channel1.itec_pin,
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
);
let mv = self.channel1.adc.sample_to_millivolts(sample);
let mv = self.pins_adc.sample_to_millivolts(sample);
Volts(mv as f64 / 1000.0)
}
_ => unreachable!(),
@ -163,19 +166,19 @@ impl Channels {
pub fn read_vref(&mut self, channel: usize) -> Volts {
match channel {
0 => {
let sample = self.channel0.adc.convert(
let sample = self.pins_adc.convert(
&self.channel0.vref_pin,
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
);
let mv = self.channel0.adc.sample_to_millivolts(sample);
let mv = self.pins_adc.sample_to_millivolts(sample);
Volts(mv as f64 / 1000.0)
}
1 => {
let sample = self.channel1.adc.convert(
let sample = self.pins_adc.convert(
&self.channel1.vref_pin,
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
);
let mv = self.channel1.adc.sample_to_millivolts(sample);
let mv = self.pins_adc.sample_to_millivolts(sample);
Volts(mv as f64 / 1000.0)
}
_ => unreachable!(),
@ -185,19 +188,19 @@ impl Channels {
pub fn read_tec_u_meas(&mut self, channel: usize) -> Volts {
match channel {
0 => {
let sample = self.tec_u_meas_adc.convert(
let sample = self.pins_adc.convert(
&self.channel0.tec_u_meas_pin,
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
);
let mv = self.tec_u_meas_adc.sample_to_millivolts(sample);
let mv = self.pins_adc.sample_to_millivolts(sample);
Volts(mv as f64 / 1000.0)
}
1 => {
let sample = self.tec_u_meas_adc.convert(
let sample = self.pins_adc.convert(
&self.channel1.tec_u_meas_pin,
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
);
let mv = self.tec_u_meas_adc.sample_to_millivolts(sample);
let mv = self.pins_adc.sample_to_millivolts(sample);
Volts(mv as f64 / 1000.0)
}
_ => unreachable!(),

View File

@ -273,13 +273,13 @@ fn main() -> ! {
);
}
Command::Pwm { channel, pin, duty } => {
let duty = duty as u16;
fn set_pwm_channel<P: hal::PwmPin<Duty=u16>>(pin: &mut P, duty: u16) -> u16 {
pin.set_duty(duty);
pin.get_max_duty()
fn set_pwm_channel<P: hal::PwmPin<Duty=u16>>(pin: &mut P, duty: f64) -> (u16, u16) {
let max = pin.get_max_duty();
let value = (duty * (max as f64)) as u16;
pin.set_duty(value);
(value, max)
}
let max = match (channel, pin) {
let (value, max) = match (channel, pin) {
(_, PwmPin::ISet) =>
// Handled above
unreachable!(),
@ -300,7 +300,7 @@ fn main() -> ! {
};
let _ = writeln!(
socket, "channel {}: PWM {} reconfigured to {}/{}",
channel, pin.name(), duty, max
channel, pin.name(), value, max
);
}
Command::Pid { channel, parameter, value } => {

View File

@ -16,7 +16,7 @@ use stm32f4xx_hal::{
rcc::Clocks,
pwm::{self, PwmChannels},
spi::{Spi, NoMiso},
stm32::{ADC1, ADC2, ADC3, GPIOA, GPIOB, GPIOC, GPIOE, GPIOF, GPIOG, SPI2, SPI4, SPI5, TIM1, TIM3},
stm32::{ADC1, GPIOA, GPIOB, GPIOC, GPIOE, GPIOF, GPIOG, SPI2, SPI4, SPI5, TIM1, TIM3},
time::U32Ext,
};
use crate::channel::{Channel0, Channel1};
@ -26,7 +26,6 @@ pub trait ChannelPins {
type DacSpi: Transfer<u8>;
type DacSync: OutputPin;
type Shdn: OutputPin;
type Adc;
type VRefPin;
type ItecPin;
type DacFeedbackPin;
@ -37,7 +36,6 @@ impl ChannelPins for Channel0 {
type DacSpi = Dac0Spi;
type DacSync = PE4<Output<PushPull>>;
type Shdn = PE10<Output<PushPull>>;
type Adc = Adc<ADC1>;
type VRefPin = PA0<Analog>;
type ItecPin = PA6<Analog>;
type DacFeedbackPin = PA4<Analog>;
@ -48,7 +46,6 @@ impl ChannelPins for Channel1 {
type DacSpi = Dac1Spi;
type DacSync = PF6<Output<PushPull>>;
type Shdn = PE15<Output<PushPull>>;
type Adc = Adc<ADC2>;
type VRefPin = PA3<Analog>;
type ItecPin = PB0<Analog>;
type DacFeedbackPin = PA5<Analog>;
@ -60,14 +57,12 @@ pub type AdcSpi = Spi<SPI2, (PB10<Alternate<AF5>>, PB14<Alternate<AF5>>, PB15<Al
pub type AdcNss = PB12<Output<PushPull>>;
type Dac0Spi = Spi<SPI4, (PE2<Alternate<AF5>>, NoMiso, PE6<Alternate<AF5>>)>;
type Dac1Spi = Spi<SPI5, (PF7<Alternate<AF5>>, NoMiso, PF9<Alternate<AF5>>)>;
pub type TecUMeasAdc = Adc<ADC3>;
pub type PinsAdc = Adc<ADC1>;
pub struct ChannelPinSet<C: ChannelPins> {
pub dac_spi: C::DacSpi,
pub dac_sync: C::DacSync,
pub shdn: C::Shdn,
pub adc: C::Adc,
pub vref_pin: C::VRefPin,
pub itec_pin: C::ItecPin,
pub dac_feedback_pin: C::DacFeedbackPin,
@ -77,7 +72,7 @@ pub struct ChannelPinSet<C: ChannelPins> {
pub struct Pins {
pub adc_spi: AdcSpi,
pub adc_nss: AdcNss,
pub tec_u_meas_adc: TecUMeasAdc,
pub pins_adc: PinsAdc,
pub pwm: PwmPins,
pub channel0: ChannelPinSet<Channel0>,
pub channel1: ChannelPinSet<Channel1>,
@ -90,7 +85,7 @@ impl Pins {
tim1: TIM1, tim3: TIM3,
gpioa: GPIOA, gpiob: GPIOB, gpioc: GPIOC, gpioe: GPIOE, gpiof: GPIOF, gpiog: GPIOG,
spi2: SPI2, spi4: SPI4, spi5: SPI5,
adc1: ADC1, adc2: ADC2, adc3: ADC3,
adc1: ADC1,
) -> Self {
let gpioa = gpioa.split();
let gpiob = gpiob.split();
@ -107,7 +102,7 @@ impl Pins {
let adc_spi = Self::setup_spi_adc(clocks, spi2, gpiob.pb10, gpiob.pb14, gpiob.pb15);
let adc_nss = gpiob.pb12.into_push_pull_output();
let tec_u_meas_adc = Adc::adc3(adc3, true, Default::default());
let pins_adc = Adc::adc1(adc1, true, Default::default());
let pwm = PwmPins::setup(
clocks, tim1, tim3,
@ -122,8 +117,6 @@ impl Pins {
);
let mut shdn0 = gpioe.pe10.into_push_pull_output();
let _ = shdn0.set_low();
let mut adc0 = Adc::adc1(adc1, true, Default::default());
adc0.enable();
let vref0_pin = gpioa.pa0.into_analog();
let itec0_pin = gpioa.pa6.into_analog();
let dac_feedback0_pin = gpioa.pa4.into_analog();
@ -132,7 +125,6 @@ impl Pins {
dac_spi: dac0_spi,
dac_sync: dac0_sync,
shdn: shdn0,
adc: adc0,
vref_pin: vref0_pin,
itec_pin: itec0_pin,
dac_feedback_pin: dac_feedback0_pin,
@ -145,8 +137,6 @@ impl Pins {
);
let mut shdn1 = gpioe.pe15.into_push_pull_output();
let _ = shdn1.set_low();
let mut adc1 = Adc::adc2(adc2, true, Default::default());
adc1.enable();
let vref1_pin = gpioa.pa3.into_analog();
let itec1_pin = gpiob.pb0.into_analog();
let dac_feedback1_pin = gpioa.pa5.into_analog();
@ -155,7 +145,6 @@ impl Pins {
dac_spi: dac1_spi,
dac_sync: dac1_sync,
shdn: shdn1,
adc: adc1,
vref_pin: vref1_pin,
itec_pin: itec1_pin,
dac_feedback_pin: dac_feedback1_pin,
@ -164,7 +153,7 @@ impl Pins {
Pins {
adc_spi, adc_nss,
tec_u_meas_adc,
pins_adc,
pwm,
channel0,
channel1,