Arduino R4 Wifi disabling COM ports

I'd like to disable my COM port so it doesn't show up in device manager

Here's a snippet from SerialUSB.cpp

#include "SerialUSB.h"
#include "tusb.h"

#ifndef DISABLE_USB_SERIAL
// Ensure we are installed in the USB chain
void __USBInstallSerial() { /* noop */ }
#endif

where do I pass this so it gets disabled?

The first thing you should know is that I gave it a try and this won't accomplish your goal. If you would like to test this, I recommend simply editing that file and then uploading a sketch to your board.

Even though there is no point in this specific case, the question of how to define a macro that will affect the core files is an interesting subject to discuss in general.

You can't do it from the sketch. You have two options for defining a macro like DISABLE_USB_SERIAL:

  • Edit the appropriate core source file and add the #define directive:
    #define DISABLE_USB_SERIAL
    
  • Add a -D DISABLE_USB_SERIAL flag to the compilation command.

The sketch build system generates the compilation commands from templates which use platform properties to fill in the parts of the command that are system or project-specific. The platform developers have included a set of properties dedicated to the use by the user to inject arbitrary arguments into the compilation commands:

The property of interest in this case where we want to define a macro when compiling the core's C++ files is compiler.cpp.extra_flags. You can see it is referenced in the C++ compilation command template here:

(note the {compiler.cpp.extra_flags} reference in the template)

So if we set the value of the compiler.cpp.extra_flags property to -D DISABLE_USB_SERIAL, then the compilation command will contain that flag.

You can't define arbitrary platform properties via Arduino IDE, but it is possible to do that if you compile and upload your sketch using the official Arduino CLI command line tool.

The arduino-cli compile command has an optional --build-property flag:

https://arduino.github.io/arduino-cli/latest/commands/arduino-cli_compile/#options

This flag allows you to define platform properties. So we can do something like this:

arduino-cli compile  --build-property "compiler.cpp.extra_flags=\"-D DISABLE_USB_SERIAL\"" --fqbn arduino:renesas_uno:unor4wifi --port COM42 --upload --verbose PERTILLISCH/sketches/Blink100

(note the --build-property "compiler.cpp.extra_flags=\"-D DISABLE_USB_SERIAL\"" flag in the command)

The previous post was discussing the subject of the DISABLE_USB_SERIAL macro specifically. I will now provide some information relevant to the underlying goal of disabling the serial port:

Arduino pin 21 on the RA4M1 is connected to NLASB3157 switches on the board that control whether the USB data lines are electrically connected directly to the RA4M1 or to the board's ESP32-S3 "bridge" module.

Normally the bridge module is connected to the USB data lines, so it is that module producing the COM port. The only way you could disable that port would be to modify the bridge module's firmware.

However, when the switches are configured to connect the RA4M1 microcontroller to the USB data lines, it is that microcontroller that produces the serial port. The code in the SerialUSB.cpp file is related to the creation of that USB CDC port.

The most common case when that port is active is when the sketch uses the "HID" library (which is more commonly done transitively via a higher level library like "Keyboard" or "Mouse"). However, you can also control the switch directly using digitalWrite in sketches that don't use the HID library. In this case, if no code in the sketch program is producing a USB CDC serial port, then the board won't produce one.

For example:

/*
Connect the RA4M1 microcontroller on the UNO R4 WiFi board to the USB data lines.

After uploading this sketch, the board will no longer produce a serial port, meaning it will not be possible to upload
normally.
To upload to the board when it is in this state, press and release the "RESET" button on the board twice quickly,
select the serial port of the board, and then start an upload.
*/
void setup() {
  pinMode(21, OUTPUT);
  digitalWrite(21, HIGH);
}
void loop() {}