I have a clone of Arduino Leonardo with ATmega32U4 and I’m using the newest version of PlatformIO and VS Code on Ubuntu 18.04.5. Everything worked just fine until I started using LowPower library with a line like this:
LowPower.powerDown(SLEEP_4S, ADC_OFF, BOD_OFF);
It seems that the board is now stuck in some low power state and uploads fail with messages like this one:
Configuring upload protocol...
CURRENT: upload_protocol = avr109
Looking for upload port...
Error: Please specify `upload_port` for environment or use global `--upload-port` option.
For some development platforms it can be a USB flash drive (i.e. /media/<user>/<device name>)
*** [upload] Explicit exit, status 1
======================================================= [FAILED] Took 0.33 seconds =======================================================
The terminal process "platformio 'run', '--target', 'upload'" terminated with exit code: 1.
Cycling the power on Leonardo, USB port or the PC doesn’t help. I took another new board out of the box with the same result: everything was fine until I used LowPower. I tried double-clicking reset button and holding it until upload starts, but none of it helped.
I didn’t have an ISP programmer handy, so before I went down the road of uploading a new bootloader, I tried something else. The bootloader on these boards seems to be working just fine. Double-clicking the reset button produces this log:
paul@desktop:~$ dmesg | grep -i USB
[46950.337876] usb 1-1: new full-speed USB device number 57 using xhci_hcd
[46950.487849] usb 1-1: New USB device found, idVendor=2341, idProduct=0036, bcdDevice= 0.01
[46950.487850] usb 1-1: New USB device strings: Mfr=2, Product=1, SerialNumber=0
[46950.487851] usb 1-1: Product: Arduino Leonardo
[46950.487852] usb 1-1: Manufacturer: Arduino LLC
[46950.488979] cdc_acm 1-1:1.0: ttyACM0: USB ACM device
[46958.085528] usb 1-1: USB disconnect, device number 57
[46958.390446] usb 1-1: new full-speed USB device number 58 using xhci_hcd
[46958.518460] usb 1-1: device descriptor read/64, error -71
[46958.754473] usb 1-1: device descriptor read/64, error -71
[46958.990506] usb 1-1: new full-speed USB device number 59 using xhci_hcd
[46959.122502] usb 1-1: device descriptor read/64, error -71
[46959.362508] usb 1-1: device descriptor read/64, error -71
[46959.470533] usb usb1-port1: attempt power cycle
[46959.882541] usb 1-1: new full-speed USB device number 60 using xhci_hcd
[46959.882741] usb 1-1: Device not responding to setup address.
[46960.090750] usb 1-1: Device not responding to setup address.
[46960.298424] usb 1-1: device not accepting address 60, error -71
[46960.426571] usb 1-1: new full-speed USB device number 61 using xhci_hcd
[46960.426780] usb 1-1: Device not responding to setup address.
[46960.634773] usb 1-1: Device not responding to setup address.
This and LEDs on the board indicate that USB is available for over 7s before it disconnects. The problem is with forced USB reset by PlatformIO (averdude?) during the upload:
Forcing reset using 1200bps open/close on port /dev/ttyACM0
Waiting for the new upload port...
My recovery strategy was:
Double-click board reset
When Forcing reset appears, double-click board reset again
I recovered all 3 bricked boards on the first or the second try.
Of course, I added a delay in setup() to make my life easier in the future.
Is forced USB reset really necessary during the upload process? If not, can it be disabled somehow?