📄 readme
字号:
USBtiny=======USBtiny is a software implementation of the USB low-speed protocol forthe Atmel ATtiny microcontrollers. Of course, it will also work on theATmega series. The software is written for an AVR clocked at 12 MHz.At this frequency, each bit on the USB bus takes 8 clock cycles, andwith a lot of trickery, it is possible to decode and encode the USBwaveforms by software. The USB driver needs between 1300 and 1400bytes of flash space (excluding the optional identification strings),depending on the configuration and compiler version, and 46 bytes RAM(excluding stack space). The C interface consists of 3 to 5 functions,depending on the configuration.Implementation==============USB uses two differential data signals, D+ and D-, which are normallycomplementary. However, the end of a packet is signalled by pullingboth signals low. Data is not transmitted directly on the USB bus, itis NRZI encoded first. This means that a "0" bit is encoded as a bitchange, and a "1" bit is encoded as no bit change. After 6 "1" bits,"bit stuffing" takes place to force a change on the USB signal lines.The software is interrupt driven: the start of a USB packet triggersan interrupt. The interrupt handler synchronizes with the sync byte,removes the NRZI encoding and bit stuffing, and stores the packed inone of the two RAM buffers. Two buffers are used so that the nextpacket can be received while the current one is being processed.Depending on the packet type, a reply packed may be sent backimmediately in the interrupt handler.The rest of the USB driver is written in C. A usb_poll() functionmust to be called periodically to poll for incoming packets. Only asingle endpoint is supported at the moment. Standard control requestsare directly handled by the USB driver. Other SETUP requests areforwarded to a user-supplied function usb_setup(). Support for largereplies and OUT control requests is optional, see usbtiny.h.To use the USB driver in your own application, you need to configurethe macros in usbtiny.h, and provide a function usb_setup() to handleSETUP control packets. Optionally, you need to provide the functionsusb_in() and usb_out(). Your code needs to call the initializationfunction usb_init() at program startup, and usb_poll() at regularintervals. The AVR device type and the upload command should beconfigured at the top of the Makefile.Other USB projects==================This software was inspired by two similar USB projects for the AVR,especially the second one: http://www.cesko.host.sk/IgorPlugUSB/IgorPlug-USB%20(AVR)_eng.htm http://www.obdev.at/products/avrusb/index.htmlThe IgorPlugUSB code and earlier versions of the obdev code had therestriction that the D+ signal must be connected to bit 0 of an I/Oport, in addition to an interrupt input. That means that for deviceslike the ATtiny2313, three I/O pins are required to control the USB bus.One of the reasons I wrote USBtiny was to have more freedom over whichI/O pins to use for the D+ and D- USB signals. The only restrictionis that both signals should be on the same I/O port. When you select apin for D+ that can also generate an interrupt, only two I/O pins arerequired. Later versions of the obdev code have also removed thisrestriction. The pin-change interrupt is deliberately not used, sothat it remains available for other uses. Another improvement is anoptional faster CRC calculation that uses a lookup table to calculate4 bits at a time.Apart from these advantages, I think that my code is more readable andeasier to configure, but that impression may be caused by a mild formof the NIH syndrome that I'm suffering from. In any case, I learned alot about the USB protocol, and writing the interrupt handler was anice puzzle.The USBtinyISP project is an AVR in-circuit programmer based on my ISPconverter, and is available as a kit: http://www.ladyada.net/make/usbtinyisp/Hardware========The AVR must be clocked with an external 12 MHz crystal. For anATtiny2313, this means that the low fuse byte must be reprogrammed,for instance to 0xef. I also recommend to enable the BOD circuit,when available. For the ATtiny2313, this means programming the highfuse byte to 0xdb (BOD level is 2.7V).The USB data signals are specified at 3.3V. The easiest way toaccomplish this is to use a 3.3V power supply for the AVR. However,driving the USB signals with 5V seems to work as well in most cases,which may be more convenient when you need to interface to 5Vperipherals. According to the USB specification, a device should notbe damaged by 5V signals. I've been running the AVR and USB bus at 5V,and haven't encounter any problems so far, but I have some reportsof PCs and laptops that do not work with 5V signals. In that case,you can reduce the voltage of the USB data signals by adding 3V6 zenerdiodes from the data signals to ground.USBtiny SPI converter=====================My first USBtiny application is a USB to SPI (Serial ProgrammingInterface) converter. The SPI signals are connected to a female DB-25connecter, so that the converter can be plugged onto my AVR parallelport programmer. Because the most important parallel port signals areconnected to the DB-25 connecter, the same hardware (with differentfirmware) could be used to control other parallel port devices. Thatis also the reason why I connected the ACK signal to the INT1 pin.The software, the schematic and a Python test script can be foundin the "spi" subdirectory.My AVR parallel port programmer follows the "bsd" design from BrianDean (http://www.bsdhome.com/avrdude/). For safety, I've used 1Kseries resistors in the output signals. The Atmel device is poweredby the data lines D1-D3, via 220 Ohm resistors. D0 is used to controla LED. Here is an ASCII-art version of the schematic:DB25 male ATtiny2313--------- ----------18 GND ----------------------+---- 10 GND 2 D0 ---- 330 ---- LED ----+ 3 D1 ---- 220 ---+ 4 D2 ---- 220 ---+-------------- 20 VCC 5 D3 ---- 220 ---+ 7 D5 ---- 1K ------------------- 1 /RESET 8 D6 ---- 1K ------------------- 19 SCK 9 D7 ---- 1K ------------------- 17 MOSI10 ACK --------------------------- 18 MISOThe USBtiny circuit may be powered via the diode at pin 14 of theDB-25 connector to enable reprograming the ATtiny2313 in-system.This requires an adaptor cable between another SPI programmer anda DB-25 male connector with the following connections: SPI DB25 --- ---- GND 25 VCC 14 RESET 13 SCK 9 MISO 8 MOSI 7Bit-banging the SPI signals via USB turned out to be very slow. To getreasonable programming speeds, I've moved the SPI algorithm into theAVR. This means that you can send a 32-bit SPI command in a single USBpacket. In addition, you can read or write up to 255 bytes from/toflash or EEPROM in a single control transfer.The subdirectory "patches" contains a patch for avrdude-5.4 that addssupport for controlling this SPI converter. The programmer name is"usbtiny". You can use the -B option or the "sck" command to specifythe minimum SCK period in microseconds (range: 1..250, default: 10).Native support for USBtiny was added in avrdude version 5.5.USBtiny LIRC compatible IR receiver and LCD controller======================================================A second USBtiny application is a receiver for infrared remotecontrols that can be used with the LIRC package (http://www.lirc.org/).The firmware stores the mark/space timings from a TSOP1738 IR decoderin a buffer that is polled by a LIRC device driver. As a visualfeedback, a LED is flashed when a signal is being received. As anadditional (optional) feature, a 2x16 LCD display is attached to PORTB.You can control the display via the USB bus. The software, the schematicand a Python test script can be found in the "ir" subdirectory.A pull-down resistor at PB3 prevents a hang in the LCD initializationcode when no LCD is attached to PORTB. You can disable the LCD code bysetting the LCD_PRESENT macro in main.c to 0.I adopted the "IgorPlug-USB" protocol, so that the existing LIRCdevice driver "igorplugusb" could be used without modifications.Nevertheless, patches for lirc-0.8.0 and lirc-0.8.2 are includedin the "patches" subdirectory, with the following modifications:1) An increase of the sample rate from 10 to 100 times per second. This is not required, but improves the responsiveness and repeat behavior for some remote controls.2) A fix from Reinhard Max to get correct gap timings.3) A compilation fix for the latest 2.6 Linux kernels (lirc-0.8.0).Tools=====The software was developed on a Linux system. In addition to standardtools like GNU make, you need the AVR versions of gcc, binutils andglibc to build the code: On a Debian/Ubuntu system, you can apt-getthe following packages: gcc-avr binutils-avr avr-libcTo upload the code to an AVR, I use avrdude with a parallel portprogrammer.I initially used gcc-3.4.3 with the -Os option, which generatesreasonable compact code. Unfortunately, newer versions like gcc-4.1.0generate about 10% more code, and as a result, the application codedid not fit in 2K anymore. To make it fit, I had to remove the optionalvendor and device strings, by undefining the USBTINY_VENDOR_NAME andUSBTINY_DEVICE_NAME macros.The schematics were created with gschem, which is part of the gEDApackage. The conversion to Postscript is done with a script thatinvokes gschem non-interactively.The "util" and application code subdirectories each contain their ownMakefile. In addition, there is a global Makefile that recursivelyinvokes all other Makefiles.Host software=============The subdirectory "utils" contains a Python module "usbtiny.py" thatdefines a class USBtiny that can be used to communicate with theUSBtiny firmware. This class is used by the test scripts.The usbtiny.py module uses a Python wrapper around libusb that isgenerated by "swig". Type "make" to build the wrapper. Of course, youneed to have swig, libusb and libusb-dev installed on your system.Libusb needs appropriate permissions to open a USB device. This meansthat you either have to run the scripts as "root", or you shouldconfigure the hotplug or udev system to relax the permissions of thedevice file created under /proc/bus/usb/ when the device is pluggedin. With udev, you can change the permissions by adding a rule like SUBSYSTEM=="usb_device", MODE="0666" to the udev rules in /etc/udev/rules.d/License=======The USBtiny software is licensed under the terms of the GNU GeneralPublic License as published by the Free Software Foundation, eitherversion 2 of the license, or (at your option) any later version. Acopy of the GPL version 2 license can be found in the file COPYING.Author & website================Dick Streefland <dick@streefland.net>http://www.google.com/search?btnI&q=webtag_net_streefland_avr_usbtiny
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -