USB Port

can be used in an embedded application. - Be familiar with ... Embedded Applications! ..... Slide 57. PIC® MCU: Detecting a USB Attachment ..... “Medium Speed” networking devices. ○ ADSL ...... Microchip USB Design Center: Firmware,.
4MB taille 5 téléchargements 607 vues
390_USB USB from Scratch

V1.10 Feb 28, 2008 © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

1

Objective O

When you walk out of this class, you will − Understand the basics of USB, and how it can be used in an embedded application − Be familiar with Microchip’s MCUs, development boards, and USB software framework. − Be able to create a simple PC application that exchanges data with a USB device

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

2

Agenda O

Part 1: −Introduction to Full-Speed USB

O

Part 2: −Introduction to Microchip MCHPFSUSB Firmware Solutions and Hand’s-on with CDC Class RS-232 Replacement APIs

O

Part 3:

−Using the Microchip General Purpose USB Windows® Driver and Custom Class Device Firmware

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

3

Part 1. Introduction to Full-Speed USB

V1.10 Feb 28, 2008 © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

4

Objectives O O

O O

Understand how USB can be useful in an embedded system Learn about fundamental USB architecture, protocol and programmer’s model Be aware of the factors important in designing a USB application Identify key USB capabilities in PIC18 USB MCUs

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

5

Agenda O O

Brief history of USB & USB-IF USB Fundamentals - The serious & important stuff − − − − − − − − − −

O O

Basics/Speeds Architecture/Programmer’s Model Physical Connection USB Transactions USB Transfers Device Classes Enumeration Descriptors Power Planning VID/PID & USB Compliance

PIC18F USB Microcontrollers Microchip Demo/Development Solutions

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

6

A little history... O

USB was co-developed by a group of companies…. − − − − −

O O

Compaq Intel Microsoft NEC …who wanted to make it much easier to add/remove peripheral devices from PCs

1998 - USB 1.1 2000 - USB 2.0

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

7

Universal Serial Bus Extend the functionality of your computer! Data Analysis, Data Logging, Firmware Updates, Diagnostics, Embedded Applications!

• • • • •

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

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Serial Parallel PS/2

Slide

8

USB Basics O

USB is a “Single Master + Multiple Slaves” polled bus USB Host Controller (Master) and Root Hub

Mouse

Printer

Speaker

Start Of Frame SOF

Int EP1

Iso EP1

Iso EP1

1 ms © 2008 Microchip Technology Incorporated. All Rights Reserved.

SOF

Int EP1

Iso EP1

Iso EP1

1 ms 390_USB v1.10

SOF

Int EP1

Iso EP1

Bulk Bulk EP2 EP2

1 ms Slide

9

Buses & Speeds Comparison 1394-Fire Wire

Does not have to support High-Speed to be USB 2.0 Compliant

Ethernet WiFi (b/g)

USB 2.0 LS-USB 1.5 Mb/s

HS-USB 480 Mb/s

USB 1.1

CAN Serial Port 500 Kb/s

FS-USB 12 Mb/s

Parallel Port 1 Mb/s

© 2008 Microchip Technology Incorporated. All Rights Reserved.

1.5 Mb/s

12 Mb/s 390_USB v1.10

100 Mb/s 480 Mb/s 1 Gb Slide

10

Biggest Myth O

MYTH: A Low-Speed USB peripheral can transfer application data up to 187.5 KB/s

O

(1.5 Mbps)

FACT: Impossible, because of a USB specification restriction:

− 8 byte data transfer every 10 ms − = 800 Bytes/second only

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

11

Next Biggest Myth O

MYTH: A Full-Speed USB peripheral can transfer data up to 1.5 MB/s

O

(12 Mb/s)

FACT: Impossible, 1.5 MB/s is the total bus bandwidth − − − −

Must be shared among peripherals Protocol overhead Protocol restrictions Realistic raw data throughput to a single peripheral is ~1.0 MB/s − Only 64KB/s in some cases © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

12

Key: Speed Capability All PIC18 USB Devices Full-Speed & Low-Speed

PIC16C7X5 Low-Speed Only

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

13

Physical Bus Topology USB Host Controller & Root Hub

Host (Tier 1)

Tier 2

Keyboard

Tier 3

Speaker

Hub

Logic Analyzer

Hub

Tier 4

Hub

Tier 5

Hub

Tier 6

Hub

Tier 7

Data Logger

Up to 126 peripherals... © 2008 Microchip Technology Incorporated. All Rights Reserved.

Hub: Max Chaining = 5

PIC18 USB devices are designed to be peripherals! Hub

390_USB v1.10

Slide

14

Logical Bus Topology O O

Not a tiered-star! Host software communicates to each “logical” device as if it were directly connected to the root hub Host Logical Device

Logical Device Logical Device

Logical Device Logical Device © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Logical Device

Slide

15

Accessing PC Peripherals O O

Old Way PC Peripherals: − Memory-mapped into the x86 I/O address space − Assigned a specific IRQ line − Assigned a specific DMA channel

O

O O

− Mapped into a virtual 127 device address space − Does not use any PC I/O, IRQ or DMA resources O

Accessed directly (ISA, PCI, PCMCIA)

© 2008 Microchip Technology Incorporated. All Rights Reserved.

USB Way PC Peripherals:

390_USB v1.10

Accessed indirectly using the programming interface provided by the device driver Slide

16

Sending/Receiving on the PC O O

Only High Level Access Four Basic Function Types: − − − −

MPUSBOpen(VID, PID, Endpoint, Direction) MPUSBRead(Pointer, Size, Timeout) MPUSBWrite(Pointer, Size, Timeout) MPUSBClose(Handle)

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

17

Sending/Receiving on the Device O O

Only high level access Example: CDC Class RS-232 Emulation

void void void void byte byte

putrsUSBUSART(const rom char *data); putsUSBUSART(char *data); mUSBUSARTTxRom(rom byte *pData, byte len); mUSBUSARTTxRam(byte *pData, byte len); getsUSBUSART(char *buffer, byte len); mCDCGetRxLength(void);

You are not directly reading/writing to the peripheral SFRs anymore! © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

18

USB Device Framework Peripheral Device

Non USB Format

USB Device Driver

N - Driver Specific Pipes (Ep1..)

Non USB Format

USB System Software

Physical Device

Device Specific Comm

2-Default Control Pipes (Ep0_OUT/IN)

Logical Device

USB Data Frames

USB Adapter/Controller

USB Device Bus Interface Layer Layer

Bus Interface USB Device Layer Layer

Host System User Code

Function Layer

Function Layer

- Software View of Hardware -

USB Data Frames

Cable

Bus Interface

Physical Communication Path Logical Communication Path (“Pipe”) © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

19

The “Logical” USB Device Device (Manufacturer: Microchip Technology) (Product: Mouse in a Circle Demo) (VID_PID: 04d8_0000)

Configuration (Active)

Interface (Mouse) USB Device-Specific Pipe(s) (HID)

Endpoint 1 (Interrupt - IN)

Analog/Digital I/O

MCHPFSUSB HID TX/RX Functions

Endpoint 1 (Interrupt - OUT)

USB System Software (default control pipes)

Endpoint 0 (Control - OUT)

Endpoint 0 (Control - IN)

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Logical settings are represented by a Device Descriptor Table, stored in the device…more on this later. Slide

20

Physical Interface

O O

VBUS

VBUS

D+

D+

D-

D-

GND

GND

~ 5.0 V ~ 3.3 V

Half Duplex with NRZI Data Encoding Bus Power to each device: − − −

4.40 - 5.25 V Guaranteed 100 mA 500 mA maximum through negotiation

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Must use external power if more is required

Slide

21

USB Connectors “A” Plug USB Host

“B” FS, HS Peripheral

“mini-B” FS, HS Peripheral

“micro-B” Peripheral

LS, FS, HS Peripheral Hardwired or Custom Detachable

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

22

Agenda O O

Brief history of USB & USB-IF USB Fundamentals - The serious & important stuff − − − − − − − − − −

O O

Basics/Speeds Architecture/Programmer’s Model Physical Connection USB Transactions USB Transfers Device Classes Enumeration Descriptors Power Planning VID/PID & USB Compliance

PIC18F USB Microcontrollers Microchip Demo/Development Solutions

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

23

How do the host and the device communicate?

Transactions… V1.10 Feb 28, 2008 © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

24

An Endpoint is a Buffer Data Bucket

OUT Transaction

IN Transaction

O

RAM Endpoint 1 IN

LED

Keystroke(s)

Maximum number of endpoints per device specified by USB specification: − −

O

RAM Endpoint 1 OUT

16 OUT endpoints + 16 IN endpoints = 32 endpoints PIC18F87J50, PIC18F4550 supports up to 32 endpoints

EP0 = Default Communication Pipe

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

25

USB Transaction USB Transaction Token Packet

SETUP and OUT token types inform the target device that the host wants to send data. IN token type informs the target device that the host wants to fetch data. © 2008 Microchip Technology Incorporated. All Rights Reserved.

Specifies: • Target device address • Endpoint number • Direction of the data transfer

390_USB v1.10

Slide

26

USB Transaction - IN USB Transaction IN Token Packet

Data Packet

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

27

USB Transaction - IN USB Transaction IN Token Packet

Data Packet

Handshake Packet

Acknowledge - ACK © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

28

IN Transaction ACK USB Traffic

USB PIC® MCU

USB Transaction IN Token Packet (to EP1 IN)

Data Packet

Serial Interface SIE Sends Data

Engine

RAM Endpoint 1 IN

0

SFR EP1 IN BD (UOWN, Length, Pointer)

Update BD

390_USB v1.10

= WN Keystrokes

UO s t e S

Handshake Packet

© 2008 Microchip Technology Incorporated. All Rights Reserved.

SFR EP1 OUT BD If U RAM (UOWN, Length, OW Endpoint N = 1 OUT Pointer) 1

Slide

29

IN Transaction NAK USB Traffic

USB PIC® MCU

USB Transaction IN Token Packet (to EP1 IN)

Serial Interface

Handshake Packet

© 2008 Microchip Technology Incorporated. All Rights Reserved.

Engine

SFR EP1 OUT BD If U RAM (UOWN, Length, OW Endpoint N = 1 OUT Pointer) 0 RAM Endpoint 1 IN

SFR EP1 IN BD (UOWN, Length, Pointer)

SIE Sends NAK

390_USB v1.10

Slide

30

OUT/SETUP Transaction ACK USB Traffic

USB PIC® MCU

USB Transaction OUT/SETUP Token Packet (to EP1 OUT)

Serial Interface

Data Packet Handshake Packet

If U

SIE Deposits Data

Engine SIE Sends ACK

© 2008 Microchip Technology Incorporated. All Rights Reserved.

OW N=

1

SFR EP1 OUT BD RAM (UOWN, Length, Endpoint 1 OUT Pointer)

=0 N OW SFR EP1 IN BD U , RAM h t g (UOWN, Length, enEndpoint 1 IN L s Pointer) Set

390_USB v1.10

Slide

31

OUT/SETUP Transaction NAK USB Traffic

USB PIC® MCU

USB Transaction OUT/SETUP Token Packet (to EP1 OUT)

Serial Interface

Data Packet

Engine D

SI E

Handshake Packet

isc ar ds Da SIE Sends NAK ta

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

If U

OW N=

0

SFR EP1 OUT BD RAM (UOWN, Length, Endpoint 1 OUT Pointer) RAM Endpoint 1 IN

SFR EP1 IN BD (UOWN, Length, Pointer)

Slide

32

A Transaction in Action

- Beagle12 Capturing READ_POT -

Refer to Appendix A in Handout for the complete PDFSUSB Command/Response Reference

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

33

Key: Token Types SETUP

OUT

IN

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

34

How do the host and device communicate?

Transfers… V1.10 Feb 28, 2008 © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

35

Transfers Vs. Transactions MPUSBWrite(EP7, Pointer, Size = 129, Timeout)

Transfer Transaction 1

Key: OUT Token Packet

64 Byte Payload

Data Packet

Transaction 2

ACK Handshake Packet

64 Byte Payload

Transaction 3 Transfer: Group of related transactions.

1 Byte Payload

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

36

Data Transfer Types Transfer/ Endpoint Type

Polling Interval

Priority

USB FS.Max. # Data Bytes/Frame/Endpoint (# transactions per frame)*

Data Integrity

Interrupt

Fixed, Periodic

High

64 (1 x 64)

Yes

Isochronous

Fixed, Periodic

High

1023 (1 x 1023)

No

Bulk

Variable, Uses Low Free Bandwidth

1216 (19 x 64)

Yes

Control

Variable

832 (13 x 64)

Yes

Medium

*Assumes transfers use maximum packet sizes allowed per Ep type © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

37

Interrupt Transfer Example MPUSBWrite(EP7, Pointer, Size = 129, Timeout) Traffic to other Endpoints or Idle

64

Transactions

1

64

Frame = 1ms © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

38

Bulk Transfer Example MPUSBWrite(EP7, Pointer, Size = 129, Timeout) Transactions only occur if no higher priority traffic

Int.

Isoc.

Int.

64

64

Int.

1

Frame = 1 ms © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

39

Theoretical Maximum Transfer Rate Per Endpoint 1400

1216

1200

1023

1000 KByte/s

832

800 600

Low Full

400 200 0

24

0.8

64

Control Interrupt

Bulk

Iso

Transfer Type © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

40

Transfer Types - Examples

Mouse

Printer

Speaker

Control + Interrupt

Control + Bulk

Control + Isochronous

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

41

Key: Transfer Types All PIC18 USB Devices Control, Interrupt, Bulk, Isochronous

PIC16C7X5 Control and Interrupt only

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

42

USB Device Classes Floppy Drive

Data Glove

Ethernet Adapter

Mouse External Hard Drive PICkit™ 2 Starter Kit Joystick

Mass Storage Device Class (MSD)

Modem

Communication Device Class (CDC)

Keyboard

ICD2 Custom Class (Vendor Class)

Human Interface Device Class (HID)

Many more classes…. © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

43

Considerations When Using Standard Classes O

CDC (Virtual Com Port)

O

− Device communication protocol defined O

O

O

− Device communication protocol undefined

The PIC looks like a modem, or terminal connected to a serial port The PC Host application/PIC exchange ASCII strings (default) Call management packets sent in addition to the data packets −

O

O

PC application talks directly to the endpoint in the PIC The PC application/PIC exchange raw bytes

More on the USB CDC Device Class in Part 2

Lower data rate than custom class (80kByte/s vs 1MByte/s)

© 2008 Microchip Technology Incorporated. All Rights Reserved.

Custom

390_USB v1.10

Slide

44

Example – Using CDC Application Application

UART Driver

UART Driver

CDC Driver

UART Hardware

RS-232

USB Hardware

RS-232

USB

UART Hardware UART APIs Firmware Application

USB

USB Hardware

Microchip CDC RS-232 Emulation API

© 2008 Microchip Technology Incorporated. All Rights Reserved.

USB UART APIs

PIC18F4550

Firmware Application

390_USB v1.10

Slide

45

Agenda O O

Brief history of USB & USB-IF USB Fundamentals - The serious & important stuff − − − − − − − − − −

O O

Basics/Speeds Architecture/Programmer’s Model Physical Connection USB Transactions USB Transfers Device Classes Enumeration Descriptors Power Planning VID/PID & USB Compliance

PIC18F USB Microcontrollers Microchip Demo/Development Solutions

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

46

Enumeration and the magic behind “Plug&Play” Auto-Detection & Auto-Configuration

V1.10 Feb 28, 2008 © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

47

Auto-Detection: Full-Speed Peripheral Device PIC18 USB Microcontroller VUSB 3.3 V

Full Speed Identification D+ line pull-up

1.5 kΩ±5% +5V D+

Transceiver

DGND

USB Connector

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

48

Auto-Detection: Low-Speed Peripheral Device PIC18 USB Microcontroller VUSB 3.3 V

Low Speed Identification D- line pull-up

1.5 kΩ±5% +5V D+

Transceiver

DGND

USB Connector

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

49

On-chip Pull-up Resistors Peripheral Device

PIC18 USB Microcontroller VUSB 3.3 V On-chip pull-up resistors!

+5V

Controlled by UCFG & UCFG

D+

Transceiver

DGND

USB Connector

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

50

Endpoint 0 and Enumeration O

See Chapter 9 in USB 2.0 Spec for more info. PIC18 USB Microcontroller Dual Port/Access RAM Endpoint 0 OUT (Control Data)

Control Transfers

Endpoint 0 IN (Control Data)

Descriptors

Other Endpoints

Bus Reset

Set Address

© 2008 Microchip Technology Incorporated. All Rights Reserved.

Get Descriptors 390_USB v1.10

Set Configuration

Device Ready Slide

51

Descriptors Device

String 0 String 1

To other Configurations if any

String N

Configuration 1

To other Interfaces if any Interface 0

Endpoint O

Endpoint

Interface 1

Endpoint

Endpoint

Descriptors are typically stored in non-volatile/Flash memory

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

52

Descriptors - Example Manu. String

Device

USB 2.0, VID = 0x04D8, PID = 0x0007, Num. Configurations, Strings?

Configuration 1

Configuration #1: Bus-Powered, Remote Wakeup, 500mA, Num. Interfaces

Interface 0

Interface #0: HID Class, Num. Endpoints

Endpoint

Endpoint 1 IN, Interrupt Transfer Type, 64-byte buffer, Poll every 3 ms

Microchip Prod. String

PICDEM USB Other String

Go USB!

Unicode Characters © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

53

Viewing Descriptor Information - UVCView -

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

54

Power Planning O O

Max USB suspend current is 0.5/2.5mA Don’t: Power from USB Cable

VDD 22µF

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

55

Power Planning O O

Max USB suspend current is 0.5/2.5mA Do:

≤10µF

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

56

PIC® MCU: Detecting a USB Attachment

If the device is self-powered, you MUST use an I/O pin to detect a cable attachment. Must not pull up D+ or D- (and never source current on VBUS) until host drives VBUS high. © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

57

VID & PID O

Vendor ID (VID)

16-bit number

− Required to market your product − http://www.usb.org/developers/vendor/ − USD $2,000 − Technical & Legal trouble if not using your own VID O

Product ID (PID)

16-bit number

− Microchip’s Sub-licensing Program O

Every product line is required to have a unique combination of VID and PID

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

58

USB Compliance O

Compliance Testing − Must pass to use USB logo − USD ~$1,500

O

Ch9 and other USB Firmware − USB Protocol Analyzer − “USBCV” USB Command Verifier O

O

O O

Tests device for conformance to Ch9, Hub, HID , MSD and Video Class specifications www.usb.org/developers/tools/

Electrical Signal Quality Power Management

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

59

Compliance Testing O

For USB Compliance: Independent Test Labs For Device ‘Sanity Check’: USB “Plugfest”

O

For USB Compliance Testing:

O

− Must submit a compliance checklist − www.usb.org/developers/compliance/peripheral_low/ − Download “Peripheral Checklist” − − − − O

TID: Test ID Use certified USB receptacle and cable for testing Know the TID of your components All USB PIC® MCUs have a TID number. Get it at www.microchip.com/usb

Probably a good idea to take a look at the checklist even before starting your design!

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

60

Agenda O O

Brief history of USB & USB-IF USB Fundamentals - The serious & important stuff − − − − − − − − − −

O O

Basics/Speeds Architecture/Programmer’s Model Physical Connection USB Transactions USB Transfers Device Classes Enumeration Descriptors Power Planning VID/PID & USB Compliance

PIC18F USB Microcontrollers Microchip Demo/Development Solutions

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

61

PIC18 USB Flash Family NEW

PIC18F67J50 PIC18F67J50 2.0 2.0 Full-Speed Full-Speed

NEW

PIC18F87J50 PIC18F87J50 2.0 2.0 Full-Speed Full-Speed

NEW

PIC18F66J55 PIC18F66J55 2.0 2.0 Full-Speed Full-Speed

NEW

PIC18F86J55 PIC18F86J55 2.0 2.0 Full-Speed Full-Speed

96 KB 64 KB

32 KB

24 KB 16 KB

PIC18F14K50 PIC18F14K50 2.0 2.0 Full-Speed Full-Speed

8 KB

PIC18F13K50 PIC18F13K50 2.0 2.0 Full-Speed Full-Speed

20-Pin

PIC18F26J50 PIC18F26J50 2.0 2.0 Full-Speed Full-Speed

PIC18F46J50 PIC18F46J50 2.0 2.0 Full-Speed Full-Speed

NEW

PIC18F66J50 PIC18F66J50 2.0 2.0 Full-Speed Full-Speed

NEW

PIC18F86J50 PIC18F86J50 2.0 2.0 Full-Speed Full-Speed

PIC18F25J50 PIC18F25J50 2.0 2.0 Full-Speed Full-Speed

PIC18F45J50 PIC18F45J50 2.0 2.0 Full-Speed Full-Speed

NEW

PIC18F65J50 PIC18F65J50 2.0 2.0 Full-Speed Full-Speed

NEW

PIC18F85J50 PIC18F85J50 2.0 2.0 Full-Speed Full-Speed

PIC18F2550/3* PIC18F2550/3* 2.0 2.0 Full-Speed Full-Speed

PIC18F4550/3* PIC18F4550/3* 2.0 2.0 Full-Speed Full-Speed

PIC18F2455/8* PIC18F2455/8* 2.0 2.0 Full-Speed Full-Speed

PIC18F4455/8* PIC18F4455/8* 2.0 2.0 Full-Speed Full-Speed

PIC18F2450 PIC18F2450 2.0 2.0 Full-Speed Full-Speed

PIC18F4450 PIC18F4450 2.0 2.0 Full-Speed Full-Speed

* Denotes 12-bit ADC

NEW

Design Existing

28-Pin

© 2008 Microchip Technology Incorporated. All Rights Reserved.

40/44-Pin 390_USB v1.10

64-Pin

80-Pin Slide

62

USB Module Peripheral Device Board 3.3 V Regulator Bus Pull-Up Resistors D+/D-

PIC18F4550

Transceiver

Data RAM USB RAM

SIE

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Program Memory

Control Registers

Slide

63

Serial Interface Engine SIE ... O Serializes and deserializes USB data O Encodes and decodes NRZI data D+/DO Handles bit stuffing O Checks CRC to validate data packet O Detects bus signaling events and notifies the CPU through interrupts O Handles USB transactions O Handles handshaking protocol © 2008 Microchip Technology Incorporated. All Rights Reserved.

PIC18 USB MCU

Transceiver

Data RAM USB RAM

SIE

390_USB v1.10

Program Memory

Control Registers

Slide

64

System Clocks

Required for USB

© 2008 Microchip Technology Incorporated. All Rights Reserved.

SIE FS: 48 MHz LS: 6 MHz

CPU

390_USB v1.10

Slide

65

Clock Configuration Example Peripheral Device Board PIC18F4550 20 MHz EC

Divide by 5

96 MHz PLL

Divide by 3

Divide by 2

Accept 4 MHz Input Only

SIE

PIC® MCU Core

32 MHz : 8 MIPs

SIE Clock = 48 MHz : Full-Speed Mode

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

66

Agenda O O

Brief history of USB & USB-IF USB Fundamentals - The serious & important stuff − − − − − − − − − −

O O

Basics/Speeds Architecture/Programmer’s Model Physical Connection USB Transactions USB Transfers Device Classes Enumeration Descriptors Power Planning VID/PID & USB Compliance

PIC18F USB Microcontrollers Microchip Demo/Development Solutions

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

67

PICDEM™ Full-Speed USB O O O O

PIC18F4550, 20 MHz xtal USB Port Serial Port PICtail™ Daughter Board Compatible

© 2008 Microchip Technology Incorporated. All Rights Reserved.

O O O

Power LED Indicators Potentiometer Temperature Sensor Part Number - DM163025

390_USB v1.10

Slide

68

PIC18F87J50 FS USB Plug-In Module (PIM) O O O O

PIC18F87J50, 12 MHz xtal USB Port, mini-B Linear Regulator 2 LEDs, 2 Pushbuttons

O O

O

Part Number – MA180021

Stand alone operation Operation with: HPC Explorer Demo Board ICSP™ Technology: 6-pin header HPC Explorer – DM183022

Part Number – AC164110 © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

69

PICtail™ Daughter Board for SD and MMC O O

Secure Digital Card Interface - using SPI AN1003: USB Mass Storage

Part Number – AC164122

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

70

Microchip USB Firmware Characteristics O O O O

O

C18 Compatible MPLAB® IDE Project Centric Polling Scheme Cooperative Multi-Tasking (No Blocking Routines) Program Memory Usage − − − −

USB Enumeration (Chapter 9) HID CDC (RS-232 Emulation) USB Mass Storage

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

-

3 KB 1 KB 1 KB 4 KB Slide

71

Summary – Part 1 O

Basics/Architecture

− Up to 126 devices sharing bandwidth − Host is master

O

Host/Device Communication − Transactions − Transfers

O

Enumeration/Chapter 9 − Descriptors

O

Microchip Offerings: MCUs, Demo Boards, Firmware, Custom Driver

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

72

Part 2.

Introduction to Microchip MCHPFSUSB Firmware Solutions and Hand’s-on with CDC Class RS-232 Replacement APIs

V1.10 Feb 28, 2008 © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

73

Objectives O

O O

Introduction to Microchip’s MCHPFSUSB USB Firmware Framework Introduction to the CDC Device Class Get hands-on experience in using Microchip’s Device Class USB Firmware − CDC Class: RS-232 Emulation API (AN956)

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

74

Agenda O O

USB Knowledge Check Device Classes & Microchip’s USB Device Class Firmware Solutions − Exercise 2.1 O

Play with the General Purpose Driver Demo

− Exercise 2.2 O

O O

Upload HID Mouse Example using Bootloader

Introduction to the USB CDC Device Class Specification Using the Microchip CDC Class Firmware: RS-232 Emulation API − Exercises 2.3.1 – 2.3.5 O

CDC RS-232 Emulation APIs

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

75

USB Knowledge Check O O O O O O

Maximum number of devices USB can support? Number of pins in USB connector? What is FS USB data rate? Which direction is Data-In? USB data transfer types? What is USB Enumeration?

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

76

Device Classes & Microchip’s USB Device Class Firmware Solutions

V1.10 Feb 28, 2008 © 2007 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.0 v1.10

Slide

77

USB Device Classes -ReviewFloppy Drive

Data Glove

Ethernet Adapter

Mouse External Hard Drive PICkit 2 Joystick

Mass Storage Device Class (MSD)

Modem

Communication Device Class (CDC)

Keyboard

ICD2 Custom Class (Vendor Class)

Human Interface Device Class (HID)

Many more classes…. © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

78

Using vs. Creating Device Class PIC18F4550

Control Transfer (SETUP, OUT, IN)

EP0

Chapter9

EP1 Device Class

Interrupt Bulk Isochronous

...

(OUT, IN)

EP15

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

User Application

Slide

79

Human Interface Device (HID) PC Computer

PIC® Microcontroller HID\Mouse

Standard Windows Drivers

HID App

USB Cable

Design Considerations: • 64 KB/s max • Interrupt Transfer Type • Standard Windows driver • Custom PC application can access HID data through Win32 APIs © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

80

CDC - RS-232 Emulation PC Computer

PIC® Microcontroller CDC

Standard Windows Drivers

USB Cable

Hyper Terminal

INF File Required

Design Considerations: • ~80 KB/s max • PC applications can access the device as though it is connected to a serial COM port © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

81

General Purpose Driver PIC® Microcontroller

PC Computer

Boot MCHPUSB.sys MPUSBAPI.dll PDFSUSB.exe Demo Binary Only USB Cable INF File Required

Source and Binary

Other C++ Binary Only Examples Source and Binary

Design Considerations: • ~1,088 KB/s max • Very flexible • Not a standard Windows driver • PC programming is required © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

82

Mass Storage Device (MSD) See AN1003: USB Mass Storage PIC® Microcontroller

PC Computer

MSD

Standard Windows Driver

Windows Explorer

USB Cable

Design Considerations: • Acts like a disk drive • Fast data transfer over USB • Speed is limited depending on the physical media interface • For usefulness, a File System should also be implemented in firmware © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

83

Downloading the Microchip USB Framework

390_USB incorporates v1.3 of the Microchip USB Framework

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

84

Exercise 2.1 & Exercise 2.2 - Objectives -

O

O O

O O

Familiarize you with the development environment We will not be writing any code Just compiling, programming Flash memory, and playing with the default demonstration Exercise 2.1: General Purpose Driver Demo Exercise 2.2: Upload HID mouse example using the bootloader

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

85

MCHPFSUSB Firmware - Project

O

Folder Structure -

C:\MCHPFSUSB

Workspace file: MCHPUSB.mcw Output file: MCHPUSB.hex

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

86

Exercise 2.1

- General Purpose Driver Demo O O

O O O

Launch MPLAB® IDE Import C:\MCHPFSUSB\fw\_factory_hex\ picdemfsusb.hex Select MPLAB ICD 2 as a programmer Program the device Remove the 9 inch modular cable and follow instructions in Chapter 3 of the PICDEM™ FS USB User’s Guide on how to run the demo.

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

87

Exercise 2.2

- Download HID Mouse Demo via a Bootloader O O O O

O

Launch PDFSUSB.exe if you have not done so already Enter bootload mode by holding down push button S2 and hit S1 to reset Select Boot Mode Tab in PDFSUSB.exe Follow instructions in Chapter 3 of the User’s Guide on how to download a hex file Download: C:\MCHPFSUSB\fw\Hid\Mouse\output \MCHPUSB.hex

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

88

Introduction to the USB CDC Device Class Specification

V1.10 Feb 28, 2008 © 2007 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.0 v1.10

Slide

89

“Virtual” COM Port Application HyperTerminal HyperTerminal

UART Driver

UART Driver

CDC Driver

UART Hardware

RS-232

USB Hardware

RS-232

USB

UART Hardware UART APIs Firmware Application

USB

USB Hardware

Microchip CDC RS-232 Emulation API

© 2008 Microchip Technology Incorporated. All Rights Reserved.

USB UART APIs

PIC18F4550

Firmware Application

390_USB v1.10

Slide

90

The Communications Device Class (CDC) O

Encompasses a variety of devices − “POTs (Plain Old Telephone Service)” devices O

Analog phones and modems, ISDN terminal adaptors, Cell phones, “Virtual” COM ports

− “Medium Speed” networking devices O

O

ADSL Modems, Cable Modems, Ethernet Adaptors

The USB Interfaces in these devices carry generic data as well as encapsulated application-specific command protocols, such as V25ter for modems (AT command set).

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

91

Abstract Control Model (ACM) Serial Emulation (3.6.2.1) O O

ACM covers implementation of virtual COM ports 2 USB Interfaces are required − Communications Class Interface O O

1 - Int IN Ep Used to notify host of the current RS-232 connection status from the USB RS-232 emulated device

− Data Class Interface O O O

1 – Bulk IN Ep & 1 – Bulk OUT Ep Used to transfer raw data bytes that would normally be transferred over RS-232 Also may be used to send encapsulated call control (AT) commands

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

92

The “Logical” CDC-ACM USB Device Device (Manufacturer: Microchip Technology) (Product: CDC RS-232 Emulation Demo) (VID_PID: 04d8_000a)

Configuration#1 (Active)

Interface#1

Interface#0 (Communications Class Interface)

(Data Class Interface) Analog/Digital I/O

Endpoint 2 (Interrupt - IN)

usbser.sys

Endpoint 3 (Bulk - OUT)

Pipes

Endpoint 3 (Bulk - IN)

OS USB Control Pipes

MCHPFSUSB CDC TX/RX Functions

Endpoint 0 (Control - OUT)

Endpoint 0 (Control - IN)

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

93

Microchip CDC-ACM Firmware O

Summary of Class-specific requests & notifications implemented (see 6.2 & 6.3 in CDC specification for description): − The CDC firmware implements handlers for O O O O O

SEND_ENCAPSULATED_COMMAND GET_ENCAPSULATED_RESPONSE SET_LINE_CODING GET_LINE_CODING SET_CONTROL_LINE_STATE

− However, the CDC firmware currently returns a NAK to tell the host that no response is available O

O

i.e. – The CDC firmware will not issue reply to AT commands issued by the Host Application!! So – don’t use them! – You’re wasting USB bus bandwidth Only send/receive raw data

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

94

Using the Microchip CDC Class RS-232 Emulation API (Exercise 2.3)

V1.10 Feb 28, 2008 © 2007 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.0 v1.10

Slide

95

Open The CDC Project Workspace O O

O O O

O

If using an ICD 2... Attach 9” Modular cable to the demo board Attach USB cable to the demo board Select “File/Open Workspace…” Open project C:\MCHPFSUSB\fw\CDC\MCHPUSB.mcw Do not do anything else just yet

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

96

Configuration Bits

O

FS USB Clock Source: Clock src from 96 MHz PLL/2 CPU Sys Clk Postscaler: [OSC1/OSC2 Src: /1][96MHz PLL Src: /2] 96MHz PLL Prescaler: Div by 5 {20 MHz input} Oscillator: HS: HS+PLL, USB-HS USB Voltage Regulator: Enabled Watchdog Timer: Disabled PortB A/D Enable: configured as digital I/O on RESET Low Voltage Program: Disabled

O

The only file you will need to modify is user.c

O O O O O O O

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

97

Clock Configuration Example Peripheral Device Board PIC18F4550 20 MHz EC

Divide by 5

96 MHz PLL

Divide by 2

Divide by 2

Accepts 4 MHz Input Only

48 MHz : 12 MIPs SIE

CPU Core

SIE Clock = 48 MHz : Full-Speed Mode

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

98

Running the Default Code O O

O

O

Press F10 to compile project Select “Programmer\Select Programmer” and make sure that MPLAB® ICD 2 is selected Select “Programmer\Program” to download the code to the microcontroller Do not disconnect 9” Modular cable just yet

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

99

Device Manager O

O

Open “Device Manager” and take a look at the “Ports (COM & LPT)” category Observe how many COM ports are currently there on your computer

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

100

Device Manager O O

Now disconnect the 9” modular cable The device should be detected by Windows

You should see an additional ‘virtual’ COM port. Remember the number of the COM port

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

101

Using HyperTerminal O O

Launch HyperTerminal Type in a name for a new connection, and hit “OK”

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

102

HyperTerminal: Connect To O

Select the new COM port that was added during USB CDC installation and hit “OK”

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

103

HyperTerminal: COM Properties O O

Hit “OK” Terminal is now ready

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

104

HyperTerminal: First Use O

Now, hit switch 2 on the demo board, you should see the message below…

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

105

HyperTerminal: Important Note O

Before physically disconnecting a USB device, you must hang up the connection first, otherwise, the program must be closed and opened again the next time a USB device is connected.

O

Let’s take a look at the APIs

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

106

CDC APIs void putrsUSBUSART(const rom char *data); void putsUSBUSART(char *data); void mUSBUSARTTxRom(rom byte *pData, byte len); void mUSBUSARTTxRam(byte *pData, byte len); byte getsUSBUSART(char *buffer, byte len); byte mCDCGetRxLength(void);

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

107

Determinants of API O

O

Are you exchanging new-line terminated ASCII strings (canonical mode) or raw bytes (non-canonical mode) with the host application? Is the data stored in ROM or RAM?

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

108

API for Null-terminated Literal & ROM Data void putrsUSBUSART(const rom char *data); void putsUSBUSART(char *data); void mUSBUSARTTxRom(rom byte *pData, byte len); Use ‘putrs’ for literal and data stored in ROM

void mUSBUSARTTxRam(byte *pData, byte len); byte

Usage Example: putrsUSBUSART(“Hello”); or getsUSBUSART(char *buffer, putrsUSBUSART(welcome); where rom char welcome[]=(“Bye”);

byte len);

byte mCDCGetRxLength(void); © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

109

Importance of Checking State O

O

O

cdc_trf_state − CDC_TX_READY − CDC_TX_BUSY − CDC_TX_BUSY_ZLP − CDC_TX_COMPLETING When calling an API that sends data to the host, must check that: cdc_trf_state == CDC_TX_READY Or use macro: − BOOL mUSBUSARTIsTxTrfReady(void); Returns ‘1’ when state is CDC_TX_READY

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

110

Program Flow Reset

main

InitializeSystem

MainLoop

Cooperative Multitasking!! USBTasks()

No blocking functions. ProcessIO()

Use state machine.

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

main.c USBDriverService(); CDCTxService(); …

user.c Exercise_01(); Exercise_02(); …

Slide

111

Cooperative Multitasking Main Loop State Machine

Task A.1 - 200 us Task A.2 - 400 us

Task A 900 us

Task A.3 - 300 us

Task B - 100 us

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Max Loop Time 1000 us V.S. 500 us

Slide

112

Exercise 2.3.1

- Sending Literal Data O O

O O

Find function Exercise_01() in user.c Write code in this function that sends a literal nullterminated string of text (“Hello World!\r\n”) to the PC when switch 2 is pressed Take a look at examples in Exercise_Example(); Useful functions: − BOOL Switch2IsPressed(void); O O

O

Returns 1 if switch 2 was pressed Returns 0 if switch 2 was not pressed

− BOOL mUSBUSARTIsTxTrfReady(void) − Use: putrsUSBUSART(…) End of Exercise 2.3.1 Solution code:

C:\MCHPFSUSB\fw\Cdc\user\solutions\Exercise1.txt

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

113

Exercise 2.3.2

- Sending ROM Data O O

O

Find function Exercise_02() in user.c Write code in this function that sends a null-terminated string of text stored in program memory pointed to by “ex02_string” to the PC when switch 3 is pressed Useful functions: − BOOL Switch3IsPressed(void); − BOOL mUSBUSARTIsTxTrfReady(void) − Use: putrsUSBUSART(…) End of Exercise 2.3.2

O

Solution code:

C:\MCHPFSUSB\fw\Cdc\user\solutions\Exercise2.txt

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

114

APIs for Receiving Data void putrsUSBUSART(const rom char *data); len = expected number of input buffer = pointer to user buffer of size void putsUSBUSART(char *data); equals to or larger than len getsUSBUSART returns numberbyte len); void mUSBUSARTTxRom(rom byte actual *pData, of bytes copied to user buffer

void mUSBUSARTTxRam(byte *pData, byte len); byte getsUSBUSART(char *buffer, byte len); byte mCDCGetRxLength(void); © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

115

Exercise 2.3.3 - Receiving Data -

O O

Find function Exercise_03() in user.c Write code in this function that reads data from USB bus and toggles LED D4 when the data read equals ASCII character ‘1’ (0x31)

O

Useful functions & variables: − input_buffer[]; − mLED_4_Toggle(); − Use: getsUSBUSART(…) End of Exercise 2.3.3

O

Solution code:

C:\MCHPFSUSB\fw\Cdc\user\solutions\Exercise3.txt

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

116

APIs for a String of Data with Known Length void putrsUSBUSART(const rom char *data); Use mUSBUSARTTxRom for data stored in program memory

void putsUSBUSART(char *data);

255-byte maximum

void mUSBUSARTTxRom(rom byte *pData, byte len); void mUSBUSARTTxRam(byte *pData, byte len); byte getsUSBUSART(char *buffer, byte len); Use mUSBUSARTTxRam for data stored in data memory

byte mCDCGetRxLength(void); © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

117

Exercise 2.3.4

- Sending Non Null-terminated Data O O O O

O

Before starting, comment out call to Exercise_01() in ProcessIO() first Find function Exercise_04() in user.c Write code in this function that sends the following 4 bytes of data: 0x30,0x31,0x32,0x33 when switch 2 is pressed Useful functions & variables: − Switch2IsPressed(); − BOOL mUSBUSARTIsTxTrfReady(void) − output_buffer[]; − Use: mUSBUSARTTxRam(…) End of Exercise 2.3.4 Solution code: C:\MCHPFSUSB\fw\Cdc\user\solutions\Exercise4.txt

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

118

API for Null-terminated RAM Data void putrsUSBUSART(const rom char *data); void putsUSBUSART(char *data); void mUSBUSARTTxRom(rom byte *pData, byte len); ‘puts’ for data storedlen); in RAM void mUSBUSARTTxRam(byteUse*pData, byte

byte getsUSBUSART(char

Usage Example: putsUSBUSART(output_buffer); *buffer, byte len); where char output_buffer[]=(“Microchip”);

byte mCDCGetRxLength(void); © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

119

Exercise 2.3.5

- Temperature Data O

Find function Exercise_05() in user.c The program currently outputs temperature data on the physical UART port. Change the program to output data through the USB port.

O

Follow instructions in Exercise_05()

O

Use mUSBUSARTIsTxTrfReady() Use putsUSBUSART(...)

O

O

End of Exercise 2.3.5 O

Solution code:

C:\MCHPFSUSB\fw\Cdc\user\solutions\Exercise5.txt

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

120

What is Wrong with This Code? void Exercise_XX(void) { putrsUSBUSART(“Hello World!”); }

Did not check:

if(mUSBUSARTIsTxTrfReady())

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

121

What is Wrong with This Code? void Exercise_XX(void) { while(!mUSBUSARTIsTxTrfReady()); putrsUSBUSART(“Hello World!”); }

Answer: Blocking function! Not good for cooperative multitasking. CDCTxService() in USBTasks() will never get called, and cdc_trf_state will never be updated. Program will just be stuck in a loop. Remember, use a state machine!

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

122

CDC & COM-Port Numbering O

To prevent unwanted “COM-Port proliferation”

− Include a serial number in a string descriptor for any CDC class device you design. − A device that contains a serial number retains its COM port number even if moved to a different USB port on the system

O

Refer to Appendix G in the student handout for instructions how to do this.

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

123

Post Exercise Analysis O

O O O

Speed − 640 Kbits/s = 80 Kbytes/s − Faster than RS-232 (UART) − This solution is not possible with a low-speed USB device because it does not have bulk endpoint No hardware handshakes Program Memory Usage: ~ 3 KB Application Note −

O

AN956: Migrating Applications to USB from RS-232 UART with Minimal Impact on PC Software

What to do if need faster data transfer rate? −

Use the custom class (“generic”) framework…and that’s just what we’re going to do next!

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

124

Part 3. Using the Microchip General Purpose USB Windows® Driver and Custom Class Device Firmware

V1.10 Feb 28, 2008 © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

125

Objectives O

O

Hands on experience using the Microchip General Purpose USB Driver for Microsoft Windows® Hands on experience using the Microchip Custom Class USB Firmware

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

126

Agenda O O

PC Development Environment Windows® Basics

− Windows Architecture − .NET − Windows Application Development with Visual C++ 2005 Express − Windows Programming Principles − Static/Dynamic Linked Libraries − USB Driver Choices

O

Microchip General Purpose USB Driver − MPUSBAPI.DLL and mchpusb.sys − .inf Files

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

127

Agenda Continued O

The MPUSBAPI.DLL Functions − Function List − MPUSBGetDLLVersion( ) O

O

Exercise 3.1 – Create a Win32 Console Application

PC-Device Data Exchange (PC Application Code) − A simple command/response protocol − PC-Side Flow Chart − MPUSBGetDeviceCount( ), MPUSBOpen(), MPUSBRead(), MPUSBWrite(), MPUSBClose()

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

128

Agenda Continued O

PC-Device Data Exchange (Device Application Code) − Review of the Device-side Firmware Framework O

O O

Exercise 3.2 – Implement “Read Pot” Command

Error Handling & Error Codes Summary

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

129

PC Development Environment O

O

O O

PC connected to PICDEM™ FS USB demo board

PIC18F4550 loaded with default demo firmware, which implements the custom class (generic) USB firmware drivers.

Microsoft Visual C++ Express 2005 Microsoft Platform SDK 2003 R2

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

130

PC Environment

- Setting Up Visual C++ 2005 Express O O O O O O

Download & install “Visual C++ 2005 Express Edition” Download & Install Microsoft Platform SDK Update VC++ 2005 directories Update corewin_express.vsprops Update AppSettings.htm Step by step directions available in your class handout

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

131

MCHPFSUSB Firmware - Project Folder Structure -

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

132

® Windows

Basics

V1.10 Feb 28, 2008 © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

133

Windows XP Operating System O

Based on Windows NT/OS2 −

O

Function −

O

Designed by David Cutler (Designer of DECs VMS OS)

To manage the hardware and provide an environment in which a user can execute programs in a convenient and efficient manner.

Essential Features − − − −

Memory: large, flat, virtual memory address space File systems, console, and other I/O Multitasking: processes and threads Communication and synchronization O



Single user and networked

Security

Windows Architecture le o s ns n o tio C a s c i w pl o d Ap in W

UI s G s ion w at o d lic n i W A pp

MPUSBRead( )

MPUSBWrite( )

User DLLs, Win32 Environment Subsystem, System Libraries (NTDLL.DLL) USER

NTReadFile( ), NTWriteFile( )

KERNEL

I/O Manager, & Graphics, Device and File System Drivers (.SYS) I/O Request Packet(s) (IRP)

Hardware © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

135

.NET Framework le o s ns n Co atio T lic E .N pp A

Managed Code

UI s G s ion m at r Fo plic s p w A o d in W

.NET Framework Class Library Assemblies (DLLs) MPUSBRead( )

Unmanaged Code

MPUSBWrite( )

User DLLs, CLR DLLs, Win32 Environment Subsystem, System Libraries (NTDLL.DLL) USER

NTReadFile( ), NTWriteFile( )

KERNEL

Kernel © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

136

Windows Application Development - Visual C++ 2005 Express -

O

With Visual C++ 2005 Express Edition you have two basic ways of creating a interactive, graphical application: − Using the Windows API (Win32 USER Functions) O

Most programming intensive

− Using Windows Forms (.NET Framework) O

O

Least programming intensive

Visual C++ 2005 Express Edition also allows you to create two types of console application (Win32 and CLR (.NET)) − We will be developing a “Win32 Console” ANSI-C application

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

137

Windows Programming Principles

V1.10 Feb 28, 2008 © 2007 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.0 v1.10

Slide

138

Windows Programming Principles – Handles O O O

Nearly every resource is an “object” identified and referenced by a “handle” of type HANDLE Kernel objects must be manipulated by WindowsAPIs HANDLE datatype objects include: − files − processes − threads

O

pipes memory mapping events, mutexes, semaphores

Windows is rich and flexible

− Many functions perform the same or similar operations − Each function has numerous parameters and flags

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

139

Windows Programming Principles – Notation/Types O

Long and descriptive −

O

Predefined descriptive data types in upper case −

O

BOOL, DWORD, LPDWORD, ...

Predefined types avoid the * operator and make distinctions: − −

O

WaitForSingleObject WaitForMultipleObjects

LPTSTR (defined as TCHAR *) and LPCTSTR (defined as const TCHAR *)

Variable names in Win32 API descriptions use “Hungarian” notationÆrequires that a variable be prefixed with an abbreviation of its data type −

lpFileName — long pointer [to a zero terminated string]

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

140

Writing Windows (Win32) Programs O

is always included

O

All objects identified by variables of type HANDLE −

O

Symbolic constants and flags which explain their meaning −

O

CloseHandle function applies to (nearly) all objects

INVALID_HANDLE_VALUE and GENERIC_READ

ReadFile, WriteFile, and many other Windows

functions return Boolean values

− System error codes obtained through follow-up call to GetLastError ()

O

Standard C library always available (fopen() etc) − But you cannot fully exploit Windows with it

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

141

DLLs

V1.10 Feb 28, 2008 © 2007 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.0 v1.10

Slide

142

Library Linking O O

Library – A set of functions useable by user programs Static linking: −

O

Static Linked

Dynamic Linked

10010101..

10010101..

10010101..

10010101..

Compiler

Dynamic Linking: − −

OS Benefits O

O

O O

The application stores references only, so application size reduces compared to static linking Only one copy of common data and common resources are maintained in memory Leads to better utilization available memory Modularization at the OS level

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

143

DLLs – Win32 Application Run Time Linking O

O

O

User DLL (MPUSBAPI.DLL) functions explicitly loaded & unloaded when needed Program must explicitly get function addresses Windows® Win32 API calls: − LoadLibrary − FreeLibrary − GetProcAddress

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

144

O

O

DLLs – .NET Application Run Time Linking No need to explicitly get function addresses using LoadLibrary, GetProcAddress, FreeLibrary Bridge to “unmanaged” code:

[DllImport("MPUSBAPI.dll")] extern "C" © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

145

USB Driver Choices Generic Data Transfer

V1.10 Feb 28, 2008 © 2007 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.0 v1.10

Slide

146

mchpusb.sys vs. HID vs. CDC Features

mchpusb.sys (v1.0.0.6)

HID

CDC

Driver support built into Windows 64-bit PC Support

No

Yes

Yes (need .inf)

Yes

Yes

Yes

XP Ready

Yes

Yes

Yes

Vista Ready

Yes

Yes

Yes

Control

Yes

Yes

No

Interrupt

Yes

Yes

No

Bulk

Yes

No

Yes

~1.0 MB/s

64KB/s

~80kByte/s

Transfer Types

Max Speed

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

147

Other generic drivers… O

LibUSB − Open Source − Supports Windows and Linux − Supports Interrupt, Bulk, and Isochronous transfers

O

− http://libusb-win32.sourceforge.net/ WinUSB − Supported in Vista only − Supports Interrupt and Bulk − http://msdn2.microsoft.com/enus/library/aa476426.aspx

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

148

Microchip General Purpose Windows® USB Device Driver

V1.10 Feb 28, 2008 © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

149

MPUSBAPI

- Introduction O

O O O O O O

Wrapper Code for Win32 API system call functions (which ultimately communicate with 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

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

150

O

O O

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

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

151

mchpusb.inf O O O

.inf file tells windows 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

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

152

.inf File Modifications O

Modifiable sections in mchpusb.inf − [DeviceList] O

%DESCRIPTION%=DriverInstall, USB\VID_04D8&PID_000B, USB\VID_04D8&PID_000C

− [Strings] O

ONEYSOFT="Walter Oney Software" MFGNAME="Microchip Technology, Inc." INSTDISK="Microchip Technology, Inc. Installation Disc" DESCRIPTION="PIC18F4550 Family Device"

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

153

Using MPUSBAPI.DLL

O

For Run-Time Linking Applications − #include “mpusbapi.h” − Must use LoadLibrary, GetProcAddress, and FreeLibrary Win32 API calls on mpusbapi.dll

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

154

The MPUSBAPI.DLL Functions

V1.10 Feb 28, 2008 © 2007 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.0 v1.10

Slide

155

Functions in MPUSBAPI.DLL MPUSBAPI Functions MPUSBGetDeviceCount(..)

VID & PID

MPUSBOpen(..) MPUSBClose(..)

MPUSBReadInt( .. ) MPUSBRead( .. ) MPUSBWrite( .. )

VID & PID MPUSBGetDLLVersion( .. ) © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

156

MPUSBGetDLLVersion( ) O

DWORD MPUSBGetDLLVersion (void)

− Returns −32-bit revision level MMMMmmmm −MMMM – 16-bit Major Version# −mmmm – 16-bit Minor Version#

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

157

Part 3. Hands-on Exercises O

PC-Side Lab Source Files are in the following directory: C:\RTC\390_USB\Pc\Ex3\Ex3.N\Ex3.N (N is the number of the exercise)

O

Device-Side workspace is here (Ex3.2 Only): C:\RTC\390_USB\fw\Ex3.2\MCHPUSB.mcw

O

To do the labs: Open the soft-copy of the class handout and build the solution code step-by-step by cutting-and-pasting code pieces into the source code editor (MPLAB IDE and/or Visual Studio): C:\RTC\390_USB\Presentation & Handouts\390_USB v1.05 Handout.doc

O

Solution projects/workspaces are here: PC: Device:

C:\RTC\390_USB\Pc\Ex3\Ex3.N_solution\Ex3.N_solution.sln C:\RTC\390_USB\fw\Ex3.2_solution\MCHPUSB.mcw

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

158

Exercise 3.1

- Creating a Win32 Console Application O

Objective

− Get familiarized with the Visual C++ 2005 programming environment

O

Concepts to learn

− Creating/compiling/executing a basic Win32 Console application − How to load/unload MPUSBAPI DLL functions at run-time − Using MPUSBGetDLLVersion( )

O

To do

− Follow the instructions in the handout

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

159

Exercise 3.1 – Results O

Press Ctrl+F5 to run

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

160

PC-Device Data Exchange (PC Application Code)

V1.10 Feb 28, 2008 © 2007 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.0 v1.10

Slide

161

A Simple Protocol O O O

O

PC opens a communications “pipe” to a device endpoint PC-Device exchange command/response “buffers” Based on the “Demo” fw application and “_console” Pc application in MCHPFSUSB Full command set defined in Appendix A of the handout

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

162

Example: READ_POT O

Command Code: 0x37 − PC sends 0x37 − Device Returns

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

163

PC-Side Flow Chart Load Library Get Procedure Addresses

MPUSBGetDeviceCount( ) MPUSBOpen( )

Open Endpoints Load command buffer

MPUSBWrite( )

Send command buffer

MPUSBRead( )

Read response Buffer Print Response

MPUSBClose( )

Close Endpoints Free Library

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

164

MPUSBGetDeviceCount( ) O

DWORD MPUSBGetDeviceCount(PCHAR pVID_PID) − Passed: O

pVID_PID – a pointer to a VID_PID string − String Format: "vid_xxxx&pid_yyyy“ − xxxx is VID value in HEX − yyyy is PID value in HEX

− Returns: O

O

The number of attached devices with matching VID&PID (also referred to as the number of instances) Before you can open a handle (pipe) to an endpoint on a specific board (using MPUSBOpen( )), your program must determine the valid instance index of this board.

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

165

Finding the Valid Instance Index Numbers for a given VID&PID Application must run the following code after calling MPUSBGetDeviceCount( ) // The // // // // // //

total number of devices using the generic driver could be bigger than max_count. They could have different vid & pid numbers. This means if max_count is 2, the valid instance index do not necessary have to be '0' and '1'. Below is a sample code for searching for all valid instance indexes. MAX_NUM_MPUSB_DEV is defined in _mpusbapi.h

count = 0; for(int i = 0; i < MAX_NUM_MPUSB_DEV; i++) { tempPipe = MPUSBOpen(i,vid_pid,NULL,MP_READ,0); if(tempPipe != INVALID_HANDLE_VALUE) { printf("Instance Index # %d\r\n",i); MPUSBClose(tempPipe); count++; } if(count == max_count) break; }//end for

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

166

MPUSBOpen( ) O

HANDLE MPUSBOpen (DWORD instance, // Input PCHAR pVID_PID, // Input PCHAR pEP, // Input DWORD dwDir, // Input DWORD dwReserved);//Input − Passed: O O O

instance – a valid instance index number pVID_PID – a pointer to valid VID&PID string pEP – a pointer to a valid Endpoint String O

O

“\\MCHP_EPz“ or “\\MCHP_EPz_ASYNC” or “NULL”

dwDir – Indicates the direction of the end-point O O

MP_READ – IN end point MP_WRITE – OUT end point

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

167

MPUSBOpen( ) O

MPUSBOpen( ) continued: − Returns: O

This function returns a handle (“pipe”) to an end-point of the USB Device with (valid) instance, pVID_PID pEP, and dwDir

O

Return Value can be INVALID_HANDLE_VALUE

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

168

MPUSBClose( ) O

BOOL MPUSBClose (HANDLE handle);

//Input

− Closes the handle (pipe) to an end point − Arguments O

Handle – Handle to the IN end-point

− Returns O

BOOL – Success (1) Failure (0)

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

169

MPUSBWrite( ) O

DWORD MPUSBWrite (HANDLE handle, PVOID pData, DWORD dwLen, PDWORD pLength, DWORD dwMilliseconds);

O O

// // // // //

Input Input Input Output Input

Writes pData buffer to an OUT end-point Arguments: − − − − −

Handle – Handle to the OUT end-point pData – Buffer dwLen – Number of bytes to be written pLength – Number of bytes actually written dwMilliseconds – Time Out Period

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

170

MPUSBRead( ) O

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 O O O O O

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

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

171

MPUSBReadInt( ) O

DWORD MPUSBReadInt HANDLE handle, PVOID pData, DWORD dwLen, PDWORD pLength, DWORD dwMilliseconds);

O O O

// // // // //

Input Output Input Output 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: − − − − −

handle – Handle to the IN end-point pData – Buffer (pointer to array of integers) dwLen – Number of bytes to be read (should match report size) pLength – Number of bytes actually read dwMilliseconds – Time Out Period

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

172

MPUSBRead or MPUSBReadInt? O O

Is the device “Synchronous” or “Asynchronous”? A Synchronous device operates in lock step with host application − MPUSBWrite() sends command − MPUSBRead() reads the response

O

An Asynchronous device returns data without prompt from host − Ex. An HID device produces “periodic reports” which are read regularly by the host − MPUSBReadInt( ) returns one “report” at a time

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

173

Summary - DLL Function Transfer Type Usage Transfer/ Endpoint Type

Functions

Time-Out Applicable? Yes

Interrupt-OUT

MPUSBRead, MPUSBReadInt MPUSBWrite

Bulk-IN

MPUSBRead

Yes

Bulk-OUT

MPUSBWrite

Yes

Isochronous-IN

MPUSBRead

No

Isochronous-OUT

MPUSBWrite

No

Interrupt-IN

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Yes

Slide

174

DLL Read/Write Usages O

O

App issues multiple reads or writes to device App issues one Read or Write specifying up to 64KBytes

USB Stack UserAPP MPUSBAPI.DLL MCHPUSB.SYS USBHUB.SYS USBPORT.SYS USBEHCT.SYS USBHW.SYS

− Driver splits data over multiple operations

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

175

PC-Device Data Exchange (Device Application Code)

V1.10 Feb 28, 2008 © 2007 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.0 v1.10

Slide

176

Microchip Custom Class Device Firmware (“Demo”) O O O O O

Custom Class Firmware API Defining commands Polling loop Initializing Peripherals Setting up the Descriptor Tables − VID & PID − Bulk/Interrupt − Strings

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

177

Custom Class Firmware API - See usbgen.c & usbgen.h -

O

void USBGenInitEP(void); − −

O

void USBGenWrite(byte *buffer, byte len); −

O

Macro checks if the OUT Ep is busy (owned by the SIE or not)

(bit) mUSBGenTxIsBusy(void); −

O

Copies data from the Ep buffer to the user’s buffer

(bit) mUSBGenRxIsBusy(void); −

O

Copies data from user’s buffer to the Ep buffer

byte USBGenRead(byte *buffer, byte len); −

O

Called after a SET CONFIGURATION request Initializes endpoints, state machine and variables

Macro checks if the IN Ep is busy (owned by the SIE or not)

byte mUSBGenGetRxLength(void); − Retrieves the number of bytes copied into theuser’s buffer after the most recent call to USBGenRead() © 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

178

Custom Class (“Demo”) Firmware Structure Synchronous model O

O O

Accepts commands from host Returns results Waits for next command

Initialize

USB Tasks()

Process IO()

ServiceRequests()

© 2008 Microchip Technology Incorporated. All Rights Reserved.

390_USB v1.10

Slide

179

ServiceRequests( ) - Flow DiagramSTART

Yes

USB Packet Clear “counter” Received? USBGenRead()

Evaluate “dataPacket.CMD”

No Execute command function (