Implementation USB into microcontroller: IgorPlug-USB (AVR) using pic microcontroller
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. .
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.
For more detail: Implementation USB into microcontroller: IgorPlug-USB (AVR)
Current Project / Post can also be found using:
- design and implementation of store the information pic microcontroller into USB