ProMicro - Can the USB libraries be omitted?

I just realized that when the IDE is compiling a sketch for the ProMicro, all the USB libraries are included even if you don’t use them. Eg. the USB Serial stuff, and the keyboard and mouse HID stuff, is all compiled and included into the sketch regardless of whether there’s a single Serial or Keyboard or Mouse statement in there.

Is there a way to selectively omit the whole USB library? It looks like this might save as much as 3kB of space, which is massive considering the bootloader is already taking 4kB. (In an earlier thread I asked about bypassing the bootloader, but I haven’t had to do that yet - stripping down and optimizing my code and other libraries has kept me under the maximum so far).

Digging into the ProMicro hardware folder, I found that in (sketch folder)/hardware/proMicro/cores/arduino/main.cpp there is an #if that attaches the USB stuff:

#if defined(USBCON)
	USB.attach();
#endif

If I comment out those three lines then Verify the sketch, it comes in around 3kB smaller than it does otherwise. For example, the standard “Blink” example, with the USB libraries inluded, reports Binary sketch size: 4360 bytes (of a 28672 byte maximum). When I comment out those lines in main.cpp and reload the sketch, the Blink example drops down to Binary sketch size: 1316 bytes (of a 28672 byte maximum).

The only downside I have found in (limited) testing is that when the USB libraries are omitted, the board does not auto-reset when you try to load a new sketch - you need to manually reset it and time that with hitting the ‘upload’ button in the IDE. This shouldn’t be an issue though as the 1.0.1 version of the IDE & official Leonardo documentation already includes a fairly graceful way to handle the manual-reset if the auto-reset doesn’t work.

I’ve searched all the files in the ProMicro section but I can’t find where USBCON is defined. I found it’s tested in a handful of places, but it doesn’t seem to be defined anywhere. I also searched the Arduino IDE and it doesn’t appear to be defined there either.

So what I am wondering is if there is some way (preferences? board.txt file?) to tell the IDE that the USB libraries are not necessary and should not be included? I can go with manually editing the main.cpp but it seems like there ought to be a friendlier way to specify that the USB libraries are not needed.

Thanks!

After spending a few hours with the latest Arduino IDE, I don’t think it would be as easy as I originally thought to make this work.

For one thing, the Leonardo is set up the same as the ProMicro, in that all the USB libraries are loaded regardless. Actually what I’ve figured out is that the USBCON is defined in Avrdude’s conf file, so any AVR with onboard USB probably has it. And the way the Arduino core files are written, if the uC has USBCON defined then all the USB libraries are loaded whether you use them or not.

So making a change to allow the USB libraries to be optional would probably have to come from Arduino since it would affect a bunch of core files, so it’d probably require some not-insignificant changes to the way the IDE works.

Then the way the leonardo has a fallback incase the bootloader doesn’t trigger automatically, does not translate to other 32u4 boards - it is hardcoded into the IDE for the Leo only. I figured out how to trick it for the ProMicro but it’s a hack. It’s also not as graceful as the documentation indicated, but it does work.

Anyhow, it looks like editing main.cpp is the easiest way to drop the USB libraries for my purposes. The extra 3kB of flash space it gains me is worth the hassle of manually rebooting during the upload step.

Cheers