Microchip® Advanced Software Framework

Dual roles support

In this use case, the USB host and USB device are enabled, it is the dual role.

Note: On the Atmel boards, the switch of USB role is managed automatically by the USB stack thank to a USB OTG connector and its USB ID pin. For a dual role management without OTG connector, please refer to "AVR4950 section 6.1 Dual roles".

Setup steps

Prior to implement this use case, be sure to have already apply the UDI module "basic use case".

Usage steps

Example code

Content of conf_usb_host.h:

#define UHC_MODE_CHANGE(b_host_mode) my_callback_mode_change(b_host_mode)
extern void my_callback_mode_change(bool b_host_mode);

Add to application C-file:

void usb_init(void)
{
//udc_start();
}
bool my_host_mode;
void my_callback_mode_change(bool b_host_mode)
{
my_host_mode = b_host_mode;
}
void my_usb_task(void)
{
if (my_host_mode) {
// CALL USB Host task
} else {
// CALL USB Device task
}
}

Workflow

  1. In case of USB dual roles (Device and Host), the USB stack must be enabled by uhc_start() and the udc_start() must not be called.
  2. In dual role, to known the current USB mode, the callback to notify the mode changes can be used.
    • Ensure that conf_usb_host.h contains the following parameters.
      #define UHC_MODE_CHANGE(b_host_mode) my_callback_mode_change(b_host_mode)
      extern void my_callback_mode_change(bool b_host_mode);
    • Ensure that application contains the following code:
      bool my_host_mode;
      void my_callback_mode_change(bool b_host_mode)
      {
      my_host_mode = b_host_mode;
      }
      void my_usb_task(void)
      {
      if (my_host_mode) {
      // CALL USB Host task
      } else {
      // CALL USB Device task
      }
      }