From 0f6c9fe97a61005303584b60fb3167e9b299f7e3 Mon Sep 17 00:00:00 2001 From: malled Date: Tue, 24 Sep 2024 18:31:05 +0200 Subject: [PATCH] init --- .cargo/config.toml | 8 + .gitignore | 1 + Cargo.lock | 1007 +++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 80 ++++ README.md | 44 ++ build.rs | 35 ++ memory.x | 14 + rust-toolchain.toml | 10 + src/gnss.rs | 23 + src/main.rs | 154 +++++++ src/mqtt.rs | 61 +++ src/net.rs | 89 ++++ src/sensors.rs | 105 +++++ 13 files changed, 1631 insertions(+) create mode 100644 .cargo/config.toml create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 build.rs create mode 100644 memory.x create mode 100644 rust-toolchain.toml create mode 100644 src/gnss.rs create mode 100644 src/main.rs create mode 100644 src/mqtt.rs create mode 100644 src/net.rs create mode 100644 src/sensors.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..024c7ac --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,8 @@ +[target.'cfg(all(target_arch = "arm", target_os = "none"))'] +runner = "probe-rs run --chip stm32l431CCUx" + +[build] +target = "thumbv7em-none-eabihf" + +[env] +DEFMT_LOG = "info" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..3642a2c --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,1007 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aligned" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "377e4c0ba83e4431b10df45c1d4666f178ea9c552cac93e60c3a88bf32785923" +dependencies = [ + "as-slice", +] + +[[package]] +name = "as-slice" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "516b6b4f0e40d50dcda9365d53964ec74560ad4284da2e7fc97122cd83174516" +dependencies = [ + "stable_deref_trait", +] + +[[package]] +name = "atomic-polyfill" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4" +dependencies = [ + "critical-section", +] + +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + +[[package]] +name = "bare-metal" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5deb64efa5bd81e31fcd1938615a6d98c82eafcbcd787162b6f63b91d6bac5b3" +dependencies = [ + "rustc_version 0.2.3", +] + +[[package]] +name = "bit_field" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61" + +[[package]] +name = "bitfield" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "bitflags" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" + +[[package]] +name = "block-device-driver" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c051592f59fe68053524b4c4935249b806f72c1f544cfb7abe4f57c3be258e" +dependencies = [ + "aligned", +] + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cortex-m" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ec610d8f49840a5b376c69663b6369e71f4b34484b9b2eb29fb918d92516cb9" +dependencies = [ + "bare-metal", + "bitfield", + "critical-section", + "embedded-hal 0.2.7", + "volatile-register", +] + +[[package]] +name = "cortex-m-rt" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee84e813d593101b1723e13ec38b6ab6abbdbaaa4546553f5395ed274079ddb1" +dependencies = [ + "cortex-m-rt-macros", +] + +[[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 1.0.109", +] + +[[package]] +name = "critical-section" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f64009896348fc5af4222e9cf7d7d82a95a256c634ebcf61c53e4ea461422242" + +[[package]] +name = "darling" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn 2.0.77", +] + +[[package]] +name = "darling_macro" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" +dependencies = [ + "darling_core", + "quote", + "syn 2.0.77", +] + +[[package]] +name = "defmt" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a99dd22262668b887121d4672af5a64b238f026099f1a2a1b322066c9ecfe9e0" +dependencies = [ + "bitflags 1.3.2", + "defmt-macros", +] + +[[package]] +name = "defmt-macros" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3a9f309eff1f79b3ebdf252954d90ae440599c26c2c553fe87a2d17195f2dcb" +dependencies = [ + "defmt-parser", + "proc-macro-error", + "proc-macro2", + "quote", + "syn 2.0.77", +] + +[[package]] +name = "defmt-parser" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff4a5fefe330e8d7f31b16a318f9ce81000d8e35e69b93eae154d16d2278f70f" +dependencies = [ + "thiserror", +] + +[[package]] +name = "defmt-rtt" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab697b3dbbc1750b7c8b821aa6f6e7f2480b47a99bc057a2ed7b170ebef0c51" +dependencies = [ + "critical-section", + "defmt", +] + +[[package]] +name = "document-features" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb6969eaabd2421f8a2775cfd2471a2b634372b4a25d41e3bd647b79912850a0" +dependencies = [ + "litrs", +] + +[[package]] +name = "embassy-embedded-hal" +version = "0.2.0" +source = "git+https://github.com/embassy-rs/embassy?rev=45cbcb513dc0bbf3e12c102df0db8c15643cc78b#45cbcb513dc0bbf3e12c102df0db8c15643cc78b" +dependencies = [ + "defmt", + "embassy-futures", + "embassy-sync", + "embassy-time", + "embedded-hal 0.2.7", + "embedded-hal 1.0.0", + "embedded-hal-async", + "embedded-storage", + "embedded-storage-async", + "nb 1.1.0", +] + +[[package]] +name = "embassy-executor" +version = "0.6.0" +source = "git+https://github.com/embassy-rs/embassy?rev=45cbcb513dc0bbf3e12c102df0db8c15643cc78b#45cbcb513dc0bbf3e12c102df0db8c15643cc78b" +dependencies = [ + "cortex-m", + "critical-section", + "defmt", + "document-features", + "embassy-executor-macros", + "embassy-time-driver", + "embassy-time-queue-driver", +] + +[[package]] +name = "embassy-executor-macros" +version = "0.5.0" +source = "git+https://github.com/embassy-rs/embassy?rev=45cbcb513dc0bbf3e12c102df0db8c15643cc78b#45cbcb513dc0bbf3e12c102df0db8c15643cc78b" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn 2.0.77", +] + +[[package]] +name = "embassy-futures" +version = "0.1.1" +source = "git+https://github.com/embassy-rs/embassy?rev=45cbcb513dc0bbf3e12c102df0db8c15643cc78b#45cbcb513dc0bbf3e12c102df0db8c15643cc78b" + +[[package]] +name = "embassy-hal-internal" +version = "0.2.0" +source = "git+https://github.com/embassy-rs/embassy?rev=45cbcb513dc0bbf3e12c102df0db8c15643cc78b#45cbcb513dc0bbf3e12c102df0db8c15643cc78b" +dependencies = [ + "cortex-m", + "critical-section", + "defmt", + "num-traits", +] + +[[package]] +name = "embassy-net" +version = "0.4.0" +source = "git+https://github.com/embassy-rs/embassy?rev=45cbcb513dc0bbf3e12c102df0db8c15643cc78b#45cbcb513dc0bbf3e12c102df0db8c15643cc78b" +dependencies = [ + "defmt", + "document-features", + "embassy-net-driver", + "embassy-sync", + "embassy-time", + "embedded-io-async", + "embedded-nal-async", + "heapless 0.8.0", + "managed", + "smoltcp", +] + +[[package]] +name = "embassy-net-driver" +version = "0.2.0" +source = "git+https://github.com/embassy-rs/embassy?rev=45cbcb513dc0bbf3e12c102df0db8c15643cc78b#45cbcb513dc0bbf3e12c102df0db8c15643cc78b" +dependencies = [ + "defmt", +] + +[[package]] +name = "embassy-net-driver-channel" +version = "0.3.0" +source = "git+https://github.com/embassy-rs/embassy?rev=45cbcb513dc0bbf3e12c102df0db8c15643cc78b#45cbcb513dc0bbf3e12c102df0db8c15643cc78b" +dependencies = [ + "embassy-futures", + "embassy-net-driver", + "embassy-sync", +] + +[[package]] +name = "embassy-net-ppp" +version = "0.1.0" +source = "git+https://github.com/embassy-rs/embassy?rev=45cbcb513dc0bbf3e12c102df0db8c15643cc78b#45cbcb513dc0bbf3e12c102df0db8c15643cc78b" +dependencies = [ + "defmt", + "embassy-futures", + "embassy-net-driver-channel", + "embassy-sync", + "embedded-io-async", + "ppproto", +] + +[[package]] +name = "embassy-stm32" +version = "0.1.0" +source = "git+https://github.com/embassy-rs/embassy?rev=45cbcb513dc0bbf3e12c102df0db8c15643cc78b#45cbcb513dc0bbf3e12c102df0db8c15643cc78b" +dependencies = [ + "aligned", + "bit_field", + "bitflags 2.6.0", + "block-device-driver", + "cfg-if", + "cortex-m", + "cortex-m-rt", + "critical-section", + "defmt", + "document-features", + "embassy-embedded-hal", + "embassy-futures", + "embassy-hal-internal", + "embassy-net-driver", + "embassy-sync", + "embassy-time", + "embassy-time-driver", + "embassy-usb-driver", + "embassy-usb-synopsys-otg", + "embedded-can", + "embedded-hal 0.2.7", + "embedded-hal 1.0.0", + "embedded-hal-async", + "embedded-hal-nb", + "embedded-io", + "embedded-io-async", + "embedded-storage", + "embedded-storage-async", + "futures-util", + "nb 1.1.0", + "proc-macro2", + "quote", + "rand_core", + "sdio-host", + "static_assertions", + "stm32-fmc", + "stm32-metapac", + "vcell", + "volatile-register", +] + +[[package]] +name = "embassy-sync" +version = "0.6.0" +source = "git+https://github.com/embassy-rs/embassy?rev=45cbcb513dc0bbf3e12c102df0db8c15643cc78b#45cbcb513dc0bbf3e12c102df0db8c15643cc78b" +dependencies = [ + "cfg-if", + "critical-section", + "defmt", + "embedded-io-async", + "futures-util", + "heapless 0.8.0", +] + +[[package]] +name = "embassy-time" +version = "0.3.2" +source = "git+https://github.com/embassy-rs/embassy?rev=45cbcb513dc0bbf3e12c102df0db8c15643cc78b#45cbcb513dc0bbf3e12c102df0db8c15643cc78b" +dependencies = [ + "cfg-if", + "critical-section", + "defmt", + "document-features", + "embassy-time-driver", + "embassy-time-queue-driver", + "embedded-hal 0.2.7", + "embedded-hal 1.0.0", + "embedded-hal-async", + "futures-util", + "heapless 0.8.0", +] + +[[package]] +name = "embassy-time-driver" +version = "0.1.0" +source = "git+https://github.com/embassy-rs/embassy?rev=45cbcb513dc0bbf3e12c102df0db8c15643cc78b#45cbcb513dc0bbf3e12c102df0db8c15643cc78b" +dependencies = [ + "document-features", +] + +[[package]] +name = "embassy-time-queue-driver" +version = "0.1.0" +source = "git+https://github.com/embassy-rs/embassy?rev=45cbcb513dc0bbf3e12c102df0db8c15643cc78b#45cbcb513dc0bbf3e12c102df0db8c15643cc78b" + +[[package]] +name = "embassy-usb-driver" +version = "0.1.0" +source = "git+https://github.com/embassy-rs/embassy?rev=45cbcb513dc0bbf3e12c102df0db8c15643cc78b#45cbcb513dc0bbf3e12c102df0db8c15643cc78b" +dependencies = [ + "defmt", +] + +[[package]] +name = "embassy-usb-synopsys-otg" +version = "0.1.0" +source = "git+https://github.com/embassy-rs/embassy?rev=45cbcb513dc0bbf3e12c102df0db8c15643cc78b#45cbcb513dc0bbf3e12c102df0db8c15643cc78b" +dependencies = [ + "critical-section", + "embassy-sync", + "embassy-usb-driver", +] + +[[package]] +name = "embedded-can" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9d2e857f87ac832df68fa498d18ddc679175cf3d2e4aa893988e5601baf9438" +dependencies = [ + "nb 1.1.0", +] + +[[package]] +name = "embedded-hal" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35949884794ad573cf46071e41c9b60efb0cb311e3ca01f7af807af1debc66ff" +dependencies = [ + "nb 0.1.3", + "void", +] + +[[package]] +name = "embedded-hal" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "361a90feb7004eca4019fb28352a9465666b24f840f5c3cddf0ff13920590b89" + +[[package]] +name = "embedded-hal-async" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4c685bbef7fe13c3c6dd4da26841ed3980ef33e841cddfa15ce8a8fb3f1884" +dependencies = [ + "embedded-hal 1.0.0", +] + +[[package]] +name = "embedded-hal-nb" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fba4268c14288c828995299e59b12babdbe170f6c6d73731af1b4648142e8605" +dependencies = [ + "embedded-hal 1.0.0", + "nb 1.1.0", +] + +[[package]] +name = "embedded-io" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" +dependencies = [ + "defmt", +] + +[[package]] +name = "embedded-io-async" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ff09972d4073aa8c299395be75161d582e7629cd663171d62af73c8d50dba3f" +dependencies = [ + "defmt", + "embedded-io", +] + +[[package]] +name = "embedded-nal" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8a943fad5ed3d3f8a00f1e80f6bba371f1e7f0df28ec38477535eb318dc19cc" +dependencies = [ + "nb 1.1.0", + "no-std-net", +] + +[[package]] +name = "embedded-nal-async" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72229137a4fc12d239b0b7f50f04b30790678da6d782a0f3f1909bf57ec4b759" +dependencies = [ + "embedded-io-async", + "embedded-nal", + "no-std-net", +] + +[[package]] +name = "embedded-storage" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21dea9854beb860f3062d10228ce9b976da520a73474aed3171ec276bc0c032" + +[[package]] +name = "embedded-storage-async" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1763775e2323b7d5f0aa6090657f5e21cfa02ede71f5dc40eead06d64dcd15cc" +dependencies = [ + "embedded-storage", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "futures-core" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" + +[[package]] +name = "futures-task" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" + +[[package]] +name = "futures-util" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "pin-utils", +] + +[[package]] +name = "go-tracker" +version = "0.1.0" +dependencies = [ + "cortex-m", + "cortex-m-rt", + "defmt", + "defmt-rtt", + "embassy-executor", + "embassy-futures", + "embassy-net", + "embassy-net-ppp", + "embassy-stm32", + "embassy-sync", + "embassy-time", + "embedded-hal 1.0.0", + "embedded-hal-async", + "embedded-io", + "embedded-io-async", + "embedded-storage", + "heapless 0.8.0", + "panic-probe", + "rand", + "rust-mqtt", + "static_cell", +] + +[[package]] +name = "hash32" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0c35f58762feb77d74ebe43bdbc3210f09be9fe6742234d573bacc26ed92b67" +dependencies = [ + "byteorder", +] + +[[package]] +name = "hash32" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606" +dependencies = [ + "byteorder", +] + +[[package]] +name = "heapless" +version = "0.7.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdc6457c0eb62c71aac4bc17216026d8410337c4126773b9c5daba343f17964f" +dependencies = [ + "atomic-polyfill", + "hash32 0.2.1", + "rustc_version 0.4.1", + "spin", + "stable_deref_trait", +] + +[[package]] +name = "heapless" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad" +dependencies = [ + "defmt", + "hash32 0.3.1", + "stable_deref_trait", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "litrs" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ce301924b7887e9d637144fdade93f9dfff9b60981d4ac161db09720d39aa5" + +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + +[[package]] +name = "managed" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ca88d725a0a943b096803bd34e73a4437208b6077654cc4ecb2947a5f91618d" + +[[package]] +name = "nb" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "801d31da0513b6ec5214e9bf433a77966320625a37860f910be265be6e18d06f" +dependencies = [ + "nb 1.1.0", +] + +[[package]] +name = "nb" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d" + +[[package]] +name = "no-std-net" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43794a0ace135be66a25d3ae77d41b91615fb68ae937f904090203e81f755b65" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "num_enum" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e613fc340b2220f734a8595782c551f1250e969d87d3be1ae0579e8d4065179" +dependencies = [ + "num_enum_derive", +] + +[[package]] +name = "num_enum_derive" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + +[[package]] +name = "panic-probe" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4047d9235d1423d66cc97da7d07eddb54d4f154d6c13805c6d0793956f4f25b0" +dependencies = [ + "cortex-m", + "defmt", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "portable-atomic" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da544ee218f0d287a911e9c99a39a8c9bc8fcad3cb8db5959940044ecfc67265" + +[[package]] +name = "ppproto" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b08f23bada7fbe9266b2d485b3abefdff31f1d3cc8c77f067165cf23848c9a32" +dependencies = [ + "defmt", + "heapless 0.7.17", + "num_enum", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn 1.0.109", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" + +[[package]] +name = "rust-mqtt" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f71160765f368fd9a84e0955e2ddb6d64ac9018fee1c5323354d6d08c816b40" +dependencies = [ + "defmt", + "embedded-io", + "embedded-io-async", + "heapless 0.8.0", + "rand_core", +] + +[[package]] +name = "rustc_version" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +dependencies = [ + "semver 0.9.0", +] + +[[package]] +name = "rustc_version" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92" +dependencies = [ + "semver 1.0.23", +] + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "sdio-host" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f93c025f9cfe4c388c328ece47d11a54a823da3b5ad0370b22d95ad47137f85a" + +[[package]] +name = "semver" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" +dependencies = [ + "semver-parser", +] + +[[package]] +name = "semver" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" + +[[package]] +name = "semver-parser" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" + +[[package]] +name = "smoltcp" +version = "0.11.0" +source = "git+https://github.com/smoltcp-rs/smoltcp?rev=dd43c8f189178b0ab3bda798ed8578b5b0a6f094#dd43c8f189178b0ab3bda798ed8578b5b0a6f094" +dependencies = [ + "bitflags 1.3.2", + "byteorder", + "cfg-if", + "defmt", + "heapless 0.8.0", + "managed", +] + +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +dependencies = [ + "lock_api", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + +[[package]] +name = "static_assertions" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" + +[[package]] +name = "static_cell" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89b0684884a883431282db1e4343f34afc2ff6996fe1f4a1664519b66e14c1e" +dependencies = [ + "portable-atomic", +] + +[[package]] +name = "stm32-fmc" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7f0639399e2307c2446c54d91d4f1596343a1e1d5cab605b9cce11d0ab3858c" +dependencies = [ + "embedded-hal 0.2.7", +] + +[[package]] +name = "stm32-metapac" +version = "15.0.0" +source = "git+https://github.com/embassy-rs/stm32-data-generated?tag=stm32-data-ad00827345b4b758b2453082809d6e3b634b5364#e442835159a7a311410cd4a2879da52c6f57af52" +dependencies = [ + "cortex-m", + "cortex-m-rt", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + +[[package]] +name = "unicode-ident" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" + +[[package]] +name = "vcell" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77439c1b53d2303b20d9459b1ade71a83c716e3f9c34f3228c00e6f185d6c002" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" + +[[package]] +name = "volatile-register" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de437e2a6208b014ab52972a27e59b33fa2920d3e00fe05026167a1c509d19cc" +dependencies = [ + "vcell", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d05276a --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,80 @@ +[package] +edition = "2021" +name = "go-tracker" +version = "0.1.0" +authors = ["malled "] +resolver = "2" + +[dependencies] +defmt = "0.3" +defmt-rtt = "0.4" +panic-probe = { version = "0.3", features = ["print-defmt"] } + +embedded-hal = "1.0.0" +embedded-hal-async = "1.0.0" +embedded-io = "0.6.1" +embedded-io-async = "0.6.1" +embedded-storage = "0.3.1" + +cortex-m-rt = "0.7.3" + +embassy-executor = { version = "0.6.0", features = [ + "task-arena-size-8192", + "arch-cortex-m", + "executor-thread", + "defmt", + "integrated-timers", + "executor-interrupt", +] } +embassy-sync = { version = "0.6.0" } +embassy-time = { version = "0.3.0", features = [ + "defmt", + "defmt-timestamp-uptime", +] } + +embassy-net = { version = "0.4.0", features = [ + "dhcpv4", + "tcp", + "dns", + "defmt", + "proto-ipv4", + "medium-ip", +] } +cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } +embassy-stm32 = { version = "0.1.0", features = [ + "defmt", + "stm32l431cc", + "unstable-pac", + "memory-x", + "time-driver-any", +] } +embassy-net-ppp = { version = "0.1.0", features = ["defmt"] } +embassy-futures = "0.1.1" +heapless = { version = "0.8.0", features = ["defmt-03"] } +static_cell = "2.1.0" +rand = { version = "0.8.5", default-features = false } +rust-mqtt = { version = "0.3.0", default-features = false, features = [ + "no_std", +] } +#minimq = "0.9.0" +#embedded-nal = "0.8.0" + + +[patch.crates-io] +embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "45cbcb513dc0bbf3e12c102df0db8c15643cc78b" } +embassy-time-driver = { git = "https://github.com/embassy-rs/embassy", rev = "45cbcb513dc0bbf3e12c102df0db8c15643cc78b" } +embassy-time-queue-driver = { git = "https://github.com/embassy-rs/embassy", rev = "45cbcb513dc0bbf3e12c102df0db8c15643cc78b" } +embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "45cbcb513dc0bbf3e12c102df0db8c15643cc78b" } +embassy-stm32 = { git = "https://github.com/embassy-rs/embassy", rev = "45cbcb513dc0bbf3e12c102df0db8c15643cc78b" } +embassy-net = { git = "https://github.com/embassy-rs/embassy", rev = "45cbcb513dc0bbf3e12c102df0db8c15643cc78b" } +embassy-net-ppp = { git = "https://github.com/embassy-rs/embassy", rev = "45cbcb513dc0bbf3e12c102df0db8c15643cc78b" } +embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "45cbcb513dc0bbf3e12c102df0db8c15643cc78b" } +embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "45cbcb513dc0bbf3e12c102df0db8c15643cc78b" } + +[profile.release] +lto = true +opt-level = "s" +incremental = false +codegen-units = 1 +# note: debug = true is okay - debuginfo isn't flashed to the device! +debug = true diff --git a/README.md b/README.md new file mode 100644 index 0000000..fcef290 --- /dev/null +++ b/README.md @@ -0,0 +1,44 @@ + +# Emerson GO Tracker 1.7 (TXC-TH04) # + +## Parts ## + +| Part Number | Type | Interface | +|-------------|------|-----------| +| STM32L431CCU | MCU | | +| HDC2010 | Humidity and Temperature Sensor | I2C | +| TMP112 | Temperature Sensor | I2C | +| ????? | Light Sensor | I2C | +| Quectel BG95-M5 | GSM + GNSS | UART | +| MX25L3233F | Flash | SPI | + + +| I2C Address | Device | Function | +|-------------|--------|----------| +| 0x40 | HDC2010 | Humidity and Temperature Sensor | +| 0x44 | ????? | Light Sensor? | +| 0x48 | TMP112 | Temperature Sensor | + + +## Light Sensor ## +``` + --------------- +GND - | 1 6 | - INT (PB9) +VCC - | 2 5 | - SDA +GND - | 3 4 | - SCL + --------------- +``` + +## STM32L431CCU Pinout ## + +| STM Pin | Function | Remark | +|---------|---------------|--------| +| PA1 | GSM NetKey | - | +| PA2 | UART2_TX GSM | - | +| PA3 | UART2_RX GSM | - | +| PB2 | GSM PWR Enable| - | +| PB5 | TMP112 enable | - | +| PB6 | I2C1_SCL | - | +| PB7 | I2C1_SDA | - | +| PB9 | Light INT | not verified | +| PB12 | SPI Flash CS | - | diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..30691aa --- /dev/null +++ b/build.rs @@ -0,0 +1,35 @@ +//! This build script copies the `memory.x` file from the crate root into +//! a directory where the linker can always find it at build time. +//! For many projects this is optional, as the linker always searches the +//! project root directory -- wherever `Cargo.toml` is. However, if you +//! are using a workspace or have a more complicated build setup, this +//! build script becomes required. Additionally, by requesting that +//! Cargo re-run the build script whenever `memory.x` is changed, +//! updating `memory.x` ensures a rebuild of the application with the +//! new memory settings. + +use std::env; +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; + +fn main() { + // Put `memory.x` in our output directory and ensure it's + // on the linker search path. + let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); + File::create(out.join("memory.x")) + .unwrap() + .write_all(include_bytes!("memory.x")) + .unwrap(); + println!("cargo:rustc-link-search={}", out.display()); + + // By default, Cargo will re-run a build script whenever + // any file in the project changes. By specifying `memory.x` + // here, we ensure the build script is only re-run when + // `memory.x` is changed. + println!("cargo:rerun-if-changed=memory.x"); + + println!("cargo:rustc-link-arg-bins=--nmagic"); + println!("cargo:rustc-link-arg-bins=-Tlink.x"); + println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); +} diff --git a/memory.x b/memory.x new file mode 100644 index 0000000..e395100 --- /dev/null +++ b/memory.x @@ -0,0 +1,14 @@ +MEMORY +{ + /* NOTE K = KiBi = 1024 bytes */ + FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 256K + RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 64K +} + + + + +/* This is where the call stack will be allocated. */ +/* The stack is of the full descending type. */ +/* NOTE Do NOT modify `_stack_start` unless you know what you are doing */ +_stack_start = ORIGIN(RAM) + LENGTH(RAM); diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..f139e35 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,10 @@ +# Before upgrading check that everything is available on all tier1 targets here: +# https://rust-lang.github.io/rustup-components-history +[toolchain] +channel = "stable" +components = [ "rustfmt" ] +targets = [ + "thumbv6m-none-eabi", + "thumbv7em-none-eabihf", + "riscv32imac-unknown-none-elf", +] diff --git a/src/gnss.rs b/src/gnss.rs new file mode 100644 index 0000000..4e7b02b --- /dev/null +++ b/src/gnss.rs @@ -0,0 +1,23 @@ +use defmt::info; +use embassy_stm32::{mode::Async, usart::Uart}; +use embassy_time::{Duration, Timer}; +use heapless::String; + +#[embassy_executor::task] +pub async fn gnss_task(mut gnss_uart: Uart<'static, Async>) { + let mut buf = [0; 90]; + loop { + let rx = gnss_uart.read_until_idle(&mut buf).await; + match rx { + Ok(rx) => { + let buf = core::str::from_utf8(&buf[..rx]).unwrap(); + info!("RX: {}", buf); + } + Err(_) => { + info!("Error reading from GNSS"); + } + } + // Perform some asynchronous work here + Timer::after(Duration::from_secs(1)).await; + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e3eea55 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,154 @@ +#![no_std] +#![no_main] + +use defmt::*; +use embassy_executor::Spawner; +use embassy_net::StackResources; +use embassy_net_ppp; + +use embassy_stm32::i2c::{self, I2c}; +use embassy_stm32::rng::{self, Rng}; +use embassy_stm32::time::Hertz; +use embassy_stm32::usart::Config; +use embassy_stm32::{bind_interrupts, peripherals, usart}; +use embassy_stm32::{ + gpio::{Level, Output, Speed}, + usart::BufferedUart, +}; +use embassy_time::{Duration, Timer}; + +use net::net_init; +use rand::RngCore; + +use static_cell::StaticCell; +use {defmt_rtt as _, panic_probe as _}; + +mod gnss; +mod mqtt; +mod net; +mod sensors; + +bind_interrupts!(struct Irqs { + USART2 => usart::BufferedInterruptHandler; + USART3 => usart::InterruptHandler; + RNG => rng::InterruptHandler; + I2C1_EV => i2c::EventInterruptHandler; + I2C1_ER => i2c::ErrorInterruptHandler; + +}); + +#[embassy_executor::main] +async fn main(spawner: Spawner) { + let mut stm_config = embassy_stm32::Config::default(); + { + use embassy_stm32::rcc::*; + stm_config.rcc.sys = Sysclk::PLL1_R; + stm_config.rcc.hsi = false; + stm_config.rcc.hse = Some(Hse { + freq: embassy_stm32::time::Hertz(12_000_000), + mode: HseMode::Oscillator, + }); + stm_config.rcc.pll = Some(Pll { + source: PllSource::HSE, // 12 MHz + prediv: PllPreDiv::DIV1, + + mul: PllMul::MUL8, // 12 * 8 = 96 MHz + divp: None, + divq: None, + divr: Some(PllRDiv::DIV2), // 96 / 2 = 48 MHz + }); + stm_config.rcc.ls = LsConfig::default(); + } + + let p = embassy_stm32::init(stm_config); + let mut config = Config::default(); + config.baudrate = 115_200; + config.stop_bits = embassy_stm32::usart::StopBits::STOP1; + + defmt::info!("Moin"); + let mut rng = Rng::new(p.RNG, Irqs); + let mut net_key = Output::new(p.PA1, Level::Low, Speed::Low); + let mut pwr_en = Output::new(p.PB2, Level::Low, Speed::Low); + let mut pwr_tmp = Output::new(p.PB5, Level::Low, Speed::Low); + + // Power on modem + pwr_en.set_high(); + pwr_tmp.set_high(); + + static RX_BUF: StaticCell<[u8; 512]> = StaticCell::new(); + static TX_BUF: StaticCell<[u8; 512]> = StaticCell::new(); + + let mut usart_modem = BufferedUart::new( + p.USART2, + Irqs, + p.PA3, + p.PA2, + RX_BUF.init([0; 512]), + TX_BUF.init([0; 512]), + config, + ) + .unwrap(); + + // TODO untested + let gnss_uart = usart::Uart::new( + p.USART3, + p.PB11, + p.PB10, + Irqs, + p.DMA1_CH2, + p.DMA1_CH3, + Config::default(), + ) + .unwrap(); + + let i2c = I2c::new( + p.I2C1, + p.PB6, + p.PB7, + Irqs, + p.DMA1_CH6, + p.DMA1_CH7, + Hertz(100_000), + Default::default(), + ); + + net_init(&mut net_key, &mut usart_modem).await; + + // spawner.spawn(gnss::gnss_task(gnss_uart)).unwrap(); + + static STATE: StaticCell> = StaticCell::new(); + let state = STATE.init(embassy_net_ppp::State::<8, 8>::new()); + let (device, runner) = embassy_net_ppp::new(state); + // Generate random seed + let seed = rng.next_u64(); + + // Init network stack + static RESOURCES: StaticCell> = StaticCell::new(); + let (stack, runner_net) = embassy_net::new( + device, + embassy_net::Config::default(), + RESOURCES.init(StackResources::new()), + seed, + ); + + // Launch network task + spawner.spawn(net::net_task(runner_net)).unwrap(); + spawner + .spawn(net::ppp_task(stack, runner, usart_modem)) + .unwrap(); + + loop { + Timer::after(Duration::from_secs(1)).await; + if stack.is_link_up() { + info!("Ready"); + break; + } + } + + spawner.spawn(sensors::sensors_task(i2c)).unwrap(); + spawner.spawn(mqtt::mqtt_task(stack)).unwrap(); + + loop { + Timer::after(Duration::from_secs(1)).await; + } +} diff --git a/src/mqtt.rs b/src/mqtt.rs new file mode 100644 index 0000000..1988cb9 --- /dev/null +++ b/src/mqtt.rs @@ -0,0 +1,61 @@ +use defmt::{info, warn}; +use embassy_net::Stack; + +use embassy_time::{Duration, Timer}; +use rust_mqtt::{ + client::{client::MqttClient, client_config::ClientConfig}, + utils::rng_generator::CountingRng, +}; + +#[embassy_executor::task] +pub async fn mqtt_task(stack: Stack<'static>) { + info!("Connecting..."); + let mut rx_buffer = [0; 128]; + let mut tx_buffer = [0; 128]; + loop { + let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer); + socket.set_timeout(Some(Duration::from_secs(10))); + + let host_addrs = stack + .dns_query("test.mosquitto.org", embassy_net::dns::DnsQueryType::A) + .await + .unwrap(); + let host_addr = host_addrs.iter().next().unwrap(); + + if let Err(e) = socket.connect((*host_addr, 1883)).await { + warn!("connect error: {:?}", e); + continue; + } + info!("Connected to {:?}", socket.remote_endpoint()); + + let mut config = ClientConfig::new( + rust_mqtt::client::client_config::MqttVersion::MQTTv5, + CountingRng(20000), + ); + config.add_max_subscribe_qos(rust_mqtt::packet::v5::publish_packet::QualityOfService::QoS1); + config.add_client_id("client"); + // config.add_username(USERNAME); + // config.add_password(PASSWORD); + config.max_packet_size = 100; + let mut recv_buffer = [0; 80]; + let mut write_buffer = [0; 80]; + + let mut client = + MqttClient::<_, 5, _>::new(socket, &mut write_buffer, 80, &mut recv_buffer, 80, config); + + client.connect_to_broker().await.unwrap(); + + loop { + client + .send_message( + "/malled", + b"hello2", + rust_mqtt::packet::v5::publish_packet::QualityOfService::QoS0, + true, + ) + .await + .unwrap(); + Timer::after_millis(2000).await; + } + } +} diff --git a/src/net.rs b/src/net.rs new file mode 100644 index 0000000..7bfe2ab --- /dev/null +++ b/src/net.rs @@ -0,0 +1,89 @@ +use defmt::{info, warn}; +use embassy_net::{ConfigV4, Ipv4Address, Ipv4Cidr, Stack}; +use embassy_stm32::{gpio::Output, usart::BufferedUart}; +use embassy_time::Timer; +use embedded_io::Write; +use embedded_io_async::Read; +use heapless::{String, Vec}; + +pub async fn net_init(net_key: &mut Output<'static>, usart_modem: &mut BufferedUart<'static>) { + // Enable the modem + Timer::after_millis(750).await; + net_key.set_high(); + Timer::after_millis(750).await; + net_key.set_low(); + + let mut rx_buf = String::<128>::new(); + loop { + let mut buf = [0u8; 8]; + let result = usart_modem.read(&mut buf).await; + match result { + Ok(size) => { + for i in 0..size { + rx_buf.push(buf[i] as char).unwrap(); + } + if !rx_buf.contains("\r") { + continue; + } + info!("RX: {}", rx_buf); + if rx_buf.contains("CTZV") { + // usart_modem.write_all(b"AT+QGPS=1\r").unwrap(); + usart_modem + .write_all(b"AT+CGDCONT=1,\"ip\",\"pinternet.interkom.de\"\r") + .unwrap(); + usart_modem.flush().unwrap(); + Timer::after_millis(500).await; + usart_modem.write_all(b"ATD*99#\r").unwrap(); + usart_modem.flush().unwrap(); + Timer::after_millis(500).await; + break; + } + rx_buf.clear(); + } + Err(_err) => { + //Ignore eg. framing errors + defmt::error!("Modem RX Error"); + } + } + } +} + +#[embassy_executor::task] +pub async fn net_task( + mut runner: embassy_net::Runner<'static, embassy_net_ppp::Device<'static>>, +) -> ! { + info!("Starting Ethernet"); + runner.run().await +} + +#[embassy_executor::task] +pub async fn ppp_task( + stack: Stack<'static>, + mut runner: embassy_net_ppp::Runner<'static>, + uart: BufferedUart<'static>, +) { + let cfg = embassy_net_ppp::Config { + username: "foo".as_bytes(), + password: "bar".as_bytes(), + }; + info!("Starting PPP"); + runner + .run(uart, cfg, |ipv4| { + let Some(addr) = ipv4.address else { + warn!("PPP did not provide an IP address."); + return; + }; + let mut dns_servers = Vec::new(); + for s in ipv4.dns_servers.iter().flatten() { + let _ = dns_servers.push(Ipv4Address::from_bytes(&s.0)); + } + let config = ConfigV4::Static(embassy_net::StaticConfigV4 { + address: Ipv4Cidr::new(Ipv4Address::from_bytes(&addr.0), 0), + gateway: None, + dns_servers, + }); + stack.set_config_v4(config); + }) + .await + .unwrap(); +} diff --git a/src/sensors.rs b/src/sensors.rs new file mode 100644 index 0000000..cdb2105 --- /dev/null +++ b/src/sensors.rs @@ -0,0 +1,105 @@ +use defmt::info; +use embassy_stm32::i2c::I2c; +use embassy_stm32::mode::Async; +use embassy_time::{Duration, Timer}; + +const ADDRESS_TMP112: u8 = 0x48; +const ADDRESS_HDC2010: u8 = 0x40; +const ADDRESS_LIGHT: u8 = 0x44; + +#[embassy_executor::task] +pub async fn sensors_task(mut i2c: I2c<'static, Async>) { + info!("Sensors task started"); + Timer::after(Duration::from_secs(1)).await; + loop { + info!("TMP112 {}", read_temp_tmp112(&mut i2c).await.unwrap()); + info!("HDC2010 {}", read_temp_hdc2010(&mut i2c).await.unwrap()); + info!("Light {}", read_light(&mut i2c).await.unwrap()); + Timer::after(Duration::from_secs(1)).await; + } +} + +async fn read_light(i2c: &mut I2c<'static, Async>) -> Result { + let mut data = [0u8; 2]; + // let _request = i2c.write(ADDRESS_LIGHT, &[0x7E]).await; + Timer::after(Duration::from_millis(50)).await; + for i in 1..255 { + let resp = i2c.write_read(ADDRESS_LIGHT, &[i], &mut data).await; + match resp { + Ok(_) => { + info!("Addr {:02X} Data: {:02X} {:02X}", i, data[0], data[1],); + return Ok(42); + } + Err(e) => match e { + embassy_stm32::i2c::Error::Nack => { + info!("NAK"); + Timer::after(Duration::from_millis(10)).await; + } + _ => { + info!("custom err"); + return Err(e); + } + }, + } + } + Err(embassy_stm32::i2c::Error::Timeout) +} + +async fn read_temp_hdc2010( + i2c: &mut I2c<'static, Async>, +) -> Result { + let mut data = [0u8; 4]; + // let _request = i2c.write(ADDRESS_HUM, &[0x00]).await; + Timer::after(Duration::from_millis(50)).await; + for _ in 1..10 { + let resp = i2c.write_read(ADDRESS_HDC2010, &[0xFC], &mut data).await; + match resp { + Ok(_) => { + info!( + "Data: {:02X} {:02X} {:02X} {:02X}", + data[0], data[1], data[2], data[3] + ); + return Ok(42); + } + Err(e) => match e { + embassy_stm32::i2c::Error::Nack => { + info!("NAK"); + Timer::after(Duration::from_millis(10)).await; + } + _ => { + info!("custom err"); + return Err(e); + } + }, + } + } + Err(embassy_stm32::i2c::Error::Timeout) +} + +async fn read_temp_tmp112(i2c: &mut I2c<'static, Async>) -> Result { + let mut data = [0u8; 2]; + // Timer::after(Duration::from_millis(50)).await; + for _ in 1..10 { + let resp = i2c.write_read(ADDRESS_TMP112, &[0], &mut data).await; + match resp { + Ok(_) => { + let temp = ((data[0] as u16) << 4) | (data[1] as u16) >> 4; + let temp = temp as i16; + let temp = temp as f32; + let temp = temp * 0.0625; + return Ok(temp); + } + Err(e) => match e { + embassy_stm32::i2c::Error::Nack => { + info!("NAK"); + Timer::after(Duration::from_millis(10)).await; + } + _ => { + info!("custom err"); + return Err(e); + } + }, + } + } + Err(embassy_stm32::i2c::Error::Timeout) +}