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.
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.
Table 1-1: Common USB mass storage devices use a variety of storage media.
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.
Figure 1-1: A USB mass-storage device contains storage media, a media controller, a device CPU or microcontroller, and a USB device controller, which can be on a separate chip or embedded in a microcontroller.
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.
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.
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.