Compare commits

...

3 Commits

Author SHA1 Message Date
linuswck b8241d1f27 cargo fmt
- rustfmt.toml is ported from artiq-zynq repo
2024-04-23 17:09:26 +08:00
linuswck ded7dd7694 gui: add waitingspinnerwidget 2024-04-23 15:55:14 +08:00
linuswck 7fa4330797 gitignore: ignore python .pyc files 2024-04-23 15:52:32 +08:00
38 changed files with 1760 additions and 1234 deletions

2
.gitignore vendored
View File

@ -1,4 +1,4 @@
target/
result
*.pyc
*.jdebug*

View File

@ -1,7 +1,4 @@
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::{env, fs::File, io::Write, path::PathBuf};
fn main() {
// Put the linker script somewhere the linker can find it

View File

@ -3,11 +3,11 @@
"mozilla-overlay": {
"flake": false,
"locked": {
"lastModified": 1695805681,
"narHash": "sha256-1ElPLD8eFfnuIk0G52HGGpRtQZ4QPCjChRlEOfkZ5ro=",
"lastModified": 1704373101,
"narHash": "sha256-+gi59LRWRQmwROrmE1E2b3mtocwueCQqZ60CwLG+gbg=",
"owner": "mozilla",
"repo": "nixpkgs-mozilla",
"rev": "6eabade97bc28d707a8b9d82ad13ef143836736e",
"rev": "9b11a87c0cc54e308fa83aac5b4ee1816d5418a2",
"type": "github"
},
"original": {
@ -18,11 +18,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1710951922,
"narHash": "sha256-FOOBJ3DQenLpTNdxMHR2CpGZmYuctb92gF0lpiirZ30=",
"lastModified": 1713725259,
"narHash": "sha256-9ZR/Rbx5/Z/JZf5ehVNMoz/s5xjpP0a22tL6qNvLt5E=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "f091af045dff8347d66d186a62d42aceff159456",
"rev": "a5e4bbcb4780c63c79c87d29ea409abf097de3f7",
"type": "github"
},
"original": {

View File

@ -8,8 +8,8 @@
let
pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ (import mozilla-overlay) ]; };
rustManifest = pkgs.fetchurl {
url = "https://static.rust-lang.org/dist/2024-03-21/channel-rust-stable.toml";
sha256 = "faccaa01dda45fc2956bcfd4da0cf76e52104d3b1862ddd4eb7c4159a18e49cf";
url = "https://static.rust-lang.org/dist/2024-03-21/channel-rust-nightly.toml";
sha256 = "1c7db6ab80d20682b5cc5bda7360c63311d7188c0c082902d3790820527cd4e0";
};
targets = [
@ -22,7 +22,7 @@
inherit targets;
extensions = ["rust-src"];
};
rust = rustChannelOfTargets "stable" null targets;
rust = rustChannelOfTargets "nightly" null targets;
rustPlatform = pkgs.recurseIntoAttrs (pkgs.makeRustPlatform {
rustc = rust;
cargo = rust;

View File

@ -0,0 +1,194 @@
"""
The MIT License (MIT)
Copyright (c) 2012-2014 Alexander Turkin
Copyright (c) 2014 William Hallatt
Copyright (c) 2015 Jacob Dawid
Copyright (c) 2016 Luca Weiss
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import math
from PyQt6.QtCore import *
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
class QtWaitingSpinner(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
# WAS IN initialize()
self._color = QColor(Qt.GlobalColor.black)
self._roundness = 100.0
self._minimumTrailOpacity = 3.14159265358979323846
self._trailFadePercentage = 80.0
self._revolutionsPerSecond = 1.57079632679489661923
self._numberOfLines = 20
self._lineLength = 5
self._lineWidth = 2
self._innerRadius = 5
self._currentCounter = 0
self._timer = QTimer(self)
self._timer.timeout.connect(self.rotate)
self.updateSize()
self.updateTimer()
# END initialize()
self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
def paintEvent(self, QPaintEvent):
painter = QPainter(self)
painter.fillRect(self.rect(), Qt.GlobalColor.transparent)
painter.setRenderHint(QPainter.RenderHint.Antialiasing, True)
if self._currentCounter >= self._numberOfLines:
self._currentCounter = 0
painter.setPen(Qt.PenStyle.NoPen)
for i in range(0, self._numberOfLines):
painter.save()
painter.translate(self._innerRadius + self._lineLength, self._innerRadius + self._lineLength)
rotateAngle = float(360 * i) / float(self._numberOfLines)
painter.rotate(rotateAngle)
painter.translate(self._innerRadius, 0)
distance = self.lineCountDistanceFromPrimary(i, self._currentCounter, self._numberOfLines)
color = self.currentLineColor(distance, self._numberOfLines, self._trailFadePercentage,
self._minimumTrailOpacity, self._color)
painter.setBrush(color)
painter.drawRoundedRect(QRect(0, int(-self._lineWidth / 2), self._lineLength, self._lineWidth), self._roundness,
self._roundness, Qt.SizeMode.RelativeSize)
painter.restore()
def start(self):
if not self._timer.isActive():
self._timer.start()
self._currentCounter = 0
def stop(self):
if self._timer.isActive():
self._timer.stop()
self._currentCounter = 0
def setNumberOfLines(self, lines):
self._numberOfLines = lines
self._currentCounter = 0
self.updateTimer()
def setLineLength(self, length):
self._lineLength = length
self.updateSize()
def setLineWidth(self, width):
self._lineWidth = width
self.updateSize()
def setInnerRadius(self, radius):
self._innerRadius = radius
self.updateSize()
def color(self):
return self._color
def roundness(self):
return self._roundness
def minimumTrailOpacity(self):
return self._minimumTrailOpacity
def trailFadePercentage(self):
return self._trailFadePercentage
def revolutionsPersSecond(self):
return self._revolutionsPerSecond
def numberOfLines(self):
return self._numberOfLines
def lineLength(self):
return self._lineLength
def lineWidth(self):
return self._lineWidth
def innerRadius(self):
return self._innerRadius
def setRoundness(self, roundness):
self._roundness = max(0.0, min(100.0, roundness))
def setColor(self, color=Qt.GlobalColor.black):
self._color = QColor(color)
def setRevolutionsPerSecond(self, revolutionsPerSecond):
self._revolutionsPerSecond = revolutionsPerSecond
self.updateTimer()
def setTrailFadePercentage(self, trail):
self._trailFadePercentage = trail
def setMinimumTrailOpacity(self, minimumTrailOpacity):
self._minimumTrailOpacity = minimumTrailOpacity
def rotate(self):
self._currentCounter += 1
if self._currentCounter >= self._numberOfLines:
self._currentCounter = 0
self.update()
def updateSize(self):
self.size = (self._innerRadius + self._lineLength) * 2
self.setFixedSize(self.size, self.size)
def updateTimer(self):
self._timer.setInterval(int(1000 / (self._numberOfLines * self._revolutionsPerSecond)))
def lineCountDistanceFromPrimary(self, current, primary, totalNrOfLines):
distance = primary - current
if distance < 0:
distance += totalNrOfLines
return distance
def currentLineColor(self, countDistance, totalNrOfLines, trailFadePerc, minOpacity, colorinput):
color = QColor(colorinput)
if countDistance == 0:
return color
minAlphaF = minOpacity / 100.0
distanceThreshold = int(math.ceil((totalNrOfLines - 1) * trailFadePerc / 100.0))
if countDistance > distanceThreshold:
color.setAlphaF(minAlphaF)
else:
alphaDiff = color.alphaF() - minAlphaF
gradient = alphaDiff / float(distanceThreshold + 1)
resultAlpha = color.alphaF() - gradient * countDistance
# If alpha is out of bounds, clip it.
resultAlpha = min(1.0, max(0.0, resultAlpha))
color.setAlphaF(resultAlpha)
return color
if __name__ == '__main__':
app = QApplication([])
waiting_spinner = QtWaitingSpinner()
waiting_spinner.show()
waiting_spinner.start()
app.exec()

64
rustfmt.toml Normal file
View File

@ -0,0 +1,64 @@
max_width = 120
hard_tabs = false
tab_spaces = 4
newline_style = "Auto"
use_small_heuristics = "Default"
indent_style = "Block"
wrap_comments = false
format_code_in_doc_comments = false
comment_width = 100
normalize_comments = false
normalize_doc_attributes = false
format_strings = true
format_macro_matchers = true
format_macro_bodies = true
empty_item_single_line = true
struct_lit_single_line = true
fn_single_line = false
where_single_line = true
imports_indent = "Visual"
imports_layout = "Mixed"
imports_granularity="Crate"
group_imports = "StdExternalCrate"
reorder_imports = true
reorder_modules = true
reorder_impl_items = false
type_punctuation_density = "Wide"
space_before_colon = false
space_after_colon = true
spaces_around_ranges = false
binop_separator = "Front"
remove_nested_parens = true
combine_control_expr = true
overflow_delimited_expr = false
struct_field_align_threshold = 0
enum_discrim_align_threshold = 0
match_arm_blocks = true
match_arm_leading_pipes = "Never"
force_multiline_blocks = false
fn_params_layout = "Tall"
brace_style = "SameLineWhere"
control_brace_style = "AlwaysSameLine"
trailing_semicolon = true
trailing_comma = "Vertical"
match_block_trailing_comma = false
blank_lines_upper_bound = 1
blank_lines_lower_bound = 0
edition = "2018"
version = "Two"
inline_attribute_width = 0
merge_derives = true
use_try_shorthand = false
use_field_init_shorthand = false
force_explicit_abi = true
condense_wildcard_suffixes = false
color = "Auto"
unstable_features = false
disable_all_formatting = false
skip_children = false
hide_parse_errors = false
error_on_line_overflow = false
error_on_unformatted = false
ignore = []
emit_mode = "Files"
make_backup = false

View File

@ -1,23 +1,19 @@
use super::{gpio, sys_timer, usb};
use crate::device::flash_store::{self, FlashStore};
use crate::laser_diode::ld_ctrl::{*};
use crate::laser_diode::laser_diode::LdDrive;
use crate::thermostat::max1968::MAX1968;
use crate::thermostat::thermostat::Thermostat;
use crate::net::net::{IpSettings, ServerHandle};
use stm32_eth;
use fugit::ExtU32;
use log::{info, debug};
use stm32f4xx_hal::timer::TimerExt;
use stm32f4xx_hal::{
pac::{CorePeripherals, Peripherals},
use log::{debug, info};
use stm32f4xx_hal::{pac::{CorePeripherals, Peripherals},
rcc::RccExt,
time::MegaHertz,
watchdog::IndependentWatchdog,
};
use crate::DeviceSettings;
use uom::si::electric_current::milliampere;
use uom::si::{electric_current::ampere, f32::ElectricCurrent};
timer::TimerExt,
watchdog::IndependentWatchdog};
use uom::si::{electric_current::{ampere, milliampere},
f32::ElectricCurrent};
use super::{gpio, sys_timer, usb};
use crate::{device::flash_store::{self, FlashStore},
laser_diode::{laser_diode::LdDrive, ld_ctrl::*},
net::net::{IpSettings, ServerHandle},
thermostat::{max1968::MAX1968, thermostat::Thermostat},
DeviceSettings};
#[cfg(not(feature = "semihosting"))]
const WATCHDOG_PERIOD: u32 = 4000;
@ -44,7 +40,8 @@ pub fn bootup(
sys_timer::setup(core_perif.SYST, clocks);
let (mut hw_rev, eth_pins, eth_mgmt_pins, usb, current_source_phy, ad7172_phy, max1968_phy, pd_mon_phy) = gpio::setup(
let (mut hw_rev, eth_pins, eth_mgmt_pins, usb, current_source_phy, ad7172_phy, max1968_phy, pd_mon_phy) =
gpio::setup(
clocks,
perif.TIM4,
perif.GPIOA,
@ -107,7 +104,14 @@ pub fn bootup(
mmc: perif.ETHERNET_MMC,
ptp: perif.ETHERNET_PTP,
};
ServerHandle::new(eth_pins, eth_mgmt_pins, ethernet_parts_in, clocks, mac_addr, ip_settings);
ServerHandle::new(
eth_pins,
eth_mgmt_pins,
ethernet_parts_in,
clocks,
mac_addr,
ip_settings,
);
debug!("Setting Watchdog");
let mut wd = IndependentWatchdog::new(perif.IWDG);

View File

@ -1,6 +1,7 @@
use core::arch::asm;
use cortex_m_rt::pre_init;
use stm32f4xx_hal::pac::{RCC, SYSCFG};
use core::arch::asm;
const DFU_TRIG_MSG: u32 = 0xDECAFBAD;

View File

@ -1,9 +1,7 @@
use log::error;
use stm32f4xx_hal::{
flash::{Error, FlashExt},
pac::FLASH,
};
use sfkv::{Store, StoreBackend};
use stm32f4xx_hal::{flash::{Error, FlashExt},
pac::FLASH};
// Last flash sector is used to avoid overwriting the code in flash.
pub const FLASH_SECTOR: u8 = 11;
@ -34,8 +32,7 @@ impl StoreBackend for FlashBackend {
}
fn program(&mut self, offset: usize, payload: &[u8]) -> Result<(), Self::Error> {
self.flash.unlocked()
.program(get_offset() + offset, payload.iter())
self.flash.unlocked().program(get_offset() + offset, payload.iter())
}
fn backup_space(&self) -> &'static mut [u8] {
@ -53,8 +50,7 @@ pub fn store(flash: FLASH) -> FlashStore {
Ok(_) => {}
Err(e) => {
error!("corrupt store, erasing. error: {:?}", e);
let _ = store.erase()
.map_err(|e| error!("flash erase failed: {:?}", e));
let _ = store.erase().map_err(|e| error!("flash erase failed: {:?}", e));
}
}

View File

@ -1,27 +1,25 @@
use crate::laser_diode::ld_ctrl::{self, LdCtrlPhy};
use crate::laser_diode::max5719;
use crate::laser_diode::ld_pwr_exc_protector::LdPwrExcProtectorPhy;
use crate::thermostat::ad5680;
use crate::thermostat::max1968::{self, MAX1968PinSet, MAX1968Phy, PWM_FREQ_KHZ};
use crate::thermostat::ad7172;
use crate::net::net::EthernetMgmtPins;
use crate::device::hw_rev::{HWRev, HwRevPins};
use stm32_eth::EthPins;
use stm32f4xx_hal::gpio::alt::otg_fs::{Dm, Dp};
use stm32f4xx_hal::{
gpio::{gpioa::*, gpiob::*, gpioc::*, GpioExt, Input, Speed},
use stm32f4xx_hal::{gpio::{alt::otg_fs::{Dm, Dp},
gpioa::*,
gpiob::*,
gpioc::*,
GpioExt, Input, Speed},
otg_fs::USB,
pac::{
GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, OTG_FS_DEVICE, OTG_FS_GLOBAL, OTG_FS_PWRCLK, SPI1, SPI2, SPI3,
TIM4,
},
pac::{GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, OTG_FS_DEVICE, OTG_FS_GLOBAL, OTG_FS_PWRCLK, SPI1, SPI2,
SPI3, TIM4},
rcc::Clocks,
spi::{NoMiso, Spi},
timer::{Channel1, Channel2, Channel3, pwm::PwmExt}
};
timer::{pwm::PwmExt, Channel1, Channel2, Channel3}};
pub type EthernetPins =
EthPins<PA1<Input>, PA7<Input>, PB11<Input>, PB12<Input>, PB13<Input>, PC4<Input>, PC5<Input>>;
use crate::{device::hw_rev::{HWRev, HwRevPins},
laser_diode::{ld_ctrl::{self, LdCtrlPhy},
ld_pwr_exc_protector::LdPwrExcProtectorPhy,
max5719},
net::net::EthernetMgmtPins,
thermostat::{ad5680, ad7172,
max1968::{self, MAX1968Phy, MAX1968PinSet, PWM_FREQ_KHZ}}};
pub type EthernetPins = EthPins<PA1<Input>, PA7<Input>, PB11<Input>, PB12<Input>, PB13<Input>, PC4<Input>, PC5<Input>>;
pub fn setup(
clocks: Clocks,
@ -45,7 +43,7 @@ pub fn setup(
LdCtrlPhy<ld_ctrl::Channel0>,
ad7172::AdcPhy,
MAX1968Phy<max1968::Channel0>,
LdPwrExcProtectorPhy
LdPwrExcProtectorPhy,
) {
let gpioa = gpioa.split();
let gpiob = gpiob.split();
@ -53,14 +51,12 @@ pub fn setup(
let gpiod = gpiod.split();
let gpioe = gpioe.split();
let mut hw_rev = HWRev::detect_hw_rev(
HwRevPins {
let mut hw_rev = HWRev::detect_hw_rev(HwRevPins {
h0: gpioe.pe8.into_input(),
h1: gpioe.pe9.into_input(),
h2: gpioe.pe10.into_input(),
h3: gpioe.pe11.into_input(),
}
);
});
hw_rev.startup_delay_before_gpio_init();
@ -91,17 +87,15 @@ pub fn setup(
eth_mgmt_pins.mdc.set_speed(Speed::VeryHigh);
let current_source_phy = LdCtrlPhy {
dac: max5719::Dac::new(Spi::new(
dac: max5719::Dac::new(
Spi::new(
spi2,
(
gpiob.pb10.into_alternate(),
NoMiso::new(),
gpiob.pb15.into_alternate(),
),
(gpiob.pb10.into_alternate(), NoMiso::new(), gpiob.pb15.into_alternate()),
max5719::SPI_MODE,
max5719::SPI_CLOCK_MHZ.convert(),
&clocks,
), gpiod.pd8.into_push_pull_output(),
),
gpiod.pd8.into_push_pull_output(),
gpiob.pb14.into_push_pull_output(),
),
current_source_short_pin: gpioa.pa4.into_push_pull_output(),
@ -118,18 +112,13 @@ pub fn setup(
Channel2::new(gpiob.pb7),
Channel3::new(gpiob.pb8),
);
let (max_i_neg0, max_v0, max_i_pos0) =
tim4.pwm_hz(pwm_chs, PWM_FREQ_KHZ.convert(), &clocks).split();
let (max_i_neg0, max_v0, max_i_pos0) = tim4.pwm_hz(pwm_chs, PWM_FREQ_KHZ.convert(), &clocks).split();
let max1968_phy = MAX1968Phy::new(MAX1968PinSet {
dac: ad5680::Dac::new(
Spi::new(
spi1,
(
gpiob.pb3.into_alternate(),
NoMiso::new(),
gpiob.pb5.into_alternate(),
),
(gpiob.pb3.into_alternate(), NoMiso::new(), gpiob.pb5.into_alternate()),
ad5680::SPI_MODE,
ad5680::SPI_CLOCK_MHZ.convert(),
&clocks,
@ -147,7 +136,8 @@ pub fn setup(
});
let ad7172_phy = ad7172::Adc::new(
Spi::new(spi3,
Spi::new(
spi3,
(
gpioc.pc10.into_alternate(),
gpioc.pc11.into_alternate(),
@ -155,10 +145,20 @@ pub fn setup(
),
ad7172::SPI_MODE,
ad7172::SPI_CLOCK_MHZ.convert(),
&clocks
&clocks,
),
gpioa.pa15.into_push_pull_output(),
).unwrap();
)
.unwrap();
(hw_rev, eth_pins, eth_mgmt_pins, usb, current_source_phy, ad7172_phy, max1968_phy, pd_mon_phy)
(
hw_rev,
eth_pins,
eth_mgmt_pins,
usb,
current_source_phy,
ad7172_phy,
max1968_phy,
pd_mon_phy,
)
}

View File

@ -1,7 +1,8 @@
use stm32f4xx_hal::gpio::{Input, PE10, PE11, PE8, PE9};
use crate::device::sys_timer::sleep;
use stm32f4xx_hal::signature;
use crc::{Crc, CRC_24_BLE};
use stm32f4xx_hal::{gpio::{Input, PE10, PE11, PE8, PE9},
signature};
use crate::device::sys_timer::sleep;
pub struct HwRevPins {
pub h0: PE8<Input>,
@ -18,12 +19,14 @@ pub struct HWRev {
impl HWRev {
pub fn detect_hw_rev(hwrev_pins: HwRevPins) -> Self {
let (h0, h1, h2, h3) = (
hwrev_pins.h0.is_high(), hwrev_pins.h1.is_high(),
hwrev_pins.h2.is_high(), hwrev_pins.h3.is_high()
hwrev_pins.h0.is_high(),
hwrev_pins.h1.is_high(),
hwrev_pins.h2.is_high(),
hwrev_pins.h3.is_high(),
);
match (h0, h1, h2, h3) {
(true, true, true, true) => HWRev { major: 0, minor: 3 },
(_, _, _, _) => HWRev { major: 0, minor: 0 }
(_, _, _, _) => HWRev { major: 0, minor: 0 },
}
}

View File

@ -3,13 +3,15 @@ pub fn init_log() {
use super::usb;
static USB_LOGGER: usb::Logger = usb::Logger;
let _ = log::set_logger(&USB_LOGGER);
log::set_max_level(log::LevelFilter::Debug);
// log::set_max_level(log::LevelFilter::Debug);
log::set_max_level(log::LevelFilter::Trace);
}
#[cfg(feature = "RTT")]
pub fn init_log() {
use super::rtt_logger;
use rtt_target::rtt_init_print;
use super::rtt_logger;
static RTT_LOGGER: rtt_logger::Logger = rtt_logger::Logger;
rtt_init_print!();
let _ = log::set_logger(&RTT_LOGGER);
@ -18,8 +20,8 @@ pub fn init_log() {
#[cfg(feature = "semihosting")]
pub fn init_log() {
use cortex_m_log::log::{init, Logger};
use cortex_m_log::printer::semihosting::{hio::HStdout, InterruptOk};
use cortex_m_log::{log::{init, Logger},
printer::semihosting::{hio::HStdout, InterruptOk}};
use log::LevelFilter;
static mut LOGGER: Option<Logger<InterruptOk<HStdout>>> = None;
let logger = Logger {

View File

@ -1,9 +1,9 @@
pub mod boot;
pub mod dfu;
pub mod flash_store;
pub mod gpio;
pub mod hw_rev;
pub mod log_setup;
pub mod rtt_logger;
pub mod sys_timer;
pub mod usb;
pub mod flash_store;
pub mod hw_rev;
pub mod dfu;

View File

@ -1,7 +1,6 @@
use core::cell::RefCell;
use core::ops::Deref;
use cortex_m::interrupt::Mutex;
use cortex_m::peripheral::syst::SystClkSource;
use core::{cell::RefCell, ops::Deref};
use cortex_m::{interrupt::Mutex, peripheral::syst::SystClkSource};
use cortex_m_rt::exception;
use stm32f4xx_hal::{pac::SYST, rcc::Clocks};

View File

@ -1,19 +1,14 @@
use core::{
fmt::{self, Write},
mem::MaybeUninit,
};
use core::{fmt::{self, Write},
mem::MaybeUninit};
use cortex_m::interrupt::free;
use log::{Log, Metadata, Record};
use stm32f4xx_hal::{
otg_fs::{UsbBus as Bus, USB},
pac::{interrupt, Interrupt, NVIC},
};
use usb_device::{
use stm32f4xx_hal::{otg_fs::{UsbBus as Bus, USB},
pac::{interrupt, Interrupt, NVIC}};
use usb_device::{class_prelude::UsbBusAllocator,
descriptor::lang_id,
device::StringDescriptors,
class_prelude::UsbBusAllocator,
prelude::{UsbDevice, UsbDeviceBuilder, UsbVidPid},
};
prelude::{UsbDevice, UsbDeviceBuilder, UsbVidPid}};
use usbd_serial::SerialPort;
static mut EP_MEMORY: [u32; 1024] = [0; 1024];
@ -41,7 +36,8 @@ impl State {
.device_release(0x20)
.self_powered(true)
.device_class(usbd_serial::USB_CLASS_CDC)
.strings(&[str_descriptor]).unwrap()
.strings(&[str_descriptor])
.unwrap()
.build();
free(|_| unsafe {

View File

@ -1,21 +1,20 @@
use miniconf::Tree;
use stm32f4xx_hal::timer::CounterUs;
use stm32f4xx_hal::pac::{ADC3, TIM2};
use uom::si::electric_current::ampere;
use uom::si::power::milliwatt;
use crate::laser_diode::ld_ctrl::{LdCtrl, Impedance};
use crate::laser_diode::ld_pwr_exc_protector::{LdPwrExcProtector, self};
use crate::laser_diode::pd_mon_params;
use crate::laser_diode::ld_current_out_ctrl_timer::LdCurrentOutCtrlTimer;
use core::marker::PhantomData;
use crate::device::sys_timer::sleep;
use serde::{Deserialize, Serialize};
use uom::si::{
electric_current::milliampere,
f32::{ElectricPotential, ElectricCurrent, Power},
};
use uom::{si::{ISQ, SI, Quantity}, typenum::*};
use miniconf::Tree;
use serde::{Deserialize, Serialize};
use stm32f4xx_hal::{pac::{ADC3, TIM2},
timer::CounterUs};
use uom::{si::{electric_current::{ampere, milliampere},
f32::{ElectricCurrent, ElectricPotential, Power},
power::milliwatt,
Quantity, ISQ, SI},
typenum::*};
use crate::{device::sys_timer::sleep,
laser_diode::{ld_ctrl::{Impedance, LdCtrl},
ld_current_out_ctrl_timer::LdCurrentOutCtrlTimer,
ld_pwr_exc_protector::{self, LdPwrExcProtector},
pd_mon_params}};
// Volt / Ampere
pub type TransimpedanceUnit = Quantity<ISQ<P2, P1, N3, N2, Z0, Z0, Z0>, SI<f32>, f32>;
@ -89,19 +88,28 @@ pub struct LdDrive{
}
impl LdDrive {
pub fn new(current_source: LdCtrl, pins_adc: ADC3, tim2: CounterUs<TIM2>, phy: ld_pwr_exc_protector::LdPwrExcProtectorPhy)-> Self {
pub fn new(
current_source: LdCtrl,
pins_adc: ADC3,
tim2: CounterUs<TIM2>,
phy: ld_pwr_exc_protector::LdPwrExcProtectorPhy,
) -> Self {
LdPwrExcProtector::setup(pins_adc, phy);
LdCurrentOutCtrlTimer::setup(tim2);
LdDrive {
ctrl: current_source,
settings: Settings::default()
settings: Settings::default(),
}
}
pub fn setup(&mut self) {
LdPwrExcProtector::pwr_off();
self.ctrl.set_i(ElectricCurrent::new::<milliampere>(0.0), Settings::LD_DRIVE_TRANSIMPEDANCE, Settings::DAC_OUT_V_MAX);
self.ctrl.set_i(
ElectricCurrent::new::<milliampere>(0.0),
Settings::LD_DRIVE_TRANSIMPEDANCE,
Settings::DAC_OUT_V_MAX,
);
self.set_ld_drive_current_limit(Settings::LD_CURRENT_MAX);
LdCurrentOutCtrlTimer::reset();
self.ld_short();
@ -124,7 +132,11 @@ impl LdDrive{
pub fn power_up(&mut self) {
let prev_i_set = self.settings.ld_drive_current;
LdCurrentOutCtrlTimer::reset();
let _ = self.ctrl.set_i(ElectricCurrent::new::<milliampere>(0.0), Settings::LD_DRIVE_TRANSIMPEDANCE, Settings::DAC_OUT_V_MAX);
let _ = self.ctrl.set_i(
ElectricCurrent::new::<milliampere>(0.0),
Settings::LD_DRIVE_TRANSIMPEDANCE,
Settings::DAC_OUT_V_MAX,
);
// Wait for the DAC to reset its voltage back to 0V
sleep(35);
LdPwrExcProtector::pwr_on_and_arm_protection();
@ -144,7 +156,9 @@ impl LdDrive{
}
pub fn get_pd_pwr(&mut self) -> Power {
self.settings.pd_mon_params.get_ld_pwr_from_ld_i(LdPwrExcProtector::get_status().v * Settings::PD_MON_TRANSCONDUCTANCE)
self.settings
.pd_mon_params
.get_ld_pwr_from_ld_i(LdPwrExcProtector::get_status().v * Settings::PD_MON_TRANSCONDUCTANCE)
}
pub fn ld_set_i(&mut self, i: ElectricCurrent) {
@ -155,13 +169,13 @@ impl LdDrive{
pub fn poll_and_update_output_current(&mut self) -> ElectricCurrent {
match LdCurrentOutCtrlTimer::get_irq_status() {
Some(i_set) => {
let i_set = self.ctrl.set_i(i_set, Settings::LD_DRIVE_TRANSIMPEDANCE, Settings::DAC_OUT_V_MAX);
let i_set = self
.ctrl
.set_i(i_set, Settings::LD_DRIVE_TRANSIMPEDANCE, Settings::DAC_OUT_V_MAX);
LdCurrentOutCtrlTimer::clear_alarm_and_resume_listening();
i_set
}
None => {
ElectricCurrent::new::<ampere>(0.0)
}
None => ElectricCurrent::new::<ampere>(0.0),
}
}
@ -183,8 +197,8 @@ impl LdDrive{
}
pub fn set_ld_power_limit(&mut self, pwr_limit: Power) {
LdPwrExcProtector::set_trigger_threshold_v(self.settings.pd_mon_params
.get_ld_i_from_ld_pwr(pwr_limit) / Settings::PD_MON_TRANSCONDUCTANCE
LdPwrExcProtector::set_trigger_threshold_v(
self.settings.pd_mon_params.get_ld_i_from_ld_pwr(pwr_limit) / Settings::PD_MON_TRANSCONDUCTANCE,
);
self.settings.ld_pwr_limit = pwr_limit;
}
@ -221,8 +235,14 @@ impl LdDrive{
let settings = self.settings;
LdSettingsSummary {
default_pwr_on: self.settings.default_pwr_on,
ld_drive_current: LdSettingsSummaryField { value: settings.ld_drive_current, max: Settings::LD_CURRENT_MAX},
ld_drive_current_limit: LdSettingsSummaryField { value: settings.ld_drive_current_limit, max: Settings::LD_CURRENT_MAX},
ld_drive_current: LdSettingsSummaryField {
value: settings.ld_drive_current,
max: Settings::LD_CURRENT_MAX,
},
ld_drive_current_limit: LdSettingsSummaryField {
value: settings.ld_drive_current_limit,
max: Settings::LD_CURRENT_MAX,
},
pd_mon_params: settings.pd_mon_params,
ld_pwr_limit: settings.ld_pwr_limit,
ld_terms_short: settings.ld_terms_short,

View File

@ -1,19 +1,15 @@
use serde::{Deserialize, Serialize};
use stm32f4xx_hal::{
gpio::{gpioa::*, gpiob::*, gpiod::*, Input, Output, PushPull},
hal::{spi::SpiBus, digital::{OutputPin, InputPin}},
use stm32f4xx_hal::{gpio::{gpioa::*, gpiob::*, gpiod::*, Input, Output, PushPull},
hal::{digital::{InputPin, OutputPin},
spi::SpiBus},
pac::SPI2,
spi::Spi,
};
spi::Spi};
use uom::si::{electric_current::ampere,
f32::{ElectricCurrent, ElectricPotential},
ratio::ratio};
use uom::si::{
ratio::ratio,
f32::{ElectricPotential, ElectricCurrent},
electric_current::ampere,
};
use crate::laser_diode::max5719::{self, Dac};
use crate::laser_diode::laser_diode::TransimpedanceUnit;
use crate::laser_diode::{laser_diode::TransimpedanceUnit,
max5719::{self, Dac}};
#[derive(Deserialize, Serialize, Debug, Clone, Copy)]
pub enum Impedance {
@ -32,7 +28,7 @@ pub trait ChannelPins {
pub struct LdCtrlPhy<C: ChannelPins> {
pub dac: Dac<C::Max5719Spi, C::Max5719Cs, C::Max5719Load>,
pub current_source_short_pin: C::CurrentSourceShort,
pub termination_status_pin: C::TerminationStatus
pub termination_status_pin: C::TerminationStatus,
}
pub struct Channel0;
@ -57,7 +53,7 @@ impl LdCtrl {
pub fn new(phy_ch0: LdCtrlPhy<Channel0>) -> Self {
LdCtrl {
phy: phy_ch0,
i_set: ElectricCurrent::new::<ampere>(0.0)
i_set: ElectricCurrent::new::<ampere>(0.0),
}
}
@ -74,20 +70,23 @@ impl LdCtrl {
pub fn get_lf_mod_in_impedance(&mut self) -> Impedance {
if self.phy.termination_status_pin.is_high() {
Impedance::Is50Ohm
}
else {
} else {
Impedance::Not50Ohm
}
}
pub fn set_dac(&mut self, voltage: ElectricPotential, dac_out_v_max: ElectricPotential) -> ElectricPotential {
let value = ((voltage / dac_out_v_max).get::<ratio>()
* (max5719::MAX_VALUE as f32)) as u32;
let value = ((voltage / dac_out_v_max).get::<ratio>() * (max5719::MAX_VALUE as f32)) as u32;
self.phy.dac.set(value).unwrap();
value as f32 * dac_out_v_max / max5719::MAX_VALUE as f32
}
pub fn set_i(&mut self, current: ElectricCurrent, transimpedance: TransimpedanceUnit, dac_out_v_max: ElectricPotential) -> ElectricCurrent {
pub fn set_i(
&mut self,
current: ElectricCurrent,
transimpedance: TransimpedanceUnit,
dac_out_v_max: ElectricPotential,
) -> ElectricCurrent {
self.i_set = self.set_dac(current * transimpedance, dac_out_v_max) / transimpedance;
self.i_set
}

View File

@ -1,10 +1,11 @@
use stm32f4xx_hal::timer::{Event, Counter};
use stm32f4xx_hal::pac::{interrupt, Interrupt, TIM2};
use stm32f4xx_hal::Listen;
use uom::si::{f32::ElectricCurrent, electric_current::ampere};
use fugit::{TimerDurationU32, KilohertzU32};
use core::marker::PhantomData;
use fugit::{KilohertzU32, TimerDurationU32};
use log::debug;
use stm32f4xx_hal::{pac::{interrupt, Interrupt, TIM2},
timer::{Counter, Event},
Listen};
use uom::si::{electric_current::ampere, f32::ElectricCurrent};
pub struct LdCurrentOutCtrlTimer {
target_i: ElectricCurrent,
@ -32,14 +33,12 @@ impl LdCurrentOutCtrlTimer {
cortex_m::peripheral::NVIC::unmask(Interrupt::TIM2);
}
unsafe {
LD_CURRENT_OUT_CTRL_TIMER = Some(
LdCurrentOutCtrlTimer {
LD_CURRENT_OUT_CTRL_TIMER = Some(LdCurrentOutCtrlTimer {
target_i: ElectricCurrent::new::<ampere>(0.0),
now_i: ElectricCurrent::new::<ampere>(0.0),
timer: tim2,
timeout: false
}
);
timeout: false,
});
}
}
@ -62,8 +61,7 @@ impl LdCurrentOutCtrlTimer {
ld_current_out_ctrl_timer.target_i = target;
ld_current_out_ctrl_timer.now_i = now;
ld_current_out_ctrl_timer.timer.listen(Event::Update);
}
)
})
}
}
@ -91,7 +89,9 @@ impl LdCurrentOutCtrlTimer {
if let Some(ref mut ld_current_out_ctrl_timer) = LdCurrentOutCtrlTimer::get() {
match ld_current_out_ctrl_timer.timer.wait() {
Ok(_) => {}
Err(_) => {debug!("LD CTRL TIMER Interrupt is not present when clear_irq_flag() is called")}
Err(_) => {
debug!("LD CTRL TIMER Interrupt is not present when clear_irq_flag() is called")
}
}
}
}
@ -100,12 +100,13 @@ impl LdCurrentOutCtrlTimer {
if let Some(ref mut ld_current_out_ctrl_timer) = LdCurrentOutCtrlTimer::get() {
let update = ld_current_out_ctrl_timer.now_i != ld_current_out_ctrl_timer.target_i;
if ld_current_out_ctrl_timer.target_i > ld_current_out_ctrl_timer.now_i {
ld_current_out_ctrl_timer.now_i = (ld_current_out_ctrl_timer.now_i + LdCurrentOutCtrlTimer::STEP_SIZE).min(ld_current_out_ctrl_timer.target_i);
ld_current_out_ctrl_timer.now_i = (ld_current_out_ctrl_timer.now_i + LdCurrentOutCtrlTimer::STEP_SIZE)
.min(ld_current_out_ctrl_timer.target_i);
} else {
ld_current_out_ctrl_timer.now_i = (ld_current_out_ctrl_timer.now_i - LdCurrentOutCtrlTimer::STEP_SIZE)
.max(ld_current_out_ctrl_timer.target_i);
}
else {
ld_current_out_ctrl_timer.now_i = (ld_current_out_ctrl_timer.now_i - LdCurrentOutCtrlTimer::STEP_SIZE).max(ld_current_out_ctrl_timer.target_i);
}
return update
return update;
}
false
}

View File

@ -1,15 +1,8 @@
use stm32f4xx_hal::pac;
use stm32f4xx_hal::rcc::Enable;
use stm32f4xx_hal::{
use stm32f4xx_hal::{gpio::{gpioa::PA3, gpiod::PD9, Analog, Output, PushPull},
interrupt, pac,
pac::{ADC3, NVIC},
gpio::{Analog, Output, PushPull, gpioa::PA3, gpiod::PD9},
interrupt,
};
use uom::si::{
electric_potential::millivolt,
f32::ElectricPotential,
ratio::ratio
};
rcc::Enable};
use uom::si::{electric_potential::millivolt, f32::ElectricPotential, ratio::ratio};
// 12 bit Resolution
const MAX_SAMPLE: u16 = 4095;
@ -72,34 +65,46 @@ impl LdPwrExcProtector {
pac_adc.sqr2.reset();
pac_adc.sqr3.reset();
pac_adc.cr1.write(|w| w
pac_adc.cr1.write(|w| {
w
// 12 Bit Resolution
.res().twelve_bit()
.res()
.twelve_bit()
// Set Analog Watchdog to guard Single Regular Channel
.awden().enabled()
.awdsgl().single_channel()
.jawden().disabled()
.awden()
.enabled()
.awdsgl()
.single_channel()
.jawden()
.disabled()
// Disable Analog Watchdog Interrupt
.awdie().disabled()
.awdie()
.disabled()
// Set Analog Watchdog to monitor Pd Mon Pin
.awdch().variant(PD_MON_ADC_CH_ID)
);
pac_adc.cr2.write(|w| w
.awdch()
.variant(PD_MON_ADC_CH_ID)
});
pac_adc.cr2.write(|w| {
w
// Continous Conversion Mode
.cont().set_bit()
.cont()
.set_bit()
// Power up ADC
.adon().set_bit()
.adon()
.set_bit()
// Set data alignment to the right
.align().right()
.align()
.right()
// End of conversion selection: Each Sequence
.eocs().each_sequence()
.exten().disabled()
.extsel().tim1cc1()
);
.eocs()
.each_sequence()
.exten()
.disabled()
.extsel()
.tim1cc1()
});
// Set the Conversion Sequence to include Pd Mon Pin
pac_adc.sqr3.write(|w| w
.sq1().variant(PD_MON_ADC_CH_ID)
);
pac_adc.sqr3.write(|w| w.sq1().variant(PD_MON_ADC_CH_ID));
// Set all sampling channels to have fastest sampling interval
pac_adc.smpr1.reset();
pac_adc.smpr2.reset();
@ -110,21 +115,17 @@ impl LdPwrExcProtector {
pac_adc.ltr.write(|w| w.lt().variant(0));
// SWStart should only be set when ADON = 1. Otherwise no conversion is launched.
pac_adc.cr2.modify(|_, w| w
.swstart().set_bit()
);
pac_adc.cr2.modify(|_, w| w.swstart().set_bit());
phy.pwr_en_ch0.set_low();
unsafe {
LD_PWR_EXC_PROTECTOR = Some(
LdPwrExcProtector {
LD_PWR_EXC_PROTECTOR = Some(LdPwrExcProtector {
pac: pac_adc,
phy: phy,
alarm_status: Status::default(),
calibrated_vdda: 3300,
}
);
});
}
}
@ -134,14 +135,17 @@ impl LdPwrExcProtector {
fn convert_sample_to_volt(sample: u16) -> ElectricPotential {
if let Some(ref mut wdg) = LdPwrExcProtector::get() {
return ElectricPotential::new::<millivolt>(((u32::from(sample) * wdg.calibrated_vdda) / u32::from(MAX_SAMPLE)) as f32)
return ElectricPotential::new::<millivolt>(
((u32::from(sample) * wdg.calibrated_vdda) / u32::from(MAX_SAMPLE)) as f32,
);
}
ElectricPotential::new::<millivolt>(0.0)
}
pub fn set_trigger_threshold_v(htr: ElectricPotential) {
if let Some(ref mut wdg) = LdPwrExcProtector::get() {
let code: u32 = ((htr / (ElectricPotential::new::<millivolt>(wdg.calibrated_vdda as f32))).get::<ratio>() * (MAX_SAMPLE as f32)) as u32;
let code: u32 = ((htr / (ElectricPotential::new::<millivolt>(wdg.calibrated_vdda as f32))).get::<ratio>()
* (MAX_SAMPLE as f32)) as u32;
wdg.pac.htr.write(|w| unsafe { w.bits(code) });
}
}
@ -155,7 +159,7 @@ impl LdPwrExcProtector {
pub fn get_status() -> Status {
if let Some(ref mut wdg) = LdPwrExcProtector::get() {
wdg.alarm_status.v = LdPwrExcProtector::convert_sample_to_volt(wdg.pac.dr.read().data().bits());
return wdg.alarm_status.clone()
return wdg.alarm_status.clone();
}
Status::default()
}
@ -179,25 +183,19 @@ impl LdPwrExcProtector {
fn enable_watchdog_interrupt() {
if let Some(ref mut wdg) = LdPwrExcProtector::get() {
wdg.pac.cr1.modify(|_, w| w
.awdie().set_bit()
);
wdg.pac.cr1.modify(|_, w| w.awdie().set_bit());
}
}
fn disable_watchdog_interrupt() {
if let Some(ref mut wdg) = LdPwrExcProtector::get() {
wdg.pac.cr1.modify(|_, w| w
.awdie().clear_bit()
);
wdg.pac.cr1.modify(|_, w| w.awdie().clear_bit());
}
}
fn clear_interrupt_bit() {
if let Some(ref mut wdg) = LdPwrExcProtector::get() {
wdg.pac.sr.modify(|_, w| w
.awd().clear_bit()
);
wdg.pac.sr.modify(|_, w| w.awd().clear_bit());
}
}
@ -232,6 +230,5 @@ fn ADC(){
// Disable interrupt to avoid getting stuck in infinite loop
LdPwrExcProtector::disable_watchdog_interrupt();
LdPwrExcProtector::clear_interrupt_bit();
}
)
})
}

View File

@ -1,9 +1,8 @@
use crate::device::sys_timer::sleep;
use fugit::MegahertzU32;
use stm32f4xx_hal::{
hal::{spi::SpiBus, digital::OutputPin},
spi,
};
use stm32f4xx_hal::{hal::{digital::OutputPin, spi::SpiBus},
spi};
use crate::device::sys_timer::sleep;
pub const SPI_MODE: spi::Mode = spi::Mode {
polarity: spi::Polarity::IdleLow,

View File

@ -1,6 +1,6 @@
pub mod ld_ctrl;
pub mod max5719;
pub mod laser_diode;
pub mod pd_mon_params;
pub mod ld_pwr_exc_protector;
pub mod ld_ctrl;
pub mod ld_current_out_ctrl_timer;
pub mod ld_pwr_exc_protector;
pub mod max5719;
pub mod pd_mon_params;

View File

@ -1,14 +1,11 @@
use core::{f32::NAN, marker::PhantomData};
use serde::{Deserialize, Serialize};
use uom::si::{
f32::{
ElectricCurrent,
Power
},
electric_current::microampere,
};
use uom::{si::{ISQ, SI, Quantity}, typenum::*};
use miniconf::Tree;
use serde::{Deserialize, Serialize};
use uom::{si::{electric_current::microampere,
f32::{ElectricCurrent, Power},
Quantity, ISQ, SI},
typenum::*};
// Ampere / Watt
pub type ResponsitivityUnit = Quantity<ISQ<N2, N1, P3, P1, Z0, Z0, Z0>, SI<f32>, f32>;
@ -42,7 +39,11 @@ impl Parameters {
impl Default for Parameters {
fn default() -> Self {
Parameters {
responsitivity: ResponsitivityUnit {dimension: PhantomData, units: PhantomData, value: NAN},
responsitivity: ResponsitivityUnit {
dimension: PhantomData,
units: PhantomData,
value: NAN,
},
i_dark: ElectricCurrent::new::<microampere>(0.0),
}
}

View File

@ -2,22 +2,24 @@
#![cfg_attr(not(test), no_std)]
use cortex_m_rt::entry;
use log::{info, debug};
use log::{debug, info};
use stm32f4xx_hal::pac::{CorePeripherals, Peripherals};
mod device;
mod laser_diode;
mod thermostat;
mod net;
mod thermostat;
use core::ptr::addr_of_mut;
use device::{boot::bootup, log_setup, sys_timer};
use crate::net::net::IpSettings;
use serde::{Deserialize, Serialize};
use stm32f4xx_hal::pac::SCB;
// If RTT is used, print panic info through RTT
#[cfg(all(feature = "RTT", not(test)))]
use {core::panic::PanicInfo, rtt_target::rprintln};
use crate::net::net::IpSettings;
#[cfg(all(feature = "RTT", not(test)))]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
@ -57,10 +59,10 @@ fn main() -> ! {
let core_perif = CorePeripherals::take().unwrap();
let perif = Peripherals::take().unwrap();
let (mut wd, mut flash_store, mut laser, mut thermostat,) = bootup(core_perif, perif);
let (mut wd, mut flash_store, mut laser, mut thermostat) = bootup(core_perif, perif);
let mut device_settings = DeviceSettings {
ip_settings: IpSettings::default()
ip_settings: IpSettings::default(),
};
let mut active_report: [bool; net::net::NUM_OF_SOCKETS] = [false; net::net::NUM_OF_SOCKETS];
@ -72,7 +74,7 @@ fn main() -> ! {
loop {
wd.feed();
if !net::net::eth_poll_link_status_and_update_link_speed() {
if net::net::eth_poll_link_status_and_update_link_speed() {
active_report = [false; net::net::NUM_OF_SOCKETS];
}
@ -153,22 +155,24 @@ fn main() -> ! {
net::net::for_each(|mut socket, id| {
if net::net::eth_is_socket_active(socket) && net::net::eth_is_socket_connected(socket) {
if active_report[id] {
net::cmd_handler::send_status_report(eth_data_buffer, &mut laser, &mut thermostat, &mut socket);
net::cmd_handler::send_status_report(
eth_data_buffer,
&mut laser,
&mut thermostat,
&mut socket,
);
}
}
else {
} else {
active_report[id] = false;
}
});
thermostat.start_tec_readings_conversion();
}
cortex_m::interrupt::free(|cs|
{
cortex_m::interrupt::free(|cs| {
eth_is_pending = net::net::is_pending(cs);
net::net::clear_pending(cs);
}
);
});
if eth_is_pending {
net::net::for_each(|mut socket, id| {
if net::net::eth_is_socket_active(socket) && net::net::eth_is_socket_connected(socket) {
@ -177,7 +181,16 @@ fn main() -> ! {
info!("Ts: {:?}", sys_timer::now());
debug!("Number of bytes recv: {:?}", bytes);
// State Transition
net::cmd_handler::execute_cmd(eth_data_buffer, bytes, &mut socket, &mut laser, &mut thermostat, &mut state, &mut device_settings, &mut active_report[id]);
net::cmd_handler::execute_cmd(
eth_data_buffer,
bytes,
&mut socket,
&mut laser,
&mut thermostat,
&mut state,
&mut device_settings,
&mut active_report[id],
);
}
}
})
@ -232,7 +245,12 @@ fn main() -> ! {
thermostat.power_down();
net::net::for_each(|mut socket, _| {
if net::net::eth_is_socket_active(socket) {
net::cmd_handler::send_response(eth_data_buffer, net::cmd_handler::ResponseEnum::HardReset, None, &mut socket);
net::cmd_handler::send_response(
eth_data_buffer,
net::cmd_handler::ResponseEnum::HardReset,
None,
&mut socket,
);
}
});
}

View File

@ -1,26 +1,24 @@
use core::{fmt::Debug, marker::PhantomData};
use log::{debug, info};
use miniconf::{JsonCoreSlash, Tree};
use serde::{Deserialize, Serialize};
use uom::si::{
electric_current::{ampere, ElectricCurrent},
use smoltcp::iface::SocketHandle;
use uom::si::{electric_current::{ampere, ElectricCurrent},
electric_potential::{volt, ElectricPotential},
electrical_resistance::{ohm, ElectricalResistance},
power::{watt, Power},
thermodynamic_temperature::{degree_celsius, ThermodynamicTemperature}
};
use crate::{laser_diode::{laser_diode::{
LdDrive, LdSettingsSummary, StatusReport as LdStatusReport},
pd_mon_params::ResponsitivityUnit
},
thermodynamic_temperature::{degree_celsius, ThermodynamicTemperature}};
use crate::{device::{dfu, sys_timer},
laser_diode::{laser_diode::{LdDrive, LdSettingsSummary, StatusReport as LdStatusReport},
pd_mon_params::ResponsitivityUnit},
net::net,
thermostat::{ad7172::FilterType, thermostat::{StatusReport as TecStatusReport, TempAdcFilter}}
};
use crate::thermostat::thermostat::{Thermostat, ThermostatSettingsSummary};
use crate::thermostat::pid_state::PidSettings::*;
use crate::device::{dfu, sys_timer};
use log::{info, debug};
use crate::{DeviceSettings, State, IpSettings};
use smoltcp::iface::SocketHandle;
thermostat::{ad7172::FilterType,
pid_state::PidSettings::*,
thermostat::{StatusReport as TecStatusReport, TempAdcFilter, Thermostat,
ThermostatSettingsSummary}},
DeviceSettings, IpSettings, State};
#[derive(Deserialize, Serialize, Copy, Clone, Default, Debug)]
pub enum ResponseEnum {
@ -54,7 +52,7 @@ impl Default for Response<'static>{
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)]
pub struct ResponseObj<'a> {
#[serde(borrow)]
json: Response<'a>
json: Response<'a>,
}
#[derive(Deserialize, Serialize, Copy, Clone, Default, Debug)]
@ -146,7 +144,7 @@ pub struct CmdJsonObj{
}
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)]
pub struct Cmd {
json: CmdJsonObj
json: CmdJsonObj,
}
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)]
@ -159,7 +157,7 @@ pub struct StatusReport {
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)]
pub struct StatusReportObj {
json: StatusReport
json: StatusReport,
}
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)]
@ -171,7 +169,7 @@ pub struct SettingsSummary {
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)]
pub struct SettingsSummaryObj {
json: SettingsSummary
json: SettingsSummary,
}
pub fn send_response(buffer: &mut [u8], msg_type: ResponseEnum, msg: MsgType, socket: &mut SocketHandle) {
@ -179,7 +177,7 @@ pub fn send_response(buffer: &mut [u8], msg_type: ResponseEnum, msg: MsgType, so
json: Response {
msg_type: msg_type,
msg: msg,
}
},
};
debug!("{:?}", response.json);
@ -189,13 +187,18 @@ pub fn send_response(buffer: &mut [u8], msg_type: ResponseEnum, msg: MsgType, so
net::eth_send(buffer, num_bytes, *socket);
}
pub fn send_settings_summary(buffer: &mut [u8], laser: &mut LdDrive, thermostat: &mut Thermostat, socket: &mut SocketHandle){
pub fn send_settings_summary(
buffer: &mut [u8],
laser: &mut LdDrive,
thermostat: &mut Thermostat,
socket: &mut SocketHandle,
) {
let settings_summary = SettingsSummaryObj {
json: SettingsSummary {
msg_type: ResponseEnum::Settings,
laser: laser.get_settings_summary(),
thermostat: thermostat.get_settings_summary(),
}
},
};
let mut num_bytes = settings_summary.get_json("/json", buffer).unwrap();
buffer[num_bytes] = b'\n';
@ -203,15 +206,19 @@ pub fn send_settings_summary(buffer: &mut [u8], laser: &mut LdDrive, thermostat:
net::eth_send(buffer, num_bytes, *socket);
}
pub fn send_status_report(buffer: &mut [u8], laser: &mut LdDrive, thermostat: &mut Thermostat, socket: &mut SocketHandle){
pub fn send_status_report(
buffer: &mut [u8],
laser: &mut LdDrive,
thermostat: &mut Thermostat,
socket: &mut SocketHandle,
) {
let status_report = StatusReportObj {
json: StatusReport {
ts: sys_timer::now(),
msg_type: ResponseEnum::Report,
laser: laser.get_status_report(),
thermostat: thermostat.get_status_report(),
}
},
};
let mut num_bytes = status_report.get_json("/json", buffer).unwrap();
buffer[num_bytes] = b'\n';
@ -222,20 +229,29 @@ pub fn send_status_report(buffer: &mut [u8], laser: &mut LdDrive, thermostat: &m
// Use a minimal struct for high speed cmd ctrl to reduce processing overhead
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)]
pub struct TecSetICmdJson {
tec_set_i: f32
tec_set_i: f32,
}
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Default, Tree)]
pub struct TecSetICmd {
json: TecSetICmdJson
json: TecSetICmdJson,
}
/// Miniconf is very slow in debug builds (~3-4ms of cmd decoding time).
/// Make sure kirdy's firmware is flashed with release builds.
/// The received message must contain only one json cmd. TCP client should set TCP_NODELAY or equivalent flag in its TCP Socket
/// Settings to avoid unwanted buffering on TX Data and minimize TX latency.
pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, socket: &mut SocketHandle, laser: &mut LdDrive, thermostat: &mut Thermostat, state: &mut State, device_settings: &mut DeviceSettings, active_report: &mut bool){
pub fn execute_cmd(
buffer: &mut [u8],
buffer_size: usize,
socket: &mut SocketHandle,
laser: &mut LdDrive,
thermostat: &mut Thermostat,
state: &mut State,
device_settings: &mut DeviceSettings,
active_report: &mut bool,
) {
let mut cmd = TecSetICmd {
json: TecSetICmdJson::default()
json: TecSetICmdJson::default(),
};
match cmd.set_json("/json", &buffer[0..buffer_size]) {
Ok(_) => {
@ -247,27 +263,33 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, socket: &mut SocketHan
}
let mut cmd = Cmd {
json: CmdJsonObj::default()
json: CmdJsonObj::default(),
};
match cmd.set_json("/json", &buffer[0..buffer_size]) {
Ok(_) => {
info!("############ Laser Diode Command Received {:?}", cmd.json.laser_diode_cmd);
info!(
"############ Laser Diode Command Received {:?}",
cmd.json.laser_diode_cmd
);
info!("############ Thermostat Command Received {:?}", cmd.json.thermostat_cmd);
info!("############ Device Command Received {:?}", cmd.json.device_cmd);
match cmd.json.device_cmd {
Some(DeviceCmd::SetIPSettings) => {
match cmd.json.ip_settings {
Some(DeviceCmd::SetIPSettings) => match cmd.json.ip_settings {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
device_settings.ip_settings = val;
*state = State::SaveDeviceSettings;
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_IP_SETTINGS), socket);
}
}
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_IP_SETTINGS),
socket,
);
}
},
Some(DeviceCmd::Dfu) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
unsafe {
@ -275,17 +297,20 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, socket: &mut SocketHan
}
*state = State::HardReset;
}
Some(DeviceCmd::SetActiveReportMode) => {
match cmd.json.data_bool{
Some(DeviceCmd::SetActiveReportMode) => match cmd.json.data_bool {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
*active_report = val;
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_BOOL), socket);
}
}
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_BOOL),
socket,
);
}
},
Some(DeviceCmd::GetStatusReport) => {
send_status_report(buffer, laser, thermostat, socket);
}
@ -312,18 +337,20 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, socket: &mut SocketHan
}
match cmd.json.laser_diode_cmd {
Some(LdCmdEnum::SetDefaultPowerOn) => {
match cmd.json.data_bool {
Some(LdCmdEnum::SetDefaultPowerOn) => match cmd.json.data_bool {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
laser.set_default_pwr_on(val);
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_BOOL), socket);
}
}
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_BOOL),
socket,
);
}
},
Some(LdCmdEnum::PowerUp) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
laser.power_up()
@ -340,61 +367,80 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, socket: &mut SocketHan
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
laser.ld_open();
}
Some(LdCmdEnum::SetI) => {
match cmd.json.data_f32 {
Some(LdCmdEnum::SetI) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
laser.ld_set_i(ElectricCurrent::new::<ampere>(val));
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
}
}
Some(LdCmdEnum::SetISoftLimit) => {
match cmd.json.data_f32 {
},
Some(LdCmdEnum::SetISoftLimit) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
laser.set_ld_drive_current_limit(ElectricCurrent::new::<ampere>(val))
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
}
}
Some(LdCmdEnum::SetPdResponsitivity) => {
match cmd.json.data_f32 {
},
Some(LdCmdEnum::SetPdResponsitivity) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
laser.set_pd_responsitivity(ResponsitivityUnit {dimension: PhantomData, units: PhantomData, value: val})
laser.set_pd_responsitivity(ResponsitivityUnit {
dimension: PhantomData,
units: PhantomData,
value: val,
})
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
}
}
Some(LdCmdEnum::SetPdDarkCurrent) => {
match cmd.json.data_f32 {
},
Some(LdCmdEnum::SetPdDarkCurrent) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
laser.set_pd_dark_current(ElectricCurrent::new::<ampere>(val))
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
}
}
Some(LdCmdEnum::SetLdPwrLimit) => {
match cmd.json.data_f32 {
},
Some(LdCmdEnum::SetLdPwrLimit) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
laser.set_ld_power_limit(Power::new::<watt>(val))
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
}
}
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
},
Some(LdCmdEnum::ClearAlarm) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
laser.pd_mon_clear_alarm()
@ -407,17 +453,20 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, socket: &mut SocketHan
}
match cmd.json.thermostat_cmd {
Some(ThermostatCmdEnum::SetDefaultPowerOn) => {
match cmd.json.data_bool {
Some(ThermostatCmdEnum::SetDefaultPowerOn) => match cmd.json.data_bool {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_default_pwr_on(val);
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
}
}
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
},
Some(ThermostatCmdEnum::PowerUp) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.power_up()
@ -426,61 +475,76 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, socket: &mut SocketHan
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.power_down()
}
Some(ThermostatCmdEnum::SetTecMaxV) => {
match cmd.json.data_f32 {
Some(ThermostatCmdEnum::SetTecMaxV) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_max_v(ElectricPotential::new::<volt>(val));
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
}
}
Some(ThermostatCmdEnum::SetTecMaxIPos) => {
match cmd.json.data_f32 {
},
Some(ThermostatCmdEnum::SetTecMaxIPos) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_max_i_pos(ElectricCurrent::new::<ampere>(val));
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
}
}
Some(ThermostatCmdEnum::SetTecMaxINeg) => {
match cmd.json.data_f32 {
},
Some(ThermostatCmdEnum::SetTecMaxINeg) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_max_i_pos(ElectricCurrent::new::<ampere>(val));
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
}
}
Some(ThermostatCmdEnum::SetTecIOut) => {
match cmd.json.data_f32 {
},
Some(ThermostatCmdEnum::SetTecIOut) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_i(ElectricCurrent::new::<ampere>(val));
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
}
}
Some(ThermostatCmdEnum::SetTemperatureSetpoint) => {
match cmd.json.data_f32 {
},
Some(ThermostatCmdEnum::SetTemperatureSetpoint) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_temperature_setpoint(ThermodynamicTemperature::new::<degree_celsius>(val));
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
}
}
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
},
Some(ThermostatCmdEnum::SetPidEngage) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_pid_engaged(true);
@ -489,179 +553,222 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, socket: &mut SocketHan
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_pid_engaged(false);
}
Some(ThermostatCmdEnum::SetPidKp) => {
match cmd.json.data_f32 {
Some(ThermostatCmdEnum::SetPidKp) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_pid(Kp, val);
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
}
}
Some(ThermostatCmdEnum::SetPidKi) => {
match cmd.json.data_f32 {
},
Some(ThermostatCmdEnum::SetPidKi) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_pid(Ki, val);
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
}
}
Some(ThermostatCmdEnum::SetPidKd) => {
match cmd.json.data_f32 {
},
Some(ThermostatCmdEnum::SetPidKd) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_pid(Kd, val);
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
}
}
Some(ThermostatCmdEnum::SetPidOutMin) => {
match cmd.json.data_f32 {
},
Some(ThermostatCmdEnum::SetPidOutMin) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_pid(Min, val);
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
}
}
Some(ThermostatCmdEnum::SetPidOutMax) => {
match cmd.json.data_f32 {
},
Some(ThermostatCmdEnum::SetPidOutMax) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_pid(Max, val);
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
}
}
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
},
Some(ThermostatCmdEnum::SetPidUpdateInterval) => {
send_response(buffer, ResponseEnum::InvalidCmd, None, socket);
debug!("Not supported Yet")
}
Some(ThermostatCmdEnum::ConfigTempAdcFilter) => {
match cmd.json.temp_adc_filter {
Some(val) => {
match val.filter_type {
FilterType::Sinc5Sinc1With50hz60HzRejection => {
match val.sinc5sinc1postfilter {
Some(ThermostatCmdEnum::ConfigTempAdcFilter) => match cmd.json.temp_adc_filter {
Some(val) => match val.filter_type {
FilterType::Sinc5Sinc1With50hz60HzRejection => match val.sinc5sinc1postfilter {
Some(val2) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_temp_adc_sinc5_sinc1_with_postfilter(0, val2);
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_POSTFILTER), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_POSTFILTER),
socket,
);
}
}
}
FilterType::Sinc5Sinc1 => {
match val.sinc5sinc1odr {
},
FilterType::Sinc5Sinc1 => match val.sinc5sinc1odr {
Some(val2) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_temp_adc_sinc5_sinc1_filter(0, val2);
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_SINC5SINC1ODR), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_SINC5SINC1ODR),
socket,
);
}
}
}
FilterType::Sinc3WithFineODR => {
match val.sinc3fineodr {
},
FilterType::Sinc3WithFineODR => match val.sinc3fineodr {
Some(val2) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_temp_adc_sinc3_fine_filter(0, val2);
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_SINC3FINEODR), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_SINC3FINEODR),
socket,
);
}
}
}
FilterType::Sinc3 => {
match val.sinc3odr {
},
FilterType::Sinc3 => match val.sinc3odr {
Some(val2) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_temp_adc_sinc3_filter(0, val2);
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_SINC3ODR), socket);
}
}
}
}
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_SINC3ODR),
socket,
);
}
},
},
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_TEMP_ADC_FILTER), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_TEMP_ADC_FILTER),
socket,
);
}
}
}
Some(ThermostatCmdEnum::SetTempMonUpperLimit) => {
match cmd.json.data_f32 {
},
Some(ThermostatCmdEnum::SetTempMonUpperLimit) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_temp_mon_upper_limit(ThermodynamicTemperature::new::<degree_celsius>(val));
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
}
}
Some(ThermostatCmdEnum::SetTempMonLowerLimit) => {
match cmd.json.data_f32 {
},
Some(ThermostatCmdEnum::SetTempMonLowerLimit) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_temp_mon_lower_limit(ThermodynamicTemperature::new::<degree_celsius>(val));
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
}
}
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
},
Some(ThermostatCmdEnum::ClearAlarm) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.clear_temp_mon_alarm();
}
Some(ThermostatCmdEnum::SetShT0) => {
match cmd.json.data_f32 {
Some(ThermostatCmdEnum::SetShT0) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_sh_t0(ThermodynamicTemperature::new::<degree_celsius>(val));
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
}
}
Some(ThermostatCmdEnum::SetShR0) => {
match cmd.json.data_f32 {
},
Some(ThermostatCmdEnum::SetShR0) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_sh_r0(ElectricalResistance::new::<ohm>(val));
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
}
}
Some(ThermostatCmdEnum::SetShBeta) => {
match cmd.json.data_f32 {
},
Some(ThermostatCmdEnum::SetShBeta) => match cmd.json.data_f32 {
Some(val) => {
send_response(buffer, ResponseEnum::Acknowledge, None, socket);
thermostat.set_sh_beta(val);
}
None => {
send_response(buffer, ResponseEnum::InvalidDatatype, Some(ERR_MSG_MISSING_DATA_F32), socket);
}
}
send_response(
buffer,
ResponseEnum::InvalidDatatype,
Some(ERR_MSG_MISSING_DATA_F32),
socket,
);
}
},
None => { /* Do Nothing*/ }
_ => {
send_response(buffer, ResponseEnum::InvalidCmd, None, socket);
@ -669,6 +776,7 @@ pub fn execute_cmd(buffer: &mut [u8], buffer_size: usize, socket: &mut SocketHan
}
}
Err(_) => {
info!("cmd_recv: {:?}", buffer);
send_response(buffer, ResponseEnum::InvalidCmd, None, socket);
}
}

View File

@ -1,2 +1,2 @@
pub mod net;
pub mod cmd_handler;
pub mod net;

View File

@ -1,25 +1,21 @@
use crate::device::sys_timer;
use core::mem::{self, MaybeUninit};
use core::cell::RefCell;
use core::{cell::RefCell,
mem::{self, MaybeUninit}};
use cortex_m::interrupt::{CriticalSection, Mutex};
use log::{debug, info};
use smoltcp::{
iface::{
self, Interface, SocketHandle, SocketSet, SocketStorage
}, socket::tcp::{Socket, SocketBuffer, State}, time::{Instant, Duration}, wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}
};
use stm32_eth::{
Parts, EthPins, PartsIn,
mac::{Speed, EthernetMACWithMii},
dma::{
TxRingEntry, RxRingEntry, EthernetDMA
}};
use stm32f4xx_hal::{
gpio::{gpioa::*, gpiob::*, gpioc::*, Alternate, Input, Pin},
rcc::Clocks,
interrupt,
};
use serde::{Deserialize, Serialize};
use smoltcp::{iface::{self, Interface, SocketHandle, SocketSet, SocketStorage},
socket::tcp::{Socket, SocketBuffer, State},
time::{Duration, Instant},
wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}};
use stm32_eth::{dma::{EthernetDMA, RxRingEntry, TxRingEntry},
mac::{EthernetMACWithMii, Speed},
EthPins, Parts, PartsIn};
use stm32f4xx_hal::{gpio::{gpioa::*, gpiob::*, gpioc::*, Alternate, Input, Pin},
interrupt,
rcc::Clocks};
use crate::device::sys_timer;
#[derive(Debug, Clone, Copy, Deserialize, Serialize)]
pub struct IpSettings {
@ -35,7 +31,7 @@ impl Default for IpSettings {
addr: [192, 168, 1, 128],
port: 1337,
prefix_len: 24,
gateway: [192, 168, 1, 1]
gateway: [192, 168, 1, 1],
}
}
}
@ -53,8 +49,7 @@ pub struct ServerHandle {
phy: EthernetPhy<EthernetMACWithMii<Pin<'A', 2, Alternate<11>>, Pin<'C', 1, Alternate<11>>>>,
link_was_up: bool,
}
pub type EthernetPins =
EthPins<PA1<Input>, PA7<Input>, PB11<Input>, PB12<Input>, PB13<Input>, PC4<Input>, PC5<Input>>;
pub type EthernetPins = EthPins<PA1<Input>, PA7<Input>, PB11<Input>, PB12<Input>, PB13<Input>, PC4<Input>, PC5<Input>>;
pub struct EthernetMgmtPins {
pub mdio: PA2<Alternate<11>>,
pub mdc: PC1<Alternate<11>>,
@ -62,9 +57,9 @@ pub struct EthernetMgmtPins {
pub type EthInterface = Interface;
pub const NUM_OF_SOCKETS: usize = 4;
const TCP_BUFFER_SIZE: usize = 2048;
const TCP_BUFFER_SIZE: usize = 4096;
static mut RX_RING: Option<[RxRingEntry; 8]> = None;
static mut TX_RING: Option<[TxRingEntry; 2]> = None;
static mut TX_RING: Option<[TxRingEntry; 8]> = None;
static mut SOCKET_STORAGE: Option<[SocketStorage<'static>; NUM_OF_SOCKETS]> = None;
fn now_fn() -> smoltcp::time::Instant {
@ -74,7 +69,8 @@ fn now_fn() -> smoltcp::time::Instant {
static mut SERVER_HANDLE: Option<ServerHandle> = None;
impl ServerHandle {
pub fn new (eth_pins: EthernetPins,
pub fn new(
eth_pins: EthernetPins,
eth_mgmt_pins: EthernetMgmtPins,
ethernet_parts_in: PartsIn,
clocks: Clocks,
@ -85,23 +81,24 @@ impl ServerHandle {
let tx_ring = unsafe { TX_RING.get_or_insert(Default::default()) };
let socket_storage = unsafe { SOCKET_STORAGE.get_or_insert([SocketStorage::EMPTY; NUM_OF_SOCKETS]) };
let Parts {
mut dma,
mac,
#[cfg(feature = "ptp")]
..
} = stm32_eth::new_with_mii(
let Parts { mut dma, mac, .. } = stm32_eth::new_with_mii(
ethernet_parts_in,
&mut rx_ring[..],
&mut tx_ring[..],
clocks,
eth_pins,
eth_mgmt_pins.mdio,
eth_mgmt_pins.mdc
).unwrap();
eth_mgmt_pins.mdc,
)
.unwrap();
let ip_init = IpCidr::Ipv4(Ipv4Cidr::new(
Ipv4Address::new(ip_settings.addr[0], ip_settings.addr[1], ip_settings.addr[2], ip_settings.addr[3]),
Ipv4Address::new(
ip_settings.addr[0],
ip_settings.addr[1],
ip_settings.addr[2],
ip_settings.addr[3],
),
ip_settings.prefix_len,
));
let socket_addr: (IpAddress, u16) = (
@ -116,7 +113,12 @@ impl ServerHandle {
let mut routes = smoltcp::iface::Routes::new();
routes
.add_default_ipv4_route(Ipv4Address::new(ip_settings.gateway[0], ip_settings.gateway[1], ip_settings.gateway[2], ip_settings.gateway[3]))
.add_default_ipv4_route(Ipv4Address::new(
ip_settings.gateway[0],
ip_settings.gateway[1],
ip_settings.gateway[2],
ip_settings.gateway[3],
))
.ok();
dma.enable_interrupt();
@ -134,9 +136,7 @@ impl ServerHandle {
let tcp_handles = {
// Do not use NUM_OF_SOCKETS to define array size to
// remind developers to create/remove tcp_handles accordingly after changing NUM_OF_SOCKETS
let mut tcp_handles: [MaybeUninit<SocketHandle>; 4]= unsafe {
MaybeUninit::uninit().assume_init()
};
let mut tcp_handles: [MaybeUninit<SocketHandle>; 4] = unsafe { MaybeUninit::uninit().assume_init() };
macro_rules! create_tcp_handle {
($rx_storage:ident, $tx_storage:ident, $handle:expr) => {
@ -147,7 +147,7 @@ impl ServerHandle {
let tx_buffer = SocketBuffer::new(&mut $tx_storage[..]);
$handle.write(socket_set.add(Socket::new(rx_buffer, tx_buffer)));
}
}
};
}
create_tcp_handle!(RX_STORAGE0, TX_STORAGE0, tcp_handles[0]);
create_tcp_handle!(RX_STORAGE1, TX_STORAGE1, tcp_handles[1]);
@ -164,13 +164,14 @@ impl ServerHandle {
socket.set_nagle_enabled(false);
}
iface.poll(Instant::from_millis(i64::from(sys_timer::now())), &mut &mut dma, &mut socket_set);
iface.poll(
Instant::from_millis(i64::from(sys_timer::now())),
&mut &mut dma,
&mut socket_set,
);
if let Ok(mut phy) = EthernetPhy::from_miim(mac, 0) {
info!(
"Resetting PHY as an extra step. Type: {}",
phy.ident_string()
);
info!("Resetting PHY as an extra step. Type: {}", phy.ident_string());
phy.phy_init();
@ -216,7 +217,11 @@ impl ServerHandle {
self.iface.poll(now_fn(), &mut &mut self.dma, &mut self.socket_set);
}
pub fn recv(&mut self, buffer: &mut [u8], socket_handles: SocketHandle)-> Result<usize, smoltcp::socket::tcp::RecvError> {
pub fn recv(
&mut self,
buffer: &mut [u8],
socket_handles: SocketHandle,
) -> Result<usize, smoltcp::socket::tcp::RecvError> {
let socket = self.socket_set.get_mut::<Socket>(socket_handles);
socket.recv_slice(buffer)
@ -255,14 +260,9 @@ impl ServerHandle {
}
}
use ieee802_3_miim::{
phy::{
lan87xxa::{LAN8720A, LAN8742A},
BarePhy, KSZ8081R,
PhySpeed
},
Miim, Pause, Phy,
};
use ieee802_3_miim::{phy::{lan87xxa::{LAN8720A, LAN8742A},
BarePhy, PhySpeed, KSZ8081R},
Miim, Pause, Phy};
/// An ethernet PHY
pub enum EthernetPhy<M: Miim> {
@ -368,8 +368,7 @@ pub fn eth_poll_link_status_and_update_link_speed() -> bool {
});
}
return new_link_is_up;
}
else {
} else {
panic!("eth_poll_link_status_and_update_link_speed is called before init");
}
}
@ -379,8 +378,7 @@ pub fn eth_poll_iface() {
unsafe {
if let Some(ref mut server_handle) = SERVER_HANDLE {
server_handle.poll_iface();
}
else {
} else {
panic!("eth_poll_packet is called before init");
}
}
@ -390,8 +388,7 @@ pub fn eth_send(buffer: &mut [u8], num_bytes: usize, socket_handles: SocketHandl
unsafe {
if let Some(ref mut server_handle) = SERVER_HANDLE {
server_handle.send(buffer, num_bytes, socket_handles);
}
else {
} else {
panic!("eth_send is called before init");
}
}
@ -401,14 +398,13 @@ pub fn eth_recv(buffer: &mut [u8], socket_handles: SocketHandle)-> usize{
unsafe {
if let Some(ref mut server_handle) = SERVER_HANDLE {
match server_handle.recv(buffer, socket_handles) {
Ok(recv_bytes) => {return recv_bytes}
Ok(recv_bytes) => return recv_bytes,
Err(err) => {
debug!("TCP Recv Error: {}", err);
return 0
return 0;
}
};
}
else {
} else {
panic!("eth_send is called before init");
}
}
@ -418,8 +414,7 @@ pub fn eth_is_socket_connected(socket_handles: SocketHandle) -> bool {
unsafe {
if let Some(ref mut server_handle) = SERVER_HANDLE {
server_handle.is_socket_connected(socket_handles)
}
else {
} else {
panic!("eth_is_socket_connected is called before init");
}
}
@ -429,8 +424,7 @@ pub fn eth_is_socket_active(socket_handles: SocketHandle) -> bool {
unsafe {
if let Some(ref mut server_handle) = SERVER_HANDLE {
server_handle.poll_socket_status(socket_handles)
}
else {
} else {
panic!("eth_is_socket_active is called before init");
}
}
@ -440,8 +434,7 @@ pub fn eth_close_socket(socket_handles: SocketHandle) {
unsafe {
if let Some(ref mut server_handle) = SERVER_HANDLE {
server_handle.close_socket(socket_handles)
}
else {
} else {
panic!("eth_close_socket is called before init");
}
}
@ -453,8 +446,7 @@ pub fn for_each<F: FnMut(SocketHandle, usize)>(mut callback: F) {
for i in 0..NUM_OF_SOCKETS {
callback(server_handle.socket_handles[i], i);
}
}
else {
} else {
panic!("eth_close_socket is called before init");
}
}
@ -467,8 +459,7 @@ fn ETH() {
let interrupt_reason = stm32_eth::eth_interrupt_handler();
cortex_m::interrupt::free(|cs| {
if interrupt_reason.rx {
*NET_PENDING.borrow(cs)
.borrow_mut() = true;
*NET_PENDING.borrow(cs).borrow_mut() = true;
eth_poll_iface();
}
});
@ -477,13 +468,11 @@ fn ETH() {
/// Has an interrupt occurred since last call to `clear_pending()`?
pub fn is_pending(cs: &CriticalSection) -> bool {
*NET_PENDING.borrow(cs)
.borrow()
*NET_PENDING.borrow(cs).borrow()
}
/// Clear the interrupt pending flag before polling the interface for
/// data.
pub fn clear_pending(cs: &CriticalSection) {
*NET_PENDING.borrow(cs)
.borrow_mut() = false;
*NET_PENDING.borrow(cs).borrow_mut() = false;
}

View File

@ -1,9 +1,8 @@
use crate::device::sys_timer::sleep;
use fugit::MegahertzU32;
use stm32f4xx_hal::{
hal::{spi::SpiBus, digital::OutputPin},
spi,
};
use stm32f4xx_hal::{hal::{digital::OutputPin, spi::SpiBus},
spi};
use crate::device::sys_timer::sleep;
/// SPI Mode 1
pub const SPI_MODE: spi::Mode = spi::Mode {

View File

@ -1,22 +1,16 @@
use core::fmt;
use log::{info, warn};
use stm32f4xx_hal::
{
spi::Spi,
use stm32f4xx_hal::{gpio::{Output, PushPull, PA15},
hal::{digital::OutputPin, spi::SpiBus},
pac::SPI3,
gpio::{PA15, Output, PushPull},
hal::{
spi::SpiBus,
digital::OutputPin,
},
};
use uom::si::{
f32::ElectricPotential,
electric_potential::volt,
};
use super::{
checksum::{Checksum, ChecksumMode}, regs::{self, Register, RegisterData}, sinc3_fine_odr_closest, sinc3_fine_odr_output_rate, DigitalFilterOrder, FilterType, Input, Mode, PostFilter, RefSource, SingleChODR
};
spi::Spi};
use uom::si::{electric_potential::volt, f32::ElectricPotential};
use super::{checksum::{Checksum, ChecksumMode},
regs::{self, Register, RegisterData},
sinc3_fine_odr_closest, sinc3_fine_odr_output_rate, DigitalFilterOrder, FilterType, Input, Mode,
PostFilter, RefSource, SingleChODR};
/// AD7172-2 implementation
///
@ -35,7 +29,8 @@ impl<SPI: SpiBus<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS> {
pub fn new(spi: SPI, mut nss: NSS) -> Result<Self, SPI::Error> {
let _ = nss.set_high();
let mut adc = Adc {
spi, nss,
spi,
nss,
checksum_mode: ChecksumMode::Off,
};
adc.reset()?;
@ -64,8 +59,7 @@ impl<SPI: SpiBus<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS> {
/// `0x00DX` for AD7172-2
pub fn identify(&mut self) -> Result<u16, SPI::Error> {
self.read_reg(&regs::Id)
.map(|id| id.id())
self.read_reg(&regs::Id).map(|id| id.id())
}
pub fn set_checksum_mode(&mut self, mode: ChecksumMode) -> Result<(), SPI::Error> {
@ -84,9 +78,7 @@ impl<SPI: SpiBus<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS> {
})
}
pub fn setup_channel(
&mut self, index: u8, in_pos: Input, in_neg: Input
) -> Result<(), SPI::Error> {
pub fn setup_channel(&mut self, index: u8, in_pos: Input, in_neg: Input) -> Result<(), SPI::Error> {
self.update_reg(&regs::SetupCon { index }, |data| {
data.set_bipolar(false);
data.set_refbuf_pos(true);
@ -127,8 +119,7 @@ impl<SPI: SpiBus<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS> {
pub fn get_filter_type_and_rate(&mut self, index: u8) -> Result<(FilterType, f32), SPI::Error> {
let mut filter_type: FilterType = FilterType::Sinc5Sinc1With50hz60HzRejection;
let mut rate: f32 = -1.0;
self.read_reg(&regs::FiltCon { index })
.map(|data| {
self.read_reg(&regs::FiltCon { index }).map(|data| {
if data.sinc3_map() {
filter_type = FilterType::Sinc3WithFineODR;
let odr: u16 = (data.sinc3_map_fine_odr_msb() as u16) << 8 | data.sinc3_map_fine_odr_lsb() as u16;
@ -136,20 +127,32 @@ impl<SPI: SpiBus<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS> {
} else if data.enh_filt_en() {
filter_type = FilterType::Sinc5Sinc1With50hz60HzRejection;
match data.enh_filt().output_rate() {
Some(val) => { rate = val; }
None => { rate = -1.0; }
Some(val) => {
rate = val;
}
None => {
rate = -1.0;
}
};
} else if data.order() == DigitalFilterOrder::Sinc5Sinc1 {
filter_type = FilterType::Sinc5Sinc1;
match data.odr().output_rate() {
Some(val) => { rate = val; }
None => { rate = -1.0; }
Some(val) => {
rate = val;
}
None => {
rate = -1.0;
}
}
} else {
filter_type = FilterType::Sinc3;
match data.odr().output_rate() {
Some(val) => { rate = val; }
None => { rate = -1.0; }
Some(val) => {
rate = val;
}
None => {
rate = -1.0;
}
}
}
})?;
@ -191,25 +194,19 @@ impl<SPI: SpiBus<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS> {
data.set_sinc3_map(true);
data.set_sinc3_map_fine_odr_msb((sinc3_fine_odr_u16 >> 8 & 0xFF) as u8);
data.set_sinc3_map_fine_odr_lsb((sinc3_fine_odr_u16 & 0xFF) as u8);
}).map(|_| sinc3_fine_odr_output_rate(sinc3_fine_odr_u16))
})
.map(|_| sinc3_fine_odr_output_rate(sinc3_fine_odr_u16))
}
/// Returns the channel the data is from
pub fn data_ready(&mut self) -> Result<Option<u8>, SPI::Error> {
self.read_reg(&regs::Status)
.map(|status| {
if status.ready() {
Some(status.channel())
} else {
None
}
})
.map(|status| if status.ready() { Some(status.channel()) } else { None })
}
/// Get data
pub fn read_data(&mut self) -> Result<u32, SPI::Error> {
self.read_reg(&regs::Data)
.map(|data| data.data())
self.read_reg(&regs::Data).map(|data| data.data())
}
fn read_reg<R: regs::Register>(&mut self, reg: &R) -> Result<R::Data, SPI::Error> {
@ -228,7 +225,12 @@ impl<SPI: SpiBus<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS> {
break;
}
// Retry
warn!("read_reg {:02X}: checksum error: {:?}!={:?}, retrying", reg.address(), checksum_expected, checksum_in);
warn!(
"read_reg {:02X}: checksum error: {:?}!={:?}, retrying",
reg.address(),
checksum_expected,
checksum_in
);
}
Ok(reg_data)
}
@ -254,7 +256,10 @@ impl<SPI: SpiBus<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS> {
if *readback_data == **reg_data {
return Ok(());
}
warn!("write_reg {:02X}: readback error, {:?}!={:?}, retrying", address, &*readback_data, &**reg_data);
warn!(
"write_reg {:02X}: readback error, {:?}!={:?}, retrying",
address, &*readback_data, &**reg_data
);
}
}
@ -278,7 +283,12 @@ impl<SPI: SpiBus<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS> {
Ok(())
}
fn transfer<'w>(&mut self, addr: u8, reg_data: &'w mut [u8], checksum: Option<u8>) -> Result<Option<u8>, SPI::Error> {
fn transfer<'w>(
&mut self,
addr: u8,
reg_data: &'w mut [u8],
checksum: Option<u8>,
) -> Result<Option<u8>, SPI::Error> {
let mut addr_buf = [addr];
let _ = self.nss.set_low();
@ -287,8 +297,7 @@ impl<SPI: SpiBus<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS> {
Err(e) => Err(e),
};
let result = match (result, checksum) {
(Ok(_), None) =>
Ok(None),
(Ok(_), None) => Ok(None),
(Ok(_), Some(checksum_out)) => {
let mut checksum_buf = [checksum_out; 1];
match self.spi.transfer_in_place(&mut checksum_buf) {
@ -296,8 +305,7 @@ impl<SPI: SpiBus<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS> {
Err(e) => Err(e),
}
}
(Err(e), _) =>
Err(e),
(Err(e), _) => Err(e),
};
let _ = self.nss.set_high();

View File

@ -29,13 +29,13 @@ impl Checksum {
fn feed_byte(&mut self, input: u8) {
match self.mode {
ChecksumMode::Off => {},
ChecksumMode::Off => {}
ChecksumMode::Xor => self.state ^= input,
ChecksumMode::Crc => {
for i in 0..8 {
let input_mask = 0x80 >> i;
self.state = (self.state << 1) ^
if ((self.state & 0x80) != 0) != ((input & input_mask) != 0) {
self.state = (self.state << 1)
^ if ((self.state & 0x80) != 0) != ((input & input_mask) != 0) {
0x07 /* x8 + x2 + x + 1 */
} else {
0
@ -54,7 +54,7 @@ impl Checksum {
pub fn result(&self) -> Option<u8> {
match self.mode {
ChecksumMode::Off => None,
_ => Some(self.state)
_ => Some(self.state),
}
}
}

View File

@ -1,11 +1,12 @@
use core::fmt;
use num_traits::float::Float;
use serde::{Serialize, Deserialize};
use fugit::MegahertzU32;
use num_traits::float::Float;
use serde::{Deserialize, Serialize};
use stm32f4xx_hal::spi;
pub mod regs;
mod checksum;
pub mod regs;
pub use checksum::ChecksumMode;
mod adc;
pub use adc::*;
@ -20,7 +21,6 @@ pub const SPI_CLOCK_MHZ: MegahertzU32 = MegahertzU32::from_raw(21);
pub const MAX_VALUE: u32 = 0xFF_FFFF;
#[derive(Clone, Copy, Debug)]
#[repr(u8)]
pub enum Mode {
@ -103,7 +103,8 @@ impl fmt::Display for Input {
RefPos => "ref+",
RefNeg => "ref-",
_ => "<INVALID>",
}.fmt(fmt)
}
.fmt(fmt)
}
}
@ -139,7 +140,8 @@ impl fmt::Display for RefSource {
Internal => "internal",
Avdd1MinusAvss => "avdd1-avss",
_ => "<INVALID>",
}.fmt(fmt)
}
.fmt(fmt)
}
}
@ -169,9 +171,7 @@ impl PostFilter {
let mut best: Option<(f32, Self)> = None;
for value in Self::VALID_VALUES {
let error = (rate - value.output_rate().unwrap()).abs();
let better = best
.map(|(best_error, _)| error < best_error)
.unwrap_or(true);
let better = best.map(|(best_error, _)| error < best_error).unwrap_or(true);
if better {
best = Some((error, *value));
}
@ -281,9 +281,7 @@ impl SingleChODR {
let mut best: Option<(f32, Self)> = None;
for value in Self::VALID_VALUES {
let error = (rate - value.output_rate().unwrap()).abs();
let better = best
.map(|(best_error, _)| error < best_error)
.unwrap_or(true);
let better = best.map(|(best_error, _)| error < best_error).unwrap_or(true);
if better {
best = Some((error, *value));
}

View File

@ -1,6 +1,7 @@
use core::ops::{Deref, DerefMut};
use byteorder::{BigEndian, ByteOrder};
use bit_field::BitField;
use byteorder::{BigEndian, ByteOrder};
use super::*;
@ -49,7 +50,9 @@ macro_rules! def_reg {
}
};
($Reg:ident,u8, $reg:ident, $addr:expr, $size:expr) => {
pub struct $Reg { pub index: u8, }
pub struct $Reg {
pub index: u8,
}
impl Register for $Reg {
type Data = $reg::Data;
fn address(&self) -> u8 {
@ -76,7 +79,7 @@ macro_rules! def_reg {
}
}
}
}
};
}
macro_rules! reg_bit {
@ -161,7 +164,13 @@ impl adc_mode::Data {
reg_bits!(delay, set_delay, 0, 0..=2, "Delay after channel switch");
reg_bit!(sing_cyc, set_sing_cyc, 0, 5, "Can only used with single channel");
reg_bit!(hide_delay, set_hide_delay, 0, 6, "Hide delay");
reg_bit!(ref_en, set_ref_en, 0, 7, "Enable internal reference, output buffered 2.5 V to REFOUT");
reg_bit!(
ref_en,
set_ref_en,
0,
7,
"Enable internal reference, output buffered 2.5 V to REFOUT"
);
reg_bits!(clockset, set_clocksel, 1, 2..=3, "Clock source");
reg_bits!(mode, set_mode, 1, 4..=6, Mode, "Operating mode");
}
@ -174,9 +183,7 @@ impl if_mode::Data {
def_reg!(Data, data, 0x04, 3);
impl data::Data {
pub fn data(&self) -> u32 {
(u32::from(self.0[0]) << 16) |
(u32::from(self.0[1]) << 8) |
u32::from(self.0[2])
(u32::from(self.0[0]) << 16) | (u32::from(self.0[1]) << 8) | u32::from(self.0[2])
}
}
@ -200,8 +207,7 @@ impl channel::Data {
/// Which input is connected to positive input of this channel
#[allow(unused)]
pub fn a_in_pos(&self) -> Input {
((self.0[0].get_bits(0..=1) << 3) |
self.0[1].get_bits(5..=7)).into()
((self.0[0].get_bits(0..=1) << 3) | self.0[1].get_bits(5..=7)).into()
}
/// Set which input is connected to positive input of this channel
#[allow(unused)]
@ -210,39 +216,108 @@ impl channel::Data {
self.0[0].set_bits(0..=1, value >> 3);
self.0[1].set_bits(5..=7, value & 0x7);
}
reg_bits!(a_in_neg, set_a_in_neg, 1, 0..=4, Input,
"Which input is connected to negative input of this channel");
reg_bits!(
a_in_neg,
set_a_in_neg,
1,
0..=4,
Input,
"Which input is connected to negative input of this channel"
);
}
def_reg!(SetupCon, u8, setup_con, 0x20, 2);
impl setup_con::Data {
reg_bit!(bipolar, set_bipolar, 0, 4, "Unipolar (`false`) or bipolar (`true`) coded output");
reg_bit!(
bipolar,
set_bipolar,
0,
4,
"Unipolar (`false`) or bipolar (`true`) coded output"
);
reg_bit!(refbuf_pos, set_refbuf_pos, 0, 3, "Enable REF+ input buffer");
reg_bit!(refbuf_neg, set_refbuf_neg, 0, 2, "Enable REF- input buffer");
reg_bit!(ainbuf_pos, set_ainbuf_pos, 0, 1, "Enable AIN+ input buffer");
reg_bit!(ainbuf_neg, set_ainbuf_neg, 0, 0, "Enable AIN- input buffer");
reg_bit!(burnout_en, 1, 7, "enables a 10 µA current source on the positive analog input selected and a 10 µA current sink on the negative analog input selected");
reg_bits!(ref_sel, set_ref_sel, 1, 4..=5, RefSource, "Select reference source for conversion");
reg_bit!(
burnout_en,
1,
7,
"enables a 10 µA current source on the positive analog input selected and a 10 µA current sink on the \
negative analog input selected"
);
reg_bits!(
ref_sel,
set_ref_sel,
1,
4..=5,
RefSource,
"Select reference source for conversion"
);
}
def_reg!(FiltCon, u8, filt_con, 0x28, 2);
impl filt_con::Data {
reg_bit!(sinc3_map, set_sinc3_map, 0, 7, "If set, Sinc3 Filter's notch frequency rejection position can be fine tuned with FiltCon[14:0]. Best to be used with Single Channel Enabled");
reg_bit!(enh_filt_en, set_enh_filt_en, 0, 3, "Enable postfilters for enhanced 50Hz and 60Hz rejection");
reg_bits!(enh_filt, set_enh_filt, 0, 0..=2, PostFilter, "Select postfilters output data rate for enhanced 50Hz and 60Hz rejection");
reg_bits!(order, set_order, 1, 5..=6, DigitalFilterOrder, "order of the digital filter that processes the modulator data");
reg_bits!(odr, set_odr, 1, 0..=4, SingleChODR, "Output data rate for normal Sin5c + Sinc1 and Sinc3 filter with SING_CYC = 0 and Single Channel Enabled");
reg_bits!(sinc3_map_fine_odr_msb, set_sinc3_map_fine_odr_msb, 0, 0..=6, "MSB Byte Sinc3 Fine Output Config");
reg_bits!(sinc3_map_fine_odr_lsb, set_sinc3_map_fine_odr_lsb, 1, 0..=7, "LSB Byte Sinc3 Fine Output Config");
reg_bit!(
sinc3_map,
set_sinc3_map,
0,
7,
"If set, Sinc3 Filter's notch frequency rejection position can be fine tuned with FiltCon[14:0]. Best to be \
used with Single Channel Enabled"
);
reg_bit!(
enh_filt_en,
set_enh_filt_en,
0,
3,
"Enable postfilters for enhanced 50Hz and 60Hz rejection"
);
reg_bits!(
enh_filt,
set_enh_filt,
0,
0..=2,
PostFilter,
"Select postfilters output data rate for enhanced 50Hz and 60Hz rejection"
);
reg_bits!(
order,
set_order,
1,
5..=6,
DigitalFilterOrder,
"order of the digital filter that processes the modulator data"
);
reg_bits!(
odr,
set_odr,
1,
0..=4,
SingleChODR,
"Output data rate for normal Sin5c + Sinc1 and Sinc3 filter with SING_CYC = 0 and Single Channel Enabled"
);
reg_bits!(
sinc3_map_fine_odr_msb,
set_sinc3_map_fine_odr_msb,
0,
0..=6,
"MSB Byte Sinc3 Fine Output Config"
);
reg_bits!(
sinc3_map_fine_odr_lsb,
set_sinc3_map_fine_odr_lsb,
1,
0..=7,
"LSB Byte Sinc3 Fine Output Config"
);
}
def_reg!(Offset, u8, offset, 0x30, 3);
impl offset::Data {
#[allow(unused)]
pub fn offset(&self) -> u32 {
(u32::from(self.0[0]) << 16) |
(u32::from(self.0[1]) << 8) |
u32::from(self.0[2])
(u32::from(self.0[0]) << 16) | (u32::from(self.0[1]) << 8) | u32::from(self.0[2])
}
#[allow(unused)]
pub fn set_offset(&mut self, value: u32) {
@ -256,9 +331,7 @@ def_reg!(Gain, u8, gain, 0x38, 3);
impl gain::Data {
#[allow(unused)]
pub fn gain(&self) -> u32 {
(u32::from(self.0[0]) << 16) |
(u32::from(self.0[1]) << 8) |
u32::from(self.0[2])
(u32::from(self.0[0]) << 16) | (u32::from(self.0[1]) << 8) | u32::from(self.0[2])
}
#[allow(unused)]
pub fn set_gain(&mut self, value: u32) {

View File

@ -1,22 +1,18 @@
use crate::thermostat::ad5680;
use core::ptr::addr_of_mut;
use fugit::KilohertzU32;
use stm32f4xx_hal::{
adc::{config::{self, AdcConfig}, Adc},
use stm32f4xx_hal::{adc::{config::{self, AdcConfig},
Adc},
dma::{config::DmaConfig, PeripheralToMemory, Stream2, StreamsTuple, Transfer as DMA_Transfer},
gpio::{gpioa::*, gpiob::*, gpioc::*, Analog, Output, PushPull},
hal::{self, spi::SpiBus, digital::OutputPin},
pac::{ADC1, ADC2, DMA2, SPI1, TIM4, Peripherals, NVIC},
hal::{digital::OutputPin, spi::SpiBus},
interrupt,
pac::{Peripherals, ADC1, ADC2, DMA2, NVIC, SPI1, TIM4},
spi::Spi,
timer::pwm::PwmChannel,
interrupt
};
timer::pwm::PwmChannel};
use uom::si::{electric_potential::millivolt, f32::ElectricPotential, ratio::ratio};
use uom::si::{
electric_potential::millivolt,
f32::ElectricPotential,
ratio::ratio,
};
use crate::thermostat::ad5680;
pub const PWM_FREQ_KHZ: KilohertzU32 = KilohertzU32::from_raw(20);
@ -150,25 +146,79 @@ impl MAX1968 {
let mut pins_adc2 = Adc::adc2(adc2, false, adc_config);
pins_adc2.configure_channel(&phy_ch0.itec_pin, config::Sequence::One, config::SampleTime::Cycles_480);
pins_adc2.configure_channel(&phy_ch0.vtec_pin, config::Sequence::Two, config::SampleTime::Cycles_480);
pins_adc2.configure_channel(&phy_ch0.itec_pin, config::Sequence::Three, config::SampleTime::Cycles_480);
pins_adc2.configure_channel(&phy_ch0.vtec_pin, config::Sequence::Four, config::SampleTime::Cycles_480);
pins_adc2.configure_channel(&phy_ch0.itec_pin, config::Sequence::Five, config::SampleTime::Cycles_480);
pins_adc2.configure_channel(
&phy_ch0.itec_pin,
config::Sequence::Three,
config::SampleTime::Cycles_480,
);
pins_adc2.configure_channel(
&phy_ch0.vtec_pin,
config::Sequence::Four,
config::SampleTime::Cycles_480,
);
pins_adc2.configure_channel(
&phy_ch0.itec_pin,
config::Sequence::Five,
config::SampleTime::Cycles_480,
);
pins_adc2.configure_channel(&phy_ch0.vtec_pin, config::Sequence::Six, config::SampleTime::Cycles_480);
pins_adc2.configure_channel(&phy_ch0.itec_pin, config::Sequence::Seven, config::SampleTime::Cycles_480);
pins_adc2.configure_channel(&phy_ch0.vtec_pin, config::Sequence::Eight, config::SampleTime::Cycles_480);
pins_adc2.configure_channel(&phy_ch0.itec_pin, config::Sequence::Nine, config::SampleTime::Cycles_480);
pins_adc2.configure_channel(
&phy_ch0.itec_pin,
config::Sequence::Seven,
config::SampleTime::Cycles_480,
);
pins_adc2.configure_channel(
&phy_ch0.vtec_pin,
config::Sequence::Eight,
config::SampleTime::Cycles_480,
);
pins_adc2.configure_channel(
&phy_ch0.itec_pin,
config::Sequence::Nine,
config::SampleTime::Cycles_480,
);
pins_adc2.configure_channel(&phy_ch0.vtec_pin, config::Sequence::Ten, config::SampleTime::Cycles_480);
pins_adc2.configure_channel(&phy_ch0.itec_pin, config::Sequence::Eleven, config::SampleTime::Cycles_480);
pins_adc2.configure_channel(&phy_ch0.vtec_pin, config::Sequence::Twelve, config::SampleTime::Cycles_480);
pins_adc2.configure_channel(&phy_ch0.itec_pin, config::Sequence::Thirteen, config::SampleTime::Cycles_480);
pins_adc2.configure_channel(&phy_ch0.vtec_pin, config::Sequence::Fourteen, config::SampleTime::Cycles_480);
pins_adc2.configure_channel(&phy_ch0.itec_pin, config::Sequence::Fifteen, config::SampleTime::Cycles_480);
pins_adc2.configure_channel(&phy_ch0.vtec_pin, config::Sequence::Sixteen, config::SampleTime::Cycles_480);
pins_adc2.configure_channel(
&phy_ch0.itec_pin,
config::Sequence::Eleven,
config::SampleTime::Cycles_480,
);
pins_adc2.configure_channel(
&phy_ch0.vtec_pin,
config::Sequence::Twelve,
config::SampleTime::Cycles_480,
);
pins_adc2.configure_channel(
&phy_ch0.itec_pin,
config::Sequence::Thirteen,
config::SampleTime::Cycles_480,
);
pins_adc2.configure_channel(
&phy_ch0.vtec_pin,
config::Sequence::Fourteen,
config::SampleTime::Cycles_480,
);
pins_adc2.configure_channel(
&phy_ch0.itec_pin,
config::Sequence::Fifteen,
config::SampleTime::Cycles_480,
);
pins_adc2.configure_channel(
&phy_ch0.vtec_pin,
config::Sequence::Sixteen,
config::SampleTime::Cycles_480,
);
let dma = StreamsTuple::new(dma2);
let dma_adc: DMA_Transfer<Stream2<DMA2>, 1, Adc<ADC2>, PeripheralToMemory, &'static mut [u16; 16]>;
unsafe {
dma_adc = DMA_Transfer::init_peripheral_to_memory(dma.2, pins_adc2, addr_of_mut!(ADC2_FIRST_BUFFER).as_mut().unwrap(), None, dma_config);
dma_adc = DMA_Transfer::init_peripheral_to_memory(
dma.2,
pins_adc2,
addr_of_mut!(ADC2_FIRST_BUFFER).as_mut().unwrap(),
None,
dma_config,
);
NVIC::unmask(interrupt::DMA2_STREAM2);
}
@ -183,7 +233,9 @@ impl MAX1968 {
pub fn dma_adc_start_conversion(&mut self) {
if unsafe { DMA_TRANSFER_COMPLETE } {
unsafe { DMA_TRANSFER_COMPLETE = false; }
unsafe {
DMA_TRANSFER_COMPLETE = false;
}
self.dma_adc.start(|adc| {
adc.clear_end_of_conversion_flag();
adc.start_conversion();
@ -195,7 +247,8 @@ impl MAX1968 {
if unsafe { DMA_TRANSFER_COMPLETE } {
let buffer: &[u16; 16];
unsafe {
(buffer, _) = self.dma_adc
(buffer, _) = self
.dma_adc
.next_transfer(addr_of_mut!(ADC2_LOCAL_BUFFER).as_mut().unwrap())
.unwrap();
}
@ -240,10 +293,8 @@ impl MAX1968 {
self.phy.shdn.set_high();
}
pub fn set_dac(&mut self, voltage: ElectricPotential, dac_out_v_max: ElectricPotential) -> ElectricPotential {
let value = ((voltage / dac_out_v_max).get::<ratio>()
* (ad5680::MAX_VALUE as f32)) as u32;
let value = ((voltage / dac_out_v_max).get::<ratio>() * (ad5680::MAX_VALUE as f32)) as u32;
self.phy.dac.set(value).unwrap();
voltage
}
@ -255,10 +306,10 @@ impl MAX1968 {
sample = match adc_read_target {
AdcReadTarget::VREF => {
for _ in (0..avg_pt).rev() {
sample += self.pins_adc.convert(
&self.phy.vref_pin,
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
) as u32;
sample += self
.pins_adc
.convert(&self.phy.vref_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480)
as u32;
}
sample / avg_pt as u32
}
@ -273,19 +324,19 @@ impl MAX1968 {
}
AdcReadTarget::ITec => {
for _ in (0..avg_pt).rev() {
sample += self.pins_adc.convert(
&self.phy.itec_pin,
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
) as u32;
sample += self
.pins_adc
.convert(&self.phy.itec_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480)
as u32;
}
sample / avg_pt as u32
}
AdcReadTarget::VTec => {
for _ in (0..avg_pt).rev() {
sample += self.pins_adc.convert(
&self.phy.vtec_pin,
stm32f4xx_hal::adc::config::SampleTime::Cycles_480,
) as u32;
sample += self
.pins_adc
.convert(&self.phy.vtec_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480)
as u32;
}
sample / avg_pt as u32
}
@ -307,7 +358,6 @@ impl MAX1968 {
max_value = self.phy.max_v.get_max_duty();
value = duty_cycle_value(duty, max_duty, max_value);
self.phy.max_v.set_duty(value);
}
PwmPinsEnum::MaxPosI => {
self.phy.max_i_pos.enable();
@ -324,7 +374,6 @@ impl MAX1968 {
}
return (value as f64) / (max_value as f64);
}
}
#[interrupt]
@ -332,14 +381,17 @@ fn DMA2_STREAM2(){
cortex_m::interrupt::free(|_| {
unsafe {
// Clear all DMA2_STREAM2 interrupt flags
Peripherals::steal().DMA2.lifcr.write(|w| w
.ctcif2().set_bit()
.cdmeif2().set_bit()
.chtif2().set_bit()
.cteif2().set_bit()
);
Peripherals::steal().DMA2.lifcr.write(|w| {
w.ctcif2()
.set_bit()
.cdmeif2()
.set_bit()
.chtif2()
.set_bit()
.cteif2()
.set_bit()
});
DMA_TRANSFER_COMPLETE = true;
}
}
)
})
}

View File

@ -1,7 +1,7 @@
pub mod ad5680;
pub mod max1968;
pub mod thermostat;
pub mod ad7172;
pub mod steinhart_hart;
pub mod max1968;
pub mod pid_state;
pub mod steinhart_hart;
pub mod temp_mon;
pub mod thermostat;

View File

@ -1,14 +1,11 @@
use miniconf::Tree;
use serde::{Deserialize, Serialize};
use uom::si::{
electric_potential::volt, electrical_resistance::ohm, f32::{
ElectricPotential, ElectricalResistance, ThermodynamicTemperature
}, thermodynamic_temperature::degree_celsius
};
use crate::thermostat::{
ad7172,
steinhart_hart as sh,
};
use uom::si::{electric_potential::volt,
electrical_resistance::ohm,
f32::{ElectricPotential, ElectricalResistance, ThermodynamicTemperature},
thermodynamic_temperature::degree_celsius};
use crate::thermostat::{ad7172, steinhart_hart as sh};
const R_INNER: f32 = 2.0 * 5100.0;
const VREF_SENS: f32 = 3.3 / 2.0;
@ -47,7 +44,6 @@ pub struct Controller {
pub y1: f64,
}
pub struct PidState {
adc_data: Option<u32>,
adc_calibration: ad7172::ChannelCalibration,
@ -106,7 +102,10 @@ impl PidState {
let input = self.get_temperature()?.get::<degree_celsius>();
let setpoint = self.set_point.get::<degree_celsius>();
let mut output: f64 = self.controller.y1 - setpoint as f64 * f64::from(self.controller.parameters.ki)
+ input as f64 * f64::from(self.controller.parameters.kp + self.controller.parameters.ki + self.controller.parameters.kd)
+ input as f64
* f64::from(
self.controller.parameters.kp + self.controller.parameters.ki + self.controller.parameters.kd,
)
- self.controller.x1 * f64::from(self.controller.parameters.kp + 2.0 * self.controller.parameters.kd)
+ self.controller.x2 * f64::from(self.controller.parameters.kd)
+ f64::from(self.controller.parameters.kp) * (setpoint as f64 - self.controller.u1);

View File

@ -1,15 +1,10 @@
use miniconf::Tree;
use num_traits::float::Float;
use serde::{Deserialize, Serialize};
use uom::si::{
f32::{
ElectricalResistance,
ThermodynamicTemperature,
},
electrical_resistance::ohm,
use uom::si::{electrical_resistance::ohm,
f32::{ElectricalResistance, ThermodynamicTemperature},
ratio::ratio,
thermodynamic_temperature::{degree_celsius, kelvin},
};
use miniconf::Tree;
thermodynamic_temperature::{degree_celsius, kelvin}};
/// Steinhart-Hart equation parameters
#[derive(Deserialize, Serialize, Copy, Clone, Debug, Tree)]

View File

@ -1,9 +1,7 @@
use serde::{Deserialize, Serialize};
use miniconf::Tree;
use uom::si::{
f32::ThermodynamicTemperature, thermodynamic_temperature::degree_celsius
};
use num_traits::Float;
use serde::{Deserialize, Serialize};
use uom::si::{f32::ThermodynamicTemperature, thermodynamic_temperature::degree_celsius};
#[derive(PartialEq, Deserialize, Serialize, Copy, Clone, Default, Debug)]
pub enum TempStatusEnum {
#[default]
@ -44,7 +42,7 @@ impl Default for TempMon {
set_point: ThermodynamicTemperature::new::<degree_celsius>(0.0),
status: TempStatus {
status: TempStatusEnum::Off,
over_temp_alarm: false
over_temp_alarm: false,
},
state: State::default(),
count: 0,
@ -121,8 +119,7 @@ impl TempMon {
if is_over_temp {
self.state = State::OverTempAlarm;
self.status.status = TempStatusEnum::OverTemp;
}
else if !pwr_on {
} else if !pwr_on {
self.state = State::PwrOff;
self.status.status = TempStatusEnum::Off;
} else if pid_engaged {
@ -160,8 +157,7 @@ impl TempMon {
// State Transition
if !pwr_on {
self.state = State::PwrOff;
}
else {
} else {
if self.status.status == TempStatusEnum::OverTemp {
self.state = State::OverTempAlarm;
} else if self.is_set_point_changed {

View File

@ -1,25 +1,23 @@
use core::f32::NAN;
use core::marker::PhantomData;
use crate::sys_timer;
use crate::thermostat::ad5680;
use crate::thermostat::max1968::{MAX1968, AdcReadTarget, PwmPinsEnum};
use crate::thermostat::ad7172::{self, FilterType, PostFilter, SingleChODR};
use crate::thermostat::pid_state::{PidState, PidSettings, Parameters as PidParams};
use crate::thermostat::temp_mon::{TempMon, TempStatus, TempMonSettings};
use crate::thermostat::steinhart_hart::Parameters as Sh_Params;
use serde::{Deserialize, Serialize};
use core::{f32::NAN, marker::PhantomData};
use log::debug;
use uom::si::{
electric_current::ampere,
use miniconf::Tree;
use serde::{Deserialize, Serialize};
use uom::si::{electric_current::ampere,
electric_potential::volt,
electrical_resistance::ohm,
thermodynamic_temperature::degree_celsius,
f32::{ThermodynamicTemperature, ElectricCurrent, ElectricPotential, ElectricalResistance},
f32::{ElectricCurrent, ElectricPotential, ElectricalResistance, ThermodynamicTemperature},
ratio::ratio,
};
use miniconf::Tree;
thermodynamic_temperature::degree_celsius};
use super::pid_state;
use crate::{sys_timer,
thermostat::{ad5680,
ad7172::{self, FilterType, PostFilter, SingleChODR},
max1968::{AdcReadTarget, PwmPinsEnum, MAX1968},
pid_state,
pid_state::{Parameters as PidParams, PidSettings, PidState},
steinhart_hart::Parameters as Sh_Params,
temp_mon::{TempMon, TempMonSettings, TempStatus}}};
pub const R_SENSE: ElectricalResistance = ElectricalResistance {
dimension: PhantomData,
@ -37,7 +35,6 @@ pub struct TempAdcFilter{
pub rate: Option<f32>,
}
#[derive(Deserialize, Serialize, Clone, Copy, Debug, Tree)]
pub struct TecSettings {
pub default_pwr_on: bool,
@ -79,7 +76,8 @@ impl TecSettings{
units: PhantomData,
value: 5.0,
};
const MAX_V_DUTY_MAX: f64 = TecSettings::MAX_V_MAX.value as f64 / TecSettings::MAX_V_DUTY_TO_CURRENT_RATE.value as f64;
const MAX_V_DUTY_MAX: f64 =
TecSettings::MAX_V_MAX.value as f64 / TecSettings::MAX_V_DUTY_TO_CURRENT_RATE.value as f64;
const MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE: ElectricCurrent = ElectricCurrent {
dimension: PhantomData,
units: PhantomData,
@ -96,8 +94,10 @@ impl TecSettings{
value: 1.0,
};
// .get::<ratio>() is not implemented for const
const MAX_I_POS_DUTY_MAX: f64 = TecSettings::MAX_I_POS_CURRENT.value as f64 / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE.value as f64;
const MAX_I_NEG_DUTY_MAX: f64 = TecSettings::MAX_I_NEG_CURRENT.value as f64 / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE.value as f64;
const MAX_I_POS_DUTY_MAX: f64 =
TecSettings::MAX_I_POS_CURRENT.value as f64 / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE.value as f64;
const MAX_I_NEG_DUTY_MAX: f64 =
TecSettings::MAX_I_NEG_CURRENT.value as f64 / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE.value as f64;
}
impl Default for TecSettings {
@ -171,9 +171,10 @@ impl Thermostat{
fn t_adc_setup(&mut self) -> ad7172::ChannelCalibration {
self.ad7172.set_sync_enable(false).unwrap();
self.ad7172.setup_channel(0, ad7172::Input::Ain0, ad7172::Input::Ain1).unwrap();
let adc_calibration0 = self.ad7172.get_calibration(0)
.expect("adc_calibration0");
self.ad7172
.setup_channel(0, ad7172::Input::Ain0, ad7172::Input::Ain1)
.unwrap();
let adc_calibration0 = self.ad7172.get_calibration(0).expect("adc_calibration0");
self.ad7172.start_continuous_conversion().unwrap();
adc_calibration0
}
@ -186,7 +187,8 @@ impl Thermostat{
state.update(data);
let pid_engaged = state.get_pid_engaged();
let temp = self.get_temperature();
self.temp_mon.update_status(pid_engaged, self.max1968.is_powered_on(), temp);
self.temp_mon
.update_status(pid_engaged, self.max1968.is_powered_on(), temp);
debug!("state.get_pid_engaged(): {:?}", pid_engaged);
debug!("Temperature: {:?} degree", temp.get::<degree_celsius>());
data_rdy = true;
@ -201,7 +203,10 @@ impl Thermostat{
match state.update_pid() {
Some(pid_output) => {
self.set_i(ElectricCurrent::new::<ampere>(pid_output as f32));
debug!("Temperature Set Point: {:?} degree", self.pid_ctrl_ch0.get_pid_setpoint().get::<degree_celsius>());
debug!(
"Temperature Set Point: {:?} degree",
self.pid_ctrl_ch0.get_pid_setpoint().get::<degree_celsius>()
);
}
None => {}
}
@ -239,21 +244,27 @@ impl Thermostat{
pub fn set_max_v(&mut self, max_v: ElectricPotential) -> ElectricPotential {
let duty = (max_v / TecSettings::MAX_V_DUTY_TO_CURRENT_RATE).get::<ratio>();
let duty = self.max1968.set_pwm(PwmPinsEnum::MaxV, duty as f64, TecSettings::MAX_V_DUTY_MAX);
let duty = self
.max1968
.set_pwm(PwmPinsEnum::MaxV, duty as f64, TecSettings::MAX_V_DUTY_MAX);
self.tec_settings.max_v_set = duty as f32 * TecSettings::MAX_V_DUTY_TO_CURRENT_RATE;
self.tec_settings.max_v_set
}
pub fn set_max_i_pos(&mut self, max_i_pos: ElectricCurrent) -> ElectricCurrent {
let duty = (max_i_pos / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE).get::<ratio>();
let duty = self.max1968.set_pwm(PwmPinsEnum::MaxPosI, duty as f64, TecSettings::MAX_I_POS_DUTY_MAX);
let duty = self
.max1968
.set_pwm(PwmPinsEnum::MaxPosI, duty as f64, TecSettings::MAX_I_POS_DUTY_MAX);
self.tec_settings.max_i_pos_set = duty as f32 * TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE;
self.tec_settings.max_i_pos_set
}
pub fn set_max_i_neg(&mut self, max_i_neg: ElectricCurrent) -> ElectricCurrent {
let duty = (max_i_neg / TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE).get::<ratio>();
let duty = self.max1968.set_pwm(PwmPinsEnum::MaxNegI, duty as f64, TecSettings::MAX_I_NEG_DUTY_MAX);
let duty = self
.max1968
.set_pwm(PwmPinsEnum::MaxNegI, duty as f64, TecSettings::MAX_I_NEG_DUTY_MAX);
self.tec_settings.max_i_neg_set = duty as f32 * TecSettings::MAX_I_POS_NEG_DUTY_TO_CURRENT_RATE;
self.tec_settings.max_i_neg_set
}
@ -282,7 +293,10 @@ impl Thermostat{
pub fn get_tec_readings(&mut self) -> (ElectricPotential, ElectricCurrent) {
let vref = self.tec_settings.vref;
let (vtec, itec) = self.max1968.get_tec_readings();
((vtec - TecSettings::TEC_VSEC_BIAS_V) * 4.0, (itec - vref) / ElectricalResistance::new::<ohm>(0.4))
(
(vtec - TecSettings::TEC_VSEC_BIAS_V) * 4.0,
(itec - vref) / ElectricalResistance::new::<ohm>(0.4),
)
}
/// Calibrates the DAC output to match vref of the MAX driver to reduce zero-current offset of the MAX driver output.
@ -343,9 +357,7 @@ impl Thermostat{
let temperature: Option<f32>;
match self.pid_ctrl_ch0.get_temperature() {
Some(val) => {
temperature = Some(val.get::<degree_celsius>())
}
Some(val) => temperature = Some(val.get::<degree_celsius>()),
None => {
temperature = None;
}
@ -364,10 +376,8 @@ impl Thermostat{
pub fn get_temperature(&mut self) -> ThermodynamicTemperature {
match self.pid_ctrl_ch0.get_temperature() {
Some(val) => {
val
}
None => { ThermodynamicTemperature::new::<degree_celsius>(NAN) }
Some(val) => val,
None => ThermodynamicTemperature::new::<degree_celsius>(NAN),
}
}
@ -382,26 +392,34 @@ impl Thermostat{
r0: sh.r0,
b: sh.b,
}
}
fn apply_steinhart_hart(&mut self, sh: ThermistorParams) {
self.pid_ctrl_ch0.apply_sh(
Sh_Params {
self.pid_ctrl_ch0.apply_sh(Sh_Params {
t0: ThermodynamicTemperature::new::<degree_celsius>(sh.t0),
r0: sh.r0,
b: sh.b,
})
}
)
}
fn get_tec_settings(&mut self) -> TecSettingSummary {
TecSettingSummary {
i_set: TecSettingsSummaryField { value: self.tec_settings.i_set, max: TecSettings::MAX_I_SET },
max_v: TecSettingsSummaryField { value: self.tec_settings.max_v_set, max: TecSettings::MAX_V_MAX },
max_i_pos: TecSettingsSummaryField { value: self.tec_settings.max_i_pos_set, max: TecSettings::MAX_I_POS_CURRENT },
max_i_neg: TecSettingsSummaryField { value: self.tec_settings.max_i_neg_set, max: TecSettings::MAX_I_NEG_CURRENT },
i_set: TecSettingsSummaryField {
value: self.tec_settings.i_set,
max: TecSettings::MAX_I_SET,
},
max_v: TecSettingsSummaryField {
value: self.tec_settings.max_v_set,
max: TecSettings::MAX_V_MAX,
},
max_i_pos: TecSettingsSummaryField {
value: self.tec_settings.max_i_pos_set,
max: TecSettings::MAX_I_POS_CURRENT,
},
max_i_neg: TecSettingsSummaryField {
value: self.tec_settings.max_i_neg_set,
max: TecSettings::MAX_I_NEG_CURRENT,
},
}
}
@ -426,14 +444,18 @@ impl Thermostat{
}
pub fn set_temperature_setpoint(&mut self, t: ThermodynamicTemperature) {
let t = t.min(self.temp_mon.get_upper_limit()).max(self.temp_mon.get_lower_limit());
let t = t
.min(self.temp_mon.get_upper_limit())
.max(self.temp_mon.get_lower_limit());
self.pid_ctrl_ch0.set_pid_setpoint(t);
self.temp_mon.set_setpoint(t);
}
pub fn apply_temp_mon_settings(&mut self, settings: TempMonSettings) {
self.temp_mon.set_upper_limit(ThermodynamicTemperature::new::<degree_celsius>(settings.upper_limit));
self.temp_mon.set_lower_limit(ThermodynamicTemperature::new::<degree_celsius>(settings.lower_limit));
self.temp_mon
.set_upper_limit(ThermodynamicTemperature::new::<degree_celsius>(settings.upper_limit));
self.temp_mon
.set_lower_limit(ThermodynamicTemperature::new::<degree_celsius>(settings.lower_limit));
}
pub fn set_temp_mon_upper_limit(&mut self, t: ThermodynamicTemperature) {
@ -453,7 +475,9 @@ impl Thermostat{
}
pub fn set_temp_adc_sinc5_sinc1_with_postfilter(&mut self, index: u8, odr: ad7172::PostFilter) {
self.ad7172.set_sinc5_sinc1_with_50hz_60hz_rejection(index, odr).unwrap();
self.ad7172
.set_sinc5_sinc1_with_50hz_60hz_rejection(index, odr)
.unwrap();
}
pub fn set_temp_adc_sinc3_fine_filter(&mut self, index: u8, rate: f32) {
@ -510,22 +534,14 @@ impl Thermostat{
self.apply_temp_mon_settings(settings.temp_mon_settings);
match settings.temp_adc_settings.rate {
Some(rate) => {
match settings.temp_adc_settings.filter_type {
FilterType::Sinc3 => {
self.set_temp_adc_sinc3_filter(0, SingleChODR::closest(rate).unwrap())
}
FilterType::Sinc5Sinc1 => {
self.set_temp_adc_sinc5_sinc1_filter(0, SingleChODR::closest(rate).unwrap())
}
FilterType::Sinc3WithFineODR => {
self.set_temp_adc_sinc3_fine_filter(0, rate)
}
Some(rate) => match settings.temp_adc_settings.filter_type {
FilterType::Sinc3 => self.set_temp_adc_sinc3_filter(0, SingleChODR::closest(rate).unwrap()),
FilterType::Sinc5Sinc1 => self.set_temp_adc_sinc5_sinc1_filter(0, SingleChODR::closest(rate).unwrap()),
FilterType::Sinc3WithFineODR => self.set_temp_adc_sinc3_fine_filter(0, rate),
FilterType::Sinc5Sinc1With50hz60HzRejection => {
self.set_temp_adc_sinc5_sinc1_with_postfilter(0, PostFilter::closest(rate).unwrap())
}
}
}
},
None => {
debug!(" Temperature ADC Settings is not found");
}
@ -533,7 +549,9 @@ impl Thermostat{
self.set_pid_engaged(settings.pid_engaged);
self.pid_ctrl_ch0.apply_pid_params(settings.pid_params);
self.set_temperature_setpoint(ThermodynamicTemperature::new::<degree_celsius>(settings.temperature_setpoint));
self.set_temperature_setpoint(ThermodynamicTemperature::new::<degree_celsius>(
settings.temperature_setpoint,
));
if !settings.pid_engaged {
self.set_i(settings.tec_settings.i_set.value);
}
@ -575,5 +593,5 @@ pub struct TecSettingSummary {
pub struct ThermistorParams {
t0: f32,
r0: ElectricalResistance,
b: f32
b: f32,
}