Elastic Sheep

Because elasticdog was already taken

Elastic Sheep header image 2

Testing the LUFA library on the Teensy

April 3rd, 2009 · 1 Comment · Uncategorized

LUFA is an open-source usb stack implementation from Dean Camera, suitable for the AVR family and thus the AT90USB162 on my Teensy board.

The version I am starting with in this tutorial is LUFA090209.zip

The CDC demo

The Communication Device Class is a generic class that can be used for modem communication. In this case it provides a two-way link similar to a serial connection and can use a generic driver in Windows and in OS X.

The CDC demo in /Demos/CDC sends predefined strings to the PC on keypresses. Let’s try this on the Teensy !

Connecting a push-button

First we need a button, connected to a pin configured as an input, for example PB0 (SS pin on the Teensy):

Push button on Teensy

To configure PB0 as an input:

DDRB &= ~_BV(PB0);
PORTB |= _BV(PB0);

The first line configure PB0 as an input. The second one activate an internal pull-up resistor on PB0 so we don’t need an external one. The button is connected between PB0 and GND. The pin state will be high when not pressed and low when pressed.

To read the PB0 pin value:

is_pressed = !(PIND & _BV(PB0));

Modifying the CDC demo code

First make a backup copy of the CDC directory. See the end of the post for a download link of the modified source code.

The main code of the demo is in CDC.c. This code calls Joystick_GetStatus() to get the status of the joystick on the AT90USBKEY board. As we have no joystick we will replace this function by our own :

uint8_t PushButton_GetStatus(void)
{
  return !(uint8_t)(PINB & _BV(PB0));
}

Now we have to modify the makefile to use the correct MCU target:

#MCU = at90usb1287
MCU = at90usb162

#BOARD  = USBKEY
BOARD = USER

#F_CPU = 8000000
F_CPU = 16000000

Then type make in the CDC directory to compile the project.
The result is a CDC.hex file that can be uploaded with the Teensy loader application or in my case with an ISP programmer.

Testing the application

I am using OS X as the host OS.

To check if the Teensy is correctly enumerated you can use the USB Prober utility provided with XCode in /Developper/Applications/Utilities.

Full Speed device @ 5 (0x1D110000): ...................  Communication device: "LUFA CDC Demo"
    Device Descriptor   
        Descriptor Version Number:   0x0110
        Device Class:   2   (Communication)
        Device Subclass:   0
        Device Protocol:   0
        Device MaxPacketSize:   8
        Device VendorID/ProductID:   0x03EB/0x2044   (Atmel Corporation)
        Device Version Number:   0x0000
        Number of Configurations:   1
        Manufacturer String:   1 "Dean Camera"
        Product String:   2 "LUFA CDC Demo"
        Serial Number String:   0 (none)
    Configuration Descriptor   
        Length (and contents):   67
        Number of Interfaces:   2
        Configuration Value:   1
        Attributes:   0xC0 (self-powered)
        MaxPower:   100 ma
        Interface #0 - Communications-Control   
        Interface #1 - Communications-Data/Unknown Comm Class Model   

The CDC device should appear in /dev under the name tty.usbmodemXXXXXXXXX:

HOST:~ host$ ls /dev/tty.usb*
tty.usbmodem0000111D1 (be careful it can change !!!)

You can now connect to the device using the GNU screen utility.

HOST:~ host$ screen /dev/tty.usbmodem0000111D1

Then press the push button on your breadboard and watch the magic happen.

cdc_screen

Exiting the GNU screen utility is a little tricky: you have to press control-a then control-shift-alt-£ and then confirm.

Bonus points

You can track the status of the USB connection with leds activated by the UpdateStatus function in CDC.c.

CDC Leds

Here I am using the PD4 output for the red led and the PD5 output for the green one.

Red only => USB Not Ready
Red/Green => USB Enumerating
Green Only => USB Ready

Source code

My modified CDC demo source code: cdc-teensy-20090403.zip

Tags: ···

One Comment so far ↓

  • Kevin Stokes

    Thanks for posting about your progress. I also have purchased a Teensy. I was more interested in using a hid driver, but I think I will follow your example first and try USB Serial.
    -Kevin

Leave a Comment