Give away medical masks when you place an order. learn more

MCU Peripherals: Tips, Tricks for Using USB in Embedded Applications - Part I



The same simplicity and versatility that made Universal Serial Bus (USB) such a popular interconnect technology for computer and consumer applications is also winning fans in the embedded world. What’s the best way to implement USB in your next embedded design? Is it better to use an MCU with an integrated USB interface or a separate controller? Do different solutions place different demands on an MCU’s memory and processing capacity? What does it take to minimize a design’s footprint, power consumption, and BOM costs? And are there any hardware or software tricks that can improve its performance or energy efficiency? We’ll address these and other embedded design questions in this article.

Let’s start with the essentials.

Embedded USB

USB gained its initial popularity with embedded developers during the late nineties as most PCs began to arrive on desktops equipped with USB ports. MCU manufacturers quickly realized that USB’s higher data rates, commonly-available cables, and ability to provide modest amounts of power made it a convenient alternative to teletype-era serial interfaces (RS232, UARTs, etc.) and cumbersome JTAG port connections to program, monitor, and communicate with their development platforms. Most MCU makers quickly adopted USB as the primary interface to their evaluation platforms and development kits (Figure 1).

Figure 1: The USB interface has made it possible for MCU/DSC makers to offer stand-alone development kits that can be configured, managed, and powered by a single PC connection (a). In most cases, additional power supplies are only necessary for higher-powered applications, such as lighting or motor control (b). (Courtesy of Microchip Technologies.)

Meanwhile, USB moved quickly beyond its initial role as a computer peripheral connection (mice, printers, webcams) to become an important interface in many consumer, commercial, and industrial applications. Its ability to serve as a consistent, readily available power source and data connection has made it a near-universal feature on mobile phones, cameras, media players and other handheld consumer products. Because USB also allows any PC to serve as a control panel to handle data access, configuration management, and firmware updates, it’s become the connection of choice for everything from glucose meters and fitness equipment to industrial dataloggers.

Until recently, adding USB capabilities to an embedded system was accomplished using an external device connected to the MCU. For these designs, USB-UART bridges, such as Microchip’s MCP2200 or Silicon Labs’ CP2112-F02-GM, were an easy way to use an existing RS-232 or RS-422/485 port for USB communications (Figure 2). For windows-based embedded applications, HID USB-to-SMBus/I²C bridges like Microchip Technologies’ 2210 or Silicon Labs’s CP2112 enables keyboards, mice, and other HID Class devices to communicate with most operating systems without installing any drivers.

Figure 2: A USB-to-UART bridge makes it easy to add USB connectivity to legacy embedded systems (Courtesy of Silicon Labs).

The levels of integration available in MCUs grew in parallel with USB’s increasing popularity so it’s no surprise that so many of today’s microcontrollers offer one or more variants with on-chip USB capability. The integrated USB capabilities of Atmel’s ATmega8U, ATmega16U, and ATmega32U 8-bit MCUs are why they were selected to power the original Arduino open source hardware platforms. Likewise, many versions of Microchip Technology’s 8-bit PIC18F, 16-bit PIC24F, and 32-bit PIC32MX5XX (such as the PIC32MX575F) MCUs are equipped with USB/USB OTG interfaces. Texas Instruments has also aggressively embraced USB across many of its embedded processor families, including its versatile MSP430F55xx and MSP430F66xx series (such as the MSP430F663) 16-bit MCUs. In addition, TI was one of the first manufacturers to introduce a series of complete ultra-low-cost embedded development systems, such as the EZ430D/U “dev kit-on-a-stick,” which is housed in a USB flash drive case (Figure 3).

Figure 3: Texas Instruments’ self-powered MSP-EZ430D/U “dev kit-on-a-stick” was both a novel demonstration of the value of USB-enabled MCUs and an ultra-low-cost platform that helped popularize the 16-bit MSP430 architecture (Courtesy of Texas Instruments).

Before we dig into the details of how USB is implemented in embedded systems, it’s probably a good idea to take a quick look at some of the basic elements of the USB spec that give it such versatility and popularity.

USB Specs

Conceived originally by Intel, Microsoft, IBM and several other equipment makers as an inexpensive means of providing a logic interface and modest amounts of power to end node devices keyboards, mice and other simple peripherals, USB has evolved into the de facto universal interconnect for everything media players to medical equipment.

The USB 1.1 specification, which is most commonly used in embedded applications, defines a "Low Speed" mode with data transfer rates of 1.5 Mbits/s and a "Full Speed" rate of 12 Mbit/s. The later USB 2.0 spec includes, among other things, “USB On-The-Go” (OTG), which defines a protocol for two USB devices to communicate with each other without requiring a separate USB host. USB 2.0 also defines a “high-speed” version of USB, capable of supporting transmission rates of 480 Mbits/s. While many MCU-based USB solutions support USB 2.0’s OTG feature set and other improved file handling capabilities, USB 1.1’s 12 Mbit/s data rates are sufficient for most embedded applications, and only a handful of USB-enabled MCUs include the power-hungry PHY and extra silicon required to support the high-speed mode.

From an electrical standpoint, USB is designed to use an inexpensive shielded cable containing a twisted-pair and two wires for the power and ground lines (Figure 4). The twisted pair (D+ and D-) is responsible for carrying a differential data signal as well as some single-ended signal states. The D+/D- signals are referenced to the GND wire. The fourth wire (VBUS) carries a nominal 5 V supply, which may be used by a device for power.

Figure 4: USB cabling.

The USB electrical PHY supports a tiered-star topology and a “host-centric” media access control (MAC) scheme in which the host must initiate all transfers, both outbound and inbound. In this topology, each wire segment is a point-to-point connection between the host and a hub or function, or a hub connected to another hub or function and there can be no communication directly between USB devices. USB’s addressing scheme allows up to 127 devices to be connected to a single host in any combination of hubs or peripherals. A compound or composite device (such as a web cam with a microphone) will occupy two or more of these 127 device addresses.

To prevent operator-induced calamities, the USB spec assigns physically-different connectors to the host and device interfaces. There are several "A-type" receptacles which point downstream from a Host or Hub, and matching "B-type" receptacles that point upstream from a USB device or hub. In order to make USB connections practical for small handheld devices (cameras, phones, media players), several compact connector form factors have been introduced. The most popular of the newer connector types is the “Micro-USB,” which has been adopted by virtually all cell phone manufacturers as a standard charging port (Figure 5 and Table 1).

Figure 5: USB connector types.

USB 1.x/2.0 standard pinout
Pin Name Cable color Description
1 VBUS Red +5 V
2 D— White Data —
3 D+ Green Data +
4 GND Black Ground
a)

USB 1.x/2.0 Mini/Micro pinout
Pin Name Cable color Description
1 VBUS Red +5 V
2 D— White Data —
3 D+ Green Data +
4 D None Permits distinction of host connection from slave connection
  • host: connected to Signal ground
  • slave: not connected
5 GND Black Signal ground
b)

Table 1: Pinouts for type A and B USB connectors (a). Pinouts for USB Mini/Micro connectors (b).

Data exchanges with USB

Data is exchanged between a USB controller and its related devices using several different types of transfers:

Interrupt Transfers

So-called interrupt transfers have the highest priority and involve packets of 1 to 8 bytes at low speed, 1 to 64 at full speed or up to 1024 bytes at high speed (USB2.0). They are misleadingly labeled because they are used to handle the same sorts of time-critical tasks (such as monitoring a mouse or a keyboard) for which an interrupt would have been used in earlier connection types.

Isochronous Transfers
Isochronous transfers have a guaranteed bandwidth, but error-free delivery is not guaranteed. Isochronous transfers are used primarily for applications such as streaming audio data, where the data flow must be maintained and some level of data dropout or corruption can be tolerated. Depending on the type of endpoint, an isochronous transfer uses either an IN transaction or an OUT transaction with no handshake packet at the end. Isochronous packets may contain up to 1023 bytes at full speed, or up to 1024 at high speed. Isochronous transfers are not allowed at low speed.

Bulk Transfers
Bulk transfers are designed to transfer large files with error-free delivery on a best-effort basis. To avoid interfering with time-critical operations (such as streaming audio), bulk transfers have a lower priority and are scheduled for execution by the host after any other pending transfers. Bulk transfers can move 8, 16, 32, or 64 byte blocks at a time over Full Speed (USB1.1) connections and 512 bytes over high speed (USB2.0) connections. Bulk transfers are not allowed for low speed connections.

Control Transfers
Control transfers are used for initial configuration of the device by the host, using Endpoint 0 OUT and Endpoint 0 IN addressing, which are reserved for this purpose. This is a bi-directional transfer that uses both an IN and an OUT endpoint. Each control transfer is made up of two or more transactions and may be used after configuration as part of the device-specific control protocol. The maximum packet size for the data stage is 8 bytes at low speed; 8, 16, 32, or 64 at full speed; and 64 for high speed.

USB functionality classes

A USB device can support one or more classes of functionality. The device contains a number of descriptors which help to define what the device is capable of, including the USB specifications it supports (1.0, 1.1, 2.0), its device class(es) and product ID information.

USB devices are designed with specific function sets, known as classes. Devices describe their capabilities to the USB host by sharing 8-bit tags, known as “class codes” so the host can select and load the appropriate software drivers. A class code is usually read as a two-digit hexadecimal number (i.e. 0Bh for a USB Smart Card Reader). Other common device classes include:
  • 02h Audio (Speaker, microphone, sound card, MIDI port)
  • 03h Human interface device (HID)
  • 05h Physical Interface Device (PID)
  • 06h Image (still webcam, scanner)
  • 07h Printer
  • 08h Mass storage class (MSC)
  • 0Eh Video (webcam)
  • 0Fh Personal Healthcare (pulse monitor)
Devices identified with the Vendor-Specific Class (FF) tag require non-generic drivers from a specific vendor.

Power matters

One of the features that made USB so wildly popular is its ability to run small 'bus-powered' devices such as webcams, memory sticks, low-power hard drives, not to mention all manner of cubicle toys (Figure 6), directly off its 5 V (nominal) supply. In normal operation, a bus-powered device may draw up to 100 mA, and up to 500 mA if permitted to do so by the host. The standard unit load available is 100 mA. No device is permitted to take more than this before it has been configured by the host during the connection sequence. The USB spec requires that devices that draw more than 500mA must be self-powered. It should also be noted that a device which has been 'suspended,' as a result of no bus activity, is required to reduce its current consumption to 0.5 mA or less.

If you wish to dig deeper into the USB electrical spec or protocols, the USB Reference section below provides several excellent sources for in-depth information about the USB family of interface standards.

USB References/Resources

The USB alliance website - www.usb.org.

If you need a coherent tutorial on the basics of USB, try “USB in a Nutshell” located on Craig Peacock’s www.beyondlogic.org website.

MPQ Electronics also publishes an excellent tutorial: “USB Made Simple,” available at http://www.usbmadesimple.co.uk/.

Jan Axelson, author of "USB Complete" and several other books, has an excellent USB resource website at http://www.lvr.com/usb.htm.
Supplier