arduino / ArduinoCore-renesas

MIT License
110 stars 76 forks source link

Upload to UNO R4 WiFi fails when "1200 bps touch" causes port change #73

Open per1234 opened 1 year ago

per1234 commented 1 year ago

Describe the problem

The upload operation for the UNO R4 WiFi follows this sequence:

  1. Perform a "1200 bps touch" to put the board into the mode where it can be flashed.
  2. 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" 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 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:
    #include <HID.h>
    void setup() {}
    void loop() {}
  2. Select the appropriate board and port for the UNO R4 WiFi from the Arduino IDE menus.
  3. Select Sketch > Upload from the Arduino IDE menus.
  4. Wait for the upload to complete successfully. the upload should be successful as long as the board isn't already running a problematic sketch.
  5. Select Sketch > Upload from the Arduino IDE menus.

πŸ› The upload fails:

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
$ 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
$ 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

Operating system version

Additional context

For the sake of simplicity, I used The the low level "HID" library in the demonstration sketch, but that is not commonly used in real world sketches. However, it is common to use the "Keyboard" or "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:

#include <Keyboard.h>
void setup() {}
void loop() {}

Originally reported at https://forum.arduino.cc/t/failed-uploading-after-upload-usb-hid-example/1151778

Workaround

  1. Unplug the USB cable of the UNO R4 WiFi board from your computer.
  2. Connect the UNO R4 WiFi board to your computer with the USB cable.
  3. Press and release the button marked "RESET" on your UNO R4 WiFi board quickly twice. You should now see the "L" LED pulsing.
  4. Select the port of the UNO R4 WiFi board from Arduino IDE's Tools > Port menu. ❗ The double reset you did in step (3) can cause the port number of the board to change, so don't assume you already have the correct port selected.
  5. Upload the sketch to the board as usual.
mjs513 commented 1 year ago

@per1234 - you might want to see this and the following posts: https://github.com/arduino/ArduinoCore-renesas/issues/62#issuecomment-1653725275

per1234 commented 1 year ago

Thanks for your suggestion @mjs513. The fault still occurs when uploading to the UNO R4 WiFi board with the 0.2.1-rc1 version of the firmware on its bridge module.

mjs513 commented 1 year ago

@per1234 - thanks for the explanation - haven't run into an upload issue since installing the update but only been playing I2C libraries

per1234 commented 1 year ago

A potential fix for this problem has been identified. This involves changes to two software components:

https://github.com/arduino/ArduinoCore-renesas/pull/74 was previously rejected, but the reason for rejecting it (the introduction of a 10 s delay into uploads when the sketch is not using HID) would no longer be valid (at least for users who have the updated firmware) due to the change to the firmware. Those with the older version of the firmware would still be subject to the increased upload durations, but I think this is less problematic when a remedy for the inconvenience is readily available.

OmarK124 commented 6 months ago

For whatever reason disconnecting my Samsung Galaxy Buds Bluetooth headphones is the only solution to this issue πŸ˜ƒ