346c52e617
Co-authored-by: Zachary Sunforge <zachary.sunforge@bfpower.io> Reviewed-on: #7
126 lines
3.8 KiB
Rust
126 lines
3.8 KiB
Rust
#![no_std]
|
|
#![no_main]
|
|
|
|
use cortex_m::prelude::_embedded_hal_blocking_delay_DelayUs;
|
|
// Necessary unused import.
|
|
use {defmt_rtt as _, panic_probe as _};
|
|
|
|
use {embassy_executor as executor, embassy_stm32 as stm32};
|
|
|
|
use ads1256::standard::input::{Differential, SingleEnded};
|
|
use ads1256::{
|
|
AdControl, Ads1256, AutoCal, BitOrder, Buffer, ClockOut, Config, DataRate, DigitalIo,
|
|
DigitalIoDirection, DigitalIoState, Gain, Multiplexer, MuxInput, Sdcs, Status,
|
|
};
|
|
use embassy_time::Delay;
|
|
use executor::Spawner;
|
|
use stm32::dma::NoDma;
|
|
use stm32::exti::ExtiInput;
|
|
use stm32::gpio::{Input, Level, Output, Pull, Speed};
|
|
use stm32::spi::Spi;
|
|
use stm32::time::Hertz;
|
|
use stm32::{pac, spi};
|
|
|
|
use uom::si::electric_potential::{millivolt, volt};
|
|
|
|
use defmt::info;
|
|
use physical::transducer::{lm35, thermocouple_k};
|
|
use uom::si::thermodynamic_temperature::degree_celsius;
|
|
|
|
const AUTOCAL_CONF: Config = Config {
|
|
status: Status::setting(Buffer::Enabled, AutoCal::Enabled, BitOrder::MostSigFirst),
|
|
multiplexer: Multiplexer::setting(MuxInput::AIn0, MuxInput::AIn1),
|
|
ad_control: AdControl::setting(Gain::X64, Sdcs::Off, ClockOut::Off),
|
|
data_rate: DataRate::Sps2_5,
|
|
digital_io: DigitalIo::setting(DigitalIoState::default(), DigitalIoDirection::default()),
|
|
};
|
|
|
|
struct Ads1256Delay;
|
|
|
|
impl ads1256::BlockingDelay for Ads1256Delay {
|
|
#[inline]
|
|
fn t6_delay(&mut self) {
|
|
Delay.delay_us(1u32);
|
|
}
|
|
|
|
fn t11_1_delay(&mut self) {}
|
|
|
|
fn t11_2_delay(&mut self) {}
|
|
}
|
|
|
|
#[embassy_executor::main]
|
|
async fn main(spawner: Spawner) {
|
|
unsafe {
|
|
pac::FLASH.acr().modify(|v| {
|
|
v.set_prften(true);
|
|
v.set_icen(true);
|
|
v.set_dcen(true);
|
|
});
|
|
}
|
|
|
|
let p = embassy_stm32::init(Default::default());
|
|
|
|
let mut spi_conf = spi::Config::default();
|
|
spi_conf.mode = spi::MODE_1;
|
|
spi_conf.bit_order = spi::BitOrder::MsbFirst;
|
|
spi_conf.frequency = Hertz(ads1256::defaults::SPI_CLK_HZ);
|
|
|
|
let ads1256_data_ready = ExtiInput::new(Input::new(p.PA3, Pull::Up), p.EXTI3);
|
|
let select_ads1256 = Output::new(p.PA1, Level::High, Speed::VeryHigh);
|
|
|
|
let mut spi = Spi::new(
|
|
p.SPI1,
|
|
p.PA5,
|
|
p.PA7,
|
|
p.PA6,
|
|
NoDma,
|
|
NoDma,
|
|
spi_conf,
|
|
);
|
|
|
|
let mut ads_1256 = Ads1256::new(Ads1256Delay, select_ads1256, ads1256_data_ready);
|
|
ads_1256.write_config(&mut spi, AUTOCAL_CONF).unwrap();
|
|
ads_1256.self_calibrate(&mut spi).await.unwrap();
|
|
|
|
loop {
|
|
let gain = Gain::X2;
|
|
let reference = ads_1256
|
|
.autocal_convert(
|
|
&mut spi,
|
|
SingleEnded::AIn2.into(),
|
|
None,
|
|
Some(AUTOCAL_CONF.ad_control.with_gain(gain)),
|
|
None,
|
|
false,
|
|
)
|
|
.await
|
|
.unwrap()
|
|
.to_voltage(gain);
|
|
let reference_volts = reference.get::<volt>();
|
|
let reference = lm35::convert(reference).unwrap();
|
|
let reference_celsius = reference.get::<degree_celsius>();
|
|
info!(
|
|
"Reference junction temperature: {}°C, from voltage reading: {}V",
|
|
reference_celsius, reference_volts
|
|
);
|
|
|
|
let gain = Gain::X64;
|
|
let voltage = ads_1256
|
|
.autocal_convert(
|
|
&mut spi,
|
|
Differential::AIn0.into(),
|
|
None,
|
|
Some(AUTOCAL_CONF.ad_control.with_gain(gain)),
|
|
None,
|
|
false,
|
|
)
|
|
.await
|
|
.unwrap()
|
|
.to_voltage(AUTOCAL_CONF.ad_control.gain());
|
|
let mv = voltage.get::<millivolt>();
|
|
let temperature = thermocouple_k::convert_direct(voltage, reference).unwrap();
|
|
let celsius = temperature.get::<degree_celsius>();
|
|
info!("Thermocouple temperature: {}°C, from reading: {}mV", celsius, mv);
|
|
}
|
|
}
|