Compare commits
No commits in common. "master" and "master" have entirely different histories.
|
@ -1,5 +1,2 @@
|
||||||
target/
|
target/
|
||||||
result
|
result
|
||||||
*.bin
|
|
||||||
|
|
||||||
__pycache__/
|
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aligned"
|
name = "aligned"
|
||||||
version = "0.3.4"
|
version = "0.3.4"
|
||||||
|
@ -70,9 +68,12 @@ checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cast"
|
name = "cast"
|
||||||
version = "0.3.0"
|
version = "0.2.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
|
checksum = "4b9434b9a5aa1450faa3f9cb14ea0e8c53bb5d2b3c1bfd1ab4fc03e9f33fbfb0"
|
||||||
|
dependencies = [
|
||||||
|
"rustc_version",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
|
@ -80,38 +81,15 @@ version = "0.1.10"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "chrono"
|
|
||||||
version = "0.4.19"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
|
|
||||||
dependencies = [
|
|
||||||
"num-integer",
|
|
||||||
"num-traits",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cortex-m"
|
name = "cortex-m"
|
||||||
version = "0.6.7"
|
version = "0.6.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "9075300b07c6a56263b9b582c214d0ff037b00d45ec9fde1cc711490c56f1bb9"
|
checksum = "88cdafeafba636c00c467ded7f1587210725a1adfab0c24028a7844b87738263"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aligned",
|
"aligned",
|
||||||
"bare-metal 0.2.5",
|
"bare-metal 0.2.5",
|
||||||
"bitfield",
|
"bitfield",
|
||||||
"cortex-m 0.7.4",
|
|
||||||
"volatile-register",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "cortex-m"
|
|
||||||
version = "0.7.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "37ff967e867ca14eba0c34ac25cd71ea98c678e741e3915d923999bb2fe7c826"
|
|
||||||
dependencies = [
|
|
||||||
"bare-metal 0.2.5",
|
|
||||||
"bitfield",
|
|
||||||
"embedded-hal",
|
|
||||||
"volatile-register",
|
"volatile-register",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -121,7 +99,7 @@ version = "0.6.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1d63959cb1e003dd97233fee6762351540253237eadf06fcdcb98cbfa3f9be4a"
|
checksum = "1d63959cb1e003dd97233fee6762351540253237eadf06fcdcb98cbfa3f9be4a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cortex-m 0.6.7",
|
"cortex-m",
|
||||||
"cortex-m-semihosting",
|
"cortex-m-semihosting",
|
||||||
"log",
|
"log",
|
||||||
]
|
]
|
||||||
|
@ -132,19 +110,10 @@ version = "0.6.13"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "980c9d0233a909f355ed297ef122f257942de5e0a2cb1c39f60684b65bcb90fb"
|
checksum = "980c9d0233a909f355ed297ef122f257942de5e0a2cb1c39f60684b65bcb90fb"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cortex-m-rt-macros 0.1.8",
|
"cortex-m-rt-macros",
|
||||||
"r0",
|
"r0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "cortex-m-rt"
|
|
||||||
version = "0.7.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "3c433da385b720d5bb9f52362fa2782420798e68d40d67bfe4b0d992aba5dfe7"
|
|
||||||
dependencies = [
|
|
||||||
"cortex-m-rt-macros 0.7.0",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cortex-m-rt-macros"
|
name = "cortex-m-rt-macros"
|
||||||
version = "0.1.8"
|
version = "0.1.8"
|
||||||
|
@ -156,24 +125,13 @@ dependencies = [
|
||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "cortex-m-rt-macros"
|
|
||||||
version = "0.7.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "f0f6f3e36f203cfedbc78b357fb28730aa2c6dc1ab060ee5c2405e843988d3c7"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cortex-m-semihosting"
|
name = "cortex-m-semihosting"
|
||||||
version = "0.3.7"
|
version = "0.3.7"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6bffa6c1454368a6aa4811ae60964c38e6996d397ff8095a8b9211b1c1f749bc"
|
checksum = "6bffa6c1454368a6aa4811ae60964c38e6996d397ff8095a8b9211b1c1f749bc"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cortex-m 0.7.4",
|
"cortex-m",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -196,9 +154,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "embedded-hal"
|
name = "embedded-hal"
|
||||||
version = "0.2.6"
|
version = "0.2.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "e36cfb62ff156596c892272f3015ef952fe1525e85261fa3a7f327bd6b384ab9"
|
checksum = "fa998ce59ec9765d15216393af37a58961ddcefb14c753b4816ba2191d865fcb"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"nb 0.1.3",
|
"nb 0.1.3",
|
||||||
"void",
|
"void",
|
||||||
|
@ -306,16 +264,6 @@ dependencies = [
|
||||||
"version_check",
|
"version_check",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "num-integer"
|
|
||||||
version = "0.1.44"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
|
|
||||||
dependencies = [
|
|
||||||
"autocfg",
|
|
||||||
"num-traits",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "num-traits"
|
name = "num-traits"
|
||||||
version = "0.2.14"
|
version = "0.2.14"
|
||||||
|
@ -327,10 +275,10 @@ dependencies = [
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "panic-halt"
|
name = "panic-abort"
|
||||||
version = "0.2.0"
|
version = "0.3.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "de96540e0ebde571dc55c73d60ef407c653844e6f9a1e2fdbd40c07b9252d812"
|
checksum = "4e20e6499bbbc412f280b04a42346b356c6fa0753d5fd22b7bd752ff34c778ee"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "panic-semihosting"
|
name = "panic-semihosting"
|
||||||
|
@ -338,7 +286,7 @@ version = "0.5.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c3d55dedd501dfd02514646e0af4d7016ce36bc12ae177ef52056989966a1eec"
|
checksum = "c3d55dedd501dfd02514646e0af4d7016ce36bc12ae177ef52056989966a1eec"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cortex-m 0.7.4",
|
"cortex-m",
|
||||||
"cortex-m-semihosting",
|
"cortex-m-semihosting",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -385,18 +333,9 @@ checksum = "e2a38df5b15c8d5c7e8654189744d8e396bddc18ad48041a500ce52d6948941f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rand_core"
|
name = "rand_core"
|
||||||
version = "0.6.3"
|
version = "0.5.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
|
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "rtcc"
|
|
||||||
version = "0.2.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "ef35f9dcbf434a34dcc99b3ebba1c1945d49c70832958e932e83dc63a5273994"
|
|
||||||
dependencies = [
|
|
||||||
"chrono",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustc_version"
|
name = "rustc_version"
|
||||||
|
@ -465,9 +404,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "smoltcp"
|
name = "smoltcp"
|
||||||
version = "0.7.5"
|
version = "0.6.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "3e4a069bef843d170df47e7c0a8bf8d037f217d9f5b325865acc3e466ffe40d3"
|
checksum = "0fe46639fd2ec79eadf8fe719f237a7a0bd4dac5d957f1ca5bbdbc1c3c39e53a"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
|
@ -484,10 +423,10 @@ checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "stm32-eth"
|
name = "stm32-eth"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
source = "git+https://github.com/stm32-rs/stm32-eth.git?rev=3759c5c9#3759c5c99c0ab69bb71759030766bc0fba0b6cde"
|
source = "git+https://github.com/stm32-rs/stm32-eth.git#4d6b29bf1ecdd1f68e5bc304a3d4f170049896c8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aligned",
|
"aligned",
|
||||||
"cortex-m 0.7.4",
|
"cortex-m",
|
||||||
"smoltcp",
|
"smoltcp",
|
||||||
"stm32f4xx-hal",
|
"stm32f4xx-hal",
|
||||||
"volatile-register",
|
"volatile-register",
|
||||||
|
@ -495,31 +434,29 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "stm32f4"
|
name = "stm32f4"
|
||||||
version = "0.13.0"
|
version = "0.11.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "da3d56009c8f32e4f208dbea17df72484154d1040a8969b75d8c73eb7b18fe8f"
|
checksum = "11460b4de3a84f072e2cf6e76306c64d27f405a0e83bace0a726f555ddf4bf33"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bare-metal 0.2.5",
|
"bare-metal 0.2.5",
|
||||||
"cortex-m 0.7.4",
|
"cortex-m",
|
||||||
"cortex-m-rt 0.6.13",
|
"cortex-m-rt",
|
||||||
"vcell",
|
"vcell",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "stm32f4xx-hal"
|
name = "stm32f4xx-hal"
|
||||||
version = "0.10.1"
|
version = "0.8.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "git+https://github.com/astro/stm32f4xx-hal.git?branch=flash#9171ef176a90b1177f350fe2bc1eac625769a041"
|
||||||
checksum = "3a06fde2dd27c0ba934c9e69b62af66eb1c20dbb6d741b187a763912e9892d13"
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bare-metal 1.0.0",
|
"bare-metal 0.2.5",
|
||||||
"cast",
|
"cast",
|
||||||
"cortex-m 0.7.4",
|
"cortex-m",
|
||||||
"cortex-m-rt 0.7.1",
|
"cortex-m-rt",
|
||||||
"embedded-dma",
|
"embedded-dma",
|
||||||
"embedded-hal",
|
"embedded-hal",
|
||||||
"nb 1.0.0",
|
"nb 0.1.3",
|
||||||
"rand_core",
|
"rand_core",
|
||||||
"rtcc",
|
|
||||||
"stm32f4",
|
"stm32f4",
|
||||||
"synopsys-usb-otg",
|
"synopsys-usb-otg",
|
||||||
"void",
|
"void",
|
||||||
|
@ -542,7 +479,7 @@ version = "0.2.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "461676dcf123675b3d3b02e2390e6a690cd186aacf2f439af7673c79e2561d53"
|
checksum = "461676dcf123675b3d3b02e2390e6a690cd186aacf2f439af7673c79e2561d53"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cortex-m 0.6.7",
|
"cortex-m",
|
||||||
"usb-device",
|
"usb-device",
|
||||||
"vcell",
|
"vcell",
|
||||||
]
|
]
|
||||||
|
@ -554,16 +491,16 @@ dependencies = [
|
||||||
"bare-metal 1.0.0",
|
"bare-metal 1.0.0",
|
||||||
"bit_field",
|
"bit_field",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"cortex-m 0.6.7",
|
"cortex-m",
|
||||||
"cortex-m-log",
|
"cortex-m-log",
|
||||||
"cortex-m-rt 0.6.13",
|
"cortex-m-rt",
|
||||||
"eeprom24x",
|
"eeprom24x",
|
||||||
"heapless",
|
"heapless",
|
||||||
"log",
|
"log",
|
||||||
"nb 1.0.0",
|
"nb 1.0.0",
|
||||||
"nom",
|
"nom",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"panic-halt",
|
"panic-abort",
|
||||||
"panic-semihosting",
|
"panic-semihosting",
|
||||||
"serde",
|
"serde",
|
||||||
"serde-json-core",
|
"serde-json-core",
|
||||||
|
|
14
Cargo.toml
14
Cargo.toml
|
@ -7,23 +7,23 @@ 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 = "2021"
|
edition = "2018"
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
features = []
|
features = []
|
||||||
default-target = "thumbv7em-none-eabihf"
|
default-target = "thumbv7em-none-eabihf"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
panic-halt = "0.2"
|
panic-abort = "0.3"
|
||||||
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"
|
||||||
cortex-m = "0.6"
|
cortex-m = "0.6"
|
||||||
cortex-m-rt = { version = "0.6", features = ["device"] }
|
cortex-m-rt = { version = "0.6", features = ["device"] }
|
||||||
cortex-m-log = { version = "0.6", features = ["log-integration"] }
|
cortex-m-log = { version = "0.6", features = ["log-integration"] }
|
||||||
stm32f4xx-hal = { version = "=0.10.1", features = ["rt", "stm32f427", "usb_fs"] }
|
stm32f4xx-hal = { version = "0.8", features = ["rt", "stm32f427", "usb_fs"] }
|
||||||
stm32-eth = { rev = "3759c5c9", features = ["stm32f427", "smoltcp-phy"], git = "https://github.com/stm32-rs/stm32-eth.git" }
|
stm32-eth = { version = "0.2", features = ["stm32f427", "smoltcp-phy"], git = "https://github.com/stm32-rs/stm32-eth.git" }
|
||||||
smoltcp = { version = "0.7.5", default-features = false, features = ["proto-ipv4", "socket-tcp", "log"] }
|
smoltcp = { version = "0.6.0", default-features = false, features = ["proto-ipv4", "socket-tcp", "log"] }
|
||||||
bit_field = "0.10"
|
bit_field = "0.10"
|
||||||
byteorder = { version = "1", default-features = false }
|
byteorder = { version = "1", default-features = false }
|
||||||
nom = { version = "5", default-features = false }
|
nom = { version = "5", default-features = false }
|
||||||
|
@ -38,6 +38,10 @@ heapless = "0.5"
|
||||||
serde-json-core = "0.1"
|
serde-json-core = "0.1"
|
||||||
sfkv = "0.1"
|
sfkv = "0.1"
|
||||||
|
|
||||||
|
[patch.crates-io]
|
||||||
|
# TODO: pending https://github.com/stm32-rs/stm32f4xx-hal/pull/239
|
||||||
|
stm32f4xx-hal = { git = "https://github.com/astro/stm32f4xx-hal.git", branch = "flash" }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
semihosting = ["panic-semihosting", "cortex-m-log/semihosting"]
|
semihosting = ["panic-semihosting", "cortex-m-log/semihosting"]
|
||||||
|
|
||||||
|
|
59
README.md
59
README.md
|
@ -1,35 +1,33 @@
|
||||||
# Firmware for the Sinara 8451 Thermostat
|
# Firmware for the Sinara 8451 Thermostat
|
||||||
|
|
||||||
- [x] [Continuous Integration](https://nixbld.m-labs.hk/job/mcu/thermostat/thermostat)
|
- [x] [Continuous Integration](https://nixbld.m-labs.hk/job/mcu/mcu/thermostat)
|
||||||
- [x] Download latest firmware build: [ELF](https://nixbld.m-labs.hk/job/mcu/thermostat/thermostat/latest/download/1) [BIN](https://nixbld.m-labs.hk/job/mcu/thermostat/thermostat/latest/download/2)
|
- [x] Download latest firmware build: [ELF](https://nixbld.m-labs.hk/job/mcu/mcu/thermostat/latest/download/1) [BIN](https://nixbld.m-labs.hk/job/mcu/mcu/thermostat/latest/download/2)
|
||||||
|
|
||||||
|
|
||||||
## Building
|
## Building
|
||||||
|
|
||||||
### Reproducible build with Nix
|
### Reproducible build with Nix
|
||||||
|
|
||||||
Thermostat firmware is packaged using the [Nix](https://nixos.org) Flakes system. Install Nix 2.4+ and enable flakes by adding ``experimental-features = nix-command flakes`` to ``nix.conf`` (e.g. ``~/.config/nix/nix.conf``).
|
See the `mcu` folder of the [nix-scripts repository](https://git.m-labs.hk/M-Labs/nix-scripts).
|
||||||
|
|
||||||
Once you have Flakes enabled, you can use ``nix build`` to build the firmware.
|
|
||||||
|
|
||||||
### Development environment
|
### Development environment
|
||||||
|
|
||||||
Clone this repository and with Nix Flakes enabled, use the following commands:
|
Clone this repository and [nix-scripts](https://git.m-labs.hk/M-Labs/nix-scripts).
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
nix develop
|
nix-shell -I nix-scripts=[path to nix-scripts checkout]
|
||||||
cargo build --release
|
cargo build --release
|
||||||
```
|
```
|
||||||
|
|
||||||
The resulting ELF file will be located under `target/thumbv7em-none-eabihf/release/thermostat`.
|
The resulting ELF file will be located under `target/thumbv7em-none-eabihf/release/thermostat`
|
||||||
|
|
||||||
Alternatively, you can install the Rust toolchain without Nix using rustup; see the Rust manifest file pulled in `flake.nix` to determine which Rust version to use.
|
Alternatively, you can install the Rust toolchain without Nix using rustup; see the channel manifest file in nix-scripts (`channel-rust-nightly.toml`) to determine which Rust version to use.
|
||||||
|
|
||||||
## Debugging
|
## Debugging
|
||||||
|
|
||||||
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.cfg -f target/stm32f4x.cfg
|
openocd -f interface/stlink-v2-1.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.
|
||||||
|
@ -45,7 +43,7 @@ There are several options for flashing Thermostat. DFU requires only a micro-USB
|
||||||
|
|
||||||
### dfu-util on Linux
|
### dfu-util on Linux
|
||||||
* Install the DFU USB tool (dfu-util).
|
* Install the DFU USB tool (dfu-util).
|
||||||
* Convert firmware from ELF to BIN: `llvm-objcopy -O binary target/thumbv7em-none-eabihf/release/thermostat thermostat.bin` (you can skip this step if using the BIN from Hydra)
|
* Convert firmware from ELF to BIN: `arm-none-eabi-objcopy -O binary thermostat thermostat.bin` (you can skip this step if using the BIN from Hydra)
|
||||||
* Connect to the Micro USB connector to Thermostat below the RJ45.
|
* Connect to the Micro USB connector to Thermostat below the RJ45.
|
||||||
* Add jumper to Thermostat v2.0 across 2-pin jumper adjacent to JTAG connector.
|
* Add jumper to Thermostat v2.0 across 2-pin jumper adjacent to JTAG connector.
|
||||||
* Cycle board power to put it in DFU update mode
|
* Cycle board power to put it in DFU update mode
|
||||||
|
@ -64,7 +62,7 @@ On a Windows machine install [st.com](https://st.com) DfuSe USB device firmware
|
||||||
|
|
||||||
### OpenOCD
|
### OpenOCD
|
||||||
```shell
|
```shell
|
||||||
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg -c "program target/thumbv7em-none-eabihf/release/thermostat verify reset;exit"
|
openocd -f interface/stlink-v2-1.cfg -f target/stm32f4x.cfg -c "program target/thumbv7em-none-eabihf/release/thermostat verify reset;exit"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Network
|
## Network
|
||||||
|
@ -84,7 +82,9 @@ invalidate the first line of input.
|
||||||
|
|
||||||
### Reading ADC input
|
### Reading ADC input
|
||||||
|
|
||||||
ADC input data is provided in reports. Query for the latest report with the command `report`. See the *Reports* section below.
|
Set report mode to `on` for a continuous stream of input data.
|
||||||
|
|
||||||
|
The scope of this setting is per TCP session.
|
||||||
|
|
||||||
|
|
||||||
### TCP commands
|
### TCP commands
|
||||||
|
@ -93,8 +93,10 @@ 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 <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 |
|
||||||
|
@ -110,6 +112,8 @@ formatted as line-delimited JSON.
|
||||||
| `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 |
|
||||||
|
| `pid <0/1> integral_min <value>` | Set integral lower bound |
|
||||||
|
| `pid <0/1> integral_max <value>` | Set integral upper bound |
|
||||||
| `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 |
|
||||||
|
@ -120,12 +124,6 @@ formatted as line-delimited JSON.
|
||||||
| `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
|
||||||
|
@ -180,7 +178,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 cool down with a positive software current set point, and heat up 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 heat up with a positive software current set point, and cool down 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.
|
||||||
|
|
||||||
|
@ -247,18 +245,20 @@ pwm 0 pid
|
||||||
|
|
||||||
## Reports
|
## Reports
|
||||||
|
|
||||||
Use the bare `report` command to obtain a single report. Reports are JSON objects
|
Use the bare `report` command to obtain a single report. Enable
|
||||||
|
continuous reporting with `report mode on`. Reports are JSON objects
|
||||||
with the following keys.
|
with the following keys.
|
||||||
|
|
||||||
| Key | Unit | Description |
|
| Key | Unit | Description |
|
||||||
| --- | :---: | --- |
|
| --- | :---: | --- |
|
||||||
| `channel` | Integer | Channel `0`, or `1` |
|
| `channel` | Integer | Channel `0`, or `1` |
|
||||||
| `time` | Seconds | Temperature measurement time |
|
| `time` | Milliseconds | 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,19 +266,6 @@ 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 noisy without the hardware fix shown in [this PR][https://git.m-labs.hk/M-Labs/thermostat/pulls/105].
|
|
||||||
|
|
||||||
## 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`.
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
"0qb4s06jwgj3i9df6qq9gwcnyr3jq6dh4l5ygjghq5x1bmcqliix"
|
48
flake.lock
48
flake.lock
|
@ -1,48 +0,0 @@
|
||||||
{
|
|
||||||
"nodes": {
|
|
||||||
"nixpkgs": {
|
|
||||||
"locked": {
|
|
||||||
"lastModified": 1722791413,
|
|
||||||
"narHash": "sha256-rCTrlCWvHzMCNcKxPE3Z/mMK2gDZ+BvvpEVyRM4tKmU=",
|
|
||||||
"owner": "NixOS",
|
|
||||||
"repo": "nixpkgs",
|
|
||||||
"rev": "8b5b6723aca5a51edf075936439d9cd3947b7b2c",
|
|
||||||
"type": "github"
|
|
||||||
},
|
|
||||||
"original": {
|
|
||||||
"owner": "NixOS",
|
|
||||||
"ref": "nixos-24.05",
|
|
||||||
"repo": "nixpkgs",
|
|
||||||
"type": "github"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"root": {
|
|
||||||
"inputs": {
|
|
||||||
"nixpkgs": "nixpkgs",
|
|
||||||
"rust-overlay": "rust-overlay"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"rust-overlay": {
|
|
||||||
"inputs": {
|
|
||||||
"nixpkgs": [
|
|
||||||
"nixpkgs"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"locked": {
|
|
||||||
"lastModified": 1719281921,
|
|
||||||
"narHash": "sha256-LIBMfhM9pMOlEvBI757GOK5l0R58SRi6YpwfYMbf4yc=",
|
|
||||||
"owner": "oxalica",
|
|
||||||
"repo": "rust-overlay",
|
|
||||||
"rev": "b6032d3a404d8a52ecfc8571ff0c26dfbe221d07",
|
|
||||||
"type": "github"
|
|
||||||
},
|
|
||||||
"original": {
|
|
||||||
"owner": "oxalica",
|
|
||||||
"repo": "rust-overlay",
|
|
||||||
"type": "github"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"root": "root",
|
|
||||||
"version": 7
|
|
||||||
}
|
|
72
flake.nix
72
flake.nix
|
@ -1,72 +0,0 @@
|
||||||
{
|
|
||||||
description = "Firmware for the Sinara 8451 Thermostat";
|
|
||||||
|
|
||||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
|
|
||||||
inputs.rust-overlay = {
|
|
||||||
url = "github:oxalica/rust-overlay";
|
|
||||||
inputs.nixpkgs.follows = "nixpkgs";
|
|
||||||
};
|
|
||||||
|
|
||||||
outputs = { self, nixpkgs, rust-overlay }:
|
|
||||||
let
|
|
||||||
pkgs = import nixpkgs { system = "x86_64-linux"; overlays = [ (import rust-overlay) ]; };
|
|
||||||
|
|
||||||
rust = pkgs.rust-bin.stable."1.66.0".default.override {
|
|
||||||
extensions = [ "rust-src" ];
|
|
||||||
targets = [ "thumbv7em-none-eabihf" ];
|
|
||||||
};
|
|
||||||
rustPlatform = pkgs.makeRustPlatform {
|
|
||||||
rustc = rust;
|
|
||||||
cargo = rust;
|
|
||||||
};
|
|
||||||
|
|
||||||
thermostat = rustPlatform.buildRustPackage {
|
|
||||||
name = "thermostat";
|
|
||||||
version = "0.0.0";
|
|
||||||
|
|
||||||
src = self;
|
|
||||||
cargoLock = {
|
|
||||||
lockFile = ./Cargo.lock;
|
|
||||||
outputHashes = {
|
|
||||||
"stm32-eth-0.2.0" = "sha256-48RpZgagUqgVeKm7GXdk3Oo0v19ScF9Uby0nTFlve2o=";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
nativeBuildInputs = [ pkgs.llvm ];
|
|
||||||
|
|
||||||
buildPhase = ''
|
|
||||||
cargo build --release --bin thermostat
|
|
||||||
'';
|
|
||||||
|
|
||||||
installPhase = ''
|
|
||||||
mkdir -p $out $out/nix-support
|
|
||||||
cp target/thumbv7em-none-eabihf/release/thermostat $out/thermostat.elf
|
|
||||||
echo file binary-dist $out/thermostat.elf >> $out/nix-support/hydra-build-products
|
|
||||||
llvm-objcopy -O binary target/thumbv7em-none-eabihf/release/thermostat $out/thermostat.bin
|
|
||||||
echo file binary-dist $out/thermostat.bin >> $out/nix-support/hydra-build-products
|
|
||||||
'';
|
|
||||||
|
|
||||||
dontFixup = true;
|
|
||||||
auditable = false;
|
|
||||||
};
|
|
||||||
in {
|
|
||||||
packages.x86_64-linux = {
|
|
||||||
inherit thermostat;
|
|
||||||
default = thermostat;
|
|
||||||
};
|
|
||||||
|
|
||||||
hydraJobs = {
|
|
||||||
inherit thermostat;
|
|
||||||
};
|
|
||||||
|
|
||||||
devShells.x86_64-linux.default = pkgs.mkShellNoCC {
|
|
||||||
name = "thermostat-dev-shell";
|
|
||||||
packages = with pkgs; [
|
|
||||||
rust llvm
|
|
||||||
openocd dfu-util rlwrap
|
|
||||||
] ++ (with python3Packages; [
|
|
||||||
numpy matplotlib
|
|
||||||
]);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
|
@ -114,9 +114,9 @@ class PIDAutotune:
|
||||||
|
|
||||||
# set output
|
# set output
|
||||||
if (self._state == PIDAutotuneState.STATE_RELAY_STEP_UP):
|
if (self._state == PIDAutotuneState.STATE_RELAY_STEP_UP):
|
||||||
self._output = self._initial_output - self._outputstep
|
|
||||||
elif self._state == PIDAutotuneState.STATE_RELAY_STEP_DOWN:
|
|
||||||
self._output = self._initial_output + self._outputstep
|
self._output = self._initial_output + self._outputstep
|
||||||
|
elif self._state == PIDAutotuneState.STATE_RELAY_STEP_DOWN:
|
||||||
|
self._output = self._initial_output - self._outputstep
|
||||||
|
|
||||||
# respect output limits
|
# respect output limits
|
||||||
self._output = min(self._output, self._out_max)
|
self._output = min(self._output, self._out_max)
|
||||||
|
@ -223,7 +223,7 @@ def main():
|
||||||
# Thermostat channel
|
# Thermostat channel
|
||||||
channel = 0
|
channel = 0
|
||||||
# Target temperature of the autotune routine, celcius
|
# Target temperature of the autotune routine, celcius
|
||||||
target_temperature = 20
|
target_temperature = 30
|
||||||
# Value by which output will be increased/decreased from zero, amps
|
# Value by which output will be increased/decreased from zero, amps
|
||||||
output_step = 1
|
output_step = 1
|
||||||
# Reference period for local minima/maxima, seconds
|
# Reference period for local minima/maxima, seconds
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
import socket
|
import socket
|
||||||
import json
|
import json
|
||||||
import logging
|
|
||||||
import time
|
|
||||||
|
|
||||||
class CommandError(Exception):
|
class CommandError(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -10,14 +8,6 @@ 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
|
||||||
|
@ -77,16 +67,22 @@ class Client:
|
||||||
'ki': 0.02,
|
'ki': 0.02,
|
||||||
'kd': 0.0,
|
'kd': 0.0,
|
||||||
'output_min': 0.0,
|
'output_min': 0.0,
|
||||||
'output_max': 3.0},
|
'output_max': 3.0,
|
||||||
'target': 37.0},
|
'integral_min': -100.0,
|
||||||
|
'integral_max': 100.0},
|
||||||
|
'target': 37.0,
|
||||||
|
'integral': 38.41138597026372},
|
||||||
{'channel': 1,
|
{'channel': 1,
|
||||||
'parameters': {
|
'parameters': {
|
||||||
'kp': 10.0,
|
'kp': 10.0,
|
||||||
'ki': 0.02,
|
'ki': 0.02,
|
||||||
'kd': 0.0,
|
'kd': 0.0,
|
||||||
'output_min': 0.0,
|
'output_min': 0.0,
|
||||||
'output_max': 3.0},
|
'output_max': 3.0,
|
||||||
'target': 36.5}]
|
'integral_min': -100.0,
|
||||||
|
'integral_max': 100.0},
|
||||||
|
'target': 36.5,
|
||||||
|
'integral': nan}]
|
||||||
"""
|
"""
|
||||||
return self._get_conf("pid")
|
return self._get_conf("pid")
|
||||||
|
|
||||||
|
@ -127,8 +123,9 @@ class Client:
|
||||||
'tec_u_meas': 2.5340000000000003,
|
'tec_u_meas': 2.5340000000000003,
|
||||||
'pid_output': 2.067581958092247}
|
'pid_output': 2.067581958092247}
|
||||||
"""
|
"""
|
||||||
|
self._command("report mode", "on")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
self._socket.sendall("report\n".encode('utf-8'))
|
|
||||||
line = self._read_line()
|
line = self._read_line()
|
||||||
if not line:
|
if not line:
|
||||||
break
|
break
|
||||||
|
@ -136,7 +133,6 @@ class Client:
|
||||||
yield json.loads(line)
|
yield json.loads(line)
|
||||||
except json.decoder.JSONDecodeError:
|
except json.decoder.JSONDecodeError:
|
||||||
pass
|
pass
|
||||||
time.sleep(0.05)
|
|
||||||
|
|
||||||
def set_param(self, topic, channel, field="", value=""):
|
def set_param(self, topic, channel, field="", value=""):
|
||||||
"""Set configuration parameters
|
"""Set configuration parameters
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
{ mozillaOverlay ? builtins.fetchTarball "https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz",
|
||||||
|
latestRustNightly ? false,
|
||||||
|
}:
|
||||||
|
let
|
||||||
|
pkgs = import <nixpkgs> {
|
||||||
|
overlays = [ (import mozillaOverlay) ];
|
||||||
|
};
|
||||||
|
rust =
|
||||||
|
if latestRustNightly
|
||||||
|
then pkgs.rustChannelOfTargets "nightly" null [ "thumbv7em-none-eabihf" ]
|
||||||
|
else (pkgs.recurseIntoAttrs (
|
||||||
|
pkgs.callPackage (import <nix-scripts/mcu/rustPlatform.nix>) {}
|
||||||
|
)).rust.cargo;
|
||||||
|
in
|
||||||
|
pkgs.mkShell {
|
||||||
|
name = "thermostat-env";
|
||||||
|
buildInputs = with pkgs; [
|
||||||
|
rust gcc
|
||||||
|
openocd dfu-util
|
||||||
|
] ++ (with python3Packages; [
|
||||||
|
numpy matplotlib
|
||||||
|
]);
|
||||||
|
}
|
|
@ -24,7 +24,7 @@ pub struct Channel<C: ChannelPins> {
|
||||||
pub vref_meas: ElectricPotential,
|
pub vref_meas: ElectricPotential,
|
||||||
pub shdn: C::Shdn,
|
pub shdn: C::Shdn,
|
||||||
pub vref_pin: C::VRefPin,
|
pub vref_pin: C::VRefPin,
|
||||||
pub itec_pin: C::ITecPin,
|
pub itec_pin: C::ItecPin,
|
||||||
/// feedback from `dac` output
|
/// feedback from `dac` output
|
||||||
pub dac_feedback_pin: C::DacFeedbackPin,
|
pub dac_feedback_pin: C::DacFeedbackPin,
|
||||||
pub tec_u_meas_pin: C::TecUMeasPin,
|
pub tec_u_meas_pin: C::TecUMeasPin,
|
||||||
|
|
|
@ -2,13 +2,12 @@ use smoltcp::time::{Duration, Instant};
|
||||||
use uom::si::{
|
use uom::si::{
|
||||||
f64::{
|
f64::{
|
||||||
ElectricPotential,
|
ElectricPotential,
|
||||||
ElectricCurrent,
|
|
||||||
ElectricalResistance,
|
ElectricalResistance,
|
||||||
|
ElectricCurrent,
|
||||||
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,
|
||||||
|
@ -28,10 +27,11 @@ 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,
|
||||||
|
@ -45,9 +45,10 @@ 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(),
|
||||||
|
@ -66,10 +67,10 @@ impl ChannelState {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Update PID state on ADC input, calculate new DAC output
|
/// Update PID state on ADC input, calculate new DAC output
|
||||||
pub fn update_pid(&mut self) -> Option<f64> {
|
pub fn update_pid(&mut self, current: ElectricCurrent) -> Option<f64> {
|
||||||
let temperature = self.get_temperature()?
|
let temperature = self.get_temperature()?
|
||||||
.get::<degree_celsius>();
|
.get::<degree_celsius>();
|
||||||
let pid_output = self.pid.update(temperature);
|
let pid_output = self.pid.update(temperature, self.get_adc_interval(), current);
|
||||||
Some(pid_output)
|
Some(pid_output)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
322
src/channels.rs
322
src/channels.rs
|
@ -1,6 +1,4 @@
|
||||||
use core::marker::PhantomData;
|
use heapless::{consts::{U2, U1024}, Vec};
|
||||||
use heapless::{consts::U2, Vec};
|
|
||||||
use num_traits::Zero;
|
|
||||||
use serde::{Serialize, Serializer};
|
use serde::{Serialize, Serializer};
|
||||||
use smoltcp::time::Instant;
|
use smoltcp::time::Instant;
|
||||||
use stm32f4xx_hal::hal;
|
use stm32f4xx_hal::hal;
|
||||||
|
@ -18,44 +16,15 @@ use crate::{
|
||||||
channel::{Channel, Channel0, Channel1},
|
channel::{Channel, Channel0, Channel1},
|
||||||
channel_state::ChannelState,
|
channel_state::ChannelState,
|
||||||
command_parser::{CenterPoint, PwmPin},
|
command_parser::{CenterPoint, PwmPin},
|
||||||
command_handler::JsonBuffer,
|
pins,
|
||||||
pins::{self, Channel0VRef, Channel1VRef},
|
|
||||||
steinhart_hart,
|
steinhart_hart,
|
||||||
};
|
};
|
||||||
use crate::timer::sleep;
|
|
||||||
|
|
||||||
pub enum PinsAdcReadTarget {
|
|
||||||
VREF,
|
|
||||||
DacVfb,
|
|
||||||
ITec,
|
|
||||||
VTec,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const CHANNELS: usize = 2;
|
pub const CHANNELS: usize = 2;
|
||||||
pub const R_SENSE: f64 = 0.05;
|
pub const R_SENSE: f64 = 0.05;
|
||||||
|
|
||||||
// From design specs
|
|
||||||
pub const MAX_TEC_I: ElectricCurrent = ElectricCurrent {
|
|
||||||
dimension: PhantomData,
|
|
||||||
units: PhantomData,
|
|
||||||
value: 2.0,
|
|
||||||
};
|
|
||||||
pub const MAX_TEC_V: ElectricPotential = ElectricPotential {
|
|
||||||
dimension: PhantomData,
|
|
||||||
units: PhantomData,
|
|
||||||
value: 4.0,
|
|
||||||
};
|
|
||||||
const MAX_TEC_I_DUTY_TO_CURRENT_RATE: ElectricCurrent = ElectricCurrent {
|
|
||||||
dimension: PhantomData,
|
|
||||||
units: PhantomData,
|
|
||||||
value: 1.0 / (10.0 * R_SENSE / 3.3),
|
|
||||||
};
|
|
||||||
// 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: ElectricPotential = ElectricPotential {
|
const DAC_OUT_V_MAX: f64 = 3.0;
|
||||||
dimension: PhantomData,
|
|
||||||
units: PhantomData,
|
|
||||||
value: 3.0,
|
|
||||||
};
|
|
||||||
// TODO: -pub
|
// TODO: -pub
|
||||||
pub struct Channels {
|
pub struct Channels {
|
||||||
channel0: Channel<Channel0>,
|
channel0: Channel<Channel0>,
|
||||||
|
@ -87,6 +56,7 @@ impl Channels {
|
||||||
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 };
|
||||||
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));
|
||||||
}
|
}
|
||||||
|
@ -105,9 +75,10 @@ impl Channels {
|
||||||
pub fn poll_adc(&mut self, instant: Instant) -> Option<u8> {
|
pub fn poll_adc(&mut self, instant: Instant) -> Option<u8> {
|
||||||
self.adc.data_ready().unwrap().map(|channel| {
|
self.adc.data_ready().unwrap().map(|channel| {
|
||||||
let data = self.adc.read_data().unwrap();
|
let data = self.adc.read_data().unwrap();
|
||||||
|
let current = self.get_tec_i(channel.into());
|
||||||
let state = self.channel_state(channel);
|
let state = self.channel_state(channel);
|
||||||
state.update(instant, data);
|
state.update(instant, data);
|
||||||
match state.update_pid() {
|
match state.update_pid(current) {
|
||||||
Some(pid_output) if state.pid_engaged => {
|
Some(pid_output) if state.pid_engaged => {
|
||||||
// Forward PID output to i_set DAC
|
// Forward PID output to i_set DAC
|
||||||
self.set_i(channel.into(), ElectricCurrent::new::<ampere>(pid_output));
|
self.set_i(channel.into(), ElectricCurrent::new::<ampere>(pid_output));
|
||||||
|
@ -126,8 +97,11 @@ 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 => {
|
||||||
self.adc_read(channel, PinsAdcReadTarget::VREF, 8),
|
let vref = 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()),
|
||||||
}
|
}
|
||||||
|
@ -140,13 +114,16 @@ impl Channels {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_i(&mut self, channel: usize) -> ElectricCurrent {
|
pub fn get_i(&mut self, channel: usize) -> ElectricCurrent {
|
||||||
let i_set = self.channel_state(channel).i_set;
|
let center_point = self.get_center(channel);
|
||||||
i_set
|
let r_sense = ElectricalResistance::new::<ohm>(R_SENSE);
|
||||||
|
let voltage = self.get_dac(channel);
|
||||||
|
let i_tec = (voltage - center_point) / (10.0 * r_sense);
|
||||||
|
i_tec
|
||||||
}
|
}
|
||||||
|
|
||||||
/// i_set DAC
|
/// i_set DAC
|
||||||
fn set_dac(&mut self, channel: usize, voltage: ElectricPotential) -> ElectricPotential {
|
fn set_dac(&mut self, channel: usize, voltage: ElectricPotential) -> ElectricPotential {
|
||||||
let value = ((voltage / DAC_OUT_V_MAX).get::<ratio>() * (ad5680::MAX_VALUE as f64)) as u32 ;
|
let value = ((voltage / ElectricPotential::new::<volt>(DAC_OUT_V_MAX)).get::<ratio>() * (ad5680::MAX_VALUE as f64)) as u32 ;
|
||||||
match channel {
|
match channel {
|
||||||
0 => self.channel0.dac.set(value).unwrap(),
|
0 => self.channel0.dac.set(value).unwrap(),
|
||||||
1 => self.channel1.dac.set(value).unwrap(),
|
1 => self.channel1.dac.set(value).unwrap(),
|
||||||
|
@ -156,8 +133,7 @@ impl Channels {
|
||||||
voltage
|
voltage
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_i(&mut self, channel: usize, i_set: ElectricCurrent) -> ElectricCurrent {
|
pub fn set_i(&mut self, channel: usize, i_tec: ElectricCurrent) -> ElectricCurrent {
|
||||||
let i_set = i_set.min(MAX_TEC_I).max(-MAX_TEC_I);
|
|
||||||
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,
|
||||||
|
@ -165,112 +141,109 @@ 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_set * 10.0 * r_sense + center_point;
|
let voltage = i_tec * 10.0 * r_sense + center_point;
|
||||||
let voltage = self.set_dac(channel, voltage);
|
let voltage = self.set_dac(channel, voltage);
|
||||||
let i_set = (voltage - center_point) / (10.0 * r_sense);
|
let i_tec = (voltage - center_point) / (10.0 * r_sense);
|
||||||
self.channel_state(channel).i_set = i_set;
|
i_tec
|
||||||
i_set
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// AN4073: ADC Reading Dispersion can be reduced through Averaging
|
pub fn read_dac_feedback(&mut self, channel: usize) -> ElectricPotential {
|
||||||
pub fn adc_read(&mut self, channel: usize, adc_read_target: PinsAdcReadTarget, avg_pt: u16) -> ElectricPotential {
|
|
||||||
let mut sample: u32 = 0;
|
|
||||||
match channel {
|
match channel {
|
||||||
0 => {
|
0 => {
|
||||||
sample = match adc_read_target {
|
let sample = self.pins_adc.convert(
|
||||||
PinsAdcReadTarget::VREF => {
|
&self.channel0.dac_feedback_pin,
|
||||||
match &self.channel0.vref_pin {
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
||||||
Channel0VRef::Analog(vref_pin) => {
|
);
|
||||||
for _ in (0..avg_pt).rev() {
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
sample += self
|
|
||||||
.pins_adc
|
|
||||||
.convert(vref_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480)
|
|
||||||
as u32;
|
|
||||||
}
|
|
||||||
sample / avg_pt as u32
|
|
||||||
},
|
|
||||||
Channel0VRef::Disabled(_) => {2048 as u32}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
PinsAdcReadTarget::DacVfb => {
|
|
||||||
for _ in (0..avg_pt).rev() {
|
|
||||||
sample += self
|
|
||||||
.pins_adc
|
|
||||||
.convert(&self.channel0.dac_feedback_pin,stm32f4xx_hal::adc::config::SampleTime::Cycles_480)
|
|
||||||
as u32;
|
|
||||||
}
|
|
||||||
sample / avg_pt as u32
|
|
||||||
}
|
|
||||||
PinsAdcReadTarget::ITec => {
|
|
||||||
for _ in (0..avg_pt).rev() {
|
|
||||||
sample += self
|
|
||||||
.pins_adc
|
|
||||||
.convert(&self.channel0.itec_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480)
|
|
||||||
as u32;
|
|
||||||
}
|
|
||||||
sample / avg_pt as u32
|
|
||||||
}
|
|
||||||
PinsAdcReadTarget::VTec => {
|
|
||||||
for _ in (0..avg_pt).rev() {
|
|
||||||
sample += self
|
|
||||||
.pins_adc
|
|
||||||
.convert(&self.channel0.tec_u_meas_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480)
|
|
||||||
as u32;
|
|
||||||
}
|
|
||||||
sample / avg_pt as u32
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let mv = self.pins_adc.sample_to_millivolts(sample as u16);
|
|
||||||
ElectricPotential::new::<millivolt>(mv as f64)
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
}
|
}
|
||||||
1 => {
|
1 => {
|
||||||
sample = match adc_read_target {
|
let sample = self.pins_adc.convert(
|
||||||
PinsAdcReadTarget::VREF => {
|
&self.channel1.dac_feedback_pin,
|
||||||
match &self.channel1.vref_pin {
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
||||||
Channel1VRef::Analog(vref_pin) => {
|
);
|
||||||
for _ in (0..avg_pt).rev() {
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
sample += self
|
|
||||||
.pins_adc
|
|
||||||
.convert(vref_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480)
|
|
||||||
as u32;
|
|
||||||
}
|
|
||||||
sample / avg_pt as u32
|
|
||||||
},
|
|
||||||
Channel1VRef::Disabled(_) => {2048 as u32}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
PinsAdcReadTarget::DacVfb => {
|
|
||||||
for _ in (0..avg_pt).rev() {
|
|
||||||
sample += self
|
|
||||||
.pins_adc
|
|
||||||
.convert(&self.channel1.dac_feedback_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480)
|
|
||||||
as u32;
|
|
||||||
}
|
|
||||||
sample / avg_pt as u32
|
|
||||||
}
|
|
||||||
PinsAdcReadTarget::ITec => {
|
|
||||||
for _ in (0..avg_pt).rev() {
|
|
||||||
sample += self
|
|
||||||
.pins_adc
|
|
||||||
.convert(&self.channel1.itec_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480)
|
|
||||||
as u32;
|
|
||||||
}
|
|
||||||
sample / avg_pt as u32
|
|
||||||
}
|
|
||||||
PinsAdcReadTarget::VTec => {
|
|
||||||
for _ in (0..avg_pt).rev() {
|
|
||||||
sample += self
|
|
||||||
.pins_adc
|
|
||||||
.convert(&self.channel1.tec_u_meas_pin, stm32f4xx_hal::adc::config::SampleTime::Cycles_480)
|
|
||||||
as u32;
|
|
||||||
}
|
|
||||||
sample / avg_pt as u32
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let mv = self.pins_adc.sample_to_millivolts(sample as u16);
|
|
||||||
ElectricPotential::new::<millivolt>(mv as f64)
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
}
|
}
|
||||||
_ => unreachable!()
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_dac_feedback_until_stable(&mut self, channel: usize, tolerance: ElectricPotential) -> ElectricPotential {
|
||||||
|
let mut prev = self.read_dac_feedback(channel);
|
||||||
|
loop {
|
||||||
|
let current = self.read_dac_feedback(channel);
|
||||||
|
if (current - prev).abs() < tolerance {
|
||||||
|
return current;
|
||||||
|
}
|
||||||
|
prev = current;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_itec(&mut self, channel: usize) -> ElectricPotential {
|
||||||
|
match channel {
|
||||||
|
0 => {
|
||||||
|
let sample = self.pins_adc.convert(
|
||||||
|
&self.channel0.itec_pin,
|
||||||
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
||||||
|
);
|
||||||
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
|
}
|
||||||
|
1 => {
|
||||||
|
let sample = self.pins_adc.convert(
|
||||||
|
&self.channel1.itec_pin,
|
||||||
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
||||||
|
);
|
||||||
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// should be 1.5V
|
||||||
|
pub fn read_vref(&mut self, channel: usize) -> ElectricPotential {
|
||||||
|
match channel {
|
||||||
|
0 => {
|
||||||
|
let sample = self.pins_adc.convert(
|
||||||
|
&self.channel0.vref_pin,
|
||||||
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
||||||
|
);
|
||||||
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
|
}
|
||||||
|
1 => {
|
||||||
|
let sample = self.pins_adc.convert(
|
||||||
|
&self.channel1.vref_pin,
|
||||||
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
||||||
|
);
|
||||||
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_tec_u_meas(&mut self, channel: usize) -> ElectricPotential {
|
||||||
|
match channel {
|
||||||
|
0 => {
|
||||||
|
let sample = self.pins_adc.convert(
|
||||||
|
&self.channel0.tec_u_meas_pin,
|
||||||
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
||||||
|
);
|
||||||
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
|
}
|
||||||
|
1 => {
|
||||||
|
let sample = self.pins_adc.convert(
|
||||||
|
&self.channel1.tec_u_meas_pin,
|
||||||
|
stm32f4xx_hal::adc::config::SampleTime::Cycles_480
|
||||||
|
);
|
||||||
|
let mv = self.pins_adc.sample_to_millivolts(sample);
|
||||||
|
ElectricPotential::new::<millivolt>(mv as f64)
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -300,7 +273,8 @@ impl Channels {
|
||||||
let mut start_value = 1;
|
let mut start_value = 1;
|
||||||
let mut best_error = ElectricPotential::new::<volt>(100.0);
|
let mut best_error = ElectricPotential::new::<volt>(100.0);
|
||||||
|
|
||||||
for step in (5..18).rev() {
|
for step in (0..18).rev() {
|
||||||
|
let mut prev_value = start_value;
|
||||||
for value in (start_value..=ad5680::MAX_VALUE).step_by(1 << step) {
|
for value in (start_value..=ad5680::MAX_VALUE).step_by(1 << step) {
|
||||||
match channel {
|
match channel {
|
||||||
0 => {
|
0 => {
|
||||||
|
@ -311,23 +285,24 @@ impl Channels {
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
sleep(10);
|
|
||||||
|
|
||||||
let dac_feedback = self.adc_read(channel, PinsAdcReadTarget::DacVfb, 64);
|
let dac_feedback = self.read_dac_feedback_until_stable(channel, ElectricPotential::new::<volt>(0.001));
|
||||||
let error = target_voltage - dac_feedback;
|
let error = target_voltage - dac_feedback;
|
||||||
if error < ElectricPotential::new::<volt>(0.0) {
|
if error < ElectricPotential::new::<volt>(0.0) {
|
||||||
break;
|
break;
|
||||||
} else if error < best_error {
|
} else if error < best_error {
|
||||||
best_error = error;
|
best_error = error;
|
||||||
start_value = value;
|
start_value = prev_value;
|
||||||
|
|
||||||
let vref = (value as f64 / ad5680::MAX_VALUE as f64) * DAC_OUT_V_MAX;
|
let vref = (value as f64 / ad5680::MAX_VALUE as f64) * ElectricPotential::new::<volt>(DAC_OUT_V_MAX);
|
||||||
match channel {
|
match channel {
|
||||||
0 => self.channel0.vref_meas = vref,
|
0 => self.channel0.vref_meas = vref,
|
||||||
1 => self.channel1.vref_meas = vref,
|
1 => self.channel1.vref_meas = vref,
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
prev_value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -379,30 +354,32 @@ impl Channels {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_max_v(&mut self, channel: usize) -> (ElectricPotential, ElectricPotential) {
|
pub fn get_max_v(&mut self, channel: usize) -> ElectricPotential {
|
||||||
let max = 4.0 * ElectricPotential::new::<volt>(3.3);
|
let max = 4.0 * ElectricPotential::new::<volt>(3.3);
|
||||||
let duty = self.get_pwm(channel, PwmPin::MaxV);
|
let duty = self.get_pwm(channel, PwmPin::MaxV);
|
||||||
(duty * max, MAX_TEC_V)
|
duty * max
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_max_i_pos(&mut self, channel: usize) -> (ElectricCurrent, ElectricCurrent) {
|
pub fn get_max_i_pos(&mut self, channel: usize) -> (ElectricCurrent, ElectricCurrent) {
|
||||||
|
let max = ElectricCurrent::new::<ampere>(3.0);
|
||||||
let duty = self.get_pwm(channel, PwmPin::MaxIPos);
|
let duty = self.get_pwm(channel, PwmPin::MaxIPos);
|
||||||
(duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, MAX_TEC_I)
|
(duty * max, max)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_max_i_neg(&mut self, channel: usize) -> (ElectricCurrent, ElectricCurrent) {
|
pub fn get_max_i_neg(&mut self, channel: usize) -> (ElectricCurrent, ElectricCurrent) {
|
||||||
|
let max = ElectricCurrent::new::<ampere>(3.0);
|
||||||
let duty = self.get_pwm(channel, PwmPin::MaxINeg);
|
let duty = self.get_pwm(channel, PwmPin::MaxINeg);
|
||||||
(duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, MAX_TEC_I)
|
(duty * max, max)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get current passing through TEC
|
// Get current passing through TEC
|
||||||
pub fn get_tec_i(&mut self, channel: usize) -> ElectricCurrent {
|
pub fn get_tec_i(&mut self, channel: usize) -> ElectricCurrent {
|
||||||
(self.adc_read(channel, PinsAdcReadTarget::ITec, 16) - self.adc_read(channel, PinsAdcReadTarget::VREF, 16)) / ElectricalResistance::new::<ohm>(0.4)
|
(self.read_itec(channel) - self.read_vref(channel)) / ElectricalResistance::new::<ohm>(0.4)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get voltage across TEC
|
// Get voltage across TEC
|
||||||
pub fn get_tec_v(&mut self, channel: usize) -> ElectricPotential {
|
pub fn get_tec_v(&mut self, channel: usize) -> ElectricPotential {
|
||||||
(self.adc_read(channel, PinsAdcReadTarget::VTec, 16) - ElectricPotential::new::<volt>(1.5)) * 4.0
|
(self.read_tec_u_meas(channel) - ElectricPotential::new::<volt>(1.5)) * 4.0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_pwm(&mut self, channel: usize, pin: PwmPin, duty: f64) -> f64 {
|
fn set_pwm(&mut self, channel: usize, pin: PwmPin, duty: f64) -> f64 {
|
||||||
|
@ -434,32 +411,35 @@ impl Channels {
|
||||||
|
|
||||||
pub fn set_max_v(&mut self, channel: usize, max_v: ElectricPotential) -> (ElectricPotential, ElectricPotential) {
|
pub fn set_max_v(&mut self, channel: usize, max_v: ElectricPotential) -> (ElectricPotential, ElectricPotential) {
|
||||||
let max = 4.0 * ElectricPotential::new::<volt>(3.3);
|
let max = 4.0 * ElectricPotential::new::<volt>(3.3);
|
||||||
let duty = (max_v.min(MAX_TEC_V).max(ElectricPotential::zero()) / max).get::<ratio>();
|
let duty = (max_v / max).get::<ratio>();
|
||||||
let duty = self.set_pwm(channel, PwmPin::MaxV, duty);
|
let duty = self.set_pwm(channel, PwmPin::MaxV, duty);
|
||||||
(duty * max, max)
|
(duty * max, max)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_max_i_pos(&mut self, channel: usize, max_i_pos: ElectricCurrent) -> (ElectricCurrent, ElectricCurrent) {
|
pub fn set_max_i_pos(&mut self, channel: usize, max_i_pos: ElectricCurrent) -> (ElectricCurrent, ElectricCurrent) {
|
||||||
let max = ElectricCurrent::new::<ampere>(3.0);
|
let max = ElectricCurrent::new::<ampere>(3.0);
|
||||||
let duty = (max_i_pos.min(MAX_TEC_I).max(ElectricCurrent::zero()) / MAX_TEC_I_DUTY_TO_CURRENT_RATE).get::<ratio>();
|
let duty = (max_i_pos / max).get::<ratio>();
|
||||||
let duty = self.set_pwm(channel, PwmPin::MaxIPos, duty);
|
let duty = self.set_pwm(channel, PwmPin::MaxIPos, duty);
|
||||||
(duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, max)
|
(duty * max, max)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_max_i_neg(&mut self, channel: usize, max_i_neg: ElectricCurrent) -> (ElectricCurrent, ElectricCurrent) {
|
pub fn set_max_i_neg(&mut self, channel: usize, max_i_neg: ElectricCurrent) -> (ElectricCurrent, ElectricCurrent) {
|
||||||
let max = ElectricCurrent::new::<ampere>(3.0);
|
let max = ElectricCurrent::new::<ampere>(3.0);
|
||||||
let duty = (max_i_neg.min(MAX_TEC_I).max(ElectricCurrent::zero()) / MAX_TEC_I_DUTY_TO_CURRENT_RATE).get::<ratio>();
|
let duty = (max_i_neg / max).get::<ratio>();
|
||||||
let duty = self.set_pwm(channel, PwmPin::MaxINeg, duty);
|
let duty = self.set_pwm(channel, PwmPin::MaxINeg, duty);
|
||||||
(duty * MAX_TEC_I_DUTY_TO_CURRENT_RATE, max)
|
(duty * max, max)
|
||||||
}
|
}
|
||||||
|
|
||||||
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.adc_read(channel, PinsAdcReadTarget::ITec, 16);
|
let i_tec = self.read_itec(channel);
|
||||||
let tec_i = self.get_tec_i(channel);
|
let tec_i = self.get_tec_i(channel);
|
||||||
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 = state.pid.last_output.map(|last_output|
|
||||||
|
ElectricCurrent::new::<ampere>(last_output)
|
||||||
|
);
|
||||||
Report {
|
Report {
|
||||||
channel,
|
channel,
|
||||||
time: state.get_adc_time(),
|
time: state.get_adc_time(),
|
||||||
|
@ -470,8 +450,9 @@ 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.adc_read(channel, PinsAdcReadTarget::DacVfb, 1),
|
dac_feedback: self.read_dac_feedback(channel),
|
||||||
i_tec,
|
i_tec,
|
||||||
tec_i,
|
tec_i,
|
||||||
tec_u_meas: self.get_tec_v(channel),
|
tec_u_meas: self.get_tec_v(channel),
|
||||||
|
@ -495,21 +476,12 @@ 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,
|
||||||
center: CenterPointJson(self.channel_state(channel).center.clone()),
|
center: CenterPointJson(self.channel_state(channel).center.clone()),
|
||||||
i_set: (self.get_i(channel), MAX_TEC_I).into(),
|
i_set: (self.get_i(channel), ElectricCurrent::new::<ampere>(3.0)).into(),
|
||||||
max_v: self.get_max_v(channel).into(),
|
max_v: (self.get_max_v(channel), ElectricPotential::new::<volt>(5.0)).into(),
|
||||||
max_i_pos: self.get_max_i_pos(channel).into(),
|
max_i_pos: self.get_max_i_pos(channel).into(),
|
||||||
max_i_neg: self.get_max_i_neg(channel).into(),
|
max_i_neg: self.get_max_i_neg(channel).into(),
|
||||||
}
|
}
|
||||||
|
@ -549,15 +521,10 @@ impl Channels {
|
||||||
}
|
}
|
||||||
serde_json_core::to_vec(&summaries)
|
serde_json_core::to_vec(&summaries)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn current_abs_max_tec_i(&mut self) -> ElectricCurrent {
|
|
||||||
(0..CHANNELS)
|
|
||||||
.map(|channel| self.get_tec_i(channel).abs())
|
|
||||||
.max_by(|a, b| a.partial_cmp(b).unwrap_or(core::cmp::Ordering::Equal))
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type JsonBuffer = Vec<u8, U1024>;
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct Report {
|
pub struct Report {
|
||||||
channel: usize,
|
channel: usize,
|
||||||
|
@ -568,12 +535,13 @@ 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: ElectricPotential,
|
||||||
tec_i: ElectricCurrent,
|
tec_i: ElectricCurrent,
|
||||||
tec_u_meas: ElectricPotential,
|
tec_u_meas: ElectricPotential,
|
||||||
pid_output: ElectricCurrent,
|
pid_output: Option<ElectricCurrent>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CenterPointJson(CenterPoint);
|
pub struct CenterPointJson(CenterPoint);
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
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::{
|
||||||
|
@ -13,6 +12,7 @@ use super::{
|
||||||
PwmPin,
|
PwmPin,
|
||||||
ShParameter
|
ShParameter
|
||||||
},
|
},
|
||||||
|
leds::Leds,
|
||||||
ad7172,
|
ad7172,
|
||||||
CHANNEL_CONFIG_KEY,
|
CHANNEL_CONFIG_KEY,
|
||||||
channels::{
|
channels::{
|
||||||
|
@ -22,8 +22,7 @@ use super::{
|
||||||
config::ChannelConfig,
|
config::ChannelConfig,
|
||||||
dfu,
|
dfu,
|
||||||
flash_store::FlashStore,
|
flash_store::FlashStore,
|
||||||
FanCtrl,
|
session::Session
|
||||||
hw_rev::HWRev,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use uom::{
|
use uom::{
|
||||||
|
@ -56,8 +55,6 @@ 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 {
|
||||||
|
@ -86,6 +83,16 @@ fn send_line(socket: &mut TcpSocket, data: &[u8]) -> bool {
|
||||||
|
|
||||||
impl Handler {
|
impl Handler {
|
||||||
|
|
||||||
|
fn reporting(socket: &mut TcpSocket) -> Result<Handler, Error> {
|
||||||
|
send_line(socket, b"{}");
|
||||||
|
Ok(Handler::Handled)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn show_report_mode(socket: &mut TcpSocket, session: &Session) -> Result<Handler, Error> {
|
||||||
|
let _ = writeln!(socket, "{{ \"report\": {:?} }}", session.reporting());
|
||||||
|
Ok(Handler::Handled)
|
||||||
|
}
|
||||||
|
|
||||||
fn show_report(socket: &mut TcpSocket, channels: &mut Channels) -> Result<Handler, Error> {
|
fn show_report(socket: &mut TcpSocket, channels: &mut Channels) -> Result<Handler, Error> {
|
||||||
match channels.reports_json() {
|
match channels.reports_json() {
|
||||||
Ok(buf) => {
|
Ok(buf) => {
|
||||||
|
@ -164,16 +171,18 @@ impl Handler {
|
||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn engage_pid (socket: &mut TcpSocket, channels: &mut Channels, channel: usize) -> Result<Handler, Error> {
|
fn engage_pid (socket: &mut TcpSocket, channels: &mut Channels, leds: &mut Leds, 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, channel: usize, pin: PwmPin, value: f64) -> Result<Handler, Error> {
|
fn set_pwm (socket: &mut TcpSocket, channels: &mut Channels, leds: &mut Leds, 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);
|
||||||
|
@ -196,11 +205,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_set = channels.get_i(channel);
|
let i_tec = 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_set);
|
channels.set_i(channel, i_tec);
|
||||||
}
|
}
|
||||||
send_line(socket, b"{}");
|
send_line(socket, b"{}");
|
||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
|
@ -222,6 +231,10 @@ impl Handler {
|
||||||
pid.parameters.output_min = value as f32,
|
pid.parameters.output_min = value as f32,
|
||||||
OutputMax =>
|
OutputMax =>
|
||||||
pid.parameters.output_max = value as f32,
|
pid.parameters.output_max = value as f32,
|
||||||
|
IntegralMin =>
|
||||||
|
pid.parameters.integral_min = value as f32,
|
||||||
|
IntegralMax =>
|
||||||
|
pid.parameters.integral_max = value as f32,
|
||||||
}
|
}
|
||||||
send_line(socket, b"{}");
|
send_line(socket, b"{}");
|
||||||
Ok(Handler::Handled)
|
Ok(Handler::Handled)
|
||||||
|
@ -332,86 +345,19 @@ impl Handler {
|
||||||
Ok(Handler::Reset)
|
Ok(Handler::Reset)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_fan(socket: &mut TcpSocket, fan_pwm: u32, fan_ctrl: &mut FanCtrl) -> Result<Handler, Error> {
|
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> {
|
||||||
if !fan_ctrl.fan_available() {
|
|
||||||
send_line(socket, b"{ \"warning\": \"this thermostat doesn't have a 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 a 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, 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::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, channel),
|
Command::PwmPid { channel } => Handler::engage_pid(socket, channels, leds, channel),
|
||||||
Command::Pwm { channel, pin, value } => Handler::set_pwm(socket, channels, channel, pin, value),
|
Command::Pwm { channel, pin, value } => Handler::set_pwm(socket, channels, leds, 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),
|
||||||
|
@ -421,13 +367,7 @@ 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,7 +10,6 @@ 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};
|
||||||
|
@ -96,6 +95,7 @@ pub struct Ipv4Config {
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum ShowCommand {
|
pub enum ShowCommand {
|
||||||
Input,
|
Input,
|
||||||
|
Reporting,
|
||||||
Pwm,
|
Pwm,
|
||||||
Pid,
|
Pid,
|
||||||
SteinhartHart,
|
SteinhartHart,
|
||||||
|
@ -111,6 +111,8 @@ pub enum PidParameter {
|
||||||
KD,
|
KD,
|
||||||
OutputMin,
|
OutputMin,
|
||||||
OutputMax,
|
OutputMax,
|
||||||
|
IntegralMin,
|
||||||
|
IntegralMax,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Steinhart-Hart equation parameter
|
/// Steinhart-Hart equation parameter
|
||||||
|
@ -147,6 +149,7 @@ pub enum Command {
|
||||||
Reset,
|
Reset,
|
||||||
Ipv4(Ipv4Config),
|
Ipv4(Ipv4Config),
|
||||||
Show(ShowCommand),
|
Show(ShowCommand),
|
||||||
|
Reporting(bool),
|
||||||
/// PWM parameter setting
|
/// PWM parameter setting
|
||||||
Pwm {
|
Pwm {
|
||||||
channel: usize,
|
channel: usize,
|
||||||
|
@ -177,18 +180,6 @@ 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], ()> {
|
||||||
|
@ -231,6 +222,12 @@ fn float(input: &[u8]) -> IResult<&[u8], Result<f64, Error>> {
|
||||||
Ok((input, result))
|
Ok((input, result))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn off_on(input: &[u8]) -> IResult<&[u8], bool> {
|
||||||
|
alt((value(false, tag("off")),
|
||||||
|
value(true, tag("on"))
|
||||||
|
))(input)
|
||||||
|
}
|
||||||
|
|
||||||
fn channel(input: &[u8]) -> IResult<&[u8], usize> {
|
fn channel(input: &[u8]) -> IResult<&[u8], usize> {
|
||||||
map(one_of("01"), |c| (c as usize) - ('0' as usize))(input)
|
map(one_of("01"), |c| (c as usize) - ('0' as usize))(input)
|
||||||
}
|
}
|
||||||
|
@ -238,8 +235,24 @@ fn channel(input: &[u8]) -> IResult<&[u8], usize> {
|
||||||
fn report(input: &[u8]) -> IResult<&[u8], Command> {
|
fn report(input: &[u8]) -> IResult<&[u8], Command> {
|
||||||
preceded(
|
preceded(
|
||||||
tag("report"),
|
tag("report"),
|
||||||
|
alt((
|
||||||
|
preceded(
|
||||||
|
whitespace,
|
||||||
|
preceded(
|
||||||
|
tag("mode"),
|
||||||
|
alt((
|
||||||
|
preceded(
|
||||||
|
whitespace,
|
||||||
|
// `report mode <on | off>` - Switch repoting mode
|
||||||
|
map(off_on, Command::Reporting)
|
||||||
|
),
|
||||||
|
// `report mode` - Show current reporting state
|
||||||
|
value(Command::Show(ShowCommand::Reporting), end)
|
||||||
|
))
|
||||||
|
)),
|
||||||
// `report` - Report once
|
// `report` - Report once
|
||||||
value(Command::Show(ShowCommand::Input), end)
|
value(Command::Show(ShowCommand::Input), end)
|
||||||
|
))
|
||||||
)(input)
|
)(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -356,6 +369,8 @@ fn pid_parameter(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
|
||||||
value(PidParameter::KD, tag("kd")),
|
value(PidParameter::KD, tag("kd")),
|
||||||
value(PidParameter::OutputMin, tag("output_min")),
|
value(PidParameter::OutputMin, tag("output_min")),
|
||||||
value(PidParameter::OutputMax, tag("output_max")),
|
value(PidParameter::OutputMax, tag("output_max")),
|
||||||
|
value(PidParameter::IntegralMin, tag("integral_min")),
|
||||||
|
value(PidParameter::IntegralMax, tag("integral_max"))
|
||||||
))(input)?;
|
))(input)?;
|
||||||
let (input, _) = whitespace(input)?;
|
let (input, _) = whitespace(input)?;
|
||||||
let (input, value) = float(input)?;
|
let (input, value) = float(input)?;
|
||||||
|
@ -509,57 +524,6 @@ 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,
|
||||||
|
@ -573,9 +537,6 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -658,6 +619,24 @@ mod test {
|
||||||
assert_eq!(command, Ok(Command::Show(ShowCommand::Input)));
|
assert_eq!(command, Ok(Command::Show(ShowCommand::Input)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_report_mode() {
|
||||||
|
let command = Command::parse(b"report mode");
|
||||||
|
assert_eq!(command, Ok(Command::Show(ShowCommand::Reporting)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_report_mode_on() {
|
||||||
|
let command = Command::parse(b"report mode on");
|
||||||
|
assert_eq!(command, Ok(Command::Reporting(true)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_report_mode_off() {
|
||||||
|
let command = Command::parse(b"report mode off");
|
||||||
|
assert_eq!(command, Ok(Command::Reporting(false)));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_pwm_i_set() {
|
fn parse_pwm_i_set() {
|
||||||
let command = Command::parse(b"pwm 1 i_set 16383");
|
let command = Command::parse(b"pwm 1 i_set 16383");
|
||||||
|
@ -722,6 +701,16 @@ mod test {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_pid_integral_max() {
|
||||||
|
let command = Command::parse(b"pid 1 integral_max 2000");
|
||||||
|
assert_eq!(command, Ok(Command::Pid {
|
||||||
|
channel: 1,
|
||||||
|
parameter: PidParameter::IntegralMax,
|
||||||
|
value: 2000.0,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_steinhart_hart() {
|
fn parse_steinhart_hart() {
|
||||||
let command = Command::parse(b"s-h");
|
let command = Command::parse(b"s-h");
|
||||||
|
@ -779,44 +768,4 @@ 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,4 +1,3 @@
|
||||||
use num_traits::Zero;
|
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use uom::si::{
|
use uom::si::{
|
||||||
electric_potential::volt,
|
electric_potential::volt,
|
||||||
|
@ -19,7 +18,6 @@ pub struct ChannelConfig {
|
||||||
pid: pid::Parameters,
|
pid: pid::Parameters,
|
||||||
pid_target: f32,
|
pid_target: f32,
|
||||||
pid_engaged: bool,
|
pid_engaged: bool,
|
||||||
i_set: ElectricCurrent,
|
|
||||||
sh: steinhart_hart::Parameters,
|
sh: steinhart_hart::Parameters,
|
||||||
pwm: PwmLimits,
|
pwm: PwmLimits,
|
||||||
/// uses variant `PostFilter::Invalid` instead of `None` to save space
|
/// uses variant `PostFilter::Invalid` instead of `None` to save space
|
||||||
|
@ -35,17 +33,11 @@ impl ChannelConfig {
|
||||||
.unwrap_or(PostFilter::Invalid);
|
.unwrap_or(PostFilter::Invalid);
|
||||||
|
|
||||||
let state = channels.channel_state(channel);
|
let state = channels.channel_state(channel);
|
||||||
let i_set = if state.pid_engaged {
|
|
||||||
ElectricCurrent::zero()
|
|
||||||
} else {
|
|
||||||
state.i_set
|
|
||||||
};
|
|
||||||
ChannelConfig {
|
ChannelConfig {
|
||||||
center: state.center.clone(),
|
center: state.center.clone(),
|
||||||
pid: state.pid.parameters.clone(),
|
pid: state.pid.parameters.clone(),
|
||||||
pid_target: state.pid.target as f32,
|
pid_target: state.pid.target as f32,
|
||||||
pid_engaged: state.pid_engaged,
|
pid_engaged: state.pid_engaged,
|
||||||
i_set: i_set,
|
|
||||||
sh: state.sh.clone(),
|
sh: state.sh.clone(),
|
||||||
pwm,
|
pwm,
|
||||||
adc_postfilter,
|
adc_postfilter,
|
||||||
|
@ -67,7 +59,6 @@ impl ChannelConfig {
|
||||||
adc_postfilter => Some(adc_postfilter),
|
adc_postfilter => Some(adc_postfilter),
|
||||||
};
|
};
|
||||||
let _ = channels.adc.set_postfilter(channel as u8, adc_postfilter);
|
let _ = channels.adc.set_postfilter(channel as u8, adc_postfilter);
|
||||||
let _ = channels.set_i(channel, self.i_set);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +71,7 @@ struct PwmLimits {
|
||||||
|
|
||||||
impl PwmLimits {
|
impl PwmLimits {
|
||||||
pub fn new(channels: &mut Channels, channel: usize) -> Self {
|
pub fn new(channels: &mut Channels, channel: usize) -> Self {
|
||||||
let (max_v, _) = channels.get_max_v(channel);
|
let max_v = channels.get_max_v(channel);
|
||||||
let (max_i_pos, _) = channels.get_max_i_pos(channel);
|
let (max_i_pos, _) = channels.get_max_i_pos(channel);
|
||||||
let (max_i_neg, _) = channels.get_max_i_neg(channel);
|
let (max_i_neg, _) = channels.get_max_i_neg(channel);
|
||||||
PwmLimits {
|
PwmLimits {
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
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
152
src/fan_ctrl.rs
|
@ -1,152 +0,0 @@
|
||||||
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.get::<ampere>() 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,
|
|
||||||
}
|
|
|
@ -41,7 +41,7 @@ impl StoreBackend for FlashBackend {
|
||||||
|
|
||||||
fn program(&mut self, offset: usize, payload: &[u8]) -> Result<(), Self::Error> {
|
fn program(&mut self, offset: usize, payload: &[u8]) -> Result<(), Self::Error> {
|
||||||
self.flash.unlocked()
|
self.flash.unlocked()
|
||||||
.program(get_offset() + offset, payload.iter())
|
.program(get_offset() + offset, payload.iter().cloned())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn backup_space(&self) -> &'static mut [u8] {
|
fn backup_space(&self) -> &'static mut [u8] {
|
||||||
|
|
|
@ -1,82 +0,0 @@
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
40
src/main.rs
40
src/main.rs
|
@ -1,14 +1,16 @@
|
||||||
#![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, maybe_uninit_ref, 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_halt as _;
|
use panic_abort 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::{
|
||||||
|
@ -52,9 +54,6 @@ 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"))]
|
||||||
|
@ -119,8 +118,8 @@ fn main() -> ! {
|
||||||
|
|
||||||
timer::setup(cp.SYST, clocks);
|
timer::setup(cp.SYST, clocks);
|
||||||
|
|
||||||
let (pins, mut leds, mut eeprom, eth_pins, usb, fan, hwrev, hw_settings) = Pins::setup(
|
let (pins, mut leds, mut eeprom, eth_pins, usb) = Pins::setup(
|
||||||
clocks, dp.TIM1, dp.TIM3, dp.TIM8,
|
clocks, dp.TIM1, dp.TIM3,
|
||||||
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,
|
||||||
|
@ -138,6 +137,7 @@ fn main() -> ! {
|
||||||
|
|
||||||
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);
|
||||||
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]) {
|
||||||
|
@ -150,8 +150,6 @@ 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],
|
||||||
|
@ -180,14 +178,9 @@ fn main() -> ! {
|
||||||
loop {
|
loop {
|
||||||
let mut new_ipv4_config = None;
|
let mut new_ipv4_config = None;
|
||||||
let instant = Instant::from_millis(i64::from(timer::now()));
|
let instant = Instant::from_millis(i64::from(timer::now()));
|
||||||
channels.poll_adc(instant);
|
let updated_channel = channels.poll_adc(instant);
|
||||||
|
if let Some(channel) = updated_channel {
|
||||||
fan_ctrl.cycle(channels.current_abs_max_tec_i());
|
server.for_each(|_, session| session.set_report_pending(channel.into()));
|
||||||
|
|
||||||
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()));
|
||||||
|
@ -213,7 +206,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, &mut store, &mut ipv4_config, &mut fan_ctrl, hwrev) {
|
match Handler::handle_command(command, &mut socket, &mut channels, session, &mut leds, &mut store, &mut ipv4_config) {
|
||||||
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(),
|
||||||
|
@ -228,6 +221,19 @@ fn main() -> ! {
|
||||||
Err(_) =>
|
Err(_) =>
|
||||||
socket.close(),
|
socket.close(),
|
||||||
}
|
}
|
||||||
|
} else if socket.can_send() {
|
||||||
|
if let Some(channel) = session.is_report_pending() {
|
||||||
|
match channels.reports_json() {
|
||||||
|
Ok(buf) => {
|
||||||
|
send_line(&mut socket, &buf[..]);
|
||||||
|
session.mark_report_sent(channel);
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("unable to serialize report: {:?}", e);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -5,14 +5,14 @@ use core::cell::RefCell;
|
||||||
use cortex_m::interrupt::{CriticalSection, Mutex};
|
use cortex_m::interrupt::{CriticalSection, Mutex};
|
||||||
use stm32f4xx_hal::{
|
use stm32f4xx_hal::{
|
||||||
rcc::Clocks,
|
rcc::Clocks,
|
||||||
pac::{interrupt, Peripherals, ETHERNET_MAC, ETHERNET_DMA},
|
stm32::{interrupt, Peripherals, ETHERNET_MAC, ETHERNET_DMA},
|
||||||
};
|
};
|
||||||
use smoltcp::wire::{EthernetAddress, Ipv4Address, Ipv4Cidr};
|
use smoltcp::wire::{EthernetAddress, Ipv4Address, Ipv4Cidr};
|
||||||
use smoltcp::iface::{
|
use smoltcp::iface::{
|
||||||
EthernetInterfaceBuilder, EthernetInterface,
|
EthernetInterfaceBuilder, EthernetInterface,
|
||||||
NeighborCache, Routes,
|
NeighborCache, Routes,
|
||||||
};
|
};
|
||||||
use stm32_eth::{Eth, RingEntry, RxDescriptor, TxDescriptor};
|
use stm32_eth::{Eth, RingEntry, PhyAddress, RxDescriptor, TxDescriptor};
|
||||||
use crate::command_parser::Ipv4Config;
|
use crate::command_parser::Ipv4Config;
|
||||||
use crate::pins::EthernetPins;
|
use crate::pins::EthernetPins;
|
||||||
|
|
||||||
|
@ -48,6 +48,7 @@ pub fn run<F>(
|
||||||
let mut eth_dev = Eth::new(
|
let mut eth_dev = Eth::new(
|
||||||
ethernet_mac, ethernet_dma,
|
ethernet_mac, ethernet_dma,
|
||||||
&mut rx_ring[..], &mut tx_ring[..],
|
&mut rx_ring[..], &mut tx_ring[..],
|
||||||
|
PhyAddress::_0,
|
||||||
clocks,
|
clocks,
|
||||||
eth_pins,
|
eth_pins,
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
101
src/pid.rs
101
src/pid.rs
|
@ -1,4 +1,12 @@
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
use uom::si::{
|
||||||
|
f64::{Time, ElectricCurrent},
|
||||||
|
time::second,
|
||||||
|
electric_current::ampere,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Allowable current error for integral accumulation
|
||||||
|
const CURRENT_ERROR_MAX: f64 = 0.1;
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct Parameters {
|
pub struct Parameters {
|
||||||
|
@ -12,6 +20,10 @@ pub struct Parameters {
|
||||||
pub output_min: f32,
|
pub output_min: f32,
|
||||||
/// Output limit maximum
|
/// Output limit maximum
|
||||||
pub output_max: f32,
|
pub output_max: f32,
|
||||||
|
/// Integral clipping minimum
|
||||||
|
pub integral_min: f32,
|
||||||
|
/// Integral clipping maximum
|
||||||
|
pub integral_max: f32
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Parameters {
|
impl Default for Parameters {
|
||||||
|
@ -22,6 +34,8 @@ impl Default for Parameters {
|
||||||
kd: 0.0,
|
kd: 0.0,
|
||||||
output_min: -2.0,
|
output_min: -2.0,
|
||||||
output_max: 2.0,
|
output_max: 2.0,
|
||||||
|
integral_min: -10.0,
|
||||||
|
integral_max: 10.0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -29,48 +43,69 @@ impl Default for Parameters {
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Controller {
|
pub struct Controller {
|
||||||
pub parameters: Parameters,
|
pub parameters: Parameters,
|
||||||
pub target : f64,
|
pub target: f64,
|
||||||
u1 : f64,
|
integral: f64,
|
||||||
x1 : f64,
|
last_input: Option<f64>,
|
||||||
x2 : f64,
|
pub last_output: Option<f64>,
|
||||||
pub y1 : f64,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Controller {
|
impl Controller {
|
||||||
pub const fn new(parameters: Parameters) -> Controller {
|
pub const fn new(parameters: Parameters) -> Controller {
|
||||||
Controller {
|
Controller {
|
||||||
parameters: parameters,
|
parameters: parameters,
|
||||||
target : 0.0,
|
target: 0.0,
|
||||||
u1 : 0.0,
|
last_input: None,
|
||||||
x1 : 0.0,
|
integral: 0.0,
|
||||||
x2 : 0.0,
|
last_output: None,
|
||||||
y1 : 0.0,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Based on https://hackmd.io/IACbwcOTSt6Adj3_F9bKuw PID implementation
|
pub fn update(&mut self, input: f64, time_delta: Time, current: ElectricCurrent) -> f64 {
|
||||||
// Input x(t), target u(t), output y(t)
|
let time_delta = time_delta.get::<second>();
|
||||||
// y0' = y1 - ki * u0
|
|
||||||
// + x0 * (kp + ki + kd)
|
|
||||||
// - x1 * (kp + 2kd)
|
|
||||||
// + x2 * kd
|
|
||||||
// y0 = clip(y0', ymin, ymax)
|
|
||||||
pub fn update(&mut self, input: f64) -> f64 {
|
|
||||||
|
|
||||||
let mut output: f64 = self.y1 - self.target * f64::from(self.parameters.ki)
|
// error
|
||||||
+ input * f64::from(self.parameters.kp + self.parameters.ki + self.parameters.kd)
|
let error = self.target - input;
|
||||||
- self.x1 * f64::from(self.parameters.kp + 2.0 * self.parameters.kd)
|
|
||||||
+ self.x2 * f64::from(self.parameters.kd);
|
// proportional
|
||||||
|
let p = f64::from(self.parameters.kp) * error;
|
||||||
|
|
||||||
|
// integral
|
||||||
|
if let Some(last_output_val) = self.last_output {
|
||||||
|
let electric_current_error = ElectricCurrent::new::<ampere>(last_output_val) - current;
|
||||||
|
// anti integral windup
|
||||||
|
if last_output_val < self.parameters.output_max.into() &&
|
||||||
|
last_output_val > self.parameters.output_min.into() &&
|
||||||
|
electric_current_error < ElectricCurrent::new::<ampere>(CURRENT_ERROR_MAX) &&
|
||||||
|
electric_current_error > -ElectricCurrent::new::<ampere>(CURRENT_ERROR_MAX) {
|
||||||
|
self.integral += error * time_delta;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if self.integral < self.parameters.integral_min.into() {
|
||||||
|
self.integral = self.parameters.integral_min.into();
|
||||||
|
}
|
||||||
|
if self.integral > self.parameters.integral_max.into() {
|
||||||
|
self.integral = self.parameters.integral_max.into();
|
||||||
|
}
|
||||||
|
let i = self.integral * f64::from(self.parameters.ki);
|
||||||
|
|
||||||
|
// derivative
|
||||||
|
let d = match self.last_input {
|
||||||
|
None =>
|
||||||
|
0.0,
|
||||||
|
Some(last_input) =>
|
||||||
|
f64::from(self.parameters.kd) * (last_input - input) / time_delta,
|
||||||
|
};
|
||||||
|
self.last_input = Some(input);
|
||||||
|
|
||||||
|
// output
|
||||||
|
let mut output = p + i + d;
|
||||||
if output < self.parameters.output_min.into() {
|
if output < self.parameters.output_min.into() {
|
||||||
output = self.parameters.output_min.into();
|
output = self.parameters.output_min.into();
|
||||||
}
|
}
|
||||||
if output > self.parameters.output_max.into() {
|
if output > self.parameters.output_max.into() {
|
||||||
output = self.parameters.output_max.into();
|
output = self.parameters.output_max.into();
|
||||||
}
|
}
|
||||||
self.x2 = self.x1;
|
self.last_output = Some(output);
|
||||||
self.x1 = input;
|
|
||||||
self.u1 = self.target;
|
|
||||||
self.y1 = output;
|
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,10 +114,17 @@ impl Controller {
|
||||||
channel,
|
channel,
|
||||||
parameters: self.parameters.clone(),
|
parameters: self.parameters.clone(),
|
||||||
target: self.target,
|
target: self.target,
|
||||||
|
integral: self.integral,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_ki(&mut self, new_ki: f32) {
|
pub fn update_ki(&mut self, new_ki: f32) {
|
||||||
|
if new_ki == 0.0 {
|
||||||
|
self.integral = 0.0;
|
||||||
|
} else {
|
||||||
|
// Rescale integral with changes to kI, aka "Bumpless operation"
|
||||||
|
self.integral = f64::from(self.parameters.ki) * self.integral / f64::from(new_ki);
|
||||||
|
}
|
||||||
self.parameters.ki = new_ki;
|
self.parameters.ki = new_ki;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,6 +134,7 @@ pub struct Summary {
|
||||||
channel: usize,
|
channel: usize,
|
||||||
parameters: Parameters,
|
parameters: Parameters,
|
||||||
target: f64,
|
target: f64,
|
||||||
|
integral: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -104,6 +147,8 @@ mod test {
|
||||||
kd: 0.15,
|
kd: 0.15,
|
||||||
output_min: -10.0,
|
output_min: -10.0,
|
||||||
output_max: 10.0,
|
output_max: 10.0,
|
||||||
|
integral_min: -1000.0,
|
||||||
|
integral_max: 1000.0,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -132,9 +177,9 @@ mod test {
|
||||||
while !values.iter().all(|value| target.contains(value)) && total_t < CYCLE_LIMIT {
|
while !values.iter().all(|value| target.contains(value)) && total_t < CYCLE_LIMIT {
|
||||||
let next_t = (t + 1) % DELAY;
|
let next_t = (t + 1) % DELAY;
|
||||||
// Feed the oldest temperature
|
// Feed the oldest temperature
|
||||||
output = pid.update(values[next_t]);
|
output = pid.update(values[next_t], Time::new::<second>(1.0), ElectricCurrent::new::<ampere>(output));
|
||||||
// Overwrite oldest with previous temperature - output
|
// Overwrite oldest with previous temperature - output
|
||||||
values[next_t] = values[t] - output - (values[t] - DEFAULT) * LOSS;
|
values[next_t] = values[t] + output - (values[t] - DEFAULT) * LOSS;
|
||||||
t = next_t;
|
t = next_t;
|
||||||
total_t += 1;
|
total_t += 1;
|
||||||
println!("{}", values[t].to_string());
|
println!("{}", values[t].to_string());
|
||||||
|
|
127
src/pins.rs
127
src/pins.rs
|
@ -16,16 +16,15 @@ use stm32f4xx_hal::{
|
||||||
otg_fs::USB,
|
otg_fs::USB,
|
||||||
rcc::Clocks,
|
rcc::Clocks,
|
||||||
pwm::{self, PwmChannels},
|
pwm::{self, PwmChannels},
|
||||||
spi::{Spi, NoMiso, TransferModeNormal},
|
spi::{Spi, NoMiso},
|
||||||
pac::{
|
stm32::{
|
||||||
ADC1,
|
ADC1,
|
||||||
GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG,
|
GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG,
|
||||||
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, TIM8
|
TIM1, TIM3,
|
||||||
},
|
},
|
||||||
timer::Timer,
|
|
||||||
time::U32Ext,
|
time::U32Ext,
|
||||||
};
|
};
|
||||||
use eeprom24x::{self, Eeprom24x};
|
use eeprom24x::{self, Eeprom24x};
|
||||||
|
@ -33,14 +32,12 @@ 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<
|
||||||
I2c<I2C1, (
|
I2c<I2C1, (
|
||||||
PB8<AlternateOD<{ stm32f4xx_hal::gpio::AF4 }>>,
|
PB8<AlternateOD<stm32f4xx_hal::gpio::AF4>>,
|
||||||
PB9<AlternateOD<{ stm32f4xx_hal::gpio::AF4 }>>
|
PB9<AlternateOD<stm32f4xx_hal::gpio::AF4>>
|
||||||
)>,
|
)>,
|
||||||
eeprom24x::page_size::B8,
|
eeprom24x::page_size::B8,
|
||||||
eeprom24x::addr_size::OneByte
|
eeprom24x::addr_size::OneByte
|
||||||
|
@ -48,6 +45,8 @@ pub type Eeprom = Eeprom24x<
|
||||||
|
|
||||||
pub type EthernetPins = EthPins<
|
pub type EthernetPins = EthPins<
|
||||||
PA1<Input<Floating>>,
|
PA1<Input<Floating>>,
|
||||||
|
PA2<Input<Floating>>,
|
||||||
|
PC1<Input<Floating>>,
|
||||||
PA7<Input<Floating>>,
|
PA7<Input<Floating>>,
|
||||||
PB11<Input<Floating>>,
|
PB11<Input<Floating>>,
|
||||||
PG13<Input<Floating>>,
|
PG13<Input<Floating>>,
|
||||||
|
@ -61,46 +60,36 @@ pub trait ChannelPins {
|
||||||
type DacSync: OutputPin;
|
type DacSync: OutputPin;
|
||||||
type Shdn: OutputPin;
|
type Shdn: OutputPin;
|
||||||
type VRefPin;
|
type VRefPin;
|
||||||
type ITecPin;
|
type ItecPin;
|
||||||
type DacFeedbackPin;
|
type DacFeedbackPin;
|
||||||
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 = Channel0VRef;
|
type VRefPin = PA0<Analog>;
|
||||||
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 = Channel1VRef;
|
type VRefPin = PA3<Analog>;
|
||||||
type ITecPin = PB0<Analog>;
|
type ItecPin = PB0<Analog>;
|
||||||
type DacFeedbackPin = PA5<Analog>;
|
type DacFeedbackPin = PA5<Analog>;
|
||||||
type TecUMeasPin = PC3<Analog>;
|
type TecUMeasPin = PC3<Analog>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// SPI peripheral used for communication with the ADC
|
/// SPI peripheral used for communication with the ADC
|
||||||
pub type AdcSpi = Spi<SPI2, (PB10<Alternate<AF5>>, PB14<Alternate<AF5>>, PB15<Alternate<AF5>>), TransferModeNormal>;
|
pub type AdcSpi = Spi<SPI2, (PB10<Alternate<AF5>>, PB14<Alternate<AF5>>, PB15<Alternate<AF5>>)>;
|
||||||
pub type AdcNss = PB12<Output<PushPull>>;
|
pub type AdcNss = PB12<Output<PushPull>>;
|
||||||
type Dac0Spi = Spi<SPI4, (PE2<Alternate<AF5>>, NoMiso, PE6<Alternate<AF5>>), TransferModeNormal>;
|
type Dac0Spi = Spi<SPI4, (PE2<Alternate<AF5>>, NoMiso, PE6<Alternate<AF5>>)>;
|
||||||
type Dac1Spi = Spi<SPI5, (PF7<Alternate<AF5>>, NoMiso, PF9<Alternate<AF5>>), TransferModeNormal>;
|
type Dac1Spi = Spi<SPI5, (PF7<Alternate<AF5>>, NoMiso, PF9<Alternate<AF5>>)>;
|
||||||
pub type PinsAdc = Adc<ADC1>;
|
pub type PinsAdc = Adc<ADC1>;
|
||||||
|
|
||||||
pub struct ChannelPinSet<C: ChannelPins> {
|
pub struct ChannelPinSet<C: ChannelPins> {
|
||||||
|
@ -108,18 +97,11 @@ pub struct ChannelPinSet<C: ChannelPins> {
|
||||||
pub dac_sync: C::DacSync,
|
pub dac_sync: C::DacSync,
|
||||||
pub shdn: C::Shdn,
|
pub shdn: C::Shdn,
|
||||||
pub vref_pin: C::VRefPin,
|
pub vref_pin: C::VRefPin,
|
||||||
pub itec_pin: C::ITecPin,
|
pub itec_pin: C::ItecPin,
|
||||||
pub dac_feedback_pin: C::DacFeedbackPin,
|
pub dac_feedback_pin: C::DacFeedbackPin,
|
||||||
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,
|
||||||
|
@ -133,13 +115,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, tim8: TIM8,
|
tim1: TIM1, tim3: TIM3,
|
||||||
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, Option<FanPin>, HWRev, HWSettings) {
|
) -> (Self, Leds, Eeprom, EthernetPins, USB) {
|
||||||
let gpioa = gpioa.split();
|
let gpioa = gpioa.split();
|
||||||
let gpiob = gpiob.split();
|
let gpiob = gpiob.split();
|
||||||
let gpioc = gpioc.split();
|
let gpioc = gpioc.split();
|
||||||
|
@ -160,17 +142,13 @@ 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 = if hwrev.major > 2 {Channel0VRef::Analog(gpioa.pa0.into_analog())} else {Channel0VRef::Disabled(gpioa.pa0)};
|
let vref0_pin = gpioa.pa0.into_analog();
|
||||||
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();
|
||||||
|
@ -190,7 +168,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 = if hwrev.major > 2 {Channel1VRef::Analog(gpioa.pa3.into_analog())} else {Channel1VRef::Disabled(gpioa.pa3)};
|
let vref1_pin = gpioa.pa3.into_analog();
|
||||||
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();
|
||||||
|
@ -214,13 +192,15 @@ impl Pins {
|
||||||
|
|
||||||
let leds = Leds::new(gpiod.pd9, gpiod.pd10.into_push_pull_output(), gpiod.pd11.into_push_pull_output());
|
let leds = Leds::new(gpiod.pd9, gpiod.pd10.into_push_pull_output(), gpiod.pd11.into_push_pull_output());
|
||||||
|
|
||||||
let eeprom_scl = gpiob.pb8.into_alternate().set_open_drain();
|
let eeprom_scl = gpiob.pb8.into_alternate_af4().set_open_drain();
|
||||||
let eeprom_sda = gpiob.pb9.into_alternate().set_open_drain();
|
let eeprom_sda = gpiob.pb9.into_alternate_af4().set_open_drain();
|
||||||
let eeprom_i2c = I2c::new(i2c1, (eeprom_scl, eeprom_sda), 400.khz(), clocks);
|
let eeprom_i2c = I2c::i2c1(i2c1, (eeprom_scl, eeprom_sda), 400.khz(), clocks);
|
||||||
let eeprom = Eeprom24x::new_24x02(eeprom_i2c, eeprom24x::SlaveAddr::default());
|
let eeprom = Eeprom24x::new_24x02(eeprom_i2c, eeprom24x::SlaveAddr::default());
|
||||||
|
|
||||||
let eth_pins = EthPins {
|
let eth_pins = EthPins {
|
||||||
ref_clk: gpioa.pa1,
|
ref_clk: gpioa.pa1,
|
||||||
|
md_io: gpioa.pa2,
|
||||||
|
md_clk: gpioc.pc1,
|
||||||
crs: gpioa.pa7,
|
crs: gpioa.pa7,
|
||||||
tx_en: gpiob.pb11,
|
tx_en: gpiob.pb11,
|
||||||
tx_d0: gpiog.pg13,
|
tx_d0: gpiog.pg13,
|
||||||
|
@ -233,16 +213,12 @@ impl Pins {
|
||||||
usb_global: otg_fs_global,
|
usb_global: otg_fs_global,
|
||||||
usb_device: otg_fs_device,
|
usb_device: otg_fs_device,
|
||||||
usb_pwrclk: otg_fs_pwrclk,
|
usb_pwrclk: otg_fs_pwrclk,
|
||||||
pin_dm: gpioa.pa11.into_alternate(),
|
pin_dm: gpioa.pa11.into_alternate_af10(),
|
||||||
pin_dp: gpioa.pa12.into_alternate(),
|
pin_dp: gpioa.pa12.into_alternate_af10(),
|
||||||
hclk: clocks.hclk(),
|
hclk: clocks.hclk(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let fan = if hw_settings.fan_available {
|
(pins, leds, eeprom, eth_pins, usb)
|
||||||
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
|
||||||
|
@ -254,14 +230,14 @@ impl Pins {
|
||||||
mosi: PB15<M3>,
|
mosi: PB15<M3>,
|
||||||
) -> AdcSpi
|
) -> AdcSpi
|
||||||
{
|
{
|
||||||
let sck = sck.into_alternate();
|
let sck = sck.into_alternate_af5();
|
||||||
let miso = miso.into_alternate();
|
let miso = miso.into_alternate_af5();
|
||||||
let mosi = mosi.into_alternate();
|
let mosi = mosi.into_alternate_af5();
|
||||||
Spi::new(
|
Spi::spi2(
|
||||||
spi2,
|
spi2,
|
||||||
(sck, miso, mosi),
|
(sck, miso, mosi),
|
||||||
crate::ad7172::SPI_MODE,
|
crate::ad7172::SPI_MODE,
|
||||||
crate::ad7172::SPI_CLOCK,
|
crate::ad7172::SPI_CLOCK.into(),
|
||||||
clocks
|
clocks
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -270,13 +246,13 @@ impl Pins {
|
||||||
clocks: Clocks, spi4: SPI4,
|
clocks: Clocks, spi4: SPI4,
|
||||||
sclk: PE2<M1>, sync: PE4<M2>, sdin: PE6<M3>
|
sclk: PE2<M1>, sync: PE4<M2>, sdin: PE6<M3>
|
||||||
) -> (Dac0Spi, <Channel0 as ChannelPins>::DacSync) {
|
) -> (Dac0Spi, <Channel0 as ChannelPins>::DacSync) {
|
||||||
let sclk = sclk.into_alternate();
|
let sclk = sclk.into_alternate_af5();
|
||||||
let sdin = sdin.into_alternate();
|
let sdin = sdin.into_alternate_af5();
|
||||||
let spi = Spi::new(
|
let spi = Spi::spi4(
|
||||||
spi4,
|
spi4,
|
||||||
(sclk, NoMiso {}, sdin),
|
(sclk, NoMiso, sdin),
|
||||||
crate::ad5680::SPI_MODE,
|
crate::ad5680::SPI_MODE,
|
||||||
crate::ad5680::SPI_CLOCK,
|
crate::ad5680::SPI_CLOCK.into(),
|
||||||
clocks
|
clocks
|
||||||
);
|
);
|
||||||
let sync = sync.into_push_pull_output();
|
let sync = sync.into_push_pull_output();
|
||||||
|
@ -288,13 +264,13 @@ impl Pins {
|
||||||
clocks: Clocks, spi5: SPI5,
|
clocks: Clocks, spi5: SPI5,
|
||||||
sclk: PF7<M1>, sync: PF6<M2>, sdin: PF9<M3>
|
sclk: PF7<M1>, sync: PF6<M2>, sdin: PF9<M3>
|
||||||
) -> (Dac1Spi, <Channel1 as ChannelPins>::DacSync) {
|
) -> (Dac1Spi, <Channel1 as ChannelPins>::DacSync) {
|
||||||
let sclk = sclk.into_alternate();
|
let sclk = sclk.into_alternate_af5();
|
||||||
let sdin = sdin.into_alternate();
|
let sdin = sdin.into_alternate_af5();
|
||||||
let spi = Spi::new(
|
let spi = Spi::spi5(
|
||||||
spi5,
|
spi5,
|
||||||
(sclk, NoMiso {}, sdin),
|
(sclk, NoMiso, sdin),
|
||||||
crate::ad5680::SPI_MODE,
|
crate::ad5680::SPI_MODE,
|
||||||
crate::ad5680::SPI_CLOCK,
|
crate::ad5680::SPI_CLOCK.into(),
|
||||||
clocks
|
clocks
|
||||||
);
|
);
|
||||||
let sync = sync.into_push_pull_output();
|
let sync = sync.into_push_pull_output();
|
||||||
|
@ -331,22 +307,21 @@ impl PwmPins {
|
||||||
pin.enable();
|
pin.enable();
|
||||||
}
|
}
|
||||||
let channels = (
|
let channels = (
|
||||||
max_v0.into_alternate(),
|
max_v0.into_alternate_af2(),
|
||||||
max_v1.into_alternate(),
|
max_v1.into_alternate_af2(),
|
||||||
);
|
);
|
||||||
//let (mut max_v0, mut max_v1) = pwm::tim3(tim3, channels, clocks, freq);
|
let (mut max_v0, mut max_v1) = pwm::tim3(tim3, channels, clocks, freq);
|
||||||
let (mut max_v0, mut max_v1) = Timer::new(tim3, &clocks).pwm(channels, freq);
|
|
||||||
init_pwm_pin(&mut max_v0);
|
init_pwm_pin(&mut max_v0);
|
||||||
init_pwm_pin(&mut max_v1);
|
init_pwm_pin(&mut max_v1);
|
||||||
|
|
||||||
let channels = (
|
let channels = (
|
||||||
max_i_pos0.into_alternate(),
|
max_i_pos0.into_alternate_af1(),
|
||||||
max_i_pos1.into_alternate(),
|
max_i_pos1.into_alternate_af1(),
|
||||||
max_i_neg0.into_alternate(),
|
max_i_neg0.into_alternate_af1(),
|
||||||
max_i_neg1.into_alternate(),
|
max_i_neg1.into_alternate_af1(),
|
||||||
);
|
);
|
||||||
let (mut max_i_pos0, mut max_i_pos1, mut max_i_neg0, mut max_i_neg1) =
|
let (mut max_i_pos0, mut max_i_pos1, mut max_i_neg0, mut max_i_neg1) =
|
||||||
Timer::new(tim1, &clocks).pwm(channels, freq);
|
pwm::tim1(tim1, channels, clocks, freq);
|
||||||
init_pwm_pin(&mut max_i_pos0);
|
init_pwm_pin(&mut max_i_pos0);
|
||||||
init_pwm_pin(&mut max_i_neg0);
|
init_pwm_pin(&mut max_i_neg0);
|
||||||
init_pwm_pin(&mut max_i_pos1);
|
init_pwm_pin(&mut max_i_pos1);
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use core::mem::MaybeUninit;
|
||||||
use smoltcp::{
|
use smoltcp::{
|
||||||
iface::EthernetInterface,
|
iface::EthernetInterface,
|
||||||
socket::{SocketSet, SocketHandle, TcpSocket, TcpSocketBuffer, SocketRef},
|
socket::{SocketSet, SocketHandle, TcpSocket, TcpSocketBuffer, SocketRef},
|
||||||
|
@ -12,18 +13,6 @@ 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,38 +24,39 @@ const TCP_TX_BUFFER_SIZE: usize = 2048;
|
||||||
/// Contains a number of server sockets that get all sent the same
|
/// Contains a number of server sockets that get all sent the same
|
||||||
/// data (through `fmt::Write`).
|
/// data (through `fmt::Write`).
|
||||||
pub struct Server<'a, 'b, S> {
|
pub struct Server<'a, 'b, S> {
|
||||||
net: EthernetInterface<'a, &'a mut stm32_eth::Eth<'static, 'static>>,
|
net: EthernetInterface<'a, 'a, 'a, &'a mut stm32_eth::Eth<'static, 'static>>,
|
||||||
sockets: SocketSet<'b>,
|
sockets: SocketSet<'b, 'b, 'b>,
|
||||||
states: [SocketState<S>; SOCKET_COUNT],
|
states: [SocketState<S>; SOCKET_COUNT],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b, S: Default> Server<'a, 'b, S> {
|
impl<'a, 'b, S: Default> Server<'a, 'b, S> {
|
||||||
/// Run a server with stack-allocated sockets
|
/// Run a server with stack-allocated sockets
|
||||||
pub fn run<F>(net: EthernetInterface<'a, &'a mut stm32_eth::Eth<'static, 'static>>, f: F)
|
pub fn run<F>(net: EthernetInterface<'a, 'a, 'a, &'a mut stm32_eth::Eth<'static, 'static>>, f: F)
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut Server<'a, '_, S>),
|
F: FnOnce(&mut Server<'a, '_, S>),
|
||||||
{
|
{
|
||||||
macro_rules! create_rtx_storage {
|
|
||||||
($rx_storage:ident, $tx_storage:ident) => {
|
|
||||||
let mut $rx_storage = [0; TCP_RX_BUFFER_SIZE];
|
|
||||||
let mut $tx_storage = [0; TCP_TX_BUFFER_SIZE];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
create_rtx_storage!(tcp_rx_storage0, tcp_tx_storage0);
|
|
||||||
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_storage: [_; SOCKET_COUNT] = Default::default();
|
||||||
let mut sockets = SocketSet::new(&mut sockets_storage[..]);
|
let mut sockets = SocketSet::new(&mut sockets_storage[..]);
|
||||||
|
let mut states: [SocketState<S>; SOCKET_COUNT] = unsafe { MaybeUninit::uninit().assume_init() };
|
||||||
|
|
||||||
let states: [SocketState<S>; SOCKET_COUNT] = [
|
macro_rules! create_socket {
|
||||||
SocketState::<S>::new(&mut sockets, &mut tcp_rx_storage0, &mut tcp_tx_storage0),
|
($set:ident, $rx_storage:ident, $tx_storage:ident, $target:expr) => {
|
||||||
SocketState::<S>::new(&mut sockets, &mut tcp_rx_storage1, &mut tcp_tx_storage1),
|
let mut $rx_storage = [0; TCP_RX_BUFFER_SIZE];
|
||||||
SocketState::<S>::new(&mut sockets, &mut tcp_rx_storage2, &mut tcp_tx_storage2),
|
let mut $tx_storage = [0; TCP_TX_BUFFER_SIZE];
|
||||||
SocketState::<S>::new(&mut sockets, &mut tcp_rx_storage3, &mut tcp_tx_storage3),
|
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 {
|
||||||
|
state.state = S::default();
|
||||||
|
}
|
||||||
|
|
||||||
let mut server = Server {
|
let mut server = Server {
|
||||||
states,
|
states,
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use super::command_parser::{Command, Error as ParserError};
|
use super::command_parser::{Command, Error as ParserError};
|
||||||
|
use super::channels::CHANNELS;
|
||||||
|
|
||||||
const MAX_LINE_LEN: usize = 64;
|
const MAX_LINE_LEN: usize = 64;
|
||||||
|
|
||||||
|
@ -52,6 +53,8 @@ impl From<Result<Command, ParserError>> for SessionInput {
|
||||||
|
|
||||||
pub struct Session {
|
pub struct Session {
|
||||||
reader: LineReader,
|
reader: LineReader,
|
||||||
|
reporting: bool,
|
||||||
|
report_pending: [bool; CHANNELS],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Session {
|
impl Default for Session {
|
||||||
|
@ -64,11 +67,43 @@ impl Session {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Session {
|
Session {
|
||||||
reader: LineReader::new(),
|
reader: LineReader::new(),
|
||||||
|
reporting: false,
|
||||||
|
report_pending: [false; CHANNELS],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reset(&mut self) {
|
pub fn reset(&mut self) {
|
||||||
self.reader = LineReader::new();
|
self.reader = LineReader::new();
|
||||||
|
self.reporting = false;
|
||||||
|
self.report_pending = [false; CHANNELS];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reporting(&self) -> bool {
|
||||||
|
self.reporting
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_report_pending(&mut self, channel: usize) {
|
||||||
|
if self.reporting {
|
||||||
|
self.report_pending[channel] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_report_pending(&self) -> Option<usize> {
|
||||||
|
if ! self.reporting {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
self.report_pending.iter()
|
||||||
|
.enumerate()
|
||||||
|
.fold(None, |result, (channel, report_pending)| {
|
||||||
|
result.or_else(|| {
|
||||||
|
if *report_pending { Some(channel) } else { None }
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn mark_report_sent(&mut self, channel: usize) {
|
||||||
|
self.report_pending[channel] = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn feed(&mut self, buf: &[u8]) -> (usize, SessionInput) {
|
pub fn feed(&mut self, buf: &[u8]) -> (usize, SessionInput) {
|
||||||
|
@ -79,6 +114,12 @@ impl Session {
|
||||||
match line {
|
match line {
|
||||||
Some(line) => {
|
Some(line) => {
|
||||||
let command = Command::parse(&line);
|
let command = Command::parse(&line);
|
||||||
|
match command {
|
||||||
|
Ok(Command::Reporting(reporting)) => {
|
||||||
|
self.reporting = reporting;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
return (buf_bytes, command.into());
|
return (buf_bytes, command.into());
|
||||||
}
|
}
|
||||||
None => {}
|
None => {}
|
||||||
|
|
|
@ -18,10 +18,8 @@ static TIMER_MS: Mutex<RefCell<u32>> = Mutex::new(RefCell::new(0));
|
||||||
|
|
||||||
/// Setup SysTick exception
|
/// Setup SysTick exception
|
||||||
pub fn setup(syst: SYST, clocks: Clocks) {
|
pub fn setup(syst: SYST, clocks: Clocks) {
|
||||||
|
let mut timer = Timer::syst(syst, TIMER_RATE.hz(), clocks);
|
||||||
let timer = Timer::syst(syst, &clocks);
|
timer.listen(TimerEvent::TimeOut);
|
||||||
let mut countdown = timer.start_count_down(TIMER_RATE.hz());
|
|
||||||
countdown.listen(TimerEvent::TimeOut);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// SysTick exception (Timer)
|
/// SysTick exception (Timer)
|
||||||
|
|
Loading…
Reference in New Issue