From 95ad37bcaa2496ddfd9d3030c4c3a5cdd171e5cf Mon Sep 17 00:00:00 2001 From: macronova Date: Sun, 13 Oct 2024 21:01:33 -0700 Subject: [PATCH] Connect to wireless network --- Cargo.lock | 287 ++++++++++++++++++++++++++-------------------------- Cargo.toml | 14 +-- src/main.rs | 109 +++++++++++++++----- 3 files changed, 239 insertions(+), 171 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1d8b5aa..f23b4b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,7 +65,7 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5deb64efa5bd81e31fcd1938615a6d98c82eafcbcd787162b6f63b91d6bac5b3" dependencies = [ - "rustc_version 0.2.3", + "rustc_version", ] [[package]] @@ -95,6 +95,12 @@ version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2d7e60934ceec538daadb9d8432424ed043a904d8e0243f3c6446bce549a46ac" +[[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" @@ -103,15 +109,15 @@ checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" [[package]] name = "bytemuck" -version = "1.18.0" +version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94bbb0ad554ad961ddc5da507a12a29b14e4ae5bda06b19f575a3e6079d2e2ae" +checksum = "8334215b81e418a0a7bdb8ef0849474f40bb10c8b71f1c4ed315cff49f32494d" [[package]] name = "byteorder" -version = "1.4.3" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "cfg-if" @@ -185,26 +191,24 @@ checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" [[package]] name = "cyw43" version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04c691efd1af993c7c4cb79eb13da4b901c1e8c6d148280e2a36ae2885f83218" +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" dependencies = [ "cortex-m", "cortex-m-rt", "embassy-futures", - "embassy-net-driver-channel 0.3.0", - "embassy-sync 0.6.0", + "embassy-net-driver-channel", + "embassy-sync", "embassy-time", "embedded-hal 1.0.0", "futures", - "heapless 0.8.0", + "heapless", "num_enum", ] [[package]] name = "cyw43-pio" version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22bb644457cfefdd1f0eeea7a3a708a61f9ab223aaf56b7f50589180808d302a" +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" dependencies = [ "cyw43", "embassy-rp", @@ -299,11 +303,10 @@ checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "embassy-embedded-hal" version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5794414bc20e0d750f145bc0e82366b19dd078e9e075e8331fb8dd069a1cb6a2" +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" dependencies = [ "embassy-futures", - "embassy-sync 0.6.0", + "embassy-sync", "embassy-time", "embedded-hal 0.2.7", "embedded-hal 1.0.0", @@ -316,8 +319,7 @@ dependencies = [ [[package]] name = "embassy-executor" version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09ed0e24bdd4a5f4ff1b72ee4f264b1d23e179ea71a77d984b5fd24877a2bbe1" +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" dependencies = [ "cortex-m", "critical-section", @@ -330,8 +332,7 @@ dependencies = [ [[package]] name = "embassy-executor-macros" version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4d4c0c34b32c2c653c9eecce1cefaf8539dd9a54e61deb5499254f01e2fcac2" +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" dependencies = [ "darling", "proc-macro2", @@ -342,14 +343,12 @@ dependencies = [ [[package]] name = "embassy-futures" version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f878075b9794c1e4ac788c95b728f26aa6366d32eeb10c7051389f898f7d067" +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" [[package]] name = "embassy-hal-internal" version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ef3bac31ec146321248a169e9c7b5799f1e0b3829c7a9b324cb4600a7438f59" +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" dependencies = [ "cortex-m", "critical-section", @@ -357,38 +356,40 @@ dependencies = [ ] [[package]] -name = "embassy-net-driver" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "524eb3c489760508f71360112bca70f6e53173e6fe48fc5f0efd0f5ab217751d" +name = "embassy-net" +version = "0.4.0" +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" +dependencies = [ + "document-features", + "embassy-net-driver", + "embassy-sync", + "embassy-time", + "embedded-io-async", + "embedded-nal-async", + "heapless", + "managed", + "smoltcp", +] [[package]] -name = "embassy-net-driver-channel" +name = "embassy-net-driver" version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "584ab4da7e5612efaa7d55ee76161d9549adf788eab48d49362eddbf322f9933" -dependencies = [ - "embassy-futures", - "embassy-net-driver", - "embassy-sync 0.3.0", -] +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" [[package]] name = "embassy-net-driver-channel" version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4818c32afec43e3cae234f324bad9a976c9aa7501022d26ff60a4017a1a006b7" +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" dependencies = [ "embassy-futures", "embassy-net-driver", - "embassy-sync 0.6.0", + "embassy-sync", ] [[package]] name = "embassy-rp" version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c5d0300b0ed5229bf1c25488c64c1ce55024c5153c246f378b1f2e353d5ec9a" +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" dependencies = [ "atomic-polyfill", "cfg-if", @@ -399,7 +400,7 @@ dependencies = [ "embassy-embedded-hal", "embassy-futures", "embassy-hal-internal", - "embassy-sync 0.6.0", + "embassy-sync", "embassy-time", "embassy-time-driver", "embassy-usb-driver", @@ -418,51 +419,26 @@ dependencies = [ "rand_core", "rp-pac", "rp2040-boot2", -] - -[[package]] -name = "embassy-sync" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0525b466ca3ace30b57f2db868a35215dfaecd038d8668cb2db03feb7c069a0" -dependencies = [ - "cfg-if", - "critical-section", - "futures-util", - "heapless 0.7.17", -] - -[[package]] -name = "embassy-sync" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd938f25c0798db4280fcd8026bf4c2f48789aebf8f77b6e5cf8a7693ba114ec" -dependencies = [ - "cfg-if", - "critical-section", - "embedded-io-async", - "futures-util", - "heapless 0.8.0", + "sha2-const-stable", + "smart-leds", ] [[package]] name = "embassy-sync" version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3e0c49ff02ebe324faf3a8653ba91582e2d0a7fdef5bc88f449d5aa1bfcc05c" +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" dependencies = [ "cfg-if", "critical-section", "embedded-io-async", "futures-util", - "heapless 0.8.0", + "heapless", ] [[package]] name = "embassy-time" version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "158080d48f824fad101d7b2fae2d83ac39e3f7a6fa01811034f7ab8ffc6e7309" +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" dependencies = [ "cfg-if", "critical-section", @@ -473,14 +449,13 @@ dependencies = [ "embedded-hal 1.0.0", "embedded-hal-async", "futures-util", - "heapless 0.8.0", + "heapless", ] [[package]] name = "embassy-time-driver" version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e0c214077aaa9206958b16411c157961fb7990d4ea628120a78d1a5a28aed24" +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" dependencies = [ "document-features", ] @@ -488,20 +463,18 @@ dependencies = [ [[package]] name = "embassy-time-queue-driver" version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1177859559ebf42cd24ae7ba8fe6ee707489b01d0bf471f8827b7b12dcb0bc0" +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" [[package]] name = "embassy-usb" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66444f442f4efc155138a996e250821a6d83a0c40e28ce3de8f0d9033617838b" +version = "0.3.0" +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" dependencies = [ "embassy-futures", - "embassy-net-driver-channel 0.2.0", - "embassy-sync 0.5.0", + "embassy-net-driver-channel", + "embassy-sync", "embassy-usb-driver", - "heapless 0.8.0", + "heapless", "ssmarshal", "usbd-hid", ] @@ -509,17 +482,15 @@ dependencies = [ [[package]] name = "embassy-usb-driver" version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fc247028eae04174b6635104a35b1ed336aabef4654f5e87a8f32327d231970" +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" [[package]] name = "embassy-usb-logger" version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffefdf915abf7230f2e44753b552e60895540e4931fc9a743bb9b1c7dca242f7" +source = "git+https://github.com/embassy-rs/embassy.git#c84495ef2eb99580fea5392b2b3aff5ad66043a0" dependencies = [ "embassy-futures", - "embassy-sync 0.5.0", + "embassy-sync", "embassy-usb", "log", ] @@ -574,6 +545,25 @@ dependencies = [ "embedded-io", ] +[[package]] +name = "embedded-nal" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c56a28be191a992f28f178ec338a0bf02f63d7803244add736d026a471e6ed77" +dependencies = [ + "nb 1.1.0", +] + +[[package]] +name = "embedded-nal-async" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76959917cd2b86f40a98c28dd5624eddd1fa69d746241c8257eac428d83cb211" +dependencies = [ + "embedded-io-async", + "embedded-nal", +] + [[package]] name = "embedded-storage" version = "0.3.1" @@ -728,15 +718,6 @@ dependencies = [ "crunchy", ] -[[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" @@ -761,26 +742,13 @@ version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" -[[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 = [ - "hash32 0.3.1", + "hash32", "stable_deref_trait", ] @@ -875,7 +843,7 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" dependencies = [ - "bitflags", + "bitflags 2.6.0", "libc", ] @@ -901,6 +869,12 @@ version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +[[package]] +name = "managed" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ca88d725a0a943b096803bd34e73a4437208b6077654cc4ecb2947a5f91618d" + [[package]] name = "memchr" version = "2.7.4" @@ -1025,12 +999,14 @@ dependencies = [ "cyw43", "cyw43-pio", "embassy-executor", + "embassy-net", "embassy-rp", "embassy-time", "embassy-usb-logger", "log", "panic-halt", "portable-atomic", + "rand", "serde", "serde_yaml", "static_cell", @@ -1145,6 +1121,15 @@ 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" @@ -1157,7 +1142,7 @@ version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" dependencies = [ - "bitflags", + "bitflags 2.6.0", ] [[package]] @@ -1206,11 +1191,19 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "rgb" +version = "0.8.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57397d16646700483b67d2dd6511d79318f9d057fdbd21a4066aeac8b41d310a" +dependencies = [ + "bytemuck", +] + [[package]] name = "rp-pac" version = "6.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f30f6c4c846269293db805e9c77864ff7b923395b480550df44f0868e3765337" +source = "git+https://github.com/embassy-rs/rp-pac.git?rev=a7f42d25517f7124ad3b4ed492dec8b0f50a0e6c#a7f42d25517f7124ad3b4ed492dec8b0f50a0e6c" dependencies = [ "cortex-m", "cortex-m-rt", @@ -1231,16 +1224,7 @@ 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", + "semver", ] [[package]] @@ -1270,12 +1254,6 @@ 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" @@ -1315,6 +1293,12 @@ dependencies = [ "unsafe-libyaml", ] +[[package]] +name = "sha2-const-stable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f179d4e11094a893b82fff208f74d448a7512f99f5a0acbd5c679b705f83ed9" + [[package]] name = "siphasher" version = "0.3.11" @@ -1328,12 +1312,33 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] -name = "spin" -version = "0.9.8" +name = "smart-leds" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +checksum = "66df34e571fa9993fa6f99131a374d58ca3d694b75f9baac93458fe0d6057bf0" dependencies = [ - "lock_api", + "smart-leds-trait", +] + +[[package]] +name = "smart-leds-trait" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bc64ee02bbbf469603016df746c0ed224f263280b6ebb49b7ebadbff375c572" +dependencies = [ + "rgb", +] + +[[package]] +name = "smoltcp" +version = "0.11.0" +source = "git+https://github.com/smoltcp-rs/smoltcp?rev=b65e1b64dc9b66fa984a2ad34e90685cb0b606de#b65e1b64dc9b66fa984a2ad34e90685cb0b606de" +dependencies = [ + "bitflags 1.3.2", + "byteorder", + "cfg-if", + "heapless", + "managed", ] [[package]] @@ -1487,15 +1492,15 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98816b1accafbb09085168b90f27e93d790b4bfa19d883466b5e53315b5f06a6" dependencies = [ - "heapless 0.8.0", + "heapless", "portable-atomic", ] [[package]] name = "usbd-hid" -version = "0.7.1" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e49faaa950e4f8bf93fff4bb4355722b8188b019541fb0aab4f10d27eb2f747" +checksum = "e6f291ab53d428685cc780f08a2eb9d5d6ff58622db2b36e239a4f715f1e184c" dependencies = [ "serde", "ssmarshal", @@ -1505,18 +1510,18 @@ dependencies = [ [[package]] name = "usbd-hid-descriptors" -version = "0.7.1" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "425346576e9c7e6e6436323eb0d257692b671cc37134c8b482fbe10258ac1dcb" +checksum = "0eee54712c5d778d2fb2da43b1ce5a7b5060886ef7b09891baeb4bf36910a3ed" dependencies = [ "bitfield 0.14.0", ] [[package]] name = "usbd-hid-macros" -version = "0.7.1" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15d4b4288f034e48d96b3f662988774a5b3eb4308e42f515162c1f0ab9212ee8" +checksum = "bb573c76e7884035ac5e1ab4a81234c187a82b6100140af0ab45757650ccda38" dependencies = [ "byteorder", "hashbrown 0.13.2", diff --git a/Cargo.toml b/Cargo.toml index 092c9cb..a5791ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,15 +10,17 @@ serde_yaml = "*" [dependencies] cortex-m-rt = "*" -cyw43 = "*" -cyw43-pio = "*" -embassy-executor = { version = "*", features = ["arch-cortex-m", "executor-interrupt", "executor-thread", "integrated-timers", "task-arena-size-98304"] } -embassy-rp = { version = "*", features = ["critical-section-impl", "time-driver"] } -embassy-time = "*" -embassy-usb-logger = "*" +cyw43 = { git = "https://github.com/embassy-rs/embassy.git" } +cyw43-pio = { git = "https://github.com/embassy-rs/embassy.git" } +embassy-executor = { git = "https://github.com/embassy-rs/embassy.git", features = ["arch-cortex-m", "executor-interrupt", "executor-thread", "integrated-timers", "nightly"] } +embassy-net = { git = "https://github.com/embassy-rs/embassy.git", features = ["tcp", "udp", "raw", "dhcpv4", "dhcpv4-hostname", "dns", "proto-ipv4", "multicast"] } +embassy-rp = { git = "https://github.com/embassy-rs/embassy.git", features = ["critical-section-impl", "time-driver", "rp2040"] } +embassy-time = { git = "https://github.com/embassy-rs/embassy.git" } +embassy-usb-logger = { git = "https://github.com/embassy-rs/embassy.git" } log ="*" panic-halt = "*" portable-atomic = { version = "*", features = ["critical-section"] } +rand = { version = "*", default-features = false } static_cell = "*" [profile.release] diff --git a/src/main.rs b/src/main.rs index 0630ce6..3099e2f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,16 @@ +#![feature(impl_trait_in_assoc_type)] #![no_std] #![no_main] extern crate panic_halt; + +use cyw43::JoinOptions; use cyw43_pio::PioSpi; use embassy_executor::Spawner; +use embassy_net::{Config, DhcpConfig, StackResources}; use embassy_rp::{ bind_interrupts, + clocks::RoscRng, gpio::{Level, Output}, peripherals::{DMA_CH0, PIO0, USB}, pio::{self, Pio}, @@ -13,41 +18,51 @@ use embassy_rp::{ }; use embassy_time::Timer; use log::info; +use rand::RngCore; use static_cell::StaticCell; -const WIFI_CREDENTIALS: &[(&str, &str)] = +const HOSTNAME: &str = "picow"; +const WIRELESS_CREDENTIALS: &[(&str, &str)] = &include!(concat!(env!("OUT_DIR"), "/wireless-credentials.rs")); -// bind interrupt request to handler +const WIRELESS_SCAN_PERIOD_SECOND: u64 = 7; + bind_interrupts!(struct Irqs { PIO0_IRQ_0 => pio::InterruptHandler; USBCTRL_IRQ => usb::InterruptHandler; }); -// logger task #[embassy_executor::task] -async fn logger_task(driver: Driver<'static, USB>) { - embassy_usb_logger::run!(1024, log::LevelFilter::Info, driver); +async fn logger_task(usb_driver: Driver<'static, USB>) -> ! { + embassy_usb_logger::run!(1024, log::LevelFilter::Info, usb_driver); } -// wireless task #[embassy_executor::task] -async fn cyw43_task( - runner: cyw43::Runner<'static, Output<'static>, PioSpi<'static, PIO0, 0, DMA_CH0>>, +async fn network_task( + mut network_runner: embassy_net::Runner<'static, cyw43::NetDriver<'static>>, ) -> ! { - runner.run().await + network_runner.run().await; +} + +#[embassy_executor::task] +async fn wireless_task( + wireless_runner: cyw43::Runner<'static, Output<'static>, PioSpi<'static, PIO0, 0, DMA_CH0>>, +) -> ! { + wireless_runner.run().await; } #[embassy_executor::main] -async fn main(spawner: Spawner) { +async fn main(spawner: Spawner) -> ! { // hardware abstraction layer let hal = embassy_rp::init(Default::default()); // usb driver - let driver = Driver::new(hal.USB, Irqs); + let usb_driver = Driver::new(hal.USB, Irqs); // spawn logger task - spawner.spawn(logger_task(driver)).unwrap(); + spawner + .spawn(logger_task(usb_driver)) + .expect("Logger task should not fail to spawn"); // wireless firmware binary let fw = include_bytes!("../firmware/43439A0.bin"); @@ -80,23 +95,69 @@ async fn main(spawner: Spawner) { let state = STATE.init(cyw43::State::new()); // spawn wireless task - let (_, mut control, runner) = cyw43::new(state, pwr, spi, fw).await; - spawner.spawn(cyw43_task(runner)).unwrap(); + let (network_device, mut wireless_control, wireless_runner) = + cyw43::new(state, pwr, spi, fw).await; + spawner + .spawn(wireless_task(wireless_runner)) + .expect("Wireless task should not fail to spawn"); - // initialize wireless - control.init(clm).await; - control + // initialize wireless control + wireless_control.init(clm).await; + wireless_control .set_power_management(cyw43::PowerManagementMode::PowerSave) .await; - // time to blink - let mut led = false; - loop { - for (k, v) in WIFI_CREDENTIALS { - info!("{k}: {v}"); + // set hostname + let mut dhcpv4_config = DhcpConfig::default(); + dhcpv4_config.hostname = HOSTNAME.try_into().ok(); + + // setup network stack + static RESOURCES: StaticCell> = StaticCell::new(); + let (network_stack, network_runner) = embassy_net::new( + network_device, + Config::dhcpv4(dhcpv4_config), + RESOURCES.init(StackResources::new()), + RoscRng.next_u64(), + ); + + // spawn network task + spawner + .spawn(network_task(network_runner)) + .expect("Network task should not fail to spawn"); + + // search and join wireless network + for (ssid, password) in WIRELESS_CREDENTIALS.iter().cycle() { + info!("Searching for wireless network: {ssid}"); + if let Ok(_) = wireless_control + .join(ssid, JoinOptions::new(password.as_bytes())) + .await + { + info!("Successfully joined wireless network: {ssid}"); + break; + } else if ssid + == &WIRELESS_CREDENTIALS + .last() + .expect("The list of wireless credentials should not be empty") + .0 + { + info!("Unable to join any wireless network with provided credentials"); + info!("Sleep for {WIRELESS_SCAN_PERIOD_SECOND} seconds before rescan"); + Timer::after_secs(WIRELESS_SCAN_PERIOD_SECOND).await; } - led = !led; - control.gpio_set(0, led).await; + } + + // wait for network stack + info!("Wait for network setup"); + network_stack.wait_config_up().await; + network_stack.wait_link_up().await; + info!("Successfully setup network stack"); + info!("Network configuration: {:?}", network_stack.config_v4()); + + // turn on led to indicate network connection + wireless_control.gpio_set(0, true).await; + + loop { + info!("System is idle"); Timer::after_secs(1).await; } }