USB Mass Storage: Designing and Programming Devices and Embedded Hosts

USB Mass Storage: Designing and Programming Devices and Embedded Hosts

by Jan Axelson
     
 

View All Available Formats & Editions


This developer’s guide for designers and programmers of mass-storage devices that use the Universal Serial Bus (USB) interface provides developers with information on how to choose storage media, interface the media to a microcontroller or other CPU, and write device firmware to access the media and perform USB communications. Comparisons of popular…  See more details below

Overview


This developer’s guide for designers and programmers of mass-storage devices that use the Universal Serial Bus (USB) interface provides developers with information on how to choose storage media, interface the media to a microcontroller or other CPU, and write device firmware to access the media and perform USB communications. Comparisons of popular storage-media options to help users choose a media type for a project are included, and the types described cover hard drives and flash-memory cards such as the MultiMediaCard (MMC), Secure Digital (SD) card, and CompactFlash card. Helpful tips on developing an embedded host that can access USB mass-storage devices are also covered.

Editorial Reviews

From the Publisher

"Jan Axelson knows better than almost anybody else how to write clearly and how to explain complex technical issues."  —EDN

"If a design employs a USB mass-storage device connection, you will need this book. I recommend it highly."  —ECN magazine

ECN Magazine
If a design employs a USB mass-storage device connection, you will need this book. I recommend it highly.
EDN
Jan Axelson knows better than almost anybody else how to write clearly and how to explain complex technical issues.

Product Details

ISBN-13:
9781931448048
Publisher:
Lakeview Research
Publication date:
09/01/2006
Pages:
287
Product dimensions:
7.00(w) x 9.00(h) x 0.54(d)

Read an Excerpt

USB Mass Storage

Designing and Programming Devices and Embedded Hosts


By Jan Axelson

Lakeview Research LLC

Copyright © 2006 Janet L. Axelson
All rights reserved.
ISBN: 978-1-931448-23-9



CHAPTER 1

Mass Storage Basics


A mass-storage device is electronic hardware that stores information and supports a protocol for sending and retrieving the information over a hardware interface. The information can be anything that can be stored electronically: executable programs, source code, documents, images, spreadsheet numbers, database entries, data logger output, configuration data, or other text or numeric data. Mass-storage devices typically store information in files. A file system defines how the files are organized in the storage media.

In Windows computers, mass-storage devices appear as drives in My Computer. From Windows Explorer, users can copy, move, and delete files in the devices. Program code can access files using file-system APIs or .NET's File class.


When to Use a Storage Device

Implementing a mass-storage function is a solution for systems that need to read or write moderate to large amounts of data.

If the device has a Universal Serial Bus (USB) interface, any PC or other USB host can access the storage media. Generic USB mass-storage devices include the hard drives, flash drives, CD drives, and DVD drives available from any computer-hardware store. Table 1–1 lists popular device types. These devices have just one function: to provide storage space for the systems they connect to.

Another type of USB mass-storage device (or storage device for short) is the special-purpose device with storage capabilities. For example, a camera can capture images and store the images in files. A data logger can collect and store sensor readings in files. A robotic device can receive files containing configuration parameters. With the addition of a USB mass-storage interface, any of these devices can use USB to exchange files with PCs and other USB hosts.

Generic storage devices are readily available and inexpensive. Unless you're employed by a storage-device manufacturer, there isn't much point in designing and programming your own generic devices. But special-purpose USB storage devices are useful in many embedded systems, including one-of-a-kind projects and products manufactured in small quantities.

Another option for some systems is to add USB host-controller hardware and mass-storage firmware. The embedded system can then store and read files in off-the-shelf USB storage devices.


Benefits

Adding storage-device capabilities to a system has several benefits:

• With a USB device controller, a system can make the contents of its storage media available to any PC or other USB host computer.

• File systems provide a standard way to store and access data. A PC or other USB host can format the media in a USB storage device to use the FAT16 or FAT32 file system. When the device is connected to a PC, the operating system enables reading and writing to files. Users can access the files without having to install and learn a vendor-specific application.

• Storage media is readily available. Flash-memory cards are convenient and have enough capacity for many applications. Some cards require only a few port pins to access. Devices that need large amounts of storage can interface to hard drives.


Other Considerations

A storage device isn't the solution for every application, however.

• Mass-storage firmware is complex. A USB mass-storage device must support the USB protocols required for all USB devices as well as class-specific mass-storage protocols. If the device firmware needs to create, read, or write to files and directories on its own (not via the USB interface), the firmware must also support a file system. For some applications, a different USB class or a vendor-specific protocol would require less time and expense to implement.

• USB mass-storage devices transfer data using bulk transfers. These provide the fastest transfers on an otherwise idle bus but have no guaranteed timing or bus bandwidth. If your device needs precise timing in transferring data, the mass-storage class isn't appropriate.

• A storage device should have one mass-storage master at a time. The master, or mass-storage host, is the computer that reads and writes to the storage media. Special-purpose mass-storage devices can function as masters on their own and can also permit a PC or other USB host to function as the master. If one master adds, deletes, or changes a file and the other master isn't aware of the changes, confusion or worse problems can result. Devices that support two masters can have a manual or electronic switch to enable one master at a time, or a device can use firmware protocols to inform the host when the media's contents have changed. For some designs, another approach without this added complexity makes more sense.

Alternate approaches for USB devices that transfer generic or vendor-specific data include the human-interface device class, a device accessed via a virtual COM port, or a generic or vendor-specific driver.


Requirements

Adding storage capabilities and a USB interface to an embedded system requires hardware and firmware to support accessing the storage media and communicating over the USB interface.


Devices

An embedded system that functions as a USB mass-storage device requires the following hardware (Figure 1–1):

• A microcontroller or other CPU or intelligent hardware to manage the embedded system's operation.

• A USB device controller, which can be embedded in a microcontroller chip or on a separate chip that interfaces to a CPU or microcontroller.

• A generic hard drive, flash drive, or other media that interfaces to the device's CPU.

In a USB mass-storage device, the hardware or firmware must perform the following functions:

• Detect and respond to generic USB requests and other events on the bus.

• Detect and respond to USB mass-storage requests for information or actions from the device.

• Detect and respond to SCSI commands received in USB transfers. These industry-standard commands read and write blocks of data in the storage media, request status information, and control device operation.

In addition, devices that create, read, or write to files and directories on their own (not via a USB host) must implement a file system. A file is a named collection of data. A directory structure provides an index to the files. Popular file systems for embedded systems include FAT16 and FAT32.

Two popular types of storage media for embedded systems are flash- memory cards and hard drives. A flash-memory card contains flash- memory chips to provide storage, a controller that manages reading and writing to the memory, and an interface to the outside world. Common types of flash-memory cards includes the MultimediaCard (MMC), Secure Digital (SD) Card, and CompactFlash (CF) card. A hard drive contains a hard disk that provides storage, drive components to perform functions such as spinning the disk and positioning the heads, a drive controller, and an interface to the outside world. An embedded system that accesses flash-memory cards or hard drives must have a microcontroller or other CPU or intelligent hardware to manage communications with the cards or drives.

This book focuses on block storage devices, where data is transferred in blocks of defined sizes. USB hard drives and flash drives are block storage devices. Other devices are stream devices, where each data transfer is a sequence, or stream, of data that can be any length. An example of a stream device is a modem that carries voice communications.


Embedded Hosts

An embedded system that functions as a USB host for flash or hard drives requires the following hardware (Figure 1–2):

• A microcontroller or other CPU or intelligent hardware to manage the embedded system's operation.

• A USB host controller, which can be embedded in a microcontroller chip or on a separate chip that interfaces to the CPU, microcontroller, or other intelligent hardware.

• A generic hard drive, flash drive, or other media connected to a USB port on the host.

The hardware or firmware in an embedded USB mass-storage host must provide the following functions:

• Issue USB requests and initiate other events on the bus to identify attached devices and manage traffic and power on the bus.

• Issue USB mass-storage requests that ask for status information or specify actions for the device to perform.

• Issue SCSI commands in USB transfers. The commands read and write blocks of data in the storage media, request status information, and control the device operation.

• Support a file system to access files in the media.


Selecting a Media Type

The storage media is the physical entity that holds a device's data. In embedded systems, a storage device's media is typically separate from the system's program memory, which stores the code executed by the system's CPU. Over time, various storage technologies and form factors have come and gone in popularity. Currently popular technologies include hard drives, CD/DVD drives, flash-memory cards, and USB flash drives (Figure 1–3). Other names for a USB flash drive (UFD) include USB key, pen drive, ThumbDrive®, DiskOnKey®, and JumpDrive®.

The different media types vary in the hardware and circuits required to access the media, the ability to erase and rewrite, methods of write protection, whether the media is removable from its drive, and interface options for external CPUs.

For many devices, flash memory is a good choice for storage media. Flash- memory cards are physically small, can store moderate amounts of data, and manage the low-level protocols for accessing the memory. Some cards require only a few port pins to access. With the addition of a USB device controller and supporting firmware, USB hosts can access the data in a device's flash-memory card. Users can also remove a card from the device and insert the card in a card reader attached to a PC or other computer. Flash memory consumes less power than other media types. When attached to a USB host or hub, a typical flash-memory storage device can receive all of its power from the bus.

Hard drives are the cheapest per byte and can hold massive quantities of data. CD and DVD drives are less common in embedded systems because embedded applications tend to require media that is easily erased and rewritten. CD-RW, DVD-RW, and DVD+RW discs can be erased and rewritten, but not as easily as magnetic media.

A device that contains a USB host controller and supporting firmware can access ordinary USB flash drives and hard drives. Because a USB host must manage the bus, USB host programming is more complex than USB device programming. But for some applications, the ability to store data in generic drives makes the increased complexity worthwhile.


Drive Mechanisms

Hard disks require a drive mechanism to spin the disks and position the read and write heads (Figure 1–4). A hard drive contains a stack of platters. Each platter has magnetic storage media arranged in concentric circles, called tracks, on both sides. Each surface of a platter has a head positioned above the platter's surface. The head can read or write to the bit of data directly opposite the head.

An area on a drive can be identified by cylinder, head, and sector. A cylinder is a stack of tracks of the same diameter. Each surface has a head, so the head identifies a surface on a platter. A sector is a portion of a track and contains the smallest addressable quantity of data in the media. All sectors in a drive have the same capacity, typically 512 bytes.

The drive mechanism spins the disks and moves the heads to requested tracks. When a requested sector on a spinning disk passes under the head, the head performs the read or write operation on the media. The head reads and writes a minimum of a sector's data in each read or write operation.

Flash memory resides in chips. Accessing flash memory requires no moving parts. USB storage devices with flash memory don't have mechanical drives, but the term flash drive for these devices has stuck.


Addressing Methods

All USB drives and other drives of recent vintage support logical block addressing (LBA). With LBA, blocks of storage capability are numbered sequentially beginning at zero. All blocks have the same size, again typically 512 bytes. The logical block address is often referred to as a sector address because the block size equals the capacity of a sector in a hard drive. To access the media, software specifies the logical block address to read or write to. For hard drives, the drive's controller translates each LBA to a cylinder, head, and sector on the drive. For flash drives, the drive's controller translates each LBA to a block, page, and column in the memory array. The sequence of logical block addresses doesn't have to correspond to the physical locations of the sectors in a drive or the memory in a chip. All that matters is that the media's controller knows what area of storage corresponds to each address.

In older systems, software accessed storage media using CHS addressing, where the software specifies a cylinder, head, and sector number to read or write to. A storage device can support both CHS addressing and LBA.

Compared to CHS addressing, LBA is simpler, more flexible, and supports larger capacities. File-system drivers in embedded systems are unlikely to need to use CHS addressing.


Reading and Writing Considerations

Storage media varies in the available methods of write-protecting the contents, support for erasing, and copy-protection technologies.


Write Protection

The storage media, drive mechanism, circuits, or a manual switch can permit or forbid writing to the media. For example, a flash-memory controller can forbid writing to all or a portion of the memory. Or a manual switch on a flash-memory card can inform the host that the media shouldn't be erased or overwritten. Higher-level software in the mass-storage master can also control access to data on a storage device.


Erasing

The media in a hard drive can be erased and rewritten virtually endlessly, while flash memory can survive 10,000 or more erase cycles, depending on the technology. Some memory cards contain programmed ROM chips, which can't be erased and rewritten.

The flash memory used in storage devices must be read and written in pages and erased in blocks. The page size for read and programming (write) operations is typically either 528 bytes (small block) or 2112 bytes (large block). A 528-byte page can hold one 512-byte sector and 16 additional bytes for error-correcting codes (ECC), address-mapping information for use in wear leveling, and other information. A 2112-byte page holds four 512-byte sectors with 16 additional bytes per sector. Newer memory chips tend to use large blocks.

The block size for erase operations is much larger than the page size for reading and writing. In the past, blocks of 16 KB and 32 KB were common, while current flash memory has erase blocks of 128 KB or 256 KB. Before writing to previously programmed memory, the area to be written must be erased. So to write even a single byte to a previously programmed area, the memory controller must erase an entire erase block and then program a page's contents back into the memory.

The controllers in flash-memory cards manage the erase operations and enable device firmware to work with 512-byte blocks. To write a byte to a flash-memory card, device firmware typically reads 512 bytes into a buffer, changes the byte to be written, and writes the buffer back to the memory card. The card's controller handles the erase and write operations.

The controllers in flash-memory cards use wear-leveling techniques that help extend the useful life of the memory array by spreading erase/write cycles evenly among all of the memory cells. A file-system driver that accesses raw flash-memory chips can implement wear leveling as well.


Security

Some media types have built-in copy-protection capabilities. For example, an SD Card can be configured to require authentication before allowing access to the card's contents, and a card can restrict the number of allowed copies.


Removable Media and Devices

A device can have removable media, and an entire device can be removable from the computer that communicates with the device.


Removable Media

In a drive with removable media, users can easily insert and remove media in the drive. CD and DVD drives have removable media because you can easily swap discs. A memory-card reader with a card slot has removable media. Hard drives and flash drives have non-removable media because you can't easily remove the hard disk from its drive or the flash memory from its circuit board. A device reports whether it has removable media in the response to a SCSI INQUIRY command. Some flash drives with non-removable media report that they have removable media. Chapter 6 has more about the INQUIRY command.


Removable Devices

An entire storage device can also be removable or non-removable from the computer that accesses the drive. USB drives are removable. An internal drive is considered non-removable because removing the drive requires more work than detaching a cable.


Managing Removal

A user can detach a USB device or remove a flash-memory card at any time. If a device or card is removed while the host is writing to the media, the device and host should detect the removal and handle it as gracefully as possible.


(Continues...)

Excerpted from USB Mass Storage by Jan Axelson. Copyright © 2006 Janet L. Axelson. Excerpted by permission of Lakeview Research LLC.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.

Read More

Meet the Author


Jan Axelson is the author of seven books about computers and electronics technology, including Embedded Ethernet and Internet Complete, Serial Port Complete, and USB Complete. Her articles have appeared in Circuit Cellar, EDN, Embedded Systems Programming, Nuts & Volts, and Popular Electronics. She lives in Madison, Wisconsin.

Customer Reviews

Average Review:

Write a Review

and post it to your social network

     

Most Helpful Customer Reviews

See all customer reviews >