Close

Move to Adafruits ProTrinketHidCombo lib

A project log for Pro Trinket USB Keyboard

A USB keyboard for custom shortcuts based on the Pro Trinket and Pro Micro

stefan-lochbrunnerStefan Lochbrunner 06/02/2015 at 22:470 Comments

TLDNR: what it says in the caption, v3 sketch on GitHub.

A few days ago I started moving the sketch to the (relatively) new ProTrinketHidCombo library directly from Adafruit thinking it would be an easy task...

Besides the VUSB for Arduino linrary not compiling in the 1.6.4 IDE the ProTrinketHidCombo lib has the advantage (as the name implies) of 'complete'(?) HID functionality meaning not only keyboard but also mouse and multimedia key functionality.

I ran into similar problems as before with the device not properly re-enumerating and the like. Going back and forth between this one and the working VUSB for Arduino library I found out that the thing that was missing to make it work was to disable the timer 0 overflow interrupt in the beginning of the usbBegin() function in ProTrinketHidComboC.c (starting in line 32). The changed function now looks like this:

void usbBegin()
{
  // disable timer 0 overflow interrupt (used for millis)
  TIMSK0&=!(1<<TOIE0);
  
  // Clear interrupts while performing time-critical operations
  cli();
  
  // run at full speed, because Trinket defaults to 8MHz for low voltage compatibility reasons
  clock_prescale_set(clock_div_1);
  
  // fake a disconnect to force the computer to re-enumerate
  //PORTD &= ~(_BV(USB_CFG_DMINUS_BIT) | _BV(USB_CFG_DPLUS_BIT));
  usbDeviceDisconnect();
  _delay_ms(250);
  usbDeviceConnect();
  
  // start the USB driver
  usbInit();
  sei();
}

Another minor thing with this function is that the write to PORTD (or PORTB as it says on their GitHub) is redundant. As the comment states this is intended to force re-enumeration which can be done by forcing D- (and D+) low. D- and D+ are on port D not B, pins 7 and 2 respectively. With the 'Optional Hardware Config' section in usbconfig.h commented out, D- is already being forced low by usbDeviceDisconnect() found in usbdrv.h (starting in line 298).

As you might know I'm not using a genuine Pro Trinket but I don't think that should make much of a difference. I'd be interested if the library and the included example work without modification on a proper Pro Trinket.

edit:

Obviously if you disable the timer overflow interrupt right before initializing the library, there is no need to modify the library.

TIMSK0&=!(1<<TOIE0);
TrinketHidCombo.begin();

edit 2015-09-26:

added link to the post about the library above

Discussions