Download - 1646
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 1
1646 USB7
USB Embedded Host and On-The-Go (OTG)
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 2
Objectives
To know what host enabled options are available
To understand how they are different and when they should be selected over another
Get hands-on experience on several of the available USB host class drivers
To know where to go next to get more information, tools, training, etc., to get a design going
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 3
Agenda
What’s Available? OTG vs Embedded Host vs Dual Role
Host and OTG stack architecture overview
Mass Storage Host Lab 1
Human Interface Device Lab 2
Communication Device Class Lab 3
OTG in more detail
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 4
WHAT’S AVAILABLE
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 5
Embedded Host
Always a host, never a USB device
Standard A connector
Must always supply power
Example: Data Logger
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 6
On-The-Go (OTG)
Mobile, simple hosts
Want to be host sometimes but device sometimes
Power consumption
Micro A/B connector
Example: Smart Phones or Tablets
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 7
Dual Role (multi-connector devices)
2 connectors – must only have one accessible at any point of time
Wants to be either embedded host or USB device, but doesn’t need to dynamically switch
Some electrical considerations
Example: Data Logger with field update via PC
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 8
HOST AND OTG STACK OVERVIEW
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 9
High Level Software Architecture
Host
Class
Drivers
User Code
Hardware
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 10
High Level Software Architecture
Host
User Code
US
BH
ostInitia
lize()
US
BH
ostT
asks()
US
B_
HO
ST
_A
PP
_E
VE
NT
_H
AN
DL
ER
()
US
B_H
OS
T_A
PP
_D
AT
A_E
VE
NT
_H
AN
DLE
R()
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 11
USBHostTasks()
Keeps the USB host state machine going
Needs to be called periodically
Needed for transfers to complete
Affects throughput
Needed for notifications of events
Attach, detach, etc.
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 12
USB_HOST_APP_EVENT_HANDLER()
Notification of most events occurring on the bus
Device attach
Device detach
Transfer complete
Errors
Is called from USBHostTasks()
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 13
USB_HOST_APP_DATA_EVENT_HANDLER()
Notification of time critical events on the bus
Timer interrupts
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 14
Host/Client Driver Interface
Host
Class
Drivers
usb_config.c
usb_config.h
<D
river>
Even
t()
<D
river>
Da
taE
ven
t()
Tra
nsm
it F
un
ction
s:
US
BH
ostW
rite
()
US
BH
ostR
ea
d()
US
BH
ostIsR
ead
Co
mple
te()
Etc
.
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 15
usb_config.c/usb_config.h
Used for stack configuration
Used for host, device, and OTG configuration
Contains build options and data tables required for USB stack operation
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 16
Targeted Peripheral List (TPL)
List of supported devices for that embedded host and OTG Devices not on that list will not be
able to enumerate
Specified by Vendor ID(VID) and Product ID(PID)
pair
Or Class, Subclass, and Protocol set
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 17
TPL – Implementation Located in usb_config.c USB_TPL usbTPL[NUM_TPL_ENTRIES] =
{
/*[1] Device identification information
[2] Initial USB configuration to use
[3] Client driver table entry
[4] Flags (HNP supported, client driver entry, SetConfiguration() commands allowed)
---------------------------------------------------------------------
[1] [2][3] [4]
---------------------------------------------------------------------*/
{ INIT_CL_SC_P( 8ul, 6ul, 0x50ul ), 0, 0, {TPL_CLASS_DRV} } // SCSI - most MSD drives
{ INIT_VID_PID( 0x18D1ul, 0x2D00ul ), 0, 1, {0} }, // This specific device (Android)
}
CLIENT_DRIVER_TABLE usbClientDrvTable[NUM_CLIENT_DRIVER_ENTRIES] =
{
{
USBHostMSDEventHandler,
NULL,
0
},
{
AndroidAppEventHandler,
AndroidAppDataEventHandler,
ANDROID_INIT_FLAG_BYPASS_PROTOCOL
}
};
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 18
MASS STORAGE DEVICE CLASS (MSD) HOST
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 19
What is MSD?
Used to talk to memory devices
Thumb drives
USB hard drives
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 20
MSD Host Driver Architecture
MSD SCSI Transport
MSD
USB Host
File System Layer (FAT/FAT32)
FS
fopen()
FS
fclo
se()
FS
fread()
FS
fwrite
()
US
BH
ostM
SD
SC
SIM
edia
Dete
ct(
)
FS
Init()
US
BH
ostM
SD
Tasks()
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 21
MSD Host Driver Architecture
MSD
MSD SCSI Transport
USB Host
File System Layer (FAT/FAT32)
Se
cto
rRe
ad()
Se
cto
rWrite
()
Me
dia
Initia
lize
()
USBHostMSDSCSI…
Me
dia
De
tect(
)
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 22
MSD Host Driver Architecture
MSD
MSD SCSI Transport
USB Host
File System Layer (FAT/FAT32)
Tra
nsfe
r()
Tra
nsfe
rIsC
om
ple
te()
Initia
lize
()
USBHostMSD…
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 23
MSD Host Driver Architecture
MSD
MSD SCSI Transport
USB Host
File System Layer (FAT/FAT32)
Rea
d()
Tra
nsfe
rIsC
om
ple
te()
Write
()
USBHost…
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 24
Microchip Disk Drive (MDD) Overview
MSD
MSD SCSI Transport
USB Host
File System Layer (FAT/FAT32)
MDD/FAT library used to talk to drives Supports FAT12/FAT16/FAT32
Supports long file names and several character encodings
Currently only supports a single drive only one type
only one at a time
Uses POSIX style interface FSfopen()
FSfclose()
FSfread(), FSfwrite()
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 25
The FSInit Function
Initializes data structures
Loads device information from the MBR and Boot Sector
Initializes Media
Prototype:
int FSInit (void);
Returns: TRUE if initialization is successful FALSE otherwise
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 26
The FSfopen Function
Loads file information or creates a new file
Prototype:
FSFILE * FSfopen (const char * fileName, const char * mode);
Arguments fileName: The name of the file to open Mode: FS_READ, FS_WRITE, FS_APPEND, or –PLUS modes
Returns: A pointer to the initialized file object on success NULL on failure
Can also be used to open directories
Example
FSFILE * pointer;
pointer = FSfopen (“FILE.TXT”, “w+”); // Or use WRITEPLUS macro
if (pointer == NULL)
// Error
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 27
The FSfwrite Function
This function will write ‘n’ items of ‘size’ bytes from the structure pointed to by ‘ptr’ to the file pointed to by ‘stream’
Prototype:
size_t FSfwrite (const void * ptr, size_t size, size_t n, FSFILE * stream);
Arguments: ptr: A pointer to the data to be written size: The size of the objects to write n: The number of objects to write stream: The file the data will be written to
Returns: The number of objects written
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 28
The FSfclose Function
Updates information in the root and FAT
Frees the memory used by the FSFILE object
Prototype:
int FSfclose (FSFILE * fo);
Argument: A pointer to the file to close
Returns: 0 if the file was closed successfully EOF (-1) otherwise
Example
FSFILE * pointer;
pointer = FSfopen (“FILE.TXT”, “w”);
if ( FSfclose (pointer) != 0)
// Error
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 29
LAB 1 Mass Storage Host (MSD)
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 30
Lab 1 Objectives
Write file to drive
Lab manual can be found at
C:\Masters\1646 USB7\manual.pdf
Open MPLAB® X IDE,
Select “File->Open Project”
Select the “c:\Masters\1646 USB7\lab1_msd\MPLAB.X” file.
Solutions are both in the lab manual and in the “C:\Masters\1646 USB7” folder
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 31
Lab 1 Summary
How to write a file on a USB thumb drive
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 32
HUMAN INTERFACE DEVICE CLASS (HID) HOST
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 33
What is the Human Interface Device Class (HID)?
Typically used for input devices
Keyboards, mice, barcode scanners, touch digitizers, game controllers, etc.
Sometimes used for other custom devices that don’t need large amounts of throughput
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 34
Reports
HID Devices transfer what are known as reports
Standardizes the data values for each type of input event
Allows designer to select which features are implemented or not
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 35
Reports
Report ID
L
X shift
Y shift
R
Report ID
L
X shift
Y shift
R
Report ID
R
X shift
Y shift
L
A B C D E F G H
I J K M N O
Q
P
T S
Right Click Occurred
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 36
HID Host Driver Architecture
HID Host
USB Host
Report Parser
HID Keyboard Driver HID Mouse Driver
USB_HOST_APP_EVENT_HANDLER (
EVENT_HOST_HID_KEYBOARD_ATTACHED,
EVENT_HOST_HID_KEYBOARD_DETACHED,
EVENT_HOST_HID_KEYBOARD_KEY_PRESSED,
EVENT_HOST_HID_KEYBOARD_KEY_RELEASED
)
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 37
EVENT_HOST_HID_KEYBOARD_ATTACHED
Signals when a new keyboard is attached
*data is the handle of the attached keyboard Useful for getting information about the attached
keyboard
Used to identify which keyboard is sending a given message
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 38
EVENT_HOST_HID_KEYBOARD_DETACHED
Signals when a keyboard is detached
*data is the handle of the detached keyboard
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 39
EVENT_HOST_HID_KEYBOARD_KEY_PRESSED EVENT_HOST_HID_KEYBOARD_KEY_RELEASED
*data is a EVENT_HOST_HID_KEYBOARD_KEY_EVENT_DATA* typedef struct
{
USB_HID_KEYBOARD_KEYS key;
void* keyboard;
} EVENT_HOST_HID_KEYBOARD_KEY_EVENT_DATA;
Provides the HID key pressed and the handle for the associated keyboard.
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 42
LAB 2 Human Interface Device Class (HID)
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 43
Lab 2 Objectives
Create code to talk to a HID class device
Get the information from a USB keyboard when a key is pressed
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 44
Lab 2 Summary
Read information from a USB keyboard
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 45
COMMUNICATION DEVICE CLASS (CDC) HOST
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 46
What is the Communication Device Class (CDC)
Used for communication devices
USB modems
Some cell phones
Cable modems
USB Wi-Fi® dongles
USB to Serial cables
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 47
CDC Host API
CDC Host
USB Host
US
BH
ost_
CD
C_
AC
M_
Write
()
US
BH
ost_
CD
C_
AC
M_
Re
ad
()
US
BH
ost_
CD
C_
AC
M_
Write
Sta
tus()
US
BH
ost_
CD
C_
AC
M_
Po
rtS
tatu
sG
et(
)
US
BH
ost_
CD
C_
AC
M_
Re
ad
Sta
tus()
US
BH
ost_
CD
C_
AC
M_
Po
rtD
ete
ct(
)
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 48
USBHost_CDC_ACM_PortDetect()
Detects new CDC ports available and returns handles to those ports for access by the application
Prototype:
void* USBHost_CDC_ACM_PortDetect(void);
Arguments None
Returns: void* - handle to the detected CDC port. NULL if no port detected
void* device = NULL;
while(1)
{
USBTasks();
if(device == NULL)
{
device = USBHost_CDC_ACM_PortDetect();
continue;
}
Example
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 49
USBHost_CDC_ACM_PortStatusGet()
Determines the status of a specified CDC device
Prototype:
USB_CDC_ACM_DEVICE_STATUS
USBHost_CDC_ACM_PortStatusGet(void* handle);
Arguments void* handle – the handle of the device that you are checking the
status of.
Returns: USB_HOST_CDC_ACM_STATUS__UNKNOWN_DEVICE USB_HOST_CDC_ASCM_STATUS__DEVICE_READY
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 50
USBHost_CDC_ACM_PortStatusGet()
void* device = NULL;
while(1)
{
USBTasks();
if(device == NULL)
{
device = USBHost_CDC_ACM_PortDetect();
continue;
}
switch(USBHost_CDC_ACM_PortStatusGet(device))
{
case USB_HOST_CDC_ACM_STATUS__DEVICE_READY:
break;
case USB_HOST_CDC_ACM_STATUS__UNKNOWN_DEVICE:
device = NULL;
continue;
default:
continue;
}
//… do something meaningful with the CDC device here…
Example
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 51
USBHost_CDC_ACM_Write()
Writes data to a CDC device
Prototype:
USB_CDC_ACM_RETURN_CODE USBHost_CDC_ACM_Write(
void* handle,
BYTE* data,
DWORD size);
Arguments void* handle – handle of the device to write to BYTE* data – the data that needs to be written DWORD size – the amount of data to send
Returns: USB_CDC_ACM_RETURN_CODE
USB_SUCCESS USB_BUSY USB_UNKNOWN_DEVICE USB_<various error codes>
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 52
USBHost_CDC_ACM_WriteStatus()
Writes data to a CDC device
Prototype:
USB_CDC_ACM_RETURN_CODE USBHost_CDC_ACM_WriteStatus(
void* handle,
DWORD *size);
Arguments void* handle – handle of the device to check DWORD *size – pointer to a DWORD where the actual amount of
data written will be stored
Returns: USB_CDC_ACM_RETURN_CODE
USB_SUCCESS USB_BUSY USB_UNKNOWN_DEVICE USB_<various error codes>
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 53
USBHost_CDC_ACM_WriteStatus()
BYTE TxData[2];
DWORD size;
//... Do all of the device detection...
if(USBHost_CDC_ACM_WriteStatus(device, &size) != USB_BUSY)
{
TxData[0] = 'H';
TxData[1] = 'i';
USBHost_CDC_ACM_Write(device, TxData, 2);
}
Example
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 54
USBHost_CDC_ACM_Read() USBHost_CDC_ACM_ReadStatus()
Identical to Write() and WriteStatus() expect that they read instead…
Note: The ReadStatus() function return USB_SUCCESS when either: The request amount of data is received A transfer is completed (i.e. - you requested 100 bytes but the
transfer was only 2 bytes)
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 55
CDC Events
USB_EVENT_CDC_ACM_DEVICE_ATTACH data = handle of the device attached
USB_EVENT_CDC_ACM_DEVICE_DETACH data = handle of the device that detached
USB_EVENT_CDC_ACM_READ_COMPLETE data = handle of the device that completed read. Use
ReadStatus() to get the amount of data
USB_EVENT_CDC_ACM_WRITE_COMPLETE
data = handle of the device that completed write. Use WriteStatus() to get the amount of data
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 56
LAB 3 Communication Device Class (CDC)
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 57
Lab 3 Objectives
Establish communication with a serial device using a USB to Serial converter cable
Test sending and receiving data
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 58
Lab 3 Summary
Created a connection to a USB to UART conversion cable
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 59
PIC24, dsPIC® DSC,
or PIC32MX
with the USB OTG
module
Full size A
Receptacle
Embedded Host Example Circuit
VBUS
D+
D-
GND
5v
PPTC
A/D
D+
D-
VUSB
2KΩ
2KΩ
150μF
.1μF
3.3v
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 60
Certification Considerations Embedded Host
Checklists Systems
No Silent Failures Hub error message
Device not supported message
Power Over-current notification
Resettable overcurrent protection
Drop voltage
TPL
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 61
OTG IN DETAIL
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 62
High Level Software Architecture
OTG Controls Host Device
Function
Drivers
Class
Drivers
User Code
Hardware
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 63
Device Types
A – Device
Device plugged into the A side of a cable. Starts out as the host
B – Device
Device plugged into the B side of a cable. Starts out as a peripheral
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 64
New 5th Pin
Old connectors had 4 pins on the receptacle that were used: VBUS, GND, D+, and D-
OTG connectors have 5 pins on the receptacle that are used: VBUS, GND, D+, D-, and ID ID pin is used to determine which side of
the cable is the A (host) side ID should be pulled high through a
resistor Built into PIC24F and PIC32MX devices
with USB OTG
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 65
Mechanical
Micro A/B Receptacle
Pin 1: VBus
Pin 2: D-
Pin 3: D+
Pin 4: ID
Pin 5: GND
Pin 1
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 66
Mechanical
Micro B Receptacle
Pin 1: VBus
Pin 2: D-
Pin 3: D+
Pin 4: ID
Pin 5: GND
Pin 1
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 67
Mechanical
Micro A Plug
Pin 1: VBus
Pin 2: D-
Pin 3: D+
Pin 4: GND (ID)
Pin 5: GND
Pin 1
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 68
Mechanical
Micro B Plug
Pin 1: VBus
Pin 2: D-
Pin 3: D+
Pin 4: Floating (ID)
Pin 5: GND
Pin 1
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 69
OTG Cable Example
Micro
A Plug Micro
A/B
Host Peripheral
Micro
B Plug Micro
A/B
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 70
OTG Cable Example
Micro
A Plug Micro
A/B
Host Peripheral
Micro
B Plug Micro
A/B
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 71
OTG Cable Example
Micro
A Plug Micro
A/B
Host Peripheral
Micro
B Plug Micro
B
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 72
OTG Cable Example
Micro
A/B
Peripheral DOESN’T FIT!!
Micro
B
Micro
A Plug
Micro
B Plug
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 73
Mechanical
Cables
Allowable Types
Micro-A plug to Micro-B plug
Micro-A plug to Standard-A receptacle
Micro-B plug to Standard-A plug
Captive cable with Micro-A plug
Length
2 meters or less (different from USB-v2.0 limit of 5 meters)
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 74
OTG Descriptor
Returned in the GetDescriptor(Configuration) request
Required only if B-Device supports any OTG specific features
Tells the host (A-Device) what the B-Device is capable of
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 75
OTG Descriptor
Offset Field Size Value Description
0 bLength 1 Number Size of Descriptor (3 for v1.3 or earlier, 5 for v2.0)
1 bDescriptorType 1 Constant OTG type = 9
2 bmAttributes 1 Bitmap Attribute Fields
D7-D3: reserved
D2: ADP supported
D1: HNP supported
D0: SRP supported
3 bcdOTG 2 OTG Binary coded decimal revision number that the device is compliant to (0200H for example)
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 76
Host Negotiation Protocol (HNP)
Allows two connected OTG devices to exchange roles
A-device still provides the power
Roles stay swapped until bus goes idle
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 77
Session Request Protocol (SRP)
OTG allows host to power down bus when not in use
SRP provides a way for a device to signal to a power down host that it wants to talk
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 78
Attach Detection Protocol (ADP)
OTG allows host to power down bus when not in use
Determine if a device has detached while the bus is powered down
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 79
OTG Acceptance
OTG acceptance is currently very low
Nearly impossible to find cables
Certified connectors are easier, but not a wide range of selection
Nearly impossible to find real products
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 80
Certification Considerations OTG
Checklists OTG Peripheral Systems
SRP
HNP
TPL
Power restrictions Unconfigured power
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 81
OTG Example Circuit
PIC24F or PIC32MX
USB device
Micro A/B
Receptacle
VBUS
D+
D-
GND
VBUS
D+
D-
VUSB
ID
GPIO
GPIO
USBID
Vdd .1 μF
MCP1253
PGOOD
SELECT
3.3v
VOUT
VIN nSHDN
GND
10 μF
3.3v
4.7 μF
C+
C-
1 μF
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 82
OTG vs. Embedded Host
OTG Embedded Host OTG Protocols (SRP, HNP, ADP)
Some are required Optional (or not possible)
Mechanical Micro A/B receptacle
2m max cable
A receptacle
5m max cable
Electrical 1.0μF < CDRD_VBUS < 6.5μF
8mA min to connected peripheral device (if A-device)
CHST_VBUS > 120μF
100mA min to connected peripheral
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 83
GETTING STARTED Where to go for more information
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 84
Software Examples Available
Embedded Host Data logging to a thumb drive
MCHPUSB host – temperature, pot reader
HID Host – talking to a keyboard
HID Host – talking to a mouse
Printer Host (PCL5 and PostScript)
CDC Host – hosting a serial to USB converter
Simple USB Charger
Simple demo writing to a thumb drive
Bootloading from a thumb drive
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 85
Software Examples Available
OTG
MCHPUSB OTG (We currently support only v1.3 of the OTG specification)
All software available, free download from:
www.microchip.com/usb
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 86
Demo Tools Available
Development Kit Explorer 16 (DV164033)
PIC24FJ64GB004 USB PIM (MA240019) PIC24FJ256GB110 USB PIM (MA240014) PIC32MX USB PIM (MA320002)
USB PICtail™ Plus Daughter Card (AC164131)
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 87
Demo Tools Available
Starter Kits
PIC24F Starter Kit (DM240011)
PIC32MX USB Starter Board (DM320003)
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 88
Microchip Regional Training Centers (RTCs)
Worldwide network of training facilities
Product, Tools and Applications-orientated classes (Ethernet, USB, Motor Control, etc..)
Flexible ☼ Most classes are half or one day duration
☼ Easily modified to fit a specific customer need
Cost Effective ☼ Most classes are $49 or $99 (US)
☼ Significant discounts on development tools used in the class
☼ Many centers reduce travel expenses
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 89
RTC Training Curriculum
New to Microchip MPLAB® Tool
Basics TLS0103
New to PIC18
New to PIC24
New to USB
PIC18 Arch.
MCU2101
PIC18 Peri.
MCU2121
16-bit Arch.
MCU3101
16-bit Peri.
MCU3121
Intro USB
COM3101
USB Device Ap.
USB Emb. Host
USB Device
COM3201
USB Host
COM3202
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 90
Summary
Today we covered: What host options are available
How they are different
Got hands on experience Writing to a thumb drive
Getting information from a keyboard
Using a CDC serial converter
Available development resources
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 91
The End
Questions?
Ask now…
Ask the experts later…
Find me in the hall, at lunch, at dinner, …
Thanks!!
Please fill out a survey
Come play with all of the demos
Enjoy the rest of your classes!
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 92
Trademarks
The Microchip name and logo, the Microchip logo, dsPIC, KeeLoq, KeeLoq logo, MPLAB, PIC,
PICmicro, PICSTART, PIC32 logo, rfPIC and UNI/O are registered trademarks of Microchip
Technology Incorporated in the U.S.A. and other countries.
FilterLab, Hampshire, HI-TECH C, Linear Active Thermistor, MXDEV, MXLAB, SEEVAL and
The Embedded Control Solutions Company are registered trademarks of Microchip Technology
Incorporated in the U.S.A.
Analog-for-the-Digital Age, Application Maestro, chipKIT, chipKIT logo, CodeGuard, dsPICDEM,
dsPICDEM.net, dsPICworks, dsSPEAK, ECAN, ECONOMONITOR, FanSense, HI-TIDE,
In-Circuit Serial Programming, ICSP, Mindi, MiWi, MPASM, MPLAB Certified logo, MPLIB,
MPLINK, mTouch, Omniscient Code Generation, PICC, PICC-18, PICDEM, PICDEM.net,
PICkit, PICtail, REAL ICE, rfLAB, Select Mode, Total Endurance, TSHARC, UniWinDriver,
WiperLock and ZENA are trademarks of Microchip Technology Incorporated in the U.S.A. and
other countries.
SQTP is a service mark of Microchip Technology Incorporated in the U.S.A.
All other trademarks mentioned herein are property of their respective companies.
© 2012, Microchip Technology Incorporated, All Rights Reserved.
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 93
OTG Appendix
Below is additional material for those interested to know more about how OTG works.
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 94
Device Types
A – Device
Device plugged into the A side of a cable. Starts out as the host
B – Device
Device plugged into the B side of a cable. Starts out as a peripheral
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 95
New 5th Pin
Old connectors had 4 pins on the receptacle that were used: VBUS, GND, D+, and D-
OTG connectors have 5 pins on the receptacle that are used: VBUS, GND, D+, D-, and ID ID pin is used to determine which side of
the cable is the A (host) side ID should be pulled high through a
resistor Built into PIC24F and PIC32MX devices
with USB OTG
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 96
Mechanical
Micro A/B Receptacle
Pin 1: VBus
Pin 2: D-
Pin 3: D+
Pin 4: ID
Pin 5: GND
Pin 1
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 97
Mechanical
Micro B Receptacle
Pin 1: VBus
Pin 2: D-
Pin 3: D+
Pin 4: ID
Pin 5: GND
Pin 1
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 98
Mechanical
Micro A Plug
Pin 1: VBus
Pin 2: D-
Pin 3: D+
Pin 4: GND (ID)
Pin 5: GND
Pin 1
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 99
Mechanical
Micro B Plug
Pin 1: VBus
Pin 2: D-
Pin 3: D+
Pin 4: Floating (ID)
Pin 5: GND
Pin 1
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 100
OTG Cable Example
Micro
A Plug Micro
A/B
Host Peripheral
Micro
B Plug Micro
A/B
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 101
OTG Cable Example
Micro
A Plug Micro
A/B
Host Peripheral
Micro
B Plug Micro
A/B
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 102
OTG Cable Example
Micro
A Plug Micro
A/B
Host Peripheral
Micro
B Plug Micro
B
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 103
OTG Cable Example
Micro
A/B
Peripheral DOESN’T FIT!!
Micro
B
Micro
A Plug
Micro
B Plug
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 104
Mechanical
Cables
Allowable Types
Micro-A plug to Micro-B plug
Micro-A plug to Standard-A receptacle
Micro-B plug to Standard-A plug
Captive cable with Micro-A plug
Length
2 meters or less (different from USB-v2.0 limit of 5 meters)
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 105
Targeted Peripheral List (TPL)
List of supported devices for that embedded host and OTG Devices not on that list will not be
able to enumerate
Manufacturer, Model, and Description are minimally required
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 106
OTG Descriptor
Returned in the GetDescriptor(Configuration) request
Required only if B-Device supports any OTG specific features
Tells the host (A-Device) what the B-Device is capable of
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 107
OTG Descriptor
Offset Field Size Value Description
0 bLength 1 Number Size of Descriptor (3 for v1.3 or earlier, 5 for v2.0)
1 bDescriptorType 1 Constant OTG type = 9
2 bmAttributes 1 Bitmap Attribute Fields
D7-D3: reserved
D2: ADP supported
D1: HNP supported
D0: SRP supported
3 bcdOTG 2 OTG Binary coded decimal revision number that the device is compliant to (0200H for example)
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 108
Session Request Protocol (SRP)
Saves power on A-Device
B-Device needs way to request VBUS from A-Device
Session
The time between the VBUS rising above the valid threshold until it drops below the valid threshold again
?
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 109
Session Request Protocol (SRP)
SRP support
OTG devices are required to be able to respond to and initiate SRP
A-Devices allowed to respond to SRP
B-Devices allowed to initiate SRP
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 110
Session Request Protocol (SRP)
B-Device
Before attempting to start new session, must first determine the previous session has ended
Time the decay of the previous session end
Pull VBUS down to speed up end of session
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 111
Session Request Protocol (SRP) D+ Pulsing
A-Device Driving B-Device Driving
A-Device Pull-Downs B-Device Pull-ups
VIH
VIL
D+
1 2 3 5 6 4
VIH
VIL
Vbus
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 112
Host Negotiation Protocol (HNP)
Cable determines which device is the host (A-Device) and the peripheral (B-Device) Whichever device that has the Micro A plug
plugged into its Micro A/B receptacle is the default host/A-Device
Micro
A Plug Micro
A/B
Host Peripheral
Micro
B Plug Micro
A/B
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 113
Host Negotiation Protocol (HNP)
HNP allows devices to switch roles without having to switch cable The B-Device will become the host until the
session ends A-Device continues to source the VBUS power
Micro
A Plug Micro
A/B
Micro
B Plug Micro
A/B
Host Peripheral
Host Peripheral
Host Peripheral
HNP
Session End
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 114
Set Feature Requests
Lets the B-device know that the A-Device supports HNP Can be set in the default, address, or
configured states
Only cleared at the end of a session or on a bus reset Clear feature does not work on these
features
If HNP is not supported on the B-Device, then it should STALL on any of these Set Feature requests
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 115
Host Negotiation Protocol (HNP)
1) A-Device uses SetFeature(HNP)
2) During suspend the B-Device turns off D+ pull-up
3) A-Device turns D+ pull-up on
4) B-Device detects D+ pull-up and asserts a bus reset
VIH
VIL
D+
1 2 3 4
A-Device Driving
A-Device Pull-Downs B-Device Pull-ups
A-Device Pull-ups
A Host Bus Traffic
B Host Bus Traffic
B-Device Driving
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 116
Host Negotiation Protocol (HNP)
5) B is now the host and controls the bus
6) When B-Device is done, stops all bus activity
7) On the Idle condition, the B-Device enables its D+ pull-up and the A-device disables its pull-up
8) A-Device either asserts reset or turns off VBUS
VIH
VIL
D+
1 2 3 5 6 4 7
A-Device Driving
A-Device Pull-Downs B-Device Pull-ups
A-Device Pull-ups
A Host Bus Traffic
B Host Bus Traffic
B-Device Driving
8
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 117
Attach Detection Protocol (ADP)
Used to determine if remote device is still attached after a session has ended
Done using a constant current source to determine if the capacitance on VBUS has changed (called ADP probing)
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 118
Attach Detection Protocol (ADP)
A B-device shouldn’t try to start a new session until it determines that the A-Device isn’t ADP probing anymore (called ADP sensing)
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 119
Micro A/B
Receptacle
VBUS
D+
D-
GND
ID
Attach Detection Protocol (ADP)
I = C1 * dV/dT
I is constant
C1 is constant
Thus dV/dT is constant
V
T
C1
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 120
Micro A/B
Receptacle
VBUS
D+
D-
GND
ID
Attach Detection Protocol (ADP)
C1
Micro A/B
Receptacle
VBUS
D+
D-
GND
ID
C2
I = (C1 + C2) * dV/dT, thus it will take longer for the
same current source to reach the same voltage
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 121
Current Sourcing Requirements
A-Devices supporting loads <= 100mA
IA_VBUS_OUT min = 8mA
4.4v <= VA_VBUS_OUT <= 5.25v
Must error if VA_VBUS_OUT < VA_VBUS_VLD
A-Devices supporting loads > 100mA
4.75v <= VA_VBUS_OUT <= 5.25v
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 122
Current Draw Limits
Dual Role Device
Unconfigured: 150uA average over 1ms
Peripheral Only
Unconfigured: 8mA average over 1ms
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 123
VBus
When A-Device is powered but not supplying VBus, RA_BUS_IN max <= 100KΩ
1.0μF < CDRD_VBUS < 6.5μF As compared to CHST_VBUS > 120μF
Pin 1
RA_BUS_IN CDRD_VBUS
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 124
Quiz!
1) True or False: If I plug in any 100mA normal USB device into an OTG device, everything should always be fine.
2) True or False: There is no electrical difference between an OTG host and an Embedded host.
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 125
OTG vs. Embedded Host
OTG Embedded Host SRP Required Optional
HNP Required Not possible
ADP Optional Not possible
Targeted Peripheral list
Allowed to support generic classes (i.e.- any HID mouse)
Allowed to support generic classes (i.e.- any HID mouse)
Mechanical Micro A/B A
Electrical 1.0μF < CDRD_VBUS < 6.5μF
CHST_VBUS > 120μF
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 126
Agenda
Overview
Mechanical
Protocol
Electrical
Certification Considerations
Resources (Examples, Classes, Software, etc.)
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 127
Certification Considerations Embedded Host
Checklists Systems
No Silent Failures Hub error message
Device not supported message
Power Over-current notification
Resettable overcurrent protection
Drop voltage
TPL
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 128
Certification Considerations OTG
Checklists OTG Peripheral Systems
SRP
HNP
TPL
Power restrictions Unconfigured power
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 129
Certification Considerations DRD
Port accessibility
If more than one connector is accessible at any point of time, then they need to be able to work at the same time
Checklists
Peripheral
Systems
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 130
PIC24F or PIC32MX
USB device
Full size A
Receptacle
Embedded Host Example Circuit
VBUS
D+
D-
GND
5v
PPTC
A/D
D+
D-
VUSB
2KΩ
2KΩ
150μF
.1μF
3.3v
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 131
OTG Example Circuit
PIC24F or PIC32MX
USB device
Micro A/B
Receptacle
VBUS
D+
D-
GND
VBUS
D+
D-
VUSB
ID
GPIO
GPIO
USBID
Vdd .1 μF
MCP1253
PGOOD
SELECT
3.3v
VOUT
VIN nSHDN
GND
10 μF
3.3v
4.7 μF
C+
C-
1 μF
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 132
PIC24F or PIC32MX
USB device
Full size A
Receptacle
DRD Example Circuit
VBUS
D+
D-
GND
5v
PPTC
A/D
VBUS
D+
D-
VUSB
2KΩ
2KΩ
150μF
.1μF
3.3v
Input
B, Micro B,
or Mini B
Receptacle
VBUS
D+
D-
GND
1μF
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 133
Software Architecture
OTG Controls Host Device
Function
Drivers
Class
Drivers
User Code
Hardware
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 134
OTG References
1) USB_OTG_and_EH_2-0.pdf – “On-The-Go Supplement to the USB 2.0 Specification”
2) Micro-USB_1_01.pdf – “Universal Serial Bus Micro-USB Cables and Connectors Specification”
© 2012 Microchip Technology Incorporated. All Rights Reserved. 1646 USB7 Slide 135
Acronyms
SRP – Session Request Protocol
HNP – Host Negotiation Protocol
OTG – On-The-Go
USB – Universal Serial Bus
DRD – Dual Role Device
PPTC – Polymeric Positive Temperature Coefficient
SIE – Serial Interface Engine
BDT – Buffer Descriptor Table