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
[wpfilebase tag=file id=19]
Interesting article about attiny85 HID as password logger http://dptechnology.jimdo.com/projects/hidhid/
Where is code (firmware) for password logger please? Where is scheme? Thanks.
I bought a Digispark(based on Attiny85) and a Keyes IR remote a while ago on Ebay, with the idea to make a IR receiver to USB to control my PowerPoint presentations. After setting up the digispark using arduino I found out that I was kind of allergic to arduino, and decided to use C in atmel studio. Your blog post contained useful informtation to get the V-USB working! Thanks!
Hello Henk,
Would you be able to guide me on setting up ATMEL STUDIO to accept the DigiSpark using VUSB?
Kind Regards,
Aerro
I am sorry, but I can not give any support for ATMEL development. See my other comments.
Hi George,
Thanks for adding some valuable information on V-USB on your website. I have a working setup that spits out data fine under Windows (open Notepad and off it goes), but I can’t seem to replicate this under linux (e.g. open vi in xterm, set vi to ‚insert‘ mode but nothing is printed on screen) from the device.
Do I need some other mechanism under linux to fetch the output data from my avr device?
Cheers,
marcel
Hi Marcel
I am not really using VUSB anymore. But I know that it worked fine under Linux (my main system).
What does dmesg say?
I would expect it to work out of the box. I do not need a special editor, you can also just use the terminal or any GUI editor.
Hey Geroge, I’m looking into getting started in the world of AVR.
You mentioned you’re not using V-USB anymore. Is there something better to get started? What are you using nowadays?
Cheers,
Hi Bruno
I moved from Atmega to the STM32 MCUs.
They are very nice to use.
Have a look on the
NUCLEO-L053R8: http://www.st.com/web/en/catalog/tools/FM116/SC959/SS1532/LN1847/PF260001
Hi, tried your attiny85 mod. Got this error
root@mail:/v-usb-avr/vusb-20121206/examples/hid-data/firmware# make hex
avr-gcc -Wall -Os -DF_CPU=16500000 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=attiny85 -o main.elf usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o
usbdrv/usbdrv.o: In function `usbPoll‘:
usbdrv.c:(.text+0x2ba): undefined reference to `calibrateOscillator‘
collect2: error: ld returned 1 exit status
make: *** [main.elf] Error 1
Hi
See my post above.
I can no longer give support for the Atmega microcontrollers!
For support, try to search the internet for „undefined reference to `calibrateOscillator’“.
Pingback:ItOpen – Open Web Solutions, WebGis Development » Blog Archive » V-USB on Attiny85