Access Flash Drives with a Microcontroller
TeraTerm Pro, under Setup > Serial
Port, set Port to the COM port your
device uses and set Baud rate = 9600,
Data = 8 bit, Parity = none, Stop = 1
bit, and Flow control = none. If the
port uses a USB/serial adapter, the
adapter will need to be attached
before TeraTerm will list it. Under
Setup > Terminal, set New-line Receive
to CR+LF and New-line Transmit to CR.
Check the Local echo checkbox.
You’re now ready to start sending
commands to the USBwiz (Figure 4).
If you have another favorite
terminal utility, you can probably use
it by setting the options as described
I’ll show some of the essential
commands for accessing files on Flash
drives. The USBwiz’s User Manual
documents many more commands,
including commands that access
other device types.
Before reading and writing to files
on a Flash drive, the USBwiz must
receive and execute four commands.
USB hosts learn about attached
devices by sending a series of requests
in a process called enumeration. The
device returns a series of data
structures called descriptors, which
describe the device’s functions and
capabilities. A Flash drive sends
descriptors that tell the host that the
device belongs to USB’s mass-storage
class. The host then knows it can use
mass-storage protocols to communicate with the device.
The UI command causes the
USBwiz to enumerate an attached
where p is the port number and h is
the device handle to assign to the
The USBwiz-OEM board has two
USB connectors: Port 0 on the bottom
and Port 1 on top. This command
enumerates the device on Port 0 and
assigns device handle 0:
■ FIGURE 4. The USBwiz communicates via text commands.
The UM command registers an
enumerated USB mass-storage device
and assigns a mass-storage handle for
communicating with the device:
where h is the assigned device handle
and m is the mass-storage device
handle to assign to the device.
This command assigns mass-storage handle 0 to the device with
device handle 0:
After executing the UM
command and returning a status code
of !00, the USBwiz returns “$”
followed by the number of the highest
logical unit (LUN) in the media:
On a PC, each LUN corresponds
to a drive letter. Partitioned drives
have multiple LUNs. Flash drives
typically have just one LUN,
The AM command prepares the
USBwiz to communicate with a mass-storage device and checks to ensure
the storage media is available:
where m is an assigned mass-storage
handle and n is the LUN.
This command prepares to
communicate with LUN 0 in the
device with mass-storage handle 0:
Note that this command uses “<”
rather than “>”.
To read and write to a file, the
USBwiz must mount a file system to
the device. The MU command
mounts a file system to a mass-storage
where m is the file system and n is the
mass-storage handle of a USB device.
The USBwiz supports up to three
independent file systems.
This command mounts file system
0 on the USB device with mass-storage handle 0:
If the USBwiz returns !00 in
response to each of the above
commands, you’re ready to read and
write to files.
Before you can write to a file, you
need to open it. The OF command
opens a file:
where n is a file handle, filename
is the name of the file, and M is
the read or write mode. Set M to
“W” to overwrite any existing file of
the same name, “A” to append to an
existing file, or “R” to read from
This command opens the file
“test.txt” for overwriting using file
If the file doesn’t exist, the
USBwiz creates it.
February 2007 69