### Describe the problem
The upload operation for the [**UNO R4 WiFi**](https…://docs.arduino.cc/hardware/uno-r4-wifi) follows this sequence:
1. Perform a ["1200 bps touch"](https://arduino.github.io/arduino-cli/dev/platform-specification/#1200-bps-bootloader-reset) to put the board into the mode where it can be flashed.
1. Invoke the `bossac` command that flashes the binary to the board.
It is common for the address of the board port to change after the "1200 bps touch" step and so the Arduino development tools have a system for detecting the post-touch address and using that address when the upload command is generated from [the "pattern"](https://arduino.github.io/arduino-cli/dev/platform-specification/#tools) defined for the board in `platform.txt`. That system involves a post-touch wait to watch for the new port to appear before eventually timing out and resorting to the fallback behavior of using the original address to the upload pattern if no address change was detected. Since some boards will never produce a post-touch address change (meaning the post-touch wait step would only cause an unnecessary delay in the upload operation) it is possible to configure the board to skip the wait step by setting [the `upload.wait_for_upload_port` property](https://arduino.github.io/arduino-cli/dev/platform-specification/#1200-bps-bootloader-reset) to `false` in the board definition.
The assumption was made that the address of the **UNO R4 WiFi** board's port will never experience a post-touch address change, so its `upload.wait_for_upload_port` property is set to `false`. However, the address can change under certain conditions and this causes the `bossac` command invocation to fail with a "`No device found on ...`" error because the original port address is used in the generated command instead of the post-touch address.
### To reproduce
#### Arduino IDE
1. Create a sketch with the following code:
```cpp
#include <HID.h>
void setup() {}
void loop() {}
```
1. Select the appropriate board and port for the **UNO R4 WiFi** from the Arduino IDE menus.
1. Select **Sketch > Upload** from the Arduino IDE menus.
1. Wait for the upload to complete successfully.
the upload should be successful as long as the board isn't already running a problematic sketch.
1. Select **Sketch > Upload** from the Arduino IDE menus.
🐛 The upload fails:
```text
Performing 1200-bps touch reset on serial port COM12
No device found on COM12
"C:\Users\per\AppData\Local\Arduino15\packages\arduino\tools\bossac\1.9.1-arduino5/bossac" -d --port=COM12 -U -e -w "C:\Users\per\AppData\Local\Temp\arduino\sketches\6CF073A8170159518E82E009B3D511D0/sketch_jul27a.ino.bin" -R
Failed uploading: uploading error: exit status 1
```
#### Arduino CLI
##### Setup
```text
$ arduino-cli version
arduino-cli.exe Version: git-snapshot Commit: f66becdf Date: 2023-07-23T20:34:26Z
$ mkdir /tmp/HIDSketch
$ printf "#include <HID.h>\nvoid setup() {}\nvoid loop() {}\n" > "/tmp/HIDSketch/HIDSketch.ino"
$ arduino-cli compile --fqbn arduino:renesas_uno:unor4wifi /tmp/HIDSketch
[...]
Used library Version Path
HID C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.2\libraries\HID
Used platform Version Path
arduino:renesas_uno 1.0.2 C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\renesas_uno\1.0.2
$ arduino-cli board list
Port Protocol Type Board Name FQBN Core
COM42 serial Serial Port (USB) Arduino UNO R4 WiFi arduino:renesas_uno:unor4wifi arduino:renesas_uno
$ arduino-cli upload --fqbn arduino:renesas_uno:unor4wifi --port COM42 --verbose /tmp/HIDSketch # Upload of the sketch is successful as long as the board is not already running a problematic sketch
Performing 1200-bps touch reset on serial port COM42
"C:\Users\per\AppData\Local\Arduino15\packages\arduino\tools\bossac\1.9.1-arduino5/bossac" -d --port=COM42 -U -e -w "C:\Users\per\AppData\Local\Temp\arduino\sketches\1F72A0DD582A60EAC64D21C07F93BFD9/HIDSketch.ino.bin" -R
Set binary mode
version()=Arduino Bootloader (SAM-BA extended) 2.0 [Arduino:IKXYZ]
Connected at 921600 baud
identifyChip()=nRF52840-QIAA
write(addr=0,size=0x34)
writeWord(addr=0x30,value=0x400)
writeWord(addr=0x20,value=0)
Erase flash
chipErase(addr=0)
Done in 0.001 seconds
Write 34604 bytes to flash (9 pages)
[ ] 0% (0/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0, size=0x1000)
[=== ] 11% (1/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x1000, size=0x1000)
[====== ] 22% (2/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x2000, size=0x1000)
[========== ] 33% (3/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x3000, size=0x1000)
[============= ] 44% (4/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x4000, size=0x1000)
[================ ] 55% (5/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x5000, size=0x1000)
[==================== ] 66% (6/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x6000, size=0x1000)
[======================= ] 77% (7/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x7000, size=0x1000)
[========================== ] 88% (8/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x8000, size=0x1000)
[==============================] 100% (9/9 pages)
Done in 2.238 seconds
reset()
```
##### Demo
```text
$ arduino-cli board list # The HID sketch causes the port address to change
Port Protocol Type Board Name FQBN Core
COM12 serial Serial Port (USB) Arduino UNO R4 WiFi arduino:renesas_uno:unor4wifi arduino:renesas_uno
$ arduino-cli upload --fqbn arduino:renesas_uno:unor4wifi --port COM12 --verbose /tmp/HIDSketch # Touch step fails and the bossac command runs with the wrong port
Performing 1200-bps touch reset on serial port COM12
Cannot perform port reset: TOUCH: error during reset: opening port at 1200bps: Invalid serial port
"C:\Users\per\AppData\Local\Arduino15\packages\arduino\tools\bossac\1.9.1-arduino5/bossac" -d --port=COM12 -U -e -w "C:\Users\per\AppData\Local\Temp\arduino\sketches\1F72A0DD582A60EAC64D21C07F93BFD9/HIDSketch.ino.bin" -R
No device found on COM12
Failed uploading: uploading error: exit status 1
$ arduino-cli board list # Even though the touch process failed, it did put the board into bootloader mode
Port Protocol Type Board Name FQBN Core
COM42 serial Serial Port (USB) Arduino UNO R4 WiFi arduino:renesas_uno:unor4wifi arduino:renesas_uno
$ arduino-cli upload --fqbn arduino:renesas_uno:unor4wifi --port COM42 /tmp/HIDSketch # The next upload succeeds because the board is in bootloader mode
Performing 1200-bps touch reset on serial port COM42
"C:\Users\per\AppData\Local\Arduino15\packages\arduino\tools\bossac\1.9.1-arduino5/bossac" -d --port=COM42 -U -e -w "C:\Users\per\AppData\Local\Temp\arduino\sketches\1F72A0DD582A60EAC64D21C07F93BFD9/HIDSketch.ino.bin" -R
Set binary mode
version()=Arduino Bootloader (SAM-BA extended) 2.0 [Arduino:IKXYZ]
Connected at 921600 baud
identifyChip()=nRF52840-QIAA
write(addr=0,size=0x34)
writeWord(addr=0x30,value=0x400)
writeWord(addr=0x20,value=0)
Erase flash
chipErase(addr=0)
Done in 0.001 seconds
Write 34604 bytes to flash (9 pages)
[ ] 0% (0/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0, size=0x1000)
[=== ] 11% (1/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x1000, size=0x1000)
[====== ] 22% (2/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x2000, size=0x1000)
[========== ] 33% (3/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x3000, size=0x1000)
[============= ] 44% (4/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x4000, size=0x1000)
[================ ] 55% (5/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x5000, size=0x1000)
[==================== ] 66% (6/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x6000, size=0x1000)
[======================= ] 77% (7/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x7000, size=0x1000)
[========================== ] 88% (8/9 pages)write(addr=0x34,size=0x1000)
writeBuffer(scr_addr=0x34, dst_addr=0x8000, size=0x1000)
[==============================] 100% (9/9 pages)
Done in 2.238 seconds
reset()
```
### Expected behavior
Correct port address is always used in the upload command.
### Project version
e5ab69859d70dd7f4e6ed7769b027fc371cd613c
### Operating system
- Windows
- Linux
- macOS
### Operating system version
- Windows 11
- Ubuntu 22.04
- macOS Ventura
### Additional context
For the sake of simplicity, I used The [the low level "**HID**" library](https://github.com/arduino/ArduinoCore-renesas/tree/main/libraries/HID) in the demonstration sketch, but that is not commonly used in real world sketches. However, it is common to use the "[**Keyboard**](https://www.arduino.cc/reference/en/language/functions/usb/keyboard/)" or "[**Mouse**](https://www.arduino.cc/reference/en/language/functions/usb/mouse/)" libraries, which have a dependency on the "**HID**" library and are documented as supported for use with the **UNO R4 WiFi**:
https://docs.arduino.cc/tutorials/uno-r4-wifi/usb-hid
The fault will also occur when using either of those libraries in a sketch:
```cpp
#include <Keyboard.h>
void setup() {}
void loop() {}
```
---
Originally reported at https://forum.arduino.cc/t/failed-uploading-after-upload-usb-hid-example/1151778
<a name="workaround"></a>
#### Workaround
1. Connect your Arduino board to your computer with the USB cable.
1. Press and release the button marked "**RESET**" on your Arduino UNO R4 WiFi board quickly twice.
You should now see the "**L**" LED pulsing.
1. Check the port of the **UNO R4 WiFi** board.
❗ The double reset you did in step (1) can cause the port number of the board to change, so don't assume you already have the correct port selected.
1. Upload the sketch to the board as usual.