Set USB Type per-sketch?

Status
Not open for further replies.

MuShoo

Well-known member
Is it possible to setup (I assume with some #defines and #includes?) a sketch to override the USB Type selected via the menus in Teensyduino? I'm programming two different boards simultaneously (they interact), and it'd be great if I could force each sketch to it's own USB type instead of having to change the menu option each time I upload.

I've tried a number of things (some hacking of the Teensy core files related to USB (usb.c, usb_api.h, etc)) but since I need to put my tag (TPP_MIDI or TPP_SERIAL) inside the sketch - which happens after all the usb related #includes - it fails. Any thoughts?
 
Well, since I couldn't figure this out, I sculpted a little workaround that might help some people. I'm on a mac, so these are all mac... things.

First thing I did was to find instances of '_reboot_Teensyduino_()' in usb.c for the MIDI and USB Serial cores, and commented it out. This stops the Teensy Loader application from automatically rebooting the Teensy (since there's two connected, and it has no way of knowing which one I want to program.) So, hafta press the button on the correct one when updating.

Then, in each sketch, I put:

Code:
#include "core_id.h"
#ifndef CORE_TEENSY_SERIAL
#error "You need to select Serial Core!"
#endif

(substitute MIDI or whatever for the other sketch). This makes the preprocessor/compiler throw an error and not compile if I'm not in the correct USB Type for the sketch.

Lastly, I setup a QuickKeys macro that lets me quickly swap between the MIDI and the Serial USB types (if one of those menu items is selected, it goes to the other - if neither are selected, it selects MIDI.)

It's a little kludgy, but it keeps me from making stupid mistakes when uploading...

Also, a side note - far as I can tell, killing that 'reboot' line in usb.c will let you embed a Teensy without fear of someone accidentally re-flashing your device to, say, Blink.ino. You can set up your own watchdog code to jump to the bootloader for your own firmware updates.
 
Thanks, these are helpful tips.
I wonder if it would be useful to make the commenting out of '_reboot_Teensyduino_()' something that could be done with #ifdef's so the
user could do this from the sketch or a menu toggle. It might not be worth Paul's time as there are probably few of us testing with a lot of teensy's at once
in different communication modes.
 
Status
Not open for further replies.
Back
Top