Repository: quartiq/stabilizer (language: rust)

Issue:
USB device not detected on Windows
Hello,

I've just tried setting up a stabiliser using the docs just released, configuring the MQTT broker using pyserial running on a Windows PC. I've managed to flash the firmware on the device alright.

However, when I plug the USB cable in to set the MQTT broker, Windows gives these error messages:
![Error1](https://github.com/quartiq/stabilizer/assets/36235630/acfa9ef1-6559-4623-8de3-19b03e536b3d)
![Error2](https://github.com/quartiq/stabilizer/assets/36235630/ff23d7a2-1b07-4fbb-89d5-eb1e10936cd7)


Also, when I launch pyserial, it doesn't really show up in the COM ports (as far as I can tell).

I wonder if there's some driver to install or similar to get this to work? The documentation doesn't say much about this.


Discussion / hints:
Potentially https://github.com/rust-embedded-community/usb-device/pull/129
It'll need trickling down the dependency chain.
Alternatively set the broker per compilation option as in previous releases.
This is indeed the above issue. We're working on getting it patched up through the dependency chain and will likely take a few days.
Ah amazing, many thanks for letting me know. Perhaps just comment here once that has been done.
Try again with #813 
I've just cloned the repo again from scratch, compiled and flashed the firmware, but unfortunately I'm still getting the same errors.

When I run pyserial, I just get this:
![Error](https://github.com/quartiq/stabilizer/assets/36235630/b54372fb-7131-40bb-8ad8-4fb1ed0d16aa)

It's the same whichever port I select - it just hangs there and doesn't do anything.
Yeah, nothing will show up by default, you have to press `Enter` to get the menu currently. I should mention this in the docs.
I'm afraid nothing happens when I press Enter (whichever port I choose).

I still get these errors mentioned earlier regarding Windows saying 'USB device not recognised', so I'm not even sure either of these ports is the right one.
> Potentially [rust-embedded-community/usb-device#129](https://github.com/rust-embedded-community/usb-device/pull/129) It'll need trickling down the dependency chain. Alternatively set the broker per compilation option as in previous releases.

Also, I've just tried doing this again, but this somehow also doesn't work for me any longer (it worked with previous versions) - I need to hardcode the MQTT broker address in `flash.rs`.
Hello - I wonder if you were able to have a look at this?
Unfortunately no. It works fine on my machine and in the past, I have required detailed USB logs to understand why the USB enumeration fails in the past.

I can look into getting that implemented to help here, but this is not a trivial issue to solve currently
@juhaszp95 Can you please provide all of the steps that you are using to build and then flash the device? I.e. provide all the command line steps that you run _exactly_. That may enlighten us to a potential issue.
The commands I run mirror exactly what is written at https://quartiq.de/stabilizer/setup.html.

At the beginning, the device is powered off and disconnected from the PC. The commands I run, in this order:
- `rustup update`
- `rustup target add thumbv7em-none-eabihf`
- `cargo install cargo-binutils`
- `rustup component add llvm-tools-preview`

These commands run without any error, saying that these components have already been installed (given I installed them previously when I flashed the stabiliser on previous occasions).

I then run:
- `git clone https://github.com/quartiq/stabilizer`
- `cd stabilizer`
- `cargo build --release`
- `cargo objcopy --release --bin dual-iir -- -O binary dual-iir.bin`

These commands finish OK. I then connect the stabiliser using an ST-Link probe and power it up. I then run `cargo install probe-run` which exits OK saying it's already installed. I then run `cargo run --release --bin dual-iir`, which says:
![image](https://github.com/quartiq/stabilizer/assets/36235630/687e94d4-d82f-424a-a717-3d1e29dc86b5)

It seems to flash OK and get the network, but no MQTT server (as you'd expect - nothing has been set at this point). As the program hangs in here, I press `Ctrl+C` to terminate it, upon which it produces this log:
![image](https://github.com/quartiq/stabilizer/assets/36235630/a34cb7cc-dc83-42b6-ac3c-8566854840c2)

I then power off the device and then unconnect the probe. I connect the USB cable, then power the device back on at which point Windows gives me the warning 'USB device not recognised'. After this I try to run `python -m serial`, but the port doesn't show up.

Instead of this interface, I also tried to use the previous build method, where after `cd stabilizer` I run:
- `$env:BROKER='10.255.6.4'; cargo build --release`
- `$env:BROKER='10.255.6.4'; cargo objcopy --release --bin dual-iir -- -O binary dual-iir.bin`

I then plug the device in, power it on and run `$env:BROKER='10.255.6.4'; cargo run --release --bin dual-iir`. It flashes OK, gets on the network, but still no MQTT connection - as if the BROKER variable didn't have any effect on the build process.

However, if I hardcode the MQTT address in `src/hardware/flash.rs` (line 37), and I run the commands given in the manual at the moment (i.e. no env variable), it connects to MQTT alright:
![image](https://github.com/quartiq/stabilizer/assets/36235630/27c243d2-eae9-47c0-9c57-2b505cef4cb4)

This is the only way I can get it to work at the moment.
Hmm this is indeed suspicious - thanks for the detailed list of commands, it was helpful in ruling out some potential causes of this issue.

I suspect this is an issue with the usb-device stack, as I've already had to fix it a few times for various QUARTIQ projects.

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9120037fd..1f3bef52a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,6 +22,9 @@ console.
   See also the `iir_coefficients` Python CLI implementation.
 * Bumped MSRV 1.66.0 -> 1.66.1
 
+### Fixed
+* Fixed an issue where the device would rarely not enumerate on Windows
+
 ## [0.9.0](https://github.com/quartiq/stabilizer/compare/v0.8.1...v0.9.0)
 
 ### Fixed
diff --git a/Cargo.lock b/Cargo.lock
index 16620880b..b0d3d5713 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -715,15 +715,6 @@ dependencies = [
  "num_enum_derive 0.5.11",
 ]
 
-[[package]]
-name = "num_enum"
-version = "0.6.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7a015b430d3c108a207fd776d2e2196aaf8b1cf8cf93253e3a097ff3085076a1"
-dependencies = [
- "num_enum_derive 0.6.1",
-]
-
 [[package]]
 name = "num_enum"
 version = "0.7.2"
@@ -744,17 +735,6 @@ dependencies = [
  "syn 1.0.109",
 ]
 
-[[package]]
-name = "num_enum_derive"
-version = "0.6.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "96667db765a921f7b295ffee8b60472b686a51d4f21c2ee4ffdb94c7013b65a6"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn 2.0.48",
-]
-
 [[package]]
 name = "num_enum_derive"
 version = "0.7.2"
@@ -1295,11 +1275,9 @@ dependencies = [
 [[package]]
 name = "usb-device"
 version = "0.3.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e73e438f527e567fb3982f2370967821fab4f5aea84c42e218a211dd2002b6a2"
+source = "git+https://github.com/rust-embedded-community/usb-device#2dbba84d71797bc12d964b1b936a7c464777c864"
 dependencies = [
- "heapless 0.7.17",
- "num_enum 0.6.1",
+ "heapless 0.8.0",
  "portable-atomic",
 ]
 
diff --git a/Cargo.toml b/Cargo.toml
index f7e29d4f9..fb47d8b90 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -76,6 +76,9 @@ bbqueue = "0.5"
 postcard = "1"
 bit_field = "0.10.2"
 
+[patch.crates-io.usb-device]
+git = "https://github.com/rust-embedded-community/usb-device"
+
 [build-dependencies]
 built = { version = "0.7", features = ["git2"], default-features = false }
 


Test patch: