Beruflich Dokumente
Kultur Dokumente
Author: Kim Otten Most USB peripheral devices are divided into
Microchip Technology Inc. categories, called classes. Each class has special
requirements regarding its communication format. The
host must be able to recognize a devices class and
INTRODUCTION meet the classs requirements or the host cannot com-
municate with the device. Two example classes are
USB has become the standard method for devices to HID (Human Interface Device), such as found on a
communicate with a PC. From general purpose mouse, and Mass Storage, such as found on a Flash
devices, such as Flash drives and mice, to special pur- drive. Client drivers provide application level support
pose devices for specific applications, this popular for classes. Some USB peripheral devices are vendor-
standard has almost totally replaced other serial specific and do not fall into one of the predefined
communication protocols. classes. Client drivers must be specifically written for
Under the USB standard, USB devices may not these devices.
communicate directly with each other. They may only The number of devices that can attach to a host can be
communicate with a USB host which controls the bus expanded through the use of hubs. USB is a tiered star
on which one or more devices communicate. The most network. Typically, a hub allows four or seven devices
common USB host is a PC. With the introduction of to attach to a single port. A maximum of five hubs can
Microchips microcontrollers with the USB On-The-Go be chained together, creating up to five tiers. A
(OTG) module, it is now possible for embedded maximum of 127 devices (including the hubs) can be
applications to utilize the wide range of USB devices as connected on the bus.
a USB embedded host.
A full USB host uses a Type-A receptacle, and must be
able to communicate with any device. This support may
USB OVERVIEW be provided via special drivers that must be installed on
the host prior to attaching the device. Hubs must be
There are many excellent references on USB, including
supported, and each port must be able to deliver a
the USB 2.0 Specification itself, that provide detailed
minimum of 100 mA.
information on USB operation. This section is intended
only to provide a brief definition of the terms referenced
in this application note or required to understand Stack Host vs. Embedded Host
operation. A USB embedded host differs from a USB host in
several small but important aspects. A USB embedded
USB Hosts and Peripheral Devices host:
A typical USB system consists of one host and one or Supports only specific peripheral devices and/or
more peripheral devices, often referred to as simply classes of devices.
devices. Each device can communicate only with the Supports only transfer types required by the
host; devices may not communicate directly with each supported devices.
other. The host initiates all communication on the bus. A Hub support is optional.
device may send data to the host only when the host
Has relaxed power requirements.
requests it, and it must be able to receive the data that
the host sends. A device normally uses a Type-B These restrictions allow an embedded host to be
receptacle or has a captive cable. implemented on a device with fixed, limited memory.
The USB Embedded Host Stack interfaces with the explicitly issue commands to transfer the two OUT and
USB OTG module in Host mode at the packet level, two DATA0/1 packets. When the embedded host
with the exception that ACK packets are handled auto- issues the control transfer shown in Figure 2, it must
matically by the module. When the embedded host explicitly issue commands to transmit all SETUP,
issues the bulk transfer shown in Figure 3, it must DATA0/1, IN and OUT packets, for a total of 6 packets.
Dual Role Device or On-The-Go? initially act as an embedded host. If the micro-B plug of
the cable is inserted, the device will initially act as a
A device can be a dual role device by supporting both peripheral device.
embedded host and USB device functionality via two
receptacles. For example, a digital camera can act as The choice of whether or not to utilize OTG depends on
a peripheral device when downloading pictures to a the need for the two devices to dynamically switch
PC, and act as an embedded host when transferring modes. In most cases, the host-peripheral role is con-
pictures to a printer. The camera would determine its stant for a pair of communicating devices. For example,
role as device or host depending on what cable was the dual role digital camera described above would
attached. If a cable was attached to the Type-A never need to swap roles with the PC, or with the
receptacle, the camera would act as a host. If a cable printer, so it would not require OTG functionality.
was attached to the Type-B receptacle, the camera There are also restrictions placed on the Targeted
would act as a device. The Type-A and Type-B recep- Peripheral List of an On-The-Go device. The TPL of the
tacles must both operate concurrently unless they are OTG device must list only specific devices; it may not
only accessible one at a time. list supported classes. For example, an OTG device
A USB On-The-Go device is a device that can cannot generically support USB Flash drives. An OTG
dynamically change its role from an embedded host to device may support a non OTG peripheral, but it must
a peripheral device without changing cables. OTG do so by specifying the VID and PID of that device. A
devices use a micro-AB receptacle. The initial configu- TPL for an OTG device that supports Microchips
ration is determined by the cable orientation. If the PICDEM FS USB demonstration would look like
micro-A plug of the cable is inserted, the device will Table 2:
INIT
Attach
Interrupt
ATTACHED
Validation STATE
Failed
DETACHED Validation
STATE Successful
Deattach
Interrupt
Deattach Deattach
Interrupt Interrupt ADDRESSING
STATE
Deattach
Interrupt
RUNNING
STATE Address
Assigned
Device
Deconfigured
CONFIGURING
Device STATE
Configured
Application-specific class support can be configured The oscillator source must operate at a frequency that is
with other tabs. Refer to the associated USB applica- a multiple of 4 MHz. This frequency is divided down to 4
tion notes for more information about the classes and MHz by the USB PLL prescaler. The USB PLL prescaler
their client drivers. does not automatically sense the incoming oscillator fre-
quency. The user must manually set the PLL divider
PIC24F Device Requirements appropriately to generate the required 4 MHz prescaler
output, using the PLLDIV2:PLLDIV0 Configuration bits.
The USB OTG module requires a 48 MHz clock. Since This limits the choices for primary oscillator frequency to
this is beyond the maximum CPU clock speed, there is a total of 8 possibilities, shown in Table 3.
a method to provide both the CPU and USB clocks
from a single oscillator source. Note: The tolerance of the FRC oscillator is
greater than the USB specification allows;
therefore, FRC is not recommended.
The TPL can be defined using the configuration tool, typedef BOOL (*USB_CLIENT_INIT) ( BYTE
USBConfig.exe, provided with the Stack. See the address, DWORD flags );
Configuring the Stack section. This function performs device initialization specific to the
devices class. If initialization occurs with no error, this
routine should return TRUE. If errors are encountered,
this routine should return FALSE, and the device will not
be enumerated. Example 1 shows a sample initialization
handler.
Several events are provided for OTG operations. In The Stack requires a list of all initialization and event
addition, the Stack can be configured to provide trans- handlers for every supported client driver. This list
fer events (EVENT_TRANSFER). Refer to the Using can be defined using the configuration tool,
Transfer Events section. USBConfig.exe, provided with the Stack. See the
See the API documentation in the Help directory for a Configuring the Stack section.
complete list of events.
If no event handler is implemented, the Embedded This function implements the state machine required to
Host Stack will respond as if the event handler returned enumerate a device. It should be called at regular
TRUE. intervals to ensure timely enumeration.
The USB configuration tool will create a macro,
Stack Initialization USBTasks(), to call all of the task routines required by
the USB host driver and the supported client drivers.
USB embedded host operation is initialized by a single
function: Note: Applications should use the USBTasks()
BYTE USBHostInit( void ); macro instead of the lower level function
call.
This function initializes all internal variables for Stack
operation. It should only be called once during the
applications execution. The USB OTG module on the Communicating with a Peripheral Device
microcontroller is not configured in this function.
Note: This section describes the routines
The USB configuration tool will create a macro, required for a client driver to communicate
USBInitialize(), to call all of the initialization with a peripheral device. Application level
routines required by the USB host driver and the code will not call these functions; instead,
supported client drivers. they will utilize the appropriate routine(s)
in the client driver(s).
Note: Applications should use the
USBInitialize() macro instead of the Normal communication with a device is initiated by two
lower level function call. functions:
BYTE USBHostRead( BYTE deviceAddress,
Normal Stack Operation BYTE endpoint, BYTE *data, DWORD size );
After initialization, USB OTG module configuration BYTE USBHostWrite( BYTE deviceAddress,
and USB embedded host operation are triggered by a BYTE endpoint, BYTE *data, DWORD size );
single function: A return code of USB_SUCCESS (0x00) indicates that
void USBHostTasks( void ); the operation was started successfully.
if (snIndex != 0)
{
if (!USBHostGetStringDescriptor( device, snIndex, buffer, 50 ))
{
while(!USBHostTransferIsComplete( device, 0, &error, &count ))
{
USBHostTasks();
}
if (!error)
{
// Serial number is in buffer[] in UNICODE
}
}
}
Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchips Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
Microchip is willing to work with the customer who is concerned about the integrity of their code.
Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as unbreakable.
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchips code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.
01/02/08