You are on page 1of 88

2007 Microchip Technology Incorporated. All Rights Reserved.

11046 UWD Slide 1


11046 UWD
Using the Microchip
General Purpose USB Windows

Driver
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 2
Prerequisites
Working knowledge of:
C and C++ Windows

Programming
USB basics
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 3
Class Objective
Hands on experience using the
Microchip General Purpose USB Driver
for Microsoft Windows

2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 4
Agenda
USB Review
Windows

Programming
Microchip USB Driver
USB Demo Firmware
Hands On Exercises
Detecting Connect/Disconnect
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 5
PIC18 USB Flash Family
PIC18F2550
PIC18F2553*
PIC18F2550
PIC18F2553*
PIC18F2455
PIC18F2458*
PIC18F2455
PIC18F2458*
PIC18F4550
PIC18F4553*
PIC18F4550
PIC18F4553*
PIC18F4455
PIC18F4458*
PIC18F4455
PIC18F4458*
PIC18F2450
PIC18F2450
PIC18F4450
PIC18F4450
8 KB
32 KB
28-Pin 40/44-Pin 64-Pin 80-Pin
64 KB
16 KB
24 KB
PIC18F66J50
PIC18F66J50
PIC18F86J50
PIC18F86J50
128 KB
PIC18F67J50
PIC18F67J50
PIC18F87J50
PIC18F87J50
96 KB
PIC18F66J55
PIC18F66J55
PIC18F86J55
PIC18F86J55
PIC18F65J50
PIC18F65J50
PIC18F85J50
PIC18F85J50
PIC18F13K50
PIC18F13K50
20-Pin
PIC18F25J50
PIC18F25J50
PIC18F45J50
PIC18F45J50
PIC18F26J50
PIC18F26J50
PIC18F46J50
PIC18F46J50
PIC18F14K50
PIC18F14K50
NEW NEW
Under Consideration
Existing
Design
* Denotes 12-bit ADC
NEW
NEW
NEW
NEW
NEW
NEW
NEW
All 2.0 Full-Speed
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 6
USB Hookup
PIC

MCU wires
directly to USB
connector
Only one external
component required
Weak pullup may be
on either D+ or D-
line (FS or LS)
PIC MCU
Vreg
D+
D-
470nF
(min 220nF)
USB
SIE
Xcvr
1.5k Weak
Pullup
USB B
Connector
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 7
PICDEMFS USB Demo Board
Part Number - DM163025
PIC18F4550
TC77
Temperature
Sensor
MAX232C
level
converter
USB B
connector
Status
LEDs
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 8
Environment
PC connected to PICDEM
FS USB demo board
PIC18F4550 loaded with default
demo firmware
Microsoft Visual C++ Express
2005
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 9
Environment
Download & install VC++
http://msdn.microsoft.com/vstudio/express/downloads/default.aspx
Download & Install Platform SDK
Update C++ directories
Update corewin_express.vsprops
Update AppSettings.htm
Step by step directions at
http://msdn.microsoft.com/vstudio/express/visualc/usingpsdk/
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 10
Contents of MCHPFSUSB
C:\MCHPFSUSB
This folder and the
files are created on
your system when you
install
MCHPFSUSB_Setup.EXE
This file is available
on the Distribution
Package CD or can
be downloaded by
going to:
http://www.microchip.com/usb
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 11
Hands-On Agenda
Hands-On 1: Hello World
Hands-On 2: Run-time Linking
Hands-On 3: Visual C++ .NET
Demonstration:
Using WM_DEVICECHANGE
Windows message to detect
device attach & detach
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 12
Quick USB Review
We will briefly cover materials on:
USB in Embedded Applications
Transfer types
Vendor ID (VID) & Product ID (PID)
Device Classes
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 13
Universal Serial Bus
Computer is great
Processing power!
Data Analysis,
Data Logging,
Firmware Updates,
Diagnostic
Auto detection & configuration (Plug&Play)
Easy expansion using hubs
Bus power
Data CRC protected, bad packets resent.
Three speeds:
Low- 1.5, Full- 12, High- 480 Megabits / second
Serial
Parallel
PS/2
Embedded
Applications
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 14
USB Transfer Types
Control
Bus Enumeration & Configuration
Interrupt
Guarantees quality and timeliness but only for
low quantity
Bulk
Guarantees data integrity
Timeliness not guaranteed
Isochronous
Guarantees timeliness
Data quality not guaranteed
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 15
Categorizing USB
Peripherals
Joystick
Mouse
External
Hard Drive
ICD2
Modem
External
DVD Drive
Cash Register
Keyboard
Ethernet
Adapter
Human Interface Device Class
(HID)
Mass Storage Device
Class (MSD)
Communication Device
Class (CDC)
Custom Class
(Vendor Class)
Many more Device Classes.
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 16
USB Communication
USB Devices identified by a
Vendor and a Product ID
Specific function within device
specified by an EndPoint
Data transferred in buffers
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 17
Every product line is required to have a unique
combination of VID and PID
Vendor ID (VID) 16-bit number
Required to market your product
USD $2,000
Included with USB Implementors Forum membership
($4000)
Technical & Legal trouble if not using your own VID
http://www.usb.org/developers/vendor/
Product ID (PID) 16-bit number
Identifies unique product within the Vendor ID
Vendor ID & Product ID
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 18
MCHP PID Sublicense
Microchip may lend a PID for your
device
Low volume (<10k) devices only
MUST apply for this sublicense
MUST receive written permission
Approval not automatic
One per company
Download Application for Sublicense to
Microchip Universal Serial Bus Vendor
IDfrom www.microchip.com/USB
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 19
Endpoint
Buffer 8-64 bytes
Data transferred via endpoints
Endpoint is a combination of an EP
number and direction
EP0 control endpoint
EP1-15 application specific
Examples:
Mouse:
1 Interrupt IN
CDC:
1 Interrupt IN
1 Bulk IN & 1 Bulk OUT,
EP15 Out
EP0 Out
EP0 In
EP15 In
EP1 Out
EP1 In
PIC MCU
Memory
Space
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 20
Windows

Programming
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 21
10010101..
Library Linking
10010101..
Static
Linked
10010101..
10010101..
Dynamic
Linked
Library A set of
functions useable
by user programs
Static linking:
Compiler
Dynamic Linking:
OS
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 22
DLLs Run Time Linking
DLLs explicitly loaded &
unloaded when needed
Program must explicitly get
function addresses
Windows

OS calls:
LoadLibrary
FreeLibrary
GetProcAddress
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 23
.NET Environment
.NET works with managed
code
Bridge to unmanaged code
[DllImport("MPUSBAPI.dll")] extern "C" <fn prototype>
No need to do LoadLibrary,
GetProcAddress, FreeLibrary
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 24
DLL search order
1. Already loaded in Memory
2. The directory where the executable
module for the current process is
located
3. The current directory
4. The Windows system directory
5. The Windows directory
6. The directories listed in the PATH
environment variable
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 25
Microchip General
Purpose USB Device
Driver
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 26
Windows Applications
C
o
n
s
o
l
e

A
p
p
l
i
c
a
t
i
o
n
s
C
o
n
s
o
l
e

A
p
p
l
i
c
a
t
i
o
n
s
G
U
I

A
p
p
l
i
c
a
t
i
o
n
s
G
U
I

A
p
p
l
i
c
a
t
i
o
n
s
System Libraries, User Libraries System Libraries, User Libraries
(DLLs) (DLLs)
Device Drivers (.SYS)
Device Drivers (.SYS)
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 27
MPUSBAPI - Introduction
Wrapper Code for mchpusb.sys
Works with all Microchip USB Devices
Works with Visual C++ .NET, C++ Builder,
Visual Basic .NET, and more
Easy & Flexible data transfer
Supports Bulk and Interrupt transfers
Functional interface simplifies application
development
Free
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 28
Class Driver Choices
HID
Mchpusb.sys
CDC, MSD,
Audio, HID,
etc.
Control PC
Application
Bandwidth
> 64KB/sec
<= 64KB/sec
No
Yes
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 29
MCHPUSB.sys vs. HID
~1.0 MB/s
Yes
Yes
Yes
In Development
Yes
In Development
No
Mchpusb.sys
Yes XP Ready
64KB/s Max Speed
No Bulk
Yes Interrupt
Yes Control
Transfer Types
Yes Vista Ready
Yes 64-bit PC Support
Yes Driver support built
into Windows
HID Features
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 30
Other generic drivers
LibUSB
Open Source
Supports Windows and Linux
Supports Interrupt, Bulk, and Isochronous
transfers
http://libusb-win32.sourceforge.net/
WinUSB
Supported in Vista only
Supports Interrupt and Bulk
http://msdn2.microsoft.com/en-
us/library/aa476426.aspx
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 31
Microchip General Purpose
USB Device Driver
mchpusb.inf This file contains
the information about the device
driver
mchpusb.sys - Microchip USB
Device Driver
These files can be found in the
following path
C:\MCHPFSUSB\Pc\MCHPUSB
Driver\Release
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 32
MCHPUSB.INF
.INF file tells window how to
configure and use a device
Device Manager reads the VID and
PID from the target device
Windows uses .INF file to:
Associates driver to use with VID & PID
Specifies device identification strings
Specifies source and destination of
device files
Sets registry keys
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 33
INF File Modifications
Modifiable sections in MCHPUSB.INF
[DeviceList]
%DESCRIPTION%=DriverInstall,
USB\VID_04D8&PID_000B,
USB\VID_04D8&PID_000C
[Strings]
ONEYSOFT="Walter Oney Software"
MFGNAME="Microchip Technology, Inc."
INSTDISK="Microchip Technology, Inc.
Installation Disc"
DESCRIPTION="PIC18F4550 Family Device"
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 34
Using MPUSBAPI.DLL
Run-Time Linking Applications
#include mpusbapi.h
Use mpusbapi.dll
For non .NET applications, must use LoadLibrary,
GetProcAddress, and FreeLibrary on mpusbapi.dll
The .NET framework automatically loads, resolves, and
unloads DLL functions
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 35
Functions in MPUSBAPI.DLL
MPUSBAPI Functions
MPUSBOpen( .. )
MPUSBClose( .. )
MPUSBReadInt( .. )
MPUSBRead( .. )
MPUSBWrite( .. )
VID & PID
MPUSBGetDeviceCount(..)
MPUSBGetDLLVersion( .. )
VID & PID
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 36
DLL Functions
DWORD MPUSBGetDeviceCount (PCHAR
pVID_PID)
pVID_PID
Datatype: String
Format: " vid_xxxx&pid_yyyy
xxxx is VID value in HEX
yyyy is PID value in HEX
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 37
DLL Functions
HANDLE MPUSBOpen
(DWORD instance, // Input
PCHAR pVID_PID, // Input
PCHAR pEP, // Input
DWORD dwDir, // Input
DWORD dwReserved);// Input
This function returns a handle to an end-point of the
USB Device with specified pVID_PID and pEP
The returned handle is an end-point handle
dwDi r Indicates the direction of the end-point
MP_READ read end point
MP_WRITE write end point
Return Value can be INVALID_HANDLE_VALUE
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 38
DLL Functions
MPUSBOpen continued:
pVI D_PI D
Datatype: String
Format: " vid_xxxx&pid_yyyy
xxxx is VID value in HEX
yyyy is PID value in HEX
pEP
Datatype: String
Format: " \\MCHP_EPz"
NOTE:
To use MPUSBReadInt(), the format of pEP has
to be "\\MCHP_EPz_ASYNC"
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 39
DLL Functions
BOOL MPUSBClose
(HANDLE handle); //Input
Closes the handle to an end point
Arguments
Handle Handle to the IN end-point
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 40
DLL Functions
DWORD MPUSBWrite
(HANDLE handle, // Input
PVOID pData, // Input
DWORD dwLen, // Input
PDWORD pLength, // Output
DWORD dwMilliseconds); // Input
Writes pData buffer to an OUT end-point
Arguments:
Handl e Handle to the OUT end-point
pDat a Buffer
dwLen Number of bytes to be written
pLengt h Number of bytes actually written
dwMi l l i seconds Time Out Period
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 41
DLL Functions
DWORD MPUSBRead
(HANDLE handle, // Input
PVOID pData, // Output
DWORD dwLen, // Input
PDWORD pLength, // Output
DWORD dwMilliseconds);// Input
Reads from the IN end-point and fills a buffer
Arguments
Handle Handle to the IN end-point
pData Buffer
dwLength Number of bytes to be read
pLength Number of bytes actually read
dwMilliseconds TimeOut period
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 42
DLL Functions
DWORD MPUSBReadInt
HANDLE handle, // Input
PVOID pData, // Output
DWORD dwLen, // Input
PDWORD pLength, // Output
DWORD dwMilliseconds); // Input
Reads from an Interrupt IN end-point and fills a buffer
The user application should call MPUSBReadInt() often
enough to avoid data losses
Arguments:
handl e Handle to the IN end-point
pDat a Buffer (pointer to array of integers)
dwLen Number of bytes to be read
pLengt h Number of bytes actually read
dwMi l l i seconds Time Out Period
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 43
DLL Functions
DWORD MPUSBGetDLLVersion (void)
32-bit revision level MMMMmmmm
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 44
DLL Read/Write Usages
App issues multiple
reads or writes to device
App issues one Read or
Write specifying up to
64KBytes
Driver splits data over
multiple operations
UserAPP
MPUSBAPI.DLL
MCHPUSB.SYS
USBHUB.SYS
USBPORT.SYS
USBEHCT.SYS
USBHW.SYS
USB Stack
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 45
MPUSBRead or MPUSBReadInt
Synchronous or Asynchronous?
Synchronous device operates in
lock step with host application
Asynchronous returns data
without prompt from host
Typically used for periodic report
ReadInt returns one buffer at a
time
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 46
Error Handling
Good Practice: Check the return
code for error values
MPUSB_SUCCESS
MPUSB_FAIL
INVALID_HANDLE_VALUE
GetLastError()
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 47
Some GetLastError() Codes
0 - ERROR_SUCCESS
No Error
2 ERROR_FILE_NOT FOUND
Attempt to open Bulk endpoint for ASYNC Read
5 - ERROR_ACCESS_DENIED
6 - ERROR_INVALID_HANDLE
File Handle returned by MPUSBOpen when VID & PID
not attached to bus
997 - ERROR_IO_PENDING
Link: http://msdn2.microsoft.com/en-
us/library/ms681381.aspx
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 48
MPUSB Write Results
MPUSB_SUCCESS
Buf f er was transferred
MPUSB_FAIL
GetLastError: 997 (IO Pending)
Firmware not servicing endpoint fast
enough
Timeout value too short
GetLastError: 5 (Access Denied)
Device unplugged
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 49
MPUSBRead Results
Read (Hnd, buffer, Expected, &Actual,
Timeout)
Sent = Expected
MPUSB_SUCCESS
Act ual = #Bytes Sent = Expected
Buf f er contains data
Sent > Expected, but correct # packets sent
MPUSB_FAIL GetLastError = 31 (Invalid data)
Act ual = 0
Buf f er contains data received
Sent > Expected, extra # packets sent
Same as above
Extra packet left in Buffer, Pgm& Device out of sync
Next read gets buffered packet instead of newest
Issue extra reads to clean out buffer until GetLastError = 997
SENT
Expected
SENT
Expected
DEVICE SENT
Expected
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 50
MPUSBRead Results
Sent < Expected, but correct # packets sent
MPUSB_SUCCESS
Act ual = #Bytes Sent
Buf f er contains data
Sent < Expected, insufficient # packets sent
MPUSB_FAIL GetLastError = 997 (IO
pending)
Act ual = 0
Buf f er contains data received
Device Sent
Expected
Device Sent
Expected
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 51
MPUSBReadInt Results
Only returns one packet at a time
Extra packets buffered for next read
If no packet available
MPUSB_FAIL GetLastError = 997 (IO pending)
Act ual = 0
Buf f er contains data received
Sent < Expected
MPUSB_SUCCESS
Act ual = #Bytes Sent
Buf f er contains data
Sent > Expected
MPUSB_SUCCESS
Act ual = Expect ed
Buf f er contains data
Device Sent
Expected
Device Sent
Expected
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 52
USB Demo Firmware
Polling loop
Adding commands
Descriptors
VID & PID
Bulk/Interrupt
Strings
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 53
Demo Firmware Structure
Synchronous model
Accepts commands
from host
Returns results
Waits for next
command
Service
Requests
Process IO
USB Tasks
Initialize
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 54
Polling Loop
void ServiceRequests(void)
{
if(USBGenRead((byte*)&dataPacket,sizeof(dataPacket)))
{
counter = 0;
switch(dataPacket.CMD)
{
case READ_VERSION:
...
case ID_BOARD:
...
case UPDATE_LED:
...
case SET_TEMP_REAL:
...
case RD_TEMP:
...
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 55
Returning Data
case RD_POT:
ReadPOT();
dataPacket._byte[1] = ADRESL;
dataPacket._byte[2] = ADRESH;
counter = 0x03;
break;
}//end switch()
if(counter != 0){
if(!mUSBGenTxIsBusy())
USBGenWrite(&dataPacket,counter);
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 56
Sending bulk data
while(mUSBGenTxIsBusy();
USBGenWrite(&dataPacket,counter);
while(mUSBGenTxIsBusy();
USBGenWrite(&dataPacket,counter);
while(mUSBGenTxIsBusy();
USBGenWrite(&dataPacket,counter);
Inside ProcessIO:
if (smActive && !mUSBGenTxIsBusy) {
for (i=0; i<64; i++) {
dataPacket[i]=data[offset++];
if (offset >= limit) {
smActive = FALSE;
break;
}
}
USBGenWrite(&dataPacket,counter);
}
Cant Block
process
Use a State
Machine
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 57
Demo Firmware Commands
0x00 - Read Version
Returns cmd, 0x02, Major, Minor
0x34 - Read Temperature Sensor
Returns cmd, TempL, TempH
Formatted 2s complement in deg C
0x37 - Read POT
Returns cmd, ADRESL, ADRESH
10 bits, right justified
See handout Appendix A for more
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 58
Descriptors
File usbdsc.c
/* Device Descriptor */
rom USB_DEV_DSC device_dsc=
{
sizeof(USB_DEV_DSC), // Size of this descriptor in bytes
DSC_DEV, // DEVICE descriptor type
0x0200, // USB Spec Release Number in BCD format
0x00, // Class Code
0x00, // Subclass code
0x00, // Protocol code
EP0_BUFF_SIZE, // Max packet size for EP0, see usbcfg.h
0x04D8, // Vendor ID
0x000C, // Product ID: PICDEM FS USB (DEMO Mode)
0x0000, // Device release number in BCD format
0x01, // Manufacturer string index
0x02, // Product string index
0x00, // Device serial number string index
0x01 // Number of possible configurations
};
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 59
Endpoint Descriptors
File usbdsc.c
/* EndPoint Descriptor */
sizeof(USB_EP_DSC),DSC_EP,_EP01_OUT,_BULK,USBGEN_EP_SIZE,1,
sizeof(USB_EP_DSC),DSC_EP,_EP01_IN,_BULK,USBGEN_EP_SIZE,1
_BULK or _INT (Interrupt)
USB_EP_SIZE defined in USBCFG.H
#define USBGEN_EP_SIZE 64
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 60
String Descriptors
r omst r uct {byt e bLengt h; byt e bDscType; wor d st r i ng[ 1] ; }sd000={
si zeof ( sd000) , DSC_STR, 0x0409};
rom struct{byte bLength;byte bDscType;word string[25];}sd001={
sizeof(sd001),DSC_STR,
'M','i','c','r','o','c','h','i','p',' ',
'T','e','c','h','n','o','l','o','g','y',' ','I','n','c','.'};
rom struct{byte bLength;byte bDscType;word string[33];}sd002={
sizeof(sd002),DSC_STR,
'P','I','C','D','E','M',' ','F','S',' ','U','S','B',' ',
'D','e','m','o',' ','B','o','a','r','d',' ','(','C',')',
' ','2','0','0','4'};
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 61
Exercise 1 - Hello World
Start Visual C++ 2005 Express
File | New | Project
Select CLR Console
Application
Name project Ex1
Select OK
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 62
Exercise 1
VC++ automatically creates Hello
World application
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 63
Ex1 - results
Press Ctrl/F5 to run
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 64
Exercise 2
Run-time Linking
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 65
Application Interacting with
Demo Firmware
Objective
Obtain DLL Version information, number
of devices and Firmware Version
Concepts to learn
Using MPUSBAPI.DLL in Run-Time
Linking
Obtain device count, and data from the
device
Using the functions MPUSBOpen,
MPUSBRead, MPUSBWrite,
MPUSBClose
Error Handling
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 66
Exercise 2 Algorithm
Open Endpoints
Load command buffer
Send command buffer
Read response Buffer
Print Response
Close Endpoints
Load Library
Get Procedure Addresses
Free Library
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 67
Run-time Linking with
MPUSBAPI.DLL
Expected Output
Application compiles successfully
Returns the number of devices
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 68
Exercise 3 VC++.NET
Open project FormsApp
File|Open|Project/Solution FormsApp
Double click on Form1.h
Fill in buttons with code on
handout
Walk through first one
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 69
Application Interacting with
Demo Firmware
Recap of what you learned
Run time linking
Visual C++.NET
Sending and Receiving data between USB
Device and Host using the MPUSBAPI.DLL
MPUSBOpen, MPUSBRead, MPUSBWrite,
MPUSBClose, MPUSBReadInt
Error Handling
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 70
WM_DEVICECHANGE
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 71
Windows Events
Windows is Event Driven OS
Event: Notification sent by OS to
application when activity related to
the application occurs
Events are Global
Examples:
WM_COMMAND, WM_PAINT
WM_DEVICECHANGE
Can be handled in GUI Applications
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 72
Application Development
Using Win32 API
Structure of the Win32 API application
Entry Point - WinMain
Register with the OS the Main
Window and Event Handler using the
RegisterClass APIs and WNDCLASS
structures
Create the Main Window using the
CreateWindowAPI
Start the Message Loop
WinMain WinMain
Call Call RegisterClass RegisterClass
(Register the (Register the
Main Window Class , Main Window Class ,
Event Handler) Event Handler)
Call Call CreateWindow CreateWindow
(Create the Main Window) (Create the Main Window)
Receive Message (OS Event) Receive Message (OS Event)
Invoke the Event Handler Invoke the Event Handler
Exit Application Exit Application
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 73
Event Handler - WndProc
During registration process a Windows
Message Handler Function WndProc()
is associated with the Main Window
Prototype:
LRESULT CALLBACK WndProc(HWND
hWnd, unsigned msg, WORD wParam, LONG
lParam)
msg Message received by the application
wParam, lParam Message specific
Information
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 74
WM_DEVICECHANGE
Notifies an application of change of H/W
configuration
A Window receives this message through
WndProc(HWNDhWnd, unsigned msg, WORD wParam,
LONG lParam) function
hWnd Handle of the Window
msg WM_DEVICECHANGE
wParam Event that has occurred
DBT_DEVICEARRIVAL
DBT_DEVNODES_CHANGED
DBT_CONFIGCHANGECANCELED
DBT_CONFIGCHANGED etc...
lParam Contains a pointer to the structure that
contains the event specific data
Each event has associated structure defined in
C
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 75
WM_DEVICECHANGE
Notifications
DBT_CONFIGCHANGECANCELED
0x0019 A request to change the current configuration
(dock or undock) has been canceled
DBT_CONFIGCHANGED
0x0018 The current configuration has changed, due to
a dock or undock
DBT_CUSTOMEVENT
0x8006 A custom event has occurred.
Windows NT 4.0 and Windows 95: This value is not
supported
DBT_DEVICEARRIVAL
0x8000 A device or piece of media has been inserted
and is now available
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 76
WM_DEVICECHANGE
Notifications
DBT_DEVICEQUERYREMOVE
0x8001 Permission is requested to remove a device or
piece of media; any application can deny this request
and cancel the removal
DBT_DEVICEQUERYREMOVEFAILED
0x8002 A request to remove a device or piece of media
has been canceled
DBT_DEVICEREMOVECOMPLETE
0x8004 A device or piece of media has been removed
DBT_DEVICEREMOVEPENDING
0x8003 A device or piece of media is about to be
removed; cannot be denied
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 77
WM_DEVICECHANGE
Notifications
DBT_DEVICETYPESPECIFIC
0x8005 A device-specific event has occurred
DBT_DEVNODES_CHANGED
0x0007 A device has been added to or removed from
the system
Windows NT 4.0 and Windows Me/98/95: This value is
not supported
DBT_QUERYCHANGECONFIG
0x0017 Permission is requested to change the current
configuration (dock or undock)
DBT_USERDEFINED
0xFFFF The meaning of this message is user-defined
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 78
Example Code
The following code shows how the
device arrival and device removal
can be handled
Sample Code for
WM_DEVICECHANGE
Sample MFC C++ Program
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 79
Exercise 6 - Demonstration
WM_DEVICECHANGE
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 80
Using WM_DEVICECHANGE
Objective Develop a small
application that uses
WM_DEVICECHANGE
Concepts to learn
Learning to use WM_DEVICECHANGE
Steps
Load Demo firmware on the demo board
Create a GUI application
Follow steps in Handout Exercise 6
Run the application and see what
happens
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 81
Using WM_DEVICECHANGE
Expected Output
Application displays a dialog box whenever a device is
attached or detached
Returns the number of devices
Recap of what you learned
Using WM_DEVICECHANGE
Calling a function from DLL
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 82
Summary
Drivers built into Windows for
most USB devices
A Custom Class Device needs a
Vendor specific Device Driver
MCHPUSB.SYS file is a Vendor
specific Device Driver
.INF Files
MPUSBAPI.DLL simplifies your
development Activities
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 83
References
MPUSBAPI.DLL Source Code
http://www.microsoft.com
http://www.microchip.com/usb
http://forum.microchip.com
http://www.lvr.com
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 84
Dev Tools Used in This Class
PICDEMFS USB Demo Board
DM163025
Other USB Related Tools
PIC18F87J 50 FS USB Plug-In
Module (MA180021)
PICkit2 Starter Kit (DV164120)
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 85
Questions?
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 86
Thank you
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 87
Appendix
2007 Microchip Technology Incorporated. All Rights Reserved. 11046 UWD Slide 88
Trademarks
The Microchip name and logo, the Microchip logo, Accuron, dsPIC,
KeeLoq, KeeLoq logo, microID, MPLAB, PIC, PICmicro, PICSTART,
PRO MATE, rfPIC and SmartShunt are registered trademarks of Microchip
Technology Incorporated in the U.S.A. and other countries.
AmpLab, FilterLab, Linear Active Thermistor, Migratable Memory, MXDEV,
MXLAB, SEEVAL, SmartSensor 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, CodeGuard, dsPICDEM,
dsPICDEM.net, dsPICworks, ECAN, ECONOMONITOR, FanSense, FlexROM,
fuzzyLAB, In-Circuit Serial Programming, ICSP, ICEPIC, Mindi, MiWi,
MPASM, MPLAB Certified logo, MPLIB, MPLINK, PICkit, PICDEM,
PICDEM.net, PICLAB, PICtail, PowerCal, PowerInfo, PowerMate, PowerTool,
REAL ICE, rfLAB, Select Mode, Smart Serial, SmartTel, Total Endurance,
UNI/O, 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.

You might also like