forked from M-Labs/thermostat
Compare commits
24 Commits
Author | SHA1 | Date | |
---|---|---|---|
00d5feaa8d | |||
09be55e12a | |||
76547be90a | |||
8b975e656e | |||
ae3d8b51d4 | |||
17edae44fb | |||
03b4561142 | |||
631a10938d | |||
6cd6a6a2c2 | |||
b93e2fbb7b | |||
76b95f66e0 | |||
8008870bc1 | |||
7646ff9037 | |||
6f81a63d12 | |||
78012f6fdd | |||
bb4f43fe1c | |||
9df0fe406f | |||
5ba74c6d9b | |||
6f0acc73b8 | |||
f29e86310d | |||
b04a61c414 | |||
cd680dd6cd | |||
e3e3237d2f | |||
570c0324b3 |
8
Cargo.lock
generated
8
Cargo.lock
generated
@ -327,10 +327,10 @@ dependencies = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "panic-abort"
|
name = "panic-halt"
|
||||||
version = "0.3.2"
|
version = "0.2.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4e20e6499bbbc412f280b04a42346b356c6fa0753d5fd22b7bd752ff34c778ee"
|
checksum = "de96540e0ebde571dc55c73d60ef407c653844e6f9a1e2fdbd40c07b9252d812"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "panic-semihosting"
|
name = "panic-semihosting"
|
||||||
@ -563,7 +563,7 @@ dependencies = [
|
|||||||
"nb 1.0.0",
|
"nb 1.0.0",
|
||||||
"nom",
|
"nom",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"panic-abort",
|
"panic-halt",
|
||||||
"panic-semihosting",
|
"panic-semihosting",
|
||||||
"serde",
|
"serde",
|
||||||
"serde-json-core",
|
"serde-json-core",
|
||||||
|
@ -7,14 +7,14 @@ authors = ["Astro <astro@spaceboyz.net>"]
|
|||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
keywords = ["thermostat", "laser", "physics"]
|
keywords = ["thermostat", "laser", "physics"]
|
||||||
repository = "https://git.m-labs.hk/M-Labs/thermostat"
|
repository = "https://git.m-labs.hk/M-Labs/thermostat"
|
||||||
edition = "2018"
|
edition = "2021"
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
features = []
|
features = []
|
||||||
default-target = "thumbv7em-none-eabihf"
|
default-target = "thumbv7em-none-eabihf"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
panic-abort = "0.3"
|
panic-halt = "0.2"
|
||||||
panic-semihosting = { version = "0.5", optional = true }
|
panic-semihosting = { version = "0.5", optional = true }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
bare-metal = "1"
|
bare-metal = "1"
|
||||||
|
88
README.md
88
README.md
@ -29,7 +29,7 @@ Alternatively, you can install the Rust toolchain without Nix using rustup; see
|
|||||||
|
|
||||||
Connect SWDIO/SWCLK/RST/GND to a programmer such as ST-Link v2.1. Run OpenOCD:
|
Connect SWDIO/SWCLK/RST/GND to a programmer such as ST-Link v2.1. Run OpenOCD:
|
||||||
```shell
|
```shell
|
||||||
openocd -f interface/stlink-v2-1.cfg -f target/stm32f4x.cfg
|
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg
|
||||||
```
|
```
|
||||||
|
|
||||||
You may need to power up the programmer before powering the device.
|
You may need to power up the programmer before powering the device.
|
||||||
@ -64,7 +64,7 @@ On a Windows machine install [st.com](https://st.com) DfuSe USB device firmware
|
|||||||
|
|
||||||
### OpenOCD
|
### OpenOCD
|
||||||
```shell
|
```shell
|
||||||
openocd -f interface/stlink-v2-1.cfg -f target/stm32f4x.cfg -c "program target/thumbv7em-none-eabihf/release/thermostat verify reset;exit"
|
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg -c "program target/thumbv7em-none-eabihf/release/thermostat verify reset;exit"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Network
|
## Network
|
||||||
@ -94,36 +94,42 @@ The scope of this setting is per TCP session.
|
|||||||
Send commands as simple text string terminated by `\n`. Responses are
|
Send commands as simple text string terminated by `\n`. Responses are
|
||||||
formatted as line-delimited JSON.
|
formatted as line-delimited JSON.
|
||||||
|
|
||||||
| Syntax | Function |
|
| Syntax | Function |
|
||||||
| --- | --- |
|
|----------------------------------|-------------------------------------------------------------------------------|
|
||||||
| `report` | Show current input |
|
| `report` | Show current input |
|
||||||
| `report mode` | Show current report mode |
|
| `report mode` | Show current report mode |
|
||||||
| `report mode <off/on>` | Set report mode |
|
| `report mode <off/on>` | Set report mode |
|
||||||
| `pwm` | Show current PWM settings |
|
| `pwm` | Show current PWM settings |
|
||||||
| `pwm <0/1> max_i_pos <amp>` | Set maximum positive output current |
|
| `pwm <0/1> max_i_pos <amp>` | Set maximum positive output current |
|
||||||
| `pwm <0/1> max_i_neg <amp>` | Set maximum negative output current |
|
| `pwm <0/1> max_i_neg <amp>` | Set maximum negative output current |
|
||||||
| `pwm <0/1> max_v <volt>` | Set maximum output voltage |
|
| `pwm <0/1> max_v <volt>` | Set maximum output voltage |
|
||||||
| `pwm <0/1> i_set <amp>` | Disengage PID, set fixed output current |
|
| `pwm <0/1> i_set <amp>` | Disengage PID, set fixed output current |
|
||||||
| `pwm <0/1> pid` | Let output current to be controlled by the PID |
|
| `pwm <0/1> pid` | Let output current to be controlled by the PID |
|
||||||
| `center <0/1> <volt>` | Set the MAX1968 0A-centerpoint to the specified fixed voltage |
|
| `center <0/1> <volt>` | Set the MAX1968 0A-centerpoint to the specified fixed voltage |
|
||||||
| `center <0/1> vref` | Set the MAX1968 0A-centerpoint to measure from VREF |
|
| `center <0/1> vref` | Set the MAX1968 0A-centerpoint to measure from VREF |
|
||||||
| `pid` | Show PID configuration |
|
| `pid` | Show PID configuration |
|
||||||
| `pid <0/1> target <deg_celsius>` | Set the PID controller target temperature |
|
| `pid <0/1> target <deg_celsius>` | Set the PID controller target temperature |
|
||||||
| `pid <0/1> kp <value>` | Set proportional gain |
|
| `pid <0/1> kp <value>` | Set proportional gain |
|
||||||
| `pid <0/1> ki <value>` | Set integral gain |
|
| `pid <0/1> ki <value>` | Set integral gain |
|
||||||
| `pid <0/1> kd <value>` | Set differential gain |
|
| `pid <0/1> kd <value>` | Set differential gain |
|
||||||
| `pid <0/1> output_min <amp>` | Set mininum output |
|
| `pid <0/1> output_min <amp>` | Set mininum output |
|
||||||
| `pid <0/1> output_max <amp>` | Set maximum output |
|
| `pid <0/1> output_max <amp>` | Set maximum output |
|
||||||
| `s-h` | Show Steinhart-Hart equation parameters |
|
| `s-h` | Show Steinhart-Hart equation parameters |
|
||||||
| `s-h <0/1> <t0/b/r0> <value>` | Set Steinhart-Hart parameter for a channel |
|
| `s-h <0/1> <t0/b/r0> <value>` | Set Steinhart-Hart parameter for a channel |
|
||||||
| `postfilter` | Show postfilter settings |
|
| `postfilter` | Show postfilter settings |
|
||||||
| `postfilter <0/1> off` | Disable postfilter |
|
| `postfilter <0/1> off` | Disable postfilter |
|
||||||
| `postfilter <0/1> rate <rate>` | Set postfilter output data rate |
|
| `postfilter <0/1> rate <rate>` | Set postfilter output data rate |
|
||||||
| `load [0/1]` | Restore configuration for channel all/0/1 from flash |
|
| `load [0/1]` | Restore configuration for channel all/0/1 from flash |
|
||||||
| `save [0/1]` | Save configuration for channel all/0/1 to flash |
|
| `save [0/1]` | Save configuration for channel all/0/1 to flash |
|
||||||
| `reset` | Reset the device |
|
| `reset` | Reset the device |
|
||||||
| `dfu` | Reset device and enters USB device firmware update (DFU) mode |
|
| `dfu` | Reset device and enters USB device firmware update (DFU) mode |
|
||||||
| `ipv4 <X.X.X.X/L> [Y.Y.Y.Y]` | Configure IPv4 address, netmask length, and optional default gateway |
|
| `ipv4 <X.X.X.X/L> [Y.Y.Y.Y]` | Configure IPv4 address, netmask length, and optional default gateway |
|
||||||
|
| `fan` | Show current fan settings and sensors' measurements |
|
||||||
|
| `fan <value>` | Set fan power with values from 1 to 100 |
|
||||||
|
| `fan auto` | Enable automatic fan speed control |
|
||||||
|
| `fcurve <a> <b> <c>` | Set fan controller curve coefficients (see *Fan control* section) |
|
||||||
|
| `fcurve default` | Set fan controller curve coefficients to defaults (see *Fan control* section) |
|
||||||
|
| `hwrev` | Show hardware revision, and settings related to it |
|
||||||
|
|
||||||
|
|
||||||
## USB
|
## USB
|
||||||
@ -178,7 +184,7 @@ postfilter rate can be tuned with the `postfilter` command.
|
|||||||
- Connect TEC module device 1 to TEC1- and TEC1+.
|
- Connect TEC module device 1 to TEC1- and TEC1+.
|
||||||
- The GND pin is for shielding not for sinking TEC module currents.
|
- The GND pin is for shielding not for sinking TEC module currents.
|
||||||
|
|
||||||
When using a TEC module with the Thermostat, the Thermostat expects the thermal load (where the thermistor is located) to heat up with a positive software current set point, and cool down with a negative current set point.
|
When using a TEC module with the Thermostat, the Thermostat expects the thermal load (where the thermistor is located) to cool down with a positive software current set point, and heat up with a negative current set point.
|
||||||
|
|
||||||
Testing heat flow direction with a low set current is recommended before installation of the TEC module.
|
Testing heat flow direction with a low set current is recommended before installation of the TEC module.
|
||||||
|
|
||||||
@ -252,13 +258,12 @@ with the following keys.
|
|||||||
| Key | Unit | Description |
|
| Key | Unit | Description |
|
||||||
| --- | :---: | --- |
|
| --- | :---: | --- |
|
||||||
| `channel` | Integer | Channel `0`, or `1` |
|
| `channel` | Integer | Channel `0`, or `1` |
|
||||||
| `time` | Milliseconds | Temperature measurement time |
|
| `time` | Seconds | Temperature measurement time |
|
||||||
| `adc` | Volts | AD7172 input |
|
| `adc` | Volts | AD7172 input |
|
||||||
| `sens` | Ohms | Thermistor resistance derived from `adc` |
|
| `sens` | Ohms | Thermistor resistance derived from `adc` |
|
||||||
| `temperature` | Degrees Celsius | Steinhart-Hart conversion result derived from `sens` |
|
| `temperature` | Degrees Celsius | Steinhart-Hart conversion result derived from `sens` |
|
||||||
| `pid_engaged` | Boolean | `true` if in closed-loop mode |
|
| `pid_engaged` | Boolean | `true` if in closed-loop mode |
|
||||||
| `i_set` | Amperes | TEC output current |
|
| `i_set` | Amperes | TEC output current |
|
||||||
| `vref` | Volts | MAX1968 VREF (1.5 V) |
|
|
||||||
| `dac_value` | Volts | AD5680 output derived from `i_set` |
|
| `dac_value` | Volts | AD5680 output derived from `i_set` |
|
||||||
| `dac_feedback` | Volts | ADC measurement of the AD5680 output |
|
| `dac_feedback` | Volts | ADC measurement of the AD5680 output |
|
||||||
| `i_tec` | Volts | MAX1968 TEC current monitor |
|
| `i_tec` | Volts | MAX1968 TEC current monitor |
|
||||||
@ -266,6 +271,19 @@ with the following keys.
|
|||||||
| `tec_u_meas` | Volts | Measurement of the voltage across the TEC |
|
| `tec_u_meas` | Volts | Measurement of the voltage across the TEC |
|
||||||
| `pid_output` | Amperes | PID control output |
|
| `pid_output` | Amperes | PID control output |
|
||||||
|
|
||||||
|
Note: With Thermostat v2 and below, the voltage and current readouts `i_tec` and `tec_i` are disabled and null due to faulty hardware that introduces a lot of noise in the signal.
|
||||||
|
|
||||||
## PID Tuning
|
## PID Tuning
|
||||||
|
|
||||||
The thermostat implements a PID control loop for each of the TEC channels, more details on setting up the PID control loop can be found [here](./doc/PID%20tuning.md).
|
The thermostat implements a PID control loop for each of the TEC channels, more details on setting up the PID control loop can be found [here](./doc/PID%20tuning.md).
|
||||||
|
|
||||||
|
## Fan control
|
||||||
|
|
||||||
|
Fan control commands are available for thermostat revisions with an integrated fan system:
|
||||||
|
1. `fan` - show fan stats: `fan_pwm`, `abs_max_tec_i`, `auto_mode`, `k_a`, `k_b`, `k_c`.
|
||||||
|
2. `fan auto` - enable auto speed controller mode, where fan speed is controlled by the fan curve `fcurve`.
|
||||||
|
3. `fan <value>` - set the fan power with the value from `1` to `100` and disable auto mode. There is no way to completely disable the fan.
|
||||||
|
Please note that power doesn't correlate with the actual speed linearly.
|
||||||
|
4. `fcurve <a> <b> <c>` - set coefficients of the controlling curve `a*x^2 + b*x + c`, where `x` is `abs_max_tec_i/MAX_TEC_I`, a normalized value in range [0,1],
|
||||||
|
i.e. the (linear) proportion of current output capacity used, on the channel with the largest current flow. The controlling curve is also clamped to [0,1].
|
||||||
|
5. `fcurve default` - restore fan curve coefficients to defaults: `a = 1.0, b = 0.0, c = 0.0`.
|
||||||
|
14
flake.lock
generated
14
flake.lock
generated
@ -3,11 +3,11 @@
|
|||||||
"mozilla-overlay": {
|
"mozilla-overlay": {
|
||||||
"flake": false,
|
"flake": false,
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1638887313,
|
"lastModified": 1690536331,
|
||||||
"narHash": "sha256-FMYV6rVtvSIfthgC1sK1xugh3y7muoQcvduMdriz4ag=",
|
"narHash": "sha256-aRIf2FB2GTdfF7gl13WyETmiV/J7EhBGkSWXfZvlxcA=",
|
||||||
"owner": "mozilla",
|
"owner": "mozilla",
|
||||||
"repo": "nixpkgs-mozilla",
|
"repo": "nixpkgs-mozilla",
|
||||||
"rev": "7c1e8b1dd6ed0043fb4ee0b12b815256b0b9de6f",
|
"rev": "db89c8707edcffefcd8e738459d511543a339ff5",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@ -18,16 +18,16 @@
|
|||||||
},
|
},
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1641870998,
|
"lastModified": 1691421349,
|
||||||
"narHash": "sha256-6HkxR2WZsm37VoQS7jgp6Omd71iw6t1kP8bDbaqCDuI=",
|
"narHash": "sha256-RRJyX0CUrs4uW4gMhd/X4rcDG8PTgaaCQM5rXEJOx6g=",
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "386234e2a61e1e8acf94dfa3a3d3ca19a6776efb",
|
"rev": "011567f35433879aae5024fc6ec53f2a0568a6c4",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"ref": "nixos-21.11",
|
"ref": "nixos-23.05",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
|
14
flake.nix
14
flake.nix
@ -1,15 +1,15 @@
|
|||||||
{
|
{
|
||||||
description = "Firmware for the Sinara 8451 Thermostat";
|
description = "Firmware for the Sinara 8451 Thermostat";
|
||||||
|
|
||||||
inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-21.11;
|
inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-23.05;
|
||||||
inputs.mozilla-overlay = { url = github:mozilla/nixpkgs-mozilla; flake = false; };
|
inputs.mozilla-overlay = { url = github:mozilla/nixpkgs-mozilla; flake = false; };
|
||||||
|
|
||||||
outputs = { self, nixpkgs, mozilla-overlay }:
|
outputs = { self, nixpkgs, mozilla-overlay }:
|
||||||
let
|
let
|
||||||
pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ (import mozilla-overlay) ]; };
|
pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ (import mozilla-overlay) ]; };
|
||||||
rustManifest = pkgs.fetchurl {
|
rustManifest = pkgs.fetchurl {
|
||||||
url = "https://static.rust-lang.org/dist/2021-10-26/channel-rust-nightly.toml";
|
url = "https://static.rust-lang.org/dist/2022-12-15/channel-rust-stable.toml";
|
||||||
sha256 = "sha256-1hLbypXA+nuH7o3AHCokzSBZAvQxvef4x9+XxO3aBao=";
|
hash = "sha256-S7epLlflwt0d1GZP44u5Xosgf6dRrmr8xxC+Ml2Pq7c=";
|
||||||
};
|
};
|
||||||
|
|
||||||
targets = [
|
targets = [
|
||||||
@ -22,12 +22,12 @@
|
|||||||
inherit targets;
|
inherit targets;
|
||||||
extensions = ["rust-src"];
|
extensions = ["rust-src"];
|
||||||
};
|
};
|
||||||
rust = rustChannelOfTargets "nightly" null targets;
|
rust = rustChannelOfTargets "stable" null targets;
|
||||||
rustPlatform = pkgs.recurseIntoAttrs (pkgs.makeRustPlatform {
|
rustPlatform = pkgs.recurseIntoAttrs (pkgs.makeRustPlatform {
|
||||||
rustc = rust;
|
rustc = rust;
|
||||||
cargo = rust;
|
cargo = rust;
|
||||||
});
|
});
|
||||||
thermostat = rustPlatform.buildRustPackage rec {
|
thermostat = rustPlatform.buildRustPackage {
|
||||||
name = "thermostat";
|
name = "thermostat";
|
||||||
version = "0.0.0";
|
version = "0.0.0";
|
||||||
|
|
||||||
@ -67,9 +67,7 @@
|
|||||||
devShell.x86_64-linux = pkgs.mkShell {
|
devShell.x86_64-linux = pkgs.mkShell {
|
||||||
name = "thermostat-dev-shell";
|
name = "thermostat-dev-shell";
|
||||||
buildInputs = with pkgs; [
|
buildInputs = with pkgs; [
|
||||||
rustPlatform.rust.rustc
|
rust openocd dfu-util
|
||||||
rustPlatform.rust.cargo
|
|
||||||
openocd dfu-util
|
|
||||||
] ++ (with python3Packages; [
|
] ++ (with python3Packages; [
|
||||||
numpy matplotlib
|
numpy matplotlib
|
||||||
]);
|
]);
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import socket
|
import socket
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
|
|
||||||
class CommandError(Exception):
|
class CommandError(Exception):
|
||||||
pass
|
pass
|
||||||
@ -8,6 +9,14 @@ class Client:
|
|||||||
def __init__(self, host="192.168.1.26", port=23, timeout=None):
|
def __init__(self, host="192.168.1.26", port=23, timeout=None):
|
||||||
self._socket = socket.create_connection((host, port), timeout)
|
self._socket = socket.create_connection((host, port), timeout)
|
||||||
self._lines = [""]
|
self._lines = [""]
|
||||||
|
self._check_zero_limits()
|
||||||
|
|
||||||
|
def _check_zero_limits(self):
|
||||||
|
pwm_report = self.get_pwm()
|
||||||
|
for pwm_channel in pwm_report:
|
||||||
|
for limit in ["max_i_neg", "max_i_pos", "max_v"]:
|
||||||
|
if pwm_channel[limit]["value"] == 0.0:
|
||||||
|
logging.warning("`{}` limit is set to zero on channel {}".format(limit, pwm_channel["channel"]))
|
||||||
|
|
||||||
def _read_line(self):
|
def _read_line(self):
|
||||||
# read more lines
|
# read more lines
|
||||||
|
@ -2,11 +2,13 @@ use smoltcp::time::{Duration, Instant};
|
|||||||
use uom::si::{
|
use uom::si::{
|
||||||
f64::{
|
f64::{
|
||||||
ElectricPotential,
|
ElectricPotential,
|
||||||
|
ElectricCurrent,
|
||||||
ElectricalResistance,
|
ElectricalResistance,
|
||||||
ThermodynamicTemperature,
|
ThermodynamicTemperature,
|
||||||
Time,
|
Time,
|
||||||
},
|
},
|
||||||
electric_potential::volt,
|
electric_potential::volt,
|
||||||
|
electric_current::ampere,
|
||||||
electrical_resistance::ohm,
|
electrical_resistance::ohm,
|
||||||
thermodynamic_temperature::degree_celsius,
|
thermodynamic_temperature::degree_celsius,
|
||||||
time::millisecond,
|
time::millisecond,
|
||||||
@ -26,11 +28,10 @@ pub struct ChannelState {
|
|||||||
pub adc_calibration: ad7172::ChannelCalibration,
|
pub adc_calibration: ad7172::ChannelCalibration,
|
||||||
pub adc_time: Instant,
|
pub adc_time: Instant,
|
||||||
pub adc_interval: Duration,
|
pub adc_interval: Duration,
|
||||||
/// VREF for the TEC (1.5V)
|
|
||||||
pub vref: ElectricPotential,
|
|
||||||
/// i_set 0A center point
|
/// i_set 0A center point
|
||||||
pub center: CenterPoint,
|
pub center: CenterPoint,
|
||||||
pub dac_value: ElectricPotential,
|
pub dac_value: ElectricPotential,
|
||||||
|
pub i_set: ElectricCurrent,
|
||||||
pub pid_engaged: bool,
|
pub pid_engaged: bool,
|
||||||
pub pid: pid::Controller,
|
pub pid: pid::Controller,
|
||||||
pub sh: sh::Parameters,
|
pub sh: sh::Parameters,
|
||||||
@ -44,10 +45,9 @@ impl ChannelState {
|
|||||||
adc_time: Instant::from_secs(0),
|
adc_time: Instant::from_secs(0),
|
||||||
// default: 10 Hz
|
// default: 10 Hz
|
||||||
adc_interval: Duration::from_millis(100),
|
adc_interval: Duration::from_millis(100),
|
||||||
// updated later with Channels.read_vref()
|
|
||||||
vref: ElectricPotential::new::<volt>(1.5),
|
|
||||||
center: CenterPoint::Vref,
|
center: CenterPoint::Vref,
|
||||||
dac_value: ElectricPotential::new::<volt>(0.0),
|
dac_value: ElectricPotential::new::<volt>(0.0),
|
||||||
|
i_set: ElectricCurrent::new::<ampere>(0.0),
|
||||||
pid_engaged: false,
|
pid_engaged: false,
|
||||||
pid: pid::Controller::new(pid::Parameters::default()),
|
pid: pid::Controller::new(pid::Parameters::default()),
|
||||||
sh: sh::Parameters::default(),
|
sh: sh::Parameters::default(),
|
||||||
|
111
src/channels.rs
111
src/channels.rs
@ -1,4 +1,5 @@
|
|||||||
use heapless::{consts::{U2, U1024}, Vec};
|
use core::cmp::max_by;
|
||||||
|
use heapless::{consts::U2, Vec};
|
||||||
use serde::{Serialize, Serializer};
|
use serde::{Serialize, Serializer};
|
||||||
use smoltcp::time::Instant;
|
use smoltcp::time::Instant;
|
||||||
use stm32f4xx_hal::hal;
|
use stm32f4xx_hal::hal;
|
||||||
@ -16,27 +17,34 @@ use crate::{
|
|||||||
channel::{Channel, Channel0, Channel1},
|
channel::{Channel, Channel0, Channel1},
|
||||||
channel_state::ChannelState,
|
channel_state::ChannelState,
|
||||||
command_parser::{CenterPoint, PwmPin},
|
command_parser::{CenterPoint, PwmPin},
|
||||||
pins,
|
command_handler::JsonBuffer,
|
||||||
|
pins::{self, Channel0VRef, Channel1VRef},
|
||||||
steinhart_hart,
|
steinhart_hart,
|
||||||
|
hw_rev,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const CHANNELS: usize = 2;
|
pub const CHANNELS: usize = 2;
|
||||||
pub const R_SENSE: f64 = 0.05;
|
pub const R_SENSE: f64 = 0.05;
|
||||||
|
|
||||||
|
// as stated in the MAX1968 datasheet
|
||||||
|
pub const MAX_TEC_I: f64 = 3.0;
|
||||||
|
|
||||||
// DAC chip outputs 0-5v, which is then passed through a resistor dividor to provide 0-3v range
|
// DAC chip outputs 0-5v, which is then passed through a resistor dividor to provide 0-3v range
|
||||||
const DAC_OUT_V_MAX: f64 = 3.0;
|
const DAC_OUT_V_MAX: f64 = 3.0;
|
||||||
|
|
||||||
// TODO: -pub
|
// TODO: -pub
|
||||||
pub struct Channels {
|
pub struct Channels<'a> {
|
||||||
channel0: Channel<Channel0>,
|
channel0: Channel<Channel0>,
|
||||||
channel1: Channel<Channel1>,
|
channel1: Channel<Channel1>,
|
||||||
pub adc: ad7172::Adc<pins::AdcSpi, pins::AdcNss>,
|
pub adc: ad7172::Adc<pins::AdcSpi, pins::AdcNss>,
|
||||||
/// stm32f4 integrated adc
|
/// stm32f4 integrated adc
|
||||||
pins_adc: pins::PinsAdc,
|
pins_adc: pins::PinsAdc,
|
||||||
pub pwm: pins::PwmPins,
|
pub pwm: pins::PwmPins,
|
||||||
|
hwrev: &'a hw_rev::HWRev,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Channels {
|
impl<'a> Channels<'a> {
|
||||||
pub fn new(pins: pins::Pins) -> Self {
|
pub fn new(pins: pins::Pins, hwrev: &'a hw_rev::HWRev) -> Self {
|
||||||
let mut adc = ad7172::Adc::new(pins.adc_spi, pins.adc_nss).unwrap();
|
let mut adc = ad7172::Adc::new(pins.adc_spi, pins.adc_nss).unwrap();
|
||||||
// Feature not used
|
// Feature not used
|
||||||
adc.set_sync_enable(false).unwrap();
|
adc.set_sync_enable(false).unwrap();
|
||||||
@ -54,9 +62,8 @@ impl Channels {
|
|||||||
let channel1 = Channel::new(pins.channel1, adc_calibration1);
|
let channel1 = Channel::new(pins.channel1, adc_calibration1);
|
||||||
let pins_adc = pins.pins_adc;
|
let pins_adc = pins.pins_adc;
|
||||||
let pwm = pins.pwm;
|
let pwm = pins.pwm;
|
||||||
let mut channels = Channels { channel0, channel1, adc, pins_adc, pwm };
|
let mut channels = Channels { channel0, channel1, adc, pins_adc, pwm, hwrev };
|
||||||
for channel in 0..CHANNELS {
|
for channel in 0..CHANNELS {
|
||||||
channels.channel_state(channel).vref = channels.read_vref(channel);
|
|
||||||
channels.calibrate_dac_value(channel);
|
channels.calibrate_dac_value(channel);
|
||||||
channels.set_i(channel, ElectricCurrent::new::<ampere>(0.0));
|
channels.set_i(channel, ElectricCurrent::new::<ampere>(0.0));
|
||||||
}
|
}
|
||||||
@ -96,11 +103,8 @@ impl Channels {
|
|||||||
/// calculate the TEC i_set centerpoint
|
/// calculate the TEC i_set centerpoint
|
||||||
pub fn get_center(&mut self, channel: usize) -> ElectricPotential {
|
pub fn get_center(&mut self, channel: usize) -> ElectricPotential {
|
||||||
match self.channel_state(channel).center {
|
match self.channel_state(channel).center {
|
||||||
CenterPoint::Vref => {
|
CenterPoint::Vref =>
|
||||||
let vref = self.read_vref(channel);
|
self.read_vref(channel),
|
||||||
self.channel_state(channel).vref = vref;
|
|
||||||
vref
|
|
||||||
},
|
|
||||||
CenterPoint::Override(center_point) =>
|
CenterPoint::Override(center_point) =>
|
||||||
ElectricPotential::new::<volt>(center_point.into()),
|
ElectricPotential::new::<volt>(center_point.into()),
|
||||||
}
|
}
|
||||||
@ -113,11 +117,8 @@ impl Channels {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_i(&mut self, channel: usize) -> ElectricCurrent {
|
pub fn get_i(&mut self, channel: usize) -> ElectricCurrent {
|
||||||
let center_point = self.get_center(channel);
|
let i_set = self.channel_state(channel).i_set;
|
||||||
let r_sense = ElectricalResistance::new::<ohm>(R_SENSE);
|
i_set
|
||||||
let voltage = self.get_dac(channel);
|
|
||||||
let i_tec = (voltage - center_point) / (10.0 * r_sense);
|
|
||||||
i_tec
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// i_set DAC
|
/// i_set DAC
|
||||||
@ -132,7 +133,12 @@ impl Channels {
|
|||||||
voltage
|
voltage
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_i(&mut self, channel: usize, i_tec: ElectricCurrent) -> ElectricCurrent {
|
pub fn set_i(&mut self, channel: usize, i_set: ElectricCurrent) -> ElectricCurrent {
|
||||||
|
// Silently clamp i_set
|
||||||
|
let i_ceiling = ElectricCurrent::new::<ampere>(MAX_TEC_I);
|
||||||
|
let i_floor = ElectricCurrent::new::<ampere>(-MAX_TEC_I);
|
||||||
|
let i_set = i_set.min(i_ceiling).max(i_floor);
|
||||||
|
|
||||||
let vref_meas = match channel.into() {
|
let vref_meas = match channel.into() {
|
||||||
0 => self.channel0.vref_meas,
|
0 => self.channel0.vref_meas,
|
||||||
1 => self.channel1.vref_meas,
|
1 => self.channel1.vref_meas,
|
||||||
@ -140,10 +146,11 @@ impl Channels {
|
|||||||
};
|
};
|
||||||
let center_point = vref_meas;
|
let center_point = vref_meas;
|
||||||
let r_sense = ElectricalResistance::new::<ohm>(R_SENSE);
|
let r_sense = ElectricalResistance::new::<ohm>(R_SENSE);
|
||||||
let voltage = i_tec * 10.0 * r_sense + center_point;
|
let voltage = i_set * 10.0 * r_sense + center_point;
|
||||||
let voltage = self.set_dac(channel, voltage);
|
let voltage = self.set_dac(channel, voltage);
|
||||||
let i_tec = (voltage - center_point) / (10.0 * r_sense);
|
let i_set = (voltage - center_point) / (10.0 * r_sense);
|
||||||
i_tec
|
self.channel_state(channel).i_set = i_set;
|
||||||
|
i_set
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_dac_feedback(&mut self, channel: usize) -> ElectricPotential {
|
pub fn read_dac_feedback(&mut self, channel: usize) -> ElectricPotential {
|
||||||
@ -205,20 +212,30 @@ impl Channels {
|
|||||||
pub fn read_vref(&mut self, channel: usize) -> ElectricPotential {
|
pub fn read_vref(&mut self, channel: usize) -> ElectricPotential {
|
||||||
match channel {
|
match channel {
|
||||||
0 => {
|
0 => {
|
||||||
let sample = self.pins_adc.convert(
|
match &self.channel0.vref_pin {
|
||||||
&self.channel0.vref_pin,
|
Channel0VRef::Analog(vref_pin) => {
|
||||||
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
let sample = self.pins_adc.convert(
|
||||||
);
|
vref_pin,
|
||||||
let mv = self.pins_adc.sample_to_millivolts(sample);
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
||||||
ElectricPotential::new::<millivolt>(mv as f64)
|
);
|
||||||
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
|
},
|
||||||
|
Channel0VRef::Disabled(_) => ElectricPotential::new::<volt>(1.5)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
1 => {
|
1 => {
|
||||||
let sample = self.pins_adc.convert(
|
match &self.channel1.vref_pin {
|
||||||
&self.channel1.vref_pin,
|
Channel1VRef::Analog(vref_pin) => {
|
||||||
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
let sample = self.pins_adc.convert(
|
||||||
);
|
vref_pin,
|
||||||
let mv = self.pins_adc.sample_to_millivolts(sample);
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
||||||
ElectricPotential::new::<millivolt>(mv as f64)
|
);
|
||||||
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
|
},
|
||||||
|
Channel1VRef::Disabled(_) => ElectricPotential::new::<volt>(1.5)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
@ -430,10 +447,9 @@ impl Channels {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn report(&mut self, channel: usize) -> Report {
|
fn report(&mut self, channel: usize) -> Report {
|
||||||
let vref = self.channel_state(channel).vref;
|
|
||||||
let i_set = self.get_i(channel);
|
let i_set = self.get_i(channel);
|
||||||
let i_tec = self.read_itec(channel);
|
let i_tec = if self.hwrev.major > 2 {Some(self.read_itec(channel))} else {None};
|
||||||
let tec_i = self.get_tec_i(channel);
|
let tec_i = if self.hwrev.major > 2 {Some(self.get_tec_i(channel))} else {None};
|
||||||
let dac_value = self.get_dac(channel);
|
let dac_value = self.get_dac(channel);
|
||||||
let state = self.channel_state(channel);
|
let state = self.channel_state(channel);
|
||||||
let pid_output = ElectricCurrent::new::<ampere>(state.pid.y1);
|
let pid_output = ElectricCurrent::new::<ampere>(state.pid.y1);
|
||||||
@ -447,7 +463,6 @@ impl Channels {
|
|||||||
.map(|temperature| temperature.get::<degree_celsius>()),
|
.map(|temperature| temperature.get::<degree_celsius>()),
|
||||||
pid_engaged: state.pid_engaged,
|
pid_engaged: state.pid_engaged,
|
||||||
i_set,
|
i_set,
|
||||||
vref,
|
|
||||||
dac_value,
|
dac_value,
|
||||||
dac_feedback: self.read_dac_feedback(channel),
|
dac_feedback: self.read_dac_feedback(channel),
|
||||||
i_tec,
|
i_tec,
|
||||||
@ -473,6 +488,15 @@ impl Channels {
|
|||||||
serde_json_core::to_vec(&summaries)
|
serde_json_core::to_vec(&summaries)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn pid_engaged(&mut self) -> bool {
|
||||||
|
for channel in 0..CHANNELS {
|
||||||
|
if self.channel_state(channel).pid_engaged {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
fn pwm_summary(&mut self, channel: usize) -> PwmSummary {
|
fn pwm_summary(&mut self, channel: usize) -> PwmSummary {
|
||||||
PwmSummary {
|
PwmSummary {
|
||||||
channel,
|
channel,
|
||||||
@ -518,9 +542,13 @@ impl Channels {
|
|||||||
}
|
}
|
||||||
serde_json_core::to_vec(&summaries)
|
serde_json_core::to_vec(&summaries)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
type JsonBuffer = Vec<u8, U1024>;
|
pub fn current_abs_max_tec_i(&mut self) -> ElectricCurrent {
|
||||||
|
max_by(self.get_tec_i(0).abs(),
|
||||||
|
self.get_tec_i(1).abs(),
|
||||||
|
|a, b| a.partial_cmp(b).unwrap_or(core::cmp::Ordering::Equal))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct Report {
|
pub struct Report {
|
||||||
@ -532,11 +560,10 @@ pub struct Report {
|
|||||||
temperature: Option<f64>,
|
temperature: Option<f64>,
|
||||||
pid_engaged: bool,
|
pid_engaged: bool,
|
||||||
i_set: ElectricCurrent,
|
i_set: ElectricCurrent,
|
||||||
vref: ElectricPotential,
|
|
||||||
dac_value: ElectricPotential,
|
dac_value: ElectricPotential,
|
||||||
dac_feedback: ElectricPotential,
|
dac_feedback: ElectricPotential,
|
||||||
i_tec: ElectricPotential,
|
i_tec: Option<ElectricPotential>,
|
||||||
tec_i: ElectricCurrent,
|
tec_i: Option<ElectricCurrent>,
|
||||||
tec_u_meas: ElectricPotential,
|
tec_u_meas: ElectricPotential,
|
||||||
pid_output: ElectricCurrent,
|
pid_output: ElectricCurrent,
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use smoltcp::socket::TcpSocket;
|
use smoltcp::socket::TcpSocket;
|
||||||
use log::{error, warn};
|
use log::{error, warn};
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
|
use heapless::{consts::U1024, Vec};
|
||||||
use super::{
|
use super::{
|
||||||
net,
|
net,
|
||||||
command_parser::{
|
command_parser::{
|
||||||
@ -12,7 +13,6 @@ use super::{
|
|||||||
PwmPin,
|
PwmPin,
|
||||||
ShParameter
|
ShParameter
|
||||||
},
|
},
|
||||||
leds::Leds,
|
|
||||||
ad7172,
|
ad7172,
|
||||||
CHANNEL_CONFIG_KEY,
|
CHANNEL_CONFIG_KEY,
|
||||||
channels::{
|
channels::{
|
||||||
@ -22,7 +22,9 @@ use super::{
|
|||||||
config::ChannelConfig,
|
config::ChannelConfig,
|
||||||
dfu,
|
dfu,
|
||||||
flash_store::FlashStore,
|
flash_store::FlashStore,
|
||||||
session::Session
|
session::Session,
|
||||||
|
FanCtrl,
|
||||||
|
hw_rev::HWRev,
|
||||||
};
|
};
|
||||||
|
|
||||||
use uom::{
|
use uom::{
|
||||||
@ -55,6 +57,8 @@ pub enum Error {
|
|||||||
FlashError
|
FlashError
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type JsonBuffer = Vec<u8, U1024>;
|
||||||
|
|
||||||
fn send_line(socket: &mut TcpSocket, data: &[u8]) -> bool {
|
fn send_line(socket: &mut TcpSocket, data: &[u8]) -> bool {
|
||||||
let send_free = socket.send_capacity() - socket.send_queue();
|
let send_free = socket.send_capacity() - socket.send_queue();
|
||||||
if data.len() > send_free + 1 {
|
if data.len() > send_free + 1 {
|
||||||
@ -171,18 +175,16 @@ impl Handler {
|
|||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn engage_pid (socket: &mut TcpSocket, channels: &mut Channels, leds: &mut Leds, channel: usize) -> Result<Handler, Error> {
|
fn engage_pid (socket: &mut TcpSocket, channels: &mut Channels, channel: usize) -> Result<Handler, Error> {
|
||||||
channels.channel_state(channel).pid_engaged = true;
|
channels.channel_state(channel).pid_engaged = true;
|
||||||
leds.g3.on();
|
|
||||||
send_line(socket, b"{}");
|
send_line(socket, b"{}");
|
||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_pwm (socket: &mut TcpSocket, channels: &mut Channels, leds: &mut Leds, channel: usize, pin: PwmPin, value: f64) -> Result<Handler, Error> {
|
fn set_pwm (socket: &mut TcpSocket, channels: &mut Channels, channel: usize, pin: PwmPin, value: f64) -> Result<Handler, Error> {
|
||||||
match pin {
|
match pin {
|
||||||
PwmPin::ISet => {
|
PwmPin::ISet => {
|
||||||
channels.channel_state(channel).pid_engaged = false;
|
channels.channel_state(channel).pid_engaged = false;
|
||||||
leds.g3.off();
|
|
||||||
let current = ElectricCurrent::new::<ampere>(value);
|
let current = ElectricCurrent::new::<ampere>(value);
|
||||||
channels.set_i(channel, current);
|
channels.set_i(channel, current);
|
||||||
channels.power_up(channel);
|
channels.power_up(channel);
|
||||||
@ -205,11 +207,11 @@ impl Handler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn set_center_point(socket: &mut TcpSocket, channels: &mut Channels, channel: usize, center: CenterPoint) -> Result<Handler, Error> {
|
fn set_center_point(socket: &mut TcpSocket, channels: &mut Channels, channel: usize, center: CenterPoint) -> Result<Handler, Error> {
|
||||||
let i_tec = channels.get_i(channel);
|
let i_set = channels.get_i(channel);
|
||||||
let state = channels.channel_state(channel);
|
let state = channels.channel_state(channel);
|
||||||
state.center = center;
|
state.center = center;
|
||||||
if !state.pid_engaged {
|
if !state.pid_engaged {
|
||||||
channels.set_i(channel, i_tec);
|
channels.set_i(channel, i_set);
|
||||||
}
|
}
|
||||||
send_line(socket, b"{}");
|
send_line(socket, b"{}");
|
||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
@ -341,19 +343,88 @@ impl Handler {
|
|||||||
Ok(Handler::Reset)
|
Ok(Handler::Reset)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_command (command: Command, socket: &mut TcpSocket, channels: &mut Channels, session: &Session, leds: &mut Leds, store: &mut FlashStore, ipv4_config: &mut Ipv4Config) -> Result<Self, Error> {
|
fn set_fan(socket: &mut TcpSocket, fan_pwm: u32, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
|
||||||
|
if !fan_ctrl.fan_available() {
|
||||||
|
send_line(socket, b"{ \"warning\": \"this thermostat doesn't have fan!\" }");
|
||||||
|
return Ok(Handler::Handled);
|
||||||
|
}
|
||||||
|
fan_ctrl.set_auto_mode(false);
|
||||||
|
fan_ctrl.set_pwm(fan_pwm);
|
||||||
|
if fan_ctrl.fan_pwm_recommended() {
|
||||||
|
send_line(socket, b"{}");
|
||||||
|
} else {
|
||||||
|
send_line(socket, b"{ \"warning\": \"this fan doesn't have full PWM support. Use it at your own risk!\" }");
|
||||||
|
}
|
||||||
|
Ok(Handler::Handled)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn show_fan(socket: &mut TcpSocket, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
|
||||||
|
match fan_ctrl.summary() {
|
||||||
|
Ok(buf) => {
|
||||||
|
send_line(socket, &buf);
|
||||||
|
Ok(Handler::Handled)
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("unable to serialize fan summary: {:?}", e);
|
||||||
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
||||||
|
Err(Error::ReportError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fan_auto(socket: &mut TcpSocket, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
|
||||||
|
if !fan_ctrl.fan_available() {
|
||||||
|
send_line(socket, b"{ \"warning\": \"this thermostat doesn't have fan!\" }");
|
||||||
|
return Ok(Handler::Handled);
|
||||||
|
}
|
||||||
|
fan_ctrl.set_auto_mode(true);
|
||||||
|
if fan_ctrl.fan_pwm_recommended() {
|
||||||
|
send_line(socket, b"{}");
|
||||||
|
} else {
|
||||||
|
send_line(socket, b"{ \"warning\": \"this fan doesn't have full PWM support. Use it at your own risk!\" }");
|
||||||
|
}
|
||||||
|
Ok(Handler::Handled)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fan_curve(socket: &mut TcpSocket, fan_ctrl: &mut FanCtrl, k_a: f32, k_b: f32, k_c: f32) -> Result<Handler, Error> {
|
||||||
|
fan_ctrl.set_curve(k_a, k_b, k_c);
|
||||||
|
send_line(socket, b"{}");
|
||||||
|
Ok(Handler::Handled)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fan_defaults(socket: &mut TcpSocket, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
|
||||||
|
fan_ctrl.restore_defaults();
|
||||||
|
send_line(socket, b"{}");
|
||||||
|
Ok(Handler::Handled)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn show_hwrev(socket: &mut TcpSocket, hwrev: HWRev) -> Result<Handler, Error> {
|
||||||
|
match hwrev.summary() {
|
||||||
|
Ok(buf) => {
|
||||||
|
send_line(socket, &buf);
|
||||||
|
Ok(Handler::Handled)
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("unable to serialize HWRev summary: {:?}", e);
|
||||||
|
let _ = writeln!(socket, "{{\"error\":\"{:?}\"}}", e);
|
||||||
|
Err(Error::ReportError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn handle_command(command: Command, socket: &mut TcpSocket, channels: &mut Channels, session: &Session, store: &mut FlashStore, ipv4_config: &mut Ipv4Config, fan_ctrl: &mut FanCtrl, hwrev: HWRev) -> Result<Self, Error> {
|
||||||
match command {
|
match command {
|
||||||
Command::Quit => Ok(Handler::CloseSocket),
|
Command::Quit => Ok(Handler::CloseSocket),
|
||||||
Command::Reporting(_reporting) => Handler::reporting(socket),
|
Command::Reporting(_reporting) => Handler::reporting(socket),
|
||||||
Command::Show(ShowCommand::Reporting) => Handler::show_report_mode(socket, session),
|
Command::Show(ShowCommand::Reporting) => Handler::show_report_mode(socket, session),
|
||||||
Command::Show(ShowCommand::Input) => Handler::show_report(socket, channels),
|
Command::Show(ShowCommand::Input) => Handler::show_report(socket, channels),
|
||||||
Command::Show(ShowCommand::Pid) => Handler::show_pid(socket, channels),
|
Command::Show(ShowCommand::Pid) => Handler::show_pid(socket, channels),
|
||||||
Command::Show(ShowCommand::Pwm) => Handler::show_pwm(socket, channels),
|
Command::Show(ShowCommand::Pwm) => Handler::show_pwm(socket, channels),
|
||||||
Command::Show(ShowCommand::SteinhartHart) => Handler::show_steinhart_hart(socket, channels),
|
Command::Show(ShowCommand::SteinhartHart) => Handler::show_steinhart_hart(socket, channels),
|
||||||
Command::Show(ShowCommand::PostFilter) => Handler::show_post_filter(socket, channels),
|
Command::Show(ShowCommand::PostFilter) => Handler::show_post_filter(socket, channels),
|
||||||
Command::Show(ShowCommand::Ipv4) => Handler::show_ipv4(socket, ipv4_config),
|
Command::Show(ShowCommand::Ipv4) => Handler::show_ipv4(socket, ipv4_config),
|
||||||
Command::PwmPid { channel } => Handler::engage_pid(socket, channels, leds, channel),
|
Command::PwmPid { channel } => Handler::engage_pid(socket, channels, channel),
|
||||||
Command::Pwm { channel, pin, value } => Handler::set_pwm(socket, channels, leds, channel, pin, value),
|
Command::Pwm { channel, pin, value } => Handler::set_pwm(socket, channels, channel, pin, value),
|
||||||
Command::CenterPoint { channel, center } => Handler::set_center_point(socket, channels, channel, center),
|
Command::CenterPoint { channel, center } => Handler::set_center_point(socket, channels, channel, center),
|
||||||
Command::Pid { channel, parameter, value } => Handler::set_pid(socket, channels, channel, parameter, value),
|
Command::Pid { channel, parameter, value } => Handler::set_pid(socket, channels, channel, parameter, value),
|
||||||
Command::SteinhartHart { channel, parameter, value } => Handler::set_steinhart_hart(socket, channels, channel, parameter, value),
|
Command::SteinhartHart { channel, parameter, value } => Handler::set_steinhart_hart(socket, channels, channel, parameter, value),
|
||||||
@ -363,7 +434,13 @@ impl Handler {
|
|||||||
Command::Save { channel } => Handler::save_channel(socket, channels, channel, store),
|
Command::Save { channel } => Handler::save_channel(socket, channels, channel, store),
|
||||||
Command::Ipv4(config) => Handler::set_ipv4(socket, store, config),
|
Command::Ipv4(config) => Handler::set_ipv4(socket, store, config),
|
||||||
Command::Reset => Handler::reset(channels),
|
Command::Reset => Handler::reset(channels),
|
||||||
Command::Dfu => Handler::dfu(channels)
|
Command::Dfu => Handler::dfu(channels),
|
||||||
|
Command::FanSet {fan_pwm} => Handler::set_fan(socket, fan_pwm, fan_ctrl),
|
||||||
|
Command::ShowFan => Handler::show_fan(socket, fan_ctrl),
|
||||||
|
Command::FanAuto => Handler::fan_auto(socket, fan_ctrl),
|
||||||
|
Command::FanCurve { k_a, k_b, k_c } => Handler::fan_curve(socket, fan_ctrl, k_a, k_b, k_c),
|
||||||
|
Command::FanCurveDefaults => Handler::fan_defaults(socket, fan_ctrl),
|
||||||
|
Command::ShowHWRev => Handler::show_hwrev(socket, hwrev),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,6 +10,7 @@ use nom::{
|
|||||||
sequence::preceded,
|
sequence::preceded,
|
||||||
multi::{fold_many0, fold_many1},
|
multi::{fold_many0, fold_many1},
|
||||||
error::ErrorKind,
|
error::ErrorKind,
|
||||||
|
Needed,
|
||||||
};
|
};
|
||||||
use num_traits::{Num, ParseFloatError};
|
use num_traits::{Num, ParseFloatError};
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
@ -178,6 +179,18 @@ pub enum Command {
|
|||||||
rate: Option<f32>,
|
rate: Option<f32>,
|
||||||
},
|
},
|
||||||
Dfu,
|
Dfu,
|
||||||
|
FanSet {
|
||||||
|
fan_pwm: u32
|
||||||
|
},
|
||||||
|
FanAuto,
|
||||||
|
ShowFan,
|
||||||
|
FanCurve {
|
||||||
|
k_a: f32,
|
||||||
|
k_b: f32,
|
||||||
|
k_c: f32,
|
||||||
|
},
|
||||||
|
FanCurveDefaults,
|
||||||
|
ShowHWRev,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn end(input: &[u8]) -> IResult<&[u8], ()> {
|
fn end(input: &[u8]) -> IResult<&[u8], ()> {
|
||||||
@ -520,6 +533,57 @@ fn ipv4(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
|||||||
))(input)
|
))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn fan(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
||||||
|
let (input, _) = tag("fan")(input)?;
|
||||||
|
alt((
|
||||||
|
|input| {
|
||||||
|
let (input, _) = whitespace(input)?;
|
||||||
|
|
||||||
|
let (input, result) = alt((
|
||||||
|
|input| {
|
||||||
|
let (input, _) = tag("auto")(input)?;
|
||||||
|
Ok((input, Ok(Command::FanAuto)))
|
||||||
|
},
|
||||||
|
|input| {
|
||||||
|
let (input, value) = unsigned(input)?;
|
||||||
|
Ok((input, Ok(Command::FanSet { fan_pwm: value.unwrap_or(0)})))
|
||||||
|
},
|
||||||
|
))(input)?;
|
||||||
|
Ok((input, result))
|
||||||
|
},
|
||||||
|
value(Ok(Command::ShowFan), end)
|
||||||
|
))(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fan_curve(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
||||||
|
let (input, _) = tag("fcurve")(input)?;
|
||||||
|
alt((
|
||||||
|
|input| {
|
||||||
|
let (input, _) = whitespace(input)?;
|
||||||
|
let (input, result) = alt((
|
||||||
|
|input| {
|
||||||
|
let (input, _) = tag("default")(input)?;
|
||||||
|
Ok((input, Ok(Command::FanCurveDefaults)))
|
||||||
|
},
|
||||||
|
|input| {
|
||||||
|
let (input, k_a) = float(input)?;
|
||||||
|
let (input, _) = whitespace(input)?;
|
||||||
|
let (input, k_b) = float(input)?;
|
||||||
|
let (input, _) = whitespace(input)?;
|
||||||
|
let (input, k_c) = float(input)?;
|
||||||
|
if k_a.is_ok() && k_b.is_ok() && k_c.is_ok() {
|
||||||
|
Ok((input, Ok(Command::FanCurve { k_a: k_a.unwrap() as f32, k_b: k_b.unwrap() as f32, k_c: k_c.unwrap() as f32 })))
|
||||||
|
} else {
|
||||||
|
Err(nom::Err::Incomplete(Needed::Size(3)))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
))(input)?;
|
||||||
|
Ok((input, result))
|
||||||
|
},
|
||||||
|
value(Err(Error::Incomplete), end)
|
||||||
|
))(input)
|
||||||
|
}
|
||||||
|
|
||||||
fn command(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
fn command(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
||||||
alt((value(Ok(Command::Quit), tag("quit")),
|
alt((value(Ok(Command::Quit), tag("quit")),
|
||||||
load,
|
load,
|
||||||
@ -533,6 +597,9 @@ fn command(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
|||||||
steinhart_hart,
|
steinhart_hart,
|
||||||
postfilter,
|
postfilter,
|
||||||
value(Ok(Command::Dfu), tag("dfu")),
|
value(Ok(Command::Dfu), tag("dfu")),
|
||||||
|
fan,
|
||||||
|
fan_curve,
|
||||||
|
value(Ok(Command::ShowHWRev), tag("hwrev")),
|
||||||
))(input)
|
))(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -754,4 +821,44 @@ mod test {
|
|||||||
center: CenterPoint::Vref,
|
center: CenterPoint::Vref,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_fan_show() {
|
||||||
|
let command = Command::parse(b"fan");
|
||||||
|
assert_eq!(command, Ok(Command::ShowFan));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_fan_set() {
|
||||||
|
let command = Command::parse(b"fan 42");
|
||||||
|
assert_eq!(command, Ok(Command::FanSet {fan_pwm: 42}));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_fan_auto() {
|
||||||
|
let command = Command::parse(b"fan auto");
|
||||||
|
assert_eq!(command, Ok(Command::FanAuto));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_fcurve_set() {
|
||||||
|
let command = Command::parse(b"fcurve 1.2 3.4 5.6");
|
||||||
|
assert_eq!(command, Ok(Command::FanCurve {
|
||||||
|
k_a: 1.2,
|
||||||
|
k_b: 3.4,
|
||||||
|
k_c: 5.6
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_fcurve_default() {
|
||||||
|
let command = Command::parse(b"fcurve default");
|
||||||
|
assert_eq!(command, Ok(Command::FanCurveDefaults));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_hwrev() {
|
||||||
|
let command = Command::parse(b"hwrev");
|
||||||
|
assert_eq!(command, Ok(Command::ShowHWRev));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use core::arch::asm;
|
||||||
use cortex_m_rt::pre_init;
|
use cortex_m_rt::pre_init;
|
||||||
use stm32f4xx_hal::stm32::{RCC, SYSCFG};
|
use stm32f4xx_hal::stm32::{RCC, SYSCFG};
|
||||||
|
|
||||||
|
152
src/fan_ctrl.rs
Normal file
152
src/fan_ctrl.rs
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
use num_traits::Float;
|
||||||
|
use serde::Serialize;
|
||||||
|
use stm32f4xx_hal::{
|
||||||
|
pwm::{self, PwmChannels},
|
||||||
|
pac::TIM8,
|
||||||
|
};
|
||||||
|
use uom::si::{
|
||||||
|
f64::ElectricCurrent,
|
||||||
|
electric_current::ampere,
|
||||||
|
};
|
||||||
|
use crate::{
|
||||||
|
hw_rev::HWSettings,
|
||||||
|
command_handler::JsonBuffer,
|
||||||
|
channels::MAX_TEC_I,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub type FanPin = PwmChannels<TIM8, pwm::C4>;
|
||||||
|
|
||||||
|
const MAX_USER_FAN_PWM: f32 = 100.0;
|
||||||
|
const MIN_USER_FAN_PWM: f32 = 1.0;
|
||||||
|
|
||||||
|
|
||||||
|
pub struct FanCtrl {
|
||||||
|
fan: Option<FanPin>,
|
||||||
|
fan_auto: bool,
|
||||||
|
pwm_enabled: bool,
|
||||||
|
k_a: f32,
|
||||||
|
k_b: f32,
|
||||||
|
k_c: f32,
|
||||||
|
abs_max_tec_i: f32,
|
||||||
|
hw_settings: HWSettings,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FanCtrl {
|
||||||
|
pub fn new(fan: Option<FanPin>, hw_settings: HWSettings) -> Self {
|
||||||
|
let mut fan_ctrl = FanCtrl {
|
||||||
|
fan,
|
||||||
|
// do not enable auto mode by default,
|
||||||
|
// but allow to turn it at the user's own risk
|
||||||
|
fan_auto: hw_settings.fan_pwm_recommended,
|
||||||
|
pwm_enabled: false,
|
||||||
|
k_a: hw_settings.fan_k_a,
|
||||||
|
k_b: hw_settings.fan_k_b,
|
||||||
|
k_c: hw_settings.fan_k_c,
|
||||||
|
abs_max_tec_i: 0f32,
|
||||||
|
hw_settings,
|
||||||
|
};
|
||||||
|
if fan_ctrl.fan_auto {
|
||||||
|
fan_ctrl.enable_pwm();
|
||||||
|
}
|
||||||
|
fan_ctrl
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cycle(&mut self, abs_max_tec_i: ElectricCurrent) {
|
||||||
|
self.abs_max_tec_i = abs_max_tec_i.get::<ampere>() as f32;
|
||||||
|
if self.fan_auto && self.hw_settings.fan_available {
|
||||||
|
let scaled_current = self.abs_max_tec_i / MAX_TEC_I as f32;
|
||||||
|
// do not limit upper bound, as it will be limited in the set_pwm()
|
||||||
|
let pwm = (MAX_USER_FAN_PWM * (scaled_current * (scaled_current * self.k_a + self.k_b) + self.k_c)) as u32;
|
||||||
|
self.set_pwm(pwm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn summary(&mut self) -> Result<JsonBuffer, serde_json_core::ser::Error> {
|
||||||
|
if self.hw_settings.fan_available {
|
||||||
|
let summary = FanSummary {
|
||||||
|
fan_pwm: self.get_pwm(),
|
||||||
|
abs_max_tec_i: self.abs_max_tec_i,
|
||||||
|
auto_mode: self.fan_auto,
|
||||||
|
k_a: self.k_a,
|
||||||
|
k_b: self.k_b,
|
||||||
|
k_c: self.k_c,
|
||||||
|
};
|
||||||
|
serde_json_core::to_vec(&summary)
|
||||||
|
} else {
|
||||||
|
let summary: Option<()> = None;
|
||||||
|
serde_json_core::to_vec(&summary)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_auto_mode(&mut self, fan_auto: bool) {
|
||||||
|
self.fan_auto = fan_auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_curve(&mut self, k_a: f32, k_b: f32, k_c: f32) {
|
||||||
|
self.k_a = k_a;
|
||||||
|
self.k_b = k_b;
|
||||||
|
self.k_c = k_c;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn restore_defaults(&mut self) {
|
||||||
|
self.set_curve(self.hw_settings.fan_k_a,
|
||||||
|
self.hw_settings.fan_k_b,
|
||||||
|
self.hw_settings.fan_k_c);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_pwm(&mut self, fan_pwm: u32) -> f32 {
|
||||||
|
if self.fan.is_none() || (!self.pwm_enabled && !self.enable_pwm()) {
|
||||||
|
return 0f32;
|
||||||
|
}
|
||||||
|
let fan = self.fan.as_mut().unwrap();
|
||||||
|
let fan_pwm = fan_pwm.min(MAX_USER_FAN_PWM as u32).max(MIN_USER_FAN_PWM as u32);
|
||||||
|
let duty = scale_number(fan_pwm as f32, self.hw_settings.min_fan_pwm, self.hw_settings.max_fan_pwm, MIN_USER_FAN_PWM, MAX_USER_FAN_PWM);
|
||||||
|
let max = fan.get_max_duty();
|
||||||
|
let value = ((duty * (max as f32)) as u16).min(max);
|
||||||
|
fan.set_duty(value);
|
||||||
|
value as f32 / (max as f32)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn fan_pwm_recommended(&self) -> bool {
|
||||||
|
self.hw_settings.fan_pwm_recommended
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn fan_available(&self) -> bool {
|
||||||
|
self.hw_settings.fan_available
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_pwm(&self) -> u32 {
|
||||||
|
if let Some(fan) = &self.fan {
|
||||||
|
let duty = fan.get_duty();
|
||||||
|
let max = fan.get_max_duty();
|
||||||
|
scale_number(duty as f32 / (max as f32), MIN_USER_FAN_PWM, MAX_USER_FAN_PWM, self.hw_settings.min_fan_pwm, self.hw_settings.max_fan_pwm).round() as u32
|
||||||
|
} else { 0 }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn enable_pwm(&mut self) -> bool {
|
||||||
|
if self.fan.is_some() && self.hw_settings.fan_available {
|
||||||
|
let fan = self.fan.as_mut().unwrap();
|
||||||
|
fan.set_duty(0);
|
||||||
|
fan.enable();
|
||||||
|
self.pwm_enabled = true;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn scale_number(unscaled: f32, to_min: f32, to_max: f32, from_min: f32, from_max: f32) -> f32 {
|
||||||
|
(to_max - to_min) * (unscaled - from_min) / (from_max - from_min) + to_min
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct FanSummary {
|
||||||
|
fan_pwm: u32,
|
||||||
|
abs_max_tec_i: f32,
|
||||||
|
auto_mode: bool,
|
||||||
|
k_a: f32,
|
||||||
|
k_b: f32,
|
||||||
|
k_c: f32,
|
||||||
|
}
|
82
src/hw_rev.rs
Normal file
82
src/hw_rev.rs
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
pins::HWRevPins,
|
||||||
|
command_handler::JsonBuffer,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Serialize, Copy, Clone)]
|
||||||
|
pub struct HWRev {
|
||||||
|
pub major: u8,
|
||||||
|
pub minor: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
pub struct HWSettings {
|
||||||
|
pub fan_k_a: f32,
|
||||||
|
pub fan_k_b: f32,
|
||||||
|
pub fan_k_c: f32,
|
||||||
|
pub min_fan_pwm: f32,
|
||||||
|
pub max_fan_pwm: f32,
|
||||||
|
pub fan_pwm_freq_hz: u32,
|
||||||
|
pub fan_available: bool,
|
||||||
|
pub fan_pwm_recommended: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Clone)]
|
||||||
|
struct HWSummary<'a> {
|
||||||
|
rev: &'a HWRev,
|
||||||
|
settings: &'a HWSettings,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HWRev {
|
||||||
|
pub fn detect_hw_rev(hwrev_pins: &HWRevPins) -> Self {
|
||||||
|
let (h0, h1, h2, h3) = (hwrev_pins.hwrev0.is_high(), hwrev_pins.hwrev1.is_high(),
|
||||||
|
hwrev_pins.hwrev2.is_high(), hwrev_pins.hwrev3.is_high());
|
||||||
|
match (h0, h1, h2, h3) {
|
||||||
|
(true, true, true, false) => HWRev { major: 1, minor: 0 },
|
||||||
|
(true, false, false, false) => HWRev { major: 2, minor: 0 },
|
||||||
|
(false, true, false, false) => HWRev { major: 2, minor: 2 },
|
||||||
|
(_, _, _, _) => HWRev { major: 0, minor: 0 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn settings(&self) -> HWSettings {
|
||||||
|
match (self.major, self.minor) {
|
||||||
|
(2, 2) => HWSettings {
|
||||||
|
fan_k_a: 1.0,
|
||||||
|
fan_k_b: 0.0,
|
||||||
|
fan_k_c: 0.0,
|
||||||
|
// below this value motor's autostart feature may fail,
|
||||||
|
// according to internal experiments
|
||||||
|
min_fan_pwm: 0.04,
|
||||||
|
max_fan_pwm: 1.0,
|
||||||
|
// According to `SUNON DC Brushless Fan & Blower(255-E)` catalogue p.36-37
|
||||||
|
// model MF35101V1-1000U-G99 doesn't have a PWM wire, but we'll follow their others models'
|
||||||
|
// recommended frequency, as it is said by the Thermostat's schematics that we can
|
||||||
|
// use PWM, but not stated at which frequency
|
||||||
|
fan_pwm_freq_hz: 25_000,
|
||||||
|
fan_available: true,
|
||||||
|
// see https://github.com/sinara-hw/Thermostat/issues/115 and
|
||||||
|
// https://git.m-labs.hk/M-Labs/thermostat/issues/69#issuecomment-6464 for explanation
|
||||||
|
fan_pwm_recommended: false,
|
||||||
|
},
|
||||||
|
(_, _) => HWSettings {
|
||||||
|
fan_k_a: 0.0,
|
||||||
|
fan_k_b: 0.0,
|
||||||
|
fan_k_c: 0.0,
|
||||||
|
min_fan_pwm: 0.0,
|
||||||
|
max_fan_pwm: 0.0,
|
||||||
|
fan_pwm_freq_hz: 0,
|
||||||
|
fan_available: false,
|
||||||
|
fan_pwm_recommended: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn summary(&self) -> Result<JsonBuffer, serde_json_core::ser::Error> {
|
||||||
|
let settings = self.settings();
|
||||||
|
let summary = HWSummary { rev: self, settings: &settings };
|
||||||
|
serde_json_core::to_vec(&summary)
|
||||||
|
}
|
||||||
|
}
|
26
src/main.rs
26
src/main.rs
@ -1,16 +1,14 @@
|
|||||||
#![cfg_attr(not(test), no_std)]
|
#![cfg_attr(not(test), no_std)]
|
||||||
#![cfg_attr(not(test), no_main)]
|
#![cfg_attr(not(test), no_main)]
|
||||||
#![feature(maybe_uninit_extra, asm)]
|
|
||||||
#![cfg_attr(test, allow(unused))]
|
#![cfg_attr(test, allow(unused))]
|
||||||
// TODO: #![deny(warnings, unused)]
|
// TODO: #![deny(warnings, unused)]
|
||||||
|
|
||||||
#[cfg(not(any(feature = "semihosting", test)))]
|
#[cfg(not(any(feature = "semihosting", test)))]
|
||||||
use panic_abort as _;
|
use panic_halt as _;
|
||||||
#[cfg(all(feature = "semihosting", not(test)))]
|
#[cfg(all(feature = "semihosting", not(test)))]
|
||||||
use panic_semihosting as _;
|
use panic_semihosting as _;
|
||||||
|
|
||||||
use log::{error, info, warn};
|
use log::{error, info, warn};
|
||||||
|
|
||||||
use cortex_m::asm::wfi;
|
use cortex_m::asm::wfi;
|
||||||
use cortex_m_rt::entry;
|
use cortex_m_rt::entry;
|
||||||
use stm32f4xx_hal::{
|
use stm32f4xx_hal::{
|
||||||
@ -54,6 +52,9 @@ mod flash_store;
|
|||||||
mod dfu;
|
mod dfu;
|
||||||
mod command_handler;
|
mod command_handler;
|
||||||
use command_handler::Handler;
|
use command_handler::Handler;
|
||||||
|
mod fan_ctrl;
|
||||||
|
use fan_ctrl::FanCtrl;
|
||||||
|
mod hw_rev;
|
||||||
|
|
||||||
const HSE: MegaHertz = MegaHertz(8);
|
const HSE: MegaHertz = MegaHertz(8);
|
||||||
#[cfg(not(feature = "semihosting"))]
|
#[cfg(not(feature = "semihosting"))]
|
||||||
@ -118,8 +119,8 @@ fn main() -> ! {
|
|||||||
|
|
||||||
timer::setup(cp.SYST, clocks);
|
timer::setup(cp.SYST, clocks);
|
||||||
|
|
||||||
let (pins, mut leds, mut eeprom, eth_pins, usb) = Pins::setup(
|
let (pins, mut leds, mut eeprom, eth_pins, usb, fan, hwrev, hw_settings) = Pins::setup(
|
||||||
clocks, dp.TIM1, dp.TIM3,
|
clocks, dp.TIM1, dp.TIM3, dp.TIM8,
|
||||||
dp.GPIOA, dp.GPIOB, dp.GPIOC, dp.GPIOD, dp.GPIOE, dp.GPIOF, dp.GPIOG,
|
dp.GPIOA, dp.GPIOB, dp.GPIOC, dp.GPIOD, dp.GPIOE, dp.GPIOF, dp.GPIOG,
|
||||||
dp.I2C1,
|
dp.I2C1,
|
||||||
dp.SPI2, dp.SPI4, dp.SPI5,
|
dp.SPI2, dp.SPI4, dp.SPI5,
|
||||||
@ -136,9 +137,8 @@ fn main() -> ! {
|
|||||||
usb::State::setup(usb);
|
usb::State::setup(usb);
|
||||||
|
|
||||||
let mut store = flash_store::store(dp.FLASH);
|
let mut store = flash_store::store(dp.FLASH);
|
||||||
|
|
||||||
|
|
||||||
let mut channels = Channels::new(pins);
|
let mut channels = Channels::new(pins, &hwrev);
|
||||||
for c in 0..CHANNELS {
|
for c in 0..CHANNELS {
|
||||||
match store.read_value::<ChannelConfig>(CHANNEL_CONFIG_KEY[c]) {
|
match store.read_value::<ChannelConfig>(CHANNEL_CONFIG_KEY[c]) {
|
||||||
Ok(Some(config)) =>
|
Ok(Some(config)) =>
|
||||||
@ -150,6 +150,8 @@ fn main() -> ! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut fan_ctrl = FanCtrl::new(fan, hw_settings);
|
||||||
|
|
||||||
// default net config:
|
// default net config:
|
||||||
let mut ipv4_config = Ipv4Config {
|
let mut ipv4_config = Ipv4Config {
|
||||||
address: [192, 168, 1, 26],
|
address: [192, 168, 1, 26],
|
||||||
@ -183,6 +185,14 @@ fn main() -> ! {
|
|||||||
server.for_each(|_, session| session.set_report_pending(channel.into()));
|
server.for_each(|_, session| session.set_report_pending(channel.into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fan_ctrl.cycle(channels.current_abs_max_tec_i());
|
||||||
|
|
||||||
|
if channels.pid_engaged() {
|
||||||
|
leds.g3.on();
|
||||||
|
} else {
|
||||||
|
leds.g3.off();
|
||||||
|
}
|
||||||
|
|
||||||
let instant = Instant::from_millis(i64::from(timer::now()));
|
let instant = Instant::from_millis(i64::from(timer::now()));
|
||||||
cortex_m::interrupt::free(net::clear_pending);
|
cortex_m::interrupt::free(net::clear_pending);
|
||||||
server.poll(instant)
|
server.poll(instant)
|
||||||
@ -206,7 +216,7 @@ fn main() -> ! {
|
|||||||
// Do nothing and feed more data to the line reader in the next loop cycle.
|
// Do nothing and feed more data to the line reader in the next loop cycle.
|
||||||
Ok(SessionInput::Nothing) => {}
|
Ok(SessionInput::Nothing) => {}
|
||||||
Ok(SessionInput::Command(command)) => {
|
Ok(SessionInput::Command(command)) => {
|
||||||
match Handler::handle_command(command, &mut socket, &mut channels, session, &mut leds, &mut store, &mut ipv4_config) {
|
match Handler::handle_command(command, &mut socket, &mut channels, session, &mut store, &mut ipv4_config, &mut fan_ctrl, hwrev) {
|
||||||
Ok(Handler::NewIPV4(ip)) => new_ipv4_config = Some(ip),
|
Ok(Handler::NewIPV4(ip)) => new_ipv4_config = Some(ip),
|
||||||
Ok(Handler::Handled) => {},
|
Ok(Handler::Handled) => {},
|
||||||
Ok(Handler::CloseSocket) => socket.close(),
|
Ok(Handler::CloseSocket) => socket.close(),
|
||||||
|
43
src/pins.rs
43
src/pins.rs
@ -23,7 +23,7 @@ use stm32f4xx_hal::{
|
|||||||
I2C1,
|
I2C1,
|
||||||
OTG_FS_GLOBAL, OTG_FS_DEVICE, OTG_FS_PWRCLK,
|
OTG_FS_GLOBAL, OTG_FS_DEVICE, OTG_FS_PWRCLK,
|
||||||
SPI2, SPI4, SPI5,
|
SPI2, SPI4, SPI5,
|
||||||
TIM1, TIM3,
|
TIM1, TIM3, TIM8
|
||||||
},
|
},
|
||||||
timer::Timer,
|
timer::Timer,
|
||||||
time::U32Ext,
|
time::U32Ext,
|
||||||
@ -33,6 +33,8 @@ use stm32_eth::EthPins;
|
|||||||
use crate::{
|
use crate::{
|
||||||
channel::{Channel0, Channel1},
|
channel::{Channel0, Channel1},
|
||||||
leds::Leds,
|
leds::Leds,
|
||||||
|
fan_ctrl::FanPin,
|
||||||
|
hw_rev::{HWRev, HWSettings},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type Eeprom = Eeprom24x<
|
pub type Eeprom = Eeprom24x<
|
||||||
@ -64,21 +66,31 @@ pub trait ChannelPins {
|
|||||||
type TecUMeasPin;
|
type TecUMeasPin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum Channel0VRef {
|
||||||
|
Analog(PA0<Analog>),
|
||||||
|
Disabled(PA0<Input<Floating>>),
|
||||||
|
}
|
||||||
|
|
||||||
impl ChannelPins for Channel0 {
|
impl ChannelPins for Channel0 {
|
||||||
type DacSpi = Dac0Spi;
|
type DacSpi = Dac0Spi;
|
||||||
type DacSync = PE4<Output<PushPull>>;
|
type DacSync = PE4<Output<PushPull>>;
|
||||||
type Shdn = PE10<Output<PushPull>>;
|
type Shdn = PE10<Output<PushPull>>;
|
||||||
type VRefPin = PA0<Analog>;
|
type VRefPin = Channel0VRef;
|
||||||
type ItecPin = PA6<Analog>;
|
type ItecPin = PA6<Analog>;
|
||||||
type DacFeedbackPin = PA4<Analog>;
|
type DacFeedbackPin = PA4<Analog>;
|
||||||
type TecUMeasPin = PC2<Analog>;
|
type TecUMeasPin = PC2<Analog>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum Channel1VRef {
|
||||||
|
Analog(PA3<Analog>),
|
||||||
|
Disabled(PA3<Input<Floating>>),
|
||||||
|
}
|
||||||
|
|
||||||
impl ChannelPins for Channel1 {
|
impl ChannelPins for Channel1 {
|
||||||
type DacSpi = Dac1Spi;
|
type DacSpi = Dac1Spi;
|
||||||
type DacSync = PF6<Output<PushPull>>;
|
type DacSync = PF6<Output<PushPull>>;
|
||||||
type Shdn = PE15<Output<PushPull>>;
|
type Shdn = PE15<Output<PushPull>>;
|
||||||
type VRefPin = PA3<Analog>;
|
type VRefPin = Channel1VRef;
|
||||||
type ItecPin = PB0<Analog>;
|
type ItecPin = PB0<Analog>;
|
||||||
type DacFeedbackPin = PA5<Analog>;
|
type DacFeedbackPin = PA5<Analog>;
|
||||||
type TecUMeasPin = PC3<Analog>;
|
type TecUMeasPin = PC3<Analog>;
|
||||||
@ -101,6 +113,13 @@ pub struct ChannelPinSet<C: ChannelPins> {
|
|||||||
pub tec_u_meas_pin: C::TecUMeasPin,
|
pub tec_u_meas_pin: C::TecUMeasPin,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct HWRevPins {
|
||||||
|
pub hwrev0: stm32f4xx_hal::gpio::gpiod::PD0<Input<Floating>>,
|
||||||
|
pub hwrev1: stm32f4xx_hal::gpio::gpiod::PD1<Input<Floating>>,
|
||||||
|
pub hwrev2: stm32f4xx_hal::gpio::gpiod::PD2<Input<Floating>>,
|
||||||
|
pub hwrev3: stm32f4xx_hal::gpio::gpiod::PD3<Input<Floating>>,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Pins {
|
pub struct Pins {
|
||||||
pub adc_spi: AdcSpi,
|
pub adc_spi: AdcSpi,
|
||||||
pub adc_nss: AdcNss,
|
pub adc_nss: AdcNss,
|
||||||
@ -114,13 +133,13 @@ impl Pins {
|
|||||||
/// Setup GPIO pins and configure MCU peripherals
|
/// Setup GPIO pins and configure MCU peripherals
|
||||||
pub fn setup(
|
pub fn setup(
|
||||||
clocks: Clocks,
|
clocks: Clocks,
|
||||||
tim1: TIM1, tim3: TIM3,
|
tim1: TIM1, tim3: TIM3, tim8: TIM8,
|
||||||
gpioa: GPIOA, gpiob: GPIOB, gpioc: GPIOC, gpiod: GPIOD, gpioe: GPIOE, gpiof: GPIOF, gpiog: GPIOG,
|
gpioa: GPIOA, gpiob: GPIOB, gpioc: GPIOC, gpiod: GPIOD, gpioe: GPIOE, gpiof: GPIOF, gpiog: GPIOG,
|
||||||
i2c1: I2C1,
|
i2c1: I2C1,
|
||||||
spi2: SPI2, spi4: SPI4, spi5: SPI5,
|
spi2: SPI2, spi4: SPI4, spi5: SPI5,
|
||||||
adc1: ADC1,
|
adc1: ADC1,
|
||||||
otg_fs_global: OTG_FS_GLOBAL, otg_fs_device: OTG_FS_DEVICE, otg_fs_pwrclk: OTG_FS_PWRCLK,
|
otg_fs_global: OTG_FS_GLOBAL, otg_fs_device: OTG_FS_DEVICE, otg_fs_pwrclk: OTG_FS_PWRCLK,
|
||||||
) -> (Self, Leds, Eeprom, EthernetPins, USB) {
|
) -> (Self, Leds, Eeprom, EthernetPins, USB, Option<FanPin>, HWRev, HWSettings) {
|
||||||
let gpioa = gpioa.split();
|
let gpioa = gpioa.split();
|
||||||
let gpiob = gpiob.split();
|
let gpiob = gpiob.split();
|
||||||
let gpioc = gpioc.split();
|
let gpioc = gpioc.split();
|
||||||
@ -141,13 +160,17 @@ impl Pins {
|
|||||||
gpioe.pe13, gpioe.pe14
|
gpioe.pe13, gpioe.pe14
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let hwrev = HWRev::detect_hw_rev(&HWRevPins {hwrev0: gpiod.pd0, hwrev1: gpiod.pd1,
|
||||||
|
hwrev2: gpiod.pd2, hwrev3: gpiod.pd3});
|
||||||
|
let hw_settings = hwrev.settings();
|
||||||
|
|
||||||
let (dac0_spi, dac0_sync) = Self::setup_dac0(
|
let (dac0_spi, dac0_sync) = Self::setup_dac0(
|
||||||
clocks, spi4,
|
clocks, spi4,
|
||||||
gpioe.pe2, gpioe.pe4, gpioe.pe6
|
gpioe.pe2, gpioe.pe4, gpioe.pe6
|
||||||
);
|
);
|
||||||
let mut shdn0 = gpioe.pe10.into_push_pull_output();
|
let mut shdn0 = gpioe.pe10.into_push_pull_output();
|
||||||
let _ = shdn0.set_low();
|
let _ = shdn0.set_low();
|
||||||
let vref0_pin = gpioa.pa0.into_analog();
|
let vref0_pin = if hwrev.major > 2 {Channel0VRef::Analog(gpioa.pa0.into_analog())} else {Channel0VRef::Disabled(gpioa.pa0)};
|
||||||
let itec0_pin = gpioa.pa6.into_analog();
|
let itec0_pin = gpioa.pa6.into_analog();
|
||||||
let dac_feedback0_pin = gpioa.pa4.into_analog();
|
let dac_feedback0_pin = gpioa.pa4.into_analog();
|
||||||
let tec_u_meas0_pin = gpioc.pc2.into_analog();
|
let tec_u_meas0_pin = gpioc.pc2.into_analog();
|
||||||
@ -167,7 +190,7 @@ impl Pins {
|
|||||||
);
|
);
|
||||||
let mut shdn1 = gpioe.pe15.into_push_pull_output();
|
let mut shdn1 = gpioe.pe15.into_push_pull_output();
|
||||||
let _ = shdn1.set_low();
|
let _ = shdn1.set_low();
|
||||||
let vref1_pin = gpioa.pa3.into_analog();
|
let vref1_pin = if hwrev.major > 2 {Channel1VRef::Analog(gpioa.pa3.into_analog())} else {Channel1VRef::Disabled(gpioa.pa3)};
|
||||||
let itec1_pin = gpiob.pb0.into_analog();
|
let itec1_pin = gpiob.pb0.into_analog();
|
||||||
let dac_feedback1_pin = gpioa.pa5.into_analog();
|
let dac_feedback1_pin = gpioa.pa5.into_analog();
|
||||||
let tec_u_meas1_pin = gpioc.pc3.into_analog();
|
let tec_u_meas1_pin = gpioc.pc3.into_analog();
|
||||||
@ -215,7 +238,11 @@ impl Pins {
|
|||||||
hclk: clocks.hclk(),
|
hclk: clocks.hclk(),
|
||||||
};
|
};
|
||||||
|
|
||||||
(pins, leds, eeprom, eth_pins, usb)
|
let fan = if hw_settings.fan_available {
|
||||||
|
Some(Timer::new(tim8, &clocks).pwm(gpioc.pc9.into_alternate(), hw_settings.fan_pwm_freq_hz.hz()))
|
||||||
|
} else { None };
|
||||||
|
|
||||||
|
(pins, leds, eeprom, eth_pins, usb, fan, hwrev, hw_settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Configure the GPIO pins for SPI operation, and initialize SPI
|
/// Configure the GPIO pins for SPI operation, and initialize SPI
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
use core::mem::MaybeUninit;
|
|
||||||
use smoltcp::{
|
use smoltcp::{
|
||||||
iface::EthernetInterface,
|
iface::EthernetInterface,
|
||||||
socket::{SocketSet, SocketHandle, TcpSocket, TcpSocketBuffer, SocketRef},
|
socket::{SocketSet, SocketHandle, TcpSocket, TcpSocketBuffer, SocketRef},
|
||||||
@ -13,6 +12,18 @@ pub struct SocketState<S> {
|
|||||||
state: S,
|
state: S,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, S: Default> SocketState<S>{
|
||||||
|
fn new(sockets: &mut SocketSet<'a>, tcp_rx_storage: &'a mut [u8; TCP_RX_BUFFER_SIZE], tcp_tx_storage: &'a mut [u8; TCP_TX_BUFFER_SIZE]) -> SocketState<S> {
|
||||||
|
let tcp_rx_buffer = TcpSocketBuffer::new(&mut tcp_rx_storage[..]);
|
||||||
|
let tcp_tx_buffer = TcpSocketBuffer::new(&mut tcp_tx_storage[..]);
|
||||||
|
let tcp_socket = TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer);
|
||||||
|
SocketState::<S> {
|
||||||
|
handle: sockets.add(tcp_socket),
|
||||||
|
state: S::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Number of server sockets and therefore concurrent client
|
/// Number of server sockets and therefore concurrent client
|
||||||
/// sessions. Many data structures in `Server::run()` correspond to
|
/// sessions. Many data structures in `Server::run()` correspond to
|
||||||
/// this const.
|
/// this const.
|
||||||
@ -35,28 +46,27 @@ impl<'a, 'b, S: Default> Server<'a, 'b, S> {
|
|||||||
where
|
where
|
||||||
F: FnOnce(&mut Server<'a, '_, S>),
|
F: FnOnce(&mut Server<'a, '_, S>),
|
||||||
{
|
{
|
||||||
let mut sockets_storage: [_; SOCKET_COUNT] = Default::default();
|
macro_rules! create_rtx_storage {
|
||||||
let mut sockets = SocketSet::new(&mut sockets_storage[..]);
|
($rx_storage:ident, $tx_storage:ident) => {
|
||||||
let mut states: [SocketState<S>; SOCKET_COUNT] = unsafe { MaybeUninit::uninit().assume_init() };
|
|
||||||
|
|
||||||
macro_rules! create_socket {
|
|
||||||
($set:ident, $rx_storage:ident, $tx_storage:ident, $target:expr) => {
|
|
||||||
let mut $rx_storage = [0; TCP_RX_BUFFER_SIZE];
|
let mut $rx_storage = [0; TCP_RX_BUFFER_SIZE];
|
||||||
let mut $tx_storage = [0; TCP_TX_BUFFER_SIZE];
|
let mut $tx_storage = [0; TCP_TX_BUFFER_SIZE];
|
||||||
let tcp_rx_buffer = TcpSocketBuffer::new(&mut $rx_storage[..]);
|
|
||||||
let tcp_tx_buffer = TcpSocketBuffer::new(&mut $tx_storage[..]);
|
|
||||||
let tcp_socket = TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer);
|
|
||||||
$target = $set.add(tcp_socket);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
create_socket!(sockets, tcp_rx_storage0, tcp_tx_storage0, states[0].handle);
|
|
||||||
create_socket!(sockets, tcp_rx_storage1, tcp_tx_storage1, states[1].handle);
|
|
||||||
create_socket!(sockets, tcp_rx_storage2, tcp_tx_storage2, states[2].handle);
|
|
||||||
create_socket!(sockets, tcp_rx_storage3, tcp_tx_storage3, states[3].handle);
|
|
||||||
|
|
||||||
for state in &mut states {
|
create_rtx_storage!(tcp_rx_storage0, tcp_tx_storage0);
|
||||||
state.state = S::default();
|
create_rtx_storage!(tcp_rx_storage1, tcp_tx_storage1);
|
||||||
}
|
create_rtx_storage!(tcp_rx_storage2, tcp_tx_storage2);
|
||||||
|
create_rtx_storage!(tcp_rx_storage3, tcp_tx_storage3);
|
||||||
|
|
||||||
|
let mut sockets_storage: [_; SOCKET_COUNT] = Default::default();
|
||||||
|
let mut sockets = SocketSet::new(&mut sockets_storage[..]);
|
||||||
|
|
||||||
|
let states: [SocketState<S>; SOCKET_COUNT] = [
|
||||||
|
SocketState::<S>::new(&mut sockets, &mut tcp_rx_storage0, &mut tcp_tx_storage0),
|
||||||
|
SocketState::<S>::new(&mut sockets, &mut tcp_rx_storage1, &mut tcp_tx_storage1),
|
||||||
|
SocketState::<S>::new(&mut sockets, &mut tcp_rx_storage2, &mut tcp_tx_storage2),
|
||||||
|
SocketState::<S>::new(&mut sockets, &mut tcp_rx_storage3, &mut tcp_tx_storage3),
|
||||||
|
];
|
||||||
|
|
||||||
let mut server = Server {
|
let mut server = Server {
|
||||||
states,
|
states,
|
||||||
|
Loading…
Reference in New Issue
Block a user