AN2681 ASF - USB Device Stackww1.microchip.com/downloads/en/AppNotes/AN2681-ASF... · ASF - USB...

25
AN2681 ASF-USB Device Stack Features USB 2.0 Compliance USB chapter 9 certified Control, bulk, isochronous and interrupt transfer types Low speed (1.5Mbit/s), full speed (12Mbit/s), high speed (480Mbit/s) data rates Small Stack Size Frees Space for Main Application Real Time (OS Compliance, No Latency) Supports 8-bit and 32-bit AVR ® Platforms USB DMA Support Increases Speed Performance Supports Most USB Classes and Ready to Use Introduction This document introduces the USB device stack. This stack is included in the Advanced Software Framework (ASF), and aims to provide the customer with the quickest and easiest way to build a USB application. A full description of this stack is available in this document. Only basic knowledge of USB is required to use this stack. © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 1

Transcript of AN2681 ASF - USB Device Stackww1.microchip.com/downloads/en/AppNotes/AN2681-ASF... · ASF - USB...

  • AN2681 ASF-USB Device Stack

    Features

    • USB 2.0 Compliance– USB chapter 9 certified– Control, bulk, isochronous and interrupt transfer types– Low speed (1.5Mbit/s), full speed (12Mbit/s), high speed (480Mbit/s) data rates

    • Small Stack Size Frees Space for Main Application• Real Time (OS Compliance, No Latency)• Supports 8-bit and 32-bit AVR® Platforms• USB DMA Support Increases Speed Performance• Supports Most USB Classes and Ready to Use

    Introduction

    This document introduces the USB device stack. This stack is included in the Advanced SoftwareFramework (ASF), and aims to provide the customer with the quickest and easiest way to build a USBapplication. A full description of this stack is available in this document. Only basic knowledge of USB isrequired to use this stack.

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 1

  • Table of Contents

    Features.......................................................................................................................... 1

    Introduction......................................................................................................................1

    1. Abbreviations.............................................................................................................3

    2. USB Device Application Notes.................................................................................. 4

    3. Organization.............................................................................................................. 53.1. Overview...................................................................................................................................... 53.2. Memory Footprint......................................................................................................................... 53.3. USB Device Stack Files............................................................................................................... 5

    4. Application Programming Interface........................................................................... 74.1. External API from UDC................................................................................................................ 74.2. Internal APIs.................................................................................................................................8

    5. Behavior...................................................................................................................11

    6. Configuration........................................................................................................... 156.1. USB Configuration......................................................................................................................156.2. USB Descriptors.........................................................................................................................17

    7. Power Consumption................................................................................................ 197.1. UC3 USBB and USBC Sleep Modes......................................................................................... 197.2. XMEGA Sleep Modes................................................................................................................ 20

    8. Revision History.......................................................................................................21

    The Microchip Web Site................................................................................................ 22

    Customer Change Notification Service..........................................................................22

    Customer Support......................................................................................................... 22

    Microchip Devices Code Protection Feature................................................................. 22

    Legal Notice...................................................................................................................23

    Trademarks................................................................................................................... 23

    Quality Management System Certified by DNV.............................................................24

    Worldwide Sales and Service........................................................................................25

    AN2681

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 2

  • 1. Abbreviations• APP: User Application• ASF: Advanced Software Framework• CBW: Command Block Wrapper (from Mass Storage Class)• CDC: Communication Device Class• CSW: Command Status Wrapper (from Mass Storage Class)• DP or D+ Data Plus Differential Line• DM or D- Data Minus Differential Line• FS: USB Full Speed• HID: Human Interface Device• HS: USB High Speed• UDC: USB Device Controller• UDD: USB Device Descriptor• UDI: USB Device Interface• USB: Universal Serial Bus• MSC: Mass Storage Class• PHDC: Peripheral Health Device Class• sleepmgr: Sleep Management Service from ASF• ZLP: Zero Length Packet

    AN2681Abbreviations

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 3

  • 2. USB Device Application NotesSeveral USB device examples are provided by Microchip. Some of these examples are covered by theirown application note.

    Figure 2-1. USB Device Application Notes

    AN8445/AVR4902

    ASF-USB DeviceComposite

    Composite

    AN8481/AVR4901

    ASF-USB DeviceUser Class

    Class

    AN8409/AVR4903

    ASF-USB Device HID mouse

    mouse

    AN8446/AVR4904

    ASF-USB DeviceHID keyboard

    keyboard

    AN8499/AVR4905

    ASF-USB DeviceHID Generic

    Generic

    AN8447/AVR4907

    ASF-USB DeviceCDC

    Application notes on USB Device Class

    Application notes for Custom USB Device

    AN8360/AVR4900

    ASF-USB DeviceStack

    Stack

    Basic USB knowledge is necessary to understand the USB device class application notes (Classes: HID,and CDC).

    To create a USB device with one of the ASF provided classes, refer directly to the related application notefor this USB class.

    The new class and composite USB device application notes are designed for advanced USB developers.

    Other examples are also available in Atmel Studio. To list these, select "New Example Project..." from thestart screen or the File menu (File → New → Example Project...) in Atmel Studio 7. The list of examplescan be reduced to list only USB by either searching for USB in the search field or select USB from thetechnology tab.

    AN2681USB Device Application Notes

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 4

  • 3. Organization

    3.1 OverviewThe USB device stack is divided into three parts:

    • USB Device Controller (UDC) Provides USB Chapter 9 Compliance• USB Device Interface (UDI) Provides USB Class Compliance• USB Device Driver (UDD) Provides the USB Interface for Each AVR Product

    Figure 3-1. USB Device Stack Architecture

    UDD

    UDC UDIs

    Application

    USB Device Stack

    3.2 Memory FootprintThe USB device stack memory footprint depends on:

    • AVR Core (XMEGA®, megaAVR®, UC3)• USB Hardware Version• USB Class Used• Compiler and Optimization Level

    In average the USB device stack does not exceed 10KB of Flash and 1KB of RAM compiling with highoptimization level.

    3.3 USB Device Stack FilesThe USB device stack files are available as part of ASF in Atmel® Studio.

    To list relevant USB examples in Atmel Studio 7, select "New Example Project..." from the start screen orthe File menu (File → New → Example Project...). The list of examples can be reduced to list only USBby either searching for USB in the search field or select USB from the Technology tab.

    Note:  This USB device stack does not apply to ASF examples with names containing "from ASF V1".

    AN2681Organization

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 5

  • Table 3-1. Common Files for all AVR Products

    Files Paths

    Defines USB constant usb_protocol.h (from usb.org) common/services/usb/

    usb_atmel.h (from Microchip)

    UDC files udc.c/h common/services/usb/udc/

    udc_desc.h

    udi.h

    udd.h

    Classes protocols files usb_protocol_foo.h common/services/usb/class/foo/

    UDI files udi_foo.c/h common/services/usb/class/foo/device/udi_foo_desc.c

    udi_foo_conf.h

    Table 3-2. UDD Files Depending on Selected AVR Products

    Files Paths

    AVR32 usbb_device.c/h avr32/drivers/usbb/

    usbb_otg.h avr32/drivers/usbb/

    usbc_device.c/h avr32/drivers/usbc/

    usbc_otg.h avr32/drivers/usbc/

    XMEGA usb.c/h xmega/drivers/usb/

    MEGA AVR usb.c/h mega/drivers/usb/

    Table 3-3. Specific File for Each Application

    Files Paths

    Application file (Thisconfiguration file is mandatory)

    usb_conf.h user/application defined location,e.g. application config folder

    AN2681Organization

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 6

  • 4. Application Programming InterfaceThis section describes all USB APIs except the UDI API, which is described directly in the ASFdocumentation (http://asf.atmel.com).

    Figure 4-1. USB Blocks

    UDD

    UDC UDI -…

    Application External API from UDCDescribed in this document

    External API from UDIDescribed in external UDI documents

    Internal APIDescribed in this document for USBexperts

    4.1 External API from UDCThe external UDC API allows the application to manage common USB device behavior and receivecommon USB device events. These controls and events are common to any USB application.

    Figure 4-2. External API from UDC

    UDC

    Input functions Callback functions

    Table 4-1. External API from UDC – Input

    Declaration Description

    udc_start() Start USB device stack

    udc_stop() Stop USB device stack

    udc_attach()

    udc_detach()

    Authorize the device enumeration or not.

    Enable pull-up on DM or DP.

    udc_remotewakeup() Wake-up the USB device

    All UDC callbacks are optional and defined by user in usb_conf.h for each application. When defined, thecallbacks will be called by the UDD ISR.

    AN2681Application Programming Interface

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 7

    http://asf.atmel.com/docs/latest

  • Table 4-2. External API from UDC – Callback.

    Define name Description

    UDC_VBUS_EVENT(bool b_present) To notify VBUS level change (only if USB hardwareincludes VBUS monitoring)

    UDC_SUSPEND_EVENT() Called when USB enters in Suspend mode

    UDC_RESUME_EVENT() Called when USB wakes-up

    UDC_SOF_EVENT() Called for each received SOF each 1msNote: Available in High and Full Speed mode

    UDC_REMOTEWAKEUP_ENABLE() Called when USB host requests to enable/disableremote wake-up feature when the device supportsitUDC_REMOTEWAKEUP_DISABLE()

    UDC_GET_EXTRA_STRING()When an extra string descriptor must be supported(other than manufacturer, a product and serialstring)

    UDC_SPECIFIC_REQUEST() When a specific device setup request must besupported

    4.2 Internal APIsThe following definitions are for advanced USB users who intend to develop a specific USB device notprovided in ASF.

    Figure 4-3. Internal USB Device API Overview

    UDD

    Linked at compilation

    Callback linked during firmware execution

    (*) See tables for number references.

    (3) (5)(4)

    UDC(2)

    (1) UDI -…

    Table 4-3. UDI Input from UDC (1)

    Declaration Description

    bool (*enable)()Called by UDC to enable/disable a USB interface

    void (*disable)()

    bool (*setup)() Called when a USB setup interface request is received

    uint8_t (*getsetting)() Called by UDC to obtain the current alternate setting of a USB interface

    uint8_t (*sof_notify)() Called by UDC to notify a SOF event at USB interface enabled

    AN2681Application Programming Interface

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 8

  • Note:  The UDI API is linked with the UDC module via the UDC descriptor configuration file.

    Table 4-4. UDC Input from UDI (2)

    Declaration Description

    usb_iface_desc_t*udc_getiface()

    Gives the USB interface descriptor selected by UDC when UDI iscalled (Table 4-3)

    Table 4-5. UDC Input from UDD (3)

    Declaration Description

    void udc_reset() Called when Reset bus state occurs

    bool udc_process_setup() Called when a setup packet is received

    Table 4-6. UDD Input (4)

    Declaration Caller Description

    void udd_enable() UDCEnables/disables the USB Device mode

    void udd_disable() UDC

    void udd_attach()void udd_dettach() UDC Inserts or removes pull-up on USB line

    void udd_set_address(uint8_t add) UDCChanges/returns the USB device address

    uint8_t udd_getaddress() UDC

    bool udd_is_high_speed() UDC/UDI In case of USB HS device, then checksspeed chosen during enumeration

    uint16_t udd_get_frame_number() APP Returns the current start of frame number

    udd_send_wake_up() APPThe USB driver sends a resume signalcalled “Upstream Resume”

    bool udd_ep_alloc(usb_ep_id_t ep, uint8_tbmAttributes, uint16_t wMaxPacketSize) UDC Enables/disables endpointsudd_ep_free(usb_ep_id_t) UDC

    bool udd_ep_clear_halt(usb_ep_id_t)

    UDC/UDI

    Clears/sets/gets the endpoint state(halted or not)bool udd_ep_set_halt(usb_ep_id_t)

    bool udd_ep_is_halted(usb_ep_id_t)

    bool udd_ep_wait_stall_clear( udd_ep_id_t endp,udd_callback_nohalt_t callback)

    Registers a callback to call when endpointhalt is removed

    bool udd_ep_run( usb_ep_id_t endp, boolb_shortpacket, uint8_t *buf, uint32_tu32_size_buf, udd_callback_trans_t callback)

    UDIStarts/stops a data transfer in or out onan endpoint Note:The control endpoint is not authorizedhereudd_ep_abort(usb_ep_id_t endp) UDI

    AN2681Application Programming Interface

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 9

  • Table 4-7. UDD Callback (5)

    Declaration Description

    typedef void (*udd_callback_nohalt_t) (void);Called when the halt on endpoint is removed.This one is registered viaudd_ep_wait_stall_clear().

    typedef void (*udd_callback_trans_t)(udd_ep_status_t status, iram_size_tnb_transfered)

    Called when a transfer request is finished or canceled.This one is registered via udcdrv_ep_run().

    Table 4-8. UDD Input for High Speed Application Only (4)

    Declaration Caller Description

    uint16_tudd_get_microframe_number() APP Returns the current micro start of frame number

    udd_test_mode_j() UDC

    Features to test the USB HS device. These arerequested to run a USB certification.

    udd_test_mode_k() UDC

    udd_test_mode_se0_nak() UDC

    udd_test_mode_packet () UDC

    The global variable udd_g_ctrlreq is declared by UDD and contains two parts:• Values updated by UDD and used by UDC and UDIs (Table 4-9)• Values updated by UDC and UDIs and used by UDD (Table 4-10)

    Outside the UDD, this variable is processed by udc_process_setup() for UDC and *setup() forUDI.

    Table 4-9. udd_g_ctrlreq Field Updated by UDD

    Declaration Description

    usb_setup_req_t req Values included in SETUP packet and used to decode request.

    uint8_t *payloadThe content of the buffer is sent or filled by UDD.Can be NULL if u16_size is equal to 0.

    Table 4-10. udd_g_ctrlreq Updated by UDC or UDI

    Declaration Description

    uint8_t *payload Pointer value of the buffer to send or fill

    uint16_t u16_size Buffer size to send or fillIt can be 0 when no DATA phase is needed

    bool over_under_run(void) Called by UDD when the buffer given (.payload) is full or empty. Can beNULL

    void *callback(void) Called by UDD when the setup request is finished (setup+data+ZLP). Canbe NULL

    AN2681Application Programming Interface

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 10

  • 5. BehaviorThis UDC stack implementation is based on an interrupt driven scheme. This solution ensures lowlatency, does not require any wait loop, and ensures OS compatibility.

    Depending on the USB interrupt routine priority, the USB interrupt can be blocked by other interruptroutines with higher priority, or a critical code section. The USB hardware and software does not have anytiming requirements except for the USB “Set Address” request (performed during USB enumerationphase to assign the USB address). The user must take care that the USB interrupt is not blocked duringthe "Set Address" request for longer than the maximum delay given in the table below.

    Table 5-1. Set Address Timing

    USB Host Maximum Delay(1)

    Specification 2ms

    USB org certification tools 12ms

    Windows® XP 48ms

    Windows 7, Vista 32ms

    Mac Mini OSX 10.5.8 77ms

    Ubuntu 8.04, Ubuntu 9, Open Suse 11.1 29ms

    Fedora 9, Fedora 10 24ms

    Note:  (1) These numbers will depend on USB host hardware, and is only a ballpark number forreference. These numbers include the time for setup retry.

    USB hosts uses a timeout to reset a non-answering USB device (this time is not specified by the USBspecification). The table below lists examples of operating system's timeout:

    Table 5-2. OS Timeout

    USB host

    Timeout

    Control Endpoint Mass Storage

    Data Phase ZLP Phase CBW Data Read CSW

    Specification No timeout

    Windows XP 5.3s 5.3s 19s 9.3s 9.3s

    Windows 7, Vista 5.3s 5.3s 19s 160s/60s 160s/60s

    Mac Mini OSX 10.5.8 5.9s 5.6s 11s 31s 22s

    Ubuntu 8.04, Ubuntu 9, Open Suse 11.1 5s 5s 30s 30s 30s

    Fedora 9, Fedora 10 5s 5s 30s 60s 30s

    The following figures describe the interaction between the different layers.

    AN2681Behavior

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 11

  • Figure 5-1. USB Device Start-up and Stop

    App

    USB Drivers for AT32UC3 family

    udc_start()

    UDC

    USB Drivers for AT32UC3 family

    UDD

    USB Drivers for AT32UC3 family

    udd_enable()udc_attach()

    udd_attach()

    VBUShigh

    Enable Ep control (0)Setup (Ep0)

    Set D+/-pull-up

    Reset line

    udc_stop()udd_disable()

    Remove D+/-pull-up

    udc_start()udd_enable()

    udc_attach()udd_attach()

    VBUShigh

    Set D+/-pull-up

    udc_stop()udd_disable()

    Remove D+/-pull-up

    udc_vbus(true)UDC_VBUS_EVENT(true)

    VBUSlow

    udc_vbus(false)UDC_VBUS_EVENT(false)

    AN2681Behavior

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 12

  • Figure 5-2. Management of Control Endpoint

    UDI

    USB

    Drivers for AT32UC3 family

    UDC

    USB

    Drivers for AT32UC3 family

    UDD

    USB

    Drivers for AT32UC3 family

    Setup (Ep0)

    Decode setupSTALLit

    udc_process_setup()

    STALL (Ep0)

    Setup (Ep0)

    Decode setupACK it

    Prepare data transfer

    udc_process_setup()

    IN or OUT (Ep0)

    IN or OUT (Ep0)

    ZLP IN or OUT (Ep0)

    eof_request()

    Setup (Ep0)

    Decode setupACK it

    Prepare data transfer

    udc_process_setup()

    IN or OUT (Ep0)

    IN or OUT (Ep0)

    ZLP IN or OUT (Ep0)

    eof_request()

    req_over_under_run()Prepare new data transfer

    Setup (Ep0)

    Decode setupACK it

    Prepare data transfer

    udc_process_setup()

    IN or OUT (Ep0)

    ZLP IN or OUT (Ep0)

    eof_request()

    setup()

    Setup STALLED

    Setup ACKED by UDC with data transfer

    Setup ACKED by UDI with data transfer

    Setup ACKED by UDC with data transfer split

    Note:  The udd_g_ctrlreq variable is used to communicate between UDD and UDC/UDIs.

    AN2681Behavior

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 13

  • Figure 5-3. Typical Enumeration

    UDI-2

    USB

    UDI-1 UDC UDD

    Setup (Ep0)

    udc_process_setup()ZLP IN or OUT (Ep0)

    eof_request()

    udd_set_address()

    Setup (Ep0)

    udc_process_setup()

    SET_ADDRESS

    request

    SET_CONFIGURATION

    request

    enable()

    enable()ZLP IN or OUT (Ep0)

    udd_ep_alloc()

    udd_ep_alloc()

    AN2681Behavior

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 14

  • 6. ConfigurationThe configuration is divided into two sections: application and USB descriptors.

    The application’s configuration is defined in the conf_usb.h file. This file must be created for eachapplication. To create/edit this file only basic USB knowledge is required.

    The conf_usb.h file must define the following configuration:

    • USB Device Configuration• USB Interface Configuration• USB Driver Configuration

    The USB descriptor configuration is required when the default configuration provided by Microchip is notused. This configuration information requires more in-depth knowledge of USB.

    6.1 USB Configuration

    USB Device ConfigurationThe following configuration must be included in the conf_usb.h file of the application, which is the mainUSB device configuration.

    Table 6-1. USB Device Configuration

    Define name Type Description

    USB_DEVICE_VENDOR_ID Word Vendor ID provided by USB org (ATMEL 0x03EB)

    USB_DEVICE_PRODUCT_ID Word Product ID (referenced in usb_atmel.h)

    USB_DEVICE_MAJOR_VERSION Byte Major version of the device

    USB_DEVICE_MINOR_VERSION Byte Minor version of the device

    USB_DEVICE_MANUFACTURE_NAME(1) String(2) Static ASCII name for the manufacture

    USB_DEVICE_PRODUCT_NAME(1) String(2) Static ASCII name for the product

    USB_DEVICE_SERIAL_NAME(1) String(2) Static ASCII name to enable and set a serialnumber

    USB_DEVICE_GET_SERIAL_NAME_POINTER()(1)

    constuint8_t*function(void)

    Give a pointer on a dynamic ASCII name toenable and set a serial number.RequireUSB_DEVICE_GET_SERIAL_NAME_LENGTHand ignore USB_DEVICE_SERIAL_NAME.

    USB_DEVICE_GET_SERIAL_NAME_LENGTH()(1)

    uint8_tfunction(void)

    Give the length of dynamic ASCII name used toenable a serial number.

    USB_DEVICE_POWER Numeric Maximum device power (mA)

    USB_DEVICE_ATTR Byte USB attributes to add to enable feature:• USB_CONFIG_ATTR_SELF_POWERED

    AN2681Configuration

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 15

  • Define name Type Description

    • USB_CONFIG_ATTR_REMOTE_WAKEUP(3)

    USB_DEVICE_LOW_SPEED(1) Only defined Force the USB device to run in Low Speed

    USB_DEVICE_HS_SUPPORT(1) Only defined Authorize the USB device to run in High Speed

    USB_DEVICE_MAX_EP Byte Define the maximum endpoint number used bythe device (don’t include control endpoint)

    Note:  (1) Optional configuration. Comment the define statement to disable it (ex: // #defineUSB_DEVICE_X).

    Note:  (2) Examples of String syntax: #define USB_DEVICE_MANUFACTURE_NAME “ATMEL”. TheDefine can be omitted, thus the string is removed of USB enumeration.

    Note:  (3) If the remote wake feature is enabled, remote wake-up callbacks must be implemented.

    USB Interface ConfigurationThe UDI configurations are described in USB device class application notes.

    USB Drivers ConfigurationThe following configuration must be included in the conf_usb.h file of the application.

    The AVR products provide specific hardware features that can be enabled here.

    Table 6-2. USB Device Driver Configuration

    Define name Values UDD Description

    UDD_NO_SLEEP_MGR Only defined All Remove the management ofsleepmgr service

    UDD_ISOCHRONOUS_NB_BANK 1, 2, 3 AVR32 -USBB

    Reduces or increasesisochronous endpointbuffering. Default value: 2

    UDD_BULK_NB_BANK 1, 2, 3 AVR32 -USBB

    Reduces or increases bulkendpoint buffering.Default value: 2

    UDD_INTERRUPT_NB_BANK 1, 2, 3 AVR32 -USBB

    Reduces or increases interruptendpoint buffering.Default value: 1

    UDD_USB_INT_LEVEL 0 to 3 AVR32 -USBBAVR32 -USBC

    Sets the USB interrupt level onAVR32 core.Default value: 0(recommended)

    UDD_USB_INT_LEVEL USB_INTLVL_LO_gcUSB_INTLVL_...

    XMEGA -USB

    Sets the USB interrupt level onXmega core.

    AN2681Configuration

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 16

  • Define name Values UDD Description

    Default value:USB_INTLVL_LO_gc(recommended)

    6.2 USB DescriptorsThis section is oriented to USB developers who want to create a new UDI or a composite USB device.

    The USB classes that are already provided by ASF include default USB Device descriptors. Thesedescriptors are defined in the UDI files udi_foo_desc.c and udi_foo_conf.h, and allow an easyimplementation described in all UDI application notes.

    The descriptor file declares the global variable app_udc_config that includes:• A device descriptor for each speed possible (usb_dev_desc_t)• One device qualifier in case of High Speed device (usb_dev_qual_desc_t)• A configuration descriptor for each configuration (usb_conf_desc_x_t)• A link between UDI and configuration descriptor (udc_iface_fcnt_t)

    AN2681Configuration

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 17

  • Figure 6-1. USB Descriptors

    Device Descriptor

    *usb_dev_desc_t *udc_config_speed_t

    *usb_dev_desc_t*usb_dev_qual_desc_t*udc_config_speed_t

    *usb_dev_qual_desc_t

    *udc_config_speed_t

    udc_config_tapp_udc_config

    *usb_conf_desc_t**udc_iface_fcnt_t

    udc_config_speed_t

    Device Qualifier Descriptor

    DeviceConfigurationDescriptor

    *enable()*disable()*setup()*getsetting()

    udc_iface_fcnt_t

    From USB2.0

    High Speed descriptors

    DeviceConfigurationDescriptor

    *enable()*disable()*setup()*getsetting()

    udc_iface_fcnt_t

    *usb_conf_desc_t**udc_iface_fcnt_t

    udc_config_speed_t

    ...ASF struct

    Legend:

    Device Descriptor

    Low or Full Speed descriptors(Always mandatory)

    AN2681Configuration

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 18

  • 7. Power ConsumptionThe different Power or Sleep modes available on AVR products is supported by the USB hardwareaccording to USB line state. The USB drivers use the sleepmgr service to manage these Power or Sleepmodes. Any USB application must include the sleepmgr service, and the sleepmgr must be initialized bycalling sleepmgr_init().

    7.1 UC3 USBB and USBC Sleep ModesAll UC3 Sleep modes are described in the Power Manager chapter in the specific UC3 data sheet. Sleepmodes supported by USBB and USBC drivers are:

    • USB IDLE state: the driver requires the USB clock, hence IDLE Sleep mode is the lowestsupported Sleep mode.

    • USB SUSPEND state: the driver does not require the USB clock, but will request the clock on USBwake-up. Hence, STATIC and STANDBY is supported.

    • VBUS monitoring: used in USB Self-Power mode, allows the UC3 to go down to STOP mode.

    Table 7-1. Sleep Modes Supported in USB SUSPEND State on UC3

    USB Power Mode USB Speed Mode USB Clock Start-up(1) Sleep Mode Authorized

    Bus and self-powered LS, FS >10ms STANDBY

    Bus and self-powered HS >3ms STANDBY

    Self-powered LS, FS

  • 7.2 XMEGA Sleep ModesAll XMEGA Sleep modes are described in the XMEGA data sheet chapter Power Management and SleepModes. The Sleep modes supported by the USB stack are:

    • USB IDLE state: the driver requires the USB clock; hence IDLE Sleep mode is the lowestsupported Sleep mode.

    • USB SUSPEND state: the driver does not require the USB clock, but will request the clock on USBwake-up. Hence, POWER DOWN and STANDBY is supported.

    Table 7-2. Sleep Modes Supported in USB SUSPEND State on XMEGA

    USB Power Mode USB Speed Mode USB Clock Start-up (1) Sleep Mode Authorized

    Bus and self-powered LS, FS >10ms STANDBY

    Bus and self-powered LS, FS

  • 8. Revision HistoryDoc. Rev. Date Comments

    A 04/2018 This document, Microchip DS00002681, replaces Atmel document 8360E-08/13.Updates include: the template, language, typos, references to the latest release ofAtmel Studio, the list of relevant examples and app notes, as well as updatedreferenced names and versions to point to the latest items.

    8360E 08/2013 • Updated UDC API2.• Added clarification about callbacks.

    8360D XX/XX NOTE: Non-released version.• Add missing ATxmega product information (§4.3.2, Table 7-2).

    8360C 08/2012 • Add new option to implement a dynamic serial number in Table 7-1.• Add ATxmega product information.• In features list, fix the High Speed 48Mbit/s by 480Mbit/s.

    8360B 04/2011 • Updated all section concerning Power consumption.• Updated UDI and UDD APIs. See “sof_notify()” and

    udd_get_microframe_number() description.

    8360A 12/2010 Initial revision

    AN2681Revision History

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 21

  • The Microchip Web Site

    Microchip provides online support via our web site at http://www.microchip.com/. This web site is used asa means to make files and information easily available to customers. Accessible by using your favoriteInternet browser, the web site contains the following information:

    • Product Support – Data sheets and errata, application notes and sample programs, designresources, user’s guides and hardware support documents, latest software releases and archivedsoftware

    • General Technical Support – Frequently Asked Questions (FAQ), technical support requests,online discussion groups, Microchip consultant program member listing

    • Business of Microchip – Product selector and ordering guides, latest Microchip press releases,listing of seminars and events, listings of Microchip sales offices, distributors and factoryrepresentatives

    Customer Change Notification Service

    Microchip’s customer notification service helps keep customers current on Microchip products.Subscribers will receive e-mail notification whenever there are changes, updates, revisions or erratarelated to a specified product family or development tool of interest.

    To register, access the Microchip web site at http://www.microchip.com/. Under “Support”, click on“Customer Change Notification” and follow the registration instructions.

    Customer Support

    Users of Microchip products can receive assistance through several channels:

    • Distributor or Representative• Local Sales Office• Field Application Engineer (FAE)• Technical Support

    Customers should contact their distributor, representative or Field Application Engineer (FAE) for support.Local sales offices are also available to help customers. A listing of sales offices and locations is includedin the back of this document.

    Technical support is available through the web site at: http://www.microchip.com/support

    Microchip Devices Code Protection Feature

    Note the following details of the code protection feature on Microchip devices:

    • Microchip products meet the specification contained in their particular Microchip Data Sheet.• Microchip believes that its family of products is one of the most secure families of its kind on the

    market today, when used in the intended manner and under normal conditions.• There are dishonest and possibly illegal methods used to breach the code protection feature. All of

    these methods, to our knowledge, require using the Microchip products in a manner outside theoperating specifications contained in Microchip’s Data Sheets. Most likely, the person doing so isengaged in theft of intellectual property.

    • Microchip is willing to work with the customer who is concerned about the integrity of their code.

    AN2681

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 22

    http://www.microchip.com/http://www.microchip.com/http://www.microchip.com/support

  • • Neither Microchip nor any other semiconductor manufacturer can guarantee the security of theircode. Code protection does not mean that we are guaranteeing the product as “unbreakable.”

    Code protection is constantly evolving. We at Microchip are committed to continuously improving thecode protection features of our products. Attempts to break Microchip’s code protection feature may be aviolation of the Digital Millennium Copyright Act. If such acts allow unauthorized access to your softwareor other copyrighted work, you may have a right to sue for relief under that Act.

    Legal Notice

    Information contained in this publication regarding device applications and the like is provided only foryour convenience and may be superseded by updates. It is your responsibility to ensure that yourapplication meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS ORWARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORYOR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITSCONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE.Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in lifesupport and/or safety applications is entirely at the buyer’s risk, and the buyer agrees to defend,indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resultingfrom such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectualproperty rights unless otherwise stated.

    Trademarks

    The Microchip name and logo, the Microchip logo, AnyRate, AVR, AVR logo, AVR Freaks, BeaconThings,BitCloud, CryptoMemory, CryptoRF, dsPIC, FlashFlex, flexPWR, Heldo, JukeBlox, KeeLoq, KeeLoq logo,Kleer, LANCheck, LINK MD, maXStylus, maXTouch, MediaLB, megaAVR, MOST, MOST logo, MPLAB,OptoLyzer, PIC, picoPower, PICSTART, PIC32 logo, Prochip Designer, QTouch, RightTouch, SAM-BA,SpyNIC, SST, SST Logo, SuperFlash, tinyAVR, UNI/O, and XMEGA are registered trademarks ofMicrochip Technology Incorporated in the U.S.A. and other countries.

    ClockWorks, The Embedded Control Solutions Company, EtherSynch, Hyper Speed Control, HyperLightLoad, IntelliMOS, mTouch, Precision Edge, and Quiet-Wire are registered trademarks of MicrochipTechnology Incorporated in the U.S.A.

    Adjacent Key Suppression, AKS, Analog-for-the-Digital Age, Any Capacitor, AnyIn, AnyOut, BodyCom,chipKIT, chipKIT logo, CodeGuard, CryptoAuthentication, CryptoCompanion, CryptoController,dsPICDEM, dsPICDEM.net, Dynamic Average Matching, DAM, ECAN, EtherGREEN, In-Circuit SerialProgramming, ICSP, Inter-Chip Connectivity, JitterBlocker, KleerNet, KleerNet logo, Mindi, MiWi,motorBench, MPASM, MPF, MPLAB Certified logo, MPLIB, MPLINK, MultiTRAK, NetDetach, OmniscientCode Generation, PICDEM, PICDEM.net, PICkit, PICtail, PureSilicon, QMatrix, RightTouch logo, REALICE, Ripple Blocker, SAM-ICE, Serial Quad I/O, SMART-I.S., SQI, SuperSwitcher, SuperSwitcher II, TotalEndurance, TSHARC, USBCheck, VariSense, ViewSpan, WiperLock, Wireless DNA, and ZENA aretrademarks 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.

    Silicon Storage Technology is a registered trademark of Microchip Technology Inc. in other countries.

    GestIC is a registered trademark of Microchip Technology Germany II GmbH & Co. KG, a subsidiary ofMicrochip Technology Inc., in other countries.

    All other trademarks mentioned herein are property of their respective companies.

    AN2681

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 23

  • © 2018, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved.

    ISBN: 978-1-5224-2869-5

    Quality Management System Certified by DNV

    ISO/TS 16949Microchip received ISO/TS-16949:2009 certification for its worldwide headquarters, design and waferfabrication facilities in Chandler and Tempe, Arizona; Gresham, Oregon and design centers in Californiaand India. The Company’s quality system processes and procedures are for its PIC® MCUs and dsPIC®

    DSCs, KEELOQ® code hopping devices, Serial EEPROMs, microperipherals, nonvolatile memory andanalog products. In addition, Microchip’s quality system for the design and manufacture of developmentsystems is ISO 9001:2000 certified.

    AN2681

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 24

  • AMERICAS ASIA/PACIFIC ASIA/PACIFIC EUROPECorporate Office2355 West Chandler Blvd.Chandler, AZ 85224-6199Tel: 480-792-7200Fax: 480-792-7277Technical Support:http://www.microchip.com/supportWeb Address:www.microchip.comAtlantaDuluth, GATel: 678-957-9614Fax: 678-957-1455Austin, TXTel: 512-257-3370BostonWestborough, MATel: 774-760-0087Fax: 774-760-0088ChicagoItasca, ILTel: 630-285-0071Fax: 630-285-0075DallasAddison, TXTel: 972-818-7423Fax: 972-818-2924DetroitNovi, MITel: 248-848-4000Houston, TXTel: 281-894-5983IndianapolisNoblesville, INTel: 317-773-8323Fax: 317-773-5453Tel: 317-536-2380Los AngelesMission Viejo, CATel: 949-462-9523Fax: 949-462-9608Tel: 951-273-7800Raleigh, NCTel: 919-844-7510New York, NYTel: 631-435-6000San Jose, CATel: 408-735-9110Tel: 408-436-4270Canada - TorontoTel: 905-695-1980Fax: 905-695-2078

    Australia - SydneyTel: 61-2-9868-6733China - BeijingTel: 86-10-8569-7000China - ChengduTel: 86-28-8665-5511China - ChongqingTel: 86-23-8980-9588China - DongguanTel: 86-769-8702-9880China - GuangzhouTel: 86-20-8755-8029China - HangzhouTel: 86-571-8792-8115China - Hong Kong SARTel: 852-2943-5100China - NanjingTel: 86-25-8473-2460China - QingdaoTel: 86-532-8502-7355China - ShanghaiTel: 86-21-3326-8000China - ShenyangTel: 86-24-2334-2829China - ShenzhenTel: 86-755-8864-2200China - SuzhouTel: 86-186-6233-1526China - WuhanTel: 86-27-5980-5300China - XianTel: 86-29-8833-7252China - XiamenTel: 86-592-2388138China - ZhuhaiTel: 86-756-3210040

    India - BangaloreTel: 91-80-3090-4444India - New DelhiTel: 91-11-4160-8631India - PuneTel: 91-20-4121-0141Japan - OsakaTel: 81-6-6152-7160Japan - TokyoTel: 81-3-6880- 3770Korea - DaeguTel: 82-53-744-4301Korea - SeoulTel: 82-2-554-7200Malaysia - Kuala LumpurTel: 60-3-7651-7906Malaysia - PenangTel: 60-4-227-8870Philippines - ManilaTel: 63-2-634-9065SingaporeTel: 65-6334-8870Taiwan - Hsin ChuTel: 886-3-577-8366Taiwan - KaohsiungTel: 886-7-213-7830Taiwan - TaipeiTel: 886-2-2508-8600Thailand - BangkokTel: 66-2-694-1351Vietnam - Ho Chi MinhTel: 84-28-5448-2100

    Austria - WelsTel: 43-7242-2244-39Fax: 43-7242-2244-393Denmark - CopenhagenTel: 45-4450-2828Fax: 45-4485-2829Finland - EspooTel: 358-9-4520-820France - ParisTel: 33-1-69-53-63-20Fax: 33-1-69-30-90-79Germany - GarchingTel: 49-8931-9700Germany - HaanTel: 49-2129-3766400Germany - HeilbronnTel: 49-7131-67-3636Germany - KarlsruheTel: 49-721-625370Germany - MunichTel: 49-89-627-144-0Fax: 49-89-627-144-44Germany - RosenheimTel: 49-8031-354-560Israel - Ra’ananaTel: 972-9-744-7705Italy - MilanTel: 39-0331-742611Fax: 39-0331-466781Italy - PadovaTel: 39-049-7625286Netherlands - DrunenTel: 31-416-690399Fax: 31-416-690340Norway - TrondheimTel: 47-7289-7561Poland - WarsawTel: 48-22-3325737Romania - BucharestTel: 40-21-407-87-50Spain - MadridTel: 34-91-708-08-90Fax: 34-91-708-08-91Sweden - GothenbergTel: 46-31-704-60-40Sweden - StockholmTel: 46-8-5090-4654UK - WokinghamTel: 44-118-921-5800Fax: 44-118-921-5820

    Worldwide Sales and Service

    © 2018 Microchip Technology Inc. Draft Application Note DS00002681A-page 25

    FeaturesIntroductionTable of Contents1. Abbreviations2. USB Device Application Notes3. Organization3.1. Overview3.2. Memory Footprint3.3. USB Device Stack Files

    4. Application Programming Interface4.1. External API from UDC4.2. Internal APIs

    5. Behavior6. Configuration6.1. USB Configuration6.2. USB Descriptors

    7. Power Consumption7.1. UC3 USBB and USBC Sleep Modes7.2. XMEGA Sleep Modes

    8. Revision HistoryThe Microchip Web SiteCustomer Change Notification ServiceCustomer SupportMicrochip Devices Code Protection FeatureLegal NoticeTrademarksQuality Management System Certified by DNVWorldwide Sales and Service