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.
- 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:
}
Event_VBUS_present()
{
}
- 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()
{
}
Event Key press()
{
}
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:
For SAM D21 devices, add to the initialization code:
Add to the main IDLE loop:
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:
USB Device Controller (UDC) - Workflow
Common workflow for all USB devices.
- Ensure that conf_usb.h is available and contains the following configuration, which is the main USB device configuration:
#define USB_DEVICE_VENDOR_ID 0x03EB // Type Word
#define USB_DEVICE_PRODUCT_ID 0xXXXX // Type Word
#define USB_DEVICE_MAJOR_VERSION 1 // Type Byte
#define USB_DEVICE_MINOR_VERSION 0 // Type Byte
#define USB_DEVICE_POWER 100 // Type 9-bits
#define USB_DEVICE_ATTR USB_CONFIG_ATTR_BUS_POWERED // Flags
- 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:
#define CONFIG_USBCLK_SOURCE USBCLK_SRC_RCOSC
#define CONFIG_OSC_RC32_CAL 48000000UL
#define CONFIG_OSC_AUTOCAL_RC32MHZ_REF_OSC OSC_ID_USBSOF
#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):
#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):
#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):
#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)
#define CONFIG_SYSCLK_SOURCE SYSCLK_SRC_PLL1
Content of conf_clock.h for SAM3S, SAM3SD, and SAM4S devices (UPD: USB Peripheral Device):
#define CONFIG_PLL1_SOURCE PLL_SRC_MAINCK_XTAL
#define CONFIG_PLL1_MUL 16
#define CONFIG_PLL1_DIV 2
#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):
Content of conf_clock.h for SAM3X and SAM3A devices (UOTGHS: USB OTG High Speed):
#define CONFIG_USBCLK_SOURCE USBCLK_SRC_UPLL
#define CONFIG_USBCLK_DIV 1
Content of conf_clocks.h for SAM D21 devices (USB):
# define CONF_CLOCK_FLASH_WAIT_STATES 2
# 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
# define CONF_CLOCK_CONFIGURE_GCLK true
# 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