ELECT 36: construcció d'un dispositiu USB

De wikijoan
Salta a la navegació Salta a la cerca

Referències

USB in a nutshell:

Introducció

Hi ha vàries aproximacions, no totes igual d'elegants.

1. FTDI La primera és com havia fet amb el Arduinotes, que tenim l'ordinador connectat a l'ordinador pel port USB. Clar que el dispositiu ttyUSB és en realitat un port sèrie i he d'establir una comunicació sèrie amb el dispositiu. I com es demostra en el projecte, puc rebre (i enviar) informació seriada de l'arduino a l'ordinador. Clar que això no és el que busco. a més, un inconvenient és que necessita el xip FTDI (integrat amb l'arduino, o bé extern amb un cable, o bé es pot integrar en el projecte. El xip FTDI val 3-4 euros a Sparkfun, i és SMD).

2. V-USB Una altra possibilitat és utilitzar el projecte V-USB per als xips AVR. Ha de ser la solució més fàcil. Hi ha molta documentació i projectes, i no es necessita hardware addicional. S'ha d'instal.lar el driver software (és una solució software), per a Linux i Windows, i per tant no és la solució més a baix nivell que m'agradaria, sinó una solució suposo que fàcil però una solució intermitja. A més, tot i que és GPL, s'hauria de llegir bé la llicència. En ser una solució software no necessita cap xip USB (que implementa per hardware el protocol USB) (ni cap xip FTDI que emula comunicació sèrie).

3. USB hardware La tercera possibilitat seria la solució pura. Connecto un dispositiu que el PC el reconeix. Necessito un driver per al dispositiu (API libusb o mòdul del kernel, veure el projecte del rocket launcher). El driver en definitiva em dóna les normes per tal de qué el PC es pugui comunicar amb el dispositiu, i per tal de que el dispositiu s'obri i estigui disponible. Seria la solució que busco, similar al Rocket Launcher. Ara bé, aquesta solució pura necessita un xip USB, i això potser no està justificat si el que volem fer és transmetre poca informació entre el host i el dispositiu. L'avantatge del USB hardware és que les capes més baixes del protocol USB estan implementades en el xip, i per tant això va molt bé per a low/full/high speed USB. Per exemple, un xip USB és el AT90USBKEY d'Atmel (http://www.atmel.com/dyn/products/tools_card.asp?tool_id=3879). Llegeixo en els links de V-USB:

Driver Firmware for AVRs with USB Hardware

www.fourwalledcubicle.com/MyUSB.php - This project is an LGPL’d USB driver for Atmel’s AT90USBXXX AVR microcontroller series. It runs on Atmel’s USBKEY, STK525 and STK526 boards.

savannah.nongnu.org/projects/avrusb - A freeware USB driver for the AT43USB3XX series of chips. The project seems to be dead, though.

TBD. Scripts udev. Són els scripts que es poden executar en el moment en què es detecta un dispositiu.

Build a USB device:

USB TouchScreen Mouse amb Arduino:

Projecte V-USB: Virtual USB Port for AVR Microcontrollers

Projects Based on V-USB:

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.

Tenim la versió per Windows i per Linux. La última versió: vusb-20100715.tar.gz

Projecte V-USB: Virtual USB Port for AVR Microcontrollers

Projects Based on V-USB:

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.

Tenim la versió per Windows i per Linux. La última versió: vusb-20100715.tar.gz

Features

   * Fully USB 1.1 compliant low-speed device, except handling of communication errors and electrical specifications.
   * Example projects demonstrate device and host driver implementations on Linux, Mac OS X and Windows.
   * Supports multiple endpoints: one control endpoint, two interrupt/bulk-in endpoints and up to 7 interrupt/bulk-out endpoints. (Bulk endpoints are forbidden for low speed devices by the USB standard.)
   * Transfer sizes up to 254 bytes by default, more as configuration option.
   * Comes with freely usable USB identifiers (Vendor-ID and Product-ID pairs).
   * Runs on any AVR microcontroller with at least 2 kB of Flash memory, 128 bytes RAM and a clock rate of at least 12 MHz.
   * No UART, timer, input capture unit or other special hardware is required (except one edge triggered interrupt).
   * Can be clocked with 12 MHz, 15 MHz, 16 MHz or 20 MHz crystal or from a 12.8 MHz or 16.5 MHz internal RC oscillator.
   * High level functionality is written in C and is well commented.
   * Only about 1150 to 1400 bytes code size.
   * You can choose the License: Open Source or commercial. Click here for details.

És interessant llegir el resum sobre altres alternatives basades en microcontroladors per a desenvolupar dispositius USB:

Metaboard: http://metalab.at/wiki/Metaboard

És una placa basada en Arduino que implementa de forma clara el concepte. Es Open Source i es disposa dels fitxers Eagle. És la base sobre la que es pot desenvolupar un dispositiu USB basat en xips AVR. És la part hardware sobre la que s'implementa la part software del V-USB. Ara bé, hi ha diferències respecte l'arduino (llegir).

Instal.lació

Descarregar-se la última versió (vusb-20100715.tar.gz), i mirar què hi ha dins:

WHAT IS INCLUDED IN THIS PACKAGE?
==custom-class exemple==
===Prerequisits===
Són tres: el hardware, i els entorns de desenvolupament de la part del firmware (gcc-avr) i de la part del software (gcc i llibreria libusb):
*necessito el hardware, un ''metaboard'' o similar
*necessito un AVR development environment, és a dir, el paquet gcc-avr
*Host development environment: A C compiler and libusb
===Compilar la part del firmware===
No ho puc fer fins que no tingui el hardware
<pre>
$ ~/vusb-20100715/examples/custom-class/firmware
$ make
This Makefile has no default rule. Use one of the following:
make hex ....... to build main.hex
make program ... to flash fuses and firmware
make fuse ...... to flash the fuses
make flash ..... to flash the firmware (use this on metaboard)
make clean ..... to delete objects and hex file

Funciona, però encara no tinc el metaboard.

# make flash
cp -r ../../../usbdrv .
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c usbdrv/usbdrv.c -o usbdrv/usbdrv.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -x assembler-with-cpp -c usbdrv/usbdrvasm.S -o usbdrv/usbdrvasm.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c usbdrv/oddebug.c -o usbdrv/oddebug.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c main.c -o main.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -o main.elf usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o
rm -f main.hex main.eep.hex
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
avr-size main.hex
   text	   data	    bss	    dec	    hex	filename
      0	   1488	      0	   1488	    5d0	main.hex
avrdude -c usbasp -p atmega168  -U flash:w:main.hex:i
avrdude: error: could not find USB device "USBasp" with vid=0x16c0 pid=0x5dc
# make hex
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c usbdrv/usbdrv.c -o usbdrv/usbdrv.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -x assembler-with-cpp -c usbdrv/usbdrvasm.S -o usbdrv/usbdrvasm.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c usbdrv/oddebug.c -o usbdrv/oddebug.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c main.c -o main.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -o main.elf usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o
rm -f main.hex main.eep.hex
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
avr-size main.hex
   text	   data	    bss	    dec	    hex	filename
      0	   1488	      0	   1488	    5d0	main.hex

Obtenim el fitxer make.hex, que és el binari que s'ha de carregar al metaboard.

Compilar la part del host software

Aquesta part la puc fer encara que no tingui el hardware.

$ cd vusb-20100715/examples/custom-class/commandline
$ make
gcc  `libusb-config --cflags` -O -g -Wall -c opendevice.c
gcc  `libusb-config --cflags` -O -g -Wall -c set-led.c
gcc -o set-led opendevice.o set-led.o `libusb-config --libs`

Es genera l'executable set-led

ús de l'aplicatiu set-len en línia de comanda

$ ./set-led 
usage:
  ./set-led on ....... turn on LED
  ./set-led off ...... turn off LED
  ./set-led status ... ask current status of LED
joan@joanillo:~/vusb-20100715/examples/custom-class/commandline$ ./set-led on
Could not find USB device "LEDControl" with vid=0x16c0 pid=0x5dc

De moment encara no puc fer res.


creat per Joan Quintana Compte, maig 2011

Compilar la part del firmware

No ho puc fer fins que no tingui el hardware

$ ~/vusb-20100715/examples/custom-class/firmware
$ make
This Makefile has no default rule. Use one of the following:
make hex ....... to build main.hex
make program ... to flash fuses and firmware
make fuse ...... to flash the fuses
make flash ..... to flash the firmware (use this on metaboard)
make clean ..... to delete objects and hex file

Funciona, però encara no tinc el metaboard.

# make flash
cp -r ../../../usbdrv .
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c usbdrv/usbdrv.c -o usbdrv/usbdrv.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -x assembler-with-cpp -c usbdrv/usbdrvasm.S -o usbdrv/usbdrvasm.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c usbdrv/oddebug.c -o usbdrv/oddebug.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c main.c -o main.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -o main.elf usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o
rm -f main.hex main.eep.hex
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
avr-size main.hex
   text	   data	    bss	    dec	    hex	filename
      0	   1488	      0	   1488	    5d0	main.hex
avrdude -c usbasp -p atmega168  -U flash:w:main.hex:i
avrdude: error: could not find USB device "USBasp" with vid=0x16c0 pid=0x5dc
# make hex
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c usbdrv/usbdrv.c -o usbdrv/usbdrv.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -x assembler-with-cpp -c usbdrv/usbdrvasm.S -o usbdrv/usbdrvasm.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c usbdrv/oddebug.c -o usbdrv/oddebug.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c main.c -o main.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -o main.elf usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o
rm -f main.hex main.eep.hex
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
avr-size main.hex
   text	   data	    bss	    dec	    hex	filename
      0	   1488	      0	   1488	    5d0	main.hex

Obtenim el fitxer main.hex, que és el binari que s'ha de carregar al metaboard.

:100000000C945C000C9481010C9479000C947900A0
:100010000C9479000C9479000C9479000C9479007C
:100020000C9479000C9479000C9479000C9479006C
:100030000C9479000C9479000C9479000C9479005C
:100040000C9479000C9479000C9479000C9479004C
:100050000C9479000C9479000C9479000C9479003C
:100060000C9479000C9479000403090412036F00C6
...

custom-class exemple

Compilar la part del firmware

No ho puc fer fins que no tingui el hardware

$ ~/vusb-20100715/examples/custom-class/firmware
$ make
This Makefile has no default rule. Use one of the following:
make hex ....... to build main.hex
make program ... to flash fuses and firmware
make fuse ...... to flash the fuses
make flash ..... to flash the firmware (use this on metaboard)
make clean ..... to delete objects and hex file

Funciona, però encara no tinc el metaboard.

# make flash
cp -r ../../../usbdrv .
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c usbdrv/usbdrv.c -o usbdrv/usbdrv.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -x assembler-with-cpp -c usbdrv/usbdrvasm.S -o usbdrv/usbdrvasm.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c usbdrv/oddebug.c -o usbdrv/oddebug.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c main.c -o main.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -o main.elf usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o
rm -f main.hex main.eep.hex
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
avr-size main.hex
   text	   data	    bss	    dec	    hex	filename
      0	   1488	      0	   1488	    5d0	main.hex
avrdude -c usbasp -p atmega168  -U flash:w:main.hex:i
avrdude: error: could not find USB device "USBasp" with vid=0x16c0 pid=0x5dc
# make hex
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c usbdrv/usbdrv.c -o usbdrv/usbdrv.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -x assembler-with-cpp -c usbdrv/usbdrvasm.S -o usbdrv/usbdrvasm.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c usbdrv/oddebug.c -o usbdrv/oddebug.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -c main.c -o main.o
avr-gcc -Wall -Os -DF_CPU=16000000	 -Iusbdrv -I. -DDEBUG_LEVEL=0 -mmcu=atmega168 -o main.elf usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o
rm -f main.hex main.eep.hex
avr-objcopy -j .text -j .data -O ihex main.elf main.hex
avr-size main.hex
   text	   data	    bss	    dec	    hex	filename
      0	   1488	      0	   1488	    5d0	main.hex

Obtenim el fitxer make.hex, que és el binari que s'ha de carregar al metaboard.

Prerequisits

Són tres: el hardware, i els entorns de desenvolupament de la part del firmware (gcc-avr) i de la part del software (gcc i llibreria libusb):

  • necessito el hardware, un metaboard o similar
  • necessito un AVR development environment, és a dir, el paquet gcc-avr
  • Host development environment: A C compiler and libusb

Compilar la part del firmware

No ho puc fer fins que no tingui el hardware

  • Host development environment: A C compiler and libusb

Compilar la part del host software

Aquesta part la puc fer encara que no tingui el hardware.

$ cd vusb-20100715/examples/custom-class/commandline
$ make
gcc  `libusb-config --cflags` -O -g -Wall -c opendevice.c
gcc  `libusb-config --cflags` -O -g -Wall -c set-led.c
gcc -o set-led opendevice.o set-led.o `libusb-config --libs`

Es genera l'executable set-led

ús de l'aplicatiu set-len en línia de comanda

$ ./set-led 
usage:
  ./set-led on ....... turn on LED
  ./set-led off ...... turn off LED
  ./set-led status ... ask current status of LED
joan@joanillo:~/vusb-20100715/examples/custom-class/commandline$ ./set-led on
Could not find USB device "LEDControl" with vid=0x16c0 pid=0x5dc

De moment encara no puc fer res.


creat per Joan Quintana Compte, maig 2011