Host Application Example - Freescale
Host Application Example - Freescale
Host Application Example - Freescale
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
August, 2010<br />
MQX USB <strong>Host</strong> Stack<br />
ENT-F0966<br />
K Muthukumar<br />
India University Program<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc.<br />
TM
►Agenda<br />
• Introduction<br />
• MQX USB HOST structure<br />
• Hardware layers<br />
� KHCI<br />
� EHCI<br />
• Data stack description<br />
� Data validation for HAL<br />
• Writing a new host application<br />
� Mandatory steps, 1 through 5<br />
� Additional steps<br />
� Services<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc. 2<br />
Agenda<br />
TM
►MQX USB Stack Features<br />
• Implements USB 2.0 version<br />
• Layered architecture<br />
� scalability (choose only components needed in application)<br />
� portability (class drivers independent on underlying hardware)<br />
• Class drivers supported:<br />
� Legacy MQX code<br />
– HID devices (mouse, keyboard)<br />
– MSD devices (flash disk)<br />
� <strong>Freescale</strong> MQX<br />
– HUB support (transparent communication through HUB)<br />
– CDC devices (serial communication)<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc. 3<br />
Introduction<br />
TM
HU<br />
B<br />
MFS<br />
MS<br />
D<br />
User <strong>Application</strong><br />
Other to be<br />
developed<br />
HID CD<br />
C<br />
KHCI (Kirin2U/Kirin3specific<br />
controller)<br />
Intermediate<br />
I/O Drivers<br />
HOST API (class level)<br />
USB 2.0 Chapter 9 API<br />
<strong>Host</strong> Class<br />
Drivers<br />
OS and HW-level HOST API<br />
EHCI (Enhanced <strong>Host</strong><br />
Controller Interface)<br />
USB Module (low-level drivers)<br />
Other (OHCI, ...)<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc. 4<br />
MQX USB <strong>Host</strong> Structure<br />
Hardware<br />
Independent<br />
Layer<br />
TM
►KHCI<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc. 5<br />
MQX USB <strong>Host</strong> Low-level Drivers<br />
• MCF522xx-specific USB controller (codename Kirin <strong>Host</strong> Controller)<br />
• Max. 10 Mbps (No support for high speed)<br />
• Physical transfer done by HW, DMA<br />
• Scheduling of transfers done by SW<br />
• USB transactions handled by SW<br />
• Requires separate task (khci_task), high CPU load<br />
• Does not handle any service (see later)<br />
►EHCI<br />
• Enhanced <strong>Host</strong> Controler Interface (industry standard)<br />
• ColdFire extends the specification (registers contain also bits that are<br />
not specified by EHCI standard)<br />
• Runs on interrupts / application requests<br />
• USB transactions handled by HW, DMA<br />
• Small CPU load<br />
TM
►KHCI Processing <strong>Example</strong> 1<br />
• USB Stack handles USB interrupt and notifies the main task (note that every seq. box<br />
means additional stack usage)<br />
KHC<br />
I ISR<br />
MQX lwevent<br />
KHC<br />
I<br />
TAS<br />
K<br />
USB<br />
H<br />
STA<br />
CK<br />
CLA<br />
SS<br />
DRV<br />
APP<br />
EVE<br />
NT<br />
APP<br />
TAS<br />
K<br />
MQX lwevent<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc. 6<br />
KHCI<br />
MQX ISR<br />
CONTEX<br />
T<br />
APP<br />
TASK<br />
CONTEX<br />
T<br />
KHCI<br />
TASK<br />
CONTEX<br />
T<br />
TM
►KHCI Processing <strong>Example</strong> 2<br />
• <strong>Application</strong> handles USB event, notifies main task and sends additional data to USB device<br />
(higher stack usage)<br />
KHC<br />
I ISR<br />
MQX lwevent<br />
KHC<br />
I<br />
TAS<br />
K<br />
USB<br />
H<br />
STA<br />
CK<br />
CLA<br />
SS<br />
DRV<br />
APP<br />
EVE<br />
NT<br />
CLA<br />
SS<br />
DRV<br />
USB<br />
H<br />
STA<br />
CK<br />
MQX lwevent<br />
KHC<br />
I<br />
FCN<br />
(e.g. next<br />
transfer)<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc. 7<br />
MQX<br />
lwevent<br />
APP<br />
TAS<br />
K<br />
KHC<br />
I<br />
TAS<br />
K<br />
KHCI<br />
MQX ISR<br />
CONTEX<br />
T<br />
APP<br />
TASK<br />
CONTEX<br />
T<br />
KHCI<br />
TASK<br />
CONTEX<br />
T<br />
TM
►EHCI Processing <strong>Example</strong> 1<br />
• <strong>Application</strong> handles USB event and then notifies main task<br />
EHCI<br />
ISR<br />
HAL CLA<br />
SS<br />
DRV<br />
APP<br />
EVE<br />
NT<br />
MQX lwevent<br />
APP<br />
TAS<br />
K<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc. 8<br />
EHCI<br />
MQX ISR<br />
CONTEX<br />
T<br />
APP<br />
TASK<br />
CONTEX<br />
T<br />
TM
►EHCI Processing <strong>Example</strong> 2<br />
• <strong>Application</strong> handles USB event, notifies main task and sends additional data to USB<br />
device (higher stack usage)<br />
EHCI<br />
ISR<br />
HAL CLA<br />
SS<br />
DRV<br />
APP<br />
EVE<br />
NT<br />
CLA<br />
SS<br />
DRV<br />
HAL<br />
EHCI<br />
FCN<br />
(e.g. next<br />
transfer)<br />
MQX lwevent<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc. 9<br />
APP<br />
TAS<br />
K<br />
EHCI<br />
MQX ISR<br />
CONTEX<br />
T<br />
APP<br />
TASK<br />
CONTEX<br />
T<br />
TM
► Writing host application, step 1<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc. 10<br />
<strong>Host</strong> <strong>Application</strong> <strong>Example</strong><br />
• Clone one of the USB <strong>Host</strong> examples or create new project using<br />
MQX+USB<strong>Host</strong> stationery (MQX 3.3.0 or later).<br />
• Note that the usb_classes.h file is part of your application.<br />
• This file enables selected device enumeration and class drivers in the<br />
USB <strong>Host</strong> Stack.<br />
� Use USBCLASS_INC_XXX definitions to enable class for the device<br />
TM
► Writing host application, step 2<br />
• Specify event handlers for different types of devices.<br />
• Create array of USB_HOST_DRIVER_INFO structures.<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc. 11<br />
<strong>Host</strong> <strong>Application</strong> <strong>Example</strong><br />
� In this example: all HID-BOOT keyboards connected to the host will be<br />
handled by usb_host_hid_keyboard_event<br />
TM
► Writing host application, step 3<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc. 12<br />
<strong>Host</strong> <strong>Application</strong> <strong>Example</strong><br />
• Install low-level host driver using _usb_host_driver_install<br />
� Registers low-level HCI driver routines in the USB stack.<br />
� Low-level driver routines are prepared for each HCI (KHCI, EHCI, ..).<br />
� Allways use _bsp_usb_host_callback_table as an argument<br />
• Initialize host<br />
� Initializes host, prepares memory buffers and structures<br />
TM
► Writing host application, step 4<br />
• Register device classes in the USB host stack<br />
� Static class/handler table was prepared in step #2<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc. 13<br />
<strong>Host</strong> <strong>Application</strong> <strong>Example</strong><br />
TM
► Writing host application, step 5<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc. 14<br />
<strong>Host</strong> <strong>Application</strong> <strong>Example</strong><br />
• Write your device callback function (class event handler from step 2)<br />
• Four event types should handled in this function<br />
� USB_ATTACH_EVENT: called after device was attached<br />
� USB_CONFIG_EVENT: called after device was configured<br />
� USB_INTF_EVENT: called after device was interfaced<br />
� USB_DETACH_EVENT: called after device was detached<br />
• Enumeration, attach, detach and config events are processed by<br />
Chapter 9 sublayer of USB Stack.<br />
• No special actions are needed in the handler except:<br />
� Select interface with _usb_hostdev_select_interface after the<br />
ATTACH event (causes INTF event).<br />
� Remember actual state of the device together with handles for further<br />
processing in application.<br />
� After INTF event, application may communicate with the device using<br />
pipes.<br />
TM
► HID Keyboard <strong>Example</strong><br />
• Handling ATTACH event:<br />
Event handler gets event<br />
code, device and interface<br />
handles<br />
We should remember the<br />
handles and the current<br />
state (for main application<br />
state machine)<br />
Selecting interface TBD<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc. 15<br />
<strong>Host</strong> <strong>Application</strong> <strong>Example</strong><br />
TM
► HID Keyboard <strong>Example</strong><br />
• Main task state machine<br />
When first interfaced, HID<br />
keyboard requires setting protocol.<br />
Note that we need to pass validation<br />
structure we obtained during interface<br />
select into HID class driver.<br />
Activating keyboard protocol. Note that this<br />
demo uses plain variable-based synchronization.<br />
(Rather should use lwevents)<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc. 16<br />
<strong>Host</strong> <strong>Application</strong> <strong>Example</strong><br />
TM
► HID Keyboard <strong>Example</strong><br />
• Communicating with keyboard<br />
<strong>Application</strong> obtains handle to<br />
communication pipe (in this case interrupt<br />
receive pipe).<br />
Receive transfer is initiated on the pipe,<br />
passing buffer pointer for data to be<br />
received.<br />
Waiting for interrupt transaction (Again: we<br />
should rather use lwevent-based<br />
synchronization)<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc.<br />
<strong>Host</strong> <strong>Application</strong> <strong>Example</strong><br />
17<br />
TM
► Additional steps when writing host application<br />
<strong>Freescale</strong>, the <strong>Freescale</strong> logo, AltiVec, C-5, CodeTEST, CodeWarrior, ColdFire, C-Ware, mobileGT, PowerQUICC, StarCore, and Symphony are trademarks of <strong>Freescale</strong> Semiconductor, Inc.,<br />
Reg. U.S. Pat. & Tm. Off. BeeKit, BeeStack, CoreNet, the Energy Efficient Solutions logo, Flexis, MXC, Platform in a Package, Processor Expert, QorIQ, QUICC Engine, SMARTMOS, TurboLink<br />
and VortiQa are trademarks of <strong>Freescale</strong> Semiconductor, Inc. All other product or service names are the property of their respective owners. © 2010 <strong>Freescale</strong> Semiconductor, Inc. 18<br />
<strong>Host</strong> <strong>Application</strong> <strong>Example</strong><br />
• Always specific to application or USB device to be handled:<br />
� you may want to send control to endpoint zero<br />
� you may want to run interrupt pipe and handle the callbacks (interrupt request)<br />
� you may want to send / read data on bulk pipe<br />
� use class driver functions, host API functions to read / send data over pipes<br />
� register so-called service event handlers (_usb_host_register_service) or<br />
extend standard event handler (as shown in step 5)<br />
• Read MQX USB HOST API documentation for more details and<br />
API function description<br />
► Future MQX Development (Release 3.5, Dec/2009)<br />
• Create drivers to wrap most common standard functionality<br />
� Prepare Keyboard and Mouse I/O drivers<br />
� Wrap USB code needed to service Mass Storage Device<br />
� Wrap USB code needed to install virtual TTY device over CDC<br />
TM