Monday, 13 February 2012

Implementation USB into AVR microcontroller

Implementation USB into microcontroller: IgorPlug-USB (AVR)

  Purpose of this article is to inform readers about implementation USB interface into singlechip microcontroller, which this interface directly not supports. Simply: implementation USB interface on firmware level (similar as emulation of RS232 interface in microcontrollers, which not have RS232 support). This project includes development of firmware on microcontroller side, driver development on computer side (for Windows operating system) , development of DLL library for functions calling from another programs (programmers level) and development of demo program (users level), which shows all functions of this device. Device is named IgorPlug-USB (AVR) (as successor of my previous device for computer remote control IgorPlug - serial port version).


    At present time is USB interface very popular especially within end user. This is due to the simplicity to the end users (Plug and Play, without restart). For developers is implementation of USB into their devices more complicated (with comparison to RS232 is USB more complex protocol). In addition there is need of software support on PC side - device drivers. Therefore more devices from small vendors are inclined to RS232 communication. This interface is probably the oldest in PC history and has good operating system support. But in latter years is RS232 removing from standard included PC interfaces (new computers hasn't RS232). In this case help only add-on PCI card with this interface.
    Implementation of USB into external devices is at present time solved in two choices:
    a) First is using microcontroller, which have hardware implemented USB interface. Then is necessary to know how USB works and according this write firmware into microcontroller. And in addition is necessary to create driver on the computer side (while operating system not includes it - e.g. standard USB classes). Disadvantage (and this is the main disadvantage for small vendors and amateurs) is not good availability of this microcontrollers (PIC, Cypress, Atmel, Intel, ...) and their high price (with comparison to simple "RS232" microcontrollers).
    b) Second option is to use some universal converter between USB and "another" interface. This "another" interface depends on type of convertor: there are used especially RS232, 8-bit data bus, I2C bus. In this case is not need of special firmware (actually we needn't to know how USB works) and no need of driver writing (vendor of converter offer free drivers). Disadvantage is higher price of the complete device and greater dimensions of product (need of one chip more).
    My solution, which I have resolved to develop - and successfully finished ;-)  - is USB implementation into cheap microcontroller through the emulation of USB protocol in firmware of microcontroller. Problem during design was microcontroller speed. Speed of USB bus is too high: LowSpeed - 1.5Mbit/s, FullSpeed - 12Mbit/s, HighSpeed - 480Mbit/s. Normal microcontrollers are maximum performance cca: AT89C2051 - 2MIPS = 24MHz/(12cycl/inst.), PIC16F84 - 5MIPS = 20MHz/(4cycl/inst.), AT90S23x3 - 10MIPS = 10MHz/(1cycl/inst.). There exist microcontrollers with highest speeds too, but they are poorly available (price too) and are biggest (more pins - difficult construction). For all that I have decided for microcontrollers PIC16F84 or AT90S1200/AT90S23x3, which would be "enough" for LowSpeed USB. For highest USB speeds is this solution not good - for one bit from USB processing is need of several cycles of microprocessor: reading, comparison, storing, some operation, ... .
    Firstly I choose PIC16F84-20, with which I have experiences. To ensure synchronizing with USB clock and speed increasing was used microcontroller clock 24MHz = 6MIPS (PIC was slightly overclocked). For one bit from USB processing were 4 instructions (4=6MIPS/1.5Mbit for LowSpeed USB). I reject this solution (after some time) - PIC is too slow (and in addition the no so good instruction set ...) .
    Second try was AT90S1200-12, which satisfied me with its speed - possible to receive and transmit signal on LowSpeed USB speed. But I reject this solution too - absence of enough program and data memory - simply small memory to implement this.
    Third try was successful: AT90S2313-10 (or AT90S2323-10, AT90S2343-10). This are RISC microcontrollers from Atmel  production - AVR family. With comparison with PIC are slow crystal clock, but have 1 instruction per crystal clock (PIC16F84 has to 1 instruction per 4 crystal clock). In addition their instruction set and architecture is nearly to RISC. Again because of synchronization with USB clock I used overclocking to12MHz (AT90S23x3-10 are initially to10MHz only). In this manner I obtained more performance and 12MHz crystals are "easy to obtain" (with comparison to e.g. 10.5MHz - which is the multiple of LowSpeed USB clock 1.5Mbit/s too).


    My solution offer very simple and cheap hardware (cca 3,-US$). All intelligence is in firmware. Construction is designed as infrared remote control of computer through USB (successor of construction for serial port ), but this is universal USB interface too.
This device allow:
  • receiving of infrared code (time diagram of received code) (it is transmitted to serial line in real time too) (receiving of infrared code makes  microprocessor without PC - zero usage of PC CPU)
  • control of 8-bit input-output data gate (every bit can be independently input or output, there can be independently controlled pull-up resistors on input pins)
  • reading and writing of internal 128 byte EEPROM (storing of data after no power state - for users is accessible all memory)
  • transmitting or receiving char via serial line
    (now only one non buffered char - but by firmware change is possible to make internal buffer (for injury of infra buffer) : like FTDI chips)
  • change of baudrate of serial line in range cca 4800Baud to 700 000Baud (after power on is 57600baud)
    ("Classical" baudrates are: 4800, 9600,19200,38400,57600. Device DLL automatically detects invalid baudrates.)
Thanks to versatility is possible to use microprocessor on place of universal converter e.g. in application where we want to control (read and write) some input-output pins. Possible application are e.g.: to pins we directly connect I2C thermometer, inputs and outputs of  security device, or simply only  LED diodes or LCD/LED display etc., etc., etc. .
On the next picture is schematic:
    with microprocessor AT90S2313-10 (universal interface):

or with microprocessor AT90S2323-10 (only for IR code receiving) - not tested yet, but designed for this purpose - was tested by users - WORKS GOOD:

and physical construction (IR receiver: computer remote control for program Girder):

   Simplicity of device is truly wondrous. And is accessible for amateurs too - for their devices connection to USB (this always enjoy - I know this according my experience, when Windows first time find this "New hardware found" ;-) ). Firmware - the heart of device - is the same for both microprocessors. In 8-pin version (AT90S2323-10) you can use only infrared code receiving and writing/reading to/from EEPROM. But 8-pin version is by dimension probably the smallest device connected to USB (SMD version).

Installation of device IgorPlug-USB (AVR):

    For success communication between USB device and computer, there is necessary to give driver for device. This driver is requested by operating system during first connection IgorPlug-USB (AVR) into USB connector in computer. In the future maybe this driver will not be necessary : by change in firmware is possible, that device will be standard USB class (HID class, Storage Class ,...), - for this classes has operating system build-in drivers. Advantage of own driver is, that we have control of driver behavior. I choose for "own" driver only because of learning "how this works" and mainly for simplicity of implementation into firmware of device.
    Next pictures shows device installation steps (and driver installation) on the first plugging IgorPlug-USB (AVR) into USB connector. Driver is the same for Windows98, WindowsME, Windows2000 and WindowsXP  (under Windows95 not works):
Firstly under WindowsXP:
  Windows find device IgorPlug-USB (AVR).
  Choose install from our location.
Here we browse to folder with installation files (INF file and SYS driver)
 Windows do some necessary changes.
Driver have not digital signature from Microsoft - but we not stop installation.  Windows make backup (for safe recovery ;-) ).
And in case when Windows don't to find SYS driver, ...   ... then we give to him correct path to driver. And installation is finished:

And under Windows98:
The same as under WindowsXP, only in another visual form.

    After success installation we can look at device in "Device manager":
Device Manager under WindowsXP:

Device Manager under Windows98:

Software support:

    As I mentioned - for correct device operation is necessary driver, which is requested by operating system during installation. This driver I not wrote alone - I used one freeware USB driver and for this driver I adapted firmware. Thanks to versatility of this driver this solution is conform to me. I only slightly modify this driver I wrote in Microsoft Windows2000 DDK enviroment (and has some improvements - comparison with old driver). Name of this driver is "IgorPlug.sys" and is included in installation pack.
    For installation is necessary installation descriptive file INF. Just this file was requested by Windows during installation of device and we must browse to folder with this INF file if we are asked for driver location. In this file is install information for given device - and according to this information will be installed driver and eventually will be made other actions. I was created this INF file according to template file from my USB book. Because every USB device must have assigned vendor number (VID) from USB-organization (USB-IF), then I  "borrow" this number from company Atmel (I hope that Atmel excuse me this borrowing - I won't to pay cca 1500 US$ for number assignment) and I implement it into microprocessor and into INF file. Name of this file is "IgorPlug.inf" and is included in installation pack. So for device installation are necessary only two files ("IgorPlug.sys" a "IgorPlug.inf").
    By this way we had successfully installed device IgorPlug-USB (AVR). And we can go to test it. For this purpose I had written DLL library "IgorUSB.dll", which contains functions, which device IgorPlug-USB (AVR) makes. This DLL library communicates with driver "IgorPlug.sys"  and provide serialization of communication (when we are using DLL from more threads or applications). Library DLL is primary dedicated for programmers of end user applications for access to device: exported functions from DLL are published.
    Next pictures shows information about DLL library and SYS driver as was displayed under WindowsXP:
    Finally I had created end user application - exemplar program "AVR to USB tester", which shows all functions of device IgorPlug-USB (AVR). It call functions from DLL library "IgorUSB.dll" and is in fact the sample program.
Program functions:
  • setting and reading of 8-bit data port (see schematic)
  • port bits flow control and back reading of state of this flow control
  • control of pull-up resistors on data pins (pull-up enabled, or high impedance input) and  back reading of this state
  • reading data pins level state (refresh every 200ms)
  • setting LED diodes, which are connected to data pins through resistors (cathode on GND, is necessary to set all bits as outputs)
  • reading data from all EEPROM
  • writing data to all EEPROM
  • writing - transmitting data to microprocessor serial line
  • receiving char(s) from serial line (polling every 100ms)
  • changing serial line baudrate (invalid baudrate - baudrate with error greater than 4% -  is displayed as crossed number, unsuccessful set is signaled by red color in baudrate box)
  • back reading of internal true baudrate (polling every 100ms)
  • receiving of oscilloscopic record of infrared code received into internal microprocessor buffer (refresh every 50ms)
    (received infrared code is displayed in graphical form as on oscilloscope - its decoding must make another application)
    (during infrared code receiving is this code transmitted to serial line too - but "on the fly" - therefore is necessary to set high baudrate of RS232 - therefore after power on is baudrate 57600 Baud)
Next picture shows functions of demonstrational program AVR to USB tester and its properties (in WindowsXP style):

    For programming AVR microcontroller is necessary programmer. But commercial  programmer is for most users hard to obtain because its high price. Therefore I design simple LPT programmer of this microlontrollers - simple connection to computer LPT port. For this programmer I wrote control software -  inside it is also firmware of device IgorPlug-USB (AVR) (only start program and burn microcontroller) and also schematics of programmer (for both microcontrollers: AT90S2313 and AT90S2323).
Here is schematic of programmer and its control program (works under WinXP too):

You can download control software in download section.

Something about development:

    Development of this device was cca 4 months long (every evening after work cca 4 hours - long evenings ;-) ) + previous study and knowledge about USB and AVR microprocessors. Therefore this was not too easy (I must all study, watch, try alone). But now is no problem for me to change some function of microcontroller  (e.g. omit "infra-buffer" and instead it to implement RS232 buffer). And USB is now nothing "unknown" for me (from the physical level to end user application).
Idea of this project was in my head cca 2 years (after my first download of USB specification) - reborn was after similar project of one Russian "developer", which make this - but this was only his All Fools Day joke (but at that time I give the lie to his device). But his project was impulse for me to my project activity and to slogan : nothing is impossible ;-) .
    The biggest "unknown" was understand all USB specification (but really all - from the physical level to end user level). Therefore study of USB was very important. The main information source was USB specification (free download from and lately book "USB  Měření, řízení a regulace pomocí sběrnice USB" from publisher BEN . And of course various internet sources.
    During development I was using for testing many "test" a "information utility" downloaded from internet (USBcheck, DisplayUSB, SnoopyPro, Fiddler, ...). For firmware development I was using free tool "AVR Studio 4" (from Atmel-u pages) and programmer "T51prog" (from Elnec company). For "physical observation" was used "Scopemeter FLUKE 123" (wonderful device) from FLUKE . As template design of USB device was for me USB mouse "Genius NetScroll+" from Genius company (bought in Agem-e). The microprocessor (and the "unsuccessful" too) I has bought in GM-electronic . So Many Thanks to all for help.
    I was using for development next programs and many thanks of their creators:

 And who want to see more pictures from my "development place" , here are: DevelopmentBoard1, DevelopmentBoard2, DevelopmentBoard3, DevelopmentBoard4, DevelopmentPlace1.JPG, DevelopmentPlace1, DevelopmentTools1, DevelopmentToolsElnecProgrammer, TargetDeviceAtmelAVR, TargetDeviceToUSBcable, LiteratureAndInformationSource.
And some information about my device IgorPlug-USB (AVR) as  was dislayed by test utilities: USBDesignByExample, USBCheckFullTest, USBCheckDeviceDescriptor, USBCheckConfigurationDescriptor, USBCheckInterfaceDescriptor, USBCheckEndpointDescriptor, USBCheckViewDescriptors, USBCheck, SnoopyProDeviceDescriptor, SnoopyProConfigurationDescriptor, SnoopyProVendorCommand, ......enough (for unfamiliar persons are this not so clearly).
For the persons concerned to build this device as USB receiver like small USB key USBkey remote receiver I prepared documentation: PCB layout, schematic and PCB devices location. This documentation you can see here as PDF file.


    Firmware into microprocessor is available in HEX-file and now also as source code (I publish it for more users requests - for adding your own functions). And now I publish documentation of PC software support (that was in the past only in full version). Of course: commercial use is not free. As freeware I publish e.g. computer remote control on serial port , (and all other my projects) and even if I was requesting small development fee from satisfied users, so from cca 20 000 downloads (about which I know) will be helpful only one person (4US$ - thank you) (although time for answering to cca 1500 e-mails is too much). So there is rule - if there is something free only per voluntary - no wait from anybody. 
    Because I want that device will be available for more users, then for download is : IntelHEX file for AT90S23x3-10, driver , DLL-library for access to IgorPlug-USB (AVR) and demonstration program for control of IgorPlug-USB (AVR) (so all :-o ).
    In the next I publish source code of firmware of microcontroller and also documentation of DLL library (for writing your own applications). Documentation is for Delphi, C++Builder/Visual C++, Visual Basic. So everybody can try how the device work.
    For fast device construction is available also simple LPT programmer of given microcontrollers (AT90S2313-10 and AT90S2323-10). And for less experienced users is small installation guide (step by step).
    There is only one limitation of this "free" version - DLL library shows information window during the first run (functions is the same). For your own application will be better to buy registered version of DLL library. For vendors, which want to sell complete product:: programmed microprocessor + CD with full version of software and documentation, I offer full versions for distribution (of course with my copyright). According this way I want to remove problems with payment - end users will be buying from internet shops of local vendors in your country.
Price for end user will be cca 150,-Sk (Slovakia) or 4,-Euro (Europe) or 4,-US$ (other world). For distributors will be of course discount (after contract with me).
    The persons concerned for documentation and for commercial usage of product can contact me by e-mail.
    For utilization of product as computer remote control - is recommended to read article IgorPlug .
    Here is the download (ZIP archive):   IntelHEX into AT90S23x3-10, installation files (driver), DLL library (free version), exemplar application. This files can be changed in the future (according to requests there will be changing only DLL library, SYS driver remain the same).
For programming of microcontroller through LPT port is necessary to download IgorPlugUSBprogrammer.
And developers will be interested in package: firmware source code into AT90S23x3 + interface to DLL library IgorUSB.dll (please don't abuse). DLL function descriptions are for Delphi, C++Builder/Visual C++, Visual Basic.

No comments:

Post a Comment