How to use V-USB on an attiny85

V-USB is a project from the company Objective Development Software GmbH to bring basic USB support to any micro controller. This is what they say about it:

V-USB is a software-only implementation of a low-speed USB device for Atmel’s AVR® microcontrollers, making it possible to build USB hardware with almost any AVR® microcontroller, not requiring any additional chip.

And the best thing: It’s opensource. The code is fully available under the GPL. If you want to use it commercially, you can buy a licence. Its a very well documented project and it has a good forum.

I started to use it on an attiny85 as a replacement to parallel port I/O operations. There are many examples and reference designs at the above linked website.

There is just one small issue: The last released code is from 2010. Thats ok, but most of the example projects are back from 2009 and 2008. Since then, the API has quite a bit changed and most of the examples will not work anymore out of the box. (They might work with other micro controllers, but not with an attiny85).

To give it a start, I suggest to build the EasyLogger. Its code worked out of the box for me. Then, when you know your hardware works, download the main core from www.obdev.at/products/vusb/download.html. The version I used was vusb-20100715.

hid-mouse

To get the hid-mouse example working, I had to modify some files as described here. The files are also available for download below. I explain it on a Linux system with all requirements (gcc, ..) already installed. How ever it also should work on Windows.

1. Download and extract the vusb-20100715 files
2. Copy the folder libs-device to examples/hid-mouse/
3. Go into the folder examples/hid-mouse/
4. In the file Makefile, replace

DEVICE  = atmega168
F_CPU   = 16000000    # in Hz
FUSE_L  = # see below for fuse values for particular devices
FUSE_H  =
AVRDUDE = avrdude -c usbasp -p $(DEVICE) # edit this line for your programmer

with

DEVICE=attiny85
F_CPU   = 16500000    # in Hz
FUSE_L  = 0xE1
FUSE_H  = 0xDD
AVRDUDE = avrdude -c stk500v2 -P /dev/ttyUSB0 -p $(DEVICE)

You also will have to tell the compiler to use the files in libs-device:

Replace

CFLAGS  = -Iusbdrv -I. -DDEBUG_LEVEL=0
OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o

with

CFLAGS  = -Iusbdrv -I. -Ilibs-device -DDEBUG_LEVEL=0
OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o libs-device/osccal.o

and further down replace

rm -f main.hex main.lst main.obj main.cof main.list main.map main.eep.hex main.elf *.o usbdrv/*.o main.s usbdrv/oddebug.s usbdrv/usbdrv.s

with

rm -f main.hex main.lst main.obj main.cof main.list main.map main.eep.hex main.elf *.o usbdrv/*.o main.s usbdrv/oddebug.s usbdrv/usbdrv.s  libs-device/osccal.o

5. Next, open the file usbconf.h and change the following:

#define USB_CFG_IOPORTNAME      D

to

#define USB_CFG_IOPORTNAME      B

and

#define USB_CFG_DMINUS_BIT      4

to

#define USB_CFG_DMINUS_BIT      0

Note: If you have another hardware configuration, just adapt it here!

Further down, replace

#define USB_CFG_HAVE_MEASURE_FRAME_LENGTH   0

with

#if USB_CFG_CLOCK_KHZ==16500
#define USB_CFG_HAVE_MEASURE_FRAME_LENGTH   1
#include "osccal.h"
#else
#define USB_CFG_HAVE_MEASURE_FRAME_LENGTH   0
#endif

This was needed for me as the attiny85  clock was wrong or not good enough.

6. Now go into the folder libs-device and open the file osccal.h and change the following:

Uncomment the following lines:

#ifndef __ASSEMBLER__
#include <avr/interrupt.h>  // for sei()
extern void calibrateOscillator(void);
#endif
#define USB_RESET_HOOK(resetStarts)  if(!resetStarts){cli(); calibrateOscillator(); sei();}

(add a */ before it and a /* after it).

And  further down, comment out this line as it is already defined above:

void    calibrateOscillator(void);

7. Now we should be ready to compile the project. To program it, power up the device and connect your SPI programmer. (Note: I couldn’t program it while the USB was plugged in, so I had to power it externally!)
In a terminal, run the following commands:

cd examples/hid-mouse/firmware
make hex
make flash
make fuse

This will compile it and download it and also set the fuses correctly.

Now you should be able to plug into your USB port.
After a second or so the mouse pointer should start moving around.

If you run the comand dmesg, it will show something like the following:

usb 1-1.1.3: new low speed USB device using ehci_hcd and address 17
input: obdev.at Mouse as /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.1/1-1.1.3/1-1.1.3:1.0/input/input8
generic-usb 0003:16C0:03E8.0008: input,hidraw3: USB HID v1.01 Mouse [obdev.at Mouse] on usb-0000:00:1a.0-1.1.3/input0

If you instead get a message like this:

usb 1-1.1.3: new low speed USB device using ehci_hcd and address 19
usb 1-1.1.3: device descriptor read/64, error -32

then you have most likely a problem with the attiny85 clock and the timing is wrong.

Download

V-USB for attiny85
V-USB for attiny85
vusb_attiny85_20110701.zip
486.4 KiB
99 Downloads
Details...
This entry was posted in Electronics, Linux, Programming. Bookmark the permalink.