Unity testing not working with nrf52840, Serial disappearing

I am trying to get Unit Testing working on nrf52840. I am using the Adafruit Feather nrf52840 as board, the board that I am using in Hardware is a modification of it (but pretty similar).

If I compile and upload a very simple main.cpp

#include <Arduino.h>
#include "Adafruit_TinyUSB.h"

void setup() {
  // put your setup code here, to run once:

void loop() {
  // put your main code here, to run repeatedly:

Everything works fine.

Then I try to use Unity Test Framework inside PlatformIO in Visual Studio Code.
It compiles (I also have to add #include “Adafruit_TinyUSB.h” at the top of the File):

#include <Arduino.h>
#include "Adafruit_TinyUSB.h"
#include <unity.h>

//... test functions ..

void setup()
  delay(2000); // service delay


  UNITY_END(); // stop unit testing

platform = nordicnrf52
board = adafruit_feather_nrf52840
framework = arduino
debug_tool = jlink
upload_protocol = jlink

The problem is the Microcontroller is not seen as a Serial device anymore:

I think it has something to do maybe with the TinyUSB library, because there where similar problems.
If I comment out UNITY_END(); it is working to show the Serial again in the device manager.
With the nrf52832 it was working without this problem, I think this is because the Serial2USB is externally implemented, in the nrf52840 it is already included in the microcontroller.

The lib_archive did not work:

lib_archive = no

Adafruit Feather nrf52840 Microcontroller Board
PlatformIO Core, version 6.1.5

I highly suspect this is because the standard PlatformIO implementation for the Unity helper functions

does a Serial.end() in unityOutputComplete();, likely called by UNITY_END();.

The Serial.end(); in the Arduino core you’re working with will take this as hint to completely deinitilalize the USB peripheral, leaving you with this weird device in the device manager.

PlatformIO is very customizable. Can you follow Unity — PlatformIO latest documentation and provide an implementation of the functions above but with with unityOutputComplete() as an empty function? See here for an example.


so it is true that in the unityOutputComplete() function the Serial is ended.
I found this file in my project folder → .pio/build/adafruit_feather_nrf52840/unity_config/unity_config.cpp

void unityOutputComplete(void) { Serial.end(); }

But this actually was not the problem, because the unity tests should be executed and report their results over Serial before anyway.
Via kind of trial-and-error I found out that a short delay before the other (“normal”) delay does the trick.
The serial is also shown after in the Device Manager.



Does it have the same result when you do Serial.begin(9600); while(!Serial) {} instead of the delays?

This should block the code from executing before the serial port is connected to by PlatformIO.

Yes this has the same effect, it works as well!

I realized that debugging does not work this way, it hangs on while(!Serial) …

When you start debugging the serial port is not opened automatically, you have to use a terminal program to open the port (like HTerm).