Microchip® Advanced Software Framework

USB Device Basic Setup

Custom Configuration

The following USB Device configuration must be included in the conf_usb.h file of the application:

1. USB_DEVICE_VENDOR_ID (Word).

Vendor ID provided by USB org (Atmel 0x03EB).

2. USB_DEVICE_PRODUCT_ID (Word).

Product ID (Referenced in usb_atmel.h).

3. USB_DEVICE_MAJOR_VERSION (Byte).

Major version of the device.

4. USB_DEVICE_MINOR_VERSION (Byte).

Minor version of the device.

5. USB_DEVICE_MANUFACTURE_NAME (string).

ASCII name for the manufacture.

6. USB_DEVICE_PRODUCT_NAME (string).

ASCII name for the product.

7. USB_DEVICE_SERIAL_NAME (string).

ASCII name to enable and set a serial number.

8. USB_DEVICE_POWER (Numeric).

(unit mA) Maximum device power.

9. USB_DEVICE_ATTR (Byte).

USB attributes available:

  • USB_CONFIG_ATTR_SELF_POWERED
  • USB_CONFIG_ATTR_REMOTE_WAKEUP
Note
If remote wake is enabled, this defines remotewakeup callbacks.

10. USB_DEVICE_LOW_SPEED (Only defined).

Force the USB Device to run in low speed.

11. USB_DEVICE_HS_SUPPORT (Only defined).

Authorize the USB Device to run in high speed.

12. USB_DEVICE_MAX_EP (Byte).

Define the maximum endpoint number used by the USB Device.

This one is already defined in the UDI default configuration. E.g.:

  • When endpoint control 0x00, endpoint 0x01, and endpoint 0x82 is used, then USB_DEVICE_MAX_EP=2
  • When only endpoint control 0x00 is used, then USB_DEVICE_MAX_EP=0
  • When endpoint 0x01 and endpoint 0x81 is used, then USB_DEVICE_MAX_EP=1 (configuration not possible on USBB interface)

VBUS Monitoring

The VBUS monitoring is used only for USB SELF Power application.

  • By default the USB device is automatically attached when VBUS is high or when USB starts for devices without internal VBUS monitoring. conf_usb.h file does not contain definition USB_DEVICE_ATTACH_AUTO_DISABLE.
    //#define USB_DEVICE_ATTACH_AUTO_DISABLE
  • Add custom VBUS monitoring. conf_usb.h file contains define USB_DEVICE_ATTACH_AUTO_DISABLE:
    #define USB_DEVICE_ATTACH_AUTO_DISABLE
    User C-file contains:
    // Authorize VBUS monitoring
    // Implement custom VBUS monitoring via GPIO or other
    }
    Event_VBUS_present() // VBUS interrupt or GPIO interrupt or other
    {
    // Attach USB Device
    }
  • Case of battery charging. conf_usb.h file contains define USB_DEVICE_ATTACH_AUTO_DISABLE:
    #define USB_DEVICE_ATTACH_AUTO_DISABLE
    User C-file contains:
    Event VBUS present() // VBUS interrupt or GPIO interrupt or ..
    {
    // Authorize battery charging, but wait key press to start USB.
    }
    Event Key press()
    {
    // Stop batteries charging
    // Start USB
    }

USB Device Basic Setup

USB Device Controller (UDC) - Prerequisites

Common prerequisites for all USB devices.

This module is based on USB device stack full interrupt driven, and supporting sleepmgr. For AVR® and Atmel® | SMART ARM®-based SAM3/4 devices the clock services is supported. For SAM D21 devices the clock driver is supported.

The following procedure must be executed to set up the project correctly:

  • Specify the clock configuration:
    • XMEGA® USB devices need 48MHz clock input. XMEGA USB devices need CPU frequency higher than 12MHz. You can use either an internal RC 48MHz auto calibrated by Start of Frames or an external OSC.
    • UC3 and SAM3/4 devices without USB high speed support need 48MHz clock input. You must use a PLL and an external OSC.
    • UC3 and SAM3/4 devices with USB high speed support need 12MHz clock input. You must use an external OSC.
    • UC3 devices with USBC hardware need CPU frequency higher than 25MHz
    • SAM D21 devices without USB high speed support need 48MHz clock input. You should use DFLL with USBCRM.
  • In conf_board.h, the define CONF_BOARD_USB_PORT must be added to enable USB lines. (Not mandatory for all boards).
  • Enable interrupts
  • Initialize the clock service

The usage of sleep manager service is optional, but recommended to reduce power consumption:

  • Initialize the sleep manager service
  • Activate sleep mode when the application is in IDLE state

For AVR and SAM3/4 devices, add to the initialization code:

sysclk_init();
sleepmgr_init(); // Optional

For SAM D21 devices, add to the initialization code:

Add to the main IDLE loop:

sleepmgr_enter_sleep(); // Optional

USB Device Controller (UDC) - Example Code

Common example code for all USB devices.

Content of conf_usb.h:

#define USB_DEVICE_VENDOR_ID 0x03EB
#define USB_DEVICE_PRODUCT_ID 0xXXXX
#define USB_DEVICE_MAJOR_VERSION 1
#define USB_DEVICE_MINOR_VERSION 0
#define USB_DEVICE_POWER 100
#define USB_DEVICE_ATTR USB_CONFIG_ATTR_BUS_POWERED

Add to application C-file:

void usb_init(void)
{
}

USB Device Controller (UDC) - Workflow

Common workflow for all USB devices.

  1. Ensure that conf_usb.h is available and contains the following configuration, which is the main USB device configuration:
    // Vendor ID provided by USB org (Atmel 0x03EB)
    #define USB_DEVICE_VENDOR_ID 0x03EB // Type Word
    // Product ID (Atmel PID referenced in usb_atmel.h)
    #define USB_DEVICE_PRODUCT_ID 0xXXXX // Type Word
    // Major version of the device
    #define USB_DEVICE_MAJOR_VERSION 1 // Type Byte
    // Minor version of the device
    #define USB_DEVICE_MINOR_VERSION 0 // Type Byte
    // Maximum device power (mA)
    #define USB_DEVICE_POWER 100 // Type 9-bits
    // USB attributes to enable features
    #define USB_DEVICE_ATTR USB_CONFIG_ATTR_BUS_POWERED // Flags
  2. Call the USB device stack start function to enable stack and start USB:
    \note In case of USB dual roles (Device and Host) managed through USB OTG connector
    
    (USB ID pin), the call of udc_start() must be removed and replaced by uhc_start(). Refer to section "Dual roles" for further information in the application note: Atmel AVR4950: ASF - USB Host Stack

conf_clock.h examples with USB support

Content of XMEGA conf_clock.h:

// Configuration based on internal RC:
// USB clock need of 48MHz
#define CONFIG_USBCLK_SOURCE USBCLK_SRC_RCOSC
#define CONFIG_OSC_RC32_CAL 48000000UL
#define CONFIG_OSC_AUTOCAL_RC32MHZ_REF_OSC OSC_ID_USBSOF
// CPU clock need of clock > 12MHz to run with USB (Here 24MHz)
#define CONFIG_SYSCLK_SOURCE SYSCLK_SRC_RC32MHZ
#define CONFIG_SYSCLK_PSADIV SYSCLK_PSADIV_2
#define CONFIG_SYSCLK_PSBCDIV SYSCLK_PSBCDIV_1_1

Content of conf_clock.h for AT32UC3A0, AT32UC3A1, and AT32UC3B devices (USBB):

// Configuration based on 12MHz external OSC:
#define CONFIG_PLL1_SOURCE PLL_SRC_OSC0
#define CONFIG_PLL1_MUL 8
#define CONFIG_PLL1_DIV 2
#define CONFIG_USBCLK_SOURCE USBCLK_SRC_PLL1
#define CONFIG_USBCLK_DIV 1 // Fusb = Fsys/(2 ^ USB_div)

Content of conf_clock.h for AT32UC3A3 and AT32UC3A4 devices (USBB with high speed support):

// Configuration based on 12MHz external OSC:
#define CONFIG_USBCLK_SOURCE USBCLK_SRC_OSC0
#define CONFIG_USBCLK_DIV 1 // Fusb = Fsys/(2 ^ USB_div)

Content of conf_clock.h for AT32UC3C, ATUCXXD, ATUCXXL3U, and ATUCXXL4U devices (USBC):

// Configuration based on 12MHz external OSC:
#define CONFIG_PLL1_SOURCE PLL_SRC_OSC0
#define CONFIG_PLL1_MUL 8
#define CONFIG_PLL1_DIV 2
#define CONFIG_USBCLK_SOURCE USBCLK_SRC_PLL1
#define CONFIG_USBCLK_DIV 1 // Fusb = Fsys/(2 ^ USB_div)
// CPU clock need of clock > 25MHz to run with USBC
#define CONFIG_SYSCLK_SOURCE SYSCLK_SRC_PLL1

Content of conf_clock.h for SAM3S, SAM3SD, and SAM4S devices (UPD: USB Peripheral Device):

// PLL1 (B) Options (Fpll = (Fclk * PLL_mul) / PLL_div)
#define CONFIG_PLL1_SOURCE PLL_SRC_MAINCK_XTAL
#define CONFIG_PLL1_MUL 16
#define CONFIG_PLL1_DIV 2
// USB Clock Source Options (Fusb = FpllX / USB_div)
#define CONFIG_USBCLK_SOURCE USBCLK_SRC_PLL1
#define CONFIG_USBCLK_DIV 2

Content of conf_clock.h for SAM3U device (UPDHS: USB Peripheral Device High Speed):

// USB Clock Source fixed at UPLL.

Content of conf_clock.h for SAM3X and SAM3A devices (UOTGHS: USB OTG High Speed):

// USB Clock Source fixed at UPLL.
#define CONFIG_USBCLK_SOURCE USBCLK_SRC_UPLL
#define CONFIG_USBCLK_DIV 1

Content of conf_clocks.h for SAM D21 devices (USB):

// System clock bus configuration
# define CONF_CLOCK_FLASH_WAIT_STATES 2
// USB Clock Source fixed at DFLL.
// SYSTEM_CLOCK_SOURCE_DFLL configuration - Digital Frequency Locked Loop
# define CONF_CLOCK_DFLL_ENABLE true
# define CONF_CLOCK_DFLL_LOOP_MODE SYSTEM_CLOCK_DFLL_LOOP_MODE_USB_RECOVERY
# define CONF_CLOCK_DFLL_ON_DEMAND true
// Set this to true to configure the GCLK when running clocks_init.
// If set to false, none of the GCLK generators will be configured in clocks_init().
# define CONF_CLOCK_CONFIGURE_GCLK true
// Configure GCLK generator 0 (Main Clock)
# define CONF_CLOCK_GCLK_0_ENABLE true
# define CONF_CLOCK_GCLK_0_RUN_IN_STANDBY true
# define CONF_CLOCK_GCLK_0_CLOCK_SOURCE SYSTEM_CLOCK_SOURCE_DFLL
# define CONF_CLOCK_GCLK_0_PRESCALER 1
# define CONF_CLOCK_GCLK_0_OUTPUT_ENABLE false