Microchip® Advanced Software Framework

Quick start guide for USB device Mass Storage module (UDI MSC)

This is the quick start guide for the USB device interface MSC module (UDI MSC) with step-by-step instructions on how to configure and use the modules in a selection of use cases.

The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.

Basic use case

In this basic use case, the "USB MSC (Single Interface Device)" module is used. The "USB MSC (Composite Device)" module usage is described in Advanced use cases.

Setup steps

Prerequisites

This module is based on USB device stack full interrupt driven, and supporting Sleep manager sleepmgr. For AVR and SAM3/4 devices the Clock Management clock services is supported. For SAMD devices the asfdoc_sam0_system_clock_group clock driver is supported.

The following procedure must be executed to setup 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 RC48MHz 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.
    • SAMD 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 sleepmgr service is optional, but recommended to reduce power consumption:

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

conf_clock.h examples with USB support.

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

For SAMD devices, add to the initialization code:

system_init();
sleepmgr_init(); // Optional

Add to the main IDLE loop:

sleepmgr_enter_sleep(); // Optional

Example code

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)
{
}

Workflow

  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
  1. 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(). SeRefer to "AVR4950 section 6.1 Dual roles" for further information about dual roles.

Usage steps

Example code

Content of conf_usb.h:

#define USB_DEVICE_SERIAL_NAME "12...EF" // Disk SN for MSC
#define UDI_MSC_GLOBAL_VENDOR_ID \
'A', 'T', 'M', 'E', 'L', ' ', ' ', ' '
#define UDI_MSC_GLOBAL_PRODUCT_VERSION \
'1', '.', '0', '0'
#define UDI_MSC_ENABLE_EXT() my_callback_msc_enable()
extern bool my_callback_msc_enable(void);
#define UDI_MSC_DISABLE_EXT() my_callback_msc_disable()
extern void my_callback_msc_disable(void);
#include "udi_msc_conf.h" // At the end of conf_usb.h file

Add to application C-file:

static bool my_flag_autorize_msc_transfert = false;
bool my_callback_msc_enable(void)
{
my_flag_autorize_msc_transfert = true;
return true;
}
void my_callback_msc_disable(void)
{
my_flag_autorize_msc_transfert = false;
}
void task(void)
{
}

Workflow

  1. Ensure that conf_usb.h is available and contains the following configuration, which is the USB device MSC configuration:
    • #define USB_DEVICE_SERIAL_NAME "12...EF" // Disk SN for MSC
      Note
      The USB serial number is mandatory when a MSC interface is used.
    • #define UDI_MSC_GLOBAL_VENDOR_ID \
      'A', 'T', 'M', 'E', 'L', ' ', ' ', ' '
      #define UDI_MSC_GLOBAL_PRODUCT_VERSION \
      '1', '.', '0', '0'
      Note
      The USB MSC interface requires a vendor ID (8 ASCII characters) and a product version (4 ASCII characters).
    • #define UDI_MSC_ENABLE_EXT() my_callback_msc_enable()
      extern bool my_callback_msc_enable(void);
      Note
      After the device enumeration (detecting and identifying USB devices), the USB host starts the device configuration. When the USB MSC interface from the device is accepted by the host, the USB host enables this interface and the UDI_MSC_ENABLE_EXT() callback function is called and return true. Thus, when this event is received, the tasks which call udi_msc_process_trans() must be enabled.
    • #define UDI_MSC_DISABLE_EXT() my_callback_msc_disable()
      extern void my_callback_msc_disable(void);
      Note
      When the USB device is unplugged or is reset by the USB host, the USB interface is disabled and the UDI_MSC_DISABLE_EXT() callback function is called. Thus, it is recommended to disable the task which is called udi_msc_process_trans().
  2. The MSC is automatically linked with memory control access component which provides the memories interfaces. However, the memory data transfers must be done outside USB interrupt routine. This is done in the MSC process ("udi_msc_process_trans()") called by main loop:
  3. The MSC speed depends on task periodicity. To get the best speed the notification callback "UDI_MSC_NOTIFY_TRANS_EXT" can be used to wakeup this task (Example, through a mutex):
    • #define UDI_MSC_NOTIFY_TRANS_EXT() msc_notify_trans()
      void msc_notify_trans(void) {
      wakeup_my_task();
      }

Advanced use cases

For more advanced use of the UDI MSC module, see the following use cases: