Unity test fails with 'Error: Please specify `upload_port`

I am running Ubuntu 22, Visual Studio and my target device is a ‘mkr wifi 1010’.

I have written a simple Unity test that passes in native mode.

However, when I try run the test on the target device it fails with the following message.

TimeoutError: Could not automatically find serial port for the `Arduino MKR WiFi 1010` board based on the declared HWIDs=['2341:8054', '2341:0054']
TimeoutError: Could not automatically find serial port based on the known UART bridges
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
Uploading stage has failed, see errors above. Use `pio test -vvv` option to enable verbose output

I can see from the that the code has been uploaded to the device successfully
My platformio settings are

[env:mkrwifi1010]
platform = atmelsam
board = mkrwifi1010
test_port = /dev/ttyACM0
framework = arduino
lib_deps = 
	kmackay/micro-ecc@^1.0.0

and I have tried removing test_port line.

The ‘calculator’ example project works ok.

For these dev boards that don’t have a permanent-on USB-to-UART chip on them, you have to give PlatformIO a little bit of delay before it tries to find the serial port.

See

The link you supplied took to a post with a solution and a link to a further solution.
I have tired both, neither work!

Solution 1

extra_scripts = extra_script.py

Import("env")

def after_upload(source, target, env):
    print("Delay while uploading...")
    import time
    time.sleep(5)
    print("Done!")

env.AddPostAction("upload", after_upload)

Log contains

writeWord(addr=0xe000ed0c,value=0x5fa0004)
after_upload([“upload”], [“.pio/build/mkrwifi1010/firmware.bin”])
Delay while uploading…
Done!

Testing…
If you don’t see any output for the first 10 secs, please reset board (press reset button)

[Errno 2] could not open port /dev/ttyACM0: [Errno 2] No such file or directory: ‘/dev/ttyACM0’

Solution 2

extra_scripts = post:extra_script2.py

import time
Import("env")
if "test" in env.GetBuildType():
    print("Starting delay...")
    env.AddPostAction("upload", lambda *_, **__: time.sleep(20))
    print("done")

Log contains

writeWord(addr=0xe000ed0c,value=0x5fa0004)
([“upload”], [“.pio/build/mkrwifi1010/firmware.bin”])

Testing…
If you don’t see any output for the first 10 secs, please reset board (press reset button)

[Errno 2] could not open port /dev/ttyACM0: [Errno 2] No such file or directory: ‘/dev/ttyACM0’

I have noticed that after this the board is ‘stuck’ and I have to give it a hard reset (double click of the reset button to get the LED to slow flash) before I can program it again. Is there a fault in the Unity firmware?

If the unit test firmware crashes and thus loses the USB connection, there is no serial port to connect to anymore.

Are you trying this with the absolut simplest possible unit test code? What exactly are the test/* files here?

The test is pretty basic.

I don’t think there is a problem with my tests because:

  • I can get it to work if I target a ‘nano every’
  • If I run the test on a ‘mkr wifi 1010’ and wait for it to fail and then open a serial port and press reset I can see the output to a successful test run
  • The example ‘calculator’ project also has similar issues. Generally the device does not hang but at lest one of the two test cases will fail randomly.

It looks its a problem with the device disconnecting from the port when it swaps from upload to serial port mode.

Unsurprising, because that board has a permant-on SAMD21 as the USB-to-UART (and SWD) converter and you program the actual ATMega4809 sitting behind that; but with a mrk wifi 1010, you program the SAMD21 chip directly. The SAMD21 is running your firmware and hosting the USB stack / serial port at the same time.

And that is happening even though you put the “sleep(5)” in the after_upload?

Consistent with “the project doesn’t have the delay after upload script”

Adding a 5 second delay after _upload did help at all.

1 Like